perf: Optimize IPID (Avoid the futex of rand() while ensuring that IPID increases monotonically)

This commit is contained in:
luwenpeng
2023-10-11 18:11:48 +08:00
parent db517610e0
commit 49ccb5149f
5 changed files with 15 additions and 11 deletions

View File

@@ -105,7 +105,7 @@ struct udp_hdr
} __attribute__((__packed__)); } __attribute__((__packed__));
void build_udp_header(const char *l3_hdr, int l3_hdr_len, struct udp_hdr *udp_hdr, uint16_t udp_sport, uint16_t udp_dport, int payload_len); void build_udp_header(const char *l3_hdr, int l3_hdr_len, struct udp_hdr *udp_hdr, uint16_t udp_sport, uint16_t udp_dport, int payload_len);
void build_ip_header(struct ip *ip_hdr, uint8_t next_protocol, const char *src_addr, const char *dst_addr, uint16_t payload_len); void build_ip_header(struct ip *ip_hdr, uint8_t next_protocol, uint16_t ipid, const char *src_addr, const char *dst_addr, uint16_t payload_len);
void build_ether_header(struct ethhdr *eth_hdr, uint16_t next_protocol, const char *src_mac, const char *dst_mac); void build_ether_header(struct ethhdr *eth_hdr, uint16_t next_protocol, const char *src_mac, const char *dst_mac);
/****************************************************************************** /******************************************************************************

View File

@@ -196,16 +196,16 @@ void build_udp_header(const char *l3_hdr, int l3_hdr_len, struct udp_hdr *udp_hd
udp_hdr->uh_sum = CHECKSUM_CARRY(sum); udp_hdr->uh_sum = CHECKSUM_CARRY(sum);
} }
void build_ip_header(struct ip *ip_hdr, uint8_t next_protocol, const char *src_addr, const char *dst_addr, uint16_t payload_len) void build_ip_header(struct ip *ip_hdr, uint8_t next_protocol, uint16_t ipid, const char *src_addr, const char *dst_addr, uint16_t payload_len)
{ {
memset(ip_hdr, 0, sizeof(struct ip)); memset(ip_hdr, 0, sizeof(struct ip));
ip_hdr->ip_hl = 5; /* 20 byte header */ ip_hdr->ip_hl = 5; /* 20 byte header */
ip_hdr->ip_v = 4; /* version 4 */ ip_hdr->ip_v = 4; /* version 4 */
ip_hdr->ip_tos = 0; /* IP tos */ ip_hdr->ip_tos = 0; /* IP tos */
ip_hdr->ip_id = htons(random()); /* IP ID */ ip_hdr->ip_id = htons(ipid); /* IP ID */
ip_hdr->ip_ttl = 80; /* time to live */ ip_hdr->ip_ttl = 80; /* time to live */
ip_hdr->ip_p = next_protocol; /* transport protocol */ ip_hdr->ip_p = next_protocol; /* transport protocol */
ip_hdr->ip_src.s_addr = inet_addr(src_addr); ip_hdr->ip_src.s_addr = inet_addr(src_addr);
ip_hdr->ip_dst.s_addr = inet_addr(dst_addr); ip_hdr->ip_dst.s_addr = inet_addr(dst_addr);
ip_hdr->ip_len = htons(sizeof(struct ip) + payload_len); /* total length */ ip_hdr->ip_len = htons(sizeof(struct ip) + payload_len); /* total length */

View File

@@ -34,6 +34,7 @@ struct thread_ctx
int session_table_need_reset; int session_table_need_reset;
int thread_is_runing; int thread_is_runing;
uint64_t tx_packets_to_sf;
}; };
/****************************************************************************** /******************************************************************************

View File

@@ -194,6 +194,7 @@ int main(int argc, char **argv)
ctx->work_threads[i].ref_enforcer = ctx->enforcer; ctx->work_threads[i].ref_enforcer = ctx->enforcer;
ctx->work_threads[i].ref_sce_ctx = ctx; ctx->work_threads[i].ref_sce_ctx = ctx;
ctx->work_threads[i].session_table_need_reset = 0; ctx->work_threads[i].session_table_need_reset = 0;
ctx->work_threads[i].tx_packets_to_sf = 0;
} }
for (int i = 0; i < ctx->nr_worker_threads; i++) for (int i = 0; i < ctx->nr_worker_threads; i++)

View File

@@ -397,7 +397,8 @@ static struct session_ctx *inject_packet_search_session(struct session_table *ta
static void vxlan_encapsulate(char *buffer, static void vxlan_encapsulate(char *buffer,
const char *src_mac_str, const char *dst_mac_str, const char *src_mac_str, const char *dst_mac_str,
const char *src_ip_str, const char *dst_ip_str, const char *src_ip_str, const char *dst_ip_str,
int payload_len, int is_e2i, int is_decrypted, int sf_index, uint64_t session_id) int payload_len, int is_e2i, int is_decrypted, int sf_index,
uint64_t session_id, uint16_t ipid)
{ {
struct ethhdr *eth_hdr = (struct ethhdr *)buffer; struct ethhdr *eth_hdr = (struct ethhdr *)buffer;
struct ip *ip_hdr = (struct ip *)((char *)eth_hdr + sizeof(struct ethhdr)); struct ip *ip_hdr = (struct ip *)((char *)eth_hdr + sizeof(struct ethhdr));
@@ -410,12 +411,13 @@ static void vxlan_encapsulate(char *buffer,
g_vxlan_set_traffic_type(g_vxlan_hdr, is_decrypted); 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_ether_header(eth_hdr, ETH_P_IP, src_mac_str, dst_mac_str);
build_ip_header(ip_hdr, IPPROTO_UDP, src_ip_str, dst_ip_str, sizeof(struct udp_hdr) + sizeof(struct g_vxlan) + payload_len); 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_udp_header((const char *)&ip_hdr->ip_src, 8, udp_hdr, session_id % (65535 - 49152) + 49152, 4789, 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);
} }
static int send_packet_to_sf(marsio_buff_t *rx_buff, struct metadata *meta, struct selected_sf *sf, struct thread_ctx *thread_ctx) static int send_packet_to_sf(marsio_buff_t *rx_buff, struct metadata *meta, struct selected_sf *sf, struct thread_ctx *thread_ctx)
{ {
thread_ctx->tx_packets_to_sf++;
struct packet_io *packet_io = thread_ctx->ref_io; struct packet_io *packet_io = thread_ctx->ref_io;
int thread_index = thread_ctx->thread_index; int thread_index = thread_ctx->thread_index;
@@ -436,7 +438,7 @@ static int send_packet_to_sf(marsio_buff_t *rx_buff, struct metadata *meta, stru
case PACKAGE_METHOD_VXLAN_G: case PACKAGE_METHOD_VXLAN_G:
prepend_len = sizeof(struct ethhdr) + sizeof(struct ip) + sizeof(struct udp_hdr) + sizeof(struct g_vxlan); prepend_len = sizeof(struct ethhdr) + sizeof(struct ip) + sizeof(struct udp_hdr) + sizeof(struct g_vxlan);
buffer = marsio_buff_prepend(rx_buff, prepend_len); 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); 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);
break; break;
case PACKAGE_METHOD_LAYER2_SWITCH: case PACKAGE_METHOD_LAYER2_SWITCH:
// TODO // TODO