blob: 6113450e7cbee53487956ee4c5f9bc29a7539ac6 [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>
Florin Coras99368312018-08-02 10:45:44 -070018#include <sys/eventfd.h>
Florin Coras65784c12018-07-04 04:17:41 -070019
Florin Coras3c2fed52018-07-04 04:15:05 -070020static inline svm_msg_q_ring_t *
21svm_msg_q_ring_inline (svm_msg_q_t * mq, u32 ring_index)
22{
23 return vec_elt_at_index (mq->rings, ring_index);
24}
25
26svm_msg_q_ring_t *
27svm_msg_q_ring (svm_msg_q_t * mq, u32 ring_index)
28{
29 return svm_msg_q_ring_inline (mq, ring_index);
30}
31
32static inline void *
33svm_msg_q_ring_data (svm_msg_q_ring_t * ring, u32 elt_index)
34{
35 ASSERT (elt_index < ring->nitems);
36 return (ring->data + elt_index * ring->elsize);
37}
38
Florin Coras65784c12018-07-04 04:17:41 -070039svm_msg_q_t *
40svm_msg_q_alloc (svm_msg_q_cfg_t * cfg)
41{
Florin Corase91bdb32018-07-11 16:35:38 -070042 svm_msg_q_ring_cfg_t *ring_cfg;
Florin Corasc470e222018-08-01 07:53:18 -070043 uword rings_sz = 0, mq_sz;
Florin Coras65784c12018-07-04 04:17:41 -070044 svm_msg_q_ring_t *ring;
Florin Corase91bdb32018-07-11 16:35:38 -070045 u8 *base, *rings_ptr;
Florin Corase91bdb32018-07-11 16:35:38 -070046 vec_header_t *vh;
Florin Corasc470e222018-08-01 07:53:18 -070047 u32 vec_sz, q_sz;
Florin Coras65784c12018-07-04 04:17:41 -070048 svm_msg_q_t *mq;
49 int i;
50
Florin Corase91bdb32018-07-11 16:35:38 -070051 ASSERT (cfg);
52
53 vec_sz = vec_header_bytes (0) + sizeof (svm_msg_q_ring_t) * cfg->n_rings;
54 for (i = 0; i < cfg->n_rings; i++)
55 {
56 if (cfg->ring_cfgs[i].data)
57 continue;
58 ring_cfg = &cfg->ring_cfgs[i];
59 rings_sz += (uword) ring_cfg->nitems * ring_cfg->elsize;
60 }
61
Florin Corasc470e222018-08-01 07:53:18 -070062 q_sz = sizeof (svm_queue_t) + cfg->q_nitems * sizeof (svm_msg_q_msg_t);
63 mq_sz = sizeof (svm_msg_q_t) + vec_sz + rings_sz + q_sz;
64 base = clib_mem_alloc_aligned (mq_sz, CLIB_CACHE_LINE_BYTES);
Florin Corase91bdb32018-07-11 16:35:38 -070065 if (!base)
Florin Coras65784c12018-07-04 04:17:41 -070066 return 0;
67
Florin Corase91bdb32018-07-11 16:35:38 -070068 mq = (svm_msg_q_t *) base;
Florin Corasc470e222018-08-01 07:53:18 -070069 mq->q = svm_queue_init (base + sizeof (svm_msg_q_t), cfg->q_nitems,
70 sizeof (svm_msg_q_msg_t));
71 mq->q->consumer_pid = cfg->consumer_pid;
72 vh = (vec_header_t *) ((u8 *) mq->q + q_sz);
Florin Corase91bdb32018-07-11 16:35:38 -070073 vh->len = cfg->n_rings;
74 mq->rings = (svm_msg_q_ring_t *) (vh + 1);
Lijian.Zhangb6d61e32019-05-22 18:33:52 +080075 rings_ptr = (u8 *) mq->rings + sizeof (svm_msg_q_ring_t) * cfg->n_rings;
Florin Coras65784c12018-07-04 04:17:41 -070076 for (i = 0; i < cfg->n_rings; i++)
77 {
78 ring = &mq->rings[i];
79 ring->elsize = cfg->ring_cfgs[i].elsize;
80 ring->nitems = cfg->ring_cfgs[i].nitems;
Florin Coras54693d22018-07-17 10:46:29 -070081 ring->cursize = ring->head = ring->tail = 0;
Florin Coras65784c12018-07-04 04:17:41 -070082 if (cfg->ring_cfgs[i].data)
83 ring->data = cfg->ring_cfgs[i].data;
84 else
Florin Coras95e0ce02018-07-05 23:44:23 -070085 {
Florin Corase91bdb32018-07-11 16:35:38 -070086 ring->data = rings_ptr;
87 rings_ptr += (uword) ring->nitems * ring->elsize;
Florin Coras95e0ce02018-07-05 23:44:23 -070088 }
Florin Coras65784c12018-07-04 04:17:41 -070089 }
90
91 return mq;
92}
93
94void
95svm_msg_q_free (svm_msg_q_t * mq)
96{
Florin Corase91bdb32018-07-11 16:35:38 -070097 svm_queue_free (mq->q);
Florin Coras65784c12018-07-04 04:17:41 -070098 clib_mem_free (mq);
99}
100
101svm_msg_q_msg_t
Florin Coras3c2fed52018-07-04 04:15:05 -0700102svm_msg_q_alloc_msg_w_ring (svm_msg_q_t * mq, u32 ring_index)
103{
Florin Coras54693d22018-07-17 10:46:29 -0700104 svm_msg_q_msg_t msg;
Florin Coras3c2fed52018-07-04 04:15:05 -0700105 svm_msg_q_ring_t *ring = svm_msg_q_ring_inline (mq, ring_index);
106
Florin Coras54693d22018-07-17 10:46:29 -0700107 ASSERT (ring->cursize < ring->nitems);
Florin Coras3c2fed52018-07-04 04:15:05 -0700108 msg.ring_index = ring - mq->rings;
109 msg.elt_index = ring->tail;
110 ring->tail = (ring->tail + 1) % ring->nitems;
Sirshak Das2f6d7bb2018-10-03 22:53:51 +0000111 clib_atomic_fetch_add (&ring->cursize, 1);
Florin Coras3c2fed52018-07-04 04:15:05 -0700112 return msg;
113}
114
115int
116svm_msg_q_lock_and_alloc_msg_w_ring (svm_msg_q_t * mq, u32 ring_index,
117 u8 noblock, svm_msg_q_msg_t * msg)
118{
119 if (noblock)
120 {
121 if (svm_msg_q_try_lock (mq))
122 return -1;
Florin Coras2b5fed82019-07-25 14:51:09 -0700123 if (PREDICT_FALSE (svm_msg_q_is_full (mq)
124 || svm_msg_q_ring_is_full (mq, ring_index)))
Florin Coras3c2fed52018-07-04 04:15:05 -0700125 {
126 svm_msg_q_unlock (mq);
127 return -2;
128 }
129 *msg = svm_msg_q_alloc_msg_w_ring (mq, ring_index);
Florin Coras3c2fed52018-07-04 04:15:05 -0700130 }
131 else
132 {
133 svm_msg_q_lock (mq);
Florin Coras2b5fed82019-07-25 14:51:09 -0700134 while (svm_msg_q_is_full (mq)
135 || svm_msg_q_ring_is_full (mq, ring_index))
Florin Coras54693d22018-07-17 10:46:29 -0700136 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;
Sirshak Das2f6d7bb2018-10-03 22:53:51 +0000155 clib_atomic_fetch_add (&ring->cursize, 1);
Florin Coras65784c12018-07-04 04:17:41 -0700156 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
Florin Coras41c9e042018-09-11 00:10:41 -0700173 ASSERT (vec_len (mq->rings) > msg->ring_index);
Florin Coras65784c12018-07-04 04:17:41 -0700174 ring = &mq->rings[msg->ring_index];
175 if (msg->elt_index == ring->head)
176 {
177 ring->head = (ring->head + 1) % ring->nitems;
178 }
179 else
180 {
Florin Coras41c9e042018-09-11 00:10:41 -0700181 clib_warning ("message out of order");
Florin Coras65784c12018-07-04 04:17:41 -0700182 /* for now, expect messages to be processed in order */
183 ASSERT (0);
184 }
Sirshak Das2f6d7bb2018-10-03 22:53:51 +0000185 clib_atomic_fetch_sub (&ring->cursize, 1);
Florin Coras65784c12018-07-04 04:17:41 -0700186}
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 Coras99368312018-08-02 10:45:44 -0700236void
237svm_msg_q_set_consumer_eventfd (svm_msg_q_t * mq, int fd)
238{
239 mq->q->consumer_evtfd = fd;
240}
241
242void
243svm_msg_q_set_producer_eventfd (svm_msg_q_t * mq, int fd)
244{
245 mq->q->producer_evtfd = fd;
246}
247
248int
249svm_msg_q_alloc_consumer_eventfd (svm_msg_q_t * mq)
250{
251 int fd;
252 if ((fd = eventfd (0, EFD_NONBLOCK)) < 0)
253 return -1;
254 svm_msg_q_set_consumer_eventfd (mq, fd);
255 return 0;
256}
257
258int
259svm_msg_q_alloc_producer_eventfd (svm_msg_q_t * mq)
260{
261 int fd;
262 if ((fd = eventfd (0, EFD_NONBLOCK)) < 0)
263 return -1;
264 svm_msg_q_set_producer_eventfd (mq, fd);
265 return 0;
266}
267
Florin Coras65784c12018-07-04 04:17:41 -0700268/*
269 * fd.io coding-style-patch-verification: ON
270 *
271 * Local Variables:
272 * eval: (c-set-style "gnu")
273 * End:
274 */