[BUGFIX]fix xx_plugin double free

This commit is contained in:
liuwentan
2024-01-31 16:41:31 +08:00
parent 42bd2f35ea
commit 18843fafa7
8 changed files with 503 additions and 298 deletions

View File

@@ -55,6 +55,8 @@ struct ex_data_runtime *bool_plugin_runtime_get_ex_data_rt(void *bool_plugin_run
int bool_plugin_runtime_get_ex_data(void *bool_plugin_runtime, unsigned long long *item_ids,
size_t n_item, void **ex_data_array, size_t n_ex_data);
void bool_plugin_expr_free(void *bool_expr);
long long bool_plugin_runtime_update_err_count(void *bool_plugin_runtime);
void bool_plugin_runtime_scan_times_inc(void *bool_plugin_runtime, int thread_id);

View File

@@ -55,6 +55,8 @@ struct ex_data_runtime *ip_plugin_runtime_get_ex_data_rt(void *ip_plugin_runtime
int ip_plugin_runtime_get_ex_data(void *ip_plugin_runtime, const struct ip_addr *ip_addr,
void **ex_data_array, size_t n_ex_data_array);
void ip_plugin_rule_free(void *ip_rule);
long long ip_plugin_runtime_update_err_count(void *ip_plugin_runtime);
void ip_plugin_runtime_scan_times_inc(void *ip_plugin_rt, int thread_id);

View File

@@ -57,6 +57,8 @@ struct ex_data_runtime *ipport_plugin_runtime_get_ex_data_rt(void *ipport_plugin
int ipport_plugin_runtime_get_ex_data(void *ipport_plugin_runtime, const struct ip_addr *ip_addr,
uint16_t port, void **ex_data_array, size_t n_ex_data);
void ipport_item_free(void *ipport_item);
long long ipport_plugin_runtime_update_err_count(void *ipport_plugin_runtime);
void ipport_plugin_runtime_scan_times_inc(void *ipport_plugin_rt, int thread_id);

View File

@@ -620,12 +620,14 @@ static int generic_plugin_table_set_ex_schema(struct table_manager *tbl_mgr, int
case TABLE_TYPE_IP_PLUGIN:
ret = ip_plugin_table_set_ex_container_schema(schema, table_id,
new_func, free_func, dup_func,
free, argl, argp);
ip_plugin_rule_free,
argl, argp);
break;
case TABLE_TYPE_IPPORT_PLUGIN:
ret = ipport_plugin_table_set_ex_container_schema(schema, table_id,
new_func, free_func, dup_func,
free, argl, argp);
ipport_item_free,
argl, argp);
break;
case TABLE_TYPE_FQDN_PLUGIN:
ret = fqdn_plugin_table_set_ex_container_schema(schema, table_id,
@@ -636,7 +638,8 @@ static int generic_plugin_table_set_ex_schema(struct table_manager *tbl_mgr, int
case TABLE_TYPE_BOOL_PLUGIN:
ret = bool_plugin_table_set_ex_container_schema(schema, table_id,
new_func, free_func, dup_func,
free, argl, argp);
bool_plugin_expr_free,
argl, argp);
break;
default:
log_fatal(logger, MODULE_MAAT_API,

View File

@@ -147,7 +147,8 @@ int bool_plugin_table_set_ex_container_schema(void *bool_plugin_schema, int tabl
struct ex_container_schema *
bool_plugin_table_get_ex_container_schema(void *bool_plugin_schema)
{
struct bool_plugin_schema *schema = (struct bool_plugin_schema *)bool_plugin_schema;
struct bool_plugin_schema *schema =
(struct bool_plugin_schema *)bool_plugin_schema;
return &(schema->container_schema);
}
@@ -186,11 +187,15 @@ void *bool_plugin_runtime_new(void *bool_plugin_schema, size_t max_thread_num,
return NULL;
}
struct bool_plugin_schema *schema = (struct bool_plugin_schema *)bool_plugin_schema;
struct bool_plugin_runtime *bool_plugin_rt = ALLOC(struct bool_plugin_runtime, 1);
struct bool_plugin_schema *schema =
(struct bool_plugin_schema *)bool_plugin_schema;
struct bool_plugin_runtime *bool_plugin_rt =
ALLOC(struct bool_plugin_runtime, 1);
bool_plugin_rt->ex_data_rt =
ex_data_runtime_new(schema->table_id, schema->gc_timeout_s, logger);
bool_plugin_rt->ex_data_rt = ex_data_runtime_new(schema->table_id, schema->gc_timeout_s,
logger);
if (1 == schema->container_schema.set_flag) {
ex_data_runtime_set_ex_container_schema(bool_plugin_rt->ex_data_rt,
&(schema->container_schema));
@@ -210,7 +215,9 @@ void bool_plugin_runtime_free(void *bool_plugin_runtime)
return;
}
struct bool_plugin_runtime *bool_plugin_rt = (struct bool_plugin_runtime *)bool_plugin_runtime;
struct bool_plugin_runtime *bool_plugin_rt =
(struct bool_plugin_runtime *)bool_plugin_runtime;
if (bool_plugin_rt->matcher != NULL) {
bool_matcher_free(bool_plugin_rt->matcher);
bool_plugin_rt->matcher = NULL;
@@ -229,10 +236,11 @@ void bool_plugin_runtime_free(void *bool_plugin_runtime)
FREE(bool_plugin_rt);
}
static int bool_plugin_runtime_update_row(struct bool_plugin_runtime *bool_plugin_rt,
const char *table_name, const char *row,
const char *key, size_t key_len,
struct bool_expr *expr, int is_valid)
static int
bool_plugin_runtime_update_row(struct bool_plugin_runtime *bool_plugin_rt,
const char *table_name, const char *row,
const char *key, size_t key_len,
struct bool_expr *expr, int is_valid)
{
int ret = -1;
struct ex_data_runtime *ex_data_rt = bool_plugin_rt->ex_data_rt;
@@ -258,9 +266,10 @@ static int bool_plugin_runtime_update_row(struct bool_plugin_runtime *bool_plugi
return 0;
}
static int bool_plugin_accept_tag_match(struct bool_plugin_schema *schema,
const char *table_name, const char *line,
struct log_handle *logger)
static int
bool_plugin_accept_tag_match(struct bool_plugin_schema *schema,
const char *table_name, const char *line,
struct log_handle *logger)
{
size_t column_offset = 0;
size_t column_len = 0;
@@ -271,8 +280,9 @@ static int bool_plugin_accept_tag_match(struct bool_plugin_schema *schema,
&column_offset, &column_len);
if (ret < 0) {
log_fatal(logger, MODULE_BOOL_PLUGIN,
"[%s:%d] bool_plugin table:<%s> has no rule_tag in line:%s",
__FUNCTION__, __LINE__, table_name, line);
"[%s:%d] bool_plugin table:<%s> has no rule_tag"
" in line:%s", __FUNCTION__, __LINE__, table_name,
line);
return TAG_MATCH_ERR;
}
@@ -283,15 +293,17 @@ static int bool_plugin_accept_tag_match(struct bool_plugin_schema *schema,
FREE(tag_str);
if (TAG_MATCH_ERR == ret) {
log_fatal(logger, MODULE_BOOL_PLUGIN,
"[%s:%d] bool_plugin table:<%s> has invalid tag format in line:%s",
__FUNCTION__, __LINE__, table_name, line);
"[%s:%d] bool_plugin table:<%s> has invalid "
"tag format in line:%s", __FUNCTION__, __LINE__,
table_name, line);
return TAG_MATCH_ERR;
}
if (TAG_MATCH_UNMATCHED == ret) {
log_fatal(logger, MODULE_BOOL_PLUGIN,
"[%s:%d] bool_plugin table:<%s> has unmatched tag in line:%s",
__FUNCTION__, __LINE__, table_name, line);
"[%s:%d] bool_plugin table:<%s> has unmatched tag"
" in line:%s", __FUNCTION__, __LINE__, table_name,
line);
return TAG_MATCH_UNMATCHED;
}
}
@@ -317,20 +329,24 @@ bool_plugin_expr_new(struct bool_plugin_schema *schema, const char *table_name,
char *token = NULL, *sub_token = NULL, *saveptr;
struct bool_expr *bool_expr = ALLOC(struct bool_expr, 1);
ret = get_column_pos(line, schema->item_id_column, &column_offset, &column_len);
ret = get_column_pos(line, schema->item_id_column,
&column_offset, &column_len);
if (ret < 0) {
log_fatal(logger, MODULE_BOOL_PLUGIN,
"[%s:%d] bool_plugin table:<%s> has no item_id in line:%s",
__FUNCTION__, __LINE__, table_name, line);
"[%s:%d] bool_plugin table:<%s> has "
"no item_id in line:%s", __FUNCTION__,
__LINE__, table_name, line);
goto error;
}
bool_expr->expr_id = atoll(line + column_offset);
ret = get_column_pos(line, schema->bool_expr_column, &column_offset, &column_len);
ret = get_column_pos(line, schema->bool_expr_column,
&column_offset, &column_len);
if (ret < 0) {
log_fatal(logger, MODULE_BOOL_PLUGIN,
"[%s:%d] bool_plugin table:<%s> has no bool_expr in line:%s",
__FUNCTION__, __LINE__, table_name, line);
"[%s:%d] bool_plugin table:<%s> has "
"no bool_expr in line:%s", __FUNCTION__,
__LINE__, table_name, line);
goto error;
}
@@ -346,8 +362,9 @@ bool_plugin_expr_new(struct bool_plugin_schema *schema, const char *table_name,
n_item++;
if (ret != 1 || n_item > MAX_ITEMS_PER_BOOL_EXPR) {
log_fatal(logger, MODULE_BOOL_PLUGIN,
"[%s:%d] bool_plugin table:<%s> has invalid format of "
"bool_expr in line:%s", __FUNCTION__, __LINE__, table_name, line);
"[%s:%d] bool_plugin table:<%s> has "
"invalid format of bool_expr in line:%s",
__FUNCTION__, __LINE__, table_name, line);
goto error;
}
}
@@ -366,13 +383,15 @@ error:
return NULL;
}
static void bool_plugin_expr_free(struct bool_expr *expr)
void bool_plugin_expr_free(void *bool_expr)
{
FREE(expr);
FREE(bool_expr);
}
int bool_plugin_runtime_update(void *bool_plugin_runtime, void *bool_plugin_schema,
const char *table_name, const char *line, int valid_column)
int bool_plugin_runtime_update(void *bool_plugin_runtime,
void *bool_plugin_schema,
const char *table_name,
const char *line, int valid_column)
{
if (NULL == bool_plugin_runtime || NULL == bool_plugin_schema ||
NULL == line) {
@@ -380,21 +399,27 @@ int bool_plugin_runtime_update(void *bool_plugin_runtime, void *bool_plugin_sche
}
struct bool_expr *bool_expr = NULL;
struct bool_plugin_schema *schema = (struct bool_plugin_schema *)bool_plugin_schema;
struct bool_plugin_runtime *bool_plugin_rt = (struct bool_plugin_runtime *)bool_plugin_runtime;
struct bool_plugin_schema *schema =
(struct bool_plugin_schema *)bool_plugin_schema;
struct bool_plugin_runtime *bool_plugin_rt =
(struct bool_plugin_runtime *)bool_plugin_runtime;
size_t item_id_offset = 0, item_id_len = 0;
int is_valid = get_column_value(line, valid_column);
if (is_valid < 0) {
log_fatal(bool_plugin_rt->logger, MODULE_BOOL_PLUGIN,
"[%s:%d] bool_plugin table:<%s> has no is_valid(column seq:%d)"
" in table_line:%s", __FUNCTION__, __LINE__, table_name,
"[%s:%d] bool_plugin table:<%s> has no "
"is_valid(column seq:%d) in table_line:%s",
__FUNCTION__, __LINE__, table_name,
valid_column, line);
bool_plugin_rt->update_err_cnt++;
return -1;
}
int ret = get_column_pos(line, schema->item_id_column, &item_id_offset, &item_id_len);
int ret = get_column_pos(line, schema->item_id_column,
&item_id_offset, &item_id_len);
if (ret < 0) {
log_fatal(bool_plugin_rt->logger, MODULE_BOOL_PLUGIN,
"[%s:%d] bool_plugin table:<%s> has no item_id(column seq:%d)"
@@ -407,7 +432,8 @@ int bool_plugin_runtime_update(void *bool_plugin_runtime, void *bool_plugin_sche
if (1 == schema->container_schema.set_flag) {
if (1 == is_valid) {
// add
bool_expr = bool_plugin_expr_new(schema, table_name, line, bool_plugin_rt->logger);
bool_expr = bool_plugin_expr_new(schema, table_name, line,
bool_plugin_rt->logger);
if (NULL == bool_expr) {
bool_plugin_rt->update_err_cnt++;
return -1;
@@ -416,23 +442,24 @@ int bool_plugin_runtime_update(void *bool_plugin_runtime, void *bool_plugin_sche
const char *key = line + item_id_offset;
size_t key_len = item_id_len;
ret = bool_plugin_runtime_update_row(bool_plugin_rt, table_name, line, key, key_len,
bool_expr, is_valid);
ret = bool_plugin_runtime_update_row(bool_plugin_rt, table_name, line,
key, key_len, bool_expr, is_valid);
if (ret < 0) {
if (bool_expr != NULL) {
bool_plugin_expr_free(bool_expr);
}
log_fatal(bool_plugin_rt->logger, MODULE_BOOL_PLUGIN,
"[%s:%d]bool_plugin table:<%s> update one line failed, "
"line:%s", __FUNCTION__, __LINE__, table_name, line);
bool_plugin_rt->update_err_cnt++;
return -1;
}
log_debug(bool_plugin_rt->logger, MODULE_BOOL_PLUGIN,
"bool_plugin table:<%s> update one line, key:%s, key_len:%zu, is_valid:%d",
table_name, key, key_len, is_valid);
"bool_plugin table:<%s> update one line, key:%s, key_len:%zu,"
" is_valid:%d", table_name, key, key_len, is_valid);
} else {
//ex_schema not set
ex_data_runtime_cache_row_put(bool_plugin_rt->ex_data_rt, line);
bool_plugin_rt->rule_num = ex_data_runtime_cached_row_count(bool_plugin_rt->ex_data_rt);
bool_plugin_rt->rule_num =
ex_data_runtime_cached_row_count(bool_plugin_rt->ex_data_rt);
}
return 0;
@@ -444,14 +471,17 @@ void garbage_bool_matcher_free(void *matcher, void *arg)
bool_matcher_free(bm);
}
int bool_plugin_runtime_commit(void *bool_plugin_runtime, const char *table_name,
int bool_plugin_runtime_commit(void *bool_plugin_runtime,
const char *table_name,
long long maat_rt_version)
{
if (NULL == bool_plugin_runtime) {
return -1;
}
struct bool_plugin_runtime *bool_plugin_rt = (struct bool_plugin_runtime *)bool_plugin_runtime;
struct bool_plugin_runtime *bool_plugin_rt =
(struct bool_plugin_runtime *)bool_plugin_runtime;
struct ex_data_runtime *ex_data_rt = bool_plugin_rt->ex_data_rt;
if (NULL == ex_data_rt) {
return -1;
@@ -464,12 +494,14 @@ int bool_plugin_runtime_commit(void *bool_plugin_runtime, const char *table_name
struct bool_expr *rules = NULL;
struct ex_container **ex_container = NULL;
size_t rule_cnt = ex_data_runtime_list_updating_ex_container(ex_data_rt, &ex_container);
size_t rule_cnt = ex_data_runtime_list_updating_ex_container(ex_data_rt,
&ex_container);
if (rule_cnt > 0) {
rules = ALLOC(struct bool_expr, rule_cnt);
for (size_t i = 0; i < rule_cnt; i++) {
rules[i] = *(struct bool_expr *)(struct bool_expr *)ex_container[i]->custom_data;
assert(rules[i].user_tag == ex_container[i] || NULL == rules[i].user_tag);
rules[i] = *(struct bool_expr *)ex_container[i]->custom_data;
assert(rules[i].user_tag == ex_container[i] ||
NULL == rules[i].user_tag);
rules[i].user_tag = ex_container[i];
}
}
@@ -489,15 +521,15 @@ int bool_plugin_runtime_commit(void *bool_plugin_runtime, const char *table_name
if (NULL == new_bool_matcher) {
log_fatal(bool_plugin_rt->logger, MODULE_BOOL_PLUGIN,
"[%s:%d] table[%s] rebuild bool_matcher engine failed when "
"update %zu bool_plugin rules", __FUNCTION__, __LINE__,
table_name, rule_cnt);
"[%s:%d] table[%s] rebuild bool_matcher engine failed"
" when update %zu bool_plugin rules", __FUNCTION__,
__LINE__, table_name, rule_cnt);
ret = -1;
} else {
log_info(bool_plugin_rt->logger, MODULE_BOOL_PLUGIN,
"table[%s] commit %zu bool_plugin rules and rebuild bool_matcher"
" completed, version:%lld, consume:%lldms", table_name, rule_cnt,
maat_rt_version, time_elapse_ms);
"table[%s] commit %zu bool_plugin rules and rebuild"
"bool_matcher completed, version:%lld, consume:%lldms",
table_name, rule_cnt, maat_rt_version, time_elapse_ms);
}
}
@@ -506,8 +538,8 @@ int bool_plugin_runtime_commit(void *bool_plugin_runtime, const char *table_name
ex_data_runtime_commit(ex_data_rt);
if (old_bool_matcher != NULL) {
maat_garbage_bagging(bool_plugin_rt->ref_garbage_bin, old_bool_matcher, NULL,
garbage_bool_matcher_free);
maat_garbage_bagging(bool_plugin_rt->ref_garbage_bin, old_bool_matcher,
NULL, garbage_bool_matcher_free);
}
bool_plugin_rt->rule_num = rule_cnt;
@@ -529,30 +561,38 @@ long long bool_plugin_runtime_rule_count(void *bool_plugin_runtime)
return 0;
}
struct bool_plugin_runtime *bool_plugin_rt = (struct bool_plugin_runtime *)bool_plugin_runtime;
struct bool_plugin_runtime *bool_plugin_rt =
(struct bool_plugin_runtime *)bool_plugin_runtime;
return bool_plugin_rt->rule_num;
}
struct ex_data_runtime *bool_plugin_runtime_get_ex_data_rt(void *bool_plugin_runtime)
struct ex_data_runtime *
bool_plugin_runtime_get_ex_data_rt(void *bool_plugin_runtime)
{
if (NULL == bool_plugin_runtime) {
return NULL;
}
struct bool_plugin_runtime *bool_plugin_rt = (struct bool_plugin_runtime *)bool_plugin_runtime;
struct bool_plugin_runtime *bool_plugin_rt =
(struct bool_plugin_runtime *)bool_plugin_runtime;
return bool_plugin_rt->ex_data_rt;
}
int bool_plugin_runtime_get_ex_data(void *bool_plugin_runtime, unsigned long long *item_ids,
size_t n_item, void **ex_data_array, size_t n_ex_data)
int bool_plugin_runtime_get_ex_data(void *bool_plugin_runtime,
unsigned long long *item_ids,
size_t n_item, void **ex_data_array,
size_t n_ex_data)
{
if (NULL == bool_plugin_runtime || NULL == ex_data_array ||
NULL == item_ids || 0 == n_item || 0 == n_ex_data) {
return -1;
}
struct bool_plugin_runtime *bool_plugin_rt = (struct bool_plugin_runtime *)bool_plugin_runtime;
struct bool_plugin_runtime *bool_plugin_rt =
(struct bool_plugin_runtime *)bool_plugin_runtime;
if (0 == bool_plugin_rt->rule_num) {
return 0;
}
@@ -564,10 +604,12 @@ int bool_plugin_runtime_get_ex_data(void *bool_plugin_runtime, unsigned long lon
struct bool_expr_match results[n_ex_data];
n_item = ull_dedup(item_ids, n_item);
int n_result = bool_matcher_match(bool_plugin_rt->matcher, item_ids, n_item, results, n_ex_data);
int n_result = bool_matcher_match(bool_plugin_rt->matcher, item_ids, n_item,
results, n_ex_data);
for (int i = 0; i < n_result; i++) {
ex_data_array[i] = ex_data_runtime_get_ex_data_by_container(bool_plugin_rt->ex_data_rt,
(struct ex_container *)results[i].user_tag);
ex_data_array[i] =
ex_data_runtime_get_ex_data_by_container(bool_plugin_rt->ex_data_rt,
(struct ex_container *)results[i].user_tag);
}
return n_result;
@@ -579,7 +621,9 @@ long long bool_plugin_runtime_update_err_count(void *bool_plugin_runtime)
return 0;
}
struct bool_plugin_runtime *bool_plugin_rt = (struct bool_plugin_runtime *)bool_plugin_runtime;
struct bool_plugin_runtime *bool_plugin_rt =
(struct bool_plugin_runtime *)bool_plugin_runtime;
return bool_plugin_rt->update_err_cnt;
}
@@ -589,7 +633,9 @@ void bool_plugin_runtime_scan_times_inc(void *bool_plugin_runtime, int thread_id
return;
}
struct bool_plugin_runtime *bool_plugin_rt = (struct bool_plugin_runtime *)bool_plugin_runtime;
struct bool_plugin_runtime *bool_plugin_rt =
(struct bool_plugin_runtime *)bool_plugin_runtime;
alignment_int64_array_add(bool_plugin_rt->scan_times, thread_id, 1);
}
@@ -599,10 +645,14 @@ long long bool_plugin_runtime_scan_times(void *bool_plugin_runtime)
return 0;
}
struct bool_plugin_runtime *bool_plugin_rt = (struct bool_plugin_runtime *)bool_plugin_runtime;
struct bool_plugin_runtime *bool_plugin_rt =
(struct bool_plugin_runtime *)bool_plugin_runtime;
long long sum = alignment_int64_array_sum(bool_plugin_rt->scan_times,
bool_plugin_rt->n_worker_thread);
alignment_int64_array_reset(bool_plugin_rt->scan_times, bool_plugin_rt->n_worker_thread);
alignment_int64_array_reset(bool_plugin_rt->scan_times,
bool_plugin_rt->n_worker_thread);
return sum;
}

View File

@@ -83,8 +83,9 @@ void *fqdn_plugin_schema_new(cJSON *json, struct table_manager *tbl_mgr,
schema->suffix_match_method_column = custom_item->valueint;
} else {
log_fatal(logger, MODULE_FQDN_PLUGIN,
"[%s:%d] fqdn_plugin table:<%s> schema has no suffix_match_method column",
__FUNCTION__, __LINE__, table_name);
"[%s:%d] fqdn_plugin table:<%s> schema has no "
"suffix_match_method column", __FUNCTION__,
__LINE__, table_name);
goto error;
}
@@ -158,7 +159,8 @@ int fqdn_plugin_table_set_ex_container_schema(void *fqdn_plugin_schema, int tabl
struct ex_container_schema *
fqdn_plugin_table_get_ex_container_schema(void *fqdn_plugin_schema)
{
struct fqdn_plugin_schema *schema = (struct fqdn_plugin_schema *)fqdn_plugin_schema;
struct fqdn_plugin_schema *schema =
(struct fqdn_plugin_schema *)fqdn_plugin_schema;
return &(schema->container_schema);
}
@@ -188,8 +190,9 @@ void *fqdn_plugin_runtime_new(void *fqdn_plugin_schema, size_t max_thread_num,
struct fqdn_plugin_schema *schema = (struct fqdn_plugin_schema *)fqdn_plugin_schema;
struct fqdn_plugin_runtime *fqdn_plugin_rt = ALLOC(struct fqdn_plugin_runtime, 1);
fqdn_plugin_rt->ex_data_rt = ex_data_runtime_new(schema->table_id, schema->gc_timeout_s,
logger);
fqdn_plugin_rt->ex_data_rt =
ex_data_runtime_new(schema->table_id, schema->gc_timeout_s, logger);
if (1 == schema->container_schema.set_flag) {
ex_data_runtime_set_ex_container_schema(fqdn_plugin_rt->ex_data_rt,
&(schema->container_schema));
@@ -209,7 +212,9 @@ void fqdn_plugin_runtime_free(void *fqdn_plugin_runtime)
return;
}
struct fqdn_plugin_runtime *fqdn_plugin_rt = (struct fqdn_plugin_runtime *)fqdn_plugin_runtime;
struct fqdn_plugin_runtime *fqdn_plugin_rt =
(struct fqdn_plugin_runtime *)fqdn_plugin_runtime;
if (fqdn_plugin_rt->engine != NULL) {
FQDN_engine_free(fqdn_plugin_rt->engine);
fqdn_plugin_rt->engine = NULL;
@@ -228,9 +233,10 @@ void fqdn_plugin_runtime_free(void *fqdn_plugin_runtime)
FREE(fqdn_plugin_rt);
}
static int fqdn_plugin_accept_tag_match(struct fqdn_plugin_schema *schema,
const char *table_name, const char *line,
struct log_handle *logger)
static int
fqdn_plugin_accept_tag_match(struct fqdn_plugin_schema *schema,
const char *table_name, const char *line,
struct log_handle *logger)
{
size_t column_offset = 0;
size_t column_len = 0;
@@ -241,8 +247,9 @@ static int fqdn_plugin_accept_tag_match(struct fqdn_plugin_schema *schema,
&column_offset, &column_len);
if (ret < 0) {
log_fatal(logger, MODULE_FQDN_PLUGIN,
"[%s:%d] fqdn_plugin table:<%s> has no rule_tag in line:%s",
__FUNCTION__, __LINE__, table_name, line);
"[%s:%d] fqdn_plugin table:<%s> has no rule_tag"
" in line:%s", __FUNCTION__, __LINE__, table_name,
line);
return TAG_MATCH_ERR;
}
@@ -253,15 +260,17 @@ static int fqdn_plugin_accept_tag_match(struct fqdn_plugin_schema *schema,
FREE(tag_str);
if (TAG_MATCH_ERR == ret) {
log_fatal(logger, MODULE_FQDN_PLUGIN,
"[%s:%d] fqdn_plugin table:<%s> has invalid tag format in line:%s",
__FUNCTION__, __LINE__, table_name, line);
"[%s:%d] fqdn_plugin table:<%s> has invalid "
"tag format in line:%s", __FUNCTION__, __LINE__,
table_name, line);
return TAG_MATCH_ERR;
}
if (TAG_MATCH_UNMATCHED == ret) {
log_fatal(logger, MODULE_FQDN_PLUGIN,
"[%s:%d] fqdn_plugin table:<%s> has unmatched tag in line:%s",
__FUNCTION__, __LINE__, table_name, line);
"[%s:%d] fqdn_plugin table:<%s> has unmatched tag"
" in line:%s", __FUNCTION__, __LINE__, table_name,
line);
return TAG_MATCH_UNMATCHED;
}
}
@@ -285,7 +294,8 @@ fqdn_plugin_rule_new(const char *line, struct fqdn_plugin_schema *schema,
size_t fqdn_len = 0;
struct FQDN_rule *fqdn_plugin_rule = ALLOC(struct FQDN_rule, 1);
ret = get_column_pos(line, schema->item_id_column, &column_offset, &column_len);
ret = get_column_pos(line, schema->item_id_column,
&column_offset, &column_len);
if (ret < 0) {
log_fatal(logger, MODULE_FQDN_PLUGIN,
"[%s:%d] fqdn_plugin table:<%s> has no item_id in line:%s",
@@ -294,11 +304,12 @@ fqdn_plugin_rule_new(const char *line, struct fqdn_plugin_schema *schema,
}
fqdn_plugin_rule->id = atoi(line + column_offset);
ret = get_column_pos(line, schema->suffix_match_method_column, &column_offset, &column_len);
ret = get_column_pos(line, schema->suffix_match_method_column,
&column_offset, &column_len);
if (ret < 0) {
log_fatal(logger, MODULE_FQDN_PLUGIN,
"[%s:%d] fqdn_plugin table:<%s> has no suffix_match_method in line:%s",
__FUNCTION__, __LINE__, table_name, line);
"[%s:%d] fqdn_plugin table:<%s> has no suffix_match_method"
" in line:%s", __FUNCTION__, __LINE__, table_name, line);
goto error;
}
@@ -306,12 +317,14 @@ fqdn_plugin_rule_new(const char *line, struct fqdn_plugin_schema *schema,
if (fqdn_plugin_rule->is_suffix_match != 0 &&
fqdn_plugin_rule->is_suffix_match != 1) {
log_fatal(logger, MODULE_FQDN_PLUGIN,
"[%s:%d] fqdn_plugin table:<%s> suffix_match_method:%d is illegal in line:%s",
__FUNCTION__, __LINE__, table_name, fqdn_plugin_rule->is_suffix_match, line);
"[%s:%d] fqdn_plugin table:<%s> suffix_match_method:%d "
"is illegal in line:%s", __FUNCTION__, __LINE__, table_name,
fqdn_plugin_rule->is_suffix_match, line);
goto error;
}
ret = get_column_pos(line, schema->fqdn_column, &column_offset, &column_len);
ret = get_column_pos(line, schema->fqdn_column,
&column_offset, &column_len);
if (ret < 0) {
log_fatal(logger, MODULE_FQDN_PLUGIN,
"[%s:%d] fqdn_plugin table:<%s> has no fqdn in line:%s",
@@ -341,24 +354,12 @@ error:
return NULL;
}
static void fqdn_plugin_rule_free(struct FQDN_rule *rule)
{
if (NULL == rule) {
return;
}
if (rule->FQDN != NULL) {
FREE(rule->FQDN);
}
FREE(rule);
}
static int fqdn_plugin_runtime_update_row(struct fqdn_plugin_runtime *fqdn_plugin_rt,
const char *table_name, const char *row,
const char *key, size_t key_len,
struct FQDN_rule *fqdn_plugin_rule,
int is_valid)
static int
fqdn_plugin_runtime_update_row(struct fqdn_plugin_runtime *fqdn_plugin_rt,
const char *table_name, const char *row,
const char *key, size_t key_len,
struct FQDN_rule *fqdn_plugin_rule,
int is_valid)
{
int ret = -1;
struct ex_data_runtime *ex_data_rt = fqdn_plugin_rt->ex_data_rt;
@@ -371,10 +372,13 @@ static int fqdn_plugin_runtime_update_row(struct fqdn_plugin_runtime *fqdn_plugi
}
} else {
// add
void *ex_data = ex_data_runtime_row2ex_data(ex_data_rt, table_name, row,
key, key_len);
struct ex_container *ex_container = ex_container_new(ex_data, (void *)fqdn_plugin_rule);
ret = ex_data_runtime_add_ex_container(ex_data_rt, key, key_len, ex_container);
void *ex_data = ex_data_runtime_row2ex_data(ex_data_rt, table_name,
row, key, key_len);
struct ex_container *ex_container =
ex_container_new(ex_data, (void *)fqdn_plugin_rule);
ret = ex_data_runtime_add_ex_container(ex_data_rt, key, key_len,
ex_container);
if (ret < 0) {
ex_container_free(ex_data_rt, ex_container);
return -1;
@@ -384,8 +388,11 @@ static int fqdn_plugin_runtime_update_row(struct fqdn_plugin_runtime *fqdn_plugi
return 0;
}
int fqdn_plugin_runtime_update(void *fqdn_plugin_runtime, void *fqdn_plugin_schema,
const char *table_name, const char *line, int valid_column)
int fqdn_plugin_runtime_update(void *fqdn_plugin_runtime,
void *fqdn_plugin_schema,
const char *table_name,
const char *line,
int valid_column)
{
if (NULL == fqdn_plugin_runtime || NULL == fqdn_plugin_schema ||
NULL == line) {
@@ -393,8 +400,12 @@ int fqdn_plugin_runtime_update(void *fqdn_plugin_runtime, void *fqdn_plugin_sche
}
struct FQDN_rule *fqdn_plugin_rule = NULL;
struct fqdn_plugin_schema *schema = (struct fqdn_plugin_schema *)fqdn_plugin_schema;
struct fqdn_plugin_runtime *fqdn_plugin_rt = (struct fqdn_plugin_runtime *)fqdn_plugin_runtime;
struct fqdn_plugin_schema *schema =
(struct fqdn_plugin_schema *)fqdn_plugin_schema;
struct fqdn_plugin_runtime *fqdn_plugin_rt =
(struct fqdn_plugin_runtime *)fqdn_plugin_runtime;
size_t item_id_offset = 0, item_id_len = 0;
int is_valid = get_column_value(line, valid_column);
@@ -407,7 +418,8 @@ int fqdn_plugin_runtime_update(void *fqdn_plugin_runtime, void *fqdn_plugin_sche
return -1;
}
int ret = get_column_pos(line, schema->item_id_column, &item_id_offset, &item_id_len);
int ret = get_column_pos(line, schema->item_id_column,
&item_id_offset, &item_id_len);
if (ret < 0) {
log_fatal(fqdn_plugin_rt->logger, MODULE_FQDN_PLUGIN,
"[%s:%d] fqdn_plugin table:<%s> has no item_id(column seq:%d)"
@@ -430,23 +442,25 @@ int fqdn_plugin_runtime_update(void *fqdn_plugin_runtime, void *fqdn_plugin_sche
const char *key = line + item_id_offset;
size_t key_len = item_id_len;
ret = fqdn_plugin_runtime_update_row(fqdn_plugin_rt, table_name, line, key, key_len,
fqdn_plugin_rule, is_valid);
ret = fqdn_plugin_runtime_update_row(fqdn_plugin_rt, table_name, line,
key, key_len, fqdn_plugin_rule,
is_valid);
if (ret < 0) {
if (fqdn_plugin_rule != NULL) {
fqdn_plugin_rule_free(fqdn_plugin_rule);
}
log_fatal(fqdn_plugin_rt->logger, MODULE_FQDN_PLUGIN,
"[%s:%d]fqdn_plugin table:<%s> update one line failed, "
"line:%s", __FUNCTION__, __LINE__, table_name, line);
fqdn_plugin_rt->update_err_cnt++;
return -1;
}
log_debug(fqdn_plugin_rt->logger, MODULE_FQDN_PLUGIN,
"fqdn_plugin table:<%s> update one line, key:%s, key_len:%zu, is_valid:%d",
table_name, key, key_len, is_valid);
"fqdn_plugin table:<%s> update one line, key:%s, key_len:%zu,"
"is_valid:%d", table_name, key, key_len, is_valid);
} else {
//ex_schema not set
ex_data_runtime_cache_row_put(fqdn_plugin_rt->ex_data_rt, line);
fqdn_plugin_rt->rule_num = ex_data_runtime_cached_row_count(fqdn_plugin_rt->ex_data_rt);
fqdn_plugin_rt->rule_num =
ex_data_runtime_cached_row_count(fqdn_plugin_rt->ex_data_rt);
}
return 0;
@@ -458,14 +472,17 @@ static void garbage_fqdn_engine_free(void *fqdn_engine, void *arg)
FQDN_engine_free(engine);
}
int fqdn_plugin_runtime_commit(void *fqdn_plugin_runtime, const char *table_name,
int fqdn_plugin_runtime_commit(void *fqdn_plugin_runtime,
const char *table_name,
long long maat_rt_version)
{
if (NULL == fqdn_plugin_runtime) {
return -1;
}
struct fqdn_plugin_runtime *fqdn_plugin_rt = (struct fqdn_plugin_runtime *)fqdn_plugin_runtime;
struct fqdn_plugin_runtime *fqdn_plugin_rt =
(struct fqdn_plugin_runtime *)fqdn_plugin_runtime;
struct ex_data_runtime *ex_data_rt = fqdn_plugin_rt->ex_data_rt;
if (NULL == ex_data_rt) {
return -1;
@@ -478,12 +495,14 @@ int fqdn_plugin_runtime_commit(void *fqdn_plugin_runtime, const char *table_name
struct FQDN_rule *rules = NULL;
struct ex_container **ex_container = NULL;
size_t rule_cnt = ex_data_runtime_list_updating_ex_container(ex_data_rt, &ex_container);
size_t rule_cnt = ex_data_runtime_list_updating_ex_container(ex_data_rt,
&ex_container);
if (rule_cnt > 0) {
rules = ALLOC(struct FQDN_rule, rule_cnt);
for (size_t i = 0; i < rule_cnt; i++) {
rules[i] = *(struct FQDN_rule *)ex_container[i]->custom_data;
assert(rules[i].user_tag == ex_container[i] || rules[i].user_tag == NULL);
assert(rules[i].user_tag == ex_container[i] ||
rules[i].user_tag == NULL);
rules[i].user_tag = ex_container[i];
}
}
@@ -508,9 +527,9 @@ int fqdn_plugin_runtime_commit(void *fqdn_plugin_runtime, const char *table_name
ret = -1;
} else {
log_info(fqdn_plugin_rt->logger, MODULE_FQDN_PLUGIN,
"table[%s] commit %zu fqdn_plugin rules and rebuild FQDN engine"
" completed, version:%lld, consume:%lldms", table_name, rule_cnt,
maat_rt_version, time_elapse_ms);
"table[%s] commit %zu fqdn_plugin rules and rebuild"
" FQDN engine completed, version:%lld, consume:%lldms",
table_name, rule_cnt, maat_rt_version, time_elapse_ms);
}
}
@@ -519,8 +538,8 @@ int fqdn_plugin_runtime_commit(void *fqdn_plugin_runtime, const char *table_name
ex_data_runtime_commit(ex_data_rt);
if (old_fqdn_engine != NULL) {
maat_garbage_bagging(fqdn_plugin_rt->ref_garbage_bin, old_fqdn_engine, NULL,
garbage_fqdn_engine_free);
maat_garbage_bagging(fqdn_plugin_rt->ref_garbage_bin, old_fqdn_engine,
NULL, garbage_fqdn_engine_free);
}
fqdn_plugin_rt->rule_num = rule_cnt;
@@ -542,31 +561,39 @@ long long fqdn_plugin_runtime_rule_count(void *fqdn_plugin_runtime)
return 0;
}
struct fqdn_plugin_runtime *fqdn_plugin_rt = (struct fqdn_plugin_runtime *)fqdn_plugin_runtime;
struct fqdn_plugin_runtime *fqdn_plugin_rt =
(struct fqdn_plugin_runtime *)fqdn_plugin_runtime;
return fqdn_plugin_rt->rule_num;
}
struct ex_data_runtime *fqdn_plugin_runtime_get_ex_data_rt(void *fqdn_plugin_runtime)
struct ex_data_runtime *
fqdn_plugin_runtime_get_ex_data_rt(void *fqdn_plugin_runtime)
{
if (NULL == fqdn_plugin_runtime) {
return NULL;
}
struct fqdn_plugin_runtime *fqdn_plugin_rt = (struct fqdn_plugin_runtime *)fqdn_plugin_runtime;
struct fqdn_plugin_runtime *fqdn_plugin_rt =
(struct fqdn_plugin_runtime *)fqdn_plugin_runtime;
return fqdn_plugin_rt->ex_data_rt;
}
int fqdn_plugin_runtime_get_ex_data(void *fqdn_plugin_runtime, const char *query_fqdn,
void **ex_data_array, size_t n_ex_data)
int fqdn_plugin_runtime_get_ex_data(void *fqdn_plugin_runtime,
const char *query_fqdn,
void **ex_data_array,
size_t n_ex_data)
{
if (NULL == fqdn_plugin_runtime || NULL == query_fqdn ||
NULL == ex_data_array || 0 == n_ex_data) {
return -1;
}
struct fqdn_plugin_runtime *fqdn_plugin_rt = (struct fqdn_plugin_runtime *)fqdn_plugin_runtime;
struct fqdn_plugin_runtime *fqdn_plugin_rt =
(struct fqdn_plugin_runtime *)fqdn_plugin_runtime;
if (0 == fqdn_plugin_rt->rule_num) {
return 0;
}
@@ -576,11 +603,12 @@ int fqdn_plugin_runtime_get_ex_data(void *fqdn_plugin_runtime, const char *query
}
struct FQDN_match results[n_ex_data];
int n_result = FQDN_engine_search(fqdn_plugin_rt->engine, query_fqdn, strlen(query_fqdn),
results, n_ex_data);
int n_result = FQDN_engine_search(fqdn_plugin_rt->engine, query_fqdn,
strlen(query_fqdn), results, n_ex_data);
for (int i = 0; i < n_result; i++) {
ex_data_array[i] = ex_data_runtime_get_ex_data_by_container(fqdn_plugin_rt->ex_data_rt,
(struct ex_container *)results[i].user_tag);
ex_data_array[i] =
ex_data_runtime_get_ex_data_by_container(fqdn_plugin_rt->ex_data_rt,
(struct ex_container *)results[i].user_tag);
}
return n_result;
@@ -592,7 +620,9 @@ long long fqdn_plugin_runtime_update_err_count(void *fqdn_plugin_runtime)
return 0;
}
struct fqdn_plugin_runtime *fqdn_plugin_rt = (struct fqdn_plugin_runtime *)fqdn_plugin_runtime;
struct fqdn_plugin_runtime *fqdn_plugin_rt =
(struct fqdn_plugin_runtime *)fqdn_plugin_runtime;
return fqdn_plugin_rt->update_err_cnt;
}
@@ -602,7 +632,9 @@ void fqdn_plugin_runtime_scan_times_inc(void *fqdn_plugin_runtime, int thread_id
return;
}
struct fqdn_plugin_runtime *fqdn_plugin_rt = (struct fqdn_plugin_runtime *)fqdn_plugin_runtime;
struct fqdn_plugin_runtime *fqdn_plugin_rt =
(struct fqdn_plugin_runtime *)fqdn_plugin_runtime;
alignment_int64_array_add(fqdn_plugin_rt->scan_times, thread_id, 1);
}
@@ -612,9 +644,12 @@ long long fqdn_plugin_runtime_scan_times(void *fqdn_plugin_runtime)
return 0;
}
struct fqdn_plugin_runtime *fqdn_plugin_rt = (struct fqdn_plugin_runtime *)fqdn_plugin_runtime;
struct fqdn_plugin_runtime *fqdn_plugin_rt =
(struct fqdn_plugin_runtime *)fqdn_plugin_runtime;
long long sum = alignment_int64_array_sum(fqdn_plugin_rt->scan_times,
fqdn_plugin_rt->n_worker_thread);
alignment_int64_array_reset(fqdn_plugin_rt->scan_times,
fqdn_plugin_rt->n_worker_thread);

View File

@@ -58,16 +58,18 @@ void *ip_plugin_schema_new(cJSON *json, struct table_manager *tbl_mgr,
schema->table_id = item->valueint;
} else {
log_fatal(logger, MODULE_IP_PLUGIN,
"[%s:%d] ip_plugin table:<%s> schema has no table_id column",
__FUNCTION__, __LINE__, table_name);
"[%s:%d] ip_plugin table:<%s> schema has no"
" table_id column", __FUNCTION__, __LINE__,
table_name);
goto error;
}
item = cJSON_GetObjectItem(json, "custom");
if (NULL == item || item->type != cJSON_Object) {
log_fatal(logger, MODULE_IP_PLUGIN,
"[%s:%d] ip_plugin table:<%s> schema has no custom column",
__FUNCTION__, __LINE__, table_name);
"[%s:%d] ip_plugin table:<%s> schema has no"
" custom column", __FUNCTION__, __LINE__,
table_name);
goto error;
}
@@ -76,8 +78,9 @@ void *ip_plugin_schema_new(cJSON *json, struct table_manager *tbl_mgr,
schema->item_id_column = custom_item->valueint;
} else {
log_fatal(logger, MODULE_IP_PLUGIN,
"[%s:%d] ip_plugin table:<%s> schema has no item_id column",
__FUNCTION__, __LINE__, table_name);
"[%s:%d] ip_plugin table:<%s> schema has no"
" item_id column", __FUNCTION__, __LINE__,
table_name);
goto error;
}
@@ -86,7 +89,8 @@ void *ip_plugin_schema_new(cJSON *json, struct table_manager *tbl_mgr,
schema->ip_type_column = custom_item->valueint;
} else {
log_fatal(logger, MODULE_IP_PLUGIN,
"[%s:%d] ip_plugin table:<%s> schema has no ip_type column",
"[%s:%d] ip_plugin table:<%s> schema has no"
" ip_type column",
__FUNCTION__, __LINE__, table_name);
goto error;
}
@@ -96,8 +100,9 @@ void *ip_plugin_schema_new(cJSON *json, struct table_manager *tbl_mgr,
schema->start_ip_column = custom_item->valueint;
} else {
log_fatal(logger, MODULE_IP_PLUGIN,
"[%s:%d] ip_plugin table:<%s> schema has no start_ip column",
__FUNCTION__, __LINE__, table_name);
"[%s:%d] ip_plugin table:<%s> schema has no"
" start_ip column", __FUNCTION__, __LINE__,
table_name);
goto error;
}
@@ -106,8 +111,9 @@ void *ip_plugin_schema_new(cJSON *json, struct table_manager *tbl_mgr,
schema->end_ip_column = custom_item->valueint;
} else {
log_fatal(logger, MODULE_IP_PLUGIN,
"[%s:%d] ip_plugin table:<%s> schema has no end_ip column",
__FUNCTION__, __LINE__, table_name);
"[%s:%d] ip_plugin table:<%s> schema has no"
" end_ip column", __FUNCTION__, __LINE__,
table_name);
goto error;
}
@@ -140,9 +146,10 @@ void ip_plugin_schema_free(void *ip_plugin_schema)
FREE(ip_plugin_schema);
}
static int ip_plugin_accept_tag_match(struct ip_plugin_schema *schema,
const char *table_name, const char *line,
struct log_handle *logger)
static int
ip_plugin_accept_tag_match(struct ip_plugin_schema *schema,
const char *table_name, const char *line,
struct log_handle *logger)
{
size_t column_offset = 0;
size_t column_len = 0;
@@ -165,8 +172,8 @@ static int ip_plugin_accept_tag_match(struct ip_plugin_schema *schema,
FREE(tag_str);
if (TAG_MATCH_ERR == ret) {
log_fatal(logger, MODULE_IP_PLUGIN,
"[%s:%d] ip_plugin table:<%s> has invalid tag format in line:%s",
__FUNCTION__, __LINE__, table_name, line);
"[%s:%d] ip_plugin table:<%s> has invalid tag format"
" in line:%s", __FUNCTION__, __LINE__, table_name, line);
return TAG_MATCH_ERR;
}
@@ -197,7 +204,8 @@ ip_plugin_rule_new(struct ip_plugin_schema *schema, const char *table_name,
char end_ip_str[40] = {0};
struct ip_rule *ip_plugin_rule = ALLOC(struct ip_rule, 1);
ret = get_column_pos(line, schema->item_id_column, &column_offset, &column_len);
ret = get_column_pos(line, schema->item_id_column,
&column_offset, &column_len);
if (ret < 0) {
log_fatal(logger, MODULE_IP_PLUGIN,
"[%s:%d] ip_plugin table:<%s> has no item_id in line:%s",
@@ -206,7 +214,8 @@ ip_plugin_rule_new(struct ip_plugin_schema *schema, const char *table_name,
}
ip_plugin_rule->rule_id = atoll(line + column_offset);
ret = get_column_pos(line, schema->ip_type_column, &column_offset, &column_len);
ret = get_column_pos(line, schema->ip_type_column,
&column_offset, &column_len);
if (ret < 0) {
log_fatal(logger, MODULE_IP_PLUGIN,
"[%s:%d] ip_plugin table:<%s> has no ip_type in line:%s",
@@ -214,14 +223,17 @@ ip_plugin_rule_new(struct ip_plugin_schema *schema, const char *table_name,
goto error;
}
ip_plugin_rule->type = atoi(line + column_offset);
if (ip_plugin_rule->type != IPv4 && ip_plugin_rule->type != IPv6) {
if (ip_plugin_rule->type != IPv4 &&
ip_plugin_rule->type != IPv6) {
log_fatal(logger, MODULE_IP_PLUGIN,
"[%s:%d] ip_plugin table:<%s> ip_type[%d] invalid in line:%s",
__FUNCTION__, __LINE__, table_name, ip_plugin_rule->type, line);
"[%s:%d] ip_plugin table:<%s> ip_type[%d] invalid"
" in line:%s", __FUNCTION__, __LINE__, table_name,
ip_plugin_rule->type, line);
goto error;
}
ret = get_column_pos(line, schema->start_ip_column, &column_offset, &column_len);
ret = get_column_pos(line, schema->start_ip_column,
&column_offset, &column_len);
if (ret < 0) {
log_fatal(logger, MODULE_IP_PLUGIN,
"[%s:%d] ip_plugin table:<%s> has no start_ip in line:%s",
@@ -230,7 +242,8 @@ ip_plugin_rule_new(struct ip_plugin_schema *schema, const char *table_name,
}
strncpy(start_ip_str, line + column_offset, column_len);
ret = get_column_pos(line, schema->end_ip_column, &column_offset, &column_len);
ret = get_column_pos(line, schema->end_ip_column,
&column_offset, &column_len);
if (ret < 0) {
log_fatal(logger, MODULE_IP_PLUGIN,
"[%s:%d] ip_plugin table:<%s> has no end_ip in line:%s",
@@ -246,8 +259,9 @@ ip_plugin_rule_new(struct ip_plugin_schema *schema, const char *table_name,
&ip_plugin_rule->ipv4_rule.end_ip);
if (ret < 0) {
log_fatal(logger, MODULE_IP_PLUGIN,
"[%s:%d] ip_plugin table:<%s>> ip_format2range(ip4) failed in line:%s",
__FUNCTION__, __LINE__, table_name, line);
"[%s:%d] ip_plugin table:<%s>> ip_format2range(ip4)"
" failed in line:%s", __FUNCTION__, __LINE__,
table_name, line);
goto error;
}
} else {
@@ -258,8 +272,9 @@ ip_plugin_rule_new(struct ip_plugin_schema *schema, const char *table_name,
ip_plugin_rule->ipv6_rule.end_ip);
if (ret < 0) {
log_fatal(logger, MODULE_IP_PLUGIN,
"[%s:%d] ip_plugin table:<%s> ip_format2range(ip6) failed in line:%s",
__FUNCTION__, __LINE__, table_name, line);
"[%s:%d] ip_plugin table:<%s> ip_format2range(ip6)"
" failed in line:%s", __FUNCTION__, __LINE__,
table_name, line);
goto error;
}
}
@@ -270,9 +285,9 @@ error:
return NULL;
}
static void ip_plugin_rule_free(struct ip_rule *rule)
void ip_plugin_rule_free(void *ip_rule)
{
FREE(rule);
FREE(ip_rule);
}
int ip_plugin_table_set_ex_container_schema(void *ip_plugin_schema, int table_id,
@@ -286,7 +301,8 @@ int ip_plugin_table_set_ex_container_schema(void *ip_plugin_schema, int table_id
if (1 == schema->container_schema.set_flag) {
log_fatal(schema->logger, MODULE_IP_PLUGIN,
"[%s:%d] ip_plugin table(table_id:%d) ex_container_schema has been set, can't set again",
"[%s:%d] ip_plugin table(table_id:%d) ex_container_schema"
" has been set, can't set again",
__FUNCTION__, __LINE__, table_id);
return -1;
}
@@ -303,17 +319,20 @@ int ip_plugin_table_set_ex_container_schema(void *ip_plugin_schema, int table_id
return 0;
}
struct ex_container_schema *ip_plugin_table_get_ex_container_schema(void *ip_plugin_schema)
struct ex_container_schema *
ip_plugin_table_get_ex_container_schema(void *ip_plugin_schema)
{
struct ip_plugin_schema *schema = (struct ip_plugin_schema *)ip_plugin_schema;
struct ip_plugin_schema *schema =
(struct ip_plugin_schema *)ip_plugin_schema;
return &(schema->container_schema);
}
static int ip_plugin_runtime_update_row(struct ip_plugin_runtime *ip_plugin_rt,
const char *table_name, const char *row,
const char *key, size_t key_len,
struct ip_rule *ip_plugin_rule, int is_valid)
static int
ip_plugin_runtime_update_row(struct ip_plugin_runtime *ip_plugin_rt,
const char *table_name, const char *row,
const char *key, size_t key_len,
struct ip_rule *ip_plugin_rule, int is_valid)
{
int ret = -1;
struct ex_data_runtime *ex_data_rt = ip_plugin_rt->ex_data_rt;
@@ -329,9 +348,13 @@ static int ip_plugin_runtime_update_row(struct ip_plugin_runtime *ip_plugin_rt,
}
} else {
// add
void *ex_data = ex_data_runtime_row2ex_data(ex_data_rt, table_name, row, key, key_len);
struct ex_container *ex_container = ex_container_new(ex_data, (void *)ip_plugin_rule);
ret = ex_data_runtime_add_ex_container(ex_data_rt, key, key_len, ex_container);
void *ex_data = ex_data_runtime_row2ex_data(ex_data_rt, table_name,
row, key, key_len);
struct ex_container *ex_container =
ex_container_new(ex_data, (void *)ip_plugin_rule);
ret = ex_data_runtime_add_ex_container(ex_data_rt, key, key_len,
ex_container);
if (ret < 0) {
ex_container_free(ex_data_rt, ex_container);
return -1;
@@ -349,11 +372,14 @@ void *ip_plugin_runtime_new(void *ip_plugin_schema, size_t max_thread_num,
return NULL;
}
struct ip_plugin_schema *schema = (struct ip_plugin_schema *)ip_plugin_schema;
struct ip_plugin_schema *schema =
(struct ip_plugin_schema *)ip_plugin_schema;
struct ip_plugin_runtime *ip_plugin_rt = ALLOC(struct ip_plugin_runtime, 1);
ip_plugin_rt->ex_data_rt = ex_data_runtime_new(schema->table_id, schema->gc_timeout_s,
logger);
ip_plugin_rt->ex_data_rt =
ex_data_runtime_new(schema->table_id, schema->gc_timeout_s, logger);
if (1 == schema->container_schema.set_flag) {
ex_data_runtime_set_ex_container_schema(ip_plugin_rt->ex_data_rt,
&(schema->container_schema));
@@ -373,7 +399,9 @@ void ip_plugin_runtime_free(void *ip_plugin_runtime)
return;
}
struct ip_plugin_runtime *ip_plugin_rt = (struct ip_plugin_runtime *)ip_plugin_runtime;
struct ip_plugin_runtime *ip_plugin_rt =
(struct ip_plugin_runtime *)ip_plugin_runtime;
if (ip_plugin_rt->ip_matcher != NULL) {
ip_matcher_free(ip_plugin_rt->ip_matcher);
ip_plugin_rt->ip_matcher = NULL;
@@ -402,8 +430,12 @@ int ip_plugin_runtime_update(void *ip_plugin_runtime, void *ip_plugin_schema,
}
struct ip_rule *ip_plugin_rule = NULL;
struct ip_plugin_schema *schema = (struct ip_plugin_schema *)ip_plugin_schema;
struct ip_plugin_runtime *ip_plugin_rt = (struct ip_plugin_runtime *)ip_plugin_runtime;
struct ip_plugin_schema *schema =
(struct ip_plugin_schema *)ip_plugin_schema;
struct ip_plugin_runtime *ip_plugin_rt =
(struct ip_plugin_runtime *)ip_plugin_runtime;
size_t item_id_offset = 0, item_id_len = 0;
int is_valid = get_column_value(line, valid_column);
@@ -412,8 +444,8 @@ int ip_plugin_runtime_update(void *ip_plugin_runtime, void *ip_plugin_schema,
return -1;
}
int ret = get_column_pos(line, schema->item_id_column, &item_id_offset,
&item_id_len);
int ret = get_column_pos(line, schema->item_id_column,
&item_id_offset, &item_id_len);
if (ret < 0) {
ip_plugin_rt->update_err_cnt++;
return -1;
@@ -432,23 +464,24 @@ int ip_plugin_runtime_update(void *ip_plugin_runtime, void *ip_plugin_schema,
const char *key = line + item_id_offset;
size_t key_len = item_id_len;
ret = ip_plugin_runtime_update_row(ip_plugin_rt, table_name, line, key, key_len,
ip_plugin_rule, is_valid);
ret = ip_plugin_runtime_update_row(ip_plugin_rt, table_name, line, key,
key_len, ip_plugin_rule, is_valid);
if (ret < 0) {
if (ip_plugin_rule != NULL) {
ip_plugin_rule_free(ip_plugin_rule);
}
log_fatal(ip_plugin_rt->logger, MODULE_IP_PLUGIN,
"[%s:%d]ip_plugin table:<%s> update one line failed, "
"line:%s", __FUNCTION__, __LINE__, table_name, line);
ip_plugin_rt->update_err_cnt++;
return -1;
}
log_debug(ip_plugin_rt->logger, MODULE_IP_PLUGIN,
"ip_plugin table:<%s> update one line, key:%s, key_len:%zu, is_valid:%d",
table_name, key, key_len, is_valid);
"ip_plugin table:<%s> update one line, key:%s, key_len:%zu,"
" is_valid:%d", table_name, key, key_len, is_valid);
} else {
//ex_schema not set
ex_data_runtime_cache_row_put(ip_plugin_rt->ex_data_rt, line);
ip_plugin_rt->rule_num = ex_data_runtime_cached_row_count(ip_plugin_rt->ex_data_rt);
ip_plugin_rt->rule_num =
ex_data_runtime_cached_row_count(ip_plugin_rt->ex_data_rt);
}
return 0;
@@ -461,7 +494,9 @@ int ip_plugin_runtime_commit(void *ip_plugin_runtime, const char *table_name,
return -1;
}
struct ip_plugin_runtime *ip_plugin_rt = (struct ip_plugin_runtime *)ip_plugin_runtime;
struct ip_plugin_runtime *ip_plugin_rt =
(struct ip_plugin_runtime *)ip_plugin_runtime;
struct ex_data_runtime *ex_data_rt = ip_plugin_rt->ex_data_rt;
if (NULL == ex_data_rt) {
return -1;
@@ -474,12 +509,14 @@ int ip_plugin_runtime_commit(void *ip_plugin_runtime, const char *table_name,
struct ip_rule *rules = NULL;
struct ex_container **ex_container = NULL;
size_t rule_cnt = ex_data_runtime_list_updating_ex_container(ex_data_rt, &ex_container);
size_t rule_cnt = ex_data_runtime_list_updating_ex_container(ex_data_rt,
&ex_container);
if (rule_cnt > 0) {
rules = ALLOC(struct ip_rule, rule_cnt);
for (size_t i = 0; i < rule_cnt; i++) {
rules[i] = *(struct ip_rule *)ex_container[i]->custom_data;
assert(rules[i].user_tag == ex_container[i] || rules[i].user_tag == NULL);
assert(rules[i].user_tag == ex_container[i] ||
rules[i].user_tag == NULL);
rules[i].user_tag = ex_container[i];
}
}
@@ -515,8 +552,8 @@ int ip_plugin_runtime_commit(void *ip_plugin_runtime, const char *table_name,
ex_data_runtime_commit(ex_data_rt);
if (old_ip_matcher != NULL) {
maat_garbage_bagging(ip_plugin_rt->ref_garbage_bin, old_ip_matcher, NULL,
garbage_ip_matcher_free);
maat_garbage_bagging(ip_plugin_rt->ref_garbage_bin, old_ip_matcher,
NULL, garbage_ip_matcher_free);
}
ip_plugin_rt->rule_num = rule_cnt;
@@ -538,30 +575,38 @@ long long ip_plugin_runtime_rule_count(void *ip_plugin_runtime)
return 0;
}
struct ip_plugin_runtime *ip_plugin_rt = (struct ip_plugin_runtime *)ip_plugin_runtime;
struct ip_plugin_runtime *ip_plugin_rt =
(struct ip_plugin_runtime *)ip_plugin_runtime;
return ip_plugin_rt->rule_num;
}
struct ex_data_runtime *ip_plugin_runtime_get_ex_data_rt(void *ip_plugin_runtime)
struct ex_data_runtime *
ip_plugin_runtime_get_ex_data_rt(void *ip_plugin_runtime)
{
if (NULL == ip_plugin_runtime) {
return NULL;
}
struct ip_plugin_runtime *ip_plugin_rt = (struct ip_plugin_runtime *)ip_plugin_runtime;
struct ip_plugin_runtime *ip_plugin_rt =
(struct ip_plugin_runtime *)ip_plugin_runtime;
return ip_plugin_rt->ex_data_rt;
}
int ip_plugin_runtime_get_ex_data(void *ip_plugin_runtime, const struct ip_addr *ip_addr,
void **ex_data_array, size_t n_ex_data)
int ip_plugin_runtime_get_ex_data(void *ip_plugin_runtime,
const struct ip_addr *ip_addr,
void **ex_data_array,
size_t n_ex_data)
{
if (NULL == ip_plugin_runtime || NULL == ip_addr ||
NULL == ex_data_array || 0 == n_ex_data) {
return -1;
}
struct ip_plugin_runtime *ip_plugin_rt = (struct ip_plugin_runtime *)ip_plugin_runtime;
struct ip_plugin_runtime *ip_plugin_rt =
(struct ip_plugin_runtime *)ip_plugin_runtime;
if (0 == ip_plugin_rt->rule_num) {
return 0;
}
@@ -578,10 +623,12 @@ int ip_plugin_runtime_get_ex_data(void *ip_plugin_runtime, const struct ip_addr
ipv6_ntoh(ip_data.ipv6);
}
int n_result = ip_matcher_match(ip_plugin_rt->ip_matcher, &ip_data, results, n_ex_data);
int n_result = ip_matcher_match(ip_plugin_rt->ip_matcher, &ip_data,
results, n_ex_data);
for (int i = 0; i < n_result; i++) {
ex_data_array[i] = ex_data_runtime_get_ex_data_by_container(ip_plugin_rt->ex_data_rt,
(struct ex_container *)results[i].tag);
ex_data_array[i] =
ex_data_runtime_get_ex_data_by_container(ip_plugin_rt->ex_data_rt,
(struct ex_container *)results[i].tag);
}
return n_result;
}
@@ -592,7 +639,9 @@ long long ip_plugin_runtime_update_err_count(void *ip_plugin_runtime)
return 0;
}
struct ip_plugin_runtime *ip_plugin_rt = (struct ip_plugin_runtime *)ip_plugin_runtime;
struct ip_plugin_runtime *ip_plugin_rt =
(struct ip_plugin_runtime *)ip_plugin_runtime;
return ip_plugin_rt->update_err_cnt;
}
@@ -602,7 +651,9 @@ void ip_plugin_runtime_scan_times_inc(void *ip_plugin_runtime, int thread_id)
return;
}
struct ip_plugin_runtime *ip_plugin_rt = (struct ip_plugin_runtime *)ip_plugin_runtime;
struct ip_plugin_runtime *ip_plugin_rt =
(struct ip_plugin_runtime *)ip_plugin_runtime;
alignment_int64_array_add(ip_plugin_rt->scan_times, thread_id, 1);
}
@@ -612,9 +663,12 @@ long long ip_plugin_runtime_scan_times(void *ip_plugin_runtime)
return 0;
}
struct ip_plugin_runtime *ip_plugin_rt = (struct ip_plugin_runtime *)ip_plugin_runtime;
struct ip_plugin_runtime *ip_plugin_rt =
(struct ip_plugin_runtime *)ip_plugin_runtime;
long long sum = alignment_int64_array_sum(ip_plugin_rt->scan_times,
ip_plugin_rt->n_worker_thread);
alignment_int64_array_reset(ip_plugin_rt->scan_times,
ip_plugin_rt->n_worker_thread);

View File

@@ -80,16 +80,18 @@ void *ipport_plugin_schema_new(cJSON *json, struct table_manager *tbl_mgr,
schema->table_id = item->valueint;
} else {
log_fatal(logger, MODULE_IPPORT_PLUGIN,
"[%s:%d] ipport_plugin table:<%s> schema has no table_id column",
__FUNCTION__, __LINE__, table_name);
"[%s:%d] ipport_plugin table:<%s> schema has no"
" table_id column", __FUNCTION__, __LINE__,
table_name);
goto error;
}
item = cJSON_GetObjectItem(json, "custom");
if (NULL == item || item->type != cJSON_Object) {
log_fatal(logger, MODULE_IPPORT_PLUGIN,
"[%s:%d] ipport_plugin table:<%s> schema has no custom column",
__FUNCTION__, __LINE__, table_name);
"[%s:%d] ipport_plugin table:<%s> schema has no"
" custom column", __FUNCTION__, __LINE__,
table_name);
goto error;
}
@@ -98,8 +100,9 @@ void *ipport_plugin_schema_new(cJSON *json, struct table_manager *tbl_mgr,
schema->item_id_column = custom_item->valueint;
} else {
log_fatal(logger, MODULE_IPPORT_PLUGIN,
"[%s:%d] ipport_plugin table:<%s> schema has no item_id column",
__FUNCTION__, __LINE__, table_name);
"[%s:%d] ipport_plugin table:<%s> schema has no"
" item_id column", __FUNCTION__, __LINE__,
table_name);
goto error;
}
@@ -108,8 +111,9 @@ void *ipport_plugin_schema_new(cJSON *json, struct table_manager *tbl_mgr,
schema->ip_type_column = custom_item->valueint;
} else {
log_fatal(logger, MODULE_IPPORT_PLUGIN,
"[%s:%d] ipport_plugin table:<%s> schema has no ip_type column",
__FUNCTION__, __LINE__, table_name);
"[%s:%d] ipport_plugin table:<%s> schema has no"
" ip_type column", __FUNCTION__, __LINE__,
table_name);
goto error;
}
@@ -118,8 +122,9 @@ void *ipport_plugin_schema_new(cJSON *json, struct table_manager *tbl_mgr,
schema->ip_addr_column = custom_item->valueint;
} else {
log_fatal(logger, MODULE_IPPORT_PLUGIN,
"[%s:%d] ipport_plugin table:<%s> schema has no ip_addr column",
__FUNCTION__, __LINE__, table_name);
"[%s:%d] ipport_plugin table:<%s> schema has no"
" ip_addr column", __FUNCTION__, __LINE__,
table_name);
goto error;
}
@@ -128,8 +133,8 @@ void *ipport_plugin_schema_new(cJSON *json, struct table_manager *tbl_mgr,
schema->port1_column = custom_item->valueint;
} else {
log_fatal(logger, MODULE_IPPORT_PLUGIN,
"[%s:%d] ipport_plugin table:<%s> schema has no port1 column",
__FUNCTION__, __LINE__, table_name);
"[%s:%d] ipport_plugin table:<%s> schema has no"
" port1 column", __FUNCTION__, __LINE__, table_name);
goto error;
}
@@ -138,8 +143,8 @@ void *ipport_plugin_schema_new(cJSON *json, struct table_manager *tbl_mgr,
schema->port2_column = custom_item->valueint;
} else {
log_fatal(logger, MODULE_IPPORT_PLUGIN,
"[%s:%d] ipport_plugin table:<%s> schema has no port2 column",
__FUNCTION__, __LINE__, table_name);
"[%s:%d] ipport_plugin table:<%s> schema has no"
" port2 column", __FUNCTION__, __LINE__, table_name);
goto error;
}
@@ -173,7 +178,8 @@ int ipport_plugin_table_set_ex_container_schema(void *ipport_plugin_schema, int
void (*custom_data_free)(void *),
long argl, void *argp)
{
struct ipport_plugin_schema *schema = (struct ipport_plugin_schema *)ipport_plugin_schema;
struct ipport_plugin_schema *schema =
(struct ipport_plugin_schema *)ipport_plugin_schema;
if (1 == schema->container_schema.set_flag) {
log_fatal(schema->logger, MODULE_IPPORT_PLUGIN,
@@ -197,12 +203,14 @@ int ipport_plugin_table_set_ex_container_schema(void *ipport_plugin_schema, int
struct ex_container_schema *
ipport_plugin_table_get_ex_container_schema(void *ipport_plugin_schema)
{
struct ipport_plugin_schema *schema = (struct ipport_plugin_schema *)ipport_plugin_schema;
struct ipport_plugin_schema *schema =
(struct ipport_plugin_schema *)ipport_plugin_schema;
return &(schema->container_schema);
}
void *ipport_plugin_runtime_new(void *ipport_plugin_schema, size_t max_thread_num,
void *ipport_plugin_runtime_new(void *ipport_plugin_schema,
size_t max_thread_num,
struct maat_garbage_bin *garbage_bin,
struct log_handle *logger)
{
@@ -210,11 +218,15 @@ void *ipport_plugin_runtime_new(void *ipport_plugin_schema, size_t max_thread_nu
return NULL;
}
struct ipport_plugin_schema *schema = (struct ipport_plugin_schema *)ipport_plugin_schema;
struct ipport_plugin_runtime *ipport_plugin_rt = ALLOC(struct ipport_plugin_runtime, 1);
struct ipport_plugin_schema *schema =
(struct ipport_plugin_schema *)ipport_plugin_schema;
struct ipport_plugin_runtime *ipport_plugin_rt =
ALLOC(struct ipport_plugin_runtime, 1);
ipport_plugin_rt->ex_data_rt =
ex_data_runtime_new(schema->table_id, schema->gc_timeout_s, logger);
ipport_plugin_rt->ex_data_rt = ex_data_runtime_new(schema->table_id, schema->gc_timeout_s,
logger);
if (1 == schema->container_schema.set_flag) {
ex_data_runtime_set_ex_container_schema(ipport_plugin_rt->ex_data_rt,
&(schema->container_schema));
@@ -234,7 +246,9 @@ void ipport_plugin_runtime_free(void *ipport_plugin_runtime)
return;
}
struct ipport_plugin_runtime *ipport_plugin_rt = (struct ipport_plugin_runtime *)ipport_plugin_runtime;
struct ipport_plugin_runtime *ipport_plugin_rt =
(struct ipport_plugin_runtime *)ipport_plugin_runtime;
if (ipport_plugin_rt->matcher != NULL) {
ipport_matcher_free(ipport_plugin_rt->matcher);
ipport_plugin_rt->matcher = NULL;
@@ -349,19 +363,20 @@ error:
return NULL;
}
static void ipport_item_free(struct ipport_item *item)
void ipport_item_free(void *ipport_item)
{
if (NULL == item) {
if (NULL == ipport_item) {
return;
}
FREE(item);
FREE(ipport_item);
}
static int ipport_plugin_runtime_update_row(struct ipport_plugin_runtime *ipport_plugin_rt,
const char *table_name, const char *row,
const char *key, size_t key_len,
struct ipport_item *ipport_item, int is_valid)
static int
ipport_plugin_runtime_update_row(struct ipport_plugin_runtime *ipport_plugin_rt,
const char *table_name, const char *row,
const char *key, size_t key_len,
struct ipport_item *ipport_item, int is_valid)
{
int ret = -1;
struct ex_data_runtime *ex_data_rt = ipport_plugin_rt->ex_data_rt;
@@ -377,9 +392,14 @@ static int ipport_plugin_runtime_update_row(struct ipport_plugin_runtime *ipport
}
} else {
// add
void *ex_data = ex_data_runtime_row2ex_data(ex_data_rt, table_name, row, key, key_len);
struct ex_container *ex_container = ex_container_new(ex_data, (void *)ipport_item);
ret = ex_data_runtime_add_ex_container(ex_data_rt, key, key_len, ex_container);
void *ex_data = ex_data_runtime_row2ex_data(ex_data_rt, table_name,
row, key, key_len);
struct ex_container *ex_container = ex_container_new(ex_data,
(void *)ipport_item);
ret = ex_data_runtime_add_ex_container(ex_data_rt, key, key_len,
ex_container);
if (ret < 0) {
ex_container_free(ex_data_rt, ex_container);
return -1;
@@ -389,8 +409,10 @@ static int ipport_plugin_runtime_update_row(struct ipport_plugin_runtime *ipport
return 0;
}
int ipport_plugin_runtime_update(void *ipport_plugin_runtime, void *ipport_plugin_schema,
const char *table_name, const char *line, int valid_column)
int ipport_plugin_runtime_update(void *ipport_plugin_runtime,
void *ipport_plugin_schema,
const char *table_name,
const char *line, int valid_column)
{
if (NULL == ipport_plugin_runtime || NULL == ipport_plugin_schema ||
NULL == line) {
@@ -398,8 +420,12 @@ int ipport_plugin_runtime_update(void *ipport_plugin_runtime, void *ipport_plugi
}
struct ipport_item *ipport_item = NULL;
struct ipport_plugin_schema *schema = (struct ipport_plugin_schema *)ipport_plugin_schema;
struct ipport_plugin_runtime *ipport_plugin_rt = (struct ipport_plugin_runtime *)ipport_plugin_runtime;
struct ipport_plugin_schema *schema =
(struct ipport_plugin_schema *)ipport_plugin_schema;
struct ipport_plugin_runtime *ipport_plugin_rt =
(struct ipport_plugin_runtime *)ipport_plugin_runtime;
size_t item_id_offset = 0, item_id_len = 0;
int is_valid = get_column_value(line, valid_column);
@@ -408,8 +434,8 @@ int ipport_plugin_runtime_update(void *ipport_plugin_runtime, void *ipport_plugi
return -1;
}
int ret = get_column_pos(line, schema->item_id_column, &item_id_offset,
&item_id_len);
int ret = get_column_pos(line, schema->item_id_column,
&item_id_offset, &item_id_len);
if (ret < 0) {
ipport_plugin_rt->update_err_cnt++;
return -1;
@@ -428,29 +454,32 @@ int ipport_plugin_runtime_update(void *ipport_plugin_runtime, void *ipport_plugi
const char *key = line + item_id_offset;
size_t key_len = item_id_len;
ret = ipport_plugin_runtime_update_row(ipport_plugin_rt, table_name, line,
key, key_len, ipport_item, is_valid);
ret = ipport_plugin_runtime_update_row(ipport_plugin_rt, table_name,
line, key, key_len, ipport_item,
is_valid);
if (ret < 0) {
if (ipport_item != NULL) {
ipport_item_free(ipport_item);
}
log_fatal(ipport_plugin_rt->logger, MODULE_IPPORT_PLUGIN,
"[%s:%d]ipport_plugin table:<%s> update one line failed, "
"line:%s", __FUNCTION__, __LINE__, table_name, line);
ipport_plugin_rt->update_err_cnt++;
return -1;
}
log_debug(ipport_plugin_rt->logger, MODULE_IPPORT_PLUGIN,
"ipport_plugin table:<%s> update one line, key:%s, key_len:%zu, is_valid:%d",
table_name, key, key_len, is_valid);
"ipport_plugin table:<%s> update one line, key:%s, key_len:%zu,"
" is_valid:%d", table_name, key, key_len, is_valid);
} else {
//ex_schema not set
ex_data_runtime_cache_row_put(ipport_plugin_rt->ex_data_rt, line);
ipport_plugin_rt->rule_num = ex_data_runtime_cached_row_count(ipport_plugin_rt->ex_data_rt);
ipport_plugin_rt->rule_num =
ex_data_runtime_cached_row_count(ipport_plugin_rt->ex_data_rt);
}
return 0;
}
static void ipport_item_to_ipport_rule(struct ipport_item *item, struct ipport_rule *rule)
static void
ipport_item_to_ipport_rule(struct ipport_item *item, struct ipport_rule *rule)
{
if (IPV4 == item->ip_type) {
rule->ip.ip_type= IPV4;
@@ -466,20 +495,26 @@ static void ipport_item_to_ipport_rule(struct ipport_item *item, struct ipport_r
rule->rule_id = item->item_id;
}
static void garbage_ipport_matcher_free(void *ipport_matcher, void *arg)
static void
garbage_ipport_matcher_free(void *ipport_matcher, void *arg)
{
struct ipport_matcher *matcher = (struct ipport_matcher *)ipport_matcher;
struct ipport_matcher *matcher =
(struct ipport_matcher *)ipport_matcher;
ipport_matcher_free(matcher);
}
int ipport_plugin_runtime_commit(void *ipport_plugin_runtime, const char *table_name,
int ipport_plugin_runtime_commit(void *ipport_plugin_runtime,
const char *table_name,
long long maat_rt_version)
{
if (NULL == ipport_plugin_runtime) {
return -1;
}
struct ipport_plugin_runtime *ipport_plugin_rt = (struct ipport_plugin_runtime *)ipport_plugin_runtime;
struct ipport_plugin_runtime *ipport_plugin_rt =
(struct ipport_plugin_runtime *)ipport_plugin_runtime;
struct ex_data_runtime *ex_data_rt = ipport_plugin_rt->ex_data_rt;
if (NULL == ex_data_rt) {
return -1;
@@ -492,11 +527,14 @@ int ipport_plugin_runtime_commit(void *ipport_plugin_runtime, const char *table_
struct ipport_rule *rules = NULL;
struct ex_container **ex_container = NULL;
size_t rule_cnt = ex_data_runtime_list_updating_ex_container(ex_data_rt, &ex_container);
size_t rule_cnt = ex_data_runtime_list_updating_ex_container(ex_data_rt,
&ex_container);
if (rule_cnt > 0) {
rules = ALLOC(struct ipport_rule, rule_cnt);
for (size_t i = 0; i < rule_cnt; i++) {
struct ipport_item *item = (struct ipport_item *)ex_container[i]->custom_data;
struct ipport_item *item =
(struct ipport_item *)ex_container[i]->custom_data;
ipport_item_to_ipport_rule(item, &rules[i]);
rules[i].user_tag = ex_container[i];
}
@@ -515,14 +553,15 @@ int ipport_plugin_runtime_commit(void *ipport_plugin_runtime, const char *table_
(end.tv_nsec - start.tv_nsec) / 1000000;
if (NULL == new_matcher) {
log_fatal(ipport_plugin_rt->logger, MODULE_IPPORT_PLUGIN,
"[%s:%d] ipport_plugin table[%s] rebuild ipport_matcher failed when "
"update %zu rules", __FUNCTION__, __LINE__, table_name, rule_cnt);
"[%s:%d] ipport_plugin table[%s] rebuild ipport_matcher"
" failed when update %zu rules", __FUNCTION__, __LINE__,
table_name, rule_cnt);
ret = -1;
} else {
log_info(ipport_plugin_rt->logger, MODULE_IPPORT_PLUGIN,
"table[%s] commit %zu ipport_plugin rules and rebuild ipport_matcher "
"completed, version:%lld, consume:%lldms", table_name, rule_cnt,
maat_rt_version, time_elapse_ms);
"table[%s] commit %zu ipport_plugin rules and rebuild"
" ipport_matcher completed, version:%lld, consume:%lldms",
table_name, rule_cnt, maat_rt_version, time_elapse_ms);
}
}
@@ -531,8 +570,8 @@ int ipport_plugin_runtime_commit(void *ipport_plugin_runtime, const char *table_
ex_data_runtime_commit(ex_data_rt);
if (old_matcher != NULL) {
maat_garbage_bagging(ipport_plugin_rt->ref_garbage_bin, old_matcher, NULL,
garbage_ipport_matcher_free);
maat_garbage_bagging(ipport_plugin_rt->ref_garbage_bin, old_matcher,
NULL, garbage_ipport_matcher_free);
}
ipport_plugin_rt->rule_num = rule_cnt;
@@ -554,17 +593,21 @@ long long ipport_plugin_runtime_rule_count(void *ipport_plugin_runtime)
return 0;
}
struct ipport_plugin_runtime *ipport_plugin_rt = (struct ipport_plugin_runtime *)ipport_plugin_runtime;
struct ipport_plugin_runtime *ipport_plugin_rt =
(struct ipport_plugin_runtime *)ipport_plugin_runtime;
return ipport_plugin_rt->rule_num;
}
struct ex_data_runtime *ipport_plugin_runtime_get_ex_data_rt(void *ipport_plugin_runtime)
struct ex_data_runtime *
ipport_plugin_runtime_get_ex_data_rt(void *ipport_plugin_runtime)
{
if (NULL == ipport_plugin_runtime) {
return NULL;
}
struct ipport_plugin_runtime *ipport_plugin_rt = (struct ipport_plugin_runtime *)ipport_plugin_runtime;
struct ipport_plugin_runtime *ipport_plugin_rt =
(struct ipport_plugin_runtime *)ipport_plugin_runtime;
return ipport_plugin_rt->ex_data_rt;
}
@@ -584,15 +627,19 @@ static int validate_port(struct ipport_item *item, uint16_t port)
return 0;
}
int ipport_plugin_runtime_get_ex_data(void *ipport_plugin_runtime, const struct ip_addr *ip_addr,
uint16_t port, void **ex_data_array, size_t n_ex_data)
int ipport_plugin_runtime_get_ex_data(void *ipport_plugin_runtime,
const struct ip_addr *ip_addr,
uint16_t port, void **ex_data_array,
size_t n_ex_data)
{
if (NULL == ipport_plugin_runtime || NULL == ip_addr ||
NULL == ex_data_array || 0 == n_ex_data) {
return -1;
}
struct ipport_plugin_runtime *ipport_plugin_rt = (struct ipport_plugin_runtime *)ipport_plugin_runtime;
struct ipport_plugin_runtime *ipport_plugin_rt =
(struct ipport_plugin_runtime *)ipport_plugin_runtime;
if (0 == ipport_plugin_rt->rule_num) {
return 0;
}
@@ -602,7 +649,8 @@ int ipport_plugin_runtime_get_ex_data(void *ipport_plugin_runtime, const struct
}
struct ipport_result results[n_ex_data];
int n_hit_item = ipport_matcher_match(ipport_plugin_rt->matcher, ip_addr, port, results, n_ex_data);
int n_hit_item = ipport_matcher_match(ipport_plugin_rt->matcher, ip_addr,
port, results, n_ex_data);
if (n_hit_item <= 0) {
return n_hit_item;
}
@@ -617,8 +665,9 @@ int ipport_plugin_runtime_get_ex_data(void *ipport_plugin_runtime, const struct
continue;
}
ex_data_array[hit_result_cnt++] = ex_data_runtime_get_ex_data_by_container(ipport_plugin_rt->ex_data_rt,
ex_container);
ex_data_array[hit_result_cnt++] =
ex_data_runtime_get_ex_data_by_container(ipport_plugin_rt->ex_data_rt,
ex_container);
}
return hit_result_cnt;
@@ -630,17 +679,22 @@ long long ipport_plugin_runtime_update_err_count(void *ipport_plugin_runtime)
return 0;
}
struct ipport_plugin_runtime *ipport_plugin_rt = (struct ipport_plugin_runtime *)ipport_plugin_runtime;
struct ipport_plugin_runtime *ipport_plugin_rt =
(struct ipport_plugin_runtime *)ipport_plugin_runtime;
return ipport_plugin_rt->update_err_cnt;
}
void ipport_plugin_runtime_scan_times_inc(void *ipport_plugin_runtime, int thread_id)
void ipport_plugin_runtime_scan_times_inc(void *ipport_plugin_runtime,
int thread_id)
{
if (NULL == ipport_plugin_runtime || thread_id < 0) {
return;
}
struct ipport_plugin_runtime *ipport_plugin_rt = (struct ipport_plugin_runtime *)ipport_plugin_runtime;
struct ipport_plugin_runtime *ipport_plugin_rt =
(struct ipport_plugin_runtime *)ipport_plugin_runtime;
alignment_int64_array_add(ipport_plugin_rt->scan_times, thread_id, 1);
}
@@ -650,9 +704,12 @@ long long ipport_plugin_runtime_scan_times(void *ipport_plugin_runtime)
return 0;
}
struct ipport_plugin_runtime *ipport_plugin_rt = (struct ipport_plugin_runtime *)ipport_plugin_runtime;
struct ipport_plugin_runtime *ipport_plugin_rt =
(struct ipport_plugin_runtime *)ipport_plugin_runtime;
long long sum = alignment_int64_array_sum(ipport_plugin_rt->scan_times,
ipport_plugin_rt->n_worker_thread);
alignment_int64_array_reset(ipport_plugin_rt->scan_times,
ipport_plugin_rt->n_worker_thread);