perf: Reduce str_to_mac() and inet_addr() calls

This commit is contained in:
luwenpeng
2023-10-12 11:59:42 +08:00
parent 49ccb5149f
commit 4f870de963
7 changed files with 68 additions and 64 deletions

View File

@@ -29,7 +29,6 @@
#define BUF_SIZE 4096
#define SEND_MAX 1
#define PACKET_SIZE 64
#define HC_MAC_LEN 6
#define HC_DEV_NAME_LEN 16
#define HC_LOCAL_ADDRESS_LEN 64
@@ -63,7 +62,7 @@ struct node_addr
{
char address[64]; // key
uint8_t mac[HC_MAC_LEN];
uint8_t mac[ETH_ALEN];
int ref_cnt; // reference
UT_hash_handle hh; /* handle for first hash table */
@@ -82,7 +81,7 @@ char path[BFD_PATHLEN];
char hc_dev_name[HC_DEV_NAME_LEN];
char local_address[HC_LOCAL_ADDRESS_LEN];
char gateway_address[HC_LOCAL_ADDRESS_LEN];
uint8_t default_gw_mac[HC_MAC_LEN];
uint8_t default_gw_mac[ETH_ALEN];
static int get_mac_by_addr(char *addr, uint8_t *buf);
static int health_check_session_foreach();
@@ -98,7 +97,7 @@ static int health_check_method_table_set_mac(struct session_table_addr *table, c
return 1;
}
memcpy(tmp->mac, mac, HC_MAC_LEN);
memcpy(tmp->mac, mac, ETH_ALEN);
pthread_rwlock_unlock(&(table->rwlock));
return 0;
}
@@ -114,7 +113,7 @@ static int health_check_method_table_get_mac(struct session_table_addr *table, c
return 1;
}
memcpy(out_mac, tmp->mac, HC_MAC_LEN);
memcpy(out_mac, tmp->mac, ETH_ALEN);
pthread_rwlock_unlock(&(table->rwlock));
return 0;
}
@@ -214,7 +213,7 @@ static void *_listen_arp_table(void *arg)
struct in_addr ipaddr;
uint8_t *mac = NULL;
char str_addr[INET_ADDRSTRLEN] = {0};
uint8_t init_mac[HC_MAC_LEN] = {0};
uint8_t init_mac[ETH_ALEN] = {0};
start:
nl_sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
@@ -432,7 +431,7 @@ static uint64_t health_check_get_session_id()
uint64_t health_check_session_add(int profile_id, int vsys_id, const struct health_check *policy)
{
uint64_t session_id = 0;
uint8_t mac[HC_MAC_LEN] = {0};
uint8_t mac[ETH_ALEN] = {0};
struct session_iterm *tmp = NULL;
if (enable == 0)
@@ -585,7 +584,7 @@ static int get_mac_by_addr(char *addr, uint8_t *buf)
ret = ioctl(sfd, SIOCGARP, &arp_req);
if (ret == 0)
memcpy(buf, arp_req.arp_ha.sa_data, HC_MAC_LEN);
memcpy(buf, arp_req.arp_ha.sa_data, ETH_ALEN);
LOG_DEBUG("IP:%s, MAC: %02x:%02x:%02x:%02x:%02x:%02x",
addr, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
@@ -600,8 +599,8 @@ static void *_health_check_session_foreach(void *arg)
struct bfd_vtysh_client client;
struct session_iterm *tmp = NULL;
struct session_iterm *node = NULL;
uint8_t mac[HC_MAC_LEN] = {0};
uint8_t init_mac[HC_MAC_LEN] = {0};
uint8_t mac[ETH_ALEN] = {0};
uint8_t init_mac[ETH_ALEN] = {0};
struct sockaddr_in addr;
struct timespec current_time;
@@ -638,7 +637,7 @@ static void *_health_check_session_foreach(void *arg)
if (node->is_active != is_active) {
node->is_active = is_active;
if (node->is_active == 1) {
memset(mac, 0, HC_MAC_LEN);
memset(mac, 0, ETH_ALEN);
get_mac_by_addr(node->policy.address, mac);
health_check_method_table_set_mac(&g_handle_bfd, node->policy.address, mac);
}
@@ -713,13 +712,12 @@ static const char *health_check_method_str(enum health_check_method method)
// return 0 : success
// return -1 : key not exist
int health_check_session_get_mac(uint64_t session_id, char *mac_buff)
int health_check_session_get_mac(uint64_t session_id, u_char mac_buff[])
{
uint8_t *p = NULL;
const char *str_method = NULL;
struct session_iterm *tmp = NULL;
uint8_t mac[HC_MAC_LEN] = {0};
uint8_t init_mac[HC_MAC_LEN] = {0};
uint8_t mac[ETH_ALEN] = {0};
uint8_t init_mac[ETH_ALEN] = {0};
if (enable == 0)
{
@@ -747,17 +745,16 @@ int health_check_session_get_mac(uint64_t session_id, char *mac_buff)
health_check_method_table_get_mac(&g_handle_none, tmp->policy.address, mac);
}
if (memcmp(mac, init_mac, HC_MAC_LEN) == 0) {
if (memcmp(mac, init_mac, ETH_ALEN) == 0) {
health_check_method_table_get_mac(&g_handle_none, gateway_address, mac);
if (memcmp(mac, init_mac, HC_MAC_LEN) == 0) {
if (memcmp(mac, init_mac, ETH_ALEN) == 0) {
LOG_DEBUG("health check session id [%lu] profile id [%d] health check method [%s] get mac [null]", session_id, tmp->profile_id, str_method);
pthread_rwlock_unlock(&g_handle.rwlock);
return -1;
}
}
p = mac;
snprintf(mac_buff, 18, "%02x:%02x:%02x:%02x:%02x:%02x", p[0], p[1], p[2], p[3], p[4], p[5]);
LOG_DEBUG("health check session id [%lu] profile id [%d] health check method [%s] get mac [%s]", session_id, tmp->profile_id, str_method, mac_buff);
memcpy(mac_buff, mac, ETH_ALEN);
LOG_DEBUG("health check session id [%lu] profile id [%d] health check method [%s] get mac [%02x:%02x:%02x:%02x:%02x:%02x]", session_id, tmp->profile_id, str_method, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
pthread_rwlock_unlock(&g_handle.rwlock);
return 0;
}