blob: 1b2d2e148ed11d25144e4fd2760a278af0a60574 [file] [log] [blame]
Florin Coras65784c12018-07-04 04:17:41 -07001/*
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 Coras3c2fed52018-07-04 04:15:05 -070019static inline svm_msg_q_ring_t *
20svm_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
25svm_msg_q_ring_t *
26svm_msg_q_ring (svm_msg_q_t * mq, u32 ring_index)
27{
28 return svm_msg_q_ring_inline (mq, ring_index);
29}
30
31static inline void *
32svm_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 Coras65784c12018-07-04 04:17:41 -070038svm_msg_q_t *
39svm_msg_q_alloc (svm_msg_q_cfg_t * cfg)
40{
Florin Corase91bdb32018-07-11 16:35:38 -070041 svm_msg_q_ring_cfg_t *ring_cfg;
Florin Coras65784c12018-07-04 04:17:41 -070042 svm_msg_q_ring_t *ring;
Florin Corase91bdb32018-07-11 16:35:38 -070043 u8 *base, *rings_ptr;
44 uword rings_sz = 0;
45 vec_header_t *vh;
Florin Coras65784c12018-07-04 04:17:41 -070046 svm_msg_q_t *mq;
Florin Corase91bdb32018-07-11 16:35:38 -070047 u32 vec_sz;
Florin Coras65784c12018-07-04 04:17:41 -070048 int i;
49
Florin Corase91bdb32018-07-11 16:35:38 -070050 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 Coras65784c12018-07-04 04:17:41 -070064 return 0;
65
Florin Corase91bdb32018-07-11 16:35:38 -070066 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 Coras65784c12018-07-04 04:17:41 -070071 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 Coras54693d22018-07-17 10:46:29 -070076 ring->cursize = ring->head = ring->tail = 0;
Florin Coras65784c12018-07-04 04:17:41 -070077 if (cfg->ring_cfgs[i].data)
78 ring->data = cfg->ring_cfgs[i].data;
79 else
Florin Coras95e0ce02018-07-05 23:44:23 -070080 {
Florin Corase91bdb32018-07-11 16:35:38 -070081 ring->data = rings_ptr;
82 rings_ptr += (uword) ring->nitems * ring->elsize;
Florin Coras95e0ce02018-07-05 23:44:23 -070083 }
Florin Coras65784c12018-07-04 04:17:41 -070084 }
Florin Corase91bdb32018-07-11 16:35:38 -070085 mq->q = svm_queue_init (cfg->q_nitems, sizeof (svm_msg_q_msg_t),
86 cfg->consumer_pid, 0);
Florin Coras65784c12018-07-04 04:17:41 -070087
88 return mq;
89}
90
91void
92svm_msg_q_free (svm_msg_q_t * mq)
93{
Florin Corase91bdb32018-07-11 16:35:38 -070094 svm_queue_free (mq->q);
Florin Coras65784c12018-07-04 04:17:41 -070095 clib_mem_free (mq);
96}
97
98svm_msg_q_msg_t
Florin Coras3c2fed52018-07-04 04:15:05 -070099svm_msg_q_alloc_msg_w_ring (svm_msg_q_t * mq, u32 ring_index)
100{
Florin Coras54693d22018-07-17 10:46:29 -0700101 svm_msg_q_msg_t msg;
Florin Coras3c2fed52018-07-04 04:15:05 -0700102 svm_msg_q_ring_t *ring = svm_msg_q_ring_inline (mq, ring_index);
103
Florin Coras54693d22018-07-17 10:46:29 -0700104 ASSERT (ring->cursize < ring->nitems);
Florin Coras3c2fed52018-07-04 04:15:05 -0700105 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
112int
113svm_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 Coras54693d22018-07-17 10:46:29 -0700135 while (svm_msg_q_ring_is_full (mq, ring_index))
136 svm_msg_q_wait (mq);
Florin Coras3c2fed52018-07-04 04:15:05 -0700137 *msg = svm_msg_q_alloc_msg_w_ring (mq, ring_index);
Florin Coras3c2fed52018-07-04 04:15:05 -0700138 }
139 return 0;
140}
141
142svm_msg_q_msg_t
Florin Coras65784c12018-07-04 04:17:41 -0700143svm_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 Coras65784c12018-07-04 04:17:41 -0700161void *
162svm_msg_q_msg_data (svm_msg_q_t * mq, svm_msg_q_msg_t * msg)
163{
Florin Coras3c2fed52018-07-04 04:15:05 -0700164 svm_msg_q_ring_t *ring = svm_msg_q_ring_inline (mq, msg->ring_index);
Florin Coras65784c12018-07-04 04:17:41 -0700165 return svm_msg_q_ring_data (ring, msg->elt_index);
166}
167
168void
169svm_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
188static int
189svm_msq_q_msg_is_valid (svm_msg_q_t * mq, svm_msg_q_msg_t * msg)
190{
Florin Coras54693d22018-07-17 10:46:29 -0700191 u32 dist1, dist2, tail, head;
Florin Coras65784c12018-07-04 04:17:41 -0700192 svm_msg_q_ring_t *ring;
Florin Coras65784c12018-07-04 04:17:41 -0700193
194 if (vec_len (mq->rings) <= msg->ring_index)
195 return 0;
196 ring = &mq->rings[msg->ring_index];
Florin Coras54693d22018-07-17 10:46:29 -0700197 tail = ring->tail;
198 head = ring->head;
Florin Coras65784c12018-07-04 04:17:41 -0700199
Florin Coras54693d22018-07-17 10:46:29 -0700200 dist1 = ((ring->nitems + msg->elt_index) - head) % ring->nitems;
201 if (tail == head)
Florin Coras65784c12018-07-04 04:17:41 -0700202 dist2 = (ring->cursize == 0) ? 0 : ring->nitems;
203 else
Florin Coras54693d22018-07-17 10:46:29 -0700204 dist2 = ((ring->nitems + tail) - head) % ring->nitems;
Florin Coras65784c12018-07-04 04:17:41 -0700205 return (dist1 < dist2);
206}
207
208int
Florin Coras3c2fed52018-07-04 04:15:05 -0700209svm_msg_q_add (svm_msg_q_t * mq, svm_msg_q_msg_t * msg, int nowait)
Florin Coras65784c12018-07-04 04:17:41 -0700210{
Florin Coras3c2fed52018-07-04 04:15:05 -0700211 ASSERT (svm_msq_q_msg_is_valid (mq, msg));
212 return svm_queue_add (mq->q, (u8 *) msg, nowait);
213}
214
215void
Florin Coras52207f12018-07-12 14:48:06 -0700216svm_msg_q_add_and_unlock (svm_msg_q_t * mq, svm_msg_q_msg_t * msg)
Florin Coras3c2fed52018-07-04 04:15:05 -0700217{
218 ASSERT (svm_msq_q_msg_is_valid (mq, msg));
219 svm_queue_add_raw (mq->q, (u8 *) msg);
Florin Coras52207f12018-07-12 14:48:06 -0700220 svm_msg_q_unlock (mq);
Florin Coras65784c12018-07-04 04:17:41 -0700221}
222
223int
224svm_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 Coras3c2fed52018-07-04 04:15:05 -0700230void
231svm_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 Coras65784c12018-07-04 04:17:41 -0700236/*
237 * fd.io coding-style-patch-verification: ON
238 *
239 * Local Variables:
240 * eval: (c-set-style "gnu")
241 * End:
242 */