Skip to content
Core Design

VSI Core Engine Architecture

How vsi_ngine separates TCP acceptance and process supervision from protocol parsing and forwarding services.

System Topology

TCP ClientHTTP/1.x · CONNECTVSI CORE ENGINE (vsi_ngine)Config & Fork Supervisormain.c · vsi_config.cL4 TCP Transport Loopvsi_transport_start()accept() -> vsi_connection_cbForward Proxyforward_proxy_on_connectionHTTP/1.x + CONNECT select()Reverse Proxyreverse_proxy_on_connectionV2 Host rewrite · up to 16 portsOrigin Servergetaddrinfo() TCPBackend Targethttp://host:portShared mmap() Atomic Metrics (vsi_metrics.c) -> GET :9100/metrics JSON
Per-Connection LifecycleCoreEngine -> http1_codec -> Service -> close(client_fd)
1. Accept
accept() in vsi_transport.c
2. Dispatch
Detached pthread worker
3. Parse Headers
http1_read_and_parse_headers()
4. Transform & DNS
Origin-form / Host rewrite
5. Upstream Relay
Stream body or select() tunnel
6. Telemetry & Close
vsi_metrics_record_request()

1. Configuration & Process Supervision (src/main.c)

Startup begins in vsi_config_parse_args(), which loads config.json (or CLI flags) via vendored cJSON, validates IPv4 addresses, and rejects overlapping port bindings across services and the metrics server.

When a single service is active, vsi_ngine runs it directly in the main process. When both Forward Proxy and Reverse Proxy are enabled, run_multi_service_supervisor() forks one isolated child process per service while still single-threaded, then starts the metrics thread and monitors children via waitpid(). On SIGINT or SIGTERM, the supervisor forwards SIGTERM to all child PIDs.

2. Layer 4 TCP Transport (vsi_transport.c)

vsi_transport_start() ignores SIGPIPE, creates an AF_INET SOCK_STREAM socket with SO_REUSEADDR, binds the configured IPv4 address, and enters listen() with the configured backlog (default 128).

Each accepted client_fd is handed to a detached POSIX thread (PTHREAD_CREATE_DETACHED) that invokes the resolved vsi_connection_cb. A bounded circular queue (WorkerManager.c, MAX_WORKERS = 64) is in development to replace per-connection thread creation.

3. HTTP/1.x Header Parser & Streaming Codec (http1_x/)

Both proxy services call http1_read_and_parse_headers(), which reads up to 16 KB (HTTP1_HEADER_BUFFER_SIZE) until \r\n\r\n, parses the request line and up to 32 headers via parse_http_1_0(), and determines body framing (Content-Length or Transfer-Encoding: chunked). Request bodies of arbitrary size are streamed with constant 64 KB buffers via http1_stream_request_body().

4. Shared-Memory Atomic Telemetry (vsi_metrics.c)

vsi_metrics_init() maps a MAP_SHARED | MAP_ANONYMOUS region containing cache-line-aligned (_Alignas(64)) C11 atomic counters before fork(). Both child processes record byte counts and CLOCK_MONOTONIC_RAW latencies lock-free, while the detached metrics thread serves JSON at GET /metrics.

Service Dispatch Interface
/* CoreEngine/transport/vsi_transport.h */
typedef void (*vsi_connection_cb)(int client_fd, void *user_ctx);
/* CoreEngine/config/vsi_config.c */
vsi_connection_cb vsi_config_resolve_handler(vsi_service_mode_t mode) {
switch (mode) {
case VSI_MODE_FORWARD_PROXY:
return forward_proxy_on_connection;
case VSI_MODE_REVERSE_PROXY:
return reverse_proxy_on_connection;
default:
return NULL;
}
}

Inspect Further in Documentation

See exact buffer sizes, C source file mappings, and extension instructions for adding new services.