diff options
| author | liuwentan <[email protected]> | 2022-08-09 15:58:46 +0800 |
|---|---|---|
| committer | liuwentan <[email protected]> | 2022-08-09 15:58:46 +0800 |
| commit | c2d5b9cdb700da2ba2cca4105200248632f024d6 (patch) | |
| tree | a7745083e8e220134bc519896d1bcd8ee8ce7464 /src/packet_io | |
| parent | 7442cb09d560eaef36755965cb00f944b98c42cd (diff) | |
[PACKET_IO] add pio_packet structure for pcap_live/pcap_file mode
Diffstat (limited to 'src/packet_io')
| -rw-r--r-- | src/packet_io/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | src/packet_io/marsio_mode/pio_marsio.h | 2 | ||||
| -rw-r--r-- | src/packet_io/packet_io.cpp | 24 | ||||
| -rw-r--r-- | src/packet_io/packet_io.h | 2 | ||||
| -rw-r--r-- | src/packet_io/pcap_file_mode/pio_pcap_file.cpp | 29 | ||||
| -rw-r--r-- | src/packet_io/pcap_file_mode/pio_pcap_file.h | 7 | ||||
| -rw-r--r-- | src/packet_io/pcap_live_mode/pio_pcap_live.cpp | 36 | ||||
| -rw-r--r-- | src/packet_io/pcap_live_mode/pio_pcap_live.h | 4 | ||||
| -rw-r--r-- | src/packet_io/test/gtest_packet_io.cpp | 6 |
9 files changed, 72 insertions, 40 deletions
diff --git a/src/packet_io/CMakeLists.txt b/src/packet_io/CMakeLists.txt index 0e45228..85bc1dd 100644 --- a/src/packet_io/CMakeLists.txt +++ b/src/packet_io/CMakeLists.txt @@ -1,7 +1,7 @@ add_library(packet_io ../common/global_var.cpp - ../common/packet_queue.cpp + ../common/pio_packet_queue.cpp ../common/time_helper.cpp packet_io.cpp pcap_live_mode/pio_pcap_live.cpp diff --git a/src/packet_io/marsio_mode/pio_marsio.h b/src/packet_io/marsio_mode/pio_marsio.h index 9e77354..04a58f0 100644 --- a/src/packet_io/marsio_mode/pio_marsio.h +++ b/src/packet_io/marsio_mode/pio_marsio.h @@ -12,7 +12,7 @@ #include <stdint.h> -#include "marsio.h" +#include <marsio.h> /* * dll is short for dynamic link lib diff --git a/src/packet_io/packet_io.cpp b/src/packet_io/packet_io.cpp index e624834..a88fc20 100644 --- a/src/packet_io/packet_io.cpp +++ b/src/packet_io/packet_io.cpp @@ -63,6 +63,18 @@ struct pio_instance_operations pio_instance_ops_array[PACKET_IO_RUN_MODE_MAX] = struct packet_io_instance * packet_io_instance_create(const char *inst_name, const enum packet_io_run_mode mode, const int wrk_thread_num) { + if (nullptr == inst_name) { + return nullptr; + } + + if (mode < PACKET_IO_RUN_MODE_PCAP_FILE || mode >= PACKET_IO_RUN_MODE_MAX) { + return nullptr; + } + + if (wrk_thread_num < 0) { + return nullptr; + } + struct packet_io_instance *pio_instance = CALLOC(struct packet_io_instance, 1); if (nullptr == pio_instance) { log_error(ST_ERR_MEM_ALLOC, "packet_io instance alloc failed."); @@ -156,16 +168,14 @@ void packet_io_pkts_free(struct packet_io_instance *pinst, uint32_t qid, struct } -static int packet_copy_data_offset(struct packet *p, uint32_t offset, const uint8_t *data, uint32_t data_len) +static int packet_copy_data_offset(uint8_t *ptr, uint32_t offset, const uint8_t *data, uint32_t data_len) { - memcpy(GET_PKT_DIRECT_DATA(p) + offset, data, data_len); + memcpy(ptr + offset, data, data_len); return 0; } -int packet_copy_data(struct packet *p, const uint8_t *pkt_data, uint32_t pkt_len) -{ - SET_PKT_LEN(p, (size_t)pkt_len); - - return packet_copy_data_offset(p, 0, pkt_data, pkt_len); +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); }
\ No newline at end of file diff --git a/src/packet_io/packet_io.h b/src/packet_io/packet_io.h index 8f1f870..ed8bb6d 100644 --- a/src/packet_io/packet_io.h +++ b/src/packet_io/packet_io.h @@ -144,4 +144,4 @@ int packet_io_device_tx(struct packet_io_device *pdev, uint32_t txq_id, struct p **/ void packet_io_pkts_free(struct packet_io_device *pdev, uint32_t qid, struct packet **pkts, int nr_pkts); -int packet_copy_data(struct packet *p, const uint8_t *pkt_data, uint32_t pkt_len);
\ No newline at end of file +int packet_copy_data(uint8_t *ptr, const uint8_t *pkt_data, uint32_t pkt_len);
\ No newline at end of file diff --git a/src/packet_io/pcap_file_mode/pio_pcap_file.cpp b/src/packet_io/pcap_file_mode/pio_pcap_file.cpp index 0f7abec..2ef7fce 100644 --- a/src/packet_io/pcap_file_mode/pio_pcap_file.cpp +++ b/src/packet_io/pcap_file_mode/pio_pcap_file.cpp @@ -287,7 +287,7 @@ int pio_pcap_file_device_close(struct packet_io_device *pdev) for (uint32_t i = 0; i < PKT_QUEUE_MAX_NUM; i++) { if (pdev->entity.pcap_file_dev_ctx->pkt_queues[i].len != 0) { - release_packet_queue(&pdev->entity.pcap_file_dev_ctx->pkt_queues[i]); + release_pio_packet_queue(&pdev->entity.pcap_file_dev_ctx->pkt_queues[i]); } } @@ -299,12 +299,16 @@ int pio_pcap_file_device_close(struct packet_io_device *pdev) void pcap_file_pkt_callback_oneshot(char *user, struct pcap_pkthdr *pkt_hdr, u_char *pkt) { struct pio_pcap_file_device_context *pfile_dev_ctx = (struct pio_pcap_file_device_context *)user; - struct packet *p = CALLOC(struct packet, 1); + struct pio_packet *p = (struct pio_packet *)malloc(SIZE_OF_PIO_PACKET); if (nullptr == p) { return; } + memset(p, 0, SIZE_OF_PIO_PACKET); - if (packet_copy_data(p, pkt, pkt_hdr->caplen)) { + p->pkt_hdr = p; + p->pkt_payload = (uint8_t *)p + CUSTOM_ZONE_LEN; + p->pkt_len = pkt_hdr->caplen; + if (packet_copy_data((uint8_t *)p->pkt_payload, (uint8_t *)pkt, pkt_hdr->caplen)) { FREE(p); return; } @@ -314,10 +318,10 @@ void pcap_file_pkt_callback_oneshot(char *user, struct pcap_pkthdr *pkt_hdr, u_c hash_id = decode_packet(p) % nr_rxq; packet_enqueue(&pfile_dev_ctx->pkt_queues[hash_id], p); */ - /* + int rxq_id = 0; pthread_mutex_lock(&pfile_dev_ctx->pkt_queues[rxq_id].mutex_q); - packet_enqueue(&pfile_dev_ctx->pkt_queues[rxq_id], p); - pthread_mutex_unlock(&pfile_dev_ctx->pkt_queues[rxq_id].mutex_q); */ + pio_packet_enqueue(&pfile_dev_ctx->pkt_queues[rxq_id], p); + pthread_mutex_unlock(&pfile_dev_ctx->pkt_queues[rxq_id].mutex_q); } static int pcap_file_dispatch(struct pio_pcap_file_device_context *pfile_dev_ctx, uint32_t nr_rxq, uint32_t rxq_id, @@ -349,12 +353,12 @@ static int pcap_file_dispatch(struct pio_pcap_file_device_context *pfile_dev_ctx //TODO: close pcap file } else { // success - struct packet *p = nullptr; + struct pio_packet *p = nullptr; int i = 0; pthread_mutex_lock(&pfile_dev_ctx->pkt_queues[rxq_id].mutex_q); do { - p = packet_dequeue(&pfile_dev_ctx->pkt_queues[rxq_id]); - pkts[i] = p; + p = pio_packet_dequeue(&pfile_dev_ctx->pkt_queues[rxq_id]); + pkts[i] = (struct packet *)p; i++; } while (p != nullptr && (i < nr_pkts)); pthread_mutex_unlock(&pfile_dev_ctx->pkt_queues[rxq_id].mutex_q); @@ -603,9 +607,12 @@ int pio_pcap_file_device_send(struct packet_io_device *pdev, uint32_t txq_id, st return 0; } -void pio_pcap_file_device_pkt_free(struct packet_io_device *pdev, uint32_t qid, struct packet **pkts, int nr_pkts) +void pio_pcap_file_device_pkt_free(__unused struct packet_io_device *pdev, __unused uint32_t qid, struct packet **pkts, int nr_pkts) { - + for (uint32_t i = 0; i < nr_pkts; i++) { + struct pio_packet *p = (struct pio_packet *)pkts[i]; + FREE(p); + } } int pio_pcap_file_instance_create(struct packet_io_instance *pinst, __unused int wrk_thread_num) diff --git a/src/packet_io/pcap_file_mode/pio_pcap_file.h b/src/packet_io/pcap_file_mode/pio_pcap_file.h index c42733d..228ad91 100644 --- a/src/packet_io/pcap_file_mode/pio_pcap_file.h +++ b/src/packet_io/pcap_file_mode/pio_pcap_file.h @@ -13,9 +13,10 @@ #include <stdint.h> #include <dirent.h> #include <pcap/pcap.h> +#include <sys/queue.h> #include "../../common/global_var.h" -#include "../../common/packet_queue.h" +#include "../../common/pio_packet_queue.h" struct pio_pcap_file_instance_context { @@ -87,8 +88,8 @@ struct pio_pcap_file_device_context { bool is_dir; - /* rx packet queue */ - struct packet_queue pkt_queues[PKT_QUEUE_MAX_NUM]; + /* rx pio packet queue */ + struct pio_packet_queue pkt_queues[PKT_QUEUE_MAX_NUM]; struct pcap_file_shared_info shared; }; diff --git a/src/packet_io/pcap_live_mode/pio_pcap_live.cpp b/src/packet_io/pcap_live_mode/pio_pcap_live.cpp index eef9a70..e8b77cb 100644 --- a/src/packet_io/pcap_live_mode/pio_pcap_live.cpp +++ b/src/packet_io/pcap_live_mode/pio_pcap_live.cpp @@ -133,7 +133,7 @@ int pio_pcap_live_device_close(struct packet_io_device *pdev) for (uint32_t i = 0; i < PKT_QUEUE_MAX_NUM; i++) { if (pdev->entity.pcap_live_dev_ctx->pkt_queues[i].len != 0) { - release_packet_queue(&pdev->entity.pcap_live_dev_ctx->pkt_queues[i]); + release_pio_packet_queue(&pdev->entity.pcap_live_dev_ctx->pkt_queues[i]); } } @@ -145,12 +145,15 @@ int pio_pcap_live_device_close(struct packet_io_device *pdev) static void pcap_live_pkt_callback_oneshot(char *user, struct pcap_pkthdr *pkt_hdr, u_char *pkt) { struct pio_pcap_live_device_context *plive_dev_ctx = (struct pio_pcap_live_device_context *)user; - struct packet *p = CALLOC(struct packet, 1); + struct pio_packet *p = (struct pio_packet *)malloc(SIZE_OF_PIO_PACKET); if (nullptr == p) { return; } - - if (packet_copy_data(p, (uint8_t *)pkt, pkt_hdr->caplen)) { + memset(p, 0, sizeof(SIZE_OF_PIO_PACKET)); + p->pkt_hdr = p; + p->pkt_payload = (uint8_t *)p + CUSTOM_ZONE_LEN; + p->pkt_len = pkt_hdr->caplen; + if (packet_copy_data((uint8_t *)p->pkt_payload, (uint8_t *)pkt, pkt_hdr->caplen)) { FREE(p); return; } @@ -160,10 +163,10 @@ static void pcap_live_pkt_callback_oneshot(char *user, struct pcap_pkthdr *pkt_h hash_id = decode_packet(p) % nr_rxq; packet_enqueue(&pfile_dev_ctx->pkt_queues[hash_id], p); */ - /* + int rxq_id = 0; pthread_mutex_lock(&plive_dev_ctx->pkt_queues[rxq_id].mutex_q); - packet_enqueue(&plive_dev_ctx->pkt_queues[rxq_id], p); - pthread_mutex_unlock(&plive_dev_ctx->pkt_queues[rxq_id].mutex_q); */ + pio_packet_enqueue(&plive_dev_ctx->pkt_queues[rxq_id], p); + pthread_mutex_unlock(&plive_dev_ctx->pkt_queues[rxq_id].mutex_q); } int pio_pcap_live_device_receive(struct packet_io_device *pdev, uint32_t rxq_id, struct packet **pkts, int nr_pkts) @@ -187,12 +190,12 @@ int pio_pcap_live_device_receive(struct packet_io_device *pdev, uint32_t rxq_id, } else if (res == 0) { } else { - struct packet *p = nullptr; + struct pio_packet *p = nullptr; int i = 0; pthread_mutex_lock(&plive_dev_ctx->pkt_queues[rxq_id].mutex_q); do { - p = packet_dequeue(&plive_dev_ctx->pkt_queues[rxq_id]); - pkts[i] = p; + p = pio_packet_dequeue(&plive_dev_ctx->pkt_queues[rxq_id]); + pkts[i] = (struct packet *)p; i++; } while (p != nullptr && (i < nr_pkts)); pthread_mutex_unlock(&plive_dev_ctx->pkt_queues[rxq_id].mutex_q); @@ -220,16 +223,21 @@ int pio_pcap_live_device_send(struct packet_io_device *pdev, uint32_t txq_id, st int packet_q_len = nr_pkts; pthread_mutex_lock(&plive_dev_ctx->handle_mutex); for (uint32_t i = 0; i < nr_pkts; i++) { - res = pcap_sendpacket(plive_dev_ctx->pcap_handle, (u_char *)pkts[i], sizeof(struct packet)); + struct pio_packet *p = (struct pio_packet *)pkts[i]; + res = pcap_sendpacket(plive_dev_ctx->pcap_handle, (u_char *)p->pkt_payload, p->pkt_len); } pthread_mutex_unlock(&plive_dev_ctx->handle_mutex); - + /*TODO: when to free pio_packet? */ + return 0; } -void pio_pcap_live_device_pkt_free(struct packet_io_device *pdev, uint32_t qid, struct packet **pkts, int nr_pkts) +void pio_pcap_live_device_pkt_free(__unused struct packet_io_device *pdev, __unused uint32_t qid, struct packet **pkts, int nr_pkts) { - + for (uint32_t i = 0; i < nr_pkts; i++) { + struct pio_packet *p = (struct pio_packet *)pkts[i]; + FREE(p); + } } int pio_pcap_live_instance_create(struct packet_io_instance *pinst, __unused int wrk_thread_num) diff --git a/src/packet_io/pcap_live_mode/pio_pcap_live.h b/src/packet_io/pcap_live_mode/pio_pcap_live.h index 92e161f..d25be0a 100644 --- a/src/packet_io/pcap_live_mode/pio_pcap_live.h +++ b/src/packet_io/pcap_live_mode/pio_pcap_live.h @@ -14,7 +14,7 @@ #include <pcap/pcap.h> #include "../../common/global_var.h" -#include "../../common/packet_queue.h" +#include "../../common/pio_packet_queue.h" #define PCAP_STATE_UP 1 #define PCAP_STATE_DOWN 0 @@ -51,7 +51,7 @@ struct pio_pcap_live_device_context { int pcap_snaplen; /* rx packet queue */ - struct packet_queue pkt_queues[PKT_QUEUE_MAX_NUM]; + struct pio_packet_queue pkt_queues[PKT_QUEUE_MAX_NUM]; }; /* diff --git a/src/packet_io/test/gtest_packet_io.cpp b/src/packet_io/test/gtest_packet_io.cpp index 7ff08f7..31f3052 100644 --- a/src/packet_io/test/gtest_packet_io.cpp +++ b/src/packet_io/test/gtest_packet_io.cpp @@ -2,6 +2,12 @@ #include "../packet_io.h" +TEST(PACKET_IO_Test, packet_io_instance_create) { + struct packet_io_config ppio_config; + struct packet_io_instance *ppio_inst = packet_io_instance_create("stellar", PACKET_IO_RUN_MODE_PCAP_FILE, 2); + EXPECT_NE(ppio_inst, nullptr); +} + TEST(PACKET_IO_Test, packet_io_open_device) { struct packet_io_config ppio_config; struct packet_io_instance *ppio_inst = packet_io_instance_create("stellar", PACKET_IO_RUN_MODE_PCAP_FILE, 2); |
