diff options
author | Luke T. Shumaker <lukeshu@lukeshu.com> | 2024-11-19 23:18:45 -0700 |
---|---|---|
committer | Luke T. Shumaker <lukeshu@lukeshu.com> | 2024-11-23 13:53:35 -0700 |
commit | aeff3e7d735a8313762c9e5d5689708cff02676a (patch) | |
tree | 79875e7690f25c0d50c8ced406eb5e83ec905eb0 /libhw/host_net.c | |
parent | 70ba86d426accc06696402a8d3928ee5845b8f89 (diff) |
w5500 & timeout fixes
Diffstat (limited to 'libhw/host_net.c')
-rw-r--r-- | libhw/host_net.c | 38 |
1 files changed, 27 insertions, 11 deletions
diff --git a/libhw/host_net.c b/libhw/host_net.c index 330ce85..92a94df 100644 --- a/libhw/host_net.c +++ b/libhw/host_net.c @@ -75,12 +75,28 @@ static inline bool RUN_PTHREAD(void *(*fn)(void *), void *args) { return false; } -static inline ssize_t hostnet_map_negerrno(ssize_t v) { +enum hostnet_timeout_op { + OP_NONE, + OP_SEND, + OP_RECV, +}; + +static inline ssize_t hostnet_map_negerrno(ssize_t v, enum hostnet_timeout_op op) { if (v >= 0) return v; switch (v) { + case -EHOSTUNREACH: + return -NET_EARP_TIMEOUT; case -ETIMEDOUT: - return -NET_ETIMEDOUT; + switch (op) { + case OP_NONE: + assert_notreached("impossible ETIMEDOUT"); + case OP_SEND: + return -NET_EACK_TIMEOUT; + case OP_RECV: + return -NET_ERECV_TIMEOUT; + } + assert_notreached("invalid timeout op"); case -EBADF: return -NET_ECLOSED; case -EMSGSIZE: @@ -186,7 +202,7 @@ static int hostnet_tcplist_close(implements_net_stream_listener *_listener) { VCALL_SELF(struct hostnet_tcp_listener, implements_net_stream_listener, _listener); assert(listener); - return hostnet_map_negerrno(close(listener->fd) ? -errno : 0); + return hostnet_map_negerrno(close(listener->fd) ? -errno : 0, OP_NONE); } /* TCP read() *****************************************************************/ @@ -225,7 +241,7 @@ static void *hostnet_pthread_read(void *_args) { end: if (*(args->ret) < 0) - *(args->ret) = hostnet_map_negerrno(-errno); + *(args->ret) = hostnet_map_negerrno(-errno, OP_SEND); WAKE_COROUTINE(args); return NULL; } @@ -249,7 +265,7 @@ static ssize_t hostnet_tcp_read(implements_net_stream_conn *_conn, void *buf, si if (conn->read_deadline_ns) { uint64_t now_ns = VCALL(bootclock, get_time_ns); if (conn->read_deadline_ns < now_ns) - return -NET_ETIMEDOUT; + return -NET_ERECV_TIMEOUT; args.timeout = ns_to_host_us_time(conn->read_deadline_ns-now_ns); } else { args.timeout = (host_us_time_t){0}; @@ -279,7 +295,7 @@ static void *hostnet_pthread_write(void *_args) { while (done < args->count) { ssize_t r = write(args->connfd, args->buf, args->count); if (r < 0) { - hostnet_map_negerrno(-errno); + hostnet_map_negerrno(-errno, OP_RECV); break; } done += r; @@ -327,7 +343,7 @@ static int hostnet_tcp_close(implements_net_stream_conn *_conn, bool rd, bool wr how = SHUT_WR; else assert_notreached("invalid arguments to stream_conn.close()"); - return hostnet_map_negerrno(shutdown(conn->fd, how) ? -errno : 0); + return hostnet_map_negerrno(shutdown(conn->fd, how) ? -errno : 0, OP_NONE); } /* UDP init() *****************************************************************/ @@ -402,7 +418,7 @@ static void *hostnet_pthread_sendto(void *_args) { addr.in.sin_port = htons(args->port); *(args->ret) = sendto(args->connfd, args->buf, args->count, 0, &addr.gen, sizeof(addr)); if (*(args->ret) < 0) - *(args->ret) = hostnet_map_negerrno(-errno); + *(args->ret) = hostnet_map_negerrno(-errno, OP_SEND); WAKE_COROUTINE(args); return NULL; } @@ -489,7 +505,7 @@ static void *hostnet_pthread_recvfrom(void *_args) { end: if (*(args->ret_size) < 0) - *(args->ret_size) = hostnet_map_negerrno(-errno); + *(args->ret_size) = hostnet_map_negerrno(-errno, OP_RECV); WAKE_COROUTINE(args); return NULL; } @@ -516,7 +532,7 @@ static ssize_t hostnet_udp_recvfrom(implements_net_packet_conn *_conn, void *buf if (conn->read_deadline_ns) { uint64_t now_ns = VCALL(bootclock, get_time_ns); if (conn->read_deadline_ns < now_ns) - return -NET_ETIMEDOUT; + return -NET_ERECV_TIMEOUT; args.timeout = ns_to_host_us_time(conn->read_deadline_ns-now_ns); } else { args.timeout = (host_us_time_t){0}; @@ -534,5 +550,5 @@ static int hostnet_udp_close(implements_net_packet_conn *_conn) { VCALL_SELF(struct hostnet_udp_conn, implements_net_packet_conn, _conn); assert(conn); - return hostnet_map_negerrno(close(conn->fd) ? -errno : 0); + return hostnet_map_negerrno(close(conn->fd) ? -errno : 0, OP_NONE); } |