Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add connect/disconnect, reduce copy, simplify headers #23

Merged
merged 4 commits into from
Apr 18, 2024

Conversation

billphipps
Copy link
Contributor

A number of changes:

  1. Adds server-side external connect/disconnect handlers
  2. Adds server transport callbacks that can trigger a connect or disconnect from the transport layer
  3. Adds CommInit message to exchange client and server id's
  4. Adds CommClose message which will trigger server to disconnect
  5. Updated tests to now use CommClose to close servers async
  6. Consolidated wh_transport.h into wh_comm.h to avoid circular includes
  7. Consolidated wh_server_dma.h into wh_server.h to avoid circular includes
  8. Eliminated declaring counter variables within for loops
  9. Eliminated // comments. Now compiles clean for C90
  10. Reordered/typed whServerContext and whClientContext to ensure packet buffer is 64-bit aligned
  11. Exposed data ptr into internal buffer for CommServer and CommClient to eliminate extra copies and space
  12. Reworked server handler to use internal buffer

Nearly all of these changes are necessary to make the Bernini and Aurix ports compile clean and align packet buffers correctly as well as use optimized memcpy.

@billphipps billphipps requested review from bigbrett and jpbland1 April 18, 2024 04:27
src/wh_comm.c Show resolved Hide resolved
src/wh_server.c Outdated Show resolved Hide resolved
@@ -32,7 +32,8 @@
enum {
WH_COMM_HEADER_LEN = 8, /* whCommHeader */
WH_COMM_DATA_LEN = 1280,
WH_COMM_MTU = (WH_COMM_HEADER_LEN + WH_COMM_DATA_LEN)
WH_COMM_MTU = (WH_COMM_HEADER_LEN + WH_COMM_DATA_LEN),
WH_COMM_MTU_8 = (WH_COMM_MTU + 7) / 8,
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we should call it WH_COMM_MTU_BITS, wolfSSL defines things as bits when referring to sizes in bits: #define SP_INT_BITS 4096

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hah! This is actually the rounded-up number of 8-byte uint64_t's needed to hold an MTU packet. How about I change this to WH_COMM_MTU_64?

Copy link
Contributor

@bigbrett bigbrett Apr 18, 2024

Choose a reason for hiding this comment

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

Oooh yeah that is a little confusing, I had to double take this a few times to be sure, as +7/8 looks like rounding up bits to the next byte on first glance, but we are dealing with bytes and 64-bit words here.

Maybe one of the following, or a permutation thereof?
WH_COMM_MTU_ALIGN64_COUNT
WH_COMM_MTU_WORD64_COUNT
WH_COMM_MTU_ALIGN64_WORD_COUNT

I think explicitly calling out sizes and purpose is a good thing.

Copy link
Contributor

Choose a reason for hiding this comment

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

ah gotcha yeah thats much less confusing, I'd also add a comment

Copy link
Contributor Author

Choose a reason for hiding this comment

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

went with WH_COMM_MTU_U64_COUNT with a comment. Fixed in next push.

wolfhsm/wh_comm.h Show resolved Hide resolved
wolfhsm/wh_server.h Show resolved Hide resolved
test/wh_test_crypto.c Outdated Show resolved Hide resolved
bigbrett
bigbrett previously approved these changes Apr 18, 2024
wolfhsm/wh_server.h Show resolved Hide resolved
@@ -32,7 +32,8 @@
enum {
WH_COMM_HEADER_LEN = 8, /* whCommHeader */
WH_COMM_DATA_LEN = 1280,
WH_COMM_MTU = (WH_COMM_HEADER_LEN + WH_COMM_DATA_LEN)
WH_COMM_MTU = (WH_COMM_HEADER_LEN + WH_COMM_DATA_LEN),
WH_COMM_MTU_8 = (WH_COMM_MTU + 7) / 8,
Copy link
Contributor

@bigbrett bigbrett Apr 18, 2024

Choose a reason for hiding this comment

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

Oooh yeah that is a little confusing, I had to double take this a few times to be sure, as +7/8 looks like rounding up bits to the next byte on first glance, but we are dealing with bytes and 64-bit words here.

Maybe one of the following, or a permutation thereof?
WH_COMM_MTU_ALIGN64_COUNT
WH_COMM_MTU_WORD64_COUNT
WH_COMM_MTU_ALIGN64_WORD_COUNT

I think explicitly calling out sizes and purpose is a good thing.

Comment on lines 93 to 99
typedef int (*whCommSetConnectedCb)(void* context, int connected);

/* Status of whether a client is connected or not */
enum {
WH_COMM_DISCONNECTED = 0,
WH_COMM_CONNECTED = 1,
};
Copy link
Contributor

Choose a reason for hiding this comment

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

Thoughts on typing enums, especially when an argument is assumed to be one of a few values? I think it adds clarity as to what should be passed in, as opposed to relying on comments or a hopefully obvious enum name. I'm pro anything that lets code be more self-documenting. Something like:

/* Status of whether a client is connected or not */
typedef enum {
    WH_COMM_DISCONNECTED = 0,
    WH_COMM_CONNECTED = 1,
} whCommConnectState;

typedef int (*whCommSetConnectedCb)(void* context, whCommConnectState connected);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Concur. I was trying to keep the callbacks generic and untyped, but there's no reason not to force the type to an enum. Fixed in next push.

src/wh_server.c Outdated Show resolved Hide resolved
Comment on lines 287 to 289
for ( wh_Server_SetConnected(server, am_connected);
am_connected == WH_COMM_CONNECTED;
wh_Server_GetConnected(server, &am_connected) ){
Copy link
Contributor

Choose a reason for hiding this comment

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

this is fun

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Prolly too fancy. I'll switch it to a while structure. Fixed in next push

@@ -1235,6 +1236,7 @@ int whTest_ClientCfg(whClientConfig* clientCfg)
WH_TEST_ASSERT_RETURN(server_rc == WH_ERROR_OK);
WH_TEST_ASSERT_RETURN(avail_objects == NF_OBJECT_COUNT);

wh_Client_CommClose(client);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
wh_Client_CommClose(client);
WH_TEST_RETURN_ON_FAIL(wh_Client_CommClose(client));

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed in next push.

@billphipps billphipps dismissed jpbland1’s stale review April 18, 2024 19:50

Added refactor to backlog. Improved names

@billphipps billphipps requested a review from bigbrett April 18, 2024 19:50
@jpbland1 jpbland1 self-requested a review April 18, 2024 20:16
Copy link
Contributor

@jpbland1 jpbland1 left a comment

Choose a reason for hiding this comment

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

Looks good

@jpbland1 jpbland1 merged commit 5375227 into wolfSSL:main Apr 18, 2024
1 check passed
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.

3 participants