使用自定义的udp_hdr替换linux的udphdr
This commit is contained in:
@@ -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)
|
||||
|
||||
|
||||
@@ -40,7 +40,7 @@ int fixed_num_array_index_elem(struct fixed_num_array *array, int index);
|
||||
* sids
|
||||
******************************************************************************/
|
||||
|
||||
#include "marsio.h"
|
||||
#include <marsio.h>
|
||||
|
||||
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);
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#include <netinet/udp.h>
|
||||
#include <netinet/ip.h>
|
||||
#include <netinet/ether.h>
|
||||
|
||||
#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;
|
||||
}
|
||||
@@ -7,7 +7,6 @@
|
||||
#include <netinet/ip6.h>
|
||||
#define __FAVOR_BSD 1
|
||||
#include <netinet/tcp.h>
|
||||
#include <netinet/udp.h>
|
||||
#include <netinet/ether.h>
|
||||
#include <linux/ppp_defs.h>
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -2,12 +2,11 @@
|
||||
#include <assert.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <linux/if.h>
|
||||
#include <net/if.h>
|
||||
#include <netinet/ip.h>
|
||||
#include <netinet/ether.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <netinet/ip.h>
|
||||
#include <netinet/udp.h>
|
||||
#include <netinet/ether.h>
|
||||
|
||||
#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);
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user