2019-01-13 06:58:21 +08:00
|
|
|
/*
|
|
|
|
* libwebsockets - small server side websockets and web server implementation
|
|
|
|
*
|
2019-08-14 10:44:14 +01:00
|
|
|
* Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
|
2019-01-13 06:58:21 +08:00
|
|
|
*
|
2019-08-14 10:44:14 +01:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to
|
|
|
|
* deal in the Software without restriction, including without limitation the
|
|
|
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
|
* sell copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
2019-01-13 06:58:21 +08:00
|
|
|
*
|
2019-08-14 10:44:14 +01:00
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
2019-01-13 06:58:21 +08:00
|
|
|
*
|
2019-08-14 10:44:14 +01:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
* IN THE SOFTWARE.
|
2019-01-13 06:58:21 +08:00
|
|
|
*/
|
|
|
|
|
2019-08-15 10:49:52 +01:00
|
|
|
#include "private-lib-core.h"
|
2019-01-13 06:58:21 +08:00
|
|
|
|
|
|
|
void
|
2019-08-09 10:12:09 +01:00
|
|
|
__lws_wsi_remove_from_sul(struct lws *wsi)
|
2019-01-13 06:58:21 +08:00
|
|
|
{
|
2021-06-29 13:30:48 +01:00
|
|
|
lws_sul_cancel(&wsi->sul_timeout);
|
|
|
|
lws_sul_cancel(&wsi->sul_hrtimer);
|
|
|
|
lws_sul_cancel(&wsi->sul_validity);
|
|
|
|
#if defined(LWS_WITH_SYS_FAULT_INJECTION)
|
|
|
|
lws_sul_cancel(&wsi->sul_fault_timedclose);
|
|
|
|
#endif
|
2019-01-13 06:58:21 +08:00
|
|
|
}
|
|
|
|
|
2019-08-09 10:12:09 +01:00
|
|
|
/*
|
|
|
|
* hrtimer
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void
|
|
|
|
lws_sul_hrtimer_cb(lws_sorted_usec_list_t *sul)
|
2019-01-13 06:58:21 +08:00
|
|
|
{
|
2019-08-09 10:12:09 +01:00
|
|
|
struct lws *wsi = lws_container_of(sul, struct lws, sul_hrtimer);
|
2019-01-13 06:58:21 +08:00
|
|
|
|
fakewsi: replace with smaller substructure
Currently we always reserve a fakewsi per pt so events that don't have a related actual
wsi, like vhost-protocol-init or vhost cert init via protocol callback can make callbacks
that look reasonable to user protocol handler code expecting a valid wsi every time.
This patch splits out stuff that user callbacks often unconditionally expect to be in
a wsi, like context pointer, vhost pointer etc into a substructure, which is composed
into struct lws at the top of it. Internal references (struct lws is opaque, so there
are only internal references) are all updated to go via the substructre, the compiler
should make that a NOP.
Helpers are added when fakewsi is used and referenced.
If not PLAT_FREERTOS, we continue to provide a full fakewsi in the pt as before,
although the helpers improve consistency by zeroing down the substructure. There is
a huge amount of user code out there over the last 10 years that did not always have
the minimal examples to follow, some of it does some unexpected things.
If it is PLAT_FREERTOS, that is a newer thing in lws and users have the benefit of
being able to follow the minimal examples' approach. For PLAT_FREERTOS we don't
reserve the fakewsi in the pt any more, saving around 800 bytes. The helpers then
create a struct lws_a (the substructure) on the stack, zero it down (but it is only
like 4 pointers) and prepare it with whatever we know like the context.
Then we cast it to a struct lws * and use it in the user protocol handler call.
In this case, the remainder of the struct lws is undefined. However the amount of
old protocol handlers that might touch things outside of the substructure in
PLAT_FREERTOS is very limited compared to legacy lws user code and the saving is
significant on constrained devices.
User handlers should not be touching everything in a wsi every time anyway, there
are several cases where there is no valid wsi to do the call with. Dereference of
things outside the substructure should only happen when the callback reason shows
there is a valid wsi bound to the activity (as in all the minimal examples).
2020-07-19 08:33:46 +01:00
|
|
|
if (wsi->a.protocol &&
|
|
|
|
wsi->a.protocol->callback(wsi, LWS_CALLBACK_TIMER,
|
2019-08-09 10:12:09 +01:00
|
|
|
wsi->user_space, NULL, 0))
|
|
|
|
__lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS,
|
|
|
|
"hrtimer cb errored");
|
2019-01-13 06:58:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-08-08 16:58:55 +01:00
|
|
|
__lws_set_timer_usecs(struct lws *wsi, lws_usec_t us)
|
2019-01-13 06:58:21 +08:00
|
|
|
{
|
fakewsi: replace with smaller substructure
Currently we always reserve a fakewsi per pt so events that don't have a related actual
wsi, like vhost-protocol-init or vhost cert init via protocol callback can make callbacks
that look reasonable to user protocol handler code expecting a valid wsi every time.
This patch splits out stuff that user callbacks often unconditionally expect to be in
a wsi, like context pointer, vhost pointer etc into a substructure, which is composed
into struct lws at the top of it. Internal references (struct lws is opaque, so there
are only internal references) are all updated to go via the substructre, the compiler
should make that a NOP.
Helpers are added when fakewsi is used and referenced.
If not PLAT_FREERTOS, we continue to provide a full fakewsi in the pt as before,
although the helpers improve consistency by zeroing down the substructure. There is
a huge amount of user code out there over the last 10 years that did not always have
the minimal examples to follow, some of it does some unexpected things.
If it is PLAT_FREERTOS, that is a newer thing in lws and users have the benefit of
being able to follow the minimal examples' approach. For PLAT_FREERTOS we don't
reserve the fakewsi in the pt any more, saving around 800 bytes. The helpers then
create a struct lws_a (the substructure) on the stack, zero it down (but it is only
like 4 pointers) and prepare it with whatever we know like the context.
Then we cast it to a struct lws * and use it in the user protocol handler call.
In this case, the remainder of the struct lws is undefined. However the amount of
old protocol handlers that might touch things outside of the substructure in
PLAT_FREERTOS is very limited compared to legacy lws user code and the saving is
significant on constrained devices.
User handlers should not be touching everything in a wsi every time anyway, there
are several cases where there is no valid wsi to do the call with. Dereference of
things outside the substructure should only happen when the callback reason shows
there is a valid wsi bound to the activity (as in all the minimal examples).
2020-07-19 08:33:46 +01:00
|
|
|
struct lws_context_per_thread *pt = &wsi->a.context->pt[(int)wsi->tsi];
|
2019-01-13 06:58:21 +08:00
|
|
|
|
2019-08-09 10:12:09 +01:00
|
|
|
wsi->sul_hrtimer.cb = lws_sul_hrtimer_cb;
|
2020-05-28 12:48:17 +01:00
|
|
|
__lws_sul_insert_us(&pt->pt_sul_owner[LWSSULLI_MISS_IF_SUSPENDED],
|
|
|
|
&wsi->sul_hrtimer, us);
|
2019-01-13 06:58:21 +08:00
|
|
|
}
|
|
|
|
|
2020-01-02 08:32:23 +00:00
|
|
|
void
|
2019-01-13 06:58:21 +08:00
|
|
|
lws_set_timer_usecs(struct lws *wsi, lws_usec_t usecs)
|
|
|
|
{
|
|
|
|
__lws_set_timer_usecs(wsi, usecs);
|
|
|
|
}
|
|
|
|
|
2019-08-09 10:12:09 +01:00
|
|
|
/*
|
|
|
|
* wsi timeout
|
|
|
|
*/
|
|
|
|
|
2019-08-08 20:05:03 +01:00
|
|
|
static void
|
2019-08-09 10:12:09 +01:00
|
|
|
lws_sul_wsitimeout_cb(lws_sorted_usec_list_t *sul)
|
2019-08-08 20:05:03 +01:00
|
|
|
{
|
2019-08-09 10:12:09 +01:00
|
|
|
struct lws *wsi = lws_container_of(sul, struct lws, sul_timeout);
|
2021-04-04 04:06:24 +01:00
|
|
|
struct lws_context *cx = wsi->a.context;
|
|
|
|
struct lws_context_per_thread *pt = &cx->pt[(int)wsi->tsi];
|
2019-08-08 20:05:03 +01:00
|
|
|
|
2019-08-09 10:12:09 +01:00
|
|
|
/* no need to log normal idle keepalive timeout */
|
|
|
|
// if (wsi->pending_timeout != PENDING_TIMEOUT_HTTP_KEEPALIVE_IDLE)
|
|
|
|
#if defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2)
|
2019-08-10 07:32:32 +01:00
|
|
|
if (wsi->pending_timeout != PENDING_TIMEOUT_USER_OK)
|
2021-06-28 05:05:57 +01:00
|
|
|
lwsl_wsi_info(wsi, "TIMEDOUT WAITING %d, dhdr %d, ah %p, wl %d",
|
|
|
|
wsi->pending_timeout,
|
|
|
|
wsi->hdr_parsing_completed, wsi->http.ah,
|
|
|
|
pt->http.ah_wait_list_length);
|
2019-08-09 10:12:09 +01:00
|
|
|
#if defined(LWS_WITH_CGI)
|
|
|
|
if (wsi->http.cgi)
|
2021-06-28 05:05:57 +01:00
|
|
|
lwsl_wsi_notice(wsi, "CGI timeout: %s", wsi->http.cgi->summary);
|
2019-08-09 10:12:09 +01:00
|
|
|
#endif
|
|
|
|
#else
|
2019-08-10 07:32:32 +01:00
|
|
|
if (wsi->pending_timeout != PENDING_TIMEOUT_USER_OK)
|
2021-06-28 05:05:57 +01:00
|
|
|
lwsl_wsi_info(wsi, "TIMEDOUT WAITING on %d ",
|
|
|
|
wsi->pending_timeout);
|
2019-08-09 10:12:09 +01:00
|
|
|
#endif
|
|
|
|
/* cgi timeout */
|
|
|
|
if (wsi->pending_timeout != PENDING_TIMEOUT_HTTP_KEEPALIVE_IDLE)
|
|
|
|
/*
|
|
|
|
* Since he failed a timeout, he already had a chance to
|
|
|
|
* do something and was unable to... that includes
|
|
|
|
* situations like half closed connections. So process
|
|
|
|
* this "failed timeout" close as a violent death and
|
|
|
|
* don't try to do protocol cleanup like flush partials.
|
|
|
|
*/
|
|
|
|
wsi->socket_is_permanently_unusable = 1;
|
2019-08-18 05:04:15 +01:00
|
|
|
#if defined(LWS_WITH_CLIENT)
|
2019-08-10 09:20:27 +01:00
|
|
|
if (lwsi_state(wsi) == LRS_WAITING_SSL)
|
|
|
|
lws_inform_client_conn_fail(wsi,
|
2019-08-09 10:12:09 +01:00
|
|
|
(void *)"Timed out waiting SSL", 21);
|
2021-04-17 05:39:12 +01:00
|
|
|
if (lwsi_state(wsi) == LRS_WAITING_SERVER_REPLY)
|
|
|
|
lws_inform_client_conn_fail(wsi,
|
|
|
|
(void *)"Timed out waiting server reply", 30);
|
2019-08-10 09:20:27 +01:00
|
|
|
#endif
|
2019-01-13 06:58:21 +08:00
|
|
|
|
2021-04-04 04:06:24 +01:00
|
|
|
lws_context_lock(cx, __func__);
|
2020-08-31 08:12:16 +01:00
|
|
|
lws_pt_lock(pt, __func__);
|
2019-08-09 10:12:09 +01:00
|
|
|
__lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS, "timeout");
|
2020-08-31 08:12:16 +01:00
|
|
|
lws_pt_unlock(pt);
|
2021-04-04 04:06:24 +01:00
|
|
|
lws_context_unlock(cx);
|
2019-01-13 06:58:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
__lws_set_timeout(struct lws *wsi, enum pending_timeout reason, int secs)
|
|
|
|
{
|
fakewsi: replace with smaller substructure
Currently we always reserve a fakewsi per pt so events that don't have a related actual
wsi, like vhost-protocol-init or vhost cert init via protocol callback can make callbacks
that look reasonable to user protocol handler code expecting a valid wsi every time.
This patch splits out stuff that user callbacks often unconditionally expect to be in
a wsi, like context pointer, vhost pointer etc into a substructure, which is composed
into struct lws at the top of it. Internal references (struct lws is opaque, so there
are only internal references) are all updated to go via the substructre, the compiler
should make that a NOP.
Helpers are added when fakewsi is used and referenced.
If not PLAT_FREERTOS, we continue to provide a full fakewsi in the pt as before,
although the helpers improve consistency by zeroing down the substructure. There is
a huge amount of user code out there over the last 10 years that did not always have
the minimal examples to follow, some of it does some unexpected things.
If it is PLAT_FREERTOS, that is a newer thing in lws and users have the benefit of
being able to follow the minimal examples' approach. For PLAT_FREERTOS we don't
reserve the fakewsi in the pt any more, saving around 800 bytes. The helpers then
create a struct lws_a (the substructure) on the stack, zero it down (but it is only
like 4 pointers) and prepare it with whatever we know like the context.
Then we cast it to a struct lws * and use it in the user protocol handler call.
In this case, the remainder of the struct lws is undefined. However the amount of
old protocol handlers that might touch things outside of the substructure in
PLAT_FREERTOS is very limited compared to legacy lws user code and the saving is
significant on constrained devices.
User handlers should not be touching everything in a wsi every time anyway, there
are several cases where there is no valid wsi to do the call with. Dereference of
things outside the substructure should only happen when the callback reason shows
there is a valid wsi bound to the activity (as in all the minimal examples).
2020-07-19 08:33:46 +01:00
|
|
|
struct lws_context_per_thread *pt = &wsi->a.context->pt[(int)wsi->tsi];
|
2019-01-13 06:58:21 +08:00
|
|
|
|
2019-08-09 10:12:09 +01:00
|
|
|
wsi->sul_timeout.cb = lws_sul_wsitimeout_cb;
|
2020-05-28 12:48:17 +01:00
|
|
|
__lws_sul_insert_us(&pt->pt_sul_owner[LWSSULLI_MISS_IF_SUSPENDED],
|
|
|
|
&wsi->sul_timeout,
|
|
|
|
((lws_usec_t)secs) * LWS_US_PER_SEC);
|
2019-01-13 06:58:21 +08:00
|
|
|
|
2021-06-18 07:28:23 +01:00
|
|
|
lwsl_wsi_debug(wsi, "%d secs, reason %d\n", secs, reason);
|
2019-08-08 16:58:55 +01:00
|
|
|
|
2020-12-12 06:21:40 +00:00
|
|
|
wsi->pending_timeout = (char)reason;
|
2019-01-13 06:58:21 +08:00
|
|
|
}
|
|
|
|
|
2019-08-08 20:05:03 +01:00
|
|
|
void
|
2019-01-13 06:58:21 +08:00
|
|
|
lws_set_timeout(struct lws *wsi, enum pending_timeout reason, int secs)
|
|
|
|
{
|
fakewsi: replace with smaller substructure
Currently we always reserve a fakewsi per pt so events that don't have a related actual
wsi, like vhost-protocol-init or vhost cert init via protocol callback can make callbacks
that look reasonable to user protocol handler code expecting a valid wsi every time.
This patch splits out stuff that user callbacks often unconditionally expect to be in
a wsi, like context pointer, vhost pointer etc into a substructure, which is composed
into struct lws at the top of it. Internal references (struct lws is opaque, so there
are only internal references) are all updated to go via the substructre, the compiler
should make that a NOP.
Helpers are added when fakewsi is used and referenced.
If not PLAT_FREERTOS, we continue to provide a full fakewsi in the pt as before,
although the helpers improve consistency by zeroing down the substructure. There is
a huge amount of user code out there over the last 10 years that did not always have
the minimal examples to follow, some of it does some unexpected things.
If it is PLAT_FREERTOS, that is a newer thing in lws and users have the benefit of
being able to follow the minimal examples' approach. For PLAT_FREERTOS we don't
reserve the fakewsi in the pt any more, saving around 800 bytes. The helpers then
create a struct lws_a (the substructure) on the stack, zero it down (but it is only
like 4 pointers) and prepare it with whatever we know like the context.
Then we cast it to a struct lws * and use it in the user protocol handler call.
In this case, the remainder of the struct lws is undefined. However the amount of
old protocol handlers that might touch things outside of the substructure in
PLAT_FREERTOS is very limited compared to legacy lws user code and the saving is
significant on constrained devices.
User handlers should not be touching everything in a wsi every time anyway, there
are several cases where there is no valid wsi to do the call with. Dereference of
things outside the substructure should only happen when the callback reason shows
there is a valid wsi bound to the activity (as in all the minimal examples).
2020-07-19 08:33:46 +01:00
|
|
|
struct lws_context_per_thread *pt = &wsi->a.context->pt[(int)wsi->tsi];
|
2019-01-13 06:58:21 +08:00
|
|
|
|
2020-10-01 08:39:25 +01:00
|
|
|
lws_context_lock(pt->context, __func__);
|
2019-08-09 10:12:09 +01:00
|
|
|
lws_pt_lock(pt, __func__);
|
|
|
|
lws_dll2_remove(&wsi->sul_timeout.list);
|
|
|
|
lws_pt_unlock(pt);
|
2019-08-08 20:05:03 +01:00
|
|
|
|
2019-08-09 10:12:09 +01:00
|
|
|
if (!secs)
|
2020-10-01 08:39:25 +01:00
|
|
|
goto bail;
|
2019-01-30 20:59:56 +08:00
|
|
|
|
2019-01-13 06:58:21 +08:00
|
|
|
if (secs == LWS_TO_KILL_SYNC) {
|
2021-06-28 05:05:57 +01:00
|
|
|
lwsl_wsi_debug(wsi, "TO_KILL_SYNC");
|
2020-10-01 08:39:25 +01:00
|
|
|
lws_context_unlock(pt->context);
|
2019-01-13 06:58:21 +08:00
|
|
|
lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS,
|
|
|
|
"to sync kill");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (secs == LWS_TO_KILL_ASYNC)
|
|
|
|
secs = 0;
|
|
|
|
|
2019-12-23 11:31:57 +00:00
|
|
|
// assert(!secs || !wsi->mux_stream_immortal);
|
|
|
|
if (secs && wsi->mux_stream_immortal)
|
2021-06-18 07:28:23 +01:00
|
|
|
lwsl_wsi_err(wsi, "on immortal stream %d %d", reason, secs);
|
2019-09-13 10:33:24 +01:00
|
|
|
|
2019-01-13 06:58:21 +08:00
|
|
|
lws_pt_lock(pt, __func__);
|
|
|
|
__lws_set_timeout(wsi, reason, secs);
|
|
|
|
lws_pt_unlock(pt);
|
2020-10-01 08:39:25 +01:00
|
|
|
|
|
|
|
bail:
|
|
|
|
lws_context_unlock(pt->context);
|
2019-01-13 06:58:21 +08:00
|
|
|
}
|
|
|
|
|
2019-08-08 20:05:03 +01:00
|
|
|
void
|
|
|
|
lws_set_timeout_us(struct lws *wsi, enum pending_timeout reason, lws_usec_t us)
|
|
|
|
{
|
fakewsi: replace with smaller substructure
Currently we always reserve a fakewsi per pt so events that don't have a related actual
wsi, like vhost-protocol-init or vhost cert init via protocol callback can make callbacks
that look reasonable to user protocol handler code expecting a valid wsi every time.
This patch splits out stuff that user callbacks often unconditionally expect to be in
a wsi, like context pointer, vhost pointer etc into a substructure, which is composed
into struct lws at the top of it. Internal references (struct lws is opaque, so there
are only internal references) are all updated to go via the substructre, the compiler
should make that a NOP.
Helpers are added when fakewsi is used and referenced.
If not PLAT_FREERTOS, we continue to provide a full fakewsi in the pt as before,
although the helpers improve consistency by zeroing down the substructure. There is
a huge amount of user code out there over the last 10 years that did not always have
the minimal examples to follow, some of it does some unexpected things.
If it is PLAT_FREERTOS, that is a newer thing in lws and users have the benefit of
being able to follow the minimal examples' approach. For PLAT_FREERTOS we don't
reserve the fakewsi in the pt any more, saving around 800 bytes. The helpers then
create a struct lws_a (the substructure) on the stack, zero it down (but it is only
like 4 pointers) and prepare it with whatever we know like the context.
Then we cast it to a struct lws * and use it in the user protocol handler call.
In this case, the remainder of the struct lws is undefined. However the amount of
old protocol handlers that might touch things outside of the substructure in
PLAT_FREERTOS is very limited compared to legacy lws user code and the saving is
significant on constrained devices.
User handlers should not be touching everything in a wsi every time anyway, there
are several cases where there is no valid wsi to do the call with. Dereference of
things outside the substructure should only happen when the callback reason shows
there is a valid wsi bound to the activity (as in all the minimal examples).
2020-07-19 08:33:46 +01:00
|
|
|
struct lws_context_per_thread *pt = &wsi->a.context->pt[(int)wsi->tsi];
|
2019-08-08 20:05:03 +01:00
|
|
|
|
2019-08-09 10:12:09 +01:00
|
|
|
lws_pt_lock(pt, __func__);
|
|
|
|
lws_dll2_remove(&wsi->sul_timeout.list);
|
|
|
|
lws_pt_unlock(pt);
|
2019-08-08 20:05:03 +01:00
|
|
|
|
2019-08-09 10:12:09 +01:00
|
|
|
if (!us)
|
2019-08-08 20:05:03 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
lws_pt_lock(pt, __func__);
|
2020-05-28 12:48:17 +01:00
|
|
|
__lws_sul_insert_us(&pt->pt_sul_owner[LWSSULLI_MISS_IF_SUSPENDED],
|
|
|
|
&wsi->sul_timeout, us);
|
2019-08-08 20:05:03 +01:00
|
|
|
|
2021-06-28 05:05:57 +01:00
|
|
|
lwsl_wsi_notice(wsi, "%llu us, reason %d",
|
|
|
|
(unsigned long long)us, reason);
|
2019-08-08 20:05:03 +01:00
|
|
|
|
2020-12-12 06:21:40 +00:00
|
|
|
wsi->pending_timeout = (char)reason;
|
2019-08-08 20:05:03 +01:00
|
|
|
lws_pt_unlock(pt);
|
|
|
|
}
|
|
|
|
|
2019-09-18 13:09:32 +01:00
|
|
|
static void
|
|
|
|
lws_validity_cb(lws_sorted_usec_list_t *sul)
|
|
|
|
{
|
|
|
|
struct lws *wsi = lws_container_of(sul, struct lws, sul_validity);
|
fakewsi: replace with smaller substructure
Currently we always reserve a fakewsi per pt so events that don't have a related actual
wsi, like vhost-protocol-init or vhost cert init via protocol callback can make callbacks
that look reasonable to user protocol handler code expecting a valid wsi every time.
This patch splits out stuff that user callbacks often unconditionally expect to be in
a wsi, like context pointer, vhost pointer etc into a substructure, which is composed
into struct lws at the top of it. Internal references (struct lws is opaque, so there
are only internal references) are all updated to go via the substructre, the compiler
should make that a NOP.
Helpers are added when fakewsi is used and referenced.
If not PLAT_FREERTOS, we continue to provide a full fakewsi in the pt as before,
although the helpers improve consistency by zeroing down the substructure. There is
a huge amount of user code out there over the last 10 years that did not always have
the minimal examples to follow, some of it does some unexpected things.
If it is PLAT_FREERTOS, that is a newer thing in lws and users have the benefit of
being able to follow the minimal examples' approach. For PLAT_FREERTOS we don't
reserve the fakewsi in the pt any more, saving around 800 bytes. The helpers then
create a struct lws_a (the substructure) on the stack, zero it down (but it is only
like 4 pointers) and prepare it with whatever we know like the context.
Then we cast it to a struct lws * and use it in the user protocol handler call.
In this case, the remainder of the struct lws is undefined. However the amount of
old protocol handlers that might touch things outside of the substructure in
PLAT_FREERTOS is very limited compared to legacy lws user code and the saving is
significant on constrained devices.
User handlers should not be touching everything in a wsi every time anyway, there
are several cases where there is no valid wsi to do the call with. Dereference of
things outside the substructure should only happen when the callback reason shows
there is a valid wsi bound to the activity (as in all the minimal examples).
2020-07-19 08:33:46 +01:00
|
|
|
struct lws_context_per_thread *pt = &wsi->a.context->pt[(int)wsi->tsi];
|
2019-09-18 13:09:32 +01:00
|
|
|
const lws_retry_bo_t *rbo = wsi->retry_policy;
|
|
|
|
|
|
|
|
/* one of either the ping or hangup validity threshold was crossed */
|
|
|
|
|
|
|
|
if (wsi->validity_hup) {
|
2021-06-28 05:05:57 +01:00
|
|
|
lwsl_wsi_info(wsi, "validity too old");
|
2022-05-03 16:47:14 +02:00
|
|
|
struct lws_context *cx = wsi->a.context;
|
|
|
|
struct lws_context_per_thread *pt = &cx->pt[(int)wsi->tsi];
|
2021-08-27 13:08:03 +01:00
|
|
|
|
2022-05-03 16:47:14 +02:00
|
|
|
lws_context_lock(cx, __func__);
|
2021-08-27 13:08:03 +01:00
|
|
|
lws_pt_lock(pt, __func__);
|
2019-09-18 13:09:32 +01:00
|
|
|
__lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS,
|
|
|
|
"validity timeout");
|
2021-08-27 13:08:03 +01:00
|
|
|
lws_pt_unlock(pt);
|
2022-05-03 16:47:14 +02:00
|
|
|
lws_context_unlock(cx);
|
2019-09-18 13:09:32 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* schedule a protocol-dependent ping */
|
|
|
|
|
2021-06-28 05:05:57 +01:00
|
|
|
lwsl_wsi_info(wsi, "scheduling validity check");
|
2019-09-18 13:09:32 +01:00
|
|
|
|
roles: compress role ops structs
role ops are usually only sparsely filled, there are currently 20
function pointers but several roles only fill in two. No single
role has more than 14 of the ops. On a 32/64 bit build this part
of the ops struct takes a fixed 80 / 160 bytes then.
First reduce the type of the callback reason part from uint16_t to
uint8_t, this saves 12 bytes unconditionally.
Change to a separate function pointer array with a nybble index
array, it costs 10 bytes for the index and a pointer to the
separate array, for 32-bit the cost is
2 + (4 x ops_used)
and for 64-bit
6 + (8 x ops_used)
for 2 x ops_used it means 32-bit: 10 vs 80 / 64-bit: 22 vs 160
For a typical system with h1 (9), h2 (14), listen (2), netlink (2),
pipe (1), raw_skt (3), ws (12), == 43 ops_used out of 140, it means
the .rodata for this reduced from 32-bit: 560 -> 174 (386 byte
saving) and 64-bit: 1120 -> 350 (770 byte saving)
This doesn't account for the changed function ops calling code, two
ways were tried, a preprocessor macro and explicit functions
For an x86_64 gcc 10 build with most options, release mode,
.text + .rodata
before patch: 553282
accessor macro: 552714 (568 byte saving)
accessor functions: 553674 (392 bytes worse than without patch)
therefore we went with the macros
2020-10-19 13:55:21 +01:00
|
|
|
if (lws_rops_fidx(wsi->role_ops, LWS_ROPS_issue_keepalive))
|
|
|
|
lws_rops_func_fidx(wsi->role_ops, LWS_ROPS_issue_keepalive).
|
|
|
|
issue_keepalive(wsi, 0);
|
2019-09-18 13:09:32 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We arrange to come back here after the additional ping to hangup time
|
|
|
|
* and do the hangup, unless we get validated (by, eg, a PONG) and
|
|
|
|
* reset the timer
|
|
|
|
*/
|
|
|
|
|
|
|
|
assert(rbo->secs_since_valid_hangup > rbo->secs_since_valid_ping);
|
|
|
|
|
|
|
|
wsi->validity_hup = 1;
|
2020-05-28 12:48:17 +01:00
|
|
|
__lws_sul_insert_us(&pt->pt_sul_owner[!!wsi->conn_validity_wakesuspend],
|
|
|
|
&wsi->sul_validity,
|
|
|
|
((uint64_t)rbo->secs_since_valid_hangup -
|
2019-09-18 13:09:32 +01:00
|
|
|
rbo->secs_since_valid_ping) * LWS_US_PER_SEC);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The role calls this back to actually confirm validity on a particular wsi
|
|
|
|
* (which may not be the original wsi)
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
|
|
|
_lws_validity_confirmed_role(struct lws *wsi)
|
|
|
|
{
|
fakewsi: replace with smaller substructure
Currently we always reserve a fakewsi per pt so events that don't have a related actual
wsi, like vhost-protocol-init or vhost cert init via protocol callback can make callbacks
that look reasonable to user protocol handler code expecting a valid wsi every time.
This patch splits out stuff that user callbacks often unconditionally expect to be in
a wsi, like context pointer, vhost pointer etc into a substructure, which is composed
into struct lws at the top of it. Internal references (struct lws is opaque, so there
are only internal references) are all updated to go via the substructre, the compiler
should make that a NOP.
Helpers are added when fakewsi is used and referenced.
If not PLAT_FREERTOS, we continue to provide a full fakewsi in the pt as before,
although the helpers improve consistency by zeroing down the substructure. There is
a huge amount of user code out there over the last 10 years that did not always have
the minimal examples to follow, some of it does some unexpected things.
If it is PLAT_FREERTOS, that is a newer thing in lws and users have the benefit of
being able to follow the minimal examples' approach. For PLAT_FREERTOS we don't
reserve the fakewsi in the pt any more, saving around 800 bytes. The helpers then
create a struct lws_a (the substructure) on the stack, zero it down (but it is only
like 4 pointers) and prepare it with whatever we know like the context.
Then we cast it to a struct lws * and use it in the user protocol handler call.
In this case, the remainder of the struct lws is undefined. However the amount of
old protocol handlers that might touch things outside of the substructure in
PLAT_FREERTOS is very limited compared to legacy lws user code and the saving is
significant on constrained devices.
User handlers should not be touching everything in a wsi every time anyway, there
are several cases where there is no valid wsi to do the call with. Dereference of
things outside the substructure should only happen when the callback reason shows
there is a valid wsi bound to the activity (as in all the minimal examples).
2020-07-19 08:33:46 +01:00
|
|
|
struct lws_context_per_thread *pt = &wsi->a.context->pt[(int)wsi->tsi];
|
2019-09-18 13:09:32 +01:00
|
|
|
const lws_retry_bo_t *rbo = wsi->retry_policy;
|
|
|
|
|
|
|
|
if (!rbo || !rbo->secs_since_valid_hangup)
|
|
|
|
return;
|
|
|
|
|
|
|
|
wsi->validity_hup = 0;
|
|
|
|
wsi->sul_validity.cb = lws_validity_cb;
|
|
|
|
|
|
|
|
wsi->validity_hup = rbo->secs_since_valid_ping >=
|
|
|
|
rbo->secs_since_valid_hangup;
|
|
|
|
|
2021-06-28 05:05:57 +01:00
|
|
|
lwsl_wsi_info(wsi, "setting validity timer %ds (hup %d)",
|
|
|
|
wsi->validity_hup ? rbo->secs_since_valid_hangup :
|
2019-09-18 13:09:32 +01:00
|
|
|
rbo->secs_since_valid_ping,
|
2021-06-28 05:05:57 +01:00
|
|
|
wsi->validity_hup);
|
2019-09-18 13:09:32 +01:00
|
|
|
|
2020-05-28 12:48:17 +01:00
|
|
|
__lws_sul_insert_us(&pt->pt_sul_owner[!!wsi->conn_validity_wakesuspend],
|
|
|
|
&wsi->sul_validity,
|
|
|
|
((uint64_t)(wsi->validity_hup ?
|
2019-09-18 13:09:32 +01:00
|
|
|
rbo->secs_since_valid_hangup :
|
|
|
|
rbo->secs_since_valid_ping)) * LWS_US_PER_SEC);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
lws_validity_confirmed(struct lws *wsi)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* This may be a stream inside a muxed network connection... leave it
|
|
|
|
* to the role to figure out who actually needs to understand their
|
|
|
|
* validity was confirmed.
|
|
|
|
*/
|
2019-11-05 04:53:09 +00:00
|
|
|
if (!wsi->h2_stream_carries_ws && /* only if not encapsulated */
|
roles: compress role ops structs
role ops are usually only sparsely filled, there are currently 20
function pointers but several roles only fill in two. No single
role has more than 14 of the ops. On a 32/64 bit build this part
of the ops struct takes a fixed 80 / 160 bytes then.
First reduce the type of the callback reason part from uint16_t to
uint8_t, this saves 12 bytes unconditionally.
Change to a separate function pointer array with a nybble index
array, it costs 10 bytes for the index and a pointer to the
separate array, for 32-bit the cost is
2 + (4 x ops_used)
and for 64-bit
6 + (8 x ops_used)
for 2 x ops_used it means 32-bit: 10 vs 80 / 64-bit: 22 vs 160
For a typical system with h1 (9), h2 (14), listen (2), netlink (2),
pipe (1), raw_skt (3), ws (12), == 43 ops_used out of 140, it means
the .rodata for this reduced from 32-bit: 560 -> 174 (386 byte
saving) and 64-bit: 1120 -> 350 (770 byte saving)
This doesn't account for the changed function ops calling code, two
ways were tried, a preprocessor macro and explicit functions
For an x86_64 gcc 10 build with most options, release mode,
.text + .rodata
before patch: 553282
accessor macro: 552714 (568 byte saving)
accessor functions: 553674 (392 bytes worse than without patch)
therefore we went with the macros
2020-10-19 13:55:21 +01:00
|
|
|
wsi->role_ops &&
|
|
|
|
lws_rops_fidx(wsi->role_ops, LWS_ROPS_issue_keepalive))
|
|
|
|
lws_rops_func_fidx(wsi->role_ops, LWS_ROPS_issue_keepalive).
|
|
|
|
issue_keepalive(wsi, 1);
|
2019-09-18 13:09:32 +01:00
|
|
|
}
|