vcl/session: send unlisten over message queue
[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_debug.h>
22
23 #if (CLIB_DEBUG > 0)
24 /* Set VPPCOM_DEBUG_INIT 2 for connection debug,
25  *                       3 for read/write debug output
26  * or
27  *    export VCL_DEBUG=<#> to set dynamically.
28  */
29 #define VPPCOM_DEBUG_INIT 1
30 #else
31 #define VPPCOM_DEBUG_INIT 0
32 #endif
33
34 #define VPPCOM_DEBUG vcm->debug
35
36 extern __thread uword __vcl_worker_index;
37
38 static inline void
39 vcl_set_worker_index (uword wrk_index)
40 {
41   __vcl_worker_index = wrk_index;
42 }
43
44 static inline uword
45 vcl_get_worker_index (void)
46 {
47   return __vcl_worker_index;
48 }
49
50 /*
51  * VPPCOM Private definitions and functions.
52  */
53 typedef enum
54 {
55   STATE_APP_START,
56   STATE_APP_CONN_VPP,
57   STATE_APP_ENABLED,
58   STATE_APP_ATTACHED,
59   STATE_APP_ADDING_WORKER,
60   STATE_APP_FAILED,
61   STATE_APP_READY
62 } app_state_t;
63
64 typedef enum
65 {
66   STATE_START = 0,
67   STATE_CONNECT = 0x01,
68   STATE_LISTEN = 0x02,
69   STATE_ACCEPT = 0x04,
70   STATE_VPP_CLOSING = 0x08,
71   STATE_DISCONNECT = 0x10,
72   STATE_FAILED = 0x20,
73   STATE_UPDATED = 0x40,
74   STATE_LISTEN_NO_MQ = 0x80,
75 } vcl_session_state_t;
76
77 #define SERVER_STATE_OPEN  (STATE_ACCEPT|STATE_VPP_CLOSING)
78 #define CLIENT_STATE_OPEN  (STATE_CONNECT|STATE_VPP_CLOSING)
79 #define STATE_OPEN (SERVER_STATE_OPEN | CLIENT_STATE_OPEN)
80
81 typedef struct epoll_event vppcom_epoll_event_t;
82
83 typedef struct
84 {
85   u32 next_sh;
86   u32 prev_sh;
87   u32 vep_sh;
88   vppcom_epoll_event_t ev;
89 #define VEP_DEFAULT_ET_MASK  (EPOLLIN|EPOLLOUT)
90 #define VEP_UNSUPPORTED_EVENTS (EPOLLONESHOT|EPOLLEXCLUSIVE)
91   u32 et_mask;
92 } vppcom_epoll_t;
93
94 /* Select uses the vcl_si_set as if a clib_bitmap. Make sure they are the
95  * same size */
96 STATIC_ASSERT (sizeof (clib_bitmap_t) == sizeof (vcl_si_set),
97                "vppcom bitmap size mismatch");
98
99 typedef struct
100 {
101   u8 is_ip4;
102   ip46_address_t ip46;
103 } vppcom_ip46_t;
104
105 #define VCL_ACCEPTED_F_CLOSED   (1 << 0)
106 #define VCL_ACCEPTED_F_RESET    (1 << 1)
107
108 typedef struct vcl_session_msg
109 {
110   u32 next;
111   union
112   {
113     session_accepted_msg_t accepted_msg;
114   };
115   u32 flags;
116 } vcl_session_msg_t;
117
118 typedef enum
119 {
120   VCL_SESS_ATTR_SERVER,
121   VCL_SESS_ATTR_CUT_THRU,
122   VCL_SESS_ATTR_VEP,
123   VCL_SESS_ATTR_VEP_SESSION,
124   VCL_SESS_ATTR_LISTEN,         // SOL_SOCKET,SO_ACCEPTCONN
125   VCL_SESS_ATTR_NONBLOCK,       // fcntl,O_NONBLOCK
126   VCL_SESS_ATTR_REUSEADDR,      // SOL_SOCKET,SO_REUSEADDR
127   VCL_SESS_ATTR_REUSEPORT,      // SOL_SOCKET,SO_REUSEPORT
128   VCL_SESS_ATTR_BROADCAST,      // SOL_SOCKET,SO_BROADCAST
129   VCL_SESS_ATTR_V6ONLY,         // SOL_TCP,IPV6_V6ONLY
130   VCL_SESS_ATTR_KEEPALIVE,      // SOL_SOCKET,SO_KEEPALIVE
131   VCL_SESS_ATTR_TCP_NODELAY,    // SOL_TCP,TCP_NODELAY
132   VCL_SESS_ATTR_TCP_KEEPIDLE,   // SOL_TCP,TCP_KEEPIDLE
133   VCL_SESS_ATTR_TCP_KEEPINTVL,  // SOL_TCP,TCP_KEEPINTVL
134   VCL_SESS_ATTR_SHUT_RD,
135   VCL_SESS_ATTR_SHUT_WR,
136   VCL_SESS_ATTR_MAX
137 } vppcom_session_attr_t;
138
139 #define VCL_SESS_ATTR_SET(ATTR, VAL)            \
140 do {                                            \
141   (ATTR) |= 1 << (VAL);                         \
142  } while (0)
143
144 #define VCL_SESS_ATTR_CLR(ATTR, VAL)            \
145 do {                                            \
146   (ATTR) &= ~(1 << (VAL));                      \
147  } while (0)
148
149 #define VCL_SESS_ATTR_TEST(ATTR, VAL)           \
150   ((ATTR) & (1 << (VAL)) ? 1 : 0)
151
152 typedef struct vcl_shared_session_
153 {
154   u32 ss_index;
155   u32 *workers;
156   u32 session_index;
157 } vcl_shared_session_t;
158
159 typedef struct
160 {
161   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
162 #define _(type, name) type name;
163   foreach_app_session_field
164 #undef _
165   u32 sndbuf_size;              // VPP-TBD: Hack until support setsockopt(SO_SNDBUF)
166   u32 rcvbuf_size;              // VPP-TBD: Hack until support setsockopt(SO_RCVBUF)
167   u32 user_mss;                 // VPP-TBD: Hack until support setsockopt(TCP_MAXSEG)
168   u8 *segment_name;
169   u32 sm_seg_index;
170   u32 client_context;
171   u64 vpp_handle;
172   u32 vpp_thread_index;
173
174   /* Socket configuration state */
175   u8 is_vep;
176   u8 is_vep_session;
177   u8 has_rx_evt;
178   u32 attr;
179   u32 wait_cont_idx;
180   vppcom_epoll_t vep;
181   int libc_epfd;
182   svm_msg_q_t *our_evt_q;
183   u64 options[16];
184   vcl_session_msg_t *accept_evts_fifo;
185 #if VCL_ELOG
186   elog_track_t elog_track;
187 #endif
188 } vcl_session_t;
189
190 typedef struct vppcom_cfg_t_
191 {
192   uword heapsize;
193   u32 max_workers;
194   u32 vpp_api_q_length;
195   uword segment_baseva;
196   u32 segment_size;
197   u32 add_segment_size;
198   u32 preallocated_fifo_pairs;
199   u32 rx_fifo_size;
200   u32 tx_fifo_size;
201   u32 event_queue_size;
202   u32 listen_queue_size;
203   u8 app_proxy_transport_tcp;
204   u8 app_proxy_transport_udp;
205   u8 app_scope_local;
206   u8 app_scope_global;
207   u8 *namespace_id;
208   u64 namespace_secret;
209   u8 use_mq_eventfd;
210   f64 app_timeout;
211   f64 session_timeout;
212   f64 accept_timeout;
213   u32 event_ring_size;
214   char *event_log_path;
215   u8 *vpp_api_filename;
216   u8 *vpp_api_socket_name;
217 } vppcom_cfg_t;
218
219 void vppcom_cfg (vppcom_cfg_t * vcl_cfg);
220
221 typedef struct vcl_cut_through_registration_
222 {
223   svm_msg_q_t *mq;
224   svm_msg_q_t *peer_mq;
225   u32 sid;
226   u32 epoll_evt_conn_index;     /*< mq evt connection index part of
227                                    the mqs evtfd epoll (if used) */
228 } vcl_cut_through_registration_t;
229
230 typedef struct vcl_mq_evt_conn_
231 {
232   svm_msg_q_t *mq;
233   int mq_fd;
234 } vcl_mq_evt_conn_t;
235
236 typedef struct vcl_worker_
237 {
238   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
239
240   /* Session pool */
241   vcl_session_t *sessions;
242
243   /** Worker/thread index in current process */
244   u32 wrk_index;
245
246   /** Worker index in vpp*/
247   u32 vpp_wrk_index;
248
249   /** API client handle */
250   u32 my_client_index;
251
252   /** State of the connection, shared between msg RX thread and main thread */
253   volatile app_state_t wrk_state;
254
255   /** VPP binary api input queue */
256   svm_queue_t *vl_input_queue;
257
258   /** Message queues epoll fd. Initialized only if using mqs with eventfds */
259   int mqs_epfd;
260
261   /** Pool of event message queue event connections */
262   vcl_mq_evt_conn_t *mq_evt_conns;
263
264   /** Per worker buffer for receiving mq epoll events */
265   struct epoll_event *mq_events;
266
267   /** Hash table for disconnect processing */
268   uword *session_index_by_vpp_handles;
269
270   /** Select bitmaps */
271   clib_bitmap_t *rd_bitmap;
272   clib_bitmap_t *wr_bitmap;
273   clib_bitmap_t *ex_bitmap;
274
275   /** Our event message queue */
276   svm_msg_q_t *app_event_queue;
277
278   /** VPP workers event message queues */
279   svm_msg_q_t **vpp_event_queues;
280
281   /** For deadman timers */
282   clib_time_t clib_time;
283
284   /** Pool of cut through registrations */
285   vcl_cut_through_registration_t *cut_through_registrations;
286
287   /** Lock for accessing ct registration pool */
288   clib_spinlock_t ct_registration_lock;
289
290   /** Cut-through registration by mq address hash table */
291   uword *ct_registration_by_mq;
292
293   /** Vector acting as buffer for mq messages */
294   svm_msg_q_msg_t *mq_msg_vector;
295
296   /** Vector of unhandled events */
297   session_event_t *unhandled_evts_vector;
298
299   u32 *pending_session_wrk_updates;
300
301   /** Used also as a thread stop key buffer */
302   pthread_t thread_id;
303
304   /** Current pid, may be different from main_pid if forked child */
305   pid_t current_pid;
306
307   u32 forked_child;
308
309 } vcl_worker_t;
310
311 typedef struct vppcom_main_t_
312 {
313   u8 is_init;
314   u32 debug;
315   pthread_t main_cpu;
316
317   /** Main process pid */
318   pid_t main_pid;
319
320   /** App's index in vpp. It's used by vpp to identify the app */
321   u32 app_index;
322
323   /** State of the connection, shared between msg RX thread and main thread */
324   volatile app_state_t app_state;
325
326   u8 *app_name;
327
328   /** VCL configuration */
329   vppcom_cfg_t cfg;
330
331   volatile u32 forking;
332
333   /** Workers */
334   vcl_worker_t *workers;
335
336   /** Lock to protect worker registrations */
337   clib_spinlock_t workers_lock;
338
339   /** Pool of shared sessions */
340   vcl_shared_session_t *shared_sessions;
341
342   /** Lock to protect segment hash table */
343   clib_rwlock_t segment_table_lock;
344
345   /** Mapped segments table */
346   uword *segment_table;
347
348   svm_fifo_segment_main_t segment_main;
349
350 #ifdef VCL_ELOG
351   /* VPP Event-logger */
352   elog_main_t elog_main;
353   elog_track_t elog_track;
354 #endif
355
356   /* VNET_API_ERROR_FOO -> "Foo" hash table */
357   uword *error_string_by_error_number;
358
359 } vppcom_main_t;
360
361 extern vppcom_main_t *vcm;
362
363 #define VCL_INVALID_SESSION_INDEX ((u32)~0)
364 #define VCL_INVALID_SEGMENT_INDEX ((u32)~0)
365 #define VCL_INVALID_SEGMENT_HANDLE ((u64)~0)
366
367 static inline vcl_session_t *
368 vcl_session_alloc (vcl_worker_t * wrk)
369 {
370   vcl_session_t *s;
371   pool_get (wrk->sessions, s);
372   memset (s, 0, sizeof (*s));
373   s->session_index = s - wrk->sessions;
374   return s;
375 }
376
377 static inline void
378 vcl_session_free (vcl_worker_t * wrk, vcl_session_t * s)
379 {
380   pool_put (wrk->sessions, s);
381 }
382
383 static inline vcl_session_t *
384 vcl_session_get (vcl_worker_t * wrk, u32 session_index)
385 {
386   if (pool_is_free_index (wrk->sessions, session_index))
387     return 0;
388   return pool_elt_at_index (wrk->sessions, session_index);
389 }
390
391 static inline vcl_session_handle_t
392 vcl_session_handle_from_index (u32 session_index)
393 {
394   ASSERT (session_index < 2 << 24);
395   return (vcl_get_worker_index () << 24 | session_index);
396 }
397
398 static inline vcl_session_handle_t
399 vcl_session_handle (vcl_session_t * s)
400 {
401   return vcl_session_handle_from_index (s->session_index);
402 }
403
404 static inline void
405 vcl_session_handle_parse (u32 handle, u32 * wrk_index, u32 * session_index)
406 {
407   *wrk_index = handle >> 24;
408   *session_index = handle & 0xFFFFFF;
409 }
410
411 static inline vcl_session_t *
412 vcl_session_get_w_handle (vcl_worker_t * wrk, u32 session_handle)
413 {
414   u32 session_index, wrk_index;
415   vcl_session_handle_parse (session_handle, &wrk_index, &session_index);
416   ASSERT (wrk_index == wrk->wrk_index);
417   return vcl_session_get (wrk, session_index);
418 }
419
420 static inline vcl_session_t *
421 vcl_session_get_w_vpp_handle (vcl_worker_t * wrk, u64 vpp_handle)
422 {
423   uword *p;
424   if ((p = hash_get (wrk->session_index_by_vpp_handles, vpp_handle)))
425     return vcl_session_get (wrk, (u32) p[0]);
426   return 0;
427 }
428
429 static inline u32
430 vcl_session_index_from_vpp_handle (vcl_worker_t * wrk, u64 vpp_handle)
431 {
432   uword *p;
433   if ((p = hash_get (wrk->session_index_by_vpp_handles, vpp_handle)))
434     return p[0];
435   return VCL_INVALID_SESSION_INDEX;
436 }
437
438 static inline void
439 vcl_session_table_add_vpp_handle (vcl_worker_t * wrk, u64 handle, u32 value)
440 {
441   hash_set (wrk->session_index_by_vpp_handles, handle, value);
442 }
443
444 static inline void
445 vcl_session_table_del_vpp_handle (vcl_worker_t * wrk, u64 vpp_handle)
446 {
447   hash_unset (wrk->session_index_by_vpp_handles, vpp_handle);
448 }
449
450 static inline uword *
451 vcl_session_table_lookup_vpp_handle (vcl_worker_t * wrk, u64 handle)
452 {
453   return hash_get (wrk->session_index_by_vpp_handles, handle);
454 }
455
456 static inline void
457 vcl_session_table_add_listener (vcl_worker_t * wrk, u64 listener_handle,
458                                 u32 value)
459 {
460   hash_set (wrk->session_index_by_vpp_handles, listener_handle, value);
461 }
462
463 static inline void
464 vcl_session_table_del_listener (vcl_worker_t * wrk, u64 listener_handle)
465 {
466   hash_unset (wrk->session_index_by_vpp_handles, listener_handle);
467 }
468
469 static inline vcl_session_t *
470 vcl_session_table_lookup_listener (vcl_worker_t * wrk, u64 handle)
471 {
472   uword *p;
473   vcl_session_t *session;
474
475   p = hash_get (wrk->session_index_by_vpp_handles, handle);
476   if (!p)
477     {
478       VDBG (0, "could not find listen session: unknown vpp listener handle"
479             " %llx", handle);
480       return 0;
481     }
482   session = vcl_session_get (wrk, p[0]);
483   if (!session)
484     {
485       VDBG (1, "invalid listen session index (%u)", p[0]);
486       return 0;
487     }
488
489   ASSERT (session->session_state & (STATE_LISTEN | STATE_LISTEN_NO_MQ));
490   return session;
491 }
492
493 const char *vppcom_session_state_str (vcl_session_state_t state);
494
495 static inline u8
496 vcl_session_is_ct (vcl_session_t * s)
497 {
498   return (s->our_evt_q != 0);
499 }
500
501 static inline u8
502 vcl_session_is_open (vcl_session_t * s)
503 {
504   return ((s->session_state & STATE_OPEN)
505           || (s->session_state == STATE_LISTEN
506               && s->session_type == VPPCOM_PROTO_UDP));
507 }
508
509 static inline u8
510 vcl_session_is_closing (vcl_session_t * s)
511 {
512   return (s->session_state == STATE_VPP_CLOSING
513           || s->session_state == STATE_DISCONNECT);
514 }
515
516 static inline int
517 vcl_session_closing_error (vcl_session_t * s)
518 {
519   return s->session_state == STATE_DISCONNECT ? VPPCOM_ECONNRESET : 0;
520 }
521
522 static inline int
523 vcl_session_closed_error (vcl_session_t * s)
524 {
525   return s->session_state == STATE_DISCONNECT
526     ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN;
527 }
528
529 /*
530  * Helpers
531  */
532 int vcl_wait_for_app_state_change (app_state_t app_state);
533 vcl_cut_through_registration_t
534   * vcl_ct_registration_lock_and_alloc (vcl_worker_t * wrk);
535 void vcl_ct_registration_del (vcl_worker_t * wrk,
536                               vcl_cut_through_registration_t * ctr);
537 u32 vcl_ct_registration_index (vcl_worker_t * wrk,
538                                vcl_cut_through_registration_t * ctr);
539 void vcl_ct_registration_lock (vcl_worker_t * wrk);
540 void vcl_ct_registration_unlock (vcl_worker_t * wrk);
541 vcl_cut_through_registration_t
542   * vcl_ct_registration_lock_and_lookup (vcl_worker_t * wrk, uword mq_addr);
543 void vcl_ct_registration_lookup_add (vcl_worker_t * wrk, uword mq_addr,
544                                      u32 ctr_index);
545 void vcl_ct_registration_lookup_del (vcl_worker_t * wrk, uword mq_addr);
546 vcl_mq_evt_conn_t *vcl_mq_evt_conn_alloc (vcl_worker_t * wrk);
547 u32 vcl_mq_evt_conn_index (vcl_worker_t * wrk, vcl_mq_evt_conn_t * mqc);
548 vcl_mq_evt_conn_t *vcl_mq_evt_conn_get (vcl_worker_t * wrk, u32 mq_conn_idx);
549 int vcl_mq_epoll_add_evfd (vcl_worker_t * wrk, svm_msg_q_t * mq);
550 int vcl_mq_epoll_del_evfd (vcl_worker_t * wrk, u32 mqc_index);
551
552 vcl_worker_t *vcl_worker_alloc_and_init (void);
553 void vcl_worker_cleanup (vcl_worker_t * wrk, u8 notify_vpp);
554 int vcl_worker_register_with_vpp (void);
555 int vcl_worker_set_bapi (void);
556 void vcl_worker_share_sessions (vcl_worker_t * parent_wrk);
557 int vcl_worker_unshare_session (vcl_worker_t * wrk, vcl_session_t * s);
558 vcl_shared_session_t *vcl_shared_session_get (u32 ss_index);
559
560 void vcl_flush_mq_events (void);
561 void vcl_cleanup_bapi (void);
562 int vcl_session_cleanup (vcl_worker_t * wrk, vcl_session_t * session,
563                          vcl_session_handle_t sh, u8 do_disconnect);
564
565 void vcl_segment_table_add (u64 segment_handle, u32 svm_segment_index);
566 u32 vcl_segment_table_lookup (u64 segment_handle);
567 void vcl_segment_table_del (u64 segment_handle);
568
569 int vcl_session_read_ready (vcl_session_t * session);
570 int vcl_session_write_ready (vcl_session_t * session);
571
572 static inline vcl_worker_t *
573 vcl_worker_get (u32 wrk_index)
574 {
575   return pool_elt_at_index (vcm->workers, wrk_index);
576 }
577
578 static inline vcl_worker_t *
579 vcl_worker_get_if_valid (u32 wrk_index)
580 {
581   if (pool_is_free_index (vcm->workers, wrk_index))
582     return 0;
583   return pool_elt_at_index (vcm->workers, wrk_index);
584 }
585
586 static inline vcl_worker_t *
587 vcl_worker_get_current (void)
588 {
589   return vcl_worker_get (vcl_get_worker_index ());
590 }
591
592 static inline u8
593 vcl_n_workers (void)
594 {
595   return pool_elts (vcm->workers);
596 }
597
598 static inline svm_msg_q_t *
599 vcl_session_vpp_evt_q (vcl_worker_t * wrk, vcl_session_t * s)
600 {
601   if (vcl_session_is_ct (s))
602     return wrk->vpp_event_queues[0];
603   else
604     return wrk->vpp_event_queues[s->vpp_thread_index];
605 }
606
607 void vcl_send_session_worker_update (vcl_worker_t * wrk, vcl_session_t * s,
608                                      u32 wrk_index);
609 /*
610  * VCL Binary API
611  */
612 int vppcom_connect_to_vpp (char *app_name);
613 void vppcom_init_error_string_table (void);
614 void vppcom_send_session_enable_disable (u8 is_enable);
615 void vppcom_app_send_attach (void);
616 void vppcom_app_send_detach (void);
617 void vppcom_send_connect_sock (vcl_session_t * session);
618 void vppcom_send_disconnect_session (u64 vpp_handle);
619 void vppcom_send_bind_sock (vcl_session_t * session);
620 void vppcom_send_unbind_sock (vcl_worker_t * wrk, u64 vpp_handle);
621 void vppcom_api_hookup (void);
622 void vppcom_send_application_tls_cert_add (vcl_session_t * session,
623                                            char *cert, u32 cert_len);
624 void vppcom_send_application_tls_key_add (vcl_session_t * session, char *key,
625                                           u32 key_len);
626 void vcl_send_app_worker_add_del (u8 is_add);
627 void vcl_send_child_worker_del (vcl_worker_t * wrk);
628
629 u32 vcl_max_nsid_len (void);
630
631 u8 *format_api_error (u8 * s, va_list * args);
632
633 void vls_init ();
634 #endif /* SRC_VCL_VCL_PRIVATE_H_ */
635
636 /*
637  * fd.io coding-style-patch-verification: ON
638  *
639  * Local Variables:
640  * eval: (c-set-style "gnu")
641  * End:
642  */