Export tuple.h / packet.h / session.h to the include directory

This commit is contained in:
luwenpeng
2024-04-10 11:40:26 +08:00
parent a5a133bf91
commit 24e109e34f
51 changed files with 1238 additions and 1067 deletions

View File

@@ -1,6 +1,7 @@
add_library(packet packet.cpp)
target_include_directories(packet PUBLIC ${CMAKE_CURRENT_LIST_DIR})
target_include_directories(packet PUBLIC ${CMAKE_SOURCE_DIR}/deps/uthash)
target_include_directories(packet PUBLIC ${CMAKE_SOURCE_DIR}/include)
target_link_libraries(packet tuple log)
add_subdirectory(test)

View File

@@ -9,7 +9,7 @@
#include <linux/ppp_defs.h>
#include "uthash.h"
#include "packet.h"
#include "packet_private.h"
#include "udp_utils.h"
#include "tcp_utils.h"
#include "ipv4_utils.h"
@@ -55,7 +55,7 @@ static inline void set_tuple2(const char *data, enum layer_type type, struct tup
static inline void set_tuple4(const char *data, enum layer_type type, struct tuple4 *tuple);
static inline void set_tuple6(const char *data, enum layer_type type, struct tuple6 *tuple, uint64_t domain);
static inline struct pkt_layer *get_free_layer(struct packet *pkt);
static inline struct packet_layer *get_free_layer(struct packet *pkt);
static inline uint16_t get_gtp_hdr_len(const char *data, uint16_t len);
static inline uint16_t get_gre_hdr_len(const char *data, uint16_t len);
@@ -412,7 +412,7 @@ static inline void set_tuple6(const char *data, enum layer_type type, struct tup
}
}
static inline struct pkt_layer *get_free_layer(struct packet *pkt)
static inline struct packet_layer *get_free_layer(struct packet *pkt)
{
if (pkt->layers_used >= pkt->layers_size)
{
@@ -650,7 +650,7 @@ static inline const char *parse_ether(struct packet *pkt, const char *data, uint
return data;
}
struct pkt_layer *layer = get_free_layer(pkt);
struct packet_layer *layer = get_free_layer(pkt);
if (unlikely(layer == NULL))
{
return data;
@@ -670,7 +670,7 @@ static inline const char *parse_ppp(struct packet *pkt, const char *data, uint16
return data;
}
struct pkt_layer *layer = get_free_layer(pkt);
struct packet_layer *layer = get_free_layer(pkt);
if (unlikely(layer == NULL))
{
return data;
@@ -705,7 +705,7 @@ static inline const char *parse_vlan(struct packet *pkt, const char *data, uint1
return data;
}
struct pkt_layer *layer = get_free_layer(pkt);
struct packet_layer *layer = get_free_layer(pkt);
if (unlikely(layer == NULL))
{
return data;
@@ -728,7 +728,7 @@ static inline const char *parse_pppoe_ses(struct packet *pkt, const char *data,
return data;
}
struct pkt_layer *layer = get_free_layer(pkt);
struct packet_layer *layer = get_free_layer(pkt);
if (unlikely(layer == NULL))
{
return data;
@@ -784,7 +784,7 @@ static inline const char *parse_mpls(struct packet *pkt, const char *data, uint1
return data;
}
struct pkt_layer *layer = get_free_layer(pkt);
struct packet_layer *layer = get_free_layer(pkt);
if (unlikely(layer == NULL))
{
return data;
@@ -860,7 +860,7 @@ static inline const char *parse_ipv4(struct packet *pkt, const char *data, uint1
return data;
}
struct pkt_layer *layer = get_free_layer(pkt);
struct packet_layer *layer = get_free_layer(pkt);
if (unlikely(layer == NULL))
{
return data;
@@ -895,7 +895,7 @@ static inline const char *parse_ipv6(struct packet *pkt, const char *data, uint1
return data;
}
struct pkt_layer *layer = get_free_layer(pkt);
struct packet_layer *layer = get_free_layer(pkt);
if (unlikely(layer == NULL))
{
return data;
@@ -929,7 +929,7 @@ static inline const char *parse_gre(struct packet *pkt, const char *data, uint16
return data;
}
struct pkt_layer *layer = get_free_layer(pkt);
struct packet_layer *layer = get_free_layer(pkt);
if (unlikely(layer == NULL))
{
return data;
@@ -960,7 +960,7 @@ static inline const char *parse_udp(struct packet *pkt, const char *data, uint16
return data;
}
struct pkt_layer *layer = get_free_layer(pkt);
struct packet_layer *layer = get_free_layer(pkt);
if (unlikely(layer == NULL))
{
return data;
@@ -993,7 +993,7 @@ static inline const char *parse_tcp(struct packet *pkt, const char *data, uint16
return data;
}
struct pkt_layer *layer = get_free_layer(pkt);
struct packet_layer *layer = get_free_layer(pkt);
if (unlikely(layer == NULL))
{
return data;
@@ -1025,7 +1025,7 @@ static inline const char *parse_vxlan(struct packet *pkt, const char *data, uint
return data;
}
struct pkt_layer *layer = get_free_layer(pkt);
struct packet_layer *layer = get_free_layer(pkt);
if (unlikely(layer == NULL))
{
return data;
@@ -1045,7 +1045,7 @@ static inline const char *parse_gtpv1_u(struct packet *pkt, const char *data, ui
return data;
}
struct pkt_layer *layer = get_free_layer(pkt);
struct packet_layer *layer = get_free_layer(pkt);
if (unlikely(layer == NULL))
{
return data;
@@ -1149,7 +1149,7 @@ void packet_print(const struct packet *pkt)
pkt->layers_used, pkt->layers_size);
for (uint8_t i = 0; i < pkt->layers_used; i++)
{
const struct pkt_layer *layer = &pkt->layers[i];
const struct packet_layer *layer = &pkt->layers[i];
printf(" layer[%u]: %p, type: %s, hdr_offset: %u, hdr_ptr: %p, hdr_len: %u, pld_ptr: %p, pld_len: %u\n",
i, layer, layer_type_to_str(layer->type), layer->hdr_offset,
layer->hdr_ptr, layer->hdr_len, layer->pld_ptr, layer->pld_len);
@@ -1166,7 +1166,7 @@ void packet_print(const struct packet *pkt)
int packet_get_innermost_tuple2(const struct packet *pkt, struct tuple2 *tuple)
{
memset(tuple, 0, sizeof(struct tuple2));
const struct pkt_layer *layer = NULL;
const struct packet_layer *layer = NULL;
for (int8_t i = pkt->layers_used - 1; i >= 0; i--)
{
@@ -1187,7 +1187,7 @@ int packet_get_innermost_tuple2(const struct packet *pkt, struct tuple2 *tuple)
int packet_get_outermost_tuple2(const struct packet *pkt, struct tuple2 *tuple)
{
memset(tuple, 0, sizeof(struct tuple2));
const struct pkt_layer *layer = NULL;
const struct packet_layer *layer = NULL;
for (int8_t i = 0; i < pkt->layers_used; i++)
{
@@ -1208,9 +1208,9 @@ int packet_get_outermost_tuple2(const struct packet *pkt, struct tuple2 *tuple)
int packet_get_innermost_tuple4(const struct packet *pkt, struct tuple4 *tuple)
{
memset(tuple, 0, sizeof(struct tuple4));
const struct pkt_layer *layer_l3 = NULL;
const struct pkt_layer *layer_l4 = NULL;
const struct pkt_layer *layer = NULL;
const struct packet_layer *layer_l3 = NULL;
const struct packet_layer *layer_l4 = NULL;
const struct packet_layer *layer = NULL;
for (int8_t i = pkt->layers_used - 1; i >= 0; i--)
{
@@ -1248,9 +1248,9 @@ int packet_get_innermost_tuple4(const struct packet *pkt, struct tuple4 *tuple)
int packet_get_outermost_tuple4(const struct packet *pkt, struct tuple4 *tuple)
{
memset(tuple, 0, sizeof(struct tuple4));
const struct pkt_layer *layer_l3 = NULL;
const struct pkt_layer *layer_l4 = NULL;
const struct pkt_layer *layer = NULL;
const struct packet_layer *layer_l3 = NULL;
const struct packet_layer *layer_l4 = NULL;
const struct packet_layer *layer = NULL;
for (int8_t i = 0; i < pkt->layers_used; i++)
{
@@ -1288,9 +1288,9 @@ int packet_get_outermost_tuple4(const struct packet *pkt, struct tuple4 *tuple)
int packet_get_innermost_tuple6(const struct packet *pkt, struct tuple6 *tuple)
{
memset(tuple, 0, sizeof(struct tuple6));
const struct pkt_layer *layer_l3 = NULL;
const struct pkt_layer *layer_l4 = NULL;
const struct pkt_layer *layer = NULL;
const struct packet_layer *layer_l3 = NULL;
const struct packet_layer *layer_l4 = NULL;
const struct packet_layer *layer = NULL;
const struct metadata *meta = &pkt->meta;
for (int8_t i = pkt->layers_used - 1; i >= 0; i--)
@@ -1329,9 +1329,9 @@ int packet_get_innermost_tuple6(const struct packet *pkt, struct tuple6 *tuple)
int packet_get_outermost_tuple6(const struct packet *pkt, struct tuple6 *tuple)
{
memset(tuple, 0, sizeof(struct tuple6));
const struct pkt_layer *layer_l3 = NULL;
const struct pkt_layer *layer_l4 = NULL;
const struct pkt_layer *layer = NULL;
const struct packet_layer *layer_l3 = NULL;
const struct packet_layer *layer_l4 = NULL;
const struct packet_layer *layer = NULL;
const struct metadata *meta = &pkt->meta;
for (int8_t i = 0; i < pkt->layers_used; i++)
@@ -1365,9 +1365,9 @@ int packet_get_outermost_tuple6(const struct packet *pkt, struct tuple6 *tuple)
}
}
const struct pkt_layer *packet_get_innermost_layer(const struct packet *pkt, enum layer_type type)
const struct packet_layer *packet_get_innermost_layer(const struct packet *pkt, enum layer_type type)
{
const struct pkt_layer *layer = NULL;
const struct packet_layer *layer = NULL;
for (int8_t i = pkt->layers_used - 1; i >= 0; i--)
{
@@ -1381,9 +1381,9 @@ const struct pkt_layer *packet_get_innermost_layer(const struct packet *pkt, enu
return NULL;
}
const struct pkt_layer *packet_get_outermost_layer(const struct packet *pkt, enum layer_type type)
const struct packet_layer *packet_get_outermost_layer(const struct packet *pkt, enum layer_type type)
{
const struct pkt_layer *layer = NULL;
const struct packet_layer *layer = NULL;
for (int8_t i = 0; i < pkt->layers_used; i++)
{
@@ -1517,6 +1517,20 @@ uint64_t packet_get_hash(const struct packet *pkt, enum ldbc_method method, int
return hash_value;
}
int8_t packet_get_layers(const struct packet *pkt)
{
return pkt->layers_used;
}
const struct packet_layer *packet_get_layer(const struct packet *pkt, int8_t idx)
{
if (idx < 0 || idx >= pkt->layers_used)
{
return NULL;
}
return &pkt->layers[idx];
}
/******************************************************************************
* Packet Meta Data
******************************************************************************/

View File

@@ -1,5 +1,5 @@
#ifndef _PACKET_H
#define _PACKET_H
#ifndef _PACKET_PRIVATE_H
#define _PACKET_PRIVATE_H
#ifdef __cpluscplus
extern "C"
@@ -8,49 +8,15 @@ extern "C"
#include <stdint.h>
#include <stdio.h>
#include "tuple.h"
#include "log.h"
#include "tuple.h"
#include "packet.h"
#define PACKET_MAX_LAYERS 32
#define PACKET_LOG_ERROR(format, ...) LOG_ERROR("packet", format, ##__VA_ARGS__)
#define PACKET_LOG_DEBUG(format, ...) LOG_DEBUG("packet", format, ##__VA_ARGS__)
enum layer_type
{
// 数据链路层
LAYER_TYPE_ETHER = 1 << 0,
LAYER_TYPE_PPP = 1 << 1,
LAYER_TYPE_HDLC = 1 << 2,
LAYER_TYPE_L2 = (LAYER_TYPE_ETHER | LAYER_TYPE_PPP | LAYER_TYPE_HDLC),
// 数据链路层 -- 隧道
LAYER_TYPE_VLAN = 1 << 3,
LAYER_TYPE_PPPOE = 1 << 4,
LAYER_TYPE_MPLS = 1 << 5,
LAYER_TYPE_L2_TUN = (LAYER_TYPE_VLAN | LAYER_TYPE_PPPOE | LAYER_TYPE_MPLS),
// 网络层
LAYER_TYPE_IPV4 = 1 << 6,
LAYER_TYPE_IPV6 = 1 << 7,
LAYER_TYPE_L3 = (LAYER_TYPE_IPV4 | LAYER_TYPE_IPV6),
// 网络层 -- 隧道
LAYER_TYPE_GRE = 1 << 8,
LAYER_TYPE_L3_TUN = (LAYER_TYPE_GRE),
// 传输层
LAYER_TYPE_UDP = 1 << 9,
LAYER_TYPE_TCP = 1 << 10,
LAYER_TYPE_L4 = (LAYER_TYPE_UDP | LAYER_TYPE_TCP),
// 传输层 -- 隧道
LAYER_TYPE_VXLAN = 1 << 11,
LAYER_TYPE_GTPV1_U = 1 << 12,
// ALL
LAYER_TYPE_ALL = (LAYER_TYPE_L2 | LAYER_TYPE_L2_TUN | LAYER_TYPE_L3 | LAYER_TYPE_L3_TUN | LAYER_TYPE_L4 | LAYER_TYPE_VXLAN | LAYER_TYPE_GTPV1_U),
};
enum ldbc_method
{
LDBC_METHOD_HASH_INT_IP = 1,
@@ -102,20 +68,10 @@ struct metadata
enum packet_type type;
};
struct pkt_layer
{
enum layer_type type;
const char *hdr_ptr; // header pointer
const char *pld_ptr; // payload pointer
uint16_t hdr_offset; // header offset from data_ptr
uint16_t hdr_len; // header length
uint16_t pld_len; // payload length
};
struct packet
{
struct pkt_layer layers[PACKET_MAX_LAYERS];
struct pkt_layer *frag_layer; // fragment layer
struct packet_layer layers[PACKET_MAX_LAYERS];
struct packet_layer *frag_layer; // fragment layer
int8_t layers_used;
int8_t layers_size;
@@ -130,24 +86,6 @@ struct packet
const char *packet_parse(struct packet *pkt, const char *data, uint16_t len);
void packet_print(const struct packet *pkt);
// return 0: found
// return -1: not found
int packet_get_innermost_tuple2(const struct packet *pkt, struct tuple2 *tuple);
int packet_get_outermost_tuple2(const struct packet *pkt, struct tuple2 *tuple);
// return 0: found
// return -1: not found
int packet_get_innermost_tuple4(const struct packet *pkt, struct tuple4 *tuple);
int packet_get_outermost_tuple4(const struct packet *pkt, struct tuple4 *tuple);
// return 0: found
// return -1: not found
int packet_get_innermost_tuple6(const struct packet *pkt, struct tuple6 *tuple);
int packet_get_outermost_tuple6(const struct packet *pkt, struct tuple6 *tuple);
const struct pkt_layer *packet_get_innermost_layer(const struct packet *pkt, enum layer_type type);
const struct pkt_layer *packet_get_outermost_layer(const struct packet *pkt, enum layer_type type);
// direction 1: E2I
// direction 0: I2E
uint64_t packet_get_hash(const struct packet *pkt, enum ldbc_method method, int direction);

File diff suppressed because it is too large Load Diff

View File

@@ -1,6 +1,6 @@
#include <gtest/gtest.h>
#include "packet.h"
#include "packet_private.h"
/******************************************************************************
* [Protocols in frame: eth:ethertype:ip:data]