proxygen
|
#include <SSLSessionCacheManager.h>
Public Member Functions | |
SSLSessionCacheManager (uint32_t maxCacheSize, uint32_t cacheCullSize, folly::SSLContext *ctx, const std::string &context, SSLStats *stats, const std::shared_ptr< SSLCacheProvider > &externalCache) | |
virtual | ~SSLSessionCacheManager () |
void | onGetSuccess (SSLCacheProvider::CacheContext *cacheCtx, const std::string &value) |
void | onGetSuccess (SSLCacheProvider::CacheContext *cacheCtx, std::unique_ptr< folly::IOBuf > valueBuf) |
void | onGetFailure (SSLCacheProvider::CacheContext *cacheCtx) |
Static Public Member Functions | |
static void | shutdown () |
Private Types | |
using | session_callback_arg_session_id_t = unsigned char * |
Private Member Functions | |
int | newSession (SSL *ssl, SSL_SESSION *session) |
void | removeSession (SSL_CTX *ctx, SSL_SESSION *session) |
SSL_SESSION * | getSession (SSL *ssl, unsigned char *session_id, int id_len, int *copyflag) |
void | restoreSession (SSLCacheProvider::CacheContext *cacheCtx, const uint8_t *data, size_t length) |
bool | storeCacheRecord (const std::string &sessionId, SSL_SESSION *session) |
bool | lookupCacheRecord (const std::string &sessionId, folly::AsyncSSLSocket *sslSock) |
void | restartSSLAccept (const SSLCacheProvider::CacheContext *cacheCtx) |
Static Private Member Functions | |
static std::shared_ptr< ShardedLocalSSLSessionCache > | getLocalCache (uint32_t maxCacheSize, uint32_t cacheCullSize) |
static int | newSessionCallback (SSL *ssl, SSL_SESSION *session) |
static void | removeSessionCallback (SSL_CTX *ctx, SSL_SESSION *session) |
static SSL_SESSION * | getSessionCallback (SSL *ssl, session_callback_arg_session_id_t session_id, int id_len, int *copyflag) |
Private Attributes | |
folly::SSLContext * | ctx_ |
std::shared_ptr< ShardedLocalSSLSessionCache > | localCache_ |
PendingLookupMap | pendingLookups_ |
SSLStats * | stats_ {nullptr} |
std::shared_ptr< SSLCacheProvider > | externalCache_ |
Static Private Attributes | |
static int32_t | sExDataIndex_ = -1 |
static std::shared_ptr< ShardedLocalSSLSessionCache > | sCache_ |
static std::mutex | sCacheLock_ |
SSLSessionCacheManager handles all stateful session caching. There is an instance of this object per SSL VIP per thread, with a 1:1 correlation with SSL_CTX. The cache can work locally or in concert with an external cache to share sessions across instances.
There is a single in memory session cache shared by all VIPs. The cache is split into N buckets (currently 16) with a separate lock per bucket. The VIP ID is hashed and stored as part of the session to handle the (very unlikely) case of session ID collision.
When a new SSL session is created, it is added to the LRU cache and sent to the external cache to be stored. The external cache expiration is equal to the SSL session's expiration.
When a resume request is received, SSLSessionCacheManager first looks in the local LRU cache for the VIP. If there is a miss there, an asynchronous request for this session is dispatched to the external cache. When the external cache query returns, the LRU cache is updated if the session was found, and the SSL_accept call is resumed.
If additional resume requests for the same session ID arrive in the same thread while the request is pending, the 2nd - Nth callers attach to the original external cache requests and are resumed when it comes back. No attempt is made to coalesce external cache requests for the same session ID in different worker threads. Previous work did this, but the complexity was deemed to outweigh the potential savings.
Definition at line 136 of file SSLSessionCacheManager.h.
|
private |
Definition at line 255 of file SSLSessionCacheManager.h.
wangle::SSLSessionCacheManager::SSLSessionCacheManager | ( | uint32_t | maxCacheSize, |
uint32_t | cacheCullSize, | ||
folly::SSLContext * | ctx, | ||
const std::string & | context, | ||
SSLStats * | stats, | ||
const std::shared_ptr< SSLCacheProvider > & | externalCache | ||
) |
Constructor. SSL session related callbacks will be set on the underlying SSL_CTX. vipId is assumed to a unique string identifying the VIP and must be the same on all servers that wish to share sessions via the same external cache.
Definition at line 142 of file SSLSessionCacheManager.cpp.
References getLocalCache(), getSessionCallback(), folly::SSLContext::getSSLCtx(), wangle::SSLUtil::getSSLCtxExIndex(), localCache_, newSessionCallback(), removeSessionCallback(), folly::SSLContext::setSessionCacheContext(), and sExDataIndex_.
|
virtual |
Definition at line 174 of file SSLSessionCacheManager.cpp.
|
staticprivate |
Get or create the LRU cache for the given VIP ID
Definition at line 182 of file SSLSessionCacheManager.cpp.
References g(), sCache_, and sCacheLock_.
Referenced by SSLSessionCacheManager().
|
private |
Invoked by openssl when a client requests a stateful session resumption. Triggers a lookup in our local cache and potentially an asynchronous request to an external cache.
Definition at line 272 of file SSLSessionCacheManager.cpp.
References externalCache_, folly::AsyncSocket::getFd(), folly::AsyncSSLSocket::getFromSSL(), wangle::SSLUtil::hexlify(), localCache_, lookupCacheRecord(), folly::gen::move, folly::fibers::onFiber(), pendingLookups_, wangle::SSLStats::recordSSLSession(), folly::AsyncSSLSocket::setSessionIDResumed(), stats_, and string.
Referenced by getSessionCallback().
|
staticprivate |
Definition at line 257 of file SSLSessionCacheManager.cpp.
References folly::FATAL, getSession(), and sExDataIndex_.
Referenced by SSLSessionCacheManager().
|
private |
Lookup a session in the external cache for the specified SSL socket.
Definition at line 387 of file SSLSessionCacheManager.cpp.
References externalCache_.
Referenced by getSession().
|
private |
Invoked by openssl when a new SSL session is created
Definition at line 207 of file SSLSessionCacheManager.cpp.
References externalCache_, wangle::SSLUtil::hexlify(), localCache_, wangle::SSLStats::recordSSLSession(), stats_, and storeCacheRecord().
Referenced by newSessionCallback().
|
staticprivate |
static functions registered as callbacks to openssl via SSL_CTX_sess_set_new/get/remove_cb
Definition at line 195 of file SSLSessionCacheManager.cpp.
References folly::FATAL, newSession(), and sExDataIndex_.
Referenced by SSLSessionCacheManager().
void wangle::SSLSessionCacheManager::onGetFailure | ( | SSLCacheProvider::CacheContext * | cacheCtx | ) |
Callback for ExternalCache to call when an async get fails, either because the requested session is not in the external cache or because of an error.
context | The context that was passed to the async get request |
Definition at line 450 of file SSLSessionCacheManager.cpp.
References restartSSLAccept().
void wangle::SSLSessionCacheManager::onGetSuccess | ( | SSLCacheProvider::CacheContext * | cacheCtx, |
const std::string & | value | ||
) |
Callback for ExternalCache to call when an async get succeeds
context | The context that was passed to the async get request |
value | Serialized session |
Definition at line 434 of file SSLSessionCacheManager.cpp.
References restoreSession(), and uint8_t.
void wangle::SSLSessionCacheManager::onGetSuccess | ( | SSLCacheProvider::CacheContext * | cacheCtx, |
std::unique_ptr< folly::IOBuf > | valueBuf | ||
) |
Callback for ExternalCache to call when an async get succeeds
context | The context that was passed to the async get request |
valueBuf | Serialized session stored in folly::IOBuf, this should NOT be called with valueBuf == nullptr. |
Definition at line 440 of file SSLSessionCacheManager.cpp.
References folly::IOBuf::coalesce(), folly::IOBuf::data(), folly::IOBuf::length(), and restoreSession().
|
private |
Invoked by openssl when an SSL session is ejected from its internal cache. This can't be invoked in the current implementation because SSL's internal caching is disabled.
Definition at line 239 of file SSLSessionCacheManager.cpp.
References wangle::SSLUtil::hexlify(), localCache_, wangle::SSLStats::recordSSLSessionRemove(), and stats_.
Referenced by removeSessionCallback().
|
staticprivate |
Definition at line 228 of file SSLSessionCacheManager.cpp.
References folly::FATAL, removeSession(), and sExDataIndex_.
Referenced by SSLSessionCacheManager().
|
private |
Restart all clients waiting for the answer to an external cache query
Definition at line 403 of file SSLSessionCacheManager.cpp.
References pendingLookups_, folly::AsyncSSLSocket::restartSSLAccept(), wangle::SSLCacheProvider::CacheContext::session, wangle::SSLCacheProvider::CacheContext::sessionId, and wangle::SSLCacheProvider::CacheContext::sslSocket.
Referenced by onGetFailure(), and restoreSession().
|
private |
Invoked by onGetSuccess callback, to restore session from cache.
context | The context that was passed to the async get request |
data | Buffer that stores serialized session |
length | Buffer size |
Definition at line 420 of file SSLSessionCacheManager.cpp.
References localCache_, restartSSLAccept(), wangle::SSLCacheProvider::CacheContext::session, wangle::SSLCacheProvider::CacheContext::sessionId, and stats_.
Referenced by onGetSuccess().
|
static |
Call this on shutdown to release the global instance of the ShardedLocalSSLSessionCache.
Definition at line 177 of file SSLSessionCacheManager.cpp.
References g(), sCache_, and sCacheLock_.
|
private |
Store a new session record in the external cache
Definition at line 375 of file SSLSessionCacheManager.cpp.
References ctx_, expiration(), externalCache_, folly::SSLContext::getSSLCtx(), string, uint32_t, and uint8_t.
Referenced by newSession().
|
private |
Definition at line 187 of file SSLSessionCacheManager.h.
Referenced by storeCacheRecord().
|
private |
Definition at line 191 of file SSLSessionCacheManager.h.
Referenced by getSession(), lookupCacheRecord(), newSession(), and storeCacheRecord().
|
private |
Definition at line 188 of file SSLSessionCacheManager.h.
Referenced by getSession(), newSession(), removeSession(), restoreSession(), and SSLSessionCacheManager().
|
private |
Definition at line 189 of file SSLSessionCacheManager.h.
Referenced by getSession(), and restartSSLAccept().
|
staticprivate |
Definition at line 265 of file SSLSessionCacheManager.h.
Referenced by getLocalCache(), and shutdown().
|
staticprivate |
Definition at line 266 of file SSLSessionCacheManager.h.
Referenced by getLocalCache(), and shutdown().
|
staticprivate |
Definition at line 264 of file SSLSessionCacheManager.h.
Referenced by getSessionCallback(), newSessionCallback(), removeSessionCallback(), and SSLSessionCacheManager().
Definition at line 190 of file SSLSessionCacheManager.h.
Referenced by getSession(), newSession(), removeSession(), and restoreSession().