增加和tfe通信接口, 添加负载均衡功能等

This commit is contained in:
崔一鸣
2019-06-03 20:19:04 +08:00
parent 85aee8ba55
commit 1fa7a0673f
20 changed files with 1607 additions and 341 deletions

190
common/src/kni_cmsg.cpp Normal file
View File

@@ -0,0 +1,190 @@
#include "kni_utils.h"
#include "kni_cmsg.h"
struct kni_cmsg_tlv
{
uint16_t type;
uint16_t length;
unsigned char value_as_string[0];
} __attribute__((packed));
struct kni_cmsg
{
uint16_t nr_tlvs;
struct kni_cmsg_tlv* tlvs[KNI_CMSG_TLV_NR_MAX];
uint16_t size;
} __attribute__((packed));
struct kni_cmsg_serialize_header
{
uint8_t __magic__[2]; /* Must be 0x4d, 0x5a */
uint16_t nr_tlvs;
struct kni_cmsg_tlv tlvs[0];
} __attribute__((packed));
struct kni_cmsg* kni_cmsg_init()
{
struct kni_cmsg *cmsg = ALLOC(struct kni_cmsg, 1);
cmsg->size = sizeof(struct kni_cmsg_serialize_header);
return cmsg;
}
void kni_cmsg_destroy(struct kni_cmsg *cmsg)
{
if(cmsg != NULL)
{
for(int i = 0; i < KNI_CMSG_TLV_NR_MAX; i++)
{
FREE(&(cmsg->tlvs[i]));
}
}
FREE(&cmsg);
}
int kni_cmsg_set(struct kni_cmsg *cmsg, uint16_t type, const unsigned char *value, uint16_t size)
{
if(type >= KNI_CMSG_TLV_NR_MAX)
{
return KNI_CMSG_INVALID_TYPE;
}
struct kni_cmsg_tlv *tlv = cmsg->tlvs[type];
uint16_t length = sizeof(struct kni_cmsg_tlv) + size;
if(tlv == NULL)
{
tlv = (struct kni_cmsg_tlv*)ALLOC(char, length);
cmsg->nr_tlvs++;
cmsg->size += length;
}
tlv->type = type;
tlv->length = length;
memcpy(tlv->value_as_string, value, size);
cmsg->tlvs[type] = tlv;
return 0;
}
int kni_cmsg_get(struct kni_cmsg *cmsg, uint16_t type, uint16_t *size, unsigned char **pvalue)
{
struct kni_cmsg_tlv *tlv = NULL;
if(type >= KNI_CMSG_TLV_NR_MAX || (tlv = cmsg->tlvs[type]) == NULL)
{
*size = 0;
return KNI_CMSG_INVALID_TYPE;
}
*size = tlv->length - sizeof(struct kni_cmsg_tlv);
*pvalue = tlv->value_as_string;
return 0;
}
uint16_t kni_cmsg_serialize_size_get(struct kni_cmsg *cmsg)
{
return cmsg->size;
}
int kni_cmsg_serialize(struct kni_cmsg *cmsg, unsigned char *buff, uint16_t bufflen, uint16_t *serialize_len)
{
//size是serialize之后的实际长度
uint16_t size = cmsg->size;
//传入buff是否够长
if(bufflen < size)
{
return KNI_CMSG_BUFF_NOT_ENOUGH;
}
//size是否正确
if(size < sizeof(struct kni_cmsg_serialize_header))
{
return KNI_CMSG_INVALID_FORMAT;
}
struct kni_cmsg_serialize_header *header = (struct kni_cmsg_serialize_header*)buff;
header->__magic__[0] = 0x4d;
header->__magic__[1] = 0x5a;
header->nr_tlvs = htons(cmsg->nr_tlvs);
uint16_t offset = sizeof(struct kni_cmsg_serialize_header);
//检查nr_tlvs是否合法
int count = 0;
for(int i = 0; i < KNI_CMSG_TLV_NR_MAX; i++){
if(cmsg->tlvs[i] != NULL)
{
count++;
}
}
if(count != cmsg->nr_tlvs)
{
return KNI_CMSG_INVALID_FORMAT;
}
//序列化
for(int i = 0; i < KNI_CMSG_TLV_NR_MAX; i++)
{
struct kni_cmsg_tlv *tlv = cmsg->tlvs[i];
if(tlv == NULL)
{
continue;
}
if(i != tlv->type)
{
return KNI_CMSG_INVALID_FORMAT;
}
uint16_t length = tlv->length;
if(length < sizeof(struct kni_cmsg_tlv) || offset + length > size)
{
return KNI_CMSG_INVALID_FORMAT;
}
memcpy((char*)header + offset, (void*)tlv, length);
struct kni_cmsg_tlv *tlv1 = (struct kni_cmsg_tlv*)((char*)header + offset);
tlv1->type = htons(tlv->type);
tlv1->length = htons(tlv->length);
offset += length;
}
//检查size是否正确
if(offset != size)
{
return KNI_CMSG_INVALID_FORMAT;
}
*serialize_len = size;
return 0;
}
//反序列化
int kni_cmsg_deserialize(const unsigned char *data, uint16_t len, struct kni_cmsg** pcmsg)
{
struct kni_cmsg *cmsg = NULL;
struct kni_cmsg_serialize_header *header = (struct kni_cmsg_serialize_header*)data;
int offset = 0, nr_tlvs = -1;
if(len < sizeof(struct kni_cmsg_serialize_header))
{
goto error_out;
}
if(header->__magic__[0] != 0x4d || header->__magic__[1] != 0x5a)
{
goto error_out;
}
cmsg = ALLOC(struct kni_cmsg, 1);
offset = sizeof(struct kni_cmsg_serialize_header);
nr_tlvs = ntohs(header->nr_tlvs);
for(int i = 0; i < nr_tlvs; i++)
{
struct kni_cmsg_tlv *tlv = (struct kni_cmsg_tlv*)(data + offset);
if(offset + sizeof(struct kni_cmsg_tlv) > len)
{
goto error_out;
}
uint16_t type = ntohs(tlv->type);
uint16_t length = ntohs(tlv->length);
if(length < sizeof(struct kni_cmsg_tlv) || offset + length > len)
{
goto error_out;
}
int ret = kni_cmsg_set(cmsg, type, data + offset + sizeof(struct kni_cmsg_tlv), length - sizeof(struct kni_cmsg_tlv));
if(ret < 0)
{
goto error_out;
}
offset += length;
}
cmsg->size = offset;
*pcmsg = cmsg;
return 0;
error_out:
kni_cmsg_destroy(cmsg);
return KNI_CMSG_INVALID_FORMAT;
}

View File

@@ -1,4 +1,15 @@
#include "kni_utils.h"
#include <sys/ioctl.h>
#include <net/if.h>
int kni_stream_addr_trans(struct ipaddr* addr, char *output, int len){
char saddr[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &(addr->v4->saddr), saddr, INET_ADDRSTRLEN);
char daddr[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &(addr->v4->daddr), daddr, INET_ADDRSTRLEN);
snprintf(output, len, "%s:%d -> %s:%d", saddr, ntohs(addr->v4->source), daddr, ntohs(addr->v4->dest));
return 0;
}
uint16_t kni_ip_checksum(const void *buf, size_t hdr_len){
unsigned long sum = 0;
@@ -148,4 +159,97 @@ struct kni_tcpopt_info* kni_get_tcpopt(struct tcphdr* tcphdr,int tcphdr_len){
}
}
return tcpopt;
}
int kni_ipv4_addr_get_by_eth(const char *ifname, uint32_t *ip){
struct ifreq ifr;
int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if(sockfd == -1) {
goto error_out;
}
strcpy(ifr.ifr_name, ifname);
if(ioctl(sockfd, SIOCGIFADDR, &ifr) < 0){
goto error_out;
}
*ip = ((struct sockaddr_in*)&(ifr.ifr_addr))->sin_addr.s_addr;
close(sockfd);
return 0;
error_out:
close(sockfd);
return -1;
}
static int __wrapper_MESA_htable_set_opt(MESA_htable_handle table, enum MESA_htable_opt opt_type, unsigned value, void *logger, const char *symbol)
{
int ret = MESA_htable_set_opt(table, opt_type, &value, (int)(sizeof(value)));
if(unlikely(ret != 0))
{
KNI_LOG_ERROR(logger, "Failed at MESA_htable_set_opt, htable is %s, opt_type is %d", symbol, opt_type);
}
return ret;
}
static int __wrapper_MESA_htable_set_opt(MESA_htable_handle table, enum MESA_htable_opt opt_type, void * val, size_t len, void *logger, const char *symbol)
{
int ret = MESA_htable_set_opt(table, opt_type, val, (int)len);
if(unlikely(ret != 0))
{
KNI_LOG_ERROR(logger, "Failed at MESA_htable_set_opt, htable is %s, opt_type is %d", symbol, opt_type);
}
return ret;
}
MESA_htable_handle kni_create_htable(const char *profile, const char *section, void *free_data_cb, void *expire_notify_cb, void *logger)
{
int mho_screen_print_ctrl;
int mho_thread_safe;
int mho_mutex_num;
int mho_hash_slot_size;
int mho_hash_max_element_num;
int mho_expire_time;
char mho_eliminate_type[KNI_SYMBOL_MAX];
MESA_load_profile_int_def(profile, section, "mho_screen_print_ctrl", &mho_screen_print_ctrl, 1);
MESA_load_profile_int_def(profile, section, "mho_thread_safe", &mho_thread_safe, 0);
MESA_load_profile_int_def(profile, section, "mho_mutex_num", &mho_mutex_num, 12);
MESA_load_profile_int_def(profile, section, "mho_hash_slot_size", &mho_hash_slot_size, 1234);
MESA_load_profile_int_def(profile, section, "mho_hash_max_element_num", &mho_hash_max_element_num, 12345);
MESA_load_profile_int_def(profile, section, "mho_expire_time", &mho_expire_time, 3600);
MESA_load_profile_string_def(profile, section, "mho_eliminate_type", mho_eliminate_type, sizeof(mho_eliminate_type), "FIFO");
KNI_LOG_INFO(logger, "MESA_prof_load, [%s]:\n mho_screen_print_ctrl: %d\n mho_thread_safe: %d\n mho_mutex_num: %d\n"
"mho_hash_slot_size: %d\n mho_hash_max_element_num: %d\n mho_expire_time: %d\n mho_eliminate_type: %s\n", section,
mho_screen_print_ctrl, mho_thread_safe, mho_mutex_num, mho_hash_slot_size, mho_hash_max_element_num, mho_expire_time, mho_eliminate_type);
MESA_htable_handle htable = MESA_htable_born();
if(htable == NULL)
{
KNI_LOG_ERROR(logger, "MESA_htable: failed at MESA_htable_born");
return NULL;
}
__wrapper_MESA_htable_set_opt(htable, MHO_SCREEN_PRINT_CTRL, mho_screen_print_ctrl, logger, section);
__wrapper_MESA_htable_set_opt(htable, MHO_THREAD_SAFE, mho_thread_safe, logger, section);
__wrapper_MESA_htable_set_opt(htable, MHO_MUTEX_NUM, mho_mutex_num, logger, section);
__wrapper_MESA_htable_set_opt(htable, MHO_HASH_SLOT_SIZE, mho_hash_slot_size, logger, section);
__wrapper_MESA_htable_set_opt(htable, MHO_HASH_MAX_ELEMENT_NUM, mho_hash_max_element_num, logger, section);
__wrapper_MESA_htable_set_opt(htable, MHO_EXPIRE_TIME, mho_expire_time, logger, section);
if(strncmp(mho_eliminate_type, "LRU", KNI_SYMBOL_MAX) == 0)
{
__wrapper_MESA_htable_set_opt(htable, MHO_ELIMIMINATE_TYPE, HASH_ELIMINATE_ALGO_LRU, logger, section);
}
else
{
__wrapper_MESA_htable_set_opt(htable, MHO_ELIMIMINATE_TYPE, HASH_ELIMINATE_ALGO_FIFO, logger, section);
}
__wrapper_MESA_htable_set_opt(htable, MHO_CBFUN_DATA_FREE,
(void *)free_data_cb, sizeof(free_data_cb), logger, section);
//ret = __wrapper_MESA_htable_set_opt(htable, MHO_CBFUN_DATA_EXPIRE_NOTIFY,
// (void *)key_keeper_verify_cb);
int ret = MESA_htable_mature(htable);
if(unlikely(ret != 0))
{
KNI_LOG_ERROR(logger, "MESA_htable: failed at MESA_htable_mature, htable is %s", section);
return NULL;
}
return htable;
}