diff --git a/bin/dns_decoder.toml b/bin/dns_decoder.toml index 01957e3..d60dc6b 100644 --- a/bin/dns_decoder.toml +++ b/bin/dns_decoder.toml @@ -14,4 +14,7 @@ stat_interval_time_s=5 stat_output="metrics/dns_decoder_local_stat.json" [decoder.dns.test] +perf_worker_thread_num=1 +write_result_enable="no" commit_result_enable="yes" +decode_resource_record_enable="no" diff --git a/bin/dns_decoder_local_perf_hist.sh b/bin/dns_decoder_local_perf_hist.sh new file mode 100644 index 0000000..11a48e2 --- /dev/null +++ b/bin/dns_decoder_local_perf_hist.sh @@ -0,0 +1,27 @@ +#!/bin/bash + +ARGC=$# +EXPORTER=/opt/tsg/framework/bin/fieldstat_exporter.py +TARGET=/opt/tsg/sapp/metrics/dns_decoder_perf_test.json + +case $1 in + -h|--help) + echo "$0" + echo "Usage: [exporter] [target]" + echo " exporter: path of exporter, default is $EXPORTER" + echo " target: path of target file, default is $TARGET" + exit 0 + ;; +esac + +case $ARGC in + 1) + EXPORTER=$1 + ;; + 2) + EXPORTER=$1 + TARGET=$2 + ;; +esac +#new version +python3 $EXPORTER local -b 0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,0.99 -f summary -j $TARGET \ No newline at end of file diff --git a/src/dns_decoder.cpp b/src/dns_decoder.cpp index f726d53..70fba40 100644 --- a/src/dns_decoder.cpp +++ b/src/dns_decoder.cpp @@ -1171,6 +1171,12 @@ void dns_decoder_session_transaction_add(struct dns_decoder_context *per_ss_ctx, void dns_decoder_session_transaction_del(struct dns_decoder_context *per_ss_ctx, struct dns_transaction *current_trans) { + if(per_ss_ctx->trans_list_head==NULL || current_trans==NULL) + { + assert(per_ss_ctx->trans_list_num==0); + return ; + } + DL_DELETE(per_ss_ctx->trans_list_head, current_trans); per_ss_ctx->trans_list_num--; @@ -1224,7 +1230,6 @@ void dns_decoder_entry(struct session *ss, uint8_t *payload, size_t payload_sz, data_msg->n_authority_rr=dns_hdr.aucount; data_msg->n_additional_rr=dns_hdr.adcount; - data_msg->type=((dns_hdr.qr==0) ? DNS_MESSAGE_QUERY : DNS_MESSAGE_RESPONSE); if(data_msg->n_question==1) { size_t tag_offset=4; @@ -1245,8 +1250,10 @@ void dns_decoder_entry(struct session *ss, uint8_t *payload, size_t payload_sz, struct dns_decoder_context *per_ss_ctx=(struct dns_decoder_context *)per_session_ctx; int32_t message_id=data_msg->trans_identifier_id; + enum dns_message_type msg_type=((dns_hdr.qr==0) ? DNS_MESSAGE_QUERY : DNS_MESSAGE_RESPONSE); + HASH_FIND_INT(per_ss_ctx->trans_hash, &message_id, current_trans); - if(current_trans!=NULL && data_msg->type==DNS_MESSAGE_QUERY) + if(current_trans!=NULL && msg_type==DNS_MESSAGE_QUERY) { dns_message_transaction_publish(ss, DNS_MESSAGE_TRANSACTION_END, plugin_env->dns.topic_id, current_trans->trans_idx); dns_decoder_session_transaction_del(per_ss_ctx, current_trans); @@ -1273,6 +1280,7 @@ void dns_decoder_entry(struct session *ss, uint8_t *payload, size_t payload_sz, } data_msg->ss=ss; + data_msg->type=msg_type; data_msg->payload=payload; data_msg->payload_sz=payload_sz; data_msg->payload_offset=payload_offset; @@ -1282,12 +1290,12 @@ void dns_decoder_entry(struct session *ss, uint8_t *payload, size_t payload_sz, session_mq_publish_message(ss, plugin_env->dns.topic_id, data_msg); size_t tag_offset=4; - const char *type=((data_msg->type==DNS_MESSAGE_RESPONSE) ? "response" : "query"); + const char *type=((msg_type==DNS_MESSAGE_RESPONSE) ? "response" : "query"); const char *tag_key[tag_offset]={TAG_KEY_IP_VERSION, TAG_KEY_IP_PROTOCOL, TAG_KEY_MESSAGE_TYPE, TAG_KEY_MESSAGE_STATUS}; const char *tag_value[tag_offset]={ip_version, ip_protocol, type, "normal"}; dns_decoder_local_file_counter_incby(plugin_env, LOCAL_STAT_COUNTER_SEND, tag_key, tag_value, tag_offset, 1, session_get_current_thread_id(ss)); - if(data_msg->type==DNS_MESSAGE_RESPONSE) + if(msg_type==DNS_MESSAGE_RESPONSE) { dns_message_transaction_publish(ss, DNS_MESSAGE_TRANSACTION_END, plugin_env->dns.topic_id, current_trans->trans_idx); dns_decoder_session_transaction_del(per_ss_ctx, current_trans); diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 5d8f85e..766d204 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -3,15 +3,19 @@ aux_source_directory(${PROJECT_SOURCE_DIR}/deps/toml DEPS_SRC) add_library(${PROJECT_NAME}_test_plug SHARED dns_decoder_test.cpp ${DEPS_SRC}) add_dependencies(${PROJECT_NAME}_test_plug ${PROJECT_NAME}) -target_link_libraries(${PROJECT_NAME}_test_plug MESA_prof_load cjson) +target_link_libraries(${PROJECT_NAME}_test_plug cjson) set_target_properties(${PROJECT_NAME}_test_plug PROPERTIES PREFIX "") -add_executable(dns_decoder_perf_test dns_decoder_perf_main.cpp dns_decoder_perf_dummy.cpp) -target_link_libraries(dns_decoder_perf_test fieldstat4 pthread) +add_executable(dns_decoder_perf_test dns_decoder_perf_main.cpp dns_decoder_perf_dummy.cpp ${DEPS_SRC} dns_decoder_test.cpp ${PROJECT_SOURCE_DIR}/src/dns_decoder.cpp) +target_link_libraries(dns_decoder_perf_test fieldstat4 pthread cjson) set(TEST_RUN_DIR ${CMAKE_CURRENT_BINARY_DIR}/sapp) set(TEST_MAIN ${TEST_RUN_DIR}/plugin_test_main) + +# copy perf main +add_test(NAME COPY_PERF_TEST_MAIN COMMAND sh -c "cp ${CMAKE_BINARY_DIR}/test/dns_decoder_perf_test ${TEST_RUN_DIR}/dns_decoder_perf_test") + # assemble test env add_test(NAME INSTALL_TEST_MAIN COMMAND sh -c "rpm -i ${CMAKE_CURRENT_SOURCE_DIR}/env/sapp-4.3.57.16ea514-1.el8.x86_64.rpm --prefix=${CMAKE_CURRENT_BINARY_DIR}/sapp --force --nodeps") add_test(NAME INSTALL_STELLAR COMMAND sh -c "rpm -i ${CMAKE_CURRENT_SOURCE_DIR}/env/stellar-on-sapp-2.1.1.7875675-1.el8.x86_64.rpm --prefix=${CMAKE_CURRENT_BINARY_DIR}/ --force --nodeps") diff --git a/test/case/perf/1.perf.response.pcap b/test/case/perf/1.perf.response.pcap new file mode 100644 index 0000000..536fb63 Binary files /dev/null and b/test/case/perf/1.perf.response.pcap differ diff --git a/test/dns_decoder_perf_dummy.cpp b/test/dns_decoder_perf_dummy.cpp index dc498d2..3da2ab1 100644 --- a/test/dns_decoder_perf_dummy.cpp +++ b/test/dns_decoder_perf_dummy.cpp @@ -32,6 +32,9 @@ struct stellar { int topic_idx; int plugin_idx; + int worker_thread_num; + int udp_topic_id; + int tcp_steam_topic_id; struct message_topic topic[MAX_MESSGEA_TOPIC_NUM]; struct stellar_plugin plugin[MAX_STELLAR_PLUGIN_NUM]; }; @@ -44,6 +47,9 @@ struct session struct message_topic *topic; struct stellar_plugin *plugin; struct stellar_packet *curr_msg; + struct session_addr addr; + char *readable_addr; + enum session_addr_type addr_type; }; const char *session_get0_current_payload(struct session *ss, size_t *payload_sz) @@ -98,6 +104,11 @@ int session_mq_publish_message(struct session *ss, int topic_id, void *msg) ss->topic[topic_id].plugin_on_msg_cb[i](ss, topic_id, msg, ss->plugin[plugin_id].per_session_ctx, ss->plugin[plugin_id].plugin_env); } + if(ss->topic[topic_id].msg_free_cb!=NULL) + { + ss->topic[topic_id].msg_free_cb(ss, msg, ss->topic[topic_id].msg_free_arg); + } + return 0; } @@ -116,6 +127,11 @@ int session_mq_publish_message_by_name(struct session *ss, const char *topic_nam return 0; } +int stellar_session_mq_destroy_topic(struct stellar *st, int topic_id) +{ + return 0; +} + int stellar_session_plugin_register(struct stellar *st, session_ctx_new_func session_ctx_new, session_ctx_free_func session_ctx_free, void *plugin_env) { int plugin_id=st->plugin_idx++; @@ -126,10 +142,13 @@ int stellar_session_plugin_register(struct stellar *st, session_ctx_new_func ses return plugin_id; } -struct session *stellar_session_new(struct stellar *st, int tid) +struct session *stellar_session_new(struct stellar *st, struct stellar_packet *cur_pkt, int tid) { struct session *ss=(struct session *)malloc(sizeof(struct session)); ss->tid=tid; + ss->addr_type=cur_pkt->addr_type; + ss->readable_addr=cur_pkt->readable_addr; + memcpy(&(ss->addr), &(cur_pkt->addr), sizeof(struct session_addr)); ss->plugin=st->plugin; ss->plugin_idx=st->plugin_idx; @@ -137,14 +156,47 @@ struct session *stellar_session_new(struct stellar *st, int tid) ss->topic=st->topic; ss->topic_idx=st->topic_idx; - for(int i=0; iplugin_idx; i++) + for(int i=0; iplugin_idx; i++) { - st->plugin[i].per_session_ctx=st->plugin[i].session_ctx_new(ss, st->plugin[i].plugin_env); + ss->plugin[i].per_session_ctx=ss->plugin[i].session_ctx_new(ss, ss->plugin[i].plugin_env); } return ss; } +int session_get_current_thread_id(struct session *ss) +{ + return ss->tid; +} + +struct session_addr *session_get0_addr(struct session *ss, enum session_addr_type *addr_type) +{ + *addr_type=ss->addr_type; + return &(ss->addr); +} + + +void stellar_session_plugin_dettach_current_session(struct session *ss) +{ + +} + +int session_is_innermost(struct session *ss, uint64_t *flag) +{ + *flag=0; + return 1; +} + +enum session_state session_get_current_state(struct session *ss) +{ + return SESSION_STATE_ACTIVE; +} + +const char *session_get0_readable_addr(struct session *ss) +{ + return ss->readable_addr; +} + void stellar_session_free(struct session *ss) { for(int i=0; iplugin_idx; i++) @@ -155,12 +207,21 @@ void stellar_session_free(struct session *ss) free(ss); } - struct stellar *stellar_init(int worker_thread_num) { struct stellar *st=(struct stellar *)malloc(sizeof(struct stellar)); st->topic_idx=0; st->plugin_idx=0; + st->worker_thread_num=worker_thread_num; + + st->udp_topic_id=stellar_session_mq_create_topic(st, TOPIC_UDP, NULL, NULL); + st->tcp_steam_topic_id=stellar_session_mq_create_topic(st, TOPIC_TCP_STREAM, NULL, NULL); return st; -} \ No newline at end of file +} + +int stellar_get_worker_thread_num(struct stellar *st) +{ + return st->worker_thread_num; +} + diff --git a/test/dns_decoder_perf_dummy.h b/test/dns_decoder_perf_dummy.h index 12d742d..d7ebcca 100644 --- a/test/dns_decoder_perf_dummy.h +++ b/test/dns_decoder_perf_dummy.h @@ -1,17 +1,30 @@ #pragma once +#ifdef __cplusplus +extern "C" +{ +#endif + #include "stellar/stellar.h" +#include "stellar/session.h" #include +#ifdef __cplusplus +} +#endif + struct stellar_packet { size_t payload_sz; uint8_t payload[2048]; + struct session_addr addr; + char *readable_addr; + enum session_addr_type addr_type; }; struct stellar *stellar_init(int worker_thread_num); -struct session *stellar_session_new(struct stellar *st, int tid); +struct session *stellar_session_new(struct stellar *st, struct stellar_packet *cur_pkt, int tid); void stellar_session_free(struct session *ss); int session_mq_publish_message_by_name(struct session *ss, const char *topic_name, struct stellar_packet *msg); \ No newline at end of file diff --git a/test/dns_decoder_perf_main.cpp b/test/dns_decoder_perf_main.cpp index 5223c1c..adf8434 100644 --- a/test/dns_decoder_perf_main.cpp +++ b/test/dns_decoder_perf_main.cpp @@ -3,14 +3,38 @@ #include #include #include +#include +#include +#include +#include +#include +#include "cJSON.h" #include "fieldstat/fieldstat_easy.h" + +#include #include "stellar/stellar.h" #include #include "dns_decoder.h" +#include "toml/toml.h" #include "dns_decoder_perf_dummy.h" +#define DNS_DECODER_TEST_TOML_PATH "./etc/dns/dns_decoder.toml" + +#define TIME_START() struct timespec _start_time, _end_time; clock_gettime(CLOCK_REALTIME, &_start_time) +#define TIME_DIFF() \ + long long time_diff_ns;\ + do { \ + clock_gettime(CLOCK_REALTIME, &_end_time); \ + if (likely(_end_time.tv_sec == _start_time.tv_sec))\ + {\ + time_diff_ns = (_end_time.tv_nsec - _start_time.tv_nsec);\ + }else{\ + time_diff_ns = (_end_time.tv_sec * 1000 * 1000 * 1000 + _end_time.tv_nsec) - (_start_time.tv_sec * 1000 * 1000 * 1000 + _start_time.tv_nsec);\ + }\ + }while (0) + struct dns_decoder_context; struct dns_decoder_plugin_env; @@ -21,37 +45,113 @@ enum PERF_TAG PERF_TAG_MAX }; +struct fs_easy_schame +{ + int id[PERF_TAG_MAX]; + struct fieldstat_tag tag[PERF_TAG_MAX]; + struct fieldstat_easy *handle; +}; + struct perf_main_env { int worker_thread_num; - int fs_id[PERF_TAG_MAX]; - struct fieldstat_tag fs_tag[PERF_TAG_MAX]; - struct session *ss; + int *worker_thread_pid; + int n_worker_thread_pid; + struct fs_easy_schame fse; + struct stellar *st; + struct stellar_packet *response; }; // form dns_decoder_test.cpp extern "C" void *dns_decoder_test_init(struct stellar *st); extern "C" void *dns_decoder_init(struct stellar *st); +thread_local int local_worker_thread_id=-1; +int firewall_current_worker_thread_id_get(struct perf_main_env *main_env) +{ + if (local_worker_thread_id==-1) + { + pid_t my_pid = syscall(SYS_gettid); + for(int i=0; in_worker_thread_pid; i++) + { + if (my_pid==main_env->worker_thread_pid[i]) + { + local_worker_thread_id=i; + return local_worker_thread_id; + } + } + + main_env->worker_thread_pid[main_env->n_worker_thread_pid]=my_pid; + local_worker_thread_id=main_env->n_worker_thread_pid++; + } + + return local_worker_thread_id; +} + + +extern "C" int commit_test_result_json(cJSON *node, const char *name) +{ + char *real_result_str=cJSON_Print(node); + printf("%s\n", real_result_str); + free(real_result_str); + + cJSON_Delete(node); + + return 0; +} + void perf_resource_record_decode() { } +void *pthread_message_publish(void *arg) +{ + struct perf_main_env *main_env=(struct perf_main_env *)arg; + int tid=firewall_current_worker_thread_id_get(main_env); + struct session *ss=stellar_session_new(main_env->st, main_env->response, tid); + + while(1) + { + TIME_START(); + session_mq_publish_message_by_name(ss, TOPIC_UDP, main_env->response); + TIME_DIFF(); + fieldstat_easy_histogram_record(main_env->fse.handle, tid, main_env->fse.id[PERF_TAG_QUESTION], &(main_env->fse.tag[PERF_TAG_QUESTION]), 1, time_diff_ns); + } + + stellar_session_free(ss); +} + static void main_stat_init(struct perf_main_env *main_env) { - struct fieldstat_easy *fs4_instance=fieldstat_easy_new(1, "dns_decoder_perf_test", NULL, 0); - fieldstat_easy_enable_auto_output(fs4_instance, "./metrics/dns_decoder_perf_test.json", 1); + main_env->fse.handle=fieldstat_easy_new(main_env->worker_thread_num, "dns_decoder_perf_test", NULL, 0); + fieldstat_easy_enable_auto_output(main_env->fse.handle, "./metrics/dns_decoder_perf_test.json", 1); - main_env->fs_tag[PERF_TAG_QUESTION].key="question"; - main_env->fs_tag[PERF_TAG_QUESTION].type=TAG_DOUBLE; - main_env->fs_tag[PERF_TAG_QUESTION].value_double=0.00001; - main_env->fs_id[PERF_TAG_QUESTION]=fieldstat_easy_register_histogram(fs4_instance, "question", 1, 99999999, 5); + main_env->fse.tag[PERF_TAG_QUESTION].key="question"; + main_env->fse.tag[PERF_TAG_QUESTION].type=TAG_DOUBLE; + main_env->fse.tag[PERF_TAG_QUESTION].value_double=0.00001; + main_env->fse.id[PERF_TAG_QUESTION]=fieldstat_easy_register_histogram(main_env->fse.handle, "question", 1, 99999999, 5); - main_env->fs_tag[PERF_TAG_RESOURCE_RECORD].key="resource_record"; - main_env->fs_tag[PERF_TAG_RESOURCE_RECORD].type=TAG_DOUBLE; - main_env->fs_tag[PERF_TAG_RESOURCE_RECORD].value_double=0.00001; - main_env->fs_id[PERF_TAG_RESOURCE_RECORD]=fieldstat_easy_register_histogram(fs4_instance, "resource-record", 1, 99999999, 5); + main_env->fse.tag[PERF_TAG_RESOURCE_RECORD].key="resource_record"; + main_env->fse.tag[PERF_TAG_RESOURCE_RECORD].type=TAG_DOUBLE; + main_env->fse.tag[PERF_TAG_RESOURCE_RECORD].value_double=0.00001; + main_env->fse.id[PERF_TAG_RESOURCE_RECORD]=fieldstat_easy_register_histogram(main_env->fse.handle, "resource-record", 1, 99999999, 5); +} + +void display_packet(struct stellar_packet *cur_pkt) +{ + printf("query.payload_sz=%zu\n", cur_pkt->payload_sz); + + for(size_t i=0; ipayload_sz; i++) + { + printf("%02x ", cur_pkt->payload[i]); + if(i>0 && (i%16==0)) + { + printf("\n"); + } + } + + printf("\n"); } size_t hex_string_to_byte_array(const char *hex_str, size_t hex_str_sz, unsigned char *byte_array, size_t byte_array_sz) @@ -66,39 +166,95 @@ size_t hex_string_to_byte_array(const char *hex_str, size_t hex_str_sz, unsigned return offset; } +int32_t dns_decoder_test_config_load(const char *cfg_path, struct perf_main_env *main_env) +{ + FILE *fp=fopen(cfg_path, "r"); + if (NULL==fp) + { + fprintf(stderr, "[%s:%d] Can't open config file: %s", __FUNCTION__, __LINE__, cfg_path); + return -1; + } + + int32_t ret=0; + char errbuf[256]={0}; + + toml_table_t *root=toml_parse_file(fp, errbuf, sizeof(errbuf)); + fclose(fp); + + toml_table_t *decoder_tbl=toml_table_in(root, "decoder"); + if(NULL==decoder_tbl) + { + fprintf(stderr, "[%s:%d] config file: %s has no key: [decoder]", __FUNCTION__, __LINE__, cfg_path); + toml_free(root); + return -1; + } + + toml_table_t *dns_tbl=toml_table_in(decoder_tbl, "dns"); + if(NULL==dns_tbl) + { + fprintf(stderr, "[%s:%d] config file: %s has no key: [decoder.dns]", __FUNCTION__, __LINE__, cfg_path); + toml_free(root); + return -1; + } + + toml_table_t *test_tbl=toml_table_in(dns_tbl, "test"); + if(NULL==test_tbl) + { + fprintf(stderr, "[%s:%d] config file: %s has no key: [decoder.dns.test]", __FUNCTION__, __LINE__, cfg_path); + toml_free(root); + return -1; + } + + toml_datum_t perf_worker_thread_num_val=toml_int_in(test_tbl, "perf_worker_thread_num"); + if(perf_worker_thread_num_val.ok) + { + main_env->worker_thread_num=perf_worker_thread_num_val.u.i; + } + else + { + main_env->worker_thread_num=1; + fprintf(stderr, "[%s:%d] config file: %s has no key: [decoder.dns.test.perf_worker_thread_num]", __FUNCTION__, __LINE__, cfg_path); + } + + toml_free(root); + + return ret; +} + int main(int argc, char *argv[]) { struct perf_main_env *main_env=(struct perf_main_env *)calloc(1, sizeof(struct perf_main_env)); - + dns_decoder_test_config_load(DNS_DECODER_TEST_TOML_PATH, main_env); main_stat_init(main_env); - struct stellar *main_st=stellar_init(main_env->worker_thread_num); + main_env->st=stellar_init(main_env->worker_thread_num); + dns_decoder_init(main_env->st); + dns_decoder_test_init(main_env->st); + main_env->n_worker_thread_pid=0; + main_env->worker_thread_pid=(int *)calloc(main_env->worker_thread_num, sizeof(int)); + // Internet Protocol Version 6, Src=2001:503:231d::2:30 Dst=2001:da8:2008::10 struct stellar_packet *response=(struct stellar_packet *)calloc(1, sizeof(struct stellar_packet)); + main_env->response=response; + response->addr_type=SESSION_ADDR_TYPE_IPV6_UDP; + response->readable_addr=(char *)("s=2001:503:231d::2:30.23856 d=2001:da8:2008::10.53"); + inet_pton(AF_INET6, "2001:da8:2008::10", response->addr.ipv6.daddr); + inet_pton(AF_INET6, "2001:503:231d::2:30", response->addr.ipv6.saddr); + response->addr.ipv6.dport=htons(53); + response->addr.ipv6.sport=htons(23856); const char *response_hex_str="ce268000000100000006001404646174610862696c6963646e3203636f6d0000410001c011000200010002a300000c036e733305646e737635c01ac011000200010002a3000006036e7334c03320434b30504f4a4d473837344c4a5245463745464e38343330515649543842534dc01a003200010001518000230101000000146501a0c25720ee156f6c4e39636b3ada0312d92a000722000000000290c04d002e00010001518000b700320802000151806188b454617f5b5c3cbd03636f6d001f77dc4c5796eda9ced317925c67d91c52922152424c9dca024948c1169e8429053fdf50a23370d9c3dc79de909f2f79475b2c731d6060d1db7b5d294b8ee43c91a57b8a4afa06c25fb13127bfca3fb353c7d5a38eaf093e12ffa1e33bc80bd7118851ca730ed22bd27b6f16673b86b44898785c6e13b3dd3620750492e47bb8ca823ffa6e25225ffa5408184c25e4ff423497802deed0586629d78103b3e6e72039454d4644425347524c48514f4d4736515441534e534c4151343438454b4644c01a003200010001518000220101000000144bad0970b67bda0a716dcd12979d4451b22f7ac50006200000000012c160002e00010001518000b700320802000151806188bdde617f64e63cbd03636f6d00a9de4aee30c79978429e76969d02d2bd4c8942f1b4329a643e9d7703e9fd46dd9ab32c4feffe1f9fbed3418e40d42a00fe2c1c2cac66e1c718bf508c4f603171f9ea18e8e79a533d136c26907576ab033dc48e4ff3b355346c33ac54a359c9572c308c923f910e470315dd4de40bd3b443b7caa34309b22146dca1ed6f4758a476052fc8a33829216c5abe88f21981dfe8ae9b2b204958aac575bfaa9847af3ec02f000100010002a300000481d3b0d4c02f000100010002a3000004a20e12bcc02f000100010002a3000004a20e18fbc02f000100010002a3000004a20e19fbc02f000100010002a300000412c20289c02f000100010002a3000004b7c0c95ec02f000100010002a3000004dfa69710c02f001c00010002a300001024024e0014301102000091362b2bba61c02f000100010002a3000004344dee5cc02f000100010002a30000043d97b433c047000100010002a300000465e2dc0cc047000100010002a300000481d3b097c047000100010002a3000004a20e18f8c047000100010002a3000004a20e19f8c047000100010002a3000004b7c0a477c047000100010002a3000004dfa6977ec047001c00010002a300001024024e00102012640000913629b6fc32c047000100010002a300000434c69f92c047000100010002a30000043b2478930000291000000080000000"; response->payload_sz=hex_string_to_byte_array(response_hex_str, strlen(response_hex_str), response->payload, sizeof(response->payload)); - printf("query.payload_sz=%zu\n", response->payload_sz); - - for(size_t i=0; ipayload_sz; i++) + //display_packet(response); + + for(int i=0; iworker_thread_num; i++) { - printf("%02x ", response->payload[i]); - if(i>0 && (i%16==0)) - { - printf("\n"); - } + pthread_t ptid; + pthread_create(&ptid, NULL, pthread_message_publish, (void *)main_env); } - printf("\n"); - while(1) { - for(int i=0; iworker_thread_num; i++) - { - struct session *ss=stellar_session_new(main_st, i); - session_mq_publish_message_by_name(ss, DNS_MESSAGE_TOPIC, response); - stellar_session_free(ss); - } - + sleep(1); } return 0; diff --git a/test/dns_decoder_test.cpp b/test/dns_decoder_test.cpp index aec2e21..f0ddbd3 100644 --- a/test/dns_decoder_test.cpp +++ b/test/dns_decoder_test.cpp @@ -31,6 +31,8 @@ struct dns_decoder_test_plugin_env int topic_id; int result_index; int commit_result_enable; + int write_result_enable; + int decode_resource_record_enable; }; extern "C" int commit_test_result_json(cJSON *node, const char *name); @@ -304,25 +306,28 @@ void dns_decoder_test_message_cb(struct session *ss, int topic_id, const void *m cJSON_AddNumberToObject(real_result, "dns_rd", (double)(flag->rd)); } - cJSON *rr_object=cJSON_CreateObject(); - uint16_t n_answer_rr=0; - struct dns_resource_record *answer_rr=NULL; - dns_message_answer_resource_record_get0(dns_msg, &answer_rr, &n_answer_rr); + if(plugin_env->decode_resource_record_enable==1) + { + cJSON *rr_object=cJSON_CreateObject(); + uint16_t n_answer_rr=0; + struct dns_resource_record *answer_rr=NULL; + dns_message_answer_resource_record_get0(dns_msg, &answer_rr, &n_answer_rr); - uint16_t n_authority_rr=0; - struct dns_resource_record *authority_rr=NULL; - dns_message_authority_resource_record_get0(dns_msg, &authority_rr, &n_authority_rr); + uint16_t n_authority_rr=0; + struct dns_resource_record *authority_rr=NULL; + dns_message_authority_resource_record_get0(dns_msg, &authority_rr, &n_authority_rr); - uint16_t n_additional_rr=0; - struct dns_resource_record *additional_rr=NULL; - dns_message_additional_resource_record_get0(dns_msg, &additional_rr, &n_additional_rr); + uint16_t n_additional_rr=0; + struct dns_resource_record *additional_rr=NULL; + dns_message_additional_resource_record_get0(dns_msg, &additional_rr, &n_additional_rr); - int dns_sec=1; - dns_resource_record_json_exporter(rr_object, answer_rr, n_answer_rr, "answer", &dns_sec); - dns_resource_record_json_exporter(rr_object, authority_rr, n_authority_rr, "authority", &dns_sec); - dns_resource_record_json_exporter(rr_object, additional_rr, n_additional_rr, "additional", &dns_sec); + int dns_sec=1; + dns_resource_record_json_exporter(rr_object, answer_rr, n_answer_rr, "answer", &dns_sec); + dns_resource_record_json_exporter(rr_object, authority_rr, n_authority_rr, "authority", &dns_sec); + dns_resource_record_json_exporter(rr_object, additional_rr, n_additional_rr, "additional", &dns_sec); - cJSON_AddItemToObject(real_result, "rr", rr_object); + cJSON_AddItemToObject(real_result, "rr", rr_object); + } char result_name[16]=""; sprintf(result_name, "DNS_RESULT_%d", plugin_env->result_index++); @@ -351,7 +356,6 @@ void dns_decoder_test_per_session_context_free(struct session *sess, void *sessi } - int32_t dns_decoder_test_config_load(const char *cfg_path, struct dns_decoder_test_plugin_env *plugin_env) { FILE *fp=fopen(cfg_path, "r"); @@ -412,7 +416,53 @@ int32_t dns_decoder_test_config_load(const char *cfg_path, struct dns_decoder_te plugin_env->commit_result_enable=1; fprintf(stderr, "[%s:%d] config file: %s key: [decoder.dns.test.commit_result_enable] value is not yes or no", __FUNCTION__, __LINE__, cfg_path); } - + } + + toml_datum_t write_result_enable_val=toml_string_in(test_tbl, "write_result_enable"); + if(write_result_enable_val.ok==0) + { + plugin_env->write_result_enable=0; + fprintf(stderr, "[%s:%d] config file: %s has no key: [decoder.dns.test.write_result_enable]", __FUNCTION__, __LINE__, cfg_path); + } + else + { + if(memcmp("no", write_result_enable_val.u.s, strlen("no"))==0) + { + plugin_env->write_result_enable=0; + } + else if(memcmp("yes", write_result_enable_val.u.s, strlen("yes"))==0) + { + plugin_env->write_result_enable=1; + } + else + { + plugin_env->write_result_enable=1; + fprintf(stderr, "[%s:%d] config file: %s key: [decoder.dns.test.write_result_enable] value is not yes or no", __FUNCTION__, __LINE__, cfg_path); + } + } + + // decode_resource_record_enable + toml_datum_t decode_resource_record_enable_val=toml_string_in(test_tbl, "decode_resource_record_enable"); + if(decode_resource_record_enable_val.ok==0) + { + plugin_env->decode_resource_record_enable=0; + fprintf(stderr, "[%s:%d] config file: %s has no key: [decoder.dns.test.decode_resource_record_enable]", __FUNCTION__, __LINE__, cfg_path); + } + else + { + if(memcmp("no", decode_resource_record_enable_val.u.s, strlen("no"))==0) + { + plugin_env->decode_resource_record_enable=0; + } + else if(memcmp("yes", decode_resource_record_enable_val.u.s, strlen("yes"))==0) + { + plugin_env->decode_resource_record_enable=1; + } + else + { + plugin_env->decode_resource_record_enable=1; + fprintf(stderr, "[%s:%d] config file: %s key: [decoder.dns.test.decode_resource_record_enable] value is not yes or no", __FUNCTION__, __LINE__, cfg_path); + } } toml_free(root);