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