Skip to content

ByteBuffer BIO - #216

Closed
Scottmitch wants to merge 1 commit into
netty:masterfrom
Scottmitch:java_bytebuf_bio
Closed

Scottmitch wants to merge 1 commit into
netty:masterfrom
Scottmitch:java_bytebuf_bio

Conversation

@Scottmitch

Copy link
Copy Markdown
Member

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.

@nmittler

Copy link
Copy Markdown
Member

@Scottmitch this is great! Could you post some benchmarks?

@nmittler

Copy link
Copy Markdown
Member

FYI @kruton @davidben

We'll want to port this to conscrypt

@normanmaurer

normanmaurer commented Jan 11, 2017 via email

Copy link
Copy Markdown
Member

@nmittler

Copy link
Copy Markdown
Member

👍

Comment thread openssl-dynamic/src/main/c/ssl.c Outdated
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);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

remove

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.

these are useful for debugging / visibility for now ... I will remove them before pulling in though.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

+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);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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 ?

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.

I updated names ... ptal

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.

@normanmaurer - How would the ownership be transferred? ByteBuffer is GCed.

apr_uint32_t ticket_keys_fail;

jmethodID verifier_method;
jmethodID cert_requested_callback_method;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@Scottmitch did you change the ordering for any reasons like memory savings due layout ?

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.

@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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

yes extra PR would be nice.

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.

Comment thread openssl-dynamic/src/main/c/ssl.c Outdated
(bioUserData = (struct TCN_bio_bytebuffer*) BIO_get_data(bio)) == NULL ||
bioUserData->sslWriteBufferLength == 0 ||
bioUserData->buffer == NULL ||
!bioUserData->bufferIsSSLWriteSink)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@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.

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.

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.

Comment thread openssl-dynamic/src/main/c/ssl.c Outdated
return BIO_ctrl_pending(b);
}

#ifdef OPENSSL_IS_BORINGSSL

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

really ? They not have the method ?

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.

not while I was developing this PR no.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@Scottmitch should we bring this up with the boringssl folks ? Sounds like they should have if they want to be API compatible.

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.

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?)

@Scottmitch Scottmitch Feb 7, 2017 •

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.

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 */

https://github.com/openssl/openssl/blob/master/include/openssl/bio.h#L87

# 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?

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.

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.

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.

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.
 */

Comment thread openssl-dynamic/src/main/c/ssl.c Outdated
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");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why BIO_TYPE_FILE and not BIO_TYPE_MEM ?

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.

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why do we need the different impls here ?

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.

The APIs changed between SSL versions. BIO_meth_new and BIO_meth_free are newer.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

no need for fqcn ?

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.

sure


/**
* 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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

same as above

* 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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Or even remove this and just point to intBufferBIO(long, int)

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.

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);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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 ?

Comment thread openssl-dynamic/src/main/c/ssl.c Outdated
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);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

+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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@Scottmitch ah nevermind... I saw the type is different depending on the ifdef.

Comment thread openssl-dynamic/src/main/c/ssl.c Outdated
return BIO_ctrl_pending(b);
}

#ifdef OPENSSL_IS_BORINGSSL

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@Scottmitch should we bring this up with the boringssl folks ? Sounds like they should have if they want to be API compatible.

@Scottmitch
Scottmitch force-pushed the java_bytebuf_bio branch 2 times, most recently from bf721fe to 52e0d26 Compare February 7, 2017 01:17
@Scottmitch Scottmitch added this to the 2.0.0.Final milestone Feb 7, 2017
@Scottmitch
Scottmitch force-pushed the java_bytebuf_bio branch 3 times, most recently from b32ff69 to ea80103 Compare February 7, 2017 05:50
@Scottmitch

Copy link
Copy Markdown
Member Author

@normanmaurer - PTAL

Comment thread openssl-dynamic/src/main/c/ssl.c Outdated

BIO_set_data(bio, bioUserData);

// https://www.openssl.org/docs/man1.1.0/crypto/BIO_set_data.html

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nit: move this over BIO_set_data

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.

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.

@Scottmitch

Copy link
Copy Markdown
Member Author

@normanmaurer - Any more comments?

@normanmaurer

Copy link
Copy Markdown
Member

@Scottmitch nope all good! Nice work, 🍻 on me

@normanmaurer

Copy link
Copy Markdown
Member

@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.
@Scottmitch

Copy link
Copy Markdown
Member Author

I just cleaned up the ssl_private.h style fixes ... I will pull in tomorrow after running some benchmarks.

@Scottmitch

Copy link
Copy Markdown
Member Author

master (826513b)

@Scottmitch Scottmitch closed this Feb 9, 2017
@Scottmitch
Scottmitch deleted the java_bytebuf_bio branch February 9, 2017 17:48
@Scottmitch

Copy link
Copy Markdown
Member Author

Initial benchmark results show no concerns. I will address any concerns that pop up in the future.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants