Refactored packet API to support struct layer (using union to contain different types of encapsulation headers)
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
add_library(packet packet.cpp packet_utils.cpp)
|
||||
add_library(packet packet.cpp packet_utils.cpp packet_layer.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 mrzcpd)
|
||||
target_link_libraries(packet tuple log)
|
||||
|
||||
add_subdirectory(test)
|
||||
@@ -6,6 +6,7 @@
|
||||
|
||||
#include "uthash.h"
|
||||
#include "packet_priv.h"
|
||||
#include "packet_utils.h"
|
||||
#include "eth_utils.h"
|
||||
#include "gre_utils.h"
|
||||
#include "udp_utils.h"
|
||||
@@ -49,11 +50,11 @@
|
||||
******************************************************************************/
|
||||
|
||||
static inline const char *ldbc_method_to_str(enum ldbc_method method);
|
||||
static inline const char *layer_proto_to_str(enum layer_proto type);
|
||||
static inline const char *layer_proto_to_str(enum layer_proto proto);
|
||||
|
||||
static inline void set_tuple2(const char *data, enum layer_proto type, struct tuple2 *tuple);
|
||||
static inline void set_tuple4(const char *data, enum layer_proto type, struct tuple4 *tuple);
|
||||
static inline void set_tuple6(const char *data, enum layer_proto type, struct tuple6 *tuple, uint64_t domain);
|
||||
static inline void set_tuple2(const char *data, enum layer_proto proto, struct tuple2 *tuple);
|
||||
static inline void set_tuple4(const char *data, enum layer_proto proto, struct tuple4 *tuple);
|
||||
static inline void set_tuple6(const char *data, enum layer_proto proto, struct tuple6 *tuple, uint64_t domain);
|
||||
|
||||
static inline struct raw_layer *get_free_layer(struct packet *pkt);
|
||||
|
||||
@@ -112,9 +113,9 @@ static inline const char *ldbc_method_to_str(enum ldbc_method method)
|
||||
}
|
||||
}
|
||||
|
||||
static inline const char *layer_proto_to_str(enum layer_proto type)
|
||||
static inline const char *layer_proto_to_str(enum layer_proto proto)
|
||||
{
|
||||
switch (type)
|
||||
switch (proto)
|
||||
{
|
||||
case LAYER_PROTO_ETHER:
|
||||
return "ETH";
|
||||
@@ -155,12 +156,12 @@ static inline const char *layer_proto_to_str(enum layer_proto type)
|
||||
}
|
||||
}
|
||||
|
||||
static inline void set_tuple2(const char *data, enum layer_proto type, struct tuple2 *tuple)
|
||||
static inline void set_tuple2(const char *data, enum layer_proto proto, struct tuple2 *tuple)
|
||||
{
|
||||
const struct ip *ipv4 = NULL;
|
||||
const struct ip6_hdr *ipv6 = NULL;
|
||||
|
||||
switch (type)
|
||||
switch (proto)
|
||||
{
|
||||
case LAYER_PROTO_IPV4:
|
||||
ipv4 = (const struct ip *)data;
|
||||
@@ -179,14 +180,14 @@ static inline void set_tuple2(const char *data, enum layer_proto type, struct tu
|
||||
}
|
||||
}
|
||||
|
||||
static inline void set_tuple4(const char *data, enum layer_proto type, struct tuple4 *tuple)
|
||||
static inline void set_tuple4(const char *data, enum layer_proto proto, struct tuple4 *tuple)
|
||||
{
|
||||
const struct ip *ipv4 = NULL;
|
||||
const struct ip6_hdr *ipv6 = NULL;
|
||||
const struct tcphdr *tcp = NULL;
|
||||
const struct udphdr *udp = NULL;
|
||||
|
||||
switch (type)
|
||||
switch (proto)
|
||||
{
|
||||
case LAYER_PROTO_TCP:
|
||||
tcp = (const struct tcphdr *)data;
|
||||
@@ -215,7 +216,7 @@ static inline void set_tuple4(const char *data, enum layer_proto type, struct tu
|
||||
}
|
||||
}
|
||||
|
||||
static inline void set_tuple6(const char *data, enum layer_proto type, struct tuple6 *tuple, uint64_t domain)
|
||||
static inline void set_tuple6(const char *data, enum layer_proto proto, struct tuple6 *tuple, uint64_t domain)
|
||||
{
|
||||
const struct ip *ipv4 = NULL;
|
||||
const struct ip6_hdr *ipv6 = NULL;
|
||||
@@ -224,7 +225,7 @@ static inline void set_tuple6(const char *data, enum layer_proto type, struct tu
|
||||
|
||||
tuple->domain = domain;
|
||||
|
||||
switch (type)
|
||||
switch (proto)
|
||||
{
|
||||
case LAYER_PROTO_TCP:
|
||||
tcp = (const struct tcphdr *)data;
|
||||
@@ -265,9 +266,9 @@ static inline struct raw_layer *get_free_layer(struct packet *pkt)
|
||||
return &pkt->layers[pkt->layers_used];
|
||||
}
|
||||
|
||||
#define SET_LAYER(_pkt, _layer, _type, _hdr_len, _data, _len, _trim) \
|
||||
#define SET_LAYER(_pkt, _layer, _proto, _hdr_len, _data, _len, _trim) \
|
||||
{ \
|
||||
(_layer)->type = (_type); \
|
||||
(_layer)->proto = (_proto); \
|
||||
(_layer)->hdr_offset = (_pkt)->data_len - (_pkt)->trim_len - (_len); \
|
||||
(_layer)->hdr_ptr = (_data); \
|
||||
(_layer)->hdr_len = (_hdr_len); \
|
||||
@@ -276,7 +277,7 @@ static inline struct raw_layer *get_free_layer(struct packet *pkt)
|
||||
(_pkt)->trim_len += (_trim); \
|
||||
(_pkt)->layers_used++; \
|
||||
PACKET_LOG_DEBUG("layer[%d/%d]: %s, hdr_offset: %d, hdr_ptr: %p, hdr_len: %d, pld_ptr: %p, pld_len: %d", \
|
||||
(_pkt)->layers_used - 1, (_pkt)->layers_size, layer_proto_to_str((_type)), \
|
||||
(_pkt)->layers_used - 1, (_pkt)->layers_size, layer_proto_to_str((_proto)), \
|
||||
(_layer)->hdr_offset, (_layer)->hdr_ptr, (_layer)->hdr_len, (_layer)->pld_ptr, (_layer)->pld_len); \
|
||||
}
|
||||
|
||||
@@ -1148,10 +1149,10 @@ void packet_print_str(const struct packet *pkt)
|
||||
{
|
||||
int used = 0;
|
||||
const struct raw_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_proto_to_str(layer->type), layer->hdr_offset,
|
||||
printf(" layer[%u]: %p, proto: %s, hdr_offset: %u, hdr_ptr: %p, hdr_len: %u, pld_ptr: %p, pld_len: %u\n",
|
||||
i, layer, layer_proto_to_str(layer->proto), layer->hdr_offset,
|
||||
layer->hdr_ptr, layer->hdr_len, layer->pld_ptr, layer->pld_len);
|
||||
switch (layer->type)
|
||||
switch (layer->proto)
|
||||
{
|
||||
case LAYER_PROTO_ETHER:
|
||||
used = eth_hdr_to_str((const struct ethhdr *)layer->hdr_ptr, buffer, sizeof(buffer));
|
||||
@@ -1219,9 +1220,9 @@ int packet_get_innermost_tuple2(const struct packet *pkt, struct tuple2 *tuple)
|
||||
{
|
||||
layer = &pkt->layers[i];
|
||||
|
||||
if (layer->type == LAYER_PROTO_IPV4 || layer->type == LAYER_PROTO_IPV6)
|
||||
if (layer->proto == LAYER_PROTO_IPV4 || layer->proto == LAYER_PROTO_IPV6)
|
||||
{
|
||||
set_tuple2((const char *)pkt->data_ptr + layer->hdr_offset, layer->type, tuple);
|
||||
set_tuple2((const char *)pkt->data_ptr + layer->hdr_offset, layer->proto, tuple);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@@ -1240,9 +1241,9 @@ int packet_get_outermost_tuple2(const struct packet *pkt, struct tuple2 *tuple)
|
||||
{
|
||||
layer = &pkt->layers[i];
|
||||
|
||||
if (layer->type == LAYER_PROTO_IPV4 || layer->type == LAYER_PROTO_IPV6)
|
||||
if (layer->proto == LAYER_PROTO_IPV4 || layer->proto == LAYER_PROTO_IPV6)
|
||||
{
|
||||
set_tuple2((const char *)pkt->data_ptr + layer->hdr_offset, layer->type, tuple);
|
||||
set_tuple2((const char *)pkt->data_ptr + layer->hdr_offset, layer->proto, tuple);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@@ -1264,14 +1265,14 @@ int packet_get_innermost_tuple4(const struct packet *pkt, struct tuple4 *tuple)
|
||||
layer = &pkt->layers[i];
|
||||
|
||||
// first get L4 layer
|
||||
if (layer->type == LAYER_PROTO_UDP || layer->type == LAYER_PROTO_TCP)
|
||||
if (layer->proto == LAYER_PROTO_UDP || layer->proto == LAYER_PROTO_TCP)
|
||||
{
|
||||
layer_l4 = layer;
|
||||
continue;
|
||||
}
|
||||
|
||||
// second get L3 layer
|
||||
if (layer->type == LAYER_PROTO_IPV4 || layer->type == LAYER_PROTO_IPV6)
|
||||
if (layer->proto == LAYER_PROTO_IPV4 || layer->proto == LAYER_PROTO_IPV6)
|
||||
{
|
||||
layer_l3 = layer;
|
||||
break;
|
||||
@@ -1280,8 +1281,8 @@ int packet_get_innermost_tuple4(const struct packet *pkt, struct tuple4 *tuple)
|
||||
|
||||
if (layer_l3 && layer_l4)
|
||||
{
|
||||
set_tuple4((const char *)pkt->data_ptr + layer_l3->hdr_offset, layer_l3->type, tuple);
|
||||
set_tuple4((const char *)pkt->data_ptr + layer_l4->hdr_offset, layer_l4->type, tuple);
|
||||
set_tuple4((const char *)pkt->data_ptr + layer_l3->hdr_offset, layer_l3->proto, tuple);
|
||||
set_tuple4((const char *)pkt->data_ptr + layer_l4->hdr_offset, layer_l4->proto, tuple);
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
@@ -1304,14 +1305,14 @@ int packet_get_outermost_tuple4(const struct packet *pkt, struct tuple4 *tuple)
|
||||
layer = &pkt->layers[i];
|
||||
|
||||
// first get L3 layer
|
||||
if (layer->type == LAYER_PROTO_IPV4 || layer->type == LAYER_PROTO_IPV6)
|
||||
if (layer->proto == LAYER_PROTO_IPV4 || layer->proto == LAYER_PROTO_IPV6)
|
||||
{
|
||||
layer_l3 = layer;
|
||||
continue;
|
||||
}
|
||||
|
||||
// second get L4 layer
|
||||
if (layer->type == LAYER_PROTO_UDP || layer->type == LAYER_PROTO_TCP)
|
||||
if (layer->proto == LAYER_PROTO_UDP || layer->proto == LAYER_PROTO_TCP)
|
||||
{
|
||||
layer_l4 = layer;
|
||||
break;
|
||||
@@ -1320,8 +1321,8 @@ int packet_get_outermost_tuple4(const struct packet *pkt, struct tuple4 *tuple)
|
||||
|
||||
if (layer_l3 && layer_l4)
|
||||
{
|
||||
set_tuple4((const char *)pkt->data_ptr + layer_l3->hdr_offset, layer_l3->type, tuple);
|
||||
set_tuple4((const char *)pkt->data_ptr + layer_l4->hdr_offset, layer_l4->type, tuple);
|
||||
set_tuple4((const char *)pkt->data_ptr + layer_l3->hdr_offset, layer_l3->proto, tuple);
|
||||
set_tuple4((const char *)pkt->data_ptr + layer_l4->hdr_offset, layer_l4->proto, tuple);
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
@@ -1338,21 +1339,21 @@ int packet_get_innermost_tuple6(const struct packet *pkt, struct tuple6 *tuple)
|
||||
const struct raw_layer *layer_l3 = NULL;
|
||||
const struct raw_layer *layer_l4 = NULL;
|
||||
const struct raw_layer *layer = NULL;
|
||||
uint64_t domain = packet_get_domain(pkt);
|
||||
uint64_t domain = packet_get_domain_id(pkt);
|
||||
|
||||
for (int8_t i = pkt->layers_used - 1; i >= 0; i--)
|
||||
{
|
||||
layer = &pkt->layers[i];
|
||||
|
||||
// first get L4 layer
|
||||
if (layer->type == LAYER_PROTO_UDP || layer->type == LAYER_PROTO_TCP)
|
||||
if (layer->proto == LAYER_PROTO_UDP || layer->proto == LAYER_PROTO_TCP)
|
||||
{
|
||||
layer_l4 = layer;
|
||||
continue;
|
||||
}
|
||||
|
||||
// second get L3 layer
|
||||
if (layer->type == LAYER_PROTO_IPV4 || layer->type == LAYER_PROTO_IPV6)
|
||||
if (layer->proto == LAYER_PROTO_IPV4 || layer->proto == LAYER_PROTO_IPV6)
|
||||
{
|
||||
layer_l3 = layer;
|
||||
break;
|
||||
@@ -1361,8 +1362,8 @@ int packet_get_innermost_tuple6(const struct packet *pkt, struct tuple6 *tuple)
|
||||
|
||||
if (layer_l3 && layer_l4 && layer_l4 - layer_l3 == 1)
|
||||
{
|
||||
set_tuple6((const char *)pkt->data_ptr + layer_l3->hdr_offset, layer_l3->type, tuple, domain);
|
||||
set_tuple6((const char *)pkt->data_ptr + layer_l4->hdr_offset, layer_l4->type, tuple, domain);
|
||||
set_tuple6((const char *)pkt->data_ptr + layer_l3->hdr_offset, layer_l3->proto, tuple, domain);
|
||||
set_tuple6((const char *)pkt->data_ptr + layer_l4->hdr_offset, layer_l4->proto, tuple, domain);
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
@@ -1379,21 +1380,21 @@ int packet_get_outermost_tuple6(const struct packet *pkt, struct tuple6 *tuple)
|
||||
const struct raw_layer *layer_l3 = NULL;
|
||||
const struct raw_layer *layer_l4 = NULL;
|
||||
const struct raw_layer *layer = NULL;
|
||||
uint64_t domain = packet_get_domain(pkt);
|
||||
uint64_t domain = packet_get_domain_id(pkt);
|
||||
|
||||
for (int8_t i = 0; i < pkt->layers_used; i++)
|
||||
{
|
||||
layer = &pkt->layers[i];
|
||||
|
||||
// first get L3 layer
|
||||
if (layer->type == LAYER_PROTO_IPV4 || layer->type == LAYER_PROTO_IPV6)
|
||||
if (layer->proto == LAYER_PROTO_IPV4 || layer->proto == LAYER_PROTO_IPV6)
|
||||
{
|
||||
layer_l3 = layer;
|
||||
continue;
|
||||
}
|
||||
|
||||
// second get L4 layer
|
||||
if (layer->type == LAYER_PROTO_UDP || layer->type == LAYER_PROTO_TCP)
|
||||
if (layer->proto == LAYER_PROTO_UDP || layer->proto == LAYER_PROTO_TCP)
|
||||
{
|
||||
layer_l4 = layer;
|
||||
break;
|
||||
@@ -1402,8 +1403,8 @@ int packet_get_outermost_tuple6(const struct packet *pkt, struct tuple6 *tuple)
|
||||
|
||||
if (layer_l3 && layer_l4 && layer_l4 - layer_l3 == 1)
|
||||
{
|
||||
set_tuple6((const char *)pkt->data_ptr + layer_l3->hdr_offset, layer_l3->type, tuple, domain);
|
||||
set_tuple6((const char *)pkt->data_ptr + layer_l4->hdr_offset, layer_l4->type, tuple, domain);
|
||||
set_tuple6((const char *)pkt->data_ptr + layer_l3->hdr_offset, layer_l3->proto, tuple, domain);
|
||||
set_tuple6((const char *)pkt->data_ptr + layer_l4->hdr_offset, layer_l4->proto, tuple, domain);
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
@@ -1412,14 +1413,56 @@ int packet_get_outermost_tuple6(const struct packet *pkt, struct tuple6 *tuple)
|
||||
}
|
||||
}
|
||||
|
||||
const struct raw_layer *packet_get_innermost_raw_layer(const struct packet *pkt, enum layer_proto type)
|
||||
const char *packet_get_raw_data(const struct packet *pkt)
|
||||
{
|
||||
return pkt->data_ptr;
|
||||
}
|
||||
|
||||
uint16_t packet_get_raw_len(const struct packet *pkt)
|
||||
{
|
||||
return pkt->data_len;
|
||||
}
|
||||
|
||||
const char *packet_get_payload(const struct packet *pkt)
|
||||
{
|
||||
if (pkt->layers_used == 0)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
return pkt->layers[pkt->layers_used - 1].pld_ptr;
|
||||
}
|
||||
|
||||
uint16_t packet_get_payload_len(const struct packet *pkt)
|
||||
{
|
||||
if (pkt->layers_used == 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
return pkt->layers[pkt->layers_used - 1].pld_len;
|
||||
}
|
||||
|
||||
int packet_get_layer_count(const struct packet *pkt)
|
||||
{
|
||||
return pkt->layers_used;
|
||||
}
|
||||
|
||||
const struct raw_layer *packet_get_raw_layer(const struct packet *pkt, int idx)
|
||||
{
|
||||
if (idx < 0 || idx >= pkt->layers_used)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
return &pkt->layers[idx];
|
||||
}
|
||||
|
||||
const struct raw_layer *packet_get_innermost_raw_layer(const struct packet *pkt, enum layer_proto proto)
|
||||
{
|
||||
const struct raw_layer *layer = NULL;
|
||||
|
||||
for (int8_t i = pkt->layers_used - 1; i >= 0; i--)
|
||||
{
|
||||
layer = &pkt->layers[i];
|
||||
if (layer->type == type)
|
||||
if (layer->proto == proto)
|
||||
{
|
||||
return layer;
|
||||
}
|
||||
@@ -1428,14 +1471,14 @@ const struct raw_layer *packet_get_innermost_raw_layer(const struct packet *pkt,
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const struct raw_layer *packet_get_outermost_raw_layer(const struct packet *pkt, enum layer_proto type)
|
||||
const struct raw_layer *packet_get_outermost_raw_layer(const struct packet *pkt, enum layer_proto proto)
|
||||
{
|
||||
const struct raw_layer *layer = NULL;
|
||||
|
||||
for (int8_t i = 0; i < pkt->layers_used; i++)
|
||||
{
|
||||
layer = &pkt->layers[i];
|
||||
if (layer->type == type)
|
||||
if (layer->proto == proto)
|
||||
{
|
||||
return layer;
|
||||
}
|
||||
@@ -1444,6 +1487,7 @@ const struct raw_layer *packet_get_outermost_raw_layer(const struct packet *pkt,
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// TODO
|
||||
// direction 1: E2I
|
||||
// direction 0: I2E
|
||||
uint64_t packet_get_hash(const struct packet *pkt, enum ldbc_method method, int direction)
|
||||
|
||||
23
src/packet/packet_layer.cpp
Normal file
23
src/packet/packet_layer.cpp
Normal file
@@ -0,0 +1,23 @@
|
||||
#include "packet_priv.h"
|
||||
|
||||
int packet_get_layer(const struct packet *pkt, int idx, struct layer *out)
|
||||
{
|
||||
if (pkt == NULL || out == NULL)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (idx < 0 || idx >= pkt->layers_used)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
const struct raw_layer *raw = &pkt->layers[idx];
|
||||
out->proto = raw->proto;
|
||||
out->header_len = raw->hdr_len;
|
||||
out->payload_len = raw->pld_len;
|
||||
out->header.raw = raw->hdr_ptr;
|
||||
out->payload = raw->pld_ptr;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -6,18 +6,15 @@ extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "log.h"
|
||||
#include "tuple.h"
|
||||
#include "stellar/layer.h"
|
||||
#include "stellar/packet.h"
|
||||
|
||||
#define PACKET_MAX_LAYERS 32
|
||||
#define PACKET_LOG_ERROR(format, ...) LOG_ERROR("packet", format, ##__VA_ARGS__)
|
||||
#define PACKET_LOG_WARN(format, ...) LOG_WARN("packet", format, ##__VA_ARGS__)
|
||||
#define PACKET_LOG_DEBUG(format, ...) void(0)
|
||||
// #define PACKET_LOG_DEBUG(format, ...) LOG_DEBUG("packet", format, ##__VA_ARGS__)
|
||||
|
||||
enum ldbc_method
|
||||
{
|
||||
@@ -28,12 +25,36 @@ enum ldbc_method
|
||||
LDBC_METHOD_HASH_INNERMOST_EXT_IP = 5,
|
||||
};
|
||||
|
||||
enum packet_origin
|
||||
#define MAX_ROUTE_CTX 64
|
||||
struct route_ctx
|
||||
{
|
||||
PACKET_ORIGIN_MARSIO = 0x1, // packet data in mbuff (eg: packet I/O mrzcpd mode)
|
||||
PACKET_ORIGIN_DUMPFILE = 0x2, // packet data in pcap (eg: packet I/O dumpfile mode)
|
||||
PACKET_ORIGIN_USERSTACK = 0x3, // packet data in user stack (eg: inject packet)
|
||||
PACKET_ORIGIN_USERHEAP = 0x4, // packet data in user heap (eg: ip reassembly)
|
||||
char data[MAX_ROUTE_CTX];
|
||||
int used;
|
||||
};
|
||||
|
||||
struct metadata
|
||||
{
|
||||
struct route_ctx route_ctx;
|
||||
struct sids sids;
|
||||
|
||||
uint64_t session_id;
|
||||
uint64_t domain_id;
|
||||
uint16_t link_id;
|
||||
int is_ctrl;
|
||||
|
||||
enum packet_direction direction;
|
||||
enum packet_action action;
|
||||
const void *origin_ctx;
|
||||
};
|
||||
|
||||
struct raw_layer
|
||||
{
|
||||
enum layer_proto proto;
|
||||
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
|
||||
@@ -42,24 +63,19 @@ struct packet
|
||||
struct raw_layer *frag_layer; // fragment layer
|
||||
int8_t layers_used;
|
||||
int8_t layers_size;
|
||||
int8_t need_free;
|
||||
|
||||
const char *data_ptr;
|
||||
uint16_t data_len;
|
||||
uint16_t trim_len; // trim eth padding
|
||||
|
||||
void *origin_ctx; // mbuff or pcap pointer
|
||||
enum packet_action action;
|
||||
enum packet_origin origin;
|
||||
struct metadata meta;
|
||||
};
|
||||
|
||||
// return innermost payload
|
||||
const char *packet_parse(struct packet *pkt, const char *data, uint16_t len);
|
||||
void packet_print_str(const struct packet *pkt);
|
||||
|
||||
// direction 1: E2I
|
||||
// direction 0: I2E
|
||||
uint64_t packet_get_hash(const struct packet *pkt, enum ldbc_method method, int direction);
|
||||
|
||||
// return 0: found
|
||||
// return -1: not found
|
||||
int packet_get_innermost_tuple2(const struct packet *pkt, struct tuple2 *tuple);
|
||||
@@ -75,46 +91,14 @@ int packet_get_outermost_tuple4(const struct packet *pkt, struct tuple4 *tuple);
|
||||
int packet_get_innermost_tuple6(const struct packet *pkt, struct tuple6 *tuple);
|
||||
int packet_get_outermost_tuple6(const struct packet *pkt, struct tuple6 *tuple);
|
||||
|
||||
int packet_get_layer_count(const struct packet *pkt);
|
||||
const struct raw_layer *packet_get_raw_layer(const struct packet *pkt, int idx);
|
||||
const struct raw_layer *packet_get_innermost_raw_layer(const struct packet *pkt, enum layer_proto type);
|
||||
const struct raw_layer *packet_get_outermost_raw_layer(const struct packet *pkt, enum layer_proto type);
|
||||
|
||||
/******************************************************************************
|
||||
* Utils
|
||||
******************************************************************************/
|
||||
|
||||
#define MAX_ROUTE_CTX 64
|
||||
struct route_ctx
|
||||
{
|
||||
char data[MAX_ROUTE_CTX];
|
||||
int used;
|
||||
};
|
||||
void packet_set_route_ctx(struct packet *pkt, const struct route_ctx *ctx);
|
||||
void packet_get_route_ctx(const struct packet *pkt, struct route_ctx *ctx);
|
||||
|
||||
void packet_set_origin(struct packet *pkt, enum packet_origin origin);
|
||||
enum packet_origin packet_get_origin(const struct packet *pkt);
|
||||
|
||||
void packet_set_origin_ctx(struct packet *pkt, void *ctx);
|
||||
void *packet_get_origin_ctx(const struct packet *pkt);
|
||||
|
||||
void packet_set_domain(struct packet *pkt, uint64_t domain);
|
||||
uint64_t packet_get_domain(const struct packet *pkt);
|
||||
|
||||
void packet_set_ctrl(struct packet *pkt);
|
||||
int packet_is_ctrl(const struct packet *pkt);
|
||||
|
||||
void packet_set_sid_list(struct packet *pkt, const struct sid_list *list);
|
||||
void packet_get_sid_list(const struct packet *pkt, struct sid_list *list);
|
||||
void packet_append_sid_list(struct packet *pkt, const struct sid_list *list);
|
||||
|
||||
struct packet *packet_new(uint16_t pkt_len);
|
||||
void packet_free(struct packet *pkt);
|
||||
struct packet *packet_dup(const struct packet *pkt);
|
||||
|
||||
void packet_set_session_id(struct packet *pkt, uint64_t sess_id);
|
||||
void packet_set_direction(struct packet *pkt, enum packet_direction dir);
|
||||
|
||||
int packet_is_fragment(const struct packet *pkt);
|
||||
// direction 1: E2I
|
||||
// direction 0: I2E
|
||||
uint64_t packet_get_hash(const struct packet *pkt, enum ldbc_method method, int direction);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
@@ -1,90 +1,127 @@
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "udp_utils.h"
|
||||
#include "tcp_utils.h"
|
||||
#include "ipv4_utils.h"
|
||||
#include "ipv6_utils.h"
|
||||
#include "packet_priv.h"
|
||||
#include "marsio.h"
|
||||
#include "packet_utils.h"
|
||||
|
||||
/******************************************************************************
|
||||
* set and get from struct packet
|
||||
* metadata utils
|
||||
******************************************************************************/
|
||||
|
||||
void packet_set_origin(struct packet *pkt, enum packet_origin origin)
|
||||
void packet_set_route_ctx(struct packet *pkt, const struct route_ctx *ctx)
|
||||
{
|
||||
pkt->origin = origin;
|
||||
pkt->meta.route_ctx = *ctx;
|
||||
}
|
||||
|
||||
enum packet_origin packet_get_origin(const struct packet *pkt)
|
||||
const struct route_ctx *packet_get_route_ctx(const struct packet *pkt)
|
||||
{
|
||||
return pkt->origin;
|
||||
return &pkt->meta.route_ctx;
|
||||
}
|
||||
|
||||
int packet_get_layer_count(const struct packet *pkt)
|
||||
void packet_set_sids(struct packet *pkt, const struct sids *sids)
|
||||
{
|
||||
return pkt->layers_used;
|
||||
pkt->meta.sids = *sids;
|
||||
}
|
||||
|
||||
const struct raw_layer *packet_get_raw_layer(const struct packet *pkt, int idx)
|
||||
const struct sids *packet_get_sids(const struct packet *pkt)
|
||||
{
|
||||
if (idx < 0 || idx >= pkt->layers_used)
|
||||
return &pkt->meta.sids;
|
||||
}
|
||||
|
||||
void packet_prepend_sids(struct packet *pkt, const struct sids *sids)
|
||||
{
|
||||
if (pkt->meta.sids.used + sids->used > MAX_SIDS)
|
||||
{
|
||||
return NULL;
|
||||
PACKET_LOG_ERROR("sids overflow");
|
||||
return;
|
||||
}
|
||||
return &pkt->layers[idx];
|
||||
}
|
||||
|
||||
const char *packet_get_data(const struct packet *pkt)
|
||||
{
|
||||
return pkt->data_ptr;
|
||||
}
|
||||
|
||||
uint16_t packet_get_len(const struct packet *pkt)
|
||||
{
|
||||
return pkt->data_len;
|
||||
}
|
||||
|
||||
const char *packet_get_payload(const struct packet *pkt)
|
||||
{
|
||||
if (pkt->layers_used == 0)
|
||||
else
|
||||
{
|
||||
return NULL;
|
||||
for (int i = pkt->meta.sids.used - 1; i >= 0; i--)
|
||||
{
|
||||
pkt->meta.sids.sid[i + sids->used] = pkt->meta.sids.sid[i];
|
||||
}
|
||||
for (int i = 0; i < sids->used; i++)
|
||||
{
|
||||
pkt->meta.sids.sid[i] = sids->sid[i];
|
||||
}
|
||||
pkt->meta.sids.used += sids->used;
|
||||
}
|
||||
return pkt->layers[pkt->layers_used - 1].pld_ptr;
|
||||
}
|
||||
|
||||
uint16_t packet_get_payload_len(const struct packet *pkt)
|
||||
void packet_set_session_id(struct packet *pkt, uint64_t id)
|
||||
{
|
||||
if (pkt->layers_used == 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
return pkt->layers[pkt->layers_used - 1].pld_len;
|
||||
pkt->meta.session_id = id;
|
||||
}
|
||||
|
||||
uint64_t packet_get_session_id(const struct packet *pkt)
|
||||
{
|
||||
return pkt->meta.session_id;
|
||||
}
|
||||
|
||||
void packet_set_domain_id(struct packet *pkt, uint64_t id)
|
||||
{
|
||||
pkt->meta.domain_id = id;
|
||||
}
|
||||
|
||||
uint64_t packet_get_domain_id(const struct packet *pkt)
|
||||
{
|
||||
return pkt->meta.domain_id;
|
||||
}
|
||||
|
||||
void packet_set_link_id(struct packet *pkt, uint16_t id)
|
||||
{
|
||||
pkt->meta.link_id = id;
|
||||
}
|
||||
|
||||
uint16_t packet_get_link_id(const struct packet *pkt)
|
||||
{
|
||||
return pkt->meta.link_id;
|
||||
}
|
||||
|
||||
void packet_set_ctrl(struct packet *pkt, uint8_t ctrl)
|
||||
{
|
||||
pkt->meta.is_ctrl = ctrl;
|
||||
}
|
||||
|
||||
uint8_t packet_is_ctrl(const struct packet *pkt)
|
||||
{
|
||||
return pkt->meta.is_ctrl;
|
||||
}
|
||||
|
||||
void packet_set_direction(struct packet *pkt, enum packet_direction dir)
|
||||
{
|
||||
pkt->meta.direction = dir;
|
||||
}
|
||||
|
||||
enum packet_direction packet_get_direction(const struct packet *pkt)
|
||||
{
|
||||
return pkt->meta.direction;
|
||||
}
|
||||
|
||||
void packet_set_action(struct packet *pkt, enum packet_action action)
|
||||
{
|
||||
pkt->action = action;
|
||||
pkt->meta.action = action;
|
||||
}
|
||||
|
||||
enum packet_action packet_get_action(const struct packet *pkt)
|
||||
{
|
||||
return pkt->action;
|
||||
return pkt->meta.action;
|
||||
}
|
||||
|
||||
void packet_set_origin_ctx(struct packet *pkt, void *ctx)
|
||||
{
|
||||
pkt->origin_ctx = ctx;
|
||||
pkt->meta.origin_ctx = ctx;
|
||||
}
|
||||
|
||||
void *packet_get_origin_ctx(const struct packet *pkt)
|
||||
const void *packet_get_origin_ctx(const struct packet *pkt)
|
||||
{
|
||||
return pkt->origin_ctx;
|
||||
return pkt->meta.origin_ctx;
|
||||
}
|
||||
|
||||
/******************************************************************************
|
||||
* other uitls
|
||||
******************************************************************************/
|
||||
|
||||
int packet_is_fragment(const struct packet *pkt)
|
||||
{
|
||||
return (pkt->frag_layer) ? 1 : 0;
|
||||
@@ -99,19 +136,11 @@ struct packet *packet_new(uint16_t pkt_len)
|
||||
}
|
||||
pkt->data_len = pkt_len;
|
||||
pkt->data_ptr = (const char *)pkt + sizeof(struct packet);
|
||||
pkt->origin = PACKET_ORIGIN_USERHEAP;
|
||||
pkt->need_free = 1;
|
||||
|
||||
return pkt;
|
||||
}
|
||||
|
||||
void packet_free(struct packet *pkt)
|
||||
{
|
||||
if (pkt && pkt->origin == PACKET_ORIGIN_USERHEAP)
|
||||
{
|
||||
free((void *)pkt);
|
||||
}
|
||||
}
|
||||
|
||||
struct packet *packet_dup(const struct packet *pkt)
|
||||
{
|
||||
if (pkt == NULL)
|
||||
@@ -130,14 +159,11 @@ struct packet *packet_dup(const struct packet *pkt)
|
||||
|
||||
memcpy((char *)dup_pkt->data_ptr, pkt->data_ptr, pkt->data_len);
|
||||
dup_pkt->data_len = pkt->data_len;
|
||||
|
||||
dup_pkt->origin_ctx = NULL;
|
||||
dup_pkt->origin = PACKET_ORIGIN_USERHEAP;
|
||||
dup_pkt->action = PACKET_ACTION_DROP;
|
||||
packet_set_action(dup_pkt, PACKET_ACTION_DROP);
|
||||
|
||||
for (int8_t i = 0; i < pkt->layers_used; i++)
|
||||
{
|
||||
dup_pkt->layers[i].type = pkt->layers[i].type;
|
||||
dup_pkt->layers[i].proto = pkt->layers[i].proto;
|
||||
dup_pkt->layers[i].hdr_ptr = dup_pkt->data_ptr + pkt->layers[i].hdr_offset;
|
||||
dup_pkt->layers[i].pld_ptr = dup_pkt->data_ptr + pkt->layers[i].hdr_offset + pkt->layers[i].hdr_len;
|
||||
dup_pkt->layers[i].hdr_offset = pkt->layers[i].hdr_offset;
|
||||
@@ -154,254 +180,10 @@ struct packet *packet_dup(const struct packet *pkt)
|
||||
return dup_pkt;
|
||||
}
|
||||
|
||||
/******************************************************************************
|
||||
* set and get from mbuff
|
||||
******************************************************************************/
|
||||
|
||||
void packet_set_ctrl(struct packet *pkt)
|
||||
void packet_free(struct packet *pkt)
|
||||
{
|
||||
if (packet_get_origin(pkt) == PACKET_ORIGIN_MARSIO)
|
||||
if (pkt && pkt->need_free)
|
||||
{
|
||||
marsio_buff_t *mbuff = (marsio_buff_t *)packet_get_origin_ctx(pkt);
|
||||
assert(mbuff != NULL);
|
||||
marsio_buff_set_ctrlbuf(mbuff);
|
||||
}
|
||||
else
|
||||
{
|
||||
PACKET_LOG_WARN("packet origin is not marsio, failed to set ctrl");
|
||||
}
|
||||
}
|
||||
|
||||
int packet_is_ctrl(const struct packet *pkt)
|
||||
{
|
||||
if (packet_get_origin(pkt) == PACKET_ORIGIN_MARSIO)
|
||||
{
|
||||
marsio_buff_t *mbuff = (marsio_buff_t *)packet_get_origin_ctx(pkt);
|
||||
assert(mbuff != NULL);
|
||||
return marsio_buff_is_ctrlbuf(mbuff);
|
||||
}
|
||||
else
|
||||
{
|
||||
PACKET_LOG_WARN("packet origin is not marsio, failed to check ctrl");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
void packet_set_direction(struct packet *pkt, enum packet_direction dir)
|
||||
{
|
||||
if (packet_get_origin(pkt) == PACKET_ORIGIN_MARSIO)
|
||||
{
|
||||
marsio_buff_t *mbuff = (marsio_buff_t *)packet_get_origin_ctx(pkt);
|
||||
assert(mbuff != NULL);
|
||||
if (marsio_buff_set_metadata(mbuff, MR_BUFF_DIR, &dir, sizeof(dir)) != 0)
|
||||
{
|
||||
PACKET_LOG_ERROR("failed to set direction");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
PACKET_LOG_WARN("packet origin is not marsio, failed to set direction");
|
||||
}
|
||||
}
|
||||
|
||||
enum packet_direction packet_get_direction(const struct packet *pkt)
|
||||
{
|
||||
enum packet_direction dir = PACKET_DIRECTION_INCOMING;
|
||||
if (packet_get_origin(pkt) == PACKET_ORIGIN_MARSIO)
|
||||
{
|
||||
marsio_buff_t *mbuff = (marsio_buff_t *)packet_get_origin_ctx(pkt);
|
||||
assert(mbuff != NULL);
|
||||
if (marsio_buff_get_metadata(mbuff, MR_BUFF_DIR, &dir, sizeof(dir)) <= 0)
|
||||
{
|
||||
PACKET_LOG_ERROR("failed to get direction");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
PACKET_LOG_WARN("packet origin is not marsio, failed to get direction");
|
||||
}
|
||||
|
||||
return dir;
|
||||
}
|
||||
|
||||
void packet_set_session_id(struct packet *pkt, uint64_t sess_id)
|
||||
{
|
||||
if (packet_get_origin(pkt) == PACKET_ORIGIN_MARSIO)
|
||||
{
|
||||
marsio_buff_t *mbuff = (marsio_buff_t *)packet_get_origin_ctx(pkt);
|
||||
assert(mbuff != NULL);
|
||||
if (marsio_buff_set_metadata(mbuff, MR_BUFF_SESSION_ID, &sess_id, sizeof(sess_id)) != 0)
|
||||
{
|
||||
PACKET_LOG_ERROR("failed to set session id");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
PACKET_LOG_WARN("packet origin is not marsio, failed to set session id");
|
||||
}
|
||||
}
|
||||
|
||||
uint64_t packet_get_session_id(const struct packet *pkt)
|
||||
{
|
||||
uint64_t sess_id = 0;
|
||||
if (packet_get_origin(pkt) == PACKET_ORIGIN_MARSIO)
|
||||
{
|
||||
marsio_buff_t *mbuff = (marsio_buff_t *)packet_get_origin_ctx(pkt);
|
||||
assert(mbuff != NULL);
|
||||
if (marsio_buff_get_metadata(mbuff, MR_BUFF_SESSION_ID, &sess_id, sizeof(sess_id)) <= 0)
|
||||
{
|
||||
PACKET_LOG_ERROR("failed to get session id");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
PACKET_LOG_WARN("packet origin is not marsio, failed to get session id");
|
||||
}
|
||||
|
||||
return sess_id;
|
||||
}
|
||||
|
||||
void packet_set_domain(struct packet *pkt, uint64_t domain)
|
||||
{
|
||||
if (packet_get_origin(pkt) == PACKET_ORIGIN_MARSIO)
|
||||
{
|
||||
marsio_buff_t *mbuff = (marsio_buff_t *)packet_get_origin_ctx(pkt);
|
||||
assert(mbuff != NULL);
|
||||
// TODO
|
||||
#if 0
|
||||
if (marsio_buff_set_metadata(mbuff, MR_BUFF_DOMAIN, &domain, sizeof(domain)) != 0)
|
||||
{
|
||||
PACKET_LOG_ERROR("failed to set domain");
|
||||
}
|
||||
#endif
|
||||
}
|
||||
else
|
||||
{
|
||||
PACKET_LOG_WARN("packet origin is not marsio, failed to set domain");
|
||||
}
|
||||
}
|
||||
|
||||
uint64_t packet_get_domain(const struct packet *pkt)
|
||||
{
|
||||
uint64_t domain = 0;
|
||||
if (packet_get_origin(pkt) == PACKET_ORIGIN_MARSIO)
|
||||
{
|
||||
marsio_buff_t *mbuff = (marsio_buff_t *)packet_get_origin_ctx(pkt);
|
||||
assert(mbuff != NULL);
|
||||
// TODO
|
||||
#if 0
|
||||
if (marsio_buff_get_metadata(mbuff, MR_BUFF_DOMAIN, &domain, sizeof(domain)) <= 0)
|
||||
{
|
||||
PACKET_LOG_ERROR("failed to get domain");
|
||||
}
|
||||
#endif
|
||||
}
|
||||
else
|
||||
{
|
||||
PACKET_LOG_WARN("packet origin is not marsio, failed to get domain");
|
||||
}
|
||||
|
||||
return domain;
|
||||
}
|
||||
|
||||
void packet_set_route_ctx(struct packet *pkt, const struct route_ctx *ctx)
|
||||
{
|
||||
if (packet_get_origin(pkt) == PACKET_ORIGIN_MARSIO)
|
||||
{
|
||||
marsio_buff_t *mbuff = (marsio_buff_t *)packet_get_origin_ctx(pkt);
|
||||
assert(mbuff != NULL);
|
||||
if (marsio_buff_set_metadata(mbuff, MR_BUFF_ROUTE_CTX, (void *)ctx->data, ctx->used) != 0)
|
||||
{
|
||||
PACKET_LOG_ERROR("failed to set route ctx");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
PACKET_LOG_WARN("packet origin is not marsio, failed to set route ctx");
|
||||
}
|
||||
}
|
||||
|
||||
void packet_get_route_ctx(const struct packet *pkt, struct route_ctx *ctx)
|
||||
{
|
||||
ctx->used = 0;
|
||||
if (packet_get_origin(pkt) == PACKET_ORIGIN_MARSIO)
|
||||
{
|
||||
marsio_buff_t *mbuff = (marsio_buff_t *)packet_get_origin_ctx(pkt);
|
||||
assert(mbuff != NULL);
|
||||
ctx->used = marsio_buff_get_metadata(mbuff, MR_BUFF_ROUTE_CTX, ctx->data, sizeof(ctx->data));
|
||||
if (ctx->used <= 0)
|
||||
{
|
||||
PACKET_LOG_ERROR("failed to get route ctx");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
PACKET_LOG_WARN("packet origin is not marsio, failed to get route ctx");
|
||||
}
|
||||
}
|
||||
|
||||
void packet_set_sid_list(struct packet *pkt, const struct sid_list *list)
|
||||
{
|
||||
if (packet_get_origin(pkt) == PACKET_ORIGIN_MARSIO)
|
||||
{
|
||||
marsio_buff_t *mbuff = (marsio_buff_t *)packet_get_origin_ctx(pkt);
|
||||
assert(mbuff != NULL);
|
||||
if (marsio_buff_set_sid_list(mbuff, (sid_t *)list->sid, list->used) != 0)
|
||||
{
|
||||
PACKET_LOG_ERROR("failed to set sid list");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
PACKET_LOG_WARN("packet origin is not marsio, failed to set sid list");
|
||||
}
|
||||
}
|
||||
|
||||
void packet_get_sid_list(const struct packet *pkt, struct sid_list *list)
|
||||
{
|
||||
list->used = 0;
|
||||
if (packet_get_origin(pkt) == PACKET_ORIGIN_MARSIO)
|
||||
{
|
||||
marsio_buff_t *mbuff = (marsio_buff_t *)packet_get_origin_ctx(pkt);
|
||||
assert(mbuff != NULL);
|
||||
list->used = marsio_buff_get_sid_list(mbuff, (sid_t *)list->sid, sizeof(list->sid) / sizeof(list->sid[0]));
|
||||
}
|
||||
else
|
||||
{
|
||||
PACKET_LOG_WARN("packet origin is not marsio, failed to get sid list");
|
||||
}
|
||||
}
|
||||
|
||||
void packet_prepend_sid_list(struct packet *pkt, const struct sid_list *list)
|
||||
{
|
||||
if (packet_get_origin(pkt) == PACKET_ORIGIN_MARSIO)
|
||||
{
|
||||
marsio_buff_t *mbuff = (marsio_buff_t *)packet_get_origin_ctx(pkt);
|
||||
assert(mbuff != NULL);
|
||||
if (marsio_buff_prepend_sid_list(mbuff, (sid_t *)list->sid, list->used) != 0)
|
||||
{
|
||||
PACKET_LOG_ERROR("failed to prepend sid list");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
PACKET_LOG_WARN("packet origin is not marsio, failed to prepend sid list");
|
||||
}
|
||||
}
|
||||
|
||||
void packet_append_sid_list(struct packet *pkt, const struct sid_list *list)
|
||||
{
|
||||
if (packet_get_origin(pkt) == PACKET_ORIGIN_MARSIO)
|
||||
{
|
||||
marsio_buff_t *mbuff = (marsio_buff_t *)packet_get_origin_ctx(pkt);
|
||||
assert(mbuff != NULL);
|
||||
if (marsio_buff_append_sid_list(mbuff, (sid_t *)list->sid, list->used) != 0)
|
||||
{
|
||||
PACKET_LOG_ERROR("failed to append sid list");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
PACKET_LOG_WARN("packet origin is not marsio, failed to append sid list");
|
||||
free((void *)pkt);
|
||||
}
|
||||
}
|
||||
57
src/packet/packet_utils.h
Normal file
57
src/packet/packet_utils.h
Normal file
@@ -0,0 +1,57 @@
|
||||
#ifndef _PACKET_UTILS_H
|
||||
#define _PACKET_UTILS_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
#include <stdint.h>
|
||||
#include "packet_priv.h"
|
||||
|
||||
/******************************************************************************
|
||||
* metadata utils
|
||||
******************************************************************************/
|
||||
|
||||
void packet_set_route_ctx(struct packet *pkt, const struct route_ctx *ctx);
|
||||
const struct route_ctx *packet_get_route_ctx(const struct packet *pkt);
|
||||
|
||||
void packet_set_sids(struct packet *pkt, const struct sids *sids);
|
||||
const struct sids *packet_get_sids(const struct packet *pkt);
|
||||
void packet_prepend_sids(struct packet *pkt, const struct sids *sids);
|
||||
|
||||
void packet_set_session_id(struct packet *pkt, uint64_t id);
|
||||
uint64_t packet_get_session_id(const struct packet *pkt);
|
||||
|
||||
void packet_set_domain_id(struct packet *pkt, uint64_t id);
|
||||
uint64_t packet_get_domain_id(const struct packet *pkt);
|
||||
|
||||
void packet_set_link_id(struct packet *pkt, uint16_t id);
|
||||
uint16_t packet_get_link_id(const struct packet *pkt);
|
||||
|
||||
void packet_set_ctrl(struct packet *pkt, uint8_t ctrl);
|
||||
uint8_t packet_is_ctrl(const struct packet *pkt);
|
||||
|
||||
void packet_set_direction(struct packet *pkt, enum packet_direction dir);
|
||||
enum packet_direction packet_get_direction(const struct packet *pkt);
|
||||
|
||||
void packet_set_action(struct packet *pkt, enum packet_action action);
|
||||
enum packet_action packet_get_action(const struct packet *pkt);
|
||||
|
||||
void packet_set_origin_ctx(struct packet *pkt, void *ctx);
|
||||
const void *packet_get_origin_ctx(const struct packet *pkt);
|
||||
|
||||
/******************************************************************************
|
||||
* other uitls
|
||||
******************************************************************************/
|
||||
|
||||
int packet_is_fragment(const struct packet *pkt);
|
||||
struct packet *packet_new(uint16_t pkt_len);
|
||||
struct packet *packet_dup(const struct packet *pkt);
|
||||
void packet_free(struct packet *pkt);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -79,6 +79,7 @@ TEST(PACKET, ETH_VLAN_VLAN_IP4_IP4_UDP)
|
||||
char buffer[256];
|
||||
struct packet handler;
|
||||
|
||||
memset(&handler, 0, sizeof(handler));
|
||||
const char *payload = packet_parse(&handler, (const char *)data1, sizeof(data1));
|
||||
EXPECT_TRUE(payload != nullptr);
|
||||
EXPECT_TRUE((char *)payload - (char *)&data1 == 70);
|
||||
@@ -282,6 +283,7 @@ TEST(PACKET, ETH_IP6_IP4_TCP_SSH)
|
||||
char buffer[256];
|
||||
struct packet handler;
|
||||
|
||||
memset(&handler, 0, sizeof(handler));
|
||||
const char *payload = packet_parse(&handler, (const char *)data2, sizeof(data2));
|
||||
EXPECT_TRUE(payload != nullptr);
|
||||
EXPECT_TRUE((char *)payload - (char *)&data2 == 94);
|
||||
@@ -474,6 +476,7 @@ TEST(PACKET, ETH_VLAN_IP6_IP4_GRE_PPP_IP4_UDP_DNS)
|
||||
char buffer[256];
|
||||
struct packet handler;
|
||||
|
||||
memset(&handler, 0, sizeof(handler));
|
||||
const char *payload = packet_parse(&handler, (const char *)data3, sizeof(data3));
|
||||
EXPECT_TRUE(payload != nullptr);
|
||||
EXPECT_TRUE((char *)payload - (char *)&data3 == 126);
|
||||
@@ -680,6 +683,7 @@ TEST(PACKET, ETH_IP4_IP6_TCP)
|
||||
char buffer[256];
|
||||
struct packet handler;
|
||||
|
||||
memset(&handler, 0, sizeof(handler));
|
||||
const char *payload = packet_parse(&handler, (const char *)data4, sizeof(data4));
|
||||
EXPECT_TRUE(payload != nullptr);
|
||||
EXPECT_TRUE((char *)payload - (char *)&data4 == 106);
|
||||
@@ -834,6 +838,7 @@ TEST(PACKET, ETH_IP6_IP6_UDP)
|
||||
char buffer[256];
|
||||
struct packet handler;
|
||||
|
||||
memset(&handler, 0, sizeof(handler));
|
||||
const char *payload = packet_parse(&handler, (const char *)data5, sizeof(data5));
|
||||
EXPECT_TRUE(payload != nullptr);
|
||||
EXPECT_TRUE((char *)payload - (char *)&data5 == 102);
|
||||
@@ -989,6 +994,7 @@ TEST(PACKET, ETH_MPLS_IP4_TCP)
|
||||
char buffer[256];
|
||||
struct packet handler;
|
||||
|
||||
memset(&handler, 0, sizeof(handler));
|
||||
const char *payload = packet_parse(&handler, (const char *)data6, sizeof(data6));
|
||||
EXPECT_TRUE(payload != nullptr);
|
||||
EXPECT_TRUE((char *)payload - (char *)&data6 == 70);
|
||||
@@ -1155,6 +1161,7 @@ TEST(PACKET, ETH_MPLS_MPLS_IP4_TCP)
|
||||
char buffer[256];
|
||||
struct packet handler;
|
||||
|
||||
memset(&handler, 0, sizeof(handler));
|
||||
const char *payload = packet_parse(&handler, (const char *)data7, sizeof(data7));
|
||||
EXPECT_TRUE(payload != nullptr);
|
||||
EXPECT_TRUE((char *)payload - (char *)&data7 == 66);
|
||||
@@ -1331,6 +1338,7 @@ TEST(PACKET, ETH_VLAN_PPPOE_IP4_TCP)
|
||||
char buffer[256];
|
||||
struct packet handler;
|
||||
|
||||
memset(&handler, 0, sizeof(handler));
|
||||
const char *payload = packet_parse(&handler, (const char *)data8, sizeof(data8));
|
||||
EXPECT_TRUE(payload != nullptr);
|
||||
EXPECT_TRUE((char *)payload - (char *)&data8 == 78);
|
||||
@@ -1583,6 +1591,7 @@ TEST(PACKET, ETH_IP6_UDP_GTP_IP6_TCP_TLS)
|
||||
char buffer[256];
|
||||
struct packet handler;
|
||||
|
||||
memset(&handler, 0, sizeof(handler));
|
||||
const char *payload = packet_parse(&handler, (const char *)data9, sizeof(data9));
|
||||
EXPECT_TRUE(payload != nullptr);
|
||||
EXPECT_TRUE((char *)payload - (char *)&data9 == 130);
|
||||
@@ -1836,6 +1845,7 @@ TEST(PACKET, ETH_IP6_UDP_GTP_IP4_TCP_TLS)
|
||||
char buffer[256];
|
||||
struct packet handler;
|
||||
|
||||
memset(&handler, 0, sizeof(handler));
|
||||
const char *payload = packet_parse(&handler, (const char *)data10, sizeof(data10));
|
||||
EXPECT_TRUE(payload != nullptr);
|
||||
EXPECT_TRUE((char *)payload - (char *)&data10 == 122);
|
||||
@@ -2037,6 +2047,7 @@ TEST(PACKET, ETH_IP4_UDP_VXLAN_ETH_IP4_UDP_DNS)
|
||||
char buffer[256];
|
||||
struct packet handler;
|
||||
|
||||
memset(&handler, 0, sizeof(handler));
|
||||
const char *payload = packet_parse(&handler, (const char *)data11, sizeof(data11));
|
||||
EXPECT_TRUE(payload != nullptr);
|
||||
EXPECT_TRUE((char *)payload - (char *)&data11 == 92);
|
||||
@@ -2193,6 +2204,7 @@ TEST(PACKET, ETH_MPLS_MPLS_PWETHCW_ETH_ARP)
|
||||
{
|
||||
struct packet handler;
|
||||
|
||||
memset(&handler, 0, sizeof(handler));
|
||||
const char *payload = packet_parse(&handler, (const char *)data12, sizeof(data12));
|
||||
EXPECT_TRUE(payload != nullptr);
|
||||
EXPECT_TRUE((char *)payload - (char *)&data12 == 40);
|
||||
@@ -2329,6 +2341,7 @@ TEST(PACKET, ETH_IP4_ICMP)
|
||||
char buffer[256];
|
||||
struct packet handler;
|
||||
|
||||
memset(&handler, 0, sizeof(handler));
|
||||
const char *payload = packet_parse(&handler, (const char *)data13, sizeof(data13));
|
||||
EXPECT_TRUE(payload != nullptr);
|
||||
EXPECT_TRUE((char *)payload - (char *)&data13 == 14 + 20 + 8);
|
||||
@@ -2434,6 +2447,7 @@ TEST(PACKET, ETH_IP6_ICMP6)
|
||||
char buffer[256];
|
||||
struct packet handler;
|
||||
|
||||
memset(&handler, 0, sizeof(handler));
|
||||
const char *payload = packet_parse(&handler, (const char *)data14, sizeof(data14));
|
||||
EXPECT_TRUE(payload != nullptr);
|
||||
EXPECT_TRUE((char *)payload - (char *)&data14 == 14 + 40 + 8);
|
||||
@@ -2598,6 +2612,7 @@ TEST(PACKET, ETH_IP4_UDP_L2TPV2_PPP_IP4_UDP)
|
||||
char buffer[256];
|
||||
struct packet handler;
|
||||
|
||||
memset(&handler, 0, sizeof(handler));
|
||||
const char *payload = packet_parse(&handler, (const char *)data15, sizeof(data15));
|
||||
EXPECT_TRUE(payload != nullptr);
|
||||
EXPECT_TRUE((char *)payload - (char *)&data15 == 14 + 20 + 8 + 8 + 4 + 20 + 8);
|
||||
@@ -2762,6 +2777,7 @@ TEST(PACKET, ETH_IP4_TCP_PADDING)
|
||||
char buffer[256];
|
||||
struct packet handler;
|
||||
|
||||
memset(&handler, 0, sizeof(handler));
|
||||
const char *payload = packet_parse(&handler, (const char *)data16, sizeof(data16));
|
||||
EXPECT_TRUE(payload != nullptr);
|
||||
EXPECT_TRUE((char *)payload - (char *)&data16 == 14 + 20 + 20);
|
||||
@@ -2826,6 +2842,7 @@ TEST(PACKET, HASH_VALUE)
|
||||
{
|
||||
struct packet handler;
|
||||
|
||||
memset(&handler, 0, sizeof(handler));
|
||||
const char *payload = packet_parse(&handler, (const char *)data4, sizeof(data4));
|
||||
EXPECT_TRUE(payload != nullptr);
|
||||
EXPECT_TRUE((char *)payload - (char *)&data4 == 106);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "packet_priv.h"
|
||||
#include "packet_utils.h"
|
||||
|
||||
/******************************************************************************
|
||||
* [Protocols in frame: eth:ethertype:ip:data]
|
||||
@@ -47,6 +48,7 @@ unsigned char data1[] = {
|
||||
TEST(PACKET_FRAG, IPV4_FRAGMENT)
|
||||
{
|
||||
struct packet handler;
|
||||
memset(&handler, 0, sizeof(handler));
|
||||
packet_parse(&handler, (const char *)data1, sizeof(data1));
|
||||
EXPECT_TRUE(packet_is_fragment(&handler) == true);
|
||||
|
||||
@@ -155,6 +157,7 @@ unsigned char data2[] = {
|
||||
TEST(PACKET_FRAG, IPV6_FRAGMENT)
|
||||
{
|
||||
struct packet handler;
|
||||
memset(&handler, 0, sizeof(handler));
|
||||
packet_parse(&handler, (const char *)data2, sizeof(data2));
|
||||
EXPECT_TRUE(packet_is_fragment(&handler) == true);
|
||||
|
||||
@@ -231,6 +234,7 @@ unsigned char data3[] = {
|
||||
TEST(PACKET_FRAG, IPV4_IPV6_NOT_FRAGMENT)
|
||||
{
|
||||
struct packet handler;
|
||||
memset(&handler, 0, sizeof(handler));
|
||||
packet_parse(&handler, (const char *)data3, sizeof(data3));
|
||||
EXPECT_TRUE(packet_is_fragment(&handler) == false);
|
||||
|
||||
@@ -293,6 +297,7 @@ unsigned char data4[] = {
|
||||
TEST(PACKET_FRAG, IPV6_IPV6_NOT_FRAGMENT)
|
||||
{
|
||||
struct packet handler;
|
||||
memset(&handler, 0, sizeof(handler));
|
||||
packet_parse(&handler, (const char *)data4, sizeof(data4));
|
||||
EXPECT_TRUE(packet_is_fragment(&handler) == false);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user