2022-11-17 05:05:35 +08:00
|
|
|
/*
|
|
|
|
|
**********************************************************************************************
|
2023-05-15 07:24:36 +00:00
|
|
|
* File: maat_api.c
|
2022-11-17 05:05:35 +08:00
|
|
|
* Description: maat api entry
|
|
|
|
|
* Authors: Liu WenTan <liuwentan@geedgenetworks.com>
|
|
|
|
|
* Date: 2022-10-31
|
2023-05-15 07:24:36 +00:00
|
|
|
* Copyright: (c) Since 2022 Geedge Networks, Ltd. All rights reserved.
|
2022-11-17 05:05:35 +08:00
|
|
|
***********************************************************************************************
|
|
|
|
|
*/
|
|
|
|
|
|
2022-10-27 17:58:52 +08:00
|
|
|
#include <stdio.h>
|
2022-11-25 16:32:29 +08:00
|
|
|
#include <string.h>
|
2022-12-09 17:12:18 +08:00
|
|
|
#include <assert.h>
|
2023-04-04 09:31:20 +08:00
|
|
|
#include <unistd.h>
|
2023-02-21 11:27:18 +08:00
|
|
|
#include <linux/limits.h>
|
2022-10-27 17:58:52 +08:00
|
|
|
|
2022-12-09 17:12:18 +08:00
|
|
|
#include "maat_utils.h"
|
2022-12-03 22:23:41 +08:00
|
|
|
#include "json2iris.h"
|
2023-02-15 11:53:46 +08:00
|
|
|
#include "maat.h"
|
2022-11-17 05:05:35 +08:00
|
|
|
#include "maat_rule.h"
|
2022-11-25 16:32:29 +08:00
|
|
|
#include "maat_kv.h"
|
2022-12-03 22:23:41 +08:00
|
|
|
#include "maat_command.h"
|
2022-12-09 17:12:18 +08:00
|
|
|
#include "maat_ex_data.h"
|
2023-01-30 21:59:35 +08:00
|
|
|
#include "maat_table.h"
|
2022-11-25 16:32:29 +08:00
|
|
|
#include "maat_config_monitor.h"
|
2022-12-03 22:23:41 +08:00
|
|
|
#include "maat_redis_monitor.h"
|
2023-01-30 21:59:35 +08:00
|
|
|
#include "maat_compile.h"
|
2022-12-14 15:28:21 +08:00
|
|
|
#include "alignment.h"
|
2023-03-01 09:32:36 +08:00
|
|
|
#include "ip_matcher.h"
|
2023-05-09 17:45:43 +08:00
|
|
|
#include "adapter_hs.h"
|
2023-01-31 20:39:53 +08:00
|
|
|
#include "maat_garbage_collection.h"
|
|
|
|
|
#include "maat_group.h"
|
|
|
|
|
#include "maat_expr.h"
|
2023-02-07 11:25:31 +08:00
|
|
|
#include "maat_flag.h"
|
2023-02-15 11:53:46 +08:00
|
|
|
#include "maat_interval.h"
|
2023-01-31 20:39:53 +08:00
|
|
|
#include "maat_ip.h"
|
2023-01-30 21:59:35 +08:00
|
|
|
#include "maat_plugin.h"
|
|
|
|
|
#include "maat_ip_plugin.h"
|
2023-02-20 11:43:43 +08:00
|
|
|
#include "maat_fqdn_plugin.h"
|
2023-02-21 11:27:18 +08:00
|
|
|
#include "maat_bool_plugin.h"
|
2023-02-06 08:14:25 +08:00
|
|
|
#include "maat_virtual.h"
|
2023-04-20 15:34:56 +08:00
|
|
|
#include "maat_stat.h"
|
2023-05-17 14:34:34 +08:00
|
|
|
#include "uthash/utarray.h"
|
2022-11-17 05:05:35 +08:00
|
|
|
|
2022-12-09 17:12:18 +08:00
|
|
|
#define MODULE_MAAT_API module_name_str("maat.api")
|
|
|
|
|
|
2023-06-14 09:10:16 +00:00
|
|
|
enum district_flag {
|
|
|
|
|
DISTRICT_FLAG_UNSET,
|
|
|
|
|
DISTRICT_FLAG_SET
|
|
|
|
|
};
|
|
|
|
|
|
2023-02-15 11:53:46 +08:00
|
|
|
struct maat_stream {
|
2023-06-16 15:59:30 +08:00
|
|
|
struct maat *ref_maat_inst;
|
|
|
|
|
struct adapter_hs_stream *handle; //each physical table open one stream
|
2023-04-12 19:20:05 +08:00
|
|
|
long long last_full_version;
|
2023-04-13 14:56:35 +08:00
|
|
|
long long expr_rt_version;
|
2023-04-12 19:20:05 +08:00
|
|
|
struct log_handle *logger;
|
2023-02-24 17:29:38 +08:00
|
|
|
int thread_id;
|
2023-02-15 11:53:46 +08:00
|
|
|
int vtable_id;
|
2023-06-19 09:44:25 +00:00
|
|
|
int phy_table_id;
|
2023-02-15 11:53:46 +08:00
|
|
|
};
|
|
|
|
|
|
2022-11-17 05:05:35 +08:00
|
|
|
struct maat_options* maat_options_new(void)
|
|
|
|
|
{
|
|
|
|
|
struct maat_options *options = ALLOC(struct maat_options, 1);
|
|
|
|
|
|
2023-05-04 17:10:19 +08:00
|
|
|
options->nr_worker_thread = 1;
|
2022-11-25 16:32:29 +08:00
|
|
|
options->deferred_load_on = 0;
|
|
|
|
|
options->rule_effect_interval_ms = 60 * 1000;
|
|
|
|
|
options->rule_update_checking_interval_ms = 1 * 1000;
|
|
|
|
|
options->gc_timeout_ms = 10 * 1000;
|
|
|
|
|
options->input_mode = DATA_SOURCE_NONE;
|
2022-12-09 17:12:18 +08:00
|
|
|
options->log_level = 0;
|
2022-11-17 05:05:35 +08:00
|
|
|
|
|
|
|
|
return options;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-30 21:59:35 +08:00
|
|
|
void maat_options_free(struct maat_options *opts)
|
|
|
|
|
{
|
|
|
|
|
if (NULL == opts) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FREE(opts);
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-06 18:54:59 +08:00
|
|
|
int maat_options_set_caller_thread_number(struct maat_options *opts, size_t n_thread)
|
2022-11-17 05:05:35 +08:00
|
|
|
{
|
2023-03-15 11:36:54 +08:00
|
|
|
if (NULL == opts) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-04 17:10:19 +08:00
|
|
|
opts->nr_worker_thread = n_thread;
|
2022-11-17 05:05:35 +08:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-30 21:59:35 +08:00
|
|
|
int maat_options_set_accept_tags(struct maat_options *opts, const char *accept_tags)
|
|
|
|
|
{
|
2023-03-15 11:36:54 +08:00
|
|
|
if (NULL == opts || NULL == accept_tags) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2023-01-30 21:59:35 +08:00
|
|
|
|
2023-03-15 11:36:54 +08:00
|
|
|
opts->accept_tags = maat_strdup(accept_tags);
|
2023-01-30 21:59:35 +08:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-25 16:32:29 +08:00
|
|
|
int maat_options_set_rule_effect_interval_ms(struct maat_options *opts, int interval_ms)
|
|
|
|
|
{
|
2023-03-15 11:36:54 +08:00
|
|
|
if (NULL == opts || interval_ms < 0) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-25 16:32:29 +08:00
|
|
|
opts->rule_effect_interval_ms = interval_ms;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int maat_options_set_rule_update_checking_interval_ms(struct maat_options *opts, int interval_ms)
|
|
|
|
|
{
|
2023-03-15 11:36:54 +08:00
|
|
|
if (NULL == opts || interval_ms < 0) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2022-11-25 16:32:29 +08:00
|
|
|
|
2023-03-15 11:36:54 +08:00
|
|
|
opts->rule_update_checking_interval_ms = interval_ms;
|
2022-11-25 16:32:29 +08:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int maat_options_set_gc_timeout_ms(struct maat_options *opts, int interval_ms)
|
|
|
|
|
{
|
2023-03-15 11:36:54 +08:00
|
|
|
if (NULL == opts || interval_ms < 0) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2022-11-25 16:32:29 +08:00
|
|
|
|
2023-03-15 11:36:54 +08:00
|
|
|
opts->gc_timeout_ms = interval_ms;
|
2022-11-25 16:32:29 +08:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-16 11:59:46 +08:00
|
|
|
int maat_options_set_instance_name(struct maat_options *opts, const char *instance_name)
|
2022-12-09 17:12:18 +08:00
|
|
|
{
|
2023-03-16 11:59:46 +08:00
|
|
|
if (NULL == opts || NULL == instance_name ||
|
2023-06-16 15:59:30 +08:00
|
|
|
strlen(instance_name) > MAX_INSTANCE_NAME_LEN) {
|
2023-03-15 11:36:54 +08:00
|
|
|
return -1;
|
|
|
|
|
}
|
2022-12-09 17:12:18 +08:00
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
memcpy(opts->inst_name, instance_name, strlen(instance_name));
|
2022-12-09 17:12:18 +08:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-25 16:32:29 +08:00
|
|
|
int maat_options_set_deferred_load_on(struct maat_options *opts)
|
|
|
|
|
{
|
2023-03-15 11:36:54 +08:00
|
|
|
if (NULL == opts) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-25 16:32:29 +08:00
|
|
|
opts->deferred_load_on = 1;
|
2023-03-15 11:36:54 +08:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-30 10:00:38 +08:00
|
|
|
int maat_options_set_stat_on(struct maat_options *opts)
|
|
|
|
|
{
|
2023-04-20 15:34:56 +08:00
|
|
|
if (NULL == opts) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
opts->stat_on = 1;
|
2023-03-30 15:22:33 +08:00
|
|
|
return 0;
|
2023-03-30 10:00:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int maat_options_set_perf_on(struct maat_options *opts)
|
|
|
|
|
{
|
2023-04-20 15:34:56 +08:00
|
|
|
if (NULL == opts) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
opts->perf_on = 1;
|
2023-03-30 15:22:33 +08:00
|
|
|
return 0;
|
2023-03-30 10:00:38 +08:00
|
|
|
}
|
|
|
|
|
|
2023-03-16 11:59:46 +08:00
|
|
|
int maat_options_set_foreign_cont_dir(struct maat_options *opts, const char *dir)
|
2023-03-15 11:36:54 +08:00
|
|
|
{
|
2023-03-16 11:59:46 +08:00
|
|
|
if (NULL == opts || NULL == dir || strlen(dir) >= NAME_MAX) {
|
2023-03-15 11:36:54 +08:00
|
|
|
return -1;
|
|
|
|
|
}
|
2022-12-03 22:23:41 +08:00
|
|
|
|
2023-03-16 11:59:46 +08:00
|
|
|
memcpy(opts->foreign_cont_dir, dir, strlen(dir));
|
2022-11-25 16:32:29 +08:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-03 17:28:14 +08:00
|
|
|
int maat_options_set_iris(struct maat_options *opts, const char *full_directory,
|
|
|
|
|
const char *increment_directory)
|
2022-11-25 16:32:29 +08:00
|
|
|
{
|
2023-03-15 11:36:54 +08:00
|
|
|
if (NULL == opts || strlen(full_directory) >= NAME_MAX ||
|
|
|
|
|
strlen(increment_directory) >= NAME_MAX) {
|
2022-11-25 16:32:29 +08:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-06 18:54:59 +08:00
|
|
|
memcpy(opts->iris_ctx.full_idx_dir, full_directory, strlen(full_directory));
|
|
|
|
|
memcpy(opts->iris_ctx.inc_idx_dir, increment_directory, strlen(increment_directory));
|
2022-11-25 16:32:29 +08:00
|
|
|
opts->input_mode = DATA_SOURCE_IRIS_FILE;
|
2022-12-03 22:23:41 +08:00
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int maat_options_set_json_file(struct maat_options *opts, const char *json_filename)
|
|
|
|
|
{
|
|
|
|
|
strncpy(opts->json_ctx.json_file, json_filename, sizeof(opts->json_ctx.json_file));
|
|
|
|
|
opts->input_mode = DATA_SOURCE_JSON_FILE;
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-04 17:10:19 +08:00
|
|
|
int maat_options_set_json_file_gzip_flag(struct maat_options *opts, int flag)
|
2023-05-04 17:10:19 +08:00
|
|
|
{
|
2023-05-04 17:10:19 +08:00
|
|
|
if (NULL == opts || (flag != 0 && flag != 1)) {
|
2023-05-04 17:10:19 +08:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-04 17:10:19 +08:00
|
|
|
opts->maat_json_is_gzipped = flag;
|
2023-05-04 17:10:19 +08:00
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-19 09:44:25 +00:00
|
|
|
int maat_options_set_json_file_decrypt_key(struct maat_options *opts,
|
|
|
|
|
const char *decrypt_key)
|
2023-05-04 17:10:19 +08:00
|
|
|
{
|
|
|
|
|
if (NULL == opts || NULL == decrypt_key) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t str_len = MIN(sizeof(opts->decrypt_key), strlen(decrypt_key));
|
|
|
|
|
|
|
|
|
|
memcpy(opts->decrypt_key, decrypt_key, str_len);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-03 17:28:14 +08:00
|
|
|
int maat_options_set_redis(struct maat_options *opts, const char *redis_ip,
|
|
|
|
|
uint16_t redis_port, int redis_db)
|
2022-12-03 22:23:41 +08:00
|
|
|
{
|
|
|
|
|
memcpy(opts->redis_ctx.redis_ip, redis_ip, strlen(redis_ip));
|
|
|
|
|
opts->redis_ctx.redis_port = redis_port;
|
2023-01-06 18:54:59 +08:00
|
|
|
opts->redis_ctx.redis_db = redis_db;
|
|
|
|
|
opts->input_mode = DATA_SOURCE_REDIS;
|
2022-12-03 22:23:41 +08:00
|
|
|
|
2022-11-25 16:32:29 +08:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-20 15:34:56 +08:00
|
|
|
int maat_options_set_stat_file(struct maat_options *opts, const char *stat_filename)
|
|
|
|
|
{
|
|
|
|
|
size_t str_len = MIN(sizeof(opts->stat_file), strlen(stat_filename));
|
|
|
|
|
|
|
|
|
|
memcpy(opts->stat_file, stat_filename, str_len);
|
|
|
|
|
opts->stat_on = 1;
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-19 09:44:25 +00:00
|
|
|
int maat_options_set_logger(struct maat_options *opts, const char *log_path,
|
|
|
|
|
enum log_level level)
|
2022-12-09 17:12:18 +08:00
|
|
|
{
|
2023-03-16 15:16:42 +08:00
|
|
|
if (NULL == opts || NULL == log_path || strlen(log_path) >= PATH_MAX) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
memcpy(opts->log_path, log_path, strlen(log_path));
|
2023-03-28 15:41:24 +08:00
|
|
|
opts->log_level = level;
|
2022-12-09 17:12:18 +08:00
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-20 07:00:49 +00:00
|
|
|
static void _maat_free(struct maat *maat_inst)
|
2023-04-20 15:34:56 +08:00
|
|
|
{
|
2023-06-16 15:59:30 +08:00
|
|
|
if (NULL == maat_inst) {
|
2023-04-20 15:34:56 +08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
if (maat_inst->logger != NULL) {
|
|
|
|
|
log_handle_destroy(maat_inst->logger);
|
|
|
|
|
maat_inst->logger = NULL;
|
2023-04-20 15:34:56 +08:00
|
|
|
}
|
|
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
if (maat_inst->garbage_bin != NULL) {
|
|
|
|
|
maat_garbage_bin_free(maat_inst->garbage_bin);
|
|
|
|
|
maat_inst->garbage_bin = NULL;
|
2023-04-20 15:34:56 +08:00
|
|
|
}
|
|
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
if (maat_inst->stat != NULL) {
|
|
|
|
|
maat_stat_free(maat_inst->stat);
|
|
|
|
|
maat_inst->stat = NULL;
|
2023-04-20 15:34:56 +08:00
|
|
|
}
|
|
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
if (maat_inst->opts.accept_tags != NULL) {
|
|
|
|
|
FREE(maat_inst->opts.accept_tags);
|
2023-05-04 17:10:19 +08:00
|
|
|
}
|
|
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
pthread_mutex_destroy(&(maat_inst->background_update_mutex));
|
2023-04-20 15:34:56 +08:00
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
FREE(maat_inst);
|
2023-04-20 15:34:56 +08:00
|
|
|
}
|
|
|
|
|
|
2022-11-25 16:32:29 +08:00
|
|
|
struct maat *maat_new(struct maat_options *opts, const char *table_info_path)
|
2022-11-17 05:05:35 +08:00
|
|
|
{
|
2023-03-02 14:52:31 +08:00
|
|
|
if (NULL == opts || NULL == table_info_path) {
|
2022-11-17 05:05:35 +08:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
struct maat *maat_inst = ALLOC(struct maat, 1);
|
2023-05-04 17:10:19 +08:00
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
maat_inst->opts = *opts;
|
2023-05-04 17:10:19 +08:00
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
if (strlen(maat_inst->opts.log_path) != 0) {
|
|
|
|
|
maat_inst->logger = log_handle_create(maat_inst->opts.log_path,
|
|
|
|
|
maat_inst->opts.log_level);
|
2022-12-09 17:12:18 +08:00
|
|
|
} else {
|
2023-06-16 15:59:30 +08:00
|
|
|
char log_path[MAX_NAME_STR_LEN] = {0};
|
|
|
|
|
if (strlen(maat_inst->opts.inst_name) > 0) {
|
|
|
|
|
snprintf(log_path, sizeof(log_path), "%s.log", maat_inst->opts.inst_name);
|
2022-12-09 17:12:18 +08:00
|
|
|
} else {
|
|
|
|
|
snprintf(log_path, sizeof(log_path), "maat.log");
|
|
|
|
|
}
|
2023-06-16 15:59:30 +08:00
|
|
|
maat_inst->logger = log_handle_create(log_path, maat_inst->opts.log_level);
|
2022-12-09 17:12:18 +08:00
|
|
|
}
|
2023-01-30 21:59:35 +08:00
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
if (0 == strlen(maat_inst->opts.foreign_cont_dir)) {
|
|
|
|
|
snprintf(maat_inst->opts.foreign_cont_dir, sizeof(maat_inst->opts.foreign_cont_dir),
|
2023-03-30 15:22:33 +08:00
|
|
|
"%s_files", table_info_path);
|
|
|
|
|
}
|
2023-05-04 17:10:19 +08:00
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
system_cmd_mkdir(maat_inst->opts.foreign_cont_dir);
|
2023-03-30 15:22:33 +08:00
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
if (0 == strlen(maat_inst->opts.stat_file)) {
|
|
|
|
|
snprintf(maat_inst->opts.stat_file, sizeof(maat_inst->opts.stat_file), "maat.fs3");
|
2023-04-24 02:14:26 +00:00
|
|
|
}
|
|
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
snprintf(maat_inst->opts.decrypt_algo, sizeof(maat_inst->opts.decrypt_algo), "aes-256-cbc");
|
2022-11-25 16:32:29 +08:00
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
maat_inst->is_running = 0;
|
|
|
|
|
maat_inst->maat_version = 0;
|
|
|
|
|
maat_inst->last_full_version = 0;
|
2023-05-04 17:10:19 +08:00
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
int garbage_gc_timeout_s = (maat_inst->opts.rule_effect_interval_ms / 1000) +
|
|
|
|
|
(maat_inst->opts.gc_timeout_ms / 1000);
|
2023-05-04 17:10:19 +08:00
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
if (maat_inst->opts.input_mode != DATA_SOURCE_IRIS_FILE &&
|
|
|
|
|
maat_inst->opts.input_mode != DATA_SOURCE_JSON_FILE &&
|
|
|
|
|
maat_inst->opts.input_mode != DATA_SOURCE_REDIS) {
|
|
|
|
|
log_error(maat_inst->logger, MODULE_MAAT_API,
|
2023-05-04 17:10:19 +08:00
|
|
|
"[%s:%d] data source(%d) unsupported",
|
2023-06-16 15:59:30 +08:00
|
|
|
__FUNCTION__, __LINE__, maat_inst->opts.input_mode);
|
2023-05-04 17:10:19 +08:00
|
|
|
goto failed;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
maat_inst->garbage_bin = maat_garbage_bin_new(garbage_gc_timeout_s);
|
2023-06-19 09:44:25 +00:00
|
|
|
maat_inst->stat = maat_stat_new(maat_inst->opts.stat_file,
|
|
|
|
|
maat_inst->opts.nr_worker_thread,
|
|
|
|
|
maat_inst->logger);
|
2023-04-07 14:43:04 +08:00
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
pthread_mutex_init(&(maat_inst->background_update_mutex), NULL);
|
2022-12-14 15:28:21 +08:00
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
maat_inst->tbl_mgr = table_manager_create(table_info_path, maat_inst->opts.accept_tags,
|
2023-06-19 09:44:25 +00:00
|
|
|
maat_inst->garbage_bin, maat_inst->logger);
|
2023-06-16 15:59:30 +08:00
|
|
|
if (NULL == maat_inst->tbl_mgr) {
|
2023-02-07 11:25:31 +08:00
|
|
|
goto failed;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
if (0 == maat_inst->opts.deferred_load_on) {
|
|
|
|
|
maat_read_full_config(maat_inst);
|
2022-11-25 16:32:29 +08:00
|
|
|
}
|
2022-11-17 05:05:35 +08:00
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
if (1 == maat_inst->opts.stat_on) {
|
|
|
|
|
int ret = maat_stat_init(maat_inst->stat, maat_inst->tbl_mgr, maat_inst->garbage_bin);
|
2023-04-24 02:14:26 +00:00
|
|
|
if (ret < 0) {
|
2023-06-16 15:59:30 +08:00
|
|
|
log_error(maat_inst->logger, MODULE_MAAT_API,
|
2023-04-24 02:14:26 +00:00
|
|
|
"[%s:%d] maat_stat_init failed.", __FUNCTION__, __LINE__);
|
|
|
|
|
goto failed;
|
|
|
|
|
}
|
2023-04-20 15:34:56 +08:00
|
|
|
}
|
|
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
pthread_create(&(maat_inst->cfg_mon_thread), NULL, rule_monitor_loop, (void *)maat_inst);
|
2022-11-17 05:05:35 +08:00
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
return maat_inst;
|
2022-11-25 16:32:29 +08:00
|
|
|
failed:
|
2023-06-16 15:59:30 +08:00
|
|
|
_maat_free(maat_inst);
|
2022-11-17 05:05:35 +08:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
void maat_free(struct maat *maat_inst)
|
2022-11-17 05:05:35 +08:00
|
|
|
{
|
2023-06-16 15:59:30 +08:00
|
|
|
if (NULL == maat_inst) {
|
2022-12-05 23:21:18 +08:00
|
|
|
return;
|
|
|
|
|
}
|
2022-11-17 05:05:35 +08:00
|
|
|
|
2022-12-05 23:21:18 +08:00
|
|
|
void *ret = NULL;
|
2023-06-16 15:59:30 +08:00
|
|
|
while (0 == maat_inst->is_running) {
|
2023-04-04 09:31:20 +08:00
|
|
|
usleep(500 * 1000);
|
|
|
|
|
}
|
2023-05-23 17:50:53 +08:00
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
maat_inst->is_running = 0;
|
|
|
|
|
pthread_join(maat_inst->cfg_mon_thread, &ret);
|
2022-11-17 05:05:35 +08:00
|
|
|
}
|
|
|
|
|
|
2023-03-02 10:18:20 +08:00
|
|
|
int maat_helper_read_column(const char *table_line, int Nth_column,
|
|
|
|
|
size_t *column_offset, size_t *column_len)
|
|
|
|
|
{
|
|
|
|
|
return get_column_pos(table_line, Nth_column, column_offset, column_len);
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-09 17:45:43 +08:00
|
|
|
int maat_helper_verify_regex_expression(const char *regex_expr)
|
|
|
|
|
{
|
|
|
|
|
if (NULL == regex_expr) {
|
2023-05-11 11:21:46 +08:00
|
|
|
return 0;
|
2023-05-09 17:45:43 +08:00
|
|
|
}
|
|
|
|
|
|
2023-05-11 11:21:46 +08:00
|
|
|
int ret = adapter_hs_verify_regex_expression(regex_expr, NULL);
|
|
|
|
|
if (ret < 0) {
|
|
|
|
|
return 0;
|
|
|
|
|
} else {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2023-05-09 17:45:43 +08:00
|
|
|
}
|
|
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
int maat_get_table_id(struct maat *maat_inst, const char *table_name)
|
2022-11-25 16:32:29 +08:00
|
|
|
{
|
|
|
|
|
int table_id = -1;
|
|
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
struct table_manager *table_mgr = maat_inst->tbl_mgr;
|
2023-01-30 21:59:35 +08:00
|
|
|
table_id = table_manager_get_table_id(table_mgr, table_name);
|
2022-11-25 16:32:29 +08:00
|
|
|
|
|
|
|
|
return table_id;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-20 17:34:46 +08:00
|
|
|
static inline void maat_runtime_ref_inc(struct maat_runtime *maat_rt,
|
|
|
|
|
int thread_id)
|
2022-12-14 15:28:21 +08:00
|
|
|
{
|
2023-06-20 17:34:46 +08:00
|
|
|
if (NULL == maat_rt) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-14 15:28:21 +08:00
|
|
|
alignment_int64_array_add(maat_rt->ref_cnt, thread_id, 1);
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-20 17:34:46 +08:00
|
|
|
static inline void maat_runtime_ref_dec(struct maat_runtime *maat_rt,
|
|
|
|
|
int thread_id)
|
2022-12-14 15:28:21 +08:00
|
|
|
{
|
2023-06-20 17:34:46 +08:00
|
|
|
if (NULL == maat_rt) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-14 15:28:21 +08:00
|
|
|
alignment_int64_array_add(maat_rt->ref_cnt, thread_id, -1);
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-30 21:59:35 +08:00
|
|
|
/* must be plugin table */
|
2023-06-16 15:59:30 +08:00
|
|
|
int maat_table_callback_register(struct maat *maat_inst, int table_id,
|
2022-11-25 16:32:29 +08:00
|
|
|
maat_start_callback_t *start,
|
|
|
|
|
maat_update_callback_t *update,
|
|
|
|
|
maat_finish_callback_t *finish,
|
|
|
|
|
void *u_para)
|
|
|
|
|
{
|
|
|
|
|
int ret = -1;
|
|
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
pthread_mutex_lock(&(maat_inst->background_update_mutex));
|
|
|
|
|
void *schema = table_manager_get_schema(maat_inst->tbl_mgr, table_id);
|
2023-05-23 17:50:53 +08:00
|
|
|
if (NULL == schema) {
|
2023-06-16 15:59:30 +08:00
|
|
|
pthread_mutex_unlock(&(maat_inst->background_update_mutex));
|
|
|
|
|
log_error(maat_inst->logger, MODULE_MAAT_API,
|
2023-05-23 17:50:53 +08:00
|
|
|
"[%s:%d] table(table_id:%d) schema is NULL, register callback failed",
|
|
|
|
|
__FUNCTION__, __LINE__, table_id);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-30 21:59:35 +08:00
|
|
|
ret = plugin_table_add_callback(schema, table_id, start, update, finish,
|
2023-06-16 15:59:30 +08:00
|
|
|
u_para, maat_inst->logger);
|
2022-11-25 16:32:29 +08:00
|
|
|
if (ret < 0) {
|
2023-06-16 15:59:30 +08:00
|
|
|
pthread_mutex_unlock(&(maat_inst->background_update_mutex));
|
2022-11-25 16:32:29 +08:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
if (!maat_inst->maat_rt) {
|
|
|
|
|
pthread_mutex_unlock(&(maat_inst->background_update_mutex));
|
2022-11-25 16:32:29 +08:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
void *runtime = table_manager_get_runtime(maat_inst->tbl_mgr, table_id);
|
2023-05-23 17:50:53 +08:00
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
enum table_type table_type = table_manager_get_table_type(maat_inst->tbl_mgr, table_id);
|
2023-05-23 17:50:53 +08:00
|
|
|
if (table_type != TABLE_TYPE_PLUGIN) {
|
2023-06-16 15:59:30 +08:00
|
|
|
pthread_mutex_unlock(&(maat_inst->background_update_mutex));
|
|
|
|
|
log_error(maat_inst->logger, MODULE_MAAT_API,
|
2023-05-23 17:50:53 +08:00
|
|
|
"[%s:%d] table type:%d illegal", __FUNCTION__, __LINE__, table_type);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2023-01-30 21:59:35 +08:00
|
|
|
|
2023-05-23 17:50:53 +08:00
|
|
|
size_t row_cnt = plugin_runtime_cached_row_count(runtime);
|
2023-01-30 21:59:35 +08:00
|
|
|
if (row_cnt > 0) {
|
2022-11-25 16:32:29 +08:00
|
|
|
if (start != NULL) {
|
2023-02-03 17:28:14 +08:00
|
|
|
start(MAAT_UPDATE_TYPE_FULL, u_para);
|
2022-11-25 16:32:29 +08:00
|
|
|
}
|
|
|
|
|
|
2023-01-31 20:39:53 +08:00
|
|
|
for (size_t i = 0; i < row_cnt; i++) {
|
2023-02-21 11:27:18 +08:00
|
|
|
const char *line = plugin_runtime_cached_row_get(runtime, i);
|
2022-11-25 16:32:29 +08:00
|
|
|
if (NULL == line) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
update(table_id, line, u_para);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (finish != NULL) {
|
|
|
|
|
finish(u_para);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
pthread_mutex_unlock(&(maat_inst->background_update_mutex));
|
2022-11-25 16:32:29 +08:00
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-20 07:00:49 +00:00
|
|
|
static int compile_table_ex_schema_register(struct maat *maat_inst, int table_id,
|
|
|
|
|
maat_ex_new_func_t *new_func,
|
|
|
|
|
maat_ex_free_func_t *free_func,
|
|
|
|
|
maat_ex_dup_func_t *dup_func,
|
|
|
|
|
long argl, void *argp)
|
2023-02-09 22:13:15 +08:00
|
|
|
{
|
2023-06-16 15:59:30 +08:00
|
|
|
void *schema = table_manager_get_schema(maat_inst->tbl_mgr, table_id);
|
2023-03-01 13:12:22 +08:00
|
|
|
assert(schema != NULL);
|
2023-02-09 22:13:15 +08:00
|
|
|
|
2023-04-05 21:09:19 +08:00
|
|
|
int ret = compile_table_set_ex_data_schema((struct compile_schema *)schema, table_id,
|
|
|
|
|
new_func, free_func, dup_func,
|
|
|
|
|
argl, argp);
|
2023-02-23 11:37:02 +08:00
|
|
|
if (ret < 0) {
|
2023-02-21 11:27:18 +08:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
if (maat_inst->maat_rt != NULL) {
|
|
|
|
|
void *runtime = table_manager_get_runtime(maat_inst->tbl_mgr, table_id);
|
2023-03-01 13:12:22 +08:00
|
|
|
assert(runtime != NULL);
|
|
|
|
|
compile_runtime_ex_data_iterate((struct compile_runtime *)runtime,
|
|
|
|
|
(struct compile_schema *)schema);
|
2023-02-21 11:27:18 +08:00
|
|
|
}
|
|
|
|
|
|
2023-02-23 11:37:02 +08:00
|
|
|
return 0;
|
2023-02-09 22:13:15 +08:00
|
|
|
}
|
|
|
|
|
|
2023-06-20 07:00:49 +00:00
|
|
|
static int generic_plugin_table_set_ex_schema(struct table_manager *tbl_mgr, int table_id,
|
|
|
|
|
maat_ex_new_func_t *new_func,
|
|
|
|
|
maat_ex_free_func_t *free_func,
|
|
|
|
|
maat_ex_dup_func_t *dup_func,
|
|
|
|
|
long argl, void *argp,
|
|
|
|
|
struct log_handle *logger)
|
2023-01-30 21:59:35 +08:00
|
|
|
{
|
|
|
|
|
void *schema = table_manager_get_schema(tbl_mgr, table_id);
|
|
|
|
|
if (NULL == schema) {
|
2023-01-31 20:39:53 +08:00
|
|
|
log_error(logger, MODULE_MAAT_API,
|
2023-06-19 09:44:25 +00:00
|
|
|
"[%s:%d], table(table_id:%d) is not registered, can't "
|
|
|
|
|
"register ex_container_schema", __FUNCTION__, __LINE__, table_id);
|
2023-01-30 21:59:35 +08:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-05 21:09:19 +08:00
|
|
|
int ret = -1;
|
2023-01-30 21:59:35 +08:00
|
|
|
enum table_type table_type = table_manager_get_table_type(tbl_mgr, table_id);
|
|
|
|
|
switch (table_type) {
|
|
|
|
|
case TABLE_TYPE_PLUGIN:
|
2023-06-19 09:44:25 +00:00
|
|
|
ret = plugin_table_set_ex_container_schema(schema, table_id,
|
|
|
|
|
new_func, free_func, dup_func,
|
|
|
|
|
free, argl, argp);
|
2023-01-30 21:59:35 +08:00
|
|
|
break;
|
|
|
|
|
case TABLE_TYPE_IP_PLUGIN:
|
2023-06-19 09:44:25 +00:00
|
|
|
ret = ip_plugin_table_set_ex_container_schema(schema, table_id,
|
|
|
|
|
new_func, free_func, dup_func,
|
|
|
|
|
free, argl, argp);
|
2023-01-30 21:59:35 +08:00
|
|
|
break;
|
2023-02-21 11:27:18 +08:00
|
|
|
case TABLE_TYPE_FQDN_PLUGIN:
|
2023-06-19 09:44:25 +00:00
|
|
|
ret = fqdn_plugin_table_set_ex_container_schema(schema, table_id,
|
|
|
|
|
new_func, free_func, dup_func,
|
|
|
|
|
(void (*)(void *))fqdn_rule_free,
|
2023-05-23 03:23:39 +00:00
|
|
|
argl, argp);
|
2023-02-21 11:27:18 +08:00
|
|
|
break;
|
|
|
|
|
case TABLE_TYPE_BOOL_PLUGIN:
|
2023-06-19 09:44:25 +00:00
|
|
|
ret = bool_plugin_table_set_ex_container_schema(schema, table_id,
|
|
|
|
|
new_func, free_func, dup_func,
|
|
|
|
|
free, argl, argp);
|
2023-02-21 11:27:18 +08:00
|
|
|
break;
|
2023-01-30 21:59:35 +08:00
|
|
|
default:
|
2023-03-01 13:12:22 +08:00
|
|
|
log_error(logger, MODULE_MAAT_API,
|
2023-03-02 14:52:31 +08:00
|
|
|
"[%s:%d], table(table_id:%d) is not plugin table, can't set ex schema",
|
|
|
|
|
__FUNCTION__, __LINE__, table_id);
|
2023-03-01 13:12:22 +08:00
|
|
|
return -1;
|
2023-01-30 21:59:35 +08:00
|
|
|
}
|
|
|
|
|
|
2023-04-05 21:09:19 +08:00
|
|
|
return ret;
|
2023-01-30 21:59:35 +08:00
|
|
|
}
|
|
|
|
|
|
2023-06-20 07:00:49 +00:00
|
|
|
static void plugin_runtime_commit_ex_schema(void *runtime, void *schema,
|
|
|
|
|
const char *table_name,
|
|
|
|
|
int valid_column)
|
2023-01-30 21:59:35 +08:00
|
|
|
{
|
2023-06-19 09:44:25 +00:00
|
|
|
struct ex_container_schema *container_schema = NULL;
|
|
|
|
|
struct ex_data_runtime *ex_data_rt = NULL;
|
|
|
|
|
|
|
|
|
|
container_schema = plugin_table_get_ex_container_schema(schema);
|
|
|
|
|
ex_data_rt = plugin_runtime_get_ex_data_rt(runtime);
|
2023-05-04 17:10:19 +08:00
|
|
|
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);
|
|
|
|
|
for (size_t i = 0; i < n_cached_row; i++) {
|
|
|
|
|
const char *row = ex_data_runtime_cached_row_get(ex_data_rt, i);
|
|
|
|
|
plugin_runtime_update(runtime, schema, table_name, row, valid_column);
|
2023-03-27 15:52:47 +08:00
|
|
|
}
|
|
|
|
|
|
2023-05-04 17:10:19 +08:00
|
|
|
ex_data_runtime_clear_row_cache(ex_data_rt);
|
|
|
|
|
plugin_runtime_commit(runtime, table_name, 0);
|
|
|
|
|
}
|
2023-01-30 21:59:35 +08:00
|
|
|
|
2023-06-20 07:00:49 +00:00
|
|
|
static void ip_plugin_runtime_commit_ex_schema(void *runtime, void *schema,
|
|
|
|
|
const char *table_name,
|
|
|
|
|
int valid_column)
|
2023-05-04 17:10:19 +08:00
|
|
|
{
|
2023-06-19 09:44:25 +00:00
|
|
|
struct ex_container_schema *container_schema = NULL;
|
|
|
|
|
struct ex_data_runtime *ex_data_rt = NULL;
|
|
|
|
|
|
|
|
|
|
container_schema = ip_plugin_table_get_ex_container_schema(schema);
|
|
|
|
|
ex_data_rt = ip_plugin_runtime_get_ex_data_rt(runtime);
|
2023-05-04 17:10:19 +08:00
|
|
|
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);
|
|
|
|
|
for (size_t i = 0; i < n_cached_row; i++) {
|
|
|
|
|
const char *row = ex_data_runtime_cached_row_get(ex_data_rt, i);
|
|
|
|
|
ip_plugin_runtime_update(runtime, schema, table_name, row, valid_column);
|
2023-01-30 21:59:35 +08:00
|
|
|
}
|
2023-05-04 17:10:19 +08:00
|
|
|
|
|
|
|
|
ex_data_runtime_clear_row_cache(ex_data_rt);
|
|
|
|
|
ip_plugin_runtime_commit(runtime, table_name, 0);
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-20 07:00:49 +00:00
|
|
|
static void fqdn_plugin_runtime_commit_ex_schema(void *runtime, void *schema,
|
|
|
|
|
const char *table_name,
|
|
|
|
|
int valid_column)
|
2023-05-04 17:10:19 +08:00
|
|
|
{
|
2023-06-19 09:44:25 +00:00
|
|
|
struct ex_container_schema *container_schema = NULL;
|
|
|
|
|
struct ex_data_runtime *ex_data_rt = NULL;
|
|
|
|
|
|
|
|
|
|
container_schema = fqdn_plugin_table_get_ex_container_schema(schema);
|
|
|
|
|
ex_data_rt = fqdn_plugin_runtime_get_ex_data_rt(runtime);
|
2023-05-04 17:10:19 +08:00
|
|
|
ex_data_runtime_set_ex_container_schema(ex_data_rt, container_schema);
|
2023-01-30 21:59:35 +08:00
|
|
|
|
|
|
|
|
size_t n_cached_row = ex_data_runtime_cached_row_count(ex_data_rt);
|
|
|
|
|
for (size_t i = 0; i < n_cached_row; i++) {
|
|
|
|
|
const char *row = ex_data_runtime_cached_row_get(ex_data_rt, i);
|
2023-05-04 17:10:19 +08:00
|
|
|
fqdn_plugin_runtime_update(runtime, schema, table_name, row, valid_column);
|
2023-01-30 21:59:35 +08:00
|
|
|
}
|
2023-05-04 17:10:19 +08:00
|
|
|
|
2023-01-30 21:59:35 +08:00
|
|
|
ex_data_runtime_clear_row_cache(ex_data_rt);
|
2023-05-04 17:10:19 +08:00
|
|
|
fqdn_plugin_runtime_commit(runtime, table_name, 0);
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-20 07:00:49 +00:00
|
|
|
static void bool_plugin_runtime_commit_ex_schema(void *runtime, void *schema,
|
|
|
|
|
const char *table_name,
|
|
|
|
|
int valid_column)
|
2023-05-04 17:10:19 +08:00
|
|
|
{
|
2023-06-19 09:44:25 +00:00
|
|
|
struct ex_container_schema *container_schema = NULL;
|
|
|
|
|
struct ex_data_runtime *ex_data_rt = NULL;
|
|
|
|
|
|
|
|
|
|
container_schema = bool_plugin_table_get_ex_container_schema(schema);
|
|
|
|
|
ex_data_rt = bool_plugin_runtime_get_ex_data_rt(runtime);
|
2023-05-04 17:10:19 +08:00
|
|
|
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);
|
|
|
|
|
for (size_t i = 0; i < n_cached_row; i++) {
|
|
|
|
|
const char *row = ex_data_runtime_cached_row_get(ex_data_rt, i);
|
|
|
|
|
bool_plugin_runtime_update(runtime, schema, table_name, row, valid_column);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ex_data_runtime_clear_row_cache(ex_data_rt);
|
|
|
|
|
bool_plugin_runtime_commit(runtime, table_name, 0);
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-20 07:00:49 +00:00
|
|
|
static int generic_plugin_runtime_commit_ex_schema(void *runtime, void *schema,
|
|
|
|
|
const char *table_name,
|
|
|
|
|
enum table_type table_type,
|
|
|
|
|
int valid_column,
|
|
|
|
|
struct log_handle *logger)
|
2023-05-04 17:10:19 +08:00
|
|
|
{
|
|
|
|
|
if (NULL == runtime || NULL == schema || valid_column < 0) {
|
|
|
|
|
log_error(logger, MODULE_MAAT_API,
|
|
|
|
|
"[%s:%d] input parameter invalid, runtime:%p, schema:%p, valid_column:%d",
|
|
|
|
|
__FUNCTION__, __LINE__, runtime, schema, valid_column);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2023-01-30 21:59:35 +08:00
|
|
|
|
|
|
|
|
switch (table_type) {
|
|
|
|
|
case TABLE_TYPE_PLUGIN:
|
2023-05-04 17:10:19 +08:00
|
|
|
plugin_runtime_commit_ex_schema(runtime, schema, table_name, valid_column);
|
2023-01-30 21:59:35 +08:00
|
|
|
break;
|
|
|
|
|
case TABLE_TYPE_IP_PLUGIN:
|
2023-05-04 17:10:19 +08:00
|
|
|
ip_plugin_runtime_commit_ex_schema(runtime, schema, table_name, valid_column);
|
2023-02-21 11:27:18 +08:00
|
|
|
break;
|
|
|
|
|
case TABLE_TYPE_FQDN_PLUGIN:
|
2023-05-04 17:10:19 +08:00
|
|
|
fqdn_plugin_runtime_commit_ex_schema(runtime, schema, table_name, valid_column);
|
2023-02-21 11:27:18 +08:00
|
|
|
break;
|
|
|
|
|
case TABLE_TYPE_BOOL_PLUGIN:
|
2023-05-04 17:10:19 +08:00
|
|
|
bool_plugin_runtime_commit_ex_schema(runtime, schema, table_name, valid_column);
|
2023-01-30 21:59:35 +08:00
|
|
|
break;
|
|
|
|
|
default:
|
2023-05-04 17:10:19 +08:00
|
|
|
log_error(logger, MODULE_MAAT_API,
|
|
|
|
|
"[%s:%d] table_type:%d invalid", __FUNCTION__, __LINE__, table_type);
|
|
|
|
|
return -1;
|
2023-01-30 21:59:35 +08:00
|
|
|
}
|
2023-04-05 21:09:19 +08:00
|
|
|
|
|
|
|
|
return 0;
|
2023-01-30 21:59:35 +08:00
|
|
|
}
|
|
|
|
|
|
2023-06-20 07:00:49 +00:00
|
|
|
static int generic_plugin_table_ex_schema_register(struct maat *maat_inst,
|
|
|
|
|
const char *table_name, int table_id,
|
|
|
|
|
maat_ex_new_func_t *new_func,
|
|
|
|
|
maat_ex_free_func_t *free_func,
|
|
|
|
|
maat_ex_dup_func_t *dup_func,
|
|
|
|
|
long argl, void *argp)
|
2022-11-17 05:05:35 +08:00
|
|
|
{
|
2023-06-16 15:59:30 +08:00
|
|
|
int ret = generic_plugin_table_set_ex_schema(maat_inst->tbl_mgr, table_id,
|
2023-03-01 13:12:22 +08:00
|
|
|
new_func, free_func, dup_func,
|
2023-06-16 15:59:30 +08:00
|
|
|
argl, argp, maat_inst->logger);
|
2022-11-25 16:32:29 +08:00
|
|
|
if (ret < 0) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-03 17:28:14 +08:00
|
|
|
enum table_type table_type = TABLE_TYPE_INVALID;
|
|
|
|
|
int valid_column = -1;
|
2023-06-16 15:59:30 +08:00
|
|
|
if (maat_inst->maat_rt != NULL) {
|
|
|
|
|
void *runtime = table_manager_get_runtime(maat_inst->tbl_mgr, table_id);
|
|
|
|
|
void *schema = table_manager_get_schema(maat_inst->tbl_mgr, table_id);
|
2023-02-03 17:28:14 +08:00
|
|
|
assert(runtime != NULL && schema != NULL);
|
|
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
table_type = table_manager_get_table_type(maat_inst->tbl_mgr, table_id);
|
|
|
|
|
valid_column = table_manager_get_valid_column(maat_inst->tbl_mgr, table_id);
|
2023-03-27 15:52:47 +08:00
|
|
|
if (table_type == TABLE_TYPE_INVALID || valid_column < 0) {
|
2023-06-16 15:59:30 +08:00
|
|
|
log_error(maat_inst->logger, MODULE_MAAT_API,
|
2023-05-04 17:10:19 +08:00
|
|
|
"[%s:%d] table_type:%d or valid_column:%d invalid",
|
|
|
|
|
__FUNCTION__, __LINE__, table_type, valid_column);
|
2023-03-27 15:52:47 +08:00
|
|
|
return -1;
|
|
|
|
|
}
|
2023-03-28 14:07:44 +08:00
|
|
|
|
2023-05-04 17:10:19 +08:00
|
|
|
ret = generic_plugin_runtime_commit_ex_schema(runtime, schema, table_name,
|
|
|
|
|
table_type, valid_column,
|
2023-06-16 15:59:30 +08:00
|
|
|
maat_inst->logger);
|
2022-11-25 16:32:29 +08:00
|
|
|
}
|
|
|
|
|
|
2023-04-05 21:09:19 +08:00
|
|
|
return ret;
|
2022-11-17 05:05:35 +08:00
|
|
|
}
|
|
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
int maat_plugin_table_ex_schema_register(struct maat *maat_inst,
|
2023-03-29 22:25:14 +08:00
|
|
|
const char *table_name,
|
2023-03-01 13:12:22 +08:00
|
|
|
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)
|
2023-02-22 15:08:52 +08:00
|
|
|
{
|
2023-06-16 15:59:30 +08:00
|
|
|
if (NULL == maat_inst || NULL == table_name) {
|
|
|
|
|
log_error(maat_inst->logger, MODULE_MAAT_API,
|
2023-03-29 22:25:14 +08:00
|
|
|
"[%s:%d] [table:%s] ex_schema register input parameter is invalid.",
|
|
|
|
|
__FUNCTION__, __LINE__, table_name);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
int table_id = maat_get_table_id(maat_inst, table_name);
|
2023-03-29 22:25:14 +08:00
|
|
|
if (table_id < 0) {
|
2023-06-16 15:59:30 +08:00
|
|
|
log_error(maat_inst->logger, MODULE_MAAT_API,
|
2023-05-04 17:10:19 +08:00
|
|
|
"[%s:%d] [table:%s] is not registered before.",
|
|
|
|
|
__FUNCTION__, __LINE__, table_name);
|
2023-03-01 13:12:22 +08:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
pthread_mutex_lock(&(maat_inst->background_update_mutex));
|
2023-02-22 15:08:52 +08:00
|
|
|
|
2023-03-01 13:12:22 +08:00
|
|
|
int ret = -1;
|
2023-06-16 15:59:30 +08:00
|
|
|
enum table_type table_type = table_manager_get_table_type(maat_inst->tbl_mgr,
|
2023-06-08 07:32:41 +00:00
|
|
|
table_id);
|
2023-03-01 13:12:22 +08:00
|
|
|
if (TABLE_TYPE_COMPILE == table_type) {
|
2023-06-16 15:59:30 +08:00
|
|
|
ret = compile_table_ex_schema_register(maat_inst, table_id, new_func,
|
2023-06-08 07:32:41 +00:00
|
|
|
free_func, dup_func, argl, argp);
|
2023-03-01 13:12:22 +08:00
|
|
|
} else {
|
2023-06-16 15:59:30 +08:00
|
|
|
ret = generic_plugin_table_ex_schema_register(maat_inst, table_name,
|
2023-06-08 07:32:41 +00:00
|
|
|
table_id, new_func, free_func,
|
|
|
|
|
dup_func, argl, argp);
|
2023-03-01 13:12:22 +08:00
|
|
|
}
|
|
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
pthread_mutex_unlock(&(maat_inst->background_update_mutex));
|
2023-03-01 13:12:22 +08:00
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
void *maat_plugin_table_get_ex_data(struct maat *maat_inst, int table_id,
|
2023-06-08 07:32:41 +00:00
|
|
|
const char *key, size_t key_len)
|
2023-03-01 13:12:22 +08:00
|
|
|
{
|
|
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
if (NULL == maat_inst || table_id < 0 || table_id >= MAX_TABLE_NUM
|
2023-03-02 14:52:31 +08:00
|
|
|
|| NULL == key) {
|
2023-02-22 15:08:52 +08:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
struct maat_runtime *maat_rt = maat_inst->maat_rt;
|
2023-02-22 15:08:52 +08:00
|
|
|
if (NULL == maat_rt) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-01 13:12:22 +08:00
|
|
|
void *runtime = table_manager_get_runtime(maat_rt->ref_tbl_mgr, table_id);
|
|
|
|
|
if (NULL == runtime) {
|
2023-02-22 15:08:52 +08:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-01 13:12:22 +08:00
|
|
|
void *schema = table_manager_get_schema(maat_rt->ref_tbl_mgr, table_id);
|
|
|
|
|
if (NULL == schema) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void *ret = NULL;
|
2023-06-16 15:59:30 +08:00
|
|
|
enum table_type table_type = table_manager_get_table_type(maat_inst->tbl_mgr,
|
2023-06-08 07:32:41 +00:00
|
|
|
table_id);
|
2023-03-01 13:12:22 +08:00
|
|
|
if (TABLE_TYPE_COMPILE == table_type) {
|
|
|
|
|
ret = compile_runtime_get_ex_data(runtime, schema, *(long long *)key);
|
|
|
|
|
} else if (TABLE_TYPE_PLUGIN == table_type) {
|
2023-06-08 07:32:41 +00:00
|
|
|
ret = plugin_runtime_get_ex_data(runtime, schema, key, key_len);
|
2023-03-01 13:12:22 +08:00
|
|
|
} else {
|
2023-02-22 15:08:52 +08:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-01 13:12:22 +08:00
|
|
|
return ret;
|
2023-02-22 15:08:52 +08:00
|
|
|
}
|
|
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
int maat_ip_plugin_table_get_ex_data(struct maat *maat_inst, int table_id,
|
2023-06-19 09:44:25 +00:00
|
|
|
const struct ip_addr *ip_addr,
|
|
|
|
|
void **ex_data_array, size_t n_ex_data)
|
2022-11-25 16:32:29 +08:00
|
|
|
{
|
2023-06-16 15:59:30 +08:00
|
|
|
if (NULL == maat_inst || table_id < 0 || table_id >= MAX_TABLE_NUM
|
2023-02-21 11:27:18 +08:00
|
|
|
|| NULL == ip_addr || NULL == ex_data_array || 0 == n_ex_data) {
|
2023-02-09 22:13:15 +08:00
|
|
|
return -1;
|
2022-11-25 16:32:29 +08:00
|
|
|
}
|
2023-02-20 10:57:40 +08:00
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
struct maat_runtime *maat_rt = maat_inst->maat_rt;
|
2023-02-20 10:57:40 +08:00
|
|
|
if (NULL == maat_rt) {
|
|
|
|
|
return -1;
|
2022-12-09 17:12:18 +08:00
|
|
|
}
|
2023-02-20 10:57:40 +08:00
|
|
|
|
|
|
|
|
void *ip_plugin_rt = table_manager_get_runtime(maat_rt->ref_tbl_mgr, table_id);
|
|
|
|
|
if (NULL == ip_plugin_rt) {
|
2023-02-09 22:13:15 +08:00
|
|
|
return -1;
|
2022-12-09 17:12:18 +08:00
|
|
|
}
|
2022-11-25 16:32:29 +08:00
|
|
|
|
2023-04-05 21:09:19 +08:00
|
|
|
int n_hit_ex_data = ip_plugin_runtime_get_ex_data(ip_plugin_rt, ip_addr,
|
2023-03-16 09:55:35 +08:00
|
|
|
ex_data_array, n_ex_data);
|
2023-03-02 14:52:31 +08:00
|
|
|
if (n_hit_ex_data < 0) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-20 10:57:40 +08:00
|
|
|
return n_hit_ex_data;
|
2022-11-25 16:32:29 +08:00
|
|
|
}
|
|
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
int maat_fqdn_plugin_table_get_ex_data(struct maat *maat_inst, int table_id,
|
2023-02-21 11:27:18 +08:00
|
|
|
const char *fqdn, void **ex_data_array,
|
|
|
|
|
size_t n_ex_data)
|
2023-02-20 11:43:43 +08:00
|
|
|
{
|
2023-06-16 15:59:30 +08:00
|
|
|
if (NULL == maat_inst || table_id < 0 || table_id >= MAX_TABLE_NUM
|
2023-02-21 11:27:18 +08:00
|
|
|
|| NULL == fqdn || NULL == ex_data_array || 0 == n_ex_data) {
|
2023-02-20 11:43:43 +08:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
struct maat_runtime *maat_rt = maat_inst->maat_rt;
|
2023-02-20 11:43:43 +08:00
|
|
|
if (NULL == maat_rt) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void *fqdn_plugin_rt = table_manager_get_runtime(maat_rt->ref_tbl_mgr, table_id);
|
|
|
|
|
if (NULL == fqdn_plugin_rt) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-05 21:09:19 +08:00
|
|
|
int n_hit_ex_data = fqdn_plugin_runtime_get_ex_data(fqdn_plugin_rt, fqdn,
|
|
|
|
|
ex_data_array, n_ex_data);
|
2023-03-02 14:52:31 +08:00
|
|
|
if (n_hit_ex_data < 0) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-20 11:43:43 +08:00
|
|
|
return n_hit_ex_data;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
int maat_bool_plugin_table_get_ex_data(struct maat *maat_inst, int table_id,
|
2023-02-20 11:43:43 +08:00
|
|
|
unsigned long long *item_ids, size_t n_item,
|
|
|
|
|
void **ex_data_array, size_t n_ex_data)
|
|
|
|
|
{
|
2023-06-16 15:59:30 +08:00
|
|
|
if (NULL == maat_inst || table_id < 0 || table_id >= MAX_TABLE_NUM
|
2023-02-21 11:27:18 +08:00
|
|
|
|| NULL == item_ids || NULL == ex_data_array || 0 == n_ex_data) {
|
2023-02-20 11:43:43 +08:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
struct maat_runtime *maat_rt = maat_inst->maat_rt;
|
2023-02-20 11:43:43 +08:00
|
|
|
if (NULL == maat_rt) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void *bool_plugin_rt = table_manager_get_runtime(maat_rt->ref_tbl_mgr, table_id);
|
|
|
|
|
if (NULL == bool_plugin_rt) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-08 07:32:41 +00:00
|
|
|
int n_hit_ex_data = bool_plugin_runtime_get_ex_data(bool_plugin_rt, item_ids, n_item,
|
2023-04-05 21:09:19 +08:00
|
|
|
ex_data_array, n_ex_data);
|
2023-03-02 14:52:31 +08:00
|
|
|
if (n_hit_ex_data < 0) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-20 11:43:43 +08:00
|
|
|
return n_hit_ex_data;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-15 11:53:46 +08:00
|
|
|
static inline int scan_status_should_compile_NOT(struct maat_state *state)
|
2023-02-03 17:28:14 +08:00
|
|
|
{
|
2023-06-01 14:47:20 +08:00
|
|
|
if (state && (LAST_SCAN_SET == state->is_last_scan) && state->compile_state
|
2023-02-03 17:28:14 +08:00
|
|
|
&& maat_compile_state_has_NOT_clause(state->compile_state)) {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-20 07:00:49 +00:00
|
|
|
static size_t hit_group_to_compile(void *compile_runtime, long long *compile_ids,
|
|
|
|
|
size_t compile_ids_size, struct maat_state *state)
|
2023-01-06 18:54:59 +08:00
|
|
|
{
|
2023-02-09 22:13:15 +08:00
|
|
|
size_t n_hit_compile = compile_runtime_match((struct compile_runtime *)compile_runtime,
|
2023-03-22 20:40:36 +08:00
|
|
|
compile_ids, compile_ids_size, state);
|
2023-02-09 22:13:15 +08:00
|
|
|
return n_hit_compile;
|
2023-02-03 17:28:14 +08:00
|
|
|
}
|
2023-01-06 18:54:59 +08:00
|
|
|
|
2023-06-20 07:00:49 +00:00
|
|
|
static int flag_scan(struct table_manager *tbl_mgr, int thread_id, long long flag,
|
|
|
|
|
int phy_table_id, int vtable_id, struct maat_state *state)
|
2023-02-22 15:08:52 +08:00
|
|
|
{
|
2023-06-19 09:44:25 +00:00
|
|
|
enum table_type table_type = table_manager_get_table_type(tbl_mgr, phy_table_id);
|
2023-06-08 07:32:41 +00:00
|
|
|
if (table_type == TABLE_TYPE_FLAG_PLUS &&
|
|
|
|
|
DISTRICT_FLAG_UNSET == state->is_set_district) {
|
2023-03-22 11:10:00 +08:00
|
|
|
return -1;
|
|
|
|
|
}
|
2023-03-01 17:44:07 +08:00
|
|
|
|
2023-03-22 11:10:00 +08:00
|
|
|
if (table_type != TABLE_TYPE_FLAG && table_type != TABLE_TYPE_FLAG_PLUS) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2023-02-20 10:57:40 +08:00
|
|
|
|
2023-06-19 09:44:25 +00:00
|
|
|
void *flag_rt = table_manager_get_runtime(tbl_mgr, phy_table_id);
|
2023-03-22 11:10:00 +08:00
|
|
|
if (NULL == flag_rt) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2023-02-22 15:08:52 +08:00
|
|
|
|
2023-03-22 11:10:00 +08:00
|
|
|
int group_hit_cnt = flag_runtime_scan((struct flag_runtime *)flag_rt, thread_id,
|
2023-03-23 11:57:17 +08:00
|
|
|
flag, vtable_id, state);
|
2023-03-30 22:53:56 +08:00
|
|
|
if (group_hit_cnt <= 0) {
|
|
|
|
|
return group_hit_cnt;
|
2023-03-22 11:10:00 +08:00
|
|
|
}
|
2023-02-22 15:08:52 +08:00
|
|
|
|
2023-04-20 15:34:56 +08:00
|
|
|
flag_runtime_hit_inc((struct flag_runtime *)flag_rt, thread_id);
|
2023-02-22 15:08:52 +08:00
|
|
|
|
2023-03-22 11:10:00 +08:00
|
|
|
return group_hit_cnt;
|
2023-02-22 15:08:52 +08:00
|
|
|
}
|
|
|
|
|
|
2023-06-20 07:00:49 +00:00
|
|
|
static int interval_scan(struct table_manager *tbl_mgr, int thread_id, long long integer,
|
|
|
|
|
int phy_table_id, int vtable_id, struct maat_state *state)
|
2023-02-22 15:08:52 +08:00
|
|
|
{
|
|
|
|
|
|
2023-06-19 09:44:25 +00:00
|
|
|
enum table_type table_type = table_manager_get_table_type(tbl_mgr, phy_table_id);
|
2023-05-31 09:13:14 +00:00
|
|
|
if (table_type == TABLE_TYPE_INTERVAL_PLUS &&
|
|
|
|
|
DISTRICT_FLAG_UNSET == state->is_set_district) {
|
2023-03-22 11:10:00 +08:00
|
|
|
return -1;
|
|
|
|
|
}
|
2023-02-22 15:08:52 +08:00
|
|
|
|
2023-03-22 11:10:00 +08:00
|
|
|
if (table_type != TABLE_TYPE_INTERVAL && table_type != TABLE_TYPE_INTERVAL_PLUS) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2023-02-22 15:08:52 +08:00
|
|
|
|
2023-06-19 09:44:25 +00:00
|
|
|
void *interval_rt = table_manager_get_runtime(tbl_mgr, phy_table_id);
|
2023-03-22 11:10:00 +08:00
|
|
|
if (NULL == interval_rt) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2023-02-20 10:57:40 +08:00
|
|
|
|
2023-03-22 11:10:00 +08:00
|
|
|
int group_hit_cnt = interval_runtime_scan((struct interval_runtime *)interval_rt,
|
2023-03-23 11:57:17 +08:00
|
|
|
thread_id, integer, vtable_id, state);
|
2023-03-30 22:53:56 +08:00
|
|
|
if (group_hit_cnt <= 0) {
|
|
|
|
|
return group_hit_cnt;
|
2023-02-07 11:25:31 +08:00
|
|
|
}
|
|
|
|
|
|
2023-04-20 15:34:56 +08:00
|
|
|
interval_runtime_hit_inc((struct interval_runtime *)interval_rt, thread_id);
|
2023-03-22 11:10:00 +08:00
|
|
|
|
|
|
|
|
return group_hit_cnt;
|
2023-02-22 15:08:52 +08:00
|
|
|
}
|
|
|
|
|
|
2023-06-20 07:00:49 +00:00
|
|
|
static int ipv4_scan(struct table_manager *tbl_mgr, int thread_id, uint32_t ip_addr,
|
|
|
|
|
uint16_t port, int proto, int phy_table_id, int vtable_id,
|
|
|
|
|
struct maat_state *state)
|
2023-02-22 15:08:52 +08:00
|
|
|
{
|
|
|
|
|
|
2023-06-19 09:44:25 +00:00
|
|
|
enum table_type table_type = table_manager_get_table_type(tbl_mgr, phy_table_id);
|
2023-03-22 11:10:00 +08:00
|
|
|
if (table_type != TABLE_TYPE_IP_PLUS) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2023-02-22 15:08:52 +08:00
|
|
|
|
2023-06-19 09:44:25 +00:00
|
|
|
void *ip_rt = table_manager_get_runtime(tbl_mgr, phy_table_id);
|
2023-03-22 11:10:00 +08:00
|
|
|
if (NULL == ip_rt) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2023-02-22 15:08:52 +08:00
|
|
|
|
2023-03-22 11:10:00 +08:00
|
|
|
int group_hit_cnt = ip_runtime_scan((struct ip_runtime *)ip_rt, thread_id, IPv4,
|
2023-03-27 15:52:47 +08:00
|
|
|
(uint8_t *)&ip_addr, port, proto, vtable_id,
|
|
|
|
|
state);
|
2023-03-30 22:53:56 +08:00
|
|
|
if (group_hit_cnt <= 0) {
|
|
|
|
|
return group_hit_cnt;
|
2023-02-07 11:25:31 +08:00
|
|
|
}
|
|
|
|
|
|
2023-04-20 15:34:56 +08:00
|
|
|
ip_runtime_hit_inc((struct ip_runtime *)ip_rt, thread_id);
|
2023-03-30 22:53:56 +08:00
|
|
|
|
2023-03-22 11:10:00 +08:00
|
|
|
return group_hit_cnt;
|
2023-02-22 15:08:52 +08:00
|
|
|
}
|
|
|
|
|
|
2023-06-20 07:00:49 +00:00
|
|
|
static int ipv6_scan(struct table_manager *tbl_mgr, int thread_id,
|
|
|
|
|
uint8_t *ip_addr, uint16_t port, int proto,
|
|
|
|
|
int phy_table_id, int vtable_id, struct maat_state *state)
|
2023-02-22 15:08:52 +08:00
|
|
|
{
|
2023-02-07 11:25:31 +08:00
|
|
|
|
2023-06-19 09:44:25 +00:00
|
|
|
enum table_type table_type = table_manager_get_table_type(tbl_mgr, phy_table_id);
|
2023-03-22 11:10:00 +08:00
|
|
|
if (table_type != TABLE_TYPE_IP_PLUS) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2023-02-20 10:57:40 +08:00
|
|
|
|
2023-06-19 09:44:25 +00:00
|
|
|
void *ip_rt = table_manager_get_runtime(tbl_mgr, phy_table_id);
|
2023-03-22 11:10:00 +08:00
|
|
|
if (NULL == ip_rt) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2023-02-20 10:57:40 +08:00
|
|
|
|
2023-03-22 11:10:00 +08:00
|
|
|
int group_hit_cnt = ip_runtime_scan((struct ip_runtime *)ip_rt, thread_id, IPv6,
|
2023-03-27 15:52:47 +08:00
|
|
|
ip_addr, port, proto, vtable_id, state);
|
2023-03-30 22:53:56 +08:00
|
|
|
if (group_hit_cnt <= 0) {
|
|
|
|
|
return group_hit_cnt;
|
2023-03-22 11:10:00 +08:00
|
|
|
}
|
|
|
|
|
|
2023-04-20 15:34:56 +08:00
|
|
|
ip_runtime_hit_inc((struct ip_runtime *)ip_rt, thread_id);
|
2023-03-30 22:53:56 +08:00
|
|
|
|
2023-03-22 11:10:00 +08:00
|
|
|
return group_hit_cnt;
|
2023-02-22 15:08:52 +08:00
|
|
|
}
|
|
|
|
|
|
2023-06-20 07:00:49 +00:00
|
|
|
static int string_scan(struct table_manager *tbl_mgr, int thread_id,
|
|
|
|
|
const char *data, size_t data_len, int phy_table_id,
|
|
|
|
|
int vtable_id, struct maat_state *state)
|
2023-02-22 15:08:52 +08:00
|
|
|
{
|
2023-06-19 09:44:25 +00:00
|
|
|
enum table_type table_type = table_manager_get_table_type(tbl_mgr, phy_table_id);
|
2023-05-31 09:13:14 +00:00
|
|
|
if (table_type == TABLE_TYPE_EXPR_PLUS &&
|
|
|
|
|
DISTRICT_FLAG_UNSET == state->is_set_district) {
|
2023-03-22 11:10:00 +08:00
|
|
|
return -1;
|
|
|
|
|
}
|
2023-02-22 15:08:52 +08:00
|
|
|
|
2023-03-22 11:10:00 +08:00
|
|
|
if (table_type != TABLE_TYPE_EXPR && table_type != TABLE_TYPE_EXPR_PLUS) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2023-02-22 15:08:52 +08:00
|
|
|
|
2023-06-19 09:44:25 +00:00
|
|
|
void *expr_rt = table_manager_get_runtime(tbl_mgr, phy_table_id);
|
2023-03-22 11:10:00 +08:00
|
|
|
if (NULL == expr_rt) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2023-02-22 15:08:52 +08:00
|
|
|
|
2023-03-22 11:10:00 +08:00
|
|
|
int group_hit_cnt = expr_runtime_scan((struct expr_runtime *)expr_rt,
|
|
|
|
|
thread_id, data, data_len,
|
2023-03-23 11:57:17 +08:00
|
|
|
vtable_id, state);
|
2023-03-30 22:53:56 +08:00
|
|
|
if (group_hit_cnt <= 0) {
|
|
|
|
|
return group_hit_cnt;
|
2023-03-22 11:10:00 +08:00
|
|
|
}
|
2023-02-07 11:25:31 +08:00
|
|
|
|
2023-04-20 15:34:56 +08:00
|
|
|
expr_runtime_hit_inc((struct expr_runtime *)expr_rt, thread_id);
|
2023-02-07 11:25:31 +08:00
|
|
|
|
2023-03-22 11:10:00 +08:00
|
|
|
return group_hit_cnt;
|
2023-02-22 15:08:52 +08:00
|
|
|
}
|
|
|
|
|
|
2023-06-20 07:00:49 +00:00
|
|
|
static int expr_stream_scan(struct maat_stream *stream, const char *data,
|
|
|
|
|
size_t data_len, struct maat_state *state)
|
2023-02-24 17:29:38 +08:00
|
|
|
{
|
2023-03-22 11:10:00 +08:00
|
|
|
if (NULL == stream || NULL == data) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2023-02-24 17:29:38 +08:00
|
|
|
|
2023-06-19 09:44:25 +00:00
|
|
|
enum table_type table_type = TABLE_TYPE_INVALID;
|
2023-06-16 15:59:30 +08:00
|
|
|
struct table_manager *tbl_mgr = stream->ref_maat_inst->tbl_mgr;
|
2023-06-19 09:44:25 +00:00
|
|
|
table_type = table_manager_get_table_type(tbl_mgr, stream->phy_table_id);
|
2023-03-30 22:53:56 +08:00
|
|
|
if (table_type == TABLE_TYPE_EXPR_PLUS &&
|
|
|
|
|
DISTRICT_FLAG_UNSET == state->is_set_district) {
|
2023-03-22 11:10:00 +08:00
|
|
|
return -1;
|
|
|
|
|
}
|
2023-02-24 17:29:38 +08:00
|
|
|
|
2023-03-22 11:10:00 +08:00
|
|
|
if (table_type != TABLE_TYPE_EXPR && table_type != TABLE_TYPE_EXPR_PLUS) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2023-02-24 17:29:38 +08:00
|
|
|
|
2023-06-19 09:44:25 +00:00
|
|
|
void *expr_rt = table_manager_get_runtime(tbl_mgr, stream->phy_table_id);
|
2023-03-22 11:10:00 +08:00
|
|
|
if (NULL == expr_rt) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2023-02-24 17:29:38 +08:00
|
|
|
|
2023-06-19 09:44:25 +00:00
|
|
|
int group_hit_cnt = expr_runtime_stream_scan((struct expr_runtime *)expr_rt,
|
|
|
|
|
stream->handle, data, data_len,
|
|
|
|
|
stream->vtable_id, state);
|
2023-03-30 22:53:56 +08:00
|
|
|
if (group_hit_cnt <= 0) {
|
|
|
|
|
return group_hit_cnt;
|
2023-03-22 11:10:00 +08:00
|
|
|
}
|
2023-02-24 17:29:38 +08:00
|
|
|
|
2023-04-20 15:34:56 +08:00
|
|
|
expr_runtime_hit_inc((struct expr_runtime *)expr_rt, stream->thread_id);
|
2023-02-24 17:29:38 +08:00
|
|
|
|
2023-03-22 11:10:00 +08:00
|
|
|
return group_hit_cnt;
|
2023-02-24 17:29:38 +08:00
|
|
|
}
|
|
|
|
|
|
2023-06-20 07:00:49 +00:00
|
|
|
static size_t group_to_compile(struct maat *maat_inst, long long *results,
|
|
|
|
|
size_t n_result, struct maat_state *state)
|
2023-02-22 15:08:52 +08:00
|
|
|
{
|
2023-06-19 09:44:25 +00:00
|
|
|
int compile_table_id = table_manager_get_default_compile_table_id(maat_inst->tbl_mgr);
|
2023-02-07 11:25:31 +08:00
|
|
|
|
2023-03-30 20:49:59 +08:00
|
|
|
if (state->compile_table_id > 0) {
|
2023-03-22 20:40:36 +08:00
|
|
|
compile_table_id = state->compile_table_id;
|
2023-06-19 09:44:25 +00:00
|
|
|
}
|
2023-02-22 15:08:52 +08:00
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
void *compile_rt = table_manager_get_runtime(maat_inst->tbl_mgr, compile_table_id);
|
2023-03-30 20:49:59 +08:00
|
|
|
if (NULL == compile_rt) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-22 20:40:36 +08:00
|
|
|
size_t n_hit_compile = hit_group_to_compile(compile_rt, results, n_result, state);
|
2023-02-22 15:08:52 +08:00
|
|
|
|
2023-03-22 20:40:36 +08:00
|
|
|
assert(state->is_last_scan < LAST_SCAN_FINISHED);
|
|
|
|
|
if (LAST_SCAN_SET == state->is_last_scan) {
|
|
|
|
|
state->is_last_scan = LAST_SCAN_FINISHED;
|
2023-02-22 15:08:52 +08:00
|
|
|
}
|
|
|
|
|
|
2023-03-22 20:40:36 +08:00
|
|
|
return n_hit_compile;
|
2023-02-22 15:08:52 +08:00
|
|
|
}
|
2023-02-07 11:25:31 +08:00
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
int maat_scan_flag(struct maat *maat_inst, int table_id,
|
2023-02-22 15:22:41 +08:00
|
|
|
long long flag, long long *results, size_t n_result,
|
2023-03-23 11:57:17 +08:00
|
|
|
size_t *n_hit_result, struct maat_state *state)
|
2023-02-22 15:08:52 +08:00
|
|
|
{
|
2023-06-16 15:59:30 +08:00
|
|
|
if ((NULL == maat_inst) || table_id < 0 || table_id >= MAX_TABLE_NUM
|
2023-03-28 14:07:44 +08:00
|
|
|
|| (NULL == results) || (0 == n_result) || (NULL == state) ||
|
|
|
|
|
(state->thread_id < 0)) {
|
2023-02-22 15:08:52 +08:00
|
|
|
return MAAT_SCAN_ERR;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-20 15:34:56 +08:00
|
|
|
struct timespec start, end;
|
2023-06-16 15:59:30 +08:00
|
|
|
if (1 == maat_inst->opts.perf_on) {
|
2023-04-20 15:34:56 +08:00
|
|
|
clock_gettime(CLOCK_MONOTONIC, &start);
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-23 11:57:17 +08:00
|
|
|
state->scan_cnt++;
|
2023-02-22 15:08:52 +08:00
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
if (NULL == maat_inst->maat_rt) {
|
2023-02-22 15:08:52 +08:00
|
|
|
return MAAT_SCAN_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int vtable_id = 0;
|
2023-06-19 09:44:25 +00:00
|
|
|
int phy_table_id = table_id;
|
|
|
|
|
enum table_type table_type = TABLE_TYPE_INVALID;
|
|
|
|
|
|
|
|
|
|
table_type = table_manager_get_table_type(maat_inst->tbl_mgr, table_id);
|
2023-03-22 11:10:00 +08:00
|
|
|
if (table_type == TABLE_TYPE_VIRTUAL) {
|
2023-06-19 09:44:25 +00:00
|
|
|
phy_table_id = vtable_get_physical_table_id(maat_inst->tbl_mgr, table_id);
|
2023-03-22 11:10:00 +08:00
|
|
|
vtable_id = table_id;
|
|
|
|
|
}
|
2023-02-22 15:08:52 +08:00
|
|
|
|
2023-06-19 09:44:25 +00:00
|
|
|
if (phy_table_id < 0) {
|
2023-06-16 15:59:30 +08:00
|
|
|
maat_inst->stat->scan_err_cnt++;
|
2023-02-22 15:08:52 +08:00
|
|
|
return MAAT_SCAN_ERR;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-19 09:44:25 +00:00
|
|
|
table_type = table_manager_get_table_type(maat_inst->tbl_mgr, phy_table_id);
|
2023-03-22 11:10:00 +08:00
|
|
|
if (table_type != TABLE_TYPE_FLAG && table_type != TABLE_TYPE_FLAG_PLUS) {
|
2023-06-16 15:59:30 +08:00
|
|
|
maat_inst->stat->scan_err_cnt++;
|
2023-03-22 11:10:00 +08:00
|
|
|
return MAAT_SCAN_ERR;
|
2023-02-07 11:25:31 +08:00
|
|
|
}
|
|
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
maat_runtime_ref_inc(maat_inst->maat_rt, state->thread_id);
|
|
|
|
|
alignment_int64_array_add(maat_inst->stat->thread_call_cnt, state->thread_id, 1);
|
2023-02-22 15:08:52 +08:00
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
int hit_group_cnt = flag_scan(maat_inst->tbl_mgr, state->thread_id, flag,
|
2023-06-19 09:44:25 +00:00
|
|
|
phy_table_id, vtable_id, state);
|
2023-02-22 15:08:52 +08:00
|
|
|
if (hit_group_cnt < 0) {
|
2023-06-16 15:59:30 +08:00
|
|
|
maat_inst->stat->scan_err_cnt++;
|
2023-02-22 15:08:52 +08:00
|
|
|
return MAAT_SCAN_ERR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t sum_hit_compile_cnt = 0;
|
2023-03-23 11:57:17 +08:00
|
|
|
if (hit_group_cnt > 0 || scan_status_should_compile_NOT(state)) {
|
2023-06-16 15:59:30 +08:00
|
|
|
sum_hit_compile_cnt = group_to_compile(maat_inst, results, n_result, state);
|
2023-02-22 15:08:52 +08:00
|
|
|
*n_hit_result = sum_hit_compile_cnt;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-20 10:57:40 +08:00
|
|
|
if (sum_hit_compile_cnt > 0) {
|
2023-06-16 15:59:30 +08:00
|
|
|
alignment_int64_array_add(maat_inst->stat->hit_cnt, state->thread_id, 1);
|
2023-02-22 15:08:52 +08:00
|
|
|
if (0 == hit_group_cnt) {
|
2023-02-09 22:13:15 +08:00
|
|
|
//hit NOT group
|
2023-06-16 15:59:30 +08:00
|
|
|
alignment_int64_array_add(maat_inst->stat->not_grp_hit_cnt,
|
2023-06-08 07:32:41 +00:00
|
|
|
state->thread_id, 1);
|
2023-02-09 22:13:15 +08:00
|
|
|
}
|
2023-04-20 15:34:56 +08:00
|
|
|
}
|
|
|
|
|
|
2023-06-19 09:44:25 +00:00
|
|
|
void *flag_rt = table_manager_get_runtime(maat_inst->tbl_mgr, phy_table_id);
|
2023-04-20 15:34:56 +08:00
|
|
|
assert(flag_rt != NULL);
|
|
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
if (1 == maat_inst->opts.perf_on) {
|
2023-04-20 15:34:56 +08:00
|
|
|
clock_gettime(CLOCK_MONOTONIC, &end);
|
|
|
|
|
flag_runtime_perf_stat(flag_rt, &start, &end, state->thread_id);
|
2023-02-09 22:13:15 +08:00
|
|
|
} else {
|
2023-04-20 15:34:56 +08:00
|
|
|
flag_runtime_perf_stat(flag_rt, NULL, NULL, state->thread_id);
|
2023-02-07 11:25:31 +08:00
|
|
|
}
|
|
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
maat_runtime_ref_dec(maat_inst->maat_rt, state->thread_id);
|
2023-02-07 11:25:31 +08:00
|
|
|
|
2023-04-20 15:34:56 +08:00
|
|
|
if (sum_hit_compile_cnt > 0) {
|
|
|
|
|
return MAAT_SCAN_HIT;
|
|
|
|
|
} else if (hit_group_cnt > 0) {
|
|
|
|
|
return MAAT_SCAN_HALF_HIT;
|
|
|
|
|
} else {
|
|
|
|
|
return MAAT_SCAN_OK;
|
|
|
|
|
}
|
2023-01-06 18:54:59 +08:00
|
|
|
}
|
|
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
int maat_scan_integer(struct maat *maat_inst, int table_id,
|
2023-03-01 17:44:07 +08:00
|
|
|
long long integer, long long *results, size_t n_result,
|
2023-03-23 11:57:17 +08:00
|
|
|
size_t *n_hit_result, struct maat_state *state)
|
2022-10-27 17:58:52 +08:00
|
|
|
{
|
2023-06-16 15:59:30 +08:00
|
|
|
if ((NULL == maat_inst) || table_id < 0 || table_id >= MAX_TABLE_NUM
|
2023-03-28 14:07:44 +08:00
|
|
|
|| (NULL == results) || (0 == n_result) || (NULL == state) ||
|
|
|
|
|
(state->thread_id < 0)) {
|
2023-02-15 11:53:46 +08:00
|
|
|
return MAAT_SCAN_ERR;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-20 15:34:56 +08:00
|
|
|
struct timespec start, end;
|
2023-06-16 15:59:30 +08:00
|
|
|
if (1 == maat_inst->opts.perf_on) {
|
2023-04-20 15:34:56 +08:00
|
|
|
clock_gettime(CLOCK_MONOTONIC, &start);
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-23 11:57:17 +08:00
|
|
|
state->scan_cnt++;
|
2023-02-15 11:53:46 +08:00
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
if (NULL == maat_inst->maat_rt) {
|
2023-02-22 15:08:52 +08:00
|
|
|
return MAAT_SCAN_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-15 11:53:46 +08:00
|
|
|
int vtable_id = 0;
|
2023-06-19 09:44:25 +00:00
|
|
|
int phy_table_id = table_id;
|
|
|
|
|
enum table_type table_type = TABLE_TYPE_INVALID;
|
|
|
|
|
|
|
|
|
|
table_type = table_manager_get_table_type(maat_inst->tbl_mgr, table_id);
|
2023-03-22 11:10:00 +08:00
|
|
|
if (table_type == TABLE_TYPE_VIRTUAL) {
|
2023-06-19 09:44:25 +00:00
|
|
|
phy_table_id = vtable_get_physical_table_id(maat_inst->tbl_mgr, table_id);
|
2023-03-22 11:10:00 +08:00
|
|
|
vtable_id = table_id;
|
|
|
|
|
}
|
2023-02-15 11:53:46 +08:00
|
|
|
|
2023-06-19 09:44:25 +00:00
|
|
|
if (phy_table_id < 0) {
|
2023-06-16 15:59:30 +08:00
|
|
|
maat_inst->stat->scan_err_cnt++;
|
2023-02-22 15:08:52 +08:00
|
|
|
return MAAT_SCAN_ERR;
|
|
|
|
|
}
|
2023-02-20 10:57:40 +08:00
|
|
|
|
2023-06-19 09:44:25 +00:00
|
|
|
table_type = table_manager_get_table_type(maat_inst->tbl_mgr, phy_table_id);
|
2023-03-22 11:10:00 +08:00
|
|
|
if (table_type != TABLE_TYPE_INTERVAL && table_type != TABLE_TYPE_INTERVAL_PLUS) {
|
2023-06-16 15:59:30 +08:00
|
|
|
maat_inst->stat->scan_err_cnt++;
|
2023-03-22 11:10:00 +08:00
|
|
|
return MAAT_SCAN_ERR;
|
2023-02-15 11:53:46 +08:00
|
|
|
}
|
|
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
maat_runtime_ref_inc(maat_inst->maat_rt, state->thread_id);
|
|
|
|
|
alignment_int64_array_add(maat_inst->stat->thread_call_cnt, state->thread_id, 1);
|
2023-02-16 16:45:06 +08:00
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
int hit_group_cnt = interval_scan(maat_inst->tbl_mgr, state->thread_id, integer,
|
2023-06-19 09:44:25 +00:00
|
|
|
phy_table_id, vtable_id, state);
|
2023-02-22 15:08:52 +08:00
|
|
|
if (hit_group_cnt < 0) {
|
2023-06-16 15:59:30 +08:00
|
|
|
maat_inst->stat->scan_err_cnt++;
|
2023-02-22 15:08:52 +08:00
|
|
|
return MAAT_SCAN_ERR;
|
2023-02-20 10:57:40 +08:00
|
|
|
}
|
2023-02-22 15:08:52 +08:00
|
|
|
|
2023-02-20 10:57:40 +08:00
|
|
|
size_t sum_hit_compile_cnt = 0;
|
2023-03-23 11:57:17 +08:00
|
|
|
if (hit_group_cnt > 0 || scan_status_should_compile_NOT(state)) {
|
2023-06-16 15:59:30 +08:00
|
|
|
sum_hit_compile_cnt = group_to_compile(maat_inst, results, n_result, state);
|
2023-02-20 10:57:40 +08:00
|
|
|
*n_hit_result = sum_hit_compile_cnt;
|
2023-02-15 11:53:46 +08:00
|
|
|
}
|
|
|
|
|
|
2023-02-20 10:57:40 +08:00
|
|
|
if (sum_hit_compile_cnt > 0) {
|
2023-06-16 15:59:30 +08:00
|
|
|
alignment_int64_array_add(maat_inst->stat->hit_cnt, state->thread_id, 1);
|
2023-02-22 15:08:52 +08:00
|
|
|
if (0 == hit_group_cnt) {
|
2023-02-15 11:53:46 +08:00
|
|
|
//hit NOT group
|
2023-06-16 15:59:30 +08:00
|
|
|
alignment_int64_array_add(maat_inst->stat->not_grp_hit_cnt,
|
2023-06-08 07:32:41 +00:00
|
|
|
state->thread_id, 1);
|
2023-02-15 11:53:46 +08:00
|
|
|
}
|
2023-04-20 15:34:56 +08:00
|
|
|
}
|
|
|
|
|
|
2023-06-19 09:44:25 +00:00
|
|
|
void *interval_rt = table_manager_get_runtime(maat_inst->tbl_mgr, phy_table_id);
|
2023-04-20 15:34:56 +08:00
|
|
|
assert(interval_rt != NULL);
|
|
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
if (1 == maat_inst->opts.perf_on) {
|
2023-04-20 15:34:56 +08:00
|
|
|
clock_gettime(CLOCK_MONOTONIC, &end);
|
|
|
|
|
interval_runtime_perf_stat(interval_rt, &start, &end, state->thread_id);
|
2023-02-15 11:53:46 +08:00
|
|
|
} else {
|
2023-04-20 15:34:56 +08:00
|
|
|
interval_runtime_perf_stat(interval_rt, NULL, NULL, state->thread_id);
|
2023-02-15 11:53:46 +08:00
|
|
|
}
|
|
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
maat_runtime_ref_dec(maat_inst->maat_rt, state->thread_id);
|
2023-02-15 11:53:46 +08:00
|
|
|
|
2023-04-20 15:34:56 +08:00
|
|
|
if (sum_hit_compile_cnt > 0) {
|
|
|
|
|
return MAAT_SCAN_HIT;
|
|
|
|
|
} else if (hit_group_cnt > 0) {
|
|
|
|
|
return MAAT_SCAN_HALF_HIT;
|
|
|
|
|
} else {
|
|
|
|
|
return MAAT_SCAN_OK;
|
|
|
|
|
}
|
2022-10-27 17:58:52 +08:00
|
|
|
}
|
|
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
int maat_scan_ipv4(struct maat *maat_inst, int table_id, uint32_t ip_addr,
|
2023-06-08 07:32:41 +00:00
|
|
|
uint16_t port, int protocol, long long *results, size_t n_result,
|
2023-03-23 11:57:17 +08:00
|
|
|
size_t *n_hit_result, struct maat_state *state)
|
2022-12-09 17:12:18 +08:00
|
|
|
{
|
2023-06-16 15:59:30 +08:00
|
|
|
if ((NULL == maat_inst) || table_id < 0 || table_id >= MAX_TABLE_NUM
|
2023-03-28 14:07:44 +08:00
|
|
|
|| (protocol < -1) || (NULL == results) || (0 == n_result)
|
|
|
|
|
|| (NULL == state) || (state->thread_id < 0)) {
|
2023-02-03 17:28:14 +08:00
|
|
|
return MAAT_SCAN_ERR;
|
2022-12-09 17:12:18 +08:00
|
|
|
}
|
|
|
|
|
|
2023-04-20 15:34:56 +08:00
|
|
|
struct timespec start, end;
|
2023-06-16 15:59:30 +08:00
|
|
|
if (1 == maat_inst->opts.perf_on) {
|
2023-04-20 15:34:56 +08:00
|
|
|
clock_gettime(CLOCK_MONOTONIC, &start);
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-23 11:57:17 +08:00
|
|
|
state->scan_cnt++;
|
2023-01-30 21:59:35 +08:00
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
if (NULL == maat_inst->maat_rt) {
|
2023-02-22 15:08:52 +08:00
|
|
|
return MAAT_SCAN_OK;
|
|
|
|
|
}
|
2023-02-03 17:28:14 +08:00
|
|
|
|
|
|
|
|
int vtable_id = 0;
|
2023-06-19 09:44:25 +00:00
|
|
|
int phy_table_id = table_id;
|
|
|
|
|
enum table_type table_type = TABLE_TYPE_INVALID;
|
|
|
|
|
|
|
|
|
|
table_type = table_manager_get_table_type(maat_inst->tbl_mgr, table_id);
|
2023-03-22 11:10:00 +08:00
|
|
|
if (table_type == TABLE_TYPE_VIRTUAL) {
|
2023-06-19 09:44:25 +00:00
|
|
|
phy_table_id = vtable_get_physical_table_id(maat_inst->tbl_mgr, table_id);
|
2023-03-22 11:10:00 +08:00
|
|
|
vtable_id = table_id;
|
|
|
|
|
}
|
2023-02-20 10:57:40 +08:00
|
|
|
|
2023-06-19 09:44:25 +00:00
|
|
|
if (phy_table_id < 0) {
|
2023-06-16 15:59:30 +08:00
|
|
|
maat_inst->stat->scan_err_cnt++;
|
2023-02-22 15:08:52 +08:00
|
|
|
return MAAT_SCAN_ERR;
|
|
|
|
|
}
|
2023-01-31 20:39:53 +08:00
|
|
|
|
2023-06-19 09:44:25 +00:00
|
|
|
table_type = table_manager_get_table_type(maat_inst->tbl_mgr, phy_table_id);
|
2023-03-22 11:10:00 +08:00
|
|
|
if (table_type != TABLE_TYPE_IP_PLUS) {
|
2023-06-16 15:59:30 +08:00
|
|
|
maat_inst->stat->scan_err_cnt++;
|
2023-03-22 11:10:00 +08:00
|
|
|
return MAAT_SCAN_ERR;
|
2023-02-20 10:57:40 +08:00
|
|
|
}
|
|
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
maat_runtime_ref_inc(maat_inst->maat_rt, state->thread_id);
|
|
|
|
|
alignment_int64_array_add(maat_inst->stat->thread_call_cnt, state->thread_id, 1);
|
2023-02-20 10:57:40 +08:00
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
int hit_group_cnt = ipv4_scan(maat_inst->tbl_mgr, state->thread_id, ip_addr,
|
2023-06-19 09:44:25 +00:00
|
|
|
port, protocol, phy_table_id, vtable_id, state);
|
2023-02-22 15:08:52 +08:00
|
|
|
if (hit_group_cnt < 0) {
|
2023-06-16 15:59:30 +08:00
|
|
|
maat_inst->stat->scan_err_cnt++;
|
2023-02-22 15:08:52 +08:00
|
|
|
return MAAT_SCAN_ERR;
|
2023-02-20 10:57:40 +08:00
|
|
|
}
|
|
|
|
|
|
2023-02-22 15:08:52 +08:00
|
|
|
size_t sum_hit_compile_cnt = 0;
|
2023-03-23 11:57:17 +08:00
|
|
|
if (hit_group_cnt > 0 || scan_status_should_compile_NOT(state)) {
|
2023-06-16 15:59:30 +08:00
|
|
|
sum_hit_compile_cnt = group_to_compile(maat_inst, results, n_result, state);
|
2023-02-20 10:57:40 +08:00
|
|
|
*n_hit_result = sum_hit_compile_cnt;
|
2022-12-14 15:28:21 +08:00
|
|
|
}
|
2023-02-03 17:28:14 +08:00
|
|
|
|
2023-02-20 10:57:40 +08:00
|
|
|
if (sum_hit_compile_cnt > 0) {
|
2023-06-16 15:59:30 +08:00
|
|
|
alignment_int64_array_add(maat_inst->stat->hit_cnt, state->thread_id, 1);
|
2023-02-22 15:08:52 +08:00
|
|
|
if (0 == hit_group_cnt) {
|
2023-02-09 22:13:15 +08:00
|
|
|
//hit NOT group
|
2023-06-16 15:59:30 +08:00
|
|
|
alignment_int64_array_add(maat_inst->stat->not_grp_hit_cnt,
|
2023-06-08 07:32:41 +00:00
|
|
|
state->thread_id, 1);
|
2023-02-09 22:13:15 +08:00
|
|
|
}
|
2023-04-20 15:34:56 +08:00
|
|
|
}
|
|
|
|
|
|
2023-06-19 09:44:25 +00:00
|
|
|
void *ip_rt = table_manager_get_runtime(maat_inst->tbl_mgr, phy_table_id);
|
2023-04-20 15:34:56 +08:00
|
|
|
assert(ip_rt != NULL);
|
|
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
if (1 == maat_inst->opts.perf_on) {
|
2023-04-20 15:34:56 +08:00
|
|
|
clock_gettime(CLOCK_MONOTONIC, &end);
|
|
|
|
|
ip_runtime_perf_stat(ip_rt, &start, &end, state->thread_id);
|
2023-02-09 22:13:15 +08:00
|
|
|
} else {
|
2023-04-20 15:34:56 +08:00
|
|
|
ip_runtime_perf_stat(ip_rt, NULL, NULL, state->thread_id);
|
2023-02-03 17:28:14 +08:00
|
|
|
}
|
|
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
maat_runtime_ref_dec(maat_inst->maat_rt, state->thread_id);
|
2023-02-03 17:28:14 +08:00
|
|
|
|
2023-04-20 15:34:56 +08:00
|
|
|
if (sum_hit_compile_cnt > 0) {
|
|
|
|
|
return MAAT_SCAN_HIT;
|
|
|
|
|
} else if (hit_group_cnt > 0) {
|
|
|
|
|
return MAAT_SCAN_HALF_HIT;
|
|
|
|
|
} else {
|
|
|
|
|
return MAAT_SCAN_OK;
|
|
|
|
|
}
|
2023-03-01 17:44:07 +08:00
|
|
|
}
|
2023-01-30 21:59:35 +08:00
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
int maat_scan_ipv6(struct maat *maat_inst, int table_id,
|
2023-03-27 15:52:47 +08:00
|
|
|
uint8_t *ip_addr, uint16_t port, int protocol,
|
|
|
|
|
long long *results, size_t n_result,
|
2023-03-23 11:57:17 +08:00
|
|
|
size_t *n_hit_result, struct maat_state *state)
|
2023-01-30 21:59:35 +08:00
|
|
|
{
|
2023-06-16 15:59:30 +08:00
|
|
|
if ((NULL == maat_inst) || table_id < 0 || table_id >= MAX_TABLE_NUM
|
2023-03-28 14:07:44 +08:00
|
|
|
|| (protocol < -1) || (NULL == ip_addr) || (NULL == results)
|
|
|
|
|
|| (0 == n_result) || (NULL == state) || (state->thread_id < 0)) {
|
2023-02-20 11:43:43 +08:00
|
|
|
return MAAT_SCAN_ERR;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-20 15:34:56 +08:00
|
|
|
struct timespec start, end;
|
2023-06-16 15:59:30 +08:00
|
|
|
if (1 == maat_inst->opts.perf_on) {
|
2023-04-20 15:34:56 +08:00
|
|
|
clock_gettime(CLOCK_MONOTONIC, &start);
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-23 11:57:17 +08:00
|
|
|
state->scan_cnt++;
|
2023-02-03 17:28:14 +08:00
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
if (NULL == maat_inst->maat_rt) {
|
2023-02-22 15:08:52 +08:00
|
|
|
return MAAT_SCAN_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-03 17:28:14 +08:00
|
|
|
int vtable_id = 0;
|
2023-06-19 09:44:25 +00:00
|
|
|
int phy_table_id = table_id;
|
|
|
|
|
enum table_type table_type = TABLE_TYPE_INVALID;
|
|
|
|
|
|
|
|
|
|
table_type = table_manager_get_table_type(maat_inst->tbl_mgr, table_id);
|
2023-03-22 11:10:00 +08:00
|
|
|
if (table_type == TABLE_TYPE_VIRTUAL) {
|
2023-06-19 09:44:25 +00:00
|
|
|
phy_table_id = vtable_get_physical_table_id(maat_inst->tbl_mgr, table_id);
|
2023-03-22 11:10:00 +08:00
|
|
|
vtable_id = table_id;
|
|
|
|
|
}
|
2023-02-20 10:57:40 +08:00
|
|
|
|
2023-06-19 09:44:25 +00:00
|
|
|
if (phy_table_id < 0) {
|
2023-06-16 15:59:30 +08:00
|
|
|
maat_inst->stat->scan_err_cnt++;
|
2023-02-22 15:08:52 +08:00
|
|
|
return MAAT_SCAN_ERR;
|
|
|
|
|
}
|
2023-02-03 17:28:14 +08:00
|
|
|
|
2023-06-19 09:44:25 +00:00
|
|
|
table_type = table_manager_get_table_type(maat_inst->tbl_mgr, phy_table_id);
|
2023-03-22 11:10:00 +08:00
|
|
|
if (table_type != TABLE_TYPE_IP_PLUS) {
|
2023-06-16 15:59:30 +08:00
|
|
|
maat_inst->stat->scan_err_cnt++;
|
2023-03-22 11:10:00 +08:00
|
|
|
return MAAT_SCAN_ERR;
|
2023-02-20 10:57:40 +08:00
|
|
|
}
|
|
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
maat_runtime_ref_inc(maat_inst->maat_rt, state->thread_id);
|
|
|
|
|
alignment_int64_array_add(maat_inst->stat->thread_call_cnt, state->thread_id, 1);
|
2023-02-20 10:57:40 +08:00
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
int hit_group_cnt = ipv6_scan(maat_inst->tbl_mgr, state->thread_id, ip_addr,
|
2023-06-19 09:44:25 +00:00
|
|
|
port, protocol, phy_table_id, vtable_id, state);
|
2023-02-22 15:08:52 +08:00
|
|
|
if (hit_group_cnt < 0) {
|
2023-06-16 15:59:30 +08:00
|
|
|
maat_inst->stat->scan_err_cnt++;
|
2023-02-22 15:08:52 +08:00
|
|
|
return MAAT_SCAN_ERR;
|
2023-02-20 10:57:40 +08:00
|
|
|
}
|
2023-02-03 17:28:14 +08:00
|
|
|
|
2023-02-20 10:57:40 +08:00
|
|
|
size_t sum_hit_compile_cnt = 0;
|
2023-03-23 11:57:17 +08:00
|
|
|
if (hit_group_cnt > 0 || scan_status_should_compile_NOT(state)) {
|
2023-06-16 15:59:30 +08:00
|
|
|
sum_hit_compile_cnt = group_to_compile(maat_inst, results, n_result, state);
|
2023-02-20 10:57:40 +08:00
|
|
|
*n_hit_result = sum_hit_compile_cnt;
|
2023-02-03 17:28:14 +08:00
|
|
|
}
|
|
|
|
|
|
2023-02-20 10:57:40 +08:00
|
|
|
if (sum_hit_compile_cnt > 0) {
|
2023-06-16 15:59:30 +08:00
|
|
|
alignment_int64_array_add(maat_inst->stat->hit_cnt, state->thread_id, 1);
|
2023-02-22 15:08:52 +08:00
|
|
|
if (0 == hit_group_cnt) {
|
2023-02-09 22:13:15 +08:00
|
|
|
//hit NOT group
|
2023-06-16 15:59:30 +08:00
|
|
|
alignment_int64_array_add(maat_inst->stat->not_grp_hit_cnt,
|
2023-06-08 07:32:41 +00:00
|
|
|
state->thread_id, 1);
|
2023-02-09 22:13:15 +08:00
|
|
|
}
|
2023-04-20 15:34:56 +08:00
|
|
|
}
|
|
|
|
|
|
2023-06-19 09:44:25 +00:00
|
|
|
void *ip_rt = table_manager_get_runtime(maat_inst->tbl_mgr, phy_table_id);
|
2023-04-20 15:34:56 +08:00
|
|
|
assert(ip_rt != NULL);
|
|
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
if (1 == maat_inst->opts.perf_on) {
|
2023-04-20 15:34:56 +08:00
|
|
|
clock_gettime(CLOCK_MONOTONIC, &end);
|
|
|
|
|
ip_runtime_perf_stat(ip_rt, &start, &end, state->thread_id);
|
2023-02-09 22:13:15 +08:00
|
|
|
} else {
|
2023-04-20 15:34:56 +08:00
|
|
|
ip_runtime_perf_stat(ip_rt, NULL, NULL, state->thread_id);
|
2023-02-03 17:28:14 +08:00
|
|
|
}
|
|
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
maat_runtime_ref_dec(maat_inst->maat_rt, state->thread_id);
|
2023-02-03 17:28:14 +08:00
|
|
|
|
2023-04-20 15:34:56 +08:00
|
|
|
if (sum_hit_compile_cnt > 0) {
|
|
|
|
|
return MAAT_SCAN_HIT;
|
|
|
|
|
} else if (hit_group_cnt > 0) {
|
|
|
|
|
return MAAT_SCAN_HALF_HIT;
|
|
|
|
|
} else {
|
|
|
|
|
return MAAT_SCAN_OK;
|
|
|
|
|
}
|
2023-03-01 17:44:07 +08:00
|
|
|
}
|
|
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
int maat_scan_string(struct maat *maat_inst, int table_id, const char *data,
|
2023-03-28 14:07:44 +08:00
|
|
|
size_t data_len, long long *results, size_t n_result,
|
2023-03-23 11:57:17 +08:00
|
|
|
size_t *n_hit_result, struct maat_state *state)
|
2022-10-27 17:58:52 +08:00
|
|
|
{
|
2023-06-16 15:59:30 +08:00
|
|
|
if ((NULL == maat_inst) || table_id < 0 || table_id >= MAX_TABLE_NUM
|
2023-03-28 14:07:44 +08:00
|
|
|
|| (NULL == data) || (0 == data_len) || (NULL == results)
|
|
|
|
|
|| (0 == n_result) || (NULL == state) || (state->thread_id < 0)) {
|
2023-02-03 17:28:14 +08:00
|
|
|
return MAAT_SCAN_ERR;
|
2022-11-29 14:12:40 +08:00
|
|
|
}
|
|
|
|
|
|
2023-04-20 15:34:56 +08:00
|
|
|
struct timespec start, end;
|
2023-06-16 15:59:30 +08:00
|
|
|
if (1 == maat_inst->opts.perf_on) {
|
2023-04-20 15:34:56 +08:00
|
|
|
clock_gettime(CLOCK_MONOTONIC, &start);
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-23 11:57:17 +08:00
|
|
|
state->scan_cnt++;
|
2023-01-30 21:59:35 +08:00
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
if (NULL == maat_inst->maat_rt) {
|
2023-02-03 17:28:14 +08:00
|
|
|
return MAAT_SCAN_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-22 15:08:52 +08:00
|
|
|
int vtable_id = 0;
|
2023-06-19 09:44:25 +00:00
|
|
|
int phy_table_id = table_id;
|
|
|
|
|
enum table_type table_type = TABLE_TYPE_INVALID;
|
|
|
|
|
|
|
|
|
|
table_type = table_manager_get_table_type(maat_inst->tbl_mgr, table_id);
|
2023-03-22 11:10:00 +08:00
|
|
|
if (table_type == TABLE_TYPE_VIRTUAL) {
|
2023-06-19 09:44:25 +00:00
|
|
|
phy_table_id = vtable_get_physical_table_id(maat_inst->tbl_mgr, table_id);
|
2023-03-22 11:10:00 +08:00
|
|
|
vtable_id = table_id;
|
|
|
|
|
}
|
2023-02-22 15:08:52 +08:00
|
|
|
|
2023-06-19 09:44:25 +00:00
|
|
|
if (phy_table_id < 0) {
|
2023-06-16 15:59:30 +08:00
|
|
|
maat_inst->stat->scan_err_cnt++;
|
2023-02-22 15:08:52 +08:00
|
|
|
return MAAT_SCAN_ERR;
|
|
|
|
|
}
|
2023-02-03 17:28:14 +08:00
|
|
|
|
2023-06-19 09:44:25 +00:00
|
|
|
table_type = table_manager_get_table_type(maat_inst->tbl_mgr, phy_table_id);
|
2023-03-22 11:10:00 +08:00
|
|
|
if (table_type != TABLE_TYPE_EXPR && table_type != TABLE_TYPE_EXPR_PLUS) {
|
2023-06-16 15:59:30 +08:00
|
|
|
maat_inst->stat->scan_err_cnt++;
|
2023-03-22 11:10:00 +08:00
|
|
|
return MAAT_SCAN_ERR;
|
2023-02-22 15:08:52 +08:00
|
|
|
}
|
2023-02-03 17:28:14 +08:00
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
maat_runtime_ref_inc(maat_inst->maat_rt, state->thread_id);
|
|
|
|
|
alignment_int64_array_add(maat_inst->stat->thread_call_cnt, state->thread_id, 1);
|
2023-02-03 17:28:14 +08:00
|
|
|
|
2023-06-19 09:44:25 +00:00
|
|
|
int hit_group_cnt = string_scan(maat_inst->tbl_mgr, state->thread_id, data,
|
|
|
|
|
data_len, phy_table_id, vtable_id, state);
|
2023-02-22 15:08:52 +08:00
|
|
|
if (hit_group_cnt < 0) {
|
2023-06-16 15:59:30 +08:00
|
|
|
maat_inst->stat->scan_err_cnt++;
|
2023-02-22 15:08:52 +08:00
|
|
|
return MAAT_SCAN_ERR;
|
2023-02-20 10:57:40 +08:00
|
|
|
}
|
2023-02-03 17:28:14 +08:00
|
|
|
|
2023-05-31 09:13:14 +00:00
|
|
|
size_t sum_hit_compile_cnt = 0;
|
2023-03-23 11:57:17 +08:00
|
|
|
if (hit_group_cnt > 0 || scan_status_should_compile_NOT(state)) {
|
2023-06-16 15:59:30 +08:00
|
|
|
sum_hit_compile_cnt = group_to_compile(maat_inst, results, n_result, state);
|
2023-02-20 10:57:40 +08:00
|
|
|
*n_hit_result = sum_hit_compile_cnt;
|
2023-02-03 17:28:14 +08:00
|
|
|
}
|
|
|
|
|
|
2023-02-20 10:57:40 +08:00
|
|
|
if (sum_hit_compile_cnt > 0) {
|
2023-06-16 15:59:30 +08:00
|
|
|
alignment_int64_array_add(maat_inst->stat->hit_cnt, state->thread_id, 1);
|
2023-02-22 15:08:52 +08:00
|
|
|
if (0 == hit_group_cnt) {
|
2023-02-09 22:13:15 +08:00
|
|
|
//hit NOT group
|
2023-06-16 15:59:30 +08:00
|
|
|
alignment_int64_array_add(maat_inst->stat->not_grp_hit_cnt,
|
2023-06-08 07:32:41 +00:00
|
|
|
state->thread_id, 1);
|
2023-02-09 22:13:15 +08:00
|
|
|
}
|
2023-04-20 15:34:56 +08:00
|
|
|
}
|
|
|
|
|
|
2023-06-19 09:44:25 +00:00
|
|
|
void *expr_rt = table_manager_get_runtime(maat_inst->tbl_mgr, phy_table_id);
|
2023-04-20 15:34:56 +08:00
|
|
|
assert(expr_rt != NULL);
|
|
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
if (1 == maat_inst->opts.perf_on) {
|
2023-04-20 15:34:56 +08:00
|
|
|
clock_gettime(CLOCK_MONOTONIC, &end);
|
|
|
|
|
expr_runtime_perf_stat(expr_rt, data_len, &start, &end, state->thread_id);
|
2023-02-09 22:13:15 +08:00
|
|
|
} else {
|
2023-04-20 15:34:56 +08:00
|
|
|
expr_runtime_perf_stat(expr_rt, data_len, NULL, NULL, state->thread_id);
|
2023-01-30 21:59:35 +08:00
|
|
|
}
|
2023-02-03 17:28:14 +08:00
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
maat_runtime_ref_dec(maat_inst->maat_rt, state->thread_id);
|
2023-02-03 17:28:14 +08:00
|
|
|
|
2023-04-20 15:34:56 +08:00
|
|
|
if (sum_hit_compile_cnt > 0) {
|
|
|
|
|
return MAAT_SCAN_HIT;
|
|
|
|
|
} else if (hit_group_cnt > 0) {
|
|
|
|
|
return MAAT_SCAN_HALF_HIT;
|
|
|
|
|
} else {
|
|
|
|
|
return MAAT_SCAN_OK;
|
|
|
|
|
}
|
2022-11-17 05:05:35 +08:00
|
|
|
}
|
|
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
struct maat_stream *maat_stream_new(struct maat *maat_inst, int table_id,
|
2023-03-28 14:07:44 +08:00
|
|
|
struct maat_state *state)
|
2022-11-17 05:05:35 +08:00
|
|
|
{
|
2023-06-16 15:59:30 +08:00
|
|
|
if (NULL == maat_inst || table_id < 0 || table_id > MAX_TABLE_NUM
|
2023-03-28 14:07:44 +08:00
|
|
|
|| NULL == state || state->thread_id < 0) {
|
2023-02-24 17:29:38 +08:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct maat_stream *stream = ALLOC(struct maat_stream, 1);
|
2023-06-16 15:59:30 +08:00
|
|
|
stream->ref_maat_inst = maat_inst;
|
|
|
|
|
stream->last_full_version = maat_inst->last_full_version;
|
2023-03-28 14:07:44 +08:00
|
|
|
stream->thread_id = state->thread_id;
|
2023-06-19 09:44:25 +00:00
|
|
|
stream->phy_table_id = table_id;
|
2023-06-16 15:59:30 +08:00
|
|
|
stream->logger = maat_inst->logger;
|
2023-03-22 11:10:00 +08:00
|
|
|
|
2023-06-19 09:44:25 +00:00
|
|
|
enum table_type table_type = TABLE_TYPE_INVALID;
|
|
|
|
|
|
|
|
|
|
table_type = table_manager_get_table_type(maat_inst->tbl_mgr, table_id);
|
2023-03-22 11:10:00 +08:00
|
|
|
if (table_type == TABLE_TYPE_VIRTUAL) {
|
2023-06-19 09:44:25 +00:00
|
|
|
stream->phy_table_id = vtable_get_physical_table_id(maat_inst->tbl_mgr, table_id);
|
2023-03-22 11:10:00 +08:00
|
|
|
stream->vtable_id = table_id;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-19 09:44:25 +00:00
|
|
|
if (stream->phy_table_id < 0) {
|
2023-02-24 17:29:38 +08:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-19 09:44:25 +00:00
|
|
|
table_type = table_manager_get_table_type(maat_inst->tbl_mgr, stream->phy_table_id);
|
2023-03-22 11:10:00 +08:00
|
|
|
if (table_type != TABLE_TYPE_EXPR && table_type != TABLE_TYPE_EXPR_PLUS) {
|
|
|
|
|
return NULL;
|
2023-02-24 17:29:38 +08:00
|
|
|
}
|
|
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
void *expr_rt = table_manager_get_runtime(stream->ref_maat_inst->tbl_mgr,
|
2023-06-19 09:44:25 +00:00
|
|
|
stream->phy_table_id);
|
2023-03-22 11:10:00 +08:00
|
|
|
assert(expr_rt != NULL);
|
2023-04-13 14:56:35 +08:00
|
|
|
stream->expr_rt_version = expr_runtime_get_version(expr_rt);
|
2023-02-27 10:07:37 +08:00
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
maat_runtime_ref_inc(maat_inst->maat_rt, state->thread_id);
|
2023-03-28 14:07:44 +08:00
|
|
|
struct adapter_hs_stream *handle = expr_runtime_stream_open((struct expr_runtime *)expr_rt,
|
|
|
|
|
state->thread_id);
|
2023-03-22 11:10:00 +08:00
|
|
|
if (NULL == handle) {
|
|
|
|
|
goto error;
|
2023-02-24 17:29:38 +08:00
|
|
|
}
|
2023-06-16 15:59:30 +08:00
|
|
|
stream->handle = handle;
|
2023-02-24 17:29:38 +08:00
|
|
|
|
|
|
|
|
return stream;
|
2023-03-22 11:10:00 +08:00
|
|
|
|
|
|
|
|
error:
|
|
|
|
|
FREE(stream);
|
|
|
|
|
return NULL;
|
2022-11-17 05:05:35 +08:00
|
|
|
}
|
2022-10-27 17:58:52 +08:00
|
|
|
|
2023-03-28 14:07:44 +08:00
|
|
|
int maat_stream_scan(struct maat_stream *maat_stream, const char *data, int data_len,
|
2023-02-24 17:29:38 +08:00
|
|
|
long long *results, size_t n_result, size_t *n_hit_result,
|
2023-03-23 11:57:17 +08:00
|
|
|
struct maat_state *state)
|
2022-11-17 05:05:35 +08:00
|
|
|
{
|
2023-02-24 17:29:38 +08:00
|
|
|
if ((NULL == maat_stream) || (NULL == data) || (0 == data_len)
|
|
|
|
|
|| (NULL == results) || (0 == n_result) || (NULL == state)) {
|
|
|
|
|
return MAAT_SCAN_ERR;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-20 15:34:56 +08:00
|
|
|
struct timespec start, end;
|
2023-06-16 15:59:30 +08:00
|
|
|
if (1 == maat_stream->ref_maat_inst->opts.perf_on) {
|
2023-04-20 15:34:56 +08:00
|
|
|
clock_gettime(CLOCK_MONOTONIC, &start);
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-23 11:57:17 +08:00
|
|
|
state->scan_cnt++;
|
2023-02-24 17:29:38 +08:00
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
struct maat *maat_inst = maat_stream->ref_maat_inst;
|
|
|
|
|
if (maat_stream->last_full_version != maat_inst->last_full_version) {
|
2023-02-24 17:29:38 +08:00
|
|
|
return MAAT_SCAN_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
void *expr_rt = table_manager_get_runtime(maat_inst->tbl_mgr,
|
2023-06-19 09:44:25 +00:00
|
|
|
maat_stream->phy_table_id);
|
2023-04-13 14:56:35 +08:00
|
|
|
assert(expr_rt != NULL);
|
2023-04-13 18:53:36 +08:00
|
|
|
|
2023-04-13 14:56:35 +08:00
|
|
|
long long cur_expr_rt_version = expr_runtime_get_version(expr_rt);
|
|
|
|
|
if (maat_stream->expr_rt_version != cur_expr_rt_version) {
|
|
|
|
|
return MAAT_SCAN_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
alignment_int64_array_add(maat_inst->stat->thread_call_cnt,
|
2023-06-08 07:32:41 +00:00
|
|
|
maat_stream->thread_id, 1);
|
2023-02-24 17:29:38 +08:00
|
|
|
|
2023-03-28 14:07:44 +08:00
|
|
|
int hit_group_cnt = expr_stream_scan(maat_stream, data, data_len, state);
|
2023-02-24 17:29:38 +08:00
|
|
|
if (hit_group_cnt < 0) {
|
2023-06-16 15:59:30 +08:00
|
|
|
maat_inst->stat->scan_err_cnt++;
|
2023-02-24 17:29:38 +08:00
|
|
|
return MAAT_SCAN_ERR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t sum_hit_compile_cnt = 0;
|
2023-03-23 11:57:17 +08:00
|
|
|
if (hit_group_cnt > 0 || scan_status_should_compile_NOT(state)) {
|
2023-06-16 15:59:30 +08:00
|
|
|
sum_hit_compile_cnt = group_to_compile(maat_inst, results, n_result, state);
|
2023-02-24 17:29:38 +08:00
|
|
|
*n_hit_result = sum_hit_compile_cnt;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (sum_hit_compile_cnt > 0) {
|
2023-06-16 15:59:30 +08:00
|
|
|
alignment_int64_array_add(maat_inst->stat->hit_cnt, maat_stream->thread_id, 1);
|
2023-02-24 17:29:38 +08:00
|
|
|
if (0 == hit_group_cnt) {
|
|
|
|
|
//hit NOT group
|
2023-06-16 15:59:30 +08:00
|
|
|
alignment_int64_array_add(maat_inst->stat->not_grp_hit_cnt,
|
2023-06-08 07:32:41 +00:00
|
|
|
maat_stream->thread_id, 1);
|
2023-02-24 17:29:38 +08:00
|
|
|
}
|
2023-04-20 15:34:56 +08:00
|
|
|
}
|
|
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
if (1 == maat_inst->opts.perf_on) {
|
2023-04-20 15:34:56 +08:00
|
|
|
clock_gettime(CLOCK_MONOTONIC, &end);
|
|
|
|
|
expr_runtime_perf_stat(expr_rt, data_len, &start, &end, state->thread_id);
|
2023-02-24 17:29:38 +08:00
|
|
|
} else {
|
2023-04-20 15:34:56 +08:00
|
|
|
expr_runtime_perf_stat(expr_rt, data_len, NULL, NULL, state->thread_id);
|
|
|
|
|
}
|
2023-02-24 17:29:38 +08:00
|
|
|
|
2023-04-20 15:34:56 +08:00
|
|
|
if (sum_hit_compile_cnt > 0) {
|
|
|
|
|
return MAAT_SCAN_HIT;
|
|
|
|
|
} else if (hit_group_cnt > 0) {
|
|
|
|
|
return MAAT_SCAN_HALF_HIT;
|
|
|
|
|
} else {
|
|
|
|
|
return MAAT_SCAN_OK;
|
|
|
|
|
}
|
2022-11-17 05:05:35 +08:00
|
|
|
}
|
|
|
|
|
|
2023-03-28 14:07:44 +08:00
|
|
|
void maat_stream_free(struct maat_stream *maat_stream)
|
2022-11-17 05:05:35 +08:00
|
|
|
{
|
2023-03-28 14:07:44 +08:00
|
|
|
if (NULL == maat_stream) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-02-24 17:29:38 +08:00
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
struct maat *maat_inst = maat_stream->ref_maat_inst;
|
|
|
|
|
void *expr_rt = table_manager_get_runtime(maat_inst->tbl_mgr,
|
2023-06-19 09:44:25 +00:00
|
|
|
maat_stream->phy_table_id);
|
2023-04-20 15:34:56 +08:00
|
|
|
assert(expr_rt != NULL);
|
|
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
long long cur_expr_rt_version = expr_runtime_get_version(expr_rt);
|
|
|
|
|
if ((maat_stream->last_full_version == maat_inst->last_full_version) &&
|
|
|
|
|
(maat_stream->expr_rt_version == cur_expr_rt_version)) {
|
|
|
|
|
maat_runtime_ref_dec(maat_inst->maat_rt, maat_stream->thread_id);
|
|
|
|
|
} else {
|
|
|
|
|
maat_inst->stat->zombie_rs_stream--;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
expr_runtime_stream_close(expr_rt, maat_stream->thread_id, maat_stream->handle);
|
2023-03-28 14:07:44 +08:00
|
|
|
FREE(maat_stream);
|
2022-11-17 05:05:35 +08:00
|
|
|
}
|
|
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
struct maat_state *maat_state_new(struct maat *maat_inst, int thread_id)
|
2023-03-15 11:36:54 +08:00
|
|
|
{
|
2023-06-16 15:59:30 +08:00
|
|
|
if (NULL == maat_inst || thread_id < 0) {
|
2023-03-23 11:57:17 +08:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-22 11:23:21 +08:00
|
|
|
struct maat_state *state = ALLOC(struct maat_state, 1);
|
|
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
state->maat_inst = maat_inst;
|
2023-03-23 11:57:17 +08:00
|
|
|
state->is_set_district = DISTRICT_FLAG_UNSET;
|
|
|
|
|
state->is_last_scan = LAST_SCAN_UNSET;
|
2023-03-22 11:23:21 +08:00
|
|
|
state->district_id = DISTRICT_ANY;
|
2023-03-23 11:57:17 +08:00
|
|
|
state->thread_id = thread_id;
|
2023-03-22 11:23:21 +08:00
|
|
|
|
2023-06-01 14:47:20 +08:00
|
|
|
//state->compile_state no need to alloc memory at this point, but alloc it after hitting items
|
2023-06-16 15:59:30 +08:00
|
|
|
alignment_int64_array_add(maat_inst->stat->maat_state_cnt, thread_id, 1);
|
2023-05-31 09:13:14 +00:00
|
|
|
|
2023-03-22 11:23:21 +08:00
|
|
|
return state;
|
2023-03-15 11:36:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void maat_state_reset(struct maat_state *state)
|
|
|
|
|
{
|
2023-03-23 11:57:17 +08:00
|
|
|
if (NULL == state) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
state->compile_table_id = 0;
|
|
|
|
|
state->is_set_district = DISTRICT_FLAG_UNSET;
|
|
|
|
|
state->is_last_scan = LAST_SCAN_UNSET;
|
|
|
|
|
state->district_id = DISTRICT_ANY;
|
|
|
|
|
state->scan_cnt = 0;
|
2023-03-15 11:36:54 +08:00
|
|
|
|
2023-06-01 14:47:20 +08:00
|
|
|
if (state->compile_state != NULL) {
|
|
|
|
|
maat_compile_state_reset(state->compile_state);
|
|
|
|
|
}
|
2023-03-15 11:36:54 +08:00
|
|
|
}
|
|
|
|
|
|
2023-03-23 11:57:17 +08:00
|
|
|
void maat_state_free(struct maat_state *state)
|
2023-03-15 11:36:54 +08:00
|
|
|
{
|
2023-03-23 11:57:17 +08:00
|
|
|
if (NULL == state) {
|
2023-03-15 11:36:54 +08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
assert(state->maat_inst != NULL);
|
2023-05-31 09:13:14 +00:00
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
struct maat *maat_inst = state->maat_inst;
|
2023-05-31 09:13:14 +00:00
|
|
|
long long thread_id = state->thread_id;
|
|
|
|
|
|
2023-05-23 17:50:53 +08:00
|
|
|
if (state->compile_state != NULL) {
|
2023-06-16 15:59:30 +08:00
|
|
|
maat_compile_state_free(state->compile_state, maat_inst, thread_id);
|
2023-05-23 17:50:53 +08:00
|
|
|
state->compile_state = NULL;
|
2023-06-16 15:59:30 +08:00
|
|
|
alignment_int64_array_add(maat_inst->stat->maat_compile_state_cnt,
|
2023-06-09 17:30:20 +08:00
|
|
|
thread_id, -1);
|
2023-05-23 17:50:53 +08:00
|
|
|
}
|
|
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
state->maat_inst = NULL;
|
2023-05-23 17:50:53 +08:00
|
|
|
FREE(state);
|
2023-05-31 09:13:14 +00:00
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
alignment_int64_array_add(maat_inst->stat->maat_state_cnt,
|
2023-06-08 07:32:41 +00:00
|
|
|
thread_id, -1);
|
2023-06-16 15:59:30 +08:00
|
|
|
alignment_int64_array_add(maat_inst->stat->maat_state_free_cnt,
|
2023-06-09 17:30:20 +08:00
|
|
|
thread_id, 1);
|
2023-06-16 15:59:30 +08:00
|
|
|
alignment_int64_array_add(maat_inst->stat->maat_state_free_bytes,
|
2023-06-09 17:30:20 +08:00
|
|
|
thread_id, sizeof(struct maat_state));
|
2023-03-15 11:36:54 +08:00
|
|
|
}
|
|
|
|
|
|
2023-06-19 09:44:25 +00:00
|
|
|
int maat_state_set_scan_district(struct maat_state *state, int table_id,
|
2023-04-04 15:59:34 +08:00
|
|
|
const char *district, size_t district_len)
|
2022-12-14 15:28:21 +08:00
|
|
|
{
|
2023-03-23 11:57:17 +08:00
|
|
|
if (NULL == state || NULL == district || 0 == district_len) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
struct maat *maat_inst = state->maat_inst;
|
|
|
|
|
assert(maat_inst != NULL);
|
2023-03-23 11:57:17 +08:00
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
if (NULL == maat_inst->maat_rt) {
|
|
|
|
|
log_error(maat_inst->logger, MODULE_MAAT_API,
|
2023-04-04 15:59:34 +08:00
|
|
|
"maat runtime is NULL, can't set district");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-19 09:44:25 +00:00
|
|
|
enum table_type table_type = TABLE_TYPE_INVALID;
|
|
|
|
|
table_type = table_manager_get_table_type(maat_inst->tbl_mgr, table_id);
|
2023-04-04 15:59:34 +08:00
|
|
|
if (table_type != TABLE_TYPE_FLAG_PLUS && table_type != TABLE_TYPE_EXPR_PLUS &&
|
|
|
|
|
table_type != TABLE_TYPE_INTERVAL_PLUS && table_type != TABLE_TYPE_VIRTUAL) {
|
2023-06-16 15:59:30 +08:00
|
|
|
log_error(maat_inst->logger, MODULE_MAAT_API,
|
2023-04-04 15:59:34 +08:00
|
|
|
"table_type:%d is invalid, can't set district", table_type);
|
2023-01-30 21:59:35 +08:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-19 09:44:25 +00:00
|
|
|
int phy_table_id = table_id;
|
2023-04-04 15:59:34 +08:00
|
|
|
if (table_type == TABLE_TYPE_VIRTUAL) {
|
2023-06-19 09:44:25 +00:00
|
|
|
phy_table_id = vtable_get_physical_table_id(maat_inst->tbl_mgr, table_id);
|
2023-04-04 15:59:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int ret = -1;
|
2023-06-09 16:44:47 +08:00
|
|
|
long long district_id;
|
2023-06-19 09:44:25 +00:00
|
|
|
|
|
|
|
|
table_type = table_manager_get_table_type(maat_inst->tbl_mgr, phy_table_id);
|
|
|
|
|
void *runtime = table_manager_get_runtime(maat_inst->tbl_mgr, phy_table_id);
|
2023-04-04 15:59:34 +08:00
|
|
|
assert(runtime != NULL);
|
|
|
|
|
|
|
|
|
|
switch (table_type) {
|
|
|
|
|
case TABLE_TYPE_FLAG_PLUS:
|
2023-06-08 07:32:41 +00:00
|
|
|
ret = flag_runtime_set_scan_district((struct flag_runtime *)runtime,
|
2023-06-09 16:44:47 +08:00
|
|
|
district, district_len, &district_id);
|
2023-04-04 15:59:34 +08:00
|
|
|
break;
|
|
|
|
|
case TABLE_TYPE_EXPR_PLUS:
|
2023-06-08 07:32:41 +00:00
|
|
|
ret = expr_runtime_set_scan_district((struct expr_runtime *)runtime,
|
2023-06-09 16:44:47 +08:00
|
|
|
district, district_len, &district_id);
|
2023-04-04 15:59:34 +08:00
|
|
|
break;
|
|
|
|
|
case TABLE_TYPE_INTERVAL_PLUS:
|
2023-06-08 07:32:41 +00:00
|
|
|
ret = interval_runtime_set_scan_district((struct interval_runtime *)runtime,
|
2023-06-09 16:44:47 +08:00
|
|
|
district, district_len, &district_id);
|
2023-04-04 15:59:34 +08:00
|
|
|
break;
|
|
|
|
|
default:
|
2023-06-16 15:59:30 +08:00
|
|
|
log_error(maat_inst->logger, MODULE_MAAT_API,
|
2023-04-04 15:59:34 +08:00
|
|
|
"[%s:%d] table_type:%d can't set district",
|
|
|
|
|
__FUNCTION__, __LINE__, table_type);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-03 17:28:14 +08:00
|
|
|
if (ret < 0) {
|
2023-03-23 11:57:17 +08:00
|
|
|
state->district_id = DISTRICT_UNKNOWN;
|
2023-06-09 16:44:47 +08:00
|
|
|
} else {
|
|
|
|
|
state->district_id = (int)district_id;
|
2023-01-30 21:59:35 +08:00
|
|
|
}
|
2022-12-14 15:28:21 +08:00
|
|
|
|
2023-03-23 11:57:17 +08:00
|
|
|
state->is_set_district = DISTRICT_FLAG_SET;
|
2023-01-30 21:59:35 +08:00
|
|
|
|
|
|
|
|
return 0;
|
2022-12-14 15:28:21 +08:00
|
|
|
}
|
|
|
|
|
|
2023-03-23 11:57:17 +08:00
|
|
|
int maat_state_set_last_scan(struct maat_state *state)
|
2022-12-14 15:28:21 +08:00
|
|
|
{
|
2023-03-23 11:57:17 +08:00
|
|
|
if (NULL == state) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
struct maat *maat_inst = state->maat_inst;
|
|
|
|
|
assert(maat_inst != NULL);
|
2023-03-23 11:57:17 +08:00
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
if (NULL == maat_inst->maat_rt) {
|
2023-01-30 21:59:35 +08:00
|
|
|
return -1;
|
|
|
|
|
}
|
2022-12-14 15:28:21 +08:00
|
|
|
|
2023-03-23 11:57:17 +08:00
|
|
|
assert(state->is_last_scan == LAST_SCAN_UNSET);
|
|
|
|
|
state->is_last_scan = LAST_SCAN_SET;
|
2023-01-30 21:59:35 +08:00
|
|
|
|
|
|
|
|
return 0;
|
2022-12-14 15:28:21 +08:00
|
|
|
}
|
|
|
|
|
|
2023-03-23 11:57:17 +08:00
|
|
|
int maat_state_set_scan_compile_table(struct maat_state *state, int compile_table_id)
|
2023-01-06 18:54:59 +08:00
|
|
|
{
|
2023-03-23 11:57:17 +08:00
|
|
|
if (NULL == state || compile_table_id < 0) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
struct maat *maat_inst = state->maat_inst;
|
|
|
|
|
assert(maat_inst != NULL);
|
2023-03-23 11:57:17 +08:00
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
if (NULL == maat_inst->maat_rt) {
|
2023-01-30 21:59:35 +08:00
|
|
|
return -1;
|
|
|
|
|
}
|
2023-01-06 18:54:59 +08:00
|
|
|
|
2023-03-23 11:57:17 +08:00
|
|
|
state->compile_table_id = compile_table_id;
|
2023-01-30 21:59:35 +08:00
|
|
|
|
|
|
|
|
return 0;
|
2023-01-06 18:54:59 +08:00
|
|
|
}
|
|
|
|
|
|
2023-06-08 07:32:41 +00:00
|
|
|
int maat_state_get_hit_paths(struct maat_state *state, struct maat_hit_path *paths,
|
|
|
|
|
size_t n_path)
|
2022-11-17 05:05:35 +08:00
|
|
|
{
|
2023-03-23 11:57:17 +08:00
|
|
|
if (NULL == state || NULL == paths || 0 == n_path) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
struct maat *maat_inst = state->maat_inst;
|
|
|
|
|
assert(maat_inst != NULL);
|
2023-03-23 11:57:17 +08:00
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
if (NULL == maat_inst->maat_rt) {
|
2023-03-23 11:57:17 +08:00
|
|
|
return -1;
|
|
|
|
|
}
|
2023-01-30 21:59:35 +08:00
|
|
|
|
2023-06-19 09:44:25 +00:00
|
|
|
int compile_table_id = table_manager_get_default_compile_table_id(maat_inst->tbl_mgr);
|
2023-03-30 20:49:59 +08:00
|
|
|
if (state->compile_table_id > 0) {
|
2023-03-22 20:40:36 +08:00
|
|
|
compile_table_id = state->compile_table_id;
|
2023-01-30 21:59:35 +08:00
|
|
|
}
|
2022-11-17 05:05:35 +08:00
|
|
|
|
2023-06-19 09:44:25 +00:00
|
|
|
void *compile_rt = table_manager_get_runtime(maat_inst->tbl_mgr, compile_table_id);
|
2023-03-30 20:49:59 +08:00
|
|
|
if (NULL == compile_rt) {
|
2023-05-17 14:34:34 +08:00
|
|
|
return -1;
|
2023-03-30 20:49:59 +08:00
|
|
|
}
|
2023-05-17 14:34:34 +08:00
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
int g2g_table_id = table_manager_get_group2group_table_id(maat_inst->tbl_mgr);
|
|
|
|
|
void *g2g_runtime = table_manager_get_runtime(maat_inst->tbl_mgr, g2g_table_id);
|
2023-06-14 09:10:16 +00:00
|
|
|
|
2023-05-17 14:34:34 +08:00
|
|
|
size_t internal_hit_path_cnt = maat_compile_state_get_internal_hit_paths(state->compile_state,
|
|
|
|
|
(struct compile_runtime *)compile_rt,
|
|
|
|
|
(struct group2group_runtime *)g2g_runtime,
|
|
|
|
|
paths, n_path);
|
|
|
|
|
|
2023-06-09 16:44:47 +08:00
|
|
|
return compile_runtime_get_hit_paths((struct compile_runtime *)compile_rt, state->thread_id,
|
|
|
|
|
state->compile_state, paths, n_path, internal_hit_path_cnt);
|
2023-01-30 21:59:35 +08:00
|
|
|
}
|
|
|
|
|
|
2023-06-05 15:14:45 +08:00
|
|
|
int maat_state_get_hit_groups(struct maat_state *state, struct maat_hit_group *groups,
|
|
|
|
|
size_t n_group)
|
2023-01-30 21:59:35 +08:00
|
|
|
{
|
2023-06-05 15:14:45 +08:00
|
|
|
if (NULL == state || NULL == groups || 0 == n_group) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-16 15:59:30 +08:00
|
|
|
int g2g_table_id = table_manager_get_group2group_table_id(state->maat_inst->tbl_mgr);
|
|
|
|
|
void *g2g_runtime = table_manager_get_runtime(state->maat_inst->tbl_mgr, g2g_table_id);
|
2023-06-14 09:10:16 +00:00
|
|
|
|
2023-06-09 16:44:47 +08:00
|
|
|
return maat_compile_state_get_hit_groups(state->compile_state,
|
|
|
|
|
(struct group2group_runtime *)g2g_runtime,
|
|
|
|
|
groups, n_group);
|
2023-01-30 21:59:35 +08:00
|
|
|
}
|
|
|
|
|
|
2023-06-05 15:14:45 +08:00
|
|
|
int maat_hit_group_compile_id(struct maat *instance, struct maat_hit_group *group)
|
2023-01-30 21:59:35 +08:00
|
|
|
{
|
|
|
|
|
return 0;
|
2023-06-08 07:32:41 +00:00
|
|
|
}
|