Skip to content

Commit c85a957

Browse files
committed
Added socket send and recv timeout options
1 parent 0e17ab2 commit c85a957

File tree

12 files changed

+398
-0
lines changed

12 files changed

+398
-0
lines changed

core/iwasm/libraries/lib-socket/inc/wasi_socket_ext.h

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,62 @@ __wasi_sock_set_send_buf_size(__wasi_fd_t fd)
445445
__imported_wasi_snapshot_preview1_sock_set_send_buf_size((int32_t)fd);
446446
}
447447

448+
int32_t
449+
__imported_wasi_snapshot_preview1_sock_get_recv_timeout(int32_t arg0,
450+
int32_t arg1)
451+
__attribute__((__import_module__("wasi_snapshot_preview1"),
452+
__import_name__("sock_get_recv_timeout")));
453+
454+
static inline __wasi_errno_t
455+
__wasi_sock_get_recv_timeout(__wasi_fd_t fd, uint64_t *timeout_us)
456+
{
457+
return (__wasi_errno_t)
458+
__imported_wasi_snapshot_preview1_sock_get_recv_timeout((int32_t)fd,
459+
(int32_t)timeout_us);
460+
}
461+
462+
int32_t
463+
__imported_wasi_snapshot_preview1_sock_set_recv_timeout(int32_t arg0,
464+
int32_t arg1)
465+
__attribute__((__import_module__("wasi_snapshot_preview1"),
466+
__import_name__("sock_set_recv_timeout")));
467+
468+
static inline __wasi_errno_t
469+
__wasi_sock_set_recv_timeout(__wasi_fd_t fd, uint64_t *timeout_us)
470+
{
471+
return (__wasi_errno_t)
472+
__imported_wasi_snapshot_preview1_sock_set_recv_timeout((int32_t)fd,
473+
(int32_t)timeout_us);
474+
}
475+
476+
int32_t
477+
__imported_wasi_snapshot_preview1_sock_get_send_timeout(int32_t arg0,
478+
int32_t arg1)
479+
__attribute__((__import_module__("wasi_snapshot_preview1"),
480+
__import_name__("sock_get_send_timeout")));
481+
482+
static inline __wasi_errno_t
483+
__wasi_sock_get_send_timeout(__wasi_fd_t fd, uint64_t *timeout_us)
484+
{
485+
return (__wasi_errno_t)
486+
__imported_wasi_snapshot_preview1_sock_get_send_timeout((int32_t)fd,
487+
(int32_t)timeout_us);
488+
}
489+
490+
int32_t
491+
__imported_wasi_snapshot_preview1_sock_set_send_timeout(int32_t arg0,
492+
int32_t arg1)
493+
__attribute__((__import_module__("wasi_snapshot_preview1"),
494+
__import_name__("sock_set_send_timeout")));
495+
496+
static inline __wasi_errno_t
497+
__wasi_sock_set_send_timeout(__wasi_fd_t fd, uint64_t *timeout_us)
498+
{
499+
return (__wasi_errno_t)
500+
__imported_wasi_snapshot_preview1_sock_set_send_timeout((int32_t)fd,
501+
(int32_t)timeout_us);
502+
}
503+
448504
/**
449505
* TODO: modify recv() and send()
450506
* since don't want to re-compile the wasi-libc,

core/iwasm/libraries/libc-wasi/libc_wasi_wrapper.c

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,6 +1113,21 @@ wasi_sock_get_recv_buf_size(wasm_exec_env_t exec_env, wasi_fd_t fd,
11131113
return __WASI_ENOSYS;
11141114
}
11151115

1116+
static wasi_errno_t
1117+
wasi_sock_get_recv_timeout(wasm_exec_env_t exec_env, wasi_fd_t fd, uint64_t *timeout_us)
1118+
{
1119+
wasm_module_inst_t module_inst = get_module_inst(exec_env);
1120+
wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
1121+
struct fd_table *curfds = NULL;
1122+
1123+
if (!wasi_ctx)
1124+
return __WASI_EACCES;
1125+
1126+
curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx);
1127+
1128+
return wasmtime_ssp_sock_get_recv_timeout(curfds, fd, timeout_us);
1129+
}
1130+
11161131
static wasi_errno_t
11171132
wasi_sock_get_reuse_addr(wasm_exec_env_t exec_env, wasi_fd_t fd, uint8 *reuse)
11181133
{
@@ -1132,6 +1147,21 @@ wasi_sock_get_send_buf_size(wasm_exec_env_t exec_env, wasi_fd_t fd,
11321147
return __WASI_ENOSYS;
11331148
}
11341149

1150+
static wasi_errno_t
1151+
wasi_sock_get_send_timeout(wasm_exec_env_t exec_env, wasi_fd_t fd, uint64_t *timeout_us)
1152+
{
1153+
wasm_module_inst_t module_inst = get_module_inst(exec_env);
1154+
wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
1155+
struct fd_table *curfds = NULL;
1156+
1157+
if (!wasi_ctx)
1158+
return __WASI_EACCES;
1159+
1160+
curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx);
1161+
1162+
return wasmtime_ssp_sock_get_send_timeout(curfds, fd, timeout_us);
1163+
}
1164+
11351165
static wasi_errno_t
11361166
wasi_sock_listen(wasm_exec_env_t exec_env, wasi_fd_t fd, uint32 backlog)
11371167
{
@@ -1171,6 +1201,21 @@ wasi_sock_set_recv_buf_size(wasm_exec_env_t exec_env, wasi_fd_t fd,
11711201
return __WASI_ENOSYS;
11721202
}
11731203

1204+
static wasi_errno_t
1205+
wasi_sock_set_recv_timeout(wasm_exec_env_t exec_env, wasi_fd_t fd, uint64_t *timeout_us)
1206+
{
1207+
wasm_module_inst_t module_inst = get_module_inst(exec_env);
1208+
wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
1209+
struct fd_table *curfds = NULL;
1210+
1211+
if (!wasi_ctx)
1212+
return __WASI_EACCES;
1213+
1214+
curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx);
1215+
1216+
return wasmtime_ssp_sock_set_recv_timeout(curfds, fd, *timeout_us);
1217+
}
1218+
11741219
static wasi_errno_t
11751220
wasi_sock_set_reuse_addr(wasm_exec_env_t exec_env, wasi_fd_t fd, uint8 reuse)
11761221
{
@@ -1190,6 +1235,22 @@ wasi_sock_set_send_buf_size(wasm_exec_env_t exec_env, wasi_fd_t fd,
11901235
return __WASI_ENOSYS;
11911236
}
11921237

1238+
static wasi_errno_t
1239+
wasi_sock_set_send_timeout(wasm_exec_env_t exec_env, wasi_fd_t fd, uint64_t *timeout_us)
1240+
{
1241+
wasm_module_inst_t module_inst = get_module_inst(exec_env);
1242+
wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
1243+
struct fd_table *curfds = NULL;
1244+
1245+
if (!wasi_ctx)
1246+
return __WASI_EACCES;
1247+
1248+
curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx);
1249+
1250+
return wasmtime_ssp_sock_set_send_timeout(curfds, fd, *timeout_us);
1251+
}
1252+
1253+
11931254
static wasi_errno_t
11941255
wasi_sock_recv(wasm_exec_env_t exec_env, wasi_fd_t sock, iovec_app_t *ri_data,
11951256
uint32 ri_data_len, wasi_riflags_t ri_flags, uint32 *ro_data_len,
@@ -1414,17 +1475,21 @@ static NativeSymbol native_symbols_libc_wasi[] = {
14141475
REG_NATIVE_FUNC(sock_close, "(i)i"),
14151476
REG_NATIVE_FUNC(sock_connect, "(i*)i"),
14161477
REG_NATIVE_FUNC(sock_get_recv_buf_size, "(i*)i"),
1478+
REG_NATIVE_FUNC(sock_get_recv_timeout, "(i*)i"),
14171479
REG_NATIVE_FUNC(sock_get_reuse_addr, "(i*)i"),
14181480
REG_NATIVE_FUNC(sock_get_reuse_port, "(i*)i"),
14191481
REG_NATIVE_FUNC(sock_get_send_buf_size, "(i*)i"),
1482+
REG_NATIVE_FUNC(sock_get_send_timeout, "(i*)i"),
14201483
REG_NATIVE_FUNC(sock_listen, "(ii)i"),
14211484
REG_NATIVE_FUNC(sock_open, "(iii*)i"),
14221485
REG_NATIVE_FUNC(sock_recv, "(i*ii**)i"),
14231486
REG_NATIVE_FUNC(sock_send, "(i*ii*)i"),
14241487
REG_NATIVE_FUNC(sock_set_recv_buf_size, "(ii)i"),
1488+
REG_NATIVE_FUNC(sock_set_recv_timeout, "(i*)i"),
14251489
REG_NATIVE_FUNC(sock_set_reuse_addr, "(ii)i"),
14261490
REG_NATIVE_FUNC(sock_set_reuse_port, "(ii)i"),
14271491
REG_NATIVE_FUNC(sock_set_send_buf_size, "(ii)i"),
1492+
REG_NATIVE_FUNC(sock_set_send_timeout, "(i*)i"),
14281493
REG_NATIVE_FUNC(sock_shutdown, "(ii)i"),
14291494
REG_NATIVE_FUNC(sched_yield, "()i"),
14301495
};

core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/include/wasmtime_ssp.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,6 +1088,38 @@ __wasi_errno_t wasmtime_ssp_sock_shutdown(
10881088
__wasi_fd_t sock
10891089
) WASMTIME_SSP_SYSCALL_NAME(sock_shutdown) __attribute__((__warn_unused_result__));
10901090

1091+
__wasi_errno_t wasmtime_ssp_sock_set_recv_timeout(
1092+
#if !defined(WASMTIME_SSP_STATIC_CURFDS)
1093+
struct fd_table *curfds,
1094+
#endif
1095+
__wasi_fd_t sock,
1096+
uint64_t timeout_us
1097+
) WASMTIME_SSP_SYSCALL_NAME(sock_set_recv_timeout) __attribute__((__warn_unused_result__));
1098+
1099+
__wasi_errno_t wasmtime_ssp_sock_get_recv_timeout(
1100+
#if !defined(WASMTIME_SSP_STATIC_CURFDS)
1101+
struct fd_table *curfds,
1102+
#endif
1103+
__wasi_fd_t sock,
1104+
uint64_t *timeout_us
1105+
) WASMTIME_SSP_SYSCALL_NAME(sock_get_recv_timeout) __attribute__((__warn_unused_result__));
1106+
1107+
__wasi_errno_t wasmtime_ssp_sock_set_send_timeout(
1108+
#if !defined(WASMTIME_SSP_STATIC_CURFDS)
1109+
struct fd_table *curfds,
1110+
#endif
1111+
__wasi_fd_t sock,
1112+
uint64_t timeout_us
1113+
) WASMTIME_SSP_SYSCALL_NAME(sock_set_send_timeout) __attribute__((__warn_unused_result__));
1114+
1115+
__wasi_errno_t wasmtime_ssp_sock_get_send_timeout(
1116+
#if !defined(WASMTIME_SSP_STATIC_CURFDS)
1117+
struct fd_table *curfds,
1118+
#endif
1119+
__wasi_fd_t sock,
1120+
uint64_t *timeout_us
1121+
) WASMTIME_SSP_SYSCALL_NAME(sock_get_send_timeout) __attribute__((__warn_unused_result__));
1122+
10911123
__wasi_errno_t wasmtime_ssp_sched_yield(void)
10921124
WASMTIME_SSP_SYSCALL_NAME(sched_yield) __attribute__((__warn_unused_result__));
10931125

core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/posix.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3430,3 +3430,37 @@ addr_pool_destroy(struct addr_pool *addr_pool)
34303430
cur = next;
34313431
}
34323432
}
3433+
3434+
#ifndef WASMTIME_SSP_STATIC_CURFDS
3435+
#define WASMTIME_SSP_PASSTHROUGH_FD_TABLE struct fd_table *curfds,
3436+
#else
3437+
#define WASMTIME_SSP_PASSTHROUGH_FD_TABLE
3438+
#endif
3439+
3440+
// Defines a function that passes through the socket option to the OS implementation
3441+
#define WASMTIME_SSP_PASSTHROUGH_SOCKET_OPTION(FUNC_NAME, OPTION_TYPE) \
3442+
__wasi_errno_t wasmtime_ssp_sock_##FUNC_NAME( \
3443+
WASMTIME_SSP_PASSTHROUGH_FD_TABLE \
3444+
__wasi_fd_t sock, \
3445+
OPTION_TYPE option \
3446+
) { \
3447+
struct fd_object *fo; \
3448+
__wasi_errno_t error; \
3449+
int ret; \
3450+
error = fd_object_get(curfds, &fo, sock, 0, 0); \
3451+
if (error != 0) \
3452+
return error; \
3453+
ret = os_socket_##FUNC_NAME(fd_number(fo), option); \
3454+
fd_object_release(fo); \
3455+
if (BHT_OK != ret) \
3456+
return convert_errno(errno); \
3457+
return __WASI_ESUCCESS; \
3458+
}
3459+
3460+
WASMTIME_SSP_PASSTHROUGH_SOCKET_OPTION(set_send_timeout, uint64)
3461+
WASMTIME_SSP_PASSTHROUGH_SOCKET_OPTION(get_send_timeout, uint64*)
3462+
WASMTIME_SSP_PASSTHROUGH_SOCKET_OPTION(set_recv_timeout, uint64)
3463+
WASMTIME_SSP_PASSTHROUGH_SOCKET_OPTION(get_recv_timeout, uint64*)
3464+
3465+
#undef WASMTIME_SSP_PASSTHROUGH_FD_TABLE
3466+
#undef WASMTIME_SSP_PASSTHROUGH_SOCKET_OPTION

core/shared/platform/common/posix/posix_socket.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,54 @@ os_socket_convert_sockaddr(struct sockaddr *addr, uint8_t *buf, size_t buflen,
312312
return BHT_OK;
313313
}
314314

315+
int
316+
os_socket_set_send_timeout(bh_socket_t socket, uint64 timeout_us)
317+
{
318+
struct timeval tv;
319+
tv.tv_sec = timeout_us / 1000000UL;
320+
tv.tv_usec = timeout_us % 1000000UL;
321+
if (setsockopt(socket, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) != 0) {
322+
return BHT_ERROR;
323+
}
324+
return BHT_OK;
325+
}
326+
327+
int
328+
os_socket_get_send_timeout(bh_socket_t socket, uint64 *timeout_us)
329+
{
330+
struct timeval tv;
331+
socklen_t tv_len = sizeof(tv);
332+
if (getsockopt(socket, SOL_SOCKET, SO_SNDTIMEO, &tv, &tv_len) != 0) {
333+
return BHT_ERROR;
334+
}
335+
*timeout_us = (tv.tv_sec * 1000000UL) + tv.tv_usec;
336+
return BHT_OK;
337+
}
338+
339+
int
340+
os_socket_set_recv_timeout(bh_socket_t socket, uint64 timeout_us)
341+
{
342+
struct timeval tv;
343+
tv.tv_sec = timeout_us / 1000000UL;
344+
tv.tv_usec = timeout_us % 1000000UL;
345+
if (setsockopt(socket, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) != 0) {
346+
return BHT_ERROR;
347+
}
348+
return BHT_OK;
349+
}
350+
351+
int
352+
os_socket_get_recv_timeout(bh_socket_t socket, uint64 *timeout_us)
353+
{
354+
struct timeval tv;
355+
socklen_t tv_len = sizeof(tv);
356+
if (getsockopt(socket, SOL_SOCKET, SO_RCVTIMEO, &tv, &tv_len) != 0) {
357+
return BHT_ERROR;
358+
}
359+
*timeout_us = (tv.tv_sec * 1000000UL) + tv.tv_usec;
360+
return BHT_OK;
361+
}
362+
315363
int
316364
os_socket_addr_local(bh_socket_t socket, uint8_t *buf, size_t buflen,
317365
uint16_t *port, uint8_t *is_ipv4)

core/shared/platform/include/platform_api_extension.h

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,50 @@ int
497497
os_socket_addr_remote(bh_socket_t socket, uint8_t *buf, size_t buflen,
498498
uint16_t *port, uint8_t *is_ipv4);
499499

500+
/**
501+
* Set the send timeout until reporting an error
502+
*
503+
* @param socket the socket to set
504+
* @param time_us microseconds until timeout
505+
*
506+
* @return 0 if success, -1 otherwise
507+
*/
508+
int
509+
os_socket_set_send_timeout(bh_socket_t socket, uint64 timeout_us);
510+
511+
/**
512+
* Get the send timeout until reporting an error
513+
*
514+
* @param socket the socket to set
515+
* @param time_us the returned microseconds until timeout
516+
*
517+
* @return 0 if success, -1 otherwise
518+
*/
519+
int
520+
os_socket_get_send_timeout(bh_socket_t socket, uint64 *timeout_us);
521+
522+
/**
523+
* Set the recv timeout until reporting an error
524+
*
525+
* @param socket the socket to set
526+
* @param time_us microseconds until timeout
527+
*
528+
* @return 0 if success, -1 otherwise
529+
*/
530+
int
531+
os_socket_set_recv_timeout(bh_socket_t socket, uint64 timeout_us);
532+
533+
/**
534+
* Get the recv timeout until reporting an error
535+
*
536+
* @param socket the socket to set
537+
* @param time_us the returned microseconds until timeout
538+
*
539+
* @return 0 if success, -1 otherwise
540+
*/
541+
int
542+
os_socket_get_recv_timeout(bh_socket_t socket, uint64 *timeout_us);
543+
500544
#ifdef __cplusplus
501545
}
502546
#endif

core/shared/platform/linux-sgx/sgx_socket.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,4 +650,36 @@ os_socket_addr_remote(bh_socket_t socket, uint8_t *buf, size_t buflen,
650650
return BHT_ERROR;
651651
}
652652

653+
int
654+
os_socket_set_send_timeout(bh_socket_t socket, uint64 timeout_us)
655+
{
656+
errno = ENOSYS;
657+
658+
return BHT_ERROR
659+
}
660+
661+
int
662+
os_socket_get_send_timeout(bh_socket_t socket, uint64 *timeout_us)
663+
{
664+
errno = ENOSYS;
665+
666+
return BHT_ERROR
667+
}
668+
669+
int
670+
os_socket_set_recv_timeout(bh_socket_t socket, uint64 timeout_us)
671+
{
672+
errno = ENOSYS;
673+
674+
return BHT_ERROR
675+
}
676+
677+
int
678+
os_socket_get_recv_timeout(bh_socket_t socket, uint64 *timeout_us)
679+
{
680+
errno = ENOSYS;
681+
682+
return BHT_ERROR
683+
}
684+
653685
#endif

0 commit comments

Comments
 (0)