vcl session: extended connect/listen configuration
[vpp.git] / src / vnet / session / session_types.h
1 /*
2  * Copyright (c) 2017-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
16 #ifndef SRC_VNET_SESSION_SESSION_TYPES_H_
17 #define SRC_VNET_SESSION_SESSION_TYPES_H_
18
19 #include <svm/svm_fifo.h>
20 #include <vnet/session/transport_types.h>
21
22 #define SESSION_INVALID_INDEX ((u32)~0)
23 #define SESSION_INVALID_HANDLE ((u64)~0)
24 #define SESSION_CTRL_MSG_MAX_SIZE 86
25 #define SESSION_NODE_FRAME_SIZE 128
26
27 #define foreach_session_endpoint_fields                         \
28   foreach_transport_endpoint_cfg_fields                         \
29   _(u8, transport_proto)                                        \
30
31 typedef struct _session_endpoint
32 {
33 #define _(type, name) type name;
34   foreach_session_endpoint_fields
35 #undef _
36 } session_endpoint_t;
37
38 typedef struct _session_endpoint_cfg
39 {
40 #define _(type, name) type name;
41   foreach_session_endpoint_fields
42 #undef _
43   u32 app_wrk_index;
44   u32 opaque;
45   u32 ns_index;
46   u8 original_tp;
47   u8 *hostname;
48   u64 parent_handle;
49   u32 ckpair_index;
50   u8 crypto_engine;
51   u8 flags;
52   transport_endpt_ext_cfg_t *ext_cfg;
53 } session_endpoint_cfg_t;
54
55 #define SESSION_IP46_ZERO                       \
56 {                                               \
57     .ip6 = {                                    \
58         { 0, 0, },                              \
59     },                                          \
60 }
61
62 #define TRANSPORT_ENDPOINT_NULL                 \
63 {                                               \
64   .sw_if_index = ENDPOINT_INVALID_INDEX,        \
65   .ip = SESSION_IP46_ZERO,                      \
66   .fib_index = ENDPOINT_INVALID_INDEX,          \
67   .is_ip4 = 0,                                  \
68   .port = 0,                                    \
69 }
70 #define SESSION_ENDPOINT_NULL                   \
71 {                                               \
72   .sw_if_index = ENDPOINT_INVALID_INDEX,        \
73   .ip = SESSION_IP46_ZERO,                      \
74   .fib_index = ENDPOINT_INVALID_INDEX,          \
75   .is_ip4 = 0,                                  \
76   .port = 0,                                    \
77   .peer = TRANSPORT_ENDPOINT_NULL,              \
78   .transport_proto = 0,                         \
79 }
80 #define SESSION_ENDPOINT_CFG_NULL                                             \
81   {                                                                           \
82     .sw_if_index = ENDPOINT_INVALID_INDEX, .ip = SESSION_IP46_ZERO,           \
83     .fib_index = ENDPOINT_INVALID_INDEX, .is_ip4 = 0, .port = 0,              \
84     .peer = TRANSPORT_ENDPOINT_NULL, .transport_proto = 0,                    \
85     .app_wrk_index = ENDPOINT_INVALID_INDEX,                                  \
86     .opaque = ENDPOINT_INVALID_INDEX, .hostname = 0,                          \
87     .parent_handle = SESSION_INVALID_HANDLE, .ckpair_index = 0, .ext_cfg = 0, \
88   }
89
90 #define session_endpoint_to_transport(_sep) ((transport_endpoint_t *)_sep)
91 #define session_endpoint_to_transport_cfg(_sep)         \
92   ((transport_endpoint_cfg_t *)_sep)
93
94 always_inline u8
95 session_endpoint_fib_proto (session_endpoint_t * sep)
96 {
97   return sep->is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
98 }
99
100 static inline u8
101 session_endpoint_is_local (session_endpoint_t * sep)
102 {
103   return (ip_is_zero (&sep->ip, sep->is_ip4)
104           || ip_is_local_host (&sep->ip, sep->is_ip4));
105 }
106
107 static inline u8
108 session_endpoint_is_zero (session_endpoint_t * sep)
109 {
110   return ip_is_zero (&sep->ip, sep->is_ip4);
111 }
112
113 typedef u8 session_type_t;
114 typedef u64 session_handle_t;
115
116 typedef enum
117 {
118   SESSION_CLEANUP_TRANSPORT,
119   SESSION_CLEANUP_SESSION,
120 } session_cleanup_ntf_t;
121
122 typedef enum session_ft_action_
123 {
124   SESSION_FT_ACTION_ENQUEUED,
125   SESSION_FT_ACTION_DEQUEUED,
126   SESSION_FT_ACTION_N_ACTIONS
127 } session_ft_action_t;
128
129 /*
130  * Session states
131  */
132 #define foreach_session_state                           \
133   _(CREATED, "created")                                 \
134   _(LISTENING, "listening")                             \
135   _(CONNECTING, "connecting")                           \
136   _(ACCEPTING, "accepting")                             \
137   _(READY, "ready")                                     \
138   _(OPENED, "opened")                                   \
139   _(TRANSPORT_CLOSING, "transport-closing")             \
140   _(CLOSING, "closing")                                 \
141   _(APP_CLOSED, "app-closed")                           \
142   _(TRANSPORT_CLOSED, "transport-closed")               \
143   _(CLOSED, "closed")                                   \
144   _(TRANSPORT_DELETED, "transport-deleted")             \
145
146 typedef enum
147 {
148 #define _(sym, str) SESSION_STATE_ ## sym,
149   foreach_session_state
150 #undef _
151     SESSION_N_STATES,
152 } session_state_t;
153
154 #define foreach_session_flag                            \
155   _(RX_EVT, "rx-event")                                 \
156   _(PROXY, "proxy")                                     \
157   _(CUSTOM_TX, "custom-tx")                             \
158   _(IS_MIGRATING, "migrating")                          \
159   _(UNIDIRECTIONAL, "unidirectional")                   \
160   _(CUSTOM_FIFO_TUNING, "custom-fifo-tuning")           \
161
162 typedef enum session_flags_bits_
163 {
164 #define _(sym, str) SESSION_F_BIT_ ## sym,
165   foreach_session_flag
166 #undef _
167   SESSION_N_FLAGS
168 } session_flag_bits_t;
169
170 typedef enum session_flags_
171 {
172 #define _(sym, str) SESSION_F_ ## sym = 1 << SESSION_F_BIT_ ## sym,
173   foreach_session_flag
174 #undef _
175 } session_flags_t;
176
177 typedef struct session_
178 {
179   /** Pointers to rx/tx buffers. Once allocated, these do not move */
180   svm_fifo_t *rx_fifo;
181   svm_fifo_t *tx_fifo;
182
183   /** Type built from transport and network protocol types */
184   session_type_t session_type;
185
186   /** State in session layer state machine. See @ref session_state_t */
187   volatile u8 session_state;
188
189   /** Index in thread pool where session was allocated */
190   u32 session_index;
191
192   /** Index of the app worker that owns the session */
193   u32 app_wrk_index;
194
195   /** Index of the thread that allocated the session */
196   u8 thread_index;
197
198   /** Session flags. See @ref session_flags_t */
199   u32 flags;
200
201   /** Index of the transport connection associated to the session */
202   u32 connection_index;
203
204   /** Index of application that owns the listener. Set only if a listener */
205   u32 app_index;
206
207   union
208   {
209     /** Parent listener session index if the result of an accept */
210     session_handle_t listener_handle;
211
212     /** App listener index in app's listener pool if a listener */
213     u32 al_index;
214   };
215
216   /** Opaque, for general use */
217   u32 opaque;
218
219     CLIB_CACHE_LINE_ALIGN_MARK (pad);
220 } session_t;
221
222 always_inline session_type_t
223 session_type_from_proto_and_ip (transport_proto_t proto, u8 is_ip4)
224 {
225   return (proto << 1 | is_ip4);
226 }
227
228 always_inline transport_proto_t
229 session_type_transport_proto (session_type_t st)
230 {
231   return (st >> 1);
232 }
233
234 always_inline u8
235 session_type_is_ip4 (session_type_t st)
236 {
237   return (st & 1);
238 }
239
240 always_inline transport_proto_t
241 session_get_transport_proto (session_t * s)
242 {
243   return (s->session_type >> 1);
244 }
245
246 always_inline fib_protocol_t
247 session_get_fib_proto (session_t * s)
248 {
249   u8 is_ip4 = s->session_type & 1;
250   return (is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6);
251 }
252
253 always_inline u8
254 session_has_transport (session_t * s)
255 {
256   return (session_get_transport_proto (s) != TRANSPORT_PROTO_NONE);
257 }
258
259 static inline transport_service_type_t
260 session_transport_service_type (session_t * s)
261 {
262   transport_proto_t tp;
263   tp = session_get_transport_proto (s);
264   return transport_protocol_service_type (tp);
265 }
266
267 static inline transport_tx_fn_type_t
268 session_transport_tx_fn_type (session_t * s)
269 {
270   transport_proto_t tp;
271   tp = session_get_transport_proto (s);
272   return transport_protocol_tx_fn_type (tp);
273 }
274
275 static inline u8
276 session_tx_is_dgram (session_t * s)
277 {
278   return (session_transport_tx_fn_type (s) == TRANSPORT_TX_DGRAM);
279 }
280
281 always_inline session_handle_t
282 session_handle (session_t * s)
283 {
284   return ((u64) s->thread_index << 32) | (u64) s->session_index;
285 }
286
287 always_inline u32
288 session_index_from_handle (session_handle_t handle)
289 {
290   return handle & 0xFFFFFFFF;
291 }
292
293 always_inline u32
294 session_thread_from_handle (session_handle_t handle)
295 {
296   return handle >> 32;
297 }
298
299 always_inline void
300 session_parse_handle (session_handle_t handle, u32 * index,
301                       u32 * thread_index)
302 {
303   *index = session_index_from_handle (handle);
304   *thread_index = session_thread_from_handle (handle);
305 }
306
307 static inline session_handle_t
308 session_make_handle (u32 session_index, u32 data)
309 {
310   return (((u64) data << 32) | (u64) session_index);
311 }
312
313 always_inline u32
314 session_handle_index (session_handle_t ho_handle)
315 {
316   return (ho_handle & 0xffffffff);
317 }
318
319 always_inline u32
320 session_handle_data (session_handle_t ho_handle)
321 {
322   return (ho_handle >> 32);
323 }
324
325 typedef enum
326 {
327   SESSION_IO_EVT_RX,
328   SESSION_IO_EVT_TX,
329   SESSION_IO_EVT_TX_FLUSH,
330   SESSION_IO_EVT_BUILTIN_RX,
331   SESSION_IO_EVT_BUILTIN_TX,
332   SESSION_CTRL_EVT_RPC,
333   SESSION_CTRL_EVT_CLOSE,
334   SESSION_CTRL_EVT_RESET,
335   SESSION_CTRL_EVT_BOUND,
336   SESSION_CTRL_EVT_UNLISTEN_REPLY,
337   SESSION_CTRL_EVT_ACCEPTED,
338   SESSION_CTRL_EVT_ACCEPTED_REPLY,
339   SESSION_CTRL_EVT_CONNECTED,
340   SESSION_CTRL_EVT_DISCONNECTED,
341   SESSION_CTRL_EVT_DISCONNECTED_REPLY,
342   SESSION_CTRL_EVT_RESET_REPLY,
343   SESSION_CTRL_EVT_REQ_WORKER_UPDATE,
344   SESSION_CTRL_EVT_WORKER_UPDATE,
345   SESSION_CTRL_EVT_WORKER_UPDATE_REPLY,
346   SESSION_CTRL_EVT_DISCONNECT,
347   SESSION_CTRL_EVT_CONNECT,
348   SESSION_CTRL_EVT_CONNECT_URI,
349   SESSION_CTRL_EVT_LISTEN,
350   SESSION_CTRL_EVT_LISTEN_URI,
351   SESSION_CTRL_EVT_UNLISTEN,
352   SESSION_CTRL_EVT_APP_DETACH,
353   SESSION_CTRL_EVT_APP_ADD_SEGMENT,
354   SESSION_CTRL_EVT_APP_DEL_SEGMENT,
355   SESSION_CTRL_EVT_MIGRATED,
356   SESSION_CTRL_EVT_CLEANUP,
357   SESSION_CTRL_EVT_APP_WRK_RPC,
358   SESSION_CTRL_EVT_TRANSPORT_ATTR,
359   SESSION_CTRL_EVT_TRANSPORT_ATTR_REPLY,
360 } session_evt_type_t;
361
362 #define foreach_session_ctrl_evt                                              \
363   _ (LISTEN, listen)                                                          \
364   _ (LISTEN_URI, listen_uri)                                                  \
365   _ (BOUND, bound)                                                            \
366   _ (UNLISTEN, unlisten)                                                      \
367   _ (UNLISTEN_REPLY, unlisten_reply)                                          \
368   _ (ACCEPTED, accepted)                                                      \
369   _ (ACCEPTED_REPLY, accepted_reply)                                          \
370   _ (CONNECT, connect)                                                        \
371   _ (CONNECT_URI, connect_uri)                                                \
372   _ (CONNECTED, connected)                                                    \
373   _ (DISCONNECT, disconnect)                                                  \
374   _ (DISCONNECTED, disconnected)                                              \
375   _ (DISCONNECTED_REPLY, disconnected_reply)                                  \
376   _ (RESET_REPLY, reset_reply)                                                \
377   _ (REQ_WORKER_UPDATE, req_worker_update)                                    \
378   _ (WORKER_UPDATE, worker_update)                                            \
379   _ (WORKER_UPDATE_REPLY, worker_update_reply)                                \
380   _ (APP_DETACH, app_detach)                                                  \
381   _ (APP_ADD_SEGMENT, app_add_segment)                                        \
382   _ (APP_DEL_SEGMENT, app_del_segment)                                        \
383   _ (MIGRATED, migrated)                                                      \
384   _ (CLEANUP, cleanup)                                                        \
385   _ (APP_WRK_RPC, app_wrk_rpc)                                                \
386   _ (TRANSPORT_ATTR, transport_attr)                                          \
387   _ (TRANSPORT_ATTR_REPLY, transport_attr_reply)                              \
388 /* Deprecated and will be removed. Use types above */
389 #define FIFO_EVENT_APP_RX SESSION_IO_EVT_RX
390 #define FIFO_EVENT_APP_TX SESSION_IO_EVT_TX
391 #define FIFO_EVENT_DISCONNECT SESSION_CTRL_EVT_CLOSE
392 #define FIFO_EVENT_BUILTIN_RX SESSION_IO_EVT_BUILTIN_RX
393 #define FIFO_EVENT_BUILTIN_TX SESSION_IO_EVT_BUILTIN_TX
394
395 typedef enum
396 {
397   SESSION_MQ_IO_EVT_RING,
398   SESSION_MQ_CTRL_EVT_RING,
399   SESSION_MQ_N_RINGS
400 } session_mq_rings_e;
401
402 typedef struct
403 {
404   void *fp;
405   void *arg;
406 } session_rpc_args_t;
407
408 typedef struct
409 {
410   u8 event_type;
411   u8 postponed;
412   union
413   {
414     u32 session_index;
415     session_handle_t session_handle;
416     session_rpc_args_t rpc_args;
417     u32 ctrl_data_index;
418     struct
419     {
420       u8 data[0];
421     };
422   };
423 } __clib_packed session_event_t;
424
425 #define SESSION_MSG_NULL { }
426
427 typedef struct session_dgram_pre_hdr_
428 {
429   u32 data_length;
430   u32 data_offset;
431 } session_dgram_pre_hdr_t;
432
433 typedef struct session_dgram_header_
434 {
435   u32 data_length;
436   u32 data_offset;
437   ip46_address_t rmt_ip;
438   ip46_address_t lcl_ip;
439   u16 rmt_port;
440   u16 lcl_port;
441   u8 is_ip4;
442 } __clib_packed session_dgram_hdr_t;
443
444 #define SESSION_CONN_ID_LEN 37
445 #define SESSION_CONN_HDR_LEN 45
446
447 STATIC_ASSERT (sizeof (session_dgram_hdr_t) == (SESSION_CONN_ID_LEN + 8),
448                "session conn id wrong length");
449
450 #define foreach_session_error                                                 \
451   _ (NONE, "no error")                                                        \
452   _ (UNKNOWN, "generic/unknown error")                                        \
453   _ (REFUSED, "refused")                                                      \
454   _ (TIMEDOUT, "timedout")                                                    \
455   _ (ALLOC, "obj/memory allocation error")                                    \
456   _ (OWNER, "object not owned by application")                                \
457   _ (NOROUTE, "no route")                                                     \
458   _ (NOINTF, "no resolving interface")                                        \
459   _ (NOIP, "no ip for lcl interface")                                         \
460   _ (NOPORT, "no lcl port")                                                   \
461   _ (NOSUPPORT, "not supported")                                              \
462   _ (NOLISTEN, "not listening")                                               \
463   _ (NOSESSION, "session does not exist")                                     \
464   _ (NOAPP, "app not attached")                                               \
465   _ (PORTINUSE, "lcl port in use")                                            \
466   _ (IPINUSE, "ip in use")                                                    \
467   _ (ALREADY_LISTENING, "ip port pair already listened on")                   \
468   _ (INVALID_RMT_IP, "invalid remote ip")                                     \
469   _ (INVALID_APPWRK, "invalid app worker")                                    \
470   _ (INVALID_NS, "invalid namespace")                                         \
471   _ (SEG_NO_SPACE, "Couldn't allocate a fifo pair")                           \
472   _ (SEG_NO_SPACE2, "Created segment, couldn't allocate a fifo pair")         \
473   _ (SEG_CREATE, "Couldn't create a new segment")                             \
474   _ (FILTERED, "session filtered")                                            \
475   _ (SCOPE, "scope not supported")                                            \
476   _ (BAPI_NO_FD, "bapi doesn't have a socket fd")                             \
477   _ (BAPI_SEND_FD, "couldn't send fd over bapi socket fd")                    \
478   _ (BAPI_NO_REG, "app bapi registration not found")                          \
479   _ (MQ_MSG_ALLOC, "failed to alloc mq msg")                                  \
480   _ (TLS_HANDSHAKE, "failed tls handshake")                                   \
481   _ (EVENTFD_ALLOC, "failed to alloc eventfd")
482
483 typedef enum session_error_p_
484 {
485 #define _(sym, str) SESSION_EP_##sym,
486   foreach_session_error
487 #undef _
488   SESSION_N_ERRORS
489 } session_error_p_t;
490
491 typedef enum session_error_
492 {
493 #define _(sym, str) SESSION_E_##sym = -SESSION_EP_##sym,
494   foreach_session_error
495 #undef _
496 } session_error_t;
497
498 #define SESSION_CLI_ID_LEN "60"
499 #define SESSION_CLI_STATE_LEN "15"
500
501 /* Maintained for compatibility. Will be deprecated */
502 #define SESSION_ERROR_SEG_CREATE SESSION_E_SEG_CREATE
503 #define SESSION_ERROR_NO_SPACE SESSION_E_SEG_NO_SPACE
504 #define SESSION_ERROR_NEW_SEG_NO_SPACE SESSION_E_SEG_NO_SPACE2
505
506 #endif /* SRC_VNET_SESSION_SESSION_TYPES_H_ */
507
508 /*
509  * fd.io coding-style-patch-verification: ON
510  *
511  * Local Variables:
512  * eval: (c-set-style "gnu")
513  * End:
514  */