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