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-2022/src/packet_io/marsio_mode/pio_marsio.cpp
2022-07-29 15:41:09 +08:00

386 lines
15 KiB
C++

/*
**********************************************************************************************
* File: marsio.h
* Description: marsio runmode api
* Authors: Liu WenTan <liuwentan@geedgenetworks.com>
* Date: 2022-07-15
* Copyright: (c) 2018-2022 Geedge Networks, Inc. All rights reserved.
***********************************************************************************************
*/
#include <dlfcn.h>
#include "pio_marsio.h"
#include "../packet_io.h"
#include "../../common/global_var.h"
#include "../../common/common.h"
#include "../../sdk/include/logger.h"
#include "../../sdk/include/utils.h"
#include "../../sdk/include/util_errors.h"
#define MARSIO_BURST_PKT_MAX (256)
/* marsio dynamic link lib function entries */
static struct marsio_dll_function_entries g_marsio_dll_func;
static void fake_marsio_buff_set_rehash_index(marsio_buff_t *m, uint32_t hash)
{
return;
}
static int pio_get_marsio_dll_function_entries(void)
{
void *marsio_so_handle = dlopen(g_engine_instance.config.lib.libmarsio_path,
RTLD_NOW | RTLD_LOCAL | RTLD_NODELETE);
if (nullptr == marsio_so_handle) {
printf("\033[1;31;40m[Error]dlopen '%s' failed, %s\033[0m\n",
g_engine_instance.config.lib.libmarsio_path, dlerror());
return -1;
}
g_marsio_dll_func.marsio_create = \
(struct mr_instance *(*)(void))dlsym(marsio_so_handle, "marsio_create");
if (nullptr == g_marsio_dll_func.marsio_create) {
printf("\033[1;31;40m[Error]dlsym function '%s' from '%s' failed!\033[0m\n", "marsio_create",
g_engine_instance.config.lib.libmarsio_path);
return -1;
}
g_marsio_dll_func.marsio_init = \
(int (*)(struct mr_instance *, const char *))dlsym(marsio_so_handle, "marsio_init");
if (nullptr == g_marsio_dll_func.marsio_init) {
printf("\033[1;31;40m[Error]dlsym function '%s' from '%s' failed!\033[0m\n", "marsio_init",
g_engine_instance.config.lib.libmarsio_path);
return -1;
}
g_marsio_dll_func.marsio_destroy = \
(int (*)(struct mr_instance *))dlsym(marsio_so_handle, "marsio_destory");
if (nullptr == g_marsio_dll_func.marsio_destroy) {
printf("\033[1;31;40m[Error]dlsym function '%s' from '%s' failed!\033[0m\n", "marsio_destory",
g_engine_instance.config.lib.libmarsio_path);
return -1;
}
g_marsio_dll_func.marsio_open_device = \
(struct mr_vdev *(*)(struct mr_instance *, const char *,
unsigned int, unsigned int))dlsym(marsio_so_handle, "marsio_open_device");
if (nullptr == g_marsio_dll_func.marsio_open_device) {
printf("\033[1;31;40m[Error]dlsym function '%s' from '%s' failed!\033[0m\n", "marsio_open_device",
g_engine_instance.config.lib.libmarsio_path);
return -1;
}
g_marsio_dll_func.marsio_close_device = \
(void (*)(struct mr_vdev *))dlsym(marsio_so_handle, "marsio_close_device");
if (nullptr ==g_marsio_dll_func.marsio_close_device) {
printf("\033[1;31;40m[Error]dlsym function '%s' from '%s' failed!\033[0m\n", "marsio_close_device",
g_engine_instance.config.lib.libmarsio_path);
return -1;
}
g_marsio_dll_func.marsio_option_set = \
(int (*)(struct mr_instance *, marsio_opt_type_t, void *, size_t))dlsym(marsio_so_handle, "marsio_option_set");
if (nullptr == g_marsio_dll_func.marsio_option_set) {
printf("\033[1;31;40m[Error]dlsym function '%s' from '%s' failed!\033[0m\n", "marsio_option_set",
g_engine_instance.config.lib.libmarsio_path);
return -1;
}
g_marsio_dll_func.marsio_sendpath_create_by_vdev = \
(struct mr_sendpath *(*)(struct mr_vdev *))dlsym(marsio_so_handle, "marsio_sendpath_create_by_vdev");
if (nullptr == g_marsio_dll_func.marsio_sendpath_create_by_vdev) {
printf("\033[1;31;40m[Error]dlsym function '%s' from '%s' failed!\033[0m\n", "marsio_sendpath_create_by_vdev",
g_engine_instance.config.lib.libmarsio_path);
return -1;
}
g_marsio_dll_func.marsio_sendpath_destroy = \
(void (*)(struct mr_sendpath *))dlsym(marsio_so_handle, "marsio_sendpath_destory");
if (nullptr == g_marsio_dll_func.marsio_sendpath_destroy) {
printf("\033[1;31;40m[Error]dlsym function '%s' from '%s' failed!\033[0m\n", "marsio_sendpath_destory",
g_engine_instance.config.lib.libmarsio_path);
return -1;
}
g_marsio_dll_func.marsio_thread_init = (int (*)(struct mr_instance *))dlsym(marsio_so_handle, "marsio_thread_init");
if (nullptr == g_marsio_dll_func.marsio_thread_init) {
printf("\033[1;31;40m[Error]dlsym function '%s' from '%s' failed!\033[0m\n", "marsio_thread_init",
g_engine_instance.config.lib.libmarsio_path);
return -1;
}
g_marsio_dll_func.marsio_recv_burst = \
(int (*)(struct mr_vdev *, queue_id_t, marsio_buff_t **, int))dlsym(marsio_so_handle, "marsio_recv_burst");
if (nullptr == g_marsio_dll_func.marsio_recv_burst) {
printf("\033[1;31;40m[Error]dlsym function '%s' from '%s' failed!\033[0m\n", "marsio_recv_burst",
g_engine_instance.config.lib.libmarsio_path);
return -1;
}
g_marsio_dll_func.marsio_send_burst = \
(int (*)(struct mr_sendpath *, queue_id_t, marsio_buff_t **, int))dlsym(marsio_so_handle, "marsio_send_burst");
if (nullptr == g_marsio_dll_func.marsio_send_burst) {
printf("\033[1;31;40m[Error]dlsym function '%s' from '%s' failed!\033[0m\n", "marsio_send_burst",
g_engine_instance.config.lib.libmarsio_path);
return -1;
}
g_marsio_dll_func.marsio_send_burst_with_options =
(int (*)(struct mr_sendpath *, queue_id_t,
marsio_buff_t **, int, uint16_t))dlsym(marsio_so_handle, "marsio_send_burst_with_options");
if (nullptr == g_marsio_dll_func.marsio_send_burst_with_options) {
printf("\033[1;31;40m[Error]dlsym function '%s' from '%s' failed!\033[0m\n", "marsio_send_burst_with_options",
g_engine_instance.config.lib.libmarsio_path);
return -1;
}
g_marsio_dll_func.marsio_buff_malloc_global = \
(int (*)(struct mr_instance *, marsio_buff_t **,
unsigned int, int, int))dlsym(marsio_so_handle, "marsio_buff_malloc_global");
if (nullptr == g_marsio_dll_func.marsio_buff_malloc_global) {
printf("\033[1;31;40m[Error]dlsym function '%s' from '%s' failed!\033[0m\n", "marsio_buff_malloc_global",
g_engine_instance.config.lib.libmarsio_path);
return -1;
}
g_marsio_dll_func.marsio_buff_free = \
(void (*)(struct mr_instance *, marsio_buff_t **,
unsigned int, int, int))dlsym(marsio_so_handle, "marsio_buff_free");
if (nullptr == g_marsio_dll_func.marsio_buff_free) {
printf("\033[1;31;40m[Error]dlsym function '%s' from '%s' failed!\033[0m\n", "marsio_buff_free",
g_engine_instance.config.lib.libmarsio_path);
return -1;
}
g_marsio_dll_func.marsio_buff_append = \
(char * (*)(marsio_buff_t *, uint16_t))dlsym(marsio_so_handle, "marsio_buff_append");
if (nullptr == g_marsio_dll_func.marsio_buff_append) {
printf("\033[1;31;40m[Error]dlsym function '%s' from '%s' failed!\033[0m\n", "marsio_buff_append",
g_engine_instance.config.lib.libmarsio_path);
return -1;
}
g_marsio_dll_func.marsio_buff_ctrlzone = \
(void * (*)(marsio_buff_t *, uint8_t))dlsym(marsio_so_handle, "marsio_buff_ctrlzone");
if (nullptr == g_marsio_dll_func.marsio_buff_ctrlzone) {
printf("\033[1;31;40m[Error]dlsym function '%s' from '%s' failed!\033[0m\n", "marsio_buff_ctrlzone",
g_engine_instance.config.lib.libmarsio_path);
return -1;
}
g_marsio_dll_func.marsio_buff_ctrlzone_set = \
(void (*)(marsio_buff_t *, uint8_t,
void *, uint8_t))dlsym(marsio_so_handle, "marsio_buff_ctrlzone_set");
if (nullptr == g_marsio_dll_func.marsio_buff_ctrlzone_set) {
printf("\033[1;31;40m[Error]dlsym function '%s' from '%s' failed!\033[0m\n", "marsio_buff_ctrlzone_set",
g_engine_instance.config.lib.libmarsio_path);
return -1;
}
g_marsio_dll_func.marsio_buff_set_rehash_index = \
(void (*)(marsio_buff_t *, uint32_t))dlsym(marsio_so_handle, "marsio_buff_set_rehash_index");
if (nullptr == g_marsio_dll_func.marsio_buff_set_rehash_index) {
printf("\033[1;31;40m[Error]dlsym function '%s' from '%s' failed!\033[0m\n", "marsio_buff_set_rehash_index",
g_engine_instance.config.lib.libmarsio_path);
g_marsio_dll_func.marsio_buff_set_rehash_index = fake_marsio_buff_set_rehash_index;
}
g_marsio_dll_func.marsio_buff_mtod = (char * (*)(marsio_buff_t *))dlsym(marsio_so_handle, "marsio_buff_mtod");
if (nullptr == g_marsio_dll_func.marsio_buff_mtod) {
printf("\033[1;31;40m[Error]dlsym function '%s' from '%s' failed!\033[0m\n", "marsio_buff_mtod",
g_engine_instance.config.lib.libmarsio_path);
return -1;
}
g_marsio_dll_func.marsio_buff_datalen = (uint32_t (*)(marsio_buff_t *))dlsym(marsio_so_handle, "marsio_buff_datalen");
if (nullptr == g_marsio_dll_func.marsio_buff_datalen) {
printf("\033[1;31;40m[Error]dlsym function '%s' from '%s' failed!\033[0m\n", "marsio_buff_datalen",
g_engine_instance.config.lib.libmarsio_path);
return -1;
}
g_marsio_dll_func.marsio_buff_buflen = (uint32_t (*)(marsio_buff_t *))dlsym(marsio_so_handle, "marsio_buff_buflen");
if (nullptr == g_marsio_dll_func.marsio_buff_buflen) {
printf("\033[1;31;40m[Error]dlsym function '%s' from '%s' failed!\033[0m\n", "marsio_buff_buflen",
g_engine_instance.config.lib.libmarsio_path);
return -1;
}
g_marsio_dll_func.marsio_buff_clone_with_options = \
(marsio_buff_t *(*)(struct mr_instance *, marsio_buff_t *,
int, int, uint16_t))dlsym(marsio_so_handle, "marsio_buff_clone_with_options");
if (nullptr == g_marsio_dll_func.marsio_buff_clone_with_options) {
printf("\033[1;31;40m[Error]dlsym function '%s' from '%s' failed!\033[0m\n", "marsio_buff_clone_with_options",
g_engine_instance.config.lib.libmarsio_path);
return -1;
}
/* note: if not include marsio_send_burst_flush(), no errors. include just for backward compatibility */
g_marsio_dll_func.marsio_send_burst_flush = \
(void (*)(struct mr_sendpath *, queue_id_t))dlsym(marsio_so_handle, "marsio_send_burst_flush");
if (nullptr == g_marsio_dll_func.marsio_send_burst_flush) {
printf("\033[33m[Warning]dlsym function '%s' from '%s' failed!\033[0m\n", "marsio_send_burst_flush",
g_engine_instance.config.lib.libmarsio_path);
}
/* for vlan flipping */
g_marsio_dll_func.marsio_buff_get_metadata = \
(int (*)(marsio_buff_t *, enum mr_buff_metadata_type,
void *, unsigned int))dlsym(marsio_so_handle, "marsio_buff_get_metadata");
if (nullptr == g_marsio_dll_func.marsio_buff_get_metadata) {
printf("\033[33m[Error]dlsym function '%s' from '%s' failed!\033[0m\n", "marsio_buff_get_metadata",
g_engine_instance.config.lib.libmarsio_path);
/*
in order to be forward compatible with the previous version of mrzcpd, no error is returned here.
vlan_flipping will become invalid
*/
}
g_marsio_dll_func.marsio_buff_set_metadata = \
(int (*)(marsio_buff_t *, enum mr_buff_metadata_type,
void *, unsigned int))dlsym(marsio_so_handle, "marsio_buff_set_metadata");
if (nullptr == g_marsio_dll_func.marsio_buff_get_metadata) {
printf("\033[33m[Error]dlsym function '%s' from '%s' failed!\033[0m\n", "marsio_buff_set_metadata",
g_engine_instance.config.lib.libmarsio_path);
/*
in order to be forward compatible with the previous version of mrzcpd, no error is returned here.
vlan_flipping will become invalid
*/
}
g_marsio_dll_func.marsio_buff_unset_metadata = \
(int (*)(marsio_buff_t *, enum mr_buff_metadata_type))dlsym(marsio_so_handle, "marsio_buff_unset_metadata");
if (nullptr == g_marsio_dll_func.marsio_buff_unset_metadata) {
printf("\033[33m[Error]dlsym function '%s' from '%s' failed!\033[0m\n", "marsio_buff_unset_metadata",
g_engine_instance.config.lib.libmarsio_path);
/*
in order to be forward compatible with the previous version of mrzcpd, no error is returned here.
vlan_flipping will become invalid
*/
}
return 0;
}
int pio_marsio_device_open(struct packet_io_device *pdev, const char *dev_name, uint32_t nr_rxq, uint32_t nr_txq)
{
struct mr_instance *mr_inst_handle = pdev->ppio_inst->entity.marsio_inst->mr_inst_handle;
/* marsio_open_device() return marsio device handle*/
pdev->entity.marsio_dev->mr_dev_handle = \
g_marsio_dll_func.marsio_open_device(mr_inst_handle, dev_name, nr_rxq, nr_txq);
if (nullptr == pdev->entity.marsio_dev->mr_dev_handle) {
fprintf(stderr,"marsio_open_device:%s error\n", dev_name);
return -1;
}
/* ptr_marsio_sendpath_create_by_vdev() return marsio sendpath handle */
pdev->entity.marsio_dev->mr_sendpath_handle = \
g_marsio_dll_func.marsio_sendpath_create_by_vdev(pdev->entity.marsio_dev->mr_dev_handle);
if (nullptr == pdev->entity.marsio_dev->mr_sendpath_handle) {
log_error(ST_ERR_PIO_DEVICE, "device:%s marsio_sendpath_create_by_vdev failed!", pdev->dev_name);
return -1;
}
return 0;
}
int pio_marsio_device_close(const struct packet_io_device *pdev)
{
if (nullptr == pdev) {
log_error(ST_ERR_PIO_DEVICE, "invalid pdev pointer so close marsio device failed!");
return -1;
}
g_marsio_dll_func.marsio_close_device(pdev->entity.marsio_dev->mr_dev_handle);
g_marsio_dll_func.marsio_sendpath_destroy(pdev->entity.marsio_dev->mr_sendpath_handle);
return 0;
}
int pio_marsio_device_receive(struct packet_io_device *pdev, uint32_t rxq_id, struct packet **pkts, int nr_pkts)
{
struct mr_vdev *mr_dev_handle = pdev->entity.marsio_dev->mr_dev_handle;
marsio_buff_t *rx_buff[MARSIO_BURST_PKT_MAX];
int recv_res = g_marsio_dll_func.marsio_recv_burst(mr_dev_handle, rxq_id, rx_buff, nr_pkts);
/* receive some pkts, copy mbuf pointer to packet structure */
if (recv_res > 0) {
for (int i = 0; i < recv_res; i++) {
pkts[i]= (struct packet *)rx_buff[i];
}
}
return recv_res;
}
int pio_marsio_device_send(struct packet_io_device *pdev, uint32_t txq_id, struct packet **pkts, int nr_pkts)
{
struct mr_sendpath *sendpath_handle = pdev->entity.marsio_dev->mr_sendpath_handle;
return g_marsio_dll_func.marsio_send_burst(sendpath_handle, txq_id, (marsio_buff_t **)pkts, nr_pkts);
}
static int marsio_instance_init(struct packet_io_instance *pinst, int wrk_thread_num)
{
int ret = -1;
ret = pio_get_marsio_dll_function_entries();
if (ret < 0) {
printf("\033[1;31;40m[Error]dlopen marsio.so symbol failed!\033[0m\n");
return -1;
}
pinst->entity.marsio_inst->mr_inst_handle = g_marsio_dll_func.marsio_create();
if (nullptr == pinst->entity.marsio_inst->mr_inst_handle) {
fprintf(stderr,"%s\n","marsio_create error!\n");
return -1;
}
/* TODO: MARSIO_OPT_THREAD_NUM */
ret = g_marsio_dll_func.marsio_option_set(pinst->entity.marsio_inst->mr_inst_handle,
MARSIO_OPT_THREAD_NUM,
&wrk_thread_num, sizeof(int));
if (ret < 0) {
fprintf(stderr,"%s\n","marsio_option_set MARSIO_OPT_THREAD_NUM error!\n");
return -1;
}
/* TODO: MARSIO_OPT_THREAD_MASK_IN_CPUSET */
/* marsio_init */
ret = g_marsio_dll_func.marsio_init(pinst->entity.marsio_inst->mr_inst_handle, pinst->inst_name);
if (ret < 0) {
fprintf(stderr,"%s\n","marsio_init error!\n");
return -1;
}
return 0;
}
/*
* @brief create packet_io marsio instance
*
* @param: pinst(in/out)
*/
int pio_marsio_instance_create(struct packet_io_instance *pinst, int wrk_thread_num)
{
/* instance init */
int ret = marsio_instance_init(pinst, wrk_thread_num);
if (ret < 0) {
log_error(ST_ERR_PIO_INSTANCE, "marsio instance init failed.");
return -1;
}
return 0;
}
void pio_marsio_instance_destroy(struct packet_io_instance *pinst)
{
g_marsio_dll_func.marsio_destroy(pinst->entity.marsio_inst->mr_inst_handle);
for (uint32_t i = 0; i < pinst->dev_cnt; i++) {
pio_marsio_device_close(pinst->devices[i]);
FREE(pinst->devices[i]);
}
}