refactor ex_data_runtime & fix all leak memory

This commit is contained in:
liuwentan
2023-04-05 21:09:19 +08:00
parent 5d545d6dbf
commit fb3896c078
26 changed files with 438 additions and 693 deletions

View File

@@ -18,6 +18,7 @@ extern "C"
#include "maat.h" #include "maat.h"
#include "maat_table.h" #include "maat_table.h"
#include "maat_ex_data.h"
#include "cJSON/cJSON.h" #include "cJSON/cJSON.h"
struct bool_plugin_runtime; struct bool_plugin_runtime;
@@ -27,15 +28,13 @@ void *bool_plugin_schema_new(cJSON *json, struct table_manager *tbl_mgr,
const char *table_name, struct log_handle *logger); const char *table_name, struct log_handle *logger);
void bool_plugin_schema_free(void *bool_plugin_schema); void bool_plugin_schema_free(void *bool_plugin_schema);
/* ip plugin table ex data API */ int bool_plugin_table_set_ex_container_schema(void *bool_plugin_schema, int table_id,
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_ex_new_func_t *new_func, maat_ex_new_func_t *new_func,
maat_ex_free_func_t *free_func, maat_ex_free_func_t *free_func,
maat_ex_dup_func_t *dup_func, maat_ex_dup_func_t *dup_func,
long argl, void *argp, void (*custom_data_free)(void *),
struct log_handle *logger); long argl, void *argp);
struct ex_container_schema *bool_plugin_table_get_ex_container_schema(void *bool_plugin_schema);
/* ip plugin runtime API */ /* ip plugin runtime API */
void *bool_plugin_runtime_new(void *bool_plugin_schema, int max_thread_num, void *bool_plugin_runtime_new(void *bool_plugin_schema, int max_thread_num,
@@ -49,9 +48,8 @@ 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); struct ex_data_runtime *bool_plugin_runtime_get_ex_data_rt(void *bool_plugin_runtime);
int bool_plugin_runtime_get_ex_data(void *bool_plugin_runtime, void *bool_plugin_schema, int bool_plugin_runtime_get_ex_data(void *bool_plugin_runtime, unsigned long long *item_ids,
unsigned long long *item_ids, size_t n_item, size_t n_item, void **ex_data_array, size_t n_ex_data);
void **ex_data_array, size_t n_ex_data);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@@ -41,8 +41,7 @@ int compile_table_set_ex_data_schema(struct compile_schema *compile_schema, int
maat_ex_new_func_t *new_func, maat_ex_new_func_t *new_func,
maat_ex_free_func_t *free_func, maat_ex_free_func_t *free_func,
maat_ex_dup_func_t *dup_func, maat_ex_dup_func_t *dup_func,
long argl, void *argp, long argl, void *argp);
struct log_handle *logger);
/* compile runtime API */ /* compile runtime API */
void *compile_runtime_new(void *compile_schema, int max_thread_num, void *compile_runtime_new(void *compile_schema, int max_thread_num,

View File

@@ -20,11 +20,6 @@ extern "C"
#include "rcu_hash.h" #include "rcu_hash.h"
#include "maat_garbage_collection.h" #include "maat_garbage_collection.h"
struct ex_container {
void *ex_data;
void *custom_data;
};
struct ex_data_schema { struct ex_data_schema {
maat_ex_new_func_t *new_func; maat_ex_new_func_t *new_func;
maat_ex_free_func_t *free_func; maat_ex_free_func_t *free_func;
@@ -33,18 +28,22 @@ struct ex_data_schema {
void *argp; void *argp;
}; };
struct ex_container {
void *ex_data;
void *custom_data;
};
struct ex_container_schema { struct ex_container_schema {
int table_id; int table_id;
struct ex_data_schema *ex_schema; int set_flag;
void (*user_data_free)(void *); struct ex_data_schema ex_schema;
void (*custom_data_free)(void *);
}; };
struct ex_data_runtime; struct ex_data_runtime;
/* ex_data_runtime API */ /* ex_data_runtime API */
struct ex_data_runtime * struct ex_data_runtime *ex_data_runtime_new(int table_id, struct log_handle *logger);
ex_data_runtime_new(int table_id, rcu_hash_data_free_fn *data_free_fn,
struct log_handle *logger);
void ex_data_runtime_free(struct ex_data_runtime *ex_data_rt); void ex_data_runtime_free(struct ex_data_runtime *ex_data_rt);
void ex_data_runtime_commit(struct ex_data_runtime *ex_data_rt); void ex_data_runtime_commit(struct ex_data_runtime *ex_data_rt);
@@ -57,26 +56,14 @@ size_t ex_data_runtime_cached_row_count(struct ex_data_runtime *ex_data_rt);
void ex_data_runtime_clear_row_cache(struct ex_data_runtime *ex_data_rt); void ex_data_runtime_clear_row_cache(struct ex_data_runtime *ex_data_rt);
/* set schema API */
struct ex_data_schema *ex_data_schema_new(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 ex_data_schema_free(struct ex_data_schema *ex_schema);
/* set user_ctx API */ /* set user_ctx API */
void ex_data_runtime_set_ex_container_schema(struct ex_data_runtime *ex_data_rt, void ex_data_runtime_set_ex_container_schema(struct ex_data_runtime *ex_data_rt,
struct ex_container_schema *container_schema); struct ex_container_schema *container_schema);
struct ex_container_schema *
ex_data_runtime_get_ex_container_schema(struct ex_data_runtime *ex_data_rt);
struct ex_container *ex_container_new(void *ex_data, void *custom_data); struct ex_container *ex_container_new(void *ex_data, void *custom_data);
void ex_container_free(void *ctx, void *data);
/* ex_data_runtime ex data API */ /* ex_data_runtime ex data API */
void *ex_data_runtime_row2ex_data(struct ex_data_runtime *ex_data_rt, void *ex_data_runtime_row2ex_data(struct ex_data_runtime *ex_data_rt,
struct ex_data_schema *ex_schema,
const char *table_name, const char *row, const char *table_name, const char *row,
const char *key, size_t key_len); const char *key, size_t key_len);
@@ -95,11 +82,9 @@ size_t ex_data_runtime_ex_container_count(struct ex_data_runtime *ex_data_rt);
int ex_data_runtime_is_updating(struct ex_data_runtime *ex_data_rt); int ex_data_runtime_is_updating(struct ex_data_runtime *ex_data_rt);
void *ex_data_runtime_get_ex_data_by_key(struct ex_data_runtime *ex_data_rt, void *ex_data_runtime_get_ex_data_by_key(struct ex_data_runtime *ex_data_rt,
struct ex_data_schema *ex_schema,
const char *key, size_t key_len); const char *key, size_t key_len);
void *ex_data_runtime_get_ex_data_by_container(struct ex_data_runtime *ex_data_rt, void *ex_data_runtime_get_ex_data_by_container(struct ex_data_runtime *ex_data_rt,
struct ex_data_schema *ex_schema,
struct ex_container *ex_container); struct ex_container *ex_container);
void *ex_data_runtime_get_custom_data(struct ex_data_runtime *ex_data_rt, void *ex_data_runtime_get_custom_data(struct ex_data_runtime *ex_data_rt,

View File

@@ -18,7 +18,9 @@ extern "C"
#include "maat.h" #include "maat.h"
#include "maat_table.h" #include "maat_table.h"
#include "maat_ex_data.h"
#include "cJSON/cJSON.h" #include "cJSON/cJSON.h"
#include "fqdn_engine.h"
struct fqdn_plugin_runtime; struct fqdn_plugin_runtime;
@@ -28,14 +30,13 @@ void *fqdn_plugin_schema_new(cJSON *json, struct table_manager *tbl_mgr,
void fqdn_plugin_schema_free(void *fqdn_plugin_schema); void fqdn_plugin_schema_free(void *fqdn_plugin_schema);
/* fqdn plugin table ex data API */ /* 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_container_schema(void *fqdn_plugin_schema, int table_id,
int fqdn_plugin_table_set_ex_data_schema(void *fqdn_plugin_schema,
maat_ex_new_func_t *new_func, maat_ex_new_func_t *new_func,
maat_ex_free_func_t *free_func, maat_ex_free_func_t *free_func,
maat_ex_dup_func_t *dup_func, maat_ex_dup_func_t *dup_func,
long argl, void *argp, void (*custom_data_free)(void *),
struct log_handle *logger); long argl, void *argp);
struct ex_container_schema *fqdn_plugin_table_get_ex_container_schema(void *fqdn_plugin_schema);
/* fqdn plugin runtime API */ /* fqdn plugin runtime API */
void *fqdn_plugin_runtime_new(void *fqdn_plugin_schema, int max_thread_num, void *fqdn_plugin_runtime_new(void *fqdn_plugin_schema, int max_thread_num,
@@ -49,8 +50,11 @@ 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); struct ex_data_runtime *fqdn_plugin_runtime_get_ex_data_rt(void *fqdn_plugin_runtime);
int fqdn_plugin_runtime_get_ex_data(void *fqdn_plugin_runtime, void *fqdn_plugin_schema, int fqdn_plugin_runtime_get_ex_data(void *fqdn_plugin_runtime, const char *fqdn,
const char *fqdn, void **ex_data_array, size_t n_ex_data); void **ex_data_array, size_t n_ex_data);
void fqdn_rule_free(struct FQDN_rule *fqdn_rule);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@@ -19,6 +19,7 @@ extern "C"
#include "maat.h" #include "maat.h"
#include "cJSON/cJSON.h" #include "cJSON/cJSON.h"
#include "maat_table.h" #include "maat_table.h"
#include "maat_ex_data.h"
struct ip_plugin_runtime; struct ip_plugin_runtime;
@@ -28,14 +29,13 @@ void *ip_plugin_schema_new(cJSON *json, struct table_manager *tbl_mgr,
void ip_plugin_schema_free(void *ip_plugin_schema); void ip_plugin_schema_free(void *ip_plugin_schema);
/* ip plugin table ex data API */ /* ip plugin table ex data API */
struct ex_data_schema *ip_plugin_table_get_ex_data_schema(void *ip_plugin_schema); int ip_plugin_table_set_ex_container_schema(void *ip_plugin_schema, int table_id,
void ip_plugin_table_set_ex_data_schema(void *ip_plugin_schema,
maat_ex_new_func_t *new_func, maat_ex_new_func_t *new_func,
maat_ex_free_func_t *free_func, maat_ex_free_func_t *free_func,
maat_ex_dup_func_t *dup_func, maat_ex_dup_func_t *dup_func,
long argl, void *argp, void (*custom_data_free)(void *),
struct log_handle *logger); long argl, void *argp);
struct ex_container_schema *ip_plugin_table_get_ex_container_schema(void *ip_plugin_schema);
/* ip plugin runtime API */ /* ip plugin runtime API */
void *ip_plugin_runtime_new(void *ip_plugin_schema, int max_thread_num, void *ip_plugin_runtime_new(void *ip_plugin_schema, int max_thread_num,
@@ -49,9 +49,8 @@ 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); struct ex_data_runtime *ip_plugin_runtime_get_ex_data_rt(void *ip_plugin_runtime);
int ip_plugin_runtime_get_ex_data(void *ip_plugin_runtime, void *ip_plugin_schema, int ip_plugin_runtime_get_ex_data(void *ip_plugin_runtime, const struct ip_addr *ip_addr,
const struct ip_addr *ip_addr, void **ex_data_array, void **ex_data_array, size_t n_ex_data_array);
size_t n_ex_data_array);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@@ -18,6 +18,7 @@ extern "C"
#include "cJSON/cJSON.h" #include "cJSON/cJSON.h"
#include "maat.h" #include "maat.h"
#include "maat_ex_data.h"
#define MAX_FOREIGN_CLMN_NUM 8 #define MAX_FOREIGN_CLMN_NUM 8
@@ -40,13 +41,13 @@ void plugin_table_all_callback_finish(struct plugin_schema *plugin_schema);
int plugin_table_get_foreign_column(struct plugin_schema *plugin_schema, int *foreign_columns); int plugin_table_get_foreign_column(struct plugin_schema *plugin_schema, int *foreign_columns);
/* plugin table ex data API */ /* plugin table ex data API */
void plugin_table_set_ex_data_schema(void *plugin_schema, int plugin_table_set_ex_container_schema(void *plugin_schema, int table_id,
maat_ex_new_func_t *new_func, maat_ex_new_func_t *new_func,
maat_ex_free_func_t *free_func, maat_ex_free_func_t *free_func,
maat_ex_dup_func_t *dup_func, maat_ex_dup_func_t *dup_func,
long argl, void *argp, void (*custom_data_free)(void *),
struct log_handle *logger); long argl, void *argp);
struct ex_data_schema *plugin_table_get_ex_data_schema(void *custom_schema); struct ex_container_schema *plugin_table_get_ex_container_schema(void *plugin_schema);
/* plugin runtime API */ /* plugin runtime API */
void *plugin_runtime_new(void *plugin_schema, int max_thread_num, void *plugin_runtime_new(void *plugin_schema, int max_thread_num,

View File

@@ -18,19 +18,15 @@ extern "C"
#include "uthash/uthash.h" #include "uthash/uthash.h"
typedef void rcu_hash_data_free_fn(void *user_ctx, void *data); typedef void data_free_fn(void *user_ctx, void *data);
/* rcu hash table */ /* rcu hash table */
struct rcu_hash_table; struct rcu_hash_table;
struct rcu_hash_table *rcu_hash_new(rcu_hash_data_free_fn *free_fn); struct rcu_hash_table *rcu_hash_new(data_free_fn *free_fn, void *arg);
void rcu_hash_free(struct rcu_hash_table *htable); void rcu_hash_free(struct rcu_hash_table *htable);
void rcu_hash_set_user_ctx(struct rcu_hash_table *htable, void *user_ctx);
void *rcu_hash_get_user_ctx(struct rcu_hash_table *htable);
/** /**
* @brief Adding the updating nodes which will become effective nodes after call rcu_hash_commit * @brief Adding the updating nodes which will become effective nodes after call rcu_hash_commit
* *

View File

@@ -533,9 +533,9 @@ int compile_table_ex_schema_register(struct maat *maat_instance, int table_id,
void *schema = table_manager_get_schema(maat_instance->tbl_mgr, table_id); void *schema = table_manager_get_schema(maat_instance->tbl_mgr, table_id);
assert(schema != NULL); assert(schema != NULL);
int ret = compile_table_set_ex_data_schema((struct compile_schema *)schema, int ret = compile_table_set_ex_data_schema((struct compile_schema *)schema, table_id,
table_id, new_func, free_func, dup_func, new_func, free_func, dup_func,
argl, argp, maat_instance->logger); argl, argp);
if (ret < 0) { if (ret < 0) {
return -1; return -1;
} }
@@ -559,57 +559,29 @@ int generic_plugin_table_set_ex_schema(struct table_manager *tbl_mgr, int table_
void *schema = table_manager_get_schema(tbl_mgr, table_id); void *schema = table_manager_get_schema(tbl_mgr, table_id);
if (NULL == schema) { if (NULL == schema) {
log_error(logger, MODULE_MAAT_API, log_error(logger, MODULE_MAAT_API,
"[%s:%d], table(table_id:%d) is not registered, can't register ex schema", "[%s:%d], table(table_id:%d) is not registered, can't register ex_container_schema",
__FUNCTION__, __LINE__, table_id); __FUNCTION__, __LINE__, table_id);
return -1; return -1;
} }
struct ex_data_schema *ex_schema = NULL; int ret = -1;
enum table_type table_type = table_manager_get_table_type(tbl_mgr, table_id); enum table_type table_type = table_manager_get_table_type(tbl_mgr, table_id);
switch (table_type) { switch (table_type) {
case TABLE_TYPE_PLUGIN: case TABLE_TYPE_PLUGIN:
ex_schema = plugin_table_get_ex_data_schema(schema); ret = plugin_table_set_ex_container_schema(schema, table_id, new_func, free_func, dup_func,
if (ex_schema != NULL) { free, argl, argp);
log_error(logger, MODULE_MAAT_API,
"[%s:%d], EX data schema already registed for plugin table(table_id:%d)",
__FUNCTION__, __LINE__, table_id);
return -1;
}
plugin_table_set_ex_data_schema(schema, new_func, free_func,
dup_func, argl, argp, logger);
break; break;
case TABLE_TYPE_IP_PLUGIN: case TABLE_TYPE_IP_PLUGIN:
ex_schema = ip_plugin_table_get_ex_data_schema(schema); ret = ip_plugin_table_set_ex_container_schema(schema, table_id, new_func, free_func, dup_func,
if (ex_schema != NULL) { free, argl, argp);
log_error(logger, MODULE_MAAT_API,
"[%s:%d], EX data schema already registed for ip_plugin table(table_id:%d)",
__FUNCTION__, __LINE__, table_id);
return -1;
}
ip_plugin_table_set_ex_data_schema(schema, new_func, free_func,
dup_func, argl, argp, logger);
break; break;
case TABLE_TYPE_FQDN_PLUGIN: case TABLE_TYPE_FQDN_PLUGIN:
ex_schema = fqdn_plugin_table_get_ex_data_schema(schema); ret = fqdn_plugin_table_set_ex_container_schema(schema, table_id, new_func, free_func, dup_func,
if (ex_schema != NULL) { (void (*)(void *))fqdn_rule_free, argl, argp);
log_error(logger, MODULE_MAAT_API,
"[%s:%d], EX data schema already registed for fqdn_plugin table(table_id:%d)",
__FUNCTION__, __LINE__, table_id);
return -1;
}
fqdn_plugin_table_set_ex_data_schema(schema, new_func, free_func,
dup_func, argl, argp, logger);
break; break;
case TABLE_TYPE_BOOL_PLUGIN: case TABLE_TYPE_BOOL_PLUGIN:
ex_schema = bool_plugin_table_get_ex_data_schema(schema); ret = bool_plugin_table_set_ex_container_schema(schema, table_id, new_func, free_func, dup_func,
if (ex_schema != NULL) { free, argl, argp);
log_error(logger, MODULE_MAAT_API,
"[%s:%d], EX data schema already registed for bool_plugin table(table_id:%d)",
__FUNCTION__, __LINE__, table_id);
return -1;
}
bool_plugin_table_set_ex_data_schema(schema, new_func, free_func,
dup_func, argl, argp, logger);
break; break;
default: default:
log_error(logger, MODULE_MAAT_API, log_error(logger, MODULE_MAAT_API,
@@ -618,44 +590,44 @@ int generic_plugin_table_set_ex_schema(struct table_manager *tbl_mgr, int table_
return -1; return -1;
} }
return 0; return ret;
} }
void generic_plugin_runtime_commit_ex_schema(void *runtime, void *schema, const char *table_name, int generic_plugin_runtime_commit_ex_schema(void *runtime, void *schema, const char *table_name,
int table_id, enum table_type table_type, int valid_column) int table_id, enum table_type table_type, int valid_column)
{ {
if (NULL == runtime || NULL == schema || valid_column < 0) { if (NULL == runtime || NULL == schema || valid_column < 0) {
return; return -1;
} }
struct ex_data_schema *ex_schema = NULL; struct ex_container_schema *container_schema = NULL;
struct ex_data_runtime *ex_data_rt = NULL; struct ex_data_runtime *ex_data_rt = NULL;
switch (table_type) { switch (table_type) {
case TABLE_TYPE_PLUGIN: case TABLE_TYPE_PLUGIN:
ex_schema = plugin_table_get_ex_data_schema(schema); container_schema = plugin_table_get_ex_container_schema(schema);
ex_data_rt = plugin_runtime_get_ex_data_rt(runtime); ex_data_rt = plugin_runtime_get_ex_data_rt(runtime);
ex_data_runtime_set_ex_container_schema(ex_data_rt, container_schema);
break; break;
case TABLE_TYPE_IP_PLUGIN: case TABLE_TYPE_IP_PLUGIN:
ex_schema = ip_plugin_table_get_ex_data_schema(schema); container_schema = ip_plugin_table_get_ex_container_schema(schema);
ex_data_rt = ip_plugin_runtime_get_ex_data_rt(runtime); ex_data_rt = ip_plugin_runtime_get_ex_data_rt(runtime);
ex_data_runtime_set_ex_container_schema(ex_data_rt, container_schema);
break; break;
case TABLE_TYPE_FQDN_PLUGIN: case TABLE_TYPE_FQDN_PLUGIN:
ex_schema = fqdn_plugin_table_get_ex_data_schema(schema); container_schema = fqdn_plugin_table_get_ex_container_schema(schema);
ex_data_rt = fqdn_plugin_runtime_get_ex_data_rt(runtime); ex_data_rt = fqdn_plugin_runtime_get_ex_data_rt(runtime);
ex_data_runtime_set_ex_container_schema(ex_data_rt, container_schema);
break; break;
case TABLE_TYPE_BOOL_PLUGIN: case TABLE_TYPE_BOOL_PLUGIN:
ex_schema = bool_plugin_table_get_ex_data_schema(schema); container_schema = bool_plugin_table_get_ex_container_schema(schema);
ex_data_rt = bool_plugin_runtime_get_ex_data_rt(runtime); ex_data_rt = bool_plugin_runtime_get_ex_data_rt(runtime);
ex_data_runtime_set_ex_container_schema(ex_data_rt, container_schema);
break; break;
default: default:
break; break;
} }
struct ex_container_schema *container_schema = ALLOC(struct ex_container_schema, 1);
container_schema->table_id = table_id;
container_schema->ex_schema = ex_schema;
ex_data_runtime_set_ex_container_schema(ex_data_rt, container_schema);
size_t n_cached_row = ex_data_runtime_cached_row_count(ex_data_rt); size_t n_cached_row = ex_data_runtime_cached_row_count(ex_data_rt);
for (size_t i = 0; i < n_cached_row; i++) { for (size_t i = 0; i < n_cached_row; i++) {
@@ -695,6 +667,8 @@ void generic_plugin_runtime_commit_ex_schema(void *runtime, void *schema, const
default: default:
break; break;
} }
return 0;
} }
int generic_plugin_table_ex_schema_register(struct maat *maat_instance, int generic_plugin_table_ex_schema_register(struct maat *maat_instance,
@@ -724,11 +698,11 @@ int generic_plugin_table_ex_schema_register(struct maat *maat_instance,
return -1; return -1;
} }
generic_plugin_runtime_commit_ex_schema(runtime, schema, table_name, table_id, ret = generic_plugin_runtime_commit_ex_schema(runtime, schema, table_name, table_id,
table_type, valid_column); table_type, valid_column);
} }
return 0; return ret;
} }
int maat_plugin_table_ex_schema_register(struct maat *maat_instance, int maat_plugin_table_ex_schema_register(struct maat *maat_instance,
@@ -847,15 +821,7 @@ int maat_ip_plugin_table_get_ex_data(struct maat *maat_instance, int table_id,
return -1; return -1;
} }
void *ip_plugin_schema = table_manager_get_schema(maat_rt->ref_tbl_mgr, table_id); int n_hit_ex_data = ip_plugin_runtime_get_ex_data(ip_plugin_rt, ip_addr,
if (NULL == ip_plugin_schema) {
log_error(maat_instance->logger, MODULE_MAAT_API,
"[%s:%d] table(table_id:%d) schema is NULL",
__FUNCTION__, __LINE__, table_id);
return -1;
}
int n_hit_ex_data = ip_plugin_runtime_get_ex_data(ip_plugin_rt, ip_plugin_schema, ip_addr,
ex_data_array, n_ex_data); ex_data_array, n_ex_data);
if (n_hit_ex_data < 0) { if (n_hit_ex_data < 0) {
log_error(maat_instance->logger, MODULE_MAAT_API, log_error(maat_instance->logger, MODULE_MAAT_API,
@@ -895,16 +861,8 @@ int maat_fqdn_plugin_table_get_ex_data(struct maat *maat_instance, int table_id,
return -1; return -1;
} }
void *fqdn_plugin_schema = table_manager_get_schema(maat_rt->ref_tbl_mgr, table_id); int n_hit_ex_data = fqdn_plugin_runtime_get_ex_data(fqdn_plugin_rt, fqdn,
if (NULL == fqdn_plugin_schema) { ex_data_array, n_ex_data);
log_error(maat_instance->logger, MODULE_MAAT_API,
"[%s:%d] table(table_id:%d) schema is NULL",
__FUNCTION__, __LINE__, table_id);
return -1;
}
int n_hit_ex_data = fqdn_plugin_runtime_get_ex_data(fqdn_plugin_rt, fqdn_plugin_schema,
fqdn, ex_data_array, n_ex_data);
if (n_hit_ex_data < 0) { if (n_hit_ex_data < 0) {
log_error(maat_instance->logger, MODULE_MAAT_API, log_error(maat_instance->logger, MODULE_MAAT_API,
"[%s:%d] fqdn_plugin table(table_id:%d) get ex_data error.", "[%s:%d] fqdn_plugin table(table_id:%d) get ex_data error.",
@@ -943,17 +901,8 @@ int maat_bool_plugin_table_get_ex_data(struct maat *maat_instance, int table_id,
return -1; return -1;
} }
void *bool_plugin_schema = table_manager_get_schema(maat_rt->ref_tbl_mgr, table_id); int n_hit_ex_data = bool_plugin_runtime_get_ex_data(bool_plugin_rt, item_ids, n_item,
if (NULL == bool_plugin_schema) { ex_data_array, n_ex_data);
log_error(maat_instance->logger, MODULE_MAAT_API,
"[%s:%d] table(table_id:%d) schema is NULL",
__FUNCTION__, __LINE__, table_id);
return -1;
}
int n_hit_ex_data = bool_plugin_runtime_get_ex_data(bool_plugin_rt, bool_plugin_schema,
item_ids, n_item, ex_data_array,
n_ex_data);
if (n_hit_ex_data < 0) { if (n_hit_ex_data < 0) {
log_error(maat_instance->logger, MODULE_MAAT_API, log_error(maat_instance->logger, MODULE_MAAT_API,
"[%s:%d] bool_plugin table(table_id:%d) get ex_data error.", "[%s:%d] bool_plugin table(table_id:%d) get ex_data error.",

View File

@@ -12,7 +12,6 @@
#include "maat_bool_plugin.h" #include "maat_bool_plugin.h"
#include "bool_matcher.h" #include "bool_matcher.h"
#include "maat_ex_data.h"
#include "maat_utils.h" #include "maat_utils.h"
#include "maat_rule.h" #include "maat_rule.h"
#include "maat_garbage_collection.h" #include "maat_garbage_collection.h"
@@ -23,9 +22,10 @@ struct bool_plugin_schema {
int item_id_column; int item_id_column;
int bool_expr_column; int bool_expr_column;
int rule_tag_column; int rule_tag_column;
struct ex_data_schema *ex_schema; struct ex_container_schema container_schema;
int table_id; int table_id;
struct table_manager *ref_tbl_mgr; struct table_manager *ref_tbl_mgr;
struct log_handle *logger;
unsigned long long update_err_cnt; unsigned long long update_err_cnt;
unsigned long long unmatch_tag_cnt; unsigned long long unmatch_tag_cnt;
@@ -46,6 +46,7 @@ void *bool_plugin_schema_new(cJSON *json, struct table_manager *tbl_mgr,
const char *table_name, struct log_handle *logger) const char *table_name, struct log_handle *logger)
{ {
struct bool_plugin_schema *schema = ALLOC(struct bool_plugin_schema, 1); struct bool_plugin_schema *schema = ALLOC(struct bool_plugin_schema, 1);
schema->logger = logger;
cJSON *custom_item = NULL; cJSON *custom_item = NULL;
cJSON *item = cJSON_GetObjectItem(json, "table_id"); cJSON *item = cJSON_GetObjectItem(json, "table_id");
@@ -104,52 +105,45 @@ void bool_plugin_schema_free(void *bool_plugin_schema)
if (NULL == bool_plugin_schema) { if (NULL == bool_plugin_schema) {
return; 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); FREE(bool_plugin_schema);
} }
/* ip plugin table ex data API */ int bool_plugin_table_set_ex_container_schema(void *bool_plugin_schema, int table_id,
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_ex_new_func_t *new_func, maat_ex_new_func_t *new_func,
maat_ex_free_func_t *free_func, maat_ex_free_func_t *free_func,
maat_ex_dup_func_t *dup_func, maat_ex_dup_func_t *dup_func,
long argl, void *argp, void (*custom_data_free)(void *),
struct log_handle *logger) long argl, void *argp)
{ {
if (NULL == bool_plugin_schema) {
return -1;
}
struct bool_plugin_schema *schema = (struct bool_plugin_schema *)bool_plugin_schema; struct bool_plugin_schema *schema = (struct bool_plugin_schema *)bool_plugin_schema;
if (schema->ex_schema != NULL) {
assert(0); if (1 == schema->container_schema.set_flag) {
log_error(logger, MODULE_BOOL_PLUGIN, log_error(schema->logger, MODULE_BOOL_PLUGIN,
"[%s:%d], ex_data schema has already been registered, can't be registered again", "[%s:%d] bool_plugin table(table_id:%d) ex_container_schema has been set, can't set again",
__FUNCTION__, __LINE__); __FUNCTION__, __LINE__, table_id);
return -1; return -1;
} }
schema->ex_schema = ex_data_schema_new(new_func, free_func, dup_func, argl, argp); schema->container_schema.table_id = table_id;
schema->container_schema.custom_data_free = custom_data_free;
schema->container_schema.ex_schema.new_func = new_func;
schema->container_schema.ex_schema.free_func = free_func;
schema->container_schema.ex_schema.dup_func = dup_func;
schema->container_schema.ex_schema.argl = argl;
schema->container_schema.ex_schema.argp = argp;
schema->container_schema.set_flag = 1;
return 0; return 0;
} }
struct ex_container_schema *bool_plugin_table_get_ex_container_schema(void *bool_plugin_schema)
{
struct bool_plugin_schema *schema = (struct bool_plugin_schema *)bool_plugin_schema;
return &(schema->container_schema);
}
static int cmp_ull_p(const void *p1, const void *p2) static int cmp_ull_p(const void *p1, const void *p2)
{ {
if(* (unsigned long long*) p1 > * (unsigned long long*) p2) { if(* (unsigned long long*) p1 > * (unsigned long long*) p2) {
@@ -187,9 +181,11 @@ void *bool_plugin_runtime_new(void *bool_plugin_schema, int max_thread_num,
struct bool_plugin_schema *schema = (struct bool_plugin_schema *)bool_plugin_schema; struct bool_plugin_schema *schema = (struct bool_plugin_schema *)bool_plugin_schema;
struct bool_plugin_runtime *bool_plugin_rt = ALLOC(struct bool_plugin_runtime, 1); struct bool_plugin_runtime *bool_plugin_rt = ALLOC(struct bool_plugin_runtime, 1);
bool_plugin_rt->ex_data_rt = ex_data_runtime_new(schema->table_id, bool_plugin_rt->ex_data_rt = ex_data_runtime_new(schema->table_id, logger);
ex_container_free, if (1 == schema->container_schema.set_flag) {
logger); ex_data_runtime_set_ex_container_schema(bool_plugin_rt->ex_data_rt, &(schema->container_schema));
}
bool_plugin_rt->ref_garbage_bin = garbage_bin; bool_plugin_rt->ref_garbage_bin = garbage_bin;
bool_plugin_rt->logger = logger; bool_plugin_rt->logger = logger;
@@ -217,8 +213,8 @@ void bool_plugin_runtime_free(void *bool_plugin_runtime)
} }
int bool_plugin_runtime_update_row(struct bool_plugin_runtime *bool_plugin_rt, int bool_plugin_runtime_update_row(struct bool_plugin_runtime *bool_plugin_rt,
struct ex_data_schema *ex_schema, const char *table_name, const char *table_name, const char *row,
const char *row, const char *key, size_t key_len, const char *key, size_t key_len,
struct bool_expr *expr, int is_valid) struct bool_expr *expr, int is_valid)
{ {
int ret = -1; int ret = -1;
@@ -232,7 +228,7 @@ int bool_plugin_runtime_update_row(struct bool_plugin_runtime *bool_plugin_rt,
} }
} else { } else {
// add // add
void *ex_data = ex_data_runtime_row2ex_data(ex_data_rt, ex_schema, table_name, row, key, key_len); void *ex_data = ex_data_runtime_row2ex_data(ex_data_rt, table_name, row, key, key_len);
struct ex_container *ex_container = ex_container_new(ex_data, (void *)expr); struct ex_container *ex_container = ex_container_new(ex_data, (void *)expr);
ret = ex_data_runtime_add_ex_container(ex_data_rt, key, key_len, ex_container); ret = ex_data_runtime_add_ex_container(ex_data_rt, key, key_len, ex_container);
if (ret < 0) { if (ret < 0) {
@@ -377,8 +373,7 @@ int bool_plugin_runtime_update(void *bool_plugin_runtime, void *bool_plugin_sche
return -1; return -1;
} }
struct ex_data_schema *ex_schema = schema->ex_schema; if (1 == schema->container_schema.set_flag) {
if (ex_schema != NULL) {
if (1 == is_valid) { if (1 == is_valid) {
// add // add
bool_expr = bool_plugin_expr_new(line, schema, table_name, bool_plugin_rt->logger); bool_expr = bool_plugin_expr_new(line, schema, table_name, bool_plugin_rt->logger);
@@ -389,7 +384,7 @@ int bool_plugin_runtime_update(void *bool_plugin_runtime, void *bool_plugin_sche
const char *key = line + item_id_offset; const char *key = line + item_id_offset;
size_t key_len = item_id_len; size_t key_len = item_id_len;
ret = bool_plugin_runtime_update_row(bool_plugin_rt, ex_schema, table_name, line, key, key_len, ret = bool_plugin_runtime_update_row(bool_plugin_rt, table_name, line, key, key_len,
bool_expr, is_valid); bool_expr, is_valid);
if (ret < 0) { if (ret < 0) {
if (bool_expr != NULL) { if (bool_expr != NULL) {
@@ -493,17 +488,11 @@ struct ex_data_runtime *bool_plugin_runtime_get_ex_data_rt(void *bool_plugin_run
return bool_plugin_rt->ex_data_rt; return bool_plugin_rt->ex_data_rt;
} }
int bool_plugin_runtime_get_ex_data(void *bool_plugin_runtime, void *bool_plugin_schema, int bool_plugin_runtime_get_ex_data(void *bool_plugin_runtime, unsigned long long *item_ids,
unsigned long long *item_ids, size_t n_item, size_t n_item, void **ex_data_array, size_t n_ex_data)
void **ex_data_array, size_t n_ex_data)
{ {
if (NULL == bool_plugin_runtime || NULL == bool_plugin_schema) { if (NULL == bool_plugin_runtime || NULL == ex_data_array ||
return -1; NULL == item_ids || 0 == n_item || 0 == n_ex_data) {
}
struct bool_plugin_schema *schema = (struct bool_plugin_schema *)bool_plugin_schema;
struct ex_data_schema *ex_schema = schema->ex_schema;
if (NULL == ex_schema) {
return -1; return -1;
} }
@@ -519,7 +508,7 @@ int bool_plugin_runtime_get_ex_data(void *bool_plugin_runtime, void *bool_plugin
assert(bool_plugin_rt->matcher != NULL); assert(bool_plugin_rt->matcher != NULL);
int n_result = bool_matcher_match(bool_plugin_rt->matcher, item_ids, n_item, results, n_ex_data); int n_result = bool_matcher_match(bool_plugin_rt->matcher, item_ids, n_item, results, n_ex_data);
for (int i = 0; i < n_result; i++) { for (int i = 0; i < n_result; i++) {
ex_data_array[i] = ex_data_runtime_get_ex_data_by_container(bool_plugin_rt->ex_data_rt, ex_schema, ex_data_array[i] = ex_data_runtime_get_ex_data_by_container(bool_plugin_rt->ex_data_rt,
(struct ex_container *)results[i].user_tag); (struct ex_container *)results[i].user_tag);
} }

View File

@@ -31,10 +31,12 @@ struct compile_schema {
int compile_id_column; int compile_id_column;
int rule_tag_column; int rule_tag_column;
int declared_clause_num_column; int declared_clause_num_column;
struct ex_data_schema *ex_schema; struct ex_data_schema ex_schema;
int set_flag;
int table_id; //ugly int table_id; //ugly
char table_name[NAME_MAX]; char table_name[NAME_MAX];
struct table_manager *ref_tbl_mgr; struct table_manager *ref_tbl_mgr;
struct log_handle *logger;
unsigned long long update_err_cnt; unsigned long long update_err_cnt;
unsigned long long unmatch_tag_cnt; unsigned long long unmatch_tag_cnt;
@@ -71,8 +73,8 @@ struct compile_runtime {
struct maat_compile *compile_hash; // <compile_id, struct maat_compile> struct maat_compile *compile_hash; // <compile_id, struct maat_compile>
struct maat_runtime *ref_maat_rt; struct maat_runtime *ref_maat_rt;
time_t version; time_t version;
uint32_t rule_num;
struct maat_clause *clause_by_literals_hash; struct maat_clause *clause_by_literals_hash;
uint32_t rule_num;
int updating_flag; int updating_flag;
pthread_rwlock_t rwlock; /* TODO: replaced with mutex? */ pthread_rwlock_t rwlock; /* TODO: replaced with mutex? */
@@ -86,7 +88,7 @@ struct group2compile_runtime {
struct compile_runtime *ref_compile_rt; struct compile_runtime *ref_compile_rt;
struct group2group_runtime *ref_g2g_rt; struct group2group_runtime *ref_g2g_rt;
}; };
#pragma pack(4)
struct maat_clause_state { struct maat_clause_state {
long long clause_id; long long clause_id;
char not_flag; char not_flag;
@@ -98,6 +100,7 @@ struct maat_literal_id {
long long group_id; long long group_id;
int vtable_id; int vtable_id;
}; };
#pragma pack()
struct maat_clause { struct maat_clause {
long long clause_id; long long clause_id;
@@ -148,27 +151,25 @@ struct maat_compile_state {
UT_array *this_scan_hit_clauses; UT_array *this_scan_hit_clauses;
}; };
int compile_table_set_ex_data_schema(struct compile_schema *compile_schema, int compile_table_set_ex_data_schema(struct compile_schema *compile_schema, int table_id,
int table_id,
maat_ex_new_func_t *new_func, maat_ex_new_func_t *new_func,
maat_ex_free_func_t *free_func, maat_ex_free_func_t *free_func,
maat_ex_dup_func_t *dup_func, maat_ex_dup_func_t *dup_func,
long argl, void *argp, long argl, void *argp)
struct log_handle *logger)
{ {
if (compile_schema->ex_schema != NULL) { if (1 == compile_schema->set_flag) {
log_error(logger, MODULE_COMPILE, log_error(compile_schema->logger, MODULE_COMPILE,
"[%s:%d] compile table(table_id:%d)ex schema has been set already, can't set anymore", "[%s:%d] compile table(table_id:%d)ex schema has been set already, can't set again",
__FUNCTION__, __LINE__, table_id); __FUNCTION__, __LINE__, table_id);
return -1; return -1;
} }
compile_schema->ex_schema = ALLOC(struct ex_data_schema, 1); compile_schema->ex_schema.new_func = new_func;
compile_schema->ex_schema->argl = argl; compile_schema->ex_schema.free_func = free_func;
compile_schema->ex_schema->argp = argp; compile_schema->ex_schema.dup_func = dup_func;
compile_schema->ex_schema->new_func = new_func; compile_schema->ex_schema.argl = argl;
compile_schema->ex_schema->free_func = free_func; compile_schema->ex_schema.argp = argp;
compile_schema->ex_schema->dup_func = dup_func; compile_schema->set_flag = 1;
return 0; return 0;
} }
@@ -192,13 +193,12 @@ void *compile_runtime_get_user_data(struct compile_runtime *compile_rt, long lon
} }
void *rule_ex_data_new(const char *table_name, int table_id, const char *table_line, void *rule_ex_data_new(const char *table_name, int table_id, const char *table_line,
const struct ex_data_schema *ex_schema) struct ex_data_schema *ex_schema)
{ {
void *ex_data = NULL; void *ex_data = NULL;
ex_schema->new_func(table_name, table_id, NULL, table_line, &ex_data, ex_schema->new_func(table_name, table_id, NULL, table_line, &ex_data,
ex_schema->argl, ex_schema->argp); ex_schema->argl, ex_schema->argp);
return ex_data; return ex_data;
} }
@@ -245,6 +245,7 @@ void *compile_schema_new(cJSON *json, struct table_manager *tbl_mgr,
struct log_handle *logger) struct log_handle *logger)
{ {
struct compile_schema *compile_schema = ALLOC(struct compile_schema, 1); struct compile_schema *compile_schema = ALLOC(struct compile_schema, 1);
compile_schema->logger = logger;
cJSON *custom_item = NULL; cJSON *custom_item = NULL;
cJSON *item = cJSON_GetObjectItem(json, "table_id"); cJSON *item = cJSON_GetObjectItem(json, "table_id");
@@ -296,12 +297,6 @@ error:
void compile_schema_free(void *compile_schema) void compile_schema_free(void *compile_schema)
{ {
struct compile_schema *schema = (struct compile_schema *)compile_schema;
if (schema->ex_schema != NULL) {
FREE(schema->ex_schema);
}
FREE(compile_schema); FREE(compile_schema);
} }
@@ -806,19 +801,6 @@ void garbage_maat_compile_free(void *maat_compile, void *arg)
maat_compile_free(compile); maat_compile_free(compile);
} }
void maat_compile_hash_set(struct maat_compile **compile_hash, long long compile_id,
const char *table_name, struct maat_compile *compile)
{
struct maat_compile *tmp_compile = NULL;
HASH_FIND(hh, *compile_hash, &compile_id, sizeof(long long), tmp_compile);
assert(tmp_compile != NULL);
assert(tmp_compile->user_data == NULL);
maat_compile_set(tmp_compile, table_name, compile->declared_clause_num,
compile->user_data, compile->user_data_free);
}
int maat_compile_hash_remove(struct maat_compile **compile_hash, struct maat_compile *compile, int maat_compile_hash_remove(struct maat_compile **compile_hash, struct maat_compile *compile,
struct maat_garbage_bin *garbage_bin) struct maat_garbage_bin *garbage_bin)
{ {
@@ -1455,23 +1437,23 @@ int maat_compile_state_has_NOT_clause(struct maat_compile_state *compile_state)
} }
void compile_item_to_compile_rule(struct compile_item *compile_item, void compile_item_to_compile_rule(struct compile_item *compile_item,
struct compile_schema *compile_schema, struct compile_schema *schema,
struct compile_rule *compile_rule, struct compile_rule *compile_rule,
const char *table_name, const char *table_name,
const char *table_line) const char *table_line)
{ {
compile_rule->magic_num = COMPILE_RULE_MAGIC; compile_rule->magic_num = COMPILE_RULE_MAGIC;
compile_rule->declared_clause_num = compile_item->declared_clause_num; compile_rule->declared_clause_num = compile_item->declared_clause_num;
compile_rule->ref_table = compile_schema; compile_rule->ref_table = schema;
compile_rule->ex_data = ALLOC(void *, 1); compile_rule->ex_data = ALLOC(void *, 1);
compile_rule->table_line_len = strlen(table_line) + 1; compile_rule->table_line_len = strlen(table_line) + 1;
compile_rule->table_line = ALLOC(char, compile_rule->table_line_len); compile_rule->table_line = ALLOC(char, compile_rule->table_line_len);
memcpy(compile_rule->table_line, table_line, compile_rule->table_line_len); memcpy(compile_rule->table_line, table_line, compile_rule->table_line_len);
if (compile_schema->ex_schema != NULL) { if (1 == schema->set_flag) {
*(compile_rule->ex_data) = rule_ex_data_new(table_name, compile_schema->table_id, *(compile_rule->ex_data) = rule_ex_data_new(table_name, schema->table_id,
compile_rule->table_line, compile_rule->table_line,
compile_schema->ex_schema); &(schema->ex_schema));
} }
compile_rule->compile_id = compile_item->compile_id; compile_rule->compile_id = compile_item->compile_id;
@@ -1482,8 +1464,8 @@ void compile_rule_free(struct compile_rule *compile_rule)
struct compile_schema *schema = compile_rule->ref_table; struct compile_schema *schema = compile_rule->ref_table;
assert(compile_rule->magic_num==COMPILE_RULE_MAGIC); assert(compile_rule->magic_num==COMPILE_RULE_MAGIC);
if (schema->ex_schema != NULL) { if (1 == schema->set_flag) {
rule_ex_data_free(schema->table_id, compile_rule->ex_data, schema->ex_schema); rule_ex_data_free(schema->table_id, compile_rule->ex_data, &(schema->ex_schema));
*compile_rule->ex_data = NULL; *compile_rule->ex_data = NULL;
} }
FREE(compile_rule->ex_data); FREE(compile_rule->ex_data);
@@ -1496,12 +1478,12 @@ void compile_runtime_ex_data_iterate(struct compile_runtime *compile_rt,
struct compile_schema *compile_schema) struct compile_schema *compile_schema)
{ {
if (NULL == compile_rt || NULL == compile_schema || if (NULL == compile_rt || NULL == compile_schema ||
NULL == compile_schema->ex_schema) { (0 == compile_schema->set_flag)) {
return; return;
} }
compile_runtime_user_data_iterate(compile_rt, rule_ex_data_new_cb, compile_runtime_user_data_iterate(compile_rt, rule_ex_data_new_cb,
compile_schema->ex_schema, &(compile_schema->ex_schema),
compile_schema->table_id); compile_schema->table_id);
} }
@@ -1509,7 +1491,8 @@ void *compile_runtime_get_ex_data(struct compile_runtime *compile_rt,
struct compile_schema *compile_schema, struct compile_schema *compile_schema,
long long compile_id) long long compile_id)
{ {
if (NULL == compile_rt || NULL == compile_schema || compile_id < 0) { if (NULL == compile_rt || NULL == compile_schema || compile_id < 0 ||
(0 == compile_schema->set_flag)) {
return NULL; return NULL;
} }
@@ -1520,10 +1503,10 @@ void *compile_runtime_get_ex_data(struct compile_runtime *compile_rt,
} }
void *ex_data = NULL; void *ex_data = NULL;
struct ex_data_schema *ex_schema = compile_schema->ex_schema; compile_schema->ex_schema.dup_func(compile_schema->table_id, &ex_data,
ex_schema->dup_func(compile_schema->table_id, &ex_data, compile_rule->ex_data, compile_rule->ex_data,
ex_schema->argl, ex_schema->argp); compile_schema->ex_schema.argl,
compile_schema->ex_schema.argp);
return ex_data; return ex_data;
} }
@@ -1595,16 +1578,16 @@ int compile_runtime_update(void *compile_runtime, void *compile_schema,
return -1; return -1;
} }
maat_compile_set(compile, table_name, compile_rule->declared_clause_num,
compile_rule, (void (*)(void *))compile_rule_free);
struct maat_compile *tmp_compile = maat_compile_hash_find(&(compile_rt->compile_hash), compile_id); struct maat_compile *tmp_compile = maat_compile_hash_find(&(compile_rt->compile_hash), compile_id);
if (tmp_compile != NULL) { if (tmp_compile != NULL) {
maat_compile_hash_set(&(compile_rt->compile_hash), compile_id, table_name, compile); maat_compile_set(tmp_compile, table_name, compile_rule->declared_clause_num,
compile_rule, (void (*)(void *))compile_rule_free);
maat_compile_free(compile);
} else { } else {
maat_compile_set(compile, table_name, compile_rule->declared_clause_num,
compile_rule, (void (*)(void *))compile_rule_free);
maat_compile_hash_add(&(compile_rt->compile_hash), compile_id, compile); maat_compile_hash_add(&(compile_rt->compile_hash), compile_id, compile);
} }
// printf("compile_runtime_update compile_id:%lld, compile->declared_clause_num:%d\n",
// compile->compile_id, compile->declared_clause_num);
pthread_rwlock_unlock(&compile_rt->rwlock); pthread_rwlock_unlock(&compile_rt->rwlock);
} }

View File

@@ -10,6 +10,7 @@
#include <stddef.h> #include <stddef.h>
#include <stdio.h> #include <stdio.h>
#include <assert.h>
#include "uthash/uthash.h" #include "uthash/uthash.h"
#include "uthash/utarray.h" #include "uthash/utarray.h"
@@ -24,7 +25,8 @@ struct ex_data_runtime {
size_t cache_row_num; size_t cache_row_num;
size_t cache_size; size_t cache_size;
struct rcu_hash_table *htable; struct rcu_hash_table *htable; // store ex_container
struct ex_container_schema *ref_container_schema;
int table_id; int table_id;
struct log_handle *logger; struct log_handle *logger;
@@ -38,17 +40,16 @@ void cache_row_free(void *p)
UT_icd ut_cache_row_icd = {sizeof(char*), NULL, NULL, cache_row_free}; UT_icd ut_cache_row_icd = {sizeof(char*), NULL, NULL, cache_row_free};
struct ex_data_runtime * struct ex_data_runtime *
ex_data_runtime_new(int table_id, rcu_hash_data_free_fn *data_free_fn, ex_data_runtime_new(int table_id, struct log_handle *logger)
struct log_handle *logger)
{ {
if (NULL == data_free_fn || NULL == logger) { if (table_id < 0 || NULL == logger) {
return NULL; return NULL;
} }
struct ex_data_runtime *ex_data_rt = ALLOC(struct ex_data_runtime, 1); struct ex_data_runtime *ex_data_rt = ALLOC(struct ex_data_runtime, 1);
utarray_new(ex_data_rt->cache_rows, &ut_cache_row_icd); utarray_new(ex_data_rt->cache_rows, &ut_cache_row_icd);
ex_data_rt->htable = rcu_hash_new(data_free_fn); ex_data_rt->htable = NULL;
ex_data_rt->table_id = table_id; ex_data_rt->table_id = table_id;
ex_data_rt->logger = logger; ex_data_rt->logger = logger;
@@ -130,60 +131,21 @@ void ex_data_runtime_clear_row_cache(struct ex_data_runtime *ex_data_rt)
ex_data_rt->cache_size = 0; ex_data_rt->cache_size = 0;
} }
struct ex_data_schema *ex_data_schema_new(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)
{
struct ex_data_schema *ex_schema = ALLOC(struct ex_data_schema, 1);
ex_schema->new_func = new_func;
ex_schema->free_func = free_func;
ex_schema->dup_func = dup_func;
ex_schema->argl = argl;
ex_schema->argp = argp;
return ex_schema;
}
void ex_data_schema_free(struct ex_data_schema *ex_schema)
{
if (ex_schema != NULL) {
FREE(ex_schema);
}
}
void ex_data_runtime_set_ex_container_schema(struct ex_data_runtime *ex_data_rt, void ex_data_runtime_set_ex_container_schema(struct ex_data_runtime *ex_data_rt,
struct ex_container_schema *container_schema) struct ex_container_schema *container_schema)
{ {
if (NULL == ex_data_rt) { ex_data_rt->ref_container_schema = container_schema;
return;
}
rcu_hash_set_user_ctx(ex_data_rt->htable, container_schema);
}
struct ex_container_schema *
ex_data_runtime_get_ex_container_schema(struct ex_data_runtime *ex_data_rt)
{
if (NULL == ex_data_rt) {
return NULL;
}
return (struct ex_container_schema *)rcu_hash_get_user_ctx(ex_data_rt->htable);
} }
void *ex_data_runtime_row2ex_data(struct ex_data_runtime *ex_data_rt, void *ex_data_runtime_row2ex_data(struct ex_data_runtime *ex_data_rt,
struct ex_data_schema *ex_schema, const char *table_name, const char *row,
const char *table_name, const char *key, size_t key_len)
const char *row, const char *key,
size_t key_len)
{ {
void *ex_data = NULL; void *ex_data = NULL;
struct ex_container_schema *container_schema = ex_data_rt->ref_container_schema;
ex_schema->new_func(table_name, ex_data_rt->table_id, key, row, &ex_data, container_schema->ex_schema.new_func(table_name, ex_data_rt->table_id, key, row,
ex_schema->argl, ex_schema->argp); &ex_data, container_schema->ex_schema.argl,
container_schema->ex_schema.argp);
return ex_data; return ex_data;
} }
@@ -197,78 +159,103 @@ struct ex_container *ex_container_new(void *ex_data, void *custom_data)
return ex_container; return ex_container;
} }
void ex_container_free(void *schema, void *data) void ex_container_free(void *user_ctx, void *data)
{ {
/* schema is NULL if not call ex_data_runtime_set_ex_container_schema */ /* schema is NULL if not call ex_data_runtime_set_ex_container_schema */
if (NULL == data) { if (NULL == user_ctx || NULL == data) {
return; return;
} }
struct ex_container *ex_container = (struct ex_container *)data; struct ex_container *container = (struct ex_container *)data;
struct ex_container_schema *container_schema = (struct ex_container_schema *)schema; struct ex_container_schema *container_schema = (struct ex_container_schema *)user_ctx;
/* free ex_container->custom_data */ /* free ex_container->custom_data */
if (ex_container->custom_data != NULL) { if (container->custom_data != NULL && container_schema->custom_data_free != NULL) {
if (container_schema != NULL && container_schema->user_data_free != NULL) { container_schema->custom_data_free(container->custom_data);
container_schema->user_data_free(ex_container->custom_data);
} else {
FREE(ex_container->custom_data);
}
} }
/* free ex_container->ex_data */ /* free ex_container->ex_data */
if (ex_container->ex_data != NULL) { if (container->ex_data != NULL && container_schema->ex_schema.free_func != NULL) {
if (container_schema != NULL && container_schema->ex_schema != NULL && container_schema->ex_schema.free_func(container_schema->table_id, &(container->ex_data),
container_schema->ex_schema->free_func != NULL) { container_schema->ex_schema.argl,
long argl = container_schema->ex_schema->argl; container_schema->ex_schema.argp);
void *argp = container_schema->ex_schema->argp;
container_schema->ex_schema->free_func(container_schema->table_id,
&(ex_container->ex_data), argl, argp);
}
} }
FREE(ex_container); FREE(container);
} }
int ex_data_runtime_add_ex_container(struct ex_data_runtime *ex_data_rt, int ex_data_runtime_add_ex_container(struct ex_data_runtime *ex_data_rt,
const char *key, size_t key_len, const char *key, size_t key_len,
struct ex_container *ex_container) struct ex_container *ex_container)
{ {
if (NULL == ex_data_rt || NULL == key || 0 == key_len ||
NULL == ex_container) {
return -1;
}
if (NULL == ex_data_rt->htable) {
/* ex_data_rt->ref_container_schema has been set */
assert(ex_data_rt->ref_container_schema != NULL);
ex_data_rt->htable = rcu_hash_new(ex_container_free, ex_data_rt->ref_container_schema);
}
return rcu_hash_add(ex_data_rt->htable, key, key_len, ex_container); return rcu_hash_add(ex_data_rt->htable, key, key_len, ex_container);
} }
int ex_data_runtime_del_ex_container(struct ex_data_runtime *ex_data_rt, int ex_data_runtime_del_ex_container(struct ex_data_runtime *ex_data_rt,
const char *key, size_t key_len) const char *key, size_t key_len)
{ {
if (NULL == ex_data_rt || NULL == key || 0 == key_len ||
NULL == ex_data_rt->htable) {
return -1;
}
return rcu_hash_del(ex_data_rt->htable, key, key_len); return rcu_hash_del(ex_data_rt->htable, key, key_len);
} }
void *ex_data_runtime_get_ex_data_by_key(struct ex_data_runtime *ex_data_rt, void *ex_data_runtime_get_ex_data_by_key(struct ex_data_runtime *ex_data_rt,
struct ex_data_schema *ex_schema,
const char *key, size_t key_len) const char *key, size_t key_len)
{ {
struct ex_container *ex_container = NULL; if (NULL == ex_data_rt || NULL == key || 0 == key_len) {
return NULL;
}
ex_container = (struct ex_container *)rcu_hash_find(ex_data_rt->htable, struct ex_container_schema *container_schema = ex_data_rt->ref_container_schema;
if (NULL == container_schema || (0 == container_schema->set_flag)) {
return NULL;
}
struct ex_container *ex_container = (struct ex_container *)rcu_hash_find(ex_data_rt->htable,
key, key_len); key, key_len);
if (NULL == ex_container) { if (NULL == ex_container) {
return NULL; return NULL;
} }
void *dup_ex_data = NULL; void *dup_ex_data = NULL;
ex_schema->dup_func(ex_data_rt->table_id, &dup_ex_data, &(ex_container->ex_data), container_schema->ex_schema.dup_func(ex_data_rt->table_id, &dup_ex_data,
ex_schema->argl, ex_schema->argp); &(ex_container->ex_data),
container_schema->ex_schema.argl,
container_schema->ex_schema.argp);
return dup_ex_data; return dup_ex_data;
} }
void *ex_data_runtime_get_ex_data_by_container(struct ex_data_runtime *ex_data_rt, void *ex_data_runtime_get_ex_data_by_container(struct ex_data_runtime *ex_data_rt,
struct ex_data_schema *ex_schema,
struct ex_container *ex_container) struct ex_container *ex_container)
{ {
if (NULL == ex_data_rt || NULL == ex_container) {
return NULL;
}
struct ex_container_schema *container_schema = ex_data_rt->ref_container_schema;
if (NULL == container_schema || (0 == container_schema->set_flag)) {
return NULL;
}
void *dup_ex_data = NULL; void *dup_ex_data = NULL;
ex_schema->dup_func(ex_data_rt->table_id, &dup_ex_data, &(ex_container->ex_data), container_schema->ex_schema.dup_func(ex_data_rt->table_id, &dup_ex_data,
ex_schema->argl, ex_schema->argp); &(ex_container->ex_data),
container_schema->ex_schema.argl,
container_schema->ex_schema.argp);
return dup_ex_data; return dup_ex_data;
} }

View File

@@ -455,8 +455,8 @@ void *expr_runtime_new(void *expr_schema, int max_thread_num,
struct expr_runtime *expr_rt = ALLOC(struct expr_runtime, 1); struct expr_runtime *expr_rt = ALLOC(struct expr_runtime, 1);
expr_rt->htable = rcu_hash_new(expr_ex_data_free); expr_rt->htable = rcu_hash_new(expr_ex_data_free, NULL);
expr_rt->item_htable = rcu_hash_new(expr_maat_item_free); expr_rt->item_htable = rcu_hash_new(expr_maat_item_free, NULL);
expr_rt->n_worker_thread = max_thread_num; expr_rt->n_worker_thread = max_thread_num;
expr_rt->ref_garbage_bin = garbage_bin; expr_rt->ref_garbage_bin = garbage_bin;
expr_rt->logger = logger; expr_rt->logger = logger;

View File

@@ -173,8 +173,8 @@ void *flag_runtime_new(void *flag_schema, int max_thread_num,
struct flag_runtime *flag_rt = ALLOC(struct flag_runtime, 1); struct flag_runtime *flag_rt = ALLOC(struct flag_runtime, 1);
flag_rt->htable = rcu_hash_new(flag_ex_data_free); flag_rt->htable = rcu_hash_new(flag_ex_data_free, NULL);
flag_rt->item_htable = rcu_hash_new(flag_maat_item_free); flag_rt->item_htable = rcu_hash_new(flag_maat_item_free, NULL);
flag_rt->ref_garbage_bin = garbage_bin; flag_rt->ref_garbage_bin = garbage_bin;
flag_rt->logger = logger; flag_rt->logger = logger;
flag_rt->district_map = maat_kv_store_new(); flag_rt->district_map = maat_kv_store_new();

View File

@@ -11,8 +11,6 @@
#include <assert.h> #include <assert.h>
#include "maat_fqdn_plugin.h" #include "maat_fqdn_plugin.h"
#include "maat_ex_data.h"
#include "fqdn_engine.h"
#include "maat_utils.h" #include "maat_utils.h"
#include "maat_table.h" #include "maat_table.h"
#include "maat_rule.h" #include "maat_rule.h"
@@ -25,9 +23,10 @@ struct fqdn_plugin_schema {
int suffix_flag_column; int suffix_flag_column;
int fqdn_column; int fqdn_column;
int rule_tag_column; int rule_tag_column;
struct ex_data_schema *ex_schema; struct ex_container_schema container_schema;
int table_id; int table_id;
struct table_manager *ref_tbl_mgr; struct table_manager *ref_tbl_mgr;
struct log_handle *logger;
unsigned long long update_err_cnt; unsigned long long update_err_cnt;
unsigned long long unmatch_tag_cnt; unsigned long long unmatch_tag_cnt;
@@ -47,6 +46,7 @@ void *fqdn_plugin_schema_new(cJSON *json, struct table_manager *tbl_mgr,
const char *table_name, struct log_handle *logger) const char *table_name, struct log_handle *logger)
{ {
struct fqdn_plugin_schema *schema = ALLOC(struct fqdn_plugin_schema, 1); struct fqdn_plugin_schema *schema = ALLOC(struct fqdn_plugin_schema, 1);
schema->logger = logger;
cJSON *custom_item = NULL; cJSON *custom_item = NULL;
cJSON *item = cJSON_GetObjectItem(json, "table_id"); cJSON *item = cJSON_GetObjectItem(json, "table_id");
@@ -116,69 +116,51 @@ void fqdn_plugin_schema_free(void *fqdn_plugin_schema)
if (NULL == fqdn_plugin_schema) { if (NULL == fqdn_plugin_schema) {
return; 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); FREE(fqdn_plugin_schema);
} }
/* fqdn plugin table ex data API */ int fqdn_plugin_table_set_ex_container_schema(void *fqdn_plugin_schema, int table_id,
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_ex_new_func_t *new_func, maat_ex_new_func_t *new_func,
maat_ex_free_func_t *free_func, maat_ex_free_func_t *free_func,
maat_ex_dup_func_t *dup_func, maat_ex_dup_func_t *dup_func,
long argl, void *argp, void (*custom_data_free)(void *),
struct log_handle *logger) long argl, void *argp)
{ {
if (NULL == fqdn_plugin_schema) {
return -1;
}
struct fqdn_plugin_schema *schema = (struct fqdn_plugin_schema *)fqdn_plugin_schema; struct fqdn_plugin_schema *schema = (struct fqdn_plugin_schema *)fqdn_plugin_schema;
if (schema->ex_schema != NULL) {
assert(0); if (1 == schema->container_schema.set_flag) {
log_error(logger, MODULE_FQDN_PLUGIN, log_error(schema->logger, MODULE_FQDN_PLUGIN,
"[%s:%d], EX data schema already registed", __FUNCTION__, __LINE__); "[%s:%d] fqdn_plugin table(table_id:%d) ex_container_schema has been set, can't set again",
__FUNCTION__, __LINE__, table_id);
return -1; return -1;
} }
schema->ex_schema = ex_data_schema_new(new_func, free_func, dup_func, argl, argp); schema->container_schema.table_id = table_id;
schema->container_schema.custom_data_free = custom_data_free;
schema->container_schema.ex_schema.new_func = new_func;
schema->container_schema.ex_schema.free_func = free_func;
schema->container_schema.ex_schema.dup_func = dup_func;
schema->container_schema.ex_schema.argl = argl;
schema->container_schema.ex_schema.argp = argp;
schema->container_schema.set_flag = 1;
return 0; return 0;
} }
void fqdn_rule_free(struct FQDN_rule *fqdn_rule) struct ex_container_schema *fqdn_plugin_table_get_ex_container_schema(void *fqdn_plugin_schema)
{ {
assert(fqdn_rule->user_tag==NULL); struct fqdn_plugin_schema *schema = (struct fqdn_plugin_schema *)fqdn_plugin_schema;
free(fqdn_rule->FQDN);
fqdn_rule->FQDN=NULL; return &(schema->container_schema);
free(fqdn_rule);
return;
} }
void fqdn_ex_container_free(void *schema, void *data) void fqdn_rule_free(struct FQDN_rule *fqdn_rule)
{ {
struct ex_container_schema *container_schema = (struct ex_container_schema *)schema; assert(fqdn_rule->user_tag == NULL);
free(fqdn_rule->FQDN);
if (container_schema != NULL) { fqdn_rule->FQDN = NULL;
container_schema->user_data_free = (void (*)(void *))fqdn_rule_free; free(fqdn_rule);
}
ex_container_free(container_schema, data);
} }
void *fqdn_plugin_runtime_new(void *fqdn_plugin_schema, int max_thread_num, void *fqdn_plugin_runtime_new(void *fqdn_plugin_schema, int max_thread_num,
@@ -192,8 +174,11 @@ void *fqdn_plugin_runtime_new(void *fqdn_plugin_schema, int max_thread_num,
struct fqdn_plugin_schema *schema = (struct fqdn_plugin_schema *)fqdn_plugin_schema; 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); 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, fqdn_ex_container_free, fqdn_plugin_rt->ex_data_rt = ex_data_runtime_new(schema->table_id, logger);
logger); if (1 == schema->container_schema.set_flag) {
ex_data_runtime_set_ex_container_schema(fqdn_plugin_rt->ex_data_rt, &(schema->container_schema));
}
fqdn_plugin_rt->ref_garbage_bin = garbage_bin; fqdn_plugin_rt->ref_garbage_bin = garbage_bin;
fqdn_plugin_rt->logger = logger; fqdn_plugin_rt->logger = logger;
@@ -334,9 +319,10 @@ void fqdn_plugin_rule_free(struct FQDN_rule *rule)
} }
int fqdn_plugin_runtime_update_row(struct fqdn_plugin_runtime *fqdn_plugin_rt, int fqdn_plugin_runtime_update_row(struct fqdn_plugin_runtime *fqdn_plugin_rt,
struct ex_data_schema *ex_schema, const char *table_name, const char *table_name, const char *row,
const char *row, const char *key, size_t key_len, const char *key, size_t key_len,
struct FQDN_rule *fqdn_plugin_rule, int is_valid) struct FQDN_rule *fqdn_plugin_rule,
int is_valid)
{ {
int ret = -1; int ret = -1;
struct ex_data_runtime *ex_data_rt = fqdn_plugin_rt->ex_data_rt; struct ex_data_runtime *ex_data_rt = fqdn_plugin_rt->ex_data_rt;
@@ -349,7 +335,7 @@ int fqdn_plugin_runtime_update_row(struct fqdn_plugin_runtime *fqdn_plugin_rt,
} }
} else { } else {
// add // add
void *ex_data = ex_data_runtime_row2ex_data(ex_data_rt, ex_schema, table_name, row, key, key_len); void *ex_data = ex_data_runtime_row2ex_data(ex_data_rt, table_name, row, key, key_len);
struct ex_container *ex_container = ex_container_new(ex_data, (void *)fqdn_plugin_rule); struct ex_container *ex_container = ex_container_new(ex_data, (void *)fqdn_plugin_rule);
ret = ex_data_runtime_add_ex_container(ex_data_rt, key, key_len, ex_container); ret = ex_data_runtime_add_ex_container(ex_data_rt, key, key_len, ex_container);
if (ret < 0) { if (ret < 0) {
@@ -383,8 +369,7 @@ int fqdn_plugin_runtime_update(void *fqdn_plugin_runtime, void *fqdn_plugin_sche
return -1; return -1;
} }
struct ex_data_schema *ex_schema = schema->ex_schema; if (1 == schema->container_schema.set_flag) {
if (ex_schema != NULL) {
if (1 == is_valid) { if (1 == is_valid) {
// add // add
fqdn_plugin_rule = fqdn_plugin_rule_new(line, schema, table_name, fqdn_plugin_rt->logger); fqdn_plugin_rule = fqdn_plugin_rule_new(line, schema, table_name, fqdn_plugin_rt->logger);
@@ -395,7 +380,7 @@ int fqdn_plugin_runtime_update(void *fqdn_plugin_runtime, void *fqdn_plugin_sche
const char *key = line + item_id_offset; const char *key = line + item_id_offset;
size_t key_len = item_id_len; size_t key_len = item_id_len;
ret = fqdn_plugin_runtime_update_row(fqdn_plugin_rt, ex_schema, table_name, line, key, key_len, ret = fqdn_plugin_runtime_update_row(fqdn_plugin_rt, table_name, line, key, key_len,
fqdn_plugin_rule, is_valid); fqdn_plugin_rule, is_valid);
if (ret < 0) { if (ret < 0) {
if (fqdn_plugin_rule != NULL) { if (fqdn_plugin_rule != NULL) {
@@ -499,17 +484,11 @@ struct ex_data_runtime *fqdn_plugin_runtime_get_ex_data_rt(void *fqdn_plugin_run
} }
int fqdn_plugin_runtime_get_ex_data(void *fqdn_plugin_runtime, void *fqdn_plugin_schema, int fqdn_plugin_runtime_get_ex_data(void *fqdn_plugin_runtime, const char *query_fqdn,
const char *query_fqdn, void **ex_data_array, void **ex_data_array, size_t n_ex_data)
size_t n_ex_data)
{ {
if (NULL == fqdn_plugin_runtime || NULL == fqdn_plugin_schema) { if (NULL == fqdn_plugin_runtime || NULL == query_fqdn ||
return -1; NULL == ex_data_array || 0 == n_ex_data) {
}
struct fqdn_plugin_schema *schema = (struct fqdn_plugin_schema *)fqdn_plugin_schema;
struct ex_data_schema *ex_schema = schema->ex_schema;
if (NULL == ex_schema) {
return -1; return -1;
} }
@@ -524,7 +503,7 @@ int fqdn_plugin_runtime_get_ex_data(void *fqdn_plugin_runtime, void *fqdn_plugin
assert(fqdn_plugin_rt->engine != NULL); assert(fqdn_plugin_rt->engine != NULL);
int n_result = FQDN_engine_search(fqdn_plugin_rt->engine, query_fqdn, strlen(query_fqdn), results, n_ex_data); int n_result = FQDN_engine_search(fqdn_plugin_rt->engine, query_fqdn, strlen(query_fqdn), results, n_ex_data);
for (int i = 0; i < n_result; i++) { for (int i = 0; i < n_result; i++) {
ex_data_array[i] = ex_data_runtime_get_ex_data_by_container(fqdn_plugin_rt->ex_data_rt, ex_schema, ex_data_array[i] = ex_data_runtime_get_ex_data_by_container(fqdn_plugin_rt->ex_data_rt,
(struct ex_container *)results[i].user_tag); (struct ex_container *)results[i].user_tag);
} }

View File

@@ -170,8 +170,8 @@ void *interval_runtime_new(void *interval_schema, int max_thread_num,
struct interval_runtime *interval_rt = ALLOC(struct interval_runtime, 1); struct interval_runtime *interval_rt = ALLOC(struct interval_runtime, 1);
interval_rt->htable = rcu_hash_new(interval_ex_data_free); interval_rt->htable = rcu_hash_new(interval_ex_data_free, NULL);
interval_rt->item_htable = rcu_hash_new(interval_maat_item_free); interval_rt->item_htable = rcu_hash_new(interval_maat_item_free, NULL);
interval_rt->ref_garbage_bin = garbage_bin; interval_rt->ref_garbage_bin = garbage_bin;
interval_rt->logger = logger; interval_rt->logger = logger;
interval_rt->district_map = maat_kv_store_new(); interval_rt->district_map = maat_kv_store_new();

View File

@@ -380,8 +380,8 @@ void *ip_runtime_new(void *ip_schema, int max_thread_num,
{ {
struct ip_runtime *ip_rt = ALLOC(struct ip_runtime, 1); struct ip_runtime *ip_rt = ALLOC(struct ip_runtime, 1);
ip_rt->htable = rcu_hash_new(ip_ex_data_free); ip_rt->htable = rcu_hash_new(ip_ex_data_free, NULL);
ip_rt->item_htable = rcu_hash_new(ip_maat_item_free); ip_rt->item_htable = rcu_hash_new(ip_maat_item_free, NULL);
ip_rt->ref_garbage_bin = garbage_bin; ip_rt->ref_garbage_bin = garbage_bin;
ip_rt->logger = logger; ip_rt->logger = logger;

View File

@@ -13,10 +13,8 @@
#include "log/log.h" #include "log/log.h"
#include "maat_utils.h" #include "maat_utils.h"
#include "maat_ip_plugin.h" #include "maat_ip_plugin.h"
#include "maat_ex_data.h"
#include "ip_matcher.h" #include "ip_matcher.h"
#include "maat_rule.h" #include "maat_rule.h"
#include "maat.h"
#include "maat_garbage_collection.h" #include "maat_garbage_collection.h"
#define MODULE_IP_PLUGIN module_name_str("maat.ip_plugin") #define MODULE_IP_PLUGIN module_name_str("maat.ip_plugin")
@@ -29,9 +27,10 @@ struct ip_plugin_schema {
int end_ip_column; int end_ip_column;
int addr_format_column; int addr_format_column;
int rule_tag_column; int rule_tag_column;
struct ex_data_schema *ex_schema; struct ex_container_schema container_schema;
int table_id; //ugly int table_id; //ugly
struct table_manager *ref_tbl_mgr; struct table_manager *ref_tbl_mgr;
struct log_handle *logger;
unsigned long long update_err_cnt; unsigned long long update_err_cnt;
unsigned long long unmatch_tag_cnt; unsigned long long unmatch_tag_cnt;
@@ -52,6 +51,7 @@ void *ip_plugin_schema_new(cJSON *json, struct table_manager *tbl_mgr,
{ {
size_t read_cnt = 0; size_t read_cnt = 0;
struct ip_plugin_schema *schema = ALLOC(struct ip_plugin_schema, 1); struct ip_plugin_schema *schema = ALLOC(struct ip_plugin_schema, 1);
schema->logger = logger;
cJSON *custom_item = NULL; cJSON *custom_item = NULL;
cJSON *item = cJSON_GetObjectItem(json, "table_id"); cJSON *item = cJSON_GetObjectItem(json, "table_id");
@@ -122,24 +122,8 @@ void ip_plugin_schema_free(void *ip_plugin_schema)
if (NULL == ip_plugin_schema) { if (NULL == ip_plugin_schema) {
return; return;
} }
struct ip_plugin_schema *schema = (struct ip_plugin_schema *)ip_plugin_schema;
if (schema->ex_schema != NULL) {
ex_data_schema_free(schema->ex_schema);
schema->ex_schema = NULL;
}
free(schema); FREE(ip_plugin_schema);
}
struct ex_data_schema *ip_plugin_table_get_ex_data_schema(void *ip_plugin_schema)
{
if (NULL == ip_plugin_schema) {
return NULL;
}
struct ip_plugin_schema *schema = (struct ip_plugin_schema *)ip_plugin_schema;
return schema->ex_schema;
} }
int ip_plugin_accept_tag_match(struct ip_plugin_schema *schema, const char *line, int ip_plugin_accept_tag_match(struct ip_plugin_schema *schema, const char *line,
@@ -298,27 +282,44 @@ void ip_plugin_rule_free(struct ip_rule *rule)
FREE(rule); FREE(rule);
} }
void garbage_ip_plugin_rule_free(void *ip_rule, void *arg) int ip_plugin_table_set_ex_container_schema(void *ip_plugin_schema, int table_id,
{
struct ip_rule *rule = (struct ip_rule *)ip_rule;
ip_plugin_rule_free(rule);
}
void ip_plugin_table_set_ex_data_schema(void *ip_plugin_schema,
maat_ex_new_func_t *new_func, maat_ex_new_func_t *new_func,
maat_ex_free_func_t *free_func, maat_ex_free_func_t *free_func,
maat_ex_dup_func_t *dup_func, maat_ex_dup_func_t *dup_func,
long argl, void *argp, void (*custom_data_free)(void *),
struct log_handle *logger) long argl, void *argp)
{ {
struct ip_plugin_schema *schema = (struct ip_plugin_schema *)ip_plugin_schema; struct ip_plugin_schema *schema = (struct ip_plugin_schema *)ip_plugin_schema;
schema->ex_schema = ex_data_schema_new(new_func, free_func, dup_func, argl, argp); if (1 == schema->container_schema.set_flag) {
log_error(schema->logger, MODULE_IP_PLUGIN,
"[%s:%d] ip_plugin table(table_id:%d) ex_container_schema has been set, can't set again",
__FUNCTION__, __LINE__, table_id);
return -1;
}
schema->container_schema.table_id = table_id;
schema->container_schema.custom_data_free = custom_data_free;
schema->container_schema.ex_schema.new_func = new_func;
schema->container_schema.ex_schema.free_func = free_func;
schema->container_schema.ex_schema.dup_func = dup_func;
schema->container_schema.ex_schema.argl = argl;
schema->container_schema.ex_schema.argp = argp;
schema->container_schema.set_flag = 1;
return 0;
}
struct ex_container_schema *ip_plugin_table_get_ex_container_schema(void *ip_plugin_schema)
{
struct ip_plugin_schema *schema = (struct ip_plugin_schema *)ip_plugin_schema;
return &(schema->container_schema);
} }
int ip_plugin_runtime_update_row(struct ip_plugin_runtime *ip_plugin_rt, int ip_plugin_runtime_update_row(struct ip_plugin_runtime *ip_plugin_rt,
struct ex_data_schema *ex_schema, const char *table_name, const char *table_name, const char *row,
const char *row, const char *key, size_t key_len, const char *key, size_t key_len,
struct ip_rule *ip_plugin_rule, int is_valid) struct ip_rule *ip_plugin_rule, int is_valid)
{ {
int ret = -1; int ret = -1;
@@ -335,7 +336,7 @@ int ip_plugin_runtime_update_row(struct ip_plugin_runtime *ip_plugin_rt,
} }
} else { } else {
// add // add
void *ex_data = ex_data_runtime_row2ex_data(ex_data_rt, ex_schema, table_name, row, key, key_len); void *ex_data = ex_data_runtime_row2ex_data(ex_data_rt, table_name, row, key, key_len);
struct ex_container *ex_container = ex_container_new(ex_data, (void *)ip_plugin_rule); struct ex_container *ex_container = ex_container_new(ex_data, (void *)ip_plugin_rule);
ret = ex_data_runtime_add_ex_container(ex_data_rt, key, key_len, ex_container); ret = ex_data_runtime_add_ex_container(ex_data_rt, key, key_len, ex_container);
if (ret < 0) { if (ret < 0) {
@@ -357,8 +358,11 @@ void *ip_plugin_runtime_new(void *ip_plugin_schema, int max_thread_num,
struct ip_plugin_schema *schema = (struct ip_plugin_schema *)ip_plugin_schema; struct ip_plugin_schema *schema = (struct ip_plugin_schema *)ip_plugin_schema;
struct ip_plugin_runtime *ip_plugin_rt = ALLOC(struct ip_plugin_runtime, 1); struct ip_plugin_runtime *ip_plugin_rt = ALLOC(struct ip_plugin_runtime, 1);
ip_plugin_rt->ex_data_rt = ex_data_runtime_new(schema->table_id, ex_container_free, ip_plugin_rt->ex_data_rt = ex_data_runtime_new(schema->table_id, logger);
logger); if (1 == schema->container_schema.set_flag) {
ex_data_runtime_set_ex_container_schema(ip_plugin_rt->ex_data_rt, &(schema->container_schema));
}
ip_plugin_rt->ref_garbage_bin = garbage_bin; ip_plugin_rt->ref_garbage_bin = garbage_bin;
ip_plugin_rt->logger = logger; ip_plugin_rt->logger = logger;
@@ -409,8 +413,7 @@ int ip_plugin_runtime_update(void *ip_plugin_runtime, void *ip_plugin_schema,
return -1; return -1;
} }
struct ex_data_schema *ex_schema = schema->ex_schema; if (1 == schema->container_schema.set_flag) {
if (ex_schema != NULL) {
if (1 == is_valid) { if (1 == is_valid) {
// add // add
ip_plugin_rule = ip_plugin_rule_new(line, schema, table_name, ip_plugin_rt->logger); ip_plugin_rule = ip_plugin_rule_new(line, schema, table_name, ip_plugin_rt->logger);
@@ -421,7 +424,7 @@ int ip_plugin_runtime_update(void *ip_plugin_runtime, void *ip_plugin_schema,
const char *key = line + item_id_offset; const char *key = line + item_id_offset;
size_t key_len = item_id_len; size_t key_len = item_id_len;
ret = ip_plugin_runtime_update_row(ip_plugin_rt, ex_schema, table_name, line, key, key_len, ret = ip_plugin_runtime_update_row(ip_plugin_rt, table_name, line, key, key_len,
ip_plugin_rule, is_valid); ip_plugin_rule, is_valid);
if (ret < 0) { if (ret < 0) {
if (ip_plugin_rule != NULL) { if (ip_plugin_rule != NULL) {
@@ -520,17 +523,11 @@ struct ex_data_runtime *ip_plugin_runtime_get_ex_data_rt(void *ip_plugin_runtime
return ip_plugin_rt->ex_data_rt; return ip_plugin_rt->ex_data_rt;
} }
int ip_plugin_runtime_get_ex_data(void *ip_plugin_runtime, void *ip_plugin_schema, int ip_plugin_runtime_get_ex_data(void *ip_plugin_runtime, const struct ip_addr *ip_addr,
const struct ip_addr *ip_addr, void **ex_data_array, void **ex_data_array, size_t n_ex_data)
size_t n_ex_data)
{ {
if (NULL == ip_plugin_runtime || NULL == ip_plugin_schema) { if (NULL == ip_plugin_runtime || NULL == ip_addr ||
return -1; NULL == ex_data_array || 0 == n_ex_data) {
}
struct ip_plugin_schema *schema = (struct ip_plugin_schema *)ip_plugin_schema;
struct ex_data_schema *ex_schema = schema->ex_schema;
if (NULL == ex_schema) {
return -1; return -1;
} }
@@ -552,7 +549,7 @@ int ip_plugin_runtime_get_ex_data(void *ip_plugin_runtime, void *ip_plugin_schem
assert(ip_plugin_rt->ip_matcher != NULL); assert(ip_plugin_rt->ip_matcher != NULL);
int n_result = ip_matcher_match(ip_plugin_rt->ip_matcher, &ip_data, results, n_ex_data); int n_result = ip_matcher_match(ip_plugin_rt->ip_matcher, &ip_data, results, n_ex_data);
for (int i = 0; i < n_result; i++) { for (int i = 0; i < n_result; i++) {
ex_data_array[i] = ex_data_runtime_get_ex_data_by_container(ip_plugin_rt->ex_data_rt, ex_schema, ex_data_array[i] = ex_data_runtime_get_ex_data_by_container(ip_plugin_rt->ex_data_rt,
(struct ex_container *)results[i].tag); (struct ex_container *)results[i].tag);
} }
return n_result; return n_result;

View File

@@ -16,7 +16,6 @@
#include "maat_utils.h" #include "maat_utils.h"
#include "maat_rule.h" #include "maat_rule.h"
#include "maat_plugin.h" #include "maat_plugin.h"
#include "maat_ex_data.h"
#include "maat_limits.h" #include "maat_limits.h"
#include "maat_table.h" #include "maat_table.h"
@@ -54,9 +53,10 @@ struct plugin_schema {
int foreign_columns[MAX_FOREIGN_CLMN_NUM]; int foreign_columns[MAX_FOREIGN_CLMN_NUM];
size_t cb_cnt; size_t cb_cnt;
struct plugin_callback_schema cb[MAX_PLUGIN_PER_TABLE]; struct plugin_callback_schema cb[MAX_PLUGIN_PER_TABLE];
struct ex_data_schema *ex_schema; struct ex_container_schema container_schema;
int table_id; //ugly int table_id; //ugly
struct table_manager *ref_tbl_mgr; struct table_manager *ref_tbl_mgr;
struct log_handle *logger;
unsigned long long update_err_cnt; unsigned long long update_err_cnt;
unsigned long long unmatch_tag_cnt; unsigned long long unmatch_tag_cnt;
@@ -84,6 +84,8 @@ void *plugin_schema_new(cJSON *json, struct table_manager *tbl_mgr,
{ {
struct plugin_schema *schema = ALLOC(struct plugin_schema, 1); struct plugin_schema *schema = ALLOC(struct plugin_schema, 1);
schema->logger = logger;
cJSON *custom_item = NULL; cJSON *custom_item = NULL;
cJSON *item = cJSON_GetObjectItem(json, "table_id"); cJSON *item = cJSON_GetObjectItem(json, "table_id");
if (NULL == item || item->type != cJSON_Number) { if (NULL == item || item->type != cJSON_Number) {
@@ -162,13 +164,7 @@ void plugin_schema_free(void *plugin_schema)
return; return;
} }
struct plugin_schema *schema = (struct plugin_schema *)plugin_schema; FREE(plugin_schema);
if (schema->ex_schema != NULL) {
ex_data_schema_free(schema->ex_schema);
schema->ex_schema = NULL;
}
free(schema);
} }
int plugin_table_add_callback(void *plugin_schema, int table_id, int plugin_table_add_callback(void *plugin_schema, int table_id,
@@ -233,26 +229,39 @@ int plugin_table_get_foreign_column(struct plugin_schema *plugin_schema,
return n_foreign; return n_foreign;
} }
void plugin_table_set_ex_data_schema(void *plugin_schema, int plugin_table_set_ex_container_schema(void *plugin_schema, int table_id,
maat_ex_new_func_t *new_func, maat_ex_new_func_t *new_func,
maat_ex_free_func_t *free_func, maat_ex_free_func_t *free_func,
maat_ex_dup_func_t *dup_func, maat_ex_dup_func_t *dup_func,
long argl, void *argp, void (*custom_data_free)(void *),
struct log_handle *logger) long argl, void *argp)
{ {
struct plugin_schema *schema = (struct plugin_schema *)plugin_schema; struct plugin_schema *schema = (struct plugin_schema *)plugin_schema;
schema->ex_schema = ex_data_schema_new(new_func, free_func, dup_func, argl, argp); if (1 == schema->container_schema.set_flag) {
} log_error(schema->logger, MODULE_PLUGIN,
"[%s:%d] plugin table(table_id:%d) ex_container_schema has been set, can't set again",
struct ex_data_schema *plugin_table_get_ex_data_schema(void *plugin_schema) __FUNCTION__, __LINE__, table_id);
{ return -1;
if (NULL == plugin_schema) {
return NULL;
} }
schema->container_schema.table_id = table_id;
schema->container_schema.custom_data_free = custom_data_free;
schema->container_schema.ex_schema.new_func = new_func;
schema->container_schema.ex_schema.free_func = free_func;
schema->container_schema.ex_schema.dup_func = dup_func;
schema->container_schema.ex_schema.argl = argl;
schema->container_schema.ex_schema.argp = argp;
schema->container_schema.set_flag = 1;
return 0;
}
struct ex_container_schema *plugin_table_get_ex_container_schema(void *plugin_schema)
{
struct plugin_schema *schema = (struct plugin_schema *)plugin_schema; struct plugin_schema *schema = (struct plugin_schema *)plugin_schema;
return schema->ex_schema;
return &(schema->container_schema);
} }
void *plugin_runtime_new(void *plugin_schema, int max_thread_num, void *plugin_runtime_new(void *plugin_schema, int max_thread_num,
@@ -265,7 +274,11 @@ void *plugin_runtime_new(void *plugin_schema, int max_thread_num,
struct plugin_schema *schema = (struct plugin_schema *)plugin_schema; struct plugin_schema *schema = (struct plugin_schema *)plugin_schema;
struct plugin_runtime *plugin_rt = ALLOC(struct plugin_runtime, 1); struct plugin_runtime *plugin_rt = ALLOC(struct plugin_runtime, 1);
plugin_rt->ex_data_rt = ex_data_runtime_new(schema->table_id, ex_container_free, logger); plugin_rt->ex_data_rt = ex_data_runtime_new(schema->table_id, logger);
if (1 == schema->container_schema.set_flag) {
ex_data_runtime_set_ex_container_schema(plugin_rt->ex_data_rt, &(schema->container_schema));
}
plugin_rt->ref_garbage_bin = garbage_bin; plugin_rt->ref_garbage_bin = garbage_bin;
plugin_rt->logger = logger; plugin_rt->logger = logger;
@@ -293,10 +306,10 @@ int plugin_runtime_update_row(struct plugin_runtime *plugin_rt,
const char *key, size_t key_len, int is_valid) const char *key, size_t key_len, int is_valid)
{ {
int ret = -1; int ret = -1;
struct ex_data_schema *ex_schema = plugin_schema->ex_schema; struct ex_container_schema *container_schema = &(plugin_schema->container_schema);
/* already set plugin_table_schema's ex_data_schema */ /* already set plugin_table_schema's ex_data_schema */
if (ex_schema != NULL) { if (1 == container_schema->set_flag) {
if (is_valid == 0) { if (is_valid == 0) {
// delete // delete
ret = ex_data_runtime_del_ex_container(plugin_rt->ex_data_rt, key, key_len); ret = ex_data_runtime_del_ex_container(plugin_rt->ex_data_rt, key, key_len);
@@ -305,8 +318,8 @@ int plugin_runtime_update_row(struct plugin_runtime *plugin_rt,
} }
} else { } else {
// add // add
void *ex_data = ex_data_runtime_row2ex_data(plugin_rt->ex_data_rt, ex_schema, void *ex_data = ex_data_runtime_row2ex_data(plugin_rt->ex_data_rt, table_name,
table_name, row, key, key_len); row, key, key_len);
struct ex_container *ex_container = ex_container_new(ex_data, NULL); struct ex_container *ex_container = ex_container_new(ex_data, NULL);
ret = ex_data_runtime_add_ex_container(plugin_rt->ex_data_rt, key, key_len, ex_container); ret = ex_data_runtime_add_ex_container(plugin_rt->ex_data_rt, key, key_len, ex_container);
if (ret < 0) { if (ret < 0) {
@@ -324,7 +337,7 @@ int plugin_runtime_update_row(struct plugin_runtime *plugin_rt,
} }
} }
if ((NULL == ex_schema) && (0 == cb_count)) { if ((0 == container_schema->set_flag) && (0 == cb_count)) {
ex_data_runtime_cache_row_put(plugin_rt->ex_data_rt, row); ex_data_runtime_cache_row_put(plugin_rt->ex_data_rt, row);
} }
@@ -486,14 +499,10 @@ void *plugin_runtime_get_ex_data(void *plugin_runtime, void *plugin_schema,
struct plugin_runtime *plugin_rt = (struct plugin_runtime *)plugin_runtime; struct plugin_runtime *plugin_rt = (struct plugin_runtime *)plugin_runtime;
struct plugin_schema *schema = (struct plugin_schema *)plugin_schema; struct plugin_schema *schema = (struct plugin_schema *)plugin_schema;
struct ex_data_schema *ex_schema = schema->ex_schema;
if (NULL == ex_schema) {
return NULL;
}
if (schema->key_type == PLUGIN_KEY_TYPE_INTEGER) { if (schema->key_type == PLUGIN_KEY_TYPE_INTEGER) {
key_len = sizeof(long long); key_len = sizeof(long long);
} }
return ex_data_runtime_get_ex_data_by_key(plugin_rt->ex_data_rt, ex_schema, key, key_len); return ex_data_runtime_get_ex_data_by_key(plugin_rt->ex_data_rt, key, key_len);
} }

View File

@@ -90,7 +90,7 @@ void rcu_hash_node_free(struct rcu_hash_node *node)
FREE(node); FREE(node);
} }
struct rcu_hash_table *rcu_hash_new(rcu_hash_data_free_fn *free_fn) struct rcu_hash_table *rcu_hash_new(data_free_fn *free_fn, void *user_ctx)
{ {
if (NULL == free_fn) { if (NULL == free_fn) {
return NULL; return NULL;
@@ -103,6 +103,7 @@ struct rcu_hash_table *rcu_hash_new(rcu_hash_data_free_fn *free_fn)
TAILQ_INIT(&htable->garbage_q); TAILQ_INIT(&htable->garbage_q);
htable->garbage_q_len = 0; htable->garbage_q_len = 0;
htable->data_free_fn = free_fn; htable->data_free_fn = free_fn;
htable->user_ctx = user_ctx;
pthread_mutex_init(&htable->update_mutex, NULL); pthread_mutex_init(&htable->update_mutex, NULL);
return htable; return htable;
@@ -129,25 +130,11 @@ void rcu_hash_free(struct rcu_hash_table *htable)
} }
rcu_hash_garbage_queue_free(&(htable->garbage_q)); rcu_hash_garbage_queue_free(&(htable->garbage_q));
if (htable->user_ctx != NULL) {
FREE(htable->user_ctx);
}
pthread_mutex_destroy(&htable->update_mutex); pthread_mutex_destroy(&htable->update_mutex);
FREE(htable); FREE(htable);
} }
void rcu_hash_set_user_ctx(struct rcu_hash_table *htable, void *user_ctx)
{
htable->user_ctx = user_ctx;
}
void *rcu_hash_get_user_ctx(struct rcu_hash_table *htable)
{
return htable->user_ctx;
}
void rcu_hash_commit_prepare(struct rcu_hash_table *htable) void rcu_hash_commit_prepare(struct rcu_hash_table *htable)
{ {
struct rcu_hash_node *node = NULL; struct rcu_hash_node *node = NULL;

View File

@@ -26,9 +26,6 @@ target_link_libraries(adapter_hs_gtest maat_frame_static gtest_static)
add_executable(maat_ex_data_gtest maat_ex_data_gtest.cpp) add_executable(maat_ex_data_gtest maat_ex_data_gtest.cpp)
target_link_libraries(maat_ex_data_gtest maat_frame_static gtest_static) target_link_libraries(maat_ex_data_gtest maat_frame_static gtest_static)
add_executable(maat_garbage_collection_gtest maat_garbage_collection_gtest.cpp)
target_link_libraries(maat_garbage_collection_gtest maat_frame_static gtest_static)
file(COPY table_info.conf DESTINATION ./) file(COPY table_info.conf DESTINATION ./)
file(COPY file_test_tableinfo.conf DESTINATION ./) file(COPY file_test_tableinfo.conf DESTINATION ./)
file(COPY literal_expr.conf DESTINATION ./) file(COPY literal_expr.conf DESTINATION ./)

View File

@@ -59,14 +59,23 @@ TEST(EXDataRuntime, Update) {
ASSERT_GT(table_id, 0); ASSERT_GT(table_id, 0);
int ex_data_counter = 0; int ex_data_counter = 0;
struct ex_data_runtime *ex_data_rt = ex_data_runtime_new(table_id, ex_container_free, g_logger); struct ex_data_runtime *ex_data_rt = ex_data_runtime_new(table_id, g_logger);
struct ex_data_schema *ex_schema = ex_data_schema_new(ex_data_new_cb, ex_data_free_cb, ex_data_dup_cb, struct ex_container_schema *container_schema = ALLOC(struct ex_container_schema, 1);
0, &ex_data_counter); container_schema->custom_data_free = NULL;
container_schema->table_id = table_id;
container_schema->ex_schema.new_func = ex_data_new_cb;
container_schema->ex_schema.free_func = ex_data_free_cb;
container_schema->ex_schema.dup_func = ex_data_dup_cb;
container_schema->ex_schema.argl = 0;
container_schema->ex_schema.argp = &ex_data_counter;
container_schema->set_flag = 1;
ex_data_runtime_set_ex_container_schema(ex_data_rt, container_schema);
const char *row1 = "1\t192.168.0.1\tmahuateng\t1\t0"; const char *row1 = "1\t192.168.0.1\tmahuateng\t1\t0";
const char *key1 = "192.168.0.1"; const char *key1 = "192.168.0.1";
size_t key1_len = strlen(key1); size_t key1_len = strlen(key1);
void *ex_data = ex_data_runtime_row2ex_data(ex_data_rt, ex_schema, table_name, row1, key1, key1_len); void *ex_data = ex_data_runtime_row2ex_data(ex_data_rt, table_name, row1, key1, key1_len);
EXPECT_EQ(ex_data_counter, 1); EXPECT_EQ(ex_data_counter, 1);
struct ex_container *ex_container = ex_container_new(ex_data, NULL); struct ex_container *ex_container = ex_container_new(ex_data, NULL);
@@ -76,14 +85,14 @@ TEST(EXDataRuntime, Update) {
const char *row2 = "2\t192.168.0.2\tliyanhong\t1\t0"; const char *row2 = "2\t192.168.0.2\tliyanhong\t1\t0";
const char *key2 = "192.168.0.2"; const char *key2 = "192.168.0.2";
size_t key2_len = strlen(key2); size_t key2_len = strlen(key2);
ex_data = ex_data_runtime_row2ex_data(ex_data_rt, ex_schema, table_name, row2, key2, key2_len); ex_data = ex_data_runtime_row2ex_data(ex_data_rt, table_name, row2, key2, key2_len);
ex_container = ex_container_new(ex_data, NULL); ex_container = ex_container_new(ex_data, NULL);
ret = ex_data_runtime_add_ex_container(ex_data_rt, key2, key2_len, ex_container); ret = ex_data_runtime_add_ex_container(ex_data_rt, key2, key2_len, ex_container);
EXPECT_EQ(ret, 0); EXPECT_EQ(ret, 0);
ex_data_runtime_commit(ex_data_rt); ex_data_runtime_commit(ex_data_rt);
void *res_data1 = ex_data_runtime_get_ex_data_by_key(ex_data_rt, ex_schema, "192.168.0.1", 11); void *res_data1 = ex_data_runtime_get_ex_data_by_key(ex_data_rt, "192.168.0.1", 11);
EXPECT_TRUE(res_data1 != NULL); EXPECT_TRUE(res_data1 != NULL);
struct user_info *info = (struct user_info *)res_data1; struct user_info *info = (struct user_info *)res_data1;
@@ -92,7 +101,7 @@ TEST(EXDataRuntime, Update) {
ex_data_free_cb(table_id, (void **)&res_data1, 0, NULL); ex_data_free_cb(table_id, (void **)&res_data1, 0, NULL);
ex_container = NULL; ex_container = NULL;
void *res_data2 = ex_data_runtime_get_ex_data_by_key(ex_data_rt, ex_schema, "192.168.0.2", 11); void *res_data2 = ex_data_runtime_get_ex_data_by_key(ex_data_rt, "192.168.0.2", 11);
EXPECT_TRUE(res_data2 != NULL); EXPECT_TRUE(res_data2 != NULL);
info = (struct user_info *)res_data2; info = (struct user_info *)res_data2;
@@ -101,7 +110,6 @@ TEST(EXDataRuntime, Update) {
ex_data_free_cb(table_id, (void **)&res_data2, 0, NULL); ex_data_free_cb(table_id, (void **)&res_data2, 0, NULL);
ex_data_runtime_free(ex_data_rt); ex_data_runtime_free(ex_data_rt);
ex_data_schema_free(ex_schema);
} }
int main(int argc, char ** argv) int main(int argc, char ** argv)

View File

@@ -95,6 +95,9 @@ int write_config_to_redis(char *redis_ip, int redis_port, int redis_db,
redisReply *reply = maat_cmd_wrap_redis_command(c, "flushdb"); redisReply *reply = maat_cmd_wrap_redis_command(c, "flushdb");
if (NULL == reply) { if (NULL == reply) {
return -1; return -1;
} else {
freeReplyObject(reply);
reply = NULL;
} }
if (0 == access(json_iris_path, F_OK)) { if (0 == access(json_iris_path, F_OK)) {
@@ -2569,7 +2572,7 @@ TEST_F(CompileTable, CompileRuleUpdate) {
int ret = compile_table_set_line(maat_instance, compile_table_name, MAAT_OP_ADD, compile_id, NULL, 1, 0); int ret = compile_table_set_line(maat_instance, compile_table_name, MAAT_OP_ADD, compile_id, NULL, 1, 0);
EXPECT_EQ(ret, 1); EXPECT_EQ(ret, 1);
sleep(WAIT_FOR_EFFECTIVE_S); sleep(WAIT_FOR_EFFECTIVE_S * 2);
ret = compile_table_set_line(maat_instance, compile_table_name, MAAT_OP_DEL, compile_id, NULL, 1, 0); ret = compile_table_set_line(maat_instance, compile_table_name, MAAT_OP_DEL, compile_id, NULL, 1, 0);
EXPECT_EQ(ret, 1); EXPECT_EQ(ret, 1);
sleep(WAIT_FOR_EFFECTIVE_S); sleep(WAIT_FOR_EFFECTIVE_S);
@@ -4144,7 +4147,7 @@ TEST_F(MaatCmdTest, SetFile) {
ret = maat_cmd_set_line(maat_instance, &line_rule); ret = maat_cmd_set_line(maat_instance, &line_rule);
EXPECT_EQ(ret, 1); EXPECT_EQ(ret, 1);
sleep(WAIT_FOR_EFFECTIVE_S);//wait for callback triggered. sleep(WAIT_FOR_EFFECTIVE_S * 2);//wait for callback triggered.
EXPECT_EQ(g_test_foregin_read_OK, 1); EXPECT_EQ(g_test_foregin_read_OK, 1);
g_test_foreign_del_OK = 0; g_test_foreign_del_OK = 0;
@@ -4185,7 +4188,7 @@ TEST_F(MaatCmdTest, SetFile) {
ret = maat_cmd_set_line(maat_instance, &line_rule); ret = maat_cmd_set_line(maat_instance, &line_rule);
EXPECT_EQ(ret, 1); EXPECT_EQ(ret, 1);
sleep(WAIT_FOR_EFFECTIVE_S);//wait for callback triggered. sleep(WAIT_FOR_EFFECTIVE_S * 2);//wait for callback triggered.
EXPECT_EQ(g_test_foregin_read_OK, 1); EXPECT_EQ(g_test_foregin_read_OK, 1);
} }
@@ -4497,7 +4500,7 @@ TEST_F(MaatCmdTest, UpdateBoolPlugin) {
EXPECT_GT(ret, 0); EXPECT_GT(ret, 0);
} }
sleep(WAIT_FOR_EFFECTIVE_S); sleep(WAIT_FOR_EFFECTIVE_S * 2);
int ex_data_counter = 0; int ex_data_counter = 0;
ret = maat_plugin_table_ex_schema_register(maat_instance, table_name, ret = maat_plugin_table_ex_schema_register(maat_instance, table_name,
@@ -4528,7 +4531,7 @@ TEST_F(MaatCmdTest, UpdateBoolPlugin) {
ret = maat_cmd_set_line(maat_instance, &line_rule); ret = maat_cmd_set_line(maat_instance, &line_rule);
EXPECT_GT(ret, 0); EXPECT_GT(ret, 0);
} }
sleep(WAIT_FOR_EFFECTIVE_S); sleep(WAIT_FOR_EFFECTIVE_S * 2);
memset(results, 0, sizeof(results)); memset(results, 0, sizeof(results));
ret = maat_bool_plugin_table_get_ex_data(maat_instance, table_id, items, 3, ret = maat_bool_plugin_table_get_ex_data(maat_instance, table_id, items, 3,
@@ -4607,7 +4610,7 @@ TEST_F(MaatCmdTest, GroupInMassCompiles) {
target_compile_id, 0, "null", 2, 0); target_compile_id, 0, "null", 2, 0);
EXPECT_EQ(ret, 1); EXPECT_EQ(ret, 1);
sleep(WAIT_FOR_EFFECTIVE_S); sleep(WAIT_FOR_EFFECTIVE_S * 2);
const char* http_url1 = "https://www.baidu.com/s?wd=tsg"; const char* http_url1 = "https://www.baidu.com/s?wd=tsg";
const char* http_url2 = "https://www.baidu.com/s?wd=zhengzhou&rsv_spt=1&rsv_iqid=0x8b4cae8100000560&issp=1&f=8&rsv_bp=1"; const char* http_url2 = "https://www.baidu.com/s?wd=zhengzhou&rsv_spt=1&rsv_iqid=0x8b4cae8100000560&issp=1&f=8&rsv_bp=1";
@@ -5014,7 +5017,7 @@ TEST_F(MaatCmdTest, GroupEdit) {
scan_app_id, scan_app_id, NULL, 0); scan_app_id, scan_app_id, NULL, 0);
EXPECT_EQ(ret, 1); EXPECT_EQ(ret, 1);
sleep(WAIT_FOR_EFFECTIVE_S); sleep(WAIT_FOR_EFFECTIVE_S * 2);
table_id = maat_get_table_id(maat_instance, ip_table_name); table_id = maat_get_table_id(maat_instance, ip_table_name);
ASSERT_GT(table_id, 0); ASSERT_GT(table_id, 0);
@@ -5086,7 +5089,7 @@ TEST_F(MaatCmdTest, CompileDelete_TSG6548) {
IPv4, "192.168.73.163", "192.168.73.180", 0, 65535, 0); IPv4, "192.168.73.163", "192.168.73.180", 0, 65535, 0);
EXPECT_EQ(ret, 1); EXPECT_EQ(ret, 1);
sleep(WAIT_FOR_EFFECTIVE_S); sleep(WAIT_FOR_EFFECTIVE_S * 2);
uint32_t ip_addr; uint32_t ip_addr;
inet_pton(AF_INET, "192.168.73.169", &ip_addr); inet_pton(AF_INET, "192.168.73.169", &ip_addr);

View File

@@ -100,6 +100,9 @@ static int write_config_to_redis(char *redis_ip, int redis_port, int redis_db,
redisReply *reply = maat_cmd_wrap_redis_command(c, "flushdb"); redisReply *reply = maat_cmd_wrap_redis_command(c, "flushdb");
if (NULL == reply) { if (NULL == reply) {
return -1; return -1;
} else {
freeReplyObject(reply);
reply = NULL;
} }
if (0 == access(json_iris_path, F_OK)) { if (0 == access(json_iris_path, F_OK)) {

View File

@@ -1,48 +0,0 @@
#include <limits.h>
#include "maat_utils.h"
#include "maat_garbage_collection.h"
#include <gtest/gtest.h>
struct user_info {
char user_name[NAME_MAX];
int user_id;
};
struct user_info *user_info_new(const char *user_name, int user_id)
{
struct user_info *info = ALLOC(struct user_info, 1);
memcpy(info->user_name, user_name, sizeof(info->user_name));
info->user_id = user_id;
return info;
}
void user_info_free(struct user_info *user_info)
{
free(user_info);
}
void garbage_user_info_free(void *user_info, void *arg)
{
struct user_info *info = (struct user_info *)user_info;
user_info_free(info);
}
TEST(garbage_collection, basic)
{
struct maat_garbage_bin *garbage_bin = maat_garbage_bin_new(2);
struct user_info *info = user_info_new("zhangsan", 101);
maat_garbage_bagging(garbage_bin, info, NULL, garbage_user_info_free);
maat_garbage_collect_routine(garbage_bin);
maat_garbage_bin_free(garbage_bin);
}
int main(int argc, char ** argv)
{
int ret=0;
::testing::InitGoogleTest(&argc, argv);
ret=RUN_ALL_TESTS();
return ret;
}

View File

@@ -168,6 +168,8 @@ TEST(redis_mode, maat_scan_string) {
redisReply *reply = maat_cmd_wrap_redis_command(c, "flushdb"); redisReply *reply = maat_cmd_wrap_redis_command(c, "flushdb");
EXPECT_TRUE(reply != NULL); EXPECT_TRUE(reply != NULL);
freeReplyObject(reply);
reply = NULL;
if (access(json_iris_path, F_OK) < 0) { if (access(json_iris_path, F_OK) < 0) {
char tmp_iris_path[128] = {0}; char tmp_iris_path[128] = {0};

View File

@@ -16,13 +16,13 @@ void data_free(void *user_ctx, void *data)
} }
TEST(rcu_hash_new, invalid_input_parameter) { TEST(rcu_hash_new, invalid_input_parameter) {
struct rcu_hash_table *htable = rcu_hash_new(NULL); struct rcu_hash_table *htable = rcu_hash_new(NULL, NULL);
EXPECT_TRUE(htable == NULL); EXPECT_TRUE(htable == NULL);
} }
TEST(rcu_hash_add_one_node, single_thread) { TEST(rcu_hash_add_one_node, single_thread) {
/* add one node to hash */ /* add one node to hash */
struct rcu_hash_table *htable = rcu_hash_new(data_free); struct rcu_hash_table *htable = rcu_hash_new(data_free, NULL);
EXPECT_TRUE(htable != NULL); EXPECT_TRUE(htable != NULL);
struct user_data *data = ALLOC(struct user_data, 1); struct user_data *data = ALLOC(struct user_data, 1);
@@ -69,7 +69,7 @@ TEST(rcu_hash_add_one_node, single_thread) {
TEST(rcu_hash_add_multi_node, single_thread) { TEST(rcu_hash_add_multi_node, single_thread) {
/* add multi node to hash */ /* add multi node to hash */
struct rcu_hash_table *htable = rcu_hash_new(data_free); struct rcu_hash_table *htable = rcu_hash_new(data_free, NULL);
EXPECT_TRUE(htable != NULL); EXPECT_TRUE(htable != NULL);
struct user_data *data0 = ALLOC(struct user_data, 1); struct user_data *data0 = ALLOC(struct user_data, 1);
@@ -163,7 +163,7 @@ TEST(rcu_hash_add_multi_node, single_thread) {
TEST(rcu_hash_del_one_node, single_thread) { TEST(rcu_hash_del_one_node, single_thread) {
/* case1: add and del before commit */ /* case1: add and del before commit */
struct rcu_hash_table *htable = rcu_hash_new(data_free); struct rcu_hash_table *htable = rcu_hash_new(data_free, NULL);
EXPECT_TRUE(htable != NULL); EXPECT_TRUE(htable != NULL);
struct user_data *data = ALLOC(struct user_data, 1); struct user_data *data = ALLOC(struct user_data, 1);
@@ -243,7 +243,7 @@ TEST(rcu_hash_del_one_node, single_thread) {
TEST(rcu_hash_del_multi_node, single_thread) { TEST(rcu_hash_del_multi_node, single_thread) {
/* case1: add and del before commit */ /* case1: add and del before commit */
struct rcu_hash_table *htable = rcu_hash_new(data_free); struct rcu_hash_table *htable = rcu_hash_new(data_free, NULL);
EXPECT_TRUE(htable != NULL); EXPECT_TRUE(htable != NULL);
struct user_data *data1 = ALLOC(struct user_data, 1); struct user_data *data1 = ALLOC(struct user_data, 1);
@@ -288,7 +288,7 @@ TEST(rcu_hash_del_multi_node, single_thread) {
} }
TEST(rcu_hash_add_with_same_key, single_thread) { TEST(rcu_hash_add_with_same_key, single_thread) {
struct rcu_hash_table *htable = rcu_hash_new(data_free); struct rcu_hash_table *htable = rcu_hash_new(data_free, NULL);
EXPECT_TRUE(htable != NULL); EXPECT_TRUE(htable != NULL);
char key[64] = "http_url"; char key[64] = "http_url";
@@ -327,7 +327,7 @@ TEST(rcu_hash_add_with_same_key, single_thread) {
} }
TEST(rcu_hash_del_with_same_key, single_thread) { TEST(rcu_hash_del_with_same_key, single_thread) {
struct rcu_hash_table *htable = rcu_hash_new(data_free); struct rcu_hash_table *htable = rcu_hash_new(data_free, NULL);
EXPECT_TRUE(htable != NULL); EXPECT_TRUE(htable != NULL);
char key[64] = "http_url"; char key[64] = "http_url";
@@ -372,7 +372,7 @@ TEST(rcu_hash_del_with_same_key, single_thread) {
} }
TEST(rcu_hash_modify_with_same_key, single_thread) { TEST(rcu_hash_modify_with_same_key, single_thread) {
struct rcu_hash_table *htable = rcu_hash_new(data_free); struct rcu_hash_table *htable = rcu_hash_new(data_free, NULL);
EXPECT_TRUE(htable != NULL); EXPECT_TRUE(htable != NULL);
char key[64] = "http_url"; char key[64] = "http_url";
@@ -457,95 +457,13 @@ TEST(global_rcu_hash_del, single_thread) {
res = rcu_hash_find(g_htable, key, key_len); res = rcu_hash_find(g_htable, key, key_len);
EXPECT_TRUE(res == NULL); EXPECT_TRUE(res == NULL);
} }
#define THREAD_NUM 10
struct thread_param {
int thread_id;
int test_count;
unsigned long long time_elapse_ms;
};
void *rcu_scan_thread(void *arg)
{
struct thread_param *param = (struct thread_param *)arg;
int hit_cnt = 0;
void **data_array = NULL;
sleep(2);
for (int index = 0; index < param->test_count; index++) {
size_t count = rcu_hash_list(g_htable, &data_array);
for (size_t i = 0; i < count; i++) {
struct user_data *user_data = (struct user_data *)data_array[i];
if (user_data != NULL && user_data->id == 101) {
hit_cnt++;
}
}
}
int *is_all_hit = ALLOC(int, 1);
*is_all_hit = (hit_cnt == param->test_count) ? 1 : 0;
printf("thread[%d]: hit_cnt:%d\n", param->thread_id, hit_cnt);
return is_all_hit;
}
void *rcu_update_thread(void *arg)
{
const char *key = "http_url";
size_t key_len = strlen(key);
for (int i = 0; i < 10; i++) {
rcu_hash_del(g_htable, key, key_len);
rcu_hash_commit(g_htable);
sleep(1);
struct user_data *data = ALLOC(struct user_data, 1);
data->id = 101;
char name[64] = "www.baidu.com";
memcpy(data->name, name, strlen(name));
rcu_hash_add(g_htable, key, key_len, data);
rcu_hash_commit(g_htable);
}
int *is_all_hit = ALLOC(int, 1);
*is_all_hit = 1;
return is_all_hit;
}
TEST(rcu_hash_update, multi_thread) {
pthread_t threads[THREAD_NUM + 1];
struct thread_param t_param[THREAD_NUM + 1];
for (int i = 0; i < THREAD_NUM + 1; i++) {
t_param[i].thread_id = i;
t_param[i].test_count = 1000 * 1000;
t_param[i].time_elapse_ms = 0;
if (i < THREAD_NUM) {
pthread_create(&threads[i], NULL, rcu_scan_thread, t_param+i);
} else {
pthread_create(&threads[i], NULL, rcu_update_thread, t_param+i);
}
}
int *is_all_hit = NULL;
unsigned long long time_elapse_ms = 0, scan_count = 0;
for (int i = 0; i < THREAD_NUM + 1; i++) {
pthread_join(threads[i], (void **)&is_all_hit);
time_elapse_ms += t_param[i].time_elapse_ms;
scan_count += t_param[i].test_count;
EXPECT_EQ(*is_all_hit, 1);
*is_all_hit = 0;
free(is_all_hit);
}
}
int main(int argc, char ** argv) int main(int argc, char ** argv)
{ {
int ret=0; int ret=0;
::testing::InitGoogleTest(&argc, argv); ::testing::InitGoogleTest(&argc, argv);
g_htable = rcu_hash_new(data_free); g_htable = rcu_hash_new(data_free, NULL);
ret=RUN_ALL_TESTS(); ret=RUN_ALL_TESTS();