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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <svm/message_queue.h>
17 #include <vppinfra/mem.h>
18 #include <vppinfra/format.h>
19 #include <sys/eventfd.h>
21 static inline svm_msg_q_ring_t *
22 svm_msg_q_ring_inline (svm_msg_q_t * mq, u32 ring_index)
24 return vec_elt_at_index (mq->rings, ring_index);
28 svm_msg_q_ring (svm_msg_q_t * mq, u32 ring_index)
30 return svm_msg_q_ring_inline (mq, ring_index);
34 svm_msg_q_ring_data (svm_msg_q_ring_t * ring, u32 elt_index)
36 ASSERT (elt_index < ring->nitems);
37 return (ring->data + elt_index * ring->elsize);
41 svm_msg_q_alloc (svm_msg_q_cfg_t * cfg)
43 svm_msg_q_ring_cfg_t *ring_cfg;
44 uword rings_sz = 0, mq_sz;
45 svm_msg_q_ring_t *ring;
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++)
57 if (cfg->ring_cfgs[i].data)
59 ring_cfg = &cfg->ring_cfgs[i];
60 rings_sz += (uword) ring_cfg->nitems * ring_cfg->elsize;
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);
69 mq = (svm_msg_q_t *) base;
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);
74 vh->len = cfg->n_rings;
75 mq->rings = (svm_msg_q_ring_t *) (vh + 1);
76 rings_ptr = (u8 *) mq->rings + sizeof (svm_msg_q_ring_t) * cfg->n_rings;
77 for (i = 0; i < cfg->n_rings; i++)
80 ring->elsize = cfg->ring_cfgs[i].elsize;
81 ring->nitems = cfg->ring_cfgs[i].nitems;
82 ring->cursize = ring->head = ring->tail = 0;
83 if (cfg->ring_cfgs[i].data)
84 ring->data = cfg->ring_cfgs[i].data;
87 ring->data = rings_ptr;
88 rings_ptr += (uword) ring->nitems * ring->elsize;
96 svm_msg_q_free (svm_msg_q_t * mq)
98 svm_queue_free (mq->q);
103 svm_msg_q_alloc_msg_w_ring (svm_msg_q_t * mq, u32 ring_index)
106 svm_msg_q_ring_t *ring = svm_msg_q_ring_inline (mq, ring_index);
108 ASSERT (ring->cursize < ring->nitems);
109 msg.ring_index = ring - mq->rings;
110 msg.elt_index = ring->tail;
111 ring->tail = (ring->tail + 1) % ring->nitems;
112 clib_atomic_fetch_add (&ring->cursize, 1);
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)
122 if (svm_msg_q_try_lock (mq))
124 if (PREDICT_FALSE (svm_msg_q_is_full (mq)
125 || svm_msg_q_ring_is_full (mq, ring_index)))
127 svm_msg_q_unlock (mq);
130 *msg = svm_msg_q_alloc_msg_w_ring (mq, ring_index);
135 while (svm_msg_q_is_full (mq)
136 || svm_msg_q_ring_is_full (mq, ring_index))
138 *msg = svm_msg_q_alloc_msg_w_ring (mq, ring_index);
144 svm_msg_q_alloc_msg (svm_msg_q_t * mq, u32 nbytes)
146 svm_msg_q_msg_t msg = {.as_u64 = ~0 };
147 svm_msg_q_ring_t *ring;
149 vec_foreach (ring, mq->rings)
151 if (ring->elsize < nbytes || ring->cursize == ring->nitems)
153 msg.ring_index = ring - mq->rings;
154 msg.elt_index = ring->tail;
155 ring->tail = (ring->tail + 1) % ring->nitems;
156 clib_atomic_fetch_add (&ring->cursize, 1);
163 svm_msg_q_msg_data (svm_msg_q_t * mq, svm_msg_q_msg_t * msg)
165 svm_msg_q_ring_t *ring = svm_msg_q_ring_inline (mq, msg->ring_index);
166 return svm_msg_q_ring_data (ring, msg->elt_index);
170 svm_msg_q_free_msg (svm_msg_q_t * mq, svm_msg_q_msg_t * msg)
172 svm_msg_q_ring_t *ring;
175 ASSERT (vec_len (mq->rings) > msg->ring_index);
176 ring = &mq->rings[msg->ring_index];
177 if (msg->elt_index == ring->head)
179 ring->head = (ring->head + 1) % ring->nitems;
183 clib_warning ("message out of order");
184 /* for now, expect messages to be processed in order */
188 need_signal = ring->cursize == ring->nitems;
189 clib_atomic_fetch_sub (&ring->cursize, 1);
191 if (PREDICT_FALSE (need_signal))
192 svm_queue_send_signal (mq->q, 0);
196 svm_msq_q_msg_is_valid (svm_msg_q_t * mq, svm_msg_q_msg_t * msg)
198 u32 dist1, dist2, tail, head;
199 svm_msg_q_ring_t *ring;
201 if (vec_len (mq->rings) <= msg->ring_index)
203 ring = &mq->rings[msg->ring_index];
207 dist1 = ((ring->nitems + msg->elt_index) - head) % ring->nitems;
209 dist2 = (ring->cursize == 0) ? 0 : ring->nitems;
211 dist2 = ((ring->nitems + tail) - head) % ring->nitems;
212 return (dist1 < dist2);
216 svm_msg_q_add (svm_msg_q_t * mq, svm_msg_q_msg_t * msg, int nowait)
218 ASSERT (svm_msq_q_msg_is_valid (mq, msg));
219 return svm_queue_add (mq->q, (u8 *) msg, nowait);
223 svm_msg_q_add_and_unlock (svm_msg_q_t * mq, svm_msg_q_msg_t * msg)
225 ASSERT (svm_msq_q_msg_is_valid (mq, msg));
226 svm_queue_add_raw (mq->q, (u8 *) msg);
227 svm_msg_q_unlock (mq);
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)
234 return svm_queue_sub (mq->q, (u8 *) msg, cond, time);
238 svm_msg_q_sub_w_lock (svm_msg_q_t * mq, svm_msg_q_msg_t * msg)
240 svm_queue_sub_raw (mq->q, (u8 *) msg);
244 svm_msg_q_set_consumer_eventfd (svm_msg_q_t * mq, int fd)
246 mq->q->consumer_evtfd = fd;
250 svm_msg_q_set_producer_eventfd (svm_msg_q_t * mq, int fd)
252 mq->q->producer_evtfd = fd;
256 svm_msg_q_alloc_consumer_eventfd (svm_msg_q_t * mq)
259 if ((fd = eventfd (0, EFD_NONBLOCK)) < 0)
261 svm_msg_q_set_consumer_eventfd (mq, fd);
266 svm_msg_q_alloc_producer_eventfd (svm_msg_q_t * mq)
269 if ((fd = eventfd (0, EFD_NONBLOCK)) < 0)
271 svm_msg_q_set_producer_eventfd (mq, fd);
276 format_svm_msg_q (u8 * s, va_list * args)
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++)
282 s = format (s, " [R%d:%d/%d]", i, mq->rings[i].cursize,
283 mq->rings[i].nitems);
289 * fd.io coding-style-patch-verification: ON
292 * eval: (c-set-style "gnu")