TSG-8230 PacketAdapter支持输出json格式的日志,并支持动态调整日志级别

This commit is contained in:
luwenpeng
2021-10-27 20:10:06 +03:00
parent f567aa1d3d
commit f934023b40
12 changed files with 132 additions and 78 deletions

View File

@@ -27,7 +27,7 @@ extern "C"
} gtp_info_t;
int decode_gtp(gtp_info_t *packet, const uint8_t *data, uint32_t len);
void dump_gtp_info(uint32_t pkt_id, gtp_info_t *packet);
int dump_gtp_info(gtp_info_t *packet, char *buff, size_t size);
#ifdef __cpluscplus
}

View File

@@ -46,7 +46,7 @@ extern "C"
} ipv4_info_t;
int decode_ipv4(ipv4_info_t *packet, const uint8_t *data, uint32_t len);
void dump_ipv4_info(uint32_t pkt_id, ipv4_info_t *packet);
int dump_ipv4_info(ipv4_info_t *packet, char *buff, size_t size);
#ifdef __cpluscplus
}

View File

@@ -49,7 +49,7 @@ extern "C"
} ipv6_info_t;
int decode_ipv6(ipv6_info_t *packet, const uint8_t *data, uint32_t len);
void dump_ipv6_info(uint32_t pkt_id, ipv6_info_t *packet);
int dump_ipv6_info(ipv6_info_t *packet, char *buff, size_t size);
#ifdef __cpluscplus
}

View File

@@ -37,7 +37,7 @@ extern "C"
} tcp_info_t;
int decode_tcp(tcp_info_t *packet, const uint8_t *data, uint32_t len);
void dump_tcp_info(uint32_t pkt_id, tcp_info_t *packet);
int dump_tcp_info(tcp_info_t *packet, char *buff, size_t size);
#ifdef __cpluscplus
}

View File

@@ -31,7 +31,7 @@ extern "C"
} udp_info_t;
int decode_udp(udp_info_t *packet, const uint8_t *data, uint32_t len);
void dump_udp_info(uint32_t pkt_id, udp_info_t *packet);
int dump_udp_info(udp_info_t *packet, char *buff, size_t size);
#ifdef __cpluscplus
}

View File

@@ -12,27 +12,24 @@ extern "C"
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/stat.h>
#include <arpa/inet.h>
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define IP_GET_RAW_VER(raw_pkt) ((((raw_pkt)[0] & 0xf0) >> 4))
#define PRINT_FILE_INFO 0
#if (PRINT_FILE_INFO)
#define LOG_DEBUG(format, ...) \
fprintf(stdout, "[%s-%s()-%05d] " format "\n", __FILE__, __FUNCTION__, __LINE__, ##__VA_ARGS__);
{ \
fprintf(stdout, format "\n", ##__VA_ARGS__); \
fflush(stdout); \
}
#define LOG_ERROR(format, ...) \
fprintf(stderr, "[%s-%s()-%05d] " format "\n", __FILE__, __FUNCTION__, __LINE__, ##__VA_ARGS__);
#else
#define LOG_DEBUG(format, ...) \
fprintf(stdout, format "\n", ##__VA_ARGS__);
#define LOG_ERROR(format, ...) \
fprintf(stderr, format "\n", ##__VA_ARGS__);
#endif
{ \
fprintf(stderr, format "\n", ##__VA_ARGS__); \
fflush(stderr); \
}
#ifdef __cpluscplus
}

View File

@@ -47,10 +47,10 @@ int decode_gtp(gtp_info_t *packet, const uint8_t *data, uint32_t len)
return 0;
}
void dump_gtp_info(uint32_t pkt_id, gtp_info_t *packet)
int dump_gtp_info(gtp_info_t *packet, char *buff, size_t size)
{
LOG_DEBUG("id: %u, gtp_info: {hdr_len: %u, data_len: %u}",
pkt_id,
return snprintf(buff, size,
"{\"hdr_len\":%u,\"data_len\":%u}",
packet->hdr_len,
packet->payload_len);
}

View File

@@ -56,10 +56,10 @@ int decode_ipv4(ipv4_info_t *packet, const uint8_t *data, uint32_t len)
return 0;
}
void dump_ipv4_info(uint32_t pkt_id, ipv4_info_t *packet)
int dump_ipv4_info(ipv4_info_t *packet, char *buff, size_t size)
{
LOG_DEBUG("id: %u, ipv4_info: {src_addr: %s, dst_addr: %s, hdr_len: %u, opt_len: %u, data_len: %u}",
pkt_id,
return snprintf(buff, size,
"{\"src_addr\":\"%s\",\"dst_addr\":\"%s\",\"hdr_len\":%u,\"opts_len\":%u,\"data_len\":%u}",
packet->src_addr,
packet->dst_addr,
packet->hdr_len,

View File

@@ -44,10 +44,10 @@ int decode_ipv6(ipv6_info_t *packet, const uint8_t *data, uint32_t len)
return 0;
}
void dump_ipv6_info(uint32_t pkt_id, ipv6_info_t *packet)
int dump_ipv6_info(ipv6_info_t *packet, char *buff, size_t size)
{
LOG_DEBUG("id: %u, ipv6_info: {src_addr: %s, dst_addr: %s, hdr_len: %u, data_len: %u}",
pkt_id,
return snprintf(buff, size,
"{\"src_addr\":\"%s\",\"dst_addr\":\"%s\",\"hdr_len\":%u,\"data_len\":%u}",
packet->src_addr,
packet->dst_addr,
packet->hdr_len,

View File

@@ -40,10 +40,10 @@ int decode_tcp(tcp_info_t *packet, const uint8_t *data, uint32_t len)
return 0;
}
void dump_tcp_info(uint32_t pkt_id, tcp_info_t *packet)
int dump_tcp_info(tcp_info_t *packet, char *buff, size_t size)
{
LOG_DEBUG("id: %u, tcp_info: {src_port: %u, dst_port: %u, hdr_len: %u, opt_len: %u, data_len:%u}",
pkt_id,
return snprintf(buff, size,
"{\"src_port\":%u,\"dst_port\":%u,\"hdr_len\":%u,\"opt_len\":%u,\"data_len\":%u}",
packet->src_port,
packet->dst_port,
packet->hdr_len,

View File

@@ -37,10 +37,10 @@ int decode_udp(udp_info_t *packet, const uint8_t *data, uint32_t len)
return 0;
}
void dump_udp_info(uint32_t pkt_id, udp_info_t *packet)
int dump_udp_info(udp_info_t *packet, char *buff, size_t size)
{
LOG_DEBUG("id: %u, udp_info: {src_port: %u, dst_port: %u, hdr_len: %u, data_len: %u}",
pkt_id,
return snprintf(buff, size,
"{\"src_port\":%u,\"dst_port\":%u,\"hdr_len\":%u,\"data_len\":%u}",
packet->src_port,
packet->dst_port,
packet->hdr_len,

View File

@@ -48,10 +48,17 @@ typedef struct pkt_paser_s
union_info_t internal;
} pkt_paser_t;
static int is_dump_packet_info = 0;
static void dump_info(pkt_paser_t *parser)
{
uint32_t pkt_id = parser->raw.id;
LOG_DEBUG("raw: {id: %u, protocol: %u, hook: %u, mark: %u, indev: %u, outdev: %u, phys_indev: %u, phys_outdev: %u, src_addr: %s, data_len: %u}",
char buff[4096] = {0};
size_t size = sizeof(buff);
size_t len = 0;
len += snprintf(buff + len, size - len, "{");
len += snprintf(buff + len, size - len,
"\"raw_info\":{\"id\":%u,\"protocol\":%u,\"hook\":%u,\"mark\":%u,\"indev\":%u,\"outdev\":%u,\"phys_indev\":%u,\"phys_outdev\":%u,\"src_addr\":\"%s\",\"data_len\":%u}",
parser->raw.id,
parser->raw.protocol,
parser->raw.hook,
@@ -66,44 +73,56 @@ static void dump_info(pkt_paser_t *parser)
// external
if (parser->external.ipv4.hdr)
{
dump_ipv4_info(pkt_id, &(parser->external.ipv4));
len += snprintf(buff + len, size - len, ",\"external_ipv4\":");
len += dump_ipv4_info(&(parser->external.ipv4), buff + len, size - len);
}
if (parser->external.ipv6.hdr)
{
dump_ipv6_info(pkt_id, &(parser->external.ipv6));
len += snprintf(buff + len, size - len, ",\"external_ipv6\":");
len += dump_ipv6_info(&(parser->external.ipv6), buff + len, size - len);
}
if (parser->external.udp.hdr)
{
dump_udp_info(pkt_id, &(parser->external.udp));
len += snprintf(buff + len, size - len, ",\"external_udp\":");
len += dump_udp_info(&(parser->external.udp), buff + len, size - len);
}
if (parser->external.tcp.hdr)
{
dump_tcp_info(pkt_id, &(parser->external.tcp));
len += snprintf(buff + len, size - len, ",\"external_tcp\":");
len += dump_tcp_info(&(parser->external.tcp), buff + len, size - len);
}
// gtp
if (parser->gtp.hdr)
{
dump_gtp_info(pkt_id, &(parser->gtp));
len += snprintf(buff + len, size - len, ",\"gtp\":");
len += dump_gtp_info(&(parser->gtp), buff + len, size - len);
}
// internal
if (parser->internal.ipv4.hdr)
{
dump_ipv4_info(pkt_id, &(parser->internal.ipv4));
len += snprintf(buff + len, size - len, ",\"internal_ipv4\":");
len += dump_ipv4_info(&(parser->internal.ipv4), buff + len, size - len);
}
if (parser->internal.ipv6.hdr)
{
dump_ipv6_info(pkt_id, &(parser->internal.ipv6));
len += snprintf(buff + len, size - len, ",\"internal_ipv6\":");
len += dump_ipv6_info(&(parser->internal.ipv6), buff + len, size - len);
}
if (parser->internal.udp.hdr)
{
dump_udp_info(pkt_id, &(parser->internal.udp));
len += snprintf(buff + len, size - len, ",\"internal_udp\":");
len += dump_udp_info(&(parser->internal.udp), buff + len, size - len);
}
if (parser->internal.tcp.hdr)
{
dump_tcp_info(pkt_id, &(parser->internal.tcp));
len += snprintf(buff + len, size - len, ",\"internal_tcp\":");
len += dump_tcp_info(&(parser->internal.tcp), buff + len, size - len);
}
len += snprintf(buff + len, size - len, "}");
LOG_DEBUG("%s", buff);
}
static int decode_ip_tcp_udp(union_info_t *parser, const uint8_t *data, uint32_t len)
@@ -276,8 +295,11 @@ static int packet_handler_cb(struct nfq_q_handle *qh, struct nfgenmsg *nfmsg, st
offest += parser.external.udp.hdr_len;
offest += parser.gtp.hdr_len;
if (is_dump_packet_info)
{
dump_info(&parser);
LOG_DEBUG("Offset : %d", offest);
}
uint8_t *inject_data = parser.raw.payload + offest;
uint32_t inject_data_len = parser.raw.payload_len - offest;
@@ -312,13 +334,31 @@ end:
return nfq_set_verdict(qh, packet_id, NF_ACCEPT, 0, NULL);
}
static void sig_handler(int signo)
{
if (signo == SIGUSR1)
{
is_dump_packet_info = 1;
LOG_ERROR("received SIGUSR1, enable dump packet info");
}
if (signo == SIGUSR2)
{
is_dump_packet_info = 0;
LOG_ERROR("received SIGUSR2, disable dump packet info");
}
}
static void usage(char *cmd)
{
fprintf(stderr, "USAGE: %s [OPTIONS]\n", cmd);
fprintf(stderr, " -v -- show version\n");
fprintf(stderr, " -i id -- set queue id\n");
fprintf(stderr, " -d -- run daemon\n");
fprintf(stderr, " -p -- dump packet info\n");
fprintf(stderr, " -h -- show help\n");
fprintf(stderr, "Signal: \n");
fprintf(stderr, " kill -s SIGUSR1 `pidof %s` -- enable dump packet info\n", cmd);
fprintf(stderr, " kill -s SIGUSR2 `pidof %s` -- disable dump packet info\n", cmd);
}
/*
@@ -337,7 +377,9 @@ int main(int argc, char **argv)
struct nfq_q_handle *q_handle;
char buf[65535] __attribute__((aligned));
while ((opt = getopt(argc, argv, "vi:dh")) != -1)
is_dump_packet_info = 0;
while ((opt = getopt(argc, argv, "vi:dph")) != -1)
{
switch (opt)
{
@@ -355,6 +397,9 @@ int main(int argc, char **argv)
case 'd':
run_daemon();
break;
case 'p':
is_dump_packet_info = 1;
break;
case 'h': /* fall through */
default:
usage(argv[0]);
@@ -364,6 +409,18 @@ int main(int argc, char **argv)
LOG_DEBUG("Using queue: %d", queue);
if (signal(SIGUSR1, sig_handler) == SIG_ERR)
{
LOG_ERROR("Failed at signal(SIGUSR1), %d: %s", errno, strerror(errno));
goto error;
}
if (signal(SIGUSR2, sig_handler) == SIG_ERR)
{
LOG_ERROR("Failed at signal(SIGUSR2), %d: %s", errno, strerror(errno));
goto error;
}
handle = nfq_open();
if (handle == NULL)
{