Florin Coras | 65784c1 | 2018-07-04 04:17:41 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 Cisco and/or its affiliates. |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | |
| 16 | #include <svm/message_queue.h> |
| 17 | #include <vppinfra/mem.h> |
Nathan Skrzypczak | cfdb109 | 2019-12-02 16:44:42 +0100 | [diff] [blame] | 18 | #include <vppinfra/format.h> |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 19 | #include <sys/eventfd.h> |
Florin Coras | 65784c1 | 2018-07-04 04:17:41 -0700 | [diff] [blame] | 20 | |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 21 | static inline svm_msg_q_ring_t * |
| 22 | svm_msg_q_ring_inline (svm_msg_q_t * mq, u32 ring_index) |
| 23 | { |
| 24 | return vec_elt_at_index (mq->rings, ring_index); |
| 25 | } |
| 26 | |
| 27 | svm_msg_q_ring_t * |
| 28 | svm_msg_q_ring (svm_msg_q_t * mq, u32 ring_index) |
| 29 | { |
| 30 | return svm_msg_q_ring_inline (mq, ring_index); |
| 31 | } |
| 32 | |
| 33 | static inline void * |
| 34 | svm_msg_q_ring_data (svm_msg_q_ring_t * ring, u32 elt_index) |
| 35 | { |
| 36 | ASSERT (elt_index < ring->nitems); |
| 37 | return (ring->data + elt_index * ring->elsize); |
| 38 | } |
| 39 | |
Florin Coras | 65784c1 | 2018-07-04 04:17:41 -0700 | [diff] [blame] | 40 | svm_msg_q_t * |
| 41 | svm_msg_q_alloc (svm_msg_q_cfg_t * cfg) |
| 42 | { |
Florin Coras | e91bdb3 | 2018-07-11 16:35:38 -0700 | [diff] [blame] | 43 | svm_msg_q_ring_cfg_t *ring_cfg; |
Florin Coras | c470e22 | 2018-08-01 07:53:18 -0700 | [diff] [blame] | 44 | uword rings_sz = 0, mq_sz; |
Florin Coras | 65784c1 | 2018-07-04 04:17:41 -0700 | [diff] [blame] | 45 | svm_msg_q_ring_t *ring; |
Florin Coras | e91bdb3 | 2018-07-11 16:35:38 -0700 | [diff] [blame] | 46 | u8 *base, *rings_ptr; |
Florin Coras | e91bdb3 | 2018-07-11 16:35:38 -0700 | [diff] [blame] | 47 | vec_header_t *vh; |
Florin Coras | c470e22 | 2018-08-01 07:53:18 -0700 | [diff] [blame] | 48 | u32 vec_sz, q_sz; |
Florin Coras | 65784c1 | 2018-07-04 04:17:41 -0700 | [diff] [blame] | 49 | svm_msg_q_t *mq; |
| 50 | int i; |
| 51 | |
Florin Coras | e91bdb3 | 2018-07-11 16:35:38 -0700 | [diff] [blame] | 52 | ASSERT (cfg); |
| 53 | |
| 54 | vec_sz = vec_header_bytes (0) + sizeof (svm_msg_q_ring_t) * cfg->n_rings; |
| 55 | for (i = 0; i < cfg->n_rings; i++) |
| 56 | { |
| 57 | if (cfg->ring_cfgs[i].data) |
| 58 | continue; |
| 59 | ring_cfg = &cfg->ring_cfgs[i]; |
| 60 | rings_sz += (uword) ring_cfg->nitems * ring_cfg->elsize; |
| 61 | } |
| 62 | |
Florin Coras | c470e22 | 2018-08-01 07:53:18 -0700 | [diff] [blame] | 63 | q_sz = sizeof (svm_queue_t) + cfg->q_nitems * sizeof (svm_msg_q_msg_t); |
| 64 | mq_sz = sizeof (svm_msg_q_t) + vec_sz + rings_sz + q_sz; |
| 65 | base = clib_mem_alloc_aligned (mq_sz, CLIB_CACHE_LINE_BYTES); |
Florin Coras | e91bdb3 | 2018-07-11 16:35:38 -0700 | [diff] [blame] | 66 | if (!base) |
Florin Coras | 65784c1 | 2018-07-04 04:17:41 -0700 | [diff] [blame] | 67 | return 0; |
| 68 | |
Florin Coras | e91bdb3 | 2018-07-11 16:35:38 -0700 | [diff] [blame] | 69 | mq = (svm_msg_q_t *) base; |
Florin Coras | c470e22 | 2018-08-01 07:53:18 -0700 | [diff] [blame] | 70 | mq->q = svm_queue_init (base + sizeof (svm_msg_q_t), cfg->q_nitems, |
| 71 | sizeof (svm_msg_q_msg_t)); |
| 72 | mq->q->consumer_pid = cfg->consumer_pid; |
| 73 | vh = (vec_header_t *) ((u8 *) mq->q + q_sz); |
Florin Coras | e91bdb3 | 2018-07-11 16:35:38 -0700 | [diff] [blame] | 74 | vh->len = cfg->n_rings; |
| 75 | mq->rings = (svm_msg_q_ring_t *) (vh + 1); |
Lijian.Zhang | b6d61e3 | 2019-05-22 18:33:52 +0800 | [diff] [blame] | 76 | rings_ptr = (u8 *) mq->rings + sizeof (svm_msg_q_ring_t) * cfg->n_rings; |
Florin Coras | 65784c1 | 2018-07-04 04:17:41 -0700 | [diff] [blame] | 77 | for (i = 0; i < cfg->n_rings; i++) |
| 78 | { |
| 79 | ring = &mq->rings[i]; |
| 80 | ring->elsize = cfg->ring_cfgs[i].elsize; |
| 81 | ring->nitems = cfg->ring_cfgs[i].nitems; |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 82 | ring->cursize = ring->head = ring->tail = 0; |
Florin Coras | 65784c1 | 2018-07-04 04:17:41 -0700 | [diff] [blame] | 83 | if (cfg->ring_cfgs[i].data) |
| 84 | ring->data = cfg->ring_cfgs[i].data; |
| 85 | else |
Florin Coras | 95e0ce0 | 2018-07-05 23:44:23 -0700 | [diff] [blame] | 86 | { |
Florin Coras | e91bdb3 | 2018-07-11 16:35:38 -0700 | [diff] [blame] | 87 | ring->data = rings_ptr; |
| 88 | rings_ptr += (uword) ring->nitems * ring->elsize; |
Florin Coras | 95e0ce0 | 2018-07-05 23:44:23 -0700 | [diff] [blame] | 89 | } |
Florin Coras | 65784c1 | 2018-07-04 04:17:41 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | return mq; |
| 93 | } |
| 94 | |
| 95 | void |
| 96 | svm_msg_q_free (svm_msg_q_t * mq) |
| 97 | { |
Florin Coras | e91bdb3 | 2018-07-11 16:35:38 -0700 | [diff] [blame] | 98 | svm_queue_free (mq->q); |
Florin Coras | 65784c1 | 2018-07-04 04:17:41 -0700 | [diff] [blame] | 99 | clib_mem_free (mq); |
| 100 | } |
| 101 | |
| 102 | svm_msg_q_msg_t |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 103 | svm_msg_q_alloc_msg_w_ring (svm_msg_q_t * mq, u32 ring_index) |
| 104 | { |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 105 | svm_msg_q_msg_t msg; |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 106 | svm_msg_q_ring_t *ring = svm_msg_q_ring_inline (mq, ring_index); |
| 107 | |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 108 | ASSERT (ring->cursize < ring->nitems); |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 109 | msg.ring_index = ring - mq->rings; |
| 110 | msg.elt_index = ring->tail; |
| 111 | ring->tail = (ring->tail + 1) % ring->nitems; |
Sirshak Das | 2f6d7bb | 2018-10-03 22:53:51 +0000 | [diff] [blame] | 112 | clib_atomic_fetch_add (&ring->cursize, 1); |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 113 | return msg; |
| 114 | } |
| 115 | |
| 116 | int |
| 117 | svm_msg_q_lock_and_alloc_msg_w_ring (svm_msg_q_t * mq, u32 ring_index, |
| 118 | u8 noblock, svm_msg_q_msg_t * msg) |
| 119 | { |
| 120 | if (noblock) |
| 121 | { |
| 122 | if (svm_msg_q_try_lock (mq)) |
| 123 | return -1; |
Florin Coras | 2b5fed8 | 2019-07-25 14:51:09 -0700 | [diff] [blame] | 124 | if (PREDICT_FALSE (svm_msg_q_is_full (mq) |
| 125 | || svm_msg_q_ring_is_full (mq, ring_index))) |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 126 | { |
| 127 | svm_msg_q_unlock (mq); |
| 128 | return -2; |
| 129 | } |
| 130 | *msg = svm_msg_q_alloc_msg_w_ring (mq, ring_index); |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 131 | } |
| 132 | else |
| 133 | { |
| 134 | svm_msg_q_lock (mq); |
Florin Coras | 2b5fed8 | 2019-07-25 14:51:09 -0700 | [diff] [blame] | 135 | while (svm_msg_q_is_full (mq) |
| 136 | || svm_msg_q_ring_is_full (mq, ring_index)) |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 137 | svm_msg_q_wait (mq); |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 138 | *msg = svm_msg_q_alloc_msg_w_ring (mq, ring_index); |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 139 | } |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | svm_msg_q_msg_t |
Florin Coras | 65784c1 | 2018-07-04 04:17:41 -0700 | [diff] [blame] | 144 | svm_msg_q_alloc_msg (svm_msg_q_t * mq, u32 nbytes) |
| 145 | { |
| 146 | svm_msg_q_msg_t msg = {.as_u64 = ~0 }; |
| 147 | svm_msg_q_ring_t *ring; |
| 148 | |
| 149 | vec_foreach (ring, mq->rings) |
| 150 | { |
| 151 | if (ring->elsize < nbytes || ring->cursize == ring->nitems) |
| 152 | continue; |
| 153 | msg.ring_index = ring - mq->rings; |
| 154 | msg.elt_index = ring->tail; |
| 155 | ring->tail = (ring->tail + 1) % ring->nitems; |
Sirshak Das | 2f6d7bb | 2018-10-03 22:53:51 +0000 | [diff] [blame] | 156 | clib_atomic_fetch_add (&ring->cursize, 1); |
Florin Coras | 65784c1 | 2018-07-04 04:17:41 -0700 | [diff] [blame] | 157 | break; |
| 158 | } |
| 159 | return msg; |
| 160 | } |
| 161 | |
Florin Coras | 65784c1 | 2018-07-04 04:17:41 -0700 | [diff] [blame] | 162 | void * |
| 163 | svm_msg_q_msg_data (svm_msg_q_t * mq, svm_msg_q_msg_t * msg) |
| 164 | { |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 165 | svm_msg_q_ring_t *ring = svm_msg_q_ring_inline (mq, msg->ring_index); |
Florin Coras | 65784c1 | 2018-07-04 04:17:41 -0700 | [diff] [blame] | 166 | return svm_msg_q_ring_data (ring, msg->elt_index); |
| 167 | } |
| 168 | |
| 169 | void |
| 170 | svm_msg_q_free_msg (svm_msg_q_t * mq, svm_msg_q_msg_t * msg) |
| 171 | { |
| 172 | svm_msg_q_ring_t *ring; |
Florin Coras | fea813a | 2019-12-27 10:26:56 -0800 | [diff] [blame] | 173 | int need_signal; |
Florin Coras | 65784c1 | 2018-07-04 04:17:41 -0700 | [diff] [blame] | 174 | |
Florin Coras | 41c9e04 | 2018-09-11 00:10:41 -0700 | [diff] [blame] | 175 | ASSERT (vec_len (mq->rings) > msg->ring_index); |
Florin Coras | 65784c1 | 2018-07-04 04:17:41 -0700 | [diff] [blame] | 176 | ring = &mq->rings[msg->ring_index]; |
| 177 | if (msg->elt_index == ring->head) |
| 178 | { |
| 179 | ring->head = (ring->head + 1) % ring->nitems; |
| 180 | } |
| 181 | else |
| 182 | { |
Florin Coras | 41c9e04 | 2018-09-11 00:10:41 -0700 | [diff] [blame] | 183 | clib_warning ("message out of order"); |
Florin Coras | 65784c1 | 2018-07-04 04:17:41 -0700 | [diff] [blame] | 184 | /* for now, expect messages to be processed in order */ |
| 185 | ASSERT (0); |
| 186 | } |
Florin Coras | fea813a | 2019-12-27 10:26:56 -0800 | [diff] [blame] | 187 | |
| 188 | need_signal = ring->cursize == ring->nitems; |
Sirshak Das | 2f6d7bb | 2018-10-03 22:53:51 +0000 | [diff] [blame] | 189 | clib_atomic_fetch_sub (&ring->cursize, 1); |
Florin Coras | fea813a | 2019-12-27 10:26:56 -0800 | [diff] [blame] | 190 | |
| 191 | if (PREDICT_FALSE (need_signal)) |
| 192 | svm_queue_send_signal (mq->q, 0); |
Florin Coras | 65784c1 | 2018-07-04 04:17:41 -0700 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | static int |
| 196 | svm_msq_q_msg_is_valid (svm_msg_q_t * mq, svm_msg_q_msg_t * msg) |
| 197 | { |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 198 | u32 dist1, dist2, tail, head; |
Florin Coras | 65784c1 | 2018-07-04 04:17:41 -0700 | [diff] [blame] | 199 | svm_msg_q_ring_t *ring; |
Florin Coras | 65784c1 | 2018-07-04 04:17:41 -0700 | [diff] [blame] | 200 | |
| 201 | if (vec_len (mq->rings) <= msg->ring_index) |
| 202 | return 0; |
| 203 | ring = &mq->rings[msg->ring_index]; |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 204 | tail = ring->tail; |
| 205 | head = ring->head; |
Florin Coras | 65784c1 | 2018-07-04 04:17:41 -0700 | [diff] [blame] | 206 | |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 207 | dist1 = ((ring->nitems + msg->elt_index) - head) % ring->nitems; |
| 208 | if (tail == head) |
Florin Coras | 65784c1 | 2018-07-04 04:17:41 -0700 | [diff] [blame] | 209 | dist2 = (ring->cursize == 0) ? 0 : ring->nitems; |
| 210 | else |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 211 | dist2 = ((ring->nitems + tail) - head) % ring->nitems; |
Florin Coras | 65784c1 | 2018-07-04 04:17:41 -0700 | [diff] [blame] | 212 | return (dist1 < dist2); |
| 213 | } |
| 214 | |
| 215 | int |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 216 | svm_msg_q_add (svm_msg_q_t * mq, svm_msg_q_msg_t * msg, int nowait) |
Florin Coras | 65784c1 | 2018-07-04 04:17:41 -0700 | [diff] [blame] | 217 | { |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 218 | ASSERT (svm_msq_q_msg_is_valid (mq, msg)); |
| 219 | return svm_queue_add (mq->q, (u8 *) msg, nowait); |
| 220 | } |
| 221 | |
| 222 | void |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 223 | svm_msg_q_add_and_unlock (svm_msg_q_t * mq, svm_msg_q_msg_t * msg) |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 224 | { |
| 225 | ASSERT (svm_msq_q_msg_is_valid (mq, msg)); |
| 226 | svm_queue_add_raw (mq->q, (u8 *) msg); |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 227 | svm_msg_q_unlock (mq); |
Florin Coras | 65784c1 | 2018-07-04 04:17:41 -0700 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | int |
| 231 | svm_msg_q_sub (svm_msg_q_t * mq, svm_msg_q_msg_t * msg, |
| 232 | svm_q_conditional_wait_t cond, u32 time) |
| 233 | { |
| 234 | return svm_queue_sub (mq->q, (u8 *) msg, cond, time); |
| 235 | } |
| 236 | |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 237 | void |
| 238 | svm_msg_q_sub_w_lock (svm_msg_q_t * mq, svm_msg_q_msg_t * msg) |
| 239 | { |
| 240 | svm_queue_sub_raw (mq->q, (u8 *) msg); |
| 241 | } |
| 242 | |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 243 | void |
| 244 | svm_msg_q_set_consumer_eventfd (svm_msg_q_t * mq, int fd) |
| 245 | { |
| 246 | mq->q->consumer_evtfd = fd; |
| 247 | } |
| 248 | |
| 249 | void |
| 250 | svm_msg_q_set_producer_eventfd (svm_msg_q_t * mq, int fd) |
| 251 | { |
| 252 | mq->q->producer_evtfd = fd; |
| 253 | } |
| 254 | |
| 255 | int |
| 256 | svm_msg_q_alloc_consumer_eventfd (svm_msg_q_t * mq) |
| 257 | { |
| 258 | int fd; |
| 259 | if ((fd = eventfd (0, EFD_NONBLOCK)) < 0) |
| 260 | return -1; |
| 261 | svm_msg_q_set_consumer_eventfd (mq, fd); |
| 262 | return 0; |
| 263 | } |
| 264 | |
| 265 | int |
| 266 | svm_msg_q_alloc_producer_eventfd (svm_msg_q_t * mq) |
| 267 | { |
| 268 | int fd; |
| 269 | if ((fd = eventfd (0, EFD_NONBLOCK)) < 0) |
| 270 | return -1; |
| 271 | svm_msg_q_set_producer_eventfd (mq, fd); |
| 272 | return 0; |
| 273 | } |
| 274 | |
Nathan Skrzypczak | cfdb109 | 2019-12-02 16:44:42 +0100 | [diff] [blame] | 275 | u8 * |
| 276 | format_svm_msg_q (u8 * s, va_list * args) |
| 277 | { |
| 278 | svm_msg_q_t *mq = va_arg (*args, svm_msg_q_t *); |
| 279 | s = format (s, " [Q:%d/%d]", mq->q->cursize, mq->q->maxsize); |
| 280 | for (u32 i = 0; i < vec_len (mq->rings); i++) |
| 281 | { |
| 282 | s = format (s, " [R%d:%d/%d]", i, mq->rings[i].cursize, |
| 283 | mq->rings[i].nitems); |
| 284 | } |
| 285 | return s; |
| 286 | } |
| 287 | |
Florin Coras | 65784c1 | 2018-07-04 04:17:41 -0700 | [diff] [blame] | 288 | /* |
| 289 | * fd.io coding-style-patch-verification: ON |
| 290 | * |
| 291 | * Local Variables: |
| 292 | * eval: (c-set-style "gnu") |
| 293 | * End: |
| 294 | */ |