From e2a8dc4131da35c200252697eaf437a467696fc5 Mon Sep 17 00:00:00 2001 From: Daniel Willmann Date: Fri, 30 Jun 2023 10:51:53 +0200 Subject: [PATCH] osmo_io: Add osmo_iofd_notify_connected() Don't call write_enable() in osmo_iofd_register(). This was used to detect whether a socket is connected or not, but would always be enabled, even on unconnected sockets. Instead make this behaviour explicit by calling osmo_iofd_notify_connected(). Change-Id: Ieed10bc94c8aad821c0a8f7764db0e05c054c1e3 --- include/osmocom/core/osmo_io.h | 2 ++ src/core/libosmocore.map | 1 + src/core/osmo_io.c | 14 +++++++++++++- tests/osmo_io/osmo_io_test.c | 2 ++ tests/osmo_io/osmo_io_test.ok | 4 ---- 5 files changed, 18 insertions(+), 5 deletions(-) diff --git a/include/osmocom/core/osmo_io.h b/include/osmocom/core/osmo_io.h index aac25a348..932b90986 100644 --- a/include/osmocom/core/osmo_io.h +++ b/include/osmocom/core/osmo_io.h @@ -75,6 +75,8 @@ void osmo_iofd_txqueue_clear(struct osmo_io_fd *iofd); int osmo_iofd_close(struct osmo_io_fd *iofd); void osmo_iofd_free(struct osmo_io_fd *iofd); +void osmo_iofd_notify_connected(struct osmo_io_fd *iofd); + int osmo_iofd_write_msgb(struct osmo_io_fd *iofd, struct msgb *msg); int osmo_iofd_sendto_msgb(struct osmo_io_fd *iofd, struct msgb *msg, int sendto_flags, const struct osmo_sockaddr *dest); diff --git a/src/core/libosmocore.map b/src/core/libosmocore.map index d31392bc8..50be67c9c 100644 --- a/src/core/libosmocore.map +++ b/src/core/libosmocore.map @@ -273,6 +273,7 @@ osmo_iofd_txqueue_clear; osmo_iofd_txqueue_len; osmo_iofd_unregister; osmo_iofd_uring_init; +osmo_iofd_notify_connected; osmo_iofd_write_msgb; osmo_ip_str_type; osmo_isdnhdlc_decode; diff --git a/src/core/osmo_io.c b/src/core/osmo_io.c index 4cef14267..fdb9e32db 100644 --- a/src/core/osmo_io.c +++ b/src/core/osmo_io.c @@ -433,7 +433,9 @@ int osmo_iofd_register(struct osmo_io_fd *iofd, int fd) IOFD_FLAG_UNSET(iofd, IOFD_FLAG_CLOSED); osmo_iofd_ops.read_enable(iofd); - osmo_iofd_ops.write_enable(iofd); + + if (iofd->tx_queue.current_length > 0) + osmo_iofd_ops.write_enable(iofd); return rc; } @@ -603,4 +605,14 @@ void osmo_iofd_set_ioops(struct osmo_io_fd *iofd, const struct osmo_io_ops *ioop iofd->io_ops = *ioops; } +/*! Notify the user if/when the socket is connected + * When the socket is connected the write_cb will be called. + * \param[in] iofd the file descriptor */ +void osmo_iofd_notify_connected(struct osmo_io_fd *iofd) +{ + OSMO_ASSERT(iofd->mode == OSMO_IO_FD_MODE_READ_WRITE); + osmo_iofd_ops.write_enable(iofd); +} + + #endif /* defined(__linux__) */ diff --git a/tests/osmo_io/osmo_io_test.c b/tests/osmo_io/osmo_io_test.c index a42e6d0cb..cff594b75 100644 --- a/tests/osmo_io/osmo_io_test.c +++ b/tests/osmo_io/osmo_io_test.c @@ -86,6 +86,8 @@ static void test_connected(void) osmo_iofd_register(iofd1, fds[0]); iofd2 = osmo_iofd_setup(ctx, fds[1], "ep2", OSMO_IO_FD_MODE_READ_WRITE, &ioops_conn_read_write, NULL); osmo_iofd_register(iofd2, fds[1]); + // Explicitly check if ep1 is connected through write_cb + osmo_iofd_notify_connected(iofd1); /* Allow enough cycles to handle the messages */ for (int i = 0; i < 128; i++) diff --git a/tests/osmo_io/osmo_io_test.ok b/tests/osmo_io/osmo_io_test.ok index 82a6f3804..6527c191c 100644 --- a/tests/osmo_io/osmo_io_test.ok +++ b/tests/osmo_io/osmo_io_test.ok @@ -1,10 +1,6 @@ Running test_connected ep1: write() returned rc=0 -ep2: write() returned rc=0 ep1: write() returned rc=16 -ep2: write() returned rc=16 -ep1: read() msg with len=16 -01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 ep2: read() msg with len=16 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 Running test_unconnected