完成polling接口改造和重复流量识别功能

This commit is contained in:
崔一鸣
2019-09-04 18:58:50 +08:00
parent b01f282f34
commit 94e8c6184d
15 changed files with 886 additions and 641 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -1,8 +1,6 @@
#include "kni_utils.h"
#include "kni_maat.h"
extern int g_iThreadNum;
/* default action:
1. read kni.conf
@@ -62,7 +60,7 @@ void compile_ex_param_dup(int idx, MAAT_RULE_EX_DATA *to, MAAT_RULE_EX_DATA *fro
return;
}
struct kni_maat_handle* kni_maat_init(const char* profile, void *logger){
struct kni_maat_handle* kni_maat_init(const char* profile, void *logger, int thread_count){
const char *section = "maat";
int readconf_mode;
char tableinfo_path[KNI_PATH_MAX];
@@ -105,11 +103,11 @@ struct kni_maat_handle* kni_maat_init(const char* profile, void *logger){
KNI_LOG_ERROR(logger, "MESA_prof_load, [%s]:\n readconf_mode: %d\n tableinfo_path: %s\n tablename_intercept_ip: %s\n tablename_intercept_domain: %s\n"
"default_action: %d", section, readconf_mode, tableinfo_path, tablename_intercept_ip,
tablename_intercept_domain, g_maat_default_action);
feather = Maat_feather(g_iThreadNum, tableinfo_path, logger);
feather = Maat_feather(thread_count, tableinfo_path, logger);
handle = ALLOC(struct kni_maat_handle, 1);
handle->feather = feather;
if(feather == NULL){
KNI_LOG_ERROR(logger, "Failed at Maat_feather, max_thread_num is %d, tableinfo_path is %s", g_iThreadNum, tableinfo_path);
KNI_LOG_ERROR(logger, "Failed at Maat_feather, max_thread_num is %d, tableinfo_path is %s", thread_count, tableinfo_path);
return NULL;
}
Maat_set_feather_opt(feather, MAAT_OPT_EFFECT_INVERVAL_MS, &effective_interval_ms, sizeof(effective_interval_ms));

74
entry/src/kni_tap.cpp Normal file
View File

@@ -0,0 +1,74 @@
#include <sys/ioctl.h>
#include <fcntl.h>
#include <linux/if_tun.h>
#include <stdio.h>
#include <stdlib.h>
#include "kni_utils.h"
#include "MESA_prof_load.h"
struct kni_tap_handle{
int fd;
void *logger;
};
struct kni_tap_handle* kni_tap_init(void *logger){
struct kni_tap_handle * tap_handle = (struct kni_tap_handle*)malloc(sizeof(struct kni_tap_handle));
char tap_path[1024] = "tap";
char tap_name[IFNAMSIZ] = {0};
struct ifreq ifr;
int err = 0;
MESA_load_profile_string_def(".kniconf/kni.conf", "tap", (char*)"tap_path", tap_path, 1024, "/dev/net/tap");
MESA_load_profile_string_def(".kniconf/kni.conf", "tap", (char*)"tap_name", tap_name, 1024, "/dev/net/tap");
memset(&ifr, 0, sizeof(ifr));
ifr.ifr_flags = IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE;
if(*tap_name){
strncpy(ifr.ifr_name, tap_name, IFNAMSIZ);
}
if((tap_handle ->fd = open(tap_path, O_RDWR)) < 0){
KNI_LOG_ERROR(logger, "kni_tap_init():open error,errno is:%d,%s",errno,strerror(errno));
free(tap_handle);
return NULL;
}
err = ioctl(tap_handle ->fd, TUNSETIFF, (void *)&ifr);
if(err) {
KNI_LOG_ERROR(logger ,"kni_tap_init():ioctl error,errno is:%d,%s",errno,strerror(errno));
close(tap_handle ->fd);
free(tap_handle);
return NULL;
}
tap_handle->logger = logger;
retrun tap_handle;
}
int kni_tap_write(struct kni_tap_handle *handle, char *buff, uint16_t buff_len){
uint16_t send_len = write(handle->fd, buff, buff_len);
if(send_len < 0){
KNI_LOG_ERROR(handle->logger, "Failed at kni_tap_write, errno = %d(%s)", errno, strerror(errno));
return -1;
}
if(send_len < buff_len){
KNI_LOG_ERROR(handle->logger, "kni_tap_write: need send %dB, only send %dB", buff_len, send_len);
return -2;
}
return 0;
}
/*
* > 0 : read data length
* = 0 : read null
* = -1 : error
*/
int kni_tap_read(struct kni_tap_handle *handle, char *buff, uint16_t buff_len){
int recv_len = 0;
recv_len = read(handle -> fd, buff, buff_len);
if(recv_len < 0){
KNI_LOG_ERROR(handle -> logger, "kni_tap_read() error %d, %s", errno, strerror(errno));
return -1;
}
else{
return recv_len;
}
return 0;
}