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