Update Stellar export header and add stellar_get_current_thread_index() function

This commit is contained in:
luwenpeng
2024-04-22 20:01:15 +08:00
parent 8a41a79f06
commit 74f0504d3d
59 changed files with 134 additions and 87 deletions

View File

@@ -1,12 +1,12 @@
#ifndef _PACKET_H
#define _PACKET_H
#ifndef _PACKET_PUB_H
#define _PACKET_PUB_H
#ifdef __cplusplus
extern "C"
{
#endif
#include "tuple.h"
#include "stellar/tuple.h"
enum layer_type
{

View File

@@ -1,5 +1,5 @@
#ifndef _SESSION_H
#define _SESSION_H
#ifndef _SESSION_PUB_H
#define _SESSION_PUB_H
#ifdef __cplusplus
extern "C"
@@ -8,7 +8,7 @@ extern "C"
#include <stdint.h>
#include "packet.h"
#include "stellar/packet.h"
enum session_state
{

17
include/stellar/stellar.h Normal file
View File

@@ -0,0 +1,17 @@
#ifndef _STELLAR_PUB_H
#define _STELLAR_PUB_H
#ifdef __cplusplus
extern "C"
{
#endif
#include <stdint.h>
uint8_t stellar_get_current_thread_index();
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -1,5 +1,5 @@
#ifndef _TUPLE_H
#define _TUPLE_H
#ifndef _TUPLE_PUB_H
#define _TUPLE_PUB_H
#ifdef __cplusplus
extern "C"

View File

@@ -4,6 +4,7 @@ add_subdirectory(timestamp)
add_subdirectory(tuple)
add_subdirectory(packet)
add_subdirectory(packet_io)
add_subdirectory(thread_idx)
add_subdirectory(id_generator)
add_subdirectory(ip_reassembly)
add_subdirectory(tcp_reassembly)

View File

@@ -9,7 +9,7 @@ extern "C"
// Duplicated Packet Filter for IPv4 Packet
#include "log.h"
#include "packet.h"
#include "packet_priv.h"
#define DUPLICATED_PACKET_FILTER_LOG_ERROR(format, ...) LOG_ERROR("duplicated packet filter", format, ##__VA_ARGS__)

View File

@@ -1,7 +1,7 @@
#include <gtest/gtest.h>
#include "duplicated_packet_filter.h"
#include "packet_private.h"
#include "packet_priv.h"
/******************************************************************************
* [Protocols in frame: eth:ethertype:ip:ipv6:tcp]

View File

@@ -7,7 +7,7 @@ extern "C"
#endif
#include "log.h"
#include "tuple.h"
#include "stellar/tuple.h"
#define EVICTED_SESSION_FILTER_LOG_ERROR(format, ...) LOG_ERROR("evicted session filter", format, ##__VA_ARGS__)

View File

@@ -1,4 +1,4 @@
add_library(id_generator id_generator.cpp)
target_include_directories(id_generator PUBLIC ${CMAKE_CURRENT_LIST_DIR})
target_include_directories(id_generator PUBLIC ${CMAKE_SOURCE_DIR}/src/stellar)
target_link_libraries(id_generator log)
target_link_libraries(id_generator log thread_idx)

View File

@@ -1,8 +1,9 @@
#include <time.h>
#include <string.h>
#include "macro.h"
#include "thread_idx.h"
#include "id_generator.h"
#include "stellar.h"
struct id_generator
{

View File

@@ -3,7 +3,7 @@
#include <sys/queue.h>
#include <assert.h>
#include "packet_private.h"
#include "packet_priv.h"
#include "crc32_hash.h"
#include "checksum.h"
#include "ipv4_utils.h"

View File

@@ -6,7 +6,7 @@ extern "C"
{
#endif
#include "packet.h"
#include "packet_priv.h"
#include "log.h"
#define IP_REASSEMBLE_DEBUG(format, ...) LOG_DEBUG("ip_reassembly", format, ##__VA_ARGS__)

View File

@@ -13,7 +13,7 @@ extern "C"
#include "ipv4_utils.h"
#include "ipv6_utils.h"
#include "ip_reassembly.h"
#include "packet_private.h"
#include "packet_priv.h"
static inline void packet_set_ipv4_src_addr(struct packet *pkt, uint32_t saddr)
{

View File

@@ -11,7 +11,7 @@
#include <linux/ppp_defs.h>
#include "uthash.h"
#include "packet_private.h"
#include "packet_priv.h"
#include "udp_utils.h"
#include "tcp_utils.h"
#include "ipv4_utils.h"

View File

@@ -1,5 +1,5 @@
#ifndef _PACKET_PRIVATE_H
#define _PACKET_PRIVATE_H
#ifndef _PACKET_PRIV_H
#define _PACKET_PRIV_H
#ifdef __cplusplus
extern "C"
@@ -10,8 +10,8 @@ extern "C"
#include <stdio.h>
#include "log.h"
#include "tuple.h"
#include "packet.h"
#include "stellar/tuple.h"
#include "stellar/packet.h"
#define PACKET_MAX_LAYERS 32
#define PACKET_LOG_ERROR(format, ...) LOG_ERROR("packet", format, ##__VA_ARGS__)

View File

@@ -2,7 +2,7 @@
#include <assert.h>
#include <string.h>
#include "packet_private.h"
#include "packet_priv.h"
#include "marsio.h"
/******************************************************************************

View File

@@ -1,7 +1,7 @@
#include <gtest/gtest.h>
#include <arpa/inet.h>
#include "packet_private.h"
#include "packet_priv.h"
/******************************************************************************
* Pcap file: 1-ETH_VLAN_VLAN_IP4_IP4_UDP.pcap

View File

@@ -1,6 +1,6 @@
#include <gtest/gtest.h>
#include "packet_private.h"
#include "packet_priv.h"
/******************************************************************************
* [Protocols in frame: eth:ethertype:ip:data]

View File

@@ -5,9 +5,9 @@
#include <stdlib.h>
#include <assert.h>
#include "stellar.h"
#include "macro.h"
#include "packet_priv.h"
#include "file_scan.h"
#include "packet_private.h"
#include "lock_free_queue.h"
#include "dumpfile_io.h"

View File

@@ -1,8 +1,9 @@
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdint.h>
#include "stellar.h"
#include "macro.h"
#include "lock_free_queue.h"
struct lock_free_queue

View File

@@ -4,9 +4,9 @@
#include <string.h>
#include <netinet/ether.h>
#include "stellar.h"
#include "macro.h"
#include "marsio.h"
#include "packet_private.h"
#include "packet_priv.h"
#include "marsio_io.h"
struct marsio_io

View File

@@ -7,8 +7,8 @@ extern "C"
#endif
#include "log.h"
#include "packet.h"
#include "stellar.h"
#include "macro.h"
#include "packet_priv.h"
#define PACKET_IO_LOG_STATE(format, ...) LOG_STATE("packet_io", format, ##__VA_ARGS__)
#define PACKET_IO_LOG_ERROR(format, ...) LOG_ERROR("packet_io", format, ##__VA_ARGS__)

View File

@@ -1,3 +1,3 @@
add_library(plugin_manager plugin_manager.cpp)
target_include_directories(plugin_manager PUBLIC ${CMAKE_CURRENT_LIST_DIR})
target_link_libraries(plugin_manager session_manager)
target_link_libraries(plugin_manager session_manager thread_idx)

View File

@@ -1,6 +1,7 @@
#include <assert.h>
#include "plugin_manager.h"
#include "session_private.h"
#include "session_priv.h"
#include "thread_idx.h"
struct plugin_manager
{
@@ -42,7 +43,7 @@ void plugin_manager_dispatch_session(struct plugin_manager *mgr, struct session
struct tcp_segment *seg;
enum session_state state = session_get_state(sess);
enum session_type type = session_get_type(sess);
PLUGIN_MANAGER_LOG_DEBUG("=> plugin dispatch session: %u %s %s %s", session_get_id(sess), session_get_tuple_str(sess), session_type_to_str(type), session_state_to_str(state));
PLUGIN_MANAGER_LOG_DEBUG("=> thread [%d] plugin dispatch session: %u %s %s %s", stellar_get_current_thread_index(), session_get_id(sess), session_get_tuple_str(sess), session_type_to_str(type), session_state_to_str(state));
// session_print(sess);
if (packet_is_ctrl(pkt))

View File

@@ -6,7 +6,7 @@ extern "C"
{
#endif
#include "session.h"
#include "session_priv.h"
#define PLUGIN_MANAGER_LOG_ERROR(format, ...) LOG_ERROR("plugin manager", format, ##__VA_ARGS__)
#define PLUGIN_MANAGER_LOG_DEBUG(format, ...) LOG_DEBUG("plugin manager", format, ##__VA_ARGS__)

View File

@@ -1,6 +1,6 @@
#include <assert.h>
#include "session_private.h"
#include "session_priv.h"
#include "tcp_utils.h"
#include "tcp_reassembly.h"

View File

@@ -2,7 +2,7 @@
#include <stdlib.h>
#include <assert.h>
#include "stellar.h"
#include "macro.h"
#include "tcp_utils.h"
#include "udp_utils.h"
#include "id_generator.h"

View File

@@ -6,7 +6,7 @@ extern "C"
{
#endif
#include "session_private.h"
#include "session_priv.h"
#include "log.h"
#define SESSION_LOG_ERROR(format, ...) LOG_ERROR("session", format, ##__VA_ARGS__)

View File

@@ -6,7 +6,7 @@ extern "C"
{
#endif
#include "session_private.h"
#include "session_priv.h"
struct session_pool;
struct session_pool *session_pool_new(uint64_t count);

View File

@@ -1,5 +1,5 @@
#ifndef _SESSION_PRIVATE_H
#define _SESSION_PRIVATE_H
#ifndef _SESSION_PRIV_H
#define _SESSION_PRIV_H
#ifdef __cplusplus
extern "C"
@@ -7,11 +7,11 @@ extern "C"
#endif
#include "list.h"
#include "tuple.h"
#include "packet_private.h"
#include "packet_priv.h"
#include "timeout.h"
#include "uthash.h"
#include "session.h"
#include "stellar/tuple.h"
#include "stellar/session.h"
#include "tcp_reassembly.h"
#include "session_manager.h"

View File

@@ -6,7 +6,7 @@ extern "C"
{
#endif
#include "session_private.h"
#include "session_priv.h"
struct session_table;
struct session_table *session_table_new();

View File

@@ -6,7 +6,7 @@ extern "C"
{
#endif
#include "session_private.h"
#include "session_priv.h"
struct session_timer;
struct session_timer *session_timer_new(uint64_t now);

View File

@@ -7,7 +7,7 @@ extern "C"
#endif
#include "log.h"
#include "session_private.h"
#include "session_priv.h"
#define SESSION_TRANSITION_LOG_INFO(format, ...) LOG_INFO("session transition", format, ##__VA_ARGS__)

View File

@@ -1,6 +1,6 @@
#include <gtest/gtest.h>
#include "session_private.h"
#include "session_priv.h"
#include "session_manager.h"
#include "ipv4_utils.h"

View File

@@ -1,9 +1,9 @@
#include <gtest/gtest.h>
#include "session_private.h"
#include "session_priv.h"
#include "session_manager.h"
#include "stellar.h"
#include "macro.h"
#include "ipv4_utils.h"
#include "test_packets.h"

View File

@@ -1,9 +1,9 @@
#include <gtest/gtest.h>
#include "session_private.h"
#include "session_priv.h"
#include "session_manager.h"
#include "stellar.h"
#include "macro.h"
#include "ipv4_utils.h"
#include "test_packets.h"

View File

@@ -1,6 +1,6 @@
#include <gtest/gtest.h>
#include "session_private.h"
#include "session_priv.h"
#include "session_manager.h"
#include "ipv4_utils.h"

View File

@@ -1,6 +1,6 @@
#include <gtest/gtest.h>
#include "session_private.h"
#include "session_priv.h"
#define SESSION_KEY_IPV4_TCP(name) \
struct tuple6 name; \

View File

@@ -1,7 +1,7 @@
// TCP state machine test: active -> closing
#include <gtest/gtest.h>
#include "session_private.h"
#include "session_priv.h"
#include "session_manager.h"
#include "tcp_utils.h"

View File

@@ -1,7 +1,7 @@
// TCP state machine test: init -> opening
#include <gtest/gtest.h>
#include "session_private.h"
#include "session_priv.h"
#include "session_manager.h"
#include "ipv4_utils.h"

View File

@@ -1,7 +1,7 @@
// TCP state machine test: init -> opening -> active -> closing -> closed
#include <gtest/gtest.h>
#include "session_private.h"
#include "session_priv.h"
#include "session_manager.h"
#include "test_packets.h"

View File

@@ -1,7 +1,7 @@
// TCP state machine test: opening -> active
#include <gtest/gtest.h>
#include "session_private.h"
#include "session_priv.h"
#include "session_manager.h"
#include "test_packets.h"

View File

@@ -1,7 +1,7 @@
// TCP state machine test: opening -> closing
#include <gtest/gtest.h>
#include "session_private.h"
#include "session_priv.h"
#include "session_manager.h"
#include "tcp_utils.h"

View File

@@ -1,7 +1,7 @@
// UDP state machine test: init -> opening -> active -> closing
#include <gtest/gtest.h>
#include "session_private.h"
#include "session_priv.h"
#include "session_manager.h"
#include "test_packets.h"

View File

@@ -1,7 +1,7 @@
// UDP state machine test: init -> opening -> closing
#include <gtest/gtest.h>
#include "session_private.h"
#include "session_priv.h"
#include "session_manager.h"
#include "test_packets.h"

View File

@@ -1,6 +1,6 @@
#include <gtest/gtest.h>
#include "session_private.h"
#include "session_priv.h"
#include "session_manager.h"
#include "test_packets.h"

View File

@@ -1,6 +1,6 @@
#include <gtest/gtest.h>
#include "session_private.h"
#include "session_priv.h"
#include "session_manager.h"
#include "ipv4_utils.h"

View File

@@ -1,6 +1,6 @@
#include <gtest/gtest.h>
#include "session_private.h"
#include "session_priv.h"
#include "session_manager.h"
#include "ipv4_utils.h"

View File

@@ -1,6 +1,6 @@
#include <gtest/gtest.h>
#include "session_private.h"
#include "session_priv.h"
#include "session_manager.h"
#include "test_packets.h"

View File

@@ -1,5 +1,5 @@
add_executable(stellar config.cpp stat.cpp stellar.cpp)
target_link_libraries(stellar timestamp plugin_manager session_manager ip_reassembly packet_io)
target_link_libraries(stellar timestamp plugin_manager session_manager ip_reassembly packet_io thread_idx)
target_link_libraries(stellar pthread fieldstat4 toml)
install(TARGETS stellar RUNTIME DESTINATION bin COMPONENT Program)

View File

@@ -1,15 +1,13 @@
#ifndef _STELLAR_H
#define _STELLAR_H
#ifndef _MACRO_H
#define _MACRO_H
#ifdef __cplusplus
extern "C"
{
#endif
#include <stdint.h>
#define MAX_THREAD_NUM 256
#define RX_BURST_MAX 32
#define MAX_THREAD_NUM 256 // limit by id_generator
#define ATOMIC_INC(x) __atomic_fetch_add(x, 1, __ATOMIC_RELAXED)
#define ATOMIC_DEC(x) __atomic_fetch_sub(x, 1, __ATOMIC_RELAXED)
@@ -28,11 +26,6 @@ static __attribute__((__used__)) const char *__stellar_version = STELLAR_GIT_VER
static __attribute__((__used__)) const char *__stellar_version = "Unknown";
#endif
static inline uint8_t stellar_get_current_thread_index()
{
return 0;
}
#ifdef __cplusplus
}
#endif

View File

@@ -10,11 +10,11 @@
#include "logo.h"
#include "stat.h"
#include "stellar.h"
#include "config.h"
#include "packet_private.h"
#include "packet_priv.h"
#include "packet_io.h"
#include "timestamp.h"
#include "thread_idx.h"
#include "id_generator.h"
#include "ip_reassembly.h"
#include "session_manager.h"
@@ -159,7 +159,6 @@ static void *work_thread(void *arg)
{
int nr_recv;
uint64_t now = 0;
uint16_t thr_idx = 0;
char thd_name[16] = {0};
void *plugin_ctx = NULL;
struct packet *pkt = NULL;
@@ -170,7 +169,8 @@ static void *work_thread(void *arg)
struct stellar_thread *thread = (struct stellar_thread *)arg;
struct ip_reassembly *ip_reass = thread->ip_mgr;
struct session_manager *sess_mgr = thread->sess_mgr;
thr_idx = thread->idx;
uint16_t thr_idx = thread->idx;
stellar_set_current_thread_index(thr_idx);
snprintf(thd_name, sizeof(thd_name), "stellar:%d", thr_idx);
prctl(PR_SET_NAME, (unsigned long long)thd_name, NULL, NULL, NULL);

View File

@@ -0,0 +1,4 @@
add_library(thread_idx thread_idx.cpp)
target_include_directories(thread_idx PUBLIC ${CMAKE_CURRENT_LIST_DIR})
target_include_directories(thread_idx PUBLIC ${CMAKE_SOURCE_DIR}/include)
target_link_libraries(thread_idx)

View File

@@ -0,0 +1,13 @@
#include "thread_idx.h"
static thread_local uint8_t __thread_id = 0;
uint8_t stellar_get_current_thread_index()
{
return __thread_id;
}
void stellar_set_current_thread_index(uint8_t idx)
{
__thread_id = idx;
}

View File

@@ -0,0 +1,17 @@
#ifndef _THREAD_IDX_H
#define _THREAD_IDX_H
#ifdef __cplusplus
extern "C"
{
#endif
#include "stellar/stellar.h"
void stellar_set_current_thread_index(uint8_t idx);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -1,5 +1,6 @@
add_library(timestamp timestamp.cpp)
target_include_directories(timestamp PUBLIC ${CMAKE_CURRENT_LIST_DIR})
target_include_directories(timestamp PUBLIC ${CMAKE_SOURCE_DIR}/src/stellar)
target_link_libraries(timestamp)
add_subdirectory(test)

View File

@@ -1,5 +1,6 @@
#include <time.h>
#include "macro.h"
#include "timestamp.h"
// 1 s = 1000 ms
@@ -23,9 +24,6 @@ struct timestamp
uint64_t ts_in_sec;
} g_timestamp;
#define ATOMIC_SET(x, y) __atomic_store_n(x, y, __ATOMIC_RELAXED)
#define ATOMIC_READ(x) __atomic_load_n(x, __ATOMIC_RELAXED)
void timestamp_update()
{
/*

View File

@@ -1,6 +1,6 @@
#include <gtest/gtest.h>
#include "tuple.h"
#include "stellar/tuple.h"
TEST(TUPLE, TUPLE2)
{

View File

@@ -1,7 +1,7 @@
#include <string.h>
#include <stdio.h>
#include "tuple.h"
#include "stellar/tuple.h"
#include "crc32_hash.h"
uint32_t tuple2_hash(const struct tuple2 *tuple)