Update Stellar export header and add stellar_get_current_thread_index() function

This commit is contained in:
luwenpeng
2024-04-22 20:01:15 +08:00
parent 8a41a79f06
commit 74f0504d3d
59 changed files with 134 additions and 87 deletions

155
include/stellar/packet.h Normal file
View File

@@ -0,0 +1,155 @@
#ifndef _PACKET_PUB_H
#define _PACKET_PUB_H
#ifdef __cplusplus
extern "C"
{
#endif
#include "stellar/tuple.h"
enum layer_type
{
// L2 -- data link layer
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),
// L2 -- tunnel
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),
// L3 -- network layer
LAYER_TYPE_IPV4 = 1 << 6,
LAYER_TYPE_IPV6 = 1 << 7,
LAYER_TYPE_L3 = (LAYER_TYPE_IPV4 | LAYER_TYPE_IPV6),
// L3 -- tunnel
LAYER_TYPE_GRE = 1 << 8,
LAYER_TYPE_L3_TUN = (LAYER_TYPE_GRE),
// L4 -- transport layer
LAYER_TYPE_UDP = 1 << 9,
LAYER_TYPE_TCP = 1 << 10,
LAYER_TYPE_ICMP = 1 << 11,
LAYER_TYPE_ICMP6 = 1 << 12,
LAYER_TYPE_L4 = (LAYER_TYPE_UDP | LAYER_TYPE_TCP | LAYER_TYPE_ICMP | LAYER_TYPE_ICMP6),
// L4 -- tunnel
LAYER_TYPE_VXLAN = 1 << 13,
LAYER_TYPE_GTPV1_U = 1 << 14,
// 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),
};
struct packet_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
};
// 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 packet_layer *packet_get_innermost_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);
/******************************************************************************
* Utils
******************************************************************************/
int8_t packet_get_layers(const struct packet *pkt);
const struct packet_layer *packet_get_layer(const struct packet *pkt, int8_t idx);
const char *packet_get_data(const struct packet *pkt);
uint16_t packet_get_len(const struct packet *pkt);
const char *packet_get_payload(const struct packet *pkt);
uint16_t packet_get_payload_len(const struct packet *pkt);
int packet_need_drop(const struct packet *pkt);
void packet_set_drop(struct packet *pkt);
void packet_set_ctrl(struct packet *pkt);
int packet_is_ctrl(const struct packet *pkt);
void packet_set_direction(struct packet *pkt, int dir);
int packet_get_direction(const struct packet *pkt); // 1: E2I, 0: I2E
void packet_set_session_id(struct packet *pkt, uint64_t sess_id);
uint64_t packet_get_session_id(const struct packet *pkt);
void packet_set_sid_list(struct packet *pkt, uint16_t *sid, int num);
int packet_get_sid_list(const struct packet *pkt, uint16_t *sid, int num); // return number of sid
void packet_prepend_sid_list(struct packet *pkt, uint16_t *sid, int num);
void packet_append_sid_list(struct packet *pkt, uint16_t *sid, int num);
/*
******************************************************************************
* Example: getting the innermost TCP layer
******************************************************************************
*
* |<--------------------------- pkt->data_len -------------------------->|
* +----------+------+-----+-------+------+---------------+---------------+
* | Ethernet | IPv4 | UDP | GTP-U | IPv4 | TCP | Payload |
* +----------+------+-----+-------+------+---------------+---------------+
* ^ ^ ^
* | | |
* |<------------ hdr_offset ------------>|<-- hdr_len -->|<-- pld_len -->|
* | | |
* | | +-- pld_ptr
* | +-- hdr_ptr
* +-- data_ptr
*
* const struct packet_layer *tcp_layer = packet_get_innermost_layer(pkt, LAYER_TYPE_TCP);
* const struct tcphdr *hdr = (const struct tcphdr *)tcp_layer->hdr_ptr;
* uint16_t src_port = ntohs(hdr->th_sport);
* uint16_t dst_port = ntohs(hdr->th_dport);
* uint32_t seq = ntohl(hdr->th_seq);
* uint32_t ack = ntohl(hdr->th_ack);
*
******************************************************************************
* Example: foreach layer in packet
******************************************************************************
*
* // inorder
* int8_t layers = packet_get_layers(pkt);
* for (int8_t i = 0; i < layers; i++)
* {
* const struct packet_layer *layer = packet_get_layer(pkt, i);
* printf("layer[%d]: type=%d, hdr_offset=%d, hdr_len=%d, pld_len=%d\n", i, layer->type, layer->hdr_offset, layer->hdr_len, layer->pld_len);
* }
*
* // reverse
* for (int8_t i = layers - 1; i >= 0; i--)
* {
* const struct packet_layer *layer = packet_get_layer(pkt, i);
* printf("layer[%d]: type=%d, hdr_offset=%d, hdr_len=%d, pld_len=%d\n", i, layer->type, layer->hdr_offset, layer->hdr_len, layer->pld_len);
* }
*/
#ifdef __cplusplus
}
#endif
#endif

131
include/stellar/session.h Normal file
View File

@@ -0,0 +1,131 @@
#ifndef _SESSION_PUB_H
#define _SESSION_PUB_H
#ifdef __cplusplus
extern "C"
{
#endif
#include <stdint.h>
#include "stellar/packet.h"
enum session_state
{
SESSION_STATE_INIT = 0,
SESSION_STATE_OPENING = 1,
SESSION_STATE_ACTIVE = 2,
SESSION_STATE_CLOSING = 3,
SESSION_STATE_DISCARD = 4,
SESSION_STATE_CLOSED = 5,
MAX_STATE = 6,
};
enum session_type
{
SESSION_TYPE_TCP = 0x1,
SESSION_TYPE_UDP = 0x2,
};
enum session_direction
{
SESSION_DIRECTION_NONE = -1,
SESSION_DIRECTION_C2S = 0,
SESSION_DIRECTION_S2C = 1,
MAX_DIRECTION = 2,
};
enum closing_reason
{
CLOSING_BY_TIMEOUT = 0x1,
CLOSING_BY_EVICTED = 0x2,
CLOSING_BY_CLIENT_FIN = 0x3,
CLOSING_BY_CLIENT_RST = 0x4,
CLOSING_BY_SERVER_FIN = 0x5,
CLOSING_BY_SERVER_RST = 0x6,
};
enum session_stat
{
// raw packet
STAT_RAW_PKTS_RX,
STAT_RAW_BYTES_RX,
STAT_RAW_PKTS_TX,
STAT_RAW_BYTES_TX,
STAT_RAW_PKTS_DROP,
STAT_RAW_BYTES_DROP,
STAT_DUP_PKTS_BYPASS,
STAT_DUP_BYTES_BYPASS,
// control packet
STAT_CTRL_PKTS_RX, // TODO
STAT_CTRL_BYTES_RX, // TODO
STAT_CTRL_PKTS_TX,
STAT_CTRL_BYTES_TX,
STAT_CTRL_PKTS_DROP,
STAT_CTRL_BYTES_DROP,
// TCP segment
STAT_TCP_SEGS_RX,
STAT_TCP_PLDS_RX,
STAT_TCP_SEGS_EXPIRED,
STAT_TCP_PLDS_EXPIRED,
STAT_TCP_SEGS_OVERLAP,
STAT_TCP_PLDS_OVERLAP,
STAT_TCP_SEGS_NOSPACE,
STAT_TCP_PLDS_NOSPACE,
STAT_TCP_SEGS_INORDER,
STAT_TCP_PLDS_INORDER,
STAT_TCP_SEGS_REORDERED,
STAT_TCP_PLDS_REORDERED,
STAT_TCP_SEGS_BUFFERED,
STAT_TCP_PLDS_BUFFERED,
STAT_TCP_SEGS_RELEASED,
STAT_TCP_PLDS_RELEASED,
MAX_STAT,
};
enum session_timestamp
{
SESSION_TIMESTAMP_START,
SESSION_TIMESTAMP_LAST,
MAX_TIMESTAMP,
};
struct session;
int session_has_dup_traffic(const struct session *sess);
enum session_type session_get_type(const struct session *sess);
enum session_state session_get_state(const struct session *sess);
enum closing_reason session_get_closing_reason(const struct session *sess);
enum session_direction session_get_current_direction(const struct session *sess);
const struct packet *session_get_1st_packet(const struct session *sess, enum session_direction dir);
uint64_t session_get_id(const struct session *sess);
uint64_t session_get_timestamp(const struct session *sess, enum session_timestamp type);
uint64_t session_get_stat(const struct session *sess, enum session_direction dir, enum session_stat stat);
const char *session_get_tuple_str(const struct session *sess);
const char *session_type_to_str(enum session_type type);
const char *session_state_to_str(enum session_state state);
const char *session_direction_to_str(enum session_direction dir);
const char *closing_reason_to_str(enum closing_reason reason);
#ifdef __cplusplus
}
#endif
#endif

17
include/stellar/stellar.h Normal file
View File

@@ -0,0 +1,17 @@
#ifndef _STELLAR_PUB_H
#define _STELLAR_PUB_H
#ifdef __cplusplus
extern "C"
{
#endif
#include <stdint.h>
uint8_t stellar_get_current_thread_index();
#ifdef __cplusplus
}
#endif
#endif

91
include/stellar/tuple.h Normal file
View File

@@ -0,0 +1,91 @@
#ifndef _TUPLE_PUB_H
#define _TUPLE_PUB_H
#ifdef __cplusplus
extern "C"
{
#endif
#include <arpa/inet.h>
enum ip_type
{
IP_TYPE_V4,
IP_TYPE_V6,
};
union ip_address
{
struct in_addr v4; /* network order */
struct in6_addr v6; /* network order */
};
struct tuple2
{
enum ip_type ip_type;
union ip_address src_addr; /* network order */
union ip_address dst_addr; /* network order */
};
struct tuple4
{
enum ip_type ip_type;
union ip_address src_addr; /* network order */
union ip_address dst_addr; /* network order */
in_port_t src_port; /* network order */
in_port_t dst_port; /* network order */
};
struct tuple5
{
enum ip_type ip_type;
union ip_address src_addr; /* network order */
union ip_address dst_addr; /* network order */
in_port_t src_port; /* network order */
in_port_t dst_port; /* network order */
uint16_t ip_proto; /* network order */
};
struct tuple6
{
enum ip_type ip_type;
union ip_address src_addr; /* network order */
union ip_address dst_addr; /* network order */
uint16_t src_port; /* network order */
uint16_t dst_port; /* network order */
uint16_t ip_proto; /* network order */
uint64_t domain;
};
uint32_t tuple2_hash(const struct tuple2 *tuple);
uint32_t tuple4_hash(const struct tuple4 *tuple);
uint32_t tuple5_hash(const struct tuple5 *tuple);
uint32_t tuple6_hash(const struct tuple6 *tuple);
int tuple2_cmp(const struct tuple2 *tuple_a, const struct tuple2 *tuple_b);
int tuple4_cmp(const struct tuple4 *tuple_a, const struct tuple4 *tuple_b);
int tuple5_cmp(const struct tuple5 *tuple_a, const struct tuple5 *tuple_b);
int tuple6_cmp(const struct tuple6 *tuple_a, const struct tuple6 *tuple_b);
void tuple2_reverse(const struct tuple2 *in, struct tuple2 *out);
void tuple4_reverse(const struct tuple4 *in, struct tuple4 *out);
void tuple5_reverse(const struct tuple5 *in, struct tuple5 *out);
void tuple6_reverse(const struct tuple6 *in, struct tuple6 *out);
void tuple2_to_str(const struct tuple2 *tuple, char *buf, uint32_t size);
void tuple4_to_str(const struct tuple4 *tuple, char *buf, uint32_t size);
void tuple5_to_str(const struct tuple5 *tuple, char *buf, uint32_t size);
// output format: "src_addr:src_port -> dst_addr:dst_port, proto: ip_proto, domain: domain"
// output max len: 46 + 1 + 5 + 4 + 46 + 1 + 5 + 9 + 1 + 10 + 20 = 107
void tuple6_to_str(const struct tuple6 *tuple, char *buf, uint32_t size);
#ifdef __cplusplus
}
#endif
#endif