1 .. This work is licensed under a Creative Commons Attribution 4.0 International License.
2 .. SPDX-License-Identifier: CC-BY-4.0
3 .. CAUTION: this document is generated from source in doc/src/rtd.
4 .. To make changes edit the source and recompile the document.
5 .. Do NOT make changes directly to .rst or .md files.
7 ============================================================================================
9 ============================================================================================
10 --------------------------------------------------------------------------------------------
11 RIC Message Router -- RMR
12 --------------------------------------------------------------------------------------------
18 The RIC Message Router (RMR) is a library for peer-to-peer
19 communication. Applications use the library to send and
20 receive messages where the message routing and endpoint
21 selection is based on the message type rather than DNS host
22 name-IP port combinations. The library provides the following
26 * Routing and endpoint selection is based on *message type.*
28 * Application is insulated from the underlying transport
29 mechanism and/or protocols.
31 * Message distribution (round robin or fanout) is selectable
34 * Route management updates are received and processed
35 asynchronously and without overt application involvement.
43 RMR's main purpose is to provide an application with the
44 ability to send and receive messages to/from other peer
45 applications with minimal effort on the application's part.
46 To achieve this, RMR manages all endpoint information,
47 connections, and routing information necessary to establish
48 and maintain communication. From the application's point of
49 view, all that is required to send a message is to allocate
50 (via RMR) a message buffer, add the payload data, and set the
51 message type. To receive a message, the application needs
52 only to invoke the receive function; when a message arrives a
53 message buffer will be returned as the function result.
59 Applications are required to place a message type into a
60 message before sending, and may optionally add a subscription
61 ID when appropriate. The combination of message type, and
62 subscription ID are refered to as the *message key,* and is
63 used to match an entry in a routing table which provides the
64 possible endpoints expecting to receive messages with the
71 An endpoint from RMR's perspective is an application to which
72 RMR may establish a connection, and expect to send messages
73 with one or more defined message keys. Each entry in the
74 route table consists of one or more endpoint groups, called
75 round robin groups. When a message matches a specific entry,
76 the entry's groups are used to select the destination of the
77 message. A message is sent once to each group, with messages
78 being *balanced* across the endpoints of a group via round
79 robin selection. Care should be taken when defining multiple
80 groups for a message type as there is extra overhead required
81 and thus the overall message latency is somewhat increased.
87 Route table information is made available to RMR a static
88 file (loaded once), or by updates sent from a separate route
89 manager application. If a static table is provided, it is
90 loaded during RMR initialization and will remain in use until
91 an external process connects and delivers a route table
92 update (often referred to as a dynamic update). Dynamic
93 updates are listened for in a separate process thread and
94 applied automatically; the application does not need to allow
95 for, or trigger, updates.
98 Latency And Throughput
99 ----------------------
101 While providing insulation from the underlying message
102 transport mechanics, RMR must also do so in such a manner
103 that message latency and throughput are not impacted. In
104 general, the RMR induced overhead, incurred due to the
105 process of selecting an endpoint for each message, is minimal
106 and should not impact the overall latency or throughput of
107 the application. This impact has been measured with test
108 applications running on the same physical host and the
109 average latency through RMR for a message was on the order of
112 As an application's throughput increases, it becomes easy for
113 the application to overrun the underlying transport mechanism
114 (e.g. NNG), consume all available TCP transmit buffers, or
115 otherwise find itself in a situation where a send might not
116 immediately complete. RMR offers different *modes* which
117 allow the application to manage these states based on the
118 overall needs of the application. These modes are discussed
119 in the *Configuration* section of this document.
125 To use, the RMR based application simply needs to initialise
126 the RMR environment, wait for RMR to have received a routing
127 table (become ready), and then invoke either the send or
128 receive functions. These steps, and some behind the scenes
129 details, are described in the following paragraphs.
135 The RMR function ``rmr_init()`` is used to set up the RMR
136 environment and must be called before messages can be sent or
137 received. One of the few parameters that the application must
138 communicate to RMR is the port number that will be used as
139 the listen port for new connections. The port number is
140 passed on the initialisation function call and a TCP listen
141 socket will be opened with this port. If the port is already
142 in use RMR will report a failure; the application will need
143 to reinitialise with a different port number, abort, or take
144 some other action appropriate for the application.
146 In addition to creating a TCP listen port, RMR will start a
147 process thread which will be responsible for receiving
148 dynamic updates to the route table. This thread also causes a
149 TCP listen port to be opened as it is expected that the
150 process which generates route table updates will connect and
151 send new information when needed. The route table update port
152 is **not** supplied by the application, but is supplied via
153 an environment variable as this value is likely determined by
154 the mechanism which is starting and configuring the
161 On successful initialisation, a void pointer, often called a
162 *handle* by some programming languages, is returned to the
163 application. This is a reference to the RMR control
164 information and must be passed as the first parameter on most
165 RMR function calls. RMR refers to this as the context, or
172 An application which is only receiving messages does not need
173 to wait for RMR to *become ready* after the call to the
174 initialization function. However, before the application can
175 successfully send a message, RMR must have loaded a route
176 table, and the application must wait for RMR to report that
177 it has done so. The RMR function ``rmr_ready()`` will return
178 the value *true* (1) when a complete route table has been
179 loaded and can be used to determine the endpoint for a send
186 The process of receiving is fairly straight forward. The
187 application invokes the RMR ``rmr_rcv_msg()`` function which
188 will block until a message is received. The function returns
189 a pointer to a message block which provides all of the
190 details about the message. Specifically, the application has
191 access to the following information either directly or
195 * The payload (actual data)
197 * The total payload length in bytes
199 * The number of bytes of the payload which contain valid data
201 * The message type and subscription ID values
203 * The hostname and IP address of the source of the message
208 * Tracing data (if provided)
216 The message payload contains the *raw* data that was sent by
217 the peer application. The format will likely depend on the
218 message type, and is expected to be known by the application.
219 A direct pointer to the payload is available from the message
220 buffer (see appendix B for specific message buffer details).
222 Two payload-related length values are also directly
223 available: the total payload length, and the number of bytes
224 actually filled with data. The used length is set by the
225 caller, and may or not be an accurate value. The total
226 payload length is determined when the buffer is created for
227 sending, and is the maximum number of bytes that the
228 application may modify should the buffer be used to return a
232 Message Type and Subscription ID
233 --------------------------------
235 The message type and subscription ID are both directly
236 available from the message buffer, and are the values which
237 were used to by RMR in the sending application to select the
238 endpoint. If the application resends the message, as opposed
239 to returning the message buffer as a response, the message
240 number and/or the subscription ID might need to be changed to
241 avoid potential issues[1].
247 The source, or sender information, is indirectly available to
248 the application via the ``rmr_get_src()`` and
249 ``rmr_get_ip()`` functions. The former returns a string
250 containing ``hostname:port,`` while the string
251 ``ip:port`` is returned by the latter.
257 The message buffer contains a fixed length set of bytes which
258 applications can set to track related messages across the
259 application concept of a transaction. RMR will use the
260 transaction ID for matching a response message when the
261 ``rmr_call()`` function is used to send a message.
267 RMR supports the addition of an optional trace information to
268 any message. The presence and size is controlled by the
269 application, and can vary from message to message if desired.
270 The actual contents of the trace information is determined by
271 the application; RMR provides only the means to set, extract,
272 and obtain a direct reference to the trace bytes. The trace
273 data field in a message buffer is discussed in greater detail
274 in the *Trace Data* section.
280 Sending requires only slightly more work on the part of the
281 application than receiving a message. The application must
282 allocate an RMR message buffer, populate the message payload
283 with data, set the message type and length, and optionally
284 set the subscription ID. Information such as the source IP
285 address, hostname, and port are automatically added to the
286 message buffer by RMR, so there is no need for the
287 application to worry about these.
290 Message Buffer Allocation
291 -------------------------
293 The function ``rmr_msg_alloc()`` allocates a *zero copy*
294 buffer and returns a pointer to the RMR ``rmr_mbuf_t``
295 structure. The message buffer provides direct access to the
296 payload, length, message type and subscription ID fields. The
297 buffer must be preallocated in order to allow the underlying
298 transport mechanism to allocate the payload space from its
299 internal memory pool; this eliminates multiple copies as the
300 message is sent, and thus is more efficient.
302 If a message buffer has been received, and the application
303 wishes to use the buffer to send a response, or to forward
304 the buffer to another application, a new buffer does **not**
305 need to be allocated. The application may set the necessary
306 information (message type, etc.), and adjust the payload, as
307 is necessary and then pass the message buffer to
308 ``rmr_send_msg()`` or ``rmr_rts_msg()`` to be sent or
309 returned to the sender.
312 Populating the Message Buffer
313 -----------------------------
315 The application has direct access to several of the message
316 buffer fields, and should set them appropriately.
326 This is the number of bytes that the application placed into
327 the payload. Setting length to 0 is allowed, and length may
328 be less than the allocated payload size.
332 The message type that RMR will use to determine the endpoint
333 used as the target of the send.
337 The subscription ID if the message is to be routed based on
338 the combination of message type and subscription ID. If no
339 subscription ID is valid for the message, the application
340 should set the field with the RMR constant
345 The application should obtain the reference (pointer) to the
346 payload from the message buffer and place any data into the
347 payload. The application is responsible for ensuring that the
348 maximum payload size is not exceeded. The application may
349 obtain the maximum size via the ``rmr_payload_size()``
354 Optionally, the application may add trace information to the
361 Sending a Message Buffer
362 ------------------------
364 Once the application has populated the necessary bits of a
365 message, it may be sent by passing the buffer to the
366 ``rmr_send_msg()`` function. This function will select an
367 endpoint to receive the message, based on message type and
368 subscription ID, and will pass the message to the underlying
369 transport mechanism for actual transmission on the
370 connection. (Depending on the underlying transport mechanism,
371 the actual connection to the endpoint may happen at the time
372 of the first message sent to the endpoint, and thus the
373 latency of the first send might be longer than expected.)
375 On success, the send function will return a reference to a
376 message buffer; the status within that message buffer will
377 indicate what the message buffer contains. When the status is
378 ``RMR_OK`` the reference is to a **new** message buffer for
379 the application to use for the next send; the payload size is
380 the same as the payload size allocated for the message that
381 was just sent. This is a convenience as it eliminates the
382 need for the application to call the message allocation
383 function at some point in the future, and assumes the
384 application will send many messages which will require the
385 same payload dimensions.
387 If the message contains any status other than ``RMR_OK,``
388 then the message could **not** be sent, and the reference is
389 to the unsent message buffer. The value of the status will
390 indicate whether the nature of the failure was transient (
391 ``RMR_ERR_RETRY``) or not. Transient failures are likely to
392 be successful if the application attempts to send the message
393 at a later time. Unfortunately, it is impossible for RMR to
394 know the exact transient failure (e.g. connection being
395 established, or TCP buffer shortage), and thus it is not
396 possible to communicate how long the application should wait
397 before attempting to resend, if the application wishes to
398 resend the message. (More discussion with respect to message
399 retries can be found in the *Handling Failures* section.)
405 Several forms of usage fall into a more advanced category and
406 are described in the following sections. These include
407 blocking call, return to sender and wormhole functions.
413 The RMR function ``rmr_call()`` sends a message in the exact
414 same manner as the ``rmr_send_msg()()`` function, with the
415 endpoint selection based on the message key. But unlike the
416 send function, ``rmr_call()`` will block and wait for a
417 response from the application that is selected to receive the
418 message. The matching message is determined by the
419 transaction ID which the application must place into the
420 message buffer prior to invoking ``rmr_call()``. Similarly,
421 the responding application must ensure that the same
422 transaction ID is placed into the message buffer before
423 returning its response.
425 The return from the call is a message buffer with the
426 response message; there is no difference between a message
427 buffer returned by the receive function and one returned by
428 the ``rmr_call()`` function. If a response is not received in
429 a reasonable amount of time, a nil message buffer is returned
430 to the calling application.
436 Because of the nature of RMR's routing policies, it is
437 generally not possible for an application to control exactly
438 which endpoint is sent a message. There are cases, such as
439 responding to a message delivered via ``rmr_call()`` that the
440 application must send a message and guarantee that RMR routes
441 it to an exact destination. To enable this, RMR provides the
442 ``rmr_rts_msg(),`` return to sender, function. Upon receipt
443 of any message, an application may alter the payload, and if
444 necessary the message type and subscription ID, and pass the
445 altered message buffer to the ``rmr_rts_msg()`` function to
446 return the altered message to the application which sent it.
447 When this function is used, RMR will examine the message
448 buffer for the source information and use that to select the
449 connection on which to write the response.
455 The basic call mechanism described above is **not** thread
456 safe, as it is not possible to guarantee that a response
457 message is delivered to the correct thread. The RMR function
458 ``rmr_mt_call()`` accepts an additional parameter which
459 identifies the calling thread in order to ensure that the
460 response is delivered properly. In addition, the application
461 must specifically initialise the multi-threaded call
462 environment by passing the ``RMRFL_MTCALL`` flag as an option
463 to the ``rmr_init()`` function.
465 One advantage of the multi-threaded call capability in RMR is
466 the fact that only the calling thread is blocked. Messages
467 received which are not responses to the call are continued to
468 be delivered via normal ``rmr_rcv_msg()`` calls.
470 While the process is blocked waiting for the response, it is
471 entirely possible that asynchronous, non-matching, messages
472 will arrive. When this happens, RMR will queues the messages
473 and return them to the application over the next calls to
480 As was mentioned earlier, the design of RMR is to eliminate
481 the need for an application to know a specific endpoint, even
482 when a response message is being sent. In some rare cases it
483 may be necessary for an application to establish a direct
484 connection to an RMR-based application rather than relying on
485 message type and subscription ID based routing. The
486 *wormhole* functions provide an application with the ability
487 to create a direct connection and then to send and receive
488 messages across the connection. The following are the RMR
489 functions which provide wormhole communications:
499 Open a connection to an endpoint. Name or IP address and port
500 of the endpoint is supplied. Returns a wormhole ID that the
501 application must use when sending a direct message.
503 * - **rmr_wh_send_msg**
505 Sends an RMR message buffer to the connected application. The
506 message type and subscription ID may be set in the message,
507 but RMR will ignore both.
511 Closes the direct connection.
520 The vast majority of states reported by RMR are fatal; if
521 encountered during setup or initialization, then it is
522 unlikely that any message oriented processing should
523 continue, and when encountered on a message operation
524 continued operation on that message should be abandoned.
525 Specifically with regard to message sending, it is very
526 likely that the underlying transport mechanism will report a
527 *soft,* or transient, failure which might be successful if
528 the operation is retried at a later point in time. The
529 paragraphs below discuss the methods that an application
530 might deal with these soft failures.
536 When a soft failure is reported, the returned message buffer
537 returned by the RMR function will be ``RMR_ERR_RETRY.`` These
538 types of failures can occur for various reasons; one of two
539 reasons is typically the underlying cause:
542 * The session to the targeted recipient (endpoint) is not
545 * The transport mechanism buffer pool is full and cannot
546 accept another buffer.
550 Unfortunately, it is not possible for RMR to determine which
551 of these two cases is occurring, and equally as unfortunate
552 the time to resolve each is different. The first, no
553 connection, may require up to a second before a message can
554 be accepted, while a rejection because of buffer shortage is
555 likely to resolve in less than a millisecond.
561 The action which an application takes when a soft failure is
562 reported ultimately depends on the nature of the application
563 with respect to factors such as tolerance to extended message
564 latency, dropped messages, and over all message rate.
570 In an effort to reduce the workload of an application
571 developer, RMR has a default retry policy such that RMR will
572 attempt to retransmit a message up to 1000 times when a soft
573 failure is reported. These retries generally take less than 1
574 millisecond (if all 1000 are attempted) and in most cases
575 eliminates nearly all reported soft failures to the
576 application. When using this mode, it might allow the
577 application to simply treat all bad return values from a send
578 attempt as permanent failures.
580 If an application is so sensitive to any delay in RMR, or the
581 underlying transport mechanism, it is possible to set RMR to
582 return a failure immediately on any kind of error (permanent
583 failures are always reported without retry). In this mode,
584 RMR will still set the state in the message buffer to
585 ``RMR_ERR_RETRY,`` but will **not** make any attempts to
586 resend the message. This zero-retry policy is enabled by
587 invoking the ``rmr_set_stimeout()`` with a value of 0; this
588 can be done once immediately after ``rmr_init()`` is invoked.
590 Regardless of the retry mode which the application sets, it
591 will ultimately be up to the application to handle failures
592 by queuing the message internally for resend, retrying
593 immediately, or dropping the send attempt all together. As
594 stated before, only the application can determine how to best
595 handle send failures.
601 RMR will return the state of processing for message based
602 operations (send/receive) as the status in the message
603 buffer. For non-message operations, state is returned to the
604 caller as the integer return value for all functions which
605 are not expected to return a pointer (e.g.
606 ``rmr_init()``.) The following are the RMR state constants
607 and a brief description of their meaning.
617 state is good; operation finished successfully
619 * - **RMR_ERR_BADARG**
621 argument passed to function was unusable
623 * - **RMR_ERR_NOENDPT**
625 send/call could not find an endpoint based on msg type
627 * - **RMR_ERR_EMPTY**
629 msg received had no payload; attempt to send an empty message
631 * - **RMR_ERR_NOHDR**
633 message didn't contain a valid header
635 * - **RMR_ERR_SENDFAILED**
637 send failed; errno may contain the transport provider reason
639 * - **RMR_ERR_CALLFAILED**
641 unable to send the message for a call function; errno may
642 contain the transport provider reason
644 * - **RMR_ERR_NOWHOPEN**
646 no wormholes are open
650 the wormhole id provided was invalid
652 * - **RMR_ERR_OVERFLOW**
654 operation would have busted through a buffer/field size
656 * - **RMR_ERR_RETRY**
658 request (send/call/rts) failed, but caller should retry
659 (EAGAIN for wrappers)
661 * - **RMR_ERR_RCVFAILED**
663 receive failed (hard error)
665 * - **RMR_ERR_TIMEOUT**
667 response message not received in a reasonable amount of time
669 * - **RMR_ERR_UNSET**
671 the message hasn't been populated with a transport buffer
673 * - **RMR_ERR_TRUNC**
675 length in the received buffer is longer than the size of the
676 allocated payload, received message likely truncated (length
677 set by sender could be wrong, but we can't know that)
679 * - **RMR_ERR_INITFAILED**
681 initialisation of something (probably message) failed
683 * - **RMR_ERR_NOTSUPP**
685 the request is not supported, or RMR was not initialised for
690 Depending on the underlying transport mechanism, and the
691 nature of the call that RMR attempted, the system
692 ``errno`` value might reflect additional detail about the
693 failure. Applications should **not** rely on errno as some
694 transport mechanisms do not set it with any consistency.
697 Configuration and Control
698 =========================
700 With the assumption that most RMR based applications will be
701 executed in a containerised environment, there are some
702 underlying mechanics which the developer may need to know in
703 order to properly provide a configuration specification to
704 the container management system. The following paragraphs
705 briefly discuss these.
712 RMR requires two (2) TCP listen ports: one for general
713 application-to-application communications and one for
714 route-table updates. The general communication port is
715 specified by the application at the time RMR is initialised.
716 The port used to listen for route table updates is likely to
717 be a constant port shared by all applications provided they
718 are running in separate containers. To that end, the port
719 number defaults to 4561, but can be configured with an
720 environment variable (see later paragraph in this section).
726 RMR is typically host name agnostic. Route table entries may
727 contain endpoints defined either by host name or IP address.
728 In the container world the concept of a *service name* might
729 exist, and likely is different than a host name. RMR's only
730 requirement with respect to host names is that a name used on
731 a route table entry must be resolvable via the
732 ``gethostbyname`` system call.
735 Environment Variables
736 ---------------------
738 Several environment variables are recognised by RMR which, in
739 general, are used to define interfaces and listen ports (e.g.
740 the route table update listen port), or debugging
741 information. Generally this information is system controlled
742 and thus RMR expects this information to be defined in the
743 environment rather than provided by the application. The
744 following is a list of the environment variables which RMR
753 * - **RMR_ASYNC_CONN**
755 Allows the async connection mode to be turned off (by setting
756 the value to 0). When set to 1, or missing from the
757 environment, RMR will invoke the connection interface in the
758 transport mechanism using the non-blocking (async) mode. This
759 will likely result in many "soft failures" (retry) until the
760 connection is established, but allows the application to
761 continue unimpeded should the connection be slow to set up.
765 This provides the interface that RMR will bind listen ports
766 to, allowing for a single interface to be used rather than
767 listening across all interfaces. This should be the IP
768 address assigned to the interface that RMR should listen on,
769 and if not defined RMR will listen on all interfaces.
773 This variable defines the port that RMR should open for
774 communications with Route Manager, and other RMR control
775 applications. If not defined, the port 4561 is assumed.
777 Previously, the ``RMR_RTG_SVC`` (route table generator
778 service port) was used to define this port. However, a future
779 version of Route Manager will require RMR to connect and
780 request tables, thus that variable is now used to supply the
781 Route Manager's well-known address and port.
783 To maintain backwards compatibility with the older Route
784 Manager versions, the presence of this variable in the
785 environment will shift RMR's behaviour with respect to the
786 default value used when ``RMR_RTG_SVC`` is **not** defined.
788 When ``RMR_CTL_PORT`` is **defined:** RMR assumes that Route
789 Manager requires RMR to connect and request table updates is
790 made, and the default well-known address for Route manager is
791 used (routemgr:4561).
793 When ``RMR_CTL_PORT`` is **undefined:** RMR assumes that
794 Route Manager will connect and push table updates, thus the
795 default listen port (4561) is used.
797 To avoid any possible misinterpretation and/or incorrect
798 assumptions on the part of RMR, it is recommended that both
799 the ``RMR_CTL_PORT`` and ``RMR_RTG_SVC`` be defined. In the
800 case where both variables are defined, RMR will behave
801 exactly as is communicated with the variable's values.
803 * - **RMR_RTREQ_FREQ**
805 When RMR needs a new route table it will send a request once
806 every ``n`` seconds. The default value for ``n`` is 5, but
807 can be changed if this variable is set prior to invoking the
808 process. Accepted values are between 1 and 300 inclusive.
812 The value of this variable depends on the Route Manager in
815 When the Route Manager is expecting to connect to an xAPP and
816 push route tables, this variable must indicate the
817 ``port`` which RMR should use to listen for these
820 When the Route Manager is expecting RMR to connect and
821 request a table update during initialisation, the variable
822 should be the ``host`` of the Route Manager process.
824 The ``RMR_CTL_PORT`` variable (added with the support of
825 sending table update requests to Route manager), controls the
826 behaviour if this variable is not set. See the description of
827 that variable for details.
831 By default RMR writes messages to standard error (incorrectly
832 referred to as log messages) in human readable format. If
833 this environment variable is set to 0, the format of standard
834 error messages might be written in some format not easily
835 read by humans. If missing, a value of 1 is assumed.
837 * - **RMR_LOG_VLEVEL**
839 This is a numeric value which corresponds to the verbosity
840 level used to limit messages written to standard error. The
841 lower the number the less chatty RMR functions are during
842 execution. The following is the current relationship between
843 the value set on this variable and the messages written:
853 Off; no messages of any sort are written.
857 Only critical messages are written (default if this variable
862 Errors and all messages written with a lower value.
866 Warnings and all messages written with a lower value.
870 Informational and all messages written with a lower value.
874 Debugging mode -- all messages written, however this requires
875 RMR to have been compiled with debugging support enabled.
879 * - **RMR_RTG_ISRAW**
881 **Deprecated.** Should be set to 1 if the route table
882 generator is sending "plain" messages (not using RMR to send
883 messages), 0 if the RTG is using RMR to send. The default is
884 1 as we don't expect the RTG to use RMR.
886 This variable is only recognised when using the NNG transport
887 library as it is not possible to support NNG "raw"
888 communications with other transport libraries. It is also
889 necessary to match the value of this variable with the
890 capabilities of the Route Manager; at some point in the
891 future RMR will assume that all Route Manager messages will
892 arrive via an RMR connection and will ignore this variable.
896 This is used to supply a static route table which can be used
897 for debugging, testing, or if no route table generator
898 process is being used to supply the route table. If not
899 defined, no static table is used and RMR will not report
900 *ready* until a table is received. The static route table may
901 contain both the route table (between newrt start and end
902 records), and the MEID map (between meid_map start and end
907 This is either the name or IP address which is placed into
908 outbound messages as the message source. This will used when
909 an RMR based application uses the rmr_rts_msg() function to
910 return a response to the sender. If not supplied RMR will use
911 the hostname which in some container environments might not
914 The value of this variable is also used for Route Manager
915 messages which are sent via an RMR connection.
917 * - **RMR_VCTL_FILE**
919 This supplies the name of a verbosity control file. The core
920 RMR functions do not produce messages unless there is a
921 critical failure. However, the route table collection thread,
922 not a part of the main message processing component, can
923 write additional messages to standard error. If this variable
924 is set, RMR will extract the verbosity level for these
925 messages (0 is silent) from the first line of the file.
926 Changes to the file are detected and thus the level can be
927 changed dynamically, however RMR will only suss out this
928 variable during initialisation, so it is impossible to enable
929 verbosity after startup.
933 If set to 1, RMR will write some warnings which are
934 non-performance impacting. If the variable is not defined, or
935 set to 0, RMR will not write these additional warnings.
939 There are other, non-RMR, variables which may exist and are
940 used by RMR. These variable names are not under the control
941 of RMR, so they are subject to change without potentiallyb
942 being reflected in either RMR's code, or this document. The
943 following is a list of these environment variables.
951 * - **ALARM_MANAGER_SERVICE_NAME**
953 This is the DNS name, or IP address, of the process which is
954 listening for RMR alarm messages. If this variable is
955 missing, ``service-ricplt-alarmmanager-rmr`` is assumed.
957 * - **ALARM_MANAGER_SERVICE_PORT**
959 This is the port that the alarm manager is using to accept
960 RMR messages. If the environment variable is missing the
961 value ``4560`` is assumed.
969 As with nearly all UNIX libraries, errors, warnings and
970 informational messages are written in plain text to the
971 standard error device (stderr). All RMR messages are prefixed
972 with the current time (in milliseconds past the standard UNIX
973 epoch), the process ID, and a severity indicator. RMR
974 messages are written with one of three severity strings:
984 The event is of a critical nature and it is unlikely that RMR
985 will continue to operate correctly if at all. It is almost
986 certain that immediate action will be needed to resolve the
991 The event is not expected and RMR is not able to handle it.
992 There is a small chance that continued operation will be
993 negatively impacted. Eventual action to diagnose and correct
994 the issue will be necessary.
998 The event was not expected by RMR, but can be worked round.
999 Normal operation will continue, but it is recommended that
1000 the cause of the problem be investigated.
1006 Log message supression
1007 ----------------------
1009 For the most part, the *fast path* code in RMR does no
1010 logging; even when messages are squelched, there is a
1011 non-zero cosst to check for the setting each time a potential
1012 message is to be written. To that end, RMRM will log only
1013 severe errors once initialisation has completed. An exception
1014 to this policy exists in the route table collection thread.
1015 The thread of execution which collects route table updates
1016 does not need to be concerned with performance, and as such
1017 has the potential to log its actions in a very verbose
1018 manner. The environment variable `` RMR_VCTL_FILE `` can be
1019 used to define a file where the desired verbosity level (0 to
1020 4 where 0 is off) can be placed. If the environment variable
1021 is not set when the process starts, RMR will assume that the
1022 file ``/tmp/rmr.v`` will be used. Beginning with version
1023 4.6.0 this file does **not** need to exist when the process
1024 is started. To change the verbosity level, the desired value
1025 is written to the file on the first line.
1031 The route table colleciton thread is also responsible for
1032 watching for situations which need to be reported as alarms
1033 to the platform's alarm management service. When a state
1034 exists RMR will create and send an alarm (via RMR message) to
1035 the alarm service, and will send a *clear* message when the
1036 state no longer exists. Currently RMR will alarm only when
1037 the application is not removing messages from the receive
1038 ring quicklye enough causing RMR to drop messages as they are
1046 [1] It is entirely possible to design a routing table, and
1047 application group, such that the same message type is is
1048 left unchanged and the message is forwarded by an
1049 application after updating the payload. This type of
1050 behaviour is often referred to as service chaining, and can
1051 be done without any "knowledge" by an application with
1052 respect to where the message goes next. Service chaining is
1053 supported by RMR in as much as it allows the message to be
1054 resent, but the actual complexities of designing and
1055 implementing service chaining lie with the route table
1063 Appendix A -- Quick Reference
1064 =============================
1066 Please refer to the RMR manual pages on the Read the Docs
1069 https://docs.o-ran-sc.org/projects/o-ran-sc-ric-plt-lib-rmr/en/latest/index.html
1073 Appendix B -- Message Buffer Details
1074 ====================================
1076 The RMR message buffer is a C structure which is exposed in
1077 the ``rmr.h`` header file. It is used to manage a message
1078 received from a peer endpoint, or a message that is being
1079 sent to a peer. Fields include payload length, amount of
1080 payload actually used, status, and a reference to the
1081 payload. There are also fields which the application should
1082 ignore, and could be hidden in the header file, but we chose
1083 not to. These fields include a reference to the RMR header
1084 information, and to the underlying transport mechanism
1085 message struct which may or may not be the same as the RMR
1092 The following is the C structure. Readers are cautioned to
1093 examine the ``rmr.h`` header file directly; the information
1094 here may be out of date (old document in some cache), and
1095 thus it may be incorrect.
1102 int state; // state of processing
1103 int mtype; // message type
1104 int len; // length of data in the payload (send or received)
1105 unsigned char* payload; // transported data
1106 unsigned char* xaction; // pointer to fixed length transaction id bytes
1107 int sub_id; // subscription id
1108 int tp_state; // transport state (errno)
1110 // these things are off limits to the user application
1111 void* tp_buf; // underlying transport allocated pointer (e.g. nng message)
1112 void* header; // internal message header (whole buffer: header+payload)
1113 unsigned char* id; // if we need an ID in the message separate from the xaction id
1114 int flags; // various MFL_ (private) flags as needed
1115 int alloc_len; // the length of the allocated space (hdr+payload)
1116 void* ring; // ring this buffer should be queued back to
1117 int rts_fd; // SI fd for return to sender
1118 int cookie; // cookie to detect user misuse of free'd msg
1124 State vs Transport State
1125 ------------------------
1127 The state field reflects the state at the time the message
1128 buffer is returned to the calling application. For a send
1129 operation, if the state is not ``RMR_OK`` then the message
1130 buffer references the payload that could not be sent, and
1131 when the state is ``RMR_OK`` the buffer references a *fresh*
1132 payload that the application may fill in.
1134 When the state is not ``RMR_OK,`` C programmes may examine
1135 the global ``errno`` value which RMR will have left set, if
1136 it was set, by the underlying transport mechanism. In some
1137 cases, wrapper modules are not able to directly access the
1138 C-library ``errno`` value, and to assist with possible
1139 transport error details, the send and receive operations
1140 populate ``tp_state`` with the value of ``errno.``
1142 Regardless of whether the application makes use of the
1143 ``tp_state,`` or the ``errno`` value, it should be noted that
1144 the underlying transport mechanism may not actually update
1145 the errno value; in other words: it might not be accurate. In
1146 addition, RMR populates the ``tp_state`` value in the message
1147 buffer **only** when the state is not ``RMR_OK.``
1153 The transaction field was exposed in the first version of
1154 RMR, and in hindsight this shouldn't have been done. Rather
1155 than break any existing code the reference was left, but
1156 additional fields such as trace data, were not directly
1157 exposed to the application. The application developer is
1158 strongly encouraged to use the functions which get and set
1159 the transaction ID rather than using the pointer directly;
1160 any data overruns will not be detected if the reference is
1163 In contrast, the payload reference should be used directly by
1164 the application in the interest of speed and ease of
1165 programming. The same care to prevent writing more bytes to
1166 the payload buffer than it can hold must be taken by the
1167 application. By the nature of the allocation of the payload
1168 in transport space, RMR is unable to add guard bytes and/or
1169 test for data overrun.
1175 When RMR sends the application's message, the message buffer
1176 is **not** transmitted. The transport buffer (tp_buf) which
1177 contains the RMR header and application payload is the only
1178 set of bytes which are transmitted. While it may seem to the
1179 caller like the function ``rmr_send_msg()`` is returning a
1180 new message buffer, the same struct is reused and only a new
1181 transport buffer is allocated. The intent is to keep the
1182 alloc/free cycles to a minimum.
1186 Appendix C -- Glossary
1187 ======================
1189 Many terms in networking can be interpreted with multiple
1190 meanings, and several terms used in various RMR documentation
1191 are RMR specific. The following definitions are the meanings
1192 of terms used within RMR documentation and should help the
1193 reader to understand the intent of meaning.
1202 A programme which uses RMR to send and/or receive messages
1203 to/from another RMR based application.
1205 * - **Critical error**
1207 An error that RMR has encountered which will prevent further
1208 successful processing by RMR. Critical errors usually
1209 indicate that the application should abort.
1213 An RMR based application that is defined as being capable of
1214 receiving one or more types of messages (as defined by a
1217 * - **Environment variable**
1219 A key/value pair which is set externally to the application,
1220 but which is available to the application (and referenced
1221 libraries) through the ``getenv`` system call. Environment
1222 variables are the main method of communicating information
1223 such as port numbers to RMR.
1227 An abnormal condition that RMR has encountered, but will not
1228 affect the overall processing by RMR, but may impact certain
1229 aspects such as the ability to communicate with a specific
1230 endpoint. Errors generally indicate that something, usually
1231 external to RMR, must be addressed.
1235 The name of the host as returned by the ``gethostbyname``
1236 system call. In a containerised environment this might be the
1237 container or service name depending on how the container is
1238 started. From RMR's point of view, a host name can be used to
1239 resolve an *endpoint* definition in a *route* table.)
1243 Internet protocol. A low level transmission protocol which
1244 governs the transmission of datagrams across network
1247 * - **Listen socket**
1249 A *TCP* socket used to await incoming connection requests.
1250 Listen sockets are defined by an interface and port number
1251 combination where the port number is unique for the
1256 A series of bytes transmitted from the application to another
1257 RMR based application. A message is comprised of RMR specific
1258 data (a header), and application data (a payload).
1260 * - **Message buffer**
1262 A data structure used to describe a message which is to be
1263 sent or has been received. The message buffer includes the
1264 payload length, message type, message source, and other
1267 * - **Message type**
1269 A signed integer (0-32000) which identifies the type of
1270 message being transmitted, and is one of the two components
1271 of a *routing key.* See *Subscription ID.*
1275 The portion of a message which holds the user data to be
1276 transmitted to the remote *endpoint.* The payload contents
1277 are completely application defined.
1281 A set of information which defines the current state of the
1282 underlying transport connections that RMR is managing. The
1283 application will be give a context reference (pointer) that
1284 is supplied to most RMR functions as the first parameter.
1288 The method of selecting an *endpoint* from a list such that
1289 all *endpoints* are selected before starting at the head of
1294 A series of "rules" which define the possible *endpoints* for
1297 * - **Route table manager**
1299 An application responsible for building a *route table* and
1300 then distributing it to all applicable RMR based
1305 The process of selecting an *endpoint* which will be the
1306 recipient of a message.
1310 A combination of *message type* and *subscription ID* which
1311 RMR uses to select the destination *endpoint* when sending a
1316 The sender of a message.
1318 * - **Subscription ID**
1320 A signed integer value (0-32000) which identifies the
1321 subscription characteristic of a message. It is used in
1322 conjunction with the *message type* to determine the *routing
1327 The *endpoint* selected to receive a message.
1331 Transmission Control Protocol. A connection based internet
1332 protocol which provides for lossless packet transportation,
1337 Also called a *process thread, or pthread.* This is a
1338 lightweight process which executes in concurrently with the
1339 application and shares the same address space. RMR uses
1340 threads to manage asynchronous functions such as route table
1343 * - **Trace information**
1345 An optional portion of the message buffer that the
1346 application may populate with data that allows for tracing
1347 the progress of the transaction or application activity
1348 across components. RMR makes no use of this data.
1350 * - **Transaction ID**
1352 A fixed number of bytes in the *message* buffer) which the
1353 application may populate with information related to the
1354 transaction. RMR makes use of the transaction ID for matching
1355 response messages with the &c function is used to send a
1358 * - **Transient failure**
1360 An error state that is believed to be short lived and that
1361 the operation, if retried by the application, might be
1362 successful. C programmers will recognise this as
1367 A warning occurs when RMR has encountered something that it
1368 believes isn't correct, but has a defined work round.
1372 A direct connection managed by RMR between the user
1373 application and a remote, RMR based, application.
1379 Appendix D -- Code Examples
1380 ===========================
1382 The following snippet of code illustrate some of the basic
1383 operation of the RMR library. Please refer to the examples
1384 and test directories in the RMR repository for complete RMR
1391 The following code segment shows how a message buffer can be
1392 allocated, populated, and sent. The snippet also illustrates
1393 how the result from the ``rmr_send_msg()`` function is used
1394 to send the next message. It does not illustrate error and/or
1406 #include <sys/epoll.h>
1409 #include <rmr/rmr.h>
1411 int main( int argc, char** argv ) {
1412 void* mrc; // msg router context
1413 struct epoll_event events[1]; // list of events to give to epoll
1414 struct epoll_event epe; // event definition for event to listen to
1415 int ep_fd = -1; // epoll's file des (given to epoll_wait)
1416 int rcv_fd; // file des for epoll checks
1417 int nready; // number of events ready for receive
1418 rmr_mbuf_t* sbuf; // send buffer
1419 rmr_mbuf_t* rbuf; // received buffer
1422 char* listen_port = "43086";
1423 int delay = 1000000; // mu-sec delay between messages
1425 int stats_freq = 100;
1427 if( argc > 1 ) { // simplistic arg picking
1428 listen_port = argv[1];
1431 delay = atoi( argv[2] );
1434 mtype = atoi( argv[3] );
1437 fprintf( stderr, "<DEMO> listen port: %s; mtype: %d; delay: %d\\n",
1438 listen_port, mtype, delay );
1440 if( (mrc = rmr_init( listen_port, 1400, RMRFL_NONE )) == NULL ) {
1441 fprintf( stderr, "<DEMO> unable to initialise RMR\\n" );
1445 rcv_fd = rmr_get_rcvfd( mrc ); // set up epoll things, start by getting the FD from RMR
1447 fprintf( stderr, "<DEMO> unable to set up polling fd\\n" );
1450 if( (ep_fd = epoll_create1( 0 )) < 0 ) {
1451 fprintf( stderr, "[FAIL] unable to create epoll fd: %d\\n", errno );
1454 epe.events = EPOLLIN;
1455 epe.data.fd = rcv_fd;
1457 if( epoll_ctl( ep_fd, EPOLL_CTL_ADD, rcv_fd, &epe ) != 0 ) {
1458 fprintf( stderr, "[FAIL] epoll_ctl status not 0 : %s\\n", strerror( errno ) );
1462 sbuf = rmr_alloc_msg( mrc, 256 ); // alloc 1st send buf; subsequent bufs alloc on send
1463 rbuf = NULL; // don't need to alloc receive buffer
1465 while( ! rmr_ready( mrc ) ) { // must have route table
1466 sleep( 1 ); // wait til we get one
1468 fprintf( stderr, "<DEMO> rmr is ready\\n" );
1471 while( 1 ) { // send messages until the cows come home
1472 snprintf( sbuf->payload, 200,
1473 "count=%d received= %d ts=%lld %d stand up and cheer!", // create the payload
1474 count, rcvd_count, (long long) time( NULL ), rand() );
1476 sbuf->mtype = mtype; // fill in the message bits
1477 sbuf->len = strlen( sbuf->payload ) + 1; // send full ascii-z string
1479 sbuf = rmr_send_msg( mrc, sbuf ); // send & get next buf to fill in
1480 while( sbuf->state == RMR_ERR_RETRY ) { // soft failure (device busy?) retry
1481 sbuf = rmr_send_msg( mrc, sbuf ); // w/ simple spin that doesn't give up
1485 // check to see if anything was received and pull all messages in
1486 while( (nready = epoll_wait( ep_fd, events, 1, 0 )) > 0 ) { // 0 is non-blocking
1487 if( events[0].data.fd == rcv_fd ) { // waiting on 1 thing, so [0] is ok
1489 rbuf = rmr_rcv_msg( mrc, rbuf ); // receive and ignore; just count
1496 if( (count % stats_freq) == 0 ) { // occasional stats out to tty
1497 fprintf( stderr, "<DEMO> sent %d received %d\\n", count, rcvd_count );
1510 The receiver code is even simpler than the sender code as it
1511 does not need to wait for a route table to arrive (only
1512 senders need to do that), nor does it need to allocate an
1513 initial buffer. The example assumes that the sender is
1514 transmitting a zero terminated string as the payload.
1526 #include <rmr/rmr.h>
1529 int main( int argc, char** argv ) {
1530 void* mrc; // msg router context
1531 long long total = 0;
1532 rmr_mbuf_t* msg = NULL; // message received
1533 int stat_freq = 10; // write stats after reciving this many messages
1535 char* listen_port = "4560"; // default to what has become the standard RMR port
1536 long long count = 0;
1538 long long empty = 0;
1541 listen_port = argv[1];
1544 stat_freq = atoi( argv[2] );
1546 fprintf( stderr, "<DEMO> listening on port: %s\\n", listen_port );
1547 fprintf( stderr, "<DEMO> stats will be reported every %d messages\\n", stat_freq );
1549 mrc = rmr_init( listen_port, RMR_MAX_RCV_BYTES, RMRFL_NONE );
1551 fprintf( stderr, "<DEMO> ABORT: unable to initialise RMr\\n" );
1555 while( ! rmr_ready( mrc ) ) { // wait for RMR to get a route table
1556 fprintf( stderr, "<DEMO> waiting for ready\\n" );
1559 fprintf( stderr, "<DEMO> rmr now shows ready\\n" );
1561 while( 1 ) { // receive until killed
1562 msg = rmr_rcv_msg( mrc, msg ); // block until one arrives
1565 if( msg->state == RMR_OK ) {
1566 count++; // nothing fancy, just count
1574 if( (count % stat_freq) == 0 ) {
1575 fprintf( stderr, "<DEMO> total received: %lld; errors: %lld; empty: %lld\\n",
1576 count, bad, empty );
1584 Receive and Send Sample
1585 -----------------------
1587 The following code snippet receives messages and responds to
1588 the sender if the message type is odd. The code illustrates
1589 how the received message may be used to return a message to
1590 the source. Variable type definitions are omitted for clarity
1591 and should be obvious.
1593 It should also be noted that things like the message type
1594 which id returned to the sender (99) is a random value that
1595 these applications would have agreed on in advance and is
1596 **not** an RMR definition.
1601 mrc = rmr_init( listen_port, MAX_BUF_SZ, RMRFL_NOFLAGS );
1602 rmr_set_stimeout( mrc, 1 ); // allow RMR to retry failed sends for ~1ms
1604 while( ! rmr_ready( mrc ) ) { // we send, therefore we need a route table
1608 mbuf = NULL; // ensure our buffer pointer is nil for 1st call
1611 mbuf = rmr_rcv_msg( mrc, mbuf ); // wait for message
1613 if( mbuf == NULL || mbuf->state != RMR_OK ) {
1617 if( mbuf->mtype % 2 ) { // respond to odd message types
1618 plen = rmr_payload_size( mbuf ); // max size
1620 // reset necessary fields in msg
1621 mbuf->mtype = 99; // response type
1622 mbuf->sub_id = RMR_VOID_SUBID; // we turn subid off
1623 mbuf->len = snprintf( mbuf->payload, plen, "pong: %s", get_info() );
1625 mbuf = rmr_rts_msg( mrc, mbuf ); // return to sender
1626 if( mbuf == NULL || mbuf->state != RMR_OK ) {
1627 fprintf( stderr, "return to sender failed\\n" );
1632 fprintf( stderr, "abort: receive failure\\n" );