Refactor enum session_stat with improved naming conventions
This commit is contained in:
@@ -308,7 +308,7 @@ int stellar_inject_tcp_flags(const struct session *sess, enum flow_direction inj
|
||||
|
||||
if (session_get_type(sess) != SESSION_TYPE_TCP)
|
||||
{
|
||||
session_inc_stat((struct session *)sess, inject_dir, STAT_INJ_PKTS_FAIL, 1);
|
||||
session_inc_stat((struct session *)sess, inject_dir, STAT_INJECTED_PACKETS_FAILED, 1);
|
||||
INJECT_PACKET_LOG_ERROR("session %ld is not a TCP session, cannot inject TCP RST", session_get_id(sess));
|
||||
return -1;
|
||||
}
|
||||
@@ -316,7 +316,7 @@ int stellar_inject_tcp_flags(const struct session *sess, enum flow_direction inj
|
||||
const struct packet *pkt = session_get_first_packet(sess, inject_dir);
|
||||
if (pkt == NULL)
|
||||
{
|
||||
session_inc_stat((struct session *)sess, inject_dir, STAT_INJ_PKTS_FAIL, 1);
|
||||
session_inc_stat((struct session *)sess, inject_dir, STAT_INJECTED_PACKETS_FAILED, 1);
|
||||
INJECT_PACKET_LOG_ERROR("session %ld has no %s first packet, cannot inject TCP RST", session_get_id(sess), flow_direction_to_str(inject_dir));
|
||||
return -1;
|
||||
}
|
||||
@@ -332,7 +332,7 @@ int stellar_inject_tcp_flags(const struct session *sess, enum flow_direction inj
|
||||
if (len <= 0)
|
||||
{
|
||||
INJECT_PACKET_LOG_ERROR("session %ld build TCP %s RST packet failed, %s", session_get_id(sess), flow_direction_to_str(inject_dir), strerror(len));
|
||||
session_inc_stat((struct session *)sess, inject_dir, STAT_INJ_PKTS_FAIL, 1);
|
||||
session_inc_stat((struct session *)sess, inject_dir, STAT_INJECTED_PACKETS_FAILED, 1);
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -347,14 +347,14 @@ int stellar_inject_tcp_flags(const struct session *sess, enum flow_direction inj
|
||||
packet_set_origin_ctx(&inj_pkt, &meta);
|
||||
if (packet_io_inject(runtime->packet_io, thr_idx, &inj_pkt, 1) == 1)
|
||||
{
|
||||
session_inc_stat((struct session *)sess, inject_dir, STAT_INJ_PKTS_SUSS, 1);
|
||||
session_inc_stat((struct session *)sess, inject_dir, STAT_INJ_BYTES_SUSS, len);
|
||||
session_inc_stat((struct session *)sess, inject_dir, STAT_INJECTED_PACKETS_SUCCESS, 1);
|
||||
session_inc_stat((struct session *)sess, inject_dir, STAT_INJECTED_BYTES_SUCCESS, len);
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
INJECT_PACKET_LOG_ERROR("session %ld inject TCP %s RST packet failed, packet I/O nospace", session_get_id(sess), flow_direction_to_str(inject_dir));
|
||||
session_inc_stat((struct session *)sess, inject_dir, STAT_INJ_PKTS_FAIL, 1);
|
||||
session_inc_stat((struct session *)sess, inject_dir, STAT_INJECTED_PACKETS_FAILED, 1);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,27 +18,23 @@ static void update_session_stat(struct session *sess, struct packet *pkt)
|
||||
{
|
||||
if (sess)
|
||||
{
|
||||
enum session_stat stat_pkt;
|
||||
enum session_stat stat_byte;
|
||||
enum flow_direction dir = session_get_flow_direction(sess);
|
||||
int is_ctrl = packet_is_ctrl(pkt);
|
||||
switch (packet_get_action(pkt))
|
||||
{
|
||||
case PACKET_ACTION_DROP:
|
||||
stat_pkt = is_ctrl ? STAT_CTRL_PKTS_DROP : STAT_RAW_PKTS_DROP;
|
||||
stat_byte = is_ctrl ? STAT_CTRL_BYTES_DROP : STAT_RAW_BYTES_DROP;
|
||||
session_inc_stat(sess, dir, (is_ctrl ? STAT_CONTROL_PACKETS_DROPPED : STAT_RAW_PACKETS_DROPPED), 1);
|
||||
session_inc_stat(sess, dir, (is_ctrl ? STAT_CONTROL_BYTES_DROPPED : STAT_RAW_BYTES_DROPPED), packet_get_len(pkt));
|
||||
break;
|
||||
case PACKET_ACTION_FORWARD:
|
||||
session_inc_stat(sess, dir, (is_ctrl ? STAT_CONTROL_PACKETS_TRANSMITTED : STAT_RAW_PACKETS_TRANSMITTED), 1);
|
||||
session_inc_stat(sess, dir, (is_ctrl ? STAT_CONTROL_BYTES_TRANSMITTED : STAT_RAW_BYTES_TRANSMITTED), packet_get_len(pkt));
|
||||
break;
|
||||
stat_pkt = is_ctrl ? STAT_CTRL_PKTS_TX : STAT_RAW_PKTS_TX;
|
||||
stat_byte = is_ctrl ? STAT_CTRL_BYTES_TX : STAT_RAW_BYTES_TX;
|
||||
default:
|
||||
assert(0);
|
||||
break;
|
||||
}
|
||||
|
||||
session_inc_stat(sess, dir, stat_pkt, 1);
|
||||
session_inc_stat(sess, dir, stat_byte, packet_get_len(pkt));
|
||||
session_set_current_packet(sess, NULL);
|
||||
session_set_flow_direction(sess, FLOW_DIRECTION_NONE);
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <netinet/tcp.h>
|
||||
|
||||
#include "logo.h"
|
||||
#include "config.h"
|
||||
#include "timestamp.h"
|
||||
#include "id_generator.h"
|
||||
@@ -24,47 +25,57 @@ static void inject_tcp_rst(struct session *sess)
|
||||
{
|
||||
case 1:
|
||||
// recv SYN packet
|
||||
if (session_get_stat(sess, FLOW_DIRECTION_C2S, STAT_RAW_PKTS_RX) == 1 && session_get_stat(sess, FLOW_DIRECTION_S2C, STAT_RAW_PKTS_RX) == 0)
|
||||
if (session_get_stat(sess, FLOW_DIRECTION_C2S, STAT_RAW_PACKETS_RECEIVED) == 1 &&
|
||||
session_get_stat(sess, FLOW_DIRECTION_S2C, STAT_RAW_PACKETS_RECEIVED) == 0)
|
||||
{
|
||||
EXPECT_TRUE(stellar_inject_tcp_flags(sess, FLOW_DIRECTION_C2S, TH_RST | TH_ACK) == 0);
|
||||
EXPECT_TRUE(stellar_inject_tcp_flags(sess, FLOW_DIRECTION_S2C, TH_RST | TH_ACK) == -1);
|
||||
GMOCK_PLUGIN_MANAGER_LOG_DEBUG("=> inject TCP RST for session %u %s on recv SYN packet", session_get_id(sess), session_get_tuple_str(sess));
|
||||
GMOCK_PLUGIN_MANAGER_LOG_DEBUG("=> inject TCP RST for session %u %s on recv SYN packet",
|
||||
session_get_id(sess), session_get_tuple_str(sess));
|
||||
}
|
||||
break;
|
||||
case 2:
|
||||
// recv SYN-ACK packet
|
||||
if (session_get_stat(sess, FLOW_DIRECTION_C2S, STAT_RAW_PKTS_RX) == 1 && session_get_stat(sess, FLOW_DIRECTION_S2C, STAT_RAW_PKTS_RX) == 1)
|
||||
if (session_get_stat(sess, FLOW_DIRECTION_C2S, STAT_RAW_PACKETS_RECEIVED) == 1 &&
|
||||
session_get_stat(sess, FLOW_DIRECTION_S2C, STAT_RAW_PACKETS_RECEIVED) == 1)
|
||||
{
|
||||
EXPECT_TRUE(stellar_inject_tcp_flags(sess, FLOW_DIRECTION_C2S, TH_RST | TH_ACK) == 0);
|
||||
EXPECT_TRUE(stellar_inject_tcp_flags(sess, FLOW_DIRECTION_S2C, TH_RST | TH_ACK) == 0);
|
||||
GMOCK_PLUGIN_MANAGER_LOG_DEBUG("=> inject TCP RST for session %u %s on recv SYN-ACK packet", session_get_id(sess), session_get_tuple_str(sess));
|
||||
GMOCK_PLUGIN_MANAGER_LOG_DEBUG("=> inject TCP RST for session %u %s on recv SYN-ACK packet",
|
||||
session_get_id(sess), session_get_tuple_str(sess));
|
||||
}
|
||||
break;
|
||||
case 3:
|
||||
// recv sub-ACK packet
|
||||
if (session_get_stat(sess, FLOW_DIRECTION_C2S, STAT_RAW_PKTS_RX) == 2 && session_get_stat(sess, FLOW_DIRECTION_S2C, STAT_RAW_PKTS_RX) == 1)
|
||||
if (session_get_stat(sess, FLOW_DIRECTION_C2S, STAT_RAW_PACKETS_RECEIVED) == 2 &&
|
||||
session_get_stat(sess, FLOW_DIRECTION_S2C, STAT_RAW_PACKETS_RECEIVED) == 1)
|
||||
{
|
||||
EXPECT_TRUE(stellar_inject_tcp_flags(sess, FLOW_DIRECTION_C2S, TH_RST | TH_ACK) == 0);
|
||||
EXPECT_TRUE(stellar_inject_tcp_flags(sess, FLOW_DIRECTION_S2C, TH_RST | TH_ACK) == 0);
|
||||
GMOCK_PLUGIN_MANAGER_LOG_DEBUG("=> inject TCP RST for session %u %s on recv sub-ACK packet", session_get_id(sess), session_get_tuple_str(sess));
|
||||
GMOCK_PLUGIN_MANAGER_LOG_DEBUG("=> inject TCP RST for session %u %s on recv sub-ACK packet",
|
||||
session_get_id(sess), session_get_tuple_str(sess));
|
||||
}
|
||||
break;
|
||||
case 4:
|
||||
// recv C2S Payload
|
||||
if (session_get_stat(sess, FLOW_DIRECTION_C2S, STAT_INJ_PKTS_SUSS) == 0 && session_get_stat(sess, FLOW_DIRECTION_C2S, STAT_TCP_SEGS_RX) == 1)
|
||||
if (session_get_stat(sess, FLOW_DIRECTION_C2S, STAT_INJECTED_PACKETS_SUCCESS) == 0 &&
|
||||
session_get_stat(sess, FLOW_DIRECTION_C2S, STAT_TCP_SEGMENTS_RECEIVED) == 1)
|
||||
{
|
||||
EXPECT_TRUE(stellar_inject_tcp_flags(sess, FLOW_DIRECTION_C2S, TH_RST | TH_ACK) == 0);
|
||||
EXPECT_TRUE(stellar_inject_tcp_flags(sess, FLOW_DIRECTION_S2C, TH_RST | TH_ACK) == 0);
|
||||
GMOCK_PLUGIN_MANAGER_LOG_DEBUG("=> inject TCP RST for session %u %s on recv C2S first payload packet", session_get_id(sess), session_get_tuple_str(sess));
|
||||
GMOCK_PLUGIN_MANAGER_LOG_DEBUG("=> inject TCP RST for session %u %s on recv C2S first payload packet",
|
||||
session_get_id(sess), session_get_tuple_str(sess));
|
||||
}
|
||||
break;
|
||||
case 5:
|
||||
// recv S2C Payload
|
||||
if (session_get_stat(sess, FLOW_DIRECTION_S2C, STAT_INJ_PKTS_SUSS) == 0 && session_get_stat(sess, FLOW_DIRECTION_S2C, STAT_TCP_SEGS_RX) == 1)
|
||||
if (session_get_stat(sess, FLOW_DIRECTION_S2C, STAT_INJECTED_PACKETS_SUCCESS) == 0 &&
|
||||
session_get_stat(sess, FLOW_DIRECTION_S2C, STAT_TCP_SEGMENTS_RECEIVED) == 1)
|
||||
{
|
||||
EXPECT_TRUE(stellar_inject_tcp_flags(sess, FLOW_DIRECTION_C2S, TH_RST | TH_ACK) == 0);
|
||||
EXPECT_TRUE(stellar_inject_tcp_flags(sess, FLOW_DIRECTION_S2C, TH_RST | TH_ACK) == 0);
|
||||
GMOCK_PLUGIN_MANAGER_LOG_DEBUG("=> inject TCP RST for session %u %s on recv S2C first payload packet", session_get_id(sess), session_get_tuple_str(sess));
|
||||
GMOCK_PLUGIN_MANAGER_LOG_DEBUG("=> inject TCP RST for session %u %s on recv S2C first payload packet",
|
||||
session_get_id(sess), session_get_tuple_str(sess));
|
||||
}
|
||||
break;
|
||||
default:
|
||||
@@ -146,23 +157,98 @@ void plugin_manager_dispatch_packet(struct plugin_manager *mgr, struct packet *p
|
||||
static const char *log_config_file = "./conf/log.toml";
|
||||
static const char *stellar_config_file = "./conf/stellar.toml";
|
||||
|
||||
int gmock_main(int argc, char **argv)
|
||||
static void signal_handler(int signo)
|
||||
{
|
||||
if (signo == SIGINT)
|
||||
{
|
||||
STELLAR_LOG_STATE("SIGINT received, notify threads to exit !!!");
|
||||
ATOMIC_SET(&runtime->need_exit, 1);
|
||||
}
|
||||
|
||||
if (signo == SIGQUIT)
|
||||
{
|
||||
STELLAR_LOG_STATE("SIGQUIT received, notify threads to exit !!!");
|
||||
ATOMIC_SET(&runtime->need_exit, 1);
|
||||
}
|
||||
|
||||
if (signo == SIGTERM)
|
||||
{
|
||||
STELLAR_LOG_STATE("SIGTERM received, notify threads to exit !!!");
|
||||
ATOMIC_SET(&runtime->need_exit, 1);
|
||||
}
|
||||
|
||||
if (signo == SIGHUP)
|
||||
{
|
||||
STELLAR_LOG_STATE("SIGHUP received, reload log level !!!");
|
||||
log_reload_level(log_config_file);
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int run_time_ms = atoi(argv[0]);
|
||||
timestamp_update();
|
||||
EXPECT_TRUE(log_init(log_config_file) == 0);
|
||||
EXPECT_TRUE(stellar_load_config(stellar_config_file, config) == 0);
|
||||
EXPECT_TRUE(id_generator_init(config->dev_opts.base, config->dev_opts.offset) == 0);
|
||||
|
||||
signal(SIGINT, signal_handler);
|
||||
signal(SIGQUIT, signal_handler);
|
||||
signal(SIGTERM, signal_handler);
|
||||
signal(SIGHUP, signal_handler);
|
||||
|
||||
if (log_init(log_config_file) != 0)
|
||||
{
|
||||
STELLAR_LOG_ERROR("unable to init log");
|
||||
goto error_out;
|
||||
}
|
||||
STELLAR_LOG_STATE("start stellar (version: %s)\n %s", __stellar_version, logo_str);
|
||||
|
||||
if (stellar_load_config(stellar_config_file, config) != 0)
|
||||
{
|
||||
STELLAR_LOG_ERROR("unable to load config file");
|
||||
goto error_out;
|
||||
}
|
||||
stellar_print_config(config);
|
||||
STELLAR_LOG_DEBUG("sizeof(struct session) = %lu bytes", sizeof(struct session));
|
||||
|
||||
if (id_generator_init(config->dev_opts.base, config->dev_opts.offset) != 0)
|
||||
{
|
||||
STELLAR_LOG_ERROR("unable to init id generator");
|
||||
goto error_out;
|
||||
}
|
||||
|
||||
runtime->stat = stellar_stat_new(config->io_opts.nr_threads);
|
||||
EXPECT_TRUE(runtime->stat);
|
||||
if (runtime->stat == NULL)
|
||||
{
|
||||
STELLAR_LOG_ERROR("unable to create stellar stat");
|
||||
goto error_out;
|
||||
}
|
||||
|
||||
runtime->plug_mgr = plugin_manager_new();
|
||||
EXPECT_TRUE(runtime->plug_mgr);
|
||||
if (runtime->plug_mgr == NULL)
|
||||
{
|
||||
STELLAR_LOG_ERROR("unable to create plugin manager");
|
||||
goto error_out;
|
||||
}
|
||||
|
||||
runtime->packet_io = packet_io_new(&config->io_opts);
|
||||
EXPECT_TRUE(runtime->packet_io);
|
||||
EXPECT_TRUE(stellar_thread_init(runtime, config) == 0);
|
||||
EXPECT_TRUE(stellar_thread_run(runtime, config) == 0);
|
||||
if (runtime->packet_io == NULL)
|
||||
{
|
||||
STELLAR_LOG_ERROR("unable to create packet io");
|
||||
goto error_out;
|
||||
}
|
||||
|
||||
if (stellar_thread_init(runtime, config) != 0)
|
||||
{
|
||||
STELLAR_LOG_ERROR("unable to init thread context");
|
||||
goto error_out;
|
||||
}
|
||||
|
||||
if (stellar_thread_run(runtime, config) != 0)
|
||||
{
|
||||
STELLAR_LOG_ERROR("unable to create worker thread");
|
||||
goto error_out;
|
||||
}
|
||||
|
||||
runtime->stat_last_output_ts = timestamp_get_msec();
|
||||
for (int i = 0; i < run_time_ms; i++)
|
||||
while (!ATOMIC_READ(&runtime->need_exit))
|
||||
{
|
||||
timestamp_update();
|
||||
if (timestamp_get_msec() - runtime->stat_last_output_ts > 2000)
|
||||
@@ -172,27 +258,15 @@ int gmock_main(int argc, char **argv)
|
||||
}
|
||||
usleep(1000); // 1ms
|
||||
}
|
||||
ATOMIC_SET(&runtime->need_exit, 1);
|
||||
|
||||
error_out:
|
||||
stellar_thread_join(runtime, config);
|
||||
stellar_thread_clean(runtime, config);
|
||||
packet_io_free(runtime->packet_io);
|
||||
plugin_manager_free(runtime->plug_mgr);
|
||||
stellar_stat_free(runtime->stat);
|
||||
STELLAR_LOG_STATE("stellar exit !!!\n");
|
||||
log_free();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#if 1
|
||||
TEST(INJECT, TCP_RST)
|
||||
{
|
||||
char *argv[] = {"30000"}; // 30 seconds
|
||||
EXPECT_TRUE(gmock_main(1, argv) == 0);
|
||||
}
|
||||
#endif
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
::testing::InitGoogleTest(&argc, argv);
|
||||
return RUN_ALL_TESTS();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user