FD.io VPP  v18.07-34-g55fbdb9
Vector Packet Processing
message_queue.h
Go to the documentation of this file.
1 /*
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 
26 typedef 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 */
35 
36 typedef 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 
42 typedef struct svm_msg_q_ring_cfg_
43 {
46  void *data;
48 
49 typedef 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 */
56 
57 typedef union
58 {
59  struct
60  {
61  u32 ring_index; /**< ring index, could be u8 */
62  u32 elt_index; /**< index in ring */
63  };
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  */
79 
80 /**
81  * Free message queue
82  *
83  * @param mq message queue to be freed
84  */
85 void 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  */
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  */
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  */
120 int 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  */
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);
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  */
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  */
svm_msg_q_ring_t * rings
rings with message data
Definition: message_queue.h:39
unsigned long u64
Definition: types.h:89
svm_msg_q_msg_t svm_msg_q_alloc_msg(svm_msg_q_t *mq, u32 nbytes)
Allocate message buffer.
Definition: message_queue.c:66
unsigned char u8
Definition: types.h:56
void * svm_msg_q_msg_data(svm_msg_q_t *mq, svm_msg_q_msg_t *msg)
Get data for message in queu.
Definition: message_queue.c:98
struct svm_msg_q_ svm_msg_q_t
struct svm_msg_q_ring_ svm_msg_q_ring_t
svm_msg_q_t * svm_msg_q_alloc(svm_msg_q_cfg_t *cfg)
Allocate message queue.
Definition: message_queue.c:20
unsigned int u32
Definition: types.h:88
struct svm_msg_q_cfg_ svm_msg_q_cfg_t
volatile u32 cursize
current size of the ring
Definition: message_queue.h:28
int svm_msg_q_add(svm_msg_q_t *mq, svm_msg_q_msg_t msg, int nowait)
Producer enqueue one message to queue.
struct svm_msg_q_ring_cfg_ svm_msg_q_ring_cfg_t
u32 n_rings
number of msg rings
Definition: message_queue.h:53
svm_q_conditional_wait_t
Definition: queue.h:39
u32 head
current head (for dequeue)
Definition: message_queue.h:30
svm_queue_t * q
queue for exchanging messages
Definition: message_queue.h:38
int svm_msg_q_sub(svm_msg_q_t *mq, svm_msg_q_msg_t *msg, svm_q_conditional_wait_t cond, u32 time)
Consumer dequeue one message from queue.
u32 elt_index
index in ring
Definition: message_queue.h:62
u32 ring_index
ring index, could be u8
Definition: message_queue.h:61
u8 * data
chunk of memory for msg data
Definition: message_queue.h:33
svm_msg_q_ring_cfg_t * ring_cfgs
array of ring cfgs
Definition: message_queue.h:54
u32 q_nitems
msg queue size (not rings)
Definition: message_queue.h:52
void svm_msg_q_free_msg(svm_msg_q_t *mq, svm_msg_q_msg_t *msg)
Free message buffer.
struct _svm_queue svm_queue_t
u32 elsize
size of an element
Definition: message_queue.h:32
u32 tail
current tail (for enqueue)
Definition: message_queue.h:31
int consumer_pid
pid of msg consumer
Definition: message_queue.h:51
void svm_msg_q_free(svm_msg_q_t *mq)
Free message queue.
Definition: message_queue.c:53
u32 nitems
max size of the ring
Definition: message_queue.h:29