Skip to content

Commit 80e4e8f

Browse files
authored
Merge pull request #188 from Morcules/improve-scalability
Added source port to pending messages and packets completed
2 parents a48ed1a + 9c7bdfd commit 80e4e8f

8 files changed

Lines changed: 78 additions & 28 deletions

src/cleanup_swiftnet.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ void swiftnet_cleanup() {
5252

5353
allocator_destroy(&listener_memory_allocator);
5454
allocator_destroy(&uint16_memory_allocator);
55+
allocator_destroy(&packet_completed_key_allocator);
56+
allocator_destroy(&pending_message_key_allocator);
5557

5658
#ifdef SWIFT_NET_INTERNAL_TESTING
5759
printf("Bytes leaked: %d\nItems leaked: %d\n", bytes_leaked, items_leaked);

src/execute_packet_callback.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,13 @@ static struct PacketCallbackQueueNode* const wait_for_next_packet_callback(struc
3535

3636
static inline void remove_pending_message_from_hashmap(struct SwiftNetHashMap* const pending_messages, struct SwiftNetPendingMessage* const pending_message) {
3737
LOCK_ATOMIC_DATA_TYPE(&pending_messages->atomic_lock);
38+
39+
struct PendingMessagesKey key = {
40+
.source_port = pending_message->source_port,
41+
.packet_id = pending_message->packet_id
42+
};
3843

39-
hashmap_remove(&pending_message->packet_id, sizeof(uint16_t), pending_messages);
44+
hashmap_remove(&key, sizeof(key), pending_messages);
4045

4146
UNLOCK_ATOMIC_DATA_TYPE(&pending_messages->atomic_lock);
4247
}

src/initialize_client_connection.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ static inline struct SwiftNetClientConnection* const construct_client_connection
8686
new_connection->pending_messages_memory_allocator = allocator_create(sizeof(struct SwiftNetPendingMessage), 100);
8787
new_connection->packets_sending_memory_allocator = allocator_create(sizeof(struct SwiftNetPacketSending), 100);
8888
new_connection->packets_completed_memory_allocator = allocator_create(sizeof(struct SwiftNetPacketCompleted), 100);
89-
new_connection->packets_completed = hashmap_create(&uint16_memory_allocator);
89+
new_connection->packets_completed = hashmap_create(&packet_completed_key_allocator);
9090
new_connection->packets_sending = hashmap_create(&uint16_memory_allocator);
91-
new_connection->pending_messages = hashmap_create(&uint16_memory_allocator);
91+
new_connection->pending_messages = hashmap_create(&pending_message_key_allocator);
9292

9393
new_connection->packet_queue = (struct PacketQueue){
9494
.first_node = NULL,

src/initialize_server.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ static inline struct SwiftNetServer* const construct_server(const bool loopback,
4747
new_server->packets_sending_memory_allocator = allocator_create(sizeof(struct SwiftNetPacketSending), 100);
4848
new_server->packets_completed_memory_allocator = allocator_create(sizeof(struct SwiftNetPacketCompleted), 100);
4949

50-
new_server->packets_completed = hashmap_create(&uint16_memory_allocator);
50+
new_server->packets_completed = hashmap_create(&packet_completed_key_allocator);
5151
new_server->packets_sending = hashmap_create(&uint16_memory_allocator);
52-
new_server->pending_messages = hashmap_create(&uint16_memory_allocator);
52+
new_server->pending_messages = hashmap_create(&pending_message_key_allocator);
5353

5454
return new_server;
5555
}

src/initialize_swiftnet.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ struct SwiftNetMemoryAllocator client_connection_memory_allocator;
3535
struct SwiftNetMemoryAllocator listener_memory_allocator;
3636
struct SwiftNetMemoryAllocator hashmap_item_memory_allocator;
3737
struct SwiftNetMemoryAllocator uint16_memory_allocator;
38+
struct SwiftNetMemoryAllocator pending_message_key_allocator;
39+
struct SwiftNetMemoryAllocator packet_completed_key_allocator;
3840

3941
#ifdef SWIFT_NET_REQUESTS
4042
struct SwiftNetMemoryAllocator requests_sent_memory_allocator;
@@ -58,6 +60,8 @@ static inline void initialize_allocators() {
5860
listener_memory_allocator = allocator_create(sizeof(struct Listener), 100);
5961
hashmap_item_memory_allocator = allocator_create(sizeof(struct SwiftNetHashMapItem), 0xFF);
6062
uint16_memory_allocator = allocator_create(sizeof(uint16_t), 0xFF);
63+
pending_message_key_allocator = allocator_create(sizeof(struct PendingMessagesKey), 0xFF);
64+
packet_completed_key_allocator = allocator_create(sizeof(struct PacketCompletedKey), 0xFF);
6165

6266
#ifdef SWIFT_NET_REQUESTS
6367
requests_sent_memory_allocator = allocator_create(sizeof(struct RequestSent), 100);

src/internal/internal.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,16 @@ enum AllocatorStackState {
147147
ALLOCATOR_STACK_OCCUPIED = 1
148148
};
149149

150+
struct PendingMessagesKey {
151+
uint16_t source_port;
152+
uint16_t packet_id;
153+
};
154+
155+
struct PacketCompletedKey {
156+
uint16_t source_port;
157+
uint16_t packet_id;
158+
};
159+
150160
struct Listener {
151161
pcap_t* pcap;
152162
pthread_t listener_thread;
@@ -166,6 +176,9 @@ extern uint64_t seed;
166176

167177
extern struct SwiftNetHashMap listeners;
168178

179+
extern struct SwiftNetMemoryAllocator pending_message_key_allocator;
180+
extern struct SwiftNetMemoryAllocator packet_completed_key_allocator;
181+
169182
extern pthread_t memory_cleanup_thread;
170183
extern _Atomic bool swiftnet_closing;
171184

src/process_packets.c

Lines changed: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ static inline const uint32_t return_lost_chunk_indexes(const uint8_t* const chun
7171
return offset;
7272
}
7373

74-
static inline void packet_completed(uint16_t packet_id, struct SwiftNetHashMap* const packets_completed_history, struct SwiftNetMemoryAllocator* const packets_completed_history_memory_allocator) {
74+
static inline void packet_completed(const uint16_t packet_id, const uint16_t source_port, struct SwiftNetHashMap* const packets_completed_history, struct SwiftNetMemoryAllocator* const packets_completed_history_memory_allocator) {
7575
struct SwiftNetPacketCompleted* const new_packet_completed = allocator_allocate(packets_completed_history_memory_allocator);
7676
new_packet_completed->packet_id = packet_id;
7777
new_packet_completed->marked_cleanup = false;
@@ -81,23 +81,39 @@ static inline void packet_completed(uint16_t packet_id, struct SwiftNetHashMap*
8181

8282
LOCK_ATOMIC_DATA_TYPE(&packets_completed_history->atomic_lock);
8383

84-
hashmap_insert(heap_key_data_location, sizeof(uint16_t), new_packet_completed, packets_completed_history);
84+
struct PacketCompletedKey* const key = allocator_allocate(&packet_completed_key_allocator);
85+
*key = (struct PacketCompletedKey){
86+
.source_port = source_port,
87+
.packet_id = packet_id
88+
};
89+
90+
hashmap_insert(key, sizeof(struct PacketCompletedKey), new_packet_completed, packets_completed_history);
8591

8692
UNLOCK_ATOMIC_DATA_TYPE(&packets_completed_history->atomic_lock);
8793

8894
return;
8995
}
9096

91-
static inline bool check_packet_already_completed(uint16_t packet_id, struct SwiftNetHashMap* const packets_completed_history) {
92-
const struct SwiftNetPacketCompleted* const item = hashmap_get(&packet_id, sizeof(packet_id), packets_completed_history);
97+
static inline bool check_packet_already_completed(const uint16_t packet_id, const uint16_t source_port, struct SwiftNetHashMap* const packets_completed_history) {
98+
const struct PacketCompletedKey key = {
99+
.packet_id = packet_id,
100+
.source_port = source_port
101+
};
102+
103+
const struct SwiftNetPacketCompleted* const item = hashmap_get(&key, sizeof(key), packets_completed_history);
93104

94105
return item != NULL;
95106
}
96107

97-
static inline struct SwiftNetPendingMessage* const get_pending_message(struct SwiftNetHashMap* const pending_messages, const enum ConnectionType connection_type, uint16_t packet_id) {
108+
static inline struct SwiftNetPendingMessage* const get_pending_message(struct SwiftNetHashMap* const pending_messages, const enum ConnectionType connection_type, const uint16_t packet_id, const uint16_t source_port) {
98109
LOCK_ATOMIC_DATA_TYPE(&pending_messages->atomic_lock);
99110

100-
struct SwiftNetPendingMessage* const pending_message = hashmap_get(&packet_id, sizeof(uint16_t), pending_messages);
111+
const struct PendingMessagesKey key = {
112+
.source_port = source_port,
113+
.packet_id = packet_id
114+
};
115+
116+
struct SwiftNetPendingMessage* const pending_message = hashmap_get(&key, sizeof(key), pending_messages);
101117

102118
UNLOCK_ATOMIC_DATA_TYPE(&pending_messages->atomic_lock);
103119

@@ -130,7 +146,7 @@ static inline void insert_callback_queue_node(struct PacketCallbackQueueNode* co
130146

131147
#ifdef SWIFT_NET_REQUESTS
132148

133-
static inline void handle_request_response(uint16_t packet_id, struct SwiftNetPendingMessage* const pending_message, void* const packet_data, struct SwiftNetHashMap* const pending_messages, struct SwiftNetMemoryAllocator* const pending_message_memory_allocator, const enum ConnectionType connection_type, const bool loopback) {
149+
static inline void handle_request_response(uint16_t packet_id, struct SwiftNetPendingMessage* const pending_message, void* const packet_data, struct SwiftNetHashMap* const pending_messages, struct SwiftNetMemoryAllocator* const pending_message_memory_allocator, const enum ConnectionType connection_type, const bool loopback, const uint16_t source_port) {
134150
bool is_valid_response = false;
135151

136152
LOCK_ATOMIC_DATA_TYPE(&requests_sent.atomic_lock);
@@ -153,7 +169,12 @@ static inline void handle_request_response(uint16_t packet_id, struct SwiftNetPe
153169
if (pending_message != NULL) {
154170
LOCK_ATOMIC_DATA_TYPE(&pending_messages->atomic_lock);
155171

156-
hashmap_remove(&packet_id, sizeof(uint16_t), pending_messages);
172+
struct PendingMessagesKey key = {
173+
.source_port = source_port,
174+
.packet_id = packet_id
175+
};
176+
177+
hashmap_remove(&key, sizeof(key), pending_messages);
157178

158179
UNLOCK_ATOMIC_DATA_TYPE(&pending_messages->atomic_lock);
159180
}
@@ -196,7 +217,7 @@ static inline void chunk_received(uint8_t* const chunks_received, const uint32_t
196217
chunks_received[byte] |= 1 << bit;
197218
}
198219

199-
static inline struct SwiftNetPendingMessage* const create_new_pending_message(struct SwiftNetHashMap* const pending_messages, struct SwiftNetMemoryAllocator* const pending_messages_memory_allocator, const struct SwiftNetPacketInfo* const packet_info, const enum ConnectionType connection_type, const uint16_t packet_id) {
220+
static inline struct SwiftNetPendingMessage* const create_new_pending_message(struct SwiftNetHashMap* const pending_messages, struct SwiftNetMemoryAllocator* const pending_messages_memory_allocator, const struct SwiftNetPacketInfo* const packet_info, const enum ConnectionType connection_type, const uint16_t packet_id, const uint16_t source_port) {
200221
struct SwiftNetPendingMessage* const new_pending_message = allocator_allocate(pending_messages_memory_allocator);
201222

202223
uint8_t* const allocated_memory = malloc(packet_info->packet_length);
@@ -212,13 +233,17 @@ static inline struct SwiftNetPendingMessage* const create_new_pending_message(st
212233
new_pending_message->chunks_received = calloc(chunks_received_byte_size, 1);
213234

214235
new_pending_message->packet_id = packet_id;
236+
new_pending_message->source_port = source_port;
215237

216238
LOCK_ATOMIC_DATA_TYPE(&pending_messages->atomic_lock);
217239

218-
uint16_t* const packet_id_mem = allocator_allocate(&uint16_memory_allocator);
219-
*packet_id_mem = packet_id;
240+
struct PendingMessagesKey* const key = allocator_allocate(&pending_message_key_allocator);
241+
*key = (struct PendingMessagesKey){
242+
.source_port = source_port,
243+
.packet_id = packet_id
244+
};
220245

221-
hashmap_insert(packet_id_mem, sizeof(uint16_t), new_pending_message, pending_messages);
246+
hashmap_insert(key, sizeof(struct PendingMessagesKey), new_pending_message, pending_messages);
222247

223248
UNLOCK_ATOMIC_DATA_TYPE(&pending_messages->atomic_lock);
224249

@@ -391,9 +416,9 @@ static inline void swiftnet_process_packets(
391416
{
392417
const uint32_t mtu = MIN(packet_info.maximum_transmission_unit, maximum_transmission_unit);
393418

394-
struct SwiftNetPendingMessage* const pending_message = get_pending_message(pending_messages, connection_type, ip_header.ip_id);
419+
struct SwiftNetPendingMessage* const pending_message = get_pending_message(pending_messages, connection_type, ip_header.ip_id, packet_info.port_info.source_port);
395420
if(pending_message == NULL) {
396-
const bool packet_already_completed = check_packet_already_completed(ip_header.ip_id, packets_completed_history);
421+
const bool packet_already_completed = check_packet_already_completed(ip_header.ip_id, packet_info.port_info.source_port, packets_completed_history);
397422
if(likely(packet_already_completed == true)) {
398423
const struct ip send_packet_ip_header = construct_ip_header(ip_header.ip_src, PACKET_HEADER_SIZE, ip_header.ip_id);
399424

@@ -506,7 +531,7 @@ static inline void swiftnet_process_packets(
506531
break;
507532
}
508533

509-
if (check_packet_already_completed(ip_header.ip_id, packets_completed_history)) {
534+
if (check_packet_already_completed(ip_header.ip_id, packet_info.port_info.source_port, packets_completed_history)) {
510535
allocator_free(&packet_buffer_memory_allocator, packet_buffer);
511536
goto next_packet;
512537
}
@@ -523,12 +548,12 @@ static inline void swiftnet_process_packets(
523548
const uint32_t mtu = MIN(packet_info.maximum_transmission_unit, maximum_transmission_unit);
524549
const uint32_t chunk_data_size = mtu - PACKET_HEADER_SIZE;
525550

526-
struct SwiftNetPendingMessage* const pending_message = get_pending_message(pending_messages, connection_type, ip_header.ip_id);
551+
struct SwiftNetPendingMessage* const pending_message = get_pending_message(pending_messages, connection_type, ip_header.ip_id, packet_info.port_info.source_port);
527552

528553
if(pending_message == NULL) {
529554
if(packet_info.packet_length > chunk_data_size) {
530555
// Split packet into chunks
531-
struct SwiftNetPendingMessage* const new_pending_message = create_new_pending_message(pending_messages, pending_messages_memory_allocator, &packet_info, connection_type, ip_header.ip_id);
556+
struct SwiftNetPendingMessage* const new_pending_message = create_new_pending_message(pending_messages, pending_messages_memory_allocator, &packet_info, connection_type, ip_header.ip_id, packet_info.port_info.source_port);
532557

533558
new_pending_message->chunks_received_number++;
534559

@@ -540,7 +565,7 @@ static inline void swiftnet_process_packets(
540565

541566
goto next_packet;
542567
} else {
543-
packet_completed(ip_header.ip_id, packets_completed_history, packets_completed_history_memory_allocator);
568+
packet_completed(ip_header.ip_id, packet_info.port_info.source_port, packets_completed_history, packets_completed_history_memory_allocator);
544569

545570
if(connection_type == CONNECTION_TYPE_SERVER) {
546571
struct SwiftNetServerPacketData* const new_packet_data = allocator_allocate(&server_packet_data_memory_allocator) ;
@@ -559,7 +584,7 @@ static inline void swiftnet_process_packets(
559584

560585
#ifdef SWIFT_NET_REQUESTS
561586
if (packet_info.packet_type == RESPONSE) {
562-
handle_request_response(ip_header.ip_id, NULL, new_packet_data, pending_messages, pending_messages_memory_allocator, connection_type, loopback);
587+
handle_request_response(ip_header.ip_id, NULL, new_packet_data, pending_messages, pending_messages_memory_allocator, connection_type, loopback, packet_info.port_info.source_port);
563588
} else {
564589
pass_callback_execution(new_packet_data, packet_callback_queue, NULL, ip_header.ip_id, execute_callback_mtx, execute_callback_cond);
565590
}
@@ -582,7 +607,7 @@ static inline void swiftnet_process_packets(
582607

583608
#ifdef SWIFT_NET_REQUESTS
584609
if (packet_info.packet_type == RESPONSE) {
585-
handle_request_response(ip_header.ip_id, NULL, new_packet_data, pending_messages, pending_messages_memory_allocator, connection_type, loopback);
610+
handle_request_response(ip_header.ip_id, NULL, new_packet_data, pending_messages, pending_messages_memory_allocator, connection_type, loopback, packet_info.port_info.source_port);
586611
} else {
587612
pass_callback_execution(new_packet_data, packet_callback_queue, NULL, ip_header.ip_id, execute_callback_mtx, execute_callback_cond);
588613
}
@@ -622,7 +647,7 @@ static inline void swiftnet_process_packets(
622647
}
623648
#endif
624649

625-
packet_completed(ip_header.ip_id, packets_completed_history, packets_completed_history_memory_allocator);
650+
packet_completed(ip_header.ip_id, packet_info.port_info.source_port, packets_completed_history, packets_completed_history_memory_allocator);
626651

627652
if(connection_type == CONNECTION_TYPE_SERVER) {
628653
uint8_t* const ptr = pending_message->packet_data_start;
@@ -643,7 +668,7 @@ static inline void swiftnet_process_packets(
643668

644669
#ifdef SWIFT_NET_REQUESTS
645670
if (packet_info.packet_type == RESPONSE) {
646-
handle_request_response(ip_header.ip_id, pending_message, packet_data, pending_messages, pending_messages_memory_allocator, connection_type, loopback);
671+
handle_request_response(ip_header.ip_id, pending_message, packet_data, pending_messages, pending_messages_memory_allocator, connection_type, loopback, packet_info.port_info.source_port);
647672
} else {
648673
pass_callback_execution(packet_data, packet_callback_queue, pending_message, ip_header.ip_id, execute_callback_mtx, execute_callback_cond);
649674
}
@@ -668,7 +693,7 @@ static inline void swiftnet_process_packets(
668693

669694
#ifdef SWIFT_NET_REQUESTS
670695
if (packet_info.packet_type == RESPONSE) {
671-
handle_request_response(ip_header.ip_id, pending_message, packet_data, pending_messages, pending_messages_memory_allocator, connection_type, loopback);
696+
handle_request_response(ip_header.ip_id, pending_message, packet_data, pending_messages, pending_messages_memory_allocator, connection_type, loopback, packet_info.port_info.source_port);
672697
} else {
673698
pass_callback_execution(packet_data, packet_callback_queue, pending_message, ip_header.ip_id, execute_callback_mtx, execute_callback_cond);
674699
}

src/swift_net.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ struct SwiftNetPendingMessage {
103103
struct SwiftNetPacketInfo packet_info;
104104
uint32_t chunks_received_length;
105105
uint32_t chunks_received_number;
106+
uint16_t source_port;
106107
uint16_t packet_id;
107108
};
108109

0 commit comments

Comments
 (0)