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

@@ -1,4 +1,6 @@
#include <assert.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/udp.h>
#include <netinet/ether.h>
@@ -29,8 +31,11 @@ struct config
char app_symbol[256];
char dev_endpoint[256];
char dev_nf_interface[256];
char dev_endpoint_src_ip[16];
char dev_endpoint_src_mac[32];
char dev_endpoint_src_ip_str[16];
char dev_endpoint_src_mac_str[32];
in_addr_t dev_endpoint_src_ip;
u_char dev_endpoint_src_mac[ETH_ALEN];
};
struct device
@@ -395,8 +400,8 @@ static struct session_ctx *inject_packet_search_session(struct session_table *ta
******************************************************************************/
static void vxlan_encapsulate(char *buffer,
const char *src_mac_str, const char *dst_mac_str,
const char *src_ip_str, const char *dst_ip_str,
const u_char src_mac[], const u_char dst_mac[],
const in_addr_t src_ip, const in_addr_t dst_ip,
int payload_len, int is_e2i, int is_decrypted, int sf_index,
uint64_t session_id, uint16_t ipid)
{
@@ -410,8 +415,8 @@ static void vxlan_encapsulate(char *buffer,
g_vxlan_set_sf_index(g_vxlan_hdr, sf_index);
g_vxlan_set_traffic_type(g_vxlan_hdr, is_decrypted);
build_ether_header(eth_hdr, ETH_P_IP, src_mac_str, dst_mac_str);
build_ip_header(ip_hdr, IPPROTO_UDP, ipid, src_ip_str, dst_ip_str, sizeof(struct udp_hdr) + sizeof(struct g_vxlan) + payload_len);
build_ether_header(eth_hdr, ETH_P_IP, src_mac, dst_mac);
build_ip_header(ip_hdr, IPPROTO_UDP, ipid, src_ip, dst_ip, sizeof(struct udp_hdr) + sizeof(struct g_vxlan) + payload_len);
build_udp_header((const char *)&ip_hdr->ip_src, 8, udp_hdr, session_id % (65535 - 49152) + 49152, 4789, sizeof(struct g_vxlan) + payload_len);
}
@@ -421,10 +426,6 @@ static int send_packet_to_sf(marsio_buff_t *rx_buff, struct metadata *meta, stru
struct packet_io *packet_io = thread_ctx->ref_io;
int thread_index = thread_ctx->thread_index;
const char *src_mac_str = packet_io->config.dev_endpoint_src_mac;
const char *dst_mac_str = sf->sf_dst_mac;
const char *src_ip_str = packet_io->config.dev_endpoint_src_ip;
const char *dst_ip_str = sf->sf_dst_ip;
int payload_len = meta->raw_len;
int is_e2i = meta->is_e2i_dir;
int is_decrypted = meta->is_decrypted;
@@ -438,7 +439,7 @@ static int send_packet_to_sf(marsio_buff_t *rx_buff, struct metadata *meta, stru
case PACKAGE_METHOD_VXLAN_G:
prepend_len = sizeof(struct ethhdr) + sizeof(struct ip) + sizeof(struct udp_hdr) + sizeof(struct g_vxlan);
buffer = marsio_buff_prepend(rx_buff, prepend_len);
vxlan_encapsulate(buffer, src_mac_str, dst_mac_str, src_ip_str, dst_ip_str, payload_len, is_e2i, is_decrypted, sf_index, meta->session_id, thread_ctx->tx_packets_to_sf % 65535);
vxlan_encapsulate(buffer, packet_io->config.dev_endpoint_src_mac, sf->sf_dst_mac, packet_io->config.dev_endpoint_src_ip, sf->sf_dst_ip, payload_len, is_e2i, is_decrypted, sf_index, meta->session_id, thread_ctx->tx_packets_to_sf % 65535);
break;
case PACKAGE_METHOD_LAYER2_SWITCH:
// TODO
@@ -1140,8 +1141,8 @@ static int packet_io_config(const char *profile, struct config *config)
MESA_load_profile_string_nodef(profile, "PACKET_IO", "dev_endpoint", config->dev_endpoint, sizeof(config->dev_endpoint));
MESA_load_profile_string_nodef(profile, "PACKET_IO", "dev_nf_interface", config->dev_nf_interface, sizeof(config->dev_nf_interface));
MESA_load_profile_string_nodef(profile, "PACKET_IO", "dev_endpoint_src_ip", config->dev_endpoint_src_ip, sizeof(config->dev_endpoint_src_ip));
MESA_load_profile_string_nodef(profile, "PACKET_IO", "dev_endpoint_src_mac", config->dev_endpoint_src_mac, sizeof(config->dev_endpoint_src_mac));
MESA_load_profile_string_nodef(profile, "PACKET_IO", "dev_endpoint_src_ip", config->dev_endpoint_src_ip_str, sizeof(config->dev_endpoint_src_ip_str));
MESA_load_profile_string_nodef(profile, "PACKET_IO", "dev_endpoint_src_mac", config->dev_endpoint_src_mac_str, sizeof(config->dev_endpoint_src_mac_str));
if (config->rx_burst_max > RX_BURST_MAX)
{
@@ -1172,10 +1173,10 @@ static int packet_io_config(const char *profile, struct config *config)
LOG_DEBUG("%s: PACKET_IO->app_symbol : %s", LOG_TAG_PKTIO, config->app_symbol);
LOG_DEBUG("%s: PACKET_IO->dev_endpoint : %s", LOG_TAG_PKTIO, config->dev_endpoint);
LOG_DEBUG("%s: PACKET_IO->dev_nf_interface : %s", LOG_TAG_PKTIO, config->dev_nf_interface);
LOG_DEBUG("%s: PACKET_IO->dev_endpoint_src_ip : %s", LOG_TAG_PKTIO, config->dev_endpoint_src_ip);
if (strlen(config->dev_endpoint_src_mac))
LOG_DEBUG("%s: PACKET_IO->dev_endpoint_src_ip : %s", LOG_TAG_PKTIO, config->dev_endpoint_src_ip_str);
if (strlen(config->dev_endpoint_src_mac_str))
{
LOG_DEBUG("%s: PACKET_IO->dev_endpoint_src_mac : %s (get from configuration file)", LOG_TAG_PKTIO, config->dev_endpoint_src_mac);
LOG_DEBUG("%s: PACKET_IO->dev_endpoint_src_mac : %s (get from configuration file)", LOG_TAG_PKTIO, config->dev_endpoint_src_mac_str);
}
return 0;
@@ -1246,11 +1247,13 @@ struct packet_io *packet_io_create(const char *profile, int thread_num, cpu_set_
goto error_out;
}
if (strlen(handle->config.dev_endpoint_src_mac) == 0)
if (strlen(handle->config.dev_endpoint_src_mac_str) == 0)
{
marsio_get_device_ether_addr(handle->dev_endpoint.mr_dev, handle->config.dev_endpoint_src_mac, sizeof(handle->config.dev_endpoint_src_mac));
LOG_DEBUG("%s: PACKET_IO->dev_endpoint_src_mac : %s (get from marsio api)", LOG_TAG_PKTIO, handle->config.dev_endpoint_src_mac);
marsio_get_device_ether_addr(handle->dev_endpoint.mr_dev, handle->config.dev_endpoint_src_mac_str, sizeof(handle->config.dev_endpoint_src_mac_str));
LOG_DEBUG("%s: PACKET_IO->dev_endpoint_src_mac : %s (get from marsio api)", LOG_TAG_PKTIO, handle->config.dev_endpoint_src_mac_str);
}
str_to_mac(handle->config.dev_endpoint_src_mac_str, handle->config.dev_endpoint_src_mac);
handle->config.dev_endpoint_src_ip = inet_addr(handle->config.dev_endpoint_src_ip_str);
return handle;