Refactored packet API to support struct layer (using union to contain different types of encapsulation headers)
This commit is contained in:
@@ -7,6 +7,7 @@
|
||||
#include "macro.h"
|
||||
#include "marsio.h"
|
||||
#include "packet_priv.h"
|
||||
#include "packet_utils.h"
|
||||
#include "marsio_io.h"
|
||||
|
||||
struct marsio_io
|
||||
@@ -22,6 +23,131 @@ struct marsio_io
|
||||
* Private API
|
||||
******************************************************************************/
|
||||
|
||||
static void metadata_from_mbuff_to_packet(marsio_buff_t *mbuff, struct packet *pkt)
|
||||
{
|
||||
struct route_ctx route_ctx = {0};
|
||||
struct sids sids = {0};
|
||||
uint64_t session_id = {0};
|
||||
uint64_t domain_id = {0};
|
||||
uint16_t link_id = {0};
|
||||
int is_ctrl = {0};
|
||||
enum packet_direction direction = PACKET_DIRECTION_OUTGOING;
|
||||
|
||||
route_ctx.used = marsio_buff_get_metadata(mbuff, MR_BUFF_ROUTE_CTX, &route_ctx.data, sizeof(route_ctx.data));
|
||||
if (route_ctx.used > 0)
|
||||
{
|
||||
packet_set_route_ctx(pkt, &route_ctx);
|
||||
}
|
||||
else
|
||||
{
|
||||
PACKET_IO_LOG_ERROR("failed to get route ctx");
|
||||
}
|
||||
|
||||
sids.used = marsio_buff_get_sid_list(mbuff, sids.sid, sizeof(sids.sid) / sizeof(sids.sid[0]));
|
||||
if (sids.used > 0)
|
||||
{
|
||||
packet_set_sids(pkt, &sids);
|
||||
}
|
||||
else
|
||||
{
|
||||
PACKET_IO_LOG_ERROR("failed to get sids");
|
||||
}
|
||||
|
||||
if (marsio_buff_get_metadata(mbuff, MR_BUFF_SESSION_ID, &session_id, sizeof(session_id)) == sizeof(session_id))
|
||||
{
|
||||
packet_set_session_id(pkt, session_id);
|
||||
}
|
||||
else
|
||||
{
|
||||
PACKET_IO_LOG_ERROR("failed to get session id");
|
||||
}
|
||||
|
||||
// TODO
|
||||
#if 0
|
||||
if (marsio_buff_get_metadata(mbuff, MR_BUFF_DOMAIN_ID, &domain_id, sizeof(domain_id)) == sizeof(domain_id))
|
||||
{
|
||||
packet_set_domain_id(pkt, domain_id);
|
||||
}
|
||||
else
|
||||
{
|
||||
PACKET_IO_LOG_ERROR("failed to get domain id");
|
||||
}
|
||||
#endif
|
||||
|
||||
if (marsio_buff_get_metadata(mbuff, MR_BUFF_LINK_ID, &link_id, sizeof(link_id)) == sizeof(link_id))
|
||||
{
|
||||
packet_set_link_id(pkt, link_id);
|
||||
}
|
||||
else
|
||||
{
|
||||
PACKET_IO_LOG_ERROR("failed to get link id");
|
||||
}
|
||||
|
||||
is_ctrl = marsio_buff_is_ctrlbuf(mbuff);
|
||||
packet_set_ctrl(pkt, is_ctrl);
|
||||
|
||||
if (marsio_buff_get_metadata(mbuff, MR_BUFF_DIR, &direction, sizeof(direction)) == sizeof(direction))
|
||||
{
|
||||
packet_set_direction(pkt, direction);
|
||||
}
|
||||
else
|
||||
{
|
||||
PACKET_IO_LOG_ERROR("failed to get direction");
|
||||
}
|
||||
|
||||
packet_set_action(pkt, PACKET_ACTION_FORWARD);
|
||||
packet_set_origin_ctx(pkt, mbuff);
|
||||
}
|
||||
|
||||
static void metadata_from_packet_to_mbuff(struct packet *pkt, marsio_buff_t *mbuff)
|
||||
{
|
||||
const struct route_ctx *route_ctx = packet_get_route_ctx(pkt);
|
||||
const struct sids *sids = packet_get_sids(pkt);
|
||||
uint64_t session_id = packet_get_session_id(pkt);
|
||||
uint64_t domain_id = packet_get_domain_id(pkt);
|
||||
uint16_t link_id = packet_get_link_id(pkt);
|
||||
int is_ctrl = packet_is_ctrl(pkt);
|
||||
enum packet_direction direction = packet_get_direction(pkt);
|
||||
|
||||
if (marsio_buff_set_metadata(mbuff, MR_BUFF_ROUTE_CTX, (void *)route_ctx->data, route_ctx->used) != 0)
|
||||
{
|
||||
PACKET_IO_LOG_ERROR("failed to set route ctx");
|
||||
}
|
||||
|
||||
if (marsio_buff_set_sid_list(mbuff, (sid_t *)sids->sid, sids->used) != 0)
|
||||
{
|
||||
PACKET_IO_LOG_ERROR("failed to set sids");
|
||||
}
|
||||
|
||||
if (marsio_buff_set_metadata(mbuff, MR_BUFF_SESSION_ID, &session_id, sizeof(session_id)) != 0)
|
||||
{
|
||||
PACKET_IO_LOG_ERROR("failed to set session id");
|
||||
}
|
||||
|
||||
// TODO
|
||||
#if 0
|
||||
if (marsio_buff_set_metadata(mbuff, MR_BUFF_DOMAIN_ID, &domain_id, sizeof(domain_id)) != 0)
|
||||
{
|
||||
PACKET_IO_LOG_ERROR("failed to set domain id");
|
||||
}
|
||||
#endif
|
||||
|
||||
if (marsio_buff_set_metadata(mbuff, MR_BUFF_LINK_ID, &link_id, sizeof(link_id)) != 0)
|
||||
{
|
||||
PACKET_IO_LOG_ERROR("failed to set link id");
|
||||
}
|
||||
|
||||
if (is_ctrl)
|
||||
{
|
||||
marsio_buff_set_ctrlbuf(mbuff);
|
||||
}
|
||||
|
||||
if (marsio_buff_set_metadata(mbuff, MR_BUFF_DIR, &direction, sizeof(direction)) != 0)
|
||||
{
|
||||
PACKET_IO_LOG_ERROR("failed to set direction");
|
||||
}
|
||||
}
|
||||
|
||||
static inline int is_keepalive_packet(const char *data, int len)
|
||||
{
|
||||
if (data == NULL || len < (int)(sizeof(struct ethhdr)))
|
||||
@@ -170,14 +296,11 @@ int marsio_io_ingress(struct marsio_io *handle, uint16_t thr_idx, struct packet
|
||||
pkt = &pkts[nr_parsed];
|
||||
memset(pkt, 0, sizeof(struct packet));
|
||||
packet_parse(pkt, data, len);
|
||||
packet_set_origin_ctx(pkt, mbuff);
|
||||
packet_set_origin(pkt, PACKET_ORIGIN_MARSIO);
|
||||
packet_set_action(pkt, PACKET_ACTION_FORWARD);
|
||||
metadata_from_mbuff_to_packet(mbuff, pkt);
|
||||
nr_parsed++;
|
||||
|
||||
if (marsio_buff_is_ctrlbuf(mbuff))
|
||||
{
|
||||
packet_set_ctrl(pkt);
|
||||
stat->ctrl_rx_pkts++;
|
||||
stat->ctrl_rx_bytes += len;
|
||||
}
|
||||
@@ -201,13 +324,14 @@ void marsio_io_egress(struct marsio_io *handle, uint16_t thr_idx, struct packet
|
||||
for (int i = 0; i < nr_pkts; i++)
|
||||
{
|
||||
pkt = &pkts[i];
|
||||
len = packet_get_len(pkt);
|
||||
len = packet_get_raw_len(pkt);
|
||||
|
||||
stat->dev_tx_pkts++;
|
||||
stat->dev_tx_bytes += len;
|
||||
|
||||
mbuff = (marsio_buff_t *)packet_get_origin_ctx(pkt);
|
||||
assert(mbuff != NULL);
|
||||
metadata_from_packet_to_mbuff(pkt, mbuff);
|
||||
|
||||
if (marsio_buff_is_ctrlbuf(mbuff))
|
||||
{
|
||||
@@ -238,7 +362,7 @@ void marsio_io_drop(struct marsio_io *handle, uint16_t thr_idx, struct packet *p
|
||||
if (mbuff)
|
||||
{
|
||||
stat->drop_pkts++;
|
||||
stat->drop_bytes += packet_get_len(pkt);
|
||||
stat->drop_bytes += packet_get_raw_len(pkt);
|
||||
marsio_buff_free(handle->mr_ins, &mbuff, 1, 0, thr_idx);
|
||||
}
|
||||
packet_free(pkt);
|
||||
@@ -253,12 +377,11 @@ int marsio_io_inject(struct marsio_io *handle, uint16_t thr_idx, struct packet *
|
||||
struct packet *pkt;
|
||||
marsio_buff_t *mbuff;
|
||||
struct io_stat *stat = &handle->stat[thr_idx];
|
||||
struct inject_packet_meta *meta;
|
||||
|
||||
for (int i = 0; i < nr_pkts; i++)
|
||||
{
|
||||
pkt = &pkts[i];
|
||||
len = packet_get_len(pkt);
|
||||
len = packet_get_raw_len(pkt);
|
||||
|
||||
if (marsio_buff_malloc_global(handle->mr_ins, &mbuff, 1, MARSIO_SOCKET_ID_ANY, MARSIO_LCORE_ID_ANY) < 0)
|
||||
{
|
||||
@@ -278,31 +401,9 @@ int marsio_io_inject(struct marsio_io *handle, uint16_t thr_idx, struct packet *
|
||||
nr_inject++;
|
||||
|
||||
ptr = marsio_buff_append(mbuff, len);
|
||||
memcpy(ptr, packet_get_data(pkt), len);
|
||||
meta = (struct inject_packet_meta *)packet_get_origin_ctx(pkt);
|
||||
if (meta)
|
||||
{
|
||||
if (meta->route.used && marsio_buff_set_metadata(mbuff, MR_BUFF_ROUTE_CTX, meta->route.data, meta->route.used) != 0)
|
||||
{
|
||||
PACKET_IO_LOG_ERROR("unable to set route context for inject packet");
|
||||
}
|
||||
if (meta->sids.used && marsio_buff_set_sid_list(mbuff, meta->sids.sid, meta->sids.used) != 0)
|
||||
{
|
||||
PACKET_IO_LOG_ERROR("unable to set sid list for inject packet");
|
||||
}
|
||||
if (meta->session_id && marsio_buff_set_metadata(mbuff, MR_BUFF_SESSION_ID, &meta->session_id, sizeof(meta->session_id)) != 0)
|
||||
{
|
||||
PACKET_IO_LOG_ERROR("unable to set session id for inject packet");
|
||||
}
|
||||
if (meta->link_id)
|
||||
{
|
||||
// TODO
|
||||
}
|
||||
if (meta->is_ctrl)
|
||||
{
|
||||
marsio_buff_set_ctrlbuf(mbuff);
|
||||
}
|
||||
}
|
||||
memcpy(ptr, packet_get_raw_data(pkt), len);
|
||||
metadata_from_packet_to_mbuff(pkt, mbuff);
|
||||
|
||||
marsio_send_burst_with_options(handle->mr_path, thr_idx, &mbuff, 1, MARSIO_SEND_OPT_REHASH);
|
||||
packet_free(pkt);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user