Refactored packet API to support struct layer (using union to contain different types of encapsulation headers)

This commit is contained in:
luwenpeng
2024-06-14 19:24:27 +08:00
parent 1f78881cbb
commit de4c15f43c
47 changed files with 834 additions and 701 deletions

View File

@@ -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);
}
}