diff --git a/src/inc_internal/maat_rule.h b/src/inc_internal/maat_rule.h index 0caba39..12f0614 100644 --- a/src/inc_internal/maat_rule.h +++ b/src/inc_internal/maat_rule.h @@ -253,7 +253,7 @@ int compare_accept_tag(const char *value, const struct rule_tag *accept_tags, si struct maat_item *maat_item_new(long long item_id, long long group_id, void *user_data, void (*user_data_free)(void *)); -void maat_item_free(void *user_ctx, void *maat_item); +void maat_item_free(void *maat_item); struct maat_item_inner *maat_item_inner_new(long long group_id, long long item_id, long long district_id); diff --git a/src/inc_internal/rcu_hash.h b/src/inc_internal/rcu_hash.h index 3908055..4375ec1 100644 --- a/src/inc_internal/rcu_hash.h +++ b/src/inc_internal/rcu_hash.h @@ -32,12 +32,16 @@ void rcu_hash_set_user_ctx(struct rcu_hash_table *htable, void *user_ctx); void *rcu_hash_get_user_ctx(struct rcu_hash_table *htable); /** - * @brief just means add to the updating nodes - * after call rcu_hash_commit, they become effective nodes + * @brief Adding the updating nodes which will become effective nodes after call rcu_hash_commit + * + * @retval 0(success) -1(failed) */ -void rcu_hash_add(struct rcu_hash_table *htable, const char *key, size_t key_len, void *data); +int rcu_hash_add(struct rcu_hash_table *htable, const char *key, size_t key_len, void *data); -void rcu_hash_del(struct rcu_hash_table *htable, const char *key, size_t key_len); +/** + * @brief Deleting +*/ +int rcu_hash_del(struct rcu_hash_table *htable, const char *key, size_t key_len); /** * @brief find in effective nodes @@ -50,8 +54,6 @@ void rcu_hash_del(struct rcu_hash_table *htable, const char *key, size_t key_len */ void *rcu_hash_find(struct rcu_hash_table *htable, const char *key, size_t key_len); -void *rcu_hash_updating_find(struct rcu_hash_table *htable, const char *key, size_t key_len); - /** * @brief list all effective nodes * @@ -73,8 +75,6 @@ int rcu_hash_is_updating(struct rcu_hash_table *htable); */ void rcu_hash_commit(struct rcu_hash_table *htable); -size_t rcu_hash_garbage_queue_len(struct rcu_hash_table *htable); - #ifdef __cplusplus } #endif diff --git a/src/maat_ex_data.c b/src/maat_ex_data.c index fe2a3cb..7f9c122 100644 --- a/src/maat_ex_data.c +++ b/src/maat_ex_data.c @@ -43,6 +43,11 @@ struct ex_data_runtime * ex_data_runtime_new(int table_id, rcu_hash_data_free_fn *data_free_fn, struct maat_garbage_bin *garbage_bin, struct log_handle *logger) { + if (NULL == data_free_fn || NULL == garbage_bin || + NULL == logger) { + return NULL; + } + struct ex_data_runtime *ex_data_rt = ALLOC(struct ex_data_runtime, 1); utarray_new(ex_data_rt->cache_rows, &ut_cache_row_icd); @@ -75,11 +80,19 @@ void ex_data_runtime_free(struct ex_data_runtime *ex_data_rt) void ex_data_runtime_commit(struct ex_data_runtime *ex_data_rt) { + if (NULL == ex_data_rt) { + return; + } + rcu_hash_commit(ex_data_rt->htable); } void ex_data_runtime_cache_row_put(struct ex_data_runtime *ex_data_rt, const char *row) { + if (NULL == ex_data_rt || NULL == row) { + return; + } + size_t len = strlen(row) + 1; char* row_copy = ALLOC(char, len); @@ -91,19 +104,15 @@ void ex_data_runtime_cache_row_put(struct ex_data_runtime *ex_data_rt, const cha const char *ex_data_runtime_cached_row_get(struct ex_data_runtime *ex_data_rt, size_t index) { - const char** row = NULL; + if (NULL == ex_data_rt) { + return NULL; + } + + const char **row = NULL; row = (const char **)utarray_eltptr(ex_data_rt->cache_rows, index); return *row; } -void ex_data_runtime_clear_cached_row(struct ex_data_runtime *ex_data_rt) -{ - utarray_free(ex_data_rt->cache_rows); - ex_data_rt->cache_rows = NULL; - ex_data_rt->cache_row_num = 0; - ex_data_rt->cache_size = 0; -} - size_t ex_data_runtime_cached_row_count(struct ex_data_runtime *ex_data_rt) { if (NULL == ex_data_rt) { @@ -115,6 +124,10 @@ size_t ex_data_runtime_cached_row_count(struct ex_data_runtime *ex_data_rt) void ex_data_runtime_clear_row_cache(struct ex_data_runtime *ex_data_rt) { + if (NULL == ex_data_rt) { + return; + } + utarray_free(ex_data_rt->cache_rows); ex_data_rt->cache_rows = NULL; ex_data_rt->cache_row_num = 0; @@ -139,24 +152,39 @@ struct ex_data_schema *ex_data_schema_new(maat_ex_new_func_t *new_func, void ex_data_schema_free(struct ex_data_schema *ex_schema) { - free(ex_schema); + if (ex_schema != NULL) { + FREE(ex_schema); + } + } void ex_data_runtime_set_schema(struct ex_data_runtime *ex_data_rt, struct ex_data_schema *schema) { + if (NULL == ex_data_rt) { + return; + } + ex_data_rt->ref_ex_schema = schema; } void ex_data_runtime_set_ex_container_schema(struct ex_data_runtime *ex_data_rt, struct ex_container_schema *container_schema) { + if (NULL == ex_data_rt) { + return; + } + rcu_hash_set_user_ctx(ex_data_rt->htable, container_schema); } struct ex_container_schema * ex_data_runtime_get_ex_container_schema(struct ex_data_runtime *ex_data_rt) { + if (NULL == ex_data_rt) { + return NULL; + } + return (struct ex_container_schema *)rcu_hash_get_user_ctx(ex_data_rt->htable); } @@ -215,38 +243,13 @@ int ex_data_runtime_add_ex_container(struct ex_data_runtime *ex_data_rt, const char *key, size_t key_len, struct ex_container *ex_container) { - struct ex_container *tmp_container = NULL; - - tmp_container = (struct ex_container *)rcu_hash_find(ex_data_rt->htable, - key, key_len); - if (tmp_container != NULL) { - log_error(ex_data_rt->logger, MODULE_EX_DATA, - "[%s:%d] ex_data_runtime add ex container error: already exist same key:%s", - __FUNCTION__, __LINE__, key); - return -1; - } - - rcu_hash_add(ex_data_rt->htable, key, key_len, ex_container); - - return 0; + return rcu_hash_add(ex_data_rt->htable, key, key_len, ex_container); } int ex_data_runtime_del_ex_container(struct ex_data_runtime *ex_data_rt, const char *key, size_t key_len) { - struct ex_container *tmp_container = NULL; - tmp_container = (struct ex_container *)rcu_hash_find(ex_data_rt->htable, - key, key_len); - if (NULL == tmp_container) { - log_error(ex_data_rt->logger, MODULE_EX_DATA, - "[%s:%d] ex_data_runtime del ex container error: no such key:%s", - __FUNCTION__, __LINE__, key); - return -1; - } - - rcu_hash_del(ex_data_rt->htable, key, key_len); - - return 0; + return rcu_hash_del(ex_data_rt->htable, key, key_len); } void *ex_data_runtime_get_ex_data_by_key(struct ex_data_runtime *ex_data_rt, diff --git a/src/maat_expr.c b/src/maat_expr.c index 8452518..760b92a 100644 --- a/src/maat_expr.c +++ b/src/maat_expr.c @@ -417,6 +417,12 @@ void expr_ex_data_free(void *user_ctx, void *data) expr_rule_free(expr_rule); } +void expr_maat_item_free(void *user_ctx, void *data) +{ + struct maat_item *item = (struct maat_item *)data; + maat_item_free(item); +} + void *expr_runtime_new(void *expr_schema, int max_thread_num, struct maat_garbage_bin *garbage_bin, struct log_handle *logger) @@ -429,7 +435,7 @@ void *expr_runtime_new(void *expr_schema, int max_thread_num, struct expr_runtime *expr_rt = ALLOC(struct expr_runtime, 1); expr_rt->htable = rcu_hash_new(expr_ex_data_free); - expr_rt->item_htable = rcu_hash_new(maat_item_free); + expr_rt->item_htable = rcu_hash_new(expr_maat_item_free); expr_rt->scan_mode = schema->scan_mode; expr_rt->pattern_type = schema->pattern_type; expr_rt->n_worker_thread = max_thread_num; @@ -485,41 +491,20 @@ void expr_runtime_free(void *expr_runtime) int expr_runtime_update_row(struct expr_runtime *expr_rt, char *key, size_t key_len, long long item_id, struct hs_expr *expr_rule, int is_valid) { - void *data = NULL; + int ret = -1; if (0 == is_valid) { //delete - data = rcu_hash_find(expr_rt->htable, key, key_len); - if (NULL == data) { - data = rcu_hash_updating_find(expr_rt->htable, key, key_len); - if (NULL == data) { - log_error(expr_rt->logger, MODULE_EXPR, - "[%s:%d] the key of expr rule(rule_id:%lld) not exist, can't be deleted from expr runtime htable", - __FUNCTION__, __LINE__, item_id); - return -1; - } - } - rcu_hash_del(expr_rt->htable, key, key_len); } else { //add - data = rcu_hash_find(expr_rt->htable, key, key_len); - if (data != NULL) { + ret = rcu_hash_add(expr_rt->htable, key, key_len, (void *)expr_rule); + if (ret < 0) { log_error(expr_rt->logger, MODULE_EXPR, - "[%s:%d] the key of expr rule(rule_id:%lld) already exist, can't be added to expr runtime htable", + "[%s:%d] expr rule(rule_id:%lld) added to expr runtime htable failed", __FUNCTION__, __LINE__, item_id); return -1; } - - data = rcu_hash_updating_find(expr_rt->htable, key, key_len); - if (data != NULL) { - log_error(expr_rt->logger, MODULE_EXPR, - "[%s:%d] the key of expr rule(rule_id:%lld) already exist, can't be added to expr runtime htable", - __FUNCTION__, __LINE__, item_id); - return -1; - } - - rcu_hash_add(expr_rt->htable, key, key_len, (void *)expr_rule); } return 0; @@ -741,33 +726,9 @@ int expr_runtime_update(void *expr_runtime, void *expr_schema, return -1; } else if (0 == is_valid) { //delete - item = (struct maat_item *)rcu_hash_find(expr_rt->item_htable, (char *)&item_id, sizeof(item_id)); - if (NULL == item) { - item = (struct maat_item *)rcu_hash_updating_find(expr_rt->item_htable, (char *)&item_id, sizeof(item_id)); - if (NULL == item) { - return -1; - } - } - rcu_hash_del(expr_rt->item_htable, (char *)&item_id, sizeof(item_id)); } else { //add - item = (struct maat_item *)rcu_hash_find(expr_rt->item_htable, (char *)&item_id, sizeof(item_id)); - if (item) { - log_error(expr_rt->logger, MODULE_EXPR, - "[%s:%d] expr runtime add item(item_id:%lld) to item_htable failed, already exist", - __FUNCTION__, __LINE__, item_id); - return -1; - } - - item = (struct maat_item *)rcu_hash_updating_find(expr_rt->item_htable, (char *)&item_id, sizeof(item_id)); - if (item) { - log_error(expr_rt->logger, MODULE_EXPR, - "[%s:%d] expr runtime add item(item_id:%lld) to item_htable failed, already exist", - __FUNCTION__, __LINE__, item_id); - return -1; - } - struct expr_item *expr_item = expr_item_new(line, schema, expr_rt->logger); if (NULL == expr_item) { return -1; @@ -775,7 +736,15 @@ int expr_runtime_update(void *expr_runtime, void *expr_schema, u_para = maat_item_inner_new(expr_item->group_id, item_id, expr_item->district_id); item = maat_item_new(item_id, expr_item->group_id, u_para, maat_item_inner_free); - rcu_hash_add(expr_rt->item_htable, (char *)&item_id, sizeof(item_id), item); + ret = rcu_hash_add(expr_rt->item_htable, (char *)&item_id, sizeof(item_id), item); + if (ret < 0) { + log_error(expr_rt->logger, MODULE_EXPR, + "[%s:%d] expr runtime add item(item_id:%lld) to item_htable failed", + __FUNCTION__, __LINE__, item_id); + expr_item_free(expr_item); + maat_item_free(item); + return -1; + } expr_rule = expr_item_to_expr_rule(expr_item, u_para, expr_rt->logger); expr_item_free(expr_item); diff --git a/src/maat_flag.c b/src/maat_flag.c index 689a003..d378882 100644 --- a/src/maat_flag.c +++ b/src/maat_flag.c @@ -128,6 +128,12 @@ void flag_ex_data_free(void *user_ctx, void *data) FREE(flag_item); } +void flag_maat_item_free(void *user_ctx, void *data) +{ + struct maat_item *item = (struct maat_item *)data; + maat_item_free(item); +} + void *flag_runtime_new(void *flag_schema, int max_thread_num, struct maat_garbage_bin *garbage_bin, struct log_handle *logger) @@ -139,7 +145,7 @@ void *flag_runtime_new(void *flag_schema, int max_thread_num, struct flag_runtime *flag_rt = ALLOC(struct flag_runtime, 1); flag_rt->htable = rcu_hash_new(flag_ex_data_free); - flag_rt->item_htable = rcu_hash_new(maat_item_free); + flag_rt->item_htable = rcu_hash_new(flag_maat_item_free); flag_rt->ref_garbage_bin = garbage_bin; flag_rt->logger = logger; @@ -187,41 +193,20 @@ void flag_runtime_free(void *flag_runtime) int flag_runtime_update_row(struct flag_runtime *flag_rt, char *key, size_t key_len, long long item_id, struct flag_rule *rule, int is_valid) { - void *data = NULL; + int ret = -1; if (0 == is_valid) { //delete - data = rcu_hash_find(flag_rt->htable, key, key_len); - if (NULL == data) { - data = rcu_hash_updating_find(flag_rt->htable, key, key_len); - if (NULL == data) { - log_error(flag_rt->logger, MODULE_FLAG, - "[%s:%d] the key of flag rule(rule_id:%lld) not exist, can't be deleted from flag runtime htable", - __FUNCTION__, __LINE__, item_id); - return -1; - } - } - rcu_hash_del(flag_rt->htable, key, key_len); } else { //add - data = rcu_hash_find(flag_rt->htable, key, key_len); - if (data != NULL) { + ret = rcu_hash_add(flag_rt->htable, key, key_len, (void *)rule); + if (ret < 0) { log_error(flag_rt->logger, MODULE_FLAG, - "[%s:%d] the key of flag rule(rule_id:%lld) already exist, can't be added to flag runtime htable", + "[%s:%d] flag rule(rule_id:%lld) add to flag runtime htable failed", __FUNCTION__, __LINE__, item_id); return -1; } - - data = rcu_hash_updating_find(flag_rt->htable, key, key_len); - if (data != NULL) { - log_error(flag_rt->logger, MODULE_FLAG, - "[%s:%d] the key of flag rule(rule_id:%lld) already exist, can't be added to flag runtime htable", - __FUNCTION__, __LINE__, item_id); - return -1; - } - - rcu_hash_add(flag_rt->htable, key, key_len, (void *)rule); } return 0; @@ -331,6 +316,7 @@ int flag_runtime_update(void *flag_runtime, void *flag_schema, return -1; } + int ret = -1; struct maat_item_inner *u_para = NULL; struct maat_item *item = NULL; struct flag_rule *flag_rule = NULL; @@ -347,33 +333,9 @@ int flag_runtime_update(void *flag_runtime, void *flag_schema, return -1; } else if (0 == is_valid) { //delete - item = (struct maat_item *)rcu_hash_find(flag_rt->item_htable, (char *)&item_id, sizeof(item_id)); - if (NULL == item) { - item = (struct maat_item *)rcu_hash_updating_find(flag_rt->item_htable, (char *)&item_id, sizeof(item_id)); - if (NULL == item) { - return -1; - } - } - - rcu_hash_del(flag_rt->item_htable, (char *)&item_id, sizeof(item_id)); + rcu_hash_del(flag_rt->item_htable, (char *)&item_id, sizeof(item_id)); } else { //add - item = (struct maat_item *)rcu_hash_find(flag_rt->item_htable, (char *)&item_id, sizeof(item_id)); - if (item) { - log_error(flag_rt->logger, MODULE_FLAG, - "[%s:%d] flag runtime add item(item_id:%lld) to item_htable failed, already exist", - __FUNCTION__, __LINE__, item_id); - return -1; - } - - item = (struct maat_item *)rcu_hash_updating_find(flag_rt->item_htable, (char *)&item_id, sizeof(item_id)); - if (item) { - log_error(flag_rt->logger, MODULE_FLAG, - "[%s:%d] flag runtime add item(item_id:%lld) to item_htable failed, already exist", - __FUNCTION__, __LINE__, item_id); - return -1; - } - struct flag_item *flag_item = flag_item_new(line, schema, flag_rt->logger); if (NULL == flag_item) { return -1; @@ -381,7 +343,15 @@ int flag_runtime_update(void *flag_runtime, void *flag_schema, u_para = maat_item_inner_new(flag_item->group_id, item_id, flag_item->district_id); item = maat_item_new(item_id, flag_item->group_id, u_para, maat_item_inner_free); - rcu_hash_add(flag_rt->item_htable, (char *)&(item_id), sizeof(item_id), item); + ret = rcu_hash_add(flag_rt->item_htable, (char *)&(item_id), sizeof(item_id), item); + if (ret < 0) { + log_error(flag_rt->logger, MODULE_FLAG, + "[%s:%d] flag runtime add item(item_id:%lld) to item_htable failed", + __FUNCTION__, __LINE__, item_id); + flag_item_free(flag_item); + maat_item_free(item); + return -1; + } flag_rule = flag_item_to_flag_rule(flag_item, u_para); flag_item_free(flag_item); @@ -394,19 +364,13 @@ int flag_runtime_update(void *flag_runtime, void *flag_schema, } char *key = (char *)&item_id; - int ret = flag_runtime_update_row(flag_rt, key, sizeof(long long), item_id, flag_rule, is_valid); + ret = flag_runtime_update_row(flag_rt, key, sizeof(long long), item_id, flag_rule, is_valid); if (ret < 0) { if (flag_rule != NULL) { flag_rule_free(flag_rule); flag_rule = NULL; } return -1; - } else { - if (0 == is_valid) { - flag_rt->rule_num--; - } else { - flag_rt->rule_num++; - } } return 0; diff --git a/src/maat_interval.c b/src/maat_interval.c index 62b2c8c..6a00f37 100644 --- a/src/maat_interval.c +++ b/src/maat_interval.c @@ -125,6 +125,12 @@ void interval_ex_data_free(void *user_ctx, void *data) FREE(item); } +void interval_maat_item_free(void *user_ctx, void *data) +{ + struct maat_item *item = (struct maat_item *)data; + maat_item_free(item); +} + void *interval_runtime_new(void *interval_schema, int max_thread_num, struct maat_garbage_bin *garbage_bin, struct log_handle *logger) @@ -136,7 +142,7 @@ void *interval_runtime_new(void *interval_schema, int max_thread_num, struct interval_runtime *interval_rt = ALLOC(struct interval_runtime, 1); interval_rt->htable = rcu_hash_new(interval_ex_data_free); - interval_rt->item_htable = rcu_hash_new(maat_item_free); + interval_rt->item_htable = rcu_hash_new(interval_maat_item_free); interval_rt->ref_garbage_bin = garbage_bin; interval_rt->logger = logger; @@ -279,41 +285,20 @@ void interval_rule_free(struct interval_rule *rule) int interval_runtime_update_row(struct interval_runtime *interval_rt, char *key, size_t key_len, long long item_id, struct interval_rule *rule, int is_valid) { - void *data = NULL; + int ret = -1; if (0 == is_valid) { //delete - data = rcu_hash_find(interval_rt->htable, key, key_len); - if (NULL == data) { - data = rcu_hash_updating_find(interval_rt->htable, key, key_len); - if (NULL == data) { - log_error(interval_rt->logger, MODULE_INTERVAL, - "[%s:%d] the key of interval rule(rule_id:%lld) not exist, can't be deleted from interval runtime htable", - __FUNCTION__, __LINE__, item_id); - return -1; - } - } - rcu_hash_del(interval_rt->htable, key, key_len); } else { //add - data = rcu_hash_find(interval_rt->htable, key, key_len); - if (data != NULL) { + ret = rcu_hash_add(interval_rt->htable, key, key_len, (void *)rule); + if (ret < 0) { log_error(interval_rt->logger, MODULE_INTERVAL, - "[%s:%d] the key of interval rule(rule_id:%lld) already exist, can't be added to interval runtime htable", + "[%s:%d] interval rule(rule_id:%lld) add to interval runtime htable failed", __FUNCTION__, __LINE__, item_id); return -1; } - - data = rcu_hash_updating_find(interval_rt->htable, key, key_len); - if (data != NULL) { - log_error(interval_rt->logger, MODULE_INTERVAL, - "[%s:%d] the key of interval rule(rule_id:%lld) already exist, can't be added to interval runtime htable", - __FUNCTION__, __LINE__, item_id); - return -1; - } - - rcu_hash_add(interval_rt->htable, key, key_len, (void *)rule); } return 0; @@ -327,6 +312,7 @@ int interval_runtime_update(void *interval_runtime, void *interval_schema, return -1; } + int ret = -1; struct maat_item_inner *u_para = NULL; struct maat_item *item = NULL; struct interval_rule *interval_rule = NULL; @@ -343,37 +329,9 @@ int interval_runtime_update(void *interval_runtime, void *interval_schema, return -1; } else if (0 == is_valid) { //delete - item = (struct maat_item *)rcu_hash_find(interval_rt->item_htable, - (char *)&item_id, sizeof(item_id)); - if (NULL == item) { - item = (struct maat_item *)rcu_hash_updating_find(interval_rt->item_htable, - (char *)&item_id, sizeof(item_id)); - if (NULL == item) { - return -1; - } - } - rcu_hash_del(interval_rt->item_htable, (char *)&item_id, sizeof(item_id)); } else { //add - item = (struct maat_item *)rcu_hash_find(interval_rt->item_htable, - (char *)&item_id, sizeof(item_id));; - if (item) { - log_error(interval_rt->logger, MODULE_INTERVAL, - "[%s:%d] interval runtime add item(item_id:%lld) to item_htable failed, already exist", - __FUNCTION__, __LINE__, item_id); - return -1; - } - - item = (struct maat_item *)rcu_hash_updating_find(interval_rt->item_htable, - (char *)&item_id, sizeof(item_id));; - if (item) { - log_error(interval_rt->logger, MODULE_INTERVAL, - "[%s:%d] interval runtime add item(item_id:%lld) to item_htable failed, already exist", - __FUNCTION__, __LINE__, item_id); - return -1; - } - struct interval_item *interval_item = interval_item_new(line, schema, interval_rt->logger); if (NULL == interval_item) { return -1; @@ -381,7 +339,15 @@ int interval_runtime_update(void *interval_runtime, void *interval_schema, u_para = maat_item_inner_new(interval_item->group_id, item_id, interval_item->district_id); item = maat_item_new(item_id, interval_item->group_id, u_para, maat_item_inner_free); - rcu_hash_add(interval_rt->item_htable, (char *)&(item_id), sizeof(item_id), item); + ret = rcu_hash_add(interval_rt->item_htable, (char *)&(item_id), sizeof(item_id), item); + if (ret < 0) { + log_error(interval_rt->logger, MODULE_INTERVAL, + "[%s:%d] interval runtime add item(item_id:%lld) to item_htable failed", + __FUNCTION__, __LINE__, item_id); + interval_item_free(interval_item); + maat_item_free(item); + return -1; + } interval_rule = interval_item_to_interval_rule(interval_item, u_para); interval_item_free(interval_item); @@ -394,7 +360,7 @@ int interval_runtime_update(void *interval_runtime, void *interval_schema, } char *key = (char *)&item_id; - int ret = interval_runtime_update_row(interval_rt, key, sizeof(long long), item_id, interval_rule, is_valid); + ret = interval_runtime_update_row(interval_rt, key, sizeof(long long), item_id, interval_rule, is_valid); if (ret < 0) { if (interval_rule != NULL) { interval_rule_free(interval_rule); diff --git a/src/maat_ip.c b/src/maat_ip.c index a510044..b175285 100644 --- a/src/maat_ip.c +++ b/src/maat_ip.c @@ -255,6 +255,12 @@ void ip_ex_data_free(void *user_ctx, void *data) ip_item_free(item); } +void ip_maat_item_free(void *user_ctx, void *data) +{ + struct maat_item *item = (struct maat_item *)data; + maat_item_free(item); +} + void *ip_runtime_new(void *ip_schema, int max_thread_num, struct maat_garbage_bin *garbage_bin, struct log_handle *logger) @@ -262,7 +268,7 @@ void *ip_runtime_new(void *ip_schema, int max_thread_num, struct ip_runtime *ip_rt = ALLOC(struct ip_runtime, 1); ip_rt->htable = rcu_hash_new(ip_ex_data_free); - ip_rt->item_htable = rcu_hash_new(maat_item_free); + ip_rt->item_htable = rcu_hash_new(ip_maat_item_free); ip_rt->ref_garbage_bin = garbage_bin; ip_rt->logger = logger; @@ -326,41 +332,20 @@ void ip_item_to_ip_rule(struct ip_item *item, struct ip_rule *rule) int ip_runtime_update_row(struct ip_runtime *ip_rt, char *key, size_t key_len, struct ip_item *item, int is_valid) { - void *data = NULL; + int ret = -1; if (0 == is_valid) { // delete - data = rcu_hash_find(ip_rt->htable, key, key_len); - if (NULL == data) { - data = rcu_hash_updating_find(ip_rt->htable, key, key_len); - if (NULL == data) { - log_error(ip_rt->logger, MODULE_IP, - "[%s:%d] the key of ip rule(rule_id:%lld) not exist, can't be deleted from ip runtime htable", - __FUNCTION__, __LINE__, item->item_id); - return -1; - } - } - rcu_hash_del(ip_rt->htable, key, key_len); } else { // add - data = rcu_hash_find(ip_rt->htable, key, key_len); - if (data != NULL) { + ret = rcu_hash_add(ip_rt->htable, key, key_len, (void *)item); + if (ret < 0) { log_error(ip_rt->logger, MODULE_IP, - "[%s:%d] the key of ip rule(rule_id:%lld) already exist, can't be added to ip runtime htable", + "[%s:%d] ip rule(rule_id:%lld) add to ip runtime htable failed", __FUNCTION__, __LINE__, item->item_id); return -1; } - - data = rcu_hash_updating_find(ip_rt->htable, key, key_len); - if (data != NULL) { - log_error(ip_rt->logger, MODULE_IP, - "[%s:%d] the key of ip rule(rule_id:%lld) already exist, can't be added to ip runtime htable", - __FUNCTION__, __LINE__, item->item_id); - return -1; - } - - rcu_hash_add(ip_rt->htable, key, key_len, (void *)item); } return 0; @@ -373,6 +358,7 @@ int ip_runtime_update(void *ip_runtime, void *ip_schema, return -1; } + int ret = -1; struct maat_item *item = NULL; struct ip_item *ip_item = NULL; struct maat_item_inner *u_para = NULL; @@ -389,39 +375,9 @@ int ip_runtime_update(void *ip_runtime, void *ip_schema, return -1; } else if (0 == is_valid) { //delete - item = (struct maat_item *)rcu_hash_find(ip_rt->item_htable, - (char *)&item_id, - sizeof(item_id)); - if (NULL == item) { - item = (struct maat_item *)rcu_hash_updating_find(ip_rt->item_htable, - (char *)&item_id, - sizeof(item_id)); - if (NULL == item) { - return -1; - } - } - rcu_hash_del(ip_rt->item_htable, (char *)&item_id, sizeof(item_id)); } else { //add - item = (struct maat_item *)rcu_hash_find(ip_rt->item_htable, - (char *)&item_id, sizeof(item_id));; - if (item) { - log_error(ip_rt->logger, MODULE_IP, - "[%s:%d] ip runtime add item(item_id:%lld) to item_htable failed, already exist", - __FUNCTION__, __LINE__, item_id); - return -1; - } - - item = (struct maat_item *)rcu_hash_updating_find(ip_rt->item_htable, - (char *)&item_id, sizeof(item_id));; - if (item) { - log_error(ip_rt->logger, MODULE_IP, - "[%s:%d] ip runtime add item(item_id:%lld) to item_htable failed, already exist", - __FUNCTION__, __LINE__, item_id); - return -1; - } - ip_item = ip_item_new(line, schema, ip_rt->logger); if (NULL == ip_item) { return -1; @@ -429,11 +385,19 @@ int ip_runtime_update(void *ip_runtime, void *ip_schema, u_para = maat_item_inner_new(ip_item->group_id, item_id, 0); item = maat_item_new(item_id, ip_item->group_id, u_para, maat_item_inner_free); - rcu_hash_add(ip_rt->item_htable, (char *)&(item_id), sizeof(item_id), item); + ret = rcu_hash_add(ip_rt->item_htable, (char *)&(item_id), sizeof(item_id), item); + if (ret < 0) { + log_error(ip_rt->logger, MODULE_IP, + "[%s:%d] ip runtime add item(item_id:%lld) to item_htable failed", + __FUNCTION__, __LINE__, item_id); + ip_item_free(ip_item); + maat_item_free(item); + return -1; + } } char *key = (char *)&item_id; - int ret = ip_runtime_update_row(ip_rt, key, sizeof(long long), ip_item, is_valid); + ret = ip_runtime_update_row(ip_rt, key, sizeof(long long), ip_item, is_valid); if (ret < 0) { if (ip_item != NULL) { ip_item_free(ip_item); diff --git a/src/maat_ip_plugin.c b/src/maat_ip_plugin.c index 449a844..35ef420 100644 --- a/src/maat_ip_plugin.c +++ b/src/maat_ip_plugin.c @@ -316,6 +316,9 @@ int ip_plugin_runtime_update_row(struct ip_plugin_runtime *ip_plugin_rt, { int ret = -1; struct ex_data_runtime *ex_data_rt = ip_plugin_rt->ex_data_rt; + if (NULL == ex_data_rt) { + return -1; + } if (0 == is_valid) { // delete @@ -413,12 +416,6 @@ int ip_plugin_runtime_update(void *ip_plugin_runtime, void *ip_plugin_schema, ip_plugin_rule_free(ip_plugin_rule); } return -1; - } else { - if (0 == is_valid) { - ip_plugin_rt->rule_num--; - } else { - ip_plugin_rt->rule_num++; - } } } else { //ex_schema not set diff --git a/src/maat_rule.c b/src/maat_rule.c index d822bc5..a01ce05 100644 --- a/src/maat_rule.c +++ b/src/maat_rule.c @@ -44,7 +44,7 @@ struct maat_item *maat_item_new(long long item_id, long long group_id, void *use return item; } -void maat_item_free(void *user_ctx, void *maat_item) +void maat_item_free(void *maat_item) { struct maat_item *item = (struct maat_item *)maat_item; if (item->user_data_free && item->user_data) { diff --git a/src/rcu_hash.c b/src/rcu_hash.c index 321b8cd..f61e9aa 100644 --- a/src/rcu_hash.c +++ b/src/rcu_hash.c @@ -64,11 +64,6 @@ void rcu_hash_garbage_queue_free(struct rcu_hash_garbage_q *garbage_q) } } -size_t rcu_hash_garbage_queue_len(struct rcu_hash_table *htable) -{ - return htable->garbage_q_len; -} - void rcu_hash_garbage_bagging(struct rcu_hash_garbage_q* garbage_q, void* garbage, void (* func)(void *)) { @@ -173,11 +168,11 @@ void rcu_hash_commit_prepare(struct rcu_hash_table *htable) htable->is_updating = 1; } -void rcu_hash_add(struct rcu_hash_table *htable, const char *key, +int rcu_hash_add(struct rcu_hash_table *htable, const char *key, size_t key_len, void *data) { if (NULL == htable || NULL == key || 0 == key_len) { - return; + return -1; } if (!htable->is_updating) { @@ -191,26 +186,30 @@ void rcu_hash_add(struct rcu_hash_table *htable, const char *key, HASH_FIND(hh_a, htable->hashmap_a, key, key_len, tmp); } - if (NULL == tmp) { - struct rcu_hash_node *node = ALLOC(struct rcu_hash_node, 1); - node->key = ALLOC(char, key_len); - memcpy(node->key, key, key_len); - node->key_len = key_len; - node->data = data; - node->htable = htable; - - if (htable->effective_hash == 'a') { - HASH_ADD_KEYPTR(hh_b, htable->hashmap_b, node->key, node->key_len, node); - } else { - HASH_ADD_KEYPTR(hh_a, htable->hashmap_a, node->key, node->key_len, node); - } + if (tmp != NULL) { + return -1; } + + struct rcu_hash_node *node = ALLOC(struct rcu_hash_node, 1); + node->key = ALLOC(char, key_len); + memcpy(node->key, key, key_len); + node->key_len = key_len; + node->data = data; + node->htable = htable; + + if (htable->effective_hash == 'a') { + HASH_ADD_KEYPTR(hh_b, htable->hashmap_b, node->key, node->key_len, node); + } else { + HASH_ADD_KEYPTR(hh_a, htable->hashmap_a, node->key, node->key_len, node); + } + + return 0; } -void rcu_hash_del(struct rcu_hash_table *htable, const char *key, size_t key_len) +int rcu_hash_del(struct rcu_hash_table *htable, const char *key, size_t key_len) { if (NULL == htable || NULL == key || 0 == key_len) { - return; + return -1; } if (!htable->is_updating) { @@ -235,6 +234,8 @@ void rcu_hash_del(struct rcu_hash_table *htable, const char *key, size_t key_len (void (*)(void*))rcu_hash_node_free); htable->garbage_q_len++; } + + return 0; } void *rcu_hash_find(struct rcu_hash_table *htable, const char *key, size_t key_len) @@ -259,28 +260,6 @@ void *rcu_hash_find(struct rcu_hash_table *htable, const char *key, size_t key_l return NULL; } -void *rcu_hash_updating_find(struct rcu_hash_table *htable, const char *key, size_t key_len) -{ - if (NULL == htable || NULL == key || 0 == key_len) { - return NULL; - } - - struct rcu_hash_node *node = NULL; - if (htable->effective_hash == 'a') { - HASH_FIND(hh_b, htable->hashmap_b, key, key_len, node); - if (node != NULL) { - return node->data; - } - } else { - HASH_FIND(hh_a, htable->hashmap_a, key, key_len, node); - if (node != NULL) { - return node->data; - } - } - - return NULL; -} - size_t rcu_hash_count(struct rcu_hash_table *htable) { if (NULL == htable) { diff --git a/test/maat_framework_gtest.cpp b/test/maat_framework_gtest.cpp index f49cddd..5ab9268 100644 --- a/test/maat_framework_gtest.cpp +++ b/test/maat_framework_gtest.cpp @@ -2566,7 +2566,7 @@ TEST_F(MaatCmdTest, SetExpr) { EXPECT_EQ(ret, MAAT_SCAN_HALF_HIT); maat_state_free(&state); } -#if 0 + TEST_F(MaatCmdTest, SetExpr8) { const char *scan_data8 = "string1, string2, string3, string4, string5, string6, string7, string8"; const char *scan_data7 = "string1, string2, string3, string4, string5, string6, string7"; @@ -2629,7 +2629,7 @@ TEST_F(MaatCmdTest, SetExpr8) { EXPECT_EQ(results[0], compile_id); maat_state_free(&state); } -#endif + struct user_info { char name[256]; char ip_addr[32]; diff --git a/test/rcu_hash_gtest.cpp b/test/rcu_hash_gtest.cpp index 31c53b5..563e0d7 100644 --- a/test/rcu_hash_gtest.cpp +++ b/test/rcu_hash_gtest.cpp @@ -42,9 +42,6 @@ TEST(rcu_hash_add_one_node, single_thread) { int ret = rcu_hash_count(htable); EXPECT_EQ(ret, 0); - ret = rcu_hash_garbage_queue_len(htable); - EXPECT_EQ(ret, 0); - void **data_array = NULL; rcu_hash_commit(htable); @@ -67,9 +64,6 @@ TEST(rcu_hash_add_one_node, single_thread) { ret = rcu_hash_count(htable); EXPECT_EQ(ret, 1); - ret = rcu_hash_garbage_queue_len(htable); - EXPECT_EQ(ret, 0); - rcu_hash_free(htable); } @@ -119,9 +113,6 @@ TEST(rcu_hash_add_multi_node, single_thread) { int ret = rcu_hash_count(htable); EXPECT_EQ(ret, 0); - ret = rcu_hash_garbage_queue_len(htable); - EXPECT_EQ(ret, 0); - void **data_array = NULL; ret = rcu_hash_list(htable, &data_array); EXPECT_EQ(ret, 0); @@ -167,9 +158,6 @@ TEST(rcu_hash_add_multi_node, single_thread) { ret = rcu_hash_count(htable); EXPECT_EQ(ret, 4); - ret = rcu_hash_garbage_queue_len(htable); - EXPECT_EQ(ret, 0); - rcu_hash_free(htable); } @@ -203,9 +191,6 @@ TEST(rcu_hash_del_one_node, single_thread) { ret = rcu_hash_count(htable); EXPECT_EQ(ret, 0); - ret = rcu_hash_garbage_queue_len(htable); - EXPECT_EQ(ret, 0); - rcu_hash_del(htable, key, key_len); ret = rcu_hash_list(htable, &data_array); @@ -244,9 +229,6 @@ TEST(rcu_hash_del_one_node, single_thread) { ret = rcu_hash_count(htable); EXPECT_EQ(ret, 1); - - ret = rcu_hash_garbage_queue_len(htable); - EXPECT_EQ(ret, 1); /* delete commit */ rcu_hash_commit(htable); @@ -256,9 +238,6 @@ TEST(rcu_hash_del_one_node, single_thread) { ret = rcu_hash_count(htable); EXPECT_EQ(ret, 0); - ret = rcu_hash_garbage_queue_len(htable); - EXPECT_EQ(ret, 0); - rcu_hash_free(htable); } @@ -290,15 +269,9 @@ TEST(rcu_hash_del_multi_node, single_thread) { int ret = rcu_hash_count(htable); EXPECT_EQ(ret, 0); - ret = rcu_hash_garbage_queue_len(htable); - EXPECT_EQ(ret, 0); - /* add del, then commit */ rcu_hash_del(htable, key1, key1_len); - ret = rcu_hash_garbage_queue_len(htable); - EXPECT_EQ(ret, 1); - rcu_hash_commit(htable); /* find in hash after commit */ @@ -311,21 +284,140 @@ TEST(rcu_hash_del_multi_node, single_thread) { ret = rcu_hash_count(htable); EXPECT_EQ(ret, 1); - ret = rcu_hash_garbage_queue_len(htable); + rcu_hash_free(htable); +} + +TEST(rcu_hash_add_with_same_key, single_thread) { + struct rcu_hash_table *htable = rcu_hash_new(data_free); + EXPECT_TRUE(htable != NULL); + + char key[64] = "http_url"; + size_t key_len = strlen(key); + + //add data1 + struct user_data *data1 = ALLOC(struct user_data, 1); + data1->id = 101; + char name1[64] = "www.baidu.com"; + memcpy(data1->name, name1, strlen(name1)); + int ret = rcu_hash_add(htable, key, key_len, (void *)data1); EXPECT_EQ(ret, 0); + void *res = rcu_hash_find(htable, key, key_len); + EXPECT_TRUE(res == NULL); + + //add data2 + struct user_data *data2 = ALLOC(struct user_data, 1); + data2->id = 102; + char name2[64] = "www.google.com"; + memcpy(data2->name, name2, strlen(name2)); + ret = rcu_hash_add(htable, key, key_len, (void *)data2); + EXPECT_EQ(ret, -1); + FREE(data2); + + rcu_hash_commit(htable); + + res = rcu_hash_find(htable, key, key_len); + EXPECT_TRUE(res != NULL); + + struct user_data *tmp_data = (struct user_data *)res; + EXPECT_EQ(tmp_data->id, 101); + EXPECT_STREQ(tmp_data->name, "www.baidu.com"); + + rcu_hash_free(htable); +} + +TEST(rcu_hash_del_with_same_key, single_thread) { + struct rcu_hash_table *htable = rcu_hash_new(data_free); + EXPECT_TRUE(htable != NULL); + + char key[64] = "http_url"; + size_t key_len = strlen(key); + + int ret = rcu_hash_del(htable, key, key_len); + EXPECT_EQ(ret, 0); + + //add data1 + struct user_data *data1 = ALLOC(struct user_data, 1); + data1->id = 101; + char name1[64] = "www.baidu.com"; + memcpy(data1->name, name1, strlen(name1)); + + ret = rcu_hash_add(htable, key, key_len, (void *)data1); + EXPECT_EQ(ret, 0); + + void *res = rcu_hash_find(htable, key, key_len); + EXPECT_TRUE(res == NULL); + + ret = rcu_hash_del(htable, key, key_len); + EXPECT_EQ(ret, 0); + + //add data2 + struct user_data *data2 = ALLOC(struct user_data, 1); + data2->id = 102; + char name2[64] = "www.google.com"; + memcpy(data2->name, name2, strlen(name2)); + ret = rcu_hash_add(htable, key, key_len, (void *)data2); + EXPECT_EQ(ret, 0); + + rcu_hash_commit(htable); + + res = rcu_hash_find(htable, key, key_len); + EXPECT_TRUE(res != NULL); + + struct user_data *tmp_data = (struct user_data *)res; + EXPECT_EQ(tmp_data->id, 102); + EXPECT_STREQ(tmp_data->name, "www.google.com"); + + rcu_hash_free(htable); +} + +TEST(rcu_hash_modify_with_same_key, single_thread) { + struct rcu_hash_table *htable = rcu_hash_new(data_free); + EXPECT_TRUE(htable != NULL); + + char key[64] = "http_url"; + size_t key_len = strlen(key); + + //add data1 + struct user_data *data1 = ALLOC(struct user_data, 1); + data1->id = 101; + char name1[64] = "www.baidu.com"; + memcpy(data1->name, name1, strlen(name1)); + rcu_hash_add(htable, key, key_len, (void *)data1); + + void *res = rcu_hash_find(htable, key, key_len); + EXPECT_TRUE(res == NULL); + + //del data1 + rcu_hash_del(htable, key, key_len); + + //add data2 + struct user_data *data2 = ALLOC(struct user_data, 1); + data2->id = 102; + char name2[64] = "www.google.com"; + memcpy(data2->name, name2, strlen(name2)); + + rcu_hash_add(htable, key, key_len, (void *)data2); + rcu_hash_commit(htable); + + res = rcu_hash_find(htable, key, key_len); + struct user_data *tmp_data = (struct user_data *)res; + EXPECT_EQ(tmp_data->id, 102); + EXPECT_STREQ(tmp_data->name, "www.google.com"); + rcu_hash_free(htable); } TEST(global_rcu_hash_add, single_thread) { EXPECT_TRUE(g_htable != NULL); + char key1[64] = "http_url"; + size_t key1_len = strlen(key1); + struct user_data *data1 = ALLOC(struct user_data, 1); data1->id = 101; char name1[64] = "www.baidu.com"; memcpy(data1->name, name1, strlen(name1)); - char key1[64] = "http_url"; - size_t key1_len = strlen(key1); rcu_hash_add(g_htable, key1, key1_len, (void *)data1); int ret = rcu_hash_count(g_htable); @@ -366,51 +458,6 @@ TEST(global_rcu_hash_del, single_thread) { EXPECT_TRUE(res == NULL); } -TEST(rcu_hash_updating_find, single_thread) { - struct rcu_hash_table *htable = rcu_hash_new(data_free); - EXPECT_TRUE(htable != NULL); - - //add data1 - struct user_data *data1 = ALLOC(struct user_data, 1); - data1->id = 101; - char name1[64] = "www.baidu.com"; - memcpy(data1->name, name1, strlen(name1)); - char key[64] = "http_url"; - size_t key_len = strlen(key); - rcu_hash_add(htable, key, key_len, (void *)data1); - - void *res = rcu_hash_find(htable, key, key_len); - EXPECT_TRUE(res == NULL); - - res = rcu_hash_updating_find(htable, key, key_len); - EXPECT_TRUE(res != NULL); - - //del data1 - rcu_hash_del(htable, key, key_len); - - res = rcu_hash_updating_find(htable, key, key_len); - EXPECT_TRUE(res == NULL); - - //add data2 - struct user_data *data2 = ALLOC(struct user_data, 1); - data2->id = 102; - char name2[64] = "www.google.com"; - memcpy(data2->name, name2, strlen(name2)); - - rcu_hash_add(htable, key, key_len, (void *)data2); - res = rcu_hash_updating_find(htable, key, key_len); - EXPECT_TRUE(res != NULL); - - rcu_hash_commit(htable); - - res = rcu_hash_find(htable, key, key_len); - struct user_data *tmp_data = (struct user_data *)res; - EXPECT_EQ(tmp_data->id, 102); - EXPECT_STREQ(tmp_data->name, "www.google.com"); - - rcu_hash_free(htable); -} - int main(int argc, char ** argv) { int ret=0;