FD.io VPP  v21.06-3-gbb25fbf28
Vector Packet Processing
message_queue.h File Reference

Unidirectional shared-memory multi-ring message queue. More...

+ Include dependency graph for message_queue.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  svm_msg_q_shr_queue_
 
struct  svm_msg_q_queue_
 
struct  svm_msg_q_ring_shared_
 
struct  svm_msg_q_ring_
 
struct  svm_msg_q_shared_
 
struct  svm_msg_q_
 
struct  svm_msg_q_ring_cfg_
 
struct  svm_msg_q_cfg_
 
union  svm_msg_q_msg_t
 

Macros

#define SVM_MQ_INVALID_MSG   { .as_u64 = ~0 }
 

Typedefs

typedef struct svm_msg_q_shr_queue_ svm_msg_q_shared_queue_t
 
typedef struct svm_msg_q_queue_ svm_msg_q_queue_t
 
typedef struct svm_msg_q_ring_shared_ svm_msg_q_ring_shared_t
 
typedef struct svm_msg_q_ring_ svm_msg_q_ring_t
 
typedef struct svm_msg_q_shared_ svm_msg_q_shared_t
 
typedef struct svm_msg_q_ svm_msg_q_t
 
typedef struct svm_msg_q_ring_cfg_ svm_msg_q_ring_cfg_t
 
typedef struct svm_msg_q_cfg_ svm_msg_q_cfg_t
 
typedef enum svm_msg_q_wait_type_ svm_msg_q_wait_type_t
 

Enumerations

enum  svm_msg_q_wait_type_ { SVM_MQ_WAIT_EMPTY, SVM_MQ_WAIT_FULL }
 

Functions

svm_msg_q_shared_tsvm_msg_q_alloc (svm_msg_q_cfg_t *cfg)
 Allocate message queue. More...
 
svm_msg_q_shared_tsvm_msg_q_init (void *base, svm_msg_q_cfg_t *cfg)
 
uword svm_msg_q_size_to_alloc (svm_msg_q_cfg_t *cfg)
 
void svm_msg_q_attach (svm_msg_q_t *mq, void *smq_base)
 
void svm_msg_q_cleanup (svm_msg_q_t *mq)
 Cleanup mq's private data. More...
 
void svm_msg_q_free (svm_msg_q_t *mq)
 Free message queue. More...
 
svm_msg_q_msg_t svm_msg_q_alloc_msg (svm_msg_q_t *mq, u32 nbytes)
 Allocate message buffer. More...
 
svm_msg_q_msg_t svm_msg_q_alloc_msg_w_ring (svm_msg_q_t *mq, u32 ring_index)
 Allocate message buffer on ring. More...
 
int svm_msg_q_lock_and_alloc_msg_w_ring (svm_msg_q_t *mq, u32 ring_index, u8 noblock, svm_msg_q_msg_t *msg)
 Lock message queue and allocate message buffer on ring. More...
 
void svm_msg_q_free_msg (svm_msg_q_t *mq, svm_msg_q_msg_t *msg)
 Free message buffer. More...
 
int svm_msg_q_add (svm_msg_q_t *mq, svm_msg_q_msg_t *msg, int nowait)
 Producer enqueue one message to queue. More...
 
void svm_msg_q_add_and_unlock (svm_msg_q_t *mq, svm_msg_q_msg_t *msg)
 Producer enqueue one message to queue with mutex held. More...
 
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. More...
 
int svm_msg_q_sub_raw (svm_msg_q_t *mq, svm_msg_q_msg_t *elem)
 Consumer dequeue one message from queue. More...
 
int svm_msg_q_sub_raw_batch (svm_msg_q_t *mq, svm_msg_q_msg_t *msg_buf, u32 n_msgs)
 Consumer dequeue multiple messages from queue. More...
 
void * svm_msg_q_msg_data (svm_msg_q_t *mq, svm_msg_q_msg_t *msg)
 Get data for message in queue. More...
 
svm_msg_q_ring_tsvm_msg_q_ring (svm_msg_q_t *mq, u32 ring_index)
 Get message queue ring. More...
 
void svm_msg_q_set_eventfd (svm_msg_q_t *mq, int fd)
 Set event fd for queue. More...
 
int svm_msg_q_alloc_eventfd (svm_msg_q_t *mq)
 Allocate event fd for queue. More...
 
u8format_svm_msg_q (u8 *s, va_list *args)
 Format message queue, shows msg count for each ring. More...
 
static u32 svm_msg_q_size (svm_msg_q_t *mq)
 Check length of message queue. More...
 
static u8 svm_msg_q_is_full (svm_msg_q_t *mq)
 Check if message queue is full. More...
 
static u8 svm_msg_q_ring_is_full (svm_msg_q_t *mq, u32 ring_index)
 
static u8 svm_msg_q_is_empty (svm_msg_q_t *mq)
 Check if message queue is empty. More...
 
static u8 svm_msg_q_msg_is_invalid (svm_msg_q_msg_t *msg)
 Check if message is invalid. More...
 
static int svm_msg_q_try_lock (svm_msg_q_t *mq)
 Try locking message queue. More...
 
static int svm_msg_q_lock (svm_msg_q_t *mq)
 Lock, or block trying, the message queue. More...
 
static void svm_msg_q_unlock (svm_msg_q_t *mq)
 Unlock message queue. More...
 
int svm_msg_q_wait (svm_msg_q_t *mq, svm_msg_q_wait_type_t type)
 Wait for message queue event. More...
 
int svm_msg_q_wait_prod (svm_msg_q_t *mq)
 Wait for message queue event as producer. More...
 
int svm_msg_q_timedwait (svm_msg_q_t *mq, double timeout)
 Timed wait for message queue event. More...
 
static int svm_msg_q_get_eventfd (svm_msg_q_t *mq)
 

Detailed Description

Unidirectional shared-memory multi-ring message queue.

Definition in file message_queue.h.

Macro Definition Documentation

◆ SVM_MQ_INVALID_MSG

#define SVM_MQ_INVALID_MSG   { .as_u64 = ~0 }

Definition at line 103 of file message_queue.h.

Typedef Documentation

◆ svm_msg_q_cfg_t

◆ svm_msg_q_queue_t

◆ svm_msg_q_ring_cfg_t

◆ svm_msg_q_ring_shared_t

◆ svm_msg_q_ring_t

◆ svm_msg_q_shared_queue_t

◆ svm_msg_q_shared_t

◆ svm_msg_q_t

typedef struct svm_msg_q_ svm_msg_q_t

◆ svm_msg_q_wait_type_t

Enumeration Type Documentation

◆ svm_msg_q_wait_type_

Enumerator
SVM_MQ_WAIT_EMPTY 
SVM_MQ_WAIT_FULL 

Definition at line 105 of file message_queue.h.

Function Documentation

◆ format_svm_msg_q()

u8* format_svm_msg_q ( u8 s,
va_list *  args 
)

Format message queue, shows msg count for each ring.

Definition at line 628 of file message_queue.c.

+ Here is the caller graph for this function:

◆ svm_msg_q_add()

int svm_msg_q_add ( svm_msg_q_t mq,
svm_msg_q_msg_t msg,
int  nowait 
)

Producer enqueue one message to queue.

Prior to calling this, the producer should've obtained a message buffer from one of the rings by calling svm_msg_q_alloc_msg.

Parameters
mqmessage queue
msgmessage (pointer to ring position) to be enqueued
nowaitflag to indicate if request is blocking or not
Returns
success status

Definition at line 363 of file message_queue.c.

+ Here is the call graph for this function:

◆ svm_msg_q_add_and_unlock()

void svm_msg_q_add_and_unlock ( svm_msg_q_t mq,
svm_msg_q_msg_t msg 
)

Producer enqueue one message to queue with mutex held.

Prior to calling this, the producer should've obtained a message buffer from one of the rings by calling svm_msg_q_alloc_msg. It assumes the queue mutex is held.

Parameters
mqmessage queue
msgmessage (pointer to ring position) to be enqueued
Returns
success status

Definition at line 394 of file message_queue.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ svm_msg_q_alloc()

svm_msg_q_shared_t* svm_msg_q_alloc ( svm_msg_q_cfg_t cfg)

Allocate message queue.

Allocates a message queue on the heap. Based on the configuration options, apart from the message queue this also allocates (one or multiple) shared-memory rings for the messages.

Parameters
cfgconfiguration options: queue len, consumer pid, ring configs
Returns
message queue

Definition at line 130 of file message_queue.c.

+ Here is the call graph for this function:

◆ svm_msg_q_alloc_eventfd()

int svm_msg_q_alloc_eventfd ( svm_msg_q_t mq)

Allocate event fd for queue.

Definition at line 491 of file message_queue.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ svm_msg_q_alloc_msg()

svm_msg_q_msg_t svm_msg_q_alloc_msg ( svm_msg_q_t mq,
u32  nbytes 
)

Allocate message buffer.

Message is allocated on the first available ring capable of holding the requested number of bytes.

Parameters
mqmessage queue
nbytesnumber of bytes needed for message
Returns
message structure pointing to the ring and position allocated

Definition at line 266 of file message_queue.c.

◆ svm_msg_q_alloc_msg_w_ring()

svm_msg_q_msg_t svm_msg_q_alloc_msg_w_ring ( svm_msg_q_t mq,
u32  ring_index 
)

Allocate message buffer on ring.

Message is allocated, on requested ring. The caller MUST check that the ring is not full.

Parameters
mqmessage queue
ring_indexring on which the allocation should occur
Returns
message structure pointing to the ring and position allocated

Definition at line 221 of file message_queue.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ svm_msg_q_attach()

void svm_msg_q_attach ( svm_msg_q_t mq,
void *  smq_base 
)

Definition at line 144 of file message_queue.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ svm_msg_q_cleanup()

void svm_msg_q_cleanup ( svm_msg_q_t mq)

Cleanup mq's private data.

Definition at line 170 of file message_queue.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ svm_msg_q_free()

void svm_msg_q_free ( svm_msg_q_t mq)

Free message queue.

Parameters
mqmessage queue to be freed

Definition at line 179 of file message_queue.c.

+ Here is the call graph for this function:

◆ svm_msg_q_free_msg()

void svm_msg_q_free_msg ( svm_msg_q_t mq,
svm_msg_q_msg_t msg 
)

Free message buffer.

Marks message buffer on ring as free.

Parameters
mqmessage queue
msgmessage to be freed

Definition at line 294 of file message_queue.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ svm_msg_q_get_eventfd()

static int svm_msg_q_get_eventfd ( svm_msg_q_t mq)
inlinestatic

Definition at line 431 of file message_queue.h.

+ Here is the caller graph for this function:

◆ svm_msg_q_init()

svm_msg_q_shared_t* svm_msg_q_init ( void *  base,
svm_msg_q_cfg_t cfg 
)

Definition at line 72 of file message_queue.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ svm_msg_q_is_empty()

static u8 svm_msg_q_is_empty ( svm_msg_q_t mq)
inlinestatic

Check if message queue is empty.

Definition at line 335 of file message_queue.h.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ svm_msg_q_is_full()

static u8 svm_msg_q_is_full ( svm_msg_q_t mq)
inlinestatic

Check if message queue is full.

Definition at line 319 of file message_queue.h.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ svm_msg_q_lock()

static int svm_msg_q_lock ( svm_msg_q_t mq)
inlinestatic

Lock, or block trying, the message queue.

Definition at line 372 of file message_queue.h.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ svm_msg_q_lock_and_alloc_msg_w_ring()

int svm_msg_q_lock_and_alloc_msg_w_ring ( svm_msg_q_t mq,
u32  ring_index,
u8  noblock,
svm_msg_q_msg_t msg 
)

Lock message queue and allocate message buffer on ring.

This should be used when multiple writers/readers are expected to compete for the rings/queue. Message should be enqueued by calling svm_msg_q_add_w_lock and the caller MUST unlock the queue once the message in enqueued.

Parameters
mqmessage queue
ring_indexring on which the allocation should occur
noblockflag that indicates if request should block
msgpointer to message to be filled in
Returns
0 on success, negative number otherwise

Definition at line 239 of file message_queue.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ svm_msg_q_msg_data()

void* svm_msg_q_msg_data ( svm_msg_q_t mq,
svm_msg_q_msg_t msg 
)

Get data for message in queue.

Parameters
mqmessage queue
msgmessage for which the data is requested
Returns
pointer to data

Definition at line 287 of file message_queue.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ svm_msg_q_msg_is_invalid()

static u8 svm_msg_q_msg_is_invalid ( svm_msg_q_msg_t msg)
inlinestatic

Check if message is invalid.

Definition at line 344 of file message_queue.h.

◆ svm_msg_q_ring()

svm_msg_q_ring_t* svm_msg_q_ring ( svm_msg_q_t mq,
u32  ring_index 
)

Get message queue ring.

Parameters
mqmessage queue
ring_indexindex of ring
Returns
pointer to ring

Definition at line 30 of file message_queue.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ svm_msg_q_ring_is_full()

static u8 svm_msg_q_ring_is_full ( svm_msg_q_t mq,
u32  ring_index 
)
inlinestatic

Definition at line 325 of file message_queue.h.

+ Here is the caller graph for this function:

◆ svm_msg_q_set_eventfd()

void svm_msg_q_set_eventfd ( svm_msg_q_t mq,
int  fd 
)

Set event fd for queue.

If set, queue will exclusively use eventfds for signaling. Moreover, afterwards, the queue should only be used in non-blocking mode. Waiting for events should be done externally using something like epoll.

Parameters
mqmessage queue
fdconsumer eventfd

Definition at line 485 of file message_queue.c.

+ Here is the caller graph for this function:

◆ svm_msg_q_size()

static u32 svm_msg_q_size ( svm_msg_q_t mq)
inlinestatic

Check length of message queue.

Definition at line 310 of file message_queue.h.

+ Here is the caller graph for this function:

◆ svm_msg_q_size_to_alloc()

uword svm_msg_q_size_to_alloc ( svm_msg_q_cfg_t cfg)

Definition at line 104 of file message_queue.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ svm_msg_q_sub()

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.

This returns the message pointing to the data in the message rings. Should only be used in single consumer scenarios as no locks are grabbed. The consumer is expected to call svm_msg_q_free_msg once it finishes processing/copies the message data.

Parameters
mqmessage queue
msgpointer to structure where message is to be received
condflag that indicates if request should block or not
timetime to wait if condition it SVM_Q_TIMEDWAIT
Returns
success status

Definition at line 457 of file message_queue.c.

+ Here is the call graph for this function:

◆ svm_msg_q_sub_raw()

int svm_msg_q_sub_raw ( svm_msg_q_t mq,
svm_msg_q_msg_t elem 
)

Consumer dequeue one message from queue.

Returns the message pointing to the data in the message rings. Should only be used in single consumer scenarios as no locks are grabbed. The consumer is expected to call svm_msg_q_free_msg once it finishes processing/copies the message data.

Parameters
mqmessage queue
msgpointer to structure where message is to be received
Returns
success status

Definition at line 402 of file message_queue.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ svm_msg_q_sub_raw_batch()

int svm_msg_q_sub_raw_batch ( svm_msg_q_t mq,
svm_msg_q_msg_t msg_buf,
u32  n_msgs 
)

Consumer dequeue multiple messages from queue.

Returns the message pointing to the data in the message rings. Should only be used in single consumer scenarios as no locks are grabbed. The consumer is expected to call svm_msg_q_free_msg once it finishes processing/copies the message data.

Parameters
mqmessage queue
msg_bufpointer to array of messages to received
n_msgslengt of msg_buf array
Returns
number of messages dequeued

Definition at line 423 of file message_queue.c.

+ Here is the call graph for this function:

◆ svm_msg_q_timedwait()

int svm_msg_q_timedwait ( svm_msg_q_t mq,
double  timeout 
)

Timed wait for message queue event.

Must be called with mutex held.

Parameters
mqmessage queue
timeouttime in seconds

Definition at line 572 of file message_queue.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ svm_msg_q_try_lock()

static int svm_msg_q_try_lock ( svm_msg_q_t mq)
inlinestatic

Try locking message queue.

Definition at line 353 of file message_queue.h.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ svm_msg_q_unlock()

static void svm_msg_q_unlock ( svm_msg_q_t mq)
inlinestatic

Unlock message queue.

Definition at line 392 of file message_queue.h.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ svm_msg_q_wait()

int svm_msg_q_wait ( svm_msg_q_t mq,
svm_msg_q_wait_type_t  type 
)

Wait for message queue event.

When eventfds are not configured, the shared memory mutex is locked before waiting on the condvar. Typically called by consumers.

Definition at line 501 of file message_queue.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ svm_msg_q_wait_prod()

int svm_msg_q_wait_prod ( svm_msg_q_t mq)

Wait for message queue event as producer.

Similar to svm_msg_q_wait but lock (mutex or spinlock) must be held. Should only be called by producers.

Definition at line 543 of file message_queue.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function: