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_types.h>
22 #define SESSION_LOCAL_HANDLE_PREFIX 0x7FFFFFFF
23 #define SESSION_LISTENER_PREFIX 0x5FFFFFFF
25 #define foreach_session_endpoint_fields \
26 foreach_transport_endpoint_cfg_fields \
27 _(u8, transport_proto) \
29 typedef struct _session_endpoint
31 #define _(type, name) type name;
32 foreach_session_endpoint_fields
36 typedef struct _session_endpoint_cfg
38 #define _(type, name) type name;
39 foreach_session_endpoint_fields
44 } session_endpoint_cfg_t;
46 #define SESSION_IP46_ZERO \
53 #define TRANSPORT_ENDPOINT_NULL \
55 .sw_if_index = ENDPOINT_INVALID_INDEX, \
56 .ip = SESSION_IP46_ZERO, \
57 .fib_index = ENDPOINT_INVALID_INDEX, \
61 #define SESSION_ENDPOINT_NULL \
63 .sw_if_index = ENDPOINT_INVALID_INDEX, \
64 .ip = SESSION_IP46_ZERO, \
65 .fib_index = ENDPOINT_INVALID_INDEX, \
68 .peer = TRANSPORT_ENDPOINT_NULL, \
69 .transport_proto = 0, \
71 #define SESSION_ENDPOINT_CFG_NULL \
73 .sw_if_index = ENDPOINT_INVALID_INDEX, \
74 .ip = SESSION_IP46_ZERO, \
75 .fib_index = ENDPOINT_INVALID_INDEX, \
78 .peer = TRANSPORT_ENDPOINT_NULL, \
79 .transport_proto = 0, \
80 .app_wrk_index = ENDPOINT_INVALID_INDEX, \
81 .opaque = ENDPOINT_INVALID_INDEX, \
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)
90 session_endpoint_fib_proto (session_endpoint_t * sep)
92 return sep->is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
96 session_endpoint_is_local (session_endpoint_t * sep)
98 return (ip_is_zero (&sep->ip, sep->is_ip4)
99 || ip_is_local_host (&sep->ip, sep->is_ip4));
103 session_endpoint_is_zero (session_endpoint_t * sep)
105 return ip_is_zero (&sep->ip, sep->is_ip4);
108 typedef u8 session_type_t;
109 typedef u64 session_handle_t;
112 * Application session state
116 SESSION_STATE_CREATED,
117 SESSION_STATE_LISTENING,
118 SESSION_STATE_CONNECTING,
119 SESSION_STATE_ACCEPTING,
121 SESSION_STATE_OPENED,
122 SESSION_STATE_TRANSPORT_CLOSING,
123 SESSION_STATE_CLOSING,
124 SESSION_STATE_CLOSED_WAITING,
125 SESSION_STATE_TRANSPORT_CLOSED,
126 SESSION_STATE_CLOSED,
127 SESSION_STATE_N_STATES,
130 typedef struct generic_session_
132 svm_fifo_t *rx_fifo; /**< rx fifo */
133 svm_fifo_t *tx_fifo; /**< tx fifo */
134 session_type_t session_type; /**< session type */
135 volatile u8 session_state; /**< session state */
136 u32 session_index; /**< index in owning pool */
139 typedef struct session_
141 /** fifo pointers. Once allocated, these do not move */
146 session_type_t session_type;
149 volatile u8 session_state;
151 /** Session index in per_thread pool */
154 /** App worker pool index */
159 /** To avoid n**2 "one event per frame" check */
162 /** svm segment index where fifos were allocated */
163 u32 svm_segment_index;
165 /** Transport specific */
166 u32 connection_index;
170 /** Parent listener session if the result of an accept */
173 /** Application index if a listener */
179 /** Transport app index for apps acting as transports */
182 /** App listener index */
185 /** Opaque, for general use */
189 CLIB_CACHE_LINE_ALIGN_MARK (pad);
192 always_inline session_type_t
193 session_type_from_proto_and_ip (transport_proto_t proto, u8 is_ip4)
195 return (proto << 1 | is_ip4);
198 always_inline transport_proto_t
199 session_type_transport_proto (session_type_t st)
205 session_type_is_ip4 (session_type_t st)
210 always_inline transport_proto_t
211 session_get_transport_proto (session_t * s)
213 return (s->session_type >> 1);
216 always_inline fib_protocol_t
217 session_get_fib_proto (session_t * s)
219 u8 is_ip4 = s->session_type & 1;
220 return (is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6);
224 session_has_transport (session_t * s)
226 return (session_get_transport_proto (s) != TRANSPORT_PROTO_NONE);
229 static inline transport_service_type_t
230 session_transport_service_type (session_t * s)
232 transport_proto_t tp;
233 tp = session_get_transport_proto (s);
234 return transport_protocol_service_type (tp);
237 static inline transport_tx_fn_type_t
238 session_transport_tx_fn_type (session_t * s)
240 transport_proto_t tp;
241 tp = session_get_transport_proto (s);
242 return transport_protocol_tx_fn_type (tp);
246 session_tx_is_dgram (session_t * s)
248 return (session_transport_tx_fn_type (s) == TRANSPORT_TX_DGRAM);
251 always_inline session_handle_t
252 session_handle (session_t * s)
254 return ((u64) s->thread_index << 32) | (u64) s->session_index;
258 session_index_from_handle (session_handle_t handle)
260 return handle & 0xFFFFFFFF;
264 session_thread_from_handle (session_handle_t handle)
270 session_parse_handle (session_handle_t handle, u32 * index,
273 *index = session_index_from_handle (handle);
274 *thread_index = session_thread_from_handle (handle);
278 session_handle_is_local (session_handle_t handle)
280 if ((handle >> 32) == SESSION_LOCAL_HANDLE_PREFIX)
285 typedef struct local_session_
287 /** fifo pointers. Once allocated, these do not move */
292 session_type_t session_type;
295 volatile u8 session_state;
303 /** Port for connection. Overlaps thread_index/enqueue_epoch */
306 /** Partly overlaps enqueue_epoch */
309 /** Segment index where fifos were allocated */
310 u32 svm_segment_index;
312 /** Transport listener index. Overlaps connection index */
313 u32 transport_listener_index;
323 /** Has transport embedded when listener not purely local */
324 session_type_t listener_session_type;
329 u32 client_wrk_index;
335 CLIB_CACHE_LINE_ALIGN_MARK (pad);
339 local_session_id (local_session_t * ls)
341 ASSERT (ls->session_index < (2 << 16));
342 u32 app_or_wrk_index;
344 if (ls->session_state == SESSION_STATE_LISTENING)
346 ASSERT (ls->app_index < (2 << 16));
347 app_or_wrk_index = ls->app_index;
351 ASSERT (ls->app_wrk_index < (2 << 16));
352 app_or_wrk_index = ls->app_wrk_index;
355 return ((u32) app_or_wrk_index << 16 | (u32) ls->session_index);
359 local_session_parse_id (u32 ls_id, u32 * app_or_wrk, u32 * session_index)
361 *app_or_wrk = ls_id >> 16;
362 *session_index = ls_id & 0xFF;
366 local_session_parse_handle (session_handle_t handle, u32 * app_or_wrk_index,
370 ASSERT ((handle >> 32) == SESSION_LOCAL_HANDLE_PREFIX);
371 bottom = (handle & 0xFFFFFFFF);
372 local_session_parse_id (bottom, app_or_wrk_index, session_index);
375 always_inline session_handle_t
376 application_local_session_handle (local_session_t * ls)
378 return ((u64) SESSION_LOCAL_HANDLE_PREFIX << 32)
379 | (u64) local_session_id (ls);
385 SESSION_IO_EVT_CT_RX,
387 SESSION_IO_EVT_CT_TX,
388 SESSION_IO_EVT_TX_FLUSH,
389 FIFO_EVENT_DISCONNECT,
390 FIFO_EVENT_BUILTIN_RX,
391 FIFO_EVENT_BUILTIN_TX,
393 SESSION_CTRL_EVT_BOUND,
394 SESSION_CTRL_EVT_ACCEPTED,
395 SESSION_CTRL_EVT_ACCEPTED_REPLY,
396 SESSION_CTRL_EVT_CONNECTED,
397 SESSION_CTRL_EVT_CONNECTED_REPLY,
398 SESSION_CTRL_EVT_DISCONNECTED,
399 SESSION_CTRL_EVT_DISCONNECTED_REPLY,
400 SESSION_CTRL_EVT_RESET,
401 SESSION_CTRL_EVT_RESET_REPLY,
402 SESSION_CTRL_EVT_REQ_WORKER_UPDATE,
403 SESSION_CTRL_EVT_WORKER_UPDATE,
404 SESSION_CTRL_EVT_WORKER_UPDATE_REPLY,
405 } session_evt_type_t;
407 static inline const char *
408 fifo_event_type_str (session_evt_type_t et)
412 case FIFO_EVENT_APP_RX:
413 return "FIFO_EVENT_APP_RX";
414 case FIFO_EVENT_APP_TX:
415 return "FIFO_EVENT_APP_TX";
416 case FIFO_EVENT_DISCONNECT:
417 return "FIFO_EVENT_DISCONNECT";
418 case FIFO_EVENT_BUILTIN_RX:
419 return "FIFO_EVENT_BUILTIN_RX";
421 return "FIFO_EVENT_RPC";
423 return "UNKNOWN FIFO EVENT";
429 SESSION_MQ_IO_EVT_RING,
430 SESSION_MQ_CTRL_EVT_RING,
432 } session_mq_rings_e;
438 } session_rpc_args_t;
448 session_handle_t session_handle;
449 session_rpc_args_t rpc_args;
455 } __clib_packed session_event_t;
458 #define SESSION_MSG_NULL { }
460 typedef struct session_dgram_pre_hdr_
464 } session_dgram_pre_hdr_t;
467 typedef CLIB_PACKED (struct session_dgram_header_
471 ip46_address_t rmt_ip;
472 ip46_address_t lcl_ip;
476 }) session_dgram_hdr_t;
479 #define SESSION_CONN_ID_LEN 37
480 #define SESSION_CONN_HDR_LEN 45
482 STATIC_ASSERT (sizeof (session_dgram_hdr_t) == (SESSION_CONN_ID_LEN + 8),
483 "session conn id wrong length");
484 #endif /* SRC_VNET_SESSION_SESSION_TYPES_H_ */
487 * fd.io coding-style-patch-verification: ON
490 * eval: (c-set-style "gnu")