[FEATURE]Compile table must register plugin table to get compile ex_data & maat_state_get_compile_table_ids API

This commit is contained in:
刘文坛
2023-08-07 04:26:13 +00:00
parent e9ffca8392
commit d29eef0423
20 changed files with 711 additions and 608 deletions

View File

@@ -279,6 +279,9 @@ int maat_state_set_last_scan(struct maat_state *state);
int maat_state_set_scan_compile_table(struct maat_state *state, int compile_table_id);
int maat_state_get_compile_table_ids(struct maat_state *state, long long *compile_ids,
size_t n_compile_ids, int *compile_table_ids);
int maat_state_get_hit_paths(struct maat_state *state, struct maat_hit_path *paths,
size_t n_path);

View File

@@ -37,12 +37,6 @@ void *group2compile_schema_new(cJSON *json, struct table_manager *tbl_mgr,
void group2compile_schema_free(void *g2c_schema);
int group2compile_associated_compile_table_id(void *g2c_schema);
int compile_table_set_ex_data_schema(struct compile_schema *compile_schema, int table_id,
maat_ex_new_func_t *new_func,
maat_ex_free_func_t *free_func,
maat_ex_dup_func_t *dup_func,
long argl, void *argp);
/* compile runtime API */
void *compile_runtime_new(void *compile_schema, size_t max_thread_num,
struct maat_garbage_bin *garbage_bin,
@@ -70,14 +64,6 @@ size_t compile_runtime_get_hit_paths(struct compile_runtime *compile_rt, int thr
struct maat_hit_path *hit_path_array,
size_t array_size, size_t n_internal_hit_path);
void *compile_runtime_get_ex_data(struct compile_runtime *compile_rt,
struct compile_schema *compile_schema,
long long compile_id);
void compile_runtime_ex_data_iterate(struct compile_runtime *compile_rt,
struct compile_schema *compile_schema);
void compile_runtime_garbage_collect_routine(void *compile_runtime);
/* group2compile runtime API */
void *group2compile_runtime_new(void *g2c_schema, size_t max_thread_num,
struct maat_garbage_bin *garbage_bin,
@@ -119,6 +105,9 @@ size_t maat_compile_state_get_hit_groups(struct maat_compile_state *compile_stat
struct maat_hit_group *hit_group_array,
size_t array_size);
int maat_compile_state_get_compile_table_id(struct maat_compile_state *compile_state,
long long compile_id);
int maat_compile_state_has_NOT_clause(struct maat_compile_state *compile_state);
#ifdef __cplusplus

View File

@@ -26,12 +26,15 @@ void maat_kv_store_free(struct maat_kv_store *store);
int maat_kv_register(struct maat_kv_store *store, const char *key, long long value);
int maat_kv_read(struct maat_kv_store *store, const char *key, long long *value);
int maat_kv_read(struct maat_kv_store *store, const char *key, long long *value_array, size_t n_array);
int maat_kv_read_unNull(struct maat_kv_store *store, const char *key, size_t key_sz, long long *value);
int maat_kv_read_unNull(struct maat_kv_store *store, const char *key, size_t key_sz,
long long *value_array, size_t n_array);
int maat_kv_write(struct maat_kv_store *store, const char *key, long long value);
int maat_kv_append(struct maat_kv_store *store, const char *key, long long value);
struct maat_kv_store *maat_kv_store_duplicate(struct maat_kv_store *store);
#ifdef __cplusplus

View File

@@ -59,7 +59,23 @@ void table_manager_destroy(struct table_manager *tbl_mgr);
size_t table_manager_table_size(struct table_manager *tbl_mgr);
size_t table_manager_table_count(struct table_manager *tbl_mgr);
int table_manager_get_table_id(struct table_manager *tbl_mgr, const char *name);
int table_manager_get_table_id(struct table_manager *tbl_mgr, const char *table_name);
/**
* @brief get table_name's all conjunction parents' table_id
*
* for example: "table_id": 1,
* "table_name":"COMPILE_CONJ"
* "db_tables":["COMPILE_DEFAULT", "COMPILE_ALIAS"]
*
* "table_id": 2,
* "table_name": "COMPILE_PLUGIN",
* "db_tables": ["COMPILE_DEFAULT"]
*
* so COMPILE_DEFAULT has two conjunction parents whose table_id is 1 and 2.
*/
int table_manager_get_conj_parent_table_ids(struct table_manager *tbl_mgr, const char *table_name,
long long *table_ids_array, size_t n_table_ids_array);
const char *table_manager_get_table_name(struct table_manager *tbl_mgr, int table_id);
enum table_type table_manager_get_table_type(struct table_manager *tbl_mgr, int table_id);

View File

@@ -377,7 +377,7 @@ static int direct_write_rule(cJSON *json, struct maat_kv_store *str2int,
if (1 == cmd[i].str2int_flag) {
long long int_value = 0;
char *p = item->valuestring;
ret = maat_kv_read(str2int, p, &int_value);
ret = maat_kv_read(str2int, p, &int_value, 1);
if (ret < 0) {
log_error(logger, MODULE_JSON2IRIS,
"[%s:%d] %s's value %s is not valid format",
@@ -636,7 +636,7 @@ static int write_region_rule(cJSON *region_json, int compile_id, int group_id,
const char *table_type_str = item->valuestring;
long long table_type_int;
int ret = maat_kv_read(p_iris->str2int_map, table_type_str, &(table_type_int));
int ret = maat_kv_read(p_iris->str2int_map, table_type_str, &(table_type_int), 1);
if (ret != 1) {
log_error(logger, MODULE_JSON2IRIS,
"[%s:%d] compile rule %d table name %s's table_type %s invalid",

View File

@@ -522,32 +522,6 @@ int maat_table_callback_register(struct maat *maat_inst, int table_id,
return 0;
}
static int compile_table_ex_schema_register(struct maat *maat_inst, int table_id,
maat_ex_new_func_t *new_func,
maat_ex_free_func_t *free_func,
maat_ex_dup_func_t *dup_func,
long argl, void *argp)
{
void *schema = table_manager_get_schema(maat_inst->tbl_mgr, table_id);
assert(schema != NULL);
int ret = compile_table_set_ex_data_schema((struct compile_schema *)schema, table_id,
new_func, free_func, dup_func,
argl, argp);
if (ret < 0) {
return -1;
}
if (maat_inst->maat_rt != NULL) {
void *runtime = table_manager_get_runtime(maat_inst->tbl_mgr, table_id);
assert(runtime != NULL);
compile_runtime_ex_data_iterate((struct compile_runtime *)runtime,
(struct compile_schema *)schema);
}
return 0;
}
static int generic_plugin_table_set_ex_schema(struct table_manager *tbl_mgr, int table_id,
maat_ex_new_func_t *new_func,
maat_ex_free_func_t *free_func,
@@ -781,16 +755,14 @@ int maat_plugin_table_ex_schema_register(struct maat *maat_inst,
int ret = -1;
enum table_type table_type = table_manager_get_table_type(maat_inst->tbl_mgr,
table_id);
if (TABLE_TYPE_COMPILE == table_type) {
ret = compile_table_ex_schema_register(maat_inst, table_id, new_func,
free_func, dup_func, argl, argp);
} else {
if (table_type == TABLE_TYPE_PLUGIN || table_type == TABLE_TYPE_IP_PLUGIN ||
table_type == TABLE_TYPE_BOOL_PLUGIN || table_type == TABLE_TYPE_FQDN_PLUGIN ) {
ret = generic_plugin_table_ex_schema_register(maat_inst, table_name,
table_id, new_func, free_func,
dup_func, argl, argp);
}
pthread_mutex_unlock(&(maat_inst->background_update_mutex));
return ret;
}
@@ -821,9 +793,7 @@ void *maat_plugin_table_get_ex_data(struct maat *maat_inst, int table_id,
void *ret = NULL;
enum table_type table_type = table_manager_get_table_type(maat_inst->tbl_mgr,
table_id);
if (TABLE_TYPE_COMPILE == table_type) {
ret = compile_runtime_get_ex_data(runtime, schema, *(long long *)key);
} else if (TABLE_TYPE_PLUGIN == table_type) {
if (TABLE_TYPE_PLUGIN == table_type) {
ret = plugin_runtime_get_ex_data(runtime, schema, key, key_len);
}
@@ -1899,6 +1869,22 @@ int maat_state_set_scan_compile_table(struct maat_state *state, int compile_tabl
return 0;
}
int maat_state_get_compile_table_ids(struct maat_state *state, long long *compile_ids,
size_t n_compile_ids, int *compile_table_ids)
{
if (NULL == state || NULL == compile_ids || 0 == n_compile_ids ||
NULL == compile_table_ids) {
return -1;
}
for (size_t i = 0; i < n_compile_ids; i++) {
compile_table_ids[i] = maat_compile_state_get_compile_table_id(state->compile_state,
compile_ids[i]);
}
return n_compile_ids;
}
int maat_state_get_hit_paths(struct maat_state *state, struct maat_hit_path *paths,
size_t n_path)
{

View File

@@ -27,16 +27,12 @@
#include "alignment.h"
#define MODULE_COMPILE module_name_str("maat.compile")
#define DEFAULT_GC_TIMEOUT_S 10
struct compile_schema {
int compile_id_column;
int rule_tag_column;
int declared_clause_num_column;
int set_flag;
int gc_timeout_s;
int table_id; //ugly
struct ex_data_schema ex_schema;
struct table_manager *ref_tbl_mgr;
struct log_handle *logger;
};
@@ -57,8 +53,6 @@ struct compile_item {
long long compile_id;
char *table_line;
size_t table_line_len;
struct compile_schema *ref_schema;
void **ex_data;
char table_name[MAX_NAME_STR_LEN];
};
@@ -85,7 +79,8 @@ struct literal_clause {
/* compile_runtime and group2compile_runtime share compile_hash_map */
struct compile_runtime {
struct bool_matcher *bm;
struct rcu_hash_table *cfg_hash; // <compile_id, struct maat_compile>
struct rcu_hash_table *cfg_hash; // <compile_id, struct maat_compile>
struct rcu_hash_table *tbl_cfg_hash; // <compile_id, table_id>
struct maat_runtime *ref_maat_rt;
time_t version;
struct literal_clause *literal2clause_hash;
@@ -137,6 +132,11 @@ struct maat_internal_hit_path {
int vtable_id;
};
struct maat_compile_table {
long long compile_id;
int table_id;
};
struct maat_compile_state {
uint8_t this_scan_hit_item_flag;
uint8_t not_clause_hit_flag;
@@ -148,12 +148,14 @@ struct maat_compile_state {
UT_array *internal_inc_hit_paths;
UT_array *all_hit_clauses;
UT_array *this_scan_hit_clauses;
UT_array *hit_compile_tables;
};
UT_icd ut_literal_id_icd = {sizeof(struct maat_literal_id), NULL, NULL, NULL};
UT_icd ut_clause_id_icd = {sizeof(long long), NULL, NULL, NULL};
UT_icd ut_hit_group_icd = {sizeof(struct maat_hit_group), NULL, NULL, NULL};
UT_icd ut_hit_path_icd = {sizeof(struct maat_internal_hit_path), NULL, NULL, NULL};
UT_icd ut_hit_compile_table_icd = {sizeof(struct maat_compile_table)};
static struct maat_compile *maat_compile_new(long long compile_id)
{
@@ -186,23 +188,6 @@ static int maat_compile_set(struct maat_compile *compile, const char *table_name
return 0;
}
static void *rule_ex_data_new(const char *table_name, int table_id,
const char *table_line,
struct ex_data_schema *ex_schema)
{
void *ex_data = NULL;
ex_schema->new_func(table_name, table_id, NULL, table_line, &ex_data,
ex_schema->argl, ex_schema->argp);
return ex_data;
}
static void rule_ex_data_free(int table_id, void **ex_data,
const struct ex_data_schema *ex_schema)
{
ex_schema->free_func(table_id, ex_data, ex_schema->argl, ex_schema->argp);
}
static int compile_accept_tag_match(struct compile_schema *schema, const char *line,
const char *table_name, struct log_handle *logger)
{
@@ -277,18 +262,11 @@ compile_item_new(const char *table_line, struct compile_schema *schema,
}
compile_item->declared_clause_num = atoi(table_line + column_offset);
compile_item->ref_schema = schema;
compile_item->ex_data = ALLOC(void *, 1);
memcpy(compile_item->table_name, table_name, sizeof(compile_item->table_name));
compile_item->table_line_len = strlen(table_line) + 1;
compile_item->table_line = ALLOC(char, compile_item->table_line_len);
memcpy(compile_item->table_line, table_line, compile_item->table_line_len);
if (1 == schema->set_flag) {
*(compile_item->ex_data) = rule_ex_data_new(table_name, schema->table_id,
compile_item->table_line,
&(schema->ex_schema));
}
return compile_item;
error:
FREE(compile_item);
@@ -297,17 +275,6 @@ error:
static void compile_item_free(struct compile_item *item)
{
struct compile_schema *schema = item->ref_schema;
if (1 == schema->set_flag) {
rule_ex_data_free(schema->table_id, item->ex_data, &(schema->ex_schema));
*item->ex_data = NULL;
}
if (item->ex_data != NULL) {
FREE(item->ex_data);
}
item->declared_clause_num = -1;
if (item->table_line != NULL) {
@@ -348,70 +315,9 @@ static void rcu_compile_cfg_free(void *user_ctx, void *data)
maat_compile_free(compile);
}
int compile_table_set_ex_data_schema(struct compile_schema *compile_schema, int table_id,
maat_ex_new_func_t *new_func,
maat_ex_free_func_t *free_func,
maat_ex_dup_func_t *dup_func,
long argl, void *argp)
static void rcu_compile_table_cfg_free(void *user_ctx, void *data)
{
if (1 == compile_schema->set_flag) {
log_error(compile_schema->logger, MODULE_COMPILE,
"[%s:%d] compile table(table_id:%d)ex schema has been set already,"
" can't set again", __FUNCTION__, __LINE__, table_id);
return -1;
}
compile_schema->ex_schema.new_func = new_func;
compile_schema->ex_schema.free_func = free_func;
compile_schema->ex_schema.dup_func = dup_func;
compile_schema->ex_schema.argl = argl;
compile_schema->ex_schema.argp = argp;
compile_schema->set_flag = 1;
return 0;
}
static void *compile_runtime_get_user_data(struct compile_runtime *compile_rt,
long long compile_id)
{
struct maat_compile *compile = rcu_hash_find(compile_rt->cfg_hash,
(char *)&compile_id,
sizeof(long long));
void *ret = NULL;
if (compile != NULL) {
ret = compile->user_data;
}
return ret;
}
static void rule_ex_data_new_cb(void *user_data, void *param,
const char *table_name, int table_id)
{
struct ex_data_schema *ex_schema = (struct ex_data_schema *)param;
struct compile_item *compile = (struct compile_item *)user_data;
void *ad = rule_ex_data_new(table_name, table_id, compile->table_line, ex_schema);
*compile->ex_data = ad;
}
static void compile_runtime_user_data_iterate(struct compile_runtime *compile_rt,
void (*callback)(void *user_data, void *param,
const char *table_name, int table_id),
void *param, int table_id)
{
/* I'm in background_update_mutex, config update can't happen, so no need to lock cfg_hash */
void **data_array = NULL;
size_t data_cnt = rcu_hash_list(compile_rt->cfg_hash, &data_array);
for (size_t i = 0; i < data_cnt; i++) {
struct maat_compile *compile = (struct maat_compile *)data_array[i];
if (compile->user_data) {
callback(compile->user_data, param, compile->table_name, table_id);
}
}
FREE(data_array);
FREE(data);
}
void *compile_schema_new(cJSON *json, struct table_manager *tbl_mgr,
@@ -465,12 +371,6 @@ void *compile_schema_new(cJSON *json, struct table_manager *tbl_mgr,
goto error;
}
//gc_timeout_s is optional
custom_item = cJSON_GetObjectItem(item, "gc_timeout_s");
if (custom_item != NULL && custom_item->type == cJSON_Number) {
schema->gc_timeout_s = custom_item->valueint;
}
schema->ref_tbl_mgr = tbl_mgr;
return schema;
error:
@@ -594,14 +494,13 @@ void *compile_runtime_new(void *compile_schema, size_t max_thread_num,
return NULL;
}
struct compile_schema *schema = (struct compile_schema *)compile_schema;
struct compile_runtime *compile_rt = ALLOC(struct compile_runtime, 1);
compile_rt->expr_match_buff = ALLOC(struct bool_expr_match,
max_thread_num * MAX_SCANNER_HIT_COMPILE_NUM);
compile_rt->version = time(NULL);
compile_rt->cfg_hash = rcu_hash_new(rcu_compile_cfg_free, NULL,
schema->gc_timeout_s + DEFAULT_GC_TIMEOUT_S);
compile_rt->cfg_hash = rcu_hash_new(rcu_compile_cfg_free, NULL, 0);
compile_rt->tbl_cfg_hash = rcu_hash_new(rcu_compile_table_cfg_free, NULL, 0);
compile_rt->literal2clause_hash = NULL;
compile_rt->logger = logger;
compile_rt->ref_garbage_bin = garbage_bin;
@@ -648,6 +547,11 @@ void compile_runtime_free(void *compile_runtime)
compile_rt->cfg_hash = NULL;
}
if (compile_rt->tbl_cfg_hash != NULL) {
rcu_hash_free(compile_rt->tbl_cfg_hash);
compile_rt->tbl_cfg_hash = NULL;
}
if (compile_rt->literal2clause_hash != NULL) {
literal2clause_hash_free(compile_rt->literal2clause_hash);
compile_rt->literal2clause_hash = NULL;
@@ -999,6 +903,19 @@ static inline int compare_hit_group(const void *pa, const void *pb)
return ret;
}
static inline int compare_compile_id(const void *a, const void *b)
{
long long ret = *(const long long *)a - *(const long long *)b;
if (0 == ret) {
return 0;
} else if (ret < 0) {
return -1;
} else {
return 1;
}
}
static struct literal_clause *
maat_compile_build_literal2clause_hash(struct compile_runtime *compile_rt)
{
@@ -1079,6 +996,29 @@ static size_t compile_state_if_new_hit_compile(struct maat_compile_state *compil
return r_in_c_cnt;
}
static void compile_state_update_hit_compile_table_id(struct maat_compile_state *compile_state,
long long compile_id, int table_id)
{
if (!utarray_find(compile_state->hit_compile_tables, &compile_id, compare_compile_id)) {
struct maat_compile_table compile_table = {compile_id, table_id};
utarray_push_back(compile_state->hit_compile_tables, &compile_table);
utarray_sort(compile_state->hit_compile_tables, compare_compile_id);
}
}
static int compile_runtime_get_compile_table_id(struct compile_runtime *compile_rt,
long long compile_id)
{
if (NULL == compile_rt || compile_id < 0) {
return -1;
}
int *table_id = rcu_hash_find(compile_rt->tbl_cfg_hash, (char *)&compile_id,
sizeof(long long));
return *table_id;
}
static size_t maat_compile_bool_matcher_match(struct compile_runtime *compile_rt,
int is_last_scan, int thread_id,
struct maat_compile_state *compile_state,
@@ -1131,6 +1071,11 @@ static size_t maat_compile_bool_matcher_match(struct compile_runtime *compile_rt
hit a compile that refer a NOT-logic group in previous scan */
user_data_array[ud_result_cnt] = compile->user_data;
ud_result_cnt++;
int table_id = compile_runtime_get_compile_table_id(compile_rt, compile->compile_id);
if (table_id >= 0) {
compile_state_update_hit_compile_table_id(compile_state, compile->compile_id, table_id);
}
}
}
}
@@ -1146,18 +1091,11 @@ static struct compile_item *compile_item_clone(struct compile_item *item)
new_item->compile_id = item->compile_id;
new_item->declared_clause_num = item->declared_clause_num;
new_item->ref_schema = item->ref_schema;
new_item->ex_data = ALLOC(void *, 1);
memcpy(new_item->table_name, item->table_name, sizeof(new_item->table_name));
new_item->table_line_len = item->table_line_len;
new_item->table_line = ALLOC(char, new_item->table_line_len);
memcpy(new_item->table_line, item->table_line, new_item->table_line_len);
if (1 == item->ref_schema->set_flag) {
*(new_item->ex_data) = rule_ex_data_new(item->table_name, item->ref_schema->table_id,
item->table_line, &(item->ref_schema->ex_schema));
}
return new_item;
}
@@ -1366,6 +1304,7 @@ struct maat_compile_state *maat_compile_state_new(void)
utarray_new(compile_state->internal_inc_hit_paths, &ut_hit_path_icd);
utarray_new(compile_state->all_hit_clauses, &ut_clause_id_icd);
utarray_new(compile_state->this_scan_hit_clauses, &ut_clause_id_icd);
utarray_new(compile_state->hit_compile_tables, &ut_hit_compile_table_icd);
return compile_state;
}
@@ -1386,6 +1325,8 @@ void maat_compile_state_reset(struct maat_compile_state *compile_state)
utarray_clear(compile_state->internal_inc_hit_paths);
utarray_clear(compile_state->all_hit_clauses);
utarray_clear(compile_state->this_scan_hit_clauses);
utarray_clear(compile_state->hit_compile_tables);
}
void maat_compile_state_free(struct maat_compile_state *compile_state,
@@ -1420,6 +1361,12 @@ void maat_compile_state_free(struct maat_compile_state *compile_state,
compile_state->this_scan_hit_clauses = NULL;
}
if (compile_state->hit_compile_tables != NULL) {
free_bytes += utarray_len(compile_state->hit_compile_tables) * sizeof(struct maat_compile_table);
utarray_free(compile_state->hit_compile_tables);
compile_state->hit_compile_tables = NULL;
}
FREE(compile_state);
free_bytes += sizeof(struct maat_compile_state);
@@ -1603,46 +1550,25 @@ static void maat_compile_state_update_hit_clause(struct maat_compile_state *comp
}
}
int maat_compile_state_get_compile_table_id(struct maat_compile_state *compile_state,
long long compile_id)
{
struct maat_compile_table *compile_table = NULL;
compile_table = utarray_find(compile_state->hit_compile_tables, &compile_id,
compare_compile_id);
if (NULL == compile_table) {
return -1;
}
return compile_table->table_id;
}
int maat_compile_state_has_NOT_clause(struct maat_compile_state *compile_state)
{
return compile_state->not_clause_hit_flag;
}
void compile_runtime_ex_data_iterate(struct compile_runtime *compile_rt,
struct compile_schema *compile_schema)
{
if (NULL == compile_rt || NULL == compile_schema ||
(0 == compile_schema->set_flag)) {
return;
}
compile_runtime_user_data_iterate(compile_rt, rule_ex_data_new_cb,
&(compile_schema->ex_schema),
compile_schema->table_id);
}
void *compile_runtime_get_ex_data(struct compile_runtime *compile_rt,
struct compile_schema *schema,
long long compile_id)
{
if (NULL == compile_rt || NULL == schema || compile_id < 0
|| (0 == schema->set_flag)) {
return NULL;
}
struct compile_item *item = NULL;
item = (struct compile_item *)compile_runtime_get_user_data(compile_rt,
compile_id);
if (NULL == item) {
return NULL;
}
void *ex_data = NULL;
schema->ex_schema.dup_func(schema->table_id, &ex_data, item->ex_data,
schema->ex_schema.argl, schema->ex_schema.argp);
return ex_data;
}
static int compile_runtime_add_compile(struct compile_runtime *compile_rt,
struct compile_schema *schema,
long long compile_id, const char *table_name,
@@ -1811,6 +1737,7 @@ int compile_runtime_update(void *compile_runtime, void *compile_schema,
if (0 == is_valid) {
// delete
compile_runtime_del_compile(compile_rt, compile_id);
rcu_hash_del(compile_rt->tbl_cfg_hash, (char *)&compile_id, sizeof(long long));
} else {
// add
int ret = compile_runtime_add_compile(compile_rt, schema, compile_id,
@@ -1818,6 +1745,14 @@ int compile_runtime_update(void *compile_runtime, void *compile_schema,
if (ret < 0) {
compile_rt->update_err_cnt++;
}
int *table_id = ALLOC(int, 1);
*table_id = table_manager_get_table_id(schema->ref_tbl_mgr, table_name);
ret = rcu_hash_add(compile_rt->tbl_cfg_hash, (char *)&compile_id,
sizeof(long long), table_id);
if (ret < 0) {
FREE(table_id);
}
}
return 0;
@@ -1970,6 +1905,7 @@ int compile_runtime_commit(void *compile_runtime, const char *table_name,
compile_rt->bm = new_bool_matcher;
compile_rt->literal2clause_hash = new_literal2clause;
rcu_hash_commit(compile_rt->cfg_hash);
rcu_hash_commit(compile_rt->tbl_cfg_hash);
maat_garbage_bagging(compile_rt->ref_garbage_bin, old_bool_matcher, NULL,
garbage_bool_matcher_free);
@@ -2232,15 +2168,3 @@ size_t maat_compile_state_get_internal_hit_paths(struct maat_compile_state *comp
return hit_path_cnt;
}
void compile_runtime_garbage_collect_routine(void *compile_runtime)
{
if (NULL == compile_runtime) {
return;
}
struct compile_runtime *compile_rt = (struct compile_runtime *)compile_runtime;
if (compile_rt->cfg_hash != NULL) {
rcu_hash_garbage_collect_routine(compile_rt->cfg_hash);
}
}

View File

@@ -144,13 +144,13 @@ static int expr_runtime_get_district_id(struct expr_runtime *expr_rt,
{
long long district_id = DISTRICT_ANY;
int map_ret = maat_kv_read(expr_rt->district_map, district, &district_id);
int map_ret = maat_kv_read(expr_rt->district_map, district, &district_id, 1);
if (map_ret < 0) {
if (NULL == expr_rt->tmp_district_map) {
expr_rt->tmp_district_map = maat_kv_store_duplicate(expr_rt->district_map);
}
map_ret = maat_kv_read(expr_rt->tmp_district_map, district, &district_id);
map_ret = maat_kv_read(expr_rt->tmp_district_map, district, &district_id, 1);
if (map_ret < 0) {
district_id = expr_rt->district_num;
maat_kv_register(expr_rt->tmp_district_map, district, district_id);
@@ -168,7 +168,7 @@ int expr_runtime_set_scan_district(struct expr_runtime *expr_rt, const char *dis
return -1;
}
return maat_kv_read_unNull(expr_rt->district_map, district, district_len, district_id);
return maat_kv_read_unNull(expr_rt->district_map, district, district_len, district_id, 1);
}
static struct expr_item *

View File

@@ -269,13 +269,13 @@ static int flag_runtime_get_district_id(struct flag_runtime *flag_rt,
{
long long district_id = DISTRICT_ANY;
int map_ret = maat_kv_read(flag_rt->district_map, district, &district_id);
int map_ret = maat_kv_read(flag_rt->district_map, district, &district_id, 1);
if (map_ret < 0) {
if (NULL == flag_rt->tmp_district_map) {
flag_rt->tmp_district_map = maat_kv_store_duplicate(flag_rt->district_map);
}
map_ret = maat_kv_read(flag_rt->tmp_district_map, district, &district_id);
map_ret = maat_kv_read(flag_rt->tmp_district_map, district, &district_id, 1);
if (map_ret < 0) {
district_id = flag_rt->district_num;
maat_kv_register(flag_rt->tmp_district_map, district, district_id);
@@ -293,7 +293,7 @@ int flag_runtime_set_scan_district(struct flag_runtime *flag_rt, const char *dis
return -1;
}
return maat_kv_read_unNull(flag_rt->district_map, district, district_len, district_id);
return maat_kv_read_unNull(flag_rt->district_map, district, district_len, district_id, 1);
}
static struct flag_item *

View File

@@ -242,13 +242,13 @@ static int interval_runtime_get_district_id(struct interval_runtime *interval_rt
{
long long district_id = DISTRICT_ANY;
int map_ret = maat_kv_read(interval_rt->district_map, district, &district_id);
int map_ret = maat_kv_read(interval_rt->district_map, district, &district_id, 1);
if (map_ret < 0) {
if (NULL == interval_rt->tmp_district_map) {
interval_rt->tmp_district_map = maat_kv_store_duplicate(interval_rt->district_map);
}
map_ret = maat_kv_read(interval_rt->tmp_district_map, district, &district_id);
map_ret = maat_kv_read(interval_rt->tmp_district_map, district, &district_id, 1);
if (map_ret < 0) {
district_id = interval_rt->district_num;
maat_kv_register(interval_rt->tmp_district_map, district, district_id);
@@ -268,7 +268,7 @@ int interval_runtime_set_scan_district(struct interval_runtime *interval_rt,
}
return maat_kv_read_unNull(interval_rt->district_map, district,
district_len, district_id);
district_len, district_id, 1);
}
static struct interval_item *

View File

@@ -12,14 +12,21 @@
#include "uthash/uthash.h"
#include "maat_utils.h"
#include "maat_kv.h"
#define MAAT_KV_MAX_KEY_LEN 512
#define MAX_ENTITY_ID_CNT 4
struct kv_entity {
long long id[MAX_ENTITY_ID_CNT];
int id_cnt; //0~4
};
struct maat_kv_pair
{
char* key; //must be lower case.
size_t keylen;
long long val;
size_t key_len;
struct kv_entity *val;
UT_hash_handle hh;
};
@@ -41,10 +48,27 @@ maat_kv_pair_new(const char* key, size_t keylen, long long value)
struct maat_kv_pair *kv = ALLOC(struct maat_kv_pair, 1);
kv->key = ALLOC(char, keylen);
kv->keylen = keylen;
kv->val = value;
kv->key_len = keylen;
kv->val = ALLOC(struct kv_entity, 1);
strlowercase(key, keylen, kv->key, kv->keylen);
strlowercase(key, keylen, kv->key, kv->key_len);
kv->val->id[0] = value;
kv->val->id_cnt = 1;
return kv;
}
static struct maat_kv_pair *
maat_kv_pair_clone(const char *key, size_t key_len, struct kv_entity *entity)
{
struct maat_kv_pair *kv = ALLOC(struct maat_kv_pair, 1);
kv->key = ALLOC(char, key_len);
kv->key_len = key_len;
kv->val = ALLOC(struct kv_entity, 1);
strlowercase(key, key_len, kv->key, kv->key_len);
*(kv->val) = *entity;
return kv;
}
@@ -55,10 +79,14 @@ static void maat_kv_pair_free(struct maat_kv_pair *kv)
return;
}
if (kv->key != NULL) {
if (kv->key != NULL) {
FREE(kv->key);
}
if (kv->val != NULL) {
FREE(kv->val);
}
FREE(kv);
}
@@ -114,8 +142,8 @@ int maat_kv_register(struct maat_kv_store* store, const char *key, long long val
return ret;
}
int maat_kv_read_unNull(struct maat_kv_store *store, const char* key,
size_t keylen, long long *value)
int maat_kv_read_unNull(struct maat_kv_store *store, const char *key, size_t keylen,
long long *value_array, size_t n_array)
{
struct maat_kv_pair *kv = NULL;
char key_lowercase[MAAT_KV_MAX_KEY_LEN] = {0};
@@ -126,36 +154,42 @@ int maat_kv_read_unNull(struct maat_kv_store *store, const char* key,
strlowercase(key, keylen, key_lowercase, sizeof(key_lowercase));
HASH_FIND(hh, store->hash, key_lowercase, keylen, kv);
int i = 0;
if (kv) {
*value = kv->val;
return 1;
for (i = 0; i < kv->val->id_cnt && i < (int)n_array; i++) {
value_array[i] = kv->val->id[i];
}
return i;
} else {
return -1;
}
}
int maat_kv_read(struct maat_kv_store *store, const char * key, long long *value)
int maat_kv_read(struct maat_kv_store *store, const char * key,
long long *value_array, size_t n_array)
{
return maat_kv_read_unNull(store, key, strlen(key), value);
return maat_kv_read_unNull(store, key, strlen(key), value_array, n_array);
}
int maat_kv_write_unNull(struct maat_kv_store* store, const char* key,
size_t keylen, long long value)
size_t key_len, long long value)
{
struct maat_kv_pair *kv = NULL;
char key_lowercase[MAAT_KV_MAX_KEY_LEN] = {0};
if (keylen > MAAT_KV_MAX_KEY_LEN) {
if (key_len > MAAT_KV_MAX_KEY_LEN) {
return -1;
}
strlowercase(key, keylen, key_lowercase, sizeof(key_lowercase));
HASH_FIND(hh, store->hash, key_lowercase, keylen, kv);
strlowercase(key, key_len, key_lowercase, sizeof(key_lowercase));
HASH_FIND(hh, store->hash, key_lowercase, key_len, kv);
if (kv) {
kv->val = value;
kv->val->id[0] = value;
kv->val->id_cnt = 1;
} else {
kv = maat_kv_pair_new(key, keylen, value);
HASH_ADD_KEYPTR(hh, store->hash, kv->key, keylen, kv);
kv = maat_kv_pair_new(key, key_len, value);
HASH_ADD_KEYPTR(hh, store->hash, kv->key, key_len, kv);
}
return 1;
@@ -166,14 +200,47 @@ int maat_kv_write(struct maat_kv_store *store, const char *key, long long value)
return maat_kv_write_unNull(store, key, strlen(key), value);
}
int maat_kv_append_unNull(struct maat_kv_store* store, const char* key,
size_t key_len, long long value)
{
struct maat_kv_pair *kv = NULL;
char key_lowercase[MAAT_KV_MAX_KEY_LEN] = {0};
if (key_len > MAAT_KV_MAX_KEY_LEN) {
return -1;
}
strlowercase(key, key_len, key_lowercase, sizeof(key_lowercase));
HASH_FIND(hh, store->hash, key_lowercase, key_len, kv);
if (kv) {
size_t id_cnt = kv->val->id_cnt;
if (id_cnt >= MAX_ENTITY_ID_CNT) {
return -1;
}
kv->val->id[id_cnt] = value;
kv->val->id_cnt++;
} else {
kv = maat_kv_pair_new(key, key_len, value);
HASH_ADD_KEYPTR(hh, store->hash, kv->key, key_len, kv);
}
return 1;
}
int maat_kv_append(struct maat_kv_store *store, const char *key, long long value)
{
return maat_kv_append_unNull(store, key, strlen(key), value);
}
struct maat_kv_store *maat_kv_store_duplicate(struct maat_kv_store *origin_map)
{
struct maat_kv_store *target = maat_kv_store_new();
struct maat_kv_pair *kv = NULL, *tmp_kv = NULL, *copy_kv = NULL;
HASH_ITER (hh, origin_map->hash, kv, tmp_kv) {
copy_kv = maat_kv_pair_new(kv->key, kv->keylen, kv->val);
HASH_ADD_KEYPTR(hh, target->hash, copy_kv->key, copy_kv->keylen, copy_kv);
copy_kv = maat_kv_pair_clone(kv->key, kv->key_len, kv->val);
HASH_ADD_KEYPTR(hh, target->hash, copy_kv->key, copy_kv->key_len, copy_kv);
}
return target;

View File

@@ -128,7 +128,6 @@ static int maat_update_cb(const char *table_name, const char *line, void *u_para
}
struct maat *maat_inst =(struct maat *)u_param;
struct maat_runtime* maat_rt = NULL;
int table_id = table_manager_get_table_id(maat_inst->tbl_mgr, table_name);
if (table_id < 0) {
log_error(maat_inst->logger, MODULE_MAAT_RULE,
@@ -137,23 +136,36 @@ static int maat_update_cb(const char *table_name, const char *line, void *u_para
return -1;
}
void *schema = table_manager_get_schema(maat_inst->tbl_mgr, table_id);
if (NULL == schema) {
log_error(maat_inst->logger, MODULE_MAAT_RULE,
"[%s:%d] update warning, table name %s doesn't have table schema",
__FUNCTION__, __LINE__, table_name);
return -1;
int update_type = MAAT_UPDATE_TYPE_INC;
if (maat_inst->creating_maat_rt != NULL) { // Full update
update_type = MAAT_UPDATE_TYPE_FULL;
}
int update_type = MAAT_UPDATE_TYPE_INC;
if (maat_inst->creating_maat_rt != NULL) { //Full update
maat_rt = maat_inst->creating_maat_rt;
update_type = MAAT_UPDATE_TYPE_FULL;
} else {
maat_rt = maat_inst->maat_rt;
}
table_manager_update_runtime(maat_rt->ref_tbl_mgr, table_name, table_id, line, update_type);
// find conjunction id for table_id
long long conj_parent_table_ids[4];
int conj_parent_table_cnt = table_manager_get_conj_parent_table_ids(maat_inst->tbl_mgr, table_name,
conj_parent_table_ids, 4);
if (conj_parent_table_cnt > 0) {
for (int i = 0; i < conj_parent_table_cnt; i++) {
int ret = table_manager_update_runtime(maat_inst->tbl_mgr, table_name,
(int)conj_parent_table_ids[i], line, update_type);
if (ret < 0) {
log_error(maat_inst->logger, MODULE_MAAT_RULE,
"[%s:%d] table<%s> update runtime error for rule:%s",
__FUNCTION__, __LINE__, table_name, line);
continue;
}
}
} else {
int ret = table_manager_update_runtime(maat_inst->tbl_mgr, table_name,
table_id, line, update_type);
if (ret < 0) {
log_error(maat_inst->logger, MODULE_MAAT_RULE,
"[%s:%d] table<%s> update runtime error for rules:%s",
__FUNCTION__, __LINE__, table_name, line);
return -1;
}
}
return 0;
}
@@ -201,10 +213,6 @@ static void maat_plugin_table_garbage_collect_routine(struct table_manager *tbl_
table_type = table_manager_get_table_type(tbl_mgr, i);
switch (table_type) {
case TABLE_TYPE_COMPILE:
runtime = table_manager_get_runtime(tbl_mgr, i);
compile_runtime_garbage_collect_routine(runtime);
break;
case TABLE_TYPE_PLUGIN:
runtime = table_manager_get_runtime(tbl_mgr, i);
ex_data_rt = plugin_runtime_get_ex_data_rt(runtime);
@@ -345,7 +353,7 @@ long long maat_runtime_get_sequence(struct maat_runtime *maat_rt, const char *ke
}
long long sequence = 1;
int map_ret = maat_kv_read(maat_rt->sequence_map, key, &sequence);
int map_ret = maat_kv_read(maat_rt->sequence_map, key, &sequence, 1);
if (map_ret < 0) {
maat_kv_register(maat_rt->sequence_map, key, sequence);
} else {

View File

@@ -51,6 +51,7 @@ struct table_manager {
int default_compile_table_id;
int g2g_table_id;
struct maat_kv_store *tablename2id_map;
struct maat_kv_store *conj_tablename2id_map;
struct maat_garbage_bin *ref_garbage_bin;
struct log_handle *logger;
@@ -517,7 +518,7 @@ maat_table_new(cJSON *json, struct maat_kv_store *reserved_word_map,
}
ret = maat_kv_read(reserved_word_map, item->valuestring,
(long long *)&(ptable->table_type));
(long long *)&(ptable->table_type), 1);
if (ret < 0) {
log_error(logger, MODULE_TABLE,
"[%s:%d] table:%s table_type %s is illegal",
@@ -579,30 +580,48 @@ static void maat_table_free(struct maat_table *maat_tbl)
FREE(maat_tbl);
}
static int register_tablename2id(cJSON *json, struct maat_kv_store *tablename2id_map,
struct log_handle *logger)
int register_single_tablename2id(struct maat_kv_store *tablename2id_map, const char *table_name,
int table_id, struct log_handle *logger)
{
cJSON *item = cJSON_GetObjectItem(json, "table_id");
if (NULL == item || item->type != cJSON_Number) {
return -1;
}
int table_id = item->valueint;
item = cJSON_GetObjectItem(json, "db_tables");
if (item != NULL && item->type != cJSON_Array) {
if (strlen(table_name) >= NAME_MAX) {
log_error(logger, MODULE_TABLE,
"[%s:%d] table(table_id:%d) has db_tables, but format is invalid"
", should be array", __FUNCTION__, __LINE__, table_id);
"[%s:%d] table:<%s> name length exceed maxium:%d",
__FUNCTION__, __LINE__, table_name, NAME_MAX);
return -1;
}
int ret = 0;
long long tmp_table_id = 0;
if (item != NULL) {
int n_table_name = cJSON_GetArraySize(item);
long long tmp_table_id = -1;
int ret = maat_kv_read(tablename2id_map, table_name, &tmp_table_id, 1);
if (ret > 0 && tmp_table_id != table_id) {
log_error(logger, MODULE_TABLE,
"[%s:%d] table:<%s>(table_id:%lld) has already been registered"
", can't register again",
__FUNCTION__, __LINE__, table_name, tmp_table_id);
return -1;
}
for (int i = 0; i < n_table_name; i++) {
cJSON *tmp_item = cJSON_GetArrayItem(item, i);
maat_kv_register(tablename2id_map, table_name, table_id);
return 0;
}
static int register_conjunction_tablename2id(cJSON *root, struct maat_kv_store *conj_tablename2id_map,
struct log_handle *logger)
{
int json_array_size = cJSON_GetArraySize(root);
for (int i = 0; i < json_array_size; i++) {
cJSON *json = cJSON_GetArrayItem(root, i);
cJSON *item = cJSON_GetObjectItem(json, "table_id");
int table_id = item->valueint;
item = cJSON_GetObjectItem(json, "db_tables");
if (NULL == item || item->type != cJSON_Array) {
continue;
}
int n_table_name = cJSON_GetArraySize(item);
for (int j = 0; j < n_table_name; j++) {
cJSON *tmp_item = cJSON_GetArrayItem(item, j);
if (NULL == tmp_item || tmp_item->type != cJSON_String) {
log_error(logger, MODULE_TABLE,
"[%s:%d] table(table_id:%d) db_tables element format invalid"
@@ -610,56 +629,106 @@ static int register_tablename2id(cJSON *json, struct maat_kv_store *tablename2id
return -1;
}
if (strlen(tmp_item->valuestring) >= NAME_MAX) {
log_error(logger, MODULE_TABLE,
"[%s:%d] table(table_id:%d) db_tables string %s length "
"exceed maxium:%d", __FUNCTION__, __LINE__, table_id,
tmp_item->valuestring, NAME_MAX);
int ret = maat_kv_append(conj_tablename2id_map, tmp_item->valuestring, table_id);
if (ret < 0) {
return -1;
}
ret = maat_kv_read(tablename2id_map, tmp_item->valuestring, &tmp_table_id);
if (ret > 0 && tmp_table_id != table_id) {
log_error(logger, MODULE_TABLE,
"[%s:%d] table:<%s>(table_id:%lld) has already been registered"
", can't register again", __FUNCTION__, __LINE__,
tmp_item->valuestring, tmp_table_id);
return -1;
}
maat_kv_register(tablename2id_map, tmp_item->valuestring, table_id);
}
}
item = cJSON_GetObjectItem(json, "table_name");
if (NULL == item || item->type != cJSON_String) {
log_error(logger, MODULE_TABLE,
"[%s:%d] table(table_id:%d) has no table_name",
__FUNCTION__, __LINE__, table_id);
return -1;
return 0;
}
static int register_tablename2id(cJSON *root, struct maat_kv_store *tablename2id_map,
const char *table_info_path, struct log_handle *logger)
{
int i = 0;
int json_array_size = cJSON_GetArraySize(root);
//pre register tablename2id
for (i = 0; i < json_array_size; i++) {
cJSON *json = cJSON_GetArrayItem(root, i);
if (NULL == json || json->type != cJSON_Object) {
log_error(logger, MODULE_TABLE,
"[%s:%d] %s has invalid json object",
__FUNCTION__, __LINE__, table_info_path);
return -1;
}
cJSON *item = cJSON_GetObjectItem(json, "table_id");
if (NULL == item || item->type != cJSON_Number) {
log_error(logger, MODULE_TABLE,
"[%s:%d] %s has invalid json object, happened in table_id column ",
__FUNCTION__, __LINE__, table_info_path);
return -1;
}
int table_id = item->valueint;
item = cJSON_GetObjectItem(json, "table_name");
if (NULL == item || item->type != cJSON_String) {
log_error(logger, MODULE_TABLE,
"[%s:%d] %s has invalid json object, happened in table_name column",
__FUNCTION__, __LINE__, table_info_path);
return -1;
}
int ret = register_single_tablename2id(tablename2id_map, item->valuestring,
table_id, logger);
if (ret < 0) {
return -1;
}
}
if (strlen(item->valuestring) >= NAME_MAX) {
log_error(logger, MODULE_TABLE,
"[%s:%d] table(table_id:%d) table_name %s length too long",
__FUNCTION__, __LINE__, table_id, item->valuestring);
return -1;
}
//register db_tables's table_name
for (i = 0; i < json_array_size; i++) {
cJSON *json = cJSON_GetArrayItem(root, i);
cJSON *item = cJSON_GetObjectItem(json, "table_id");
int table_id = item->valueint;
ret = maat_kv_read(tablename2id_map, item->valuestring, &tmp_table_id);
if (ret > 0 && tmp_table_id != table_id) {
log_error(logger, MODULE_TABLE,
"[%s:%d] table:<%s>(table_id:%lld) has already been registered,"
" can't register again", __FUNCTION__, __LINE__, item->valuestring,
tmp_table_id);
return -1;
}
item = cJSON_GetObjectItem(json, "db_tables");
if (NULL == item || item->type != cJSON_Array) {
continue;
}
maat_kv_register(tablename2id_map, item->valuestring, table_id);
int n_table_name = cJSON_GetArraySize(item);
for (int j = 0; j < n_table_name; j++) {
cJSON *tmp_item = cJSON_GetArrayItem(item, j);
if (NULL == tmp_item || tmp_item->type != cJSON_String) {
log_error(logger, MODULE_TABLE,
"[%s:%d] table(table_id:%d) db_tables element format invalid"
", should be string", __FUNCTION__, __LINE__, table_id);
return -1;
}
long long tmp_table_id = -1;
int ret = maat_kv_read(tablename2id_map, tmp_item->valuestring, &tmp_table_id, 1);
if (ret > 0) {
continue;
}
ret = register_single_tablename2id(tablename2id_map, tmp_item->valuestring,
table_id, logger);
if (ret < 0) {
return -1;
}
}
}
return 0;
}
int maat_default_compile_table_id(cJSON *json, struct log_handle *logger)
{
cJSON *item = cJSON_GetObjectItem(json, "default_compile_table");
if (NULL == item || item->type != cJSON_Number) {
return -1;
}
item = cJSON_GetObjectItem(json, "table_id");
//item is cJSON_Number which has been checked in maat_table_new
return item->valueint;
}
struct table_manager *
table_manager_create(const char *table_info_path, const char *accept_tags,
enum maat_expr_engine expr_engine, struct maat_garbage_bin *garbage_bin,
@@ -704,24 +773,28 @@ table_manager_create(const char *table_info_path, const char *accept_tags,
tbl_mgr->n_accept_tag = parse_accept_tag(accept_tags, &tbl_mgr->accept_tags, logger);
tbl_mgr->logger = logger;
tbl_mgr->tablename2id_map = maat_kv_store_new();
tbl_mgr->conj_tablename2id_map = maat_kv_store_new();
tbl_mgr->expr_engine = expr_engine;
tbl_mgr->ref_garbage_bin = garbage_bin;
for (int i = 0; i < json_array_size; i++) {
json = cJSON_GetArrayItem(root, i);
ret = register_tablename2id(root, tbl_mgr->tablename2id_map, table_info_path, logger);
if (ret < 0) {
log_error(logger, MODULE_TABLE,
"[%s:%d] register_tablename2id failed.", __FUNCTION__, __LINE__);
FREE(json_buff);
cJSON_Delete(root);
table_manager_destroy(tbl_mgr);
return NULL;
}
if (json != NULL && json->type == cJSON_Object) {
ret = register_tablename2id(json, tbl_mgr->tablename2id_map, logger);
if (ret < 0) {
log_error(logger, MODULE_TABLE,
"[%s:%d] register_tablename2id failed",
__FUNCTION__, __LINE__);
FREE(json_buff);
cJSON_Delete(root);
table_manager_destroy(tbl_mgr);
return NULL;
}
}
ret = register_conjunction_tablename2id(root, tbl_mgr->conj_tablename2id_map, logger);
if (ret < 0) {
log_error(logger, MODULE_TABLE,
"[%s:%d] register_conjunction_tablename2id failed.", __FUNCTION__, __LINE__);
FREE(json_buff);
cJSON_Delete(root);
table_manager_destroy(tbl_mgr);
return NULL;
}
int default_compile_table_id = -1;
@@ -739,21 +812,26 @@ table_manager_create(const char *table_info_path, const char *accept_tags,
goto next;
}
maat_tbl->schema = maat_table_schema_new(json, maat_tbl->table_name,
maat_tbl->table_type, tbl_mgr, logger);
if (NULL == maat_tbl->schema) {
log_error(logger, MODULE_TABLE,
"[%s:%d] Maat table schema new failed, table_name:%s",
__FUNCTION__, __LINE__, maat_tbl->table_name);
ret = -1;
goto next;
long long parent_table_ids[4];
int parent_table_cnt = table_manager_get_conj_parent_table_ids(tbl_mgr, maat_tbl->table_name,
parent_table_ids, 4);
if (parent_table_cnt <= 0) {
// if table has conjuncion parent, which can share schema and
// runtime with parent.
maat_tbl->schema = maat_table_schema_new(json, maat_tbl->table_name, maat_tbl->table_type,
tbl_mgr, logger);
if (NULL == maat_tbl->schema) {
log_error(logger, MODULE_TABLE,
"[%s:%d] Maat table schema new failed, table_name:%s",
__FUNCTION__, __LINE__, maat_tbl->table_name);
ret = -1;
goto next;
}
}
if (maat_tbl->table_type == TABLE_TYPE_COMPILE) {
if (default_compile_table_id < 0) {
default_compile_table_id = maat_tbl->table_id;
} else if (maat_tbl->table_id < default_compile_table_id) {
default_compile_table_id = maat_tbl->table_id;
default_compile_table_id = maat_default_compile_table_id(json, logger);
}
}
@@ -919,6 +997,11 @@ void table_manager_destroy(struct table_manager *tbl_mgr)
tbl_mgr->tablename2id_map = NULL;
}
if (tbl_mgr->conj_tablename2id_map != NULL) {
maat_kv_store_free(tbl_mgr->conj_tablename2id_map);
tbl_mgr->conj_tablename2id_map = NULL;
}
FREE(tbl_mgr);
}
@@ -936,14 +1019,14 @@ size_t table_manager_table_count(struct table_manager *tbl_mgr)
return tbl_mgr->n_table;
}
int table_manager_get_table_id(struct table_manager *tbl_mgr, const char *name)
int table_manager_get_table_id(struct table_manager *tbl_mgr, const char *table_name)
{
if (NULL == tbl_mgr || NULL == name) {
if (NULL == tbl_mgr || NULL == table_name) {
return -1;
}
long long table_id = -1;
int ret = maat_kv_read(tbl_mgr->tablename2id_map, name, &table_id);
int ret = maat_kv_read(tbl_mgr->tablename2id_map, table_name, &table_id, 1);
if (ret < 0) {
return -1;
}
@@ -951,6 +1034,17 @@ int table_manager_get_table_id(struct table_manager *tbl_mgr, const char *name)
return (int)table_id;
}
int table_manager_get_conj_parent_table_ids(struct table_manager *tbl_mgr, const char *table_name,
long long *table_ids_array, size_t n_table_ids_array)
{
if (NULL == tbl_mgr || NULL == table_name) {
return -1;
}
return maat_kv_read(tbl_mgr->conj_tablename2id_map, table_name,
table_ids_array, n_table_ids_array);
}
const char *table_manager_get_table_name(struct table_manager *tbl_mgr, int table_id)
{
if (NULL == tbl_mgr || table_id < 0) {

View File

@@ -1,5 +1,5 @@
{
"compile_table": "COMPILE",
"compile_table": "COMPILE_DEFAULT",
"group_table": "GROUP",
"rules": [
{

View File

@@ -1,6 +1,6 @@
{
"compile_table": "COMPILE",
"group2compile_table": "GROUP2COMPILE",
"compile_table": "COMPILE_DEFAULT",
"group2compile_table": "GROUP2COMPILE_DEFAULT",
"group2group_table": "GROUP2GROUP",
"rules": [
{

View File

@@ -1,6 +1,6 @@
{
"compile_table": "COMPILE",
"group2compile_table": "GROUP2COMPILE",
"compile_table": "COMPILE_DEFAULT",
"group2compile_table": "GROUP2COMPILE_DEFAULT",
"group2group_table": "GROUP2GROUP",
"rules": [
{

View File

@@ -287,12 +287,12 @@ int test_add_expr_command(struct maat *maat_inst, const char *expr_table,
memset(huge_serv_def, 's', sizeof(huge_serv_def) - 1);
huge_serv_def[sizeof(huge_serv_def) - 1] = '\0';
int ret = compile_table_set_line(maat_inst, "COMPILE", MAAT_OP_ADD, compile_id,
int ret = compile_table_set_line(maat_inst, "COMPILE_DEFAULT", MAAT_OP_ADD, compile_id,
huge_serv_def, 1, timeout);
EXPECT_EQ(ret, 1);
long long group_id = maat_cmd_incrby(maat_inst, "SEQUENCE_GROUP", 1);
ret = group2compile_table_set_line(maat_inst, "GROUP2COMPILE", MAAT_OP_ADD, group_id,
ret = group2compile_table_set_line(maat_inst, "GROUP2COMPILE_DEFAULT", MAAT_OP_ADD, group_id,
compile_id, 0, "null", 1, timeout);
EXPECT_EQ(ret, 1);
@@ -306,7 +306,7 @@ int test_add_expr_command(struct maat *maat_inst, const char *expr_table,
int del_command(struct maat *maat_inst, int compile_id)
{
return compile_table_set_line(maat_inst, "COMPILE", MAAT_OP_DEL, compile_id, "null", 1, 0);
return compile_table_set_line(maat_inst, "COMPILE_DEFAULT", MAAT_OP_DEL, compile_id, "null", 1, 0);
}
static void random_keyword_generate(char *keyword_buf, size_t sz)
@@ -461,6 +461,11 @@ void scan_with_old_or_new_cfg(struct maat *maat_inst, int is_old)
if (is_old) {
EXPECT_EQ(ret, MAAT_SCAN_HIT);
EXPECT_TRUE(results[0] == 1);
int table_id = -1;
int table_cnt = maat_state_get_compile_table_ids(state, results, 1, &table_id);
EXPECT_EQ(table_cnt, 1);
EXPECT_EQ(table_id, 0);
} else {
EXPECT_EQ(ret, MAAT_SCAN_OK);
}
@@ -1360,8 +1365,8 @@ TEST_F(MaatHsStringScan, dynamic_config) {
EXPECT_EQ(n_hit_result, 0);
maat_state_reset(state);
const char *compile_table_name = "COMPILE";
const char *g2c_table_name = "GROUP2COMPILE";
const char *compile_table_name = "COMPILE_DEFAULT";
const char *g2c_table_name = "GROUP2COMPILE_DEFAULT";
/* compile table add line */
long long compile_id = maat_cmd_incrby(maat_inst, "TEST_SEQ", 1);
@@ -2030,8 +2035,8 @@ TEST_F(MaatRsStringScan, dynamic_config) {
EXPECT_EQ(n_hit_result, 0);
maat_state_reset(state);
const char *compile_table_name = "COMPILE";
const char *g2c_table_name = "GROUP2COMPILE";
const char *compile_table_name = "COMPILE_DEFAULT";
const char *g2c_table_name = "GROUP2COMPILE_DEFAULT";
/* compile table add line */
long long compile_id = maat_cmd_incrby(maat_inst, "TEST_SEQ", 1);
@@ -2593,8 +2598,8 @@ TEST_F(MaatIPScan, dynamic_config) {
EXPECT_EQ(n_hit_result, 0);
maat_state_reset(state);
const char *compile_table_name = "COMPILE";
const char *g2c_table_name = "GROUP2COMPILE";
const char *compile_table_name = "COMPILE_DEFAULT";
const char *g2c_table_name = "GROUP2COMPILE_DEFAULT";
/* compile table add line */
long long compile_id = maat_cmd_incrby(maat_inst, "TEST_SEQ", 1);
@@ -4143,7 +4148,7 @@ void compile_ex_param_dup(int table_id, void **to, void **from, long argl, void
TEST_F(CompileTable, CompileRuleUpdate) {
struct maat *maat_inst = CompileTable::_shared_maat_inst;
const char *compile_table_name = "COMPILE";
const char *compile_table_name = "COMPILE_DEFAULT";
long long compile_id = maat_cmd_incrby(maat_inst, "TEST_SEQ", 1);
int ret = compile_table_set_line(maat_inst, compile_table_name,
MAAT_OP_ADD, compile_id, "null", 1, 0);
@@ -4175,6 +4180,12 @@ TEST_F(CompileTable, Conjunction1) {
EXPECT_EQ(results[0], 197);
EXPECT_EQ(results[1], 141);
int table_ids[2] = {-1, -1};
ret = maat_state_get_compile_table_ids(state, results, 2, table_ids);
EXPECT_EQ(ret, 2);
EXPECT_EQ(table_ids[0], 0);
EXPECT_EQ(table_ids[1], 1);
struct maat_hit_path hit_path[HIT_PATH_SIZE] = {0};
int n_read = maat_state_get_hit_paths(state, hit_path, HIT_PATH_SIZE);
EXPECT_EQ(n_read, 2);
@@ -4203,6 +4214,12 @@ TEST_F(CompileTable, Conjunction2) {
EXPECT_EQ(results[0], 197);
EXPECT_EQ(results[1], 141);
int table_ids[2] = {-1, -1};
ret = maat_state_get_compile_table_ids(state, results, 2, table_ids);
EXPECT_EQ(ret, 2);
EXPECT_EQ(table_ids[0], 0);
EXPECT_EQ(table_ids[1], 1);
struct maat_hit_path hit_path[HIT_PATH_SIZE] = {0};
int n_read = maat_state_get_hit_paths(state, hit_path, HIT_PATH_SIZE);
EXPECT_EQ(n_read, 2);
@@ -4219,6 +4236,40 @@ TEST_F(CompileTable, Conjunction2) {
state = NULL;
}
TEST_F(CompileTable, GetHitCompileTableID) {
long long results[ARRAY_SIZE] = {0};
size_t n_hit_result = 0;
int thread_id = 0;
const char *compile_table_name = "COMPILE_FIREWALL_CONJUNCTION";
const char *scan_data = "This is a firewall engine.";
const char *table_name = "HTTP_URL";
struct maat *maat_inst = CompileTable::_shared_maat_inst;
struct maat_state *state = maat_state_new(maat_inst, thread_id);
int table_id = maat_get_table_id(maat_inst, table_name);
ASSERT_GT(table_id, 0);
int compile_table_id = maat_get_table_id(maat_inst, compile_table_name);
ASSERT_GT(table_id, 0);
int ret = maat_state_set_scan_compile_table(state, compile_table_id);
EXPECT_EQ(ret, 0);
ret = maat_scan_string(maat_inst, table_id, scan_data, strlen(scan_data),
results, ARRAY_SIZE, &n_hit_result, state);
EXPECT_EQ(ret, MAAT_SCAN_HIT);
EXPECT_EQ(n_hit_result, 1);
EXPECT_EQ(results[0], 198);
int hit_compile_table_id = -1;
ret = maat_state_get_compile_table_ids(state, results, 1, &hit_compile_table_id);
EXPECT_EQ(ret, 1);
EXPECT_EQ(hit_compile_table_id, 4);
maat_state_free(state);
state = NULL;
}
class Policy : public testing::Test
{
protected:
@@ -4348,16 +4399,18 @@ TEST_F(Policy, CompileEXData) {
int thread_id = 0;
const char *url = "firewall should hit";
const char *table_name = "HTTP_URL";
const char *compile_table_name = "COMPILE_FIREWALL";
const char *plugin_table_name = "COMPILE_FIREWALL_PLUGIN";
const char *compile_table_name = "COMPILE_FIREWALL_CONJUNCTION";
const char *expect_name = "I have a name";
struct maat *maat_inst = Policy::_shared_maat_inst;
struct maat_state *state = maat_state_new(maat_inst, thread_id);
int table_id = maat_get_table_id(maat_inst, table_name);
int plugin_table_id = maat_get_table_id(maat_inst, plugin_table_name);
int compile_table_id = maat_get_table_id(maat_inst, compile_table_name);
int ex_data_counter = 0;
int ret = maat_plugin_table_ex_schema_register(maat_inst, compile_table_name,
int ret = maat_plugin_table_ex_schema_register(maat_inst, plugin_table_name,
compile_ex_param_new,
compile_ex_param_free,
compile_ex_param_dup,
@@ -4374,7 +4427,7 @@ TEST_F(Policy, CompileEXData) {
EXPECT_EQ(n_hit_result, 1);
EXPECT_EQ(results[0], 198);
void *ex_data = maat_plugin_table_get_ex_data(maat_inst, compile_table_id,
void *ex_data = maat_plugin_table_get_ex_data(maat_inst, plugin_table_id,
(char *)&results[0], sizeof(long long));
ASSERT_TRUE(ex_data!=NULL);
struct rule_ex_param *param = (struct rule_ex_param *)ex_data;
@@ -4916,8 +4969,8 @@ TEST_F(MaatCmdTest, SetIP) {
size_t n_hit_result = 0;
int thread_id = 0;
const char *ip_table_name = "IP_CONFIG";
const char *compile_table_name = "COMPILE";
const char *g2c_table_name = "GROUP2COMPILE";
const char *compile_table_name = "COMPILE_DEFAULT";
const char *g2c_table_name = "GROUP2COMPILE_DEFAULT";
struct maat *maat_inst = MaatCmdTest::_shared_maat_inst;
struct maat_state *state = maat_state_new(maat_inst, thread_id);
@@ -4970,6 +5023,7 @@ TEST_F(MaatCmdTest, SetExpr) {
const char *keywords1 = "Hiredis";
const char *keywords2 = "C Client";
const char *compile_table_name = "COMPILE_DEFAULT";
char escape_buff1[256], escape_buff2[256];
char keywords[512];
@@ -4999,10 +5053,10 @@ TEST_F(MaatCmdTest, SetExpr) {
EXPECT_TRUE(results[0] == compile_id || results[0] == (compile_id - 1));
maat_state_reset(state);
ret = compile_table_set_line(maat_inst, "COMPILE", MAAT_OP_DEL, compile_id-1,
ret = compile_table_set_line(maat_inst, compile_table_name, MAAT_OP_DEL, compile_id-1,
"null", 1, 0);
EXPECT_EQ(ret, 1);
ret = compile_table_set_line(maat_inst, "COMPILE", MAAT_OP_DEL, compile_id,
ret = compile_table_set_line(maat_inst, compile_table_name, MAAT_OP_DEL, compile_id,
"null", 1, 0);
EXPECT_EQ(ret, 1);
sleep(WAIT_FOR_EFFECTIVE_S);
@@ -5027,9 +5081,9 @@ TEST_F(MaatCmdTest, SetExpr8) {
const char *scan_data8 = "string1, string2, string3, string4, string5, string6, string7, string8";
const char *scan_data7 = "string1, string2, string3, string4, string5, string6, string7";
const char *compile_table_name = "COMPILE";
const char *compile_table_name = "COMPILE_DEFAULT";
const char *table_name = "KEYWORDS_TABLE";
const char *g2c_table_name = "GROUP2COMPILE";
const char *g2c_table_name = "GROUP2COMPILE_DEFAULT";
const char *keywords8 = "string1&string2&string3&string4&string5&string6&string7&string8";
const char *keywords7 = "string1&string2&string3&string4&string5&string6&string7";
@@ -5096,6 +5150,8 @@ TEST_F(MaatCmdTest, SameFilterRefByOneCompile) {
const char *vtable_name = "HTTP_URL_FILTER";
const char *scan_data = "http://filtermenot.com";
const char *keywords = "menot.com";
const char *compile_table_name = "COMPILE_DEFAULT";
const char *g2c_table_name = "GROUP2COMPILE_DEFAULT";
long long results[ARRAY_SIZE] = {0};
size_t n_hit_result = 0;
int thread_id = 0;
@@ -5106,17 +5162,17 @@ TEST_F(MaatCmdTest, SameFilterRefByOneCompile) {
ASSERT_GT(table_id, 0);
long long compile_id = maat_cmd_incrby(maat_inst, "TEST_SEQ", 1);
int ret = compile_table_set_line(maat_inst, "COMPILE", MAAT_OP_ADD, compile_id,
int ret = compile_table_set_line(maat_inst, compile_table_name, MAAT_OP_ADD, compile_id,
"null", 2, 0); // compile has two clause
EXPECT_EQ(ret, 1);
//clause1 & clause2 has same filter => {vtable_id, group_id}
long long group_id = maat_cmd_incrby(maat_inst, "SEQUENCE_GROUP", 1);
ret = group2compile_table_set_line(maat_inst, "GROUP2COMPILE", MAAT_OP_ADD,
ret = group2compile_table_set_line(maat_inst, g2c_table_name, MAAT_OP_ADD,
group_id, compile_id, 0, vtable_name, 1, 0);
EXPECT_EQ(ret, 1);
ret = group2compile_table_set_line(maat_inst, "GROUP2COMPILE", MAAT_OP_ADD,
ret = group2compile_table_set_line(maat_inst, g2c_table_name, MAAT_OP_ADD,
group_id, compile_id, 0, vtable_name, 2, 0);
EXPECT_EQ(ret, 1);
@@ -5220,9 +5276,9 @@ TEST_F(MaatCmdTest, ReturnRuleIDWithDescendingOrder) {
TEST_F(MaatCmdTest, SubGroup) {
const char *table_name = "HTTP_URL";
const char *g2c_table_name = "GROUP2COMPILE";
const char *compile_table_name = "COMPILE_DEFAULT";
const char *g2c_table_name = "GROUP2COMPILE_DEFAULT";
const char *g2g_table_name = "GROUP2GROUP";
const char *compile_table_name = "COMPILE";
const char *scan_data1 = "www.v2ex.com/t/573028#程序员的核心竞争力是什么";
const char *keyword1 = "程序员&核心竞争力";
const char *scan_data2 = "https://ask.leju.com/bj/detail/12189672562229248/?bi=tg&type=sina-pc"
@@ -5376,8 +5432,8 @@ TEST_F(MaatCmdTest, SubGroup) {
TEST_F(MaatCmdTest, RefGroup) {
const char *table_name = "HTTP_URL";
const char* g2c_table_name = "GROUP2COMPILE";
const char* compile_table_name = "COMPILE";
const char* compile_table_name = "COMPILE_DEFAULT";
const char* g2c_table_name = "GROUP2COMPILE_DEFAULT";
const char* scan_data1 = "m.facebook.com/help/2297503110373101?helpref=hc_nav&refid=69";
const char* keyword1 = "something-should-not-hit";
const char* keyword2 = "facebook.com/help/2297503110373101";
@@ -5454,8 +5510,8 @@ TEST_F(MaatCmdTest, RefGroup) {
}
TEST_F(MaatCmdTest, VirtualTable) {
const char* g2c_table_name = "GROUP2COMPILE";
const char* compile_table_name = "COMPILE";
const char* compile_table_name = "COMPILE_DEFAULT";
const char* g2c_table_name = "GROUP2COMPILE_DEFAULT";
const char* table_name="HTTP_SIGNATURE";
int thread_id = 0;
struct maat *maat_inst = MaatCmdTest::_shared_maat_inst;
@@ -5836,11 +5892,12 @@ void plugin_ex_dup_cb(int table_id, void **to, void **from, long argl, void *arg
}
TEST_F(MaatCmdTest, CompileEXData) {
const char *compile_table_name = "COMPILE_FIREWALL";
const char *plugin_table_name = "COMPILE_FIREWALL_PLUGIN";
const char *compile_table_name = "COMPILE_FIREWALL_DEFAULT";
struct maat *maat_inst = MaatCmdTest::_shared_maat_inst;
int *ex_data_counter = MaatCmdTest::_ex_data_counter;
int compile_table_id = maat_get_table_id(maat_inst, compile_table_name);
EXPECT_GT(compile_table_id, 0);
int plugin_table_id = maat_get_table_id(maat_inst, plugin_table_name);
EXPECT_GT(plugin_table_id, 0);
long long compile1_id = maat_cmd_incrby(maat_inst, "TEST_SEQ", 1);
int ret = compile_table_set_line(maat_inst, compile_table_name, MAAT_OP_ADD,
@@ -5853,7 +5910,7 @@ TEST_F(MaatCmdTest, CompileEXData) {
sleep(WAIT_FOR_EFFECTIVE_S);
*ex_data_counter = 0;
ret = maat_plugin_table_ex_schema_register(maat_inst, compile_table_name,
ret = maat_plugin_table_ex_schema_register(maat_inst, plugin_table_name,
compile_ex_param_new,
compile_ex_param_free,
compile_ex_param_dup,
@@ -5861,13 +5918,13 @@ TEST_F(MaatCmdTest, CompileEXData) {
ASSERT_TRUE(ret == 0);
EXPECT_EQ(*ex_data_counter, 2);
void *ex_data = maat_plugin_table_get_ex_data(maat_inst, compile_table_id,
void *ex_data = maat_plugin_table_get_ex_data(maat_inst, plugin_table_id,
(char *)&compile1_id, sizeof(long long));
ASSERT_TRUE(ex_data != NULL);
struct rule_ex_param *param = (struct rule_ex_param *)ex_data;
EXPECT_EQ(param->id, 1111);
ex_data = maat_plugin_table_get_ex_data(maat_inst, compile_table_id,
ex_data = maat_plugin_table_get_ex_data(maat_inst, plugin_table_id,
(char *)&compile2_id, sizeof(long long));
ASSERT_TRUE(ex_data != NULL);
param = (struct rule_ex_param *)ex_data;
@@ -5875,10 +5932,10 @@ TEST_F(MaatCmdTest, CompileEXData) {
ret = compile_table_set_line(maat_inst, compile_table_name, MAAT_OP_DEL,
compile2_id, "test:compile2,2222", 1, 0);
sleep(WAIT_FOR_EFFECTIVE_S * 5);
sleep(WAIT_FOR_EFFECTIVE_S);
EXPECT_EQ(param->id, 2222);
sleep(2);
//excced gc_timeout_s(11s), the data pointed by param has been freed
//excced gc_timeout_s(3s), the data pointed by param has been freed
}
TEST_F(MaatCmdTest, PluginEXData) {
@@ -6218,8 +6275,8 @@ TEST_F(MaatCmdTest, UpdateBoolPlugin) {
#define COMPILE_ID_NUMS 1000
TEST_F(MaatCmdTest, GroupInMassCompiles) {
const char* g2c_table_name = "GROUP2COMPILE";
const char* compile_table_name = "COMPILE";
const char* compile_table_name = "COMPILE_DEFAULT";
const char* g2c_table_name = "GROUP2COMPILE_DEFAULT";
const char* table_url = "HTTP_URL";
const char* table_appid = "APP_ID";
int thread_id = 0;
@@ -6323,9 +6380,9 @@ TEST_F(MaatCmdTest, GroupInMassCompiles) {
}
TEST_F(MaatCmdTest, HitGroup) {
const char *compile_table_name = "COMPILE_DEFAULT";
const char *g2c_table_name = "GROUP2COMPILE_DEFAULT";
const char *g2g_table_name = "GROUP2GROUP";
const char *g2c_table_name = "GROUP2COMPILE";
const char *compile_table_name = "COMPILE";
const char *http_sig_table_name = "HTTP_SIGNATURE";
const char *ip_table_name = "IP_CONFIG";
const char *keywords_table_name = "KEYWORDS_TABLE";
@@ -6652,9 +6709,9 @@ TEST_F(MaatCmdTest, HitGroup) {
}
TEST_F(MaatCmdTest, HitPath) {
const char *g2g_table_name = "GROUP2GROUP";
const char *g2c_table_name = "GROUP2COMPILE";
const char *compile_table_name = "COMPILE";
const char *compile_table_name = "COMPILE_DEFAULT";
const char *g2c_table_name = "GROUP2COMPILE_DEFAULT";
const char *g2g_table_name = "GROUP2GROUP";
const char *http_sig_table_name = "HTTP_SIGNATURE";
const char *ip_table_name = "IP_CONFIG";
const char *keywords_table_name = "KEYWORDS_TABLE";
@@ -6911,9 +6968,9 @@ that the edges be all directed in the same direction.";
TEST_F(MaatCmdTest, SameSuperGroupRefByMultiCompile) {
char temp[1024]={0};
int thread_id = 0;
const char *g2g_table_name = "GROUP2GROUP";
const char *g2c_table_name = "GROUP2COMPILE";
const char *compile_table_name = "COMPILE";
const char *compile_table_name = "COMPILE_DEFAULT";
const char *g2c_table_name = "GROUP2COMPILE_DEFAULT";
const char *g2g_table_name = "GROUP2GROUP";
const char *http_sig_table_name = "HTTP_SIGNATURE";
struct maat *maat_inst = MaatCmdTest::_shared_maat_inst;
@@ -7002,8 +7059,8 @@ TEST_F(MaatCmdTest, SameSuperGroupRefByMultiCompile) {
}
TEST_F(MaatCmdTest, SameScanStatusWhenClauseUpdate_TSG6419) {
const char *g2c_table_name = "GROUP2COMPILE";
const char* compile_table_name = "COMPILE";
const char* compile_table_name = "COMPILE_DEFAULT";
const char *g2c_table_name = "GROUP2COMPILE_DEFAULT";
const char* ip_table_name = "IP_PLUS_CONFIG";
const char *app_id_table_name = "APP_ID";
int thread_id = 0;
@@ -7096,8 +7153,8 @@ TEST_F(MaatCmdTest, SameScanStatusWhenClauseUpdate_TSG6419) {
}
TEST_F(MaatCmdTest, GroupEdit) {
const char *g2c_table_name = "GROUP2COMPILE";
const char *compile_table_name = "COMPILE";
const char *compile_table_name = "COMPILE_DEFAULT";
const char *g2c_table_name = "GROUP2COMPILE_DEFAULT";
const char *ip_table_name = "IP_PLUS_CONFIG";
const char *app_id_table_name = "APP_ID";
int thread_id = 0;
@@ -7215,8 +7272,8 @@ TEST_F(MaatCmdTest, GroupEdit) {
}
TEST_F(MaatCmdTest, CompileDelete_TSG6548) {
const char* g2c_table_name = "GROUP2COMPILE";
const char* compile_table_name = "COMPILE";
const char* compile_table_name = "COMPILE_DEFAULT";
const char* g2c_table_name = "GROUP2COMPILE_DEFAULT";
const char* ip_table_name = "IP_PLUS_CONFIG";
int thread_id = 0;
struct maat *maat_inst = MaatCmdTest::_shared_maat_inst;
@@ -7286,8 +7343,8 @@ TEST_F(MaatCmdTest, CompileDelete_TSG6548) {
}
TEST_F(MaatCmdTest, UpdateDeadLockDetection) {
const char* g2c_table_name = "GROUP2COMPILE";
const char* compile_table_name = "COMPILE";
const char* compile_table_name = "COMPILE_DEFAULT";
const char* g2c_table_name = "GROUP2COMPILE_DEFAULT";
const char* table_http_url = "HTTP_URL";
int thread_id = 0;
struct maat *maat_inst = MaatCmdTest::_shared_maat_inst;
@@ -7361,8 +7418,8 @@ TEST_F(MaatCmdTest, UpdateDeadLockDetection) {
}
TEST_F(MaatCmdTest, StreamScanWhenExprTableIncUpdate) {
const char* g2c_table_name = "GROUP2COMPILE";
const char* compile_table_name = "COMPILE";
const char* compile_table_name = "COMPILE_DEFAULT";
const char* g2c_table_name = "GROUP2COMPILE_DEFAULT";
const char* scan_table_name = "KEYWORDS_TABLE";
int thread_id = 0;
struct maat *maat_inst = MaatCmdTest::_shared_maat_inst;
@@ -7423,8 +7480,8 @@ TEST_F(MaatCmdTest, StreamScanWhenExprTableIncUpdate) {
}
TEST_F(MaatCmdTest, StreamScanSegfaultWhenVersionRollBack_TSG6324) {
const char* g2c_table_name = "GROUP2COMPILE";
const char* compile_table_name = "COMPILE";
const char* compile_table_name = "COMPILE_DEFAULT";
const char* g2c_table_name = "GROUP2COMPILE_DEFAULT";
const char* scan_table_name = "KEYWORDS_TABLE";
int thread_id = 0;
struct maat *maat_inst = MaatCmdTest::_shared_maat_inst;
@@ -7482,8 +7539,8 @@ TEST_F(MaatCmdTest, StreamScanSegfaultWhenVersionRollBack_TSG6324) {
}
TEST_F(MaatCmdTest, IPAndStreamScanWhenIncUpdate) {
const char *g2c_table_name = "GROUP2COMPILE";
const char *compile_table_name = "COMPILE";
const char *compile_table_name = "COMPILE_DEFAULT";
const char *g2c_table_name = "GROUP2COMPILE_DEFAULT";
const char *expr_table_name = "KEYWORDS_TABLE";
const char *ip_table_name = "IP_PLUS_CONFIG";
int thread_id = 0;
@@ -7574,8 +7631,8 @@ TEST_F(MaatCmdTest, IPAndStreamScanWhenIncUpdate) {
}
TEST_F(MaatCmdTest, IPAndStreamScanWhenFullUpdate) {
const char *g2c_table_name = "GROUP2COMPILE";
const char *compile_table_name = "COMPILE";
const char *compile_table_name = "COMPILE_DEFAULT";
const char *g2c_table_name = "GROUP2COMPILE_DEFAULT";
const char *ip_table_name = "IP_PLUS_CONFIG";
const char *expr_table_name = "KEYWORDS_TABLE";
int thread_id = 0;
@@ -7664,8 +7721,8 @@ TEST_F(MaatCmdTest, IPAndStreamScanWhenFullUpdate) {
}
TEST_F(MaatCmdTest, IPAndStringScanWhenIncUpdate) {
const char *g2c_table_name = "GROUP2COMPILE";
const char *compile_table_name = "COMPILE";
const char *compile_table_name = "COMPILE_DEFAULT";
const char *g2c_table_name = "GROUP2COMPILE_DEFAULT";
const char *expr_table_name = "HTTP_URL";
const char *ip_table_name = "IP_PLUS_CONFIG";
const char *keywords = "IP&stringinc";
@@ -7755,8 +7812,8 @@ TEST_F(MaatCmdTest, IPAndStringScanWhenIncUpdate) {
}
TEST_F(MaatCmdTest, IPAndStringScanWhenFullupdate) {
const char *g2c_table_name = "GROUP2COMPILE";
const char *compile_table_name = "COMPILE";
const char *compile_table_name = "COMPILE_DEFAULT";
const char *g2c_table_name = "GROUP2COMPILE_DEFAULT";
const char *ip_table_name = "IP_PLUS_CONFIG";
const char *expr_table_name = "HTTP_URL";
const char *keywords = "IP&string";

View File

@@ -306,11 +306,11 @@ static void test_add_expr_command(struct maat *maat_inst, const char *table_name
const char *keywords)
{
long long compile_id = maat_cmd_incrby(maat_inst, "TEST_SEQ", 1);
int ret = compile_table_set_line(maat_inst, "COMPILE", MAAT_OP_ADD, compile_id, "null", 1, 0);
int ret = compile_table_set_line(maat_inst, "COMPILE_DEFAULT", MAAT_OP_ADD, compile_id, "null", 1, 0);
EXPECT_EQ(ret, 1);
long long group_id = maat_cmd_incrby(maat_inst, "SEQUENCE_GROUP", 1);
ret = group2compile_table_set_line(maat_inst, "GROUP2COMPILE", MAAT_OP_ADD, group_id,
ret = group2compile_table_set_line(maat_inst, "GROUP2COMPILE_DEFAULT", MAAT_OP_ADD, group_id,
compile_id, 0, "null", 1, 0);
EXPECT_EQ(ret, 1);
@@ -324,11 +324,11 @@ static void test_add_ip_command(struct maat *maat_inst, const char *table_name,
const char *ip, uint16_t port)
{
long long compile_id = maat_cmd_incrby(maat_inst, "TEST_SEQ", 1);
int ret = compile_table_set_line(maat_inst, "COMPILE", MAAT_OP_ADD, compile_id, "null", 1, 0);
int ret = compile_table_set_line(maat_inst, "COMPILE_DEFAULT", MAAT_OP_ADD, compile_id, "null", 1, 0);
EXPECT_EQ(ret, 1);
long long group_id = maat_cmd_incrby(maat_inst, "SEQUENCE_GROUP", 1);
ret = group2compile_table_set_line(maat_inst, "GROUP2COMPILE", MAAT_OP_ADD, group_id,
ret = group2compile_table_set_line(maat_inst, "GROUP2COMPILE_DEFAULT", MAAT_OP_ADD, group_id,
compile_id, 0, "null", 1, 0);
EXPECT_EQ(ret, 1);
@@ -342,11 +342,11 @@ static void test_add_integer_command(struct maat *maat_inst, const char *table_n
int low_bound, int up_bound)
{
long long compile_id = maat_cmd_incrby(maat_inst, "TEST_SEQ", 1);
int ret = compile_table_set_line(maat_inst, "COMPILE", MAAT_OP_ADD, compile_id, "null", 1, 0);
int ret = compile_table_set_line(maat_inst, "COMPILE_DEFAULT", MAAT_OP_ADD, compile_id, "null", 1, 0);
EXPECT_EQ(ret, 1);
long long group_id = maat_cmd_incrby(maat_inst, "SEQUENCE_GROUP", 1);
ret = group2compile_table_set_line(maat_inst, "GROUP2COMPILE", MAAT_OP_ADD, group_id,
ret = group2compile_table_set_line(maat_inst, "GROUP2COMPILE_DEFAULT", MAAT_OP_ADD, group_id,
compile_id, 0, "null", 1, 0);
EXPECT_EQ(ret, 1);
@@ -360,11 +360,11 @@ static void test_add_flag_command(struct maat *maat_inst, const char *table_name
long long flag, long long flag_mask)
{
long long compile_id = maat_cmd_incrby(maat_inst, "TEST_SEQ", 1);
int ret = compile_table_set_line(maat_inst, "COMPILE", MAAT_OP_ADD, compile_id, "null", 1, 0);
int ret = compile_table_set_line(maat_inst, "COMPILE_DEFAULT", MAAT_OP_ADD, compile_id, "null", 1, 0);
EXPECT_EQ(ret, 1);
long long group_id = maat_cmd_incrby(maat_inst, "SEQUENCE_GROUP", 1);
ret = group2compile_table_set_line(maat_inst, "GROUP2COMPILE", MAAT_OP_ADD, group_id,
ret = group2compile_table_set_line(maat_inst, "GROUP2COMPILE_DEFAULT", MAAT_OP_ADD, group_id,
compile_id, 0, "null", 1, 0);
EXPECT_EQ(ret, 1);

View File

@@ -1687,28 +1687,6 @@
}
]
},
{
"compile_id": 175,
"service": 0,
"action": 0,
"do_blacklist": 0,
"do_log": 0,
"effective_rage": 0,
"user_region": "ipv4_composition.match",
"is_valid": "yes",
"groups": [
{
"group_name": "ipv4_composition.source",
"virtual_table": "COMPOSITION_IP_SOURCE",
"not_flag": 0
},
{
"group_name": "ipv4_composition.destination",
"virtual_table": "COMPOSITION_IP_DESTINATION",
"not_flag": 0
}
]
},
{
"compile_id": 176,
"service": 0,
@@ -1741,24 +1719,6 @@
}
]
},
{
"compile_id": 177,
"service": 0,
"action": 0,
"do_blacklist": 0,
"do_log": 0,
"effective_rage": 0,
"user_region": "ipv4_composition.session.match",
"is_valid": "yes",
"groups": [
{
"group_name": "ipv4_composition.session",
"virtual_table": "COMPOSITION_IP_SESSION",
"not_flag": 0,
"clause_index": 1
}
]
},
{
"compile_id": 178,
"service": 1,
@@ -1819,47 +1779,6 @@
}
]
},
{
"compile_id": 180,
"service": 0,
"action": 0,
"do_blacklist": 0,
"do_log": 0,
"effective_rage": 0,
"user_region": "Hierarchy_VirtualWithTwoPhysical",
"is_valid": "yes",
"groups": [
{
"group_name": "FQDN_OBJ1",
"virtual_table": "VIRTUAL_SSL_SNI",
"not_flag": 0,
"clause_index": 0
},
{
"group_name": "FQDN_CAT1",
"virtual_table": "VIRTUAL_SSL_SNI",
"not_flag": 0,
"clause_index": 0
}
]
},
{
"compile_id": 181,
"service": 0,
"action": 0,
"do_blacklist": 0,
"do_log": 0,
"effective_rage": 0,
"user_region": "ipv4_composition.match",
"is_valid": "yes",
"groups": [
{
"group_name": "IPv4-composition-source-only",
"virtual_table": "COMPOSITION_IP_SOURCE",
"not_flag": 0
}
]
},
{
"compile_id": 182,
"service": 1,
@@ -1942,28 +1861,6 @@
}
]
},
{
"compile_id": 185,
"service": 0,
"action": 0,
"do_blacklist": 0,
"do_log": 0,
"effective_rage": 0,
"user_region": "ipv4_composition.NOT_match",
"is_valid": "yes",
"groups": [
{
"group_name": "IPv4-composition-NOT-client-ip",
"virtual_table": "COMPOSITION_IP_SOURCE",
"not_flag": 0
},
{
"group_name": "IPv4-composition-NOT-server-ip",
"virtual_table": "COMPOSITION_IP_DESTINATION",
"not_flag": 1
}
]
},
{
"compile_id": 186,
"service": 1,
@@ -2361,7 +2258,7 @@
"do_blacklist": 1,
"do_log": 1,
"user_region": "Something:I\\bhave\\ba\\bname,7799",
"compile_table_name": "COMPILE_FIREWALL",
"compile_table_name": "COMPILE_FIREWALL_DEFAULT",
"is_valid": "yes",
"groups": [
{
@@ -3112,14 +3009,6 @@
"3\t192.168.1.1\t103\t1"
]
},
{
"table_name": "TEST_PLUGIN_TABLE",
"table_content": [
"1\t3388\t99\t1",
"2\t3355\t66\t1",
"3\tcccc\t11\t1"
]
},
{
"table_name": "TEST_PLUGIN_EXDATA_TABLE",
"table_content": [

View File

@@ -1,12 +1,10 @@
[
{
"table_id":0,
"table_name":"COMPILE",
"db_tables":["COMPILE_DEFAULT", "COMPILE_ALIAS"],
"table_name":"COMPILE_DEFAULT",
"table_type":"compile",
"valid_column":8,
"custom": {
"gc_timeout_s": 3,
"compile_id":1,
"tags":6,
"clause_num":9
@@ -14,22 +12,20 @@
},
{
"table_id":1,
"table_name":"GROUP2COMPILE",
"db_tables":["GROUP2COMPILE_DEFAULT", "GROUP2COMPILE_ALIAS"],
"table_type":"group2compile",
"associated_compile_table_id":0,
"valid_column":3,
"table_name":"COMPILE_ALIAS",
"table_type":"compile",
"valid_column":8,
"custom": {
"group_id":1,
"compile_id":2,
"not_flag":4,
"virtual_table_name":5,
"clause_index":6
"compile_id":1,
"tags":6,
"clause_num":9
}
},
{
"table_id":2,
"table_name":"COMPILE_FIREWALL",
"table_name":"COMPILE_CONJUNCTION",
"db_tables":["COMPILE_DEFAULT", "COMPILE_ALIAS"],
"default_compile_table":1,
"table_type":"compile",
"valid_column":8,
"custom": {
@@ -40,7 +36,8 @@
},
{
"table_id":3,
"table_name":"GROUP2COMPILE_FIREWALL",
"table_name":"GROUP2COMPILE",
"db_tables":["GROUP2COMPILE_DEFAULT", "GROUP2COMPILE_ALIAS"],
"table_type":"group2compile",
"associated_compile_table_id":2,
"valid_column":3,
@@ -54,6 +51,43 @@
},
{
"table_id":4,
"table_name":"COMPILE_FIREWALL_DEFAULT",
"table_type":"compile",
"valid_column":8,
"custom": {
"compile_id":1,
"tags":6,
"clause_num":9
}
},
{
"table_id":5,
"table_name":"COMPILE_FIREWALL_CONJUNCTION",
"db_tables":["COMPILE_FIREWALL_DEFAULT"],
"table_type":"compile",
"valid_column":8,
"custom": {
"compile_id":1,
"tags":6,
"clause_num":9
}
},
{
"table_id":6,
"table_name":"GROUP2COMPILE_FIREWALL",
"table_type":"group2compile",
"associated_compile_table_id":5,
"valid_column":3,
"custom": {
"group_id":1,
"compile_id":2,
"not_flag":4,
"virtual_table_name":5,
"clause_index":6
}
},
{
"table_id":7,
"table_name":"GROUP2GROUP",
"table_type":"group2group",
"valid_column":4,
@@ -64,7 +98,33 @@
}
},
{
"table_id":5,
"table_id":8,
"table_name":"COMPILE_PLUGIN",
"db_tables":["COMPILE_DEFAULT", "COMPILE_ALIAS"],
"table_type":"plugin",
"valid_column":8,
"custom": {
"gc_timeout_s":3,
"key_type":"integer",
"key_len":8,
"key":1
}
},
{
"table_id":9,
"table_name":"COMPILE_FIREWALL_PLUGIN",
"db_tables":["COMPILE_FIREWALL_DEFAULT"],
"table_type":"plugin",
"valid_column":8,
"custom": {
"gc_timeout_s":3,
"key_type":"integer",
"key_len":8,
"key":1
}
},
{
"table_id":10,
"table_name":"HTTP_REGION",
"db_tables":["HTTP_URL", "HTTP_HOST"],
"table_type":"expr",
@@ -79,7 +139,7 @@
}
},
{
"table_id":6,
"table_id":11,
"table_name":"KEYWORDS_TABLE",
"table_type":"expr",
"valid_column":7,
@@ -93,7 +153,7 @@
}
},
{
"table_id":7,
"table_id":12,
"table_name":"IP_CONFIG",
"table_type":"ip_plus",
"valid_column":11,
@@ -111,7 +171,7 @@
}
},
{
"table_id":8,
"table_id":13,
"table_name":"CONTENT_SIZE",
"table_type":"intval",
"valid_column":5,
@@ -123,18 +183,19 @@
}
},
{
"table_id":9,
"table_id":14,
"table_name":"QD_ENTRY_INFO",
"table_type":"plugin",
"valid_column":4,
"custom": {
"gc_timeout_s":3,
"key_type":"integer",
"key_len":8,
"key":1
}
},
{
"table_id":10,
"table_id":15,
"table_name":"HTTP_SIGNATURE",
"table_type":"expr_plus",
"valid_column":8,
@@ -149,7 +210,7 @@
}
},
{
"table_id":11,
"table_id":16,
"table_name":"IMAGE_FP",
"table_type":"expr",
"valid_column":7,
@@ -163,11 +224,12 @@
}
},
{
"table_id":12,
"table_id":17,
"table_name":"TEST_EFFECTIVE_RANGE_TABLE",
"table_type":"plugin",
"valid_column":4,
"custom": {
"gc_timeout_s":3,
"key_type":"integer",
"key_len":8,
"key":1,
@@ -175,11 +237,12 @@
}
},
{
"table_id":13,
"table_id":18,
"table_name":"TEST_FOREIGN_KEY",
"table_type":"plugin",
"valid_column":4,
"custom": {
"gc_timeout_s":3,
"key_type":"pointer",
"key":2,
"tag":3,
@@ -187,7 +250,7 @@
}
},
{
"table_id":14,
"table_id":19,
"table_name":"TEST_PLUGIN_EXDATA_TABLE",
"table_type":"plugin",
"valid_column":4,
@@ -199,18 +262,19 @@
}
},
{
"table_id":15,
"table_id":20,
"table_name":"IR_INTERCEPT_IP",
"table_type":"plugin",
"valid_column":14,
"custom": {
"gc_timeout_s":3,
"key_type":"pointer",
"key":2,
"tag":18
}
},
{
"table_id":16,
"table_id":21,
"table_name":"APP_PAYLOAD",
"table_type":"expr_plus",
"valid_column":8,
@@ -225,7 +289,7 @@
}
},
{
"table_id":17,
"table_id":22,
"table_name":"TROJAN_PAYLOAD",
"table_type":"expr",
"valid_column":7,
@@ -240,7 +304,7 @@
}
},
{
"table_id":18,
"table_id":23,
"table_name":"MAIL_ADDR",
"table_type":"expr",
"valid_column":7,
@@ -254,7 +318,7 @@
}
},
{
"table_id":19,
"table_id":24,
"table_name":"IP_PLUS_CONFIG",
"table_type":"ip_plus",
"valid_column":11,
@@ -272,32 +336,32 @@
}
},
{
"table_id":20,
"table_id":25,
"table_name":"HTTP_RESPONSE_KEYWORDS",
"table_type":"virtual",
"physical_table": "KEYWORDS_TABLE"
},
{
"table_id":21,
"table_id":26,
"table_name":"HTTP_REQUEST_HEADER",
"table_type":"virtual",
"physical_table": "HTTP_SIGNATURE"
},
{
"table_id":22,
"table_id":27,
"table_name":"HTTP_RESPONSE_HEADER",
"table_type":"virtual",
"physical_table": "HTTP_SIGNATURE"
},
{
"table_id":23,
"table_id":28,
"table_name":"VIRTUAL_IP_PLUS_TABLE",
"db_tables":["VIRTUAL_IP_PLUS_SOURCE", "VIRTUAL_IP_PLUS_DESTINATION"],
"table_type":"virtual",
"physical_table": "IP_PLUS_CONFIG"
},
{
"table_id":24,
"table_id":29,
"table_name":"TEST_IP_PLUGIN_WITH_EXDATA",
"table_type":"ip_plugin",
"valid_column":6,
@@ -311,7 +375,7 @@
}
},
{
"table_id":25,
"table_id":30,
"table_name":"AS_NUMBER",
"table_type":"expr",
"valid_column":7,
@@ -325,19 +389,19 @@
}
},
{
"table_id":26,
"table_id":31,
"table_name":"SOURCE_IP_ASN",
"table_type":"virtual",
"physical_table":"AS_NUMBER"
},
{
"table_id":27,
"table_id":32,
"table_name":"DESTINATION_IP_ASN",
"table_type":"virtual",
"physical_table":"AS_NUMBER"
},
{
"table_id":28,
"table_id":33,
"table_name":"GeoLocation",
"table_type":"expr",
"valid_column":7,
@@ -351,13 +415,13 @@
}
},
{
"table_id":29,
"table_id":34,
"table_name":"SOURCE_IP_GEO",
"table_type":"virtual",
"physical_table":"GeoLocation"
},
{
"table_id":30,
"table_id":35,
"table_name":"INTERGER_PLUS",
"table_type":"intval_plus",
"valid_column":6,
@@ -370,7 +434,7 @@
}
},
{
"table_id":31,
"table_id":36,
"table_name":"TEST_FQDN_PLUGIN_WITH_EXDATA",
"table_type":"fqdn_plugin",
"valid_column":5,
@@ -382,7 +446,7 @@
}
},
{
"table_id":32,
"table_id":37,
"table_name":"APP_ID",
"table_type":"intval",
"valid_column":5,
@@ -394,7 +458,7 @@
}
},
{
"table_id":33,
"table_id":38,
"table_name":"EMPTY_KEYWORD",
"table_type":"expr",
"valid_column":7,
@@ -408,7 +472,7 @@
}
},
{
"table_id":34,
"table_id":39,
"table_name":"EMPTY_INTERGER",
"table_type":"intval",
"valid_column":5,
@@ -420,7 +484,7 @@
}
},
{
"table_id":35,
"table_id":40,
"table_name":"TEST_BOOL_PLUGIN_WITH_EXDATA",
"table_type":"bool_plugin",
"valid_column":4,
@@ -431,7 +495,7 @@
}
},
{
"table_id":36,
"table_id":41,
"table_name":"FLAG_CONFIG",
"table_type":"flag",
"valid_column":5,
@@ -443,7 +507,7 @@
}
},
{
"table_id":37,
"table_id":42,
"table_name":"FLAG_PLUS_CONFIG",
"table_type":"flag_plus",
"valid_column":6,
@@ -456,11 +520,12 @@
}
},
{
"table_id":38,
"table_id":43,
"table_name":"TEST_PLUGIN_LONG_KEY_TYPE_TABLE",
"table_type":"plugin",
"valid_column":4,
"custom": {
"gc_timeout_s":3,
"key_type":"integer",
"key_len":8,
"key":2,
@@ -468,11 +533,12 @@
}
},
{
"table_id":39,
"table_id":44,
"table_name":"TEST_PLUGIN_INT_KEY_TYPE_TABLE",
"table_type":"plugin",
"valid_column":4,
"custom": {
"gc_timeout_s":3,
"key_type":"integer",
"key_len":4,
"key":2,
@@ -480,24 +546,25 @@
}
},
{
"table_id":40,
"table_id":45,
"table_name":"TEST_PLUGIN_IP_KEY_TYPE_TABLE",
"table_type":"plugin",
"valid_column":4,
"custom": {
"gc_timeout_s":3,
"key_type":"ip_addr",
"addr_type":1,
"key":2
}
},
{
"table_id":41,
"table_id":46,
"table_name":"HTTP_URL_FILTER",
"table_type":"virtual",
"physical_table": "HTTP_URL"
},
{
"table_id":42,
"table_id":47,
"table_name":"IP_PERF_CONFIG",
"table_type":"ip_plus",
"valid_column":11,
@@ -515,7 +582,7 @@
}
},
{
"table_id":43,
"table_id":48,
"table_name":"INTEGER_PERF_CONFIG",
"table_type":"intval",
"valid_column":5,
@@ -527,7 +594,7 @@
}
},
{
"table_id":44,
"table_id":49,
"table_name":"EXPR_LITERAL_PERF_CONFIG",
"table_type":"expr",
"valid_column":7,
@@ -541,7 +608,7 @@
}
},
{
"table_id":45,
"table_id":50,
"table_name":"EXPR_REGEX_PERF_CONFIG",
"table_type":"expr",
"valid_column":7,
@@ -555,7 +622,7 @@
}
},
{
"table_id":46,
"table_id":51,
"table_name":"FLAG_PERF_CONFIG",
"table_type":"flag",
"valid_column":5,