878742c5585e6d95385f74eb59e2b5b922617c59
[ric-plt/lib/rmr.git] / src / rmr / nng / include / rmr_nng_private.h
1 //  vim: ts=4 sw=4 noet :
2 /*
3 ==================================================================================
4         Copyright (c) 2019 Nokia
5         Copyright (c) 2018-2019 AT&T Intellectual Property.
6
7    Licensed under the Apache License, Version 2.0 (the "License");
8    you may not use this file except in compliance with the License.
9    You may obtain a copy of the License at
10
11            http://www.apache.org/licenses/LICENSE-2.0
12
13    Unless required by applicable law or agreed to in writing, software
14    distributed under the License is distributed on an "AS IS" BASIS,
15    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16    See the License for the specific language governing permissions and
17    limitations under the License.
18 ==================================================================================
19 */
20
21 /*
22         Mnemonic:       uta_nng_private.h
23         Abstract:       Private header information for the uta nng library functions.
24                                 These are structs which have specific NNG types; anything that
25                                 does not can be included in the common/rmr_agnostic.h header.
26
27         Author:         E. Scott Daniels
28         Date:           31 November 2018
29
30         Mods:           28 Feb 2019 -- moved the majority to the agnostic header.
31 */
32
33 #ifndef _uta_private_h
34 #define _uta_private_h
35
36 /*
37         Manages an endpoint. Type def for this is defined in agnostic.
38 */
39 struct endpoint {
40         char*   name;                   // end point name (symtab reference)
41         char*   proto;                  // connection proto (should only be TCP, but future might bring others)
42         char*   addr;                   // address used for connection
43         nng_socket      nn_sock;        // the nano-msg socket to write to for this entry
44         nng_dialer      dialer;         // the connection specific information (retry timout etc)
45         int             open;                   // set to true if we've connected as socket cannot be checked directly)
46         pthread_mutex_t gate;   // we must serialise when we open/link to the endpoint
47         int             notify;                 // when set we can write connect failure msgs to stderr
48         long long scounts[EPSC_SIZE];           // send counts (indexed by EPSCOUNT_* constants
49 };
50
51 /*
52         Epoll information needed for the rmr_torcv_msg() funciton
53 */
54 typedef struct epoll_stuff {
55         struct epoll_event events[1];                           // wait on 1 possible events
56         struct epoll_event epe;                                         // event definition for event to listen to
57         int ep_fd;                                                                      // file des from nng
58         int nng_fd;                                                                     // fd from nng
59 } epoll_stuff_t;
60
61 /*
62         Context describing our world. Should be returned to user programme on
63         call to initialise, and passed as first parm on all calls to other
64         visible functions.
65
66         The typedef is declared in the agnostic header.
67 */
68 struct uta_ctx {
69         char*   my_name;                        // dns name of this host to set in sender field of a message
70         char*   my_ip;                          // the ip address we _think_ we are using sent in src_ip of the message for rts
71         int     shutdown;                               // thread notification if we need to tell them to stop
72         int max_mlen;                           // max message length payload+header
73         int     max_plen;                               // max payload length
74         int     flags;                                  // CTXFL_ constants
75         int nrtele;                                     // number of elements in the routing table
76         int send_retries;                       // number of retries send_msg() should attempt if eagain/timeout indicated by nng
77         int     trace_data_len;                 // number of bytes to allocate in header for trace data
78         int d1_len;                                     // extra header data 1 length
79         int d2_len;                                     // extra header data 2 length   (future)
80         nng_socket      nn_sock;                // our general listen socket
81         route_table_t* rtable;          // the active route table
82         route_table_t* old_rtable;      // the previously used rt, sits here to allow for draining
83         route_table_t* new_rtable;      // route table under construction
84         if_addrs_t*     ip_list;                // list manager of the IP addresses that are on our known interfaces
85         void*   mring;                          // ring where msgs are queued while waiting for a call response msg
86         chute_t*        chutes;
87
88         char*   rtg_addr;                       // addr/port of the route table generation publisher
89         int             rtg_port;                       // the port that the rtg listens on
90
91         wh_mgt_t*       wormholes;              // management of user opened wormholes
92         epoll_stuff_t*  eps;            // epoll information needed for the rcv with timeout call
93
94         pthread_t       rtc_th;                 // thread info for the rtc listener
95         pthread_t       mtc_th;                 // thread info for the multi-thread call receive process
96 };
97
98
99
100 /*
101         Static prototypes for functions located here. All common protos are in the
102         agnostic header file.
103 */
104
105 // --- initialisation and housekeeping -------
106 static void* init(  char* uproto_port, int max_msg_size, int flags );
107 static void free_ctx( uta_ctx_t* ctx );
108
109 // --- rt table things ---------------------------
110 static int uta_link2( endpoint_t* ep );
111 static int rt_link2_ep( void* vctx,  endpoint_t* ep );
112 static int uta_epsock_byname( route_table_t* rt, char* ep_name, nng_socket* nn_sock, endpoint_t** uepp );
113 static int uta_epsock_rr( rtable_ent_t* rte, int group, int* more, nng_socket* nn_sock, endpoint_t** uepp );
114 static rtable_ent_t* uta_get_rte( route_table_t *rt, int sid, int mtype, int try_alt );
115 static inline int xlate_nng_state( int state, int def_state );
116
117
118 // --- msg ---------------------------------------
119 static rmr_mbuf_t* alloc_zcmsg( uta_ctx_t* ctx, rmr_mbuf_t* msg, int size, int state, int trlo );
120 static rmr_mbuf_t* alloc_mbuf( uta_ctx_t* ctx, int state );
121 static void ref_tpbuf( rmr_mbuf_t* msg, size_t alen ) ;
122 static inline rmr_mbuf_t* clone_msg( rmr_mbuf_t* old_msg  );
123 static rmr_mbuf_t* rcv_msg( uta_ctx_t* ctx, rmr_mbuf_t* old_msg );
124 static void* rcv_payload( uta_ctx_t* ctx, rmr_mbuf_t* old_msg );
125 static inline rmr_mbuf_t* realloc_msg( rmr_mbuf_t* old_msg, int tr_len  );
126 static rmr_mbuf_t* send_msg( uta_ctx_t* ctx, rmr_mbuf_t* msg, nng_socket nn_sock, int retries );
127 static rmr_mbuf_t* send2ep( uta_ctx_t* ctx, endpoint_t* ep, rmr_mbuf_t* msg );
128 static rmr_mbuf_t* realloc_payload( rmr_mbuf_t* mbuf, int new_len, int copy, int clone );
129
130
131
132 #endif