vcl: move binary api and cfg to separate files
[vpp.git] / src / vcl / vcl_private.h
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
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 #ifndef SRC_VCL_VCL_PRIVATE_H_
17 #define SRC_VCL_VCL_PRIVATE_H_
18
19 #include <vnet/session/application_interface.h>
20 #include <vcl/vppcom.h>
21 #include <vcl/vcl_event.h>
22 #include <vcl/vcl_debug.h>
23
24 #if (CLIB_DEBUG > 0)
25 /* Set VPPCOM_DEBUG_INIT 2 for connection debug,
26  *                       3 for read/write debug output
27  * or
28  *    export VCL_DEBUG=<#> to set dynamically.
29  */
30 #define VPPCOM_DEBUG_INIT 1
31 #else
32 #define VPPCOM_DEBUG_INIT 0
33 #endif
34
35 #define VPPCOM_DEBUG vcm->debug
36
37 /*
38  * VPPCOM Private definitions and functions.
39  */
40 typedef enum
41 {
42   STATE_APP_START,
43   STATE_APP_CONN_VPP,
44   STATE_APP_ENABLED,
45   STATE_APP_ATTACHED,
46 } app_state_t;
47
48 typedef enum
49 {
50   STATE_START = 0x01,
51   STATE_CONNECT = 0x02,
52   STATE_LISTEN = 0x04,
53   STATE_ACCEPT = 0x08,
54   STATE_CLOSE_ON_EMPTY = 0x10,
55   STATE_DISCONNECT = 0x20,
56   STATE_FAILED = 0x40
57 } session_state_t;
58
59 #define SERVER_STATE_OPEN  (STATE_ACCEPT|STATE_CLOSE_ON_EMPTY)
60 #define CLIENT_STATE_OPEN  (STATE_CONNECT|STATE_CLOSE_ON_EMPTY)
61
62 typedef struct epoll_event vppcom_epoll_event_t;
63
64 typedef struct
65 {
66   u32 next_sid;
67   u32 prev_sid;
68   u32 vep_idx;
69   vppcom_epoll_event_t ev;
70 #define VEP_DEFAULT_ET_MASK  (EPOLLIN|EPOLLOUT)
71 #define VEP_UNSUPPORTED_EVENTS (EPOLLONESHOT|EPOLLEXCLUSIVE)
72   u32 et_mask;
73 } vppcom_epoll_t;
74
75 typedef struct
76 {
77   u8 is_ip4;
78   ip46_address_t ip46;
79 } vppcom_ip46_t;
80
81 enum
82 {
83   VCL_SESS_ATTR_SERVER,
84   VCL_SESS_ATTR_CUT_THRU,
85   VCL_SESS_ATTR_VEP,
86   VCL_SESS_ATTR_VEP_SESSION,
87   VCL_SESS_ATTR_LISTEN,         // SOL_SOCKET,SO_ACCEPTCONN
88   VCL_SESS_ATTR_NONBLOCK,       // fcntl,O_NONBLOCK
89   VCL_SESS_ATTR_REUSEADDR,      // SOL_SOCKET,SO_REUSEADDR
90   VCL_SESS_ATTR_REUSEPORT,      // SOL_SOCKET,SO_REUSEPORT
91   VCL_SESS_ATTR_BROADCAST,      // SOL_SOCKET,SO_BROADCAST
92   VCL_SESS_ATTR_V6ONLY,         // SOL_TCP,IPV6_V6ONLY
93   VCL_SESS_ATTR_KEEPALIVE,      // SOL_SOCKET,SO_KEEPALIVE
94   VCL_SESS_ATTR_TCP_NODELAY,    // SOL_TCP,TCP_NODELAY
95   VCL_SESS_ATTR_TCP_KEEPIDLE,   // SOL_TCP,TCP_KEEPIDLE
96   VCL_SESS_ATTR_TCP_KEEPINTVL,  // SOL_TCP,TCP_KEEPINTVL
97   VCL_SESS_ATTR_MAX
98 } vppcom_session_attr_t;
99
100 #define VCL_SESS_ATTR_SET(ATTR, VAL)            \
101 do {                                            \
102   (ATTR) |= 1 << (VAL);                         \
103  } while (0)
104
105 #define VCL_SESS_ATTR_CLR(ATTR, VAL)            \
106 do {                                            \
107   (ATTR) &= ~(1 << (VAL));                      \
108  } while (0)
109
110 #define VCL_SESS_ATTR_TEST(ATTR, VAL)           \
111   ((ATTR) & (1 << (VAL)) ? 1 : 0)
112
113 typedef struct
114 {
115 #define _(type, name) type name;
116   foreach_app_session_field
117 #undef _
118   u32 sndbuf_size;              // VPP-TBD: Hack until support setsockopt(SO_SNDBUF)
119   u32 rcvbuf_size;              // VPP-TBD: Hack until support setsockopt(SO_RCVBUF)
120   u32 user_mss;                 // VPP-TBD: Hack until support setsockopt(TCP_MAXSEG)
121   u8 *segment_name;
122   u32 sm_seg_index;
123   u32 client_context;
124   u64 vpp_handle;
125
126   /* Socket configuration state */
127   u8 is_vep;
128   u8 is_vep_session;
129   u32 attr;
130   u32 wait_cont_idx;
131   vppcom_epoll_t vep;
132   int libc_epfd;
133   u64 client_queue_address;
134   u64 options[16];
135   vce_event_handler_reg_t *poll_reg;
136 #if VCL_ELOG
137   elog_track_t elog_track;
138 #endif
139 } vcl_session_t;
140
141 typedef struct vppcom_cfg_t_
142 {
143   u64 heapsize;
144   u32 vpp_api_q_length;
145   u64 segment_baseva;
146   u32 segment_size;
147   u32 add_segment_size;
148   u32 preallocated_fifo_pairs;
149   u32 rx_fifo_size;
150   u32 tx_fifo_size;
151   u32 event_queue_size;
152   u32 listen_queue_size;
153   u8 app_proxy_transport_tcp;
154   u8 app_proxy_transport_udp;
155   u8 app_scope_local;
156   u8 app_scope_global;
157   u8 *namespace_id;
158   u64 namespace_secret;
159   f64 app_timeout;
160   f64 session_timeout;
161   f64 accept_timeout;
162   u32 event_ring_size;
163   char *event_log_path;
164   u8 *vpp_api_filename;
165 } vppcom_cfg_t;
166
167 void vppcom_cfg (vppcom_cfg_t * vcl_cfg);
168
169 typedef struct vppcom_main_t_
170 {
171   u8 init;
172   u32 debug;
173   int main_cpu;
174
175   /* FIFO for accepted connections - used in epoll/select */
176   clib_spinlock_t session_fifo_lockp;
177   u32 *client_session_index_fifo;
178
179   /* vpp input queue */
180   svm_queue_t *vl_input_queue;
181
182   /* API client handle */
183   u32 my_client_index;
184   /* Session pool */
185   clib_spinlock_t sessions_lockp;
186   vcl_session_t *sessions;
187
188   /* Hash table for disconnect processing */
189   uword *session_index_by_vpp_handles;
190
191   /* Select bitmaps */
192   clib_bitmap_t *rd_bitmap;
193   clib_bitmap_t *wr_bitmap;
194   clib_bitmap_t *ex_bitmap;
195
196   /* Our event queue */
197   svm_queue_t *app_event_queue;
198
199   /* unique segment name counter */
200   u32 unique_segment_index;
201
202   /* For deadman timers */
203   clib_time_t clib_time;
204
205   /* State of the connection, shared between msg RX thread and main thread */
206   volatile app_state_t app_state;
207
208   vppcom_cfg_t cfg;
209
210   /* Event thread */
211   vce_event_thread_t event_thread;
212
213   /* IO thread */
214   vppcom_session_io_thread_t session_io_thread;
215
216 #ifdef VCL_ELOG
217   /* VPP Event-logger */
218   elog_main_t elog_main;
219   elog_track_t elog_track;
220 #endif
221
222   /* VNET_API_ERROR_FOO -> "Foo" hash table */
223   uword *error_string_by_error_number;
224 } vppcom_main_t;
225
226 extern vppcom_main_t *vcm;
227
228 #define VCL_SESSION_LOCK_AND_GET(I, S)                          \
229 do {                                                            \
230   clib_spinlock_lock (&vcm->sessions_lockp);                    \
231   rv = vppcom_session_at_index (I, S);                          \
232   if (PREDICT_FALSE (rv))                                       \
233     {                                                           \
234       clib_spinlock_unlock (&vcm->sessions_lockp);              \
235       clib_warning ("VCL<%d>: ERROR: Invalid ##I (%u)!",        \
236                     getpid (), I);                              \
237       goto done;                                                \
238     }                                                           \
239 } while (0)
240
241 #define VCL_SESSION_LOCK() clib_spinlock_lock (&(vcm->sessions_lockp))
242 #define VCL_SESSION_UNLOCK() clib_spinlock_unlock (&(vcm->sessions_lockp))
243
244 #define VCL_IO_SESSIONS_LOCK() \
245   clib_spinlock_lock (&(vcm->session_io_thread.io_sessions_lockp))
246 #define VCL_IO_SESSIONS_UNLOCK() \
247   clib_spinlock_unlock (&(vcm->session_io_thread.io_sessions_lockp))
248
249 #define VCL_ACCEPT_FIFO_LOCK() clib_spinlock_lock (&(vcm->session_fifo_lockp))
250 #define VCL_ACCEPT_FIFO_UNLOCK() \
251   clib_spinlock_unlock (&(vcm->session_fifo_lockp))
252
253 #define VCL_EVENTS_LOCK() \
254   clib_spinlock_lock (&(vcm->event_thread.events_lockp))
255 #define VCL_EVENTS_UNLOCK() \
256   clib_spinlock_unlock (&(vcm->event_thread.events_lockp))
257
258 static inline int
259 vppcom_session_at_index (u32 session_index, vcl_session_t * volatile *sess)
260 {
261   /* Assumes that caller has acquired spinlock: vcm->sessions_lockp */
262   if (PREDICT_FALSE ((session_index == ~0) ||
263                      pool_is_free_index (vcm->sessions, session_index)))
264     {
265       clib_warning ("VCL<%d>: invalid session, sid (%u) has been closed!",
266                     getpid (), session_index);
267       return VPPCOM_EBADFD;
268     }
269   *sess = pool_elt_at_index (vcm->sessions, session_index);
270   return VPPCOM_OK;
271 }
272
273 static inline void
274 vppcom_session_table_add_listener (u64 listener_handle, u32 value)
275 {
276   /* Session and listener handles have different formats. The latter has
277    * the thread index in the upper 32 bits while the former has the session
278    * type. Knowing that, for listeners we just flip the MSB to 1 */
279   listener_handle |= 1ULL << 63;
280   hash_set (vcm->session_index_by_vpp_handles, listener_handle, value);
281 }
282
283 static inline vcl_session_t *
284 vppcom_session_table_lookup_listener (u64 listener_handle)
285 {
286   uword *p;
287   u64 handle = listener_handle | (1ULL << 63);
288   vcl_session_t *session;
289
290   p = hash_get (vcm->session_index_by_vpp_handles, handle);
291   if (!p)
292     {
293       clib_warning ("VCL<%d>: couldn't find listen session: unknown vpp "
294                     "listener handle %llx", getpid (), listener_handle);
295       return 0;
296     }
297   if (pool_is_free_index (vcm->sessions, p[0]))
298     {
299       VDBG (1, "VCL<%d>: invalid listen session, sid (%u)", getpid (), p[0]);
300       return 0;
301     }
302
303   session = pool_elt_at_index (vcm->sessions, p[0]);
304   ASSERT (session->session_state & STATE_LISTEN);
305   return session;
306 }
307
308 const char *vppcom_session_state_str (session_state_t state);
309
310 /*
311  * VCL Binary API
312  */
313 int vppcom_connect_to_vpp (char *app_name);
314 void vppcom_init_error_string_table (void);
315 void vppcom_send_session_enable_disable (u8 is_enable);
316 void vppcom_app_send_attach (void);
317 void vppcom_app_send_detach (void);
318 void vppcom_send_connect_sock (vcl_session_t * session, u32 session_index);
319 void vppcom_send_disconnect_session_reply (u64 vpp_handle, u32 session_index,
320                                            int rv);
321 void vppcom_send_disconnect_session (u64 vpp_handle, u32 session_index);
322 void vppcom_send_bind_sock (vcl_session_t * session, u32 session_index);
323 void vppcom_send_unbind_sock (u64 vpp_handle);
324 void vppcom_api_hookup (void);
325 void vppcom_send_accept_session_reply (u64 handle, u32 context, int retval);
326
327 u32 vcl_max_nsid_len (void);
328
329 #endif /* SRC_VCL_VCL_PRIVATE_H_ */
330
331 /*
332  * fd.io coding-style-patch-verification: ON
333  *
334  * Local Variables:
335  * eval: (c-set-style "gnu")
336  * End:
337  */