support expr offset match

This commit is contained in:
liuwentan
2023-02-09 22:13:15 +08:00
parent c1902f8deb
commit d5e6808e1f
41 changed files with 3046 additions and 711 deletions

View File

@@ -11,7 +11,8 @@ add_definitions(-fPIC)
set(MAAT_SRC json2iris.cpp maat_api.cpp rcu_hash.cpp maat_garbage_collection.cpp maat_config_monitor.cpp
maat_rule.cpp maat_kv.cpp maat_ex_data.cpp maat_utils.cpp maat_command.cpp maat_redis_monitor.cpp
maat_table.cpp maat_compile.cpp maat_group.cpp maat_ip.cpp maat_flag.cpp maat_interval.cpp
maat_expr.cpp maat_plugin.cpp maat_ip_plugin.cpp maat_virtual.cpp)
maat_expr.cpp maat_plugin.cpp maat_ip_plugin.cpp maat_bool_plugin.cpp maat_fqdn_plugin.cpp
maat_virtual.cpp)
set(LIB_SOURCE_FILES
${PROJECT_SOURCE_DIR}/deps/cJSON/cJSON.c ${PROJECT_SOURCE_DIR}/deps/log/log.c)

View File

@@ -0,0 +1,56 @@
/*
**********************************************************************************************
* File: maat_bool_plugin.h
* Description:
* Authors: Liu WenTan <liuwentan@geedgenetworks.com>
* Date: 2022-10-31
* Copyright: (c) 2018-2022 Geedge Networks, Inc. All rights reserved.
***********************************************************************************************
*/
#ifndef _MAAT_BOOL_PLUGIN_H_
#define _MAAT_BOOL_PLUGIN_H_
#ifdef __cpluscplus
extern "C"
{
#endif
#include "log/log.h"
#include "maat/maat.h"
#include "cJSON/cJSON.h"
struct bool_plugin_runtime;
/* bool plugin schema API */
void *bool_plugin_schema_new(cJSON *json, struct table_manager *tbl_mgr,
const char *table_name, struct log_handle *logger);
void bool_plugin_schema_free(void *bool_plugin_schema);
/* ip plugin table ex data API */
struct ex_data_schema *bool_plugin_table_get_ex_data_schema(void *bool_plugin_schema);
int bool_plugin_table_set_ex_data_schema(void *bool_plugin_schema,
maat_plugin_ex_new_func_t *new_func,
maat_plugin_ex_free_func_t *free_func,
maat_plugin_ex_dup_func_t *dup_func,
long argl, void *argp,
struct log_handle *logger);
/* ip plugin runtime API */
void *bool_plugin_runtime_new(void *bool_plugin_schema, int max_thread_num,
struct maat_garbage_bin *garbage_bin,
struct log_handle *logger);
void bool_plugin_runtime_free(void *bool_plugin_runtime);
int bool_plugin_runtime_update(void *bool_plugin_runtime, void *bool_plugin_schema,
const char *line, int valid_column);
int bool_plugin_runtime_commit(void *bool_plugin_runtime, const char *table_name);
struct ex_data_runtime *bool_plugin_runtime_get_ex_data_rt(void *bool_plugin_runtime);
#ifdef __cpluscplus
}
#endif
#endif

View File

@@ -49,8 +49,7 @@ int compile_table_set_rule_ex_data_schema(struct compile_schema *compile_schema,
maat_rule_ex_dup_func_t *dup_func,
long argl, void *argp,
struct log_handle *logger);
struct compile_ex_data_schema *
compile_table_get_rule_ex_data_schema(struct compile_schema *compile_schema, size_t idx);
void *compile_table_get_rule_ex_data(struct compile_schema *compile_schema, int compile_id, size_t idx);
size_t compile_table_rule_ex_data_schema_count(struct compile_schema *compile_schema);
@@ -62,7 +61,7 @@ void compile_runtime_free(void *compile_runtime);
int compile_runtime_update(void *compile_runtime, void *compile_schema,
const char *line, int valid_column);
int compile_runtime_commit(void *compile_runtime);
int compile_runtime_commit(void *compile_runtime, const char *table_name);
int compile_runtime_match(struct compile_runtime *compile_rt, int *compile_ids,
size_t compile_ids_size, struct maat_state *state);

View File

@@ -33,7 +33,7 @@ void expr_runtime_free(void *expr_runtime);
int expr_runtime_update(void *expr_runtime, void *expr_schema,
const char *line, int valid_column);
int expr_runtime_commit(void *expr_runtime);
int expr_runtime_commit(void *expr_runtime, const char *table_name);
/* expr runtime scan API */
/**

View File

@@ -35,7 +35,7 @@ void flag_runtime_free(void *flag_runtime);
int flag_runtime_update(void *flag_runtime, void *flag_schema,
const char *line, int valid_column);
int flag_runtime_commit(void *flag_runtime);
int flag_runtime_commit(void *flag_runtime, const char *table_name);
/* flag runtime scan API */
/**

View File

@@ -0,0 +1,56 @@
/*
**********************************************************************************************
* File: maat_fqdn_plugin.h
* Description:
* Authors: Liu WenTan <liuwentan@geedgenetworks.com>
* Date: 2022-10-31
* Copyright: (c) 2018-2022 Geedge Networks, Inc. All rights reserved.
***********************************************************************************************
*/
#ifndef _MAAT_FQDN_PLUGIN_H_
#define _MAAT_FQDN_PLUGIN_H_
#ifdef __cpluscplus
extern "C"
{
#endif
#include "log/log.h"
#include "maat/maat.h"
#include "cJSON/cJSON.h"
struct fqdn_plugin_runtime;
/* fqdn plugin schema API */
void *fqdn_plugin_schema_new(cJSON *json, struct table_manager *tbl_mgr,
const char *table_name, struct log_handle *logger);
void fqdn_plugin_schema_free(void *fqdn_plugin_schema);
/* fqdn plugin table ex data API */
struct ex_data_schema *fqdn_plugin_table_get_ex_data_schema(void *fqdn_plugin_schema);
int fqdn_plugin_table_set_ex_data_schema(void *fqdn_plugin_schema,
maat_plugin_ex_new_func_t *new_func,
maat_plugin_ex_free_func_t *free_func,
maat_plugin_ex_dup_func_t *dup_func,
long argl, void *argp,
struct log_handle *logger);
/* fqdn plugin runtime API */
void *fqdn_plugin_runtime_new(void *fqdn_plugin_schema, int max_thread_num,
struct maat_garbage_bin *garbage_bin,
struct log_handle *logger);
void fqdn_plugin_runtime_free(void *fqdn_plugin_runtime);
int fqdn_plugin_runtime_update(void *fqdn_plugin_runtime, void *fqdn_plugin_schema,
const char *line, int valid_column);
int fqdn_plugin_runtime_commit(void *fqdn_plugin_runtime, const char *table_name);
struct ex_data_runtime *fqdn_plugin_runtime_get_ex_data_rt(void *fqdn_plugin_runtime);
#ifdef __cpluscplus
}
#endif
#endif

View File

@@ -47,7 +47,7 @@ int group2group_runtime_get_top_groups(void *g2g_runtime, int *group_ids,
int group2group_runtime_update(void *g2g_runtime, void *g2g_schema,
const char *line, int valid_column);
int group2group_runtime_commit(void *g2g_runtime);
int group2group_runtime_commit(void *g2g_runtime, const char *table_name);
#ifdef __cpluscplus
}

View File

@@ -35,7 +35,7 @@ void interval_runtime_free(void *interval_runtime);
int interval_runtime_update(void *interval_runtime, void *interval_schema,
const char *line, int valid_column);
int interval_runtime_commit(void *interval_runtime);
int interval_runtime_commit(void *interval_runtime, const char *table_name);
/* interval runtime scan API */
/**

View File

@@ -30,7 +30,7 @@ void ip_runtime_free(void *ip_runtime);
int ip_runtime_update(void *ip_runtime, void *ip_schema,
const char *line, int valid_column);
int ip_runtime_commit(void *ip_runtime);
int ip_runtime_commit(void *ip_runtime, const char *table_name);
struct ex_data_runtime *ip_runtime_get_ex_data_rt(struct ip_runtime *ip_rt);

View File

@@ -45,7 +45,7 @@ void ip_plugin_runtime_free(void *ip_plugin_runtime);
int ip_plugin_runtime_update(void *ip_plugin_runtime, void *ip_plugin_schema,
const char *line, int valid_column);
int ip_plugin_runtime_commit(void *ip_plugin_runtime);
int ip_plugin_runtime_commit(void *ip_plugin_runtime, const char *table_name);
struct ex_data_runtime *ip_plugin_runtime_get_ex_data_rt(void *ip_plugin_runtime);

View File

@@ -55,7 +55,7 @@ void plugin_runtime_free(void *plugin_runtime);
int plugin_runtime_update(void *plugin_runtime, void *plugin_schema,
const char *line, int valid_column);
int plugin_runtime_commit(void *plugin_runtime);
int plugin_runtime_commit(void *plugin_runtime, const char *table_name);
struct ex_data_runtime *plugin_runtime_get_ex_data_rt(void *plugin_runtime);

View File

@@ -136,6 +136,7 @@ int set_iris_descriptor(const char *json_file, cJSON *json,
maat_kv_register(iris_cfg->str2int_map, "yes", 1);
maat_kv_register(iris_cfg->str2int_map, "no", 0);
maat_kv_register(iris_cfg->str2int_map, "flag", TABLE_TYPE_FLAG);
maat_kv_register(iris_cfg->str2int_map, "ip_plus", TABLE_TYPE_IP_PLUS);
maat_kv_register(iris_cfg->str2int_map, "string", TABLE_TYPE_EXPR);
maat_kv_register(iris_cfg->str2int_map, "expr", TABLE_TYPE_EXPR);
@@ -405,6 +406,37 @@ error_out:
return ret;
}
int write_flag_line(cJSON *region_json, struct iris_description *p_iris,
struct iris_table *table, struct log_handle *logger)
{
struct translate_command json_cmd[MAX_COLUMN_NUM];
int cmd_cnt = 0;
memset(json_cmd, 0, sizeof(json_cmd));
json_cmd[cmd_cnt].json_string = "region_id";
json_cmd[cmd_cnt].json_type=cJSON_Number;
cmd_cnt++;
json_cmd[cmd_cnt].json_string = "group_id";
json_cmd[cmd_cnt].json_type = cJSON_Number;
cmd_cnt++;
json_cmd[cmd_cnt].json_string = "flag";
json_cmd[cmd_cnt].json_type = cJSON_Number;
cmd_cnt++;
json_cmd[cmd_cnt].json_string = "flag_mask";
json_cmd[cmd_cnt].json_type = cJSON_Number;
cmd_cnt++;
json_cmd[cmd_cnt].json_string = "is_valid";
json_cmd[cmd_cnt].json_type = cJSON_Number;
cmd_cnt++;
return direct_write_rule(region_json, p_iris->str2int_map,
json_cmd, cmd_cnt, table, logger);
}
int write_expr_line(cJSON *region_json, struct iris_description *p_iris,
struct iris_table *table, struct log_handle *logger)
{
@@ -715,6 +747,9 @@ int write_region_rule(cJSON *region_json, int compile_id, int group_id,
struct iris_table *table_info = query_table_info(p_iris, table_name, table_type);
switch(table_type)
{
case TABLE_TYPE_FLAG:
ret = write_flag_line(table_content, p_iris, table_info, logger);
break;
case TABLE_TYPE_EXPR:
case TABLE_TYPE_EXPR_PLUS:
ret = write_expr_line(table_content, p_iris, table_info, logger);

View File

@@ -460,6 +460,34 @@ int maat_table_callback_register(struct maat *maat_instance, int table_id,
return 0;
}
int maat_compile_table_ex_schema_register(struct maat *maat_instance, int table_id,
maat_rule_ex_new_func_t *new_func,
maat_rule_ex_free_func_t *free_func,
maat_rule_ex_dup_func_t *dup_func,
long argl, void *argp)
{
if (NULL == maat_instance || table_id < 0 || table_id > MAX_TABLE_NUM) {
return -1;
}
enum table_type table_type = table_manager_get_table_type(maat_instance->tbl_mgr, table_id);
assert(table_type == TABLE_TYPE_COMPILE);
void *compile_schema = table_manager_get_schema(maat_instance->tbl_mgr, table_id);
assert(compile_schema != NULL);
return compile_table_set_rule_ex_data_schema((struct compile_schema *)compile_schema, table_id,
new_func, free_func, dup_func,
argl, argp, maat_instance->logger);
}
void *maat_compile_table_get_ex_data(struct maat *maat_instance, int table_id, int compile_id, size_t idx)
{
struct compile_schema *schema = (struct compile_schema *)table_manager_get_schema(maat_instance->tbl_mgr,
table_id);
return compile_table_get_rule_ex_data(schema, compile_id, idx);
}
int generic_plugin_table_ex_schema_register(struct table_manager *tbl_mgr, int table_id,
maat_plugin_ex_new_func_t *new_func,
maat_plugin_ex_free_func_t *free_func,
@@ -556,10 +584,10 @@ void generic_plugin_runtime_commit_ex_schema(void *runtime, void *schema,
switch (table_type) {
case TABLE_TYPE_PLUGIN:
plugin_runtime_commit(runtime);
plugin_runtime_commit(runtime, "false_plugin_table");
break;
case TABLE_TYPE_IP_PLUGIN:
ip_plugin_runtime_commit(runtime);
ip_plugin_runtime_commit(runtime, "false_plugin_table");
break;
default:
break;
@@ -598,18 +626,19 @@ int maat_plugin_table_ex_schema_register(struct maat *maat_instance, int table_i
return 0;
}
void *maat_plugin_table_get_ex_data(struct maat *maat_instance, int table_id,
const char *key, size_t key_len)
int maat_plugin_table_get_ex_data(struct maat *maat_instance, int table_id,
const char *key, size_t key_len,
void **ex_data_array, size_t n_ex_data)
{
struct maat_runtime *maat_rt = maat_instance->maat_rt;
if (NULL == maat_rt) {
return NULL;
return -1;
}
enum table_type table_type = table_manager_get_table_type(maat_rt->ref_tbl_mgr, table_id);
void *runtime = table_manager_get_runtime(maat_rt->ref_tbl_mgr, table_id);
if (NULL == runtime) {
return NULL;
return -1;
}
struct ex_data_runtime *ex_data_rt = NULL;
@@ -625,10 +654,11 @@ void *maat_plugin_table_get_ex_data(struct maat *maat_instance, int table_id,
}
if (NULL == ex_data_rt) {
return NULL;
return -1;
}
return ex_data_runtime_get_ex_data(ex_data_rt, key, key_len);
*ex_data_array = ex_data_runtime_get_ex_data(ex_data_rt, key, key_len);
return 0;
}
struct maat_state *make_outer_state(struct maat *maat_instance, int thread_id)
@@ -681,17 +711,12 @@ inline int scan_status_should_compile_NOT(struct maat_state *state)
return 0;
}
int hit_group_to_compile(void *compile_runtime, int *compile_ids, size_t compile_ids_size,
size_t *n_hit_compile_id, struct maat_state *mid)
size_t hit_group_to_compile(void *compile_runtime, int *compile_ids, size_t compile_ids_size,
struct maat_state *mid)
{
int compile_id_cnt = compile_runtime_match((struct compile_runtime *)compile_runtime,
compile_ids, compile_ids_size, mid);
*n_hit_compile_id = compile_id_cnt;
if (compile_id_cnt > 0) {
return MAAT_SCAN_HIT;
} else {
return MAAT_SCAN_HALF_HIT;
}
size_t n_hit_compile = compile_runtime_match((struct compile_runtime *)compile_runtime,
compile_ids, compile_ids_size, mid);
return n_hit_compile;
}
int maat_scan_flag(struct maat *maat_instance, int table_id, int thread_id,
@@ -746,7 +771,7 @@ int maat_scan_flag(struct maat *maat_instance, int table_id, int thread_id,
return MAAT_SCAN_ERR;
}
int compile_ret = 0;
size_t n_hit_compile = 0;
if (group_hit_cnt > 0 || scan_status_should_compile_NOT(mid)) {
// come here means group_hit_cnt > 0, at least MAAT_SCAN_HALF_HIT, or MAAT_SCAN_HIT
if (group_hit_cnt > 0) {
@@ -761,7 +786,8 @@ int maat_scan_flag(struct maat *maat_instance, int table_id, int thread_id,
}
void *compile_rt = table_manager_get_runtime(maat_instance->tbl_mgr, compile_table_id);
compile_ret = hit_group_to_compile(compile_rt, results, n_result, n_hit_result, mid);
n_hit_compile = hit_group_to_compile(compile_rt, results, n_result, mid);
*n_hit_result = n_hit_compile;
assert(mid->is_last_scan < LAST_SCAN_FINISHED);
if (LAST_SCAN_SET == mid->is_last_scan) {
@@ -769,22 +795,23 @@ int maat_scan_flag(struct maat *maat_instance, int table_id, int thread_id,
}
}
if (compile_ret > 0) {
if (n_hit_compile > 0) {
alignment_int64_array_add(maat_instance->hit_cnt, thread_id, 1);
}
if (0 == group_hit_cnt && compile_ret > 0) {
// hit NOT group
alignment_int64_array_add(maat_instance->not_grp_hit_cnt, thread_id, 1);
if (0 == group_hit_cnt) {
//hit NOT group
alignment_int64_array_add(maat_instance->not_grp_hit_cnt, thread_id, 1);
}
return MAAT_SCAN_HIT;
} else {
// n_hit_compile == 0
if (group_hit_cnt > 0) {
return MAAT_SCAN_HALF_HIT;
}
}
maat_runtime_ref_dec(maat_instance->maat_rt, thread_id);
if (0 == compile_ret && group_hit_cnt > 0) {
return MAAT_SCAN_HALF_HIT;
}
return MAAT_SCAN_HIT;
return MAAT_SCAN_OK;
}
int maat_scan_integer(struct maat *instance, int table_id, int thread_id,
@@ -837,7 +864,7 @@ int maat_scan_ipv4(struct maat *maat_instance, int table_id, int thread_id,
return MAAT_SCAN_ERR;
}
int compile_ret = 0;
size_t n_hit_compile = 0;
if (group_hit_cnt > 0 || scan_status_should_compile_NOT(mid)) {
// come here means group_hit_cnt > 0, at least MAAT_SCAN_HALF_HIT, or MAAT_SCAN_HIT
if (group_hit_cnt > 0) {
@@ -852,7 +879,8 @@ int maat_scan_ipv4(struct maat *maat_instance, int table_id, int thread_id,
}
void *compile_rt = table_manager_get_runtime(maat_instance->tbl_mgr, compile_table_id);
compile_ret = hit_group_to_compile(compile_rt, results, n_result, n_hit_result, mid);
n_hit_compile = hit_group_to_compile(compile_rt, results, n_result, mid);
*n_hit_result = n_hit_compile;
assert(mid->is_last_scan < LAST_SCAN_FINISHED);
if (LAST_SCAN_SET == mid->is_last_scan) {
@@ -860,22 +888,23 @@ int maat_scan_ipv4(struct maat *maat_instance, int table_id, int thread_id,
}
}
if (compile_ret > 0) {
if (n_hit_compile > 0) {
alignment_int64_array_add(maat_instance->hit_cnt, thread_id, 1);
}
if (0 == group_hit_cnt && compile_ret > 0) {
// hit NOT group
alignment_int64_array_add(maat_instance->not_grp_hit_cnt, thread_id, 1);
if (0 == group_hit_cnt) {
//hit NOT group
alignment_int64_array_add(maat_instance->not_grp_hit_cnt, thread_id, 1);
}
return MAAT_SCAN_HIT;
} else {
// n_hit_compile == 0
if (group_hit_cnt > 0) {
return MAAT_SCAN_HALF_HIT;
}
}
maat_runtime_ref_dec(maat_instance->maat_rt, thread_id);
if (0 == compile_ret && group_hit_cnt > 0) {
return MAAT_SCAN_HALF_HIT;
}
return MAAT_SCAN_HIT;
return MAAT_SCAN_OK;
}
@@ -921,7 +950,7 @@ int maat_scan_ipv6(struct maat *maat_instance, int table_id, int thread_id,
return MAAT_SCAN_ERR;
}
int compile_ret = 0;
size_t n_hit_compile = 0;
if (group_hit_cnt > 0 || scan_status_should_compile_NOT(mid)) {
// come here means group_hit_cnt > 0, at least MAAT_SCAN_HALF_HIT, or MAAT_SCAN_HIT
if (group_hit_cnt > 0) {
@@ -936,7 +965,8 @@ int maat_scan_ipv6(struct maat *maat_instance, int table_id, int thread_id,
}
void *compile_rt = table_manager_get_runtime(maat_instance->tbl_mgr, compile_table_id);
compile_ret = hit_group_to_compile(compile_rt, results, n_result, n_hit_result, mid);
n_hit_compile = hit_group_to_compile(compile_rt, results, n_result, mid);
*n_hit_result = n_hit_compile;
assert(mid->is_last_scan < LAST_SCAN_FINISHED);
if (LAST_SCAN_SET == mid->is_last_scan) {
@@ -944,22 +974,23 @@ int maat_scan_ipv6(struct maat *maat_instance, int table_id, int thread_id,
}
}
if (compile_ret > 0) {
if (n_hit_compile > 0) {
alignment_int64_array_add(maat_instance->hit_cnt, thread_id, 1);
}
if (0 == group_hit_cnt && compile_ret > 0) {
// hit NOT group
alignment_int64_array_add(maat_instance->not_grp_hit_cnt, thread_id, 1);
if (0 == group_hit_cnt) {
//hit NOT group
alignment_int64_array_add(maat_instance->not_grp_hit_cnt, thread_id, 1);
}
return MAAT_SCAN_HIT;
} else {
// n_hit_compile == 0
if (group_hit_cnt > 0) {
return MAAT_SCAN_HALF_HIT;
}
}
maat_runtime_ref_dec(maat_instance->maat_rt, thread_id);
if (0 == compile_ret && group_hit_cnt > 0) {
return MAAT_SCAN_HALF_HIT;
}
return MAAT_SCAN_HIT;
return MAAT_SCAN_OK;
}
int maat_scan_string(struct maat *maat_instance, int table_id, int thread_id,
@@ -1023,7 +1054,7 @@ int maat_scan_string(struct maat *maat_instance, int table_id, int thread_id,
return MAAT_SCAN_ERR;
}
int compile_ret = 0;
size_t n_hit_compile = 0;
if (group_hit_cnt > 0 || scan_status_should_compile_NOT(mid)) {
// come here means group_hit_cnt > 0, at least MAAT_SCAN_HALF_HIT, or MAAT_SCAN_HIT
if (group_hit_cnt > 0) {
@@ -1038,7 +1069,8 @@ int maat_scan_string(struct maat *maat_instance, int table_id, int thread_id,
}
void *compile_rt = table_manager_get_runtime(maat_instance->tbl_mgr, compile_table_id);
compile_ret = hit_group_to_compile(compile_rt, results, n_result, n_hit_result, mid);
n_hit_compile = hit_group_to_compile(compile_rt, results, n_result, mid);
*n_hit_result = n_hit_compile;
assert(mid->is_last_scan < LAST_SCAN_FINISHED);
if (LAST_SCAN_SET == mid->is_last_scan) {
@@ -1046,22 +1078,23 @@ int maat_scan_string(struct maat *maat_instance, int table_id, int thread_id,
}
}
if (compile_ret > 0) {
if (n_hit_compile > 0) {
alignment_int64_array_add(maat_instance->hit_cnt, thread_id, 1);
}
if (0 == group_hit_cnt && compile_ret > 0) {
// hit NOT group
alignment_int64_array_add(maat_instance->not_grp_hit_cnt, thread_id, 1);
if (0 == group_hit_cnt) {
//hit NOT group
alignment_int64_array_add(maat_instance->not_grp_hit_cnt, thread_id, 1);
}
return MAAT_SCAN_HIT;
} else {
// n_hit_compile == 0
if (group_hit_cnt > 0) {
return MAAT_SCAN_HALF_HIT;
}
}
maat_runtime_ref_dec(maat_instance->maat_rt, thread_id);
if (0 == compile_ret && group_hit_cnt > 0) {
return MAAT_SCAN_HALF_HIT;
}
return MAAT_SCAN_HIT;
return MAAT_SCAN_OK;
}
struct maat_stream *maat_scan_stream_open(struct maat *instance, int table_id, int thread_id)

491
src/maat_bool_plugin.cpp Normal file
View File

@@ -0,0 +1,491 @@
/*
**********************************************************************************************
* File: maat_bool_plugin.cpp
* Description:
* Authors: Liu wentan <liuwentan@geedgenetworks.com>
* Date: 2022-10-31
* Copyright: (c) 2018-2022 Geedge Networks, Inc. All rights reserved.
***********************************************************************************************
*/
#include <assert.h>
#include "maat_bool_plugin.h"
#include "bool_matcher.h"
#include "maat_ex_data.h"
#include "log/log.h"
#include "maat_utils.h"
#include "maat_rule.h"
#include "maat_garbage_collection.h"
#define MODULE_BOOL_PLUGIN module_name_str("maat.bool_plugin")
struct bool_plugin_schema {
int item_id_column;
int bool_expr_column;
int rule_tag_column;
struct ex_data_schema *ex_schema;
int table_id;
struct table_manager *ref_tbl_mgr;
unsigned long long update_err_cnt;
unsigned long long unmatch_tag_cnt;
};
struct bool_plugin_item {
int item_id;
size_t n_bool_item;
unsigned long long bool_item_id[MAX_ITEMS_PER_BOOL_EXPR];
};
struct bool_plugin_runtime {
struct bool_matcher *matcher;
struct ex_data_runtime *ex_data_rt;
uint32_t rule_num;
uint32_t updating_rule_num;
struct maat_garbage_bin *ref_garbage_bin;
struct log_handle *logger;
};
/* bool plugin schema API */
void *bool_plugin_schema_new(cJSON *json, struct table_manager *tbl_mgr,
const char *table_name, struct log_handle *logger)
{
size_t read_cnt = 0;
struct bool_plugin_schema *schema = ALLOC(struct bool_plugin_schema, 1);
cJSON *custom_item = NULL;
cJSON *item = cJSON_GetObjectItem(json, "table_id");
if (item != NULL && item->type == cJSON_Number) {
schema->table_id = item->valueint;
read_cnt++;
}
item = cJSON_GetObjectItem(json, "custom");
if (NULL == item || item->type != cJSON_Object) {
log_error(logger, MODULE_BOOL_PLUGIN,
"table %s has no custom column", table_name);
goto error;
}
custom_item = cJSON_GetObjectItem(item, "item_id");
if (custom_item != NULL && custom_item->type == cJSON_Number) {
schema->item_id_column = custom_item->valueint;
read_cnt++;
}
custom_item = cJSON_GetObjectItem(item, "bool_expr");
if (custom_item != NULL && custom_item->type == cJSON_Number) {
schema->bool_expr_column = custom_item->valueint;
read_cnt++;
}
// rule_tag is optional
custom_item = cJSON_GetObjectItem(item, "rule_tag");
if (custom_item != NULL && custom_item->type == cJSON_Number) {
schema->rule_tag_column = custom_item->valueint;
}
schema->ref_tbl_mgr = tbl_mgr;
if (read_cnt < 3) {
goto error;
}
return schema;
error:
FREE(schema);
return NULL;
}
void bool_plugin_schema_free(void *bool_plugin_schema)
{
if (NULL == bool_plugin_schema) {
return;
}
struct bool_plugin_schema *schema = (struct bool_plugin_schema *)bool_plugin_schema;
if (schema->ex_schema != NULL) {
ex_data_schema_free(schema->ex_schema);
schema->ex_schema = NULL;
}
FREE(schema);
}
/* ip plugin table ex data API */
struct ex_data_schema *bool_plugin_table_get_ex_data_schema(void *bool_plugin_schema)
{
if (NULL == bool_plugin_schema) {
return NULL;
}
struct bool_plugin_schema *schema = (struct bool_plugin_schema *)bool_plugin_schema;
return schema->ex_schema;
}
int bool_plugin_table_set_ex_data_schema(void *bool_plugin_schema,
maat_plugin_ex_new_func_t *new_func,
maat_plugin_ex_free_func_t *free_func,
maat_plugin_ex_dup_func_t *dup_func,
long argl, void *argp,
struct log_handle *logger)
{
if (NULL == bool_plugin_schema) {
return -1;
}
struct bool_plugin_schema *schema = (struct bool_plugin_schema *)bool_plugin_schema;
if (schema->ex_schema != NULL) {
assert(0);
log_error(logger, MODULE_BOOL_PLUGIN,
"Error: %s, EX data schema already registed", __FUNCTION__);
return -1;
}
schema->ex_schema = ex_data_schema_new(new_func, free_func, dup_func, argl, argp);
return 0;
}
void *bool_plugin_runtime_new(void *bool_plugin_schema, int max_thread_num,
struct maat_garbage_bin *garbage_bin,
struct log_handle *logger)
{
if (NULL == bool_plugin_schema) {
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);
bool_plugin_rt->ex_data_rt = ex_data_runtime_new(schema->table_id, ex_data_container_free,
logger);
bool_plugin_rt->ref_garbage_bin = garbage_bin;
bool_plugin_rt->logger = logger;
return bool_plugin_rt;
}
void bool_plugin_runtime_free(void *bool_plugin_runtime)
{
if (NULL == bool_plugin_runtime) {
return;
}
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;
}
if (bool_plugin_rt->ex_data_rt != NULL) {
ex_data_runtime_free(bool_plugin_rt->ex_data_rt);
bool_plugin_rt->ex_data_rt = NULL;
}
FREE(bool_plugin_rt);
}
int bool_plugin_table_ex_data_schema_flag(struct bool_plugin_schema *bool_plugin_schema)
{
return 0;
}
int bool_plugin_runtime_update_row(struct bool_plugin_runtime *rt,
struct bool_plugin_schema *schema,
const char *row, char *key, size_t key_len,
struct bool_expr *expr, int is_valid)
{
int ret = -1;
struct ex_data_runtime *ex_data_rt = rt->ex_data_rt;
int set_flag = bool_plugin_table_ex_data_schema_flag(schema);
if (1 == set_flag) {
if (0 == is_valid) {
//delete
ret = ex_data_runtime_del_ex_container(ex_data_rt, key, key_len);
if (ret < 0) {
return -1;
}
} else {
//add
void *ex_data = ex_data_runtime_row2ex_data(ex_data_rt, row, key, key_len);
struct ex_data_container *ex_container = ex_data_container_new(ex_data, (void *)expr);
ret = ex_data_runtime_add_ex_container(ex_data_rt, key, key_len, ex_container);
if (ret < 0) {
return -1;
}
}
} else {
ex_data_runtime_cache_row_put(ex_data_rt, row);
}
return 0;
}
int bool_plugin_accept_tag_match(struct bool_plugin_schema *schema, const char *line,
struct log_handle *logger)
{
size_t column_offset = 0;
size_t column_len = 0;
size_t n_tag = table_manager_accept_tags_count(schema->ref_tbl_mgr);
if (schema->rule_tag_column > 0 && n_tag > 0) {
int ret = get_column_pos(line, schema->rule_tag_column,
&column_offset, &column_len);
if (ret < 0) {
log_error(logger, MODULE_BOOL_PLUGIN,
"bool_plugin table(table_id:%d) has no rule_tag, line:%s",
schema->table_id, line);
schema->update_err_cnt++;
return TAG_MATCH_ERR;
}
if (column_len > 2) {
char *tag_str = ALLOC(char, column_len + 1);
memcpy(tag_str, (line + column_offset), column_len);
ret = table_manager_accept_tags_match(schema->ref_tbl_mgr, tag_str);
FREE(tag_str);
if (TAG_MATCH_ERR == ret) {
log_error(logger, MODULE_BOOL_PLUGIN,
"bool_plugin table(table_id:%d) has invalid tag format, line:%s",
schema->table_id, line);
schema->update_err_cnt++;
return TAG_MATCH_ERR;
}
if (TAG_MATCH_UNMATCHED == ret) {
schema->unmatch_tag_cnt++;
return TAG_MATCH_UNMATCHED;
}
}
}
return TAG_MATCH_MATCHED;
}
struct bool_plugin_item *
bool_plugin_item_new(const char *line, struct bool_plugin_schema *schema,
struct log_handle *logger)
{
int ret = bool_plugin_accept_tag_match(schema, line, logger);
if (ret == TAG_MATCH_UNMATCHED) {
return NULL;
}
size_t column_offset = 0;
size_t column_len = 0;
size_t n_item = 0;
char expr_buffer[BUFSIZ] = {0};
char *token = NULL, *sub_token = NULL, *saveptr;
struct bool_plugin_item *item = ALLOC(struct bool_plugin_item, 1);
ret = get_column_pos(line, schema->item_id_column, &column_offset, &column_len);
if (ret < 0) {
log_error(logger, MODULE_BOOL_PLUGIN,
"bool_plugin table(table_id:%d) line:%s has no item_id column",
schema->table_id, line);
goto error;
}
item->item_id = atoi(line + column_offset);
ret = get_column_pos(line, schema->bool_expr_column, &column_offset, &column_len);
if (ret < 0) {
log_error(logger, MODULE_BOOL_PLUGIN,
"bool_plugin table(table_id:%d) line:%s has no bool_expr column",
schema->table_id, line);
goto error;
}
memcpy(expr_buffer, line + column_offset, column_len);
for (token = expr_buffer; ; token = NULL) {
sub_token = strtok_r(token, "&", &saveptr);
if (NULL == sub_token) {
break;
}
ret = sscanf(sub_token, "%llu", item->bool_item_id + n_item);
n_item++;
if (ret != 1 || n_item > MAX_ITEMS_PER_BOOL_EXPR) {
log_error(logger, MODULE_BOOL_PLUGIN,
"bool_plugin table(table_id:%d) invalid format of bool_expr column, line:%s",
schema->table_id, line);
goto error;
}
}
item->n_bool_item = n_item;
return item;
error:
FREE(item);
return NULL;
}
void bool_plugin_item_free(struct bool_plugin_item *item)
{
FREE(item);
}
static int cmp_ull_p(const void *p1, const void *p2)
{
if(* (unsigned long long*) p1 > * (unsigned long long*) p2) {
return 1;
} else if(* (unsigned long long*) p1 < * (unsigned long long*) p2) {
return -1;
} else {
return 0;
}
}
size_t ull_dedup(unsigned long long item_ids[], size_t n_item)
{
size_t index = 0;
qsort(item_ids, n_item, sizeof(unsigned long long), cmp_ull_p);
for (size_t i = 1; i < n_item; i++) {
if (item_ids[i] != item_ids[index]) {
item_ids[++index] = item_ids[i];
}
}
return index + 1;
}
struct bool_expr *bool_expr_new(int item_id, struct bool_plugin_item *item)
{
struct bool_expr *expr = ALLOC(struct bool_expr, 1);
expr->expr_id = item_id;
size_t n_item = ull_dedup(item->bool_item_id, item->n_bool_item);
for (size_t i = 0; i < n_item; i++) {
expr->items[i].item_id = item->bool_item_id[i];
expr->items[i].not_flag = 0;
}
expr->item_num = n_item;
return expr;
}
int bool_plugin_runtime_update(void *bool_plugin_runtime, void *bool_plugin_schema,
const char *line, int valid_column)
{
if (NULL == bool_plugin_runtime || NULL == bool_plugin_schema ||
NULL == line) {
return -1;
}
struct bool_plugin_item *item = NULL;
struct bool_expr *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;
int item_id = get_column_value(line, schema->item_id_column);
int is_valid = get_column_value(line, valid_column);
if (is_valid < 0) {
return -1;
}
if (schema->ex_schema != NULL) {
if (1 == is_valid) {
// add
item = bool_plugin_item_new(line, schema, bool_plugin_rt->logger);
if (NULL == item) {
return -1;
}
expr = bool_expr_new(item_id, item);
assert(expr != NULL);
bool_plugin_item_free(item);
}
char *key = (char *)&item_id;
int ret = bool_plugin_runtime_update_row(bool_plugin_rt, schema, line, key,
sizeof(int), expr, is_valid);
if (ret < 0) {
if (item != NULL) {
FREE(item);
}
return -1;
} else {
if (0 == is_valid) {
bool_plugin_rt->rule_num--;
} else {
bool_plugin_rt->rule_num++;
}
}
} 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);
}
return 0;
}
int bool_plugin_runtime_commit(void *bool_plugin_runtime, const char *table_name)
{
if (NULL == bool_plugin_runtime) {
return -1;
}
int ret = 0;
struct ex_data_container **ex_container = NULL;
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;
size_t expr_cnt = ex_data_runtime_list_updating_ex_container(ex_data_rt, &ex_container);
if (0 == expr_cnt) {
FREE(ex_container);
return 0;
}
struct bool_expr *exprs = ALLOC(struct bool_expr, expr_cnt);
for (size_t i = 0; i < expr_cnt; i++) {
exprs[i] = *(struct bool_expr *)(struct bool_expr *)ex_container[i]->custom_data;
assert(exprs[i].user_tag == ex_container[i] || NULL == exprs[i].user_tag);
exprs[i].user_tag = ex_container[i];
}
struct bool_matcher *new_bool_matcher = NULL;
struct bool_matcher *old_bool_matcher = NULL;
size_t mem_used = 0;
log_info(bool_plugin_rt->logger, MODULE_BOOL_PLUGIN,
"table[%s] committing %zu bool_plugin rules for rebuilding bool_matcher engine",
table_name, expr_cnt);
new_bool_matcher = bool_matcher_new(exprs, expr_cnt, &mem_used);
if (NULL == new_bool_matcher) {
log_error(bool_plugin_rt->logger, MODULE_BOOL_PLUGIN,
"table[%s] rebuild bool_matcher engine failed when update %zu bool_plugin rules",
table_name, expr_cnt);
ret = -1;
}
old_bool_matcher = bool_plugin_rt->matcher;
bool_plugin_rt->matcher = new_bool_matcher;
maat_garbage_bagging(bool_plugin_rt->ref_garbage_bin, old_bool_matcher,
(void (*)(void*))bool_matcher_free);
ex_data_runtime_commit(ex_data_rt);
FREE(exprs);
FREE(ex_container);
return ret;
}
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;
return bool_plugin_rt->ex_data_rt;
}

View File

@@ -48,7 +48,7 @@ struct compile_schema {
size_t n_ex_schema;
struct compile_ex_data_schema ex_schema[MAX_COMPILE_EX_DATA_NUM];
int table_id; //ugly
struct table_manager *tbl_mgr;
struct table_manager *ref_tbl_mgr;
size_t unmatched_tag_cnt;
};
@@ -59,9 +59,8 @@ struct group2compile_schema {
int vtable_name_column;
int clause_index_column;
char associated_compile_table_id;
struct table_manager *ref_tbl_mgr;
int table_id;//ugly
struct table_manager *tbl_mgr;
struct table_manager *ref_tbl_mgr;
};
struct compile_item {
@@ -192,18 +191,45 @@ int compile_table_set_rule_ex_data_schema(struct compile_schema *compile_schema,
return idx;
}
struct compile_ex_data_schema *
compile_table_get_rule_ex_data_schema(struct compile_schema *compile_schema, size_t idx)
void *compile_runtime_get_user_data(struct compile_runtime *compile_rt, int compile_id, int is_dettach)
{
struct maat_compile *compile = NULL;
void *ret = NULL;
//TODO: add mutex
HASH_FIND_INT(compile_rt->compile_hash, &compile_id, compile);
if (compile != NULL) {
ret = compile->user_data;
if (is_dettach) {
compile->user_data = NULL;
}
}
return ret;
}
void *compile_table_get_rule_ex_data(struct compile_schema *compile_schema, int compile_id, size_t idx)
{
if (NULL == compile_schema) {
return NULL;
}
if (idx < compile_schema->n_ex_schema) {
return (compile_schema->ex_schema + idx);
}
struct compile_rule *compile_rule = NULL;
struct compile_runtime *compile_rt = NULL;
compile_rt = (struct compile_runtime *)table_manager_get_runtime(compile_schema->ref_tbl_mgr,
compile_schema->table_id);
compile_rule = (struct compile_rule *)compile_runtime_get_user_data(compile_rt, compile_id, 0);
if (NULL == compile_rule) {
return NULL;
}
return NULL;
void *ex_data = NULL;
assert(idx < compile_schema->n_ex_schema);
struct compile_ex_data_schema *ex_schema = &(compile_schema->ex_schema[idx]);
ex_schema->dup_func(ex_schema->idx, &ex_data, compile_rule->ex_data + idx, ex_schema->argl, ex_schema->argp);
return ex_data;
}
size_t compile_table_rule_ex_data_schema_count(struct compile_schema *compile_schema)
@@ -295,7 +321,7 @@ void *compile_schema_new(cJSON *json, struct table_manager *tbl_mgr,
read_cnt++;
}
compile_schema->tbl_mgr = tbl_mgr;
compile_schema->ref_tbl_mgr = tbl_mgr;
if (read_cnt < 10) {
goto error;
@@ -367,7 +393,7 @@ void *group2compile_schema_new(cJSON *json, struct table_manager *tbl_mgr,
read_cnt++;
}
g2c_schema->tbl_mgr = tbl_mgr;
g2c_schema->ref_tbl_mgr = tbl_mgr;
if (read_cnt < 7) {
goto error;
@@ -461,11 +487,11 @@ compile_item_new(const char *line, struct compile_schema *compile_schema,
}
memcpy(tag_str, (line + column_offset), column_len);
n_accept_tag = table_manager_accept_tags_count(compile_schema->tbl_mgr);
n_accept_tag = table_manager_accept_tags_count(compile_schema->ref_tbl_mgr);
if (n_accept_tag > 0 && strlen(tag_str) > 2) {
str_unescape(tag_str);
ret = table_manager_accept_tags_match(compile_schema->tbl_mgr, tag_str);
ret = table_manager_accept_tags_match(compile_schema->ref_tbl_mgr, tag_str);
if (TAG_MATCH_ERR == ret) {
log_error(logger, MODULE_COMPILE,
"compile table(table_id:%d) line:%s is invalid tag",
@@ -836,9 +862,24 @@ struct maat_compile *maat_compile_hash_find(struct maat_compile **compile_hash,
return compile;
}
size_t maat_compile_hash_count(struct maat_compile *compile_hash)
size_t maat_compile_in_use_count(struct maat_compile *compile_hash)
{
return HASH_COUNT(compile_hash);
struct maat_compile *compile = NULL, *tmp_compile = NULL;
size_t in_use_compile_cnt = 0;
struct maat_clause_state *clause_state = NULL;
HASH_ITER(hh, compile_hash, compile, tmp_compile) {
//find how much compile whose clause is in_use
for (int i = 0; i < MAX_ITEMS_PER_BOOL_EXPR; i++) {
clause_state = compile->clause_states + i;
if (clause_state->in_use) {
in_use_compile_cnt++;
break;
}
}
}
return in_use_compile_cnt;
}
int compare_literal_id(const void *pa, const void *pb)
@@ -982,7 +1023,7 @@ maat_compile_bool_matcher_new(struct maat_compile *compile_hash,
//STEP 2, serial compile clause states to a bool expression array
size_t expr_cnt = 0;
size_t compile_cnt = maat_compile_hash_count(compile_hash);
size_t compile_cnt = maat_compile_in_use_count(compile_hash);
struct bool_expr *bool_expr_array = ALLOC(struct bool_expr, compile_cnt);
HASH_ITER(hh, compile_hash, compile, tmp_compile) {
for (i = 0, j = 0; i < MAX_ITEMS_PER_BOOL_EXPR; i++) {
@@ -992,7 +1033,7 @@ maat_compile_bool_matcher_new(struct maat_compile *compile_hash,
}
//TODO:mytest need to delete
#if 1
#if 0
struct maat_literal_id *p = NULL;
for(p = (struct maat_literal_id *)utarray_front(compile->clause_states[i].literal_ids); p!=NULL; p=(struct maat_literal_id *)utarray_next(compile->clause_states[i].literal_ids,p)) {
printf("compile_id:%d, clause_id:%llu, literal{%d: %d}\n",
@@ -1067,7 +1108,7 @@ size_t maat_compile_bool_matcher_match(struct bool_matcher *bm, int is_last_scan
size_t ud_result_cnt = 0;
//TODO:mytest need to delete
#if 1
#if 0
unsigned long long *p;
printf("utarray_len:%u\n", utarray_len(compile_state->all_hit_clauses));
for (p = (unsigned long long *)utarray_front(compile_state->all_hit_clauses); p != NULL; p = (unsigned long long *)utarray_next(compile_state->all_hit_clauses, p))
@@ -1098,6 +1139,7 @@ size_t maat_compile_bool_matcher_match(struct bool_matcher *bm, int is_last_scan
}
}
compile_state->this_scan_hit_item_cnt = 0;
return ud_result_cnt;
}
@@ -1120,11 +1162,12 @@ int maat_add_group_to_compile(struct maat_compile **compile_hash, void *g2g_runt
}
struct maat_literal_id literal_id = {g2c_item->group_id, g2c_item->vtable_id};
ret = maat_compile_clause_add_literal(compile, &literal_id, g2c_item->clause_index,
g2c_item->not_flag);
if (ret < 0) {
log_error(logger, MODULE_COMPILE,
"add literal_id{group_id:%d, vtable_id:%d} to clause %d of compile %d failed",
"add literal_id{group_id:%d, vtable_id:%d} to clause_index: %d of compile %d failed",
g2c_item->group_id, g2c_item->vtable_id, g2c_item->clause_index,
g2c_item->compile_id);
ret = -1;
@@ -1474,9 +1517,9 @@ void compile_item_to_compile_rule(struct compile_item *compile_item,
compile_rule->head.serv_def_len);
compile_rule->evaluation_order = compile_item->evaluation_order;
size_t n_rule_ex_schema = compile_table_rule_ex_data_schema_count(compile_schema);
size_t n_rule_ex_schema =compile_schema->n_ex_schema;
for (size_t i = 0; i < n_rule_ex_schema; i++) {
struct compile_ex_data_schema *ex_schema = compile_table_get_rule_ex_data_schema(compile_schema, i);
struct compile_ex_data_schema *ex_schema = &(compile_schema->ex_schema[i]);
compile_rule->ex_data[i] = rule_ex_data_new(&compile_rule->head,
compile_rule->service_defined, ex_schema);
}
@@ -1489,9 +1532,9 @@ void destroy_compile_rule(struct compile_rule *compile_rule)
struct compile_schema *schema = compile_rule->ref_table;
assert(compile_rule->magic_num==COMPILE_RULE_MAGIC);
size_t n_rule_ex_schema = compile_table_rule_ex_data_schema_count(schema);
size_t n_rule_ex_schema = schema->n_ex_schema;
for (size_t i = 0; i < n_rule_ex_schema; i++) {
struct compile_ex_data_schema *ex_schema = compile_table_get_rule_ex_data_schema(schema, i);
struct compile_ex_data_schema *ex_schema = &(schema->ex_schema[i]);
rule_ex_data_free(&(compile_rule->head), compile_rule->service_defined,
compile_rule->ex_data+i, ex_schema);
compile_rule->ex_data[i] = NULL;
@@ -1612,14 +1655,14 @@ int group2compile_runtime_update(void *g2c_runtime, void *g2c_schema,
return ret;
}
int compile_runtime_commit(void *compile_runtime)
int compile_runtime_commit(void *compile_runtime, const char *table_name)
{
if (NULL == compile_runtime) {
return -1;
}
struct compile_runtime *compile_rt = (struct compile_runtime *)compile_runtime;
size_t compile_cnt = maat_compile_hash_count(compile_rt->compile_hash);
size_t compile_cnt = maat_compile_in_use_count(compile_rt->compile_hash);
if (0 == compile_cnt) {
return 0;
}
@@ -1628,8 +1671,8 @@ int compile_runtime_commit(void *compile_runtime)
struct bool_matcher *new_bool_matcher = NULL;
log_info(compile_rt->logger, MODULE_COMPILE,
"committing %zu compile rules for rebuilding compile bool_matcher engine",
compile_cnt);
"table[%s] committing %zu compile rules for rebuilding compile bool_matcher engine",
table_name, compile_cnt);
int ret = 0;
new_bool_matcher = maat_compile_bool_matcher_new(compile_rt->compile_hash,
@@ -1637,8 +1680,8 @@ int compile_runtime_commit(void *compile_runtime)
compile_rt->logger);
if (NULL == new_bool_matcher) {
log_error(compile_rt->logger, MODULE_COMPILE,
"rebuild compile bool_matcher engine failed when update %zu compile rules",
compile_cnt);
"table[%s] rebuild compile bool_matcher engine failed when update %zu compile rules",
table_name, compile_cnt);
ret = -1;
}
@@ -1701,10 +1744,6 @@ int compile_runtime_match(struct compile_runtime *compile_rt,
int *compile_ids, size_t compile_ids_size,
struct maat_state *state)
{
if (NULL == compile_rt) {
return -1;
}
struct maat_compile_state *compile_state = state->compile_state;
int is_last_scan = state->is_last_scan;
struct compile_rule *compile_rules[compile_ids_size];
@@ -1780,10 +1819,10 @@ int maat_compile_state_update(struct maat_item *item_hash, int vtable_id,
int top_group_ids[MAX_SCANNER_HIT_GROUP_NUM] = {-1};
int top_group_cnt = group2group_runtime_get_top_groups(g2g_rt, &group_ids[i],
1, top_group_ids);
if (0 == top_group_cnt) {
maat_compile_state_update_hit_clause(state->compile_state, compile_rt,
group_ids[i], vtable_id);
}
// if (0 == top_group_cnt) {
// maat_compile_state_update_hit_clause(state->compile_state, compile_rt,
// group_ids[i], vtable_id);
// }
for (int j = 0; j < top_group_cnt; j++) {
maat_compile_state_update_hit_clause(state->compile_state, compile_rt,

View File

@@ -10,6 +10,7 @@
#include <string.h>
#include <assert.h>
#include <ctype.h>
#include "maat_expr.h"
#include "adapter_hs.h"
@@ -42,6 +43,7 @@ enum expr_type {
EXPR_TYPE_STRING = 0,
EXPR_TYPE_AND,
EXPR_TYPE_REGEX,
EXPR_TYPE_OFFSET,
EXPR_TYPE_MAX
};
@@ -99,6 +101,9 @@ enum expr_type int_to_expr_type(int expr_type)
case 2:
type = EXPR_TYPE_REGEX;
break;
case 3:
type = EXPR_TYPE_OFFSET;
break;
default:
break;
}
@@ -179,22 +184,6 @@ struct expr_item *expr_item_new(const char *line, struct expr_schema *expr_schem
str_unescape(district);
expr_item->district_id = table_manager_get_district_id(expr_schema->ref_tbl_mgr, district);
}
ret = get_column_pos(line, expr_schema->keywords_column, &column_offset, &column_len);
if (ret < 0) {
log_error(logger, MODULE_EXPR,
"expr table(table_id:%d) line:%s has no keywords",
expr_schema->table_id, line);
goto error;
}
if (column_len >= MAX_KEYWORDS_STR) {
log_error(logger, MODULE_EXPR,
"expr table(table_id:%d) line:%s keywords length too long",
expr_schema->table_id, line);
goto error;
}
memcpy(expr_item->keywords, (line + column_offset), column_len);
ret = get_column_pos(line, expr_schema->expr_type_column, &column_offset, &column_len);
if (ret < 0) {
@@ -247,6 +236,23 @@ struct expr_item *expr_item_new(const char *line, struct expr_schema *expr_schem
goto error;
}
ret = get_column_pos(line, expr_schema->keywords_column, &column_offset, &column_len);
if (ret < 0) {
log_error(logger, MODULE_EXPR,
"expr table(table_id:%d) line:%s has no keywords",
expr_schema->table_id, line);
goto error;
}
if (column_len >= MAX_KEYWORDS_STR) {
log_error(logger, MODULE_EXPR,
"expr table(table_id:%d) line:%s keywords length too long",
expr_schema->table_id, line);
goto error;
}
memcpy(expr_item->keywords, (line + column_offset), column_len);
return expr_item;
error:
FREE(expr_item);
@@ -478,6 +484,7 @@ enum pattern_type expr_type2pattern_type(enum expr_type expr_type)
switch (expr_type) {
case EXPR_TYPE_STRING:
case EXPR_TYPE_AND:
case EXPR_TYPE_OFFSET:
break;
case EXPR_TYPE_REGEX:
pattern_type = PATTERN_TYPE_REG;
@@ -489,6 +496,33 @@ enum pattern_type expr_type2pattern_type(enum expr_type expr_type)
return pattern_type;
}
int converHextoint(char srctmp)
{
if (isdigit(srctmp)) {
return srctmp - '0';
} else {
char temp = toupper(srctmp);
temp = temp - 'A' + 10;
return temp;
}
}
size_t hex2bin(char *hex, int hex_len, char *binary, size_t size)
{
size_t resultlen = 0;
int high,low;
for (int i = 0; i < hex_len && size > resultlen; i += 2, resultlen++) {
high = converHextoint(hex[i]);
low = converHextoint(hex[i+1]);
binary[resultlen] = high * 16 + low;
}
size = resultlen;
binary[resultlen] = '\0';
return resultlen;
}
#define MAAT_MAX_EXPR_ITEM_NUM 8
and_expr_t *expr_item_to_expr_rule(struct expr_item *expr_item, void *user_data,
struct log_handle *logger)
@@ -496,15 +530,18 @@ and_expr_t *expr_item_to_expr_rule(struct expr_item *expr_item, void *user_data,
size_t i = 0;
size_t sub_expr_cnt = 0;
char *pos = NULL;
char *tmp = NULL;
char *saveptr = NULL;
char *sub_key_array[MAAT_MAX_EXPR_ITEM_NUM];
int key_left_offset[MAAT_MAX_EXPR_ITEM_NUM] = {-1};
int key_right_offset[MAAT_MAX_EXPR_ITEM_NUM] = {-1};
and_expr_t *expr_rule = ALLOC(and_expr_t, 1);
switch (expr_item->expr_type) {
case EXPR_TYPE_AND:
case EXPR_TYPE_REGEX:
for (i = 0, pos = expr_item->keywords; ; i++, pos = NULL) {
char *tmp = strtok_r_esc(pos, '&', &saveptr);
tmp = strtok_r_esc(pos, '&', &saveptr);
if (NULL == tmp) {
break;
}
@@ -525,6 +562,42 @@ and_expr_t *expr_item_to_expr_rule(struct expr_item *expr_item, void *user_data,
}
sub_expr_cnt = i;
break;
case EXPR_TYPE_OFFSET:
for (i = 0, pos = expr_item->keywords; ; i++, pos = NULL) {
tmp = strtok_r_esc(pos, '&', &saveptr);
if (NULL == tmp) {
break;
}
if (i >= MAAT_MAX_EXPR_ITEM_NUM) {
log_error(logger, MODULE_EXPR,
"expr item_id:%d too many patterns",
expr_item->item_id);
return NULL;
}
sub_key_array[i] = tmp;
sscanf(sub_key_array[i], "%d-%d:", &(key_left_offset[i]),&(key_right_offset[i]));
if (!(key_left_offset[i] >= 0 && key_right_offset[i] > 0
&& key_left_offset[i] <= key_right_offset[i])) {
log_error(logger, MODULE_EXPR,
"expr item:%d has invalid offset.", expr_item->item_id);
return NULL;
}
sub_key_array[i] = (char *)memchr(sub_key_array[i], ':', strlen(sub_key_array[i]));
if (NULL == sub_key_array[i]) {
log_error(logger, MODULE_EXPR,
"expr item:%d has invalid offset keyword format.",
expr_item->item_id);
return NULL;
}
sub_key_array[i]++;//jump over ':'
sub_key_array[i] = str_unescape(sub_key_array[i]);
}
sub_expr_cnt = i;
break;
case EXPR_TYPE_STRING:
sub_expr_cnt = 1;
sub_key_array[0] = expr_item->keywords;
@@ -534,12 +607,36 @@ and_expr_t *expr_item_to_expr_rule(struct expr_item *expr_item, void *user_data,
break;
}
size_t region_str_len = 0;
char *region_string = NULL;
size_t sub_key_len = 0;
for (i = 0; i < sub_expr_cnt; i++) {
expr_rule->expr_id = expr_item->item_id;
expr_rule->patterns[i].pat = ALLOC(char, strlen(sub_key_array[i]));
memcpy(expr_rule->patterns[i].pat, sub_key_array[i], strlen(sub_key_array[i]));
expr_rule->patterns[i].pat_len = strlen(sub_key_array[i]);
if (FALSE == expr_item->is_case_sensitive) {
// insensitive
expr_rule->patterns[i].case_sensitive = 1;
}
expr_rule->patterns[i].type = expr_type2pattern_type(expr_item->expr_type);
if (TRUE == expr_item->is_hexbin && expr_rule->patterns[i].type != EXPR_TYPE_REGEX) {
region_str_len = strlen(sub_key_array[i]) + 1;
region_string = ALLOC(char, region_str_len);
region_str_len = hex2bin(sub_key_array[i], strlen(sub_key_array[i]), region_string, region_str_len);
}
if (region_string != NULL) {
expr_rule->patterns[i].pat = ALLOC(char, region_str_len);
memcpy(expr_rule->patterns[i].pat, region_string, region_str_len);
expr_rule->patterns[i].pat_len = region_str_len;
FREE(region_string);
} else {
sub_key_len = strlen(sub_key_array[i]);
expr_rule->patterns[i].pat = ALLOC(char, sub_key_len);
memcpy(expr_rule->patterns[i].pat, sub_key_array[i], sub_key_len);
expr_rule->patterns[i].pat_len = sub_key_len;
}
}
expr_rule->user_tag = user_data;
@@ -631,7 +728,7 @@ int expr_runtime_update(void *expr_runtime, void *expr_schema,
return 0;
}
int expr_runtime_commit(void *expr_runtime)
int expr_runtime_commit(void *expr_runtime, const char *table_name)
{
if (NULL == expr_runtime) {
return -1;
@@ -648,26 +745,24 @@ int expr_runtime_commit(void *expr_runtime)
}
and_expr_t *rules = ALLOC(and_expr_t, rule_cnt);
for (size_t i = 0; i < rule_cnt; i++) {
for (size_t i = 0 ; i < rule_cnt; i++) {
rules[i] = *(and_expr_t *)ex_data_array[i];
printf("rule_id:%d\n", rules[i].expr_id);
}
printf("\n\n");
struct adapter_hs *new_adapter_hs = NULL;
struct adapter_hs *old_adapter_hs = NULL;
log_info(expr_rt->logger, MODULE_EXPR,
"committing %zu expr rules for rebuilding adapter_hs engine",
rule_cnt);
"table[%s] committing %zu expr rules for rebuilding adapter_hs engine",
table_name, rule_cnt);
new_adapter_hs = adapter_hs_initialize(expr_rt->scan_mode,
expr_rt->n_worker_thread,
rules, rule_cnt,
expr_rt->logger);
if (NULL == new_adapter_hs) {
log_error(expr_rt->logger, MODULE_EXPR,
"rebuild adapter_hs engine failed when update %zu expr rules",
rule_cnt);
"table[%s] rebuild adapter_hs engine failed when update %zu expr rules",
table_name, rule_cnt);
ret = -1;
}

View File

@@ -359,7 +359,7 @@ int flag_runtime_update(void *flag_runtime, void *flag_schema,
return 0;
}
int flag_runtime_commit(void *flag_runtime)
int flag_runtime_commit(void *flag_runtime, const char *table_name)
{
if (NULL == flag_runtime) {
return -1;
@@ -385,13 +385,13 @@ int flag_runtime_commit(void *flag_runtime)
struct flag_matcher *old_flag_matcher = NULL;
log_info(flag_rt->logger, MODULE_FLAG,
"committing %zu flag rules for rebuilding flag_matcher engine",
rule_cnt);
"table[%s] committing %zu flag rules for rebuilding flag_matcher engine",
table_name, rule_cnt);
new_flag_matcher = flag_matcher_new(rules, rule_cnt);
if (NULL == new_flag_matcher) {
log_error(flag_rt->logger, MODULE_FLAG,
"rebuild flag_matcher engine failed when update %zu flag rules",
rule_cnt);
"table[%s] rebuild flag_matcher engine failed when update %zu flag rules",
table_name, rule_cnt);
ret = -1;
}

430
src/maat_fqdn_plugin.cpp Normal file
View File

@@ -0,0 +1,430 @@
/*
**********************************************************************************************
* File: maat_fqdn_plugin.cpp
* Description:
* Authors: Liu wentan <liuwentan@geedgenetworks.com>
* Date: 2022-10-31
* Copyright: (c) 2018-2022 Geedge Networks, Inc. All rights reserved.
***********************************************************************************************
*/
#include <assert.h>
#include "maat_fqdn_plugin.h"
#include "maat_ex_data.h"
#include "fqdn_engine.h"
#include "log/log.h"
#include "maat_utils.h"
#include "maat_table.h"
#include "maat_rule.h"
#include "maat_garbage_collection.h"
#define MODULE_FQDN_PLUGIN module_name_str("maat.bool_plugin")
struct fqdn_plugin_schema {
int item_id_column;
int suffix_flag_column;
int fqdn_column;
int rule_tag_column;
struct ex_data_schema *ex_schema;
int table_id;
struct table_manager *ref_tbl_mgr;
unsigned long long update_err_cnt;
unsigned long long unmatch_tag_cnt;
};
struct fqdn_plugin_item {
int item_id;
int suffix_flag;
};
struct fqdn_plugin_runtime {
struct FQDN_engine *engine;
struct ex_data_runtime *ex_data_rt;
uint32_t rule_num;
uint32_t updating_rule_num;
struct maat_garbage_bin *ref_garbage_bin;
struct log_handle *logger;
};
void *fqdn_plugin_schema_new(cJSON *json, struct table_manager *tbl_mgr,
const char *table_name, struct log_handle *logger)
{
size_t read_cnt = 0;
struct fqdn_plugin_schema *schema = ALLOC(struct fqdn_plugin_schema, 1);
cJSON *custom_item = NULL;
cJSON *item = cJSON_GetObjectItem(json, "table_id");
if (item != NULL && item->type == cJSON_Number) {
schema->table_id = item->valueint;
read_cnt++;
}
item = cJSON_GetObjectItem(json, "custom");
if (NULL == item || item->type != cJSON_Object) {
log_error(logger, MODULE_FQDN_PLUGIN,
"table %s has no custom column", table_name);
goto error;
}
custom_item = cJSON_GetObjectItem(item, "item_id");
if (custom_item != NULL && custom_item->type == cJSON_Number) {
schema->item_id_column = custom_item->valueint;
read_cnt++;
}
custom_item = cJSON_GetObjectItem(item, "suffix_match_method");
if (custom_item != NULL && custom_item->type == cJSON_Number) {
schema->suffix_flag_column = custom_item->valueint;
read_cnt++;
}
// rule_tag is optional
custom_item = cJSON_GetObjectItem(item, "fqdn");
if (custom_item != NULL && custom_item->type == cJSON_Number) {
schema->fqdn_column = custom_item->valueint;
read_cnt++;
}
// rule_tag is optional
custom_item = cJSON_GetObjectItem(item, "rule_tag");
if (custom_item != NULL && custom_item->type == cJSON_Number) {
schema->rule_tag_column = custom_item->valueint;
}
schema->ref_tbl_mgr = tbl_mgr;
if (read_cnt < 4) {
goto error;
}
return schema;
error:
FREE(schema);
return NULL;
}
void fqdn_plugin_schema_free(void *fqdn_plugin_schema)
{
if (NULL == fqdn_plugin_schema) {
return;
}
struct fqdn_plugin_schema *schema = (struct fqdn_plugin_schema *)fqdn_plugin_schema;
if (schema->ex_schema != NULL) {
ex_data_schema_free(schema->ex_schema);
schema->ex_schema = NULL;
}
FREE(schema);
}
/* fqdn plugin table ex data API */
struct ex_data_schema *fqdn_plugin_table_get_ex_data_schema(void *fqdn_plugin_schema)
{
if (NULL == fqdn_plugin_schema) {
return NULL;
}
struct fqdn_plugin_schema *schema = (struct fqdn_plugin_schema *)fqdn_plugin_schema;
return schema->ex_schema;
}
int fqdn_plugin_table_set_ex_data_schema(void *fqdn_plugin_schema,
maat_plugin_ex_new_func_t *new_func,
maat_plugin_ex_free_func_t *free_func,
maat_plugin_ex_dup_func_t *dup_func,
long argl, void *argp,
struct log_handle *logger)
{
if (NULL == fqdn_plugin_schema) {
return -1;
}
struct fqdn_plugin_schema *schema = (struct fqdn_plugin_schema *)fqdn_plugin_schema;
if (schema->ex_schema != NULL) {
assert(0);
log_error(logger, MODULE_FQDN_PLUGIN,
"Error: %s, EX data schema already registed", __FUNCTION__);
return -1;
}
schema->ex_schema = ex_data_schema_new(new_func, free_func, dup_func, argl, argp);
return 0;
}
void *fqdn_plugin_runtime_new(void *fqdn_plugin_schema, int max_thread_num,
struct maat_garbage_bin *garbage_bin,
struct log_handle *logger)
{
if (NULL == fqdn_plugin_schema) {
return NULL;
}
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, ex_data_container_free,
logger);
fqdn_plugin_rt->ref_garbage_bin = garbage_bin;
fqdn_plugin_rt->logger = logger;
return fqdn_plugin_rt;
}
void fqdn_plugin_runtime_free(void *fqdn_plugin_runtime)
{
if (NULL == fqdn_plugin_runtime) {
return;
}
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;
}
if (fqdn_plugin_rt->ex_data_rt != NULL) {
ex_data_runtime_free(fqdn_plugin_rt->ex_data_rt);
fqdn_plugin_rt->ex_data_rt = NULL;
}
FREE(fqdn_plugin_rt);
}
int fqdn_plugin_accept_tag_match(struct fqdn_plugin_schema *schema, const char *line,
struct log_handle *logger)
{
size_t column_offset = 0;
size_t column_len = 0;
size_t n_tag = table_manager_accept_tags_count(schema->ref_tbl_mgr);
if (schema->rule_tag_column > 0 && n_tag > 0) {
int ret = get_column_pos(line, schema->rule_tag_column,
&column_offset, &column_len);
if (ret < 0) {
log_error(logger, MODULE_FQDN_PLUGIN,
"fqdn_plugin table(table_id:%d) has no rule_tag, line:%s",
schema->table_id, line);
schema->update_err_cnt++;
return TAG_MATCH_ERR;
}
if (column_len > 2) {
char *tag_str = ALLOC(char, column_len + 1);
memcpy(tag_str, (line + column_offset), column_len);
ret = table_manager_accept_tags_match(schema->ref_tbl_mgr, tag_str);
FREE(tag_str);
if (TAG_MATCH_ERR == ret) {
log_error(logger, MODULE_FQDN_PLUGIN,
"fqdn_plugin table(table_id:%d) has invalid tag format, line:%s",
schema->table_id, line);
schema->update_err_cnt++;
return TAG_MATCH_ERR;
}
if (TAG_MATCH_UNMATCHED == ret) {
schema->unmatch_tag_cnt++;
return TAG_MATCH_UNMATCHED;
}
}
}
return TAG_MATCH_MATCHED;
}
struct fqdn_plugin_item *
fqdn_plugin_item_new(const char *line, struct fqdn_plugin_schema *schema,
struct log_handle *logger)
{
int ret = fqdn_plugin_accept_tag_match(schema, line, logger);
if (ret == TAG_MATCH_UNMATCHED) {
return NULL;
}
size_t column_offset = 0;
size_t column_len = 0;
struct fqdn_plugin_item *item = ALLOC(struct fqdn_plugin_item, 1);
ret = get_column_pos(line, schema->item_id_column, &column_offset, &column_len);
if (ret < 0) {
log_error(logger, MODULE_FQDN_PLUGIN,
"fqdn_plugin table(table_id:%d) line:%s has no item_id column",
schema->table_id, line);
goto error;
}
item->item_id = atoi(line + column_offset);
ret = get_column_pos(line, schema->suffix_flag_column, &column_offset, &column_len);
if (ret < 0) {
log_error(logger, MODULE_FQDN_PLUGIN,
"fqdn_plugin table(table_id:%d) line:%s has no suffix_match_method column",
schema->table_id, line);
goto error;
}
item->suffix_flag = atoi(line + column_offset);
ret = get_column_pos(line, schema->fqdn_column, &column_offset, &column_len);
if (ret < 0) {
log_error(logger, MODULE_FQDN_PLUGIN,
"fqdn_plugin table(table_id:%d) line:%s has no fqdn column",
schema->table_id, line);
goto error;
}
return item;
error:
FREE(item);
return NULL;
}
struct FQDN_rule *fqdn_rule_new(unsigned int id, const char* fqdn, size_t fqdn_len, int is_suffix_match)
{
struct FQDN_rule *fqdn_rule=ALLOC(struct FQDN_rule, 1);
//Todo: check FQDN format with regex ^([a-zA-Z0-9._-])+$
if(fqdn[0]=='.')
{
fqdn++;
fqdn_len--;
}
if(fqdn[fqdn_len]=='/')
{
fqdn_len--;
}
fqdn_rule->FQDN=ALLOC(char, fqdn_len+1);
memcpy(fqdn_rule->FQDN, fqdn, fqdn_len);
fqdn_rule->len=fqdn_len;
fqdn_rule->is_suffix_match=is_suffix_match;
fqdn_rule->id=id;
return fqdn_rule;
}
int fqdn_plugin_runtime_update_row(struct fqdn_plugin_runtime *rt,
struct fqdn_plugin_schema *schema,
const char *row, char *key, size_t key_len,
struct FQDN_rule *rule, int is_valid)
{
return 0;
}
int fqdn_plugin_runtime_update(void *fqdn_plugin_runtime, void *fqdn_plugin_schema,
const char *line, int valid_column)
{
if (NULL == fqdn_plugin_runtime || NULL == fqdn_plugin_schema ||
NULL == line) {
return -1;
}
struct fqdn_plugin_item *item = NULL;
struct FQDN_rule *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;
int item_id = get_column_value(line, schema->item_id_column);
int is_valid = get_column_value(line, valid_column);
if (is_valid < 0) {
return -1;
}
if (schema->ex_schema != NULL) {
if (1 == is_valid) {
// add
item = fqdn_plugin_item_new(line, schema, fqdn_plugin_rt->logger);
if (NULL == item) {
return -1;
}
//rule = fqdn_rule_new(line, schema, fqdn_plugin_rt->logger);
assert(rule != NULL);
//fqdn_plugin_item_free(item);
}
char *key = (char *)&item_id;
int ret = fqdn_plugin_runtime_update_row(fqdn_plugin_rt, schema, line, key,
sizeof(int), rule, is_valid);
if (ret < 0) {
if (item != NULL) {
FREE(item);
}
return -1;
} else {
if (0 == is_valid) {
fqdn_plugin_rt->rule_num--;
} else {
fqdn_plugin_rt->rule_num++;
}
}
} 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);
}
return 0;
}
int fqdn_plugin_runtime_commit(void *fqdn_plugin_runtime, const char *table_name)
{
if (NULL == fqdn_plugin_runtime) {
return -1;
}
int ret = 0;
struct ex_data_container **ex_container = NULL;
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;
size_t rule_cnt = ex_data_runtime_list_updating_ex_container(ex_data_rt, &ex_container);
if (0 == rule_cnt) {
FREE(ex_container);
return 0;
}
struct FQDN_rule *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] || NULL == rules[i].user_tag);
rules[i].user_tag = ex_container[i];
}
struct FQDN_engine *new_fqdn_engine = NULL;
struct FQDN_engine *old_fqdn_engine = NULL;
log_info(fqdn_plugin_rt->logger, MODULE_FQDN_PLUGIN,
"table[%s] committing %zu fqdn_plugin rules for rebuilding FQDN engine",
table_name, rule_cnt);
new_fqdn_engine = FQDN_engine_new(rules, rule_cnt);
if (NULL == new_fqdn_engine) {
log_error(fqdn_plugin_rt->logger, MODULE_FQDN_PLUGIN,
"table[%s] rebuild FQDN engine failed when update %zu fqdn_plugin rules",
table_name, rule_cnt);
ret = -1;
}
old_fqdn_engine = fqdn_plugin_rt->engine;
fqdn_plugin_rt->engine = new_fqdn_engine;
maat_garbage_bagging(fqdn_plugin_rt->ref_garbage_bin, old_fqdn_engine,
(void (*)(void*))FQDN_engine_free);
ex_data_runtime_commit(ex_data_rt);
FREE(rules);
FREE(ex_container);
return ret;
}
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;
return fqdn_plugin_rt->ex_data_rt;
}

View File

@@ -45,7 +45,7 @@ void maat_garbage_bin_free(struct maat_garbage_bin* bin)
{
struct maat_garbage_bag *p = NULL;
while (p = TAILQ_FIRST(&bin->garbage_q)) {
while ((p = TAILQ_FIRST(&bin->garbage_q)) != NULL) {
p->garbage_free(p->garbage);
TAILQ_REMOVE(&bin->garbage_q, p, entries);
FREE(p);
@@ -96,7 +96,7 @@ void maat_garbage_collect_routine(struct maat_garbage_bin* bin)
void maat_garbage_collect_by_force(struct maat_garbage_bin* bin)
{
struct maat_garbage_bag *p = NULL;
while (p = TAILQ_FIRST(&bin->garbage_q)) {
while ((p = TAILQ_FIRST(&bin->garbage_q)) != NULL) {
p->garbage_free(p->garbage);
TAILQ_REMOVE(&bin->garbage_q, p, entries);
FREE(p);

View File

@@ -28,7 +28,7 @@ struct group2group_schema {
int group_id_column;
int super_group_id_column;
int table_id;//ugly
struct table_manager *tbl_mgr;
struct table_manager *ref_tbl_mgr;
};
struct maat_group {
@@ -99,7 +99,7 @@ void *group2group_schema_new(cJSON *json, struct table_manager *tbl_mgr,
read_cnt++;
}
g2g_schema->tbl_mgr = tbl_mgr;
g2g_schema->ref_tbl_mgr = tbl_mgr;
if (read_cnt < 3) {
goto error;
@@ -554,13 +554,20 @@ int group2group_runtime_update(void *g2g_runtime, void *g2g_schema,
return ret;
}
int group2group_runtime_commit(void *g2g_runtime)
int group2group_runtime_commit(void *g2g_runtime, const char *table_name)
{
if (NULL == g2g_runtime) {
return -1;
}
return group2group_runtime_build_top_groups(g2g_runtime);
struct group2group_runtime *g2g_rt = (struct group2group_runtime *)g2g_runtime;
int ret = group2group_runtime_build_top_groups(g2g_runtime);
if (ret < 0) {
log_error(g2g_rt->logger, MODULE_GROUP,
"table[%s] group2group runtime commit failed", table_name);
}
return ret;
}
int group2group_runtime_get_top_groups(void *g2g_runtime, int *group_ids,

View File

@@ -387,7 +387,7 @@ int interval_runtime_update(void *interval_runtime, void *interval_schema,
return 0;
}
int interval_runtime_commit(void *interval_runtime)
int interval_runtime_commit(void *interval_runtime, const char *table_name)
{
if (NULL == interval_runtime) {
return -1;
@@ -413,14 +413,14 @@ int interval_runtime_commit(void *interval_runtime)
struct interval_matcher *old_interval_matcher = NULL;
log_info(interval_rt->logger, MODULE_INTERVAL,
"committing %zu interval rules for rebuilding interval_matcher engine",
rule_cnt);
"table[%s] committing %zu interval rules for rebuilding interval_matcher engine",
table_name, rule_cnt);
new_interval_matcher = interval_matcher_new(rules, rule_cnt);
if (NULL == new_interval_matcher) {
log_error(interval_rt->logger, MODULE_INTERVAL,
"rebuild interval_matcher engine failed when update %zu interval rules",
rule_cnt);
"table[%s]rebuild interval_matcher engine failed when update %zu interval rules",
table_name, rule_cnt);
ret = -1;
}

View File

@@ -32,7 +32,7 @@ struct ip_schema {
int sip1_column;
int sip2_column;
int table_id; //ugly
struct table_manager *tbl_mgr;
struct table_manager *ref_tbl_mgr;
};
struct ipv4_item_rule {
@@ -127,7 +127,7 @@ void *ip_schema_new(cJSON *json, struct table_manager *tbl_mgr,
read_cnt++;
}
ip_schema->tbl_mgr = tbl_mgr;
ip_schema->ref_tbl_mgr = tbl_mgr;
if (read_cnt < 7) {
goto error;
@@ -208,13 +208,8 @@ struct ip_item *ip_item_new(const char *line, struct ip_schema *ip_schema,
size_t column_offset = 0;
size_t column_len = 0;
char saddr_format[16] = {0};
char sport_format[16] = {0};
char sip1_str[40] = {0};
char sip2_str[40] = {0};
uint16_t sport1 = 0;
uint16_t sport2 = 0;
uint16_t protocol = 0;
uint16_t direction = 0;
struct ip_item *ip_item = ALLOC(struct ip_item, 1);
int ret = get_column_pos(line, ip_schema->item_id_column, &column_offset, &column_len);
@@ -431,7 +426,7 @@ int ip_runtime_update(void *ip_runtime, void *ip_schema,
return 0;
}
int ip_runtime_commit(void *ip_runtime)
int ip_runtime_commit(void *ip_runtime, const char *table_name)
{
if (NULL == ip_runtime) {
return -1;
@@ -461,13 +456,13 @@ int ip_runtime_commit(void *ip_runtime)
if (rule_cnt > 0) {
log_info(ip_rt->logger, MODULE_IP,
"committing %zu ip rules for rebuilding ip_matcher engine",
rule_cnt);
"table[%s] committing %zu ip rules for rebuilding ip_matcher engine",
table_name, rule_cnt);
new_ip_matcher = ip_matcher_new(rules, rule_cnt, &mem_used);
if (NULL == new_ip_matcher) {
log_error(ip_rt->logger, MODULE_IP,
"rebuild ip_matcher engine failed when update %zu ip rules",
rule_cnt);
"table[%s] rebuild ip_matcher engine failed when update %zu ip rules",
table_name, rule_cnt);
ret = -1;
}
}

View File

@@ -36,7 +36,7 @@ struct ip_plugin_schema {
int rule_tag_column;
struct ex_data_schema *ex_schema;
int table_id; //ugly
struct table_manager *tbl_mgr;
struct table_manager *ref_tbl_mgr;
unsigned long long update_err_cnt;
unsigned long long unmatch_tag_cnt;
@@ -57,12 +57,12 @@ void *ip_plugin_schema_new(cJSON *json, struct table_manager *tbl_mgr,
const char *table_name, struct log_handle *logger)
{
size_t read_cnt = 0;
struct ip_plugin_schema *ip_plugin_schema = ALLOC(struct ip_plugin_schema, 1);
struct ip_plugin_schema *schema = ALLOC(struct ip_plugin_schema, 1);
cJSON *custom_item = NULL;
cJSON *item = cJSON_GetObjectItem(json, "table_id");
if (item != NULL && item->type == cJSON_Number) {
ip_plugin_schema->table_id = item->valueint;
schema->table_id = item->valueint;
read_cnt++;
}
@@ -75,37 +75,43 @@ void *ip_plugin_schema_new(cJSON *json, struct table_manager *tbl_mgr,
custom_item = cJSON_GetObjectItem(item, "item_id");
if (custom_item != NULL && custom_item->type == cJSON_Number) {
ip_plugin_schema->item_id_column = custom_item->valueint;
schema->item_id_column = custom_item->valueint;
read_cnt++;
}
custom_item = cJSON_GetObjectItem(item, "ip_type");
if (custom_item != NULL && custom_item->type == cJSON_Number) {
ip_plugin_schema->ip_type_column = custom_item->valueint;
schema->ip_type_column = custom_item->valueint;
read_cnt++;
}
custom_item = cJSON_GetObjectItem(item, "start_ip");
if (custom_item != NULL && custom_item->type == cJSON_Number) {
ip_plugin_schema->start_ip_column = custom_item->valueint;
schema->start_ip_column = custom_item->valueint;
read_cnt++;
}
custom_item = cJSON_GetObjectItem(item, "end_ip");
if (custom_item != NULL && custom_item->type == cJSON_Number) {
ip_plugin_schema->end_ip_column = custom_item->valueint;
schema->end_ip_column = custom_item->valueint;
read_cnt++;
}
ip_plugin_schema->tbl_mgr = tbl_mgr;
// rule_tag is optional
custom_item = cJSON_GetObjectItem(item, "rule_tag");
if (custom_item != NULL && custom_item->type == cJSON_Number) {
schema->rule_tag_column = custom_item->valueint;
}
schema->ref_tbl_mgr = tbl_mgr;
if (read_cnt < 5) {
goto error;
}
return ip_plugin_schema;
return schema;
error:
FREE(ip_plugin_schema);
FREE(schema);
return NULL;
}
@@ -139,7 +145,7 @@ int ip_plugin_accept_tag_match(struct ip_plugin_schema *schema, const char *line
{
size_t column_offset = 0;
size_t column_len = 0;
size_t n_tag = table_manager_accept_tags_count(schema->tbl_mgr);
size_t n_tag = table_manager_accept_tags_count(schema->ref_tbl_mgr);
if (schema->rule_tag_column > 0 && n_tag > 0) {
int ret = get_column_pos(line, schema->rule_tag_column,
@@ -155,7 +161,7 @@ int ip_plugin_accept_tag_match(struct ip_plugin_schema *schema, const char *line
if (column_len > 2) {
char *tag_str = ALLOC(char, column_len + 1);
memcpy(tag_str, (line + column_offset), column_len);
ret = table_manager_accept_tags_match(schema->tbl_mgr, tag_str);
ret = table_manager_accept_tags_match(schema->ref_tbl_mgr, tag_str);
FREE(tag_str);
if (TAG_MATCH_ERR == ret) {
log_error(logger, MODULE_IP_PLUGIN,
@@ -372,36 +378,42 @@ int ip_plugin_runtime_update(void *ip_plugin_runtime, void *ip_plugin_schema,
int is_valid = get_column_value(line, valid_column);
if (is_valid < 0) {
return -1;
}
if (1 == is_valid) {
//add
ip_plugin_item = ip_plugin_item_new(line, schema, ip_plugin_rt->logger);
if (NULL == ip_plugin_item) {
return -1;
}
}
char *key = (char *)&item_id;
int ret = ip_plugin_runtime_update_row(ip_plugin_rt, schema, line, key,
sizeof(int), ip_plugin_item, is_valid);
if (ret < 0) {
if (ip_plugin_item != NULL) {
FREE(ip_plugin_item);
if (schema->ex_schema != NULL) {
if (1 == is_valid) {
// add
ip_plugin_item = ip_plugin_item_new(line, schema, ip_plugin_rt->logger);
if (NULL == ip_plugin_item) {
return -1;
}
}
return -1;
} else {
if (0 == is_valid) {
ip_plugin_rt->rule_num--;
char *key = (char *)&item_id;
int ret = ip_plugin_runtime_update_row(ip_plugin_rt, schema, line, key,
sizeof(int), ip_plugin_item, is_valid);
if (ret < 0) {
if (ip_plugin_item != NULL) {
FREE(ip_plugin_item);
}
return -1;
} else {
ip_plugin_rt->rule_num++;
if (0 == is_valid) {
ip_plugin_rt->rule_num--;
} else {
ip_plugin_rt->rule_num++;
}
}
} 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);
}
return 0;
}
int ip_plugin_runtime_commit(void *ip_plugin_runtime)
int ip_plugin_runtime_commit(void *ip_plugin_runtime, const char *table_name)
{
if (NULL == ip_plugin_runtime) {
return -1;
@@ -431,13 +443,13 @@ int ip_plugin_runtime_commit(void *ip_plugin_runtime)
if (rule_cnt > 0) {
log_info(ip_plugin_rt->logger, MODULE_IP_PLUGIN,
"committing %zu ip_plugin rules for rebuilding ip_matcher engine",
rule_cnt);
"table[%s] committing %zu ip_plugin rules for rebuilding ip_matcher engine",
table_name, rule_cnt);
new_ip_matcher = ip_matcher_new(rules, rule_cnt, &mem_used);
if (NULL == new_ip_matcher) {
log_error(ip_plugin_rt->logger, MODULE_IP_PLUGIN,
"rebuild ip_matcher engine failed when update %zu ip_plugin rules",
rule_cnt);
"table[%s] rebuild ip_matcher engine failed when update %zu ip_plugin rules",
table_name, rule_cnt);
ret = -1;
}
}

View File

@@ -120,7 +120,7 @@ int maat_kv_read_unNull(struct maat_kv_store* store, const char* key, size_t key
strlowercase(key, keylen, key_lowercase, sizeof(key_lowercase));
HASH_FIND(hh, store->hash, key_lowercase, keylen, kv);
if (kv) {
*value=kv->val;
*value = kv->val;
return 1;
} else {
return -1;

View File

@@ -41,7 +41,6 @@ struct plugin_runtime {
#define MAX_PLUGIN_PER_TABLE 32
struct plugin_schema {
int item_id_column;
int key_column;
int rule_tag_column;
int n_foreign;
@@ -50,7 +49,7 @@ struct plugin_schema {
struct plugin_callback_schema cb[MAX_PLUGIN_PER_TABLE];
struct ex_data_schema *ex_schema;
int table_id; //ugly
struct table_manager *tbl_mgr;
struct table_manager *ref_tbl_mgr;
unsigned long long update_err_cnt;
unsigned long long unmatch_tag_cnt;
@@ -76,67 +75,57 @@ static int read_integer_array(char *string, int *array, int size)
void *plugin_schema_new(cJSON *json, struct table_manager *tbl_mgr,
const char *table_name, struct log_handle *logger)
{
size_t read_cnt = 0;
struct plugin_schema *plugin_schema = ALLOC(struct plugin_schema, 1);
struct plugin_schema *schema = ALLOC(struct plugin_schema, 1);
cJSON *custom_item = NULL;
cJSON *item = cJSON_GetObjectItem(json, "table_id");
if (item != NULL && item->type == cJSON_Number) {
plugin_schema->table_id = item->valueint;
read_cnt++;
if (NULL == item || item->type != cJSON_Number) {
log_error(logger, MODULE_PLUGIN,
"plugin table %s has no table_id column", table_name);
goto error;
}
schema->table_id = item->valueint;
item = cJSON_GetObjectItem(json, "custom");
if (item == NULL || item->type != cJSON_Object) {
log_error(logger, MODULE_PLUGIN,
"table %s has no custom column", table_name);
"plugin table %s has no custom column", table_name);
goto error;
}
custom_item = cJSON_GetObjectItem(item, "item_id");
if (custom_item != NULL && custom_item->type == cJSON_Number) {
plugin_schema->item_id_column = custom_item->valueint;
read_cnt++;
}
custom_item = cJSON_GetObjectItem(item, "key");
if (custom_item != NULL && custom_item->type == cJSON_Number) {
plugin_schema->key_column = custom_item->valueint;
read_cnt++;
if (NULL == custom_item || custom_item->type != cJSON_Number) {
log_error(logger, MODULE_PLUGIN,
"plugin table: %s has no key column", table_name);
goto error;
}
schema->key_column = custom_item->valueint;
custom_item = cJSON_GetObjectItem(item, "tag");
if (custom_item != NULL && custom_item->type == cJSON_Number) {
plugin_schema->rule_tag_column = custom_item->valueint;
read_cnt++;
schema->rule_tag_column = custom_item->valueint;
}
custom_item = cJSON_GetObjectItem(item, "foreign");
if (custom_item != NULL) {
read_cnt++;
if (custom_item->type == cJSON_String) {
plugin_schema->n_foreign = read_integer_array(custom_item->valuestring,
plugin_schema->foreign_columns,
MAX_FOREIGN_CLMN_NUM);
schema->n_foreign = read_integer_array(custom_item->valuestring,
schema->foreign_columns,
MAX_FOREIGN_CLMN_NUM);
} else if (custom_item->type == cJSON_Array) {
plugin_schema->n_foreign = cJSON_GetArraySize(custom_item);
for (int i = 0; i < plugin_schema->n_foreign; i++) {
schema->n_foreign = cJSON_GetArraySize(custom_item);
for (int i = 0; i < schema->n_foreign; i++) {
cJSON *foreign_item = cJSON_GetArrayItem(custom_item, i);
assert(foreign_item->type == cJSON_Number);
plugin_schema->foreign_columns[i] = foreign_item->valueint;
schema->foreign_columns[i] = foreign_item->valueint;
}
}
}
plugin_schema->tbl_mgr = tbl_mgr;
if (read_cnt < 5) {
goto error;
}
return plugin_schema;
schema->ref_tbl_mgr = tbl_mgr;
return schema;
error:
FREE(plugin_schema);
FREE(schema);
return NULL;
}
@@ -331,7 +320,7 @@ int plugin_accept_tag_match(struct plugin_schema *schema, const char *line,
{
size_t column_offset = 0;
size_t column_len = 0;
size_t n_tag = table_manager_accept_tags_count(schema->tbl_mgr);
size_t n_tag = table_manager_accept_tags_count(schema->ref_tbl_mgr);
if (schema->rule_tag_column > 0 && n_tag > 0) {
int ret = get_column_pos(line, schema->rule_tag_column, &column_offset, &column_len);
@@ -346,7 +335,7 @@ int plugin_accept_tag_match(struct plugin_schema *schema, const char *line,
if (column_len > 2) {
char *tag_str = ALLOC(char, column_len + 1);
memcpy(tag_str, (line + column_offset), column_len);
ret = table_manager_accept_tags_match(schema->tbl_mgr, tag_str);
ret = table_manager_accept_tags_match(schema->ref_tbl_mgr, tag_str);
FREE(tag_str);
if (TAG_MATCH_ERR == ret) {
log_error(logger, MODULE_PLUGIN,
@@ -386,7 +375,7 @@ int plugin_runtime_update(void *plugin_runtime, void *plugin_schema,
return -1;
}
int item_id = get_column_value(line, schema->item_id_column);
int item_id = get_column_value(line, schema->key_column);
char *key = (char *)&item_id;
ret = plugin_runtime_update_row(plugin_rt, schema, line, key, sizeof(int), is_valid);
if (ret < 0) {
@@ -398,7 +387,7 @@ int plugin_runtime_update(void *plugin_runtime, void *plugin_schema,
return 0;
}
int plugin_runtime_commit(void *plugin_runtime)
int plugin_runtime_commit(void *plugin_runtime, const char *table_name)
{
if (NULL == plugin_runtime) {
return -1;

View File

@@ -1351,8 +1351,6 @@ void redis_monitor_traverse(long long version, struct source_redis_ctx *mr_ctx,
int call_update_num = 0;
int valid_column = -1;
enum table_type table_type;
enum scan_type scan_type;
//void *table_schema = NULL;
struct maat *maat_instance = (struct maat *)u_param;
//authorized to write

View File

@@ -24,6 +24,9 @@
#include "maat_flag.h"
#include "maat_plugin.h"
#include "maat_ip_plugin.h"
#include "maat_bool_plugin.h"
#include "maat_fqdn_plugin.h"
#include "maat_interval.h"
#include "maat_virtual.h"
#define MODULE_TABLE module_name_str("maat.table")
@@ -72,21 +75,18 @@ struct table_operations {
void (*free_runtime)(void *runtime);
int (*update_runtime)(void *runtime, void *schema, const char *line, int valid_column);
int (*commit_runtime)(void *runtime);
int (*commit_runtime)(void *runtime, const char *table_name);
};
struct table_operations table_ops[TABLE_TYPE_MAX] = {
// {
// .type = TABLE_TYPE_FLAG,
// .new_schema = flag_schema_new,
// .free_schema = flag_schema_free,
// .new_runtime = flag_runtime_new,
// .free_runtime = flag_runtime_free,
// .update_runtime = flag_runtime_update,
// .commit_runtime = flag_runtime_commit
// },
{
.type = TABLE_TYPE_FLAG
.type = TABLE_TYPE_FLAG,
.new_schema = flag_schema_new,
.free_schema = flag_schema_free,
.new_runtime = flag_runtime_new,
.free_runtime = flag_runtime_free,
.update_runtime = flag_runtime_update,
.commit_runtime = flag_runtime_commit
},
{
.type = TABLE_TYPE_EXPR,
@@ -117,21 +117,21 @@ struct table_operations table_ops[TABLE_TYPE_MAX] = {
},
{
.type = TABLE_TYPE_INTERVAL,
.new_schema = NULL,
.free_schema = NULL,
.new_runtime = NULL,
.free_runtime = NULL,
.update_runtime = NULL,
.commit_runtime = NULL
.new_schema = interval_schema_new,
.free_schema = interval_schema_free,
.new_runtime = interval_runtime_new,
.free_runtime = interval_runtime_free,
.update_runtime = interval_runtime_update,
.commit_runtime = interval_runtime_commit
},
{
.type = TABLE_TYPE_INTERVAL_PLUS,
.new_schema = NULL,
.free_schema = NULL,
.new_runtime = NULL,
.free_runtime = NULL,
.update_runtime = NULL,
.commit_runtime = NULL
.new_schema = interval_schema_new,
.free_schema = interval_schema_free,
.new_runtime = interval_runtime_new,
.free_runtime = interval_runtime_free,
.update_runtime = interval_runtime_update,
.commit_runtime = interval_runtime_commit
},
{
.type = TABLE_TYPE_DIGEST
@@ -168,21 +168,21 @@ struct table_operations table_ops[TABLE_TYPE_MAX] = {
},
{
.type = TABLE_TYPE_FQDN_PLUGIN,
.new_schema = NULL,
.free_schema = NULL,
.new_runtime = NULL,
.free_runtime = NULL,
.update_runtime = NULL,
.commit_runtime = NULL
.new_schema = fqdn_plugin_schema_new,
.free_schema = fqdn_plugin_schema_free,
.new_runtime = fqdn_plugin_runtime_new,
.free_runtime = fqdn_plugin_runtime_free,
.update_runtime = fqdn_plugin_runtime_update,
.commit_runtime = fqdn_plugin_runtime_commit
},
{
.type = TABLE_TYPE_BOOL_PLUGIN,
.new_schema = NULL,
.free_schema = NULL,
.new_runtime = NULL,
.free_runtime = NULL,
.update_runtime = NULL,
.commit_runtime = NULL
.new_schema = bool_plugin_schema_new,
.free_schema = bool_plugin_schema_free,
.new_runtime = bool_plugin_runtime_new,
.free_runtime = bool_plugin_runtime_free,
.update_runtime = bool_plugin_runtime_update,
.commit_runtime = bool_plugin_runtime_commit
},
{
.type = TABLE_TYPE_VIRTUAL,
@@ -252,6 +252,7 @@ static void register_reserved_word(struct maat_kv_store *reserved_word_map)
maat_kv_register(reserved_word_map, "compile", TABLE_TYPE_COMPILE);
maat_kv_register(reserved_word_map, "group2compile", TABLE_TYPE_GROUP2COMPILE);
maat_kv_register(reserved_word_map, "group2group", TABLE_TYPE_GROUP2GROUP);
maat_kv_register(reserved_word_map, "flag", TABLE_TYPE_FLAG);
maat_kv_register(reserved_word_map, "expr", TABLE_TYPE_EXPR);
maat_kv_register(reserved_word_map, "expr_plus", TABLE_TYPE_EXPR_PLUS);
maat_kv_register(reserved_word_map, "intval", TABLE_TYPE_INTERVAL);
@@ -314,7 +315,7 @@ struct maat_table *maat_table_new(cJSON *json, struct maat_kv_store *reserved_wo
"table(table_id:%d) has no table name", ptable->table_id);
goto error;
}
if (strlen(item->valuestring) >= NAME_MAX) {
log_error(logger, MODULE_TABLE,
"table(table_id:%d) name %s length too long",
@@ -440,7 +441,6 @@ table_manager_create(const char *table_info_path, const char *accept_tags,
if (json != NULL && json->type == cJSON_Object) {
struct maat_table *maat_tbl = maat_table_new(json, reserved_word_map, logger);
if (NULL == maat_tbl) {
log_error(logger, MODULE_TABLE, "Maat table new failed.");
continue;
}
@@ -452,7 +452,9 @@ table_manager_create(const char *table_info_path, const char *accept_tags,
maat_table_free(maat_tbl);
continue;
}
log_info(logger, MODULE_TABLE, "register table[%s]->table_id:%d",
maat_tbl->table_name, maat_tbl->table_id);
if (maat_tbl->table_type == TABLE_TYPE_COMPILE) {
if (maat_tbl->table_id < default_compile_table_id) {
default_compile_table_id = maat_tbl->table_id;
@@ -778,8 +780,8 @@ void table_manager_commit_runtime(struct table_manager *tbl_mgr, int table_id)
return;
}
printf("table_id:%d\n", table_id);
struct maat_table *ptable = tbl_mgr->tbl[table_id];
if ( table_ops[table_type].commit_runtime != NULL) {
table_ops[table_type].commit_runtime(runtime);
table_ops[table_type].commit_runtime(runtime, ptable->table_name);;
}
}

View File

@@ -23,7 +23,7 @@
struct virtual_schema {
int physical_table_id[SCAN_TYPE_MAX];
int table_id;
struct table_manager *tbl_mgr;
struct table_manager *ref_tbl_mgr;
};
void *virtual_schema_new(cJSON *json, struct table_manager *tbl_mgr,
@@ -37,7 +37,7 @@ void *virtual_schema_new(cJSON *json, struct table_manager *tbl_mgr,
}
struct virtual_schema *vt_schema = ALLOC(struct virtual_schema, 1);
vt_schema->tbl_mgr = tbl_mgr;
vt_schema->ref_tbl_mgr = tbl_mgr;
int cnt = cJSON_GetArraySize(item);
for (int i = 0; i < cnt; i++) {