session: increase max ctrl msg size
[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 68
25
26 #define foreach_session_endpoint_fields                         \
27   foreach_transport_endpoint_cfg_fields                         \
28   _(u8, transport_proto)                                        \
29
30 typedef struct _session_endpoint
31 {
32 #define _(type, name) type name;
33   foreach_session_endpoint_fields
34 #undef _
35 } session_endpoint_t;
36
37 typedef struct _session_endpoint_cfg
38 {
39 #define _(type, name) type name;
40   foreach_session_endpoint_fields
41 #undef _
42   u32 app_wrk_index;
43   u32 opaque;
44   u32 ns_index;
45   u8 original_tp;
46   u8 *hostname;
47   u64 parent_handle;
48   u32 ckpair_index;
49 } session_endpoint_cfg_t;
50
51 #define SESSION_IP46_ZERO                       \
52 {                                               \
53     .ip6 = {                                    \
54         { 0, 0, },                              \
55     },                                          \
56 }
57
58 #define TRANSPORT_ENDPOINT_NULL                 \
59 {                                               \
60   .sw_if_index = ENDPOINT_INVALID_INDEX,        \
61   .ip = SESSION_IP46_ZERO,                      \
62   .fib_index = ENDPOINT_INVALID_INDEX,          \
63   .is_ip4 = 0,                                  \
64   .port = 0,                                    \
65 }
66 #define SESSION_ENDPOINT_NULL                   \
67 {                                               \
68   .sw_if_index = ENDPOINT_INVALID_INDEX,        \
69   .ip = SESSION_IP46_ZERO,                      \
70   .fib_index = ENDPOINT_INVALID_INDEX,          \
71   .is_ip4 = 0,                                  \
72   .port = 0,                                    \
73   .peer = TRANSPORT_ENDPOINT_NULL,              \
74   .transport_proto = 0,                         \
75 }
76 #define SESSION_ENDPOINT_CFG_NULL               \
77 {                                               \
78   .sw_if_index = ENDPOINT_INVALID_INDEX,        \
79   .ip = SESSION_IP46_ZERO,                      \
80   .fib_index = ENDPOINT_INVALID_INDEX,          \
81   .is_ip4 = 0,                                  \
82   .port = 0,                                    \
83   .peer = TRANSPORT_ENDPOINT_NULL,              \
84   .transport_proto = 0,                         \
85   .app_wrk_index = ENDPOINT_INVALID_INDEX,      \
86   .opaque = ENDPOINT_INVALID_INDEX,             \
87   .hostname = 0,                                \
88   .parent_handle = SESSION_INVALID_HANDLE,      \
89   .ckpair_index = 0                             \
90 }
91
92 #define session_endpoint_to_transport(_sep) ((transport_endpoint_t *)_sep)
93 #define session_endpoint_to_transport_cfg(_sep)         \
94   ((transport_endpoint_cfg_t *)_sep)
95
96 always_inline u8
97 session_endpoint_fib_proto (session_endpoint_t * sep)
98 {
99   return sep->is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
100 }
101
102 static inline u8
103 session_endpoint_is_local (session_endpoint_t * sep)
104 {
105   return (ip_is_zero (&sep->ip, sep->is_ip4)
106           || ip_is_local_host (&sep->ip, sep->is_ip4));
107 }
108
109 static inline u8
110 session_endpoint_is_zero (session_endpoint_t * sep)
111 {
112   return ip_is_zero (&sep->ip, sep->is_ip4);
113 }
114
115 typedef u8 session_type_t;
116 typedef u64 session_handle_t;
117
118 typedef enum
119 {
120   SESSION_CLEANUP_TRANSPORT,
121   SESSION_CLEANUP_SESSION,
122 } session_cleanup_ntf_t;
123
124 /*
125  * Session states
126  */
127 #define foreach_session_state                           \
128   _(CREATED, "created")                                 \
129   _(LISTENING, "listening")                             \
130   _(CONNECTING, "connecting")                           \
131   _(ACCEPTING, "accepting")                             \
132   _(READY, "ready")                                     \
133   _(OPENED, "opened")                                   \
134   _(TRANSPORT_CLOSING, "transport-closing")             \
135   _(CLOSING, "closing")                                 \
136   _(APP_CLOSED, "app-closed")                           \
137   _(TRANSPORT_CLOSED, "transport-closed")               \
138   _(TRANSPORT_DELETED, "transport-deleted")             \
139   _(CLOSED, "closed")                                   \
140
141 typedef enum
142 {
143 #define _(sym, str) SESSION_STATE_ ## sym,
144   foreach_session_state
145 #undef _
146     SESSION_N_STATES,
147 } session_state_t;
148
149 typedef enum session_flags_
150 {
151   SESSION_F_RX_EVT = 1,
152   SESSION_F_PROXY = (1 << 1),
153   SESSION_F_CUSTOM_TX = (1 << 2),
154   SESSION_F_IS_MIGRATING = (1 << 3),
155 } session_flags_t;
156
157 typedef struct session_
158 {
159   /** Pointers to rx/tx buffers. Once allocated, these do not move */
160   svm_fifo_t *rx_fifo;
161   svm_fifo_t *tx_fifo;
162
163   /** Type built from transport and network protocol types */
164   session_type_t session_type;
165
166   /** State in session layer state machine. See @ref session_state_t */
167   volatile u8 session_state;
168
169   /** Index in thread pool where session was allocated */
170   u32 session_index;
171
172   /** Index of the app worker that owns the session */
173   u32 app_wrk_index;
174
175   /** Index of the thread that allocated the session */
176   u8 thread_index;
177
178   /** Session flags. See @ref session_flags_t */
179   u32 flags;
180
181   /** Index of the transport connection associated to the session */
182   u32 connection_index;
183
184   /** Index of application that owns the listener. Set only if a listener */
185   u32 app_index;
186
187   union
188   {
189     /** Parent listener session index if the result of an accept */
190     session_handle_t listener_handle;
191
192     /** App listener index in app's listener pool if a listener */
193     u32 al_index;
194   };
195
196   /** Opaque, for general use */
197   u32 opaque;
198
199     CLIB_CACHE_LINE_ALIGN_MARK (pad);
200 } session_t;
201
202 always_inline session_type_t
203 session_type_from_proto_and_ip (transport_proto_t proto, u8 is_ip4)
204 {
205   return (proto << 1 | is_ip4);
206 }
207
208 always_inline transport_proto_t
209 session_type_transport_proto (session_type_t st)
210 {
211   return (st >> 1);
212 }
213
214 always_inline u8
215 session_type_is_ip4 (session_type_t st)
216 {
217   return (st & 1);
218 }
219
220 always_inline transport_proto_t
221 session_get_transport_proto (session_t * s)
222 {
223   return (s->session_type >> 1);
224 }
225
226 always_inline fib_protocol_t
227 session_get_fib_proto (session_t * s)
228 {
229   u8 is_ip4 = s->session_type & 1;
230   return (is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6);
231 }
232
233 always_inline u8
234 session_has_transport (session_t * s)
235 {
236   return (session_get_transport_proto (s) != TRANSPORT_PROTO_NONE);
237 }
238
239 static inline transport_service_type_t
240 session_transport_service_type (session_t * s)
241 {
242   transport_proto_t tp;
243   tp = session_get_transport_proto (s);
244   return transport_protocol_service_type (tp);
245 }
246
247 static inline transport_tx_fn_type_t
248 session_transport_tx_fn_type (session_t * s)
249 {
250   transport_proto_t tp;
251   tp = session_get_transport_proto (s);
252   return transport_protocol_tx_fn_type (tp);
253 }
254
255 static inline u8
256 session_tx_is_dgram (session_t * s)
257 {
258   return (session_transport_tx_fn_type (s) == TRANSPORT_TX_DGRAM);
259 }
260
261 always_inline session_handle_t
262 session_handle (session_t * s)
263 {
264   return ((u64) s->thread_index << 32) | (u64) s->session_index;
265 }
266
267 always_inline u32
268 session_index_from_handle (session_handle_t handle)
269 {
270   return handle & 0xFFFFFFFF;
271 }
272
273 always_inline u32
274 session_thread_from_handle (session_handle_t handle)
275 {
276   return handle >> 32;
277 }
278
279 always_inline void
280 session_parse_handle (session_handle_t handle, u32 * index,
281                       u32 * thread_index)
282 {
283   *index = session_index_from_handle (handle);
284   *thread_index = session_thread_from_handle (handle);
285 }
286
287 static inline session_handle_t
288 session_make_handle (u32 session_index, u32 thread_index)
289 {
290   return (((u64) thread_index << 32) | (u64) session_index);
291 }
292
293 typedef enum
294 {
295   SESSION_IO_EVT_RX,
296   SESSION_IO_EVT_TX,
297   SESSION_IO_EVT_TX_FLUSH,
298   SESSION_IO_EVT_BUILTIN_RX,
299   SESSION_IO_EVT_BUILTIN_TX,
300   SESSION_CTRL_EVT_RPC,
301   SESSION_CTRL_EVT_CLOSE,
302   SESSION_CTRL_EVT_RESET,
303   SESSION_CTRL_EVT_BOUND,
304   SESSION_CTRL_EVT_UNLISTEN_REPLY,
305   SESSION_CTRL_EVT_ACCEPTED,
306   SESSION_CTRL_EVT_ACCEPTED_REPLY,
307   SESSION_CTRL_EVT_CONNECTED,
308   SESSION_CTRL_EVT_DISCONNECTED,
309   SESSION_CTRL_EVT_DISCONNECTED_REPLY,
310   SESSION_CTRL_EVT_RESET_REPLY,
311   SESSION_CTRL_EVT_REQ_WORKER_UPDATE,
312   SESSION_CTRL_EVT_WORKER_UPDATE,
313   SESSION_CTRL_EVT_WORKER_UPDATE_REPLY,
314   SESSION_CTRL_EVT_DISCONNECT,
315   SESSION_CTRL_EVT_CONNECT,
316   SESSION_CTRL_EVT_CONNECT_URI,
317   SESSION_CTRL_EVT_LISTEN,
318   SESSION_CTRL_EVT_LISTEN_URI,
319   SESSION_CTRL_EVT_UNLISTEN,
320   SESSION_CTRL_EVT_APP_DETACH,
321 } session_evt_type_t;
322
323 #define foreach_session_ctrl_evt                                \
324   _(LISTEN, listen)                                             \
325   _(LISTEN_URI, listen_uri)                                     \
326   _(BOUND, bound)                                               \
327   _(UNLISTEN, unlisten)                                         \
328   _(UNLISTEN_REPLY, unlisten_reply)                             \
329   _(ACCEPTED, accepted)                                         \
330   _(ACCEPTED_REPLY, accepted_reply)                             \
331   _(CONNECT, connect)                                           \
332   _(CONNECT_URI, connect_uri)                                   \
333   _(CONNECTED, connected)                                       \
334   _(DISCONNECT, disconnect)                                     \
335   _(DISCONNECTED, disconnected)                                 \
336   _(DISCONNECTED_REPLY, disconnected_reply)                     \
337   _(RESET_REPLY, reset_reply)                                   \
338   _(REQ_WORKER_UPDATE, req_worker_update)                       \
339   _(WORKER_UPDATE, worker_update)                               \
340   _(WORKER_UPDATE_REPLY, worker_update_reply)                   \
341   _(APP_DETACH, app_detach)                                     \
342
343
344 /* Deprecated and will be removed. Use types above */
345 #define FIFO_EVENT_APP_RX SESSION_IO_EVT_RX
346 #define FIFO_EVENT_APP_TX SESSION_IO_EVT_TX
347 #define FIFO_EVENT_DISCONNECT SESSION_CTRL_EVT_CLOSE
348 #define FIFO_EVENT_BUILTIN_RX SESSION_IO_EVT_BUILTIN_RX
349 #define FIFO_EVENT_BUILTIN_TX SESSION_IO_EVT_BUILTIN_TX
350
351 typedef enum
352 {
353   SESSION_MQ_IO_EVT_RING,
354   SESSION_MQ_CTRL_EVT_RING,
355   SESSION_MQ_N_RINGS
356 } session_mq_rings_e;
357
358 typedef struct
359 {
360   void *fp;
361   void *arg;
362 } session_rpc_args_t;
363
364 typedef struct
365 {
366   u8 event_type;
367   u8 postponed;
368   union
369   {
370     u32 session_index;
371     session_handle_t session_handle;
372     session_rpc_args_t rpc_args;
373     u32 ctrl_data_index;
374     struct
375     {
376       u8 data[0];
377     };
378   };
379 } __clib_packed session_event_t;
380
381 #define SESSION_MSG_NULL { }
382
383 typedef struct session_dgram_pre_hdr_
384 {
385   u32 data_length;
386   u32 data_offset;
387 } session_dgram_pre_hdr_t;
388
389 typedef struct session_dgram_header_
390 {
391   u32 data_length;
392   u32 data_offset;
393   ip46_address_t rmt_ip;
394   ip46_address_t lcl_ip;
395   u16 rmt_port;
396   u16 lcl_port;
397   u8 is_ip4;
398 } __clib_packed session_dgram_hdr_t;
399
400 #define SESSION_CONN_ID_LEN 37
401 #define SESSION_CONN_HDR_LEN 45
402
403 STATIC_ASSERT (sizeof (session_dgram_hdr_t) == (SESSION_CONN_ID_LEN + 8),
404                "session conn id wrong length");
405 #endif /* SRC_VNET_SESSION_SESSION_TYPES_H_ */
406
407 /*
408  * fd.io coding-style-patch-verification: ON
409  *
410  * Local Variables:
411  * eval: (c-set-style "gnu")
412  * End:
413  */