luat_netdrv_napt.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #ifndef LUAT_NETDRV_NAPT_H
  2. #define LUAT_NETDRV_NAPT_H
  3. #include "lwip/pbuf.h"
  4. // 返回值定义
  5. #define NAPT_RET_OK 0 // 转发成功
  6. #define NAPT_RET_SKIP 1 // 跳过处理,让LWIP继续
  7. #define NAPT_RET_NO_MAPPING -1 // 未找到映射关系
  8. #define NAPT_RET_NO_MEMORY -2 // 内存不足
  9. #define NAPT_RET_LOCK_FAIL -3 // 加锁失败
  10. #define NAPT_RET_INVALID_CTX -4 // NAPT上下文无效
  11. // 哈希表大小(用于加速查找)
  12. // 根据压力测试结果优化:适应高并发场景,负载因子0.5-0.6
  13. #ifndef NAPT_HASH_TABLE_SIZE
  14. #if defined(TYPE_EC718HM)
  15. #define NAPT_HASH_TABLE_SIZE 16384 // 8K映射,负载因子0.5 (~131KB,两表共计)
  16. #elif defined(TYPE_EC718PM)
  17. #define NAPT_HASH_TABLE_SIZE 8192 // 4K映射,负载因子0.5 (~65KB)
  18. #else
  19. #define NAPT_HASH_TABLE_SIZE 4096 // 2K映射,负载因子0.5 (~33KB)
  20. #endif
  21. #endif
  22. #define NAPT_HASH_INVALID_INDEX 0xFFFF
  23. #define NAPT_HASH_MAX_PROBE 96 // 提高到96,应对更高的冲突率
  24. // #define IP_NAPT_TIMEOUT_MS_TCP (30*60*1000)
  25. #define IP_NAPT_TIMEOUT_MS_TCP_DISCON (20*1000)
  26. #ifndef NAPT_TCP_MAP_ITEM_MAX
  27. #if defined(TYPE_EC718HM)
  28. #define NAPT_TCP_MAP_ITEM_MAX (8*1024)
  29. #elif defined(TYPE_EC718PM)
  30. #define NAPT_TCP_MAP_ITEM_MAX (4*1024)
  31. #else
  32. #define NAPT_TCP_MAP_ITEM_MAX (2*1024)
  33. #endif
  34. #endif
  35. typedef struct luat_netdrv_napt_icmp
  36. {
  37. uint8_t is_vaild;
  38. uint8_t adapter_id;
  39. uint16_t inet_id;
  40. uint16_t wnet_id;
  41. uint32_t inet_ip;
  42. uint32_t wnet_ip;
  43. uint8_t inet_mac[6];
  44. uint64_t tm_ms; // 最后通信时间
  45. }luat_netdrv_napt_icmp_t;
  46. typedef struct luat_netdrv_napt_tcpudp
  47. {
  48. uint8_t is_vaild;
  49. uint8_t adapter_id;
  50. uint32_t inet_ip;
  51. uint16_t inet_port;
  52. uint32_t wnet_ip;
  53. uint16_t wnet_port;
  54. uint16_t wnet_local_port;
  55. uint8_t inet_mac[6];
  56. uint64_t tm_ms; // 最后通信时间
  57. // TCP状态记录
  58. unsigned int fin1 : 1;
  59. unsigned int fin2 : 1;
  60. unsigned int finack1 : 1;
  61. unsigned int finack2 : 1;
  62. unsigned int synack : 1;
  63. unsigned int rst : 1;
  64. }luat_netdrv_napt_tcpudp_t;
  65. typedef struct napt_ctx
  66. {
  67. luat_netdrv_t* net;
  68. luat_netdrv_t* drv_gw;
  69. uint8_t* buff;
  70. size_t len;
  71. struct eth_hdr* eth;
  72. struct ip_hdr* iphdr;
  73. int is_wnet;
  74. }napt_ctx_t;
  75. struct luat_netdrv_napt_llist;
  76. typedef struct luat_netdrv_napt_llist
  77. {
  78. struct luat_netdrv_napt_llist* next;
  79. luat_netdrv_napt_tcpudp_t item;
  80. }luat_netdrv_napt_llist_t;
  81. // 哈希表项,用于加速查找(纯线性探测,无链表)
  82. typedef struct {
  83. uint16_t item_index; // 映射项在items数组中的索引,NAPT_HASH_INVALID_INDEX表示空槽
  84. } napt_hash_entry_t;
  85. typedef struct luat_netdrv_napt_ctx{
  86. uint32_t ip_tp;
  87. size_t clean_tm;
  88. size_t item_max;
  89. size_t item_last;
  90. luat_netdrv_napt_tcpudp_t items[NAPT_TCP_MAP_ITEM_MAX];
  91. luat_rtos_mutex_t lock;
  92. uint32_t *port_used;
  93. // 哈希表用于加速查找(WAN->LAN方向)
  94. napt_hash_entry_t *hash_table_wan2lan; // 按(wnet_ip, wnet_port, wnet_local_port)索引
  95. // 哈希表用于加速查找(LAN->WAN方向)
  96. napt_hash_entry_t *hash_table_lan2wan; // 按(inet_ip, inet_port, wnet_ip, wnet_port)索引
  97. }luat_netdrv_napt_ctx_t;
  98. int luat_napt_icmp_handle(napt_ctx_t* ctx);
  99. int luat_napt_tcp_handle(napt_ctx_t* ctx);
  100. int luat_napt_udp_handle(napt_ctx_t* ctx);
  101. void luat_netdrv_napt_tcp_cleanup(void);
  102. void luat_netdrv_napt_udp_cleanup(void);
  103. int luat_netdrv_napt_pkg_input(int id, uint8_t* buff, size_t len);
  104. int luat_netdrv_napt_pkg_input_pbuf(int id, struct pbuf* p);
  105. // 维护影响关系
  106. int luat_netdrv_napt_tcp_wan2lan(napt_ctx_t* ctx, luat_netdrv_napt_tcpudp_t* mapping, luat_netdrv_napt_ctx_t *napt_ctx);
  107. int luat_netdrv_napt_tcp_lan2wan(napt_ctx_t* ctx, luat_netdrv_napt_tcpudp_t* mapping, luat_netdrv_napt_ctx_t *napt_ctx);
  108. void luat_netdrv_napt_enable(int adapter_id);
  109. void luat_netdrv_napt_disable(void);
  110. #endif