diff options
| author | Lu Qiuwen <[email protected]> | 2018-11-21 19:20:19 +0800 |
|---|---|---|
| committer | Lu Qiuwen <[email protected]> | 2018-11-21 19:52:22 +0800 |
| commit | 1cfc044c43070c8a83cfc7de2334ac9219895513 (patch) | |
| tree | 48908eb24927eb8f4345990234dc871d06de7efb | |
| parent | 5a3ad0aa42710b268ae8749409bbbe59d19d1f38 (diff) | |
增加非对称隧道自动创建的开关,修正部分情况下MPLS标签填写错误的问题v4.2.43-20181121
* 增加非对称隧道自动创建的开关,在S->C和C->S两侧隧道构成不一致时,不自动根据S->C创建C->S隧道信息;
* 修正MPLS标签填写错误的问题,原实现在TunInnerEther对象复制时,复制了其中的指针,导致错误的内存写入,现修正。
| -rw-r--r-- | tunnat/include/tunnat.h | 6 | ||||
| -rw-r--r-- | tunnat/include/tunnel.h | 1 | ||||
| -rw-r--r-- | tunnat/src/core.cc | 14 | ||||
| -rw-r--r-- | tunnat/src/runtime.cc | 26 | ||||
| -rw-r--r-- | tunnat/src/tunnel.cc | 10 |
5 files changed, 45 insertions, 12 deletions
diff --git a/tunnat/include/tunnat.h b/tunnat/include/tunnat.h index ff07d43..2c8ab30 100644 --- a/tunnat/include/tunnat.h +++ b/tunnat/include/tunnat.h @@ -61,6 +61,10 @@ struct TunnatInstance /* 线程运行句柄 */ TunnatThreadInstance * thread_instances[MR_SID_MAX]; + /* 是否根据一个方向自动创建另一方向的Tunnel信息 */ + bool is_auto_reserve_tunnel; + /* 是否向最近使用的隧道注入找不到隧道信息的包 */ + bool is_use_recent_tunnel; TunnatInstance(); ~TunnatInstance() = default; @@ -195,4 +199,4 @@ struct TunnatThreadInstance SessionTable * ss_table; TunnatThreadStat * th_stat; TunnelContainerArray * tun_container_array; -};
\ No newline at end of file +}; diff --git a/tunnat/include/tunnel.h b/tunnat/include/tunnel.h index 30b6c6d..221eb9d 100644 --- a/tunnat/include/tunnel.h +++ b/tunnat/include/tunnel.h @@ -166,7 +166,6 @@ protected: constexpr static int SZ_ETHER_TUNNEL_STORAGE = 64; struct ether_hdr ether_hdr_; char ether_tunnel_on_stack_[SZ_ETHER_TUNNEL_STORAGE]; - char * ether_tunnel_; size_t ether_tunnel_len_; public: diff --git a/tunnat/src/core.cc b/tunnat/src/core.cc index 1b6cde8..b83a339 100644 --- a/tunnat/src/core.cc +++ b/tunnat/src/core.cc @@ -171,6 +171,18 @@ int tunnat_session_setup(TunnatInstance * instance) MESA_load_profile_uint_def(instance->cfgfile.c_str(), "tunnat", "expire_time", &instance->expire_time, instance->expire_time); + unsigned int __auto_reserve_tunnel; + unsigned int __is_use_recent_tunnel; + + MESA_load_profile_uint_def(instance->cfgfile.c_str(), "tunnat", "reverse_tunnel", + &__auto_reserve_tunnel, (unsigned int)instance->is_auto_reserve_tunnel); + + MESA_load_profile_uint_def(instance->cfgfile.c_str(), "tunnat", "use_recent_tunnel", + &__is_use_recent_tunnel, (unsigned int)instance->is_use_recent_tunnel); + + instance->is_auto_reserve_tunnel = __auto_reserve_tunnel > 0; + instance->is_use_recent_tunnel = __is_use_recent_tunnel > 0; + return RT_SUCCESS; } @@ -356,4 +368,4 @@ int main(int argc, char * argv[]) err_out: MR_ERROR("Tunnat Gateway initialization failed. Exit. "); return EXIT_FAILURE; -}
\ No newline at end of file +} diff --git a/tunnat/src/runtime.cc b/tunnat/src/runtime.cc index ea347d4..f2ce931 100644 --- a/tunnat/src/runtime.cc +++ b/tunnat/src/runtime.cc @@ -315,25 +315,39 @@ static int __virt_to_phy_pkt_encap(TunnatInstance * instance, TunnatThreadInstan default: assert(0); break; - } + } /* 双向查询 */ - SessionEntry * ss_entry = th_instance->ss_table->QueryDualDirect(ss_key); - if (ss_entry != NULL) goto session_realy; + SessionEntry * ss_entry; + if (instance->is_auto_reserve_tunnel) + { + ss_entry = th_instance->ss_table->QueryDualDirect(ss_key); + } + else + { + ss_entry = th_instance->ss_table->Query(ss_key); + } + + //fprintf(stderr, "query src %s dst %s, result is %p\n", str_in_addr_src, str_in_addr_dst, ss_entry); + if (ss_entry != NULL) goto session_ready; /* 没有查询到,使用最近的一次隧道数据,随便找到一个专用设备回注 */ - ss_entry = th_instance->ss_table->QueryRecent(ss_key.type); + if (instance->is_use_recent_tunnel) + { + ss_entry = th_instance->ss_table->QueryRecent(ss_key.type); + } + if (ss_entry != NULL) { TUNNAT_THREAD_STAT_ADD(TUNNAT_STAT_ENCAP_USE_RECENT_SESSION, 1); - goto session_realy; + goto session_ready; } /* 还没有查询到,报告失败 */ TUNNAT_THREAD_STAT_ADD(TUNNAT_STAT_ENCAP_FAIL_NO_SESSION, 1); return RT_ERR; -session_realy: +session_ready: /* 已经查找到隧道上下文,构建隧道报文 */ TunnelContainerArray & tun_array = ss_entry->tun_array; diff --git a/tunnat/src/tunnel.cc b/tunnat/src/tunnel.cc index ed9b2c6..f7c4ab0 100644 --- a/tunnat/src/tunnel.cc +++ b/tunnat/src/tunnel.cc @@ -161,6 +161,7 @@ int TunVxlan::PacketConstruct(const char * pkt, unsigned int pkt_len) ipv4_hdr->dst_addr = ipv4_hdr->src_addr; ipv4_hdr->src_addr = __swap_ip_addr; + ipv4_hdr->time_to_live = 88; ipv4_hdr->total_length = htons(pkt_len - offset); /* 计算头部校验和 */ @@ -250,12 +251,11 @@ int TunInnerEther::PacketParse(const char * pkt, unsigned int len) /* TODO: 超过栈上存储空间大小,在堆上分配内存 */ assert(ether_tunnel_len_ <= SZ_ETHER_TUNNEL_STORAGE); - ether_tunnel_ = ether_tunnel_on_stack_; /* 复制Ether-Tunnel(包括VLAN, MPLS等等) */ if (ether_tunnel_len_ > 0) { - memcpy(ether_tunnel_, ptr_ether_tunnel, ether_tunnel_len_); + memcpy(ether_tunnel_on_stack_, ptr_ether_tunnel, ether_tunnel_len_); } /* 三层 */ @@ -282,7 +282,11 @@ int TunInnerEther::PacketConstruct(const char * pkt, unsigned int pkt_len) pkt_len -= sizeof(struct ether_hdr); assert(pkt_len >= ether_tunnel_len_); - if(ether_tunnel_len_ > 0) memcpy((char *)pkt, ether_tunnel_, ether_tunnel_len_); + if(ether_tunnel_len_ > 0) + { + memcpy((char *) pkt, ether_tunnel_on_stack_, ether_tunnel_len_); + } + return RT_SUCCESS; } |
