session: reorganize session fields
[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_LISTENER_PREFIX         0x5FFFFFFF
23
24 #define foreach_session_endpoint_fields                         \
25   foreach_transport_endpoint_cfg_fields                         \
26   _(u8, transport_proto)                                        \
27
28 typedef struct _session_endpoint
29 {
30 #define _(type, name) type name;
31   foreach_session_endpoint_fields
32 #undef _
33 } session_endpoint_t;
34
35 typedef struct _session_endpoint_cfg
36 {
37 #define _(type, name) type name;
38   foreach_session_endpoint_fields
39 #undef _
40   u32 app_wrk_index;
41   u32 opaque;
42   u32 ns_index;
43   u8 original_tp;
44   u8 *hostname;
45 } session_endpoint_cfg_t;
46
47 #define SESSION_IP46_ZERO                       \
48 {                                               \
49     .ip6 = {                                    \
50         { 0, 0, },                              \
51     },                                          \
52 }
53
54 #define TRANSPORT_ENDPOINT_NULL                 \
55 {                                               \
56   .sw_if_index = ENDPOINT_INVALID_INDEX,        \
57   .ip = SESSION_IP46_ZERO,                      \
58   .fib_index = ENDPOINT_INVALID_INDEX,          \
59   .is_ip4 = 0,                                  \
60   .port = 0,                                    \
61 }
62 #define SESSION_ENDPOINT_NULL                   \
63 {                                               \
64   .sw_if_index = ENDPOINT_INVALID_INDEX,        \
65   .ip = SESSION_IP46_ZERO,                      \
66   .fib_index = ENDPOINT_INVALID_INDEX,          \
67   .is_ip4 = 0,                                  \
68   .port = 0,                                    \
69   .peer = TRANSPORT_ENDPOINT_NULL,              \
70   .transport_proto = 0,                         \
71 }
72 #define SESSION_ENDPOINT_CFG_NULL               \
73 {                                               \
74   .sw_if_index = ENDPOINT_INVALID_INDEX,        \
75   .ip = SESSION_IP46_ZERO,                      \
76   .fib_index = ENDPOINT_INVALID_INDEX,          \
77   .is_ip4 = 0,                                  \
78   .port = 0,                                    \
79   .peer = TRANSPORT_ENDPOINT_NULL,              \
80   .transport_proto = 0,                         \
81   .app_wrk_index = ENDPOINT_INVALID_INDEX,      \
82   .opaque = ENDPOINT_INVALID_INDEX,             \
83   .hostname = 0,                                \
84 }
85
86 #define session_endpoint_to_transport(_sep) ((transport_endpoint_t *)_sep)
87 #define session_endpoint_to_transport_cfg(_sep)         \
88   ((transport_endpoint_cfg_t *)_sep)
89
90 always_inline u8
91 session_endpoint_fib_proto (session_endpoint_t * sep)
92 {
93   return sep->is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
94 }
95
96 static inline u8
97 session_endpoint_is_local (session_endpoint_t * sep)
98 {
99   return (ip_is_zero (&sep->ip, sep->is_ip4)
100           || ip_is_local_host (&sep->ip, sep->is_ip4));
101 }
102
103 static inline u8
104 session_endpoint_is_zero (session_endpoint_t * sep)
105 {
106   return ip_is_zero (&sep->ip, sep->is_ip4);
107 }
108
109 typedef u8 session_type_t;
110 typedef u64 session_handle_t;
111
112 /*
113  * Session states
114  */
115 typedef enum
116 {
117   SESSION_STATE_CREATED,
118   SESSION_STATE_LISTENING,
119   SESSION_STATE_CONNECTING,
120   SESSION_STATE_ACCEPTING,
121   SESSION_STATE_READY,
122   SESSION_STATE_OPENED,
123   SESSION_STATE_TRANSPORT_CLOSING,
124   SESSION_STATE_CLOSING,
125   SESSION_STATE_CLOSED_WAITING,
126   SESSION_STATE_TRANSPORT_CLOSED,
127   SESSION_STATE_CLOSED,
128   SESSION_STATE_N_STATES,
129 } session_state_t;
130
131 typedef struct session_
132 {
133   /** Pointers to rx/tx buffers. Once allocated, these do not move */
134   svm_fifo_t *rx_fifo;
135   svm_fifo_t *tx_fifo;
136
137   /** Type built from transport and network protocol types */
138   session_type_t session_type;
139
140   /** State in session layer state machine. See @ref session_state_t */
141   volatile u8 session_state;
142
143   /** Index in thread pool where session was allocated */
144   u32 session_index;
145
146   /** Index of the app worker that owns the session */
147   u32 app_wrk_index;
148
149   /** Index of the thread that allocated the session */
150   u8 thread_index;
151
152   /** Tracks last enqueue epoch to avoid generating multiple enqueue events */
153   u64 enqueue_epoch;
154
155   /** Index of the transport connection associated to the session */
156   u32 connection_index;
157
158   /** Index of application that owns the listener. Set only if a listener */
159   u32 app_index;
160
161   union
162   {
163     /** Parent listener session index if the result of an accept */
164     u32 listener_index;
165
166     /** App listener index in app's listener pool if a listener */
167     u32 al_index;
168   };
169
170   /** Opaque, for general use */
171   u32 opaque;
172
173     CLIB_CACHE_LINE_ALIGN_MARK (pad);
174 } session_t;
175
176 always_inline session_type_t
177 session_type_from_proto_and_ip (transport_proto_t proto, u8 is_ip4)
178 {
179   return (proto << 1 | is_ip4);
180 }
181
182 always_inline transport_proto_t
183 session_type_transport_proto (session_type_t st)
184 {
185   return (st >> 1);
186 }
187
188 always_inline u8
189 session_type_is_ip4 (session_type_t st)
190 {
191   return (st & 1);
192 }
193
194 always_inline transport_proto_t
195 session_get_transport_proto (session_t * s)
196 {
197   return (s->session_type >> 1);
198 }
199
200 always_inline fib_protocol_t
201 session_get_fib_proto (session_t * s)
202 {
203   u8 is_ip4 = s->session_type & 1;
204   return (is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6);
205 }
206
207 always_inline u8
208 session_has_transport (session_t * s)
209 {
210   return (session_get_transport_proto (s) != TRANSPORT_PROTO_NONE);
211 }
212
213 static inline transport_service_type_t
214 session_transport_service_type (session_t * s)
215 {
216   transport_proto_t tp;
217   tp = session_get_transport_proto (s);
218   return transport_protocol_service_type (tp);
219 }
220
221 static inline transport_tx_fn_type_t
222 session_transport_tx_fn_type (session_t * s)
223 {
224   transport_proto_t tp;
225   tp = session_get_transport_proto (s);
226   return transport_protocol_tx_fn_type (tp);
227 }
228
229 static inline u8
230 session_tx_is_dgram (session_t * s)
231 {
232   return (session_transport_tx_fn_type (s) == TRANSPORT_TX_DGRAM);
233 }
234
235 always_inline session_handle_t
236 session_handle (session_t * s)
237 {
238   return ((u64) s->thread_index << 32) | (u64) s->session_index;
239 }
240
241 always_inline u32
242 session_index_from_handle (session_handle_t handle)
243 {
244   return handle & 0xFFFFFFFF;
245 }
246
247 always_inline u32
248 session_thread_from_handle (session_handle_t handle)
249 {
250   return handle >> 32;
251 }
252
253 always_inline void
254 session_parse_handle (session_handle_t handle, u32 * index,
255                       u32 * thread_index)
256 {
257   *index = session_index_from_handle (handle);
258   *thread_index = session_thread_from_handle (handle);
259 }
260
261 typedef enum
262 {
263   SESSION_IO_EVT_RX,
264   SESSION_IO_EVT_TX,
265   SESSION_IO_EVT_TX_FLUSH,
266   SESSION_IO_EVT_BUILTIN_RX,
267   SESSION_IO_EVT_BUILTIN_TX,
268   SESSION_CTRL_EVT_RPC,
269   SESSION_CTRL_EVT_CLOSE,
270   SESSION_CTRL_EVT_BOUND,
271   SESSION_CTRL_EVT_UNLISTEN_REPLY,
272   SESSION_CTRL_EVT_ACCEPTED,
273   SESSION_CTRL_EVT_ACCEPTED_REPLY,
274   SESSION_CTRL_EVT_CONNECTED,
275   SESSION_CTRL_EVT_CONNECTED_REPLY,
276   SESSION_CTRL_EVT_DISCONNECTED,
277   SESSION_CTRL_EVT_DISCONNECTED_REPLY,
278   SESSION_CTRL_EVT_RESET,
279   SESSION_CTRL_EVT_RESET_REPLY,
280   SESSION_CTRL_EVT_REQ_WORKER_UPDATE,
281   SESSION_CTRL_EVT_WORKER_UPDATE,
282   SESSION_CTRL_EVT_WORKER_UPDATE_REPLY,
283 } session_evt_type_t;
284
285 /* Deprecated and will be removed. Use types above */
286 #define FIFO_EVENT_APP_RX SESSION_IO_EVT_RX
287 #define FIFO_EVENT_APP_TX SESSION_IO_EVT_TX
288 #define FIFO_EVENT_DISCONNECT SESSION_CTRL_EVT_CLOSE
289 #define FIFO_EVENT_BUILTIN_RX SESSION_IO_EVT_BUILTIN_RX
290 #define FIFO_EVENT_BUILTIN_TX SESSION_IO_EVT_BUILTIN_TX
291
292 typedef enum
293 {
294   SESSION_MQ_IO_EVT_RING,
295   SESSION_MQ_CTRL_EVT_RING,
296   SESSION_MQ_N_RINGS
297 } session_mq_rings_e;
298
299 typedef struct
300 {
301   void *fp;
302   void *arg;
303 } session_rpc_args_t;
304
305 typedef struct
306 {
307   u8 event_type;
308   u8 postponed;
309   union
310   {
311     u32 session_index;
312     session_handle_t session_handle;
313     session_rpc_args_t rpc_args;
314     struct
315     {
316       u8 data[0];
317     };
318   };
319 } __clib_packed session_event_t;
320
321 #define SESSION_MSG_NULL { }
322
323 typedef struct session_dgram_pre_hdr_
324 {
325   u32 data_length;
326   u32 data_offset;
327 } session_dgram_pre_hdr_t;
328
329 typedef struct session_dgram_header_
330 {
331   u32 data_length;
332   u32 data_offset;
333   ip46_address_t rmt_ip;
334   ip46_address_t lcl_ip;
335   u16 rmt_port;
336   u16 lcl_port;
337   u8 is_ip4;
338 } __clib_packed session_dgram_hdr_t;
339
340 #define SESSION_CONN_ID_LEN 37
341 #define SESSION_CONN_HDR_LEN 45
342
343 STATIC_ASSERT (sizeof (session_dgram_hdr_t) == (SESSION_CONN_ID_LEN + 8),
344                "session conn id wrong length");
345 #endif /* SRC_VNET_SESSION_SESSION_TYPES_H_ */
346
347 /*
348  * fd.io coding-style-patch-verification: ON
349  *
350  * Local Variables:
351  * eval: (c-set-style "gnu")
352  * End:
353  */