summaryrefslogtreecommitdiff
path: root/infra/packet_manager
diff options
context:
space:
mode:
authorluwenpeng <[email protected]>2024-11-18 15:24:38 +0800
committerluwenpeng <[email protected]>2024-11-18 15:24:38 +0800
commita473c2922dbb58a30eacf7f70697ed3108397ebb (patch)
tree5605dd1759c674297a306b878ef652661d974774 /infra/packet_manager
parent492a7fb8ead4d9a3f36a2e773041eb285dc34a95 (diff)
feature: packet manager support build packet
Diffstat (limited to 'infra/packet_manager')
-rw-r--r--infra/packet_manager/packet_builder.h23
-rw-r--r--infra/packet_manager/packet_dabloom.h2
-rw-r--r--infra/packet_manager/packet_internal.h1
-rw-r--r--infra/packet_manager/packet_manager.c55
-rw-r--r--infra/packet_manager/packet_pool.h2
-rw-r--r--infra/packet_manager/test/gtest_packet_builder.cpp1
6 files changed, 81 insertions, 3 deletions
diff --git a/infra/packet_manager/packet_builder.h b/infra/packet_manager/packet_builder.h
new file mode 100644
index 0000000..e05e140
--- /dev/null
+++ b/infra/packet_manager/packet_builder.h
@@ -0,0 +1,23 @@
+#pragma once
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+#include "stellar/packet.h"
+
+/*
+ * tcp_seq: the sequence number of the new TCP packet (in host byte order)
+ * tcp_ack: the acknowledgment number of the new TCP packet (in host byte order)
+ * tcp_options_len: the length of the options (must be a multiple of 4)
+ */
+struct packet *packet_build_tcp(const struct packet *origin_pkt, uint32_t tcp_seq, uint32_t tcp_ack, uint8_t tcp_flags,
+ const char *tcp_options, uint16_t tcp_options_len,
+ const char *tcp_payload, uint16_t tcp_payload_len);
+struct packet *packet_build_udp(const struct packet *origin_pkt, const char *udp_payload, uint16_t udp_payload_len);
+struct packet *packet_build_l3(const struct packet *origin_pkt, uint8_t ip_proto, const char *l3_payload, uint16_t l3_payload_len);
+
+#ifdef __cplusplus
+}
+#endif
diff --git a/infra/packet_manager/packet_dabloom.h b/infra/packet_manager/packet_dabloom.h
index f19f78e..67f7624 100644
--- a/infra/packet_manager/packet_dabloom.h
+++ b/infra/packet_manager/packet_dabloom.h
@@ -5,7 +5,7 @@ extern "C"
{
#endif
-struct packet;
+#include "stellar/packet.h"
// Duplicated Packet Filter for IPv4 Packet
struct packet_dabloom;
diff --git a/infra/packet_manager/packet_internal.h b/infra/packet_manager/packet_internal.h
index d5903dc..8e6c5f5 100644
--- a/infra/packet_manager/packet_internal.h
+++ b/infra/packet_manager/packet_internal.h
@@ -127,7 +127,6 @@ uint16_t packet_get_link_id(const struct packet *pkt);
void packet_set_claim(struct packet *pkt, bool claim);
bool packet_is_claim(const struct packet *pkt);
-void packet_set_type(struct packet *pkt, enum packet_type type);
void packet_set_direction(struct packet *pkt, enum packet_direction dir);
void *packet_get_user_data(struct packet *pkt);
diff --git a/infra/packet_manager/packet_manager.c b/infra/packet_manager/packet_manager.c
index 46153cb..d615f64 100644
--- a/infra/packet_manager/packet_manager.c
+++ b/infra/packet_manager/packet_manager.c
@@ -3,6 +3,7 @@
#include "utils_internal.h"
#include "packet_internal.h"
#include "packet_manager.h"
+#include "packet_builder.h"
#include "fieldstat/fieldstat_easy.h"
#define PACKET_MANAGER_LOG_ERROR(format, ...) STELLAR_LOG_ERROR(__thread_local_logger, "packet manager", format, ##__VA_ARGS__)
@@ -430,6 +431,60 @@ void packet_manager_print_stat(struct packet_manager *pkt_mgr, uint16_t thread_i
}
}
+struct packet *packet_manager_build_tcp_packet(struct packet_manager *pkt_mgr, const struct packet *origin_pkt, uint32_t tcp_seq, uint32_t tcp_ack, uint8_t tcp_flags,
+ const char *tcp_options, uint16_t tcp_options_len, const char *tcp_payload, uint16_t tcp_payload_len)
+{
+ struct packet *pkt = packet_build_tcp(origin_pkt, tcp_seq, tcp_ack, tcp_flags, tcp_options, tcp_options_len, tcp_payload, tcp_payload_len);
+ if (pkt == NULL)
+ {
+ return NULL;
+ }
+
+ struct exdata_runtime *ex_rte = exdata_runtime_new(pkt_mgr->sche->ex_sche);
+ packet_set_user_data(pkt, ex_rte);
+
+ return pkt;
+}
+
+struct packet *packet_manager_build_udp_packet(struct packet_manager *pkt_mgr, const struct packet *origin_pkt, const char *udp_payload, uint16_t udp_payload_len)
+{
+ struct packet *pkt = packet_build_udp(origin_pkt, udp_payload, udp_payload_len);
+ if (pkt == NULL)
+ {
+ return NULL;
+ }
+
+ struct exdata_runtime *ex_rte = exdata_runtime_new(pkt_mgr->sche->ex_sche);
+ packet_set_user_data(pkt, ex_rte);
+
+ return pkt;
+}
+
+struct packet *packet_manager_build_l3_packet(struct packet_manager *pkt_mgr, const struct packet *origin_pkt, uint8_t ip_proto, const char *l3_payload, uint16_t l3_payload_len)
+{
+ struct packet *pkt = packet_build_l3(origin_pkt, ip_proto, l3_payload, l3_payload_len);
+ if (pkt == NULL)
+ {
+ return NULL;
+ }
+
+ struct exdata_runtime *ex_rte = exdata_runtime_new(pkt_mgr->sche->ex_sche);
+ packet_set_user_data(pkt, ex_rte);
+
+ return pkt;
+}
+
+void packet_manager_free_packet(struct packet_manager *pkt_mgr __attribute__((unused)), struct packet *pkt)
+{
+ if (pkt)
+ {
+ struct exdata_runtime *ex_rte = packet_get_user_data(pkt);
+ exdata_runtime_free(ex_rte);
+
+ packet_free(pkt);
+ }
+}
+
/******************************************************************************
* packet manager module
******************************************************************************/
diff --git a/infra/packet_manager/packet_pool.h b/infra/packet_manager/packet_pool.h
index 422ace4..fd5424d 100644
--- a/infra/packet_manager/packet_pool.h
+++ b/infra/packet_manager/packet_pool.h
@@ -5,7 +5,7 @@ extern "C"
{
#endif
-#include <stdint.h>
+#include "stellar/packet.h"
struct packet_pool;
struct packet_pool *packet_pool_new(uint64_t capacity);
diff --git a/infra/packet_manager/test/gtest_packet_builder.cpp b/infra/packet_manager/test/gtest_packet_builder.cpp
index 18c8aac..b7e7686 100644
--- a/infra/packet_manager/test/gtest_packet_builder.cpp
+++ b/infra/packet_manager/test/gtest_packet_builder.cpp
@@ -6,6 +6,7 @@
#include "packet_internal.h"
#include "packet_dump.h"
#include "packet_parser.h"
+#include "packet_builder.h"
#define PRINT_GREEN(fmt, ...) printf("\033[0;32m" fmt "\033[0m\n", ##__VA_ARGS__)
#define PRINT_RED(fmt, ...) printf("\033[0;31m" fmt "\033[0m\n", ##__VA_ARGS__)