Repository navigation
ByteBuffer BIO - #216
ByteBuffer BIO#216Scottmitch wants to merge 1 commit into
Conversation
|
@Scottmitch this is great! Could you post some benchmarks? |
|
We have no benchmark yet but will provide one soon
…
|
|
👍 |
4ea3280 to
41b535b
Compare
41b535b to
7e83c46
Compare
4e255ab to
c41c55e
Compare
| static jint tcn_flush_sslbuffer_to_bytebuffer(struct TCN_bio_bytebuffer* bioUserData) { | ||
| jint writeAmount = TCN_MIN(bioUserData->bufferLength, bioUserData->sslWriteBufferLength) * sizeof(char); | ||
| jint writeChunk = bioUserData->sslWriteBufferSize - bioUserData->sslWriteBufferOffset; | ||
| //fprintf(stderr, "tcn_flush_sslbuffer_to_bytebuffer1 bioUserData->sslWriteBufferLength %d bioUserData->sslWriteBufferOffset %d writeChunk %d writeAmount %d\n", bioUserData->sslWriteBufferLength, bioUserData->sslWriteBufferOffset, writeChunk, writeAmount); |
There was a problem hiding this comment.
these are useful for debugging / visibility for now ... I will remove them before pulling in though.
There was a problem hiding this comment.
+1... Also we may just want to preserve this and guard them with an ifdef. This way we could enable it when build in debug mode or such. WDYT ?
| * {@code false} if this buffer is expected to buffer data as a result of calls to {@code SSL_read}. | ||
| */ | ||
| public static native int readFromBIO(long bio, long rbuf, int rlen); | ||
| public static native void setBIOBuffer(long bio, long bufferAddress, int maxUsableBytes, boolean isSSLWriteSink); |
There was a problem hiding this comment.
@Scottmitch just a nit but should we be more consistent with naming. Like sometimes we start with bio sometimes we have BIO as suffix etc.
There was a problem hiding this comment.
Also can you add to the javadocs that we not transfer the ownership and so the memory must be freed by the caller later on ?
There was a problem hiding this comment.
I updated names ... ptal
There was a problem hiding this comment.
@normanmaurer - How would the ownership be transferred? ByteBuffer is GCed.
| apr_uint32_t ticket_keys_fail; | ||
|
|
||
| jmethodID verifier_method; | ||
| jmethodID cert_requested_callback_method; |
There was a problem hiding this comment.
@Scottmitch did you change the ordering for any reasons like memory savings due layout ?
There was a problem hiding this comment.
@normanmaurer - Yes this was the intention. To pack memory tighter and avoid padding ... and not rely upon compiler optimizations/pragma definitions to do so. At one point in development I actually modified this structure but then realized I didn't need to ... I can factor this change out into another PR if you prefer.
There was a problem hiding this comment.
yes extra PR would be nice.
| (bioUserData = (struct TCN_bio_bytebuffer*) BIO_get_data(bio)) == NULL || | ||
| bioUserData->sslWriteBufferLength == 0 || | ||
| bioUserData->buffer == NULL || | ||
| !bioUserData->bufferIsSSLWriteSink) |
There was a problem hiding this comment.
@Scottmitch actually I think it would be easier to read if you would do the assignment etc in a separate step. I know this will cause more branches but will be easier to read at the end.
There was a problem hiding this comment.
Unless you are strong here I prefer to just leave it as is. The code is relatively self contained and I don't think it makes a significant difference either way.
| return BIO_ctrl_pending(b); | ||
| } | ||
|
|
||
| #ifdef OPENSSL_IS_BORINGSSL |
There was a problem hiding this comment.
really ? They not have the method ?
There was a problem hiding this comment.
not while I was developing this PR no.
There was a problem hiding this comment.
@Scottmitch should we bring this up with the boringssl folks ? Sounds like they should have if they want to be API compatible.
There was a problem hiding this comment.
BIO_wpending? I'm... not sure why OpenSSL has two spellings for the same thing. (Or why we have both spellings for BIO_pending but only one for BIO_wpending. I guess nothing used BIO_ctrl_wpending so we didn't bother?)
There was a problem hiding this comment.
IIRC I was trying to be a "good citizen" when writing the BIO and use existing APIs when possible but its possible I'm actually abusing these interfaces.
Here is how they are described in the header files:
https://github.com/openssl/openssl/blob/master/include/openssl/bio.h#L90
# define BIO_CTRL_WPENDING 13/* opt - number of bytes still to write */# define BIO_CTRL_PENDING 10/* opt - is their more data buffered */I'm using BIO_CTRL_PENDING to mean "how many bytes are pending in the current ByteBuffer" and I'm using BIO_CTRL_WPENDING to mean "how many bytes are pending in the char[] which is part of the BIO" (which must be written to the peer). I could just create another method(s) which don't hook into the BIO_*ctrl mechanism if that is safer/more clear ... @davidben WDYT?
There was a problem hiding this comment.
Personally, I think BIO_ctrl is insane, so not hooking into it seems pretty sound. But it's also possible they're appropriate here? (Didn't look at this PR.) I dunno. OpenSSL doesn't really use values, so it doesn't especially matter either way.
But my comment was just pointing out that BIO_ctrl_wpending and BIO_wpending are the exact same thing.
There was a problem hiding this comment.
not hooking into it seems pretty sound.
The goal of re-using these APIs is that other applications which may use this BIO implementation won't have to have access to my "special APIs". The risk with hooking into BIO_ctrl is I implement these values in an unexpected way which may result in "bad behavior" from someone use may use another BIO and attempts to plug this BIO in. I think its unlikely this code will be used in another context because it already requires some special APIs so I will just make new APIs for these methods too.
But my comment was just pointing out that BIO_ctrl_wpending and BIO_wpending are the exact same thing.
I see ... here is some additional info from OpenSSL's code base:
https://github.com/openssl/openssl/blob/master/crypto/bio/bio_lib.c#L567
/*
* It is unfortunate to duplicate in functions what the BIO_(w)pending macros
* do; but those macros have inappropriate return type, and for interfacing
* from other programming languages, C macros aren't much of a help anyway.
*/| static BIO_METHOD* bio_java_bytebuffer_methods = NULL; | ||
|
|
||
| static void init_bio_methods(void) { | ||
| bio_java_bytebuffer_methods = BIO_meth_new(BIO_TYPE_FILE, "Java ByteBuffer"); |
There was a problem hiding this comment.
Why BIO_TYPE_FILE and not BIO_TYPE_MEM ?
There was a problem hiding this comment.
cpy/pasta ... changed to BIO_TYPE_MEM ... although I'm not sure this matters. In 1.1.0 OpenSSL added BIO_get_new_index to generate a new index.
| return &bio_java_bytebuffer_methods; | ||
| #else | ||
| return bio_java_bytebuffer_methods; | ||
| #endif |
There was a problem hiding this comment.
Why do we need the different impls here ?
There was a problem hiding this comment.
The APIs changed between SSL versions. BIO_meth_new and BIO_meth_free are newer.
There was a problem hiding this comment.
@Scottmitch ah nevermind... I saw the type is different depending on the ifdef.
| * @param rlen | ||
| * @return | ||
| * Initialize the BIO for the SSL instance. This is a custom BIO which is designed to play nicely with a direct | ||
| * {@link java.nio.ByteBuffer}. Because it is a special BIO it requires special usage such that |
|
|
||
| /** | ||
| * Initialize the BIO for the SSL instance. This is a custom BIO which is designed to play nicely with a direct | ||
| * {@link java.nio.ByteBuffer}. Because it is a special BIO it requires special usage such that |
| * Initialize the BIO for the SSL instance. This is a custom BIO which is designed to play nicely with a direct | ||
| * {@link java.nio.ByteBuffer}. Because it is a special BIO it requires special usage such that | ||
| * {@link #setBIOBuffer(long, long, int, boolean)} and {@link #clearBIOBuffer(long)} are called in order to provide | ||
| * to supply data to SSL, and also to ensure the internal SSL buffering mechanism is expecting write at the appropriate times. |
There was a problem hiding this comment.
Or even remove this and just point to intBufferBIO(long, int)
There was a problem hiding this comment.
method has been removed
| * {@code false} if this buffer is expected to buffer data as a result of calls to {@code SSL_read}. | ||
| */ | ||
| public static native int readFromBIO(long bio, long rbuf, int rlen); | ||
| public static native void setBIOBuffer(long bio, long bufferAddress, int maxUsableBytes, boolean isSSLWriteSink); |
There was a problem hiding this comment.
Also can you add to the javadocs that we not transfer the ownership and so the memory must be freed by the caller later on ?
| static jint tcn_flush_sslbuffer_to_bytebuffer(struct TCN_bio_bytebuffer* bioUserData) { | ||
| jint writeAmount = TCN_MIN(bioUserData->bufferLength, bioUserData->sslWriteBufferLength) * sizeof(char); | ||
| jint writeChunk = bioUserData->sslWriteBufferSize - bioUserData->sslWriteBufferOffset; | ||
| //fprintf(stderr, "tcn_flush_sslbuffer_to_bytebuffer1 bioUserData->sslWriteBufferLength %d bioUserData->sslWriteBufferOffset %d writeChunk %d writeAmount %d\n", bioUserData->sslWriteBufferLength, bioUserData->sslWriteBufferOffset, writeChunk, writeAmount); |
There was a problem hiding this comment.
+1... Also we may just want to preserve this and guard them with an ifdef. This way we could enable it when build in debug mode or such. WDYT ?
| return &bio_java_bytebuffer_methods; | ||
| #else | ||
| return bio_java_bytebuffer_methods; | ||
| #endif |
There was a problem hiding this comment.
@Scottmitch ah nevermind... I saw the type is different depending on the ifdef.
| return BIO_ctrl_pending(b); | ||
| } | ||
|
|
||
| #ifdef OPENSSL_IS_BORINGSSL |
There was a problem hiding this comment.
@Scottmitch should we bring this up with the boringssl folks ? Sounds like they should have if they want to be API compatible.
bf721fe to
52e0d26
Compare
b32ff69 to
ea80103
Compare
|
@normanmaurer - PTAL |
|
|
||
| BIO_set_data(bio, bioUserData); | ||
|
|
||
| // https://www.openssl.org/docs/man1.1.0/crypto/BIO_set_data.html |
There was a problem hiding this comment.
nit: move this over BIO_set_data
There was a problem hiding this comment.
this actually link was meant to help provide motivation for the call to BIO_set_shutdown and BIO_set_init which is necessary to inform OpenSSL of the BIO's initialization state. I can add comments to this effect but I think the comment belongs here.
ea80103 to
3f535a8
Compare
|
@normanmaurer - Any more comments? |
|
@Scottmitch nope all good! Nice work, 🍻 on me |
|
@trustin maybe you are also interested in this |
Motivation: Currently netty-tcnative uses BIO_new_bio_pair so we can control all FD lifetime and event notification in Java but delegates to OpenSSL for encryption/decryption. The current implementation sets up a pair of BIO buffers to read/write encrypted/plaintext data. This approach requires copying of data from Java ByteBuffers to native memory BIO buffers, and also requires both BIO buffers to be sufficiently large to hold application data. If direct ByteBuffers are used we can avoid copying to/from the intermediate BIO buffer and just read/write directly from the direct ByteBuffer memory. We still need an internal buffer because OpenSSL may generate write data as a result of read calls (e.g. handshake, alerts, renegotiation, etc..), but this buffer doesn't have to be be large enough to hold application data. Modifications: - Implement BIO which reads/writes from/to direct ByteBuffer memory. - The BIO must also support an intermediate storage buffer for when OpenSSL generates write data but we are reading. Result: Allows for less memory copying and lower memory for internal buffers when used from Java.
3f535a8 to
96b452e
Compare
|
I just cleaned up the ssl_private.h style fixes ... I will pull in tomorrow after running some benchmarks. |
|
master (826513b) |
|
Initial benchmark results show no concerns. I will address any concerns that pop up in the future. |
Motivation:
Currently netty-tcnative uses BIO_new_bio_pair so we can control all FD lifetime and event notification in Java but delegates to OpenSSL for encryption/decryption. The current implementation sets up a pair of BIO buffers to read/write encrypted/plaintext data. This approach requires copying of data from Java ByteBuffers to native memory BIO buffers, and also requires both BIO buffers to be sufficiently large to hold application data. If direct ByteBuffers are used we can avoid copying to/from the intermediate BIO buffer and just read/write directly from the direct ByteBuffer memory. We still need an internal buffer because OpenSSL may generate write data as a result of read calls (e.g. handshake, alerts, renegotiation, etc..), but this buffer doesn't have to be be large enough to hold application data.
Modifications:
Result:
Allows for less memory copying and lower memory for internal buffers when used from Java.