From d806fcfe73857e7a2fec65d80217f5cc7b0aaee3 Mon Sep 17 00:00:00 2001 From: Radkesvat <134321679+radkesvat@users.noreply.github.com> Date: Fri, 19 Jul 2024 08:20:19 +0000 Subject: [PATCH] buffer tweaks --- core/main.c | 4 +-- tunnels/adapters/listener/tcp/tcp_listener.c | 17 +++++----- ww/eventloop/event/nio.c | 8 +++-- ww/managers/socket_manager.h | 6 ++-- ww/shiftbuffer.c | 33 ++++++++++---------- ww/ww.c | 2 +- 6 files changed, 35 insertions(+), 35 deletions(-) diff --git a/core/main.c b/core/main.c index 84653fea..09c2c167 100644 --- a/core/main.c +++ b/core/main.c @@ -64,19 +64,19 @@ int main(void) { c_foreach(k, vec_config_path_t, getCoreSettings()->config_paths) { - // read config file LOGD("Core: begin parsing config file \"%s\"", *k.ref); config_file_t *cfile = parseConfigFile(*k.ref); /* in case of error in config file, the details is already printed out and the - program will not reach this line + program will not reach this line. */ LOGI("Core: parsing config file \"%s\" complete", *k.ref); runConfigFile(cfile); } } + LOGD("Core: starting workers ..."); startSocketManager(); runMainThread(); diff --git a/tunnels/adapters/listener/tcp/tcp_listener.c b/tunnels/adapters/listener/tcp/tcp_listener.c index 7e44af92..a63472d1 100644 --- a/tunnels/adapters/listener/tcp/tcp_listener.c +++ b/tunnels/adapters/listener/tcp/tcp_listener.c @@ -206,21 +206,18 @@ static void downStream(tunnel_t *self, context_t *c) } else { - - if (c->est) - { - assert(! cstate->established); - cstate->established = true; - hio_set_keepalive_timeout(cstate->io, kEstablishedKeepAliveTimeOutMs); - destroyContext(c); - return; - } if (c->fin) { CSTATE_DROP(c); cleanup(cstate, true); destroyContext(c); - return; + } + else if (c->est) + { + assert(! cstate->established); + cstate->established = true; + hio_set_keepalive_timeout(cstate->io, kEstablishedKeepAliveTimeOutMs); + destroyContext(c); } } } diff --git a/ww/eventloop/event/nio.c b/ww/eventloop/event/nio.c index 23499a6d..1217fa46 100644 --- a/ww/eventloop/event/nio.c +++ b/ww/eventloop/event/nio.c @@ -186,7 +186,7 @@ static int __nio_write(hio_t* io, const void* buf, int len) { static void nio_read(hio_t* io) { // printd("nio_read fd=%d\n", io->fd); int nread = 0, err = 0; -// read:; + // read:; // #if defined(OS_LINUX) && defined(HAVE_PIPE) // if(io->pfd_w){ @@ -195,7 +195,10 @@ static void nio_read(hio_t* io) { // #endif shift_buffer_t* buf = popBuffer(io->loop->bufpool); unsigned int available = rCap(buf); - if (WW_UNLIKELY(available < 1024)) { + if (available > (1U << 15)) { + available = (1U << 15); + } + else if (WW_UNLIKELY(available < 1024)) { reserveBufSpace(buf, 1024); } nread = __nio_read(io, rawBufMut(buf), available); @@ -488,7 +491,6 @@ int hio_close(hio_t* io) { // return hio_close_async(io); /* tid lost its meaning, its now ww tid */ // } - if (io->closed) { return 0; diff --git a/ww/managers/socket_manager.h b/ww/managers/socket_manager.h index 23904f6c..808fadb6 100644 --- a/ww/managers/socket_manager.h +++ b/ww/managers/socket_manager.h @@ -76,15 +76,15 @@ typedef struct udpsock_s } udpsock_t; -// if you asked for udp, youll get such struct when a udp packet received and passed all filters +// if you asked for udp, you'll get such struct when a udp packet received and passed all filters typedef struct udp_payload_s { udpsock_t *sock; tunnel_t *tunnel; - uint8_t tid; + shift_buffer_t *buf; sockaddr_u peer_addr; uint16_t real_localport; - shift_buffer_t *buf; + uint8_t tid; } udp_payload_t; diff --git a/ww/shiftbuffer.c b/ww/shiftbuffer.c index 4736b08a..7e78b8ba 100644 --- a/ww/shiftbuffer.c +++ b/ww/shiftbuffer.c @@ -7,14 +7,15 @@ #include #include -#define PREPADDING (ram_profile >= kRamProfileS2Memory ? (1U << 13) : ((1U << 8) + 512)) +#define LEFTPADDING (ram_profile >= kRamProfileS2Memory ? (1U << 13) : ((1U << 8) + 512)) +#define RIGHTPADDING (ram_profile >= kRamProfileS2Memory ? (1U << 10) : (1U << 8)) pool_item_t *allocShiftBufferPoolHandle(struct generic_pool_s *pool) { (void) pool; shift_buffer_t *self = wwmGlobalMalloc(sizeof(shift_buffer_t)); - *self = (shift_buffer_t){ + *self = (shift_buffer_t) { .refc = wwmGlobalMalloc(sizeof(self->refc[0])), }; return self; @@ -53,14 +54,14 @@ shift_buffer_t *newShiftBuffer(uint8_t tid, unsigned int pre_cap) // NOLINT pre_cap = (unsigned int) pow(2, ceil(log2((double) max(16, pre_cap)))); } - unsigned int real_cap = pre_cap + (PREPADDING); + unsigned int real_cap = pre_cap + LEFTPADDING + RIGHTPADDING; // shift_buffer_t *self = wwmGlobalMalloc(sizeof(shift_buffer_t)); shift_buffer_t *self = (shift_buffer_t *) popPoolItem(shift_buffer_pools[tid]); self->calc_len = 0; - self->offset = 0; - self->curpos = PREPADDING; + self->offset = 0; + self->curpos = LEFTPADDING; self->full_cap = real_cap; self->pbuf = wwmGlobalMalloc(real_cap); // self->refc = wwmGlobalMalloc(sizeof(self->refc[0])), @@ -99,7 +100,7 @@ void reset(shift_buffer_t *self, unsigned int pre_cap) pre_cap = (unsigned int) pow(2, ceil(log2(((double) max(16, pre_cap))))); } - unsigned int real_cap = pre_cap + (PREPADDING); + unsigned int real_cap = pre_cap + LEFTPADDING + RIGHTPADDING; if (self->full_cap != real_cap) { @@ -109,8 +110,8 @@ void reset(shift_buffer_t *self, unsigned int pre_cap) // memset(self->pbuf, 0, real_cap); } self->calc_len = 0; - self->offset = 0; - self->curpos = PREPADDING; + self->offset = 0; + self->curpos = LEFTPADDING; } void unShallow(shift_buffer_t *self) @@ -123,7 +124,7 @@ void unShallow(shift_buffer_t *self) *(self->refc) = 1; char *old_buf = self->pbuf; self->pbuf = wwmGlobalMalloc(self->full_cap); - self->offset = 0; + self->offset = 0; memcpy(&(self->pbuf[self->curpos]), &(old_buf[self->curpos]), (self->calc_len)); } @@ -139,7 +140,7 @@ void expand(shift_buffer_t *self, unsigned int increase) *(self->refc) = 1; char *old_buf = self->pbuf; self->pbuf = wwmGlobalMalloc(new_realcap); - self->offset = 0; + self->offset = 0; unsigned int dif = (new_realcap - self->full_cap) / 2; memcpy(&(self->pbuf[self->curpos + dif]), &(old_buf[self->curpos]), self->calc_len); self->curpos += dif; @@ -206,19 +207,19 @@ shift_buffer_t *sliceBuffer(const uint8_t tid, shift_buffer_t *const self, const return newbuf; } - void *tmp_pbuf = self->pbuf; - void *tmp_refc = self->refc; + void *tmp_pbuf = self->pbuf; + void *tmp_refc = self->refc; unsigned int tmpoffset = self->offset; - self->refc = newbuf->refc; - self->pbuf = newbuf->pbuf; + self->refc = newbuf->refc; + self->pbuf = newbuf->pbuf; self->offset = newbuf->offset; - *newbuf = (struct shift_buffer_s){.calc_len = self->calc_len, + *newbuf = (struct shift_buffer_s) {.calc_len = self->calc_len, .curpos = self->curpos, .full_cap = self->full_cap, .pbuf = tmp_pbuf, .refc = tmp_refc, - .offset = tmpoffset}; + .offset = tmpoffset}; memcpy(rawBufMut(self), &(((const char *) rawBuf(newbuf))[bytes]), bufLen(newbuf) - bytes); shiftr(self, bytes); diff --git a/ww/ww.c b/ww/ww.c index 70549f9d..5f5d5f0e 100644 --- a/ww/ww.c +++ b/ww/ww.c @@ -180,7 +180,7 @@ void createWW(const ww_construction_data_t init_data) // [Section] workers and pools heap allocation { workers_count = init_data.workers_count; - if (workers_count <= 0 || workers_count > (256 - kAdditionalReservedWorkers)) + if (workers_count <= 0 || workers_count > (255 - kAdditionalReservedWorkers)) { fprintf(stderr, "workers count was not in valid range, value: %u range:[1 - 255]\n", workers_count); }