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_INVALID_INDEX ((u32)~0)
23 #define SESSION_INVALID_HANDLE ((u64)~0)
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
47 } session_endpoint_cfg_t;
49 #define SESSION_IP46_ZERO \
56 #define TRANSPORT_ENDPOINT_NULL \
58 .sw_if_index = ENDPOINT_INVALID_INDEX, \
59 .ip = SESSION_IP46_ZERO, \
60 .fib_index = ENDPOINT_INVALID_INDEX, \
64 #define SESSION_ENDPOINT_NULL \
66 .sw_if_index = ENDPOINT_INVALID_INDEX, \
67 .ip = SESSION_IP46_ZERO, \
68 .fib_index = ENDPOINT_INVALID_INDEX, \
71 .peer = TRANSPORT_ENDPOINT_NULL, \
72 .transport_proto = 0, \
74 #define SESSION_ENDPOINT_CFG_NULL \
76 .sw_if_index = ENDPOINT_INVALID_INDEX, \
77 .ip = SESSION_IP46_ZERO, \
78 .fib_index = ENDPOINT_INVALID_INDEX, \
81 .peer = TRANSPORT_ENDPOINT_NULL, \
82 .transport_proto = 0, \
83 .app_wrk_index = ENDPOINT_INVALID_INDEX, \
84 .opaque = ENDPOINT_INVALID_INDEX, \
86 .parent_handle = SESSION_INVALID_HANDLE \
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)
94 session_endpoint_fib_proto (session_endpoint_t * sep)
96 return sep->is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
100 session_endpoint_is_local (session_endpoint_t * sep)
102 return (ip_is_zero (&sep->ip, sep->is_ip4)
103 || ip_is_local_host (&sep->ip, sep->is_ip4));
107 session_endpoint_is_zero (session_endpoint_t * sep)
109 return ip_is_zero (&sep->ip, sep->is_ip4);
112 typedef u8 session_type_t;
113 typedef u64 session_handle_t;
117 SESSION_CLEANUP_TRANSPORT,
118 SESSION_CLEANUP_SESSION,
119 } session_cleanup_ntf_t;
124 #define foreach_session_state \
125 _(CREATED, "created") \
126 _(LISTENING, "listening") \
127 _(CONNECTING, "connecting") \
128 _(ACCEPTING, "accepting") \
130 _(OPENED, "opened") \
131 _(TRANSPORT_CLOSING, "transport-closing") \
132 _(CLOSING, "closing") \
133 _(APP_CLOSED, "app-closed") \
134 _(TRANSPORT_CLOSED, "transport-closed") \
135 _(TRANSPORT_DELETED, "transport-deleted") \
136 _(CLOSED, "closed") \
140 #define _(sym, str) SESSION_STATE_ ## sym,
141 foreach_session_state
146 typedef enum session_flags_
148 SESSION_F_RX_EVT = 1,
149 SESSION_F_PROXY = (1 << 1),
150 SESSION_F_CUSTOM_TX = (1 << 2),
151 SESSION_F_IS_MIGRATING = (1 << 3),
154 typedef struct session_
156 /** Pointers to rx/tx buffers. Once allocated, these do not move */
160 /** Type built from transport and network protocol types */
161 session_type_t session_type;
163 /** State in session layer state machine. See @ref session_state_t */
164 volatile u8 session_state;
166 /** Index in thread pool where session was allocated */
169 /** Index of the app worker that owns the session */
172 /** Index of the thread that allocated the session */
175 /** Session flags. See @ref session_flags_t */
178 /** Index of the transport connection associated to the session */
179 u32 connection_index;
181 /** Index of application that owns the listener. Set only if a listener */
186 /** Parent listener session index if the result of an accept */
187 session_handle_t listener_handle;
189 /** App listener index in app's listener pool if a listener */
193 /** Opaque, for general use */
196 CLIB_CACHE_LINE_ALIGN_MARK (pad);
199 always_inline session_type_t
200 session_type_from_proto_and_ip (transport_proto_t proto, u8 is_ip4)
202 return (proto << 1 | is_ip4);
205 always_inline transport_proto_t
206 session_type_transport_proto (session_type_t st)
212 session_type_is_ip4 (session_type_t st)
217 always_inline transport_proto_t
218 session_get_transport_proto (session_t * s)
220 return (s->session_type >> 1);
223 always_inline fib_protocol_t
224 session_get_fib_proto (session_t * s)
226 u8 is_ip4 = s->session_type & 1;
227 return (is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6);
231 session_has_transport (session_t * s)
233 return (session_get_transport_proto (s) != TRANSPORT_PROTO_NONE);
236 static inline transport_service_type_t
237 session_transport_service_type (session_t * s)
239 transport_proto_t tp;
240 tp = session_get_transport_proto (s);
241 return transport_protocol_service_type (tp);
244 static inline transport_tx_fn_type_t
245 session_transport_tx_fn_type (session_t * s)
247 transport_proto_t tp;
248 tp = session_get_transport_proto (s);
249 return transport_protocol_tx_fn_type (tp);
253 session_tx_is_dgram (session_t * s)
255 return (session_transport_tx_fn_type (s) == TRANSPORT_TX_DGRAM);
258 always_inline session_handle_t
259 session_handle (session_t * s)
261 return ((u64) s->thread_index << 32) | (u64) s->session_index;
265 session_index_from_handle (session_handle_t handle)
267 return handle & 0xFFFFFFFF;
271 session_thread_from_handle (session_handle_t handle)
277 session_parse_handle (session_handle_t handle, u32 * index,
280 *index = session_index_from_handle (handle);
281 *thread_index = session_thread_from_handle (handle);
284 static inline session_handle_t
285 session_make_handle (u32 session_index, u32 thread_index)
287 return (((u64) thread_index << 32) | (u64) session_index);
294 SESSION_IO_EVT_TX_FLUSH,
295 SESSION_IO_EVT_BUILTIN_RX,
296 SESSION_IO_EVT_BUILTIN_TX,
297 SESSION_CTRL_EVT_RPC,
298 SESSION_CTRL_EVT_CLOSE,
299 SESSION_CTRL_EVT_RESET,
300 SESSION_CTRL_EVT_BOUND,
301 SESSION_CTRL_EVT_UNLISTEN_REPLY,
302 SESSION_CTRL_EVT_ACCEPTED,
303 SESSION_CTRL_EVT_ACCEPTED_REPLY,
304 SESSION_CTRL_EVT_CONNECTED,
305 SESSION_CTRL_EVT_DISCONNECTED,
306 SESSION_CTRL_EVT_DISCONNECTED_REPLY,
307 SESSION_CTRL_EVT_RESET_REPLY,
308 SESSION_CTRL_EVT_REQ_WORKER_UPDATE,
309 SESSION_CTRL_EVT_WORKER_UPDATE,
310 SESSION_CTRL_EVT_WORKER_UPDATE_REPLY,
311 SESSION_CTRL_EVT_DISCONNECT,
312 SESSION_CTRL_EVT_CONNECT,
313 SESSION_CTRL_EVT_CONNECT_URI,
314 SESSION_CTRL_EVT_LISTEN,
315 SESSION_CTRL_EVT_LISTEN_URI,
316 SESSION_CTRL_EVT_UNLISTEN,
317 SESSION_CTRL_EVT_APP_DETACH,
318 } session_evt_type_t;
320 #define foreach_session_ctrl_evt \
322 _(LISTEN_URI, listen_uri) \
324 _(UNLISTEN, unlisten) \
325 _(UNLISTEN_REPLY, unlisten_reply) \
326 _(ACCEPTED, accepted) \
327 _(ACCEPTED_REPLY, accepted_reply) \
328 _(CONNECT, connect) \
329 _(CONNECT_URI, connect_uri) \
330 _(CONNECTED, connected) \
331 _(DISCONNECT, disconnect) \
332 _(DISCONNECTED, disconnected) \
333 _(DISCONNECTED_REPLY, disconnected_reply) \
334 _(RESET_REPLY, reset_reply) \
335 _(REQ_WORKER_UPDATE, req_worker_update) \
336 _(WORKER_UPDATE, worker_update) \
337 _(WORKER_UPDATE_REPLY, worker_update_reply) \
338 _(APP_DETACH, app_detach) \
341 /* Deprecated and will be removed. Use types above */
342 #define FIFO_EVENT_APP_RX SESSION_IO_EVT_RX
343 #define FIFO_EVENT_APP_TX SESSION_IO_EVT_TX
344 #define FIFO_EVENT_DISCONNECT SESSION_CTRL_EVT_CLOSE
345 #define FIFO_EVENT_BUILTIN_RX SESSION_IO_EVT_BUILTIN_RX
346 #define FIFO_EVENT_BUILTIN_TX SESSION_IO_EVT_BUILTIN_TX
350 SESSION_MQ_IO_EVT_RING,
351 SESSION_MQ_CTRL_EVT_RING,
353 } session_mq_rings_e;
359 } session_rpc_args_t;
368 session_handle_t session_handle;
369 session_rpc_args_t rpc_args;
376 } __clib_packed session_event_t;
378 #define SESSION_MSG_NULL { }
380 typedef struct session_dgram_pre_hdr_
384 } session_dgram_pre_hdr_t;
386 typedef struct session_dgram_header_
390 ip46_address_t rmt_ip;
391 ip46_address_t lcl_ip;
395 } __clib_packed session_dgram_hdr_t;
397 #define SESSION_CONN_ID_LEN 37
398 #define SESSION_CONN_HDR_LEN 45
400 STATIC_ASSERT (sizeof (session_dgram_hdr_t) == (SESSION_CONN_ID_LEN + 8),
401 "session conn id wrong length");
402 #endif /* SRC_VNET_SESSION_SESSION_TYPES_H_ */
405 * fd.io coding-style-patch-verification: ON
408 * eval: (c-set-style "gnu")