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_LISTENING,
117 SESSION_STATE_CONNECTING,
118 SESSION_STATE_ACCEPTING,
120 SESSION_STATE_OPENED,
121 SESSION_STATE_TRANSPORT_CLOSING,
122 SESSION_STATE_CLOSING,
123 SESSION_STATE_CLOSED_WAITING,
124 SESSION_STATE_TRANSPORT_CLOSED,
125 SESSION_STATE_CLOSED,
126 SESSION_STATE_N_STATES,
129 typedef struct generic_session_
131 svm_fifo_t *rx_fifo; /**< rx fifo */
132 svm_fifo_t *tx_fifo; /**< tx fifo */
133 session_type_t session_type; /**< session type */
134 volatile u8 session_state; /**< session state */
135 u32 session_index; /**< index in owning pool */
138 typedef struct session_
140 /** fifo pointers. Once allocated, these do not move */
145 session_type_t session_type;
148 volatile u8 session_state;
150 /** Session index in per_thread pool */
153 /** App worker pool index */
158 /** To avoid n**2 "one event per frame" check */
161 /** svm segment index where fifos were allocated */
162 u32 svm_segment_index;
164 /** Transport specific */
165 u32 connection_index;
169 /** Parent listener session if the result of an accept */
172 /** Application index if a listener */
178 /** Transport app index for apps acting as transports */
181 /** App listener index */
184 /** Opaque, for general use */
188 CLIB_CACHE_LINE_ALIGN_MARK (pad);
191 always_inline session_type_t
192 session_type_from_proto_and_ip (transport_proto_t proto, u8 is_ip4)
194 return (proto << 1 | is_ip4);
197 always_inline transport_proto_t
198 session_type_transport_proto (session_type_t st)
204 session_type_is_ip4 (session_type_t st)
209 always_inline transport_proto_t
210 session_get_transport_proto (session_t * s)
212 return (s->session_type >> 1);
215 always_inline fib_protocol_t
216 session_get_fib_proto (session_t * s)
218 u8 is_ip4 = s->session_type & 1;
219 return (is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6);
223 session_has_transport (session_t * s)
225 return (session_get_transport_proto (s) != TRANSPORT_PROTO_NONE);
228 static inline transport_service_type_t
229 session_transport_service_type (session_t * s)
231 transport_proto_t tp;
232 tp = session_get_transport_proto (s);
233 return transport_protocol_service_type (tp);
236 static inline transport_tx_fn_type_t
237 session_transport_tx_fn_type (session_t * s)
239 transport_proto_t tp;
240 tp = session_get_transport_proto (s);
241 return transport_protocol_tx_fn_type (tp);
245 session_tx_is_dgram (session_t * s)
247 return (session_transport_tx_fn_type (s) == TRANSPORT_TX_DGRAM);
250 always_inline session_handle_t
251 session_handle (session_t * s)
253 return ((u64) s->thread_index << 32) | (u64) s->session_index;
257 session_index_from_handle (session_handle_t handle)
259 return handle & 0xFFFFFFFF;
263 session_thread_from_handle (session_handle_t handle)
269 session_parse_handle (session_handle_t handle, u32 * index,
272 *index = session_index_from_handle (handle);
273 *thread_index = session_thread_from_handle (handle);
277 session_handle_is_local (session_handle_t handle)
279 if ((handle >> 32) == SESSION_LOCAL_HANDLE_PREFIX)
284 typedef struct local_session_
286 /** fifo pointers. Once allocated, these do not move */
291 session_type_t session_type;
294 volatile u8 session_state;
302 /** Port for connection. Overlaps thread_index/enqueue_epoch */
305 /** Partly overlaps enqueue_epoch */
308 /** Segment index where fifos were allocated */
309 u32 svm_segment_index;
311 /** Transport listener index. Overlaps connection index */
312 u32 transport_listener_index;
322 /** Has transport embedded when listener not purely local */
323 session_type_t listener_session_type;
328 u32 client_wrk_index;
334 CLIB_CACHE_LINE_ALIGN_MARK (pad);
338 local_session_id (local_session_t * ls)
340 ASSERT (ls->session_index < (2 << 16));
341 u32 app_or_wrk_index;
343 if (ls->session_state == SESSION_STATE_LISTENING)
345 ASSERT (ls->app_index < (2 << 16));
346 app_or_wrk_index = ls->app_index;
350 ASSERT (ls->app_wrk_index < (2 << 16));
351 app_or_wrk_index = ls->app_wrk_index;
354 return ((u32) app_or_wrk_index << 16 | (u32) ls->session_index);
358 local_session_parse_id (u32 ls_id, u32 * app_or_wrk, u32 * session_index)
360 *app_or_wrk = ls_id >> 16;
361 *session_index = ls_id & 0xFF;
365 local_session_parse_handle (session_handle_t handle, u32 * app_or_wrk_index,
369 ASSERT ((handle >> 32) == SESSION_LOCAL_HANDLE_PREFIX);
370 bottom = (handle & 0xFFFFFFFF);
371 local_session_parse_id (bottom, app_or_wrk_index, session_index);
374 always_inline session_handle_t
375 application_local_session_handle (local_session_t * ls)
377 return ((u64) SESSION_LOCAL_HANDLE_PREFIX << 32)
378 | (u64) local_session_id (ls);
384 SESSION_IO_EVT_CT_RX,
386 SESSION_IO_EVT_CT_TX,
387 SESSION_IO_EVT_TX_FLUSH,
388 FIFO_EVENT_DISCONNECT,
389 FIFO_EVENT_BUILTIN_RX,
390 FIFO_EVENT_BUILTIN_TX,
392 SESSION_CTRL_EVT_BOUND,
393 SESSION_CTRL_EVT_ACCEPTED,
394 SESSION_CTRL_EVT_ACCEPTED_REPLY,
395 SESSION_CTRL_EVT_CONNECTED,
396 SESSION_CTRL_EVT_CONNECTED_REPLY,
397 SESSION_CTRL_EVT_DISCONNECTED,
398 SESSION_CTRL_EVT_DISCONNECTED_REPLY,
399 SESSION_CTRL_EVT_RESET,
400 SESSION_CTRL_EVT_RESET_REPLY,
401 SESSION_CTRL_EVT_REQ_WORKER_UPDATE,
402 SESSION_CTRL_EVT_WORKER_UPDATE,
403 SESSION_CTRL_EVT_WORKER_UPDATE_REPLY,
404 } session_evt_type_t;
406 static inline const char *
407 fifo_event_type_str (session_evt_type_t et)
411 case FIFO_EVENT_APP_RX:
412 return "FIFO_EVENT_APP_RX";
413 case FIFO_EVENT_APP_TX:
414 return "FIFO_EVENT_APP_TX";
415 case FIFO_EVENT_DISCONNECT:
416 return "FIFO_EVENT_DISCONNECT";
417 case FIFO_EVENT_BUILTIN_RX:
418 return "FIFO_EVENT_BUILTIN_RX";
420 return "FIFO_EVENT_RPC";
422 return "UNKNOWN FIFO EVENT";
428 SESSION_MQ_IO_EVT_RING,
429 SESSION_MQ_CTRL_EVT_RING,
431 } session_mq_rings_e;
437 } session_rpc_args_t;
447 session_handle_t session_handle;
448 session_rpc_args_t rpc_args;
454 } __clib_packed session_event_t;
457 #define SESSION_MSG_NULL { }
459 typedef struct session_dgram_pre_hdr_
463 } session_dgram_pre_hdr_t;
466 typedef CLIB_PACKED (struct session_dgram_header_
470 ip46_address_t rmt_ip;
471 ip46_address_t lcl_ip;
475 }) session_dgram_hdr_t;
478 #define SESSION_CONN_ID_LEN 37
479 #define SESSION_CONN_HDR_LEN 45
481 STATIC_ASSERT (sizeof (session_dgram_hdr_t) == (SESSION_CONN_ID_LEN + 8),
482 "session conn id wrong length");
483 #endif /* SRC_VNET_SESSION_SESSION_TYPES_H_ */
486 * fd.io coding-style-patch-verification: ON
489 * eval: (c-set-style "gnu")