vcl: add read/write udp support
[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 #define STATE_OPEN (SERVER_STATE_OPEN | CLIENT_STATE_OPEN)
62
63 typedef struct epoll_event vppcom_epoll_event_t;
64
65 typedef struct
66 {
67   u32 next_sid;
68   u32 prev_sid;
69   u32 vep_idx;
70   vppcom_epoll_event_t ev;
71 #define VEP_DEFAULT_ET_MASK  (EPOLLIN|EPOLLOUT)
72 #define VEP_UNSUPPORTED_EVENTS (EPOLLONESHOT|EPOLLEXCLUSIVE)
73   u32 et_mask;
74 } vppcom_epoll_t;
75
76 typedef struct
77 {
78   u8 is_ip4;
79   ip46_address_t ip46;
80 } vppcom_ip46_t;
81
82 typedef struct vcl_session_msg
83 {
84   u32 next;
85   union
86   {
87     session_accepted_msg_t accepted_msg;
88   };
89 } vcl_session_msg_t;
90
91 enum
92 {
93   VCL_SESS_ATTR_SERVER,
94   VCL_SESS_ATTR_CUT_THRU,
95   VCL_SESS_ATTR_VEP,
96   VCL_SESS_ATTR_VEP_SESSION,
97   VCL_SESS_ATTR_LISTEN,         // SOL_SOCKET,SO_ACCEPTCONN
98   VCL_SESS_ATTR_NONBLOCK,       // fcntl,O_NONBLOCK
99   VCL_SESS_ATTR_REUSEADDR,      // SOL_SOCKET,SO_REUSEADDR
100   VCL_SESS_ATTR_REUSEPORT,      // SOL_SOCKET,SO_REUSEPORT
101   VCL_SESS_ATTR_BROADCAST,      // SOL_SOCKET,SO_BROADCAST
102   VCL_SESS_ATTR_V6ONLY,         // SOL_TCP,IPV6_V6ONLY
103   VCL_SESS_ATTR_KEEPALIVE,      // SOL_SOCKET,SO_KEEPALIVE
104   VCL_SESS_ATTR_TCP_NODELAY,    // SOL_TCP,TCP_NODELAY
105   VCL_SESS_ATTR_TCP_KEEPIDLE,   // SOL_TCP,TCP_KEEPIDLE
106   VCL_SESS_ATTR_TCP_KEEPINTVL,  // SOL_TCP,TCP_KEEPINTVL
107   VCL_SESS_ATTR_MAX
108 } vppcom_session_attr_t;
109
110 #define VCL_SESS_ATTR_SET(ATTR, VAL)            \
111 do {                                            \
112   (ATTR) |= 1 << (VAL);                         \
113  } while (0)
114
115 #define VCL_SESS_ATTR_CLR(ATTR, VAL)            \
116 do {                                            \
117   (ATTR) &= ~(1 << (VAL));                      \
118  } while (0)
119
120 #define VCL_SESS_ATTR_TEST(ATTR, VAL)           \
121   ((ATTR) & (1 << (VAL)) ? 1 : 0)
122
123 typedef struct
124 {
125 #define _(type, name) type name;
126   foreach_app_session_field
127 #undef _
128   u32 sndbuf_size;              // VPP-TBD: Hack until support setsockopt(SO_SNDBUF)
129   u32 rcvbuf_size;              // VPP-TBD: Hack until support setsockopt(SO_RCVBUF)
130   u32 user_mss;                 // VPP-TBD: Hack until support setsockopt(TCP_MAXSEG)
131   u8 *segment_name;
132   u32 sm_seg_index;
133   u32 client_context;
134   u64 vpp_handle;
135
136   /* Socket configuration state */
137   u8 is_vep;
138   u8 is_vep_session;
139   u32 attr;
140   u32 wait_cont_idx;
141   vppcom_epoll_t vep;
142   int libc_epfd;
143   svm_msg_q_t *our_evt_q;
144   u32 ct_registration;
145   u64 options[16];
146   vce_event_handler_reg_t *poll_reg;
147   vcl_session_msg_t *accept_evts_fifo;
148 #if VCL_ELOG
149   elog_track_t elog_track;
150 #endif
151 } vcl_session_t;
152
153 typedef struct vppcom_cfg_t_
154 {
155   u64 heapsize;
156   u32 vpp_api_q_length;
157   u64 segment_baseva;
158   u32 segment_size;
159   u32 add_segment_size;
160   u32 preallocated_fifo_pairs;
161   u32 rx_fifo_size;
162   u32 tx_fifo_size;
163   u32 event_queue_size;
164   u32 listen_queue_size;
165   u8 app_proxy_transport_tcp;
166   u8 app_proxy_transport_udp;
167   u8 app_scope_local;
168   u8 app_scope_global;
169   u8 *namespace_id;
170   u64 namespace_secret;
171   f64 app_timeout;
172   f64 session_timeout;
173   f64 accept_timeout;
174   u32 event_ring_size;
175   char *event_log_path;
176   u8 *vpp_api_filename;
177 } vppcom_cfg_t;
178
179 void vppcom_cfg (vppcom_cfg_t * vcl_cfg);
180
181 typedef struct vcl_cut_through_registration_
182 {
183   svm_msg_q_t *mq;
184   u32 sid;
185 } vcl_cut_through_registration_t;
186
187 typedef struct vppcom_main_t_
188 {
189   u8 init;
190   u32 debug;
191   int main_cpu;
192
193   /* FIFO for accepted connections - used in epoll/select */
194   clib_spinlock_t session_fifo_lockp;
195   u32 *client_session_index_fifo;
196
197   /* vpp input queue */
198   svm_queue_t *vl_input_queue;
199
200   /* API client handle */
201   u32 my_client_index;
202   /* Session pool */
203   clib_spinlock_t sessions_lockp;
204   vcl_session_t *sessions;
205
206   /* Hash table for disconnect processing */
207   uword *session_index_by_vpp_handles;
208
209   /* Select bitmaps */
210   clib_bitmap_t *rd_bitmap;
211   clib_bitmap_t *wr_bitmap;
212   clib_bitmap_t *ex_bitmap;
213
214   /* Our event queue */
215   svm_msg_q_t *app_event_queue;
216
217   /* unique segment name counter */
218   u32 unique_segment_index;
219
220   /* For deadman timers */
221   clib_time_t clib_time;
222
223   /* State of the connection, shared between msg RX thread and main thread */
224   volatile app_state_t app_state;
225
226   vppcom_cfg_t cfg;
227
228   /* Event thread */
229   vce_event_thread_t event_thread;
230
231   /* IO thread */
232   vppcom_session_io_thread_t session_io_thread;
233
234   /* pool of ctrl msgs */
235   vcl_session_msg_t *ctrl_evt_pool;
236
237   /** Pool of cut through registrations */
238   vcl_cut_through_registration_t *cut_through_registrations;
239
240   /** Flag indicating that a new segment is being mounted */
241   volatile u32 mounting_segment;
242
243 #ifdef VCL_ELOG
244   /* VPP Event-logger */
245   elog_main_t elog_main;
246   elog_track_t elog_track;
247 #endif
248
249   /* VNET_API_ERROR_FOO -> "Foo" hash table */
250   uword *error_string_by_error_number;
251 } vppcom_main_t;
252
253 extern vppcom_main_t *vcm;
254
255 #define VCL_SESSION_LOCK_AND_GET(I, S)                          \
256 do {                                                            \
257   clib_spinlock_lock (&vcm->sessions_lockp);                    \
258   rv = vppcom_session_at_index (I, S);                          \
259   if (PREDICT_FALSE (rv))                                       \
260     {                                                           \
261       clib_spinlock_unlock (&vcm->sessions_lockp);              \
262       clib_warning ("VCL<%d>: ERROR: Invalid ##I (%u)!",        \
263                     getpid (), I);                              \
264       goto done;                                                \
265     }                                                           \
266 } while (0)
267
268 #define VCL_SESSION_LOCK() clib_spinlock_lock (&(vcm->sessions_lockp))
269 #define VCL_SESSION_UNLOCK() clib_spinlock_unlock (&(vcm->sessions_lockp))
270
271 #define VCL_IO_SESSIONS_LOCK() \
272   clib_spinlock_lock (&(vcm->session_io_thread.io_sessions_lockp))
273 #define VCL_IO_SESSIONS_UNLOCK() \
274   clib_spinlock_unlock (&(vcm->session_io_thread.io_sessions_lockp))
275
276 #define VCL_ACCEPT_FIFO_LOCK() clib_spinlock_lock (&(vcm->session_fifo_lockp))
277 #define VCL_ACCEPT_FIFO_UNLOCK() \
278   clib_spinlock_unlock (&(vcm->session_fifo_lockp))
279
280 #define VCL_EVENTS_LOCK() \
281   clib_spinlock_lock (&(vcm->event_thread.events_lockp))
282 #define VCL_EVENTS_UNLOCK() \
283   clib_spinlock_unlock (&(vcm->event_thread.events_lockp))
284
285 #define VCL_INVALID_SESSION_INDEX ((u32)~0)
286
287 static inline vcl_session_t *
288 vcl_session_get (u32 session_index)
289 {
290   if (pool_is_free_index (vcm->sessions, session_index))
291     return 0;
292   return pool_elt_at_index (vcm->sessions, session_index);
293 }
294
295 static inline u32
296 vcl_session_index (vcl_session_t * s)
297 {
298   return (s - vcm->sessions);
299 }
300
301 static inline vcl_session_t *
302 vcl_session_get_w_handle (u64 handle)
303 {
304   uword *p;
305   if ((p = hash_get (vcm->session_index_by_vpp_handles, handle)))
306     return vcl_session_get ((u32) p[0]);
307   return 0;
308 }
309
310 static inline u32
311 vcl_session_get_index_from_handle (u64 handle)
312 {
313   uword *p;
314   if ((p = hash_get (vcm->session_index_by_vpp_handles, handle)))
315     return p[0];
316   return VCL_INVALID_SESSION_INDEX;
317 }
318
319 static inline u8
320 vcl_session_is_ct (vcl_session_t * s)
321 {
322   return (s->our_evt_q != 0);
323 }
324
325 static inline int
326 vppcom_session_at_index (u32 session_index, vcl_session_t * volatile *sess)
327 {
328   /* Assumes that caller has acquired spinlock: vcm->sessions_lockp */
329   if (PREDICT_FALSE ((session_index == ~0) ||
330                      pool_is_free_index (vcm->sessions, session_index)))
331     {
332       clib_warning ("VCL<%d>: invalid session, sid (%u) has been closed!",
333                     getpid (), session_index);
334       return VPPCOM_EBADFD;
335     }
336   *sess = pool_elt_at_index (vcm->sessions, session_index);
337   return VPPCOM_OK;
338 }
339
340 static inline void
341 vppcom_session_table_add_listener (u64 listener_handle, u32 value)
342 {
343   /* Session and listener handles have different formats. The latter has
344    * the thread index in the upper 32 bits while the former has the session
345    * type. Knowing that, for listeners we just flip the MSB to 1 */
346   listener_handle |= 1ULL << 63;
347   hash_set (vcm->session_index_by_vpp_handles, listener_handle, value);
348 }
349
350 static inline vcl_session_t *
351 vppcom_session_table_lookup_listener (u64 listener_handle)
352 {
353   uword *p;
354   u64 handle = listener_handle | (1ULL << 63);
355   vcl_session_t *session;
356
357   p = hash_get (vcm->session_index_by_vpp_handles, handle);
358   if (!p)
359     {
360       clib_warning ("VCL<%d>: couldn't find listen session: unknown vpp "
361                     "listener handle %llx", getpid (), listener_handle);
362       return 0;
363     }
364   if (pool_is_free_index (vcm->sessions, p[0]))
365     {
366       VDBG (1, "VCL<%d>: invalid listen session, sid (%u)", getpid (), p[0]);
367       return 0;
368     }
369
370   session = pool_elt_at_index (vcm->sessions, p[0]);
371   ASSERT (session->session_state & STATE_LISTEN);
372   return session;
373 }
374
375 const char *vppcom_session_state_str (session_state_t state);
376
377 /*
378  * VCL Binary API
379  */
380 int vppcom_connect_to_vpp (char *app_name);
381 void vppcom_init_error_string_table (void);
382 void vppcom_send_session_enable_disable (u8 is_enable);
383 void vppcom_app_send_attach (void);
384 void vppcom_app_send_detach (void);
385 void vppcom_send_connect_sock (vcl_session_t * session, u32 session_index);
386 void vppcom_send_disconnect_session_reply (u64 vpp_handle, u32 session_index,
387                                            int rv);
388 void vppcom_send_disconnect_session (u64 vpp_handle, u32 session_index);
389 void vppcom_send_bind_sock (vcl_session_t * session, u32 session_index);
390 void vppcom_send_unbind_sock (u64 vpp_handle);
391 void vppcom_api_hookup (void);
392 void vppcom_send_accept_session_reply (u64 handle, u32 context, int retval);
393
394 u32 vcl_max_nsid_len (void);
395
396 u8 *format_api_error (u8 * s, va_list * args);
397
398 #endif /* SRC_VCL_VCL_PRIVATE_H_ */
399
400 /*
401  * fd.io coding-style-patch-verification: ON
402  *
403  * Local Variables:
404  * eval: (c-set-style "gnu")
405  * End:
406  */