vcl: enable gso for 'sendmsg' in LDP mode.
[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 #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 session_endpoint_fib_proto (session_endpoint_t * sep)
111 {
112   return sep->is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
113 }
114
115 static inline u8
116 session_endpoint_is_local (session_endpoint_t * sep)
117 {
118   return (ip_is_zero (&sep->ip, sep->is_ip4)
119           || ip_is_local_host (&sep->ip, sep->is_ip4));
120 }
121
122 static inline u8
123 session_endpoint_is_zero (session_endpoint_t * sep)
124 {
125   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
178 typedef enum session_flags_bits_
179 {
180 #define _(sym, str) SESSION_F_BIT_ ## sym,
181   foreach_session_flag
182 #undef _
183   SESSION_N_FLAGS
184 } session_flag_bits_t;
185
186 typedef enum session_flags_
187 {
188 #define _(sym, str) SESSION_F_ ## sym = 1 << SESSION_F_BIT_ ## sym,
189   foreach_session_flag
190 #undef _
191 } session_flags_t;
192
193 typedef struct session_
194 {
195   /** Pointers to rx/tx buffers. Once allocated, these do not move */
196   svm_fifo_t *rx_fifo;
197   svm_fifo_t *tx_fifo;
198
199   /** Type built from transport and network protocol types */
200   session_type_t session_type;
201
202   /** State in session layer state machine. See @ref session_state_t */
203   volatile u8 session_state;
204
205   /** Index in thread pool where session was allocated */
206   u32 session_index;
207
208   /** Index of the app worker that owns the session */
209   u32 app_wrk_index;
210
211   /** Index of the thread that allocated the session */
212   u8 thread_index;
213
214   /** Session flags. See @ref session_flags_t */
215   u32 flags;
216
217   /** Index of the transport connection associated to the session */
218   u32 connection_index;
219
220   /** Index of application that owns the listener. Set only if a listener */
221   u32 app_index;
222
223   union
224   {
225     /** Parent listener session index if the result of an accept */
226     session_handle_t listener_handle;
227
228     /** App listener index in app's listener pool if a listener */
229     u32 al_index;
230
231     /** Index in app worker's half-open table if a half-open */
232     u32 ho_index;
233   };
234
235   /** Opaque, for general use */
236   u32 opaque;
237
238     CLIB_CACHE_LINE_ALIGN_MARK (pad);
239 } session_t;
240
241 always_inline session_type_t
242 session_type_from_proto_and_ip (transport_proto_t proto, u8 is_ip4)
243 {
244   return (proto << 1 | is_ip4);
245 }
246
247 always_inline transport_proto_t
248 session_type_transport_proto (session_type_t st)
249 {
250   return (st >> 1);
251 }
252
253 always_inline u8
254 session_type_is_ip4 (session_type_t st)
255 {
256   return (st & 1);
257 }
258
259 always_inline transport_proto_t
260 session_get_transport_proto (session_t * s)
261 {
262   return (s->session_type >> 1);
263 }
264
265 always_inline fib_protocol_t
266 session_get_fib_proto (session_t * s)
267 {
268   u8 is_ip4 = s->session_type & 1;
269   return (is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6);
270 }
271
272 always_inline u8
273 session_has_transport (session_t * s)
274 {
275   return (session_get_transport_proto (s) != TRANSPORT_PROTO_NONE);
276 }
277
278 static inline transport_service_type_t
279 session_transport_service_type (session_t * s)
280 {
281   transport_proto_t tp;
282   tp = session_get_transport_proto (s);
283   return transport_protocol_service_type (tp);
284 }
285
286 static inline transport_tx_fn_type_t
287 session_transport_tx_fn_type (session_t * s)
288 {
289   transport_proto_t tp;
290   tp = session_get_transport_proto (s);
291   return transport_protocol_tx_fn_type (tp);
292 }
293
294 static inline u8
295 session_tx_is_dgram (session_t * s)
296 {
297   return (session_transport_tx_fn_type (s) == TRANSPORT_TX_DGRAM);
298 }
299
300 always_inline session_handle_t
301 session_handle (session_t * s)
302 {
303   return ((u64) s->thread_index << 32) | (u64) s->session_index;
304 }
305
306 always_inline u32
307 session_index_from_handle (session_handle_t handle)
308 {
309   return handle & 0xFFFFFFFF;
310 }
311
312 always_inline u32
313 session_thread_from_handle (session_handle_t handle)
314 {
315   return handle >> 32;
316 }
317
318 always_inline void
319 session_parse_handle (session_handle_t handle, u32 * index,
320                       u32 * thread_index)
321 {
322   *index = session_index_from_handle (handle);
323   *thread_index = session_thread_from_handle (handle);
324 }
325
326 static inline session_handle_t
327 session_make_handle (u32 session_index, u32 data)
328 {
329   return (((u64) data << 32) | (u64) session_index);
330 }
331
332 always_inline u32
333 session_handle_index (session_handle_t ho_handle)
334 {
335   return (ho_handle & 0xffffffff);
336 }
337
338 always_inline u32
339 session_handle_data (session_handle_t ho_handle)
340 {
341   return (ho_handle >> 32);
342 }
343
344 typedef enum
345 {
346   SESSION_IO_EVT_RX,
347   SESSION_IO_EVT_TX,
348   SESSION_IO_EVT_TX_FLUSH,
349   SESSION_IO_EVT_BUILTIN_RX,
350   SESSION_IO_EVT_BUILTIN_TX,
351   SESSION_CTRL_EVT_RPC,
352   SESSION_CTRL_EVT_HALF_CLOSE,
353   SESSION_CTRL_EVT_CLOSE,
354   SESSION_CTRL_EVT_RESET,
355   SESSION_CTRL_EVT_BOUND,
356   SESSION_CTRL_EVT_UNLISTEN_REPLY,
357   SESSION_CTRL_EVT_ACCEPTED,
358   SESSION_CTRL_EVT_ACCEPTED_REPLY,
359   SESSION_CTRL_EVT_CONNECTED,
360   SESSION_CTRL_EVT_DISCONNECTED,
361   SESSION_CTRL_EVT_DISCONNECTED_REPLY,
362   SESSION_CTRL_EVT_RESET_REPLY,
363   SESSION_CTRL_EVT_REQ_WORKER_UPDATE,
364   SESSION_CTRL_EVT_WORKER_UPDATE,
365   SESSION_CTRL_EVT_WORKER_UPDATE_REPLY,
366   SESSION_CTRL_EVT_SHUTDOWN,
367   SESSION_CTRL_EVT_DISCONNECT,
368   SESSION_CTRL_EVT_CONNECT,
369   SESSION_CTRL_EVT_CONNECT_URI,
370   SESSION_CTRL_EVT_LISTEN,
371   SESSION_CTRL_EVT_LISTEN_URI,
372   SESSION_CTRL_EVT_UNLISTEN,
373   SESSION_CTRL_EVT_APP_DETACH,
374   SESSION_CTRL_EVT_APP_ADD_SEGMENT,
375   SESSION_CTRL_EVT_APP_DEL_SEGMENT,
376   SESSION_CTRL_EVT_MIGRATED,
377   SESSION_CTRL_EVT_CLEANUP,
378   SESSION_CTRL_EVT_APP_WRK_RPC,
379   SESSION_CTRL_EVT_TRANSPORT_ATTR,
380   SESSION_CTRL_EVT_TRANSPORT_ATTR_REPLY,
381 } session_evt_type_t;
382
383 #define foreach_session_ctrl_evt                                              \
384   _ (LISTEN, listen)                                                          \
385   _ (LISTEN_URI, listen_uri)                                                  \
386   _ (BOUND, bound)                                                            \
387   _ (UNLISTEN, unlisten)                                                      \
388   _ (UNLISTEN_REPLY, unlisten_reply)                                          \
389   _ (ACCEPTED, accepted)                                                      \
390   _ (ACCEPTED_REPLY, accepted_reply)                                          \
391   _ (CONNECT, connect)                                                        \
392   _ (CONNECT_URI, connect_uri)                                                \
393   _ (CONNECTED, connected)                                                    \
394   _ (SHUTDOWN, shutdown)                                                      \
395   _ (DISCONNECT, disconnect)                                                  \
396   _ (DISCONNECTED, disconnected)                                              \
397   _ (DISCONNECTED_REPLY, disconnected_reply)                                  \
398   _ (RESET_REPLY, reset_reply)                                                \
399   _ (REQ_WORKER_UPDATE, req_worker_update)                                    \
400   _ (WORKER_UPDATE, worker_update)                                            \
401   _ (WORKER_UPDATE_REPLY, worker_update_reply)                                \
402   _ (APP_DETACH, app_detach)                                                  \
403   _ (APP_ADD_SEGMENT, app_add_segment)                                        \
404   _ (APP_DEL_SEGMENT, app_del_segment)                                        \
405   _ (MIGRATED, migrated)                                                      \
406   _ (CLEANUP, cleanup)                                                        \
407   _ (APP_WRK_RPC, app_wrk_rpc)                                                \
408   _ (TRANSPORT_ATTR, transport_attr)                                          \
409   _ (TRANSPORT_ATTR_REPLY, transport_attr_reply)                              \
410 /* Deprecated and will be removed. Use types above */
411 #define FIFO_EVENT_APP_RX SESSION_IO_EVT_RX
412 #define FIFO_EVENT_APP_TX SESSION_IO_EVT_TX
413 #define FIFO_EVENT_DISCONNECT SESSION_CTRL_EVT_CLOSE
414 #define FIFO_EVENT_BUILTIN_RX SESSION_IO_EVT_BUILTIN_RX
415 #define FIFO_EVENT_BUILTIN_TX SESSION_IO_EVT_BUILTIN_TX
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  */