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