session: support half-close connection
[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 certificate_
25 {
26   u32 *app_interests;           /* vec of application index asking for deletion cb */
27   u32 cert_key_index;           /* index in cert & key pool */
28   u8 *key;
29   u8 *cert;
30 } app_cert_key_pair_t;
31
32 typedef struct session_cb_vft_
33 {
34   /** Notify server of new segment */
35   int (*add_segment_callback) (u32 app_wrk_index, u64 segment_handle);
36
37   /** Notify server of new segment */
38   int (*del_segment_callback) (u32 app_wrk_index, u64 segment_handle);
39
40   /** Notify server of newly accepted session */
41   int (*session_accept_callback) (session_t * new_session);
42
43   /** Connection request callback */
44   int (*session_connected_callback) (u32 app_wrk_index, u32 opaque,
45                                      session_t * s, session_error_t code);
46
47   /** Notify app that session is closing */
48   void (*session_disconnect_callback) (session_t * s);
49
50   /** Notify app that transport is closed */
51   void (*session_transport_closed_callback) (session_t * s);
52
53   /** Notify app that session or transport are about to be removed */
54   void (*session_cleanup_callback) (session_t * s, session_cleanup_ntf_t ntf);
55
56   /** Notify app that session was reset */
57   void (*session_reset_callback) (session_t * s);
58
59   /** Notify app that session pool migration happened */
60   void (*session_migrate_callback) (session_t * s, session_handle_t new_sh);
61
62   /** Direct RX callback for built-in application */
63   int (*builtin_app_rx_callback) (session_t * session);
64
65   /** Direct TX callback for built-in application */
66   int (*builtin_app_tx_callback) (session_t * session);
67
68   /** Cert and key pair delete notification */
69   int (*app_cert_key_pair_delete_callback) (app_cert_key_pair_t * ckpair);
70
71   /** Delegate fifo-tuning-logic to application */
72   int (*fifo_tuning_callback) (session_t * s, svm_fifo_t * f,
73                                session_ft_action_t act, u32 bytes);
74
75 } session_cb_vft_t;
76
77 #define foreach_app_init_args                   \
78   _(u32, api_client_index)                      \
79   _(u8 *, name)                                 \
80   _(u64 *, options)                             \
81   _(u8 *, namespace_id)                         \
82   _(session_cb_vft_t *, session_cb_vft)         \
83   _(u32, app_index)                             \
84   _(u8, use_sock_api)                           \
85
86 typedef struct _vnet_app_attach_args_t
87 {
88 #define _(_type, _name) _type _name;
89   foreach_app_init_args
90 #undef _
91   ssvm_private_t * segment;
92   svm_msg_q_t *app_evt_q;
93   u64 segment_handle;
94 } vnet_app_attach_args_t;
95
96 typedef struct _vnet_app_detach_args_t
97 {
98   u32 app_index;
99   u32 api_client_index;
100 } vnet_app_detach_args_t;
101
102 typedef struct _vnet_bind_args_t
103 {
104   union
105   {
106     session_endpoint_cfg_t sep_ext;
107     session_endpoint_t sep;
108     char *uri;
109   };
110
111   u32 app_index;
112   u32 wrk_map_index;
113
114   /*
115    * Results
116    */
117   u64 handle;
118 } vnet_listen_args_t;
119
120 typedef struct _vnet_unlisten_args_t
121 {
122   union
123   {
124     char *uri;
125     u64 handle;                 /**< Session handle */
126   };
127   u32 app_index;                /**< Owning application index */
128   u32 wrk_map_index;            /**< App's local pool worker index */
129 } vnet_unlisten_args_t;
130
131 typedef struct _vnet_connect_args
132 {
133   union
134   {
135     session_endpoint_cfg_t sep_ext;
136     session_endpoint_t sep;
137     char *uri;
138   };
139   u32 app_index;
140   u32 wrk_map_index;
141   u32 api_context;
142 } vnet_connect_args_t;
143
144 typedef struct _vnet_shutdown_args_t
145 {
146   session_handle_t handle;
147   u32 app_index;
148 } vnet_shutdown_args_t;
149
150 typedef struct _vnet_disconnect_args_t
151 {
152   session_handle_t handle;
153   u32 app_index;
154 } vnet_disconnect_args_t;
155
156 typedef struct _vnet_application_add_tls_cert_args_t
157 {
158   u32 app_index;
159   u8 *cert;
160 } vnet_app_add_tls_cert_args_t;
161
162 typedef struct _vnet_application_add_tls_key_args_t
163 {
164   u32 app_index;
165   u8 *key;
166 } vnet_app_add_tls_key_args_t;
167
168 typedef enum crypto_engine_type_
169 {
170   CRYPTO_ENGINE_NONE,
171   CRYPTO_ENGINE_OPENSSL,
172   CRYPTO_ENGINE_MBEDTLS,
173   CRYPTO_ENGINE_VPP,
174   CRYPTO_ENGINE_PICOTLS,
175   CRYPTO_ENGINE_LAST = CRYPTO_ENGINE_PICOTLS,
176 } crypto_engine_type_t;
177
178 typedef struct _vnet_app_add_cert_key_pair_args_
179 {
180   u8 *cert;
181   u8 *key;
182   u32 cert_len;
183   u32 key_len;
184   u32 index;
185 } vnet_app_add_cert_key_pair_args_t;
186
187 typedef struct crypto_ctx_
188 {
189   u32 ctx_index;                /**< index in crypto context pool */
190   u32 n_subscribers;            /**< refcount of sessions using said context */
191   u32 ckpair_index;             /**< certificate & key */
192   u8 crypto_engine;
193   void *data;                   /**< protocol specific data */
194 } crypto_context_t;
195
196 /* Application attach options */
197 typedef enum
198 {
199   APP_OPTIONS_FLAGS,
200   APP_OPTIONS_EVT_QUEUE_SIZE,
201   APP_OPTIONS_SEGMENT_SIZE,
202   APP_OPTIONS_ADD_SEGMENT_SIZE,
203   APP_OPTIONS_PRIVATE_SEGMENT_COUNT,
204   APP_OPTIONS_RX_FIFO_SIZE,
205   APP_OPTIONS_TX_FIFO_SIZE,
206   APP_OPTIONS_PREALLOC_FIFO_PAIRS,
207   APP_OPTIONS_PREALLOC_FIFO_HDRS,
208   APP_OPTIONS_NAMESPACE,
209   APP_OPTIONS_NAMESPACE_SECRET,
210   APP_OPTIONS_PROXY_TRANSPORT,
211   APP_OPTIONS_ACCEPT_COOKIE,
212   APP_OPTIONS_TLS_ENGINE,
213   APP_OPTIONS_MAX_FIFO_SIZE,
214   APP_OPTIONS_HIGH_WATERMARK,
215   APP_OPTIONS_LOW_WATERMARK,
216   APP_OPTIONS_PCT_FIRST_ALLOC,
217   APP_OPTIONS_N_OPTIONS
218 } app_attach_options_index_t;
219
220 #define foreach_app_options_flags                               \
221   _(ACCEPT_REDIRECT, "Use FIFO with redirects")                 \
222   _(ADD_SEGMENT, "Add segment and signal app if needed")        \
223   _(IS_BUILTIN, "Application is builtin")                       \
224   _(IS_TRANSPORT_APP, "Application is a transport proto")       \
225   _(IS_PROXY, "Application is proxying")                        \
226   _(USE_GLOBAL_SCOPE, "App can use global session scope")       \
227   _(USE_LOCAL_SCOPE, "App can use local session scope")         \
228   _(EVT_MQ_USE_EVENTFD, "Use eventfds for signaling")           \
229
230 typedef enum _app_options
231 {
232 #define _(sym, str) APP_OPTIONS_##sym,
233   foreach_app_options_flags
234 #undef _
235 } app_options_t;
236
237 typedef enum _app_options_flags
238 {
239 #define _(sym, str) APP_OPTIONS_FLAGS_##sym = 1 << APP_OPTIONS_##sym,
240   foreach_app_options_flags
241 #undef _
242 } app_options_flags_t;
243
244 #define foreach_fd_type                                         \
245   _(VPP_MQ_SEGMENT, "Fd for vpp's event mq segment")            \
246   _(MEMFD_SEGMENT, "Fd for memfd segment")                      \
247   _(MQ_EVENTFD, "Event fd used by message queue")               \
248   _(VPP_MQ_EVENTFD, "Event fd used by vpp's message queue")     \
249
250 typedef enum session_fd_type_
251 {
252 #define _(sym, str) SESSION_FD_##sym,
253   foreach_fd_type
254 #undef _
255   SESSION_N_FD_TYPE
256 } session_fd_type_t;
257
258 typedef enum session_fd_flag_
259 {
260 #define _(sym, str) SESSION_FD_F_##sym = 1 << SESSION_FD_##sym,
261   foreach_fd_type
262 #undef _
263 } session_fd_flag_t;
264
265 int parse_uri (char *uri, session_endpoint_cfg_t * sep);
266 int vnet_bind_uri (vnet_listen_args_t *);
267 int vnet_unbind_uri (vnet_unlisten_args_t * a);
268 int vnet_connect_uri (vnet_connect_args_t * a);
269
270 int vnet_application_attach (vnet_app_attach_args_t * a);
271 int vnet_application_detach (vnet_app_detach_args_t * a);
272 int vnet_listen (vnet_listen_args_t * a);
273 int vnet_connect (vnet_connect_args_t * a);
274 int vnet_unlisten (vnet_unlisten_args_t * a);
275 int vnet_shutdown_session (vnet_shutdown_args_t *a);
276 int vnet_disconnect_session (vnet_disconnect_args_t * a);
277
278 int vnet_app_add_cert_key_pair (vnet_app_add_cert_key_pair_args_t * a);
279 int vnet_app_del_cert_key_pair (u32 index);
280 /** Ask for app cb on pair deletion */
281 int vnet_app_add_cert_key_interest (u32 index, u32 app_index);
282
283 typedef struct app_session_transport_
284 {
285   ip46_address_t rmt_ip;        /**< remote ip */
286   ip46_address_t lcl_ip;        /**< local ip */
287   u16 rmt_port;                 /**< remote port (network order) */
288   u16 lcl_port;                 /**< local port (network order) */
289   u8 is_ip4;                    /**< set if uses ip4 networking */
290 } app_session_transport_t;
291
292 #define foreach_app_session_field                                       \
293   _(svm_fifo_t, *rx_fifo)               /**< rx fifo */                 \
294   _(svm_fifo_t, *tx_fifo)               /**< tx fifo */                 \
295   _(session_type_t, session_type)       /**< session type */            \
296   _(volatile u8, session_state)         /**< session state */           \
297   _(u32, session_index)                 /**< index in owning pool */    \
298   _(app_session_transport_t, transport) /**< transport info */          \
299   _(svm_msg_q_t, *vpp_evt_q)            /**< vpp event queue  */        \
300   _(u8, is_dgram)                       /**< flag for dgram mode */     \
301
302 typedef struct
303 {
304 #define _(type, name) type name;
305   foreach_app_session_field
306 #undef _
307 } app_session_t;
308
309 typedef struct session_listen_msg_
310 {
311   u32 client_index;
312   u32 context;                  /* Not needed but keeping it for compatibility with bapi */
313   u32 wrk_index;
314   u32 vrf;
315   u16 port;
316   u8 proto;
317   u8 is_ip4;
318   ip46_address_t ip;
319   u8 flags;
320   uword ext_config;
321 } __clib_packed session_listen_msg_t;
322
323 STATIC_ASSERT (sizeof (session_listen_msg_t) <= SESSION_CTRL_MSG_MAX_SIZE,
324                "msg too large");
325
326 typedef struct session_listen_uri_msg_
327 {
328   u32 client_index;
329   u32 context;
330   u8 uri[56];
331 } __clib_packed session_listen_uri_msg_t;
332
333 STATIC_ASSERT (sizeof (session_listen_uri_msg_t) <= SESSION_CTRL_MSG_MAX_SIZE,
334                "msg too large");
335
336 typedef struct session_bound_msg_
337 {
338   u32 context;
339   u64 handle;
340   i32 retval;
341   u8 lcl_is_ip4;
342   u8 lcl_ip[16];
343   u16 lcl_port;
344   uword rx_fifo;
345   uword tx_fifo;
346   uword vpp_evt_q;
347   u64 segment_handle;
348 } __clib_packed session_bound_msg_t;
349
350 typedef struct session_unlisten_msg_
351 {
352   u32 client_index;
353   u32 context;
354   u32 wrk_index;
355   session_handle_t handle;
356 } __clib_packed session_unlisten_msg_t;
357
358 typedef struct session_unlisten_reply_msg_
359 {
360   u32 context;
361   u64 handle;
362   i32 retval;
363 } __clib_packed session_unlisten_reply_msg_t;
364
365 typedef struct session_accepted_msg_
366 {
367   u32 context;
368   u64 listener_handle;
369   u64 handle;
370   uword server_rx_fifo;
371   uword server_tx_fifo;
372   u64 segment_handle;
373   uword vpp_event_queue_address;
374   u32 mq_index;
375   transport_endpoint_t rmt;
376   u8 flags;
377 } __clib_packed session_accepted_msg_t;
378
379 typedef struct session_accepted_reply_msg_
380 {
381   u32 context;
382   i32 retval;
383   u64 handle;
384 } __clib_packed session_accepted_reply_msg_t;
385
386 typedef struct session_connect_msg_
387 {
388   u32 client_index;
389   u32 context;
390   u32 wrk_index;
391   u32 vrf;
392   u16 port;
393   u16 lcl_port;
394   u8 proto;
395   u8 is_ip4;
396   ip46_address_t ip;
397   ip46_address_t lcl_ip;
398   u64 parent_handle;
399   u32 ckpair_index;
400   u8 crypto_engine;
401   u8 flags;
402   uword ext_config;
403 } __clib_packed session_connect_msg_t;
404
405 STATIC_ASSERT (sizeof (session_connect_msg_t) <= SESSION_CTRL_MSG_MAX_SIZE,
406                "msg too large");
407
408 typedef struct session_connect_uri_msg_
409 {
410   u32 client_index;
411   u32 context;
412   u8 uri[56];
413 } __clib_packed session_connect_uri_msg_t;
414
415 STATIC_ASSERT (sizeof (session_connect_uri_msg_t) <=
416                SESSION_CTRL_MSG_MAX_SIZE, "msg too large");
417
418 typedef struct session_connected_msg_
419 {
420   u32 context;
421   i32 retval;
422   u64 handle;
423   uword server_rx_fifo;
424   uword server_tx_fifo;
425   u64 segment_handle;
426   uword ct_rx_fifo;
427   uword ct_tx_fifo;
428   u64 ct_segment_handle;
429   uword vpp_event_queue_address;
430   u32 segment_size;
431   u8 segment_name_length;
432   u8 segment_name[64];
433   transport_endpoint_t lcl;
434 } __clib_packed session_connected_msg_t;
435
436 typedef struct session_shutdown_msg_
437 {
438   u32 client_index;
439   u32 context;
440   session_handle_t handle;
441 } __clib_packed session_shutdown_msg_t;
442
443 typedef struct session_disconnect_msg_
444 {
445   u32 client_index;
446   u32 context;
447   session_handle_t handle;
448 } __clib_packed session_disconnect_msg_t;
449
450 typedef struct session_disconnected_msg_
451 {
452   u32 client_index;
453   u32 context;
454   u64 handle;
455 } __clib_packed session_disconnected_msg_t;
456
457 typedef struct session_disconnected_reply_msg_
458 {
459   u32 context;
460   i32 retval;
461   u64 handle;
462 } __clib_packed session_disconnected_reply_msg_t;
463
464 typedef struct session_reset_msg_
465 {
466   u32 client_index;
467   u32 context;
468   u64 handle;
469 } __clib_packed session_reset_msg_t;
470
471 typedef struct session_reset_reply_msg_
472 {
473   u32 context;
474   i32 retval;
475   u64 handle;
476 } __clib_packed session_reset_reply_msg_t;
477
478 typedef struct session_req_worker_update_msg_
479 {
480   u64 session_handle;
481 } __clib_packed session_req_worker_update_msg_t;
482
483 /* NOTE: using u16 for wrk indices because message needs to fit in 18B */
484 typedef struct session_worker_update_msg_
485 {
486   u32 client_index;
487   u16 wrk_index;
488   u16 req_wrk_index;
489   u64 handle;
490 } __clib_packed session_worker_update_msg_t;
491
492 typedef struct session_worker_update_reply_msg_
493 {
494   u64 handle;
495   uword rx_fifo;
496   uword tx_fifo;
497   u64 segment_handle;
498 } __clib_packed session_worker_update_reply_msg_t;
499
500 typedef struct session_app_detach_msg_
501 {
502   u32 client_index;
503   u32 context;
504 } session_app_detach_msg_t;
505
506 typedef struct app_map_another_segment_msg_
507 {
508   u32 client_index;
509   u32 context;
510   u8 fd_flags;
511   u32 segment_size;
512   u8 segment_name[128];
513   u64 segment_handle;
514 } session_app_add_segment_msg_t;
515
516 typedef struct app_unmap_segment_msg_
517 {
518   u32 client_index;
519   u32 context;
520   u64 segment_handle;
521 } session_app_del_segment_msg_t;
522
523 typedef struct session_migrate_msg_
524 {
525   uword vpp_evt_q;
526   session_handle_t handle;
527   session_handle_t new_handle;
528   u64 segment_handle;
529   u32 vpp_thread_index;
530 } __clib_packed session_migrated_msg_t;
531
532 typedef struct session_cleanup_msg_
533 {
534   session_handle_t handle;
535   u8 type;
536 } __clib_packed session_cleanup_msg_t;
537
538 typedef struct session_app_wrk_rpc_msg_
539 {
540   u32 client_index;     /**< app client index */
541   u32 wrk_index;        /**< dst worker index */
542   u8 data[64];          /**< rpc data */
543 } __clib_packed session_app_wrk_rpc_msg_t;
544
545 typedef struct session_transport_attr_msg_
546 {
547   u32 client_index;
548   session_handle_t handle;
549   transport_endpt_attr_t attr;
550   u8 is_get;
551 } __clib_packed session_transport_attr_msg_t;
552
553 typedef struct session_transport_attr_reply_msg_
554 {
555   i32 retval;
556   session_handle_t handle;
557   transport_endpt_attr_t attr;
558   u8 is_get;
559 } __clib_packed session_transport_attr_reply_msg_t;
560
561 typedef struct app_session_event_
562 {
563   svm_msg_q_msg_t msg;
564   session_event_t *evt;
565 } __clib_packed app_session_evt_t;
566
567 static inline void
568 app_alloc_ctrl_evt_to_vpp (svm_msg_q_t * mq, app_session_evt_t * app_evt,
569                            u8 evt_type)
570 {
571   svm_msg_q_lock_and_alloc_msg_w_ring (mq,
572                                        SESSION_MQ_CTRL_EVT_RING,
573                                        SVM_Q_WAIT, &app_evt->msg);
574   app_evt->evt = svm_msg_q_msg_data (mq, &app_evt->msg);
575   clib_memset (app_evt->evt, 0, sizeof (*app_evt->evt));
576   app_evt->evt->event_type = evt_type;
577 }
578
579 static inline void
580 app_send_ctrl_evt_to_vpp (svm_msg_q_t * mq, app_session_evt_t * app_evt)
581 {
582   svm_msg_q_add_and_unlock (mq, &app_evt->msg);
583 }
584
585 /**
586  * Send fifo io event to vpp worker thread
587  *
588  * Because there may be multiple writers to one of vpp's queues, this
589  * protects message allocation and enqueueing.
590  *
591  * @param mq            vpp message queue
592  * @param f             fifo for which the event is sent
593  * @param evt_type      type of event
594  * @param noblock       flag to indicate is request is blocking or not
595  * @return              0 if success, negative integer otherwise
596  */
597 static inline int
598 app_send_io_evt_to_vpp (svm_msg_q_t * mq, u32 session_index, u8 evt_type,
599                         u8 noblock)
600 {
601   session_event_t *evt;
602   svm_msg_q_msg_t msg;
603
604   if (noblock)
605     {
606       if (svm_msg_q_try_lock (mq))
607         return -1;
608       if (PREDICT_FALSE (svm_msg_q_ring_is_full (mq, SESSION_MQ_IO_EVT_RING)
609                          || svm_msg_q_is_full (mq)))
610         {
611           svm_msg_q_unlock (mq);
612           return -2;
613         }
614       msg = svm_msg_q_alloc_msg_w_ring (mq, SESSION_MQ_IO_EVT_RING);
615       evt = (session_event_t *) svm_msg_q_msg_data (mq, &msg);
616       evt->session_index = session_index;
617       evt->event_type = evt_type;
618       svm_msg_q_add_and_unlock (mq, &msg);
619       return 0;
620     }
621   else
622     {
623       svm_msg_q_lock (mq);
624       while (svm_msg_q_ring_is_full (mq, SESSION_MQ_IO_EVT_RING)
625              || svm_msg_q_is_full (mq))
626         svm_msg_q_wait_prod (mq);
627       msg = svm_msg_q_alloc_msg_w_ring (mq, SESSION_MQ_IO_EVT_RING);
628       evt = (session_event_t *) svm_msg_q_msg_data (mq, &msg);
629       evt->session_index = session_index;
630       evt->event_type = evt_type;
631       svm_msg_q_add_and_unlock (mq, &msg);
632       return 0;
633     }
634 }
635
636 always_inline int
637 app_send_dgram_raw (svm_fifo_t * f, app_session_transport_t * at,
638                     svm_msg_q_t * vpp_evt_q, u8 * data, u32 len, u8 evt_type,
639                     u8 do_evt, u8 noblock)
640 {
641   session_dgram_hdr_t hdr;
642   int rv;
643
644   if (svm_fifo_max_enqueue_prod (f) < (sizeof (session_dgram_hdr_t) + len))
645     return 0;
646
647   hdr.data_length = len;
648   hdr.data_offset = 0;
649   clib_memcpy_fast (&hdr.rmt_ip, &at->rmt_ip, sizeof (ip46_address_t));
650   hdr.is_ip4 = at->is_ip4;
651   hdr.rmt_port = at->rmt_port;
652   clib_memcpy_fast (&hdr.lcl_ip, &at->lcl_ip, sizeof (ip46_address_t));
653   hdr.lcl_port = at->lcl_port;
654
655   /* *INDENT-OFF* */
656   svm_fifo_seg_t segs[2] = {{ (u8 *) &hdr, sizeof (hdr) }, { data, len }};
657   /* *INDENT-ON* */
658
659   rv = svm_fifo_enqueue_segments (f, segs, 2, 0 /* allow partial */ );
660   if (PREDICT_FALSE (rv < 0))
661     return 0;
662
663   if (do_evt)
664     {
665       if (svm_fifo_set_event (f))
666         app_send_io_evt_to_vpp (vpp_evt_q, f->shr->master_session_index,
667                                 evt_type, noblock);
668     }
669   return len;
670 }
671
672 always_inline int
673 app_send_dgram (app_session_t * s, u8 * data, u32 len, u8 noblock)
674 {
675   return app_send_dgram_raw (s->tx_fifo, &s->transport, s->vpp_evt_q, data,
676                              len, SESSION_IO_EVT_TX, 1 /* do_evt */ ,
677                              noblock);
678 }
679
680 always_inline int
681 app_send_stream_raw (svm_fifo_t * f, svm_msg_q_t * vpp_evt_q, u8 * data,
682                      u32 len, u8 evt_type, u8 do_evt, u8 noblock)
683 {
684   int rv;
685
686   rv = svm_fifo_enqueue (f, len, data);
687   if (do_evt)
688     {
689       if (rv > 0 && svm_fifo_set_event (f))
690         app_send_io_evt_to_vpp (vpp_evt_q, f->shr->master_session_index,
691                                 evt_type, noblock);
692     }
693   return rv;
694 }
695
696 always_inline int
697 app_send_stream (app_session_t * s, u8 * data, u32 len, u8 noblock)
698 {
699   return app_send_stream_raw (s->tx_fifo, s->vpp_evt_q, data, len,
700                               SESSION_IO_EVT_TX, 1 /* do_evt */ , noblock);
701 }
702
703 always_inline int
704 app_send (app_session_t * s, u8 * data, u32 len, u8 noblock)
705 {
706   if (s->is_dgram)
707     return app_send_dgram (s, data, len, noblock);
708   return app_send_stream (s, data, len, noblock);
709 }
710
711 always_inline int
712 app_recv_dgram_raw (svm_fifo_t * f, u8 * buf, u32 len,
713                     app_session_transport_t * at, u8 clear_evt, u8 peek)
714 {
715   session_dgram_pre_hdr_t ph;
716   u32 max_deq;
717   int rv;
718
719   max_deq = svm_fifo_max_dequeue_cons (f);
720   if (max_deq <= sizeof (session_dgram_hdr_t))
721     {
722       if (clear_evt)
723         svm_fifo_unset_event (f);
724       return 0;
725     }
726
727   if (clear_evt)
728     svm_fifo_unset_event (f);
729
730   svm_fifo_peek (f, 0, sizeof (ph), (u8 *) & ph);
731   ASSERT (ph.data_length >= ph.data_offset);
732
733   /* Check if we have the full dgram */
734   if (max_deq < (ph.data_length + SESSION_CONN_HDR_LEN)
735       && len >= ph.data_length)
736     return 0;
737
738   svm_fifo_peek (f, sizeof (ph), sizeof (*at), (u8 *) at);
739   len = clib_min (len, ph.data_length - ph.data_offset);
740   rv = svm_fifo_peek (f, ph.data_offset + SESSION_CONN_HDR_LEN, len, buf);
741   if (peek)
742     return rv;
743
744   /* Discards data that did not fit in buffer */
745   svm_fifo_dequeue_drop (f, ph.data_length + SESSION_CONN_HDR_LEN);
746
747   return rv;
748 }
749
750 always_inline int
751 app_recv_dgram (app_session_t * s, u8 * buf, u32 len)
752 {
753   return app_recv_dgram_raw (s->rx_fifo, buf, len, &s->transport, 1, 0);
754 }
755
756 always_inline int
757 app_recv_stream_raw (svm_fifo_t * f, u8 * buf, u32 len, u8 clear_evt, u8 peek)
758 {
759   if (clear_evt)
760     svm_fifo_unset_event (f);
761
762   if (peek)
763     return svm_fifo_peek (f, 0, len, buf);
764
765   return svm_fifo_dequeue (f, len, buf);
766 }
767
768 always_inline int
769 app_recv_stream (app_session_t * s, u8 * buf, u32 len)
770 {
771   return app_recv_stream_raw (s->rx_fifo, buf, len, 1, 0);
772 }
773
774 always_inline int
775 app_recv (app_session_t * s, u8 * data, u32 len)
776 {
777   if (s->is_dgram)
778     return app_recv_dgram (s, data, len);
779   return app_recv_stream (s, data, len);
780 }
781
782 /* *INDENT-OFF* */
783 static char *session_error_str[] = {
784 #define _(sym, str) str,
785     foreach_session_error
786 #undef _
787 };
788 /* *INDENT-ON* */
789
790 static inline u8 *
791 format_session_error (u8 * s, va_list * args)
792 {
793   session_error_t error = va_arg (*args, session_error_t);
794   if (-error >= 0 && -error < SESSION_N_ERRORS)
795     s = format (s, "%s", session_error_str[-error]);
796   else
797     s = format (s, "invalid session err %u", -error);
798   return s;
799 }
800
801 /*
802  * Socket API messages
803  */
804
805 typedef enum app_sapi_msg_type
806 {
807   APP_SAPI_MSG_TYPE_NONE,
808   APP_SAPI_MSG_TYPE_ATTACH,
809   APP_SAPI_MSG_TYPE_ATTACH_REPLY,
810   APP_SAPI_MSG_TYPE_ADD_DEL_WORKER,
811   APP_SAPI_MSG_TYPE_ADD_DEL_WORKER_REPLY,
812   APP_SAPI_MSG_TYPE_SEND_FDS,
813 } __clib_packed app_sapi_msg_type_e;
814
815 typedef struct app_sapi_attach_msg_
816 {
817   u8 name[64];
818   u64 options[18];
819 } __clib_packed app_sapi_attach_msg_t;
820
821 STATIC_ASSERT (sizeof (u64) * APP_OPTIONS_N_OPTIONS <=
822                sizeof (((app_sapi_attach_msg_t *) 0)->options),
823                "Out of options, fix message definition");
824
825 typedef struct app_sapi_attach_reply_msg_
826 {
827   i32 retval;
828   u32 app_index;
829   u64 app_mq;
830   u64 vpp_ctrl_mq;
831   u64 segment_handle;
832   u32 api_client_handle;
833   u8 vpp_ctrl_mq_thread;
834   u8 n_fds;
835   u8 fd_flags;
836 } __clib_packed app_sapi_attach_reply_msg_t;
837
838 typedef struct app_sapi_worker_add_del_msg_
839 {
840   u32 app_index;
841   u32 wrk_index;
842   u8 is_add;
843 } __clib_packed app_sapi_worker_add_del_msg_t;
844
845 typedef struct app_sapi_worker_add_del_reply_msg_
846 {
847   i32 retval;
848   u32 wrk_index;
849   u64 app_event_queue_address;
850   u64 segment_handle;
851   u32 api_client_handle;
852   u8 n_fds;
853   u8 fd_flags;
854   u8 is_add;
855 } __clib_packed app_sapi_worker_add_del_reply_msg_t;
856
857 typedef struct app_sapi_msg_
858 {
859   app_sapi_msg_type_e type;
860   union
861   {
862     app_sapi_attach_msg_t attach;
863     app_sapi_attach_reply_msg_t attach_reply;
864     app_sapi_worker_add_del_msg_t worker_add_del;
865     app_sapi_worker_add_del_reply_msg_t worker_add_del_reply;
866   };
867 } __clib_packed app_sapi_msg_t;
868
869 static inline void
870 session_endpoint_alloc_ext_cfg (session_endpoint_cfg_t *sep_ext,
871                                 transport_endpt_ext_cfg_type_t type)
872 {
873   transport_endpt_ext_cfg_t *cfg;
874   u32 cfg_size;
875
876   cfg_size = sizeof (transport_endpt_ext_cfg_t);
877   cfg = clib_mem_alloc (cfg_size);
878   clib_memset (cfg, 0, cfg_size);
879   cfg->type = type;
880   sep_ext->ext_cfg = cfg;
881 }
882
883 #endif /* __included_uri_h__ */
884
885 /*
886  * fd.io coding-style-patch-verification: ON
887  *
888  * Local Variables:
889  * eval: (c-set-style "gnu")
890  * End:
891  */