Skip to content

Commit

Permalink
Merge pull request #932 from openziti/avoid-lwip-timer-churn
Browse files Browse the repository at this point in the history
avoid lwip timer churn
  • Loading branch information
ekoby committed Aug 13, 2024
2 parents 5861088 + 7565646 commit 707aed6
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 2 deletions.
2 changes: 2 additions & 0 deletions lib/ziti-tunnel/lwip/lwipopts.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,12 @@

// APIs
#define LWIP_RAW 1
#define LWIP_ARP 0
#define LWIP_NETCONN 0
#define LWIP_SOCKET 0

// protocols
#define LWIP_IPV6_MLD 0
#define LWIP_IPV6 1 /* enable ipv6 */
#define IPV6_FRAG_COPYHEADER 1 /* avoid assert in lwip code when ipv6 is enabled */

Expand Down
1 change: 1 addition & 0 deletions lib/ziti-tunnel/tunnel_tcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,7 @@ u8_t recv_tcp(void *tnlr_ctx_arg, struct raw_pcb *pcb, struct pbuf *p, const ip_
/* now we wait for the tunneler app to call ziti_tunneler_dial_complete() */

done:
check_tnlr_timer(tnlr_ctx);
pbuf_free(p);
return 1;
}
Expand Down
26 changes: 24 additions & 2 deletions lib/ziti-tunnel/ziti_tunnel.c
Original file line number Diff line number Diff line change
Expand Up @@ -486,8 +486,30 @@ static void on_tun_data(uv_poll_t * req, int status, int events) {
}
}

static void check_lwip_timeouts(uv_timer_t * handle) {
static void check_lwip_timeouts(uv_timer_t * timer) {
// if timer is not active it may have been a while since
// we run timers, let LWIP adjust timeouts
if (!uv_is_active((const uv_handle_t *) timer)) {
sys_restart_timeouts();
}

// run timers before potentially pausing
sys_check_timeouts();

if (tcp_active_pcbs == NULL && tcp_tw_pcbs == NULL) {
uv_timer_stop(timer);
return;
}

u32_t sleep = sys_timeouts_sleeptime();
TNL_LOG(TRACE, "next wake in %d millis", sleep);
// second sleep arg is only need to make timer `active` next time
// we hit this function to avoid calling sys_restart_timeouts()
uv_timer_start(timer, check_lwip_timeouts, sleep, sleep);
}

void check_tnlr_timer(tunneler_context tnlr_ctx) {
check_lwip_timeouts(&tnlr_ctx->lwip_timer_req);
}

/**
Expand Down Expand Up @@ -556,9 +578,9 @@ static void run_packet_loop(uv_loop_t *loop, tunneler_context tnlr_ctx) {
exit(1);
}

// don't run LWIP timers until we have active TCP connections
uv_timer_init(loop, &tnlr_ctx->lwip_timer_req);
uv_unref((uv_handle_t *) &tnlr_ctx->lwip_timer_req);
uv_timer_start(&tnlr_ctx->lwip_timer_req, check_lwip_timeouts, 0, 10);
}

typedef struct ziti_tunnel_async_call_s {
Expand Down
1 change: 1 addition & 0 deletions lib/ziti-tunnel/ziti_tunnel_priv.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ struct tunneler_io_ctx_s {
uint32_t idle_timeout;
};

extern void check_tnlr_timer(tunneler_context tnlr_ctx);
extern void free_tunneler_io_context(tunneler_io_context *tnlr_io_ctx_p);

extern void free_intercept(intercept_ctx_t *intercept);
Expand Down

0 comments on commit 707aed6

Please sign in to comment.