Overall tcp performance improvements (VPP-846)
[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 (also session type) */
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;      /**< Event logging */
42   u32 cc_stat_tstamp;           /**< CC stats timestamp */
43 #endif
44
45   /** Macros for 'derived classes' where base is named "connection" */
46 #define c_lcl_ip connection.lcl_ip
47 #define c_rmt_ip connection.rmt_ip
48 #define c_lcl_ip4 connection.lcl_ip.ip4
49 #define c_rmt_ip4 connection.rmt_ip.ip4
50 #define c_lcl_ip6 connection.lcl_ip.ip6
51 #define c_rmt_ip6 connection.rmt_ip.ip6
52 #define c_lcl_port connection.lcl_port
53 #define c_rmt_port connection.rmt_port
54 #define c_proto connection.proto
55 #define c_state connection.state
56 #define c_s_index connection.s_index
57 #define c_c_index connection.c_index
58 #define c_is_ip4 connection.is_ip4
59 #define c_thread_index connection.thread_index
60 #define c_elog_track connection.elog_track
61 #define c_cc_stat_tstamp connection.cc_stat_tstamp
62 } transport_connection_t;
63
64 /*
65  * Transport protocol virtual function table
66  */
67 typedef struct _transport_proto_vft
68 {
69   /*
70    * Setup
71    */
72   u32 (*bind) (u32, ip46_address_t *, u16);
73   u32 (*unbind) (u32);
74   int (*open) (ip46_address_t * addr, u16 port_host_byte_order);
75   void (*close) (u32 conn_index, u32 thread_index);
76   void (*cleanup) (u32 conn_index, u32 thread_index);
77
78   /*
79    * Transmission
80    */
81     u32 (*push_header) (transport_connection_t * tconn, vlib_buffer_t * b);
82     u16 (*send_mss) (transport_connection_t * tc);
83     u32 (*send_space) (transport_connection_t * tc);
84     u32 (*tx_fifo_offset) (transport_connection_t * tc);
85
86   /*
87    * Connection retrieval
88    */
89   transport_connection_t *(*get_connection) (u32 conn_idx, u32 thread_idx);
90   transport_connection_t *(*get_listener) (u32 conn_index);
91   transport_connection_t *(*get_half_open) (u32 conn_index);
92
93   /*
94    * Format
95    */
96   u8 *(*format_connection) (u8 * s, va_list * args);
97   u8 *(*format_listener) (u8 * s, va_list * args);
98   u8 *(*format_half_open) (u8 * s, va_list * args);
99 } transport_proto_vft_t;
100
101 /* *INDENT-OFF* */
102 /* 16 octets */
103 typedef CLIB_PACKED (struct {
104   union
105     {
106       struct
107         {
108           ip4_address_t src;
109           ip4_address_t dst;
110           u16 src_port;
111           u16 dst_port;
112           /* align by making this 4 octets even though its a 1-bit field
113            * NOTE: avoid key overlap with other transports that use 5 tuples for
114            * session identification.
115            */
116           u32 proto;
117         };
118       u64 as_u64[2];
119     };
120 }) v4_connection_key_t;
121
122 typedef CLIB_PACKED (struct {
123   union
124     {
125       struct
126         {
127           /* 48 octets */
128           ip6_address_t src;
129           ip6_address_t dst;
130           u16 src_port;
131           u16 dst_port;
132           u32 proto;
133           u64 unused;
134         };
135       u64 as_u64[6];
136     };
137 }) v6_connection_key_t;
138 /* *INDENT-ON* */
139
140 typedef clib_bihash_kv_16_8_t session_kv4_t;
141 typedef clib_bihash_kv_48_8_t session_kv6_t;
142
143 always_inline void
144 make_v4_ss_kv (session_kv4_t * kv, ip4_address_t * lcl, ip4_address_t * rmt,
145                u16 lcl_port, u16 rmt_port, u8 proto)
146 {
147   v4_connection_key_t *key = (v4_connection_key_t *) kv->key;
148
149   key->src.as_u32 = lcl->as_u32;
150   key->dst.as_u32 = rmt->as_u32;
151   key->src_port = lcl_port;
152   key->dst_port = rmt_port;
153   key->proto = proto;
154
155   kv->value = ~0ULL;
156 }
157
158 always_inline void
159 make_v4_listener_kv (session_kv4_t * kv, ip4_address_t * lcl, u16 lcl_port,
160                      u8 proto)
161 {
162   v4_connection_key_t *key = (v4_connection_key_t *) kv->key;
163
164   key->src.as_u32 = lcl->as_u32;
165   key->dst.as_u32 = 0;
166   key->src_port = lcl_port;
167   key->dst_port = 0;
168   key->proto = proto;
169
170   kv->value = ~0ULL;
171 }
172
173 always_inline void
174 make_v4_ss_kv_from_tc (session_kv4_t * kv, transport_connection_t * t)
175 {
176   return make_v4_ss_kv (kv, &t->lcl_ip.ip4, &t->rmt_ip.ip4, t->lcl_port,
177                         t->rmt_port, t->proto);
178 }
179
180 always_inline void
181 make_v6_ss_kv (session_kv6_t * kv, ip6_address_t * lcl, ip6_address_t * rmt,
182                u16 lcl_port, u16 rmt_port, u8 proto)
183 {
184   v6_connection_key_t *key = (v6_connection_key_t *) kv->key;
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   key->unused = 0;
194
195   kv->value = ~0ULL;
196 }
197
198 always_inline void
199 make_v6_listener_kv (session_kv6_t * kv, ip6_address_t * lcl, u16 lcl_port,
200                      u8 proto)
201 {
202   v6_connection_key_t *key = (v6_connection_key_t *) kv->key;
203
204   key->src.as_u64[0] = lcl->as_u64[0];
205   key->src.as_u64[1] = lcl->as_u64[1];
206   key->dst.as_u64[0] = 0;
207   key->dst.as_u64[1] = 0;
208   key->src_port = lcl_port;
209   key->dst_port = 0;
210   key->proto = proto;
211   key->unused = 0;
212
213   kv->value = ~0ULL;
214 }
215
216 always_inline void
217 make_v6_ss_kv_from_tc (session_kv6_t * kv, transport_connection_t * t)
218 {
219   make_v6_ss_kv (kv, &t->lcl_ip.ip6, &t->rmt_ip.ip6, t->lcl_port,
220                  t->rmt_port, t->proto);
221 }
222
223 typedef struct _transport_endpoint
224 {
225   ip46_address_t ip;    /** ip address */
226   u16 port;             /** port in host order */
227   u8 is_ip4;            /** 1 if ip4 */
228   u32 vrf;              /** fib table the endpoint is associated with */
229 } transport_endpoint_t;
230
231 typedef clib_bihash_24_8_t transport_endpoint_table_t;
232
233 #define TRANSPORT_ENDPOINT_INVALID_INDEX ((u32)~0)
234
235 u32
236 transport_endpoint_lookup (transport_endpoint_table_t * ht,
237                            ip46_address_t * ip, u16 port);
238 void transport_endpoint_table_add (transport_endpoint_table_t * ht,
239                                    transport_endpoint_t * te, u32 value);
240 void transport_endpoint_table_del (transport_endpoint_table_t * ht,
241                                    transport_endpoint_t * te);
242
243 #endif /* VNET_VNET_URI_TRANSPORT_H_ */
244
245 /*
246  * fd.io coding-style-patch-verification: ON
247  *
248  * Local Variables:
249  * eval: (c-set-style "gnu")
250  * End:
251  */