net: sockets: Refactor because of timeout overhaul

Use k_timeout_t internally, no change to user API.

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
This commit is contained in:
Jukka Rissanen 2020-04-03 09:44:30 +03:00
commit 9f17c33b60
6 changed files with 64 additions and 39 deletions

View file

@ -56,7 +56,8 @@ static void zsock_received_cb(struct net_context *ctx,
int status,
void *user_data);
static inline int k_fifo_wait_non_empty(struct k_fifo *fifo, int32_t timeout)
static inline int k_fifo_wait_non_empty(struct k_fifo *fifo,
k_timeout_t timeout)
{
struct k_poll_event events[] = {
K_POLL_EVENT_INITIALIZER(K_POLL_TYPE_FIFO_DATA_AVAILABLE,
@ -409,7 +410,7 @@ static inline int z_vrfy_zsock_listen(int sock, int backlog)
int zsock_accept_ctx(struct net_context *parent, struct sockaddr *addr,
socklen_t *addrlen)
{
s32_t timeout = K_FOREVER;
k_timeout_t timeout = K_FOREVER;
struct net_context *ctx;
struct net_pkt *last_pkt;
int fd;
@ -525,7 +526,7 @@ ssize_t zsock_sendto_ctx(struct net_context *ctx, const void *buf, size_t len,
int flags,
const struct sockaddr *dest_addr, socklen_t addrlen)
{
s32_t timeout = K_FOREVER;
k_timeout_t timeout = K_FOREVER;
int status;
if ((flags & ZSOCK_MSG_DONTWAIT) || sock_is_nonblock(ctx)) {
@ -588,7 +589,7 @@ ssize_t z_vrfy_zsock_sendto(int sock, const void *buf, size_t len, int flags,
ssize_t zsock_sendmsg_ctx(struct net_context *ctx, const struct msghdr *msg,
int flags)
{
s32_t timeout = K_FOREVER;
k_timeout_t timeout = K_FOREVER;
int status;
if ((flags & ZSOCK_MSG_DONTWAIT) || sock_is_nonblock(ctx)) {
@ -731,7 +732,7 @@ static inline ssize_t zsock_recv_dgram(struct net_context *ctx,
struct sockaddr *src_addr,
socklen_t *addrlen)
{
s32_t timeout = K_FOREVER;
k_timeout_t timeout = K_FOREVER;
size_t recv_len = 0;
struct net_pkt_cursor backup;
struct net_pkt *pkt;
@ -821,7 +822,7 @@ static inline ssize_t zsock_recv_stream(struct net_context *ctx,
size_t max_len,
int flags)
{
s32_t timeout = K_FOREVER;
k_timeout_t timeout = K_FOREVER;
size_t recv_len = 0;
struct net_pkt_cursor backup;
int res;
@ -1050,22 +1051,28 @@ static inline int time_left(u32_t start, u32_t timeout)
return timeout - elapsed;
}
int z_impl_zsock_poll(struct zsock_pollfd *fds, int nfds, int timeout)
int z_impl_zsock_poll(struct zsock_pollfd *fds, int nfds, int poll_timeout)
{
bool retry;
int ret = 0;
int i, remaining_time;
int i;
struct zsock_pollfd *pfd;
struct k_poll_event poll_events[CONFIG_NET_SOCKETS_POLL_MAX];
struct k_poll_event *pev;
struct k_poll_event *pev_end = poll_events + ARRAY_SIZE(poll_events);
const struct fd_op_vtable *vtable;
u32_t entry_time = k_uptime_get_32();
k_timeout_t timeout;
u64_t end;
if (timeout < 0) {
if (poll_timeout < 0) {
timeout = K_FOREVER;
poll_timeout = NET_WAIT_FOREVER;
} else {
timeout = K_MSEC(poll_timeout);
}
end = z_timeout_end_calc(timeout);
pev = poll_events;
for (pfd = fds, i = nfds; i--; pfd++) {
void *ctx;
@ -1102,17 +1109,26 @@ int z_impl_zsock_poll(struct zsock_pollfd *fds, int nfds, int timeout)
*/
return z_fdtable_call_ioctl(vtable, ctx,
ZFD_IOCTL_POLL_OFFLOAD,
fds, nfds, timeout);
fds, nfds, poll_timeout);
} else if (result != 0) {
errno = -result;
return -1;
}
}
remaining_time = timeout;
if (!K_TIMEOUT_EQ(timeout, K_NO_WAIT) &&
!K_TIMEOUT_EQ(timeout, K_FOREVER)) {
s64_t remaining = end - z_tick_get();
if (remaining <= 0) {
timeout = K_NO_WAIT;
} else {
timeout = Z_TIMEOUT_TICKS(remaining);
}
}
do {
ret = k_poll(poll_events, pev - poll_events, remaining_time);
ret = k_poll(poll_events, pev - poll_events, timeout);
/* EAGAIN when timeout expired, EINTR when cancelled (i.e. EOF) */
if (ret != 0 && ret != -EAGAIN && ret != -EINTR) {
errno = -ret;
@ -1161,15 +1177,17 @@ int z_impl_zsock_poll(struct zsock_pollfd *fds, int nfds, int timeout)
break;
}
if (timeout == K_NO_WAIT) {
if (K_TIMEOUT_EQ(timeout, K_NO_WAIT)) {
break;
}
if (timeout != K_FOREVER) {
/* Recalculate the timeout value. */
remaining_time = time_left(entry_time, timeout);
if (remaining_time <= 0) {
if (!K_TIMEOUT_EQ(timeout, K_FOREVER)) {
s64_t remaining = end - z_tick_get();
if (remaining <= 0) {
break;
} else {
timeout = Z_TIMEOUT_TICKS(remaining);
}
}
}

View file

@ -37,7 +37,8 @@ extern const struct socket_op_vtable sock_fd_op_vtable;
static const struct socket_op_vtable can_sock_fd_op_vtable;
static inline int k_fifo_wait_non_empty(struct k_fifo *fifo, int32_t timeout)
static inline int k_fifo_wait_non_empty(struct k_fifo *fifo,
k_timeout_t timeout)
{
struct k_poll_event events[] = {
K_POLL_EVENT_INITIALIZER(K_POLL_TYPE_FIFO_DATA_AVAILABLE,
@ -214,7 +215,7 @@ ssize_t zcan_sendto_ctx(struct net_context *ctx, const void *buf, size_t len,
{
struct sockaddr_can can_addr;
struct zcan_frame zframe;
s32_t timeout = K_FOREVER;
k_timeout_t timeout = K_FOREVER;
int ret;
/* Setting destination address does not probably make sense here so
@ -263,7 +264,7 @@ static ssize_t zcan_recvfrom_ctx(struct net_context *ctx, void *buf,
{
struct zcan_frame zframe;
size_t recv_len = 0;
s32_t timeout = K_FOREVER;
k_timeout_t timeout = K_FOREVER;
struct net_pkt *pkt;
if ((flags & ZSOCK_MSG_DONTWAIT) || sock_is_nonblock(ctx)) {

View file

@ -34,10 +34,10 @@ struct net_mgmt_socket {
u32_t mask;
/* Message allocation timeout */
s32_t alloc_timeout;
k_timeout_t alloc_timeout;
/* net_mgmt event timeout */
s32_t wait_timeout;
k_timeout_t wait_timeout;
/* Socket protocol */
int proto;
@ -151,7 +151,7 @@ static ssize_t znet_mgmt_recvfrom(struct net_mgmt_socket *mgmt, void *buf,
socklen_t *addrlen)
{
struct sockaddr_nm *nm_addr = (struct sockaddr_nm *)src_addr;
s32_t timeout = mgmt->wait_timeout;
k_timeout_t timeout = mgmt->wait_timeout;
u32_t raised_event = 0;
u8_t *copy_to = buf;
struct net_mgmt_msghdr hdr;
@ -190,7 +190,7 @@ again:
}
if ((mgmt->mask & raised_event) != raised_event) {
if (timeout == K_FOREVER) {
if (K_TIMEOUT_EQ(timeout, K_FOREVER)) {
goto again;
}

View file

@ -28,7 +28,8 @@ extern const struct socket_op_vtable sock_fd_op_vtable;
static const struct socket_op_vtable packet_sock_fd_op_vtable;
static inline int k_fifo_wait_non_empty(struct k_fifo *fifo, int32_t timeout)
static inline int k_fifo_wait_non_empty(struct k_fifo *fifo,
k_timeout_t timeout)
{
struct k_poll_event events[] = {
K_POLL_EVENT_INITIALIZER(K_POLL_TYPE_FIFO_DATA_AVAILABLE,
@ -146,7 +147,7 @@ ssize_t zpacket_sendto_ctx(struct net_context *ctx, const void *buf, size_t len,
int flags, const struct sockaddr *dest_addr,
socklen_t addrlen)
{
s32_t timeout = K_FOREVER;
k_timeout_t timeout = K_FOREVER;
int status;
if (!dest_addr) {
@ -184,7 +185,7 @@ ssize_t zpacket_recvfrom_ctx(struct net_context *ctx, void *buf, size_t max_len,
socklen_t *addrlen)
{
size_t recv_len = 0;
s32_t timeout = K_FOREVER;
k_timeout_t timeout = K_FOREVER;
struct net_pkt *pkt;
if ((flags & ZSOCK_MSG_DONTWAIT) || sock_is_nonblock(ctx)) {

View file

@ -475,13 +475,15 @@ static int dtls_tx(void *ctx, const unsigned char *buf, size_t len)
return sent;
}
static int dtls_rx(void *ctx, unsigned char *buf, size_t len, uint32_t timeout)
static int dtls_rx(void *ctx, unsigned char *buf, size_t len,
uint32_t dtls_timeout)
{
struct net_context *net_ctx = ctx;
bool is_block = !((net_ctx->tls->flags & ZSOCK_MSG_DONTWAIT) ||
sock_is_nonblock(net_ctx));
int remaining_time = (timeout == 0U) ? K_FOREVER : timeout;
u32_t entry_time = k_uptime_get_32();
k_timeout_t timeout = (dtls_timeout == 0U) ? K_FOREVER :
K_MSEC(dtls_timeout);
u64_t end = z_timeout_end_calc(timeout);
socklen_t addrlen = sizeof(struct sockaddr);
struct sockaddr addr;
int err;
@ -501,7 +503,7 @@ static int dtls_rx(void *ctx, unsigned char *buf, size_t len, uint32_t timeout)
pev.mode = K_POLL_MODE_NOTIFY_ONLY;
pev.state = K_POLL_STATE_NOT_READY;
if (k_poll(&pev, 1, remaining_time) == -EAGAIN) {
if (k_poll(&pev, 1, timeout) == -EAGAIN) {
return MBEDTLS_ERR_SSL_TIMEOUT;
}
}
@ -539,12 +541,15 @@ static int dtls_rx(void *ctx, unsigned char *buf, size_t len, uint32_t timeout)
/* Received data from different peer, ignore it. */
retry = true;
if (remaining_time != K_FOREVER) {
if (!K_TIMEOUT_EQ(timeout, K_FOREVER)) {
/* Recalculate the timeout value. */
remaining_time = time_left(entry_time, timeout);
if (remaining_time <= 0) {
s64_t remaining = end - z_tick_get();
if (remaining <= 0) {
return MBEDTLS_ERR_SSL_TIMEOUT;
}
timeout = Z_TIMEOUT_TICKS(remaining);
}
}
} while (retry);

View file

@ -124,8 +124,8 @@ void test_poll(void)
zassert_equal(res, 0, "");
tstamp = k_uptime_get_32();
res = poll(pollout, ARRAY_SIZE(pollout), K_MSEC(200));
zassert_true(k_uptime_get_32() - tstamp < K_MSEC(100), "");
res = poll(pollout, ARRAY_SIZE(pollout), 200);
zassert_true(k_uptime_get_32() - tstamp < 100, "");
zassert_equal(res, 1, "");
zassert_equal(pollout[0].revents, POLLOUT, "");
@ -146,8 +146,8 @@ void test_poll(void)
zassert_equal(res, 0, "");
tstamp = k_uptime_get_32();
res = poll(pollout, ARRAY_SIZE(pollout), K_MSEC(200));
zassert_true(k_uptime_get_32() - tstamp < K_MSEC(100), "");
res = poll(pollout, ARRAY_SIZE(pollout), 200);
zassert_true(k_uptime_get_32() - tstamp < 100, "");
zassert_equal(res, 1, "");
zassert_equal(pollout[0].revents, POLLOUT, "");