Repository navigation
rpc : one send per message and an optional mutual TLS 1.3 transport - #309
danielhanchen wants to merge 4 commits into
Conversation
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
@codex review |
|
@codex security review |
🛡️ Codex Security ReviewSecurity review completed. No security issues were found in this pull request. Reviewed commit: Only the user who started this review can view the report in Codex. ℹ️ About Codex security reviews in GitHubThis is an experimental Codex feature. Security reviews are triggered when:
Once complete, Codex will leave suggestions, or a comment if no findings are found. |
There was a problem hiding this comment.
💡 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".
| tls_set_timeouts(pimpl->fd, TLS_HANDSHAKE_TIMEOUT_S); | ||
| if (SSL_accept(ssl) != 1) { |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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.
| if (ssl) { | ||
| SSL_free(ssl); | ||
| ssl = nullptr; |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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).
| // the caps just written go over TCP: send them before the transport may switch to RDMA | ||
| tcp_flush(); |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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.
| find_package(OpenSSL REQUIRED) | ||
| target_link_libraries(ggml-rpc PRIVATE OpenSSL::SSL OpenSSL::Crypto) |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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.
| 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) |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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.
|
@codex review |
There was a problem hiding this comment.
💡 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".
| 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) |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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.
| if (st.ctx) { | ||
| SSL_CTX_free(st.ctx); | ||
| } | ||
| st.ctx = ctx; | ||
| st.allow_file = allow_file; |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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.
| SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION); | ||
| SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION); |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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.
|
@codex review |
There was a problem hiding this comment.
💡 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".
| tls_pending_state & st = tls_pending(); | ||
| std::lock_guard<std::mutex> lock(st.mu); | ||
| st.next = std::move(config); |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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.
|
@codex review |
|
Codex Review: Didn't find any major issues. Hooray! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
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: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 atflush(), which every message already ends with; a larger payload goes out together with its header in onesendmsg/WSASend, without copying. Bytes on the wire are unchanged, so old and new peers interoperate. Anything held is flushed before a blockingrecv, before the transport switches to RDMA, and on close. RDMA paths are untouched.-DGGML_RPC_TLS=ON.llama-server/ any RPC client talks TLS straight toggml-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%):
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-allowlists 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--helponly 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:portperforms the handshake and pins that server certificate; the client certificate, key and CA come fromGGML_RPC_TLS_CERT,GGML_RPC_TLS_KEYandGGML_RPC_TLS_CA, so no secret paths need to be on a command line. A plainhost:portendpoint behaves exactly as before.Security model
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.COMM_INITlinks are refused, since they would be plaintext.Build
-DGGML_RPC_TLS=ONlinks the system OpenSSL (find_package(OpenSSL)); an installed static ggml carries the OpenSSL dependency inggml-config.cmake.LLAMA_BUILD_BORINGSSLsetup): fetched as a subproject it builds its own tests inalland installs its ownlibssl/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.find_package(ggml)project linkingggml::ggmlagainst an installed static TLS build links (53 undefined OpenSSL references without theggml-config.cmakechange).ctest -R test-rpc:test-rpc-multi-serverpasses on the plain build,test-rpc-multi-serverandtest-rpc-tlson the TLS build.llama-serversplit 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.llama-serversplit 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 topr-set.jsonis a separate step.Not covered yet
SSL_set_fdas anint; macOSfind_package(OpenSSL)could pick LibreSSL), ROCm / Vulkan / ARM builds, and a static BoringSSL option for prebuilt binaries.