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