From ce00a9543bb2a1b26a36f261559d99fa25942c9e Mon Sep 17 00:00:00 2001 From: Morcules Date: Thu, 6 Aug 2026 00:42:27 +0200 Subject: [PATCH] Cleaner lib --- src/cleanup_swiftnet.c | 2 +- src/execute_packet_callback.c | 8 ++-- src/generic_functions.c | 2 +- src/handle_packets.c | 18 ++++----- src/initialize_client_connection.c | 8 ++-- src/initialize_server.c | 6 +-- src/initialize_swiftnet.c | 12 +++--- src/internal/dpdk/dpdk_buffer.c | 2 +- src/internal/internal.h | 8 ++-- src/internal/pcap/pcap_buffer.c | 2 +- src/make_request.c | 2 +- src/make_response.c | 2 +- src/manipulate_debug_flags.c | 2 +- src/process_packets.c | 50 ++++++++++++------------ src/send_packet.c | 32 +++++++-------- src/swift_net.h | 62 ++++++++++++------------------ 16 files changed, 104 insertions(+), 114 deletions(-) diff --git a/src/cleanup_swiftnet.c b/src/cleanup_swiftnet.c index c14363b..fa2d37d 100644 --- a/src/cleanup_swiftnet.c +++ b/src/cleanup_swiftnet.c @@ -37,7 +37,7 @@ void swiftnet_cleanup() { allocator_destroy(&client_packet_data_memory_allocator ENABLE_INTERNAL_CHECK); allocator_destroy(&packet_buffer_memory_allocator ENABLE_INTERNAL_CHECK); - #ifdef SWIFT_NET_REQUESTS + #ifndef SWIFT_NET_DISABLE_REQUESTS allocator_destroy(&requests_sent_memory_allocator ENABLE_INTERNAL_CHECK); hashmap_destroy(&requests_sent); diff --git a/src/execute_packet_callback.c b/src/execute_packet_callback.c index c1b856f..5f2699b 100644 --- a/src/execute_packet_callback.c +++ b/src/execute_packet_callback.c @@ -7,8 +7,8 @@ #include #include -static struct PacketCallbackQueueNode* wait_for_next_packet_callback(struct PacketCallbackQueue* const packet_queue) { - struct PacketCallbackQueueNode* restrict node_to_process; +static struct SwiftNetPacketCallbackQueueNode* wait_for_next_packet_callback(struct SwiftNetPacketCallbackQueue* const packet_queue) { + struct SwiftNetPacketCallbackQueueNode* restrict node_to_process; LOCK_ATOMIC_DATA_TYPE(&packet_queue->locked); @@ -48,7 +48,7 @@ static inline void remove_pending_message_from_hashmap(struct SwiftNetHashMap* c } void execute_packet_callback( - struct PacketCallbackQueue* const queue, + struct SwiftNetPacketCallbackQueue* const queue, void (*const _Atomic * const packet_handler)(void *const, void *const), const enum ConnectionType connection_type, const _Atomic bool * const closing, @@ -61,7 +61,7 @@ void execute_packet_callback( ) { void (*packet_handler_loaded)(void *const, void *const); - struct PacketCallbackQueueNode* restrict current_node; + struct SwiftNetPacketCallbackQueueNode* restrict current_node; uint8_t idle_stage; diff --git a/src/generic_functions.c b/src/generic_functions.c index 74746d0..81d8458 100644 --- a/src/generic_functions.c +++ b/src/generic_functions.c @@ -8,7 +8,7 @@ // Set the handler for incoming packets/messages on the server or client static inline void swiftnet_validate_new_handler(void(*new_handler)(void)) { - #ifdef SWIFT_NET_ERROR + #ifndef SWIFT_NET_DISABLE_ERROR_CHECKING if(unlikely(new_handler == NULL)) { PRINT_ERROR("Error: Invalid arguments given"); exit(EXIT_FAILURE); diff --git a/src/handle_packets.c b/src/handle_packets.c index 30cdf2e..76e2452 100644 --- a/src/handle_packets.c +++ b/src/handle_packets.c @@ -12,8 +12,8 @@ #include static inline void insert_queue_node( - struct PacketQueueNode* restrict const new_node, - struct PacketQueue* const packet_queue + struct SwiftNetPacketQueueNode* restrict const new_node, + struct SwiftNetPacketQueue* const packet_queue ) { if(unlikely(new_node == NULL)) { return; @@ -38,8 +38,8 @@ static inline void insert_queue_node( return; } -static inline struct PacketQueueNode* construct_node(const uint32_t data_read, void* restrict const data, const uint32_t sender_address) { - struct PacketQueueNode* restrict node; +static inline struct SwiftNetPacketQueueNode* construct_node(const uint32_t data_read, void* restrict const data, const uint32_t sender_address) { + struct SwiftNetPacketQueueNode* restrict node; node = allocator_allocate(&packet_queue_node_memory_allocator); @@ -51,7 +51,7 @@ static inline struct PacketQueueNode* construct_node(const uint32_t data_read, v node_assign_values: - *node = (struct PacketQueueNode){ + *node = (struct SwiftNetPacketQueueNode){ .data = data, .data_read = data_read, .sender_address = (struct in_addr){.s_addr = sender_address}, @@ -66,7 +66,7 @@ static inline struct PacketQueueNode* construct_node(const uint32_t data_read, v } static inline void swiftnet_handle_packets( - struct PacketQueue* const packet_queue, + struct SwiftNetPacketQueue* const packet_queue, pthread_mutex_t* const process_packets_mtx, pthread_cond_t* const process_packets_cond, _Atomic bool *const processing_packets, @@ -165,7 +165,7 @@ static void handle_client_init(struct SwiftNetClientConnection* const client_con validate_packet_size: if(bytes_received != PACKET_HEADER_SIZE + sizeof(struct SwiftNetServerInformation) + prepend_size) { - #ifdef SWIFT_NET_DEBUG + #ifndef SWIFT_NET_DISABLE_DEBUGGING if (check_debug_flag(SWIFTNET_DEBUG_INITIALIZATION)) { send_debug_message("Invalid packet received from server. Expected server information: {\"bytes_received\": %u, \"expected_bytes\": %lu}\n", bytes_received, PACKET_HEADER_SIZE + sizeof(struct SwiftNetServerInformation)); } @@ -189,7 +189,7 @@ static void handle_client_init(struct SwiftNetClientConnection* const client_con server_information = (struct SwiftNetServerInformation*)(buffer + prepend_size+ sizeof(struct ip) + sizeof(struct SwiftNetPacketInfo)); if(packet_info->port_info.destination_port != client_connection->port_info.source_port || packet_info->port_info.source_port != client_connection->port_info.destination_port) { - #ifdef SWIFT_NET_DEBUG + #ifndef SWIFT_NET_DISABLE_DEBUGGING if (check_debug_flag(SWIFTNET_DEBUG_INITIALIZATION)) { send_debug_message("Port info does not match: {\"destination_port\": %d, \"source_port\": %d, \"source_ip_address\": \"%s\"}\n", packet_info->port_info.destination_port, packet_info->port_info.source_port, inet_ntoa(((struct ip*)(buffer + prepend_size))->ip_src)); } @@ -199,7 +199,7 @@ static void handle_client_init(struct SwiftNetClientConnection* const client_con } if(packet_info->packet_type != REQUEST_INFORMATION) { - #ifdef SWIFT_NET_DEBUG + #ifndef SWIFT_NET_DISABLE_DEBUGGING if (check_debug_flag(SWIFTNET_DEBUG_INITIALIZATION)) { send_debug_message("Invalid packet type: {\"packet_type\": %d}\n", packet_info->packet_type); } diff --git a/src/initialize_client_connection.c b/src/initialize_client_connection.c index 14b96fb..0951156 100644 --- a/src/initialize_client_connection.c +++ b/src/initialize_client_connection.c @@ -57,7 +57,7 @@ void* request_server_information(void* restrict const request_server_information goto exit; } - #ifdef SWIFT_NET_DEBUG + #ifndef SWIFT_NET_DISABLE_DEBUGGING if (check_debug_flag(SWIFTNET_DEBUG_INITIALIZATION)) { send_debug_message("Requested server information: {\"server_ip_address\": \"%s\"}\n", inet_ntoa(request_server_information_args->server_addr)); } @@ -97,7 +97,7 @@ static inline struct SwiftNetClientConnection* construct_client_connection(const .packets_completed = hashmap_create(&packet_completed_key_allocator), .packets_sending = hashmap_create(&uint16_memory_allocator), .pending_messages = hashmap_create(&pending_message_key_allocator), - .packet_queue = (struct PacketQueue){ + .packet_queue = (struct SwiftNetPacketQueue){ .first_node = NULL, .last_node = NULL }, @@ -112,7 +112,7 @@ static inline struct SwiftNetClientConnection* construct_client_connection(const atomic_store_explicit(&new_connection->initialized, false, memory_order_release); atomic_store_explicit(&new_connection->packet_handler_user_arg, NULL, memory_order_release); - memset(&new_connection->packet_callback_queue, 0x00, sizeof(struct PacketCallbackQueue)); + memset(&new_connection->packet_callback_queue, 0x00, sizeof(struct SwiftNetPacketCallbackQueue)); return new_connection; } @@ -212,7 +212,7 @@ struct SwiftNetClientConnection* swiftnet_create_client(const char* const ip_add pthread_cond_init(&new_connection->process_packets_cond, NULL); pthread_cond_init(&new_connection->execute_callback_cond, NULL); - #ifdef SWIFT_NET_DEBUG + #ifndef SWIFT_NET_DISABLE_DEBUGGING if (check_debug_flag(SWIFTNET_DEBUG_INITIALIZATION)) { send_debug_message("Successfully initialized client\n"); } diff --git a/src/initialize_server.c b/src/initialize_server.c index 70d258d..52726f7 100644 --- a/src/initialize_server.c +++ b/src/initialize_server.c @@ -27,13 +27,13 @@ static inline struct SwiftNetServer* construct_server(const bool loopback, const .eth_header = eth_header, .server_port = server_port, .loopback = loopback, - .packet_queue = (struct PacketQueue){ + .packet_queue = (struct SwiftNetPacketQueue){ .first_node = NULL, .last_node = NULL }, }; - memset(&new_server->packet_callback_queue, 0x00, sizeof(struct PacketCallbackQueue)); + memset(&new_server->packet_callback_queue, 0x00, sizeof(struct SwiftNetPacketCallbackQueue)); UNLOCK_ATOMIC_DATA_TYPE(&new_server->packet_queue.locked); UNLOCK_ATOMIC_DATA_TYPE(&new_server->packet_callback_queue.locked); @@ -79,7 +79,7 @@ struct SwiftNetServer* swiftnet_create_server(const uint16_t port, const bool lo pthread_cond_init(&new_server->process_packets_cond, NULL); pthread_cond_init(&new_server->execute_callback_cond, NULL); - #ifdef SWIFT_NET_DEBUG + #ifndef SWIFT_NET_DISABLE_DEBUGGING if (check_debug_flag(SWIFTNET_DEBUG_INITIALIZATION)) { send_debug_message("Successfully initialized server\n"); } diff --git a/src/initialize_swiftnet.c b/src/initialize_swiftnet.c index 28d5520..59dc24d 100644 --- a/src/initialize_swiftnet.c +++ b/src/initialize_swiftnet.c @@ -9,7 +9,7 @@ #include "internal/internal.h" #include -#ifdef SWIFT_NET_DEBUG +#ifndef SWIFT_NET_DISABLE_DEBUGGING struct SwiftNetDebugger debugger = {.flags = 0}; #endif @@ -38,7 +38,7 @@ struct SwiftNetMemoryAllocator uint16_memory_allocator; struct SwiftNetMemoryAllocator pending_message_key_allocator; struct SwiftNetMemoryAllocator packet_completed_key_allocator; -#ifdef SWIFT_NET_REQUESTS +#ifndef SWIFT_NET_DISABLE_REQUESTS struct SwiftNetMemoryAllocator requests_sent_memory_allocator; struct SwiftNetHashMap requests_sent; #endif @@ -50,8 +50,8 @@ pthread_t memory_cleanup_thread; _Atomic bool swiftnet_closing; static inline void initialize_allocators() { - packet_queue_node_memory_allocator = allocator_create(sizeof(struct PacketQueueNode), 40 * SWIFT_NET_MEMORY_USAGE); - packet_callback_queue_node_memory_allocator = allocator_create(sizeof(struct PacketCallbackQueueNode), 40 * SWIFT_NET_MEMORY_USAGE); + packet_queue_node_memory_allocator = allocator_create(sizeof(struct SwiftNetPacketQueueNode), 40 * SWIFT_NET_MEMORY_USAGE); + packet_callback_queue_node_memory_allocator = allocator_create(sizeof(struct SwiftNetPacketCallbackQueueNode), 40 * SWIFT_NET_MEMORY_USAGE); server_packet_data_memory_allocator = allocator_create(sizeof(struct SwiftNetServerPacketData), 40 * SWIFT_NET_MEMORY_USAGE); client_packet_data_memory_allocator = allocator_create(sizeof(struct SwiftNetClientPacketData), 40 * SWIFT_NET_MEMORY_USAGE); packet_buffer_memory_allocator = allocator_create(maximum_transmission_unit + sizeof(struct ether_header), 40 * SWIFT_NET_MEMORY_USAGE); @@ -63,13 +63,13 @@ static inline void initialize_allocators() { pending_message_key_allocator = allocator_create(sizeof(struct PendingMessagesKey), 0xFF * SWIFT_NET_MEMORY_USAGE); packet_completed_key_allocator = allocator_create(sizeof(struct PacketCompletedKey), 0xFF * SWIFT_NET_MEMORY_USAGE); - #ifdef SWIFT_NET_REQUESTS + #ifndef SWIFT_NET_DISABLE_REQUESTS requests_sent_memory_allocator = allocator_create(sizeof(struct RequestSent), 40 * SWIFT_NET_MEMORY_USAGE); #endif } static inline void initialize_vectors() { - #ifdef SWIFT_NET_REQUESTS + #ifndef SWIFT_NET_DISABLE_REQUESTS requests_sent = hashmap_create(&uint16_memory_allocator); #endif diff --git a/src/internal/dpdk/dpdk_buffer.c b/src/internal/dpdk/dpdk_buffer.c index 38a31ef..9de8970 100644 --- a/src/internal/dpdk/dpdk_buffer.c +++ b/src/internal/dpdk/dpdk_buffer.c @@ -240,7 +240,7 @@ static inline void validate_append_to_packet_args(const void* restrict const dat } void swiftnet_append_to_buffer(const void* restrict const data, const uint32_t data_size, struct SwiftNetPacketBuffer* restrict const buffer) { - #ifdef SWIFT_NET_ERROR + #ifndef SWIFT_NET_DISABLE_ERROR_CHECKING validate_append_to_packet_args(data, data_size); #endif diff --git a/src/internal/internal.h b/src/internal/internal.h index 76894a3..3bd614d 100644 --- a/src/internal/internal.h +++ b/src/internal/internal.h @@ -233,7 +233,7 @@ extern uint32_t bytes_leaked; extern uint32_t items_leaked; #endif -#ifdef SWIFT_NET_DEBUG +#ifndef SWIFT_NET_DISABLE_DEBUGGING extern struct SwiftNetDebugger debugger; static inline ALWAYS_INLINE bool check_debug_flag(const SwiftNetDebugFlags flag) { @@ -307,7 +307,7 @@ extern void* hashmap_get(const void* const key_data, const uint32_t data_size, s extern void* server_start_pcap(void* const server_void); extern void* client_start_pcap(void* const client_void); -#ifdef SWIFT_NET_REQUESTS +#ifndef SWIFT_NET_DISABLE_REQUESTS struct RequestSent { _Atomic(void*) packet_data; struct in_addr address; @@ -318,6 +318,8 @@ extern struct SwiftNetMemoryAllocator requests_sent_memory_allocator; extern struct SwiftNetHashMap requests_sent; #endif +extern uint16_t maximum_transmission_unit; + extern void swiftnet_send_packet( const uint16_t target_maximum_transmission_unit, const struct SwiftNetPortInfo port_info, @@ -328,7 +330,7 @@ extern void swiftnet_send_packet( struct SwiftNetMemoryAllocator* const packets_sending_memory_allocator, const struct ether_header eth_hdr, const struct SwiftNetNetworkData network_data - #ifdef SWIFT_NET_REQUESTS + #ifndef SWIFT_NET_DISABLE_REQUESTS , struct RequestSent* const request_sent , const bool response , const uint16_t request_packet_id diff --git a/src/internal/pcap/pcap_buffer.c b/src/internal/pcap/pcap_buffer.c index 51eb25b..6e4531b 100644 --- a/src/internal/pcap/pcap_buffer.c +++ b/src/internal/pcap/pcap_buffer.c @@ -82,7 +82,7 @@ static inline void append_data(uint8_t* restrict * restrict const append_pointer } void swiftnet_append_to_buffer(const void* restrict const data, const uint32_t data_size, struct SwiftNetPacketBuffer* restrict const buffer) { - #ifdef SWIFT_NET_ERROR + #ifndef SWIFT_NET_DISABLE_ERROR_CHECKING validate_append_to_packet_args(data, data_size); #endif diff --git a/src/make_request.c b/src/make_request.c index a06f03b..06c3c26 100644 --- a/src/make_request.c +++ b/src/make_request.c @@ -6,7 +6,7 @@ #include #include -#ifdef SWIFT_NET_REQUESTS +#ifndef SWIFT_NET_DISABLE_REQUESTS static inline void delete_request_sent(struct RequestSent* restrict const request_sent) { LOCK_ATOMIC_DATA_TYPE(&requests_sent.atomic_lock) diff --git a/src/make_response.c b/src/make_response.c index 990be5d..49b3455 100644 --- a/src/make_response.c +++ b/src/make_response.c @@ -1,7 +1,7 @@ #include "internal/internal.h" #include "swift_net.h" -#ifdef SWIFT_NET_REQUESTS +#ifndef SWIFT_NET_DISABLE_REQUESTS void swiftnet_client_make_response(struct SwiftNetClientConnection* const client, struct SwiftNetClientPacketData* const packet_data, struct SwiftNetPacketBuffer* restrict const buffer) { #ifdef SWIFT_NET_BACKEND_DPDK diff --git a/src/manipulate_debug_flags.c b/src/manipulate_debug_flags.c index b827a94..37e71fc 100644 --- a/src/manipulate_debug_flags.c +++ b/src/manipulate_debug_flags.c @@ -1,7 +1,7 @@ #include "internal/internal.h" #include "swift_net.h" -#ifdef SWIFT_NET_DEBUG +#ifndef SWIFT_NET_DISABLE_DEBUGGING void swiftnet_add_debug_flags(const SwiftNetDebugFlags flags) { debugger.flags |= flags; } diff --git a/src/process_packets.c b/src/process_packets.c index b515efd..465f1b1 100644 --- a/src/process_packets.c +++ b/src/process_packets.c @@ -124,7 +124,7 @@ static inline struct SwiftNetPendingMessage* get_pending_message(struct SwiftNet return pending_message; } -static inline void insert_callback_queue_node(struct PacketCallbackQueueNode* restrict const new_node, struct PacketCallbackQueue* const packet_queue) { +static inline void insert_callback_queue_node(struct SwiftNetPacketCallbackQueueNode* restrict const new_node, struct SwiftNetPacketCallbackQueue* const packet_queue) { if(unlikely(new_node == NULL)) return; LOCK_ATOMIC_DATA_TYPE(&packet_queue->locked); @@ -146,7 +146,7 @@ static inline void insert_callback_queue_node(struct PacketCallbackQueueNode* re return; } -#ifdef SWIFT_NET_REQUESTS +#ifndef SWIFT_NET_DISABLE_REQUESTS static inline void handle_request_response(uint16_t packet_id, const struct SwiftNetPendingMessage* restrict const pending_message, void* restrict const packet_data, struct SwiftNetHashMap* const pending_messages, const uint16_t source_port) { struct RequestSent* request_sent; @@ -183,12 +183,12 @@ static inline void handle_request_response(uint16_t packet_id, const struct Swif #endif -static inline void pass_callback_execution(void* const packet_data, struct PacketCallbackQueue* const queue, struct SwiftNetPendingMessage* restrict const pending_message, const uint16_t packet_id, pthread_mutex_t* const execute_callback_mtx, pthread_cond_t* const execute_callback_cond, _Atomic bool* const executing_packets) { - struct PacketCallbackQueueNode* node; +static inline void pass_callback_execution(void* const packet_data, struct SwiftNetPacketCallbackQueue* const queue, struct SwiftNetPendingMessage* restrict const pending_message, const uint16_t packet_id, pthread_mutex_t* const execute_callback_mtx, pthread_cond_t* const execute_callback_cond, _Atomic bool* const executing_packets) { + struct SwiftNetPacketCallbackQueueNode* node; node = allocator_allocate(&packet_callback_queue_node_memory_allocator); - *node = (struct PacketCallbackQueueNode){.packet_data = packet_data, .next = NULL, .pending_message = pending_message, .packet_id = packet_id}; + *node = (struct SwiftNetPacketCallbackQueueNode){.packet_data = packet_data, .next = NULL, .pending_message = pending_message, .packet_id = packet_id}; insert_callback_queue_node(node, queue); @@ -272,7 +272,7 @@ static inline struct SwiftNetPacketSending* get_packet_sending(struct SwiftNetHa } #ifndef DISABLE_DYNAMIC_RATE_LIMITING -static inline void signal_delay_change(const enum PacketDelayUpdateStatus status, const struct ip* restrict const ip_header, const uint16_t source_port, const uint16_t destination_port, const struct ether_header* const eth_hdr, const struct SwiftNetNetworkData* const net_data) { +static inline void signal_delay_change(const enum SwiftNetPacketDelayUpdateStatus status, const struct ip* restrict const ip_header, const uint16_t source_port, const uint16_t destination_port, const struct ether_header* const eth_hdr, const struct SwiftNetNetworkData* const net_data) { struct ip send_server_info_ip_header; struct SwiftNetPacketInfo packet_info_new; uint16_t prepend_size; @@ -283,7 +283,7 @@ static inline void signal_delay_change(const enum PacketDelayUpdateStatus status send_server_info_ip_header = construct_ip_header(ip_header->ip_src, PACKET_HEADER_SIZE + sizeof(status), ip_header->ip_id); packet_info_new = construct_packet_info( - sizeof(enum PacketDelayUpdateStatus), + sizeof(enum SwiftNetPacketDelayUpdateStatus), PACKET_DELAY_UPDATE, 1, 0, @@ -303,8 +303,8 @@ static inline void signal_delay_change(const enum PacketDelayUpdateStatus status } #endif -struct PacketQueueNode* wait_for_next_packet(struct PacketQueue* const packet_queue) { - struct PacketQueueNode* node_to_process; +struct SwiftNetPacketQueueNode* wait_for_next_packet(struct SwiftNetPacketQueue* const packet_queue) { + struct SwiftNetPacketQueueNode* node_to_process; LOCK_ATOMIC_DATA_TYPE(&packet_queue->locked); @@ -346,8 +346,8 @@ static inline void swiftnet_process_packets( struct SwiftNetHashMap* const packets_completed_history, struct SwiftNetMemoryAllocator* const packets_completed_history_memory_allocator, const enum ConnectionType connection_type, - struct PacketQueue* const packet_queue, - struct PacketCallbackQueue* const packet_callback_queue, + struct SwiftNetPacketQueue* const packet_queue, + struct SwiftNetPacketCallbackQueue* const packet_callback_queue, _Atomic bool* const closing, pthread_mutex_t* const process_packets_mtx, pthread_cond_t* const process_packets_cond, @@ -357,7 +357,7 @@ static inline void swiftnet_process_packets( _Atomic bool* const executing_packets ) { uint8_t idle_stage; - struct PacketQueueNode* node; + struct SwiftNetPacketQueueNode* node; uint8_t* packet_buffer; uint8_t* packet_data; struct ip ip_header; @@ -437,7 +437,7 @@ static inline void swiftnet_process_packets( if(memcmp(&ip_header.ip_src, &ip_header.ip_dst, sizeof(struct in_addr)) != 0 && is_private_ip(ip_header.ip_src) == false && is_private_ip(ip_header.ip_dst)) { if(ip_header.ip_sum != 0 && packet_corrupted(checksum_received, node->data_read, packet_buffer) == true) { - #ifdef SWIFT_NET_DEBUG + #ifndef SWIFT_NET_DISABLE_DEBUGGING if (check_debug_flag(SWIFTNET_DEBUG_PACKETS_RECEIVING)) { send_debug_message("Received corrupted packet: {\"source_ip_address\": \"%s\", \"source_port\": %d, \"packet_id\": %d, \"received_checsum\": %d, \"real_checksum\": %d}\n", inet_ntoa(ip_header.ip_src), packet_info.port_info.source_port, ip_header.ip_id, checksum_received, crc32(packet_buffer, node->data_read)); } @@ -449,7 +449,7 @@ static inline void swiftnet_process_packets( } } - #ifdef SWIFT_NET_DEBUG + #ifndef SWIFT_NET_DISABLE_DEBUGGING if (check_debug_flag(SWIFTNET_DEBUG_PACKETS_RECEIVING)) { send_debug_message("Received packet: {\"source_ip_address\": \"%s\", \"source_port\": %d, \"packet_id\": %d, \"packet_type\": %d, \"packet_length\": %d, \"chunk_index\": %d, \"connection_type\": %d, \"checksum_received\": %d, \"real_checksum\": %d}\n", inet_ntoa(ip_header.ip_src), packet_info.port_info.source_port, ip_header.ip_id, packet_info.packet_type, packet_info.packet_length, packet_info.chunk_index, connection_type, checksum_received, crc32(node->data, node->data_read)); } @@ -637,12 +637,12 @@ static inline void swiftnet_process_packets( #ifndef DISABLE_DYNAMIC_RATE_LIMITING case PACKET_DELAY_UPDATE: { - enum PacketDelayUpdateStatus* status; + enum SwiftNetPacketDelayUpdateStatus* status; struct SwiftNetPacketSending* target_packet_sending; uint32_t current_delay; - status = (enum PacketDelayUpdateStatus*)packet_data; + status = (enum SwiftNetPacketDelayUpdateStatus*)packet_data; target_packet_sending = get_packet_sending(packets_sending, ip_header.ip_id); @@ -716,12 +716,12 @@ static inline void swiftnet_process_packets( .sender = sender, .data_length = packet_info.packet_length, .packet_id = ip_header.ip_id - #ifdef SWIFT_NET_REQUESTS + #ifndef SWIFT_NET_DISABLE_REQUESTS , .expecting_response = packet_info.packet_type == REQUEST #endif }; - #ifdef SWIFT_NET_REQUESTS + #ifndef SWIFT_NET_DISABLE_REQUESTS if (packet_info.packet_type == RESPONSE) { handle_request_response(ip_header.ip_id, NULL, new_packet_data, pending_messages, packet_info.port_info.source_port); } else { @@ -742,12 +742,12 @@ static inline void swiftnet_process_packets( .port_info = packet_info.port_info, .data_length = packet_info.packet_length, .packet_id = ip_header.ip_id - #ifdef SWIFT_NET_REQUESTS + #ifndef SWIFT_NET_DISABLE_REQUESTS , .expecting_response = packet_info.packet_type == REQUEST #endif }; - #ifdef SWIFT_NET_REQUESTS + #ifndef SWIFT_NET_DISABLE_REQUESTS if (packet_info.packet_type == RESPONSE) { handle_request_response(ip_header.ip_id, NULL, new_packet_data, pending_messages, packet_info.port_info.source_port); } else { @@ -784,7 +784,7 @@ static inline void swiftnet_process_packets( chunk_received(pending_message->chunks_received, packet_info.chunk_index); - #ifdef SWIFT_NET_DEBUG + #ifndef SWIFT_NET_DISABLE_DEBUGGING { uint32_t lost_chunks_buffer[chunk_data_size]; uint32_t lost_chunks_num; @@ -820,12 +820,12 @@ static inline void swiftnet_process_packets( .sender = sender, .data_length = packet_info.packet_length, .packet_id = ip_header.ip_id - #ifdef SWIFT_NET_REQUESTS + #ifndef SWIFT_NET_DISABLE_REQUESTS , .expecting_response = packet_info.packet_type == REQUEST #endif }; - #ifdef SWIFT_NET_REQUESTS + #ifndef SWIFT_NET_DISABLE_REQUESTS if (packet_info.packet_type == RESPONSE) { handle_request_response(ip_header.ip_id, pending_message, server_packet_data, pending_messages, packet_info.port_info.source_port); } else { @@ -849,12 +849,12 @@ static inline void swiftnet_process_packets( .port_info = packet_info.port_info, .data_length = packet_info.packet_length, .packet_id = ip_header.ip_id - #ifdef SWIFT_NET_REQUESTS + #ifndef SWIFT_NET_DISABLE_REQUESTS , .expecting_response = packet_info.packet_type == REQUEST #endif }; - #ifdef SWIFT_NET_REQUESTS + #ifndef SWIFT_NET_DISABLE_REQUESTS if (packet_info.packet_type == RESPONSE) { handle_request_response(ip_header.ip_id, pending_message, client_packet_data, pending_messages, packet_info.port_info.source_port); } else { diff --git a/src/send_packet.c b/src/send_packet.c index 4478cf1..21e05df 100644 --- a/src/send_packet.c +++ b/src/send_packet.c @@ -27,7 +27,7 @@ static inline enum RequestLostPacketsReturnType request_lost_packets_bitarray(co #endif ) { uint8_t times_checked; - enum PacketSendingUpdated status; + enum SwiftNetPacketSendingUpdated status; goto request_lost_packets; @@ -73,7 +73,7 @@ static inline ALWAYS_INLINE void handle_lost_packets( const uint32_t packet_length, PCAP_ONLY const uint16_t mtu, PCAP_ONLY uint32_t chunk_amount - #ifdef SWIFT_NET_REQUESTS + #ifndef SWIFT_NET_DISABLE_REQUESTS , PCAP_ONLY const uint8_t packet_type #endif ) { @@ -113,7 +113,7 @@ static inline ALWAYS_INLINE void handle_lost_packets( #ifdef SWIFT_NET_BACKEND_PCAP resend_chunk_packet_info = construct_packet_info( packet_length, - #ifdef SWIFT_NET_REQUESTS + #ifndef SWIFT_NET_DISABLE_REQUESTS packet_type, #else MESSAGE, @@ -263,7 +263,7 @@ void swiftnet_send_packet( struct SwiftNetMemoryAllocator* const packets_sending_memory_allocator, const struct ether_header eth_hdr, const struct SwiftNetNetworkData network_data - #ifdef SWIFT_NET_REQUESTS + #ifndef SWIFT_NET_DISABLE_REQUESTS , struct RequestSent* const request_sent , const bool response , const uint16_t request_packet_id @@ -272,20 +272,20 @@ void swiftnet_send_packet( uint16_t mtu; uint32_t chunk_amount; uint16_t packet_id; - #ifdef SWIFT_NET_REQUESTS + #ifndef SWIFT_NET_DISABLE_REQUESTS uint8_t packet_type; #endif mtu = MIN(target_maximum_transmission_unit, maximum_transmission_unit); - #ifdef SWIFT_NET_DEBUG + #ifndef SWIFT_NET_DISABLE_DEBUGGING if (check_debug_flag(SWIFTNET_DEBUG_PACKETS_SENDING)) { send_debug_message("Sending packet: {\"destination_ip_address\": \"%s\", \"destination_port\": %d, \"packet_length\": %d}\n", inet_ntoa(*target_addr), port_info.destination_port, packet_length); } #endif - #ifdef SWIFT_NET_REQUESTS + #ifndef SWIFT_NET_DISABLE_REQUESTS if (response == true) { packet_id = request_packet_id; } else { @@ -311,7 +311,7 @@ void swiftnet_send_packet( packet_id = (uint16_t)rand(); #endif - #ifdef SWIFT_NET_REQUESTS + #ifndef SWIFT_NET_DISABLE_REQUESTS packet_type = response ? RESPONSE : request_sent == NULL ? MESSAGE : REQUEST; #endif @@ -337,7 +337,7 @@ void swiftnet_send_packet( packet_info = construct_packet_info( packet_length, - #ifdef SWIFT_NET_REQUESTS + #ifndef SWIFT_NET_DISABLE_REQUESTS packet_type, #else MESSAGE, @@ -386,7 +386,7 @@ void swiftnet_send_packet( for(uint32_t i = 0; ; i++) { current_offset = i * (mtu - PACKET_HEADER_SIZE); - #ifdef SWIFT_NET_DEBUG + #ifndef SWIFT_NET_DISABLE_DEBUGGING if (check_debug_flag(SWIFTNET_DEBUG_PACKETS_SENDING)) { send_debug_message("Sent chunk: {\"chunk_index\": %d}\n", i); } @@ -416,7 +416,7 @@ void swiftnet_send_packet( memcpy(buffer_header_location, temp_data_buffer, prepend_size + PACKET_HEADER_SIZE); handle_lost_packets(new_packet_sending, packet, eth_hdr, target_addr, port_info.source_port, port_info.destination_port, packets_sending_memory_allocator, packets_sending, &network_data, packet_length, mtu, chunk_amount - #ifdef SWIFT_NET_REQUESTS + #ifndef SWIFT_NET_DISABLE_REQUESTS , packet_type #endif ); @@ -442,7 +442,7 @@ void swiftnet_send_packet( current_offset = i * packet->data_len_per_packet; - #ifdef SWIFT_NET_DEBUG + #ifndef SWIFT_NET_DISABLE_DEBUGGING if (check_debug_flag(SWIFTNET_DEBUG_PACKETS_SENDING)) { send_debug_message("Sent chunk: {\"chunk_index\": %d}\n", i); } @@ -480,7 +480,7 @@ void swiftnet_send_packet( current_buffer->pkt_len = old_len; handle_lost_packets(new_packet_sending, packet, eth_hdr, target_addr, port_info.source_port, port_info.destination_port, packets_sending_memory_allocator, packets_sending, &network_data, packet_length, mtu, chunk_amount - #ifdef SWIFT_NET_REQUESTS + #ifndef SWIFT_NET_DISABLE_REQUESTS , packet_type #endif ); @@ -512,7 +512,7 @@ void swiftnet_send_packet( packet_info = construct_packet_info( packet_length, - #ifdef SWIFT_NET_REQUESTS + #ifndef SWIFT_NET_DISABLE_REQUESTS packet_type, #else MESSAGE, @@ -579,7 +579,7 @@ void swiftnet_send_packet( void swiftnet_client_send_packet(struct SwiftNetClientConnection* const client, struct SwiftNetPacketBuffer* restrict const packet, const uint32_t bytes_to_send) { swiftnet_send_packet(client->maximum_transmission_unit, client->port_info, packet, bytes_to_send, &client->server_addr, &client->packets_sending, &client->packets_sending_memory_allocator, client->eth_header, client->network_data - #ifdef SWIFT_NET_REQUESTS + #ifndef SWIFT_NET_DISABLE_REQUESTS , NULL, false, 0 #endif ); @@ -598,7 +598,7 @@ void swiftnet_server_send_packet(struct SwiftNetServer* const server, struct Swi memcpy(ð_hdr.ether_dhost, &target.mac_address, sizeof(eth_hdr.ether_dhost)); swiftnet_send_packet(target.maximum_transmission_unit, port_info, packet, bytes_to_send, &target.sender_address, &server->packets_sending, &server->packets_sending_memory_allocator, eth_hdr, server->network_data - #ifdef SWIFT_NET_REQUESTS + #ifndef SWIFT_NET_DISABLE_REQUESTS , NULL, false, 0 #endif ); diff --git a/src/swift_net.h b/src/swift_net.h index 7d74238..466fe0d 100644 --- a/src/swift_net.h +++ b/src/swift_net.h @@ -26,23 +26,13 @@ extern "C" { #define restrict __restrict__ #endif -#ifndef SWIFT_NET_DISABLE_ERROR_CHECKING -#define SWIFT_NET_ERROR -#endif - -#ifndef SWIFT_NET_DISABLE_REQUESTS -#define SWIFT_NET_REQUESTS -#endif - -#ifndef SWIFT_NET_DISABLE_DEBUGGING -#define SWIFT_NET_DEBUG -#endif - #define SWIFT_NET_ALIGNED(bytes) __attribute__((aligned(bytes))) // Multiplication of memory pre allocated. // More memory = better performance +#ifndef SWIFT_NET_MEMORY_USAGE #define SWIFT_NET_MEMORY_USAGE 5 +#endif struct SwiftNetNetworkData { #ifdef SWIFT_NET_BACKEND_DPDK @@ -59,7 +49,7 @@ struct SwiftNetNetworkData { #endif } SWIFT_NET_ALIGNED(8); -enum PacketType { +enum PacketType : uint8_t { MESSAGE = 0x01, REQUEST_INFORMATION = 0x02, SEND_LOST_PACKETS_REQUEST = 0x03, @@ -68,14 +58,14 @@ enum PacketType { #ifndef DISABLE_DYNAMIC_RATE_LIMITING PACKET_DELAY_UPDATE = 0x06, #endif - #ifdef SWIFT_NET_REQUESTS + #ifndef SWIFT_NET_DISABLE_REQUESTS REQUEST = 0x07, RESPONSE = 0x08, #endif }; #ifndef DISABLE_DYNAMIC_RATE_LIMITING -enum PacketDelayUpdateStatus { +enum SwiftNetPacketDelayUpdateStatus : uint8_t { LOWER_DELAY, INCREASE_DELAY }; @@ -88,8 +78,6 @@ enum PacketDelayUpdateStatus { #define likely(x) __builtin_expect((x), 0x01) #endif -extern uint16_t maximum_transmission_unit; - #define SWIFTNET_DEBUG_FLAGS(num) ((SwiftNetDebugFlags)(num)) typedef uint8_t SwiftNetDebugFlags; @@ -119,7 +107,7 @@ struct SwiftNetPacketClientMetadata { uint32_t data_length; struct SwiftNetPortInfo port_info; uint16_t packet_id; - #ifdef SWIFT_NET_REQUESTS + #ifndef SWIFT_NET_DISABLE_REQUESTS bool expecting_response; #endif } SWIFT_NET_ALIGNED(4); @@ -154,7 +142,7 @@ struct SwiftNetPacketServerMetadata { uint32_t data_length; struct SwiftNetPortInfo port_info; uint16_t packet_id; - #ifdef SWIFT_NET_REQUESTS + #ifndef SWIFT_NET_DISABLE_REQUESTS bool expecting_response; #endif } SWIFT_NET_ALIGNED(4); @@ -163,7 +151,7 @@ struct SwiftNetServerInformation { uint16_t maximum_transmission_unit; } SWIFT_NET_ALIGNED(4); -enum PacketSendingUpdated { +enum SwiftNetPacketSendingUpdated : uint8_t { NO_UPDATE, UPDATED_LOST_CHUNKS, SUCCESSFULLY_RECEIVED @@ -171,7 +159,7 @@ enum PacketSendingUpdated { struct SwiftNetPacketSending { uint32_t* lost_chunks; - _Atomic enum PacketSendingUpdated updated; + _Atomic enum SwiftNetPacketSendingUpdated updated; #ifndef DISABLE_DYNAMIC_RATE_LIMITING _Atomic uint32_t current_send_delay; #endif @@ -199,22 +187,22 @@ struct SwiftNetPacketBuffer { #endif } SWIFT_NET_ALIGNED(8); -struct PacketQueueNode { - struct PacketQueueNode* next; +struct SwiftNetPacketQueueNode { + struct SwiftNetPacketQueueNode* next; uint8_t* data; uint32_t data_read; struct in_addr sender_address; } SWIFT_NET_ALIGNED(8); -struct PacketQueue { - struct PacketQueueNode* first_node; - struct PacketQueueNode* last_node; +struct SwiftNetPacketQueue { + struct SwiftNetPacketQueueNode* first_node; + struct SwiftNetPacketQueueNode* last_node; _Atomic bool locked; } SWIFT_NET_ALIGNED(8); -struct PacketCallbackQueueNode { +struct SwiftNetPacketCallbackQueueNode { struct SwiftNetPendingMessage* pending_message; - struct PacketCallbackQueueNode* next; + struct SwiftNetPacketCallbackQueueNode* next; void* packet_data; uint16_t packet_id; } SWIFT_NET_ALIGNED(8); @@ -233,9 +221,9 @@ struct SwiftNetClientPacketData { struct SwiftNetPacketClientMetadata metadata; } SWIFT_NET_ALIGNED(8); -struct PacketCallbackQueue { - struct PacketCallbackQueueNode* first_node; - struct PacketCallbackQueueNode* last_node; +struct SwiftNetPacketCallbackQueue { + struct SwiftNetPacketCallbackQueueNode* first_node; + struct SwiftNetPacketCallbackQueueNode* last_node; _Atomic bool locked; } SWIFT_NET_ALIGNED(8); @@ -304,8 +292,8 @@ struct SwiftNetClientConnection { struct SwiftNetMemoryAllocator pending_messages_memory_allocator; struct SwiftNetMemoryAllocator packets_sending_memory_allocator; struct SwiftNetNetworkData network_data; - struct PacketQueue packet_queue; - struct PacketCallbackQueue packet_callback_queue; + struct SwiftNetPacketQueue packet_queue; + struct SwiftNetPacketCallbackQueue packet_callback_queue; _Atomic(void (*)(struct SwiftNetClientPacketData* const, void* const user)) packet_handler; _Atomic(void*) packet_handler_user_arg; pthread_mutex_t process_packets_mtx; @@ -333,8 +321,8 @@ struct SwiftNetServer { struct SwiftNetMemoryAllocator pending_messages_memory_allocator; struct SwiftNetMemoryAllocator packets_sending_memory_allocator; struct SwiftNetNetworkData network_data; - struct PacketQueue packet_queue; - struct PacketCallbackQueue packet_callback_queue; + struct SwiftNetPacketQueue packet_queue; + struct SwiftNetPacketCallbackQueue packet_callback_queue; _Atomic(void (*)(struct SwiftNetServerPacketData* const, void* const user)) packet_handler; _Atomic(void*) packet_handler_user_arg; pthread_mutex_t process_packets_mtx; @@ -451,7 +439,7 @@ extern void swiftnet_server_destroy_packet_data( // Clean up the entire SwiftNet library. extern void swiftnet_cleanup(); -#ifdef SWIFT_NET_REQUESTS +#ifndef SWIFT_NET_DISABLE_REQUESTS // Make a request from a client and wait for a response. extern struct SwiftNetClientPacketData* swiftnet_client_make_request( @@ -483,7 +471,7 @@ extern void swiftnet_server_make_response( ); #endif -#ifdef SWIFT_NET_DEBUG +#ifndef SWIFT_NET_DISABLE_DEBUGGING // Adds one or more debug flags to the global debugger state. extern void swiftnet_add_debug_flags(const SwiftNetDebugFlags flags); // Removes one or more debug flags from the global debugger state.