[PACKET_IO]format code style

This commit is contained in:
liuwentan
2022-08-11 11:18:14 +08:00
parent b471a6eac4
commit 4005e8d716
13 changed files with 67 additions and 66 deletions

View File

@@ -1,9 +1,9 @@
add_library(packet_io
../common/global_var.cpp
../common/pio_packet_queue.cpp
../common/time_helper.cpp
packet_io.cpp
packet_io_util.cpp
pcap_live_mode/pio_pcap_live.cpp
pcap_file_mode/pio_pcap_file.cpp
marsio_mode/pio_marsio.cpp

View File

@@ -11,12 +11,12 @@
#include <dlfcn.h>
#include <string.h>
#include "global_var.h"
#include "logger.h"
#include "utils.h"
#include "util_errors.h"
#include "pio_marsio.h"
#include "../packet_io.h"
#include "../../common/global_var.h"
#include "../../sdk/include/logger.h"
#include "../../sdk/include/utils.h"
#include "../../sdk/include/util_errors.h"
#include "packet_io.h"
#define MARSIO_BURST_PKT_MAX (256)

View File

@@ -10,10 +10,10 @@
#include <string.h>
#include "logger.h"
#include "utils.h"
#include "util_errors.h"
#include "packet_io.h"
#include "../../sdk/include/logger.h"
#include "../../sdk/include/utils.h"
#include "../../sdk/include/util_errors.h"
struct pio_device_operations pio_device_ops_array[PACKET_IO_RUN_MODE_MAX] =
{

View File

@@ -20,13 +20,13 @@ extern "C"
#include <limits.h>
#include <sys/queue.h>
#include "../common/global_var.h"
#include "global_var.h"
#include "./pcap_live_mode/pio_pcap_live.h"
#include "./pcap_file_mode/pio_pcap_file.h"
#include "./marsio_mode/pio_marsio.h"
/**
* note:
* note:
* 1. packet_io_XXX function is supported by packet_io.h
* 2. pio_XXX function is supported by pio_pcap_live.h/pio_pcap_file.h/pio_marsio.h
*/

View File

@@ -0,0 +1,108 @@
/*
**********************************************************************************************
* File: packet_io_util.cpp
* Description:
* Authors: Liu WenTan <liuwentan@geedgenetworks.com>
* Date: 2022-07-15
* Copyright: (c) 2018-2022 Geedge Networks, Inc. All rights reserved.
***********************************************************************************************
*/
#include <string.h>
#include "utils.h"
#include "packet_io_util.h"
static int packet_copy_data_offset(uint8_t *ptr, uint32_t offset, const uint8_t *data, uint32_t data_len)
{
memcpy(ptr + offset, data, data_len);
return 0;
}
int packet_copy_data(uint8_t *ptr, const uint8_t *pkt_data, uint32_t pkt_len)
{
return packet_copy_data_offset(ptr, 0, pkt_data, pkt_len);
}
void pio_packet_enqueue(struct pio_packet_queue *q, struct pio_packet *p)
{
if (nullptr == p)
return;
/* more packets in queue */
if (q->top != nullptr) {
p->prev = nullptr;
p->next = q->top;
q->top->prev = p;
q->top = p;
/* only packet */
} else {
p->prev = nullptr;
p->next = nullptr;
q->top = p;
q->bot = p;
}
q->len++;
}
struct pio_packet *pio_packet_dequeue(struct pio_packet_queue *q)
{
struct pio_packet *p = nullptr;
/* if the queue is empty there are no packets left. */
if (q->len == 0) {
return nullptr;
}
q->len--;
/* pull the bottom packet from the queue */
p = q->bot;
/* more packets in queue */
if (q->bot->prev != nullptr) {
q->bot = q->bot->prev;
q->bot->next = nullptr;
/* just the one we remove, so now empty */
} else {
q->top = nullptr;
q->bot = nullptr;
}
p->next = nullptr;
p->prev = nullptr;
return p;
}
void release_pio_packet_queue(struct pio_packet_queue *q)
{
if (nullptr == q) {
return;
}
while (q->len != 0) {
struct pio_packet *p = pio_packet_dequeue(q);
q->len--;
FREE(p);
}
}
int strncpy_safe(char *dst, const char *src, size_t dst_size)
{
if (nullptr == dst || nullptr == src || dst_size == 0) {
return -1;
}
size_t slen = strlen(src);
if (slen >= dst_size) {
strncpy(dst, src, dst_size);
dst[dst_size - 1] = '\0';
} else {
strcpy(dst, src);
dst[slen - 1] = '\0';
}
return 0;
}

View File

@@ -0,0 +1,86 @@
/*
**********************************************************************************************
* File: packet_io_util.h
* Description: packet_io internal utils
* Authors: Liu WenTan <liuwentan@geedgenetworks.com>
* Date: 2022-07-15
* Copyright: (c) 2018-2022 Geedge Networks, Inc. All rights reserved.
***********************************************************************************************
*/
#ifndef _PACKET_IO_UTIL_H_
#define _PACKET_IO_UTIL_H_
#ifdef __cpluscplus
extern "C"
{
#endif
#include <stdint.h>
#include <pthread.h>
#define PKT_QUEUE_MAX_NUM 256
#define CUSTOM_ZONE_LEN 64
#define ETHERNET_HEADER_LEN 14
#define DEFAULT_MTU 1500
#define DEFAULT_PACKET_SIZE (CUSTOM_ZONE_LEN + DEFAULT_MTU + ETHERNET_HEADER_LEN)
#define SIZE_OF_PIO_PACKET (DEFAULT_PACKET_SIZE + sizeof(struct pio_packet))
/**
* @brief pcap_live/pcap_file mode packet structure
*
* |<-pkt_hdr |<-pkt_payload
* | |
* | struct pio_packet | custom zone | L2 header | ...... |
* 64bytes 14bytes 1500bytes
*
* custom zone: user can set custom field
* pkt_payload: received packet data
*/
struct pio_packet {
/* pkt header pointer */
void *pkt_hdr;
/* pkt length */
uint32_t pkt_len;
/* pkt payload pointer */
void *pkt_payload;
/* reference counts */
uint32_t ref_cnt;
struct pio_packet *prev;
struct pio_packet *next;
};
struct pio_packet_queue {
struct pio_packet *top;
struct pio_packet *bot;
uint32_t len;
pthread_mutex_t mutex_q;
};
int packet_copy_data(uint8_t *ptr, const uint8_t *pkt_data, uint32_t pkt_len);
void pio_packet_enqueue(struct pio_packet_queue *, struct pio_packet *);
struct pio_packet *pio_packet_dequeue(struct pio_packet_queue *);
void release_pio_packet_queue(struct pio_packet_queue *);
/**
* @brief string copy safely,
*
* @retval -1(failed), 0(success)
*/
int strncpy_safe(char *dst, const char *src, size_t dst_size);
#ifdef __cpluscplus
}
#endif
#endif /* _PACKET_IO_UTIL_H_ */

View File

@@ -15,12 +15,12 @@
#include <pthread.h>
#include <sys/stat.h>
#include "utils.h"
#include "util_errors.h"
#include "logger.h"
#include "time_helper.h"
#include "pio_pcap_file.h"
#include "../packet_io.h"
#include "../../../sdk/include/utils.h"
#include "../../../sdk/include/util_errors.h"
#include "../../../sdk/include/logger.h"
#include "../../common/time_helper.h"
#include "packet_io.h"
/**
* @brief validate path is a valid plain file or directory

View File

@@ -21,8 +21,8 @@ extern "C"
#include <pcap/pcap.h>
#include <sys/queue.h>
#include "../../common/global_var.h"
#include "../../common/pio_packet_queue.h"
#include "global_var.h"
#include "packet_io_util.h"
struct pio_pcap_file_instance_context {

View File

@@ -12,11 +12,11 @@
#include <string.h>
#include <pthread.h>
#include "logger.h"
#include "utils.h"
#include "util_errors.h"
#include "pio_pcap_live.h"
#include "../packet_io.h"
#include "../../../sdk/include/logger.h"
#include "../../../sdk/include/utils.h"
#include "../../../sdk/include/util_errors.h"
#include "packet_io.h"
#define DEFAULT_MAX_PACKET_SIZE 65535
#define TIMEOUT_MS 500

View File

@@ -19,8 +19,8 @@ extern "C"
#include <stdint.h>
#include <pcap/pcap.h>
#include "../../common/global_var.h"
#include "../../common/pio_packet_queue.h"
#include "global_var.h"
#include "packet_io_util.h"
#define PCAP_STATE_UP 1
#define PCAP_STATE_DOWN 0