diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index e042cdd..6b1e060 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt @@ -1,6 +1,7 @@ -add_library(common src/addr_tuple4.cpp src/session_table.cpp src/raw_packet.cpp src/ctrl_packet.cpp src/bfd.cpp src/utils.cpp src/g_vxlan.cpp) +add_library(common src/addr_tuple4.cpp src/session_table.cpp src/raw_packet.cpp src/ctrl_packet.cpp src/bfd.cpp src/utils.cpp src/g_vxlan.cpp src/log.cpp) target_link_libraries(common PUBLIC cjson) target_link_libraries(common PUBLIC mrzcpd) +target_link_libraries(common PUBLIC MESA_handle_logger) target_include_directories(common PUBLIC ${CMAKE_CURRENT_LIST_DIR}/include) diff --git a/common/include/log.h b/common/include/log.h index 0cdcfdf..9f991de 100644 --- a/common/include/log.h +++ b/common/include/log.h @@ -7,24 +7,40 @@ extern "C" #endif #include +#include -#define LOG_DEBUG(format, ...) \ - { \ - fprintf(stdout, "DEBUG " format "\n", ##__VA_ARGS__); \ - fflush(stdout); \ - } +extern void *g_default_logger; -#define LOG_INFO(format, ...) \ - { \ - fprintf(stdout, "INFO " format "\n", ##__VA_ARGS__); \ - fflush(stdout); \ - } +int LOG_INIT(const char *profile); +void LOG_CLOSE(void); +void LOG_RELOAD(void); -#define LOG_ERROR(format, ...) \ - { \ - fprintf(stderr, "ERROR " format "\n", ##__VA_ARGS__); \ - fflush(stderr); \ - } +#define LOG_DEBUG(format, ...) \ + do \ + { \ + if (g_default_logger) \ + MESA_handle_runtime_log(g_default_logger, RLOG_LV_DEBUG, __FUNCTION__, format, ##__VA_ARGS__); \ + else \ + fprintf(stdout, "DEBUG " format "\n", ##__VA_ARGS__); \ + } while (0) + +#define LOG_INFO(format, ...) \ + do \ + { \ + if (g_default_logger) \ + MESA_handle_runtime_log(g_default_logger, RLOG_LV_INFO, __FUNCTION__, format, ##__VA_ARGS__); \ + else \ + fprintf(stdout, "INFO " format "\n", ##__VA_ARGS__); \ + } while (0) + +#define LOG_ERROR(format, ...) \ + do \ + { \ + if (g_default_logger) \ + MESA_handle_runtime_log(g_default_logger, RLOG_LV_FATAL, __FUNCTION__, format, ##__VA_ARGS__); \ + else \ + fprintf(stderr, "ERROR " format "\n", ##__VA_ARGS__); \ + } while (0) #ifdef __cpluscplus } diff --git a/common/src/log.cpp b/common/src/log.cpp new file mode 100644 index 0000000..bd43959 --- /dev/null +++ b/common/src/log.cpp @@ -0,0 +1,33 @@ +#include "log.h" + +void *g_default_logger = NULL; + +// return 0 : success +// return -1 : error +int LOG_INIT(const char *profile) +{ + if (0 != MESA_handle_runtime_log_creation(profile)) + { + fprintf(stderr, "FATAL: unable to create runtime logger\n"); + return -1; + } + + g_default_logger = MESA_create_runtime_log_handle("sce", RLOG_LV_DEBUG); + if (g_default_logger == NULL) + { + fprintf(stderr, "FATAL: unable to create log handle\n"); + return -1; + } + + return 0; +} + +void LOG_CLOSE(void) +{ + MESA_handle_runtime_log_destruction(); +} + +void LOG_RELOAD(void) +{ + MESA_handle_runtime_log_reconstruction(NULL); +} diff --git a/common/src/raw_packet.cpp b/common/src/raw_packet.cpp index 2871eb1..03f64be 100644 --- a/common/src/raw_packet.cpp +++ b/common/src/raw_packet.cpp @@ -82,9 +82,7 @@ static const void *parse_ipv4(struct raw_pkt_parser *handler, const void *data, static const void *parse_ipv6(struct raw_pkt_parser *handler, const void *data, size_t length, enum layer_type this_type); static const void *parse_tcp(struct raw_pkt_parser *handler, const void *data, size_t length, enum layer_type this_type); static const void *parse_udp(struct raw_pkt_parser *handler, const void *data, size_t length, enum layer_type this_type); -static const void *parse_ppp(struct raw_pkt_parser *handler, const void *data, size_t length, enum layer_type this_type); static const void *parse_pppoe_ses(struct raw_pkt_parser *handler, const void *data, size_t length, enum layer_type this_type); -// static const void *parse_hdlc(struct raw_pkt_parser *handler, const void *data, size_t length, enum layer_type this_type); static const void *parse_vxlan(struct raw_pkt_parser *handler, const void *data, size_t length, enum layer_type this_type); static const void *parse_vlan8021q(struct raw_pkt_parser *handler, const void *data, size_t length, enum layer_type this_type); static const void *parse_gtpv1_u(struct raw_pkt_parser *handler, const void *data, size_t length, enum layer_type this_type); @@ -768,39 +766,6 @@ static const void *parse_udp(struct raw_pkt_parser *handler, const void *data, s } } -static const void *parse_ppp(struct raw_pkt_parser *handler, const void *data, size_t length, enum layer_type this_type) -{ - if (length < PPP_HDRLEN) - { - LOG_ERROR("%s: pkt_trace_id: %lu, this_layer: %s, err: data not enough", LOG_TAG_RAWPKT, handler->pkt_trace_id, layer_type2str(this_type)); - return data; - } - - if (raw_packet_parser_status(handler, data, this_type) == PARSE_STATUS_STOP) - { - return data; - } - - uint16_t next_proto = PPP_PROTOCOL(data); - uint16_t hdr_len = PPP_HDRLEN; - const void *data_next_layer = (const char *)data + hdr_len; - size_t data_next_length = length - hdr_len; - - LOG_DEBUG("%s: pkt_trace_id: %lu, this_layer: %s, payload_len: [%lu/%lu]", LOG_TAG_RAWPKT, handler->pkt_trace_id, layer_type2str(this_type), data_next_length, length); - switch (next_proto) - { - case PPP_IP: - // TODO - return parse_ipv4(handler, data_next_layer, data_next_length, LAYER_TYPE_IPV4); - case PPP_IPV6: - // TODO - return parse_ipv6(handler, data_next_layer, data_next_length, LAYER_TYPE_IPV6); - default: - LOG_ERROR("%s: pkt_trace_id: %lu, this_layer: %s, stop parse next protocol %d", LOG_TAG_RAWPKT, handler->pkt_trace_id, layer_type2str(this_type), next_proto); - return data_next_layer; - } -} - static const void *parse_pppoe_ses(struct raw_pkt_parser *handler, const void *data, size_t length, enum layer_type this_type) { if (length < 8) @@ -836,42 +801,6 @@ static const void *parse_pppoe_ses(struct raw_pkt_parser *handler, const void *d } } -static const void *parse_hdlc(struct raw_pkt_parser *handler, const void *data, size_t length, enum layer_type this_type) -{ - if (length < 4) - { - LOG_ERROR("%s: pkt_trace_id: %lu, this_layer: %s, err: data not enough", LOG_TAG_RAWPKT, handler->pkt_trace_id, layer_type2str(this_type)); - return data; - } - - if (raw_packet_parser_status(handler, data, this_type) == PARSE_STATUS_STOP) - { - return data; - } - - uint16_t next_proto = ntohs(*(const uint16_t *)((const char *)data + 2)); - uint16_t hdr_len = 4; - const void *data_next_layer = (const char *)data + hdr_len; - size_t data_next_length = length - hdr_len; - - LOG_DEBUG("%s: pkt_trace_id: %lu, this_layer: %s, payload_len: [%lu/%lu]", LOG_TAG_RAWPKT, handler->pkt_trace_id, layer_type2str(this_type), data_next_length, length); - // HDLC的协议字段与以太网的类似,对于IPv4,为0x0800 - switch (next_proto) - { - // ETHER_TYPE_IPV4 0x0800 - case 0x0800: - // TODO - return parse_ipv4(handler, data_next_layer, data_next_length, LAYER_TYPE_IPV4); - // ETHER_TYPE_IPV6 0x86DD - case 0x86DD: - // TODO - return parse_ipv6(handler, data_next_layer, data_next_length, LAYER_TYPE_IPV6); - default: - LOG_ERROR("%s: pkt_trace_id: %lu, this_layer: %s, stop parse next protocol %d", LOG_TAG_RAWPKT, handler->pkt_trace_id, layer_type2str(this_type), next_proto); - return data_next_layer; - } -} - static const void *parse_vxlan(struct raw_pkt_parser *handler, const void *data, size_t length, enum layer_type this_type) { if (length < sizeof(struct vxlan_hdr)) @@ -893,9 +822,6 @@ static const void *parse_vxlan(struct raw_pkt_parser *handler, const void *data, LOG_DEBUG("%s: pkt_trace_id: %lu, this_layer: %s, payload_len: [%lu/%lu]", LOG_TAG_RAWPKT, handler->pkt_trace_id, layer_type2str(this_type), data_next_length, length); // TESTED return parse_ether(handler, data_next_layer, data_next_length, LAYER_TYPE_ETHER); - - // TODO HDLC - // TODO PPP } static const void *parse_vlan8021q(struct raw_pkt_parser *handler, const void *data, size_t length, enum layer_type this_type) diff --git a/conf/CMakeLists.txt b/conf/CMakeLists.txt index 5beca73..02c9078 100644 --- a/conf/CMakeLists.txt +++ b/conf/CMakeLists.txt @@ -1 +1,2 @@ -install(FILES sce.conf DESTINATION conf COMPONENT Profile) \ No newline at end of file +install(FILES sce.conf DESTINATION conf COMPONENT Profile) +install(FILES zlog.conf DESTINATION conf COMPONENT Profile) \ No newline at end of file diff --git a/conf/sce.conf b/conf/sce.conf index cda9261..3afb9b0 100644 --- a/conf/sce.conf +++ b/conf/sce.conf @@ -23,7 +23,7 @@ redis_port_range=6379 [packet_io] # bypass_all_traffic:1 NF2NF and SF2SF -# bypass_all_traffic:1 NF2SF and SF2NF +# bypass_all_traffic:2 NF2SF and SF2NF bypass_all_traffic=0 rx_burst_max=128 app_symbol=sce diff --git a/conf/zlog.conf b/conf/zlog.conf new file mode 100644 index 0000000..12e7558 --- /dev/null +++ b/conf/zlog.conf @@ -0,0 +1,12 @@ +# kill -s SIGHUP "pid" + +[global] +default format = "%d(%c), %V, %F, %U, %m%n" + +[levels] +DEBUG=10 +INFO=20 +FATAL=30 + +[rules] +sce.debug "./log/sce.log.%d(%F)"; diff --git a/platform/CMakeLists.txt b/platform/CMakeLists.txt index 6989130..fc1afcb 100644 --- a/platform/CMakeLists.txt +++ b/platform/CMakeLists.txt @@ -1,7 +1,6 @@ add_library(platform src/policy.cpp src/health_check.cpp src/sce.cpp src/packet_io.cpp) target_link_libraries(platform PUBLIC common) target_link_libraries(platform PUBLIC pthread) -target_link_libraries(platform PUBLIC MESA_handle_logger) target_link_libraries(platform PUBLIC MESA_prof_load) target_link_libraries(platform PUBLIC maatframe) target_link_libraries(platform PUBLIC mrzcpd) diff --git a/platform/src/main.cpp b/platform/src/main.cpp index 1be46c6..1927b38 100644 --- a/platform/src/main.cpp +++ b/platform/src/main.cpp @@ -1,11 +1,21 @@ #include #include +#include #include #include "sce.h" #include "log.h" #include "utils.h" +static void sig_handler(int signo) +{ + if (signo == SIGHUP) + { + LOG_INFO("%s: recv SIGHUP, reload zlog.conf", LOG_TAG_SCE); + LOG_RELOAD(); + } +} + static void *worker_thread_cycle(void *arg) { struct thread_ctx *thread_ctx = (struct thread_ctx *)arg; @@ -43,9 +53,22 @@ int main(int argc, char **argv) { const char *profile = "./conf/sce.conf"; + if (LOG_INIT("./conf/zlog.conf") == -1) + { + return -1; + } + + if (signal(SIGHUP, sig_handler) == SIG_ERR) + { + LOG_ERROR("%s: unable to register SIGHUP signal handler, error %d: %s", LOG_TAG_SCE, errno, strerror(errno)); + LOG_CLOSE(); + return -1; + } + struct sce_ctx *ctx = sce_ctx_create(profile); if (ctx == NULL) { + LOG_CLOSE(); return -1; } @@ -85,5 +108,7 @@ error_out: } sce_ctx_destory(ctx); + LOG_CLOSE(); + return 0; } diff --git a/platform/src/packet_io.cpp b/platform/src/packet_io.cpp index 9aa3238..88873e3 100644 --- a/platform/src/packet_io.cpp +++ b/platform/src/packet_io.cpp @@ -1001,7 +1001,7 @@ forward: static void forward_all_sf_packet_to_nf(struct packet_io *handle, marsio_buff_t *rx_buff, int thread_seq, void *ctx) { struct thread_ctx *thread = (struct thread_ctx *)ctx; - struct global_metrics *g_metrics = thread->ref_metrics; + // struct global_metrics *g_metrics = thread->ref_metrics; // vxlan decode struct g_vxlan *g_vxlan_hdr = NULL; @@ -1145,7 +1145,7 @@ static int handle_session_closing(struct metadata *meta, struct ctrl_pkt_parser static int handle_session_active(struct metadata *meta, struct ctrl_pkt_parser *parser, int thread_seq, void *ctx) { struct thread_ctx *thread = (struct thread_ctx *)ctx; - struct global_metrics *g_metrics = thread->ref_metrics; + // struct global_metrics *g_metrics = thread->ref_metrics; struct session_node *node = session_table_search_by_id(thread->session_table, meta->session_id); if (node)