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
80
tests/unit/test_buf.c
Normal file
80
tests/unit/test_buf.c
Normal file
|
|
@ -0,0 +1,80 @@
|
|||
#include "naut/buf.h"
|
||||
#include "test.h"
|
||||
#include <pthread.h>
|
||||
#include <sched.h>
|
||||
#include <string.h>
|
||||
|
||||
/* Concurrency test: the owner thread allocates buffers and hands them to N
|
||||
* worker threads which put() them back. Mirrors recv-on-reactor / free-on-hash
|
||||
* -worker. At the end every buffer must be back on the freelist. */
|
||||
|
||||
#define NBLOCKS 1024
|
||||
#define NWORKERS 8
|
||||
|
||||
static naut_bufpool *pool;
|
||||
static _Atomic int bad_cap; /* worker-thread failures (harness CHECK is not MT-safe) */
|
||||
|
||||
struct chan { _Atomic(naut_buf *) slot[NBLOCKS]; _Atomic int head, tail; };
|
||||
static struct chan ch;
|
||||
|
||||
static void *worker(void *arg) {
|
||||
(void)arg;
|
||||
for (;;) {
|
||||
int t = atomic_load(&ch.tail);
|
||||
if (t >= NBLOCKS) break;
|
||||
if (!atomic_compare_exchange_weak(&ch.tail, &t, t + 1)) continue;
|
||||
/* spin until producer publishes slot t */
|
||||
naut_buf *b;
|
||||
while (!(b = atomic_load_explicit(&ch.slot[t], memory_order_acquire)))
|
||||
sched_yield();
|
||||
if (b->cap != NAUT_BLOCK) atomic_fetch_add(&bad_cap, 1);
|
||||
memset(b->data, 0xab, b->cap); /* touch the pages */
|
||||
naut_buf_put(b);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
pool = naut_bufpool_create(NAUT_BLOCK, NBLOCKS, false);
|
||||
CHECK(pool != NULL);
|
||||
CHECK_EQ(naut_bufpool_capacity(pool), NBLOCKS);
|
||||
CHECK_EQ(naut_bufpool_available(pool), NBLOCKS);
|
||||
|
||||
/* basic single-thread get/put */
|
||||
naut_buf *a = naut_buf_get(pool);
|
||||
CHECK(a != NULL);
|
||||
CHECK_EQ(naut_bufpool_available(pool), NBLOCKS - 1);
|
||||
naut_buf_ref(a); /* refcnt 1 -> 2 */
|
||||
naut_buf_put(a); /* 2 -> 1, stays out */
|
||||
CHECK_EQ(naut_bufpool_available(pool), NBLOCKS - 1);
|
||||
naut_buf_put(a); /* 1 -> 0, returns */
|
||||
CHECK_EQ(naut_bufpool_available(pool), NBLOCKS);
|
||||
|
||||
/* exhaustion */
|
||||
naut_buf *held[NBLOCKS];
|
||||
for (int i = 0; i < NBLOCKS; i++) { held[i] = naut_buf_get(pool); CHECK(held[i]); }
|
||||
CHECK_EQ(naut_bufpool_available(pool), 0);
|
||||
CHECK(naut_buf_get(pool) == NULL); /* empty => NULL, no crash */
|
||||
|
||||
/* concurrent producer/consumer */
|
||||
atomic_store(&ch.head, 0);
|
||||
atomic_store(&ch.tail, 0);
|
||||
for (int i = 0; i < NBLOCKS; i++)
|
||||
atomic_store_explicit(&ch.slot[i], NULL, memory_order_relaxed);
|
||||
|
||||
pthread_t th[NWORKERS];
|
||||
for (int i = 0; i < NWORKERS; i++) pthread_create(&th[i], NULL, worker, NULL);
|
||||
for (int i = 0; i < NBLOCKS; i++) /* publish */
|
||||
atomic_store_explicit(&ch.slot[i], held[i], memory_order_release);
|
||||
for (int i = 0; i < NWORKERS; i++) pthread_join(th[i], NULL);
|
||||
|
||||
CHECK_EQ(atomic_load(&bad_cap), 0);
|
||||
CHECK_EQ(naut_bufpool_available(pool), NBLOCKS);
|
||||
/* and we can drain the whole pool again afterwards */
|
||||
int got = 0;
|
||||
while (naut_buf_get(pool)) got++;
|
||||
CHECK_EQ(got, NBLOCKS);
|
||||
|
||||
naut_bufpool_destroy(pool);
|
||||
TEST_MAIN_END();
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue