Line data Source code
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 : #define SESSION_CTRL_MSG_MAX_SIZE 86
25 : #define SESSION_CTRL_MSG_TX_MAX_SIZE 160
26 : #define SESSION_NODE_FRAME_SIZE 128
27 :
28 : #define foreach_session_endpoint_fields \
29 : foreach_transport_endpoint_cfg_fields \
30 : _(u8, transport_proto) \
31 :
32 : typedef struct _session_endpoint
33 : {
34 : #define _(type, name) type name;
35 : foreach_session_endpoint_fields
36 : #undef _
37 : } session_endpoint_t;
38 :
39 : #define foreach_session_endpoint_cfg_flags _ (PROXY_LISTEN, "proxy listener")
40 :
41 : typedef enum session_endpoint_cfg_flags_bits_
42 : {
43 : #define _(sym, str) SESSION_ENDPT_CFG_F_BIT_##sym,
44 : foreach_session_endpoint_cfg_flags
45 : #undef _
46 : } __clib_packed session_endpoint_cfg_flags_bits_t;
47 :
48 : typedef enum session_endpoint_cfg_flags_
49 : {
50 : #define _(sym, str) \
51 : SESSION_ENDPT_CFG_F_##sym = 1 << SESSION_ENDPT_CFG_F_BIT_##sym,
52 : foreach_session_endpoint_cfg_flags
53 : #undef _
54 : } __clib_packed session_endpoint_cfg_flags_t;
55 :
56 : typedef struct _session_endpoint_cfg
57 : {
58 : #define _(type, name) type name;
59 : foreach_session_endpoint_fields
60 : #undef _
61 : u32 app_wrk_index;
62 : u32 opaque;
63 : u32 ns_index;
64 : u8 original_tp;
65 : u64 parent_handle;
66 : session_endpoint_cfg_flags_t flags;
67 : transport_endpt_ext_cfg_t *ext_cfg;
68 : } session_endpoint_cfg_t;
69 :
70 : #define SESSION_IP46_ZERO \
71 : { \
72 : .ip6 = { \
73 : { 0, 0, }, \
74 : }, \
75 : }
76 :
77 : #define TRANSPORT_ENDPOINT_NULL \
78 : { \
79 : .sw_if_index = ENDPOINT_INVALID_INDEX, \
80 : .ip = SESSION_IP46_ZERO, \
81 : .fib_index = ENDPOINT_INVALID_INDEX, \
82 : .is_ip4 = 0, \
83 : .port = 0, \
84 : }
85 : #define SESSION_ENDPOINT_NULL \
86 : { \
87 : .sw_if_index = ENDPOINT_INVALID_INDEX, \
88 : .ip = SESSION_IP46_ZERO, \
89 : .fib_index = ENDPOINT_INVALID_INDEX, \
90 : .is_ip4 = 0, \
91 : .port = 0, \
92 : .peer = TRANSPORT_ENDPOINT_NULL, \
93 : .transport_proto = 0, \
94 : }
95 : #define SESSION_ENDPOINT_CFG_NULL \
96 : { \
97 : .sw_if_index = ENDPOINT_INVALID_INDEX, .ip = SESSION_IP46_ZERO, \
98 : .fib_index = ENDPOINT_INVALID_INDEX, .is_ip4 = 0, .port = 0, \
99 : .peer = TRANSPORT_ENDPOINT_NULL, .transport_proto = 0, \
100 : .app_wrk_index = ENDPOINT_INVALID_INDEX, \
101 : .opaque = ENDPOINT_INVALID_INDEX, \
102 : .parent_handle = SESSION_INVALID_HANDLE, .ext_cfg = 0, \
103 : }
104 :
105 : #define session_endpoint_to_transport(_sep) ((transport_endpoint_t *)_sep)
106 : #define session_endpoint_to_transport_cfg(_sep) \
107 : ((transport_endpoint_cfg_t *)_sep)
108 :
109 : always_inline u8
110 125 : session_endpoint_fib_proto (session_endpoint_t * sep)
111 : {
112 125 : return sep->is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
113 : }
114 :
115 : static inline u8
116 112 : session_endpoint_is_local (session_endpoint_t * sep)
117 : {
118 112 : return (ip_is_zero (&sep->ip, sep->is_ip4)
119 112 : || ip_is_local_host (&sep->ip, sep->is_ip4));
120 : }
121 :
122 : static inline u8
123 214 : session_endpoint_is_zero (session_endpoint_t * sep)
124 : {
125 214 : return ip_is_zero (&sep->ip, sep->is_ip4);
126 : }
127 :
128 : typedef u8 session_type_t;
129 : typedef u64 session_handle_t;
130 :
131 : typedef enum
132 : {
133 : SESSION_CLEANUP_TRANSPORT,
134 : SESSION_CLEANUP_SESSION,
135 : } session_cleanup_ntf_t;
136 :
137 : typedef enum session_ft_action_
138 : {
139 : SESSION_FT_ACTION_ENQUEUED,
140 : SESSION_FT_ACTION_DEQUEUED,
141 : SESSION_FT_ACTION_N_ACTIONS
142 : } session_ft_action_t;
143 :
144 : /*
145 : * Session states
146 : */
147 : #define foreach_session_state \
148 : _(CREATED, "created") \
149 : _(LISTENING, "listening") \
150 : _(CONNECTING, "connecting") \
151 : _(ACCEPTING, "accepting") \
152 : _(READY, "ready") \
153 : _(OPENED, "opened") \
154 : _(TRANSPORT_CLOSING, "transport-closing") \
155 : _(CLOSING, "closing") \
156 : _(APP_CLOSED, "app-closed") \
157 : _(TRANSPORT_CLOSED, "transport-closed") \
158 : _(CLOSED, "closed") \
159 : _(TRANSPORT_DELETED, "transport-deleted") \
160 :
161 : typedef enum
162 : {
163 : #define _(sym, str) SESSION_STATE_ ## sym,
164 : foreach_session_state
165 : #undef _
166 : SESSION_N_STATES,
167 : } session_state_t;
168 :
169 : #define foreach_session_flag \
170 : _ (RX_EVT, "rx-event") \
171 : _ (PROXY, "proxy") \
172 : _ (CUSTOM_TX, "custom-tx") \
173 : _ (IS_MIGRATING, "migrating") \
174 : _ (UNIDIRECTIONAL, "unidirectional") \
175 : _ (CUSTOM_FIFO_TUNING, "custom-fifo-tuning") \
176 : _ (HALF_OPEN, "half-open") \
177 : _ (APP_CLOSED, "app-closed")
178 :
179 : typedef enum session_flags_bits_
180 : {
181 : #define _(sym, str) SESSION_F_BIT_ ## sym,
182 : foreach_session_flag
183 : #undef _
184 : SESSION_N_FLAGS
185 : } session_flag_bits_t;
186 :
187 : typedef enum session_flags_
188 : {
189 : #define _(sym, str) SESSION_F_ ## sym = 1 << SESSION_F_BIT_ ## sym,
190 : foreach_session_flag
191 : #undef _
192 : } session_flags_t;
193 :
194 : typedef struct session_
195 : {
196 : /** Pointers to rx/tx buffers. Once allocated, these do not move */
197 : svm_fifo_t *rx_fifo;
198 : svm_fifo_t *tx_fifo;
199 :
200 : /** Type built from transport and network protocol types */
201 : session_type_t session_type;
202 :
203 : /** State in session layer state machine. See @ref session_state_t */
204 : volatile u8 session_state;
205 :
206 : /** Index in thread pool where session was allocated */
207 : u32 session_index;
208 :
209 : /** Index of the app worker that owns the session */
210 : u32 app_wrk_index;
211 :
212 : /** Index of the thread that allocated the session */
213 : u8 thread_index;
214 :
215 : /** Session flags. See @ref session_flags_t */
216 : u32 flags;
217 :
218 : /** Index of the transport connection associated to the session */
219 : u32 connection_index;
220 :
221 : /** Index of application that owns the listener. Set only if a listener */
222 : u32 app_index;
223 :
224 : union
225 : {
226 : /** Parent listener session index if the result of an accept */
227 : session_handle_t listener_handle;
228 :
229 : /** App listener index in app's listener pool if a listener */
230 : u32 al_index;
231 :
232 : /** Index in app worker's half-open table if a half-open */
233 : u32 ho_index;
234 : };
235 :
236 : /** Opaque, for general use */
237 : u32 opaque;
238 :
239 : CLIB_CACHE_LINE_ALIGN_MARK (pad);
240 : } session_t;
241 :
242 : always_inline session_type_t
243 7406 : session_type_from_proto_and_ip (transport_proto_t proto, u8 is_ip4)
244 : {
245 7406 : return (proto << 1 | is_ip4);
246 : }
247 :
248 : always_inline transport_proto_t
249 18 : session_type_transport_proto (session_type_t st)
250 : {
251 18 : return (st >> 1);
252 : }
253 :
254 : always_inline u8
255 36 : session_type_is_ip4 (session_type_t st)
256 : {
257 36 : return (st & 1);
258 : }
259 :
260 : always_inline transport_proto_t
261 8160813 : session_get_transport_proto (session_t * s)
262 : {
263 8160813 : return (s->session_type >> 1);
264 : }
265 :
266 : always_inline fib_protocol_t
267 0 : session_get_fib_proto (session_t * s)
268 : {
269 0 : u8 is_ip4 = s->session_type & 1;
270 0 : return (is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6);
271 : }
272 :
273 : always_inline u8
274 124911 : session_has_transport (session_t * s)
275 : {
276 124911 : return (session_get_transport_proto (s) != TRANSPORT_PROTO_NONE);
277 : }
278 :
279 : static inline transport_service_type_t
280 43 : session_transport_service_type (session_t * s)
281 : {
282 : transport_proto_t tp;
283 43 : tp = session_get_transport_proto (s);
284 43 : return transport_protocol_service_type (tp);
285 : }
286 :
287 : static inline transport_tx_fn_type_t
288 : session_transport_tx_fn_type (session_t * s)
289 : {
290 : transport_proto_t tp;
291 : tp = session_get_transport_proto (s);
292 : return transport_protocol_tx_fn_type (tp);
293 : }
294 :
295 : static inline u8
296 : session_tx_is_dgram (session_t * s)
297 : {
298 : return (session_transport_tx_fn_type (s) == TRANSPORT_TX_DGRAM);
299 : }
300 :
301 : always_inline session_handle_t
302 10128 : session_handle (session_t * s)
303 : {
304 10128 : return ((u64) s->thread_index << 32) | (u64) s->session_index;
305 : }
306 :
307 : always_inline u32
308 1423143 : session_index_from_handle (session_handle_t handle)
309 : {
310 1423143 : return handle & 0xFFFFFFFF;
311 : }
312 :
313 : always_inline u32
314 1422835 : session_thread_from_handle (session_handle_t handle)
315 : {
316 1422835 : return handle >> 32;
317 : }
318 :
319 : always_inline void
320 1364639 : session_parse_handle (session_handle_t handle, u32 * index,
321 : u32 * thread_index)
322 : {
323 1364639 : *index = session_index_from_handle (handle);
324 1364639 : *thread_index = session_thread_from_handle (handle);
325 1364639 : }
326 :
327 : static inline session_handle_t
328 0 : session_make_handle (u32 session_index, u32 data)
329 : {
330 0 : return (((u64) data << 32) | (u64) session_index);
331 : }
332 :
333 : always_inline u32
334 : session_handle_index (session_handle_t ho_handle)
335 : {
336 : return (ho_handle & 0xffffffff);
337 : }
338 :
339 : always_inline u32
340 : session_handle_data (session_handle_t ho_handle)
341 : {
342 : return (ho_handle >> 32);
343 : }
344 :
345 : typedef enum
346 : {
347 : SESSION_IO_EVT_RX,
348 : SESSION_IO_EVT_TX,
349 : SESSION_IO_EVT_TX_FLUSH,
350 : SESSION_IO_EVT_BUILTIN_RX,
351 : SESSION_IO_EVT_TX_MAIN,
352 : SESSION_CTRL_EVT_RPC,
353 : SESSION_CTRL_EVT_HALF_CLOSE,
354 : SESSION_CTRL_EVT_CLOSE,
355 : SESSION_CTRL_EVT_RESET,
356 : SESSION_CTRL_EVT_BOUND,
357 : SESSION_CTRL_EVT_UNLISTEN_REPLY,
358 : SESSION_CTRL_EVT_ACCEPTED,
359 : SESSION_CTRL_EVT_ACCEPTED_REPLY,
360 : SESSION_CTRL_EVT_CONNECTED,
361 : SESSION_CTRL_EVT_DISCONNECTED,
362 : SESSION_CTRL_EVT_DISCONNECTED_REPLY,
363 : SESSION_CTRL_EVT_RESET_REPLY,
364 : SESSION_CTRL_EVT_REQ_WORKER_UPDATE,
365 : SESSION_CTRL_EVT_WORKER_UPDATE,
366 : SESSION_CTRL_EVT_WORKER_UPDATE_REPLY,
367 : SESSION_CTRL_EVT_SHUTDOWN,
368 : SESSION_CTRL_EVT_DISCONNECT,
369 : SESSION_CTRL_EVT_CONNECT,
370 : SESSION_CTRL_EVT_CONNECT_URI,
371 : SESSION_CTRL_EVT_LISTEN,
372 : SESSION_CTRL_EVT_LISTEN_URI,
373 : SESSION_CTRL_EVT_UNLISTEN,
374 : SESSION_CTRL_EVT_APP_DETACH,
375 : SESSION_CTRL_EVT_APP_ADD_SEGMENT,
376 : SESSION_CTRL_EVT_APP_DEL_SEGMENT,
377 : SESSION_CTRL_EVT_MIGRATED,
378 : SESSION_CTRL_EVT_CLEANUP,
379 : SESSION_CTRL_EVT_APP_WRK_RPC,
380 : SESSION_CTRL_EVT_TRANSPORT_ATTR,
381 : SESSION_CTRL_EVT_TRANSPORT_ATTR_REPLY,
382 : } session_evt_type_t;
383 :
384 : #define foreach_session_ctrl_evt \
385 : _ (LISTEN, listen) \
386 : _ (LISTEN_URI, listen_uri) \
387 : _ (BOUND, bound) \
388 : _ (UNLISTEN, unlisten) \
389 : _ (UNLISTEN_REPLY, unlisten_reply) \
390 : _ (ACCEPTED, accepted) \
391 : _ (ACCEPTED_REPLY, accepted_reply) \
392 : _ (CONNECT, connect) \
393 : _ (CONNECT_URI, connect_uri) \
394 : _ (CONNECTED, connected) \
395 : _ (SHUTDOWN, shutdown) \
396 : _ (DISCONNECT, disconnect) \
397 : _ (DISCONNECTED, disconnected) \
398 : _ (DISCONNECTED_REPLY, disconnected_reply) \
399 : _ (RESET_REPLY, reset_reply) \
400 : _ (REQ_WORKER_UPDATE, req_worker_update) \
401 : _ (WORKER_UPDATE, worker_update) \
402 : _ (WORKER_UPDATE_REPLY, worker_update_reply) \
403 : _ (APP_DETACH, app_detach) \
404 : _ (APP_ADD_SEGMENT, app_add_segment) \
405 : _ (APP_DEL_SEGMENT, app_del_segment) \
406 : _ (MIGRATED, migrated) \
407 : _ (CLEANUP, cleanup) \
408 : _ (APP_WRK_RPC, app_wrk_rpc) \
409 : _ (TRANSPORT_ATTR, transport_attr) \
410 : _ (TRANSPORT_ATTR_REPLY, transport_attr_reply) \
411 : /* Deprecated and will be removed. Use types above */
412 : #define FIFO_EVENT_APP_RX SESSION_IO_EVT_RX
413 : #define FIFO_EVENT_APP_TX SESSION_IO_EVT_TX
414 : #define FIFO_EVENT_DISCONNECT SESSION_CTRL_EVT_CLOSE
415 : #define FIFO_EVENT_BUILTIN_RX SESSION_IO_EVT_BUILTIN_RX
416 :
417 : typedef enum
418 : {
419 : SESSION_MQ_IO_EVT_RING,
420 : SESSION_MQ_CTRL_EVT_RING,
421 : SESSION_MQ_N_RINGS
422 : } session_mq_rings_e;
423 :
424 : typedef struct
425 : {
426 : void *fp;
427 : void *arg;
428 : } session_rpc_args_t;
429 :
430 : typedef struct
431 : {
432 : u8 event_type;
433 : u8 postponed;
434 : union
435 : {
436 : u32 session_index;
437 : session_handle_t session_handle;
438 : session_rpc_args_t rpc_args;
439 : u32 ctrl_data_index;
440 : struct
441 : {
442 : u8 data[0];
443 : };
444 : };
445 : } __clib_packed session_event_t;
446 :
447 : #define SESSION_MSG_NULL { }
448 :
449 : typedef struct session_dgram_pre_hdr_
450 : {
451 : u32 data_length;
452 : u32 data_offset;
453 : } session_dgram_pre_hdr_t;
454 :
455 : typedef struct session_dgram_header_
456 : {
457 : u32 data_length;
458 : u32 data_offset;
459 : ip46_address_t rmt_ip;
460 : ip46_address_t lcl_ip;
461 : u16 rmt_port;
462 : u16 lcl_port;
463 : u8 is_ip4;
464 : u16 gso_size;
465 : } __clib_packed session_dgram_hdr_t;
466 :
467 : #define SESSION_CONN_ID_LEN 37
468 : #define SESSION_CONN_HDR_LEN 47
469 : STATIC_ASSERT (sizeof (session_dgram_hdr_t) == (SESSION_CONN_ID_LEN + 10),
470 : "session conn id wrong length");
471 :
472 : #define foreach_session_error \
473 : _ (NONE, "no error") \
474 : _ (UNKNOWN, "generic/unknown error") \
475 : _ (REFUSED, "refused") \
476 : _ (TIMEDOUT, "timedout") \
477 : _ (ALLOC, "obj/memory allocation error") \
478 : _ (OWNER, "object not owned by application") \
479 : _ (NOROUTE, "no route") \
480 : _ (NOINTF, "no resolving interface") \
481 : _ (NOIP, "no ip for lcl interface") \
482 : _ (NOPORT, "no lcl port") \
483 : _ (NOSUPPORT, "not supported") \
484 : _ (NOLISTEN, "not listening") \
485 : _ (NOSESSION, "session does not exist") \
486 : _ (NOAPP, "app not attached") \
487 : _ (PORTINUSE, "lcl port in use") \
488 : _ (IPINUSE, "ip in use") \
489 : _ (ALREADY_LISTENING, "ip port pair already listened on") \
490 : _ (INVALID, "invalid value") \
491 : _ (INVALID_RMT_IP, "invalid remote ip") \
492 : _ (INVALID_APPWRK, "invalid app worker") \
493 : _ (INVALID_NS, "invalid namespace") \
494 : _ (SEG_NO_SPACE, "Couldn't allocate a fifo pair") \
495 : _ (SEG_NO_SPACE2, "Created segment, couldn't allocate a fifo pair") \
496 : _ (SEG_CREATE, "Couldn't create a new segment") \
497 : _ (FILTERED, "session filtered") \
498 : _ (SCOPE, "scope not supported") \
499 : _ (BAPI_NO_FD, "bapi doesn't have a socket fd") \
500 : _ (BAPI_SEND_FD, "couldn't send fd over bapi socket fd") \
501 : _ (BAPI_NO_REG, "app bapi registration not found") \
502 : _ (MQ_MSG_ALLOC, "failed to alloc mq msg") \
503 : _ (TLS_HANDSHAKE, "failed tls handshake") \
504 : _ (EVENTFD_ALLOC, "failed to alloc eventfd") \
505 : _ (NOEXTCFG, "no extended transport config") \
506 : _ (NOCRYPTOENG, "no crypto engine") \
507 : _ (NOCRYPTOCKP, "cert key pair not found ") \
508 : _ (LOCAL_CONNECT, "could not connect with local scope") \
509 : _ (TRANSPORT_NO_REG, "transport was not registered")
510 :
511 : typedef enum session_error_p_
512 : {
513 : #define _(sym, str) SESSION_EP_##sym,
514 : foreach_session_error
515 : #undef _
516 : SESSION_N_ERRORS
517 : } session_error_p_t;
518 :
519 : typedef enum session_error_
520 : {
521 : #define _(sym, str) SESSION_E_##sym = -SESSION_EP_##sym,
522 : foreach_session_error
523 : #undef _
524 : } session_error_t;
525 :
526 : #define SESSION_CLI_ID_LEN "60"
527 : #define SESSION_CLI_STATE_LEN "15"
528 :
529 : /* Maintained for compatibility. Will be deprecated */
530 : #define SESSION_ERROR_SEG_CREATE SESSION_E_SEG_CREATE
531 : #define SESSION_ERROR_NO_SPACE SESSION_E_SEG_NO_SPACE
532 : #define SESSION_ERROR_NEW_SEG_NO_SPACE SESSION_E_SEG_NO_SPACE2
533 :
534 : #endif /* SRC_VNET_SESSION_SESSION_TYPES_H_ */
535 :
536 : /*
537 : * fd.io coding-style-patch-verification: ON
538 : *
539 : * Local Variables:
540 : * eval: (c-set-style "gnu")
541 : * End:
542 : */
|