blob: 10266a8039cb4824427930846e09f6908feaa3e0 [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>
Nathan Skrzypczakcfdb1092019-12-02 16:44:42 +010018#include <vppinfra/format.h>
Florin Coras99368312018-08-02 10:45:44 -070019#include <sys/eventfd.h>
Florin Coras65784c12018-07-04 04:17:41 -070020
Florin Coras3c2fed52018-07-04 04:15:05 -070021static inline svm_msg_q_ring_t *
22svm_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
27svm_msg_q_ring_t *
28svm_msg_q_ring (svm_msg_q_t * mq, u32 ring_index)
29{
30 return svm_msg_q_ring_inline (mq, ring_index);
31}
32
33static inline void *
34svm_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 Coras65784c12018-07-04 04:17:41 -070040svm_msg_q_t *
41svm_msg_q_alloc (svm_msg_q_cfg_t * cfg)
42{
Florin Corase91bdb32018-07-11 16:35:38 -070043 svm_msg_q_ring_cfg_t *ring_cfg;
Florin Corasc470e222018-08-01 07:53:18 -070044 uword rings_sz = 0, mq_sz;
Florin Coras65784c12018-07-04 04:17:41 -070045 svm_msg_q_ring_t *ring;
Florin Corase91bdb32018-07-11 16:35:38 -070046 u8 *base, *rings_ptr;
Florin Corase91bdb32018-07-11 16:35:38 -070047 vec_header_t *vh;
Florin Corasc470e222018-08-01 07:53:18 -070048 u32 vec_sz, q_sz;
Florin Coras65784c12018-07-04 04:17:41 -070049 svm_msg_q_t *mq;
50 int i;
51
Florin Corase91bdb32018-07-11 16:35:38 -070052 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 Corasc470e222018-08-01 07:53:18 -070063 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 Corase91bdb32018-07-11 16:35:38 -070066 if (!base)
Florin Coras65784c12018-07-04 04:17:41 -070067 return 0;
68
Florin Corase91bdb32018-07-11 16:35:38 -070069 mq = (svm_msg_q_t *) base;
Florin Corasc470e222018-08-01 07:53:18 -070070 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 Corase91bdb32018-07-11 16:35:38 -070074 vh->len = cfg->n_rings;
75 mq->rings = (svm_msg_q_ring_t *) (vh + 1);
Lijian.Zhangb6d61e32019-05-22 18:33:52 +080076 rings_ptr = (u8 *) mq->rings + sizeof (svm_msg_q_ring_t) * cfg->n_rings;
Florin Coras65784c12018-07-04 04:17:41 -070077 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 Coras54693d22018-07-17 10:46:29 -070082 ring->cursize = ring->head = ring->tail = 0;
Florin Coras65784c12018-07-04 04:17:41 -070083 if (cfg->ring_cfgs[i].data)
84 ring->data = cfg->ring_cfgs[i].data;
85 else
Florin Coras95e0ce02018-07-05 23:44:23 -070086 {
Florin Corase91bdb32018-07-11 16:35:38 -070087 ring->data = rings_ptr;
88 rings_ptr += (uword) ring->nitems * ring->elsize;
Florin Coras95e0ce02018-07-05 23:44:23 -070089 }
Florin Coras65784c12018-07-04 04:17:41 -070090 }
91
92 return mq;
93}
94
95void
96svm_msg_q_free (svm_msg_q_t * mq)
97{
Florin Corase91bdb32018-07-11 16:35:38 -070098 svm_queue_free (mq->q);
Florin Coras65784c12018-07-04 04:17:41 -070099 clib_mem_free (mq);
100}
101
102svm_msg_q_msg_t
Florin Coras3c2fed52018-07-04 04:15:05 -0700103svm_msg_q_alloc_msg_w_ring (svm_msg_q_t * mq, u32 ring_index)
104{
Florin Coras54693d22018-07-17 10:46:29 -0700105 svm_msg_q_msg_t msg;
Florin Coras3c2fed52018-07-04 04:15:05 -0700106 svm_msg_q_ring_t *ring = svm_msg_q_ring_inline (mq, ring_index);
107
Florin Coras54693d22018-07-17 10:46:29 -0700108 ASSERT (ring->cursize < ring->nitems);
Florin Coras3c2fed52018-07-04 04:15:05 -0700109 msg.ring_index = ring - mq->rings;
110 msg.elt_index = ring->tail;
111 ring->tail = (ring->tail + 1) % ring->nitems;
Sirshak Das2f6d7bb2018-10-03 22:53:51 +0000112 clib_atomic_fetch_add (&ring->cursize, 1);
Florin Coras3c2fed52018-07-04 04:15:05 -0700113 return msg;
114}
115
116int
117svm_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 Coras2b5fed82019-07-25 14:51:09 -0700124 if (PREDICT_FALSE (svm_msg_q_is_full (mq)
125 || svm_msg_q_ring_is_full (mq, ring_index)))
Florin Coras3c2fed52018-07-04 04:15:05 -0700126 {
127 svm_msg_q_unlock (mq);
128 return -2;
129 }
130 *msg = svm_msg_q_alloc_msg_w_ring (mq, ring_index);
Florin Coras3c2fed52018-07-04 04:15:05 -0700131 }
132 else
133 {
134 svm_msg_q_lock (mq);
Florin Coras2b5fed82019-07-25 14:51:09 -0700135 while (svm_msg_q_is_full (mq)
136 || svm_msg_q_ring_is_full (mq, ring_index))
Florin Coras54693d22018-07-17 10:46:29 -0700137 svm_msg_q_wait (mq);
Florin Coras3c2fed52018-07-04 04:15:05 -0700138 *msg = svm_msg_q_alloc_msg_w_ring (mq, ring_index);
Florin Coras3c2fed52018-07-04 04:15:05 -0700139 }
140 return 0;
141}
142
143svm_msg_q_msg_t
Florin Coras65784c12018-07-04 04:17:41 -0700144svm_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 Das2f6d7bb2018-10-03 22:53:51 +0000156 clib_atomic_fetch_add (&ring->cursize, 1);
Florin Coras65784c12018-07-04 04:17:41 -0700157 break;
158 }
159 return msg;
160}
161
Florin Coras65784c12018-07-04 04:17:41 -0700162void *
163svm_msg_q_msg_data (svm_msg_q_t * mq, svm_msg_q_msg_t * msg)
164{
Florin Coras3c2fed52018-07-04 04:15:05 -0700165 svm_msg_q_ring_t *ring = svm_msg_q_ring_inline (mq, msg->ring_index);
Florin Coras65784c12018-07-04 04:17:41 -0700166 return svm_msg_q_ring_data (ring, msg->elt_index);
167}
168
169void
170svm_msg_q_free_msg (svm_msg_q_t * mq, svm_msg_q_msg_t * msg)
171{
172 svm_msg_q_ring_t *ring;
173
Florin Coras41c9e042018-09-11 00:10:41 -0700174 ASSERT (vec_len (mq->rings) > msg->ring_index);
Florin Coras65784c12018-07-04 04:17:41 -0700175 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 {
Florin Coras41c9e042018-09-11 00:10:41 -0700182 clib_warning ("message out of order");
Florin Coras65784c12018-07-04 04:17:41 -0700183 /* for now, expect messages to be processed in order */
184 ASSERT (0);
185 }
Sirshak Das2f6d7bb2018-10-03 22:53:51 +0000186 clib_atomic_fetch_sub (&ring->cursize, 1);
Florin Coras65784c12018-07-04 04:17:41 -0700187}
188
189static int
190svm_msq_q_msg_is_valid (svm_msg_q_t * mq, svm_msg_q_msg_t * msg)
191{
Florin Coras54693d22018-07-17 10:46:29 -0700192 u32 dist1, dist2, tail, head;
Florin Coras65784c12018-07-04 04:17:41 -0700193 svm_msg_q_ring_t *ring;
Florin Coras65784c12018-07-04 04:17:41 -0700194
195 if (vec_len (mq->rings) <= msg->ring_index)
196 return 0;
197 ring = &mq->rings[msg->ring_index];
Florin Coras54693d22018-07-17 10:46:29 -0700198 tail = ring->tail;
199 head = ring->head;
Florin Coras65784c12018-07-04 04:17:41 -0700200
Florin Coras54693d22018-07-17 10:46:29 -0700201 dist1 = ((ring->nitems + msg->elt_index) - head) % ring->nitems;
202 if (tail == head)
Florin Coras65784c12018-07-04 04:17:41 -0700203 dist2 = (ring->cursize == 0) ? 0 : ring->nitems;
204 else
Florin Coras54693d22018-07-17 10:46:29 -0700205 dist2 = ((ring->nitems + tail) - head) % ring->nitems;
Florin Coras65784c12018-07-04 04:17:41 -0700206 return (dist1 < dist2);
207}
208
209int
Florin Coras3c2fed52018-07-04 04:15:05 -0700210svm_msg_q_add (svm_msg_q_t * mq, svm_msg_q_msg_t * msg, int nowait)
Florin Coras65784c12018-07-04 04:17:41 -0700211{
Florin Coras3c2fed52018-07-04 04:15:05 -0700212 ASSERT (svm_msq_q_msg_is_valid (mq, msg));
213 return svm_queue_add (mq->q, (u8 *) msg, nowait);
214}
215
216void
Florin Coras52207f12018-07-12 14:48:06 -0700217svm_msg_q_add_and_unlock (svm_msg_q_t * mq, svm_msg_q_msg_t * msg)
Florin Coras3c2fed52018-07-04 04:15:05 -0700218{
219 ASSERT (svm_msq_q_msg_is_valid (mq, msg));
220 svm_queue_add_raw (mq->q, (u8 *) msg);
Florin Coras52207f12018-07-12 14:48:06 -0700221 svm_msg_q_unlock (mq);
Florin Coras65784c12018-07-04 04:17:41 -0700222}
223
224int
225svm_msg_q_sub (svm_msg_q_t * mq, svm_msg_q_msg_t * msg,
226 svm_q_conditional_wait_t cond, u32 time)
227{
228 return svm_queue_sub (mq->q, (u8 *) msg, cond, time);
229}
230
Florin Coras3c2fed52018-07-04 04:15:05 -0700231void
232svm_msg_q_sub_w_lock (svm_msg_q_t * mq, svm_msg_q_msg_t * msg)
233{
234 svm_queue_sub_raw (mq->q, (u8 *) msg);
235}
236
Florin Coras99368312018-08-02 10:45:44 -0700237void
238svm_msg_q_set_consumer_eventfd (svm_msg_q_t * mq, int fd)
239{
240 mq->q->consumer_evtfd = fd;
241}
242
243void
244svm_msg_q_set_producer_eventfd (svm_msg_q_t * mq, int fd)
245{
246 mq->q->producer_evtfd = fd;
247}
248
249int
250svm_msg_q_alloc_consumer_eventfd (svm_msg_q_t * mq)
251{
252 int fd;
253 if ((fd = eventfd (0, EFD_NONBLOCK)) < 0)
254 return -1;
255 svm_msg_q_set_consumer_eventfd (mq, fd);
256 return 0;
257}
258
259int
260svm_msg_q_alloc_producer_eventfd (svm_msg_q_t * mq)
261{
262 int fd;
263 if ((fd = eventfd (0, EFD_NONBLOCK)) < 0)
264 return -1;
265 svm_msg_q_set_producer_eventfd (mq, fd);
266 return 0;
267}
268
Nathan Skrzypczakcfdb1092019-12-02 16:44:42 +0100269u8 *
270format_svm_msg_q (u8 * s, va_list * args)
271{
272 svm_msg_q_t *mq = va_arg (*args, svm_msg_q_t *);
273 s = format (s, " [Q:%d/%d]", mq->q->cursize, mq->q->maxsize);
274 for (u32 i = 0; i < vec_len (mq->rings); i++)
275 {
276 s = format (s, " [R%d:%d/%d]", i, mq->rings[i].cursize,
277 mq->rings[i].nitems);
278 }
279 return s;
280}
281
Florin Coras65784c12018-07-04 04:17:41 -0700282/*
283 * fd.io coding-style-patch-verification: ON
284 *
285 * Local Variables:
286 * eval: (c-set-style "gnu")
287 * End:
288 */