diff --git a/include/stellar/packet.h b/include/stellar/packet.h index 8e9cf54..c20e1b5 100644 --- a/include/stellar/packet.h +++ b/include/stellar/packet.h @@ -136,21 +136,6 @@ int packet_get_tunnel_count(const struct packet *pkt); // return -1: failed int packet_get_tunnel_by_idx(const struct packet *pkt, int idx, struct tunnel *out); -/****************************************************************************** - * build - ******************************************************************************/ - -/* - * 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); - /****************************************************************************** * utils ******************************************************************************/ @@ -183,6 +168,7 @@ enum packet_type }; enum packet_type packet_get_type(const struct packet *pkt); +void packet_set_type(struct packet *pkt, enum packet_type type); void packet_set_action(struct packet *pkt, enum packet_action action); enum packet_action packet_get_action(const struct packet *pkt); @@ -228,6 +214,20 @@ typedef void on_packet_claimed_callback(struct packet *pkt, void *arg); int packet_manager_claim_packet(struct packet_manager *pkt_mgr, uint16_t thread_id, struct packet *pkt, on_packet_claimed_callback cb, void *arg); void packet_manager_schedule_packet(struct packet_manager *pkt_mgr, uint16_t thread_id, struct packet *pkt, enum packet_stage stage); +/* + * 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_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 *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 *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); + #ifdef __cplusplus } #endif 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 +#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__) diff --git a/infra/version.map b/infra/version.map index 385a2a2..8560564 100644 --- a/infra/version.map +++ b/infra/version.map @@ -5,6 +5,8 @@ global: packet_get_tunnel_count; packet_get_tunnel_by_idx; packet_prepend_sids; + packet_set_type; + packet_get_type; packet_get_direction; packet_set_action; packet_get_action; @@ -12,9 +14,6 @@ global: packet_get_raw_len; packet_get_payload_data; packet_get_payload_len; - packet_build_tcp; - packet_build_udp; - packet_build_l3; packet_manager_on_init; packet_manager_on_exit; packet_manager_on_thread_init; @@ -23,6 +22,9 @@ global: packet_manager_subscribe; packet_manager_claim_packet; packet_manager_schedule_packet; + packet_manager_build_tcp_packet; + packet_manager_build_udp_packet; + packet_manager_build_l3_packet; session_is_symmetric; session_has_duplicate_traffic;