e10dceafa1677368ffdc6226490e86fb2207fee7
[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_LOCAL_HANDLE_PREFIX     0x7FFFFFFF
23 #define SESSION_LISTENER_PREFIX         0x5FFFFFFF
24
25 #define foreach_session_endpoint_fields                         \
26   foreach_transport_endpoint_cfg_fields                         \
27   _(u8, transport_proto)                                        \
28
29 typedef struct _session_endpoint
30 {
31 #define _(type, name) type name;
32   foreach_session_endpoint_fields
33 #undef _
34 } session_endpoint_t;
35
36 typedef struct _session_endpoint_cfg
37 {
38 #define _(type, name) type name;
39   foreach_session_endpoint_fields
40 #undef _
41   u32 app_wrk_index;
42   u32 opaque;
43   u8 *hostname;
44 } session_endpoint_cfg_t;
45
46 #define SESSION_IP46_ZERO                       \
47 {                                               \
48     .ip6 = {                                    \
49         { 0, 0, },                              \
50     },                                          \
51 }
52
53 #define TRANSPORT_ENDPOINT_NULL                 \
54 {                                               \
55   .sw_if_index = ENDPOINT_INVALID_INDEX,        \
56   .ip = SESSION_IP46_ZERO,                      \
57   .fib_index = ENDPOINT_INVALID_INDEX,          \
58   .is_ip4 = 0,                                  \
59   .port = 0,                                    \
60 }
61 #define SESSION_ENDPOINT_NULL                   \
62 {                                               \
63   .sw_if_index = ENDPOINT_INVALID_INDEX,        \
64   .ip = SESSION_IP46_ZERO,                      \
65   .fib_index = ENDPOINT_INVALID_INDEX,          \
66   .is_ip4 = 0,                                  \
67   .port = 0,                                    \
68   .peer = TRANSPORT_ENDPOINT_NULL,              \
69   .transport_proto = 0,                         \
70 }
71 #define SESSION_ENDPOINT_CFG_NULL               \
72 {                                               \
73   .sw_if_index = ENDPOINT_INVALID_INDEX,        \
74   .ip = SESSION_IP46_ZERO,                      \
75   .fib_index = ENDPOINT_INVALID_INDEX,          \
76   .is_ip4 = 0,                                  \
77   .port = 0,                                    \
78   .peer = TRANSPORT_ENDPOINT_NULL,              \
79   .transport_proto = 0,                         \
80   .app_wrk_index = ENDPOINT_INVALID_INDEX,      \
81   .opaque = ENDPOINT_INVALID_INDEX,             \
82   .hostname = 0,                                \
83 }
84
85 #define session_endpoint_to_transport(_sep) ((transport_endpoint_t *)_sep)
86 #define session_endpoint_to_transport_cfg(_sep)         \
87   ((transport_endpoint_cfg_t *)_sep)
88
89 always_inline u8
90 session_endpoint_fib_proto (session_endpoint_t * sep)
91 {
92   return sep->is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
93 }
94
95 static inline u8
96 session_endpoint_is_local (session_endpoint_t * sep)
97 {
98   return (ip_is_zero (&sep->ip, sep->is_ip4)
99           || ip_is_local_host (&sep->ip, sep->is_ip4));
100 }
101
102 static inline u8
103 session_endpoint_is_zero (session_endpoint_t * sep)
104 {
105   return ip_is_zero (&sep->ip, sep->is_ip4);
106 }
107
108 typedef u8 session_type_t;
109 typedef u64 session_handle_t;
110
111 /*
112  * Application session state
113  */
114 typedef enum
115 {
116   SESSION_STATE_CREATED,
117   SESSION_STATE_LISTENING,
118   SESSION_STATE_CONNECTING,
119   SESSION_STATE_ACCEPTING,
120   SESSION_STATE_READY,
121   SESSION_STATE_OPENED,
122   SESSION_STATE_TRANSPORT_CLOSING,
123   SESSION_STATE_CLOSING,
124   SESSION_STATE_CLOSED_WAITING,
125   SESSION_STATE_TRANSPORT_CLOSED,
126   SESSION_STATE_CLOSED,
127   SESSION_STATE_N_STATES,
128 } session_state_t;
129
130 typedef struct generic_session_
131 {
132   svm_fifo_t *rx_fifo;          /**< rx fifo */
133   svm_fifo_t *tx_fifo;          /**< tx fifo */
134   session_type_t session_type;  /**< session type */
135   volatile u8 session_state;    /**< session state */
136   u32 session_index;            /**< index in owning pool */
137 } generic_session_t;
138
139 typedef struct session_
140 {
141   /** fifo pointers. Once allocated, these do not move */
142   svm_fifo_t *rx_fifo;
143   svm_fifo_t *tx_fifo;
144
145   /** Type */
146   session_type_t session_type;
147
148   /** State */
149   volatile u8 session_state;
150
151   /** Session index in per_thread pool */
152   u32 session_index;
153
154   /** App worker pool index */
155   u32 app_wrk_index;
156
157   u8 thread_index;
158
159   /** To avoid n**2 "one event per frame" check */
160   u64 enqueue_epoch;
161
162   /** svm segment index where fifos were allocated */
163   u32 svm_segment_index;
164
165   /** Transport specific */
166   u32 connection_index;
167
168   union
169   {
170     /** Parent listener session if the result of an accept */
171     u32 listener_index;
172
173     /** Application index if a listener */
174     u32 app_index;
175   };
176
177   union
178   {
179     /** Transport app index for apps acting as transports */
180     u32 t_app_index;
181
182     /** App listener index */
183     u32 al_index;
184
185     /** Opaque, for general use */
186     u32 opaque;
187   };
188
189     CLIB_CACHE_LINE_ALIGN_MARK (pad);
190 } session_t;
191
192 always_inline session_type_t
193 session_type_from_proto_and_ip (transport_proto_t proto, u8 is_ip4)
194 {
195   return (proto << 1 | is_ip4);
196 }
197
198 always_inline transport_proto_t
199 session_type_transport_proto (session_type_t st)
200 {
201   return (st >> 1);
202 }
203
204 always_inline u8
205 session_type_is_ip4 (session_type_t st)
206 {
207   return (st & 1);
208 }
209
210 always_inline transport_proto_t
211 session_get_transport_proto (session_t * s)
212 {
213   return (s->session_type >> 1);
214 }
215
216 always_inline fib_protocol_t
217 session_get_fib_proto (session_t * s)
218 {
219   u8 is_ip4 = s->session_type & 1;
220   return (is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6);
221 }
222
223 always_inline u8
224 session_has_transport (session_t * s)
225 {
226   return (session_get_transport_proto (s) != TRANSPORT_PROTO_NONE);
227 }
228
229 static inline transport_service_type_t
230 session_transport_service_type (session_t * s)
231 {
232   transport_proto_t tp;
233   tp = session_get_transport_proto (s);
234   return transport_protocol_service_type (tp);
235 }
236
237 static inline transport_tx_fn_type_t
238 session_transport_tx_fn_type (session_t * s)
239 {
240   transport_proto_t tp;
241   tp = session_get_transport_proto (s);
242   return transport_protocol_tx_fn_type (tp);
243 }
244
245 static inline u8
246 session_tx_is_dgram (session_t * s)
247 {
248   return (session_transport_tx_fn_type (s) == TRANSPORT_TX_DGRAM);
249 }
250
251 always_inline session_handle_t
252 session_handle (session_t * s)
253 {
254   return ((u64) s->thread_index << 32) | (u64) s->session_index;
255 }
256
257 always_inline u32
258 session_index_from_handle (session_handle_t handle)
259 {
260   return handle & 0xFFFFFFFF;
261 }
262
263 always_inline u32
264 session_thread_from_handle (session_handle_t handle)
265 {
266   return handle >> 32;
267 }
268
269 always_inline void
270 session_parse_handle (session_handle_t handle, u32 * index,
271                       u32 * thread_index)
272 {
273   *index = session_index_from_handle (handle);
274   *thread_index = session_thread_from_handle (handle);
275 }
276
277 always_inline u8
278 session_handle_is_local (session_handle_t handle)
279 {
280   if ((handle >> 32) == SESSION_LOCAL_HANDLE_PREFIX)
281     return 1;
282   return 0;
283 }
284
285 typedef struct local_session_
286 {
287   /** fifo pointers. Once allocated, these do not move */
288   svm_fifo_t *rx_fifo;
289   svm_fifo_t *tx_fifo;
290
291   /** Type */
292   session_type_t session_type;
293
294   /** State */
295   volatile u8 session_state;
296
297   /** Session index */
298   u32 session_index;
299
300   /** Server index */
301   u32 app_wrk_index;
302
303   /** Port for connection. Overlaps thread_index/enqueue_epoch */
304   u16 port;
305
306   /** Partly overlaps enqueue_epoch */
307   u8 pad_epoch[7];
308
309   /** Segment index where fifos were allocated */
310   u32 svm_segment_index;
311
312   /** Transport listener index. Overlaps connection index */
313   u32 transport_listener_index;
314
315   union
316   {
317     u32 listener_index;
318     u32 app_index;
319   };
320
321   u32 al_index;
322
323   /** Has transport embedded when listener not purely local */
324   session_type_t listener_session_type;
325
326   /**
327    * Client data
328    */
329   u32 client_wrk_index;
330   u32 client_opaque;
331
332   u64 server_evt_q;
333   u64 client_evt_q;
334
335     CLIB_CACHE_LINE_ALIGN_MARK (pad);
336 } local_session_t;
337
338 always_inline u32
339 local_session_id (local_session_t * ls)
340 {
341   ASSERT (ls->session_index < (2 << 16));
342   u32 app_or_wrk_index;
343
344   if (ls->session_state == SESSION_STATE_LISTENING)
345     {
346       ASSERT (ls->app_index < (2 << 16));
347       app_or_wrk_index = ls->app_index;
348     }
349   else
350     {
351       ASSERT (ls->app_wrk_index < (2 << 16));
352       app_or_wrk_index = ls->app_wrk_index;
353     }
354
355   return ((u32) app_or_wrk_index << 16 | (u32) ls->session_index);
356 }
357
358 always_inline void
359 local_session_parse_id (u32 ls_id, u32 * app_or_wrk, u32 * session_index)
360 {
361   *app_or_wrk = ls_id >> 16;
362   *session_index = ls_id & 0xFF;
363 }
364
365 always_inline void
366 local_session_parse_handle (session_handle_t handle, u32 * app_or_wrk_index,
367                             u32 * session_index)
368 {
369   u32 bottom;
370   ASSERT ((handle >> 32) == SESSION_LOCAL_HANDLE_PREFIX);
371   bottom = (handle & 0xFFFFFFFF);
372   local_session_parse_id (bottom, app_or_wrk_index, session_index);
373 }
374
375 always_inline session_handle_t
376 application_local_session_handle (local_session_t * ls)
377 {
378   return ((u64) SESSION_LOCAL_HANDLE_PREFIX << 32)
379     | (u64) local_session_id (ls);
380 }
381
382 typedef enum
383 {
384   FIFO_EVENT_APP_RX,
385   SESSION_IO_EVT_CT_RX,
386   FIFO_EVENT_APP_TX,
387   SESSION_IO_EVT_CT_TX,
388   SESSION_IO_EVT_TX_FLUSH,
389   FIFO_EVENT_DISCONNECT,
390   FIFO_EVENT_BUILTIN_RX,
391   FIFO_EVENT_BUILTIN_TX,
392   FIFO_EVENT_RPC,
393   SESSION_CTRL_EVT_BOUND,
394   SESSION_CTRL_EVT_ACCEPTED,
395   SESSION_CTRL_EVT_ACCEPTED_REPLY,
396   SESSION_CTRL_EVT_CONNECTED,
397   SESSION_CTRL_EVT_CONNECTED_REPLY,
398   SESSION_CTRL_EVT_DISCONNECTED,
399   SESSION_CTRL_EVT_DISCONNECTED_REPLY,
400   SESSION_CTRL_EVT_RESET,
401   SESSION_CTRL_EVT_RESET_REPLY,
402   SESSION_CTRL_EVT_REQ_WORKER_UPDATE,
403   SESSION_CTRL_EVT_WORKER_UPDATE,
404   SESSION_CTRL_EVT_WORKER_UPDATE_REPLY,
405 } session_evt_type_t;
406
407 static inline const char *
408 fifo_event_type_str (session_evt_type_t et)
409 {
410   switch (et)
411     {
412     case FIFO_EVENT_APP_RX:
413       return "FIFO_EVENT_APP_RX";
414     case FIFO_EVENT_APP_TX:
415       return "FIFO_EVENT_APP_TX";
416     case FIFO_EVENT_DISCONNECT:
417       return "FIFO_EVENT_DISCONNECT";
418     case FIFO_EVENT_BUILTIN_RX:
419       return "FIFO_EVENT_BUILTIN_RX";
420     case FIFO_EVENT_RPC:
421       return "FIFO_EVENT_RPC";
422     default:
423       return "UNKNOWN FIFO EVENT";
424     }
425 }
426
427 typedef enum
428 {
429   SESSION_MQ_IO_EVT_RING,
430   SESSION_MQ_CTRL_EVT_RING,
431   SESSION_MQ_N_RINGS
432 } session_mq_rings_e;
433
434 typedef struct
435 {
436   void *fp;
437   void *arg;
438 } session_rpc_args_t;
439
440 /* *INDENT-OFF* */
441 typedef struct
442 {
443   u8 event_type;
444   u8 postponed;
445   union
446   {
447     svm_fifo_t *fifo;
448     session_handle_t session_handle;
449     session_rpc_args_t rpc_args;
450     struct
451     {
452       u8 data[0];
453     };
454   };
455 } __clib_packed session_event_t;
456 /* *INDENT-ON* */
457
458 #define SESSION_MSG_NULL { }
459
460 typedef struct session_dgram_pre_hdr_
461 {
462   u32 data_length;
463   u32 data_offset;
464 } session_dgram_pre_hdr_t;
465
466 /* *INDENT-OFF* */
467 typedef CLIB_PACKED (struct session_dgram_header_
468 {
469   u32 data_length;
470   u32 data_offset;
471   ip46_address_t rmt_ip;
472   ip46_address_t lcl_ip;
473   u16 rmt_port;
474   u16 lcl_port;
475   u8 is_ip4;
476 }) session_dgram_hdr_t;
477 /* *INDENT-ON* */
478
479 #define SESSION_CONN_ID_LEN 37
480 #define SESSION_CONN_HDR_LEN 45
481
482 STATIC_ASSERT (sizeof (session_dgram_hdr_t) == (SESSION_CONN_ID_LEN + 8),
483                "session conn id wrong length");
484 #endif /* SRC_VNET_SESSION_SESSION_TYPES_H_ */
485
486 /*
487  * fd.io coding-style-patch-verification: ON
488  *
489  * Local Variables:
490  * eval: (c-set-style "gnu")
491  * End:
492  */