From 848faf9256b4b2f147b6c112a0a5c6d85b22551c Mon Sep 17 00:00:00 2001 From: Andreas Eversberg Date: Fri, 9 Feb 2024 12:38:17 +0100 Subject: [PATCH] osmo_io: Move notify_connected function to backend This relocation is necessary as the backend (osmo_io_fd or osmo_io_uring) requires a different approach in handling connect notifications. As a result, a function call has been introduced to struct iofd_backend_ops. In a subsequent patch, the process for the osmo_io_uring backend will be modified to handle SCTP connect notifications using poll/select. If connect notification is requested using poll/select, the file descriptior must be registered to osmo_fd, using osmo_fd_register. If read / write notification is requested by application, the file descriptior must be registered also. A flag is used prevent calling osmo_fd_register / osmo_fd_unregister multiple times, which would cause a crash. Change-Id: I905ec85210570aff8addadfc9603335d04eb057a Related: OS#5751 --- src/core/osmo_io.c | 3 ++- src/core/osmo_io_internal.h | 2 ++ src/core/osmo_io_poll.c | 25 ++++++++++++++++++++++++- src/core/osmo_io_uring.c | 1 + 4 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/core/osmo_io.c b/src/core/osmo_io.c index 9de9e2ef9..71249cf79 100644 --- a/src/core/osmo_io.c +++ b/src/core/osmo_io.c @@ -781,7 +781,8 @@ int osmo_iofd_set_ioops(struct osmo_io_fd *iofd, const struct osmo_io_ops *ioops 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); + OSMO_ASSERT(osmo_iofd_ops.notify_connected); + osmo_iofd_ops.notify_connected(iofd); } diff --git a/src/core/osmo_io_internal.h b/src/core/osmo_io_internal.h index e8f4ea28a..9c86e0511 100644 --- a/src/core/osmo_io_internal.h +++ b/src/core/osmo_io_internal.h @@ -31,12 +31,14 @@ struct iofd_backend_ops { void (*write_disable)(struct osmo_io_fd *iofd); void (*read_enable)(struct osmo_io_fd *iofd); void (*read_disable)(struct osmo_io_fd *iofd); + void (*notify_connected)(struct osmo_io_fd *iofd); }; #define IOFD_FLAG_CLOSED (1<<0) #define IOFD_FLAG_IN_CALLBACK (1<<1) #define IOFD_FLAG_TO_FREE (1<<2) #define IOFD_FLAG_NOTIFY_CONNECTED (1<<3) +#define IOFD_FLAG_FD_REGISTERED (1<<4) #define IOFD_FLAG_SET(iofd, flag) \ (iofd)->flags |= (flag) diff --git a/src/core/osmo_io_poll.c b/src/core/osmo_io_poll.c index d7f80c017..8398a305d 100644 --- a/src/core/osmo_io_poll.c +++ b/src/core/osmo_io_poll.c @@ -119,20 +119,32 @@ static int iofd_poll_ofd_cb_dispatch(struct osmo_fd *ofd, unsigned int what) int iofd_poll_register(struct osmo_io_fd *iofd) { struct osmo_fd *ofd = &iofd->u.poll.ofd; + int rc; + + if (IOFD_FLAG_ISSET(iofd, IOFD_FLAG_FD_REGISTERED)) + return 0; osmo_fd_setup(ofd, iofd->fd, 0, &iofd_poll_ofd_cb_dispatch, iofd, 0); - return osmo_fd_register(ofd); + rc = osmo_fd_register(ofd); + if (!rc) + IOFD_FLAG_SET(iofd, IOFD_FLAG_FD_REGISTERED); + return rc; } int iofd_poll_unregister(struct osmo_io_fd *iofd) { struct osmo_fd *ofd = &iofd->u.poll.ofd; + + if (!IOFD_FLAG_ISSET(iofd, IOFD_FLAG_FD_REGISTERED)) + return 0; osmo_fd_unregister(ofd); + IOFD_FLAG_UNSET(iofd, IOFD_FLAG_FD_REGISTERED); return 0; } int iofd_poll_close(struct osmo_io_fd *iofd) { + iofd_poll_unregister(iofd); osmo_fd_close(&iofd->u.poll.ofd); return 0; @@ -158,6 +170,16 @@ void iofd_poll_write_disable(struct osmo_io_fd *iofd) osmo_fd_write_disable(&iofd->u.poll.ofd); } +void iofd_poll_notify_connected(struct osmo_io_fd *iofd) +{ + int rc; + + rc = iofd_poll_register(iofd); + if (rc < 0) + return; + osmo_fd_write_enable(&iofd->u.poll.ofd); +} + const struct iofd_backend_ops iofd_poll_ops = { .register_fd = iofd_poll_register, .unregister_fd = iofd_poll_unregister, @@ -166,6 +188,7 @@ const struct iofd_backend_ops iofd_poll_ops = { .write_disable = iofd_poll_write_disable, .read_enable = iofd_poll_read_enable, .read_disable = iofd_poll_read_disable, + .notify_connected = iofd_poll_notify_connected, }; #endif /* defined(__linux__) */ diff --git a/src/core/osmo_io_uring.c b/src/core/osmo_io_uring.c index 1aa17d4f1..24d1e0857 100644 --- a/src/core/osmo_io_uring.c +++ b/src/core/osmo_io_uring.c @@ -402,6 +402,7 @@ const struct iofd_backend_ops iofd_uring_ops = { .write_disable = iofd_uring_write_disable, .read_enable = iofd_uring_read_enable, .read_disable = iofd_uring_read_disable, + .notify_connected = iofd_uring_write_enable, }; #endif /* defined(__linux__) */