perf: 性能优化
* io_uring使用buffer pool避免内存分配与释放
* packet io thread与worker thread无锁访问cmsg
* 为解密流量的fd设置默认的TTL
This commit is contained in:
@@ -3,7 +3,6 @@
|
||||
#include <string.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <errno.h>
|
||||
#include <pthread.h>
|
||||
|
||||
#include "tfe_types.h"
|
||||
#include "tfe_utils.h"
|
||||
@@ -26,7 +25,6 @@ struct tfe_cmsg
|
||||
{
|
||||
uint8_t flag;
|
||||
uint8_t ref;
|
||||
pthread_rwlock_t rwlock;
|
||||
uint16_t nr_tlvs;
|
||||
struct tfe_cmsg_tlv* tlvs[TFE_CMSG_TLV_NR_MAX];
|
||||
uint16_t size;
|
||||
@@ -44,8 +42,6 @@ struct tfe_cmsg* tfe_cmsg_init()
|
||||
struct tfe_cmsg *cmsg = ALLOC(struct tfe_cmsg, 1);
|
||||
cmsg->size = sizeof(struct tfe_cmsg_serialize_header);
|
||||
|
||||
pthread_rwlock_init(&(cmsg->rwlock), NULL);
|
||||
|
||||
ATOMIC_ZERO(&(cmsg->flag));
|
||||
ATOMIC_ZERO(&(cmsg->ref));
|
||||
ATOMIC_INC(&(cmsg->ref));
|
||||
@@ -58,13 +54,10 @@ void tfe_cmsg_destroy(struct tfe_cmsg **cmsg)
|
||||
{
|
||||
if ((__sync_sub_and_fetch(&((*cmsg)->ref), 1) != 0))
|
||||
return;
|
||||
pthread_rwlock_wrlock(&((*cmsg)->rwlock));
|
||||
for(int i = 0; i < TFE_CMSG_TLV_NR_MAX; i++)
|
||||
{
|
||||
FREE(&((*cmsg)->tlvs[i]));
|
||||
}
|
||||
pthread_rwlock_unlock(&((*cmsg)->rwlock));
|
||||
pthread_rwlock_destroy(&((*cmsg)->rwlock));
|
||||
FREE(cmsg);
|
||||
}
|
||||
}
|
||||
@@ -98,7 +91,6 @@ int tfe_cmsg_set(struct tfe_cmsg * cmsg, enum tfe_cmsg_tlv_type type, const unsi
|
||||
{
|
||||
return TFE_CMSG_INVALID_TYPE;
|
||||
}
|
||||
pthread_rwlock_wrlock(&cmsg->rwlock);
|
||||
struct tfe_cmsg_tlv *tlv = cmsg->tlvs[type];
|
||||
uint16_t length = sizeof(struct tfe_cmsg_tlv) + size;
|
||||
|
||||
@@ -122,7 +114,6 @@ int tfe_cmsg_set(struct tfe_cmsg * cmsg, enum tfe_cmsg_tlv_type type, const unsi
|
||||
tlv->length = length;
|
||||
memcpy(tlv->value_as_string, value, size);
|
||||
cmsg->tlvs[type] = tlv;
|
||||
pthread_rwlock_unlock(&cmsg->rwlock);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -139,7 +130,6 @@ int tfe_cmsg_get_value(struct tfe_cmsg * cmsg, enum tfe_cmsg_tlv_type type, unsi
|
||||
goto errout;
|
||||
}
|
||||
|
||||
pthread_rwlock_rdlock(&cmsg->rwlock);
|
||||
tlv = cmsg->tlvs[type];
|
||||
if (unlikely(tlv == NULL))
|
||||
{
|
||||
@@ -156,37 +146,30 @@ int tfe_cmsg_get_value(struct tfe_cmsg * cmsg, enum tfe_cmsg_tlv_type type, unsi
|
||||
|
||||
memcpy(out_value, tlv->value_as_string, value_length);
|
||||
*out_size = value_length;
|
||||
pthread_rwlock_unlock(&cmsg->rwlock);
|
||||
return 0;
|
||||
|
||||
errout:
|
||||
pthread_rwlock_unlock(&cmsg->rwlock);
|
||||
return result;
|
||||
}
|
||||
|
||||
uint16_t tfe_cmsg_serialize_size_get(struct tfe_cmsg *cmsg)
|
||||
{
|
||||
pthread_rwlock_rdlock(&cmsg->rwlock);
|
||||
uint16_t size = cmsg->size;
|
||||
pthread_rwlock_unlock(&cmsg->rwlock);
|
||||
return size;
|
||||
}
|
||||
|
||||
int tfe_cmsg_serialize(struct tfe_cmsg *cmsg, unsigned char *buff, uint16_t bufflen, uint16_t *serialize_len)
|
||||
{
|
||||
//size是serialize之后的实际长度
|
||||
pthread_rwlock_rdlock(&cmsg->rwlock);
|
||||
uint16_t size = cmsg->size;
|
||||
//传入buff是否够长
|
||||
if(bufflen < size)
|
||||
{
|
||||
pthread_rwlock_unlock(&cmsg->rwlock);
|
||||
return TFE_CMSG_BUFF_NOT_ENOUGH;
|
||||
}
|
||||
//size是否正确
|
||||
if(size < sizeof(struct tfe_cmsg_serialize_header))
|
||||
{
|
||||
pthread_rwlock_unlock(&cmsg->rwlock);
|
||||
return TFE_CMSG_BUFF_NOT_ENOUGH;
|
||||
}
|
||||
struct tfe_cmsg_serialize_header *header = (struct tfe_cmsg_serialize_header*)buff;
|
||||
@@ -204,7 +187,6 @@ int tfe_cmsg_serialize(struct tfe_cmsg *cmsg, unsigned char *buff, uint16_t buff
|
||||
}
|
||||
if(count != cmsg->nr_tlvs)
|
||||
{
|
||||
pthread_rwlock_unlock(&cmsg->rwlock);
|
||||
return TFE_CMSG_INVALID_FORMAT;
|
||||
}
|
||||
//序列化
|
||||
@@ -217,13 +199,11 @@ int tfe_cmsg_serialize(struct tfe_cmsg *cmsg, unsigned char *buff, uint16_t buff
|
||||
}
|
||||
if(i != tlv->type)
|
||||
{
|
||||
pthread_rwlock_unlock(&cmsg->rwlock);
|
||||
return TFE_CMSG_INVALID_FORMAT;
|
||||
}
|
||||
uint16_t length = tlv->length;
|
||||
if(length < sizeof(struct tfe_cmsg_tlv) || offset + length > size)
|
||||
{
|
||||
pthread_rwlock_unlock(&cmsg->rwlock);
|
||||
return TFE_CMSG_INVALID_FORMAT;
|
||||
}
|
||||
memcpy((char*)header + offset, (void*)tlv, length);
|
||||
@@ -235,11 +215,9 @@ int tfe_cmsg_serialize(struct tfe_cmsg *cmsg, unsigned char *buff, uint16_t buff
|
||||
//检查size是否正确
|
||||
if(offset != size)
|
||||
{
|
||||
pthread_rwlock_unlock(&cmsg->rwlock);
|
||||
return TFE_CMSG_INVALID_FORMAT;
|
||||
}
|
||||
*serialize_len = size;
|
||||
pthread_rwlock_unlock(&cmsg->rwlock);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -260,7 +238,6 @@ int tfe_cmsg_deserialize(const unsigned char *data, uint16_t len, struct tfe_cms
|
||||
}
|
||||
|
||||
cmsg = ALLOC(struct tfe_cmsg, 1);
|
||||
pthread_rwlock_init(&(cmsg->rwlock), NULL);
|
||||
ATOMIC_ZERO(&(cmsg->flag));
|
||||
ATOMIC_ZERO(&(cmsg->ref));
|
||||
ATOMIC_INC(&(cmsg->ref));
|
||||
|
||||
Reference in New Issue
Block a user