diff --git a/common/include/kni_utils.h b/common/include/kni_utils.h index 10bddba..0bdb946 100644 --- a/common/include/kni_utils.h +++ b/common/include/kni_utils.h @@ -15,6 +15,7 @@ #include "field_stat2.h" #include "Maat_rule.h" #include "Maat_command.h" +#include "mrtunnat.h" #define KNI_STRING_MAX 2048 #define KNI_PATH_MAX 256 diff --git a/common/src/ssl_utils.cpp b/common/src/ssl_utils.cpp index b742b27..0d0bf24 100644 --- a/common/src/ssl_utils.cpp +++ b/common/src/ssl_utils.cpp @@ -468,8 +468,8 @@ struct ssl_chello* ssl_chello_parse(const unsigned char* buff, size_t buff_len, *result = CHELLO_PARSE_INVALID_FORMAT; return _chello; } - enum chello_parse_result rtn = parse_extensions(buff + pos, len, _chello); - *result = rtn; + enum chello_parse_result ret = parse_extensions(buff + pos, len, _chello); + *result = ret; return _chello; } } diff --git a/conf/kni.conf b/conf/kni.conf index a15baa1..b159024 100644 --- a/conf/kni.conf +++ b/conf/kni.conf @@ -12,7 +12,8 @@ compile_alias = COMPILE_ALIAS [marsio] appsym = knifw -dev_symbol = eth4 +dev_eth_symbol = eth4 +dev_vxlan_symbol = vxlan_user [field_stat] stat_path = ./fs2_kni.status diff --git a/entry/include/kni_entry.h b/entry/include/kni_entry.h index e2c5709..25f16c0 100644 --- a/entry/include/kni_entry.h +++ b/entry/include/kni_entry.h @@ -32,8 +32,10 @@ struct tcp_option_restore{ struct kni_marsio_handle{ struct mr_instance *instance; - struct mr_vdev *dev_handler; - struct mr_sendpath *dev_sendpath; + struct mr_vdev *dev_eth_handler; + struct mr_vdev *dev_vxlan_handler; + struct mr_sendpath *dev_eth_sendpath; + struct mr_sendpath *dev_vxlan_sendpath; }; struct protocol_identify_result{ @@ -42,6 +44,12 @@ struct protocol_identify_result{ int domain_len; }; + +struct tfe_receiver_args{ + void *logger; + struct kni_marsio_handle *marsio_handle; +}; + //TODO: 有些字段可以不要 struct pkt_info{ struct iphdr *iphdr; @@ -65,6 +73,7 @@ enum tcp_restore_info_tlv_type TCP_RESTORE_INFO_TLV_SACK_SERVER, TCP_RESTORE_INFO_TLV_TS_CLIENT, TCP_RESTORE_INFO_TLV_TS_SERVER, + TCP_RESTORE_INFO_TLV_PROTOCOL, TCP_RESTORE_INFO_TLV_USER_DEFINED }; diff --git a/entry/src/kni_entry.cpp b/entry/src/kni_entry.cpp index e0a7ed5..34f843a 100644 --- a/entry/src/kni_entry.cpp +++ b/entry/src/kni_entry.cpp @@ -112,6 +112,8 @@ static struct tcp_restore_info_header* tcp_restore_info_header_new(struct pme_in tcp_restore_info_tlv_add(TCP_RESTORE_INFO_TLV_SACK_SERVER, 1, pmeinfo->server_tcpopt->sack, (char*)header, &offset, &nr_tlvs); tcp_restore_info_tlv_add(TCP_RESTORE_INFO_TLV_TS_CLIENT, 1, pmeinfo->client_tcpopt->ts, (char*)header, &offset, &nr_tlvs); tcp_restore_info_tlv_add(TCP_RESTORE_INFO_TLV_TS_SERVER, 1, pmeinfo->server_tcpopt->ts, (char*)header, &offset, &nr_tlvs); + uint8_t protocol_type = pmeinfo->protocol == KNI_PROTOCOL_SSL ? 0x1 : 0x0; + tcp_restore_info_tlv_add(TCP_RESTORE_INFO_TLV_PROTOCOL, 1, protocol_type, (char*)header, &offset, &nr_tlvs); header->__magic__[0] = 0x4d; header->__magic__[1] = 0x5a; header->nr_tlvs = htons(nr_tlvs); @@ -168,7 +170,8 @@ static int send_to_tfe(struct kni_marsio_handle *handle, char *raw_data, int raw marsio_buff_t *tx_buffs[BURST_MAX]; unsigned int ret = 1; //TODO: marsio配置文件: 2500 - int alloc_ret = marsio_buff_malloc_device(handle->dev_handler, tx_buffs, ret, 0, thread_seq); + //thread_seq实际上是网卡队列,一个线程对应一个网卡队列, 并不是线程号和网卡队列号一一对应,假设线程号是tid,网卡队列为n,那么tid % n就是网卡队列号 + int alloc_ret = marsio_buff_malloc_device(handle->dev_eth_handler, tx_buffs, ret, 0, thread_seq); if (alloc_ret < 0){ KNI_LOG_ERROR(logger, "Failed at alloc marsio buffer, ret is %d, thread_seq is %d", ret, thread_seq); return -1; @@ -177,7 +180,7 @@ static int send_to_tfe(struct kni_marsio_handle *handle, char *raw_data, int raw unsigned char ethernet_header[14] = {0xfe, 0x65, 0xb7, 0x03, 0x50, 0xbd, 0xe8, 0x61, 0x1f, 0x13, 0x70, 0x7a, 0x08, 0x00}; memcpy(dst_data, ethernet_header, 14); memcpy((char*)dst_data + 14, raw_data, raw_len); - marsio_send_burst(handle->dev_sendpath, thread_seq, tx_buffs, ret); + marsio_send_burst(handle->dev_eth_sendpath, thread_seq, tx_buffs, ret); return 0; } @@ -391,17 +394,47 @@ static void kni_marsio_destroy(struct kni_marsio_handle *handle){ FREE(&handle); } +void * thread_tfe_receiver(void* args){ + struct tfe_receiver_args *_args = (struct tfe_receiver_args*)args; + //void *logger = _args->logger; + struct kni_marsio_handle *marsio_handle = _args->marsio_handle; + marsio_buff_t * rx_buff[BURST_MAX]; + int nr_burst = 1; + //实际上是网卡队列id + int thread_seq = 0; + while(true){ + //从tfe上收 + int ret = marsio_recv_burst(marsio_handle->dev_eth_handler, thread_seq, rx_buff, nr_burst); + if(ret <= 0){ + continue; + } + //打上标签 + struct mr_tunnat_ctrlzone mr_ctrlzone; + mr_ctrlzone.action |= TUNNAT_CZ_ACTION_ENCAP_INNER | TUNNAT_CZ_ACTION_ENCAP_OUTER; + for(int i = 0; i < ret; i++){ + marsio_buff_ctrlzone_set(rx_buff[i], 0, &mr_ctrlzone, sizeof(struct mr_tunnat_ctrlzone)); + } + //发送给vxlan + marsio_send_burst_with_options(marsio_handle->dev_vxlan_sendpath, thread_seq, rx_buff, 1, MARSIO_SEND_OPT_FAST); + } + return NULL; +} + + static struct kni_marsio_handle* kni_marsio_init(const char* profile){ void *logger = g_kni_handle->logger; const char* section = "marsio"; char appsym[KNI_SYMBOL_MAX]; - char dev_symbol[KNI_SYMBOL_MAX]; + char dev_eth_symbol[KNI_SYMBOL_MAX]; + char dev_vxlan_symbol[KNI_SYMBOL_MAX]; MESA_load_profile_string_def(profile, section, "appsym", appsym, sizeof(appsym), "unknown"); - MESA_load_profile_string_def(profile, section, "dev_symbol", dev_symbol, sizeof(dev_symbol), "unknown"); - KNI_LOG_INFO(logger, "MESA_prof_load, [%s]:\n appsym: %s\n dev_symbol: %s", section, appsym, dev_symbol); + MESA_load_profile_string_def(profile, section, "dev_eth_symbol", dev_eth_symbol, sizeof(dev_eth_symbol), "unknown"); + MESA_load_profile_string_def(profile, section, "dev_vxlan_symbol", dev_vxlan_symbol, sizeof(dev_vxlan_symbol), "unknown"); + KNI_LOG_INFO(logger, "MESA_prof_load, [%s]:\n appsym: %s\n dev_eth_symbol: %s\n dev_vxlan_symbol: %s", + section, appsym, dev_eth_symbol, dev_vxlan_symbol); struct mr_instance *instance = marsio_create(); if(instance == NULL){ - KNI_LOG_ERROR(logger, "Failed at marsio_create"); + KNI_LOG_ERROR(logger, "Failed at create marsio instance"); return NULL; } unsigned int opt_value = 1; @@ -409,24 +442,49 @@ static struct kni_marsio_handle* kni_marsio_init(const char* profile){ //uint64_t cpu_mask = 0x3c; //?? //marsio_option_set(handle->instance, MARSIO_OPT_THREAD_MASK, &cpu_mask, sizeof(cpu_mask)); marsio_init(instance, appsym); - //设为sapp线程数 - int nr_thread = g_iThreadNum; - struct mr_vdev * dev_handler = marsio_open_device(instance, dev_symbol, nr_thread, nr_thread); - if(dev_handler == NULL){ - KNI_LOG_ERROR(logger, "Failed at marsio_open_device, dev_symbol is %s, nr_thread is %d", dev_symbol, nr_thread); + //eth_handler有一个线程收, g_iThreadNum个线程发 + struct mr_vdev *dev_eth_handler = marsio_open_device(instance, dev_eth_symbol, 1, g_iThreadNum); + if(dev_eth_handler == NULL){ + KNI_LOG_ERROR(logger, "Failed at marsio_open_device, dev_symbol is %s", dev_eth_symbol); return NULL; } - struct mr_sendpath * dev_sendpath = marsio_sendpath_create_by_vdev(dev_handler); - if(dev_sendpath == NULL){ - KNI_LOG_ERROR(logger, "Failed at marsio_sendpath_create_by_vdev"); + //vxlan_handler有0个线程收, 1个线程发 + struct mr_vdev *dev_vxlan_handler = marsio_open_device(instance, dev_vxlan_symbol, 0, 1); + if(dev_vxlan_handler == NULL){ + KNI_LOG_ERROR(logger, "Failed at marsio_open_device, dev_symbol is %s", dev_vxlan_symbol); + return NULL; + } + struct mr_sendpath * dev_eth_sendpath = marsio_sendpath_create_by_vdev(dev_eth_handler); + if(dev_eth_sendpath == NULL){ + KNI_LOG_ERROR(logger, "Failed at create marsio sendpath, dev_symbol is %s", dev_eth_symbol); + return NULL; + } + struct mr_sendpath * dev_vxlan_sendpath = marsio_sendpath_create_by_vdev(dev_vxlan_handler); + if(dev_eth_sendpath == NULL){ + KNI_LOG_ERROR(logger, "Failed at create marsio sendpath, dev_symbol is %s", dev_vxlan_symbol); return NULL; } struct kni_marsio_handle *handle = ALLOC(struct kni_marsio_handle, 1); handle->instance = instance; - handle->dev_handler = dev_handler; - handle->dev_sendpath = dev_sendpath; + handle->dev_eth_handler = dev_eth_handler; + handle->dev_eth_sendpath = dev_eth_sendpath; + handle->dev_vxlan_handler = dev_vxlan_handler; + handle->dev_vxlan_sendpath = dev_vxlan_sendpath; //暂时不用调 //marsio_thread_init(mr_instance); + + + //创建线程从tfe收包然后打上标签发送给vxlan_user + pthread_t thread_id; + struct tfe_receiver_args *args = ALLOC(struct tfe_receiver_args, 1); + args->logger = logger; + args->marsio_handle = handle; + int ret = pthread_create(&thread_id, NULL, thread_tfe_receiver, (void *)args); + if(unlikely(ret != 0)){ + KNI_LOG_ERROR(logger, "Failed at pthread_create, name is thread_tfe_receiver, ret is %d", ret); + exit(EXIT_FAILURE); + } + return handle; } diff --git a/entry/src/kni_maat.cpp b/entry/src/kni_maat.cpp index a3436e4..5293117 100644 --- a/entry/src/kni_maat.cpp +++ b/entry/src/kni_maat.cpp @@ -15,7 +15,8 @@ void kni_maat_destroy(struct kni_maat_handle *handle){ void compile_ex_param_new(int idx, const struct Maat_rule_t* rule, const char* srv_def_large, MAAT_RULE_EX_DATA* ad, long argl, void *argp){ - printf("call compile_ex_param_new\n"); + void *logger = argp; + KNI_LOG_DEBUG(logger, "call compile_ex_param_new"); if(rule->config_id == 0){ g_maat_default_action = rule->action; } @@ -23,12 +24,14 @@ void compile_ex_param_new(int idx, const struct Maat_rule_t* rule, const char* s } void compile_ex_param_free(int idx, const struct Maat_rule_t* rule, const char* srv_def_large, MAAT_RULE_EX_DATA* ad, long argl, void *argp){ - printf("call compile_ex_param_free\n"); + void *logger = argp; + KNI_LOG_DEBUG(logger, "call compile_ex_param_free"); return; } void compile_ex_param_dup(int idx, MAAT_RULE_EX_DATA *to, MAAT_RULE_EX_DATA *from, long argl, void *argp){ - printf("call compile_ex_param_dup\n"); + void *logger = argp; + KNI_LOG_DEBUG(logger, "call compile_ex_param_dup"); return; } @@ -91,8 +94,7 @@ struct kni_maat_handle* kni_maat_init(const char* profile, void *logger){ return NULL; } struct kni_maat_handle *handle = ALLOC(struct kni_maat_handle, 1); - ret = Maat_rule_get_ex_new_index(feather, compile_alias, compile_ex_param_new, compile_ex_param_free, compile_ex_param_dup, 0, NULL); - printf("Maat_rule_get_ex_new_index: compile_alias is %s, ret is %d\n", compile_alias, ret); + ret = Maat_rule_get_ex_new_index(feather, compile_alias, compile_ex_param_new, compile_ex_param_free, compile_ex_param_dup, 0, logger); if(ret < 0){ KNI_LOG_ERROR(logger, "Failed at Maat_rule_get_ex_new_index, ret is %d", ret); kni_maat_destroy(handle);