blob: 884cebddd8665ba373670510b7e883cb3f2b4b02 [file] [log] [blame]
Holger Hans Peter Freyther8df932a2010-02-26 20:30:32 +01001/*
Holger Hans Peter Freytherc7f52c42016-11-12 21:25:21 +01002 * (C) 2010-2016 by Holger Hans Peter Freyther
Holger Hans Peter Freyther8df932a2010-02-26 20:30:32 +01003 * (C) 2010 by On-Waves
4 *
5 * All Rights Reserved
6 *
Harald Weltee08da972017-11-13 01:00:26 +09007 * SPDX-License-Identifier: GPL-2.0+
8 *
Holger Hans Peter Freyther8df932a2010-02-26 20:30:32 +01009 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
Holger Hans Peter Freyther8df932a2010-02-26 20:30:32 +010019 */
20
Daniel Willmannbd8a89d2014-06-10 10:02:24 +020021#include <errno.h>
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010022#include <osmocom/core/write_queue.h>
Holger Hans Peter Freytherc7f52c42016-11-12 21:25:21 +010023#include <osmocom/core/logging.h>
Holger Hans Peter Freyther8df932a2010-02-26 20:30:32 +010024
Harald Welte2777ecd2011-08-17 14:23:42 +020025/*! \addtogroup write_queue
26 * @{
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020027 * Write queue for writing \ref msgb to sockets/fds.
28 *
29 * \file write_queue.c */
Harald Welte2777ecd2011-08-17 14:23:42 +020030
Neels Hofmeyr87e45502017-06-20 00:17:59 +020031/*! Select loop function for write queue handling
Harald Welte2777ecd2011-08-17 14:23:42 +020032 * \param[in] fd osmocom file descriptor
33 * \param[in] what bit-mask of events that have happened
Harald Welte2d2e2cc2016-04-25 12:11:20 +020034 * \returns 0 on success; negative on error
Harald Welte2777ecd2011-08-17 14:23:42 +020035 *
36 * This function is provided so that it can be registered with the
37 * select loop abstraction code (\ref osmo_fd::cb).
38 */
Pablo Neira Ayuso9111d932011-05-07 12:42:51 +020039int osmo_wqueue_bfd_cb(struct osmo_fd *fd, unsigned int what)
Holger Hans Peter Freyther8df932a2010-02-26 20:30:32 +010040{
Pablo Neira Ayuso9111d932011-05-07 12:42:51 +020041 struct osmo_wqueue *queue;
Daniel Willmannbd8a89d2014-06-10 10:02:24 +020042 int rc;
Holger Hans Peter Freyther8df932a2010-02-26 20:30:32 +010043
Pablo Neira Ayuso9111d932011-05-07 12:42:51 +020044 queue = container_of(fd, struct osmo_wqueue, bfd);
Holger Hans Peter Freyther8df932a2010-02-26 20:30:32 +010045
Harald Welte16886992019-03-20 10:26:39 +010046 if (what & OSMO_FD_READ) {
Daniel Willmannbd8a89d2014-06-10 10:02:24 +020047 rc = queue->read_cb(fd);
Holger Hans Peter Freythereb55e6a2014-07-01 19:39:26 +020048 if (rc == -EBADF)
Daniel Willmannbd8a89d2014-06-10 10:02:24 +020049 goto err_badfd;
50 }
Holger Hans Peter Freyther8df932a2010-02-26 20:30:32 +010051
Harald Welte16886992019-03-20 10:26:39 +010052 if (what & OSMO_FD_EXCEPT) {
Daniel Willmannbd8a89d2014-06-10 10:02:24 +020053 rc = queue->except_cb(fd);
Holger Hans Peter Freythereb55e6a2014-07-01 19:39:26 +020054 if (rc == -EBADF)
Daniel Willmannbd8a89d2014-06-10 10:02:24 +020055 goto err_badfd;
56 }
Holger Hans Peter Freyther4052c812010-04-08 10:58:20 +020057
Harald Welte16886992019-03-20 10:26:39 +010058 if (what & OSMO_FD_WRITE) {
Holger Hans Peter Freyther8df932a2010-02-26 20:30:32 +010059 struct msgb *msg;
60
Harald Welte16886992019-03-20 10:26:39 +010061 fd->when &= ~OSMO_FD_WRITE;
Holger Hans Peter Freyther8df932a2010-02-26 20:30:32 +010062
Harald Weltef4493782020-09-27 16:39:01 +020063 msg = msgb_dequeue_count(&queue->msg_queue, &queue->current_length);
Holger Hans Peter Freyther76681ba2011-02-15 00:42:19 +010064 /* the queue might have been emptied */
Harald Weltef4493782020-09-27 16:39:01 +020065 if (msg) {
Daniel Willmannbd8a89d2014-06-10 10:02:24 +020066 rc = queue->write_cb(fd, msg);
Harald Weltec4338892020-09-27 16:46:25 +020067 if (rc == -EBADF) {
68 msgb_free(msg);
Daniel Willmannbd8a89d2014-06-10 10:02:24 +020069 goto err_badfd;
Harald Weltec4338892020-09-27 16:46:25 +020070 } else if (rc == -EAGAIN) {
71 /* re-enqueue the msgb to the head of the queue */
72 llist_add(&msg->list, &queue->msg_queue);
73 queue->current_length++;
74 } else
75 msgb_free(msg);
Daniel Willmannbd8a89d2014-06-10 10:02:24 +020076
Holger Hans Peter Freyther76681ba2011-02-15 00:42:19 +010077 if (!llist_empty(&queue->msg_queue))
Harald Welte16886992019-03-20 10:26:39 +010078 fd->when |= OSMO_FD_WRITE;
Holger Hans Peter Freyther76681ba2011-02-15 00:42:19 +010079 }
Holger Hans Peter Freyther8df932a2010-02-26 20:30:32 +010080 }
81
Daniel Willmannbd8a89d2014-06-10 10:02:24 +020082err_badfd:
83 /* Return value is not checked in osmo_select_main() */
Holger Hans Peter Freyther8df932a2010-02-26 20:30:32 +010084 return 0;
85}
86
Neels Hofmeyr87e45502017-06-20 00:17:59 +020087/*! Initialize a \ref osmo_wqueue structure
Harald Welte2777ecd2011-08-17 14:23:42 +020088 * \param[in] queue Write queue to operate on
89 * \param[in] max_length Maximum length of write queue
90 */
Pablo Neira Ayuso9111d932011-05-07 12:42:51 +020091void osmo_wqueue_init(struct osmo_wqueue *queue, int max_length)
Holger Hans Peter Freyther8df932a2010-02-26 20:30:32 +010092{
93 queue->max_length = max_length;
94 queue->current_length = 0;
95 queue->read_cb = NULL;
96 queue->write_cb = NULL;
Holger Hans Peter Freytherc7f52c42016-11-12 21:25:21 +010097 queue->except_cb = NULL;
Pablo Neira Ayuso9111d932011-05-07 12:42:51 +020098 queue->bfd.cb = osmo_wqueue_bfd_cb;
Holger Hans Peter Freyther8df932a2010-02-26 20:30:32 +010099 INIT_LLIST_HEAD(&queue->msg_queue);
100}
101
Harald Welte66138cc2020-09-26 22:02:27 +0200102/*! Enqueue a new \ref msgb into a write queue (without logging full queue events)
103 * \param[in] queue Write queue to be used
104 * \param[in] data to-be-enqueued message buffer
Harald Weltee3527152021-11-25 13:40:51 +0100105 * \returns 0 on success; negative on error (MESSAGE NOT FREED IN CASE OF ERROR).
Harald Welte66138cc2020-09-26 22:02:27 +0200106 */
107int osmo_wqueue_enqueue_quiet(struct osmo_wqueue *queue, struct msgb *data)
108{
109 if (queue->current_length >= queue->max_length)
110 return -ENOSPC;
111
Harald Weltef4493782020-09-27 16:39:01 +0200112 msgb_enqueue_count(&queue->msg_queue, data, &queue->current_length);
Harald Welte66138cc2020-09-26 22:02:27 +0200113 queue->bfd.when |= OSMO_FD_WRITE;
114
115 return 0;
116}
117
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200118/*! Enqueue a new \ref msgb into a write queue
Harald Welte2777ecd2011-08-17 14:23:42 +0200119 * \param[in] queue Write queue to be used
120 * \param[in] data to-be-enqueued message buffer
Harald Weltee3527152021-11-25 13:40:51 +0100121 * \returns 0 on success; negative on error (MESSAGE NOT FREED IN CASE OF ERROR).
Harald Welte2777ecd2011-08-17 14:23:42 +0200122 */
Pablo Neira Ayuso9111d932011-05-07 12:42:51 +0200123int osmo_wqueue_enqueue(struct osmo_wqueue *queue, struct msgb *data)
Holger Hans Peter Freyther8df932a2010-02-26 20:30:32 +0100124{
Holger Hans Peter Freytherc7f52c42016-11-12 21:25:21 +0100125 if (queue->current_length >= queue->max_length) {
126 LOGP(DLGLOBAL, LOGL_ERROR,
127 "wqueue(%p) is full. Rejecting msgb\n", queue);
128 return -ENOSPC;
129 }
Holger Hans Peter Freyther8df932a2010-02-26 20:30:32 +0100130
Harald Welte66138cc2020-09-26 22:02:27 +0200131 return osmo_wqueue_enqueue_quiet(queue, data);
Holger Hans Peter Freyther8df932a2010-02-26 20:30:32 +0100132}
Holger Hans Peter Freyther99a263f2010-03-26 09:20:22 +0100133
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200134/*! Clear a \ref osmo_wqueue
Harald Welte2777ecd2011-08-17 14:23:42 +0200135 * \param[in] queue Write queue to be cleared
136 *
137 * This function will clear (remove/release) all messages in it.
138 */
Pablo Neira Ayuso9111d932011-05-07 12:42:51 +0200139void osmo_wqueue_clear(struct osmo_wqueue *queue)
Holger Hans Peter Freyther99a263f2010-03-26 09:20:22 +0100140{
141 while (!llist_empty(&queue->msg_queue)) {
142 struct msgb *msg = msgb_dequeue(&queue->msg_queue);
143 msgb_free(msg);
144 }
145
146 queue->current_length = 0;
Harald Welte16886992019-03-20 10:26:39 +0100147 queue->bfd.when &= ~OSMO_FD_WRITE;
Holger Hans Peter Freyther99a263f2010-03-26 09:20:22 +0100148}
Harald Welte2777ecd2011-08-17 14:23:42 +0200149
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200150/*! @} */