blob: 95aa84a0fa1fc95ee543ab6ee125d9165af3be5d [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) {
54 LOGP(DLIO, LOGL_ERROR, "iofd(%s): Could not get msgb for reading\n", iofd->name);
55 OSMO_ASSERT(0);
56 }
57
58 hdr.msg = msg;
59 hdr.iov[0].iov_base = msgb_data(msg);
60 hdr.iov[0].iov_len = msgb_tailroom(msg);
61 hdr.hdr.msg_iov = &hdr.iov[0];
62 hdr.hdr.msg_iovlen = 1;
63 hdr.hdr.msg_name = &hdr.osa.u.sa;
64 hdr.hdr.msg_namelen = osmo_sockaddr_size(&hdr.osa);
65
66 rc = recvmsg(ofd->fd, &hdr.hdr, flags);
67 if (rc > 0)
68 msgb_put(msg, rc);
69
70 switch (iofd->mode) {
71 case OSMO_IO_FD_MODE_READ_WRITE:
72 iofd_handle_segmented_read(iofd, msg, rc);
73 break;
74 case OSMO_IO_FD_MODE_RECVFROM_SENDTO:
75 iofd->io_ops.recvfrom_cb(iofd, rc, msg, &hdr.osa);
76 break;
77 case OSMO_IO_FD_MODE_SCTP_RECVMSG_SENDMSG:
78 /* TODO Implement */
79 OSMO_ASSERT(false);
80 break;
81 }
82 }
83
84 if (iofd->closed)
85 return;
86
87 if (what & OSMO_FD_WRITE) {
88 struct iofd_msghdr *msghdr = iofd_txqueue_dequeue(iofd);
89 if (msghdr) {
90 msg = msghdr->msg;
91
92 rc = sendmsg(ofd->fd, &msghdr->hdr, msghdr->flags);
93 if (rc > 0 && rc < msgb_length(msg)) {
94 msgb_pull(msg, rc);
95 iofd_txqueue_enqueue_front(iofd, msghdr);
96 return;
97 }
98
99 switch (iofd->mode) {
100 case OSMO_IO_FD_MODE_READ_WRITE:
101 iofd->io_ops.write_cb(iofd, rc, msg);
102 break;
103 case OSMO_IO_FD_MODE_RECVFROM_SENDTO:
104 iofd->io_ops.sendto_cb(iofd, rc, msg, &msghdr->osa);
105 break;
106 case OSMO_IO_FD_MODE_SCTP_RECVMSG_SENDMSG:
107 OSMO_ASSERT(false);
108 break;
109 }
110
111 talloc_free(msghdr);
112 msgb_free(msg);
113 }
114 }
115}
116
117static int iofd_poll_ofd_cb_dispatch(struct osmo_fd *ofd, unsigned int what)
118{
119 struct osmo_io_fd *iofd = ofd->data;
120
121 iofd->in_callback = true;
122 iofd_poll_ofd_cb_recvmsg_sendmsg(ofd, what);
123 iofd->in_callback = false;
124
125 if (iofd->to_free) {
126 talloc_free(iofd);
127 return 0;
128 }
129
130 return 0;
131}
132
133int iofd_poll_register(struct osmo_io_fd *iofd)
134{
135 struct osmo_fd *ofd = &iofd->u.poll.ofd;
136 osmo_fd_setup(ofd, iofd->fd, 0, &iofd_poll_ofd_cb_dispatch, iofd, 0);
137 return osmo_fd_register(ofd);
138}
139
140int iofd_poll_unregister(struct osmo_io_fd *iofd)
141{
142 struct osmo_fd *ofd = &iofd->u.poll.ofd;
143 osmo_fd_unregister(ofd);
144
145 return 0;
146}
147
148int iofd_poll_close(struct osmo_io_fd *iofd)
149{
150 osmo_fd_close(&iofd->u.poll.ofd);
151
152 return 0;
153}
154
155void iofd_poll_read_enable(struct osmo_io_fd *iofd)
156{
157 osmo_fd_read_enable(&iofd->u.poll.ofd);
158}
159
160void iofd_poll_read_disable(struct osmo_io_fd *iofd)
161{
162 osmo_fd_read_disable(&iofd->u.poll.ofd);
163}
164
165void iofd_poll_write_enable(struct osmo_io_fd *iofd)
166{
167 osmo_fd_write_enable(&iofd->u.poll.ofd);
168}
169
170void iofd_poll_write_disable(struct osmo_io_fd *iofd)
171{
172 osmo_fd_write_disable(&iofd->u.poll.ofd);
173}
174
175const struct iofd_backend_ops iofd_poll_ops = {
176 .register_fd = iofd_poll_register,
177 .unregister_fd = iofd_poll_unregister,
178 .close = iofd_poll_close,
179 .write_enable = iofd_poll_write_enable,
180 .write_disable = iofd_poll_write_disable,
181 .read_enable = iofd_poll_read_enable,
182 .read_disable = iofd_poll_read_disable,
183};
184
185#endif /* defined(__linux__) */