Skip to content
Merged

Dpdk #208

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/run_all_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ jobs:
- uses: actions/checkout@v4
- name: build
run: |
cp profile_template.cmake profile.cmake
cp profiles/pcap_profile.cmake profile.cmake
./build.sh
working-directory: build
- name: build_integration_tests
run: |
./build_tests.sh
working-directory: tests/integration_tests/build
working-directory: tests/integration_tests_pcap/build
- name: run_integration_tests
run: |
sudo ./run_tests
working-directory: tests/integration_tests/output
working-directory: tests/integration_tests_pcap/output
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Makefile
*.cmake
*.cache
!/build/profile_template.cmake
!/build/profiles/*

!/build/cmake

Expand Down
8 changes: 7 additions & 1 deletion build/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,11 @@ rm -rf cmake
rm -f Makefile
rm -f cmake_install.cmake

cmake ../src -B .
cmake ../src \
-DCMAKE_C_COMPILER=clang \
-DCMAKE_CXX_COMPILER=clang++ \
-DCMAKE_C_FLAGS="-Wall -Wextra -Wpedantic -Werror -Wconversion -Wsign-conversion -Wshadow -Wdouble-promotion -Wformat=2 -Wnull-dereference -Wimplicit-fallthrough -Wunreachable-code -Wstrict-prototypes" \
-DCMAKE_VERBOSE_MAKEFILE=ON \
-B .

make -B -j8
5 changes: 4 additions & 1 deletion build/build_for_asm_view.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#!/bin/bash
cmake --build . --config RelWithDebInfo --target swiftnet_debug -j 1

cmake -DCMAKE_C_COMPILER=clang .
cmake --build . --config RelWithDebInfo --target swiftnet_debug

exit 0
2 changes: 1 addition & 1 deletion build/build_instructions.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## Create profile
- Copy the profile_template.cmake file and name it profile.cmake
- Copy a profile file inside profiles and name it profile.cmake
- Configure the profile correctly
- Run build.sh
11 changes: 11 additions & 0 deletions build/profiles/dpdk_profile.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
set(CMAKE_BUILD_TYPE Debug)

add_compile_definitions(
SWIFT_NET_MEMORY_USAGE=5
DPDK_LCORES=1
)

set(SWIFT_NET_INTERNAL_TESTING ON)
set(SANITIZER "none")
set(BACKEND "dpdk")
set(DPDK_EXTRA_ARGS "--no-pci, --vdev=net_ring0") # Basic loopback setup
9 changes: 9 additions & 0 deletions build/profiles/pcap_profile.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
set(CMAKE_BUILD_TYPE Debug)

add_compile_definitions(
SWIFT_NET_MEMORY_USAGE=5
)

set(SWIFT_NET_INTERNAL_TESTING ON)
set(SANITIZER "none")
set(BACKEND "pcap")
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@ set(CMAKE_BUILD_TYPE Debug)
add_compile_definitions(
# SWIFT_NET_DISABLE_REQUESTS # Compile source without request features (make_request, make_response)
SWIFT_NET_MEMORY_USAGE=5 # Multiplier of memory preallocated

# DPDK SPECIFIC SETTINGS
DPDK_LCORES=1,2 # List of cores to use for DPDK (every unique interface used requires one lcore)
)

set(SWIFT_NET_INTERNAL_TESTING ON) # Only used when debugging the library itself
set(SANITIZER "none") # (thread, address, undefined, none (no sanitizer))
set(BACKEND "pcap") # (pcap, dpdk)
set(BACKEND "") # (pcap, dpdk)
# DPDK SPECIFIC SETTINGS
set(DPDK_EXTRA_ARGS "") # Extra DPDK args
37 changes: 32 additions & 5 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ project(swiftnet C CXX)
include(GNUInstallDirs)
include(../build/profile.cmake)

set(CMAKE_C_STANDARD 99)
set(CMAKE_C_STANDARD 23)

set(SOURCE_FILES
initialize_swiftnet.c
Expand All @@ -16,7 +16,6 @@ set(SOURCE_FILES
cleanup_connection.c
process_packets.c
execute_packet_callback.c
packet_buffer.c
manipulate_debug_flags.c
cleanup_swiftnet.c
make_response.c
Expand Down Expand Up @@ -50,13 +49,24 @@ if(BACKEND STREQUAL "pcap")
add_compile_definitions(SWIFT_NET_BACKEND_PCAP)

list(APPEND SOURCE_FILES
internal/pcap/pcap_buffer.c
internal/pcap/pcap_send.c
internal/pcap/pcap_open.c
)
elseif(BACKEND STREQUAL "dpdk")
add_compile_definitions(SWIFT_NET_BACKEND_DPDK)

list(APPEND SOURCE_FILES
internal/dpdk/dpdk_buffer.c
internal/dpdk/dpdk_open.c
internal/dpdk/dpdk_send.c
)
endif()


add_compile_options(-march=native)
if(CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64")
add_compile_options(-march=armv8-a+crc)
endif()

add_library(swiftnet STATIC ${SOURCE_FILES})
add_library(swiftnet_shared SHARED ${SOURCE_FILES})
Expand All @@ -78,7 +88,7 @@ else()

target_compile_options(swiftnet PRIVATE
-O3
-march=native

-flto=auto
-funroll-loops
-fomit-frame-pointer
Expand All @@ -96,7 +106,6 @@ else()

target_compile_options(swiftnet_shared PRIVATE
-O3
-march=native
-flto=auto
-funroll-loops
-fomit-frame-pointer
Expand Down Expand Up @@ -141,6 +150,24 @@ if(BACKEND STREQUAL "pcap")

message("using linking option: -lpcap")
endif()
elseif(BACKEND STREQUAL "dpdk")
find_package(PkgConfig REQUIRED)
pkg_check_modules(DPDK REQUIRED libdpdk)

string(REPLACE " " "" DPDK_EXTRA_ARGS_STRIPPED "${DPDK_EXTRA_ARGS}")
string(REPLACE "," ";" DPDK_EXTRA_ARGS_REPLACED "${DPDK_EXTRA_ARGS_STRIPPED}")

list(JOIN DPDK_EXTRA_ARGS_REPLACED "\", \"" DPDK_EXTRA_ARGS_JOINED)

set(DPDK_EXTRA_ARGS_PARSED "{\"${DPDK_EXTRA_ARGS_JOINED}\", NULL};")

add_compile_definitions(DPDK_EXTRA_ARGS=${DPDK_EXTRA_ARGS_PARSED})

target_include_directories(swiftnet PUBLIC ${DPDK_INCLUDE_DIRS})
target_include_directories(swiftnet_shared PUBLIC ${DPDK_INCLUDE_DIRS})
target_link_libraries(swiftnet PUBLIC -static ${DPDK_LIBRARIES})
target_link_libraries(swiftnet_shared PUBLIC ${DPDK_LIBRARIES})
message(STATUS "DPDK backend selected")
endif()

set_target_properties(swiftnet PROPERTIES
Expand Down
45 changes: 32 additions & 13 deletions src/cleanup_connection.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "swift_net.h"
#include "internal/internal.h"
#include "internal/networking.h"
#include "swift_net.h"
#include <stdatomic.h>
#include <stdint.h>
#include <stdlib.h>
Expand Down Expand Up @@ -31,11 +31,12 @@ static inline void cleanup_connection_resources(const enum ConnectionType connec
}
}

static inline void remove_listener(const enum ConnectionType connection_type, const char* const restrict interface_name, void* const connection) {
static inline void remove_listener(const enum ConnectionType connection_type, char* const restrict interface_name, void* const connection) {
uint32_t interface_len;
struct Listener* listener;

interface_len = strlen(interface_name);

interface_len = (uint32_t)strlen(interface_name);

LOCK_ATOMIC_DATA_TYPE(&listeners.atomic_lock);

Expand All @@ -60,11 +61,32 @@ static inline void remove_listener(const enum ConnectionType connection_type, co
UNLOCK_ATOMIC_DATA_TYPE(&listener->servers.atomic_lock);
}

if(listener->servers.size + listener->client_connections.size == 0) {
hashmap_destroy(&listener->servers);
hashmap_destroy(&listener->client_connections);

SWIFTNET_BREAK_RECEIVER_LOOP(&listener->network_data);

#ifdef SWIFT_NET_BACKEND_PCAP
pthread_join(listener->listener_thread, NULL);
#elif defined(SWIFT_NET_BACKEND_DPDK)
rte_eal_wait_lcore(listener->lcore);

lcores_used[listener->lcore_internal_index] = listener->lcore;
#endif

SWIFTNET_CLOSE_CONNECTION(&listener->network_data);

allocator_free(&listener_memory_allocator, listener);

hashmap_remove(interface_name, interface_len, &listeners);
}

UNLOCK_ATOMIC_DATA_TYPE(&listeners.atomic_lock);
}

static inline const char* get_interface_name(const bool loopback) {
return loopback ? LOOPBACK_INTERFACE_NAME : default_network_interface;
static inline char* get_interface_name(const bool loopback, const enum ConnectionType con_type) {
return loopback ? con_type == CONNECTION_TYPE_CLIENT ? CLIENT_LOOPBACK_INTERFACE_NAME : SERVER_LOOPBACK_INTERFACE_NAME : default_network_interface;
}

static inline void close_threads(const enum ConnectionType connection_type, void* const connection) {
Expand Down Expand Up @@ -114,33 +136,30 @@ static inline void close_threads(const enum ConnectionType connection_type, void
}

void swiftnet_client_cleanup(struct SwiftNetClientConnection* const client) {
const char* restrict interface_name;
char* restrict interface_name;

cleanup_connection_resources(CONNECTION_TYPE_CLIENT, client);

interface_name = get_interface_name(client->loopback);
interface_name = get_interface_name(client->loopback, CONNECTION_TYPE_CLIENT);

remove_listener(CONNECTION_TYPE_CLIENT, interface_name, client);

close_threads(CONNECTION_TYPE_CLIENT, client);

SWIFTNET_CLOSE_CONNECTION(&client->network_data);

allocator_free(&client_connection_memory_allocator, client);
}

void swiftnet_server_cleanup(struct SwiftNetServer* const server) {
const char* restrict interface_name;
char* restrict interface_name;


cleanup_connection_resources(CONNECTION_TYPE_SERVER, server);

interface_name = get_interface_name(server->loopback);
interface_name = get_interface_name(server->loopback, CONNECTION_TYPE_SERVER);

remove_listener(CONNECTION_TYPE_SERVER, interface_name, server);

close_threads(CONNECTION_TYPE_SERVER, server);

SWIFTNET_CLOSE_CONNECTION(&server->network_data);

allocator_free(&server_memory_allocator, server);
}
18 changes: 17 additions & 1 deletion src/cleanup_swiftnet.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ static inline void close_listeners() {

SWIFTNET_BREAK_RECEIVER_LOOP(&current_listener->network_data);

pthread_join(current_listener->listener_thread, NULL);
WAIT_LISTENER_THREAD(current_listener);

SWIFTNET_CLOSE_CONNECTION(&current_listener->network_data);

Expand Down Expand Up @@ -58,5 +58,21 @@ void swiftnet_cleanup() {
printf("Bytes leaked: %d\nItems leaked: %d\n", bytes_leaked, items_leaked);
#endif

#ifdef SWIFT_NET_BACKEND_DPDK
uint16_t port_id;
uint16_t count;

count = rte_eth_dev_count_avail();

for (port_id = 0; port_id < count; port_id++) {
if (!rte_eth_dev_is_valid_port(port_id))
continue;

rte_eth_dev_close(port_id);
}

rte_eal_cleanup();
#endif

close_background_service();
}
Loading
Loading