Software Bus (swbus)#

group swbus

Publisher-subscriber software bus.

For an overview, see Software Bus (SWBUS).

rcsw/swbus/swbus.h#

Publisher-subscriber software bus.

Defines

RCSW_SWBUS_MAX_NAMELEN#

Size of swbus_config::name, including the NUL.

RCSW_SWBUS_ASYNC#

Declare that swbus subscribers subscribed to the same PID can service received packets in their respective queues before all packets have been pushed to all subscribers during a given swbus_publish_release().

Functions

static inline size_t swbus_meta_space(size_t max_pools, size_t max_rxqs, size_t max_subs)#

Offsets of each region within swbus_config::meta.

Calculate the size of swbus_config::meta for use with RCSW_NOALLOC_META.

Parameters:
Returns:

The # of bytes required.

struct swbus_rxq_ent *swbus_rxq_front(struct pcqueue *queue)#

Get pointer to the top packet on a receive queue.

Doesn’t wait: if the queue is empty, returns NULL with errno set to EAGAIN. Unlike swbus_rxq_wait(), it takes no bus handle and so doesn’t wait for an in-progress swbus_publish_release() to reach every subscriber when RCSW_SWBUS_ASYNC is not set.

Parameters:
  • queue – The receive queue.

Returns:

The top of the queue, or NULL if the queue is empty or an error occurred.

struct swbus *swbus_init(struct swbus *swb_in, const struct swbus_config *params)#

Initialize a swbus instance.

Parameters:
  • swb_in – Caller storage for the handle, used only if RCSW_NOALLOC_HANDLE is passed; ignored (may be NULL) otherwise. See Memory Model .

  • params – The initialization parameters.

Returns:

The initialized bus, or NULL if an error occurred.

void swbus_destroy(struct swbus *swb)#

Destroy a swbus instance.

Any further use of the handle after calling this function is undefined.

Parameters:
  • swb – The bus handle.

struct pcqueue *swbus_rxq_init(struct swbus *swb, void *buf_p, size_t n_entries)#

Allocate and initialize a receive queue.

Parameters:
  • swb – The bus handle.

  • buf_p – Space for the RXQ entries, which are swbus_rxq_ent objects. Can be NULL, in which case the bus allocates it.

  • n_entries – Max # of entries for rxq.

Returns:

Pointer to new receive queue, or NULL if an error occurred.

status_t swbus_subscribe(struct swbus *swb, struct pcqueue *queue, uint32_t pid)#

Subscribe the specified RXQ to the specified packet ID.

Parameters:
  • swb – The bus handle.

  • queue – The RXQ to subscribe.

  • pid – The PID to subscribe to.

Returns:

status_t.

status_t swbus_unsubscribe(struct swbus *swb, struct pcqueue *queue, uint32_t pid)#

Unsubscribe the specified RXQ from the specified packet ID.

Parameters:
  • swb – The bus handle.

  • queue – The RXQ to unsubscribe.

  • pid – The PID to unsubscribe from.

Returns:

status_t.

status_t swbus_publish(struct swbus *swb, uint32_t pid, size_t pkt_size, const void *pkt)#

Publish a packet to the bus.

A memcpy() will be performed. If the packet is very large, consider using swbus_publish_release() instead; it will not perform a memcpy().

Parameters:
  • swb – The bus handle.

  • pid – The packet ID.

  • pkt_size – The size of the packet in bytes.

  • pkt – The packet to publish.

Returns:

status_t

status_t swbus_publish_reserve(struct swbus *swb, struct swbus_rsrvn *res, size_t pkt_size)#

Reserve a buffer on a swbus instance.

A suitable buffer will be found in the first pool large enough to contain the packet and has free space.

Parameters:
  • swb – The swb handle.

  • res – The reservation for the publish (to be filled on success).

  • pkt_size – Size of the packet in bytes; recorded in swbus_rsrvn::pkt_size.

Returns:

status_t. ERROR with errno=ENOSPC if no pool has a free buffer of at least pkt_size bytes.

status_t swbus_publish_release(struct swbus *swb, uint32_t pid, struct swbus_rsrvn *res)#

Release a published entry (i.e. send it to all subscribed receive queues).

If a given receive queue is full, ERROR will be returned, but the bus will still attempt to publish to the remaining queues. If the application takes on the task of synchronization/allocating memory for very large packets, then this function can be called directly, avoiding a potentially expensive memory copy.

Note

The bus mutex is held for the entire release operation by design, not oversight. Dynamic subscription/de-subscription and RXQ initialization are not common in target application environments, so this is unlikely to be a bottleneck. Doing it this way also eliminates the need to snapshot the subscribers to notify, which requires a llist copy, VLA, or hard-coded max to the # subscribers.

Parameters:
  • swb – The bus handle.

  • pid – The packet ID.

  • res – The reservation. res->pkt_size is the packet size subscribers see in swbus_rxq_ent::pkt_size; to publish fewer bytes than were reserved, lower it before calling.

Returns:

status_t.

struct swbus_rxq_ent *swbus_rxq_wait(struct swbus *swb, struct pcqueue *queue)#

Wait (indefinitely) until the given receive queue is not empty, returning a reference to the first item in the queue.

Regardless of how the packet was published, you need to call swbus_rxq_pop_front() when you are finished with the packet.

Parameters:
  • swb – The bus handle.

  • queue – The receive queue to wait on.

Returns:

A reference to the first item in the queue, or NULL if an ERROR occurred.

struct swbus_rxq_ent *swbus_rxq_timedwait(struct swbus *swb, struct pcqueue *queue, struct timespec *to)#

Wait (until a timeout) until the given receive queue is not empty.

Regardless of how the packet was published, you need to call swbus_rxq_pop_front() when you are finished with the packet.

Parameters:
  • swb – The bus handle.

  • queue – The receive queue to wait on.

  • to – A relative timeout. See Timeouts .

Returns:

A reference to the first item in the queue, or NULL if an ERROR or a timeout occurred.

status_t swbus_rxq_pop_front(struct pcqueue *queue, struct swbus_rxq_ent *ent)#

Remove and release the front element from the selected receive queue.

Parameters:
Returns:

status_t.

struct swbus_config#
#include <rcsw/swbus/swbus.h>

SWBUS initialization parameters.

Public Members

struct mpool_config *pools#

Buffer pool configurations, swbus_config::max_pools of them. Each pool can have any number of entries; a handful of pools is typical.

Pools should be ordered from smallest to largest chunk size to get best-fit behavior during swbus_publish_release().

size_t max_pools#

Max # of buffer pools to create for the bus.

size_t max_rxqs#

Max # of receive queues for the bus.

size_t max_subs#

Max # of subscriptions (RXQ-PID pairs) on the bus.

dptr_t *meta#

Application-allocated space for the bus metadata: the mpool and pcqueue handles and the subscriber list. Must be at least swbus_meta_space() bytes. Like all caller-provided memory, regions within it are aligned to RCSW_CONFIG_PTR_ALIGN, which must therefore be at least the alignment of a pointer on the target. Ignored unless RCSW_NOALLOC_META is passed.

The pools’ element storage is configured per pool through swbus_config::pools, and each RXQ’s through swbus_rxq_init(); with all three provided, the bus does no heap allocation (synchronization primitives aside).

uint32_t flags#

Configuration flags.

Valid flags are:

All other flags are ignored.

char name[32]#

Name for the swbus instance. Used to assist with debugging if multiple buses are active. Has no effect on operation.

struct swbus_rxq_ent#
#include <rcsw/swbus/swbus.h>

swbus receive queue (RXQ) entry.

When a packet is published to the bus, a receive queue entry for the packet is placed in each subscribed receive queue.

Public Members

void *data#

Pointer to the buffer with the actual data. Pool buffers are carved at multiples of the pool’s element size, so the buffer is only as aligned as that size allows: copy out of it, or use element sizes that are multiples of the alignment the packet type needs.

size_t pkt_size#

Packet size in bytes.

uint32_t pid#

ID of received packet.

struct mpool *bp#

The buffer pool that the data resides in.

struct swbus_rsrvn#
#include <rcsw/swbus/swbus.h>

A reservation which can later be used to publish some data.

Can be used in 3 ways:

All of the swbus_rxq_XX() functions can be used regardless of which way is chosen.

Public Members

void *data#

Pointer to the buffer with the actual data. As with swbus_rxq_ent::data, pool buffers are only as aligned as the pool’s element size allows.

size_t pkt_size#

Packet size in bytes. Set by swbus_publish_reserve(); swbus_publish_release() publishes this many bytes.

struct mpool *bp#

The mpool that the actual data resides in, or NULL for an application-built reservation.

struct swbus_sub#
#include <rcsw/swbus/swbus.h>

swbus subscription (maps a PID to an RXQ).

Every time a task/thread subscribes to a packet ID, it gets a subscription entry, which is inserted into the bus’s sorted subscriber list.

Public Members

uint32_t pid#

ID of subscribed packet.

struct pcqueue *subscriber#

The pcqueue (RXQ) subscriber.

struct swbus#
#include <rcsw/swbus/swbus.h>

Manage publisher-subscriber needs in an embedded environment.

Memory efficient. Basically a fully connected network (in the parallel computing sense).

Public Members

size_t n_pools#

buffer pools (static during lifetime).#

size_t n_rxqs#

Number of active receive queues (dynamic during lifetime).

size_t max_rxqs#

Max number of receive queues allowed.

size_t max_subs#

Max number of subscribers (RXQ-pid pairs) allowed.

struct mutex mutex#

Mutex to protect access to bus metadata.

uint32_t flags#

Configuration flags.

Valid flags are:

All other flags are ignored.

struct mpool *pools#

Array of buffer pool entries. Published data stored here. Carved from swbus_config::meta with RCSW_NOALLOC_META, otherwise allocated.

struct pcqueue *rxqs#

Array of receive queues. Used by the application to subscribe to packets and to receive published packets. Carved from swbus_config::meta with RCSW_NOALLOC_META, otherwise allocated.

struct llist *subscribers#

List of swbus_sub. Always sorted.

struct rdwrlock syncl#

Prevents applications from servicing their receive queues for the packet currently being pushed out via swbus_publish_release() until all subscribers have been notified if RCSW_SWBUS_ASYNC is not passed.

char name[32]#

Name for the instance. Used to assist with debugging if multiple buses are active. Has no effect on operation.