blob: d207047f9220924fcb2aca634b6b0363d61f3cd8 [file] [log] [blame]
Harald Welte8857f3b2022-11-18 13:54:44 +01001/*! \file osmo_io_poll.c
2 * New osmocom async I/O API.
3 *
4 * (C) 2022 by Harald Welte <laforge@osmocom.org>
5 * (C) 2022-2023 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de>
6 * Author: Daniel Willmann <dwillmann@sysmocom.de>
7 *
8 * All Rights Reserved.
9 *
10 * SPDX-License-Identifier: GPL-2.0+
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 */
22
23#include "../config.h"
24#if defined(__linux__)
25
26#include <errno.h>
27#include <stdio.h>
28#include <talloc.h>
29#include <unistd.h>
30#include <stdbool.h>
31#include <sys/socket.h>
32
33#include <osmocom/core/osmo_io.h>
34#include <osmocom/core/linuxlist.h>
35#include <osmocom/core/logging.h>
36#include <osmocom/core/msgb.h>
37#include <osmocom/core/select.h>
38#include <osmocom/core/socket.h>
39#include <osmocom/core/talloc.h>
40#include <osmocom/core/utils.h>
41
42#include "osmo_io_internal.h"
43
44static void iofd_poll_ofd_cb_recvmsg_sendmsg(struct osmo_fd *ofd, unsigned int what)
45{
46 struct osmo_io_fd *iofd = ofd->data;
47 struct msgb *msg;
48 int rc, flags = 0;
49
50 if (what & OSMO_FD_READ) {
51 struct iofd_msghdr hdr;
52 msg = iofd_msgb_pending_or_alloc(iofd);
53 if (!msg) {
Daniel Willmanna8a27c22023-06-26 19:26:16 +020054 LOGPIO(iofd, LOGL_ERROR, "Could not allocate msgb for reading\n");
Harald Welte8857f3b2022-11-18 13:54:44 +010055 OSMO_ASSERT(0);
56 }
57
58 hdr.msg = msg;
arehbein5099d992023-06-16 22:37:44 +020059 hdr.iov[0].iov_base = msg->tail;
Harald Welte8857f3b2022-11-18 13:54:44 +010060 hdr.iov[0].iov_len = msgb_tailroom(msg);
Daniel Willmann435856b2023-09-05 14:11:55 +020061 hdr.hdr = (struct msghdr) {
62 .msg_iov = &hdr.iov[0],
63 .msg_iovlen = 1,
64 .msg_name = &hdr.osa.u.sa,
65 .msg_namelen = sizeof(struct osmo_sockaddr),
66 };
Harald Welte8857f3b2022-11-18 13:54:44 +010067
68 rc = recvmsg(ofd->fd, &hdr.hdr, flags);
69 if (rc > 0)
70 msgb_put(msg, rc);
71
Daniel Willmann2b34e922023-08-23 18:02:13 +020072 iofd_handle_recv(iofd, msg, rc, &hdr);
Harald Welte8857f3b2022-11-18 13:54:44 +010073 }
74
Daniel Willmannf89162f2023-06-26 19:24:46 +020075 if (IOFD_FLAG_ISSET(iofd, IOFD_FLAG_CLOSED))
Harald Welte8857f3b2022-11-18 13:54:44 +010076 return;
77
78 if (what & OSMO_FD_WRITE) {
79 struct iofd_msghdr *msghdr = iofd_txqueue_dequeue(iofd);
80 if (msghdr) {
81 msg = msghdr->msg;
82
83 rc = sendmsg(ofd->fd, &msghdr->hdr, msghdr->flags);
84 if (rc > 0 && rc < msgb_length(msg)) {
85 msgb_pull(msg, rc);
86 iofd_txqueue_enqueue_front(iofd, msghdr);
87 return;
88 }
89
90 switch (iofd->mode) {
91 case OSMO_IO_FD_MODE_READ_WRITE:
92 iofd->io_ops.write_cb(iofd, rc, msg);
93 break;
94 case OSMO_IO_FD_MODE_RECVFROM_SENDTO:
95 iofd->io_ops.sendto_cb(iofd, rc, msg, &msghdr->osa);
96 break;
97 case OSMO_IO_FD_MODE_SCTP_RECVMSG_SENDMSG:
98 OSMO_ASSERT(false);
99 break;
100 }
101
102 talloc_free(msghdr);
103 msgb_free(msg);
Daniel Willmanncbbd17e2023-05-17 17:08:10 +0200104 } else {
105 if (iofd->mode == OSMO_IO_FD_MODE_READ_WRITE)
106 /* Socket is writable, but we have no data to send. A non-blocking/async
107 connect() is signalled this way. */
108 iofd->io_ops.write_cb(iofd, 0, NULL);
109 if (osmo_iofd_txqueue_len(iofd) == 0)
110 iofd_poll_ops.write_disable(iofd);
Harald Welte8857f3b2022-11-18 13:54:44 +0100111 }
Daniel Willmanncbbd17e2023-05-17 17:08:10 +0200112
Harald Welte8857f3b2022-11-18 13:54:44 +0100113 }
114}
115
116static int iofd_poll_ofd_cb_dispatch(struct osmo_fd *ofd, unsigned int what)
117{
118 struct osmo_io_fd *iofd = ofd->data;
119
Daniel Willmannf89162f2023-06-26 19:24:46 +0200120 IOFD_FLAG_SET(iofd, IOFD_FLAG_IN_CALLBACK);
Harald Welte8857f3b2022-11-18 13:54:44 +0100121 iofd_poll_ofd_cb_recvmsg_sendmsg(ofd, what);
Daniel Willmannf89162f2023-06-26 19:24:46 +0200122 IOFD_FLAG_UNSET(iofd, IOFD_FLAG_IN_CALLBACK);
Harald Welte8857f3b2022-11-18 13:54:44 +0100123
Daniel Willmannf89162f2023-06-26 19:24:46 +0200124 if (IOFD_FLAG_ISSET(iofd, IOFD_FLAG_TO_FREE)) {
Harald Welte8857f3b2022-11-18 13:54:44 +0100125 talloc_free(iofd);
126 return 0;
127 }
128
129 return 0;
130}
131
132int iofd_poll_register(struct osmo_io_fd *iofd)
133{
134 struct osmo_fd *ofd = &iofd->u.poll.ofd;
135 osmo_fd_setup(ofd, iofd->fd, 0, &iofd_poll_ofd_cb_dispatch, iofd, 0);
136 return osmo_fd_register(ofd);
137}
138
139int iofd_poll_unregister(struct osmo_io_fd *iofd)
140{
141 struct osmo_fd *ofd = &iofd->u.poll.ofd;
142 osmo_fd_unregister(ofd);
143
144 return 0;
145}
146
147int iofd_poll_close(struct osmo_io_fd *iofd)
148{
149 osmo_fd_close(&iofd->u.poll.ofd);
150
151 return 0;
152}
153
154void iofd_poll_read_enable(struct osmo_io_fd *iofd)
155{
156 osmo_fd_read_enable(&iofd->u.poll.ofd);
157}
158
159void iofd_poll_read_disable(struct osmo_io_fd *iofd)
160{
161 osmo_fd_read_disable(&iofd->u.poll.ofd);
162}
163
164void iofd_poll_write_enable(struct osmo_io_fd *iofd)
165{
166 osmo_fd_write_enable(&iofd->u.poll.ofd);
167}
168
169void iofd_poll_write_disable(struct osmo_io_fd *iofd)
170{
171 osmo_fd_write_disable(&iofd->u.poll.ofd);
172}
173
174const struct iofd_backend_ops iofd_poll_ops = {
175 .register_fd = iofd_poll_register,
176 .unregister_fd = iofd_poll_unregister,
177 .close = iofd_poll_close,
178 .write_enable = iofd_poll_write_enable,
179 .write_disable = iofd_poll_write_disable,
180 .read_enable = iofd_poll_read_enable,
181 .read_disable = iofd_poll_read_disable,
182};
183
184#endif /* defined(__linux__) */