This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
stellar-stellar/infra/packet_io/marsio_io.c

423 lines
11 KiB
C

#include <sched.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <netinet/ether.h>
#include "marsio.h"
#include "marsio_io.h"
#include "log_internal.h"
#include "packet_parser.h"
#include "packet_internal.h"
#define MARSIO_IO_LOG_ERROR(format, ...) STELLAR_LOG_ERROR(__thread_local_logger, "marsio io", format, ##__VA_ARGS__)
struct marsio_io
{
struct packet_io_config cfg;
struct mr_instance *mr_ins;
struct mr_vdev *mr_dev;
struct mr_sendpath *mr_path;
struct packet_io_stat stat[MAX_THREAD_NUM];
};
/******************************************************************************
* Private API
******************************************************************************/
static void metadata_from_mbuff_to_packet(marsio_buff_t *mbuff, struct packet *pkt)
{
struct route_ctx route_ctx = {};
struct sids sids = {};
uint64_t session_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
{
MARSIO_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
{
MARSIO_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
{
MARSIO_IO_LOG_ERROR("failed to get session id");
}
// TODO
#if 0
if (marsio_buff_get_metadata(mbuff, MR_BUFF_DOMAIN, &domain, sizeof(domain)) == sizeof(domain))
{
packet_set_domain(pkt, domain);
}
else
{
MARSIO_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
{
MARSIO_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
{
MARSIO_IO_LOG_ERROR("failed to get direction");
}
packet_set_action(pkt, PACKET_ACTION_FORWARD);
packet_set_origin_ctx(pkt, mbuff);
// TODO
const struct timeval tv = {};
packet_set_timeval(pkt, &tv);
}
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 = packet_get_domain(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)
{
MARSIO_IO_LOG_ERROR("failed to set route ctx");
}
if (marsio_buff_set_sid_list(mbuff, (sid_t *)sids->sid, sids->used) != 0)
{
MARSIO_IO_LOG_ERROR("failed to set sids");
}
if (marsio_buff_set_metadata(mbuff, MR_BUFF_SESSION_ID, &session_id, sizeof(session_id)) != 0)
{
MARSIO_IO_LOG_ERROR("failed to set session id");
}
// TODO
#if 0
if (marsio_buff_set_metadata(mbuff, MR_BUFF_DOMAIN, &domain, sizeof(domain)) != 0)
{
MARSIO_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)
{
MARSIO_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)
{
MARSIO_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)))
{
return 0;
}
struct ethhdr *eth_hdr = (struct ethhdr *)data;
if (eth_hdr->h_proto == 0xAAAA)
{
return 1;
}
else
{
return 0;
}
}
/******************************************************************************
* Public API
******************************************************************************/
void *marsio_io_new(const struct packet_io_config *cfg)
{
int opt = 1;
cpu_set_t coremask;
CPU_ZERO(&coremask);
struct marsio_io *handle = (struct marsio_io *)calloc(1, sizeof(struct marsio_io));
if (handle == NULL)
{
MARSIO_IO_LOG_ERROR("unable to allocate memory for marsio_io");
return NULL;
}
memcpy(&handle->cfg, cfg, sizeof(struct packet_io_config));
for (uint16_t i = 0; i < handle->cfg.nr_worker_thread; i++)
{
CPU_SET(handle->cfg.cpu_mask[i], &coremask);
}
handle->mr_ins = marsio_create();
if (handle->mr_ins == NULL)
{
MARSIO_IO_LOG_ERROR("unable to create marsio instance");
goto error_out;
}
marsio_option_set(handle->mr_ins, MARSIO_OPT_THREAD_MASK_IN_CPUSET, &coremask, sizeof(cpu_set_t));
marsio_option_set(handle->mr_ins, MARSIO_OPT_EXIT_WHEN_ERR, &opt, sizeof(opt));
if (marsio_init(handle->mr_ins, handle->cfg.app_symbol) != 0)
{
MARSIO_IO_LOG_ERROR("unable to init marsio instance");
goto error_out;
}
handle->mr_dev = marsio_open_device(handle->mr_ins, handle->cfg.dev_symbol, handle->cfg.nr_worker_thread, handle->cfg.nr_worker_thread);
if (handle->mr_dev == NULL)
{
MARSIO_IO_LOG_ERROR("unable to open marsio device");
goto error_out;
}
handle->mr_path = marsio_sendpath_create_by_vdev(handle->mr_dev);
if (handle->mr_path == NULL)
{
MARSIO_IO_LOG_ERROR("unable to create marsio sendpath");
goto error_out;
}
return handle;
error_out:
marsio_io_free(handle);
return NULL;
}
void marsio_io_free(void *handle)
{
struct marsio_io *mr_io = (struct marsio_io *)handle;
if (mr_io)
{
if (mr_io->mr_path)
{
marsio_sendpath_destory(mr_io->mr_path);
mr_io->mr_path = NULL;
}
if (mr_io->mr_dev)
{
marsio_close_device(mr_io->mr_dev);
mr_io->mr_dev = NULL;
}
if (mr_io->mr_ins)
{
marsio_destory(mr_io->mr_ins);
mr_io->mr_ins = NULL;
}
free(mr_io);
mr_io = NULL;
}
}
int marsio_io_isbreak(void *handle __attribute__((unused)))
{
return 0;
}
int marsio_io_init(void *handle, uint16_t thr_idx __attribute__((unused)))
{
struct marsio_io *mr_io = (struct marsio_io *)handle;
if (marsio_thread_init(mr_io->mr_ins) != 0)
{
MARSIO_IO_LOG_ERROR("unable to init marsio thread");
return -1;
}
return 0;
}
uint16_t marsio_io_ingress(void *handle, uint16_t thr_idx, struct packet *pkts, uint16_t nr_pkts)
{
int len;
char *data;
uint16_t nr_packet_parsed = 0;
struct packet *pkt;
marsio_buff_t *mbuff;
marsio_buff_t *rx_buffs[RX_BURST_MAX];
struct marsio_io *mr_io = (struct marsio_io *)handle;
struct packet_io_stat *stat = &mr_io->stat[thr_idx];
int nr_packet_received = marsio_recv_burst(mr_io->mr_dev, thr_idx, rx_buffs, MIN(RX_BURST_MAX, nr_pkts));
if (nr_packet_received <= 0)
{
return nr_packet_parsed;
}
for (int i = 0; i < nr_packet_received; i++)
{
mbuff = rx_buffs[i];
data = marsio_buff_mtod(mbuff);
len = marsio_buff_datalen(mbuff);
stat->pkts_rx++;
stat->bytes_rx += len;
if (is_keepalive_packet(data, len))
{
stat->keep_alive_pkts++;
stat->keep_alive_bytes += len;
stat->pkts_tx++;
stat->bytes_tx += len;
marsio_send_burst(mr_io->mr_path, thr_idx, &mbuff, 1);
continue;
}
pkt = &pkts[nr_packet_parsed++];
packet_parse(pkt, data, len);
metadata_from_mbuff_to_packet(mbuff, pkt);
if (marsio_buff_is_ctrlbuf(mbuff))
{
stat->ctrl_pkts_rx++;
stat->ctrl_bytes_rx += len;
}
else
{
stat->raw_pkts_rx++;
stat->raw_bytes_rx += len;
}
}
return nr_packet_parsed;
}
void marsio_io_egress(void *handle, uint16_t thr_idx, struct packet *pkts, uint16_t nr_pkts)
{
int is_injected_packet = 0;
int len;
char *ptr;
struct packet *pkt;
marsio_buff_t *mbuff;
struct marsio_io *mr_io = (struct marsio_io *)handle;
struct packet_io_stat *stat = &mr_io->stat[thr_idx];
for (uint16_t i = 0; i < nr_pkts; i++)
{
is_injected_packet = 0;
pkt = &pkts[i];
len = packet_get_raw_len(pkt);
mbuff = (marsio_buff_t *)packet_get_origin_ctx(pkt);
if (mbuff == NULL)
{
if (marsio_buff_malloc_global(mr_io->mr_ins, &mbuff, 1, MARSIO_SOCKET_ID_ANY, MARSIO_LCORE_ID_ANY) < 0)
{
MARSIO_IO_LOG_ERROR("unable to allocate marsio buffer for inject packet");
continue;
}
ptr = marsio_buff_append(mbuff, len);
memcpy(ptr, packet_get_raw_data(pkt), len);
is_injected_packet = 1;
}
metadata_from_packet_to_mbuff(pkt, mbuff);
stat->pkts_tx++;
stat->bytes_tx += len;
if (packet_is_ctrl(pkt))
{
stat->ctrl_pkts_tx++;
stat->ctrl_bytes_tx += len;
}
else
{
stat->raw_pkts_tx++;
stat->raw_bytes_tx += len;
}
if (is_injected_packet)
{
stat->pkts_injected++;
stat->bytes_injected += len;
marsio_send_burst_with_options(mr_io->mr_path, thr_idx, &mbuff, 1, MARSIO_SEND_OPT_REHASH);
}
else
{
marsio_send_burst(mr_io->mr_path, thr_idx, &mbuff, 1);
}
}
}
void marsio_io_drop(void *handle, uint16_t thr_idx, struct packet *pkts, uint16_t nr_pkts)
{
struct packet *pkt;
marsio_buff_t *mbuff;
struct marsio_io *mr_io = (struct marsio_io *)handle;
struct packet_io_stat *stat = &mr_io->stat[thr_idx];
for (uint16_t i = 0; i < nr_pkts; i++)
{
pkt = &pkts[i];
mbuff = (marsio_buff_t *)packet_get_origin_ctx(pkt);
if (mbuff)
{
stat->pkts_dropped++;
stat->bytes_dropped += packet_get_raw_len(pkt);
marsio_buff_free(mr_io->mr_ins, &mbuff, 1, 0, thr_idx);
}
}
}
void marsio_io_yield(void *handle, uint16_t thr_idx)
{
struct marsio_io *mr_io = (struct marsio_io *)handle;
struct mr_vdev *vdevs[1] = {
mr_io->mr_dev,
};
marsio_poll_wait(mr_io->mr_ins, vdevs, 1, thr_idx, mr_io->cfg.idle_yield_interval_ms);
}
struct packet_io_stat *marsio_io_stat(void *handle, uint16_t thr_idx)
{
struct marsio_io *mr_io = (struct marsio_io *)handle;
return &mr_io->stat[thr_idx];
}