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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #ifndef SRC_VNET_SESSION_SESSION_TYPES_H_
17 #define SRC_VNET_SESSION_SESSION_TYPES_H_
19 #include <svm/svm_fifo.h>
20 #include <vnet/session/transport.h>
22 #define SESSION_LOCAL_HANDLE_PREFIX 0x7FFFFFFF
24 #define foreach_session_endpoint_fields \
25 foreach_transport_endpoint_cfg_fields \
26 _(u8, transport_proto) \
28 typedef struct _session_endpoint
30 #define _(type, name) type name;
31 foreach_session_endpoint_fields
35 typedef struct _session_endpoint_cfg
37 #define _(type, name) type name;
38 foreach_session_endpoint_fields
43 } session_endpoint_cfg_t;
45 #define SESSION_IP46_ZERO \
52 #define TRANSPORT_ENDPOINT_NULL \
54 .sw_if_index = ENDPOINT_INVALID_INDEX, \
55 .ip = SESSION_IP46_ZERO, \
56 .fib_index = ENDPOINT_INVALID_INDEX, \
60 #define SESSION_ENDPOINT_NULL \
62 .sw_if_index = ENDPOINT_INVALID_INDEX, \
63 .ip = SESSION_IP46_ZERO, \
64 .fib_index = ENDPOINT_INVALID_INDEX, \
67 .peer = TRANSPORT_ENDPOINT_NULL, \
68 .transport_proto = 0, \
70 #define SESSION_ENDPOINT_CFG_NULL \
72 .sw_if_index = ENDPOINT_INVALID_INDEX, \
73 .ip = SESSION_IP46_ZERO, \
74 .fib_index = ENDPOINT_INVALID_INDEX, \
77 .peer = TRANSPORT_ENDPOINT_NULL, \
78 .transport_proto = 0, \
79 .app_wrk_index = ENDPOINT_INVALID_INDEX, \
80 .opaque = ENDPOINT_INVALID_INDEX, \
84 #define session_endpoint_to_transport(_sep) ((transport_endpoint_t *)_sep)
85 #define session_endpoint_to_transport_cfg(_sep) \
86 ((transport_endpoint_cfg_t *)_sep)
89 session_endpoint_fib_proto (session_endpoint_t * sep)
91 return sep->is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
94 typedef u8 session_type_t;
95 typedef u64 session_handle_t;
98 * Application session state
102 SESSION_STATE_LISTENING,
103 SESSION_STATE_CONNECTING,
104 SESSION_STATE_ACCEPTING,
106 SESSION_STATE_OPENED,
107 SESSION_STATE_TRANSPORT_CLOSING,
108 SESSION_STATE_CLOSING,
109 SESSION_STATE_CLOSED_WAITING,
110 SESSION_STATE_TRANSPORT_CLOSED,
111 SESSION_STATE_CLOSED,
112 SESSION_STATE_N_STATES,
115 typedef struct generic_session_
117 svm_fifo_t *rx_fifo; /**< rx fifo */
118 svm_fifo_t *tx_fifo; /**< tx fifo */
119 session_type_t session_type; /**< session type */
120 volatile u8 session_state; /**< session state */
121 u32 session_index; /**< index in owning pool */
124 typedef struct session_
126 /** fifo pointers. Once allocated, these do not move */
131 session_type_t session_type;
134 volatile u8 session_state;
136 /** Session index in per_thread pool */
139 /** App worker pool index */
144 /** To avoid n**2 "one event per frame" check */
147 /** svm segment index where fifos were allocated */
148 u32 svm_segment_index;
150 /** Transport specific */
151 u32 connection_index;
155 /** Parent listener session if the result of an accept */
158 /** Application index if a listener */
164 /** Transport app index for apps acting as transports */
167 /** Index in listener app's listener db */
168 u32 listener_db_index;
170 /** Opaque, for general use */
174 CLIB_CACHE_LINE_ALIGN_MARK (pad);
177 always_inline session_type_t
178 session_type_from_proto_and_ip (transport_proto_t proto, u8 is_ip4)
180 return (proto << 1 | is_ip4);
183 always_inline transport_proto_t
184 session_type_transport_proto (session_type_t st)
190 session_type_is_ip4 (session_type_t st)
195 always_inline transport_proto_t
196 session_get_transport_proto (session_t * s)
198 return (s->session_type >> 1);
201 always_inline fib_protocol_t
202 session_get_fib_proto (session_t * s)
204 u8 is_ip4 = s->session_type & 1;
205 return (is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6);
209 session_has_transport (session_t * s)
211 return (session_get_transport_proto (s) != TRANSPORT_PROTO_NONE);
214 static inline transport_service_type_t
215 session_transport_service_type (session_t * s)
217 transport_proto_t tp;
218 tp = session_get_transport_proto (s);
219 return transport_protocol_service_type (tp);
222 static inline transport_tx_fn_type_t
223 session_transport_tx_fn_type (session_t * s)
225 transport_proto_t tp;
226 tp = session_get_transport_proto (s);
227 return transport_protocol_tx_fn_type (tp);
231 session_tx_is_dgram (session_t * s)
233 return (session_transport_tx_fn_type (s) == TRANSPORT_TX_DGRAM);
236 always_inline session_handle_t
237 session_handle (session_t * s)
239 return ((u64) s->thread_index << 32) | (u64) s->session_index;
243 session_index_from_handle (session_handle_t handle)
245 return handle & 0xFFFFFFFF;
249 session_thread_from_handle (session_handle_t handle)
255 session_parse_handle (session_handle_t handle, u32 * index,
258 *index = session_index_from_handle (handle);
259 *thread_index = session_thread_from_handle (handle);
263 session_handle_is_local (session_handle_t handle)
265 if ((handle >> 32) == SESSION_LOCAL_HANDLE_PREFIX)
270 typedef struct local_session_
272 /** fifo pointers. Once allocated, these do not move */
277 session_type_t session_type;
280 volatile u8 session_state;
288 /** Port for connection. Overlaps thread_index/enqueue_epoch */
291 /** Partly overlaps enqueue_epoch */
294 /** Segment index where fifos were allocated */
295 u32 svm_segment_index;
297 /** Transport listener index. Overlaps connection index */
298 u32 transport_listener_index;
306 u32 listener_db_index;
308 /** Has transport embedded when listener not purely local */
309 session_type_t listener_session_type;
314 u32 client_wrk_index;
320 CLIB_CACHE_LINE_ALIGN_MARK (pad);
324 local_session_id (local_session_t * ls)
326 ASSERT (ls->session_index < (2 << 16));
327 u32 app_or_wrk_index;
329 if (ls->session_state == SESSION_STATE_LISTENING)
331 ASSERT (ls->app_index < (2 << 16));
332 app_or_wrk_index = ls->app_index;
336 ASSERT (ls->app_wrk_index < (2 << 16));
337 app_or_wrk_index = ls->app_wrk_index;
340 return ((u32) app_or_wrk_index << 16 | (u32) ls->session_index);
344 local_session_parse_id (u32 ls_id, u32 * app_or_wrk, u32 * session_index)
346 *app_or_wrk = ls_id >> 16;
347 *session_index = ls_id & 0xFF;
351 local_session_parse_handle (session_handle_t handle, u32 * app_or_wrk_index,
355 ASSERT ((handle >> 32) == SESSION_LOCAL_HANDLE_PREFIX);
356 bottom = (handle & 0xFFFFFFFF);
357 local_session_parse_id (bottom, app_or_wrk_index, session_index);
360 always_inline session_handle_t
361 application_local_session_handle (local_session_t * ls)
363 return ((u64) SESSION_LOCAL_HANDLE_PREFIX << 32)
364 | (u64) local_session_id (ls);
367 #endif /* SRC_VNET_SESSION_SESSION_TYPES_H_ */
370 * fd.io coding-style-patch-verification: ON
373 * eval: (c-set-style "gnu")