diff --git a/oaf/src/af_client.c b/oaf/src/af_client.c index 02232e8..9bb143f 100755 --- a/oaf/src/af_client.c +++ b/oaf/src/af_client.c @@ -21,6 +21,7 @@ #include "af_client.h" #include "af_client_fs.h" #include "af_log.h" +#include "af_utils.h" DEFINE_RWLOCK(af_client_lock); @@ -146,6 +147,50 @@ void check_client_expire(void) AF_CLIENT_UNLOCK_W(); } +#define MAX_EXPIRED_VISIT_INFO_COUNT 10 +void flush_expired_visit_info(af_client_info_t *node) +{ + int i; + int count = 0; + u_int32_t cur_timep = 0; + int timeout = 0; + cur_timep = af_get_timestamp_sec(); + for (i = 0; i < MAX_RECORD_APP_NUM; i++){ + if (node->visit_info[i].app_id == 0){ + return; + } + } + for (i = 0; i < MAX_RECORD_APP_NUM; i++){ + if (count >= MAX_EXPIRED_VISIT_INFO_COUNT) + break; + + if (node->visit_info[i].total_num > 3){ + timeout = 180; + } + else{ + timeout = 60; + } + + if (cur_timep - node->visit_info[i].latest_time > timeout){ + // 超时清除记录 + memset(&node->visit_info[i], 0x0, sizeof(app_visit_info_t)); + count++; + } + } + +} + +void af_visit_info_timer_handle(void){ + af_client_info_t *node; + int i; + AF_CLIENT_LOCK_W(); + for (i = 0; i < MAX_AF_CLIENT_HASH_SIZE; i++){ + list_for_each_entry(node, &af_client_list_table[i], hlist) { + flush_expired_visit_info(node); + } + } + AF_CLIENT_UNLOCK_W(); +} static inline int get_packet_dir(struct net_device *in) { if (0 == strncmp(in->name, "br", 2)){ diff --git a/oaf/src/af_client.h b/oaf/src/af_client.h index c391241..859bc5b 100755 --- a/oaf/src/af_client.h +++ b/oaf/src/af_client.h @@ -30,7 +30,7 @@ enum NFC_PKT_DIR{ #define MAX_VISIT_HISTORY_TIME 24 -#define MAX_RECORD_APP_NUM 32 +#define MAX_RECORD_APP_NUM 64 typedef struct app_visit_info{ @@ -62,4 +62,8 @@ int af_client_init(void); void af_client_exit(void); af_client_info_t * find_af_client_by_ip(unsigned int ip); +void check_client_expire(void); + +void af_visit_info_timer_handle(void); + #endif diff --git a/oaf/src/af_client_fs.c b/oaf/src/af_client_fs.c index 8a64fff..a86c33c 100755 --- a/oaf/src/af_client_fs.c +++ b/oaf/src/af_client_fs.c @@ -131,6 +131,8 @@ static int af_client_seq_show(struct seq_file *s, void *v) for(i = 0; i < MAX_RECORD_APP_NUM; i++){ if(node->visit_info[i].app_id == 0) break; + if(node->visit_info[i].total_num < 3) + break; cJSON *visit_obj = cJSON_CreateObject(); cJSON_AddNumberToObject(visit_obj, "appid", node->visit_info[i].app_id); cJSON_AddNumberToObject(visit_obj, "latest_action", node->visit_info[i].latest_action); diff --git a/oaf/src/af_utils.c b/oaf/src/af_utils.c index 3ed23d4..5ea2daf 100755 --- a/oaf/src/af_utils.c +++ b/oaf/src/af_utils.c @@ -6,6 +6,19 @@ #include #include #include "af_utils.h" +u_int32_t af_get_timestamp_sec(void) +{ +#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,17,0) + struct timespec64 ts; + ktime_get_ts64(&ts); + return (u_int32_t)ts.tv_sec; +#else + struct timespec ts; + ts = current_kernel_time(); + return ts.tv_sec; +#endif + +} int check_local_network_ip(unsigned int ip) { diff --git a/oaf/src/af_utils.h b/oaf/src/af_utils.h index 201adfc..a57f3f2 100755 --- a/oaf/src/af_utils.h +++ b/oaf/src/af_utils.h @@ -1,5 +1,6 @@ #ifndef AF_UTILS_H #define AF_UTILS_H +u_int32_t af_get_timestamp_sec(void); int check_local_network_ip(unsigned int ip); diff --git a/oaf/src/app_filter.c b/oaf/src/app_filter.c index 8035631..35d7c51 100755 --- a/oaf/src/app_filter.c +++ b/oaf/src/app_filter.c @@ -707,22 +707,21 @@ int app_filter_match(flow_info_t *flow) } #define APP_FILTER_DROP_BITS 0xf0000000 -u_int32_t af_get_timestamp_sec(void) -{ -#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,17,0) - struct timespec64 ts; - ktime_get_ts64(&ts); - return (u_int32_t)ts.tv_sec; -#else - struct timespec ts; - ts = current_kernel_time(); - return ts.tv_sec; -#endif + + +static int af_get_visit_index(af_client_info_t *node, int app_id){ + int i; + for(i = 0; i < MAX_RECORD_APP_NUM; i++){ + if(node->visit_info[i].app_id == app_id || node->visit_info[i].app_id == 0){ + return i; + } + } + // default 0 + return 0; } - int __af_update_client_app_info(flow_info_t *flow, af_client_info_t *node) { int i; @@ -734,38 +733,13 @@ int __af_update_client_app_info(flow_info_t *flow, af_client_info_t *node) AF_INFO("%s %d visit_app_num = %d\n", __func__, __LINE__, node->visit_app_num); int found = 0; - for(i = 0; i < MAX_RECORD_APP_NUM; i++){ - if(node->visit_info[i].app_id == flow->app_id){ - index = i; - found = 1; - break; - } - if(node->visit_info[i].app_id == 0) - break; - } + index = af_get_visit_index(node, flow->app_id); - if(!found){ - index = 0; - //超过最大个数,查询最老的 - for(i = 0; i < MAX_RECORD_APP_NUM; i++){ - if(node->visit_info[i].latest_time == 0){ - index = i; - break; - } - if(node->visit_info[i].latest_time < node->visit_info[index].latest_time){ - // 清除之前的数据 - node->visit_info[i].total_num = 0; - node->visit_info[i].drop_num = 0; - index = i; - } - } - } - - if(index < 0 || index >= MAX_RECORD_APP_NUM){ AF_ERROR("invalid index:%d\n\n", index); return 0; } + node->visit_info[index].total_num++; if(flow->drop) node->visit_info[index].drop_num++; @@ -925,6 +899,31 @@ void TEST_cJSON(void) } +struct timer_list oaf_timer; + +#define OAF_TIMER_INTERVAL 15 +static void oaf_timer_func(unsigned long ptr) +{ +// check_client_expire(); + af_visit_info_timer_handle(); + mod_timer(&oaf_timer, jiffies + OAF_TIMER_INTERVAL * HZ); +} + + +void init_oaf_timer(void) +{ + setup_timer(&oaf_timer, oaf_timer_func, OAF_TIMER_INTERVAL * HZ); + mod_timer(&oaf_timer, jiffies + OAF_TIMER_INTERVAL * HZ); + AF_INFO("init oaf timer...ok"); +} + +void fini_port_timer(void) +{ + del_timer_sync(&oaf_timer); + AF_INFO("del oaf timer...ok"); +} + + /* 模块初始化 */ @@ -943,6 +942,8 @@ static int __init app_filter_init(void) #else nf_register_hooks(app_filter_ops, ARRAY_SIZE(app_filter_ops)); #endif + init_oaf_timer(); + AF_INFO("init app filter ........ok\n"); return 0; } @@ -953,6 +954,7 @@ static int __init app_filter_init(void) static void app_filter_fini(void) { AF_INFO("app filter module exit\n"); + fini_port_timer(); #if LINUX_VERSION_CODE >= KERNEL_VERSION(4,13,0) nf_unregister_net_hooks(&init_net, app_filter_ops, ARRAY_SIZE(app_filter_ops)); #else