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