optimizate inject packt test case

This commit is contained in:
luwenpeng
2024-06-27 15:07:54 +08:00
parent 83bffdd008
commit da9fb7cd11
40 changed files with 904 additions and 1004 deletions

View File

@@ -4,7 +4,7 @@
#include "log.h"
#include "macro.h"
#include "times.h"
#include "stellar_utils.h"
#include "stellar_core.h"
#include "id_generator.h"
#define ID_GENERATOR_LOG_ERROR(format, ...) LOG_ERROR("id generator", format, ##__VA_ARGS__)

View File

@@ -19,8 +19,8 @@
#include "packet_parse.h"
#include "lock_free_queue.h"
#define PACKET_IO_LOG_STATE(format, ...) LOG_STATE("dumpfile io", format, ##__VA_ARGS__)
#define PACKET_IO_LOG_ERROR(format, ...) LOG_ERROR("dumpfile io", format, ##__VA_ARGS__)
#define PACKET_IO_LOG_STATE(format, ...) LOG_STATE("dumpfile", format, ##__VA_ARGS__)
#define PACKET_IO_LOG_ERROR(format, ...) LOG_ERROR("dumpfile", format, ##__VA_ARGS__)
#define MAX_PACKET_QUEUE_SIZE (4096 * 1000)

View File

@@ -11,7 +11,7 @@
#include "packet_utils.h"
#include "packet_parse.h"
#define PACKET_IO_LOG_ERROR(format, ...) LOG_ERROR("marsio io", format, ##__VA_ARGS__)
#define PACKET_IO_LOG_ERROR(format, ...) LOG_ERROR("marsio", format, ##__VA_ARGS__)
struct marsio_io
{

View File

@@ -2,7 +2,7 @@
#include "plugin_manager.h"
#include "session_utils.h"
#include "packet_utils.h"
#include "stellar_utils.h"
#include "stellar_core.h"
#include "stellar/utils.h"
#include "stellar/session_exdata.h"
#include "stellar/session_mq.h"

View File

@@ -25,9 +25,6 @@ struct tcp_half
struct tcp_segment in_order; // current packet in order segment
uint32_t in_order_ref; // reference count of current packet in order segment
uint32_t inject_inc_seq_offset; // inject packet base on current packet increase seq offset
uint32_t inject_inc_ack_offset; // inject packet base on current packet increase ack offset
uint32_t seq; // current packet sequence number
uint32_t ack; // current packet ack number
uint16_t len; // current packet payload length

View File

@@ -298,8 +298,6 @@ static void tcp_update(struct session_manager *mgr, struct session *sess, enum f
{
half->isn = tcp_hdr_get_seq(hdr);
}
half->inject_inc_ack_offset = 0;
half->inject_inc_seq_offset = 0;
half->flags = flags;
half->history |= flags;
half->seq = tcp_hdr_get_seq(hdr);

View File

@@ -1,4 +1,4 @@
set(SOURCE stellar_config.cpp stellar_stat.cpp stellar.cpp inject.cpp)
set(SOURCE stellar_config.cpp stellar_stat.cpp stellar_core.cpp)
set(LIBRARY times plugin_manager session_manager ip_reassembly packet_io pthread fieldstat4 toml)
add_library(stellar_core STATIC ${SOURCE})

View File

@@ -1,227 +0,0 @@
#include <errno.h>
#include <assert.h>
#include "log.h"
#include "times.h"
#include "packet_io.h"
#include "packet_def.h"
#include "packet_build.h"
#include "packet_utils.h"
#include "session_def.h"
#include "session_utils.h"
#include "stellar_utils.h"
#include "tcp_reassembly.h"
#include "session_manager.h"
#define INJECT_PACKET_LOG_ERROR(format, ...) LOG_ERROR("inject packet", format, ##__VA_ARGS__)
#define INJECT_PACKE_LOG_DEBUG(format, ...) LOG_DEBUG("inject packet", format, ##__VA_ARGS__)
static inline void calc_tcp_seq_ack(const struct session *sess, enum flow_direction inject_dir, uint32_t *seq, uint32_t *ack, uint8_t flags, uint16_t len)
{
/*
* +--------+ current packet +---------+ C2S RST +--------+
* | |----------------->| |----------------->| |
* | Client | | Stellar | | Server |
* | |<-----------------| |<-----------------| |
* +--------+ S2C RST +---------+ +--------+
*
* for example: current packet is C2S
*
* inject direction == current direction (inject C2S RST)
* seq = current_packet_seq
* ack = current_packet_ack
*
* inject direction != current direction (inject S2C RST)
* seq = current_packet_ack
* ack = current_packet_seq + current_packet_payload_len
* or if current packet is a SYN-ACK packet
* seq = current_packet_seq
* ack = current_packet_ack + current_packet_payload_len + 1
*/
enum flow_direction curr_dir = session_get_current_flow_direction(sess);
struct tcp_half *tcp_curr_half = (struct tcp_half *)&sess->tcp_halfs[curr_dir];
if (inject_dir == curr_dir)
{
*seq = uint32_add(tcp_curr_half->seq, tcp_curr_half->inject_inc_seq_offset);
*ack = tcp_curr_half->ack;
tcp_curr_half->inject_inc_seq_offset += len;
// inject RST packer after FIN packer, seq should be increased by 1
tcp_curr_half->inject_inc_seq_offset += (flags & TH_FIN) ? 1 : 0;
}
else
{
*seq = uint32_add(tcp_curr_half->ack, tcp_curr_half->inject_inc_ack_offset);
*ack = uint32_add(tcp_curr_half->seq, tcp_curr_half->len + (tcp_curr_half->flags & TH_SYN ? 1 : 0));
tcp_curr_half->inject_inc_ack_offset += len;
// inject RST packer after FIN packer, ack should be increased by 1
tcp_curr_half->inject_inc_ack_offset += (flags & TH_FIN) ? 1 : 0;
}
}
// return 1 if success
// return 0 if failed
int stellar_send_packet(struct stellar *st, struct packet *pkt)
{
uint64_t time_ms = stellar_get_monotonic_time_msec();
uint16_t thr_idx = stellar_get_current_thread_index();
struct packet_io *packet_io = stellar_get_packet_io(st);
struct session_manager *sess_mgr = stellar_get_session_manager(st);
session_manager_record_duplicated_packet(sess_mgr, pkt, time_ms);
if (packet_get_origin_ctx(pkt))
{
// TODO
assert(0);
packet_io_egress(packet_io, thr_idx, pkt, 1);
return 1;
}
else
{
return packet_io_inject(packet_io, thr_idx, pkt, 1);
}
}
static int inject_tcp_packet(struct stellar *st, const struct session *sess, enum flow_direction inject_dir, uint8_t tcp_flags, const char *tcp_payload, uint16_t tcp_payload_len)
{
#define TCP_FLAGS_LOG_FORMAT "URG:%d, ACK:%d, PSH:%d, RST:%d, SYN:%d, FIN:%d"
#define TCP_FLAGS_LOG_VALUE(flags) \
(((flags) & TH_URG) ? 1 : 0), (((flags) & TH_ACK) ? 1 : 0), \
(((flags) & TH_PUSH) ? 1 : 0), (((flags) & TH_RST) ? 1 : 0), \
(((flags) & TH_SYN) ? 1 : 0), (((flags) & TH_FIN) ? 1 : 0)
if (session_get_type(sess) != SESSION_TYPE_TCP)
{
session_inc_stat((struct session *)sess, inject_dir, STAT_INJECTED_PACKETS_FAILED, 1);
INJECT_PACKET_LOG_ERROR("session %ld %s is not a TCP session, cannot inject TCP packet (" TCP_FLAGS_LOG_FORMAT ", payload len:%d)",
session_get_id(sess), session_get0_readable_addr(sess),
TCP_FLAGS_LOG_VALUE(tcp_flags), tcp_payload_len);
return 0;
}
const struct packet *origin_pkt = session_get_first_packet(sess, inject_dir);
if (origin_pkt == NULL)
{
session_inc_stat((struct session *)sess, inject_dir, STAT_INJECTED_PACKETS_FAILED, 1);
INJECT_PACKET_LOG_ERROR("session %ld %s has no %s first packet, cannot inject TCP packet (" TCP_FLAGS_LOG_FORMAT ", payload len:%d)",
session_get_id(sess), session_get0_readable_addr(sess),
flow_direction_to_str(inject_dir), TCP_FLAGS_LOG_VALUE(tcp_flags), tcp_payload_len);
return 0;
}
uint32_t tcp_seq = 0;
uint32_t tcp_ack = 0;
calc_tcp_seq_ack(sess, inject_dir, &tcp_seq, &tcp_ack, tcp_flags, tcp_payload_len);
struct packet *new_pkt = imitate_tcp_packet(origin_pkt, tcp_seq, tcp_ack, tcp_flags, tcp_payload, tcp_payload_len);
if (new_pkt == NULL)
{
INJECT_PACKET_LOG_ERROR("session %ld %s build TCP %s packet (" TCP_FLAGS_LOG_FORMAT ", payload len:%d) failed",
session_get_id(sess), session_get0_readable_addr(sess),
flow_direction_to_str(inject_dir), TCP_FLAGS_LOG_VALUE(tcp_flags), tcp_payload_len);
session_inc_stat((struct session *)sess, inject_dir, STAT_INJECTED_PACKETS_FAILED, 1);
return 0;
}
int pkt_len = packet_get_raw_len(new_pkt);
if (stellar_send_packet(st, new_pkt) == 1)
{
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, pkt_len);
INJECT_PACKE_LOG_DEBUG("session %ld %s inject TCP %s packet (" TCP_FLAGS_LOG_FORMAT ", payload len:%d) success",
session_get_id(sess), session_get0_readable_addr(sess),
flow_direction_to_str(inject_dir), TCP_FLAGS_LOG_VALUE(tcp_flags), tcp_payload_len);
return pkt_len;
}
else
{
INJECT_PACKET_LOG_ERROR("session %ld %s inject TCP %s packet (" TCP_FLAGS_LOG_FORMAT ", payload len:%d) failed, packet I/O nospace",
session_get_id(sess), session_get0_readable_addr(sess),
flow_direction_to_str(inject_dir), TCP_FLAGS_LOG_VALUE(tcp_flags), tcp_payload_len);
session_inc_stat((struct session *)sess, inject_dir, STAT_INJECTED_PACKETS_FAILED, 1);
return 0;
}
}
static int inject_udp_packet(struct stellar *st, const struct session *sess, enum flow_direction inject_dir, const char *payload, uint16_t len)
{
if (session_get_type(sess) != SESSION_TYPE_UDP)
{
session_inc_stat((struct session *)sess, inject_dir, STAT_INJECTED_PACKETS_FAILED, 1);
INJECT_PACKET_LOG_ERROR("session %ld %s is not a UDP session, cannot inject UDP packet (payload len:%d)",
session_get_id(sess), session_get0_readable_addr(sess), len);
return 0;
}
const struct packet *pkt = session_get_first_packet(sess, inject_dir);
if (pkt == NULL)
{
session_inc_stat((struct session *)sess, inject_dir, STAT_INJECTED_PACKETS_FAILED, 1);
INJECT_PACKET_LOG_ERROR("session %ld %s has no %s first packet, cannot inject UDP packet (payload len:%d)",
session_get_id(sess), session_get0_readable_addr(sess),
flow_direction_to_str(inject_dir), len);
return 0;
}
struct packet *new_pkt = imitate_udp_packet(pkt, payload, len);
if (new_pkt == 0)
{
INJECT_PACKET_LOG_ERROR("session %ld %s build UDP %s packet (payload len:%d) failed",
session_get_id(sess), session_get0_readable_addr(sess),
flow_direction_to_str(inject_dir), len);
session_inc_stat((struct session *)sess, inject_dir, STAT_INJECTED_PACKETS_FAILED, 1);
return 0;
}
int pkt_len = packet_get_raw_len(new_pkt);
if (stellar_send_packet(st, new_pkt) == 1)
{
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, pkt_len);
INJECT_PACKE_LOG_DEBUG("session %ld %s inject UDP %s packet (payload len:%d) success",
session_get_id(sess), session_get0_readable_addr(sess),
flow_direction_to_str(inject_dir), len);
return pkt_len;
}
else
{
INJECT_PACKET_LOG_ERROR("session %ld %s inject UDP %s packet (payload len:%d) failed, packet I/O nospace",
session_get_id(sess), session_get0_readable_addr(sess),
flow_direction_to_str(inject_dir), len);
session_inc_stat((struct session *)sess, inject_dir, STAT_INJECTED_PACKETS_FAILED, 1);
return 0;
}
}
/******************************************************************************
* Public API
******************************************************************************/
int stellar_inject_tcp_rst(struct stellar *st, const struct session *sess, enum flow_direction inject_dir)
{
return inject_tcp_packet(st, sess, inject_dir, TH_RST | TH_ACK, NULL, 0);
}
int stellar_inject_tcp_fin(struct stellar *st, const struct session *sess, enum flow_direction inject_dir)
{
return inject_tcp_packet(st, sess, inject_dir, TH_FIN | TH_ACK, NULL, 0);
}
int stellar_inject_tcp_payload(struct stellar *st, const struct session *sess, enum flow_direction inject_dir, const char *payload, uint16_t len)
{
return inject_tcp_packet(st, sess, inject_dir, TH_ACK, payload, len);
}
int stellar_inject_udp_payload(struct stellar *st, const struct session *sess, enum flow_direction inject_dir, const char *payload, uint16_t len)
{
return inject_udp_packet(st, sess, inject_dir, payload, len);
}
int stellar_inject_ctrl_msg(struct stellar *st, const struct session *sess, const struct sids *sids, const char *msg, uint16_t len)
{
// TODO
return 0;
}

View File

@@ -1,6 +1,6 @@
#include "stellar_utils.h"
#include "stellar_core.h"
int main(int argc, char **argv)
{
return stellar_main(argc, argv);
return stellar_run(argc, argv);
}

View File

@@ -4,20 +4,23 @@
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <stdlib.h>
#include <pthread.h>
#include <sys/prctl.h>
#include "log.h"
#include "logo.h"
#include "times.h"
#include "packet_io.h"
#include "packet_def.h"
#include "packet_utils.h"
#include "session_utils.h"
#include "id_generator.h"
#include "stellar_stat.h"
#include "stellar_utils.h"
#include "stellar_core.h"
#include "stellar_config.h"
#include "plugin_manager.h"
#include "session_manager.h"
#define STELLAR_LOG_STATE(format, ...) LOG_STATE("stellar", format, ##__VA_ARGS__)
#define STELLAR_LOG_ERROR(format, ...) LOG_ERROR("stellar", format, ##__VA_ARGS__)
@@ -407,7 +410,7 @@ static void stellar_thread_join(struct stellar_runtime *runtime, struct stellar_
}
}
int stellar_main(int argc, char **argv)
int stellar_run(int argc, char **argv)
{
static struct stellar st = {0};
struct stellar_runtime *runtime = &st.runtime;
@@ -536,3 +539,24 @@ uint16_t stellar_get_current_thread_index()
{
return __thread_id;
}
// only send user crafted packet, can't send packet which come from network
void stellar_send_crafted_packet(struct stellar *st, struct packet *pkt)
{
uint64_t time_ms = stellar_get_monotonic_time_msec();
uint16_t thr_idx = stellar_get_current_thread_index();
struct packet_io *packet_io = stellar_get_packet_io(st);
struct session_manager *sess_mgr = stellar_get_session_manager(st);
session_manager_record_duplicated_packet(sess_mgr, pkt, time_ms);
if (packet_get_origin_ctx(pkt))
{
// TODO
abort();
packet_io_egress(packet_io, thr_idx, pkt, 1);
}
else
{
packet_io_inject(packet_io, thr_idx, pkt, 1);
}
}

View File

@@ -13,7 +13,9 @@ struct plugin_manager_schema *stellar_get_plugin_manager(const struct stellar *s
// TODO fix plugin manager, delete this function
void stellar_set_plugin_manger(struct stellar *st, struct plugin_manager_schema *plug_mgr);
int stellar_main(int argc, char **argv);
// only send user crafted packet, can't send packet which come from network
void stellar_send_crafted_packet(struct stellar *st, struct packet *pkt);
int stellar_run(int argc, char **argv);
#ifdef __cplusplus
}

View File

@@ -14,6 +14,8 @@ global:
packet_get_raw_len;
packet_get_payload;
packet_get_payload_len;
imitate_tcp_packet;
imitate_udp_packet;
session_exdata_free;
stellar_session_exdata_new_index;
@@ -50,12 +52,8 @@ global:
stellar_packet_plugin_register;
stellar_polling_plugin_register;
stellar_get_current_thread_index;
stellar_inject_tcp_rst;
stellar_inject_tcp_fin;
stellar_inject_tcp_payload;
stellar_inject_udp_payload;
stellar_inject_ctrl_msg;
stellar_main;
stellar_send_crafted_packet;
stellar_run;
local: *;
};