VPP-659 Improve tcp/session debugging and testing
[vpp.git] / src / vnet / session / transport.h
1 /*
2  * Copyright (c) 2016 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 VNET_VNET_URI_TRANSPORT_H_
17 #define VNET_VNET_URI_TRANSPORT_H_
18
19 #include <vnet/vnet.h>
20 #include <vnet/ip/ip.h>
21 #include <vppinfra/bihash_16_8.h>
22 #include <vppinfra/bihash_48_8.h>
23 #include <vnet/tcp/tcp_debug.h>
24 /*
25  * Protocol independent transport properties associated to a session
26  */
27 typedef struct _transport_connection
28 {
29   ip46_address_t rmt_ip;        /**< Remote IP */
30   ip46_address_t lcl_ip;        /**< Local IP */
31   u16 lcl_port;                 /**< Local port */
32   u16 rmt_port;                 /**< Remote port */
33   u8 proto;                     /**< Transport protocol id */
34
35   u32 s_index;                  /**< Parent session index */
36   u32 c_index;                  /**< Connection index in transport pool */
37   u8 is_ip4;                    /**< Flag if IP4 connection */
38   u32 thread_index;             /**< Worker-thread index */
39
40 #if TRANSPORT_DEBUG
41   elog_track_t elog_track;      /**< Debug purposes */
42 #endif
43
44   /** Macros for 'derived classes' where base is named "connection" */
45 #define c_lcl_ip connection.lcl_ip
46 #define c_rmt_ip connection.rmt_ip
47 #define c_lcl_ip4 connection.lcl_ip.ip4
48 #define c_rmt_ip4 connection.rmt_ip.ip4
49 #define c_lcl_ip6 connection.lcl_ip.ip6
50 #define c_rmt_ip6 connection.rmt_ip.ip6
51 #define c_lcl_port connection.lcl_port
52 #define c_rmt_port connection.rmt_port
53 #define c_proto connection.proto
54 #define c_state connection.state
55 #define c_s_index connection.s_index
56 #define c_c_index connection.c_index
57 #define c_is_ip4 connection.is_ip4
58 #define c_thread_index connection.thread_index
59 #define c_elog_track connection.elog_track
60 } transport_connection_t;
61
62 /*
63  * Transport protocol virtual function table
64  */
65 typedef struct _transport_proto_vft
66 {
67   /*
68    * Setup
69    */
70   u32 (*bind) (u32, ip46_address_t *, u16);
71   u32 (*unbind) (u32);
72   int (*open) (ip46_address_t * addr, u16 port_host_byte_order);
73   void (*close) (u32 conn_index, u32 thread_index);
74   void (*cleanup) (u32 conn_index, u32 thread_index);
75
76   /*
77    * Transmission
78    */
79     u32 (*push_header) (transport_connection_t * tconn, vlib_buffer_t * b);
80     u16 (*send_mss) (transport_connection_t * tc);
81     u32 (*send_space) (transport_connection_t * tc);
82     u32 (*tx_fifo_offset) (transport_connection_t * tc);
83
84   /*
85    * Connection retrieval
86    */
87   transport_connection_t *(*get_connection) (u32 conn_idx, u32 thread_idx);
88   transport_connection_t *(*get_listener) (u32 conn_index);
89   transport_connection_t *(*get_half_open) (u32 conn_index);
90
91   /*
92    * Format
93    */
94   u8 *(*format_connection) (u8 * s, va_list * args);
95   u8 *(*format_listener) (u8 * s, va_list * args);
96   u8 *(*format_half_open) (u8 * s, va_list * args);
97 } transport_proto_vft_t;
98
99 /* *INDENT-OFF* */
100 /* 16 octets */
101 typedef CLIB_PACKED (struct {
102   union
103     {
104       struct
105         {
106           ip4_address_t src; ip4_address_t dst;
107           u16 src_port;
108           u16 dst_port;
109           /* align by making this 4 octets even though its a 1-bit field
110            * NOTE: avoid key overlap with other transports that use 5 tuples for
111            * session identification.
112            */
113           u32 proto;
114         };
115       u64 as_u64[2];
116     };
117 }) v4_connection_key_t;
118
119 typedef CLIB_PACKED (struct {
120   union
121     {
122       struct
123         {
124           /* 48 octets */
125           ip6_address_t src; ip6_address_t dst;
126           u16 src_port;
127           u16 dst_port; u32 proto; u8 unused_for_now[8];
128         }; u64 as_u64[6];
129     };
130 }) v6_connection_key_t;
131 /* *INDENT-ON* */
132
133 typedef clib_bihash_kv_16_8_t session_kv4_t;
134 typedef clib_bihash_kv_48_8_t session_kv6_t;
135
136 always_inline void
137 make_v4_ss_kv (session_kv4_t * kv, ip4_address_t * lcl, ip4_address_t * rmt,
138                u16 lcl_port, u16 rmt_port, u8 proto)
139 {
140   v4_connection_key_t key;
141   memset (&key, 0, sizeof (v4_connection_key_t));
142
143   key.src.as_u32 = lcl->as_u32;
144   key.dst.as_u32 = rmt->as_u32;
145   key.src_port = lcl_port;
146   key.dst_port = rmt_port;
147   key.proto = proto;
148
149   kv->key[0] = key.as_u64[0];
150   kv->key[1] = key.as_u64[1];
151   kv->value = ~0ULL;
152 }
153
154 always_inline void
155 make_v4_listener_kv (session_kv4_t * kv, ip4_address_t * lcl, u16 lcl_port,
156                      u8 proto)
157 {
158   v4_connection_key_t key;
159   memset (&key, 0, sizeof (v4_connection_key_t));
160
161   key.src.as_u32 = lcl->as_u32;
162   key.dst.as_u32 = 0;
163   key.src_port = lcl_port;
164   key.dst_port = 0;
165   key.proto = proto;
166
167   kv->key[0] = key.as_u64[0];
168   kv->key[1] = key.as_u64[1];
169   kv->value = ~0ULL;
170 }
171
172 always_inline void
173 make_v4_ss_kv_from_tc (session_kv4_t * kv, transport_connection_t * t)
174 {
175   return make_v4_ss_kv (kv, &t->lcl_ip.ip4, &t->rmt_ip.ip4, t->lcl_port,
176                         t->rmt_port, t->proto);
177 }
178
179 always_inline void
180 make_v6_ss_kv (session_kv6_t * kv, ip6_address_t * lcl, ip6_address_t * rmt,
181                u16 lcl_port, u16 rmt_port, u8 proto)
182 {
183   v6_connection_key_t key;
184   memset (&key, 0, sizeof (v6_connection_key_t));
185
186   key.src.as_u64[0] = lcl->as_u64[0];
187   key.src.as_u64[1] = lcl->as_u64[1];
188   key.dst.as_u64[0] = rmt->as_u64[0];
189   key.dst.as_u64[1] = rmt->as_u64[1];
190   key.src_port = lcl_port;
191   key.dst_port = rmt_port;
192   key.proto = proto;
193
194   kv->key[0] = key.as_u64[0];
195   kv->key[1] = key.as_u64[1];
196   kv->key[2] = 0;
197   kv->key[3] = 0;
198   kv->key[4] = 0;
199   kv->key[5] = 0;
200   kv->value = ~0ULL;
201 }
202
203 always_inline void
204 make_v6_listener_kv (session_kv6_t * kv, ip6_address_t * lcl, u16 lcl_port,
205                      u8 proto)
206 {
207   v6_connection_key_t key;
208   memset (&key, 0, sizeof (v6_connection_key_t));
209
210   key.src.as_u64[0] = lcl->as_u64[0];
211   key.src.as_u64[1] = lcl->as_u64[1];
212   key.dst.as_u64[0] = 0;
213   key.dst.as_u64[1] = 0;
214   key.src_port = lcl_port;
215   key.dst_port = 0;
216   key.proto = proto;
217
218   kv->key[0] = key.as_u64[0];
219   kv->key[1] = key.as_u64[1];
220   kv->key[2] = 0;
221   kv->key[3] = 0;
222   kv->key[4] = 0;
223   kv->key[5] = 0;
224   kv->value = ~0ULL;
225 }
226
227 always_inline void
228 make_v6_ss_kv_from_tc (session_kv6_t * kv, transport_connection_t * t)
229 {
230   make_v6_ss_kv (kv, &t->lcl_ip.ip6, &t->rmt_ip.ip6, t->lcl_port,
231                  t->rmt_port, t->proto);
232 }
233
234 typedef struct _transport_endpoint
235 {
236   ip46_address_t ip;
237   u16 port;
238   u8 is_ip4;
239   u32 vrf;
240 } transport_endpoint_t;
241
242 typedef clib_bihash_24_8_t transport_endpoint_table_t;
243
244 #define TRANSPORT_ENDPOINT_INVALID_INDEX ((u32)~0)
245
246 u32
247 transport_endpoint_lookup (transport_endpoint_table_t * ht,
248                            ip46_address_t * ip, u16 port);
249 void transport_endpoint_table_add (transport_endpoint_table_t * ht,
250                                    transport_endpoint_t * te, u32 value);
251 void transport_endpoint_table_del (transport_endpoint_table_t * ht,
252                                    transport_endpoint_t * te);
253
254 #endif /* VNET_VNET_URI_TRANSPORT_H_ */
255
256 /*
257  * fd.io coding-style-patch-verification: ON
258  *
259  * Local Variables:
260  * eval: (c-set-style "gnu")
261  * End:
262  */