TSG-17749 tsg-service-chaining-engine更改VXLAN Frame源端口的计算方式

This commit is contained in:
luwenpeng
2023-11-20 10:31:21 +08:00
parent 83f9880ff0
commit 134d2c82b7
14 changed files with 327 additions and 271 deletions

View File

@@ -1,52 +0,0 @@
#ifndef _G_VXLAN_H
#define _G_VXLAN_H
#ifdef __cpluscplus
extern "C"
{
#endif
struct g_vxlan
{
unsigned char flags;
unsigned char reserved[3];
// VNI 3 Bytes
unsigned char vlan_id_half_high;
unsigned char link_layer_type : 4; // 二层报文封装格式
unsigned char vlan_id_half_low : 4;
unsigned int dir_is_e2i : 1;
unsigned int traffic_is_decrypted : 1;
unsigned int sf_index : 5; // max value 32
unsigned int online_test : 1;
// Reserved 1 Bytes
unsigned int r7 : 1;
unsigned int r6 : 1;
unsigned int r5 : 1;
unsigned int r4 : 1;
unsigned int vni_flag : 1;
unsigned int r2 : 1;
unsigned int r1 : 1;
unsigned int r0 : 1;
} __attribute__((__packed__));
void g_vxlan_set_packet_dir(struct g_vxlan *hdr, int dir_is_e2i);
void g_vxlan_set_sf_index(struct g_vxlan *hdr, int sf_index);
void g_vxlan_set_traffic_type(struct g_vxlan *hdr, int traffic_is_decrypted);
int g_vxlan_get_packet_dir(struct g_vxlan *hdr);
int g_vxlan_get_sf_index(struct g_vxlan *hdr);
int g_vxlan_get_traffic_type(struct g_vxlan *hdr);
// return 0 : success
// return -1 : error
int g_vxlan_decode(struct g_vxlan **g_vxlan_hd, const char *raw_data, int raw_len);
#ifdef __cpluscplus
}
#endif
#endif

View File

@@ -10,6 +10,8 @@ extern "C"
#include <string.h>
#include <arpa/inet.h>
#include "uthash.h"
enum address_type
{
ADDR_TYPE_IPV4,
@@ -349,6 +351,13 @@ inline char *four_tuple_tostring(const struct four_tuple *tuple)
return str_ret;
}
inline uint64_t four_tuple_hash(const struct four_tuple *tuple)
{
uint64_t hash_value = 0;
HASH_VALUE(tuple, sizeof(struct four_tuple), hash_value);
return hash_value;
}
#ifdef __cpluscplus
}
#endif

View File

@@ -22,7 +22,6 @@ extern "C"
#define ATOMIC_INC(x) __atomic_fetch_add(x, 1, __ATOMIC_RELAXED)
#define ATOMIC_DEC(x) __atomic_fetch_sub(x, 1, __ATOMIC_RELAXED)
//#define ATOMIC_READ(x) __atomic_fetch_add(x, 0, __ATOMIC_RELAXED)
#define ATOMIC_READ(x) __atomic_load_n(x, __ATOMIC_RELAXED)
#define ATOMIC_ZERO(x) __atomic_fetch_and(x, 0, __ATOMIC_RELAXED)
#define ATOMIC_ADD(x, y) __atomic_fetch_add(x, y, __ATOMIC_RELAXED)
@@ -95,22 +94,6 @@ struct throughput_metrics
void throughput_metrics_inc(struct throughput_metrics *iterm, uint64_t n_pkts, uint64_t n_bytes);
/******************************************************************************
* protocol
******************************************************************************/
struct udp_hdr
{
uint16_t uh_sport; /* source port */
uint16_t uh_dport; /* destination port */
uint16_t uh_ulen; /* udp length */
uint16_t uh_sum; /* udp checksum */
} __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, uint16_t ipid, const in_addr_t src_ip, const in_addr_t dst_ip, uint16_t payload_len);
void build_ether_header(struct ethhdr *eth_hdr, uint16_t next_protocol, const u_char src_mac[], const u_char dst_mac[]);
/******************************************************************************
* device
******************************************************************************/
@@ -119,10 +102,6 @@ int get_ip_by_device_name(const char *dev_name, char *ip_str);
int get_mac_by_device_name(const char *dev_name, char *mac_str);
int str_to_mac(const char *mac_str, u_char mac[]);
#define CHECKSUM_CARRY(x) (x = (x >> 16) + (x & 0xffff), (~(x + (x >> 16)) & 0xffff))
int checksum(uint16_t *addr, int len);
#ifdef __cpluscplus
}
#endif

89
common/include/vxlan.h Normal file
View File

@@ -0,0 +1,89 @@
#ifndef _VXLAN_H
#define _VXLAN_H
#ifdef __cpluscplus
extern "C"
{
#endif
#define __FAVOR_BSD 1
#include <netinet/udp.h>
#include "tuple.h"
/*
* VXLAN Header:
*
* 0 1 2 3
* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* |R|R|R|R|I|R|R|R| Reserved |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | VXLAN Network Identifier (VNI) | Reserved |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*
* Flags (8 bits):
* where the I flag MUST be set to 1 for a valid VXLAN Network ID (VNI).
* The other 7 bits (designated "R") are reserved fields and MUST be set
* to zero on transmission and ignored on receipt.
* Reserved fields (24 bits and 8 bits):
* MUST be set to zero on transmission and ignored on receipt.
*/
struct vxlan_hdr
{
uint8_t flags;
uint8_t reserved[3];
// VNI 3 Bytes
uint8_t vlan_id_half_high;
uint8_t link_layer_type : 4; // 二层报文封装格式
uint8_t vlan_id_half_low : 4;
uint8_t dir : 1;
uint8_t traffic : 1;
uint8_t sf_index : 5; // max value 32
uint8_t online_test : 1;
// Reserved 1 Bytes
uint8_t r7 : 1;
uint8_t r6 : 1;
uint8_t r5 : 1;
uint8_t r4 : 1;
uint8_t vni_flag : 1;
uint8_t r2 : 1;
uint8_t r1 : 1;
uint8_t r0 : 1;
} __attribute__((__packed__));
enum vni_opt
{
VNI_OPT_DIR = 0x80,
VNI_OPT_TRAFFIC = 0x40,
VNI_OPT_SFINDEX = 0x3f,
};
#define VXLAN_FLAGS 0x8
#define VXLAN_DST_PORT 4789
#define VXLAN_FRAME_HDR_LEN (sizeof(struct ethhdr) + sizeof(struct ip) + sizeof(struct udphdr) + sizeof(struct vxlan_hdr))
void vxlan_set_opt(struct vxlan_hdr *hdr, enum vni_opt opt, uint8_t val);
uint8_t vxlan_get_opt(struct vxlan_hdr *hdr, enum vni_opt opt);
// return 0 : success
// return -1 : error
int vxlan_frame_decode(struct vxlan_hdr **vxlan_hdr, const char *data, uint16_t len);
void vxlan_frame_encode(char *buff,
const u_char eth_src_mac[], const u_char eth_dst_mac[],
const in_addr_t ip_src_addr, const in_addr_t ip_dst_addr, uint16_t ip_id,
uint16_t udp_src_port, uint16_t udp_pld_len,
uint8_t vni_opt_dir, uint8_t vni_opt_traffic, uint8_t vni_opt_sf_index);
uint16_t calculate_vxlan_source_port(struct four_tuple *innermost_tuple4);
#ifdef __cpluscplus
}
#endif
#endif