Skip to content

rpc : one send per message and an optional mutual TLS 1.3 transport - #309

Open
danielhanchen wants to merge 4 commits into
base/upstream-1ba568c5afrom
rpc-tls-transport-pin
Open

danielhanchen wants to merge 4 commits into
base/upstream-1ba568c5afrom
rpc-tls-transport-pin

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Oct 21, 2026 •

Copy link
Copy Markdown
Member

rpc : one send per message and an optional mutual TLS 1.3 transport

Two changes to ggml-rpc's transport, both opt in or invisible on the wire:

  1. One send per message. The TCP socket used to write every RPC command as three unbuffered send() calls (command byte, 8-byte size, payload) and every reply as two. It now holds the small pieces of one message (up to 64 KiB) and sends them at flush(), which every message already ends with; a larger payload goes out together with its header in one sendmsg / WSASend, without copying. Bytes on the wire are unchanged, so old and new peers interoperate. Anything held is flushed before a blocking recv, before the transport switches to RDMA, and on close. RDMA paths are untouched.
  2. Mutual TLS 1.3, built with -DGGML_RPC_TLS=ON. llama-server / any RPC client talks TLS straight to ggml-rpc-server, so a cluster on a home network needs no external relay in front of an unauthenticated rpc-server.

Why

Unsloth Studio's home cluster link currently puts a Python relay with mutual TLS on both ends of every RPC connection. Measured per token on the same VM (Qwen3-1.7B Q4_K_M, layer split over two rpc-servers, 3 alternating rounds, greedy text identical in every arm, A/A within about 1%):

ms/token vs plain RPC Colab G4 (48 vCPU) Kaggle 2x T4 (4 vCPU)
this transport (TLS inside ggml-rpc) 1.077x 1.093x
Python TLS relay, asyncio 1.10x to 1.12x 1.22x
same relay without TLS 1.03x to 1.04x 1.10x to 1.11x
socat OpenSSL relay on both hops 1.16x to 1.18x 1.29x to 1.32x

A 52 KB out / 16 KB back round trip costs 9 us over plain TCP and 20 us over TLS 1.3 in C on the G4 (AES-NI), so the TLS work itself is small; the relay hop was the cost. On the 4 vCPU VM the write coalescing alone also made plain RPC about 2.8% faster per token (12.29 vs 12.64 ms); on the 48 vCPU VM it made no measurable difference.

Contract

Server: ggml-rpc-server --tls-cert FILE --tls-key FILE --tls-ca FILE --tls-allow FILE (library: ggml_backend_rpc_start_server_tls(), which takes the certificate, key, CA and allowlist for that server alone, so servers started concurrently never share or swap a configuration). All four together; with them set, plaintext clients are refused. --tls-allow lists the SHA-256 fingerprints (hex, DER of the leaf certificate) of the clients allowed in, one per line, # comments allowed, and is re-read on every new connection, so a supervisor can revoke a client by rewriting it (restarting the server cuts a live session). Each accepted client is logged with its fingerprint. The flags appear in --help only in TLS builds, which lets a launcher probe for support; a non-TLS build rejects them with a clear error.

Client: an endpoint of the form tls://<server sha256>@host:port performs the handshake and pins that server certificate; the client certificate, key and CA come from GGML_RPC_TLS_CERT, GGML_RPC_TLS_KEY and GGML_RPC_TLS_CA, so no secret paths need to be on a command line. A plain host:port endpoint behaves exactly as before.

Security model

  • TLS 1.3 only, AES-128-GCM preferred, then AES-256-GCM; no session tickets.
  • Both sides require a peer certificate (SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT) chaining to the configured CA, and identity is the leaf fingerprint: the client pins the server, the server checks its allowlist. Host names play no part.
  • TLS 1.3 only: a context whose library cannot pin both protocol bounds is refused.
  • The whole handshake runs against one 10 s wall-clock deadline, so a slow or silent peer cannot hold the single accept loop.
  • Sessions end with close_notify, so a normal disconnect is not reported as a truncated stream.
  • Under TLS, RDMA is never negotiated and the server-to-server COMM_INIT links are refused, since they would be plaintext.

Build

  • -DGGML_RPC_TLS=ON links the system OpenSSL (find_package(OpenSSL)); an installed static ggml carries the OpenSSL dependency in ggml-config.cmake.
  • With the option off nothing changes.
  • A static BoringSSL build is left for a later change (it can reuse the existing LLAMA_BUILD_BORINGSSL setup): fetched as a subproject it builds its own tests in all and installs its own libssl / libcrypto / openssl/ headers into the prefix.

Tests

  • tests/test-rpc-tls.sh (Linux, certificates generated with openssl, every client run bounded so a hang fails): 14/14. Accepted client with a 64 MiB byte-exact SET/GET round trip and odd-offset small transfers; refusals for a client from another CA, a client not in the allowlist, a wrong server pin, a plaintext client to a TLS server, a TLS client to a plaintext server, and a revoked fingerprint; the server keeps serving after re-allowing; a plaintext server is unaffected; a normal disconnect ends with close_notify; a peer trickling one handshake byte per second is dropped at the deadline; two TLS servers started at the same moment in one process each serve their own certificate.
  • Static install: a downstream find_package(ggml) project linking ggml::ggml against an installed static TLS build links (53 undefined OpenSSL references without the ggml-config.cmake change).
  • ctest -R test-rpc: test-rpc-multi-server passes on the plain build, test-rpc-multi-server and test-rpc-tls on the TLS build.
  • Plain TCP, this commit against its upstream base 1ba568c (CPU builds, llama-server split over two local rpc-servers, greedy): client sends per generated token drop from 54.0 to 18.0 (strace, 40 minus 8 token runs), replies go from two sends to one; 40 token text is identical, and an old client with a new server and a new client with an old server both serve text identical to old with old.
  • End to end with real cluster certificates: llama-server split over two TLS rpc-servers gives greedy text identical to plain RPC; GPU runs on CUDA (Colab G4, Kaggle T4) as in the table.

Pin set

Merged on top of the current pin set (llama_pinset.py sim, tags b11776 and b11786) this change applies cleanly and every other pin keeps its lines. The simulation's only failure there is the existing Inkling pin (ggml-org#25731), which fails the same way without this change. Adding this pin to pr-set.json is a separate step.

Not covered yet

  • Windows and macOS builds of the TLS path (the socket is handed to SSL_set_fd as an int; macOS find_package(OpenSSL) could pick LibreSSL), ROCm / Vulkan / ARM builds, and a static BoringSSL option for prebuilt binaries.
  • A client writing to a socket the server already closed can still be stopped by SIGPIPE, as on the plain TCP path today.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-21T11:25:13.919966Z 96498f9 Manual request
🔒 Security Review ✅ Completed 2026-10-21T10:28:17.765717Z d5e6c08 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex security review

@chatgpt-codex-connector

Copy link
Copy Markdown

🛡️ Codex Security Review

Security review completed. No security issues were found in this pull request.

Reviewed commit: d5e6c0886c

View security finding report

Only the user who started this review can view the report in Codex.

ℹ️ About Codex security reviews in GitHub

This is an experimental Codex feature. Security reviews are triggered when:

  • You comment "@codex security review"
  • A regular code review gets triggered (for example, "@codex review" or when a PR is opened), and you’re opted in so security review runs alongside code review

Once complete, Codex will leave suggestions, or a comment if no findings are found.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: d5e6c0886c

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread ggml/src/ggml-rpc/transport.cpp Outdated
Comment on lines +1200 to +1201
tls_set_timeouts(pimpl->fd, TLS_HANDSHAKE_TIMEOUT_S);
if (SSL_accept(ssl) != 1) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Bound the entire TLS handshake

A peer can keep SSL_accept() running indefinitely by sending another partial handshake fragment before each 10-second socket receive timeout expires. Because the server performs this handshake synchronously inside its single accept loop, one unauthenticated slow client can prevent every subsequent client from connecting; enforce an absolute wall-clock deadline with nonblocking I/O or move handshakes out of the accept loop.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in f678a0a: the handshake now runs non-blocking against one wall-clock deadline (poll / WSAPoll for the remaining time on each WANT_READ / WANT_WRITE), so a peer trickling bytes is dropped at 10 s however it paces them. test-rpc-tls.sh now sends a handshake record header and then one byte per second: dropped after 11 s here, held for the full 40 s on the previous head.

Comment on lines +198 to +200
if (ssl) {
SSL_free(ssl);
ssl = nullptr;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Send close-notify before freeing TLS sessions

When a normally connected TLS client or server is destroyed, this frees the SSL object and closes the socket without sending a TLS close_notify. The peer therefore treats every successful disconnect as a truncated TLS stream (OpenSSL 3 reports unexpected eof while reading), causing routine sessions to emit TLS: read failed errors and making real transport failures difficult to distinguish; perform an orderly SSL_shutdown() before SSL_free().

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in f678a0a: a session with no failed read or write and nothing left queued sends close_notify (SSL_shutdown) before SSL_free, with a 1 s send timeout, and with SIGPIPE blocked around the write (a pending SIGPIPE it raises is consumed) so a peer that already reset cannot stop the process. test-rpc-tls.sh checks that a normal client disconnect no longer logs 'TLS: read failed' on the server (it did on the previous head).

Comment thread ggml/src/ggml-rpc/transport.cpp Outdated
Comment on lines +749 to +750
// the caps just written go over TCP: send them before the transport may switch to RDMA
tcp_flush();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Propagate capability-flush failures

During direct RDMA capability negotiation, send_data(local_caps, ...) now only queues the bytes, so this call is the first operation that can actually report the send failure. Its result is discarded, allowing COMM_INIT to report success and possibly switch to RDMA even though the peer never received the local capabilities; return the flush status and make the callers abort negotiation when it fails.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in f678a0a: update_caps now returns the flush status (TCP and TLS); HELLO on both sides and COMM_INIT on both ranks stop when it fails instead of reporting success.

Comment thread ggml/src/ggml-rpc/CMakeLists.txt Outdated
Comment on lines +77 to +78
find_package(OpenSSL REQUIRED)
target_link_libraries(ggml-rpc PRIVATE OpenSSL::SSL OpenSSL::Crypto)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Export OpenSSL for installed static RPC builds

With BUILD_SHARED_LIBS=OFF and GGML_RPC_TLS=ON, an installed ggml package reconstructs the ggml-rpc link interface without adding OpenSSL or calling find_dependency(OpenSSL). Consequently, downstream consumers that link ggml::ggml fail with undefined references to SSL_*, ERR_*, and X509_*; record the TLS dependency in ggml-config.cmake and the reconstructed RPC interface.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in f678a0a: for GGML_RPC + GGML_RPC_TLS without BoringSSL, ggml-config.cmake now calls find_dependency(OpenSSL) and sets GGML_RPC_INTERFACE_LINK_LIBRARIES to OpenSSL::SSL and OpenSSL::Crypto (LINK_ONLY). Checked with a static TLS build installed and a downstream find_package(ggml) consumer linking ggml::ggml: it links with this change and fails with 53 undefined SSL / ERR / X509 references without it.

Comment thread tools/rpc/CMakeLists.txt Outdated
set_property(TEST test-rpc-multi-server PROPERTY LABELS main)
endif()

if (LLAMA_BUILD_TESTS AND UNIX AND NOT GGML_BACKEND_DL AND GGML_RPC_TLS)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Restrict the Linux-only TLS test registration

On macOS, this UNIX condition registers a test whose script requires commands that the base system does not provide, including timeout, sha256sum, and seq. The TLS CTest therefore fails with command-not-found errors before exercising the transport; either restrict registration to Linux or replace/probe these commands with portable equivalents.

AGENTS.md reference: AGENTS.md:L19-L19

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in f678a0a: test-rpc-tls is registered only when CMAKE_SYSTEM_NAME is Linux, since the script uses GNU coreutils.

…ed caps flush, export OpenSSL for static installs

- The TLS handshake runs non-blocking against one wall-clock deadline, so a peer trickling bytes cannot hold the single accept loop.
- A healthy TLS session ends with close_notify (SIGPIPE-safe, bounded), so a normal disconnect is no longer a truncated stream.
- update_caps returns the flush status; HELLO and COMM_INIT stop when the caps could not be sent.
- ggml-config.cmake finds OpenSSL and adds it to ggml-rpc's interface for static TLS builds.
- test-rpc-tls is registered on Linux only (the script needs GNU coreutils).
- test-rpc-tls.sh checks the close_notify and the handshake deadline.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: f678a0a2a0

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread ggml/src/ggml-rpc/CMakeLists.txt Outdated
FetchContent_MakeAvailable(boringssl)
set(BUILD_SHARED_LIBS ${GGML_RPC_SAVED_SHARED})
set_directory_properties(PROPERTIES COMPILE_OPTIONS "${GGML_RPC_SAVED_OPTIONS}")
target_link_libraries(ggml-rpc PRIVATE ssl crypto)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Export BoringSSL for installed static builds

When BUILD_SHARED_LIBS=OFF and GGML_RPC_TLS_BORINGSSL=ON, linking ssl and crypto privately does not copy their objects into libggml-rpc.a. The fetched BoringSSL targets are neither installed nor reconstructed by ggml-config.cmake (which explicitly adds RPC TLS dependencies only for non-BoringSSL builds), so downstream users of the installed ggml::ggml target receive undefined SSL_* and crypto symbols. Merge the required objects into the installed archive or install and reconstruct the BoringSSL link dependencies.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in 1b219f6 by removing GGML_RPC_TLS_BORINGSSL from this change. Building it as a fetched subproject had two more problems besides this one: with BUILD_TESTING on, the default all target built BoringSSL's own tests, which failed under its -Werror here (_XOPEN_SOURCE redefined in ssl_c_test.c), and cmake --install installed BoringSSL's libssl.a, libcrypto.a, openssl/ headers, bssl and an OpenSSL CMake package into the prefix, shadowing a system OpenSSL. The transport is OpenSSL only for now; a static BoringSSL for prebuilt binaries can come later on top of the existing LLAMA_BUILD_BORINGSSL setup.

Comment thread ggml/src/ggml-rpc/transport.cpp Outdated
Comment on lines +1341 to +1345
if (st.ctx) {
SSL_CTX_free(st.ctx);
}
st.ctx = ctx;
st.allow_file = allow_file;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Keep TLS configuration per listening server

When an embedding application runs more than one RPC server, configuring the second server replaces this process-wide context and allowlist. Each existing listener consults the singleton again for every accepted connection, so the first server silently starts presenting the second server's certificate and enforcing its CA and allowlist; clients pinned to the first certificate then fail, and the intended authorization boundary changes. The public API describes this configuration as applying to the server started next, so transfer it into that listener's state instead of replacing shared global state.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 1b219f6: rpc_tls_server_configure() now prepares a configuration that ggml_backend_rpc_start_server() takes (and clears) as it starts, and every accept uses that listener's own config; COMM_INIT checks the client socket instead of a process-wide flag. test-rpc-tls gained a two-server mode: on the previous head the first server presented the second server's certificate after the second was configured (the pinned client failed), now each keeps its own.

Comment thread ggml/src/ggml-rpc/transport.cpp Outdated
Comment on lines +1043 to +1044
SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION);
SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Fail configuration when TLS 1.3 cannot be enforced

When the selected OpenSSL build has TLS 1.3 disabled or otherwise rejects these protocol bounds, both setters can return failure while the context keeps its default version range. Because the return values are ignored, the server and client can then negotiate an older TLS version even though this transport is advertised as TLS 1.3; check both calls and reject the context if either bound cannot be installed.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 1b219f6: tls_ctx_new() checks both SSL_CTX_set_min_proto_version and SSL_CTX_set_max_proto_version and refuses the context if either fails, so the server and client never fall back to an older TLS version.

… drop the BoringSSL option

- ggml_backend_rpc_server_set_tls() prepares the configuration for the server started next, which takes it; a later call no longer changes the certificate, CA or allowlist of a server already listening.
- tls_ctx_new() fails when the library cannot pin TLS 1.3 as both protocol bounds.
- GGML_RPC_TLS_BORINGSSL is removed: as a fetched subproject BoringSSL built its own tests in all and installed its own libssl, libcrypto and openssl headers into the prefix. OpenSSL only for now.
- test-rpc-tls gains a two-server mode; test-rpc-tls.sh checks each server keeps its own certificate.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 1b219f67fe

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread ggml/src/ggml-rpc/transport.cpp Outdated
Comment on lines +1353 to +1355
tls_pending_state & st = tls_pending();
std::lock_guard<std::mutex> lock(st.mu);
st.next = std::move(config);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Bind each pending TLS config to its server start

When an embedding starts multiple servers, ggml_backend_rpc_server_set_tls(A) followed by launching server A cannot safely be followed by configuring server B: server A does not consume the pending value until after backend initialization, so the second call can overwrite st.next first. Server A can then receive B's certificate while server B receives no configuration and silently starts as plaintext; the added two-server test only masks this race with a fixed two-second sleep. Associate the configuration directly with a particular start call rather than keeping a single overwriteable process-wide slot.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 96498f9: the process-wide slot is gone. ggml_backend_rpc_server_set_tls() is replaced by ggml_backend_rpc_start_server_tls(endpoint, cache_dir, n_threads, n_devices, devices, cert, key, ca, allow_file), which loads that server's configuration and hands it to that server alone; it returns false at once when the files cannot be loaded. rpc-server uses it (bad TLS files exit 1). The two-server test now starts both servers at the same moment with no pause, and each serves its own certificate. The API was introduced by this PR, so nothing upstream changes.

ggml_backend_rpc_server_set_tls() + ggml_backend_rpc_start_server() kept the configuration in a process-wide slot between the two calls, so a server configured before an earlier one had taken its setup could swap them. ggml_backend_rpc_start_server_tls() now takes the certificate, key, CA and allowlist for that server alone and returns false at once when they cannot be loaded. rpc-server uses it; the two-server test starts both servers at the same moment.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Hooray!

Reviewed commit: 96498f9146

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant