diff options
| author | Qiuwen Lu <[email protected]> | 2017-06-01 10:54:17 +0800 |
|---|---|---|
| committer | Qiuwen Lu <[email protected]> | 2017-06-01 10:54:17 +0800 |
| commit | 672ab1f22e5b593fa53d1bbf65315cf019a5a79c (patch) | |
| tree | 847e60e2f9a97918e74059480ed42168e6989949 | |
| parent | b83cf84f01ec09ec778146011575837fbc980d20 (diff) | |
将TunArray对象在线程上下文中保存,避免调用构造函数的开销。增加burst用户参数,避免每次收报文都使用最大的Burst参数。增加Session表的测试程序。
| -rw-r--r-- | tunnat/CMakeLists.txt | 11 | ||||
| -rw-r--r-- | tunnat/include/session.h | 2 | ||||
| -rw-r--r-- | tunnat/include/tunnat.h | 4 | ||||
| -rw-r--r-- | tunnat/include/tunnel.h | 2 | ||||
| -rw-r--r-- | tunnat/src/core.cc | 8 | ||||
| -rw-r--r-- | tunnat/src/monit.cc | 2 | ||||
| -rw-r--r-- | tunnat/src/runtime.cc | 20 | ||||
| -rw-r--r-- | tunnat/src/session.cc | 31 | ||||
| -rw-r--r-- | tunnat/test/TestSession.cc | 72 |
9 files changed, 138 insertions, 14 deletions
diff --git a/tunnat/CMakeLists.txt b/tunnat/CMakeLists.txt index e83442d..30829e2 100644 --- a/tunnat/CMakeLists.txt +++ b/tunnat/CMakeLists.txt @@ -8,11 +8,16 @@ include_directories(${DPDK_INCLUDE_DIR}) add_definitions(${DPDK_C_PREDEFINED}) add_executable(mrtunnat src/core.cc src/runtime.cc src/tunnel.cc src/session.cc src/monit.cc) -target_link_libraries(mrtunnat MESA_prof_load_static MESA_htable marsio infra ) +target_link_libraries(mrtunnat MESA_prof_load_static MESA_htable marsio infra) target_link_libraries(mrtunnat rt pthread dl ${SYSTEMD_LIBRARIES}) target_include_directories(mrtunnat INTERFACE ${SYSTEMD_INCLUDE_DIRS}) target_include_directories(mrtunnat PRIVATE include) # Install -#install(TARGETS pag LIBRARY DESTINATION lib COMPONENT Program) -#install(FILES libpag.h DESTINATION include COMPONENT Program)
\ No newline at end of file +#install(TARGETS mrtunnat LIBRARY DESTINATION bin COMPONENT Program) +#install(FILES libpag.h DESTINATION include COMPONENT Program) + +# test +add_executable(TestSession test/TestSession.cc src/session.cc src/tunnel.cc) +target_link_libraries(TestSession gtest infra ${SYSTEMD_LIBRARIES} MESA_htable) +target_include_directories(TestSession PRIVATE include)
\ No newline at end of file diff --git a/tunnat/include/session.h b/tunnat/include/session.h index 979d8c7..06b9bd2 100644 --- a/tunnat/include/session.h +++ b/tunnat/include/session.h @@ -40,4 +40,6 @@ public: protected: MESA_htable_handle ht_table_; + static int htable_comp_fun_cb(const uchar * key1, uint size1, const uchar * key2, uint size2); + static int htable_free_fun_cb(void *data); };
\ No newline at end of file diff --git a/tunnat/include/tunnat.h b/tunnat/include/tunnat.h index 7eb82d1..49f56d3 100644 --- a/tunnat/include/tunnat.h +++ b/tunnat/include/tunnat.h @@ -3,6 +3,7 @@ #include <string> #include <vector> #include <cassert> +#include <tunnel.h> extern "C" { @@ -35,6 +36,8 @@ struct TunnatInstance std::string mntfile; /* 运行数据面线程数量 */ unsigned int nr_thread; + /* Burst数量 */ + unsigned int nr_burst; /* 运行数据面线程核心掩码 */ cpu_mask_t coremask; /* 会话表最大会话数 */ @@ -173,4 +176,5 @@ struct TunnatThreadInstance TunnatInstance * instance; 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 0b060f6..5909838 100644 --- a/tunnat/include/tunnel.h +++ b/tunnat/include/tunnel.h @@ -221,4 +221,6 @@ struct TunnelContainerArray } TunnelContainerArray() : sz_array(0), sz_total_len(0) {} + + void Reset() { sz_array = 0, sz_total_len = 0; } };
\ No newline at end of file diff --git a/tunnat/src/core.cc b/tunnat/src/core.cc index 982f622..7454c3a 100644 --- a/tunnat/src/core.cc +++ b/tunnat/src/core.cc @@ -38,6 +38,10 @@ extern "C" #define MR_TUNNAT_DEFAULT_SESSION_SLOT_MAX 4096 #endif +#ifndef MR_TUNNAT_DEFAULT_NR_BURST +#define MR_TUNNAT_DEFAULT_NR_BURST 32 +#endif + unsigned int g_logger_to_stdout = 1; unsigned int g_logger_level = LOG_DEBUG; unsigned int g_keep_running = 1; @@ -48,6 +52,7 @@ __thread TunnatThreadStat * TunnatThreadStat::thread_stat_object_; TunnatInstance::TunnatInstance() : str_appsym(MR_TUNNAT_DEFAULT_APPSYM), cfgfile(MR_TUNNAT_DEFAULT_CFG_FILE), mntfile(MR_TUNNAT_DEFAULT_MNT_FILE), + nr_burst(MR_TUNNAT_DEFAULT_NR_BURST), nr_max_sessions(MR_TUNNAT_DEFAULT_SESSION_MAX), nr_slots(MR_TUNNAT_DEFAULT_SESSION_SLOT_MAX) { @@ -224,7 +229,8 @@ int tunnat_thread_setup(TunnatInstance * instance, TunnatThreadInstance * th_ins th_instance->instance = instance; th_instance->ss_table = new SessionTable; th_instance->th_stat = new TunnatThreadStat; - + th_instance->tun_container_array = new TunnelContainerArray[instance->nr_burst]; + int ret = th_instance->ss_table->Init(instance->nr_max_sessions, instance->nr_slots, instance->expire_time); diff --git a/tunnat/src/monit.cc b/tunnat/src/monit.cc index 7a4026e..e89ef96 100644 --- a/tunnat/src/monit.cc +++ b/tunnat/src/monit.cc @@ -48,7 +48,7 @@ static cJSON * monit_runtime(TunnatInstance * instance) stat_value[thread_id] = th_stat_object->StatValueGet( static_cast<TunnatThreadStat::stat_type_t>(stat_entry)); - stat_speed[thread_id] = th_stat_object->StatValueGet( + stat_speed[thread_id] = th_stat_object->StatSpeedGet( static_cast<TunnatThreadStat::stat_type_t>(stat_entry)); } diff --git a/tunnat/src/runtime.cc b/tunnat/src/runtime.cc index 762621c..a26df66 100644 --- a/tunnat/src/runtime.cc +++ b/tunnat/src/runtime.cc @@ -8,6 +8,7 @@ extern "C" { #include <marsio.h> +#include <rte_cycles.h> } #ifndef MR_TUNNAT_USE_PHONY_ETHER_HEADER @@ -105,6 +106,8 @@ static int __phy_to_virt_pkt_modify(TunnatInstance * instance, TunnatThreadInsta struct ether_hdr * p_ether_hdr = (struct ether_hdr *) marsio_buff_prepend(mbuf, sizeof(struct ether_hdr)); + assert(p_ether_hdr != NULL); + ctrlzone->p_ether_type = p_ether_hdr->ether_type; switch (tun_array.GetInnerTunContainer().ThisTunType()) { @@ -121,25 +124,29 @@ static int __phy_to_virt_pkt_modify(TunnatInstance * instance, TunnatThreadInsta #endif (void)__adj_check; - return 0; + return RT_SUCCESS; } static void __phy_to_virt_one_device(TunnatInstance * instance, TunnatThreadInstance * th_instance, TunnatInstance::_devs * phy_device_hand, TunnatInstance::_devs * virt_device_hand) { unsigned int tid = th_instance->thread_id; + unsigned int nr_burst = instance->nr_burst; + marsio_buff_t * mbufs[MR_BURST_MAX]; - int nr_mbufs = marsio_recv_burst(phy_device_hand->vdev_handler, tid, mbufs, MR_BURST_MAX); + int nr_mbufs = marsio_recv_burst(phy_device_hand->vdev_handler, tid, mbufs, nr_burst); if (nr_mbufs <= 0) return; /* 报文封装信息提取结果 */ - TunnelContainerArray tun_container_array[MR_BURST_MAX]; + TunnelContainerArray * tun_container_array = th_instance->tun_container_array; unsigned int nr_encap_mbufs = 0; /* 报文解析,判断封装格式 */ for (int i = 0; i < nr_mbufs; i++) { + tun_container_array[i].Reset(); + int ret = __phy_to_virt_pkt_decap(instance, th_instance, mbufs[i], tun_container_array[i]); @@ -154,7 +161,6 @@ static void __phy_to_virt_one_device(TunnatInstance * instance, TunnatThreadInst } } - /* 统计 */ TUNNAT_THREAD_STAT_ADD(TUNNAT_STAT_PKT_INPUT, nr_mbufs); TUNNAT_THREAD_STAT_ADD(TUNNAT_STAT_PKT_ENCAP_INPUT, nr_encap_mbufs); TUNNAT_THREAD_STAT_ADD(TUNNAT_STAT_PKT_RAW_INPUT, nr_mbufs - nr_encap_mbufs); @@ -244,9 +250,11 @@ static void __virt_to_phy_one_device(TunnatInstance * instance, TunnatThreadInst TunnatInstance::_devs * phy_device_hand, TunnatInstance::_devs * virt_device_hand) { unsigned int tid = th_instance->thread_id; + unsigned int nr_burst = instance->nr_burst; + marsio_buff_t * mbufs[MR_BURST_MAX]; - int nr_mbufs = marsio_recv_burst(virt_device_hand->vdev_handler, tid, mbufs, MR_BURST_MAX); + int nr_mbufs = marsio_recv_burst(virt_device_hand->vdev_handler, tid, mbufs, nr_burst); if (nr_mbufs <= 0) return; marsio_buff_t * fwd_mbufs[MR_BURST_MAX]; @@ -298,7 +306,7 @@ void * tunnat_thread_loop(void * arg) while (g_keep_running) { __phy_to_virt_one_device(instance, th_instance, &phydev, &virtdev); - __virt_to_phy_one_device(instance, th_instance, &virtdev, &phydev); + __virt_to_phy_one_device(instance, th_instance, &phydev, &virtdev); } return 0; diff --git a/tunnat/src/session.cc b/tunnat/src/session.cc index 34dc652..86f4c0b 100644 --- a/tunnat/src/session.cc +++ b/tunnat/src/session.cc @@ -25,11 +25,12 @@ int SessionTable::Add(SessionKey & key, SessionEntry & entry) { SessionEntry * ss_entry = new SessionEntry; *ss_entry = entry; - - MESA_htable_add(ht_table_, (const unsigned char *)&key, sizeof(SessionKey), ss_entry); TUNNAT_THREAD_STAT_ADD(TUNNAT_STAT_SESSION_ADD, 1); - return 0; + int ret = MESA_htable_add(ht_table_, (const unsigned char *)&key, sizeof(SessionKey), ss_entry); + + assert(ret >= 0); + return ret; } int SessionTable::Init(unsigned int nr_max_sessions, unsigned int nr_slots, unsigned int expire_time) @@ -62,6 +63,12 @@ int SessionTable::Init(unsigned int nr_max_sessions, unsigned int nr_slots, unsi opt_int = HASH_ELIMINATE_ALGO_FIFO; MESA_htable_set_opt(htable, MHO_ELIMIMINATE_TYPE, &opt_int, sizeof(int)); + void * fn_cmp_cb = (void *)htable_comp_fun_cb; + MESA_htable_set_opt(htable, MHO_CBFUN_KEY_COMPARE, fn_cmp_cb, sizeof(fn_cmp_cb)); + + void * fn_data_free_cb = (void *)htable_free_fun_cb; + MESA_htable_set_opt(htable, MHO_CBFUN_DATA_FREE, fn_data_free_cb, sizeof(fn_data_free_cb)); + int ret = MESA_htable_mature(htable); if (ret < 0) { @@ -71,4 +78,22 @@ int SessionTable::Init(unsigned int nr_max_sessions, unsigned int nr_slots, unsi ht_table_ = htable; return RT_SUCCESS; +} + +int SessionTable::htable_comp_fun_cb(const uchar * key1, uint size1, const uchar * key2, uint size2) +{ + SessionKey * __key1 = (SessionKey *)key1; + SessionKey * __key2 = (SessionKey *)key2; + + if ((__key1->s_in_addr.s_addr == __key2->s_in_addr.s_addr) + && (__key1->d_in_addr.s_addr == __key2->d_in_addr.s_addr)) return 0; + + return 0; +} + +int SessionTable::htable_free_fun_cb(void * data) +{ + SessionEntry * ss_entry = (SessionEntry *)data; + delete ss_entry; + return 0; }
\ No newline at end of file diff --git a/tunnat/test/TestSession.cc b/tunnat/test/TestSession.cc new file mode 100644 index 0000000..3d342f1 --- /dev/null +++ b/tunnat/test/TestSession.cc @@ -0,0 +1,72 @@ + +#include <session.h> +#include <gtest/gtest.h> + +unsigned int g_logger_to_stdout = 1; +unsigned int g_logger_level = LOG_DEBUG; +unsigned int g_keep_running = 1; +unsigned int g_ctrlzone_id = 0; + +__thread TunnatThreadStat * TunnatThreadStat::thread_stat_object_; + +class TestSessionEnv : public testing::Environment +{ +public: + TunnatThreadStat * th_stat_object; + + virtual void SetUp() + { + th_stat_object = new TunnatThreadStat; + TunnatThreadStat::ThreadStatObjectSet(th_stat_object); + } + + virtual void TearDown() + { + delete th_stat_object; + } +}; + +int SessionKeyGenerate(SessionKey & key) +{ + key.s_in_addr.s_addr = rand(); + key.d_in_addr.s_addr = rand(); +} + +TEST(TunnatSession, TunnatSessionRandomAddQuery) +{ + const int nr_session = 8192; + const int nr_slots = 4096; + const int expire_time = 0; + const int rd_keys = 2048; + + int ret = 0; + SessionTable * ss_table = new SessionTable; + ASSERT_EQ(ss_table->Init(nr_session, nr_slots, expire_time), 0); + + std::vector<SessionKey> vec_test_keys; + SessionKey __test_keys; + SessionEntry __test_entry; + + for (int i = 0; i < rd_keys; i++) + { + SessionKeyGenerate(__test_keys); + ss_table->Add(__test_keys, __test_entry); + vec_test_keys.push_back(__test_keys); + } + + for (auto & _key : vec_test_keys) + { + SessionEntry * __test_entry = NULL; + __test_entry = ss_table->Query(_key); + EXPECT_TRUE(__test_entry != NULL); + } + + delete ss_table; +} + +int main(int argc, char * argv[]) +{ + testing::AddGlobalTestEnvironment(new TestSessionEnv);
+ testing::InitGoogleTest(&argc, argv);
+ return RUN_ALL_TESTS();
+}
\ No newline at end of file |
