Initial commit: Naut-Torrent — from-scratch 10 GbE BitTorrent client
A maintainable, extensible BitTorrent client (C11, Linux/io_uring) targeting 10 GbE saturation. All torrent functionality is built from scratch; liburing is the only linked third-party dependency on the data path. Implements Phases 1-7 of the roadmap: - core: page-aligned buffer pool, MPMC/Treiber queues, bitfields, worker pool - crypto: SHA-1/256 (SHA-NI + scalar), Merkle (BEP-52), RC4 (MSE) - bencode/metainfo: zero-copy parser, v1/v2/hybrid .torrent + magnet - peer: sans-IO wire codec, MSE/PE handshake state machine, BEP-10, ut_metadata, PEX - piece/storage: block-level multi-peer engine, rarest-first + endgame, per-file completion events + single-file relocate (move-as-you-finish) - tracker/dht: HTTP + UDP (BEP-15) trackers, BEP-5 KRPC iterative lookup - platform: io_uring reactor (SQPOLL, registered buffers, SEND_ZC) - surface: versioned RPC, native plugin ABI, sandboxed Lua scripting, nautd/nautctl Verified against libtorrent (single/multi/hybrid, MSE, magnet-via-DHT, swarm); unit + interop tests green; ASan/UBSan/TSan clean. Scripting reference in docs/scripting.md. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
commit
2178d6a70c
121 changed files with 12644 additions and 0 deletions
62
src/platform/net.c
Normal file
62
src/platform/net.c
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
#include "naut/net.h"
|
||||
#include "naut/log.h"
|
||||
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <netinet/tcp.h>
|
||||
#include <sys/socket.h>
|
||||
|
||||
static bool setopt(int fd, int level, int opt, int val) {
|
||||
return setsockopt(fd, level, opt, &val, sizeof(val)) == 0;
|
||||
}
|
||||
|
||||
int naut_net_listen(uint16_t port, int backlog, bool reuseport) {
|
||||
int fd = socket(AF_INET6, SOCK_STREAM, 0);
|
||||
if (fd < 0) { NAUT_ERROR("socket: %s", strerror(errno)); return -1; }
|
||||
|
||||
setopt(fd, SOL_SOCKET, SO_REUSEADDR, 1);
|
||||
if (reuseport && !setopt(fd, SOL_SOCKET, SO_REUSEPORT, 1))
|
||||
NAUT_WARN("SO_REUSEPORT unavailable: %s", strerror(errno));
|
||||
/* dual-stack v4+v6 */
|
||||
setopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, 0);
|
||||
|
||||
struct sockaddr_in6 a;
|
||||
memset(&a, 0, sizeof(a));
|
||||
a.sin6_family = AF_INET6;
|
||||
a.sin6_addr = in6addr_any;
|
||||
a.sin6_port = htons(port);
|
||||
if (bind(fd, (struct sockaddr *)&a, sizeof(a)) != 0) {
|
||||
NAUT_ERROR("bind(:%u): %s", port, strerror(errno));
|
||||
close(fd);
|
||||
return -1;
|
||||
}
|
||||
if (listen(fd, backlog) != 0) {
|
||||
NAUT_ERROR("listen: %s", strerror(errno));
|
||||
close(fd);
|
||||
return -1;
|
||||
}
|
||||
return fd;
|
||||
}
|
||||
|
||||
void naut_net_tune_peer(int fd) {
|
||||
setopt(fd, IPPROTO_TCP, TCP_NODELAY, 1);
|
||||
setopt(fd, SOL_SOCKET, SO_SNDBUF, 4 * 1024 * 1024);
|
||||
setopt(fd, SOL_SOCKET, SO_RCVBUF, 4 * 1024 * 1024);
|
||||
#ifdef TCP_QUICKACK
|
||||
setopt(fd, IPPROTO_TCP, TCP_QUICKACK, 1);
|
||||
#endif
|
||||
}
|
||||
|
||||
void naut_net_set_bufsizes(int fd, int sndbuf, int rcvbuf) {
|
||||
if (sndbuf > 0) setopt(fd, SOL_SOCKET, SO_SNDBUF, sndbuf);
|
||||
if (rcvbuf > 0) setopt(fd, SOL_SOCKET, SO_RCVBUF, rcvbuf);
|
||||
}
|
||||
|
||||
int naut_net_set_nonblock(int fd, bool on) {
|
||||
int fl = fcntl(fd, F_GETFL, 0);
|
||||
if (fl < 0) return NAUT_ERR_IO;
|
||||
fl = on ? (fl | O_NONBLOCK) : (fl & ~O_NONBLOCK);
|
||||
return fcntl(fd, F_SETFL, fl) == 0 ? NAUT_OK : NAUT_ERR_IO;
|
||||
}
|
||||
19
src/platform/system.c
Normal file
19
src/platform/system.c
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
#include "naut/system.h"
|
||||
|
||||
#include <sched.h>
|
||||
#include <unistd.h>
|
||||
|
||||
naut_err naut_pin_current_thread(int cpu) {
|
||||
int online = naut_online_cpus();
|
||||
if (cpu < 0 || cpu >= online) return NAUT_ERR_RANGE;
|
||||
cpu_set_t set;
|
||||
CPU_ZERO(&set);
|
||||
CPU_SET((unsigned)cpu, &set);
|
||||
return sched_setaffinity(0, sizeof set, &set) == 0
|
||||
? NAUT_OK : NAUT_ERR_IO;
|
||||
}
|
||||
|
||||
int naut_online_cpus(void) {
|
||||
long count = sysconf(_SC_NPROCESSORS_ONLN);
|
||||
return count > 0 && count <= INT32_MAX ? (int)count : 1;
|
||||
}
|
||||
151
src/platform/uring.c
Normal file
151
src/platform/uring.c
Normal file
|
|
@ -0,0 +1,151 @@
|
|||
#include "naut/uring.h"
|
||||
#include "naut/log.h"
|
||||
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <sys/uio.h>
|
||||
|
||||
naut_err naut_ring_init(naut_ring *r, unsigned entries, bool sqpoll) {
|
||||
return naut_ring_init_cpu(r, entries, sqpoll, -1);
|
||||
}
|
||||
|
||||
/* Build the params for one setup attempt. COOP_TASKRUN runs completion task
|
||||
* work in the submitter's context to cut IPIs when one thread owns the ring —
|
||||
* but it is *mutually exclusive* with SQPOLL (a kernel thread submits there, so
|
||||
* there is no cooperative submitter context), and the kernel rejects the combo
|
||||
* with -EINVAL. So pick exactly one of the two. */
|
||||
static void ring_params(struct io_uring_params *p, bool sqpoll, int sqpoll_cpu) {
|
||||
memset(p, 0, sizeof(*p));
|
||||
p->flags = IORING_SETUP_SINGLE_ISSUER;
|
||||
if (sqpoll) {
|
||||
p->flags |= IORING_SETUP_SQPOLL;
|
||||
p->sq_thread_idle = 1000; /* ms before the poll thread sleeps */
|
||||
if (sqpoll_cpu >= 0) {
|
||||
p->flags |= IORING_SETUP_SQ_AFF;
|
||||
p->sq_thread_cpu = (unsigned)sqpoll_cpu;
|
||||
}
|
||||
} else {
|
||||
p->flags |= IORING_SETUP_COOP_TASKRUN;
|
||||
}
|
||||
}
|
||||
|
||||
naut_err naut_ring_init_cpu(naut_ring *r, unsigned entries, bool sqpoll,
|
||||
int sqpoll_cpu) {
|
||||
struct io_uring_params p;
|
||||
ring_params(&p, sqpoll, sqpoll_cpu);
|
||||
|
||||
int rc = io_uring_queue_init_params(entries, &r->ring, &p);
|
||||
if (rc < 0 && sqpoll) {
|
||||
NAUT_WARN("io_uring SQPOLL setup failed (%s), retrying without it",
|
||||
strerror(-rc));
|
||||
ring_params(&p, false, -1);
|
||||
rc = io_uring_queue_init_params(entries, &r->ring, &p);
|
||||
sqpoll = false;
|
||||
}
|
||||
if (rc < 0) {
|
||||
/* Older fallbacks: SINGLE_ISSUER needs ~6.0; we have it, but be safe. */
|
||||
rc = io_uring_queue_init(entries, &r->ring, 0);
|
||||
if (rc < 0) {
|
||||
NAUT_ERROR("io_uring_queue_init: %s", strerror(-rc));
|
||||
return NAUT_ERR_NOSYS;
|
||||
}
|
||||
sqpoll = false;
|
||||
}
|
||||
r->sqpoll = sqpoll;
|
||||
r->send_zc = false;
|
||||
r->msg_ring = false;
|
||||
r->buffers_registered = false;
|
||||
r->recv_fixed = false;
|
||||
NAUT_INFO("io_uring: %u entries%s", entries, sqpoll ? ", sqpoll" : "");
|
||||
return NAUT_OK;
|
||||
}
|
||||
|
||||
void naut_ring_close(naut_ring *r) {
|
||||
naut_ring_unregister_buffers(r);
|
||||
io_uring_queue_exit(&r->ring);
|
||||
}
|
||||
|
||||
naut_err naut_ring_probe(naut_ring *r) {
|
||||
struct io_uring_probe *p = io_uring_get_probe();
|
||||
if (!p) { NAUT_WARN("io_uring_get_probe failed"); return NAUT_OK; }
|
||||
|
||||
struct { int op; const char *name; bool required; } want[] = {
|
||||
{ IORING_OP_RECV, "recv", true },
|
||||
{ IORING_OP_SEND, "send", true },
|
||||
{ IORING_OP_ACCEPT, "accept", true },
|
||||
{ IORING_OP_READ_FIXED, "read_fixed", true },
|
||||
{ IORING_OP_WRITE_FIXED, "write_fixed", true },
|
||||
{ IORING_OP_SEND_ZC, "send_zc", false }, /* seed fast-path */
|
||||
{ IORING_OP_MSG_RING, "msg_ring", false }, /* cross-ring wake */
|
||||
};
|
||||
naut_err result = NAUT_OK;
|
||||
for (size_t i = 0; i < NAUT_ARRAY_LEN(want); i++) {
|
||||
bool ok = io_uring_opcode_supported(p, want[i].op);
|
||||
if (want[i].op == IORING_OP_SEND_ZC) r->send_zc = ok;
|
||||
if (want[i].op == IORING_OP_MSG_RING) r->msg_ring = ok;
|
||||
if (!ok && want[i].required) {
|
||||
NAUT_ERROR("io_uring missing required op: %s", want[i].name);
|
||||
result = NAUT_ERR_NOSYS;
|
||||
} else if (!ok) {
|
||||
NAUT_WARN("io_uring optional op unavailable: %s (degraded)", want[i].name);
|
||||
}
|
||||
}
|
||||
io_uring_free_probe(p);
|
||||
NAUT_INFO("io_uring optional features: send_zc=%s, msg_ring=%s",
|
||||
r->send_zc ? "yes" : "no",
|
||||
r->msg_ring ? "yes" : "no");
|
||||
return result;
|
||||
}
|
||||
|
||||
naut_err naut_ring_register_bufpool(naut_ring *r, const naut_bufpool *pool) {
|
||||
if (!r || !pool || r->buffers_registered) return NAUT_ERR_INVAL;
|
||||
size_t bytes = 0;
|
||||
void *slab = naut_bufpool_slab(pool, &bytes);
|
||||
if (!slab || bytes == 0) return NAUT_ERR_INVAL;
|
||||
struct iovec iov = { .iov_base = slab, .iov_len = bytes };
|
||||
int rc = io_uring_register_buffers(&r->ring, &iov, 1);
|
||||
if (rc < 0) {
|
||||
NAUT_WARN("io_uring fixed-buffer registration failed: %s",
|
||||
strerror(-rc));
|
||||
return rc == -ENOMEM || rc == -EPERM ? NAUT_ERR_NOSYS : NAUT_ERR_IO;
|
||||
}
|
||||
r->buffers_registered = true;
|
||||
r->recv_fixed = true;
|
||||
NAUT_INFO("io_uring: registered %zu MiB buffer slab", bytes >> 20);
|
||||
return NAUT_OK;
|
||||
}
|
||||
|
||||
void naut_ring_unregister_buffers(naut_ring *r) {
|
||||
if (!r || !r->buffers_registered) return;
|
||||
int rc = io_uring_unregister_buffers(&r->ring);
|
||||
if (rc < 0)
|
||||
NAUT_WARN("io_uring unregister buffers: %s", strerror(-rc));
|
||||
r->buffers_registered = false;
|
||||
r->recv_fixed = false;
|
||||
}
|
||||
|
||||
bool naut_ring_prep_send(naut_ring *r, struct io_uring_sqe *sqe, int fd,
|
||||
const void *buf, size_t len, int flags,
|
||||
bool prefer_zero_copy) {
|
||||
if (prefer_zero_copy && r && r->send_zc) {
|
||||
if (r->buffers_registered)
|
||||
io_uring_prep_send_zc_fixed(sqe, fd, buf, len, flags, 0, 0);
|
||||
else
|
||||
io_uring_prep_send_zc(sqe, fd, buf, len, flags, 0);
|
||||
sqe->ioprio |= IORING_SEND_ZC_REPORT_USAGE;
|
||||
return true;
|
||||
}
|
||||
io_uring_prep_send(sqe, fd, buf, len, flags);
|
||||
return false;
|
||||
}
|
||||
|
||||
bool naut_ring_prep_recv(naut_ring *r, struct io_uring_sqe *sqe, int fd,
|
||||
void *buf, size_t len, int flags) {
|
||||
io_uring_prep_recv(sqe, fd, buf, len, flags);
|
||||
if (r && r->buffers_registered && r->recv_fixed) {
|
||||
sqe->ioprio |= IORING_RECVSEND_FIXED_BUF;
|
||||
sqe->buf_index = 0;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue