From 7bc2a594e71f956fa0b4be9a8cfc54dfa0e9a7f6 Mon Sep 17 00:00:00 2001 From: Jerry Zhao Date: Sat, 7 Nov 2026 12:18:39 +0000 Subject: [PATCH 1/2] fix(net): reuse DPDK handles after pending I/O retires Replace cumulative worker-local handle allocation with reusable slots. Pending operations and completion delivery pin each slot until the old socket can no longer be observed, including cancellation and shutdown. Keep transport close immediate and preserve the public socket APIs. Validate with 6 slot lifetime tests, 12 DPDK ring backend scenarios, and 34 existing networking regressions. Additionally pass 31 slot and kernel runtime tests under ASAN/UBSAN. Physical NIC behavior was not exercised. --- CMakeLists.txt | 13 ++ docs/architecture/networking.md | 10 +- include/bycorf/io/dpdk_backend.h | 2 + src/io/dpdk_backend.cpp | 132 +++++++++++++-------- src/io/dpdk_socket_table.h | 156 ++++++++++++++++++++++++ tests/dpdk_handle_reuse_test.cpp | 196 +++++++++++++++++++++++++++++++ tests/dpdk_socket_table_test.cpp | 128 ++++++++++++++++++++ 7 files changed, 587 insertions(+), 50 deletions(-) create mode 100644 src/io/dpdk_socket_table.h create mode 100644 tests/dpdk_handle_reuse_test.cpp create mode 100644 tests/dpdk_socket_table_test.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 0a77f8e..9bd415a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -472,6 +472,11 @@ if (BYCORF_BUILD_TESTS) target_include_directories(bycorf_dpdk_memory_budget_tests PRIVATE src/io) bycorf_discover_tests(bycorf_dpdk_memory_budget_tests 30 "runtime;net;unit") + add_executable(bycorf_dpdk_socket_table_tests tests/dpdk_socket_table_test.cpp) + target_link_libraries(bycorf_dpdk_socket_table_tests PRIVATE GTest::gtest_main) + target_include_directories(bycorf_dpdk_socket_table_tests PRIVATE src/io) + bycorf_discover_tests(bycorf_dpdk_socket_table_tests 30 "runtime;net;unit") + add_executable(bycorf_task_tests tests/task_transfer_test.cpp) target_link_libraries(bycorf_task_tests PRIVATE GTest::gtest) target_include_directories(bycorf_task_tests PRIVATE include) @@ -491,6 +496,14 @@ if (BYCORF_BUILD_TESTS) bycorf_discover_tests(bycorf_runtime_tests 90 "runtime;net;io_uring") if (BYCORF_KERNEL_BYPASS) + add_executable(bycorf_dpdk_handle_reuse_tests tests/dpdk_handle_reuse_test.cpp) + target_link_libraries(bycorf_dpdk_handle_reuse_tests PRIVATE + bycorf::core GTest::gtest_main) + target_include_directories(bycorf_dpdk_handle_reuse_tests PRIVATE src/io) + target_link_options(bycorf_dpdk_handle_reuse_tests PRIVATE + "LINKER:--wrap=bycorf_bsd_send" "LINKER:--wrap=bycorf_bsd_connect") + bycorf_discover_tests(bycorf_dpdk_handle_reuse_tests 30 "runtime;net;dpdk;stub") + add_executable(bycorf_dpdk_memory_startup_tests tests/dpdk_memory_startup_test.cpp) target_link_libraries(bycorf_dpdk_memory_startup_tests PRIVATE diff --git a/docs/architecture/networking.md b/docs/architecture/networking.md index 22f1dd4..5f07b5b 100644 --- a/docs/architecture/networking.md +++ b/docs/architecture/networking.md @@ -137,8 +137,13 @@ bounds checks remain active; `tests/freebsd_tcp_sequence_check.cpp` covers this invariant through the real stack and an in-process Ethernet peer. BSD sockets use private worker-encoded handles rather than Linux file -descriptors. Bycorf's close and peer-address helpers dispatch these handles to -the stack. Applications that directly pass stream handles to Linux syscalls or +descriptors. Handle slots are reusable within their owning worker. Closing a +socket immediately invalidates its transport; pending operations and queued +completions retain the old slot through callback delivery before it can be +assigned to another socket. Thus handle capacity bounds simultaneously live +or draining sockets, not cumulative connection churn. Bycorf's close and +peer-address helpers dispatch these handles to the stack. Applications that +directly pass stream handles to Linux syscalls or transfer a live socket to another worker need explicit adaptation. Accepted and outgoing IPv4 TCP use BSD; Unix sockets retain the kernel path. IPv6 DPDK listeners and clients are unsupported. @@ -179,4 +184,5 @@ both modes; this is separate from Bycorf's worker sleep protocol. Sources: `include/bycorf/net/connection.h`, `src/net/tcp_service.cpp`, `src/net/tcp_listener.cpp`, `src/net/tcp_stream.cpp`, `src/net/socket_ops.cpp`, `src/io/dpdk_backend.cpp`, +`src/io/dpdk_socket_table.h`, `src/io/dpdk_memory_budget.cpp`, `src/io/freebsd/abi.h`, `src/io/freebsd/`. diff --git a/include/bycorf/io/dpdk_backend.h b/include/bycorf/io/dpdk_backend.h index 439a991..70a39c3 100644 --- a/include/bycorf/io/dpdk_backend.h +++ b/include/bycorf/io/dpdk_backend.h @@ -74,6 +74,8 @@ class DpdkBackend { // Socket helpers require the owning worker thread. Return host errno values. static int Listen(const sockaddr* address, socklen_t length, int backlog); static int OpenClient(int family); + // Closes transport immediately. Pending operations retain the handle slot + // until their completions retire, after which another socket may reuse it. static int Close(int handle) noexcept; static int PeerName(int handle, sockaddr*, socklen_t*) noexcept; diff --git a/src/io/dpdk_backend.cpp b/src/io/dpdk_backend.cpp index 10b0c44..c3020d2 100644 --- a/src/io/dpdk_backend.cpp +++ b/src/io/dpdk_backend.cpp @@ -42,6 +42,7 @@ #include #include #include +#include #include #include #include @@ -52,6 +53,7 @@ #include "bycorf/net/socket_ops.h" #include "bycorf/runtime/worker.h" #include "dpdk_memory_budget.h" +#include "dpdk_socket_table.h" #include "freebsd/abi.h" #include "freebsd_errno.h" @@ -65,7 +67,7 @@ static_assert(kMaxWorkers > 0 && kMaxWorkers < RTE_MAX_LCORE); constexpr int kFirstHandle = 0x40000000; // Bit 30 distinguishes BSD handles from kernel fds; the remaining positive // bits partition the namespace by owner. Fixed 24-bit slots overflowed signed -// handles at worker 64. Leave the last slot unused so increment never wraps. +// handles at worker 64. Keep allocation inside each worker's slot range. constexpr unsigned kHandleBits = 30 - std::bit_width(kMaxWorkers - 1); constexpr unsigned kHandleMask = (1U << kHandleBits) - 1; constexpr std::size_t kMaxFrame = 1518; @@ -224,11 +226,11 @@ class DpdkBackend::Impl { unsigned id = 0; bool initialized = false, polling = false; std::size_t buffer_size = 4096; - int next_handle = kFirstHandle; unsigned next_client_port = 0; - std::unordered_map sockets; + detail::DpdkSocketTable sockets; + using SocketPin = detail::DpdkSocketTable::Pin; struct Request { - int fd; + SocketPin socket; IoCompletion* tag; std::span bytes; const msghdr* message = nullptr; @@ -237,9 +239,14 @@ class DpdkBackend::Impl { IoCompletion* tag; int result; unsigned flags; + SocketPin socket; + }; + struct ConnectionRequest { + Connection* connection; + SocketPin socket; }; std::vector accepts, sends, connects; - std::vector reads, disconnects; + std::vector reads, disconnects; std::vector completions; std::uint64_t rx = 0, tx = 0, forwarded_rx = 0, forwarded_tx = 0, drops = 0; std::uint64_t waits = 0, arms = 0, notifications = 0; @@ -275,23 +282,28 @@ class DpdkBackend::Impl { } } notification; - BsdSocket* Find(int handle) { - const auto found = sockets.find(handle); - return found == sockets.end() ? nullptr : found->second; - } + BsdSocket* Find(int handle) { return sockets.Find(handle); } int Own(BsdSocket* so) { - if ((next_handle & kHandleMask) == kHandleMask) { + int handle; + try { + handle = sockets.Insert(so); + } catch (const std::bad_alloc&) { bycorf_bsd_close(so); - errno = EMFILE; + errno = ENOMEM; return -1; } - const int handle = next_handle++; - sockets.emplace(handle, so); + if (handle < 0) { + bycorf_bsd_close(so); + errno = EMFILE; + } return handle; } void QueueCompletion(IoCompletion* tag, int result, - unsigned flags = kCompletionNone) { - completions.push_back({tag, result, flags}); + unsigned flags = kCompletionNone, + SocketPin socket = {}) { + // A callback may close a socket and create another one. Retain its old + // number through callback delivery, not merely removal from a pending list. + completions.push_back({tag, result, flags, std::move(socket)}); } void Wake(unsigned target) { Lane& lane = fabric.lanes[target]; @@ -466,8 +478,8 @@ class DpdkBackend::Impl { bool SocketCompletions() { bool work = false; for (std::size_t i = 0; i < connects.size();) { - const auto request = connects[i]; - BsdSocket* so = Find(request.fd); + auto request = connects[i]; + BsdSocket* so = request.socket.get(); const int error = so ? BsdError(bycorf_bsd_connect_status(so)) : ECANCELED; if (error == EAGAIN) { @@ -475,12 +487,13 @@ class DpdkBackend::Impl { continue; } connects.erase(connects.begin() + i); - QueueCompletion(request.tag, -error); + QueueCompletion(request.tag, -error, kCompletionNone, + std::move(request.socket)); work = true; } for (std::size_t i = 0; i < accepts.size();) { - const auto request = accepts[i]; - BsdSocket* listener = Find(request.fd); + auto request = accepts[i]; + BsdSocket* listener = request.socket.get(); BsdSocket* accepted = nullptr; const int error = listener ? BsdError(bycorf_bsd_accept(listener, &accepted)) : EBADF; @@ -491,17 +504,18 @@ class DpdkBackend::Impl { if (!error) { const int handle = Own(accepted); QueueCompletion(request.tag, handle < 0 ? -errno : handle, - kCompletionMore); + kCompletionMore, std::move(request.socket)); ++i; } else { accepts.erase(accepts.begin() + i); - QueueCompletion(request.tag, -error); + QueueCompletion(request.tag, -error, kCompletionNone, + std::move(request.socket)); } work = true; } for (std::size_t i = 0; i < sends.size();) { - const auto request = sends[i]; - BsdSocket* so = Find(request.fd); + auto request = sends[i]; + BsdSocket* so = request.socket.get(); std::size_t sent = 0; int error = so ? 0 : EBADF; if (so && request.message) { @@ -525,12 +539,14 @@ class DpdkBackend::Impl { continue; } sends.erase(sends.begin() + i); - QueueCompletion(request.tag, sent ? static_cast(sent) : -error); + QueueCompletion(request.tag, sent ? static_cast(sent) : -error, + kCompletionNone, std::move(request.socket)); work = true; } for (std::size_t i = 0; i < reads.size();) { - Connection* c = reads[i]; - BsdSocket* so = Find(c->file_.fd_); + const auto request = reads[i]; + Connection* c = request.connection; + BsdSocket* so = request.socket.get(); std::size_t got = 0; const int error = so && !c->closing_ @@ -554,8 +570,9 @@ class DpdkBackend::Impl { work = true; } for (std::size_t i = 0; i < disconnects.size();) { - Connection* c = disconnects[i]; - BsdSocket* so = Find(c->file_.fd_); + const auto request = disconnects[i]; + Connection* c = request.connection; + BsdSocket* so = request.socket.get(); const bool cancel = c->peer_disconnect_poll_cancel_requested_ || c->closing_ || !so; if (!cancel && !bycorf_bsd_disconnected(so)) { @@ -870,7 +887,8 @@ absl::Status DpdkBackend::Init(const IoBackendOptions& options, impl_->buffer_size = options.recv_buffer_size_; // Embed the owner in the handle so an accidental transfer cannot alias an // unrelated socket that happens to occupy the same slot on another worker. - impl_->next_handle = kFirstHandle | (impl_->id << kHandleBits); + impl_->sockets.Configure(kFirstHandle | (impl_->id << kHandleBits), + kHandleMask); current_ = impl_.get(); if (rte_thread_register()) { auto status = DeviceError("register worker with EAL", rte_errno); @@ -921,8 +939,7 @@ void DpdkBackend::Shutdown() { if (current_ != impl_.get()) std::abort(); // Runtime tears down on the owning thread. fabric.lanes[impl_->id].parked.store(false, std::memory_order_release); - for (auto [handle, so] : impl_->sockets) bycorf_bsd_close(so); - impl_->sockets.clear(); + impl_->sockets.CloseAll([](BsdSocket* so) { bycorf_bsd_close(so); }); impl_->SocketCompletions(); impl_->FlushTx(); std::fprintf( @@ -1036,14 +1053,18 @@ absl::Status DpdkBackend::SubmitSend(const RegisteredFile& file, if (!detail::IsDpdkSocket(file.fd_)) return kernel_.SubmitSend(file, bytes, tag); if (!tag) return absl::InvalidArgumentError("send completion is null"); - impl_->sends.push_back({file.fd_, tag, bytes}); + auto socket = impl_->sockets.Acquire(file.fd_); + if (!socket) + return absl::FailedPreconditionError("send on closed DPDK socket"); + impl_->sends.push_back({std::move(socket), tag, bytes}); return absl::OkStatus(); } absl::Status DpdkBackend::SubmitConnect(int fd, const sockaddr* address, socklen_t length, IoCompletion* tag) { if (!detail::IsDpdkSocket(fd)) return kernel_.SubmitConnect(fd, address, length, tag); - auto* so = impl_->Find(fd); + auto socket = impl_->sockets.Acquire(fd); + auto* so = socket.get(); if (!so || !tag || !address || address->sa_family != AF_INET || length < sizeof(sockaddr_in)) { return absl::InvalidArgumentError("invalid DPDK connect request"); @@ -1052,9 +1073,9 @@ absl::Status DpdkBackend::SubmitConnect(int fd, const sockaddr* address, const int error = BsdError( bycorf_bsd_connect(so, ipv4->sin_addr.s_addr, ntohs(ipv4->sin_port))); if (error && error != EINPROGRESS) { - impl_->QueueCompletion(tag, -error); + impl_->QueueCompletion(tag, -error, kCompletionNone, std::move(socket)); } else { - impl_->connects.push_back({fd, tag, {}}); + impl_->connects.push_back({std::move(socket), tag, {}}); } return absl::OkStatus(); } @@ -1065,22 +1086,28 @@ absl::Status DpdkBackend::SubmitSendMsg(const RegisteredFile& file, return kernel_.SubmitSendMsg(file, message, tag); if (!tag || !message || message->msg_control || message->msg_name) return absl::InvalidArgumentError("unsupported DPDK sendmsg request"); - impl_->sends.push_back({file.fd_, tag, {}, message}); + auto socket = impl_->sockets.Acquire(file.fd_); + if (!socket) + return absl::FailedPreconditionError("sendmsg on closed DPDK socket"); + impl_->sends.push_back({std::move(socket), tag, {}, message}); return absl::OkStatus(); } absl::Status DpdkBackend::SubmitAcceptMultishot(int fd, IoCompletion* tag) { if (!detail::IsDpdkSocket(fd)) return kernel_.SubmitAcceptMultishot(fd, tag); - if (!tag || !impl_->Find(fd)) + auto socket = impl_->sockets.Acquire(fd); + if (!tag || !socket) return absl::InvalidArgumentError("invalid DPDK accept request"); - impl_->accepts.push_back({fd, tag, {}}); + impl_->accepts.push_back({std::move(socket), tag, {}}); return absl::OkStatus(); } absl::Status DpdkBackend::SubmitCancel(IoCompletion* target) { for (auto* requests : {&impl_->accepts, &impl_->sends, &impl_->connects}) { for (auto at = requests->begin(); at != requests->end(); ++at) { if (at->tag != target) continue; + auto socket = std::move(at->socket); requests->erase(at); - impl_->QueueCompletion(target, -ECANCELED); + impl_->QueueCompletion(target, -ECANCELED, kCompletionNone, + std::move(socket)); return absl::OkStatus(); } } @@ -1092,19 +1119,26 @@ absl::Status DpdkBackend::StartRecvMultishot(Connection* c) { if (c->recv_armed_ || c->closed_ || c->closing_ || c->recv_paused_ || !c->received_buffers_.empty()) return absl::OkStatus(); + auto socket = impl_->sockets.Acquire(c->file_.fd_); + if (!socket) + return absl::FailedPreconditionError("recv on closed DPDK socket"); c->recv_mode_ = RecvMode::kOneShot; if (c->read_buffer_.empty()) c->read_buffer_.resize(impl_->buffer_size); c->recv_armed_ = true; ++c->inflight_ops_; - impl_->reads.push_back(c); + impl_->reads.push_back({c, std::move(socket)}); return absl::OkStatus(); } absl::Status DpdkBackend::SubmitCancelRecv(Connection* c, IoCompletion* tag) { - const auto at = std::find(impl_->reads.begin(), impl_->reads.end(), c); + const auto at = + std::find_if(impl_->reads.begin(), impl_->reads.end(), + [c](const auto& r) { return r.connection == c; }); if (at == impl_->reads.end()) return kernel_.SubmitCancelRecv(c, tag); + auto socket = std::move(at->socket); impl_->reads.erase(at); impl_->FinishRead(c); - if (tag != nullptr) impl_->QueueCompletion(tag, 0); + if (tag != nullptr) + impl_->QueueCompletion(tag, 0, kCompletionNone, std::move(socket)); return absl::OkStatus(); } absl::Status DpdkBackend::StartPeerDisconnectPoll(Connection* c) { @@ -1112,14 +1146,17 @@ absl::Status DpdkBackend::StartPeerDisconnectPoll(Connection* c) { return kernel_.StartPeerDisconnectPoll(c); if (c->peer_disconnect_poll_armed_ || c->closed_ || c->closing_) return absl::OkStatus(); + auto socket = impl_->sockets.Acquire(c->file_.fd_); + if (!socket) + return absl::FailedPreconditionError("poll on closed DPDK socket"); c->peer_disconnect_poll_armed_ = true; ++c->inflight_ops_; - impl_->disconnects.push_back(c); + impl_->disconnects.push_back({c, std::move(socket)}); return absl::OkStatus(); } absl::Status DpdkBackend::CancelPeerDisconnectPoll(Connection* c) { - if (std::find(impl_->disconnects.begin(), impl_->disconnects.end(), c) == - impl_->disconnects.end()) + if (std::none_of(impl_->disconnects.begin(), impl_->disconnects.end(), + [c](const auto& r) { return r.connection == c; })) return kernel_.CancelPeerDisconnectPoll(c); c->peer_disconnect_poll_cancel_requested_ = true; return absl::OkStatus(); @@ -1192,12 +1229,11 @@ int DpdkBackend::Close(int handle) noexcept { errno = EBADF; return -1; } - BsdSocket* so = current_->Find(handle); + BsdSocket* so = current_->sockets.Close(handle); if (!so) { errno = EBADF; return -1; } - current_->sockets.erase(handle); const int error = BsdError(bycorf_bsd_close(so)); if (error) { errno = error; diff --git a/src/io/dpdk_socket_table.h b/src/io/dpdk_socket_table.h new file mode 100644 index 0000000..3b1ba51 --- /dev/null +++ b/src/io/dpdk_socket_table.h @@ -0,0 +1,156 @@ +// Copyright (C) 2026 EloqData Inc. +// SPDX-License-Identifier: Apache-2.0 +#pragma once + +#include +#include +#include +#include +#include +#include + +struct socket; + +namespace bycorf::detail { + +// Worker-local socket identities. Close removes the transport immediately, +// but a number cannot be reused until every queued operation/completion drops +// its Pin. This lifetime rule replaces cumulative, never-reused handle IDs; +// no generation counter or cross-worker reference count is needed. +class DpdkSocketTable { + public: + class Pin { + public: + Pin() = default; + Pin(const Pin& other) noexcept : table_(other.table_), slot_(other.slot_) { + if (table_) ++table_->slots_[slot_].pins; + } + Pin& operator=(const Pin& other) noexcept { + if (this != &other) { + Pin copy(other); + *this = std::move(copy); + } + return *this; + } + Pin(Pin&& other) noexcept + : table_(std::exchange(other.table_, nullptr)), slot_(other.slot_) {} + Pin& operator=(Pin&& other) noexcept { + if (this != &other) { + Reset(); + table_ = std::exchange(other.table_, nullptr); + slot_ = other.slot_; + } + return *this; + } + ~Pin() { Reset(); } + + // A closed socket stays invalid even while its number is retained. + struct socket* get() const noexcept { + return table_ ? table_->slots_[slot_].socket : nullptr; + } + explicit operator bool() const noexcept { return table_ != nullptr; } + void Reset() noexcept { + if (auto* table = std::exchange(table_, nullptr)) { + auto& slot = table->slots_[slot_]; + assert(slot.pins != 0); + if (--slot.pins == 0 && slot.socket == nullptr) table->Recycle(slot_); + } + } + + private: + friend class DpdkSocketTable; + Pin(DpdkSocketTable* table, std::uint32_t slot) noexcept + : table_(table), slot_(slot) { + ++table_->slots_[slot_].pins; + } + DpdkSocketTable* table_ = nullptr; + std::uint32_t slot_ = 0; + }; + + DpdkSocketTable() = default; + DpdkSocketTable(const DpdkSocketTable&) = delete; + DpdkSocketTable& operator=(const DpdkSocketTable&) = delete; + ~DpdkSocketTable() { AssertDrained(); } + + // Initialization/reinitialization requires drained operations and sockets. + void Configure(int first, std::uint32_t capacity) { + AssertDrained(); + assert(first >= 0 && + capacity <= std::uint32_t(std::numeric_limits::max() - first)); + first_ = first; + capacity_ = capacity; + slots_.clear(); + free_ = kNoSlot; + } + + // Does not take socket ownership on exhaustion or allocation failure. + int Insert(struct socket* socket) { + assert(socket != nullptr); + std::uint32_t index = free_; + if (index == kNoSlot) { + if (slots_.size() == capacity_) return -1; + index = static_cast(slots_.size()); + slots_.emplace_back(); + } else { + free_ = slots_[index].next_free; + } + auto& slot = slots_[index]; + assert(slot.socket == nullptr && slot.pins == 0); + slot.socket = socket; + return first_ + static_cast(index); + } + + struct socket* Find(int handle) const noexcept { + const auto index = Index(handle); + return index < slots_.size() ? slots_[index].socket : nullptr; + } + Pin Acquire(int handle) noexcept { + return Find(handle) ? Pin(this, Index(handle)) : Pin{}; + } + + // Returns the transport to close. Recycling itself never allocates, and + // live pins defer recycling until after their callbacks have returned. + struct socket* Close(int handle) noexcept { + const auto index = Index(handle); + if (index >= slots_.size()) return nullptr; + auto& slot = slots_[index]; + auto* socket = std::exchange(slot.socket, nullptr); + if (socket && slot.pins == 0) Recycle(index); + return socket; + } + template + void CloseAll(CloseSocket close_socket) { + for (std::uint32_t i = 0; i < slots_.size(); ++i) { + if (auto* socket = Close(first_ + static_cast(i))) + close_socket(socket); + } + } + + private: + static constexpr auto kNoSlot = std::numeric_limits::max(); + struct Slot { + struct socket* socket = nullptr; + std::size_t pins = 0; + std::uint32_t next_free = kNoSlot; + }; + std::uint32_t Index(int handle) const noexcept { + return handle < first_ ? kNoSlot + : static_cast(handle - first_); + } + void Recycle(std::uint32_t index) noexcept { + slots_[index].next_free = free_; + free_ = index; + } + void AssertDrained() const noexcept { + for ([[maybe_unused]] const auto& slot : slots_) + assert(slot.socket == nullptr && slot.pins == 0); + } + int first_ = 0; + std::uint32_t capacity_ = 0; + std::uint32_t free_ = kNoSlot; + // Retain metadata at the peak number of live/draining sockets to keep reuse + // allocation-free. Closed slots hold neither transport nor payload buffers. + std::vector slots_; +}; + +} // namespace bycorf::detail diff --git a/tests/dpdk_handle_reuse_test.cpp b/tests/dpdk_handle_reuse_test.cpp new file mode 100644 index 0000000..586af68 --- /dev/null +++ b/tests/dpdk_handle_reuse_test.cpp @@ -0,0 +1,196 @@ +// Copyright (C) 2026 EloqData Inc. +// SPDX-License-Identifier: Apache-2.0 +#include +#include + +#include +#include +#include +#include +#include + +#include "bycorf/io/dpdk_backend.h" +#include "bycorf/io/io.h" +#include "bycorf/net/connection.h" +#include "freebsd/abi.h" +#include "runtime_test.h" + +namespace { +std::string_view scenario; +unsigned send_calls = 0; +} // namespace + +// Real EAL/ring PMD, BSD socket creation/close and runtime completion delivery; +// only readiness is controlled so old operations deterministically stay queued. +extern "C" int __wrap_bycorf_bsd_send(struct socket*, const void*, size_t size, + size_t* sent) { + ++send_calls; + *sent = scenario == "completion" ? size : 0; + return *sent ? 0 : 35; // FreeBSD EAGAIN. +} +extern "C" int __wrap_bycorf_bsd_connect(struct socket*, uint32_t, uint16_t) { + return scenario == "connect-error" ? 61 : 36; // ECONNREFUSED / EINPROGRESS. +} + +namespace { +struct Completion final : bycorf::IoCompletion { + unsigned calls = 0; + int result = 0; + unsigned flags = 0; + int close_on_complete = -1; + int opened_in_callback = -1; + void Complete(bycorf::Worker&, int value, + unsigned completion_flags) override { + ++calls; + result = value; + flags = completion_flags; + if (close_on_complete >= 0) { + EXPECT_EQ(bycorf::DpdkBackend::Close(close_on_complete), 0); + opened_in_callback = bycorf::DpdkBackend::OpenClient(AF_INET); + EXPECT_GE(opened_in_callback, 0); + EXPECT_NE(opened_in_callback, close_on_complete); + } + } +}; + +Completion shutdown_completion; +std::array shutdown_bytes{}; + +bycorf::Task CheckReuse(bycorf::Worker& worker) { + using bycorf::DpdkBackend; + if (scenario == "churn") { + const int first = DpdkBackend::OpenClient(AF_INET); + if (first < 0) co_return absl::InternalError("initial socket failed"); + EXPECT_EQ(DpdkBackend::Close(first), 0); + for (unsigned i = 0; i < 256; ++i) { + const int next = DpdkBackend::OpenClient(AF_INET); + if (next < 0) co_return absl::InternalError("socket churn failed"); + EXPECT_EQ(next, first); + EXPECT_EQ(DpdkBackend::Close(next), 0); + } + co_return absl::OkStatus(); + } + + const int old = DpdkBackend::OpenClient(AF_INET); + if (old < 0) co_return absl::InternalError("initial socket failed"); + if (scenario == "shutdown") { + // This unregistered socket and completion outlive the request coroutine. + // Backend shutdown must still close and drain them exactly once. + co_return worker.SubmitSend({.fd_ = old}, shutdown_bytes, + &shutdown_completion); + } + Completion completion; + std::array bytes{}; + iovec vector{bytes.data(), bytes.size()}; + msghdr message{}; + message.msg_iov = &vector; + message.msg_iovlen = 1; + bycorf::Connection connection; + connection.worker_ = &worker; + connection.file_.fd_ = old; + connection.closed_ = false; + absl::Status submitted; + if (scenario == "sendv") { + submitted = worker.SubmitSendMsg({.fd_ = old}, &message, &completion); + } else if (scenario.starts_with("connect")) { + sockaddr_in address{}; + address.sin_family = AF_INET; + address.sin_addr.s_addr = htonl(0xc6120002); + address.sin_port = htons(12345); + submitted = + worker.SubmitConnect(old, reinterpret_cast(&address), + sizeof(address), &completion); + } else if (scenario == "accept") { + // Closing before the first poll cancels the request without entering BSD. + submitted = worker.SubmitAcceptMultishot(old, &completion); + } else if (scenario == "recv" || scenario == "recv-cancel") { + submitted = worker.EnsureRecvArmed(&connection); + if (submitted.ok() && scenario == "recv-cancel") + submitted = worker.SubmitCancelRecv(&connection, &completion); + } else if (scenario == "disconnect") { + submitted = worker.EnsurePeerDisconnectPollArmed(&connection); + } else { + submitted = worker.SubmitSend({.fd_ = old}, bytes, &completion); + } + if (!submitted.ok()) { + DpdkBackend::Close(old); + co_return submitted; + } + + if (scenario == "send" || scenario == "sendv" || scenario == "cancel") { + co_await bycorf::Yield(worker); + EXPECT_GT(send_calls, 0U); + EXPECT_EQ(completion.calls, 0U) << "EAGAIN must leave the old send pending"; + } + const unsigned attempts_before_close = send_calls; + + int successor = -1; + if (scenario == "completion") { + completion.close_on_complete = old; + } else { + if (scenario == "cancel") + EXPECT_TRUE(worker.SubmitCancel(&completion).ok()); + EXPECT_EQ(DpdkBackend::Close(old), 0); + successor = DpdkBackend::OpenClient(AF_INET); + EXPECT_GE(successor, 0); + EXPECT_NE(successor, old) << "pending operation still names the old socket"; + } + + // Yield returns to backend polling before resuming this coroutine. + co_await bycorf::Yield(worker); + if (scenario == "recv" || scenario == "disconnect") { + EXPECT_EQ(connection.inflight_ops_, 0U); + EXPECT_FALSE(connection.recv_armed_); + EXPECT_FALSE(connection.peer_disconnect_poll_armed_); + } else { + EXPECT_EQ(completion.calls, 1U); + EXPECT_EQ(completion.flags, 0U); // Closed multishot accept is terminal. + const int expected = + scenario == "recv-cancel" ? 0 + : scenario == "completion" ? static_cast(bytes.size()) + : scenario == "connect-error" ? -ECONNREFUSED + : scenario == "connect" || scenario == "cancel" ? -ECANCELED + : -EBADF; + EXPECT_EQ(completion.result, expected); + } + if (scenario == "completion") successor = completion.opened_in_callback; + EXPECT_EQ(send_calls, scenario == "completion" ? 1U : attempts_before_close) + << "a closed request must never send through the successor socket"; + const int reused = DpdkBackend::OpenClient(AF_INET); + EXPECT_EQ(reused, old) << "completion delivery must return the handle slot"; + if (reused >= 0) EXPECT_EQ(DpdkBackend::Close(reused), 0); + if (successor >= 0) EXPECT_EQ(DpdkBackend::Close(successor), 0); + co_return absl::OkStatus(); +} + +int RunScenario(std::string_view name) { + scenario = name; + setenv("BYCORF_EAL_ARGS", "--no-pci --no-huge --vdev=net_ring0", 1); + setenv("BYCORF_DPDK_MODE", "poll", 1); + setenv("BYCORF_DPDK_QUEUES", "1", 1); + setenv("BYCORF_DPDK_RX_STEERING", "hash", 1); + setenv("BYCORF_DPDK_IP", "198.18.0.2", 1); + setenv("BYCORF_DPDK_NETMASK", "255.255.255.0", 1); + setenv("BYCORF_DPDK_MEMORY_MB", "512", 1); + unsetenv("BYCORF_DPDK_GATEWAY"); + if (!bycorf::ConfigureIoBackends({.dpdk_network = true}).ok()) return 2; + bycorf::test::RunOnWorker(CheckReuse); + if (scenario == "shutdown") { + EXPECT_EQ(shutdown_completion.calls, 1U); + EXPECT_EQ(shutdown_completion.result, -EBADF); + } + return testing::Test::HasFailure() ? 1 : 0; +} + +class DpdkHandleReuseDeathTest : public testing::TestWithParam {}; +TEST_P(DpdkHandleReuseDeathTest, ReusesOnlyAfterOldOperationsRetire) { + // Backend selection/EAL have process lifetime; each case needs a fresh child. + const auto selected = GetParam(); + ASSERT_EXIT(std::exit(RunScenario(selected)), testing::ExitedWithCode(0), ""); +} +INSTANTIATE_TEST_SUITE_P(Scenarios, DpdkHandleReuseDeathTest, + testing::Values("churn", "send", "sendv", "connect", + "connect-error", "accept", "cancel", + "completion", "recv", "recv-cancel", + "disconnect", "shutdown")); +} // namespace diff --git a/tests/dpdk_socket_table_test.cpp b/tests/dpdk_socket_table_test.cpp new file mode 100644 index 0000000..b2ee991 --- /dev/null +++ b/tests/dpdk_socket_table_test.cpp @@ -0,0 +1,128 @@ +// Copyright (C) 2026 EloqData Inc. +// SPDX-License-Identifier: Apache-2.0 +#include "dpdk_socket_table.h" + +#include + +#include +#include +#include + +namespace bycorf::detail { +namespace { +// The table never dereferences a BSD socket; distinct addresses model distinct +// transports without EAL or a physical NIC. +struct socket* Socket(unsigned index) { + static std::array transports; + return reinterpret_cast(&transports.at(index)); +} + +TEST(DpdkSocketTableTest, ChurnDoesNotConsumeHandleNamespace) { + DpdkSocketTable table; + table.Configure(0x40000000, 2); + const int listener = table.Insert(Socket(0)); + for (unsigned i = 0; i < 10000; ++i) { + const int connection = table.Insert(Socket(1)); + ASSERT_EQ(connection, listener + 1); + EXPECT_EQ(table.Insert(Socket(2)), -1); + EXPECT_EQ(table.Close(connection), Socket(1)); + } + EXPECT_EQ(table.Close(listener), Socket(0)); +} + +TEST(DpdkSocketTableTest, CloseWaitsForEveryOperationAndCompletion) { + DpdkSocketTable table; + table.Configure(0x40000000, 1); + const int old = table.Insert(Socket(0)); + auto send = table.Acquire(old); + auto connect = table.Acquire(old); + auto completion = send; + EXPECT_EQ(table.Close(old), Socket(0)); + EXPECT_EQ(send.get(), nullptr); + EXPECT_FALSE(table.Acquire(old)); + EXPECT_EQ(table.Insert(Socket(1)), -1); + send.Reset(); + connect.Reset(); + EXPECT_EQ(table.Insert(Socket(1)), -1); + completion.Reset(); + EXPECT_EQ(table.Insert(Socket(1)), old); + EXPECT_EQ(table.Close(old), Socket(1)); +} + +TEST(DpdkSocketTableTest, CallbackCannotReuseItsOwnClosedHandle) { + DpdkSocketTable table; + table.Configure(0x40000000, 2); + const int old = table.Insert(Socket(0)); + int replacement; + { + auto completion = table.Acquire(old); + ASSERT_EQ(table.Close(old), Socket(0)); + replacement = table.Insert(Socket(1)); + EXPECT_NE(replacement, old); + EXPECT_EQ(completion.get(), nullptr); + } + EXPECT_EQ(table.Insert(Socket(2)), old); + EXPECT_EQ(table.Close(old), Socket(2)); + EXPECT_EQ(table.Close(replacement), Socket(1)); +} + +TEST(DpdkSocketTableTest, PinsSurviveTableAndQueueGrowth) { + DpdkSocketTable table; + table.Configure(0x40000000, 128); + std::vector operations; + std::vector handles; + for (unsigned i = 0; i < 128; ++i) { + handles.push_back(table.Insert(Socket(i))); + operations.push_back(table.Acquire(handles.back())); + } + for (unsigned i = 0; i < handles.size(); ++i) { + EXPECT_EQ(operations[i].get(), Socket(i)); + EXPECT_EQ(table.Close(handles[i]), Socket(i)); + EXPECT_EQ(operations[i].get(), nullptr); + } + EXPECT_EQ(table.Insert(Socket(128)), -1); + // Pending vectors compact on cancellation/completion; moving a pin must + // release its old slot exactly once and retain its replacement's identity. + operations.erase(operations.begin()); + EXPECT_EQ(table.Insert(Socket(128)), handles.front()); + EXPECT_EQ(table.Insert(Socket(129)), -1); + EXPECT_EQ(table.Close(handles.front()), Socket(128)); + operations.clear(); +} + +TEST(DpdkSocketTableTest, WorkerRangesAndRepeatedCloseStayIsolated) { + DpdkSocketTable first, second; + first.Configure(0x40000000, 2); + second.Configure(0x40800000, 2); + const int a = first.Insert(Socket(0)), b = second.Insert(Socket(1)); + EXPECT_EQ(first.Find(b), nullptr); + EXPECT_EQ(second.Close(a), nullptr); + EXPECT_EQ(first.Find(-1), nullptr); + EXPECT_EQ(first.Close(a), Socket(0)); + EXPECT_EQ(first.Close(a), nullptr); + EXPECT_EQ(first.Insert(Socket(2)), a); + EXPECT_NE(first.Insert(Socket(3)), a); + EXPECT_EQ(first.Insert(Socket(4)), -1); + first.CloseAll([](struct socket*) {}); + second.CloseAll([](struct socket*) {}); +} + +TEST(DpdkSocketTableTest, ShutdownClosesTransportsBeforeDrainingPins) { + DpdkSocketTable table; + table.Configure(0x40000000, 2); + auto a = table.Acquire(table.Insert(Socket(0))); + auto b = table.Acquire(table.Insert(Socket(1))); + unsigned closed = 0; + table.CloseAll([&](struct socket*) { ++closed; }); + EXPECT_EQ(closed, 2U); + EXPECT_EQ(a.get(), nullptr); + EXPECT_EQ(b.get(), nullptr); + EXPECT_EQ(table.Insert(Socket(2)), -1); + a.Reset(); + b.Reset(); + table.Configure(0x40800000, 1); + EXPECT_EQ(table.Insert(Socket(2)), 0x40800000); + EXPECT_EQ(table.Close(0x40800000), Socket(2)); +} +} // namespace +} // namespace bycorf::detail From 6f7ecbff95366967b86b56d245f848a8981445a6 Mon Sep 17 00:00:00 2001 From: Jerry Zhao Date: Sat, 7 Nov 2026 13:09:38 +0000 Subject: [PATCH 2/2] refactor(net): let DPDK allocation failures propagate --- src/io/dpdk_backend.cpp | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/src/io/dpdk_backend.cpp b/src/io/dpdk_backend.cpp index c3020d2..e2ada84 100644 --- a/src/io/dpdk_backend.cpp +++ b/src/io/dpdk_backend.cpp @@ -42,7 +42,6 @@ #include #include #include -#include #include #include #include @@ -284,14 +283,7 @@ class DpdkBackend::Impl { BsdSocket* Find(int handle) { return sockets.Find(handle); } int Own(BsdSocket* so) { - int handle; - try { - handle = sockets.Insert(so); - } catch (const std::bad_alloc&) { - bycorf_bsd_close(so); - errno = ENOMEM; - return -1; - } + const int handle = sockets.Insert(so); if (handle < 0) { bycorf_bsd_close(so); errno = EMFILE;