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