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