From 551abde8871070c085ef8c11d99a79ce901bfc87 Mon Sep 17 00:00:00 2001 From: luwenpeng Date: Fri, 17 Feb 2023 17:53:21 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8=E8=87=AA=E5=AE=9A=E4=B9=89?= =?UTF-8?q?=E7=9A=84udp=5Fhdr=E6=9B=BF=E6=8D=A2linux=E7=9A=84udphdr?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/CMakeLists.txt | 1 + common/include/utils.h | 12 ++++++++++-- common/src/g_vxlan.cpp | 8 ++++---- common/src/raw_packet.cpp | 11 +++++------ common/src/utils.cpp | 13 ++++++------- platform/src/packet_io.cpp | 13 +++++++------ 6 files changed, 33 insertions(+), 25 deletions(-) diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index 854594f..e042cdd 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt @@ -1,5 +1,6 @@ add_library(common src/addr_tuple4.cpp src/session_table.cpp src/raw_packet.cpp src/ctrl_packet.cpp src/bfd.cpp src/utils.cpp src/g_vxlan.cpp) target_link_libraries(common PUBLIC cjson) +target_link_libraries(common PUBLIC mrzcpd) target_include_directories(common PUBLIC ${CMAKE_CURRENT_LIST_DIR}/include) diff --git a/common/include/utils.h b/common/include/utils.h index 1256ee9..740afd3 100644 --- a/common/include/utils.h +++ b/common/include/utils.h @@ -40,7 +40,7 @@ int fixed_num_array_index_elem(struct fixed_num_array *array, int index); * sids ******************************************************************************/ -#include "marsio.h" +#include struct sids { @@ -67,7 +67,15 @@ void throughput_metrics_inc(struct throughput_metrics *iterm, uint64_t n_pkts, u * protocol ******************************************************************************/ -void build_udp_header(const char *l3_hdr, int l3_hdr_len, struct udphdr *udp_hdr, u_int16_t udp_sport, u_int16_t udp_dport, int payload_len); +struct udp_hdr +{ + u_int16_t uh_sport; /* source port */ + u_int16_t uh_dport; /* destination port */ + u_int16_t uh_ulen; /* udp length */ + u_int16_t uh_sum; /* udp checksum */ +} __attribute__((__packed__)); + +void build_udp_header(const char *l3_hdr, int l3_hdr_len, struct udp_hdr *udp_hdr, u_int16_t udp_sport, u_int16_t udp_dport, int payload_len); void build_ip_header(struct ip *ip_hdr, u_int8_t next_protocol, 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/g_vxlan.cpp b/common/src/g_vxlan.cpp index ec005c3..a6dcdc9 100644 --- a/common/src/g_vxlan.cpp +++ b/common/src/g_vxlan.cpp @@ -1,7 +1,7 @@ -#include #include #include +#include "utils.h" #include "g_vxlan.h" void g_vxlan_set_packet_dir(struct g_vxlan *hdr, int dir_is_e2i) @@ -38,7 +38,7 @@ int g_vxlan_get_traffic_type(struct g_vxlan *hdr) // return -1 : error int g_vxlan_decode(struct g_vxlan **g_vxlan_hdr, const char *raw_data, int raw_len) { - if (raw_len <= (int)(sizeof(struct ethhdr) + sizeof(struct ip) + sizeof(struct udphdr) + sizeof(struct g_vxlan))) + if (raw_len <= (int)(sizeof(struct ethhdr) + sizeof(struct ip) + sizeof(struct udp_hdr) + sizeof(struct g_vxlan))) { return -1; } @@ -55,13 +55,13 @@ int g_vxlan_decode(struct g_vxlan **g_vxlan_hdr, const char *raw_data, int raw_l return -1; } - struct udphdr *udp_hdr = (struct udphdr *)((char *)ip_hdr + sizeof(struct ip)); + struct udp_hdr *udp_hdr = (struct udp_hdr *)((char *)ip_hdr + sizeof(struct ip)); if (udp_hdr->uh_dport != htons(4789)) { return -1; } - *g_vxlan_hdr = (struct g_vxlan *)((char *)udp_hdr + sizeof(struct udphdr)); + *g_vxlan_hdr = (struct g_vxlan *)((char *)udp_hdr + sizeof(struct udp_hdr)); return 0; } \ No newline at end of file diff --git a/common/src/raw_packet.cpp b/common/src/raw_packet.cpp index 9cb8ef6..2871eb1 100644 --- a/common/src/raw_packet.cpp +++ b/common/src/raw_packet.cpp @@ -7,7 +7,6 @@ #include #define __FAVOR_BSD 1 #include -#include #include #include @@ -471,7 +470,7 @@ static const char *ldbc_method_to_string(enum ldbc_method ldbc_method) static void set_addr_tuple4(const void *data, enum layer_type layer_type, struct addr_tuple4 *addr) { const struct tcphdr *tcp_hdr = NULL; - const struct udphdr *udp_hdr = NULL; + const struct udp_hdr *udp_hdr = NULL; const struct ip *ipv4_hdr = NULL; const struct ip6_hdr *ipv6_hdr = NULL; @@ -483,7 +482,7 @@ static void set_addr_tuple4(const void *data, enum layer_type layer_type, struct addr->dst_port = tcp_hdr->th_dport; break; case LAYER_TYPE_UDP: - udp_hdr = (const struct udphdr *)data; + udp_hdr = (const struct udp_hdr *)data; addr->src_port = udp_hdr->uh_sport; addr->dst_port = udp_hdr->uh_dport; break; @@ -737,7 +736,7 @@ static const void *parse_tcp(struct raw_pkt_parser *handler, const void *data, s static const void *parse_udp(struct raw_pkt_parser *handler, const void *data, size_t length, enum layer_type this_type) { - if (length < sizeof(struct udphdr)) + if (length < sizeof(struct udp_hdr)) { LOG_ERROR("%s: pkt_trace_id: %lu, this_layer: %s, err: data not enough", LOG_TAG_RAWPKT, handler->pkt_trace_id, layer_type2str(this_type)); return data; @@ -748,8 +747,8 @@ static const void *parse_udp(struct raw_pkt_parser *handler, const void *data, s return data; } - struct udphdr *hdr = (struct udphdr *)data; - uint16_t hdr_len = sizeof(struct udphdr); + struct udp_hdr *hdr = (struct udp_hdr *)data; + uint16_t hdr_len = sizeof(struct udp_hdr); const void *data_next_layer = (const char *)data + hdr_len; size_t data_next_length = length - hdr_len; diff --git a/common/src/utils.cpp b/common/src/utils.cpp index 699e424..2243e02 100644 --- a/common/src/utils.cpp +++ b/common/src/utils.cpp @@ -2,12 +2,11 @@ #include #include #include -#include +#include +#include +#include #include #include -#include -#include -#include #include "utils.h" #include "log.h" @@ -161,11 +160,11 @@ static int checksum(u_int16_t *addr, int len) return sum; } -void build_udp_header(const char *l3_hdr, int l3_hdr_len, struct udphdr *udp_hdr, u_int16_t udp_sport, u_int16_t udp_dport, int payload_len) +void build_udp_header(const char *l3_hdr, int l3_hdr_len, struct udp_hdr *udp_hdr, u_int16_t udp_sport, u_int16_t udp_dport, int payload_len) { - memset(udp_hdr, 0, sizeof(struct udphdr)); + memset(udp_hdr, 0, sizeof(struct udp_hdr)); - int udp_hlen = sizeof(struct udphdr) + payload_len; + int udp_hlen = sizeof(struct udp_hdr) + payload_len; udp_hdr->uh_sport = htons(udp_sport); udp_hdr->uh_dport = htons(udp_dport); diff --git a/platform/src/packet_io.cpp b/platform/src/packet_io.cpp index 5a17ff8..9aa3238 100644 --- a/platform/src/packet_io.cpp +++ b/platform/src/packet_io.cpp @@ -8,6 +8,7 @@ #include "log.h" #include "sce.h" +#include "utils.h" #include "g_vxlan.h" #include "ctrl_packet.h" @@ -759,7 +760,7 @@ static enum inject_pkt_action handle_inject_packet(struct packet_io *handle, mar struct metadata meta; memset(&meta, 0, sizeof(struct metadata)); meta.raw_data = (char *)g_vxlan_hdr + sizeof(struct g_vxlan); - meta.raw_len = raw_len - sizeof(struct ethhdr) - sizeof(struct ip) - sizeof(struct udphdr) - sizeof(struct g_vxlan); + meta.raw_len = raw_len - sizeof(struct ethhdr) - sizeof(struct ip) - sizeof(struct udp_hdr) - sizeof(struct g_vxlan); meta.dir_is_e2i = g_vxlan_get_packet_dir(g_vxlan_hdr); meta.traffic_is_decrypted = g_vxlan_get_traffic_type(g_vxlan_hdr); meta.is_ctrl_pkt = 0; @@ -879,10 +880,10 @@ static int forward_packet_to_sf(struct packet_io *handle, marsio_buff_t *rx_buff // TODO get dst mac by dst ip marsio_buff_reset(rx_buff); - struct ethhdr *eth_hdr = (struct ethhdr *)marsio_buff_prepend(rx_buff, sizeof(struct ethhdr) + sizeof(struct ip) + sizeof(struct udphdr) + sizeof(struct g_vxlan)); + struct ethhdr *eth_hdr = (struct ethhdr *)marsio_buff_prepend(rx_buff, sizeof(struct ethhdr) + sizeof(struct ip) + sizeof(struct udp_hdr) + sizeof(struct g_vxlan)); struct ip *ip_hdr = (struct ip *)((char *)eth_hdr + sizeof(struct ethhdr)); - struct udphdr *udp_hdr = (struct udphdr *)((char *)ip_hdr + sizeof(struct ip)); - struct g_vxlan *g_vxlan_hdr = (struct g_vxlan *)((char *)udp_hdr + sizeof(struct udphdr)); + struct udp_hdr *udp_hdr = (struct udp_hdr *)((char *)ip_hdr + sizeof(struct ip)); + struct g_vxlan *g_vxlan_hdr = (struct g_vxlan *)((char *)udp_hdr + sizeof(struct udp_hdr)); memset(g_vxlan_hdr, 0, sizeof(struct g_vxlan)); g_vxlan_set_packet_dir(g_vxlan_hdr, meta->dir_is_e2i); @@ -890,7 +891,7 @@ static int forward_packet_to_sf(struct packet_io *handle, marsio_buff_t *rx_buff g_vxlan_set_traffic_type(g_vxlan_hdr, meta->traffic_is_decrypted); build_ether_header(eth_hdr, ETH_P_IP, handle->config.default_src_mac, handle->config.default_dst_mac); - build_ip_header(ip_hdr, IPPROTO_UDP, handle->config.default_src_ip, handle->config.default_dst_ip, sizeof(struct udphdr) + sizeof(struct g_vxlan) + meta->raw_len); + build_ip_header(ip_hdr, IPPROTO_UDP, handle->config.default_src_ip, handle->config.default_dst_ip, sizeof(struct udp_hdr) + sizeof(struct g_vxlan) + meta->raw_len); build_udp_header((const char *)&ip_hdr->ip_src, 8, udp_hdr, meta->session_id % (65535 - 49152) + 49152, 4789, sizeof(struct g_vxlan) + meta->raw_len); if (marsio_send_burst(handle->dev_endpoint.mr_path, thread_seq, &rx_buff, 1) != 0) @@ -1016,7 +1017,7 @@ static void forward_all_sf_packet_to_nf(struct packet_io *handle, marsio_buff_t struct metadata meta; memset(&meta, 0, sizeof(struct metadata)); meta.raw_data = (char *)g_vxlan_hdr + sizeof(struct g_vxlan); - meta.raw_len = raw_len - sizeof(struct ethhdr) - sizeof(struct ip) - sizeof(struct udphdr) - sizeof(struct g_vxlan); + meta.raw_len = raw_len - sizeof(struct ethhdr) - sizeof(struct ip) - sizeof(struct udp_hdr) - sizeof(struct g_vxlan); meta.dir_is_e2i = g_vxlan_get_packet_dir(g_vxlan_hdr); meta.traffic_is_decrypted = g_vxlan_get_traffic_type(g_vxlan_hdr); meta.is_ctrl_pkt = 0;