4b187a6948033dc9feed942825c9b6898477e61d
[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 } session_endpoint_cfg_t;
51
52 #define SESSION_IP46_ZERO                       \
53 {                                               \
54     .ip6 = {                                    \
55         { 0, 0, },                              \
56     },                                          \
57 }
58
59 #define TRANSPORT_ENDPOINT_NULL                 \
60 {                                               \
61   .sw_if_index = ENDPOINT_INVALID_INDEX,        \
62   .ip = SESSION_IP46_ZERO,                      \
63   .fib_index = ENDPOINT_INVALID_INDEX,          \
64   .is_ip4 = 0,                                  \
65   .port = 0,                                    \
66 }
67 #define SESSION_ENDPOINT_NULL                   \
68 {                                               \
69   .sw_if_index = ENDPOINT_INVALID_INDEX,        \
70   .ip = SESSION_IP46_ZERO,                      \
71   .fib_index = ENDPOINT_INVALID_INDEX,          \
72   .is_ip4 = 0,                                  \
73   .port = 0,                                    \
74   .peer = TRANSPORT_ENDPOINT_NULL,              \
75   .transport_proto = 0,                         \
76 }
77 #define SESSION_ENDPOINT_CFG_NULL               \
78 {                                               \
79   .sw_if_index = ENDPOINT_INVALID_INDEX,        \
80   .ip = SESSION_IP46_ZERO,                      \
81   .fib_index = ENDPOINT_INVALID_INDEX,          \
82   .is_ip4 = 0,                                  \
83   .port = 0,                                    \
84   .peer = TRANSPORT_ENDPOINT_NULL,              \
85   .transport_proto = 0,                         \
86   .app_wrk_index = ENDPOINT_INVALID_INDEX,      \
87   .opaque = ENDPOINT_INVALID_INDEX,             \
88   .hostname = 0,                                \
89   .parent_handle = SESSION_INVALID_HANDLE,      \
90   .ckpair_index = 0                             \
91 }
92
93 #define session_endpoint_to_transport(_sep) ((transport_endpoint_t *)_sep)
94 #define session_endpoint_to_transport_cfg(_sep)         \
95   ((transport_endpoint_cfg_t *)_sep)
96
97 always_inline u8
98 session_endpoint_fib_proto (session_endpoint_t * sep)
99 {
100   return sep->is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
101 }
102
103 static inline u8
104 session_endpoint_is_local (session_endpoint_t * sep)
105 {
106   return (ip_is_zero (&sep->ip, sep->is_ip4)
107           || ip_is_local_host (&sep->ip, sep->is_ip4));
108 }
109
110 static inline u8
111 session_endpoint_is_zero (session_endpoint_t * sep)
112 {
113   return ip_is_zero (&sep->ip, sep->is_ip4);
114 }
115
116 typedef u8 session_type_t;
117 typedef u64 session_handle_t;
118
119 typedef enum
120 {
121   SESSION_CLEANUP_TRANSPORT,
122   SESSION_CLEANUP_SESSION,
123 } session_cleanup_ntf_t;
124
125 /*
126  * Session states
127  */
128 #define foreach_session_state                           \
129   _(CREATED, "created")                                 \
130   _(LISTENING, "listening")                             \
131   _(CONNECTING, "connecting")                           \
132   _(ACCEPTING, "accepting")                             \
133   _(READY, "ready")                                     \
134   _(OPENED, "opened")                                   \
135   _(TRANSPORT_CLOSING, "transport-closing")             \
136   _(CLOSING, "closing")                                 \
137   _(APP_CLOSED, "app-closed")                           \
138   _(TRANSPORT_CLOSED, "transport-closed")               \
139   _(CLOSED, "closed")                                   \
140   _(TRANSPORT_DELETED, "transport-deleted")             \
141
142 typedef enum
143 {
144 #define _(sym, str) SESSION_STATE_ ## sym,
145   foreach_session_state
146 #undef _
147     SESSION_N_STATES,
148 } session_state_t;
149
150 typedef enum session_flags_
151 {
152   SESSION_F_RX_EVT = 1,
153   SESSION_F_PROXY = (1 << 1),
154   SESSION_F_CUSTOM_TX = (1 << 2),
155   SESSION_F_IS_MIGRATING = (1 << 3),
156 } session_flags_t;
157
158 typedef struct session_
159 {
160   /** Pointers to rx/tx buffers. Once allocated, these do not move */
161   svm_fifo_t *rx_fifo;
162   svm_fifo_t *tx_fifo;
163
164   /** Type built from transport and network protocol types */
165   session_type_t session_type;
166
167   /** State in session layer state machine. See @ref session_state_t */
168   volatile u8 session_state;
169
170   /** Index in thread pool where session was allocated */
171   u32 session_index;
172
173   /** Index of the app worker that owns the session */
174   u32 app_wrk_index;
175
176   /** Index of the thread that allocated the session */
177   u8 thread_index;
178
179   /** Session flags. See @ref session_flags_t */
180   u32 flags;
181
182   /** Index of the transport connection associated to the session */
183   u32 connection_index;
184
185   /** Index of application that owns the listener. Set only if a listener */
186   u32 app_index;
187
188   union
189   {
190     /** Parent listener session index if the result of an accept */
191     session_handle_t listener_handle;
192
193     /** App listener index in app's listener pool if a listener */
194     u32 al_index;
195   };
196
197   /** Opaque, for general use */
198   u32 opaque;
199
200     CLIB_CACHE_LINE_ALIGN_MARK (pad);
201 } session_t;
202
203 always_inline session_type_t
204 session_type_from_proto_and_ip (transport_proto_t proto, u8 is_ip4)
205 {
206   return (proto << 1 | is_ip4);
207 }
208
209 always_inline transport_proto_t
210 session_type_transport_proto (session_type_t st)
211 {
212   return (st >> 1);
213 }
214
215 always_inline u8
216 session_type_is_ip4 (session_type_t st)
217 {
218   return (st & 1);
219 }
220
221 always_inline transport_proto_t
222 session_get_transport_proto (session_t * s)
223 {
224   return (s->session_type >> 1);
225 }
226
227 always_inline fib_protocol_t
228 session_get_fib_proto (session_t * s)
229 {
230   u8 is_ip4 = s->session_type & 1;
231   return (is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6);
232 }
233
234 always_inline u8
235 session_has_transport (session_t * s)
236 {
237   return (session_get_transport_proto (s) != TRANSPORT_PROTO_NONE);
238 }
239
240 static inline transport_service_type_t
241 session_transport_service_type (session_t * s)
242 {
243   transport_proto_t tp;
244   tp = session_get_transport_proto (s);
245   return transport_protocol_service_type (tp);
246 }
247
248 static inline transport_tx_fn_type_t
249 session_transport_tx_fn_type (session_t * s)
250 {
251   transport_proto_t tp;
252   tp = session_get_transport_proto (s);
253   return transport_protocol_tx_fn_type (tp);
254 }
255
256 static inline u8
257 session_tx_is_dgram (session_t * s)
258 {
259   return (session_transport_tx_fn_type (s) == TRANSPORT_TX_DGRAM);
260 }
261
262 always_inline session_handle_t
263 session_handle (session_t * s)
264 {
265   return ((u64) s->thread_index << 32) | (u64) s->session_index;
266 }
267
268 always_inline u32
269 session_index_from_handle (session_handle_t handle)
270 {
271   return handle & 0xFFFFFFFF;
272 }
273
274 always_inline u32
275 session_thread_from_handle (session_handle_t handle)
276 {
277   return handle >> 32;
278 }
279
280 always_inline void
281 session_parse_handle (session_handle_t handle, u32 * index,
282                       u32 * thread_index)
283 {
284   *index = session_index_from_handle (handle);
285   *thread_index = session_thread_from_handle (handle);
286 }
287
288 static inline session_handle_t
289 session_make_handle (u32 session_index, u32 thread_index)
290 {
291   return (((u64) thread_index << 32) | (u64) session_index);
292 }
293
294 typedef enum
295 {
296   SESSION_IO_EVT_RX,
297   SESSION_IO_EVT_TX,
298   SESSION_IO_EVT_TX_FLUSH,
299   SESSION_IO_EVT_BUILTIN_RX,
300   SESSION_IO_EVT_BUILTIN_TX,
301   SESSION_CTRL_EVT_RPC,
302   SESSION_CTRL_EVT_CLOSE,
303   SESSION_CTRL_EVT_RESET,
304   SESSION_CTRL_EVT_BOUND,
305   SESSION_CTRL_EVT_UNLISTEN_REPLY,
306   SESSION_CTRL_EVT_ACCEPTED,
307   SESSION_CTRL_EVT_ACCEPTED_REPLY,
308   SESSION_CTRL_EVT_CONNECTED,
309   SESSION_CTRL_EVT_DISCONNECTED,
310   SESSION_CTRL_EVT_DISCONNECTED_REPLY,
311   SESSION_CTRL_EVT_RESET_REPLY,
312   SESSION_CTRL_EVT_REQ_WORKER_UPDATE,
313   SESSION_CTRL_EVT_WORKER_UPDATE,
314   SESSION_CTRL_EVT_WORKER_UPDATE_REPLY,
315   SESSION_CTRL_EVT_DISCONNECT,
316   SESSION_CTRL_EVT_CONNECT,
317   SESSION_CTRL_EVT_CONNECT_URI,
318   SESSION_CTRL_EVT_LISTEN,
319   SESSION_CTRL_EVT_LISTEN_URI,
320   SESSION_CTRL_EVT_UNLISTEN,
321   SESSION_CTRL_EVT_APP_DETACH,
322 } session_evt_type_t;
323
324 #define foreach_session_ctrl_evt                                \
325   _(LISTEN, listen)                                             \
326   _(LISTEN_URI, listen_uri)                                     \
327   _(BOUND, bound)                                               \
328   _(UNLISTEN, unlisten)                                         \
329   _(UNLISTEN_REPLY, unlisten_reply)                             \
330   _(ACCEPTED, accepted)                                         \
331   _(ACCEPTED_REPLY, accepted_reply)                             \
332   _(CONNECT, connect)                                           \
333   _(CONNECT_URI, connect_uri)                                   \
334   _(CONNECTED, connected)                                       \
335   _(DISCONNECT, disconnect)                                     \
336   _(DISCONNECTED, disconnected)                                 \
337   _(DISCONNECTED_REPLY, disconnected_reply)                     \
338   _(RESET_REPLY, reset_reply)                                   \
339   _(REQ_WORKER_UPDATE, req_worker_update)                       \
340   _(WORKER_UPDATE, worker_update)                               \
341   _(WORKER_UPDATE_REPLY, worker_update_reply)                   \
342   _(APP_DETACH, app_detach)                                     \
343
344
345 /* Deprecated and will be removed. Use types above */
346 #define FIFO_EVENT_APP_RX SESSION_IO_EVT_RX
347 #define FIFO_EVENT_APP_TX SESSION_IO_EVT_TX
348 #define FIFO_EVENT_DISCONNECT SESSION_CTRL_EVT_CLOSE
349 #define FIFO_EVENT_BUILTIN_RX SESSION_IO_EVT_BUILTIN_RX
350 #define FIFO_EVENT_BUILTIN_TX SESSION_IO_EVT_BUILTIN_TX
351
352 typedef enum
353 {
354   SESSION_MQ_IO_EVT_RING,
355   SESSION_MQ_CTRL_EVT_RING,
356   SESSION_MQ_N_RINGS
357 } session_mq_rings_e;
358
359 typedef struct
360 {
361   void *fp;
362   void *arg;
363 } session_rpc_args_t;
364
365 typedef struct
366 {
367   u8 event_type;
368   u8 postponed;
369   union
370   {
371     u32 session_index;
372     session_handle_t session_handle;
373     session_rpc_args_t rpc_args;
374     u32 ctrl_data_index;
375     struct
376     {
377       u8 data[0];
378     };
379   };
380 } __clib_packed session_event_t;
381
382 #define SESSION_MSG_NULL { }
383
384 typedef struct session_dgram_pre_hdr_
385 {
386   u32 data_length;
387   u32 data_offset;
388 } session_dgram_pre_hdr_t;
389
390 typedef struct session_dgram_header_
391 {
392   u32 data_length;
393   u32 data_offset;
394   ip46_address_t rmt_ip;
395   ip46_address_t lcl_ip;
396   u16 rmt_port;
397   u16 lcl_port;
398   u8 is_ip4;
399 } __clib_packed session_dgram_hdr_t;
400
401 #define SESSION_CONN_ID_LEN 37
402 #define SESSION_CONN_HDR_LEN 45
403
404 STATIC_ASSERT (sizeof (session_dgram_hdr_t) == (SESSION_CONN_ID_LEN + 8),
405                "session conn id wrong length");
406 #endif /* SRC_VNET_SESSION_SESSION_TYPES_H_ */
407
408 /*
409  * fd.io coding-style-patch-verification: ON
410  *
411  * Local Variables:
412  * eval: (c-set-style "gnu")
413  * End:
414  */