Skip to content

Commit

Permalink
general buffer size changes/ tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
radkesvat committed Jun 15, 2024
1 parent 1599086 commit 3d0bce5
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 43 deletions.
2 changes: 2 additions & 0 deletions tunnels/client/http2/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ static http2_client_con_state_t *createHttp2Connection(tunnel_t *self, int tid)
nghttp2_settings_entry settings[] = {
{NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS, kMaxConcurrentStreams},
{NGHTTP2_SETTINGS_MAX_FRAME_SIZE, (1U << 18)},
{NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE, (1U << 18) }

};
nghttp2_submit_settings(con->session, NGHTTP2_FLAG_NONE, settings, ARRAY_SIZE(settings));

Expand Down
13 changes: 7 additions & 6 deletions tunnels/client/http2/http2_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "tunnel.h"
#include "types.h"
#include "utils/jsonutils.h"
#include "utils/mathutils.h"

enum
{
Expand Down Expand Up @@ -424,10 +425,10 @@ static void downStream(tunnel_t *self, context_t *c)
if (c->payload != NULL)
{

con->state = kH2WantRecv;
size_t len = bufLen(c->payload);
size_t ret = nghttp2_session_mem_recv2(con->session, (const uint8_t *) rawBuf(c->payload), len);
assert(ret == len);
con->state = kH2WantRecv;
size_t len = bufLen(c->payload);
ssize_t ret = nghttp2_session_mem_recv2(con->session, (const uint8_t *) rawBuf(c->payload), len);
assert(ret == (ssize_t) len);
reuseContextBuffer(c);

if (! isAlive(c->line))
Expand All @@ -436,7 +437,7 @@ static void downStream(tunnel_t *self, context_t *c)
return;
}

if (ret != len)
if (ret != (ssize_t) len)
{
deleteHttp2Connection(con);
self->dw->downStream(self->dw, newFinContext(c->line));
Expand Down Expand Up @@ -528,7 +529,7 @@ tunnel_t *newHttp2Client(node_instance_context_t *instance_info)
t->state = state;
t->upStream = &upStream;
t->downStream = &downStream;

return t;
}

Expand Down
2 changes: 1 addition & 1 deletion tunnels/client/reality/reality_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ static void upStream(tunnel_t *self, context_t *c)
shift_buffer_t *buf = c->payload;
c->payload = NULL;

const unsigned int chunk_size = ((1 << 16) - (kSignLen + (2 * kEncryptionBlockSize) + kIVlen));
const unsigned int chunk_size = ((1 << 15) - (kSignLen + (2 * kEncryptionBlockSize) + kIVlen));

if (bufLen(buf) < chunk_size)
{
Expand Down
5 changes: 2 additions & 3 deletions tunnels/client/reverse/reverse_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#include "helpers.h"
#include "idle_table.h"
#include "loggers/network_logger.h"
#include "shiftbuffer.h"
#include "tunnel.h"
#include "types.h"
#include "utils/jsonutils.h"
Expand Down Expand Up @@ -130,8 +129,8 @@ static void downStream(tunnel_t *self, context_t *c)

initiateConnect(self, tid, false);

ucstate->idle_handle = newIdleItem(state->starved_connections, (hash_t) (ucstate), ucstate,
onStarvedConnectionExpire, c->line->tid, kConnectionStarvationTimeOut);
// ucstate->idle_handle = newIdleItem(state->starved_connections, (hash_t) (ucstate), ucstate,
// onStarvedConnectionExpire, c->line->tid, kConnectionStarvationTimeOut);

destroyContext(c);
}
Expand Down
2 changes: 0 additions & 2 deletions tunnels/server/halfduplex/halfduplex_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -551,8 +551,6 @@ static void upStream(tunnel_t *self, context_t *c)

assert(upload_line_cstate->buffering);


shiftr(upload_line_cstate->buffering, sizeof(uint64_t));
if (bufLen(upload_line_cstate->buffering) > 0)
{
context_t *buf_ctx = newContext(main_line);
Expand Down
6 changes: 5 additions & 1 deletion tunnels/server/http2/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,11 @@ static http2_server_con_state_t *createHttp2Connection(tunnel_t *self, line_t *l

nghttp2_settings_entry settings[] = {
{NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS, kMaxConcurrentStreams},
{NGHTTP2_SETTINGS_MAX_FRAME_SIZE, (1U << 18)},
{NGHTTP2_SETTINGS_MAX_FRAME_SIZE, (1U << 18) },
{NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE, (1U << 18) },



};
nghttp2_submit_settings(con->session, NGHTTP2_FLAG_NONE, settings, ARRAY_SIZE(settings));
con->state = kH2SendSettings;
Expand Down
60 changes: 33 additions & 27 deletions tunnels/server/http2/http2_server.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#include "http2_server.h"
#include "grpc_def.h"
#include "http2_def.h"
#include "helpers.h"
#include "http2_def.h"
#include "loggers/network_logger.h"
#include "nghttp2/nghttp2.h"
#include "types.h"
#include "utils/mathutils.h"

static int onHeaderCallback(nghttp2_session *session, const nghttp2_frame *frame, const uint8_t *_name, size_t namelen,
const uint8_t *_value, size_t valuelen, uint8_t flags, void *userdata)
Expand All @@ -24,7 +25,7 @@ static int onHeaderCallback(nghttp2_session *session, const nghttp2_frame *frame
const char *value = (const char *) _value;
// LOGD("%s: %s\n", name, value);

http2_server_con_state_t *con = (http2_server_con_state_t *) userdata;
http2_server_con_state_t *con = (http2_server_con_state_t *) userdata;

if (*name == ':')
{
Expand Down Expand Up @@ -66,7 +67,7 @@ static int onDataChunkRecvCallback(nghttp2_session *session, uint8_t flags, int3
{
return 0;
}
http2_server_con_state_t *con = (http2_server_con_state_t *) userdata;
http2_server_con_state_t *con = (http2_server_con_state_t *) userdata;

http2_server_child_con_state_t *stream = nghttp2_session_get_stream_user_data(session, stream_id);
if (! stream)
Expand Down Expand Up @@ -146,7 +147,7 @@ static int onFrameRecvCallback(nghttp2_session *session, const nghttp2_frame *fr
// LOGD("onFrameRecvCallback\n");
printFrameHd(&frame->hd);
http2_server_con_state_t *con = (http2_server_con_state_t *) userdata;
tunnel_t * self = con->tunnel;
tunnel_t *self = con->tunnel;

switch (frame->hd.type)
{
Expand Down Expand Up @@ -184,7 +185,7 @@ static int onFrameRecvCallback(nghttp2_session *session, const nghttp2_frame *fr
}
nghttp2_session_set_stream_user_data(con->session, stream->stream_id, NULL);
context_t *fc = newFinContext(stream->line);
tunnel_t * dest = stream->tunnel;
tunnel_t *dest = stream->tunnel;
removeStream(con, stream);
deleteHttp2Stream(stream);
CSTATE_MUT(fc) = NULL;
Expand Down Expand Up @@ -220,8 +221,7 @@ static int onFrameRecvCallback(nghttp2_session *session, const nghttp2_frame *fr
return 0;
}

static bool trySendResponse(tunnel_t *self, http2_server_con_state_t *con, size_t stream_id,
shift_buffer_t *buf)
static bool trySendResponse(tunnel_t *self, http2_server_con_state_t *con, size_t stream_id, shift_buffer_t *buf)
{
line_t *line = con->line;
// http2_server_con_state_t *con = ((http2_server_con_state_t *)(((line->chains_state)[self->chain_index])));
Expand All @@ -230,7 +230,7 @@ static bool trySendResponse(tunnel_t *self, http2_server_con_state_t *con, size_
return false;
}

char * data = NULL;
char *data = NULL;
size_t len;
len = nghttp2_session_mem_send(con->session, (const uint8_t **) &data);
// LOGD("nghttp2_session_mem_send %d\n", len);
Expand Down Expand Up @@ -306,31 +306,39 @@ static void upStream(tunnel_t *self, context_t *c)
http2_server_con_state_t *con = CSTATE(c);
con->state = kH2WantRecv;
size_t len = bufLen(c->payload);
size_t ret = nghttp2_session_mem_recv2(con->session, (const uint8_t *) rawBuf(c->payload), len);
reuseContextBuffer(c);

while (trySendResponse(self, con, 0, NULL))
{
if (! isAlive(c->line))
{
destroyContext(c);
return;
}
}
ssize_t ret = nghttp2_session_mem_recv2(con->session, (const uint8_t *) rawBuf(c->payload), len);

if (! isAlive(c->line))
{
destroyContext(c);
return;
}

if (ret != len)
if (ret != (ssize_t) len)
{
deleteHttp2Connection(con);
self->dw->downStream(self->dw, newFinContext(c->line));
destroyContext(c);
return;
}

while (trySendResponse(self, con, 0, NULL))
if (nghttp2_session_want_read(con->session) == 0 && nghttp2_session_want_write(con->session) == 0)
{
if (! isAlive(c->line))
{
destroyContext(c);
return;
}
context_t *fin_ctx = newFinContext(con->line);
deleteHttp2Connection(con);
self->dw->downStream(self->dw, fin_ctx);
}

reuseContextBuffer(c);
destroyContext(c);
}
else
Expand All @@ -353,7 +361,7 @@ static void upStream(tunnel_t *self, context_t *c)
static void downStream(tunnel_t *self, context_t *c)
{
http2_server_child_con_state_t *stream = CSTATE(c);
http2_server_con_state_t * con = stream->parent->chains_state[self->chain_index];
http2_server_con_state_t *con = stream->parent->chains_state[self->chain_index];

if (c->payload != NULL)
{
Expand Down Expand Up @@ -389,7 +397,7 @@ static void downStream(tunnel_t *self, context_t *c)
deleteHttp2Stream(stream);
CSTATE_MUT(c) = NULL;

while (trySendResponse(self, con, 0, NULL))
while (trySendResponse(self, con, 0, NULL))
{
if (! isAlive(c->line))
{
Expand All @@ -414,11 +422,9 @@ static void downStream(tunnel_t *self, context_t *c)
}
}



tunnel_t *newHttp2Server(node_instance_context_t *instance_info)
{
(void)instance_info;
(void) instance_info;
http2_server_state_t *state = malloc(sizeof(http2_server_state_t));
memset(state, 0, sizeof(http2_server_state_t));

Expand All @@ -432,11 +438,11 @@ tunnel_t *newHttp2Server(node_instance_context_t *instance_info)
nghttp2_option_set_no_closed_streams(state->ngoptions, 1);
nghttp2_option_set_no_http_messaging(state->ngoptions, 1);

tunnel_t *t = newTunnel();
t->state = state;
t->upStream = &upStream;
t->downStream = &downStream;
tunnel_t *t = newTunnel();
t->state = state;
t->upStream = &upStream;
t->downStream = &downStream;

return t;
}

Expand Down
2 changes: 1 addition & 1 deletion tunnels/server/reality/reality_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ static void downStream(tunnel_t *self, context_t *c)
case kConAuthorized:;
shift_buffer_t *buf = c->payload;
c->payload = NULL;
const unsigned int chunk_size = ((1 << 16) - (kSignLen + (2 * kEncryptionBlockSize) + kIVlen));
const unsigned int chunk_size = ((1 << 15) - (kSignLen + (2 * kEncryptionBlockSize) + kIVlen));

if (bufLen(buf) < chunk_size)
{
Expand Down
4 changes: 2 additions & 2 deletions ww/ww.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ struct ww_runtime_state_s *getWW(void)
}

// trimming should not be necessary, using it for test purposes
// todo (remove) should be removed ? (status: disabled)
// todo (remove) should be removed ? (status: enabled)
#ifdef OS_LINUX
void idleFreeMem(htimer_t *timer)
{
Expand All @@ -107,7 +107,7 @@ htimer_t *trim_timer = NULL;
_Noreturn void runMainThread(void)
{

#if defined(OS_LINUX) && false
#if defined(OS_LINUX) && true
trim_timer = htimer_add_period(loops[0], idleFreeMem, 2, 0, 0, 0, 0, INFINITE);
#endif

Expand Down

0 comments on commit 3d0bce5

Please sign in to comment.