be8ad9cdb839bffb90d200798bfbff4a3c5601ba
[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 84
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 /*
127  * Session states
128  */
129 #define foreach_session_state                           \
130   _(CREATED, "created")                                 \
131   _(LISTENING, "listening")                             \
132   _(CONNECTING, "connecting")                           \
133   _(ACCEPTING, "accepting")                             \
134   _(READY, "ready")                                     \
135   _(OPENED, "opened")                                   \
136   _(TRANSPORT_CLOSING, "transport-closing")             \
137   _(CLOSING, "closing")                                 \
138   _(APP_CLOSED, "app-closed")                           \
139   _(TRANSPORT_CLOSED, "transport-closed")               \
140   _(CLOSED, "closed")                                   \
141   _(TRANSPORT_DELETED, "transport-deleted")             \
142
143 typedef enum
144 {
145 #define _(sym, str) SESSION_STATE_ ## sym,
146   foreach_session_state
147 #undef _
148     SESSION_N_STATES,
149 } session_state_t;
150
151 #define foreach_session_flag                            \
152   _(RX_EVT, "rx-event")                                 \
153   _(PROXY, "proxy")                                     \
154   _(CUSTOM_TX, "custom-tx")                             \
155   _(IS_MIGRATING, "migrating")                          \
156   _(UNIDIRECTIONAL, "unidirectional")                   \
157
158 typedef enum session_flags_bits_
159 {
160 #define _(sym, str) SESSION_F_BIT_ ## sym,
161   foreach_session_flag
162 #undef _
163   SESSION_N_FLAGS
164 } session_flag_bits_t;
165
166 typedef enum session_flags_
167 {
168 #define _(sym, str) SESSION_F_ ## sym = 1 << SESSION_F_BIT_ ## sym,
169   foreach_session_flag
170 #undef _
171 } session_flags_t;
172
173 typedef struct session_
174 {
175   /** Pointers to rx/tx buffers. Once allocated, these do not move */
176   svm_fifo_t *rx_fifo;
177   svm_fifo_t *tx_fifo;
178
179   /** Type built from transport and network protocol types */
180   session_type_t session_type;
181
182   /** State in session layer state machine. See @ref session_state_t */
183   volatile u8 session_state;
184
185   /** Index in thread pool where session was allocated */
186   u32 session_index;
187
188   /** Index of the app worker that owns the session */
189   u32 app_wrk_index;
190
191   /** Index of the thread that allocated the session */
192   u8 thread_index;
193
194   /** Session flags. See @ref session_flags_t */
195   u32 flags;
196
197   /** Index of the transport connection associated to the session */
198   u32 connection_index;
199
200   /** Index of application that owns the listener. Set only if a listener */
201   u32 app_index;
202
203   union
204   {
205     /** Parent listener session index if the result of an accept */
206     session_handle_t listener_handle;
207
208     /** App listener index in app's listener pool if a listener */
209     u32 al_index;
210   };
211
212   /** Opaque, for general use */
213   u32 opaque;
214
215     CLIB_CACHE_LINE_ALIGN_MARK (pad);
216 } session_t;
217
218 always_inline session_type_t
219 session_type_from_proto_and_ip (transport_proto_t proto, u8 is_ip4)
220 {
221   return (proto << 1 | is_ip4);
222 }
223
224 always_inline transport_proto_t
225 session_type_transport_proto (session_type_t st)
226 {
227   return (st >> 1);
228 }
229
230 always_inline u8
231 session_type_is_ip4 (session_type_t st)
232 {
233   return (st & 1);
234 }
235
236 always_inline transport_proto_t
237 session_get_transport_proto (session_t * s)
238 {
239   return (s->session_type >> 1);
240 }
241
242 always_inline fib_protocol_t
243 session_get_fib_proto (session_t * s)
244 {
245   u8 is_ip4 = s->session_type & 1;
246   return (is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6);
247 }
248
249 always_inline u8
250 session_has_transport (session_t * s)
251 {
252   return (session_get_transport_proto (s) != TRANSPORT_PROTO_NONE);
253 }
254
255 static inline transport_service_type_t
256 session_transport_service_type (session_t * s)
257 {
258   transport_proto_t tp;
259   tp = session_get_transport_proto (s);
260   return transport_protocol_service_type (tp);
261 }
262
263 static inline transport_tx_fn_type_t
264 session_transport_tx_fn_type (session_t * s)
265 {
266   transport_proto_t tp;
267   tp = session_get_transport_proto (s);
268   return transport_protocol_tx_fn_type (tp);
269 }
270
271 static inline u8
272 session_tx_is_dgram (session_t * s)
273 {
274   return (session_transport_tx_fn_type (s) == TRANSPORT_TX_DGRAM);
275 }
276
277 always_inline session_handle_t
278 session_handle (session_t * s)
279 {
280   return ((u64) s->thread_index << 32) | (u64) s->session_index;
281 }
282
283 always_inline u32
284 session_index_from_handle (session_handle_t handle)
285 {
286   return handle & 0xFFFFFFFF;
287 }
288
289 always_inline u32
290 session_thread_from_handle (session_handle_t handle)
291 {
292   return handle >> 32;
293 }
294
295 always_inline void
296 session_parse_handle (session_handle_t handle, u32 * index,
297                       u32 * thread_index)
298 {
299   *index = session_index_from_handle (handle);
300   *thread_index = session_thread_from_handle (handle);
301 }
302
303 static inline session_handle_t
304 session_make_handle (u32 session_index, u32 thread_index)
305 {
306   return (((u64) thread_index << 32) | (u64) session_index);
307 }
308
309 typedef enum
310 {
311   SESSION_IO_EVT_RX,
312   SESSION_IO_EVT_TX,
313   SESSION_IO_EVT_TX_FLUSH,
314   SESSION_IO_EVT_BUILTIN_RX,
315   SESSION_IO_EVT_BUILTIN_TX,
316   SESSION_CTRL_EVT_RPC,
317   SESSION_CTRL_EVT_CLOSE,
318   SESSION_CTRL_EVT_RESET,
319   SESSION_CTRL_EVT_BOUND,
320   SESSION_CTRL_EVT_UNLISTEN_REPLY,
321   SESSION_CTRL_EVT_ACCEPTED,
322   SESSION_CTRL_EVT_ACCEPTED_REPLY,
323   SESSION_CTRL_EVT_CONNECTED,
324   SESSION_CTRL_EVT_DISCONNECTED,
325   SESSION_CTRL_EVT_DISCONNECTED_REPLY,
326   SESSION_CTRL_EVT_RESET_REPLY,
327   SESSION_CTRL_EVT_REQ_WORKER_UPDATE,
328   SESSION_CTRL_EVT_WORKER_UPDATE,
329   SESSION_CTRL_EVT_WORKER_UPDATE_REPLY,
330   SESSION_CTRL_EVT_DISCONNECT,
331   SESSION_CTRL_EVT_CONNECT,
332   SESSION_CTRL_EVT_CONNECT_URI,
333   SESSION_CTRL_EVT_LISTEN,
334   SESSION_CTRL_EVT_LISTEN_URI,
335   SESSION_CTRL_EVT_UNLISTEN,
336   SESSION_CTRL_EVT_APP_DETACH,
337   SESSION_CTRL_EVT_APP_ADD_SEGMENT,
338   SESSION_CTRL_EVT_APP_DEL_SEGMENT,
339   SESSION_CTRL_EVT_MIGRATED,
340   SESSION_CTRL_EVT_CLEANUP,
341 } session_evt_type_t;
342
343 #define foreach_session_ctrl_evt                                \
344   _(LISTEN, listen)                                             \
345   _(LISTEN_URI, listen_uri)                                     \
346   _(BOUND, bound)                                               \
347   _(UNLISTEN, unlisten)                                         \
348   _(UNLISTEN_REPLY, unlisten_reply)                             \
349   _(ACCEPTED, accepted)                                         \
350   _(ACCEPTED_REPLY, accepted_reply)                             \
351   _(CONNECT, connect)                                           \
352   _(CONNECT_URI, connect_uri)                                   \
353   _(CONNECTED, connected)                                       \
354   _(DISCONNECT, disconnect)                                     \
355   _(DISCONNECTED, disconnected)                                 \
356   _(DISCONNECTED_REPLY, disconnected_reply)                     \
357   _(RESET_REPLY, reset_reply)                                   \
358   _(REQ_WORKER_UPDATE, req_worker_update)                       \
359   _(WORKER_UPDATE, worker_update)                               \
360   _(WORKER_UPDATE_REPLY, worker_update_reply)                   \
361   _(APP_DETACH, app_detach)                                     \
362   _(APP_ADD_SEGMENT, app_add_segment)                           \
363   _(APP_DEL_SEGMENT, app_del_segment)                           \
364   _(MIGRATED, migrated)                                         \
365   _(CLEANUP, cleanup)                                           \
366
367 /* Deprecated and will be removed. Use types above */
368 #define FIFO_EVENT_APP_RX SESSION_IO_EVT_RX
369 #define FIFO_EVENT_APP_TX SESSION_IO_EVT_TX
370 #define FIFO_EVENT_DISCONNECT SESSION_CTRL_EVT_CLOSE
371 #define FIFO_EVENT_BUILTIN_RX SESSION_IO_EVT_BUILTIN_RX
372 #define FIFO_EVENT_BUILTIN_TX SESSION_IO_EVT_BUILTIN_TX
373
374 typedef enum
375 {
376   SESSION_MQ_IO_EVT_RING,
377   SESSION_MQ_CTRL_EVT_RING,
378   SESSION_MQ_N_RINGS
379 } session_mq_rings_e;
380
381 typedef struct
382 {
383   void *fp;
384   void *arg;
385 } session_rpc_args_t;
386
387 typedef struct
388 {
389   u8 event_type;
390   u8 postponed;
391   union
392   {
393     u32 session_index;
394     session_handle_t session_handle;
395     session_rpc_args_t rpc_args;
396     u32 ctrl_data_index;
397     struct
398     {
399       u8 data[0];
400     };
401   };
402 } __clib_packed session_event_t;
403
404 #define SESSION_MSG_NULL { }
405
406 typedef struct session_dgram_pre_hdr_
407 {
408   u32 data_length;
409   u32 data_offset;
410 } session_dgram_pre_hdr_t;
411
412 typedef struct session_dgram_header_
413 {
414   u32 data_length;
415   u32 data_offset;
416   ip46_address_t rmt_ip;
417   ip46_address_t lcl_ip;
418   u16 rmt_port;
419   u16 lcl_port;
420   u8 is_ip4;
421 } __clib_packed session_dgram_hdr_t;
422
423 #define SESSION_CONN_ID_LEN 37
424 #define SESSION_CONN_HDR_LEN 45
425
426 STATIC_ASSERT (sizeof (session_dgram_hdr_t) == (SESSION_CONN_ID_LEN + 8),
427                "session conn id wrong length");
428 #endif /* SRC_VNET_SESSION_SESSION_TYPES_H_ */
429
430 /*
431  * fd.io coding-style-patch-verification: ON
432  *
433  * Local Variables:
434  * eval: (c-set-style "gnu")
435  * End:
436  */