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> |
| 18 | |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 19 | static inline svm_msg_q_ring_t * |
| 20 | svm_msg_q_ring_inline (svm_msg_q_t * mq, u32 ring_index) |
| 21 | { |
| 22 | return vec_elt_at_index (mq->rings, ring_index); |
| 23 | } |
| 24 | |
| 25 | svm_msg_q_ring_t * |
| 26 | svm_msg_q_ring (svm_msg_q_t * mq, u32 ring_index) |
| 27 | { |
| 28 | return svm_msg_q_ring_inline (mq, ring_index); |
| 29 | } |
| 30 | |
| 31 | static inline void * |
| 32 | svm_msg_q_ring_data (svm_msg_q_ring_t * ring, u32 elt_index) |
| 33 | { |
| 34 | ASSERT (elt_index < ring->nitems); |
| 35 | return (ring->data + elt_index * ring->elsize); |
| 36 | } |
| 37 | |
Florin Coras | 65784c1 | 2018-07-04 04:17:41 -0700 | [diff] [blame] | 38 | svm_msg_q_t * |
| 39 | svm_msg_q_alloc (svm_msg_q_cfg_t * cfg) |
| 40 | { |
Florin Coras | e91bdb3 | 2018-07-11 16:35:38 -0700 | [diff] [blame] | 41 | svm_msg_q_ring_cfg_t *ring_cfg; |
Florin Coras | 65784c1 | 2018-07-04 04:17:41 -0700 | [diff] [blame] | 42 | svm_msg_q_ring_t *ring; |
Florin Coras | e91bdb3 | 2018-07-11 16:35:38 -0700 | [diff] [blame] | 43 | u8 *base, *rings_ptr; |
| 44 | uword rings_sz = 0; |
| 45 | vec_header_t *vh; |
Florin Coras | 65784c1 | 2018-07-04 04:17:41 -0700 | [diff] [blame] | 46 | svm_msg_q_t *mq; |
Florin Coras | e91bdb3 | 2018-07-11 16:35:38 -0700 | [diff] [blame] | 47 | u32 vec_sz; |
Florin Coras | 65784c1 | 2018-07-04 04:17:41 -0700 | [diff] [blame] | 48 | int i; |
| 49 | |
Florin Coras | e91bdb3 | 2018-07-11 16:35:38 -0700 | [diff] [blame] | 50 | ASSERT (cfg); |
| 51 | |
| 52 | vec_sz = vec_header_bytes (0) + sizeof (svm_msg_q_ring_t) * cfg->n_rings; |
| 53 | for (i = 0; i < cfg->n_rings; i++) |
| 54 | { |
| 55 | if (cfg->ring_cfgs[i].data) |
| 56 | continue; |
| 57 | ring_cfg = &cfg->ring_cfgs[i]; |
| 58 | rings_sz += (uword) ring_cfg->nitems * ring_cfg->elsize; |
| 59 | } |
| 60 | |
| 61 | base = clib_mem_alloc_aligned (sizeof (svm_msg_q_t) + vec_sz + rings_sz, |
| 62 | CLIB_CACHE_LINE_BYTES); |
| 63 | if (!base) |
Florin Coras | 65784c1 | 2018-07-04 04:17:41 -0700 | [diff] [blame] | 64 | return 0; |
| 65 | |
Florin Coras | e91bdb3 | 2018-07-11 16:35:38 -0700 | [diff] [blame] | 66 | mq = (svm_msg_q_t *) base; |
| 67 | vh = (vec_header_t *) (base + sizeof (svm_msg_q_t)); |
| 68 | vh->len = cfg->n_rings; |
| 69 | mq->rings = (svm_msg_q_ring_t *) (vh + 1); |
| 70 | rings_ptr = (u8 *) mq->rings + vec_sz; |
Florin Coras | 65784c1 | 2018-07-04 04:17:41 -0700 | [diff] [blame] | 71 | for (i = 0; i < cfg->n_rings; i++) |
| 72 | { |
| 73 | ring = &mq->rings[i]; |
| 74 | ring->elsize = cfg->ring_cfgs[i].elsize; |
| 75 | ring->nitems = cfg->ring_cfgs[i].nitems; |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 76 | ring->cursize = ring->head = ring->tail = 0; |
Florin Coras | 65784c1 | 2018-07-04 04:17:41 -0700 | [diff] [blame] | 77 | if (cfg->ring_cfgs[i].data) |
| 78 | ring->data = cfg->ring_cfgs[i].data; |
| 79 | else |
Florin Coras | 95e0ce0 | 2018-07-05 23:44:23 -0700 | [diff] [blame] | 80 | { |
Florin Coras | e91bdb3 | 2018-07-11 16:35:38 -0700 | [diff] [blame] | 81 | ring->data = rings_ptr; |
| 82 | rings_ptr += (uword) ring->nitems * ring->elsize; |
Florin Coras | 95e0ce0 | 2018-07-05 23:44:23 -0700 | [diff] [blame] | 83 | } |
Florin Coras | 65784c1 | 2018-07-04 04:17:41 -0700 | [diff] [blame] | 84 | } |
Florin Coras | e91bdb3 | 2018-07-11 16:35:38 -0700 | [diff] [blame] | 85 | mq->q = svm_queue_init (cfg->q_nitems, sizeof (svm_msg_q_msg_t), |
| 86 | cfg->consumer_pid, 0); |
Florin Coras | 65784c1 | 2018-07-04 04:17:41 -0700 | [diff] [blame] | 87 | |
| 88 | return mq; |
| 89 | } |
| 90 | |
| 91 | void |
| 92 | svm_msg_q_free (svm_msg_q_t * mq) |
| 93 | { |
Florin Coras | e91bdb3 | 2018-07-11 16:35:38 -0700 | [diff] [blame] | 94 | svm_queue_free (mq->q); |
Florin Coras | 65784c1 | 2018-07-04 04:17:41 -0700 | [diff] [blame] | 95 | clib_mem_free (mq); |
| 96 | } |
| 97 | |
| 98 | svm_msg_q_msg_t |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 99 | svm_msg_q_alloc_msg_w_ring (svm_msg_q_t * mq, u32 ring_index) |
| 100 | { |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 101 | svm_msg_q_msg_t msg; |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 102 | svm_msg_q_ring_t *ring = svm_msg_q_ring_inline (mq, ring_index); |
| 103 | |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 104 | ASSERT (ring->cursize < ring->nitems); |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 105 | msg.ring_index = ring - mq->rings; |
| 106 | msg.elt_index = ring->tail; |
| 107 | ring->tail = (ring->tail + 1) % ring->nitems; |
| 108 | __sync_fetch_and_add (&ring->cursize, 1); |
| 109 | return msg; |
| 110 | } |
| 111 | |
| 112 | int |
| 113 | svm_msg_q_lock_and_alloc_msg_w_ring (svm_msg_q_t * mq, u32 ring_index, |
| 114 | u8 noblock, svm_msg_q_msg_t * msg) |
| 115 | { |
| 116 | if (noblock) |
| 117 | { |
| 118 | if (svm_msg_q_try_lock (mq)) |
| 119 | return -1; |
| 120 | if (PREDICT_FALSE (svm_msg_q_ring_is_full (mq, ring_index))) |
| 121 | { |
| 122 | svm_msg_q_unlock (mq); |
| 123 | return -2; |
| 124 | } |
| 125 | *msg = svm_msg_q_alloc_msg_w_ring (mq, ring_index); |
| 126 | if (PREDICT_FALSE (svm_msg_q_msg_is_invalid (msg))) |
| 127 | { |
| 128 | svm_msg_q_unlock (mq); |
| 129 | return -2; |
| 130 | } |
| 131 | } |
| 132 | else |
| 133 | { |
| 134 | svm_msg_q_lock (mq); |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 135 | while (svm_msg_q_ring_is_full (mq, ring_index)) |
| 136 | svm_msg_q_wait (mq); |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 137 | *msg = svm_msg_q_alloc_msg_w_ring (mq, ring_index); |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 138 | } |
| 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | svm_msg_q_msg_t |
Florin Coras | 65784c1 | 2018-07-04 04:17:41 -0700 | [diff] [blame] | 143 | svm_msg_q_alloc_msg (svm_msg_q_t * mq, u32 nbytes) |
| 144 | { |
| 145 | svm_msg_q_msg_t msg = {.as_u64 = ~0 }; |
| 146 | svm_msg_q_ring_t *ring; |
| 147 | |
| 148 | vec_foreach (ring, mq->rings) |
| 149 | { |
| 150 | if (ring->elsize < nbytes || ring->cursize == ring->nitems) |
| 151 | continue; |
| 152 | msg.ring_index = ring - mq->rings; |
| 153 | msg.elt_index = ring->tail; |
| 154 | ring->tail = (ring->tail + 1) % ring->nitems; |
| 155 | __sync_fetch_and_add (&ring->cursize, 1); |
| 156 | break; |
| 157 | } |
| 158 | return msg; |
| 159 | } |
| 160 | |
Florin Coras | 65784c1 | 2018-07-04 04:17:41 -0700 | [diff] [blame] | 161 | void * |
| 162 | svm_msg_q_msg_data (svm_msg_q_t * mq, svm_msg_q_msg_t * msg) |
| 163 | { |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 164 | 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] | 165 | return svm_msg_q_ring_data (ring, msg->elt_index); |
| 166 | } |
| 167 | |
| 168 | void |
| 169 | svm_msg_q_free_msg (svm_msg_q_t * mq, svm_msg_q_msg_t * msg) |
| 170 | { |
| 171 | svm_msg_q_ring_t *ring; |
| 172 | |
| 173 | if (vec_len (mq->rings) <= msg->ring_index) |
| 174 | return; |
| 175 | ring = &mq->rings[msg->ring_index]; |
| 176 | if (msg->elt_index == ring->head) |
| 177 | { |
| 178 | ring->head = (ring->head + 1) % ring->nitems; |
| 179 | } |
| 180 | else |
| 181 | { |
| 182 | /* for now, expect messages to be processed in order */ |
| 183 | ASSERT (0); |
| 184 | } |
| 185 | __sync_fetch_and_sub (&ring->cursize, 1); |
| 186 | } |
| 187 | |
| 188 | static int |
| 189 | svm_msq_q_msg_is_valid (svm_msg_q_t * mq, svm_msg_q_msg_t * msg) |
| 190 | { |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 191 | u32 dist1, dist2, tail, head; |
Florin Coras | 65784c1 | 2018-07-04 04:17:41 -0700 | [diff] [blame] | 192 | svm_msg_q_ring_t *ring; |
Florin Coras | 65784c1 | 2018-07-04 04:17:41 -0700 | [diff] [blame] | 193 | |
| 194 | if (vec_len (mq->rings) <= msg->ring_index) |
| 195 | return 0; |
| 196 | ring = &mq->rings[msg->ring_index]; |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 197 | tail = ring->tail; |
| 198 | head = ring->head; |
Florin Coras | 65784c1 | 2018-07-04 04:17:41 -0700 | [diff] [blame] | 199 | |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 200 | dist1 = ((ring->nitems + msg->elt_index) - head) % ring->nitems; |
| 201 | if (tail == head) |
Florin Coras | 65784c1 | 2018-07-04 04:17:41 -0700 | [diff] [blame] | 202 | dist2 = (ring->cursize == 0) ? 0 : ring->nitems; |
| 203 | else |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 204 | dist2 = ((ring->nitems + tail) - head) % ring->nitems; |
Florin Coras | 65784c1 | 2018-07-04 04:17:41 -0700 | [diff] [blame] | 205 | return (dist1 < dist2); |
| 206 | } |
| 207 | |
| 208 | int |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 209 | 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] | 210 | { |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 211 | ASSERT (svm_msq_q_msg_is_valid (mq, msg)); |
| 212 | return svm_queue_add (mq->q, (u8 *) msg, nowait); |
| 213 | } |
| 214 | |
| 215 | void |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 216 | 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] | 217 | { |
| 218 | ASSERT (svm_msq_q_msg_is_valid (mq, msg)); |
| 219 | svm_queue_add_raw (mq->q, (u8 *) msg); |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 220 | svm_msg_q_unlock (mq); |
Florin Coras | 65784c1 | 2018-07-04 04:17:41 -0700 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | int |
| 224 | svm_msg_q_sub (svm_msg_q_t * mq, svm_msg_q_msg_t * msg, |
| 225 | svm_q_conditional_wait_t cond, u32 time) |
| 226 | { |
| 227 | return svm_queue_sub (mq->q, (u8 *) msg, cond, time); |
| 228 | } |
| 229 | |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 230 | void |
| 231 | svm_msg_q_sub_w_lock (svm_msg_q_t * mq, svm_msg_q_msg_t * msg) |
| 232 | { |
| 233 | svm_queue_sub_raw (mq->q, (u8 *) msg); |
| 234 | } |
| 235 | |
Florin Coras | 65784c1 | 2018-07-04 04:17:41 -0700 | [diff] [blame] | 236 | /* |
| 237 | * fd.io coding-style-patch-verification: ON |
| 238 | * |
| 239 | * Local Variables: |
| 240 | * eval: (c-set-style "gnu") |
| 241 | * End: |
| 242 | */ |