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