session: notify app of session and transport cleanup
[vpp.git] / src / vnet / session / application_interface.h
1 /*
2  * Copyright (c) 2016-2019 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
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 #ifndef __included_uri_h__
16 #define __included_uri_h__
17
18 #include <vlibmemory/api.h>
19 #include <svm/message_queue.h>
20 #include <vnet/session/session_types.h>
21 #include <vnet/tls/tls_test.h>
22 #include <svm/fifo_segment.h>
23
24 typedef struct _stream_session_cb_vft
25 {
26   /** Notify server of new segment */
27   int (*add_segment_callback) (u32 api_client_index, u64 segment_handle);
28
29   /** Notify server of new segment */
30   int (*del_segment_callback) (u32 api_client_index, u64 segment_handle);
31
32   /** Notify server of newly accepted session */
33   int (*session_accept_callback) (session_t * new_session);
34
35   /** Connection request callback */
36   int (*session_connected_callback) (u32 app_wrk_index, u32 opaque,
37                                      session_t * s, u8 code);
38
39   /** Notify app that session is closing */
40   void (*session_disconnect_callback) (session_t * s);
41
42   /** Notify app that session or transport are about to be removed */
43   void (*session_cleanup_callback) (session_t * s, session_cleanup_ntf_t ntf);
44
45   /** Notify app that session was reset */
46   void (*session_reset_callback) (session_t * s);
47
48   /** Direct RX callback for built-in application */
49   int (*builtin_app_rx_callback) (session_t * session);
50
51   /** Direct TX callback for built-in application */
52   int (*builtin_app_tx_callback) (session_t * session);
53
54 } session_cb_vft_t;
55
56 #define foreach_app_init_args                   \
57   _(u32, api_client_index)                      \
58   _(u8 *, name)                                 \
59   _(u64 *, options)                             \
60   _(u8 *, namespace_id)                         \
61   _(session_cb_vft_t *, session_cb_vft)         \
62   _(u32, app_index)                             \
63
64 typedef struct _vnet_app_attach_args_t
65 {
66 #define _(_type, _name) _type _name;
67   foreach_app_init_args
68 #undef _
69   ssvm_private_t * segment;
70   svm_msg_q_t *app_evt_q;
71   u64 segment_handle;
72 } vnet_app_attach_args_t;
73
74 typedef struct _vnet_app_detach_args_t
75 {
76   u32 app_index;
77   u32 api_client_index;
78 } vnet_app_detach_args_t;
79
80 typedef struct _vnet_bind_args_t
81 {
82   union
83   {
84     session_endpoint_cfg_t sep_ext;
85     session_endpoint_t sep;
86     char *uri;
87   };
88
89   u32 app_index;
90   u32 wrk_map_index;
91
92   /*
93    * Results
94    */
95   char *segment_name;
96   u32 segment_name_length;
97   u64 server_event_queue_address;
98   u64 handle;
99 } vnet_listen_args_t;
100
101 typedef struct _vnet_unlisten_args_t
102 {
103   union
104   {
105     char *uri;
106     u64 handle;                 /**< Session handle */
107   };
108   u32 app_index;                /**< Owning application index */
109   u32 wrk_map_index;            /**< App's local pool worker index */
110 } vnet_unlisten_args_t;
111
112 typedef struct _vnet_connect_args
113 {
114   union
115   {
116     session_endpoint_cfg_t sep_ext;
117     session_endpoint_t sep;
118     char *uri;
119   };
120   u32 app_index;
121   u32 wrk_map_index;
122   u32 api_context;
123
124   session_handle_t session_handle;
125 } vnet_connect_args_t;
126
127 typedef struct _vnet_disconnect_args_t
128 {
129   session_handle_t handle;
130   u32 app_index;
131 } vnet_disconnect_args_t;
132
133 typedef struct _vnet_application_add_tls_cert_args_t
134 {
135   u32 app_index;
136   u8 *cert;
137 } vnet_app_add_tls_cert_args_t;
138
139 typedef struct _vnet_application_add_tls_key_args_t
140 {
141   u32 app_index;
142   u8 *key;
143 } vnet_app_add_tls_key_args_t;
144
145 typedef enum tls_engine_type_
146 {
147   TLS_ENGINE_NONE,
148   TLS_ENGINE_MBEDTLS,
149   TLS_ENGINE_OPENSSL,
150   TLS_N_ENGINES
151 } tls_engine_type_t;
152
153 /* Application attach options */
154 typedef enum
155 {
156   APP_OPTIONS_FLAGS,
157   APP_OPTIONS_EVT_QUEUE_SIZE,
158   APP_OPTIONS_SEGMENT_SIZE,
159   APP_OPTIONS_ADD_SEGMENT_SIZE,
160   APP_OPTIONS_PRIVATE_SEGMENT_COUNT,
161   APP_OPTIONS_RX_FIFO_SIZE,
162   APP_OPTIONS_TX_FIFO_SIZE,
163   APP_OPTIONS_PREALLOC_FIFO_PAIRS,
164   APP_OPTIONS_NAMESPACE,
165   APP_OPTIONS_NAMESPACE_SECRET,
166   APP_OPTIONS_PROXY_TRANSPORT,
167   APP_OPTIONS_ACCEPT_COOKIE,
168   APP_OPTIONS_TLS_ENGINE,
169   APP_OPTIONS_N_OPTIONS
170 } app_attach_options_index_t;
171
172 #define foreach_app_options_flags                               \
173   _(ACCEPT_REDIRECT, "Use FIFO with redirects")                 \
174   _(ADD_SEGMENT, "Add segment and signal app if needed")        \
175   _(IS_BUILTIN, "Application is builtin")                       \
176   _(IS_TRANSPORT_APP, "Application is a transport proto")       \
177   _(IS_PROXY, "Application is proxying")                        \
178   _(USE_GLOBAL_SCOPE, "App can use global session scope")       \
179   _(USE_LOCAL_SCOPE, "App can use local session scope")         \
180   _(EVT_MQ_USE_EVENTFD, "Use eventfds for signaling")           \
181
182 typedef enum _app_options
183 {
184 #define _(sym, str) APP_OPTIONS_##sym,
185   foreach_app_options_flags
186 #undef _
187 } app_options_t;
188
189 typedef enum _app_options_flags
190 {
191 #define _(sym, str) APP_OPTIONS_FLAGS_##sym = 1 << APP_OPTIONS_##sym,
192   foreach_app_options_flags
193 #undef _
194 } app_options_flags_t;
195
196 #define foreach_fd_type                                         \
197   _(VPP_MQ_SEGMENT, "Fd for vpp's event mq segment")            \
198   _(MEMFD_SEGMENT, "Fd for memfd segment")                      \
199   _(MQ_EVENTFD, "Event fd used by message queue")               \
200   _(VPP_MQ_EVENTFD, "Event fd used by vpp's message queue")     \
201
202 typedef enum session_fd_type_
203 {
204 #define _(sym, str) SESSION_FD_##sym,
205   foreach_fd_type
206 #undef _
207   SESSION_N_FD_TYPE
208 } session_fd_type_t;
209
210 typedef enum session_fd_flag_
211 {
212 #define _(sym, str) SESSION_FD_F_##sym = 1 << SESSION_FD_##sym,
213   foreach_fd_type
214 #undef _
215 } session_fd_flag_t;
216
217 int parse_uri (char *uri, session_endpoint_cfg_t * sep);
218 int vnet_bind_uri (vnet_listen_args_t *);
219 int vnet_unbind_uri (vnet_unlisten_args_t * a);
220 int vnet_connect_uri (vnet_connect_args_t * a);
221
222 int vnet_application_attach (vnet_app_attach_args_t * a);
223 int vnet_application_detach (vnet_app_detach_args_t * a);
224 int vnet_listen (vnet_listen_args_t * a);
225 int vnet_connect (vnet_connect_args_t * a);
226 int vnet_unlisten (vnet_unlisten_args_t * a);
227 int vnet_disconnect_session (vnet_disconnect_args_t * a);
228
229 clib_error_t *vnet_app_add_tls_cert (vnet_app_add_tls_cert_args_t * a);
230 clib_error_t *vnet_app_add_tls_key (vnet_app_add_tls_key_args_t * a);
231
232 typedef struct app_session_transport_
233 {
234   ip46_address_t rmt_ip;        /**< remote ip */
235   ip46_address_t lcl_ip;        /**< local ip */
236   u16 rmt_port;                 /**< remote port (network order) */
237   u16 lcl_port;                 /**< local port (network order) */
238   u8 is_ip4;                    /**< set if uses ip4 networking */
239 } app_session_transport_t;
240
241 #define foreach_app_session_field                                       \
242   _(svm_fifo_t, *rx_fifo)               /**< rx fifo */                 \
243   _(svm_fifo_t, *tx_fifo)               /**< tx fifo */                 \
244   _(session_type_t, session_type)       /**< session type */            \
245   _(volatile u8, session_state)         /**< session state */           \
246   _(u32, session_index)                 /**< index in owning pool */    \
247   _(app_session_transport_t, transport) /**< transport info */          \
248   _(svm_msg_q_t, *vpp_evt_q)            /**< vpp event queue  */        \
249   _(u8, is_dgram)                       /**< flag for dgram mode */     \
250
251 typedef struct
252 {
253 #define _(type, name) type name;
254   foreach_app_session_field
255 #undef _
256 } app_session_t;
257
258 typedef struct session_bound_msg_
259 {
260   u32 context;
261   u64 handle;
262   i32 retval;
263   u8 lcl_is_ip4;
264   u8 lcl_ip[16];
265   u16 lcl_port;
266   uword rx_fifo;
267   uword tx_fifo;
268   uword vpp_evt_q;
269   u32 segment_size;
270   u8 segment_name_length;
271   u8 segment_name[128];
272 } __clib_packed session_bound_msg_t;
273
274 typedef struct session_unlisten_reply_msg_
275 {
276   u32 context;
277   u64 handle;
278   i32 retval;
279 } __clib_packed session_unlisten_reply_msg_t;
280
281 typedef struct session_accepted_msg_
282 {
283   u32 context;
284   u64 listener_handle;
285   u64 handle;
286   uword server_rx_fifo;
287   uword server_tx_fifo;
288   u64 segment_handle;
289   uword vpp_event_queue_address;
290   transport_endpoint_t rmt;
291 } __clib_packed session_accepted_msg_t;
292
293 typedef struct session_accepted_reply_msg_
294 {
295   u32 context;
296   i32 retval;
297   u64 handle;
298 } __clib_packed session_accepted_reply_msg_t;
299
300 /* Make sure this is not too large, otherwise it won't fit when dequeued in
301  * the session queue node */
302 STATIC_ASSERT (sizeof (session_accepted_reply_msg_t) <= 16, "accept reply");
303
304 typedef struct session_connected_msg_
305 {
306   u32 context;
307   i32 retval;
308   u64 handle;
309   uword server_rx_fifo;
310   uword server_tx_fifo;
311   u64 segment_handle;
312   uword ct_rx_fifo;
313   uword ct_tx_fifo;
314   u64 ct_segment_handle;
315   uword vpp_event_queue_address;
316   u32 segment_size;
317   u8 segment_name_length;
318   u8 segment_name[64];
319   transport_endpoint_t lcl;
320 } __clib_packed session_connected_msg_t;
321
322 typedef struct session_disconnected_msg_
323 {
324   u32 client_index;
325   u32 context;
326   u64 handle;
327 } __clib_packed session_disconnected_msg_t;
328
329 typedef struct session_disconnected_reply_msg_
330 {
331   u32 context;
332   i32 retval;
333   u64 handle;
334 } __clib_packed session_disconnected_reply_msg_t;
335
336 typedef struct session_reset_msg_
337 {
338   u32 client_index;
339   u32 context;
340   u64 handle;
341 } __clib_packed session_reset_msg_t;
342
343 typedef struct session_reset_reply_msg_
344 {
345   u32 context;
346   i32 retval;
347   u64 handle;
348 } __clib_packed session_reset_reply_msg_t;
349
350 typedef struct session_req_worker_update_msg_
351 {
352   u64 session_handle;
353 } __clib_packed session_req_worker_update_msg_t;
354
355 /* NOTE: using u16 for wrk indices because message needs to fit in 18B */
356 typedef struct session_worker_update_msg_
357 {
358   u32 client_index;
359   u16 wrk_index;
360   u16 req_wrk_index;
361   u64 handle;
362 } __clib_packed session_worker_update_msg_t;
363
364 typedef struct session_worker_update_reply_msg_
365 {
366   u64 handle;
367   uword rx_fifo;
368   uword tx_fifo;
369   u64 segment_handle;
370 } __clib_packed session_worker_update_reply_msg_t;
371
372 typedef struct app_session_event_
373 {
374   svm_msg_q_msg_t msg;
375   session_event_t *evt;
376 } __clib_packed app_session_evt_t;
377
378 static inline void
379 app_alloc_ctrl_evt_to_vpp (svm_msg_q_t * mq, app_session_evt_t * app_evt,
380                            u8 evt_type)
381 {
382   svm_msg_q_lock_and_alloc_msg_w_ring (mq,
383                                        SESSION_MQ_CTRL_EVT_RING,
384                                        SVM_Q_WAIT, &app_evt->msg);
385   svm_msg_q_unlock (mq);
386   app_evt->evt = svm_msg_q_msg_data (mq, &app_evt->msg);
387   clib_memset (app_evt->evt, 0, sizeof (*app_evt->evt));
388   app_evt->evt->event_type = evt_type;
389 }
390
391 static inline void
392 app_send_ctrl_evt_to_vpp (svm_msg_q_t * mq, app_session_evt_t * app_evt)
393 {
394   svm_msg_q_add (mq, &app_evt->msg, SVM_Q_WAIT);
395 }
396
397 /**
398  * Send fifo io event to vpp worker thread
399  *
400  * Because there may be multiple writers to one of vpp's queues, this
401  * protects message allocation and enqueueing.
402  *
403  * @param mq            vpp message queue
404  * @param f             fifo for which the event is sent
405  * @param evt_type      type of event
406  * @param noblock       flag to indicate is request is blocking or not
407  * @return              0 if success, negative integer otherwise
408  */
409 static inline int
410 app_send_io_evt_to_vpp (svm_msg_q_t * mq, u32 session_index, u8 evt_type,
411                         u8 noblock)
412 {
413   session_event_t *evt;
414   svm_msg_q_msg_t msg;
415
416   if (noblock)
417     {
418       if (svm_msg_q_try_lock (mq))
419         return -1;
420       if (PREDICT_FALSE (svm_msg_q_ring_is_full (mq, SESSION_MQ_IO_EVT_RING)))
421         {
422           svm_msg_q_unlock (mq);
423           return -2;
424         }
425       msg = svm_msg_q_alloc_msg_w_ring (mq, SESSION_MQ_IO_EVT_RING);
426       if (PREDICT_FALSE (svm_msg_q_msg_is_invalid (&msg)))
427         {
428           svm_msg_q_unlock (mq);
429           return -2;
430         }
431       evt = (session_event_t *) svm_msg_q_msg_data (mq, &msg);
432       evt->session_index = session_index;
433       evt->event_type = evt_type;
434       svm_msg_q_add_and_unlock (mq, &msg);
435       return 0;
436     }
437   else
438     {
439       svm_msg_q_lock (mq);
440       while (svm_msg_q_ring_is_full (mq, SESSION_MQ_IO_EVT_RING))
441         svm_msg_q_wait (mq);
442       msg = svm_msg_q_alloc_msg_w_ring (mq, SESSION_MQ_IO_EVT_RING);
443       evt = (session_event_t *) svm_msg_q_msg_data (mq, &msg);
444       evt->session_index = session_index;
445       evt->event_type = evt_type;
446       if (svm_msg_q_is_full (mq))
447         svm_msg_q_wait (mq);
448       svm_msg_q_add_and_unlock (mq, &msg);
449       return 0;
450     }
451 }
452
453 always_inline int
454 app_send_dgram_raw (svm_fifo_t * f, app_session_transport_t * at,
455                     svm_msg_q_t * vpp_evt_q, u8 * data, u32 len, u8 evt_type,
456                     u8 do_evt, u8 noblock)
457 {
458   u32 max_enqueue, actual_write;
459   session_dgram_hdr_t hdr;
460   int rv;
461
462   max_enqueue = svm_fifo_max_enqueue_prod (f);
463   if (max_enqueue <= sizeof (session_dgram_hdr_t))
464     return 0;
465
466   max_enqueue -= sizeof (session_dgram_hdr_t);
467   actual_write = clib_min (len, max_enqueue);
468   hdr.data_length = actual_write;
469   hdr.data_offset = 0;
470   clib_memcpy_fast (&hdr.rmt_ip, &at->rmt_ip, sizeof (ip46_address_t));
471   hdr.is_ip4 = at->is_ip4;
472   hdr.rmt_port = at->rmt_port;
473   clib_memcpy_fast (&hdr.lcl_ip, &at->lcl_ip, sizeof (ip46_address_t));
474   hdr.lcl_port = at->lcl_port;
475   rv = svm_fifo_enqueue (f, sizeof (hdr), (u8 *) & hdr);
476   ASSERT (rv == sizeof (hdr));
477
478   rv = svm_fifo_enqueue (f, actual_write, data);
479   if (do_evt)
480     {
481       if (rv > 0 && svm_fifo_set_event (f))
482         app_send_io_evt_to_vpp (vpp_evt_q, f->master_session_index, evt_type,
483                                 noblock);
484     }
485   ASSERT (rv);
486   return rv;
487 }
488
489 always_inline int
490 app_send_dgram (app_session_t * s, u8 * data, u32 len, u8 noblock)
491 {
492   return app_send_dgram_raw (s->tx_fifo, &s->transport, s->vpp_evt_q, data,
493                              len, SESSION_IO_EVT_TX, 1 /* do_evt */ ,
494                              noblock);
495 }
496
497 always_inline int
498 app_send_stream_raw (svm_fifo_t * f, svm_msg_q_t * vpp_evt_q, u8 * data,
499                      u32 len, u8 evt_type, u8 do_evt, u8 noblock)
500 {
501   int rv;
502
503   rv = svm_fifo_enqueue (f, len, data);
504   if (do_evt)
505     {
506       if (rv > 0 && svm_fifo_set_event (f))
507         app_send_io_evt_to_vpp (vpp_evt_q, f->master_session_index, evt_type,
508                                 noblock);
509     }
510   return rv;
511 }
512
513 always_inline int
514 app_send_stream (app_session_t * s, u8 * data, u32 len, u8 noblock)
515 {
516   return app_send_stream_raw (s->tx_fifo, s->vpp_evt_q, data, len,
517                               SESSION_IO_EVT_TX, 1 /* do_evt */ , noblock);
518 }
519
520 always_inline int
521 app_send (app_session_t * s, u8 * data, u32 len, u8 noblock)
522 {
523   if (s->is_dgram)
524     return app_send_dgram (s, data, len, noblock);
525   return app_send_stream (s, data, len, noblock);
526 }
527
528 always_inline int
529 app_recv_dgram_raw (svm_fifo_t * f, u8 * buf, u32 len,
530                     app_session_transport_t * at, u8 clear_evt, u8 peek)
531 {
532   session_dgram_pre_hdr_t ph;
533   u32 max_deq;
534   int rv;
535
536   max_deq = svm_fifo_max_dequeue_cons (f);
537   if (max_deq < sizeof (session_dgram_hdr_t))
538     {
539       if (clear_evt)
540         svm_fifo_unset_event (f);
541       return 0;
542     }
543
544   if (clear_evt)
545     svm_fifo_unset_event (f);
546
547   svm_fifo_peek (f, 0, sizeof (ph), (u8 *) & ph);
548   ASSERT (ph.data_length >= ph.data_offset);
549   if (!ph.data_offset)
550     svm_fifo_peek (f, sizeof (ph), sizeof (*at), (u8 *) at);
551   len = clib_min (len, ph.data_length - ph.data_offset);
552   rv = svm_fifo_peek (f, ph.data_offset + SESSION_CONN_HDR_LEN, len, buf);
553   if (peek)
554     return rv;
555   ph.data_offset += rv;
556   if (ph.data_offset == ph.data_length)
557     svm_fifo_dequeue_drop (f, ph.data_length + SESSION_CONN_HDR_LEN);
558   else
559     svm_fifo_overwrite_head (f, (u8 *) & ph, sizeof (ph));
560   return rv;
561 }
562
563 always_inline int
564 app_recv_dgram (app_session_t * s, u8 * buf, u32 len)
565 {
566   return app_recv_dgram_raw (s->rx_fifo, buf, len, &s->transport, 1, 0);
567 }
568
569 always_inline int
570 app_recv_stream_raw (svm_fifo_t * f, u8 * buf, u32 len, u8 clear_evt, u8 peek)
571 {
572   if (clear_evt)
573     svm_fifo_unset_event (f);
574
575   if (peek)
576     return svm_fifo_peek (f, 0, len, buf);
577
578   return svm_fifo_dequeue (f, len, buf);
579 }
580
581 always_inline int
582 app_recv_stream (app_session_t * s, u8 * buf, u32 len)
583 {
584   return app_recv_stream_raw (s->rx_fifo, buf, len, 1, 0);
585 }
586
587 always_inline int
588 app_recv (app_session_t * s, u8 * data, u32 len)
589 {
590   if (s->is_dgram)
591     return app_recv_dgram (s, data, len);
592   return app_recv_stream (s, data, len);
593 }
594
595 #endif /* __included_uri_h__ */
596
597 /*
598  * fd.io coding-style-patch-verification: ON
599  *
600  * Local Variables:
601  * eval: (c-set-style "gnu")
602  * End:
603  */