21188bb03080347c05ce1a1b760ca0cc3ab68cbf
[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 foreach_session_endpoint_fields                         \
23   foreach_transport_endpoint_cfg_fields                         \
24   _(u8, transport_proto)                                        \
25
26 typedef struct _session_endpoint
27 {
28 #define _(type, name) type name;
29   foreach_session_endpoint_fields
30 #undef _
31 } session_endpoint_t;
32
33 typedef struct _session_endpoint_cfg
34 {
35 #define _(type, name) type name;
36   foreach_session_endpoint_fields
37 #undef _
38   u32 app_wrk_index;
39   u32 opaque;
40   u32 ns_index;
41   u8 original_tp;
42   u8 *hostname;
43   u64 transport_opts;
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 typedef enum
112 {
113   SESSION_CLEANUP_TRANSPORT,
114   SESSION_CLEANUP_SESSION,
115 } session_cleanup_ntf_t;
116
117 /*
118  * Session states
119  */
120 typedef enum
121 {
122   SESSION_STATE_CREATED,
123   SESSION_STATE_LISTENING,
124   SESSION_STATE_CONNECTING,
125   SESSION_STATE_ACCEPTING,
126   SESSION_STATE_READY,
127   SESSION_STATE_OPENED,
128   SESSION_STATE_TRANSPORT_CLOSING,
129   SESSION_STATE_CLOSING,
130   SESSION_STATE_APP_CLOSED,
131   SESSION_STATE_TRANSPORT_CLOSED,
132   SESSION_STATE_CLOSED,
133   SESSION_STATE_N_STATES,
134 } session_state_t;
135
136 typedef enum session_flags_
137 {
138   SESSION_F_RX_EVT = 1,
139   SESSION_F_PROXY = (1 << 1),
140 } session_flags_t;
141
142 typedef struct session_
143 {
144   /** Pointers to rx/tx buffers. Once allocated, these do not move */
145   svm_fifo_t *rx_fifo;
146   svm_fifo_t *tx_fifo;
147
148   /** Type built from transport and network protocol types */
149   session_type_t session_type;
150
151   /** State in session layer state machine. See @ref session_state_t */
152   volatile u8 session_state;
153
154   /** Index in thread pool where session was allocated */
155   u32 session_index;
156
157   /** Index of the app worker that owns the session */
158   u32 app_wrk_index;
159
160   /** Index of the thread that allocated the session */
161   u8 thread_index;
162
163   /** Session flags. See @ref session_flags_t */
164   u32 flags;
165
166   /** Index of the transport connection associated to the session */
167   u32 connection_index;
168
169   /** Index of application that owns the listener. Set only if a listener */
170   u32 app_index;
171
172   union
173   {
174     /** Parent listener session index if the result of an accept */
175     session_handle_t listener_handle;
176
177     /** App listener index in app's listener pool if a listener */
178     u32 al_index;
179   };
180
181   /** Opaque, for general use */
182   u32 opaque;
183
184     CLIB_CACHE_LINE_ALIGN_MARK (pad);
185 } session_t;
186
187 always_inline session_type_t
188 session_type_from_proto_and_ip (transport_proto_t proto, u8 is_ip4)
189 {
190   return (proto << 1 | is_ip4);
191 }
192
193 always_inline transport_proto_t
194 session_type_transport_proto (session_type_t st)
195 {
196   return (st >> 1);
197 }
198
199 always_inline u8
200 session_type_is_ip4 (session_type_t st)
201 {
202   return (st & 1);
203 }
204
205 always_inline transport_proto_t
206 session_get_transport_proto (session_t * s)
207 {
208   return (s->session_type >> 1);
209 }
210
211 always_inline fib_protocol_t
212 session_get_fib_proto (session_t * s)
213 {
214   u8 is_ip4 = s->session_type & 1;
215   return (is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6);
216 }
217
218 always_inline u8
219 session_has_transport (session_t * s)
220 {
221   return (session_get_transport_proto (s) != TRANSPORT_PROTO_NONE);
222 }
223
224 static inline transport_service_type_t
225 session_transport_service_type (session_t * s)
226 {
227   transport_proto_t tp;
228   tp = session_get_transport_proto (s);
229   return transport_protocol_service_type (tp);
230 }
231
232 static inline transport_tx_fn_type_t
233 session_transport_tx_fn_type (session_t * s)
234 {
235   transport_proto_t tp;
236   tp = session_get_transport_proto (s);
237   return transport_protocol_tx_fn_type (tp);
238 }
239
240 static inline u8
241 session_tx_is_dgram (session_t * s)
242 {
243   return (session_transport_tx_fn_type (s) == TRANSPORT_TX_DGRAM);
244 }
245
246 always_inline session_handle_t
247 session_handle (session_t * s)
248 {
249   return ((u64) s->thread_index << 32) | (u64) s->session_index;
250 }
251
252 always_inline u32
253 session_index_from_handle (session_handle_t handle)
254 {
255   return handle & 0xFFFFFFFF;
256 }
257
258 always_inline u32
259 session_thread_from_handle (session_handle_t handle)
260 {
261   return handle >> 32;
262 }
263
264 always_inline void
265 session_parse_handle (session_handle_t handle, u32 * index,
266                       u32 * thread_index)
267 {
268   *index = session_index_from_handle (handle);
269   *thread_index = session_thread_from_handle (handle);
270 }
271
272 typedef enum
273 {
274   SESSION_IO_EVT_RX,
275   SESSION_IO_EVT_TX,
276   SESSION_IO_EVT_TX_FLUSH,
277   SESSION_IO_EVT_BUILTIN_RX,
278   SESSION_IO_EVT_BUILTIN_TX,
279   SESSION_CTRL_EVT_RPC,
280   SESSION_CTRL_EVT_CLOSE,
281   SESSION_CTRL_EVT_BOUND,
282   SESSION_CTRL_EVT_UNLISTEN_REPLY,
283   SESSION_CTRL_EVT_ACCEPTED,
284   SESSION_CTRL_EVT_ACCEPTED_REPLY,
285   SESSION_CTRL_EVT_CONNECTED,
286   SESSION_CTRL_EVT_CONNECTED_REPLY,
287   SESSION_CTRL_EVT_DISCONNECTED,
288   SESSION_CTRL_EVT_DISCONNECTED_REPLY,
289   SESSION_CTRL_EVT_RESET,
290   SESSION_CTRL_EVT_RESET_REPLY,
291   SESSION_CTRL_EVT_REQ_WORKER_UPDATE,
292   SESSION_CTRL_EVT_WORKER_UPDATE,
293   SESSION_CTRL_EVT_WORKER_UPDATE_REPLY,
294 } session_evt_type_t;
295
296 /* Deprecated and will be removed. Use types above */
297 #define FIFO_EVENT_APP_RX SESSION_IO_EVT_RX
298 #define FIFO_EVENT_APP_TX SESSION_IO_EVT_TX
299 #define FIFO_EVENT_DISCONNECT SESSION_CTRL_EVT_CLOSE
300 #define FIFO_EVENT_BUILTIN_RX SESSION_IO_EVT_BUILTIN_RX
301 #define FIFO_EVENT_BUILTIN_TX SESSION_IO_EVT_BUILTIN_TX
302
303 typedef enum
304 {
305   SESSION_MQ_IO_EVT_RING,
306   SESSION_MQ_CTRL_EVT_RING,
307   SESSION_MQ_N_RINGS
308 } session_mq_rings_e;
309
310 typedef struct
311 {
312   void *fp;
313   void *arg;
314 } session_rpc_args_t;
315
316 typedef struct
317 {
318   u8 event_type;
319   u8 postponed;
320   union
321   {
322     u32 session_index;
323     session_handle_t session_handle;
324     session_rpc_args_t rpc_args;
325     struct
326     {
327       u8 data[0];
328     };
329   };
330 } __clib_packed session_event_t;
331
332 #define SESSION_MSG_NULL { }
333
334 typedef struct session_dgram_pre_hdr_
335 {
336   u32 data_length;
337   u32 data_offset;
338 } session_dgram_pre_hdr_t;
339
340 typedef struct session_dgram_header_
341 {
342   u32 data_length;
343   u32 data_offset;
344   ip46_address_t rmt_ip;
345   ip46_address_t lcl_ip;
346   u16 rmt_port;
347   u16 lcl_port;
348   u8 is_ip4;
349 } __clib_packed session_dgram_hdr_t;
350
351 #define SESSION_CONN_ID_LEN 37
352 #define SESSION_CONN_HDR_LEN 45
353
354 STATIC_ASSERT (sizeof (session_dgram_hdr_t) == (SESSION_CONN_ID_LEN + 8),
355                "session conn id wrong length");
356 #endif /* SRC_VNET_SESSION_SESSION_TYPES_H_ */
357
358 /*
359  * fd.io coding-style-patch-verification: ON
360  *
361  * Local Variables:
362  * eval: (c-set-style "gnu")
363  * End:
364  */