blob: 5ec8547016e42283719607d223b40324085009ee [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 * @file
17 * @brief Unidirectional shared-memory multi-ring message queue
18 */
19
20#ifndef SRC_SVM_MESSAGE_QUEUE_H_
21#define SRC_SVM_MESSAGE_QUEUE_H_
22
23#include <vppinfra/clib.h>
24#include <svm/queue.h>
25
26typedef struct svm_msg_q_ring_
27{
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 */
34} svm_msg_q_ring_t;
35
36typedef struct svm_msg_q_
37{
38 svm_queue_t *q; /**< queue for exchanging messages */
39 svm_msg_q_ring_t *rings; /**< rings with message data*/
40} svm_msg_q_t;
41
42typedef struct svm_msg_q_ring_cfg_
43{
44 u32 nitems;
45 u32 elsize;
46 void *data;
47} svm_msg_q_ring_cfg_t;
48
49typedef struct svm_msg_q_cfg_
50{
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 */
55} svm_msg_q_cfg_t;
56
57typedef union
58{
59 struct
60 {
61 u32 ring_index; /**< ring index, could be u8 */
62 u32 elt_index; /**< index in ring */
63 };
64 u64 as_u64;
65} svm_msg_q_msg_t;
66
67/**
68 * Allocate message queue
69 *
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.
73 *
74 * @param cfg configuration options: queue len, consumer pid,
75 * ring configs
76 * @return message queue
77 */
78svm_msg_q_t *svm_msg_q_alloc (svm_msg_q_cfg_t * cfg);
79
80/**
81 * Free message queue
82 *
83 * @param mq message queue to be freed
84 */
85void svm_msg_q_free (svm_msg_q_t * mq);
86
87/**
88 * Allocate message buffer
89 *
90 * Message is allocated on the first available ring capable of holding
91 * the requested number of bytes.
92 *
93 * @param mq message queue
94 * @param nbytes number of bytes needed for message
95 * @return message structure pointing to the ring and position
96 * allocated
97 */
98svm_msg_q_msg_t svm_msg_q_alloc_msg (svm_msg_q_t * mq, u32 nbytes);
99
100/**
101 * Free message buffer
102 *
103 * Marks message buffer on ring as free.
104 *
105 * @param mq message queue
106 * @param msg message to be freed
107 */
108void svm_msg_q_free_msg (svm_msg_q_t * mq, svm_msg_q_msg_t * msg);
109/**
110 * Producer enqueue one message to queue
111 *
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.
114 *
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
119 */
120int svm_msg_q_add (svm_msg_q_t * mq, svm_msg_q_msg_t msg, int nowait);
121
122/**
123 * Consumer dequeue one message from queue
124 *
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.
128 *
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
133 */
134int svm_msg_q_sub (svm_msg_q_t * mq, svm_msg_q_msg_t * msg,
135 svm_q_conditional_wait_t cond, u32 time);
136
137/**
138 * Get data for message in queu
139 *
140 * @param mq message queue
141 * @param msg message for which the data is requested
142 * @return pointer to data
143 */
144void *svm_msg_q_msg_data (svm_msg_q_t * mq, svm_msg_q_msg_t * msg);
145
146#endif /* SRC_SVM_MESSAGE_QUEUE_H_ */
147
148/*
149 * fd.io coding-style-patch-verification: ON
150 *
151 * Local Variables:
152 * eval: (c-set-style "gnu")
153 * End:
154 */