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