summaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
authorluwenpeng <[email protected]>2023-10-11 18:11:48 +0800
committerluwenpeng <[email protected]>2023-10-23 19:27:59 +0800
commit49ccb5149facb022cacfd98cb82fd5dd1fce2107 (patch)
treefd0dd54b6e316713766e36df6da3546d96acea05 /common
parentdb517610e023b7c986f12c2207382db627ec9d19 (diff)
perf: Optimize IPID (Avoid the futex of rand() while ensuring that IPID increases monotonically)
Diffstat (limited to 'common')
-rw-r--r--common/include/utils.h2
-rw-r--r--common/src/utils.cpp14
2 files changed, 8 insertions, 8 deletions
diff --git a/common/include/utils.h b/common/include/utils.h
index 78a6edd..81ffd3b 100644
--- a/common/include/utils.h
+++ b/common/include/utils.h
@@ -105,7 +105,7 @@ struct udp_hdr
} __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_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);
/******************************************************************************
diff --git a/common/src/utils.cpp b/common/src/utils.cpp
index 6b0f0a7..6ba90e3 100644
--- a/common/src/utils.cpp
+++ b/common/src/utils.cpp
@@ -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);
}
-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));
- ip_hdr->ip_hl = 5; /* 20 byte header */
- ip_hdr->ip_v = 4; /* version 4 */
- ip_hdr->ip_tos = 0; /* IP tos */
- ip_hdr->ip_id = htons(random()); /* IP ID */
- ip_hdr->ip_ttl = 80; /* time to live */
- ip_hdr->ip_p = next_protocol; /* transport protocol */
+ ip_hdr->ip_hl = 5; /* 20 byte header */
+ ip_hdr->ip_v = 4; /* version 4 */
+ ip_hdr->ip_tos = 0; /* IP tos */
+ ip_hdr->ip_id = htons(ipid); /* IP ID */
+ ip_hdr->ip_ttl = 80; /* time to live */
+ ip_hdr->ip_p = next_protocol; /* transport protocol */
ip_hdr->ip_src.s_addr = inet_addr(src_addr);
ip_hdr->ip_dst.s_addr = inet_addr(dst_addr);
ip_hdr->ip_len = htons(sizeof(struct ip) + payload_len); /* total length */