2022-07-26 15:05:14 +08:00
|
|
|
/*
|
|
|
|
|
**********************************************************************************************
|
|
|
|
|
* File: pcap_file.cpp
|
|
|
|
|
* Description: pcap file runmode api
|
|
|
|
|
* Authors: Liu WenTan <liuwentan@geedgenetworks.com>
|
|
|
|
|
* Date: 2022-07-15
|
|
|
|
|
* Copyright: (c) 2018-2022 Geedge Networks, Inc. All rights reserved.
|
|
|
|
|
***********************************************************************************************
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2022-08-01 15:19:57 +08:00
|
|
|
#include <errno.h>
|
2022-07-26 15:05:14 +08:00
|
|
|
|
2022-07-28 15:12:46 +08:00
|
|
|
#include "pio_pcap_file.h"
|
2022-08-01 15:19:57 +08:00
|
|
|
#include "../packet_io.h"
|
|
|
|
|
#include "../../sdk/include/utils.h"
|
|
|
|
|
#include "../../sdk/include/util_errors.h"
|
|
|
|
|
#include "../../sdk/include/logger.h"
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* @brief validate path is a valid plain file or directory
|
|
|
|
|
*
|
|
|
|
|
* @retval failed (-1) successful (0),
|
|
|
|
|
* if success, dir == nullptr <---> means path is plain file
|
|
|
|
|
* dir != nullptr <---> means path is directory
|
|
|
|
|
**/
|
|
|
|
|
static int validate_directory_or_file(const char *path, DIR **dir) {
|
|
|
|
|
DIR *temp_dir = nullptr;
|
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
|
|
temp_dir = opendir(path);
|
|
|
|
|
if (nullptr == temp_dir) {
|
|
|
|
|
switch (errno) {
|
|
|
|
|
case EACCES:
|
|
|
|
|
log_error(ST_ERR_FOPEN, "%s: Permission denied", path);
|
|
|
|
|
break;
|
|
|
|
|
case EBADF:
|
|
|
|
|
log_error(ST_ERR_FOPEN, "%s: invalid file descriptor", path);
|
|
|
|
|
break;
|
|
|
|
|
case ENOTDIR:
|
|
|
|
|
log_info("%s: is a plain file, not directory", path);
|
|
|
|
|
ret = 0;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
log_error(ST_ERR_FOPEN, "%s: errno:%d", path, errno);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
*dir = temp_dir;
|
|
|
|
|
ret = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int pcap_plain_file_init(struct pio_pcap_file_device_context *pfile_dev_ctx) {
|
|
|
|
|
struct pcap_plain_file_info *pfile_info = CALLOC(struct pcap_plain_file_info, 1);
|
|
|
|
|
if (nullptr == pfile_info) {
|
|
|
|
|
log_error(ST_ERR_PIO_PCAP_FILE_DEVICE);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int pcap_directory_file_init(struct pio_pcap_file_device_context *pfile_dev_ctx) {
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int pcap_file_shared_init(struct pio_pcap_file_device_context *pfile_dev_ctx) {
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2022-07-26 15:05:14 +08:00
|
|
|
|
2022-07-29 17:29:33 +08:00
|
|
|
int pio_pcap_file_device_open(struct packet_io_device *pdev, const char *path, uint32_t nr_rxq, uint32_t nr_txq)
|
|
|
|
|
{
|
2022-08-01 15:19:57 +08:00
|
|
|
int status = -1;
|
|
|
|
|
DIR *directory = nullptr;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
status = pcap_file_shared_init(pdev->entity.pcap_file_dev_ctx);
|
|
|
|
|
if (status < 0) {
|
|
|
|
|
log_error(ST_ERR_PIO_PCAP_FILE_DEVICE, "pcap file shared init failed.");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (validate_directory_or_file(path, &directory) != 0) {
|
|
|
|
|
log_error(ST_ERR_PIO_PCAP_FILE_DEVICE, "invalid path:%s (not plain file or directory)", path);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (nullptr == directory) {
|
|
|
|
|
/* plain file */
|
|
|
|
|
status = pcap_plain_file_init(pdev->entity.pcap_file_dev_ctx);
|
|
|
|
|
if (status < 0) {
|
|
|
|
|
log_error(ST_ERR_PIO_PCAP_FILE_DEVICE, "pcap plain file init failed.");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
/* directory */
|
|
|
|
|
status = pcap_directory_file_init(pdev->entity.pcap_file_dev_ctx);
|
|
|
|
|
if (status < 0) {
|
|
|
|
|
log_error(ST_ERR_PIO_PCAP_FILE_DEVICE, "pcap directory file init failed.");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-07-26 15:05:14 +08:00
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-01 15:19:57 +08:00
|
|
|
int pio_pcap_file_device_close(struct packet_io_device *pdev)
|
2022-07-29 17:29:33 +08:00
|
|
|
{
|
2022-08-01 15:19:57 +08:00
|
|
|
if (nullptr == pdev) {
|
|
|
|
|
log_error(ST_ERR_PIO_PCAP_FILE_DEVICE, "invalid pdev pointer so close pcap file device failed!");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* TODO: */
|
|
|
|
|
//pcap_close(pdev->entity.pcap_file_dev->pcap_handle);
|
2022-07-26 15:05:14 +08:00
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-29 17:29:33 +08:00
|
|
|
int pio_pcap_file_device_receive(struct packet_io_device *pdev, uint32_t rxq_id, struct packet **pkts, int nr_pkts)
|
|
|
|
|
{
|
2022-07-26 15:05:14 +08:00
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-29 17:29:33 +08:00
|
|
|
int pio_pcap_file_device_send(struct packet_io_device *pdev, uint32_t txq_id, struct packet **pkts, int nr_pkts)
|
|
|
|
|
{
|
2022-07-26 15:05:14 +08:00
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-29 17:29:33 +08:00
|
|
|
void pio_pcap_file_device_pkt_free(struct packet_io_device *pdev, uint32_t qid, struct packet **pkts, int nr_pkts)
|
|
|
|
|
{
|
2022-07-26 15:05:14 +08:00
|
|
|
|
2022-07-29 17:29:33 +08:00
|
|
|
}
|
|
|
|
|
|
2022-08-01 15:19:57 +08:00
|
|
|
int pio_pcap_file_instance_create(struct packet_io_instance *pinst, __unused int wrk_thread_num)
|
2022-07-29 17:29:33 +08:00
|
|
|
{
|
2022-07-26 15:05:14 +08:00
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-29 17:29:33 +08:00
|
|
|
void pio_pcap_file_instance_destroy(struct packet_io_instance *pinst)
|
|
|
|
|
{
|
2022-08-01 15:19:57 +08:00
|
|
|
for (uint32_t i = 0; i < pinst->dev_cnt; i++) {
|
|
|
|
|
pio_pcap_file_device_close(pinst->devices[i]);
|
|
|
|
|
FREE(pinst->devices[i]);
|
|
|
|
|
}
|
2022-07-26 15:05:14 +08:00
|
|
|
}
|