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