完成发送给tfe部分修改

This commit is contained in:
崔一鸣
2019-08-19 17:23:15 +08:00
parent 47eacf2c3a
commit 020782a71a
2 changed files with 100 additions and 9 deletions

View File

@@ -43,6 +43,12 @@ enum stream_error{
STREAM_ERROR_SENDTO_TFE_FAIL = -11,
};
enum sendto_tfe_mode{
SENDTO_TFE_MODE_TAP = 0,
SENDTO_TFE_MODE_NORMAL = 1,
};
SENDTO_TFE_MODE_TAP
struct http_project{
int host_len;
char host[KNI_DOMAIN_MAX];
@@ -141,6 +147,8 @@ struct thread_tfe_cmsg_receiver_args{
struct kni_handle{
int http_project_id;
struct kni_marsio_handle *marsio_handle;
struct kni_tap_handle *tap_handle;
enum sendto_tfe_mode _sendto_tfe_mode;
struct kni_maat_handle *maat_handle;
struct kni_send_logger *send_logger;
MESA_htable_handle traceid2pme_htable;
@@ -668,8 +676,9 @@ static char* add_cmsg_to_packet(struct pme_info *pmeinfo, struct pkt_info *pktin
return new_pkt;
}
static int send_to_tfe_normal_mode(struct kni_marsio_handle *handle, char *raw_data, uint16_t raw_len, int thread_seq, int tfe_id, addr_type_t addr_type){
static int send_to_tfe_normal_mode(char *raw_data, uint16_t raw_len, int thread_seq, int tfe_id, addr_type_t addr_type){
void *logger = g_kni_handle->local_logger;
struct kni_marsio_handle *handle = g_kni_handle->marsio_handle;
marsio_buff_t *tx_buffs[BURST_MAX];
int index = -1;
for(int i = 0; i < handle->tfe_enabled_node_count; i++){
@@ -712,13 +721,19 @@ static int send_to_tfe_normal_mode(struct kni_marsio_handle *handle, char *raw_d
return 0;
}
static int send_to_tfe(struct tap_mode_handle *handle, char *raw_data, uint16_t raw_len){
int ret;
ret = tap_mode_write()
static int send_to_tfe_tap_mode(char *raw_data, uint16_t raw_len){
struct kni_tap_handle *handle = g_kni_handle->tap_handle;
int ret = kni_tap_write(handle, raw_data, raw_len);
if(ret < 0){
return -1;
}
return 0;
}
static int send_to_tfe(struct kni_marsio_handle *marsio_handle, struct tap_mode_handle *tap_handle, char *raw_data,
uint16_t raw_len, int thread_seq, int tfe_id, addr_type_t addr_type, int mode){
static int send_to_tfe(char *raw_data, uint16_t raw_len, int thread_seq, int tfe_id, addr_type_t addr_type){
struct kni_marsio_handle *marsio_handle = g_kni_handle->marsio_handle;
struct kni_tap_handle *tap_handle = g_kni_handle->tap_handle;
int mode = g_kni_handle->_sendto_tfe_mode;
int ret;
if(mode == SENDTO_TFE_MODE_TAP){
ret = send_to_tfe_tap_mode(tap_handle, raw_data, raw_len);
@@ -873,7 +888,7 @@ static int first_data_intercept(const struct streaminfo *stream, struct pme_info
//action = KNI_ACTION_INTERCEPT, sendto tfe
int len = 0;
char *buff = add_cmsg_to_packet(pmeinfo, pktinfo, &len);
ret = send_to_tfe(g_kni_handle->marsio_handle, g_kni_handle->tap_mode_handle, buff, len, thread_seq, pmeinfo->tfe_id, pmeinfo->addr_type);
ret = send_to_tfe(buff, len, thread_seq, pmeinfo->tfe_id, pmeinfo->addr_type);
if(ret < 0){
KNI_LOG_ERROR(logger, "Failed at send first packet to tfe%d, stream traceid is %s", pmeinfo->tfe_id, pmeinfo->stream_traceid);
FS_operate(g_kni_fs_handle->handle, g_kni_fs_handle->fields[KNI_FIELD_SENDTO_TFE_FAIL], 0, FS_OP_ADD, 1);
@@ -920,7 +935,7 @@ static char data_opstate(const struct streaminfo *stream, struct pme_info *pmein
ipv4_hdr = (struct iphdr*)a_packet;
len = ntohs(ipv4_hdr->tot_len);
}
ret = send_to_tfe(g_kni_handle->marsio_handle, g_kni_handle->tap_mode_handle, (char*)a_packet, len, thread_seq, pmeinfo->tfe_id, pmeinfo->addr_type);
ret = send_to_tfe((char*)a_packet, len, thread_seq, pmeinfo->tfe_id, pmeinfo->addr_type);
if(ret < 0){
KNI_LOG_ERROR(logger, "Failed at send continue packet to tfe%d, stream traceid is %s", pmeinfo->tfe_id, pmeinfo->stream_traceid);
FS_operate(g_kni_fs_handle->handle, g_kni_fs_handle->fields[KNI_FIELD_SENDTO_TFE_FAIL], 0, FS_OP_ADD, 1);
@@ -1292,7 +1307,7 @@ static long keepalive_replay_search_cb(void *data, const uchar *key, uint size,
replay_packet_iphdr->saddr, replay_packet_iphdr->daddr);
}
//send to tfe: thread_seq = g_iThreadNum
int ret = send_to_tfe(marsio_handle, g_kni_handle->tap_mode_handle, replay_packet, tot_len, g_iThreadNum + thread_seq, tfe_id, args->addr_type);
int ret = send_to_tfe(replay_packet, tot_len, g_iThreadNum + thread_seq, tfe_id);
if(ret < 0){
KNI_LOG_ERROR(logger, "Failed at send keepalive replay packet to tfe");
FS_operate(g_kni_fs_handle->handle, g_kni_fs_handle->fields[KNI_FIELD_SENDTO_TFE_FAIL], 0, FS_OP_ADD, 1);
@@ -1885,6 +1900,8 @@ extern "C" int kni_init(){
goto error_out;
}
g_kni_handle->tap_handle = kni_tap_init()
//init maat
g_kni_handle->maat_handle = kni_maat_init(profile, local_logger);
if(g_kni_handle->maat_handle == NULL){

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;
}