ipa_nat_drvi.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /*
  2. * Copyright (c) 2013-2020 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. #ifndef IPA_NAT_DRVI_H
  30. #define IPA_NAT_DRVI_H
  31. #include "ipa_table.h"
  32. #include "ipa_mem_descriptor.h"
  33. #include "ipa_nat_utils.h"
  34. #undef MAKE_TBL_HDL
  35. #define MAKE_TBL_HDL(hdl, mt) \
  36. ((mt) << 31 | (hdl))
  37. #undef BREAK_TBL_HDL
  38. #define BREAK_TBL_HDL(hdl_in, mt, hdl_out) \
  39. do { \
  40. mt = (hdl_in) >> 31 & 0x0000000001; \
  41. hdl_out = (hdl_in) & 0x00000000FF; \
  42. } while ( 0 )
  43. #undef VALID_TBL_HDL
  44. #define VALID_TBL_HDL(h) \
  45. (((h) & 0x00000000FF) == IPA_NAT_MAX_IP4_TBLS)
  46. /*======= IMPLEMENTATION related data structures and functions ======= */
  47. #define IPA_NAT_MAX_IP4_TBLS 1
  48. #define IPA_NAT_RULE_FLAG_FIELD_OFFSET 18
  49. #define IPA_NAT_RULE_NEXT_FIELD_OFFSET 8
  50. #define IPA_NAT_RULE_PROTO_FIELD_OFFSET 22
  51. #define IPA_NAT_INDEX_RULE_NEXT_FIELD_OFFSET 2
  52. #define IPA_NAT_INDEX_RULE_NAT_INDEX_FIELD_OFFSET 0
  53. #define IPA_NAT_FLAG_ENABLE_BIT 1
  54. #define IPA_NAT_INVALID_PROTO_FIELD_VALUE 0xFF00
  55. /*
  56. * IPA_NAT_INVALID_PROTO_FIELD_VALUE above is what's passed to the IPA
  57. * in a DMA command. It is written into the NAT's rule, by the
  58. * IPA. After being written, It minifests in the rule in the form
  59. * below, hence it will be used when perusing the "struct
  60. * ipa_nat_rule".
  61. */
  62. #define IPA_NAT_INVALID_PROTO_FIELD_VALUE_IN_RULE 0xFF
  63. typedef enum {
  64. IPA_NAT_TABLE_FLAGS,
  65. IPA_NAT_TABLE_NEXT_INDEX,
  66. IPA_NAT_TABLE_PROTOCOL,
  67. IPA_NAT_INDEX_TABLE_ENTRY,
  68. IPA_NAT_INDEX_TABLE_NEXT_INDEX,
  69. IPA_NAT_TABLE_DMA_CMD_MAX
  70. } ipa_nat_table_dma_cmd_type;
  71. /*
  72. * ------------------------ NAT Table Entry -----------------------------------------
  73. *
  74. * ------------------------------------------------------------------------------------
  75. * | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
  76. * ------------------------------------------------------------------------------------
  77. * | Target IP(4B) | Private IP(4B) |
  78. * ------------------------------------------------------------------------------------
  79. * |Target Port(2B) | Private Port(2B) | Public Port(2B) | Next Index(2B) |
  80. * ------------------------------------------------------------------------------------
  81. * | Proto | TimeStamp(3B) | Flags(2B) | IP check sum Diff(2B)|
  82. * | (1B) | |EN|Redirect|Resv | |
  83. * ------------------------------------------------------------------------------------
  84. * | TCP/UDP checksum |PDN info|Reserved | SW Specific Parameters(4B) |
  85. * | diff (2B) | (1B) | (1B) | |
  86. * ------------------------------------------------------------------------------------
  87. *
  88. * Dont change below structure definition.
  89. *
  90. * It should be same as above(little endian order)
  91. *
  92. * -------------------------------------------------------------------------------
  93. */
  94. struct ipa_nat_rule {
  95. uint64_t private_ip:32;
  96. uint64_t target_ip:32;
  97. uint64_t next_index:16;
  98. uint64_t public_port:16;
  99. uint64_t private_port:16;
  100. uint64_t target_port:16;
  101. uint64_t ip_chksum:16;
  102. /*--------------------------------------------------
  103. IPA NAT Flag is interpreted as follows
  104. ---------------------------------------------------
  105. | EN |FIN/RST| S | IPv4 uC activation index |
  106. | [15] | [14] | [13] | [12:0] |
  107. ---------------------------------------------------
  108. --------------------------------------------------*/
  109. uint64_t uc_activation_index:13;
  110. uint64_t s:1;
  111. uint64_t redirect:1;
  112. uint64_t enable:1;
  113. uint64_t time_stamp:24;
  114. uint64_t protocol:8;
  115. /*--------------------------------------------------
  116. 32 bit sw_spec_params is interpreted as follows
  117. ------------------------------------
  118. | 16 bits | 16 bits |
  119. ------------------------------------
  120. | index table | prev index |
  121. | entry | |
  122. ------------------------------------
  123. --------------------------------------------------*/
  124. uint64_t prev_index:16;
  125. uint64_t indx_tbl_entry:16;
  126. uint64_t rsvd2:8;
  127. /*-----------------------------------------
  128. 8 bit PDN info is interpreted as following
  129. -----------------------------------------------------
  130. | 4 bits | 1 bit | 3 bits |
  131. -----------------------------------------------------
  132. | PDN index | uC processing | src dst Rsrv3 |
  133. | [7:4] | [3] | [2] [1] [0] |
  134. -----------------------------------------------------
  135. -------------------------------------------*/
  136. uint64_t rsvd3:1;
  137. uint64_t dst_only:1;
  138. uint64_t src_only:1;
  139. uint64_t ucp:1;
  140. uint64_t pdn_index:4;
  141. uint64_t tcp_udp_chksum:16;
  142. };
  143. static inline char* prep_nat_rule_4print(
  144. struct ipa_nat_rule* rule_ptr,
  145. char* buf_ptr,
  146. uint32_t buf_sz )
  147. {
  148. if ( rule_ptr && buf_ptr && buf_sz )
  149. {
  150. snprintf(
  151. buf_ptr, buf_sz,
  152. "NAT RULE: "
  153. "protocol(0x%02X) "
  154. "public_port(0x%04X) "
  155. "target_ip(0x%08X) "
  156. "target_port(0x%04X) "
  157. "private_ip(0x%08X) "
  158. "private_port(0x%04X) "
  159. "pdn_index(0x%02X) "
  160. "ip_chksum(0x%04X) "
  161. "tcp_udp_chksum(0x%04X) "
  162. "redirect(0x%02X) "
  163. "enable(0x%02X) "
  164. "time_stamp(0x%08X) "
  165. "indx_tbl_entry(0x%04X) "
  166. "prev_index(0x%04X) "
  167. "next_index(0x%04X)",
  168. rule_ptr->protocol,
  169. rule_ptr->public_port,
  170. rule_ptr->target_ip,
  171. rule_ptr->target_port,
  172. rule_ptr->private_ip,
  173. rule_ptr->private_port,
  174. rule_ptr->pdn_index,
  175. rule_ptr->ip_chksum,
  176. rule_ptr->tcp_udp_chksum,
  177. rule_ptr->redirect,
  178. rule_ptr->enable,
  179. rule_ptr->time_stamp,
  180. rule_ptr->indx_tbl_entry,
  181. rule_ptr->prev_index,
  182. rule_ptr->next_index);
  183. }
  184. return buf_ptr;
  185. }
  186. static inline const char *ipa3_nat_mem_in_as_str(
  187. enum ipa3_nat_mem_in nmi)
  188. {
  189. switch (nmi) {
  190. case IPA_NAT_MEM_IN_DDR:
  191. return "IPA_NAT_MEM_IN_DDR";
  192. case IPA_NAT_MEM_IN_SRAM:
  193. return "IPA_NAT_MEM_IN_SRAM";
  194. default:
  195. break;
  196. }
  197. return "???";
  198. }
  199. static inline char *ipa_ioc_v4_nat_init_as_str(
  200. struct ipa_ioc_v4_nat_init *ptr,
  201. char *buf,
  202. uint32_t buf_sz)
  203. {
  204. if (ptr && buf && buf_sz) {
  205. snprintf(
  206. buf, buf_sz,
  207. "V4 NAT INIT: tbl_index(0x%02X) ipv4_rules_offset(0x%08X) expn_rules_offset(0x%08X) index_offset(0x%08X) index_expn_offset(0x%08X) table_entries(0x%04X) expn_table_entries(0x%04X) ip_addr(0x%08X)",
  208. ptr->tbl_index,
  209. ptr->ipv4_rules_offset,
  210. ptr->expn_rules_offset,
  211. ptr->index_offset,
  212. ptr->index_expn_offset,
  213. ptr->table_entries,
  214. ptr->expn_table_entries,
  215. ptr->ip_addr);
  216. }
  217. return buf;
  218. }
  219. /*
  220. IPA NAT Flag is interpreted as follows
  221. ---------------------------------------------------
  222. | EN |FIN/RST| S | IPv4 uC activation index |
  223. | [15] | [14] | [13] | [12:0] |
  224. ---------------------------------------------------
  225. */
  226. typedef struct {
  227. uint32_t uc_activation_index:13;
  228. uint32_t s:1;
  229. uint32_t redirect:1;
  230. uint32_t enable:1;
  231. } ipa_nat_flags;
  232. struct ipa_nat_indx_tbl_rule {
  233. uint16_t tbl_entry;
  234. uint16_t next_index;
  235. };
  236. struct ipa_nat_indx_tbl_meta_info {
  237. uint16_t prev_index;
  238. };
  239. struct ipa_nat_ip4_table_cache {
  240. uint32_t public_addr;
  241. ipa_mem_descriptor mem_desc;
  242. ipa_table table;
  243. ipa_table index_table;
  244. struct ipa_nat_indx_tbl_meta_info *index_expn_table_meta;
  245. ipa_table_dma_cmd_helper table_dma_cmd_helpers[IPA_NAT_TABLE_DMA_CMD_MAX];
  246. };
  247. struct ipa_nat_cache {
  248. ipa_descriptor* ipa_desc;
  249. struct ipa_nat_ip4_table_cache ip4_tbl[IPA_NAT_MAX_IP4_TBLS];
  250. uint8_t table_cnt;
  251. enum ipa3_nat_mem_in nmi;
  252. };
  253. int ipa_nati_add_ipv4_tbl(
  254. uint32_t public_ip_addr,
  255. const char *mem_type_ptr,
  256. uint16_t number_of_entries,
  257. uint32_t *table_hanle);
  258. int ipa_nati_del_ipv4_table(uint32_t tbl_hdl);
  259. int ipa_nati_query_timestamp(uint32_t tbl_hdl,
  260. uint32_t rule_hdl,
  261. uint32_t *time_stamp);
  262. int ipa_nati_modify_pdn(struct ipa_ioc_nat_pdn_entry *entry);
  263. int ipa_nati_get_pdn_index(uint32_t public_ip, uint8_t *pdn_index);
  264. int ipa_nati_alloc_pdn(ipa_nat_pdn_entry *pdn_info, uint8_t *pdn_index);
  265. int ipa_nati_get_pdn_cnt(void);
  266. int ipa_nati_dealloc_pdn(uint8_t pdn_index);
  267. int ipa_nati_add_ipv4_rule(uint32_t tbl_hdl,
  268. const ipa_nat_ipv4_rule *clnt_rule,
  269. uint32_t *rule_hdl);
  270. int ipa_nati_del_ipv4_rule(uint32_t tbl_hdl,
  271. uint32_t rule_hdl);
  272. int ipa_nati_get_sram_size(
  273. uint32_t* size_ptr);
  274. int ipa_nati_clear_ipv4_tbl(
  275. uint32_t tbl_hdl );
  276. int ipa_nati_copy_ipv4_tbl(
  277. uint32_t src_tbl_hdl,
  278. uint32_t dst_tbl_hdl,
  279. ipa_table_walk_cb copy_cb );
  280. typedef enum
  281. {
  282. USE_NAT_TABLE = 0,
  283. USE_INDEX_TABLE = 1,
  284. USE_MAX
  285. } WhichTbl2Use;
  286. #define VALID_WHICHTBL2USE(w) \
  287. ( (w) >= USE_NAT_TABLE && (w) < USE_MAX )
  288. int ipa_nati_walk_ipv4_tbl(
  289. uint32_t tbl_hdl,
  290. WhichTbl2Use which,
  291. ipa_table_walk_cb walk_cb,
  292. void* arb_data_ptr );
  293. /*
  294. * The following used for retrieving table stats.
  295. */
  296. typedef struct
  297. {
  298. enum ipa3_nat_mem_in nmi;
  299. uint32_t tot_ents;
  300. uint32_t tot_base_ents;
  301. uint32_t tot_base_ents_filled;
  302. uint32_t tot_expn_ents;
  303. uint32_t tot_expn_ents_filled;
  304. uint32_t tot_chains;
  305. uint32_t min_chain_len;
  306. uint32_t max_chain_len;
  307. float avg_chain_len;
  308. } ipa_nati_tbl_stats;
  309. int ipa_nati_ipv4_tbl_stats(
  310. uint32_t tbl_hdl,
  311. ipa_nati_tbl_stats* nat_stats_ptr,
  312. ipa_nati_tbl_stats* idx_stats_ptr );
  313. int ipa_nati_vote_clock(
  314. enum ipa_app_clock_vote_type vote_type );
  315. int ipa_NATI_add_ipv4_tbl(
  316. enum ipa3_nat_mem_in nmi,
  317. uint32_t public_ip_addr,
  318. uint16_t number_of_entries,
  319. uint32_t* tbl_hdl);
  320. int ipa_NATI_del_ipv4_table(
  321. uint32_t tbl_hdl);
  322. int ipa_NATI_clear_ipv4_tbl(
  323. uint32_t tbl_hdl );
  324. int ipa_NATI_walk_ipv4_tbl(
  325. uint32_t tbl_hdl,
  326. WhichTbl2Use which,
  327. ipa_table_walk_cb walk_cb,
  328. void* arb_data_ptr );
  329. int ipa_NATI_ipv4_tbl_stats(
  330. uint32_t tbl_hdl,
  331. ipa_nati_tbl_stats* nat_stats_ptr,
  332. ipa_nati_tbl_stats* idx_stats_ptr );
  333. int ipa_NATI_query_timestamp(
  334. uint32_t tbl_hdl,
  335. uint32_t rule_hdl,
  336. uint32_t* time_stamp);
  337. int ipa_NATI_add_ipv4_rule(
  338. uint32_t tbl_hdl,
  339. const ipa_nat_ipv4_rule* clnt_rule,
  340. uint32_t* rule_hdl);
  341. int ipa_NATI_del_ipv4_rule(
  342. uint32_t tbl_hdl,
  343. uint32_t rule_hdl);
  344. int ipa_NATI_post_ipv4_init_cmd(
  345. uint32_t tbl_hdl );
  346. #endif /* #ifndef IPA_NAT_DRVI_H */