Skip to content

openssl: create SSL object once per thread in SSL_get_cipher_by_value - #48956

Merged
jwendell merged 2 commits into
envoyproxy:mainfrom
NMJlam:openssl-cipher-by-value-static
Nov 10, 2026
Merged

jwendell merged 2 commits into
envoyproxy:mainfrom
NMJlam:openssl-cipher-by-value-static

Conversation

@NMJlam

@NMJlam NMJlam commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Commit Message: openssl: create SSL object once per thread in SSL_get_cipher_by_value

Additional Description:
OpenSSL can only look up a cipher through an SSL object. The compat implementation of SSL_get_cipher_by_value created and freed a new SSL_CTX and SSL on every call. Envoy calls it once per offered cipher suite during TLS 1.2 certificate selection, meaning a single handshake could create many SSL_CTX objects.

This change creates one SSL object per thread on first use, with a thread_local bssl::UniquePtr, and reuses it for later lookups on that thread. OpenSSL does not guarantee that SSL_CIPHER_find() is thread safe, so threads do not share the object. It is freed when its thread exits.

Fixes #48858

Risk Level: Low (only affects --config=openssl builds)

Testing:

bazel test --config=openssl //compat/openssl/test/...
bazel test --config=openssl //test/common/tls:ssl_socket_test //test/common/tls:context_impl_test

Micro-benchmark (10,000 calls to SSL_get_cipher_by_value(0xC02B), --config=openssl):

  • before: 188.77 us/call
  • after: 0.34 us/call (~550x faster)

The thread_local version benchmarks about the same.

Docs Changes: N/A
Release Notes: N/A

AI Disclosure: Claude Code was used to help explain the codebase, simplify the implementation, write the throwaway benchmark, and draft this description. I have reviewed, tested, and understand all of the code in this PR.

@repokitteh-read-only

Copy link
Copy Markdown

As a reminder, PRs marked as draft will not be automatically assigned reviewers,
or be handled by maintainer-oncall triage.

Please mark your PR as ready when you want it to be reviewed!

🐱

Caused by: #48956 was opened by NMJlam.

see: more, trace.

OpenSSL can only look up a cipher through an SSL object. The compat
implementation created and freed a new SSL_CTX and SSL on every call,
and Envoy calls it once per offered cipher suite during TLS 1.2
certificate selection. Create the SSL object once on first use and
reuse it for all later lookups.

Fixes envoyproxy#48858

Signed-off-by: NMJlam <[email protected]>
@NMJlam
NMJlam force-pushed the openssl-cipher-by-value-static branch from 74429be to 9eb241f Compare November 3, 2026 09:06
@NMJlam
NMJlam marked this pull request as ready for review November 3, 2026 09:30
@NMJlam

NMJlam commented Nov 3, 2026

Copy link
Copy Markdown
Contributor Author

cc: @ggreenway @tedjpoole

@NMJlam
NMJlam force-pushed the openssl-cipher-by-value-static branch from 9eb241f to 8ad3a92 Compare November 4, 2026 22:35
@NMJlam NMJlam changed the title openssl: create SSL object once in SSL_get_cipher_by_value openssl: create SSL object once per thread in SSL_get_cipher_by_value Nov 4, 2026
OpenSSL does not guarantee that SSL_CIPHER_find() is thread safe, so cache one SSL object per thread instead of sharing one across threads. The object is now freed at thread exit.

Signed-off-by: NMJlam <[email protected]>
@NMJlam
NMJlam force-pushed the openssl-cipher-by-value-static branch from 8ad3a92 to 11ded6f Compare November 4, 2026 23:37
@NMJlam
NMJlam requested a review from tedjpoole November 5, 2026 00:43
@RyanTheOptimist

Copy link
Copy Markdown
Contributor

/assign @ggreenway

@tedjpoole tedjpoole left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nice fix thanks

@NMJlam

NMJlam commented Nov 9, 2026

Copy link
Copy Markdown
Contributor Author

@greenway bumping this, it's all green and has code owner approval. Could you take a look when you get a chance? Thanks!

@ggreenway

Copy link
Copy Markdown
Member

I'm badly backed up on PR reviews. I'll get to it eventually

@jwendell
jwendell merged commit 3d18e34 into envoyproxy:main Nov 10, 2026
27 checks passed
phlax pushed a commit to phlax/envoy that referenced this pull request Nov 11, 2026
…envoyproxy#48956)

Commit Message: openssl: create SSL object once per thread in
SSL_get_cipher_by_value

Additional Description:
OpenSSL can only look up a cipher through an SSL object. The compat
implementation of SSL_get_cipher_by_value created and freed a new
SSL_CTX and SSL on every call. Envoy calls it once per offered cipher
suite during TLS 1.2 certificate selection, meaning a single handshake
could create many SSL_CTX objects.

This change creates one SSL object per thread on first use, with a
`thread_local bssl::UniquePtr`, and reuses it for later lookups on that
thread. OpenSSL does not guarantee that SSL_CIPHER_find() is thread
safe, so threads do not share the object. It is freed when its thread
exits.

Fixes envoyproxy#48858

Risk Level: Low (only affects --config=openssl builds)

Testing:
```bash
bazel test --config=openssl //compat/openssl/test/...
bazel test --config=openssl //test/common/tls:ssl_socket_test //test/common/tls:context_impl_test
```
Micro-benchmark (10,000 calls to SSL_get_cipher_by_value(0xC02B),
--config=openssl):
- before: 188.77 us/call
- after: 0.34 us/call (~550x faster)

*The thread_local version benchmarks about the same.*

Docs Changes: N/A
Release Notes: N/A

AI Disclosure: Claude Code was used to help explain the codebase,
simplify the implementation, write the throwaway benchmark, and draft
this description. I have reviewed, tested, and understand all of the
code in this PR.

---------

Signed-off-by: NMJlam <[email protected]>
phlax pushed a commit to phlax/envoy that referenced this pull request Nov 11, 2026
…envoyproxy#48956)

Commit Message: openssl: create SSL object once per thread in
SSL_get_cipher_by_value

Additional Description:
OpenSSL can only look up a cipher through an SSL object. The compat
implementation of SSL_get_cipher_by_value created and freed a new
SSL_CTX and SSL on every call. Envoy calls it once per offered cipher
suite during TLS 1.2 certificate selection, meaning a single handshake
could create many SSL_CTX objects.

This change creates one SSL object per thread on first use, with a
`thread_local bssl::UniquePtr`, and reuses it for later lookups on that
thread. OpenSSL does not guarantee that SSL_CIPHER_find() is thread
safe, so threads do not share the object. It is freed when its thread
exits.

Fixes envoyproxy#48858

Risk Level: Low (only affects --config=openssl builds)

Testing:
```bash
bazel test --config=openssl //compat/openssl/test/...
bazel test --config=openssl //test/common/tls:ssl_socket_test //test/common/tls:context_impl_test
```
Micro-benchmark (10,000 calls to SSL_get_cipher_by_value(0xC02B),
--config=openssl):
- before: 188.77 us/call
- after: 0.34 us/call (~550x faster)

*The thread_local version benchmarks about the same.*

Docs Changes: N/A
Release Notes: N/A

AI Disclosure: Claude Code was used to help explain the codebase,
simplify the implementation, write the throwaway benchmark, and draft
this description. I have reviewed, tested, and understand all of the
code in this PR.

---------

Signed-off-by: NMJlam <[email protected]>
Signed-off-by: Ryan Northey <[email protected]>
phlax pushed a commit to phlax/envoy that referenced this pull request Nov 11, 2026
…envoyproxy#48956)

Commit Message: openssl: create SSL object once per thread in
SSL_get_cipher_by_value

Additional Description:
OpenSSL can only look up a cipher through an SSL object. The compat
implementation of SSL_get_cipher_by_value created and freed a new
SSL_CTX and SSL on every call. Envoy calls it once per offered cipher
suite during TLS 1.2 certificate selection, meaning a single handshake
could create many SSL_CTX objects.

This change creates one SSL object per thread on first use, with a
`thread_local bssl::UniquePtr`, and reuses it for later lookups on that
thread. OpenSSL does not guarantee that SSL_CIPHER_find() is thread
safe, so threads do not share the object. It is freed when its thread
exits.

Fixes envoyproxy#48858

Risk Level: Low (only affects --config=openssl builds)

Testing:
```bash
bazel test --config=openssl //compat/openssl/test/...
bazel test --config=openssl //test/common/tls:ssl_socket_test //test/common/tls:context_impl_test
```
Micro-benchmark (10,000 calls to SSL_get_cipher_by_value(0xC02B),
--config=openssl):
- before: 188.77 us/call
- after: 0.34 us/call (~550x faster)

*The thread_local version benchmarks about the same.*

Docs Changes: N/A
Release Notes: N/A

AI Disclosure: Claude Code was used to help explain the codebase,
simplify the implementation, write the throwaway benchmark, and draft
this description. I have reviewed, tested, and understand all of the
code in this PR.

---------

Signed-off-by: NMJlam <[email protected]>
Signed-off-by: Ryan Northey <[email protected]>
jwendell pushed a commit that referenced this pull request Nov 11, 2026
…#48956)

Commit Message: openssl: create SSL object once per thread in
SSL_get_cipher_by_value

Additional Description:
OpenSSL can only look up a cipher through an SSL object. The compat
implementation of SSL_get_cipher_by_value created and freed a new
SSL_CTX and SSL on every call. Envoy calls it once per offered cipher
suite during TLS 1.2 certificate selection, meaning a single handshake
could create many SSL_CTX objects.

This change creates one SSL object per thread on first use, with a
`thread_local bssl::UniquePtr`, and reuses it for later lookups on that
thread. OpenSSL does not guarantee that SSL_CIPHER_find() is thread
safe, so threads do not share the object. It is freed when its thread
exits.

Fixes #48858

Risk Level: Low (only affects --config=openssl builds)

Testing:
```bash
bazel test --config=openssl //compat/openssl/test/...
bazel test --config=openssl //test/common/tls:ssl_socket_test //test/common/tls:context_impl_test
```
Micro-benchmark (10,000 calls to SSL_get_cipher_by_value(0xC02B),
--config=openssl):
- before: 188.77 us/call
- after: 0.34 us/call (~550x faster)

*The thread_local version benchmarks about the same.*

Docs Changes: N/A
Release Notes: N/A

AI Disclosure: Claude Code was used to help explain the codebase,
simplify the implementation, write the throwaway benchmark, and draft
this description. I have reviewed, tested, and understand all of the
code in this PR.

---------

Signed-off-by: NMJlam <[email protected]>
Signed-off-by: Ryan Northey <[email protected]>
phlax pushed a commit to phlax/envoy that referenced this pull request Nov 16, 2026
Enabled format checks for `compat/openssl/...` and fixed format errors

---------

Signed-off-by: Ted Poole <[email protected]>
Backport note: also formats `SSL_get_cipher_by_value.cc`, which on `main` was
reformatted by envoyproxy#48956 rather than here.

(cherry picked from commit d276628)
Signed-off-by: Ryan Northey <[email protected]>
phlax pushed a commit to phlax/envoy that referenced this pull request Nov 16, 2026
Enabled format checks for `compat/openssl/...` and fixed format errors

---------

Signed-off-by: Ted Poole <[email protected]>
Backport note: also formats `SSL_get_cipher_by_value.cc`, which on `main` was
reformatted by envoyproxy#48956 rather than here.

(cherry picked from commit d276628)
Signed-off-by: Ryan Northey <[email protected]>
phlax pushed a commit that referenced this pull request Nov 17, 2026
Enabled format checks for `compat/openssl/...` and fixed format errors

---------

Signed-off-by: Ted Poole <[email protected]>
Backport note: also formats `SSL_get_cipher_by_value.cc`, which on `main` was
reformatted by #48956 rather than here.

(cherry picked from commit d276628)
Signed-off-by: Ryan Northey <[email protected]>
phlax pushed a commit to phlax/envoy that referenced this pull request Nov 17, 2026
…envoyproxy#48956)

Commit Message: openssl: create SSL object once per thread in
SSL_get_cipher_by_value

Additional Description:
OpenSSL can only look up a cipher through an SSL object. The compat
implementation of SSL_get_cipher_by_value created and freed a new
SSL_CTX and SSL on every call. Envoy calls it once per offered cipher
suite during TLS 1.2 certificate selection, meaning a single handshake
could create many SSL_CTX objects.

This change creates one SSL object per thread on first use, with a
`thread_local bssl::UniquePtr`, and reuses it for later lookups on that
thread. OpenSSL does not guarantee that SSL_CIPHER_find() is thread
safe, so threads do not share the object. It is freed when its thread
exits.

Fixes envoyproxy#48858

Risk Level: Low (only affects --config=openssl builds)

Testing:
```bash
bazel test --config=openssl //compat/openssl/test/...
bazel test --config=openssl //test/common/tls:ssl_socket_test //test/common/tls:context_impl_test
```
Micro-benchmark (10,000 calls to SSL_get_cipher_by_value(0xC02B),
--config=openssl):
- before: 188.77 us/call
- after: 0.34 us/call (~550x faster)

*The thread_local version benchmarks about the same.*

Docs Changes: N/A
Release Notes: N/A

AI Disclosure: Claude Code was used to help explain the codebase,
simplify the implementation, write the throwaway benchmark, and draft
this description. I have reviewed, tested, and understand all of the
code in this PR.

---------

Signed-off-by: NMJlam <[email protected]>
(cherry picked from commit 3d18e34)
Signed-off-by: Ryan Northey <[email protected]>
phlax pushed a commit to phlax/envoy that referenced this pull request Nov 17, 2026
…envoyproxy#48956)

Commit Message: openssl: create SSL object once per thread in
SSL_get_cipher_by_value

Additional Description:
OpenSSL can only look up a cipher through an SSL object. The compat
implementation of SSL_get_cipher_by_value created and freed a new
SSL_CTX and SSL on every call. Envoy calls it once per offered cipher
suite during TLS 1.2 certificate selection, meaning a single handshake
could create many SSL_CTX objects.

This change creates one SSL object per thread on first use, with a
`thread_local bssl::UniquePtr`, and reuses it for later lookups on that
thread. OpenSSL does not guarantee that SSL_CIPHER_find() is thread
safe, so threads do not share the object. It is freed when its thread
exits.

Fixes envoyproxy#48858

Risk Level: Low (only affects --config=openssl builds)

Testing:
```bash
bazel test --config=openssl //compat/openssl/test/...
bazel test --config=openssl //test/common/tls:ssl_socket_test //test/common/tls:context_impl_test
```
Micro-benchmark (10,000 calls to SSL_get_cipher_by_value(0xC02B),
--config=openssl):
- before: 188.77 us/call
- after: 0.34 us/call (~550x faster)

*The thread_local version benchmarks about the same.*

Docs Changes: N/A
Release Notes: N/A

AI Disclosure: Claude Code was used to help explain the codebase,
simplify the implementation, write the throwaway benchmark, and draft
this description. I have reviewed, tested, and understand all of the
code in this PR.

---------

Signed-off-by: NMJlam <[email protected]>
(cherry picked from commit 3d18e34)
Signed-off-by: Ryan Northey <[email protected]>
phlax pushed a commit to phlax/envoy that referenced this pull request Nov 17, 2026
…envoyproxy#48956)

Commit Message: openssl: create SSL object once per thread in
SSL_get_cipher_by_value

Additional Description:
OpenSSL can only look up a cipher through an SSL object. The compat
implementation of SSL_get_cipher_by_value created and freed a new
SSL_CTX and SSL on every call. Envoy calls it once per offered cipher
suite during TLS 1.2 certificate selection, meaning a single handshake
could create many SSL_CTX objects.

This change creates one SSL object per thread on first use, with a
`thread_local bssl::UniquePtr`, and reuses it for later lookups on that
thread. OpenSSL does not guarantee that SSL_CIPHER_find() is thread
safe, so threads do not share the object. It is freed when its thread
exits.

Fixes envoyproxy#48858

Risk Level: Low (only affects --config=openssl builds)

Testing:
```bash
bazel test --config=openssl //compat/openssl/test/...
bazel test --config=openssl //test/common/tls:ssl_socket_test //test/common/tls:context_impl_test
```
Micro-benchmark (10,000 calls to SSL_get_cipher_by_value(0xC02B),
--config=openssl):
- before: 188.77 us/call
- after: 0.34 us/call (~550x faster)

*The thread_local version benchmarks about the same.*

Docs Changes: N/A
Release Notes: N/A

AI Disclosure: Claude Code was used to help explain the codebase,
simplify the implementation, write the throwaway benchmark, and draft
this description. I have reviewed, tested, and understand all of the
code in this PR.

---------

Signed-off-by: NMJlam <[email protected]>
(cherry picked from commit 3d18e34)
Signed-off-by: Ryan Northey <[email protected]>
phlax pushed a commit to phlax/envoy that referenced this pull request Nov 17, 2026
…envoyproxy#48956)

Commit Message: openssl: create SSL object once per thread in
SSL_get_cipher_by_value

Additional Description:
OpenSSL can only look up a cipher through an SSL object. The compat
implementation of SSL_get_cipher_by_value created and freed a new
SSL_CTX and SSL on every call. Envoy calls it once per offered cipher
suite during TLS 1.2 certificate selection, meaning a single handshake
could create many SSL_CTX objects.

This change creates one SSL object per thread on first use, with a
`thread_local bssl::UniquePtr`, and reuses it for later lookups on that
thread. OpenSSL does not guarantee that SSL_CIPHER_find() is thread
safe, so threads do not share the object. It is freed when its thread
exits.

Fixes envoyproxy#48858

Risk Level: Low (only affects --config=openssl builds)

Testing:
```bash
bazel test --config=openssl //compat/openssl/test/...
bazel test --config=openssl //test/common/tls:ssl_socket_test //test/common/tls:context_impl_test
```
Micro-benchmark (10,000 calls to SSL_get_cipher_by_value(0xC02B),
--config=openssl):
- before: 188.77 us/call
- after: 0.34 us/call (~550x faster)

*The thread_local version benchmarks about the same.*

Docs Changes: N/A
Release Notes: N/A

AI Disclosure: Claude Code was used to help explain the codebase,
simplify the implementation, write the throwaway benchmark, and draft
this description. I have reviewed, tested, and understand all of the
code in this PR.

---------

Signed-off-by: NMJlam <[email protected]>
(cherry picked from commit 3d18e34)
Signed-off-by: Ryan Northey <[email protected]>
phlax pushed a commit to phlax/envoy that referenced this pull request Nov 17, 2026
…envoyproxy#48956)

Commit Message: openssl: create SSL object once per thread in
SSL_get_cipher_by_value

Additional Description:
OpenSSL can only look up a cipher through an SSL object. The compat
implementation of SSL_get_cipher_by_value created and freed a new
SSL_CTX and SSL on every call. Envoy calls it once per offered cipher
suite during TLS 1.2 certificate selection, meaning a single handshake
could create many SSL_CTX objects.

This change creates one SSL object per thread on first use, with a
`thread_local bssl::UniquePtr`, and reuses it for later lookups on that
thread. OpenSSL does not guarantee that SSL_CIPHER_find() is thread
safe, so threads do not share the object. It is freed when its thread
exits.

Fixes envoyproxy#48858

Risk Level: Low (only affects --config=openssl builds)

Testing:
```bash
bazel test --config=openssl //compat/openssl/test/...
bazel test --config=openssl //test/common/tls:ssl_socket_test //test/common/tls:context_impl_test
```
Micro-benchmark (10,000 calls to SSL_get_cipher_by_value(0xC02B),
--config=openssl):
- before: 188.77 us/call
- after: 0.34 us/call (~550x faster)

*The thread_local version benchmarks about the same.*

Docs Changes: N/A
Release Notes: N/A

AI Disclosure: Claude Code was used to help explain the codebase,
simplify the implementation, write the throwaway benchmark, and draft
this description. I have reviewed, tested, and understand all of the
code in this PR.

---------

Signed-off-by: NMJlam <[email protected]>
(cherry picked from commit 3d18e34)
Signed-off-by: Ryan Northey <[email protected]>
phlax pushed a commit that referenced this pull request Nov 17, 2026
…#48956)

Commit Message: openssl: create SSL object once per thread in
SSL_get_cipher_by_value

Additional Description:
OpenSSL can only look up a cipher through an SSL object. The compat
implementation of SSL_get_cipher_by_value created and freed a new
SSL_CTX and SSL on every call. Envoy calls it once per offered cipher
suite during TLS 1.2 certificate selection, meaning a single handshake
could create many SSL_CTX objects.

This change creates one SSL object per thread on first use, with a
`thread_local bssl::UniquePtr`, and reuses it for later lookups on that
thread. OpenSSL does not guarantee that SSL_CIPHER_find() is thread
safe, so threads do not share the object. It is freed when its thread
exits.

Fixes #48858

Risk Level: Low (only affects --config=openssl builds)

Testing:
```bash
bazel test --config=openssl //compat/openssl/test/...
bazel test --config=openssl //test/common/tls:ssl_socket_test //test/common/tls:context_impl_test
```
Micro-benchmark (10,000 calls to SSL_get_cipher_by_value(0xC02B),
--config=openssl):
- before: 188.77 us/call
- after: 0.34 us/call (~550x faster)

*The thread_local version benchmarks about the same.*

Docs Changes: N/A
Release Notes: N/A

AI Disclosure: Claude Code was used to help explain the codebase,
simplify the implementation, write the throwaway benchmark, and draft
this description. I have reviewed, tested, and understand all of the
code in this PR.

---------

Signed-off-by: NMJlam <[email protected]>
(cherry picked from commit 3d18e34)
Signed-off-by: Ryan Northey <[email protected]>
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.

OpenSSL Shim Efficiency: SSL_get_cipher_by_value Improvement

5 participants