Holger Hans Peter Freyther | 8df932a | 2010-02-26 20:30:32 +0100 | [diff] [blame] | 1 | /* |
Holger Hans Peter Freyther | c7f52c4 | 2016-11-12 21:25:21 +0100 | [diff] [blame] | 2 | * (C) 2010-2016 by Holger Hans Peter Freyther |
Holger Hans Peter Freyther | 8df932a | 2010-02-26 20:30:32 +0100 | [diff] [blame] | 3 | * (C) 2010 by On-Waves |
| 4 | * |
| 5 | * All Rights Reserved |
| 6 | * |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 7 | * SPDX-License-Identifier: GPL-2.0+ |
| 8 | * |
Holger Hans Peter Freyther | 8df932a | 2010-02-26 20:30:32 +0100 | [diff] [blame] | 9 | * 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 Freyther | 8df932a | 2010-02-26 20:30:32 +0100 | [diff] [blame] | 19 | */ |
| 20 | |
Daniel Willmann | bd8a89d | 2014-06-10 10:02:24 +0200 | [diff] [blame] | 21 | #include <errno.h> |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 22 | #include <osmocom/core/write_queue.h> |
Holger Hans Peter Freyther | c7f52c4 | 2016-11-12 21:25:21 +0100 | [diff] [blame] | 23 | #include <osmocom/core/logging.h> |
Holger Hans Peter Freyther | 8df932a | 2010-02-26 20:30:32 +0100 | [diff] [blame] | 24 | |
Harald Welte | 2777ecd | 2011-08-17 14:23:42 +0200 | [diff] [blame] | 25 | /*! \addtogroup write_queue |
| 26 | * @{ |
Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 27 | * Write queue for writing \ref msgb to sockets/fds. |
| 28 | * |
| 29 | * \file write_queue.c */ |
Harald Welte | 2777ecd | 2011-08-17 14:23:42 +0200 | [diff] [blame] | 30 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 31 | /*! Select loop function for write queue handling |
Harald Welte | 2777ecd | 2011-08-17 14:23:42 +0200 | [diff] [blame] | 32 | * \param[in] fd osmocom file descriptor |
| 33 | * \param[in] what bit-mask of events that have happened |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 34 | * \returns 0 on success; negative on error |
Harald Welte | 2777ecd | 2011-08-17 14:23:42 +0200 | [diff] [blame] | 35 | * |
| 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 Ayuso | 9111d93 | 2011-05-07 12:42:51 +0200 | [diff] [blame] | 39 | int osmo_wqueue_bfd_cb(struct osmo_fd *fd, unsigned int what) |
Holger Hans Peter Freyther | 8df932a | 2010-02-26 20:30:32 +0100 | [diff] [blame] | 40 | { |
Pablo Neira Ayuso | 9111d93 | 2011-05-07 12:42:51 +0200 | [diff] [blame] | 41 | struct osmo_wqueue *queue; |
Daniel Willmann | bd8a89d | 2014-06-10 10:02:24 +0200 | [diff] [blame] | 42 | int rc; |
Holger Hans Peter Freyther | 8df932a | 2010-02-26 20:30:32 +0100 | [diff] [blame] | 43 | |
Pablo Neira Ayuso | 9111d93 | 2011-05-07 12:42:51 +0200 | [diff] [blame] | 44 | queue = container_of(fd, struct osmo_wqueue, bfd); |
Holger Hans Peter Freyther | 8df932a | 2010-02-26 20:30:32 +0100 | [diff] [blame] | 45 | |
Harald Welte | 1688699 | 2019-03-20 10:26:39 +0100 | [diff] [blame] | 46 | if (what & OSMO_FD_READ) { |
Daniel Willmann | bd8a89d | 2014-06-10 10:02:24 +0200 | [diff] [blame] | 47 | rc = queue->read_cb(fd); |
Holger Hans Peter Freyther | eb55e6a | 2014-07-01 19:39:26 +0200 | [diff] [blame] | 48 | if (rc == -EBADF) |
Daniel Willmann | bd8a89d | 2014-06-10 10:02:24 +0200 | [diff] [blame] | 49 | goto err_badfd; |
| 50 | } |
Holger Hans Peter Freyther | 8df932a | 2010-02-26 20:30:32 +0100 | [diff] [blame] | 51 | |
Harald Welte | 1688699 | 2019-03-20 10:26:39 +0100 | [diff] [blame] | 52 | if (what & OSMO_FD_EXCEPT) { |
Daniel Willmann | bd8a89d | 2014-06-10 10:02:24 +0200 | [diff] [blame] | 53 | rc = queue->except_cb(fd); |
Holger Hans Peter Freyther | eb55e6a | 2014-07-01 19:39:26 +0200 | [diff] [blame] | 54 | if (rc == -EBADF) |
Daniel Willmann | bd8a89d | 2014-06-10 10:02:24 +0200 | [diff] [blame] | 55 | goto err_badfd; |
| 56 | } |
Holger Hans Peter Freyther | 4052c81 | 2010-04-08 10:58:20 +0200 | [diff] [blame] | 57 | |
Harald Welte | 1688699 | 2019-03-20 10:26:39 +0100 | [diff] [blame] | 58 | if (what & OSMO_FD_WRITE) { |
Holger Hans Peter Freyther | 8df932a | 2010-02-26 20:30:32 +0100 | [diff] [blame] | 59 | struct msgb *msg; |
| 60 | |
Harald Welte | 1688699 | 2019-03-20 10:26:39 +0100 | [diff] [blame] | 61 | fd->when &= ~OSMO_FD_WRITE; |
Holger Hans Peter Freyther | 8df932a | 2010-02-26 20:30:32 +0100 | [diff] [blame] | 62 | |
Harald Welte | f449378 | 2020-09-27 16:39:01 +0200 | [diff] [blame] | 63 | msg = msgb_dequeue_count(&queue->msg_queue, &queue->current_length); |
Holger Hans Peter Freyther | 76681ba | 2011-02-15 00:42:19 +0100 | [diff] [blame] | 64 | /* the queue might have been emptied */ |
Harald Welte | f449378 | 2020-09-27 16:39:01 +0200 | [diff] [blame] | 65 | if (msg) { |
Daniel Willmann | bd8a89d | 2014-06-10 10:02:24 +0200 | [diff] [blame] | 66 | rc = queue->write_cb(fd, msg); |
Harald Welte | c433889 | 2020-09-27 16:46:25 +0200 | [diff] [blame] | 67 | if (rc == -EBADF) { |
| 68 | msgb_free(msg); |
Daniel Willmann | bd8a89d | 2014-06-10 10:02:24 +0200 | [diff] [blame] | 69 | goto err_badfd; |
Harald Welte | c433889 | 2020-09-27 16:46:25 +0200 | [diff] [blame] | 70 | } 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 Willmann | bd8a89d | 2014-06-10 10:02:24 +0200 | [diff] [blame] | 76 | |
Holger Hans Peter Freyther | 76681ba | 2011-02-15 00:42:19 +0100 | [diff] [blame] | 77 | if (!llist_empty(&queue->msg_queue)) |
Harald Welte | 1688699 | 2019-03-20 10:26:39 +0100 | [diff] [blame] | 78 | fd->when |= OSMO_FD_WRITE; |
Holger Hans Peter Freyther | 76681ba | 2011-02-15 00:42:19 +0100 | [diff] [blame] | 79 | } |
Holger Hans Peter Freyther | 8df932a | 2010-02-26 20:30:32 +0100 | [diff] [blame] | 80 | } |
| 81 | |
Daniel Willmann | bd8a89d | 2014-06-10 10:02:24 +0200 | [diff] [blame] | 82 | err_badfd: |
| 83 | /* Return value is not checked in osmo_select_main() */ |
Holger Hans Peter Freyther | 8df932a | 2010-02-26 20:30:32 +0100 | [diff] [blame] | 84 | return 0; |
| 85 | } |
| 86 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 87 | /*! Initialize a \ref osmo_wqueue structure |
Harald Welte | 2777ecd | 2011-08-17 14:23:42 +0200 | [diff] [blame] | 88 | * \param[in] queue Write queue to operate on |
| 89 | * \param[in] max_length Maximum length of write queue |
| 90 | */ |
Pablo Neira Ayuso | 9111d93 | 2011-05-07 12:42:51 +0200 | [diff] [blame] | 91 | void osmo_wqueue_init(struct osmo_wqueue *queue, int max_length) |
Holger Hans Peter Freyther | 8df932a | 2010-02-26 20:30:32 +0100 | [diff] [blame] | 92 | { |
| 93 | queue->max_length = max_length; |
| 94 | queue->current_length = 0; |
| 95 | queue->read_cb = NULL; |
| 96 | queue->write_cb = NULL; |
Holger Hans Peter Freyther | c7f52c4 | 2016-11-12 21:25:21 +0100 | [diff] [blame] | 97 | queue->except_cb = NULL; |
Pablo Neira Ayuso | 9111d93 | 2011-05-07 12:42:51 +0200 | [diff] [blame] | 98 | queue->bfd.cb = osmo_wqueue_bfd_cb; |
Holger Hans Peter Freyther | 8df932a | 2010-02-26 20:30:32 +0100 | [diff] [blame] | 99 | INIT_LLIST_HEAD(&queue->msg_queue); |
| 100 | } |
| 101 | |
Harald Welte | 66138cc | 2020-09-26 22:02:27 +0200 | [diff] [blame] | 102 | /*! 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 Welte | e352715 | 2021-11-25 13:40:51 +0100 | [diff] [blame] | 105 | * \returns 0 on success; negative on error (MESSAGE NOT FREED IN CASE OF ERROR). |
Harald Welte | 66138cc | 2020-09-26 22:02:27 +0200 | [diff] [blame] | 106 | */ |
| 107 | int 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 Welte | f449378 | 2020-09-27 16:39:01 +0200 | [diff] [blame] | 112 | msgb_enqueue_count(&queue->msg_queue, data, &queue->current_length); |
Harald Welte | 66138cc | 2020-09-26 22:02:27 +0200 | [diff] [blame] | 113 | queue->bfd.when |= OSMO_FD_WRITE; |
| 114 | |
| 115 | return 0; |
| 116 | } |
| 117 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 118 | /*! Enqueue a new \ref msgb into a write queue |
Harald Welte | 2777ecd | 2011-08-17 14:23:42 +0200 | [diff] [blame] | 119 | * \param[in] queue Write queue to be used |
| 120 | * \param[in] data to-be-enqueued message buffer |
Harald Welte | e352715 | 2021-11-25 13:40:51 +0100 | [diff] [blame] | 121 | * \returns 0 on success; negative on error (MESSAGE NOT FREED IN CASE OF ERROR). |
Harald Welte | 2777ecd | 2011-08-17 14:23:42 +0200 | [diff] [blame] | 122 | */ |
Pablo Neira Ayuso | 9111d93 | 2011-05-07 12:42:51 +0200 | [diff] [blame] | 123 | int osmo_wqueue_enqueue(struct osmo_wqueue *queue, struct msgb *data) |
Holger Hans Peter Freyther | 8df932a | 2010-02-26 20:30:32 +0100 | [diff] [blame] | 124 | { |
Holger Hans Peter Freyther | c7f52c4 | 2016-11-12 21:25:21 +0100 | [diff] [blame] | 125 | 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 Freyther | 8df932a | 2010-02-26 20:30:32 +0100 | [diff] [blame] | 130 | |
Harald Welte | 66138cc | 2020-09-26 22:02:27 +0200 | [diff] [blame] | 131 | return osmo_wqueue_enqueue_quiet(queue, data); |
Holger Hans Peter Freyther | 8df932a | 2010-02-26 20:30:32 +0100 | [diff] [blame] | 132 | } |
Holger Hans Peter Freyther | 99a263f | 2010-03-26 09:20:22 +0100 | [diff] [blame] | 133 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 134 | /*! Clear a \ref osmo_wqueue |
Harald Welte | 2777ecd | 2011-08-17 14:23:42 +0200 | [diff] [blame] | 135 | * \param[in] queue Write queue to be cleared |
| 136 | * |
| 137 | * This function will clear (remove/release) all messages in it. |
| 138 | */ |
Pablo Neira Ayuso | 9111d93 | 2011-05-07 12:42:51 +0200 | [diff] [blame] | 139 | void osmo_wqueue_clear(struct osmo_wqueue *queue) |
Holger Hans Peter Freyther | 99a263f | 2010-03-26 09:20:22 +0100 | [diff] [blame] | 140 | { |
| 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 Welte | 1688699 | 2019-03-20 10:26:39 +0100 | [diff] [blame] | 147 | queue->bfd.when &= ~OSMO_FD_WRITE; |
Holger Hans Peter Freyther | 99a263f | 2010-03-26 09:20:22 +0100 | [diff] [blame] | 148 | } |
Harald Welte | 2777ecd | 2011-08-17 14:23:42 +0200 | [diff] [blame] | 149 | |
Sylvain Munaut | dca7d2c | 2012-04-18 21:53:23 +0200 | [diff] [blame] | 150 | /*! @} */ |