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