vcl: support inter worker rpc
[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
26 #define foreach_session_endpoint_fields                         \
27   foreach_transport_endpoint_cfg_fields                         \
28   _(u8, transport_proto)                                        \
29
30 typedef struct _session_endpoint
31 {
32 #define _(type, name) type name;
33   foreach_session_endpoint_fields
34 #undef _
35 } session_endpoint_t;
36
37 typedef struct _session_endpoint_cfg
38 {
39 #define _(type, name) type name;
40   foreach_session_endpoint_fields
41 #undef _
42   u32 app_wrk_index;
43   u32 opaque;
44   u32 ns_index;
45   u8 original_tp;
46   u8 *hostname;
47   u64 parent_handle;
48   u32 ckpair_index;
49   u8 crypto_engine;
50   u8 flags;
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,        \
81   .ip = SESSION_IP46_ZERO,                      \
82   .fib_index = ENDPOINT_INVALID_INDEX,          \
83   .is_ip4 = 0,                                  \
84   .port = 0,                                    \
85   .peer = TRANSPORT_ENDPOINT_NULL,              \
86   .transport_proto = 0,                         \
87   .app_wrk_index = ENDPOINT_INVALID_INDEX,      \
88   .opaque = ENDPOINT_INVALID_INDEX,             \
89   .hostname = 0,                                \
90   .parent_handle = SESSION_INVALID_HANDLE,      \
91   .ckpair_index = 0                             \
92 }
93
94 #define session_endpoint_to_transport(_sep) ((transport_endpoint_t *)_sep)
95 #define session_endpoint_to_transport_cfg(_sep)         \
96   ((transport_endpoint_cfg_t *)_sep)
97
98 always_inline u8
99 session_endpoint_fib_proto (session_endpoint_t * sep)
100 {
101   return sep->is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
102 }
103
104 static inline u8
105 session_endpoint_is_local (session_endpoint_t * sep)
106 {
107   return (ip_is_zero (&sep->ip, sep->is_ip4)
108           || ip_is_local_host (&sep->ip, sep->is_ip4));
109 }
110
111 static inline u8
112 session_endpoint_is_zero (session_endpoint_t * sep)
113 {
114   return ip_is_zero (&sep->ip, sep->is_ip4);
115 }
116
117 typedef u8 session_type_t;
118 typedef u64 session_handle_t;
119
120 typedef enum
121 {
122   SESSION_CLEANUP_TRANSPORT,
123   SESSION_CLEANUP_SESSION,
124 } session_cleanup_ntf_t;
125
126 typedef enum session_ft_action_
127 {
128   SESSION_FT_ACTION_ENQUEUED,
129   SESSION_FT_ACTION_DEQUEUED,
130   SESSION_FT_ACTION_N_ACTIONS
131 } session_ft_action_t;
132
133 /*
134  * Session states
135  */
136 #define foreach_session_state                           \
137   _(CREATED, "created")                                 \
138   _(LISTENING, "listening")                             \
139   _(CONNECTING, "connecting")                           \
140   _(ACCEPTING, "accepting")                             \
141   _(READY, "ready")                                     \
142   _(OPENED, "opened")                                   \
143   _(TRANSPORT_CLOSING, "transport-closing")             \
144   _(CLOSING, "closing")                                 \
145   _(APP_CLOSED, "app-closed")                           \
146   _(TRANSPORT_CLOSED, "transport-closed")               \
147   _(CLOSED, "closed")                                   \
148   _(TRANSPORT_DELETED, "transport-deleted")             \
149
150 typedef enum
151 {
152 #define _(sym, str) SESSION_STATE_ ## sym,
153   foreach_session_state
154 #undef _
155     SESSION_N_STATES,
156 } session_state_t;
157
158 #define foreach_session_flag                            \
159   _(RX_EVT, "rx-event")                                 \
160   _(PROXY, "proxy")                                     \
161   _(CUSTOM_TX, "custom-tx")                             \
162   _(IS_MIGRATING, "migrating")                          \
163   _(UNIDIRECTIONAL, "unidirectional")                   \
164   _(CUSTOM_FIFO_TUNING, "custom-fifo-tuning")           \
165
166 typedef enum session_flags_bits_
167 {
168 #define _(sym, str) SESSION_F_BIT_ ## sym,
169   foreach_session_flag
170 #undef _
171   SESSION_N_FLAGS
172 } session_flag_bits_t;
173
174 typedef enum session_flags_
175 {
176 #define _(sym, str) SESSION_F_ ## sym = 1 << SESSION_F_BIT_ ## sym,
177   foreach_session_flag
178 #undef _
179 } session_flags_t;
180
181 typedef struct session_
182 {
183   /** Pointers to rx/tx buffers. Once allocated, these do not move */
184   svm_fifo_t *rx_fifo;
185   svm_fifo_t *tx_fifo;
186
187   /** Type built from transport and network protocol types */
188   session_type_t session_type;
189
190   /** State in session layer state machine. See @ref session_state_t */
191   volatile u8 session_state;
192
193   /** Index in thread pool where session was allocated */
194   u32 session_index;
195
196   /** Index of the app worker that owns the session */
197   u32 app_wrk_index;
198
199   /** Index of the thread that allocated the session */
200   u8 thread_index;
201
202   /** Session flags. See @ref session_flags_t */
203   u32 flags;
204
205   /** Index of the transport connection associated to the session */
206   u32 connection_index;
207
208   /** Index of application that owns the listener. Set only if a listener */
209   u32 app_index;
210
211   union
212   {
213     /** Parent listener session index if the result of an accept */
214     session_handle_t listener_handle;
215
216     /** App listener index in app's listener pool if a listener */
217     u32 al_index;
218   };
219
220   /** Opaque, for general use */
221   u32 opaque;
222
223     CLIB_CACHE_LINE_ALIGN_MARK (pad);
224 } session_t;
225
226 always_inline session_type_t
227 session_type_from_proto_and_ip (transport_proto_t proto, u8 is_ip4)
228 {
229   return (proto << 1 | is_ip4);
230 }
231
232 always_inline transport_proto_t
233 session_type_transport_proto (session_type_t st)
234 {
235   return (st >> 1);
236 }
237
238 always_inline u8
239 session_type_is_ip4 (session_type_t st)
240 {
241   return (st & 1);
242 }
243
244 always_inline transport_proto_t
245 session_get_transport_proto (session_t * s)
246 {
247   return (s->session_type >> 1);
248 }
249
250 always_inline fib_protocol_t
251 session_get_fib_proto (session_t * s)
252 {
253   u8 is_ip4 = s->session_type & 1;
254   return (is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6);
255 }
256
257 always_inline u8
258 session_has_transport (session_t * s)
259 {
260   return (session_get_transport_proto (s) != TRANSPORT_PROTO_NONE);
261 }
262
263 static inline transport_service_type_t
264 session_transport_service_type (session_t * s)
265 {
266   transport_proto_t tp;
267   tp = session_get_transport_proto (s);
268   return transport_protocol_service_type (tp);
269 }
270
271 static inline transport_tx_fn_type_t
272 session_transport_tx_fn_type (session_t * s)
273 {
274   transport_proto_t tp;
275   tp = session_get_transport_proto (s);
276   return transport_protocol_tx_fn_type (tp);
277 }
278
279 static inline u8
280 session_tx_is_dgram (session_t * s)
281 {
282   return (session_transport_tx_fn_type (s) == TRANSPORT_TX_DGRAM);
283 }
284
285 always_inline session_handle_t
286 session_handle (session_t * s)
287 {
288   return ((u64) s->thread_index << 32) | (u64) s->session_index;
289 }
290
291 always_inline u32
292 session_index_from_handle (session_handle_t handle)
293 {
294   return handle & 0xFFFFFFFF;
295 }
296
297 always_inline u32
298 session_thread_from_handle (session_handle_t handle)
299 {
300   return handle >> 32;
301 }
302
303 always_inline void
304 session_parse_handle (session_handle_t handle, u32 * index,
305                       u32 * thread_index)
306 {
307   *index = session_index_from_handle (handle);
308   *thread_index = session_thread_from_handle (handle);
309 }
310
311 static inline session_handle_t
312 session_make_handle (u32 session_index, u32 data)
313 {
314   return (((u64) data << 32) | (u64) session_index);
315 }
316
317 always_inline u32
318 session_handle_index (session_handle_t ho_handle)
319 {
320   return (ho_handle & 0xffffffff);
321 }
322
323 always_inline u32
324 session_handle_data (session_handle_t ho_handle)
325 {
326   return (ho_handle >> 32);
327 }
328
329 typedef enum
330 {
331   SESSION_IO_EVT_RX,
332   SESSION_IO_EVT_TX,
333   SESSION_IO_EVT_TX_FLUSH,
334   SESSION_IO_EVT_BUILTIN_RX,
335   SESSION_IO_EVT_BUILTIN_TX,
336   SESSION_CTRL_EVT_RPC,
337   SESSION_CTRL_EVT_CLOSE,
338   SESSION_CTRL_EVT_RESET,
339   SESSION_CTRL_EVT_BOUND,
340   SESSION_CTRL_EVT_UNLISTEN_REPLY,
341   SESSION_CTRL_EVT_ACCEPTED,
342   SESSION_CTRL_EVT_ACCEPTED_REPLY,
343   SESSION_CTRL_EVT_CONNECTED,
344   SESSION_CTRL_EVT_DISCONNECTED,
345   SESSION_CTRL_EVT_DISCONNECTED_REPLY,
346   SESSION_CTRL_EVT_RESET_REPLY,
347   SESSION_CTRL_EVT_REQ_WORKER_UPDATE,
348   SESSION_CTRL_EVT_WORKER_UPDATE,
349   SESSION_CTRL_EVT_WORKER_UPDATE_REPLY,
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_evt_type_t;
363
364 #define foreach_session_ctrl_evt                                \
365   _(LISTEN, listen)                                             \
366   _(LISTEN_URI, listen_uri)                                     \
367   _(BOUND, bound)                                               \
368   _(UNLISTEN, unlisten)                                         \
369   _(UNLISTEN_REPLY, unlisten_reply)                             \
370   _(ACCEPTED, accepted)                                         \
371   _(ACCEPTED_REPLY, accepted_reply)                             \
372   _(CONNECT, connect)                                           \
373   _(CONNECT_URI, connect_uri)                                   \
374   _(CONNECTED, connected)                                       \
375   _(DISCONNECT, disconnect)                                     \
376   _(DISCONNECTED, disconnected)                                 \
377   _(DISCONNECTED_REPLY, disconnected_reply)                     \
378   _(RESET_REPLY, reset_reply)                                   \
379   _(REQ_WORKER_UPDATE, req_worker_update)                       \
380   _(WORKER_UPDATE, worker_update)                               \
381   _(WORKER_UPDATE_REPLY, worker_update_reply)                   \
382   _(APP_DETACH, app_detach)                                     \
383   _(APP_ADD_SEGMENT, app_add_segment)                           \
384   _(APP_DEL_SEGMENT, app_del_segment)                           \
385   _(MIGRATED, migrated)                                         \
386   _(CLEANUP, cleanup)                                           \
387
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
482 typedef enum session_error_p_
483 {
484 #define _(sym, str) SESSION_EP_##sym,
485   foreach_session_error
486 #undef _
487   SESSION_N_ERRORS
488 } session_error_p_t;
489
490 typedef enum session_error_
491 {
492 #define _(sym, str) SESSION_E_##sym = -SESSION_EP_##sym,
493   foreach_session_error
494 #undef _
495 } session_error_t;
496
497 /* Maintained for compatibility. Will be deprecated */
498 #define SESSION_ERROR_SEG_CREATE SESSION_E_SEG_CREATE
499 #define SESSION_ERROR_NO_SPACE SESSION_E_SEG_NO_SPACE
500 #define SESSION_ERROR_NEW_SEG_NO_SPACE SESSION_E_SEG_NO_SPACE2
501
502 #endif /* SRC_VNET_SESSION_SESSION_TYPES_H_ */
503
504 /*
505  * fd.io coding-style-patch-verification: ON
506  *
507  * Local Variables:
508  * eval: (c-set-style "gnu")
509  * End:
510  */