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.
17 * @brief Unidirectional shared-memory multi-ring message queue
20 #ifndef SRC_SVM_MESSAGE_QUEUE_H_
21 #define SRC_SVM_MESSAGE_QUEUE_H_
23 #include <vppinfra/clib.h>
24 #include <svm/queue.h>
26 typedef struct svm_msg_q_ring_
28 volatile u32 cursize; /**< current size of the ring */
29 u32 nitems; /**< max size of the ring */
30 u32 head; /**< current head (for dequeue) */
31 u32 tail; /**< current tail (for enqueue) */
32 u32 elsize; /**< size of an element */
33 u8 *data; /**< chunk of memory for msg data */
36 typedef struct svm_msg_q_
38 svm_queue_t *q; /**< queue for exchanging messages */
39 svm_msg_q_ring_t *rings; /**< rings with message data*/
42 typedef struct svm_msg_q_ring_cfg_
47 } svm_msg_q_ring_cfg_t;
49 typedef struct svm_msg_q_cfg_
51 int consumer_pid; /**< pid of msg consumer */
52 u32 q_nitems; /**< msg queue size (not rings) */
53 u32 n_rings; /**< number of msg rings */
54 svm_msg_q_ring_cfg_t *ring_cfgs; /**< array of ring cfgs */
61 u32 ring_index; /**< ring index, could be u8 */
62 u32 elt_index; /**< index in ring */
68 * Allocate message queue
70 * Allocates a message queue on the heap. Based on the configuration options,
71 * apart from the message queue this also allocates (one or multiple)
72 * shared-memory rings for the messages.
74 * @param cfg configuration options: queue len, consumer pid,
76 * @return message queue
78 svm_msg_q_t *svm_msg_q_alloc (svm_msg_q_cfg_t * cfg);
83 * @param mq message queue to be freed
85 void svm_msg_q_free (svm_msg_q_t * mq);
88 * Allocate message buffer
90 * Message is allocated on the first available ring capable of holding
91 * the requested number of bytes.
93 * @param mq message queue
94 * @param nbytes number of bytes needed for message
95 * @return message structure pointing to the ring and position
98 svm_msg_q_msg_t svm_msg_q_alloc_msg (svm_msg_q_t * mq, u32 nbytes);
101 * Free message buffer
103 * Marks message buffer on ring as free.
105 * @param mq message queue
106 * @param msg message to be freed
108 void svm_msg_q_free_msg (svm_msg_q_t * mq, svm_msg_q_msg_t * msg);
110 * Producer enqueue one message to queue
112 * Prior to calling this, the producer should've obtained a message buffer
113 * from one of the rings by calling @ref svm_msg_q_alloc_msg.
115 * @param mq message queue
116 * @param msg message (pointer to ring position) to be enqueued
117 * @param nowait flag to indicate if request is blocking or not
118 * @return success status
120 int svm_msg_q_add (svm_msg_q_t * mq, svm_msg_q_msg_t msg, int nowait);
123 * Consumer dequeue one message from queue
125 * This returns the message pointing to the data in the message rings.
126 * The consumer is expected to call @ref svm_msg_q_free_msg once it
127 * finishes processing/copies the message data.
129 * @param mq message queue
130 * @param msg pointer to structure where message is to be received
131 * @param cond flag that indicates if request should block or not
132 * @return success status
134 int svm_msg_q_sub (svm_msg_q_t * mq, svm_msg_q_msg_t * msg,
135 svm_q_conditional_wait_t cond, u32 time);
138 * Get data for message in queu
140 * @param mq message queue
141 * @param msg message for which the data is requested
142 * @return pointer to data
144 void *svm_msg_q_msg_data (svm_msg_q_t * mq, svm_msg_q_msg_t * msg);
146 #endif /* SRC_SVM_MESSAGE_QUEUE_H_ */
149 * fd.io coding-style-patch-verification: ON
152 * eval: (c-set-style "gnu")