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