Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1 | /* |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 2 | * Copyright (c) 2017-2019 Cisco and/or its affiliates. |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 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 | |
Florin Coras | 1ee7830 | 2019-02-05 15:51:15 -0800 | [diff] [blame] | 16 | #include <vnet/session/transport.h> |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 17 | #include <vnet/session/session.h> |
| 18 | #include <vnet/fib/fib.h> |
| 19 | |
| 20 | /** |
| 21 | * Per-type vector of transport protocol virtual function tables |
| 22 | */ |
| 23 | transport_proto_vft_t *tp_vfts; |
| 24 | |
| 25 | /* |
| 26 | * Port allocator seed |
| 27 | */ |
| 28 | static u32 port_allocator_seed; |
| 29 | |
| 30 | /* |
| 31 | * Local endpoints table |
| 32 | */ |
| 33 | static transport_endpoint_table_t local_endpoints_table; |
| 34 | |
| 35 | /* |
| 36 | * Pool of local endpoints |
| 37 | */ |
| 38 | static transport_endpoint_t *local_endpoints; |
| 39 | |
| 40 | /* |
| 41 | * Local endpoints pool lock |
| 42 | */ |
| 43 | static clib_spinlock_t local_endpoints_lock; |
| 44 | |
Florin Coras | d67f112 | 2018-05-21 17:47:40 -0700 | [diff] [blame] | 45 | /* |
| 46 | * Period used by transport pacers. Initialized by session layer |
| 47 | */ |
| 48 | static double transport_pacer_period; |
| 49 | |
Florin Coras | 7ac053b | 2018-11-05 15:57:21 -0800 | [diff] [blame] | 50 | #define TRANSPORT_PACER_MIN_MSS 1460 |
| 51 | #define TRANSPORT_PACER_MIN_BURST TRANSPORT_PACER_MIN_MSS |
Simon Zhang | 1146ff4 | 2019-09-02 22:54:00 +0800 | [diff] [blame^] | 52 | #define TRANSPORT_PACER_MAX_BURST (43 * TRANSPORT_PACER_MIN_MSS) |
Florin Coras | d67f112 | 2018-05-21 17:47:40 -0700 | [diff] [blame] | 53 | |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 54 | u8 * |
| 55 | format_transport_proto (u8 * s, va_list * args) |
| 56 | { |
| 57 | u32 transport_proto = va_arg (*args, u32); |
| 58 | switch (transport_proto) |
| 59 | { |
Florin Coras | 5bb23ec | 2019-08-31 09:45:13 -0700 | [diff] [blame] | 60 | #define _(sym, str, sstr) \ |
| 61 | case TRANSPORT_PROTO_ ## sym: \ |
| 62 | s = format (s, str); \ |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 63 | break; |
Florin Coras | 5bb23ec | 2019-08-31 09:45:13 -0700 | [diff] [blame] | 64 | foreach_transport_proto |
| 65 | #undef _ |
Nathan Skrzypczak | ba65ca4 | 2019-05-16 16:35:40 +0200 | [diff] [blame] | 66 | default: |
| 67 | s = format (s, "UNKNOWN"); |
| 68 | break; |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 69 | } |
| 70 | return s; |
| 71 | } |
| 72 | |
Florin Coras | 561af9b | 2017-12-09 10:19:43 -0800 | [diff] [blame] | 73 | u8 * |
| 74 | format_transport_proto_short (u8 * s, va_list * args) |
| 75 | { |
| 76 | u32 transport_proto = va_arg (*args, u32); |
| 77 | switch (transport_proto) |
| 78 | { |
Florin Coras | 5bb23ec | 2019-08-31 09:45:13 -0700 | [diff] [blame] | 79 | #define _(sym, str, sstr) \ |
| 80 | case TRANSPORT_PROTO_ ## sym: \ |
| 81 | s = format (s, sstr); \ |
Florin Coras | 561af9b | 2017-12-09 10:19:43 -0800 | [diff] [blame] | 82 | break; |
Florin Coras | 5bb23ec | 2019-08-31 09:45:13 -0700 | [diff] [blame] | 83 | foreach_transport_proto |
| 84 | #undef _ |
Nathan Skrzypczak | ba65ca4 | 2019-05-16 16:35:40 +0200 | [diff] [blame] | 85 | default: |
| 86 | s = format (s, "?"); |
| 87 | break; |
Florin Coras | 561af9b | 2017-12-09 10:19:43 -0800 | [diff] [blame] | 88 | } |
| 89 | return s; |
| 90 | } |
| 91 | |
Florin Coras | de9a849 | 2018-10-24 22:18:58 -0700 | [diff] [blame] | 92 | u8 * |
| 93 | format_transport_connection (u8 * s, va_list * args) |
| 94 | { |
| 95 | u32 transport_proto = va_arg (*args, u32); |
| 96 | u32 conn_index = va_arg (*args, u32); |
| 97 | u32 thread_index = va_arg (*args, u32); |
| 98 | u32 verbose = va_arg (*args, u32); |
| 99 | transport_proto_vft_t *tp_vft; |
| 100 | transport_connection_t *tc; |
| 101 | u32 indent; |
| 102 | |
| 103 | tp_vft = transport_protocol_get_vft (transport_proto); |
| 104 | if (!tp_vft) |
| 105 | return s; |
| 106 | |
| 107 | s = format (s, "%U", tp_vft->format_connection, conn_index, thread_index, |
| 108 | verbose); |
| 109 | tc = tp_vft->get_connection (conn_index, thread_index); |
| 110 | if (tc && transport_connection_is_tx_paced (tc) && verbose > 1) |
| 111 | { |
| 112 | indent = format_get_indent (s) + 1; |
| 113 | s = format (s, "%Upacer: %U\n", format_white_space, indent, |
| 114 | format_transport_pacer, &tc->pacer); |
| 115 | } |
| 116 | return s; |
| 117 | } |
| 118 | |
| 119 | u8 * |
| 120 | format_transport_listen_connection (u8 * s, va_list * args) |
| 121 | { |
| 122 | u32 transport_proto = va_arg (*args, u32); |
Florin Coras | de9a849 | 2018-10-24 22:18:58 -0700 | [diff] [blame] | 123 | transport_proto_vft_t *tp_vft; |
| 124 | |
| 125 | tp_vft = transport_protocol_get_vft (transport_proto); |
| 126 | if (!tp_vft) |
| 127 | return s; |
| 128 | |
Florin Coras | 3389dd2 | 2019-02-01 18:00:05 -0800 | [diff] [blame] | 129 | s = (tp_vft->format_listener) (s, args); |
Florin Coras | de9a849 | 2018-10-24 22:18:58 -0700 | [diff] [blame] | 130 | return s; |
| 131 | } |
| 132 | |
| 133 | u8 * |
| 134 | format_transport_half_open_connection (u8 * s, va_list * args) |
| 135 | { |
| 136 | u32 transport_proto = va_arg (*args, u32); |
| 137 | u32 listen_index = va_arg (*args, u32); |
| 138 | transport_proto_vft_t *tp_vft; |
| 139 | |
| 140 | tp_vft = transport_protocol_get_vft (transport_proto); |
| 141 | if (!tp_vft) |
| 142 | return s; |
| 143 | |
| 144 | s = format (s, "%U", tp_vft->format_half_open, listen_index); |
| 145 | return s; |
| 146 | } |
| 147 | |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 148 | uword |
| 149 | unformat_transport_proto (unformat_input_t * input, va_list * args) |
| 150 | { |
| 151 | u32 *proto = va_arg (*args, u32 *); |
Florin Coras | 5bb23ec | 2019-08-31 09:45:13 -0700 | [diff] [blame] | 152 | |
| 153 | #define _(sym, str, sstr) \ |
| 154 | if (unformat (input, str)) \ |
| 155 | { \ |
| 156 | *proto = TRANSPORT_PROTO_ ## sym; \ |
| 157 | return 1; \ |
| 158 | } |
| 159 | foreach_transport_proto |
| 160 | #undef _ |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 161 | return 0; |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 162 | } |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 163 | |
| 164 | u32 |
| 165 | transport_endpoint_lookup (transport_endpoint_table_t * ht, u8 proto, |
| 166 | ip46_address_t * ip, u16 port) |
| 167 | { |
| 168 | clib_bihash_kv_24_8_t kv; |
| 169 | int rv; |
| 170 | |
| 171 | kv.key[0] = ip->as_u64[0]; |
| 172 | kv.key[1] = ip->as_u64[1]; |
| 173 | kv.key[2] = (u64) port << 8 | (u64) proto; |
| 174 | |
| 175 | rv = clib_bihash_search_inline_24_8 (ht, &kv); |
| 176 | if (rv == 0) |
| 177 | return kv.value; |
| 178 | |
| 179 | return ENDPOINT_INVALID_INDEX; |
| 180 | } |
| 181 | |
| 182 | void |
| 183 | transport_endpoint_table_add (transport_endpoint_table_t * ht, u8 proto, |
| 184 | transport_endpoint_t * te, u32 value) |
| 185 | { |
| 186 | clib_bihash_kv_24_8_t kv; |
| 187 | |
| 188 | kv.key[0] = te->ip.as_u64[0]; |
| 189 | kv.key[1] = te->ip.as_u64[1]; |
| 190 | kv.key[2] = (u64) te->port << 8 | (u64) proto; |
| 191 | kv.value = value; |
| 192 | |
| 193 | clib_bihash_add_del_24_8 (ht, &kv, 1); |
| 194 | } |
| 195 | |
| 196 | void |
| 197 | transport_endpoint_table_del (transport_endpoint_table_t * ht, u8 proto, |
| 198 | transport_endpoint_t * te) |
| 199 | { |
| 200 | clib_bihash_kv_24_8_t kv; |
| 201 | |
| 202 | kv.key[0] = te->ip.as_u64[0]; |
| 203 | kv.key[1] = te->ip.as_u64[1]; |
| 204 | kv.key[2] = (u64) te->port << 8 | (u64) proto; |
| 205 | |
| 206 | clib_bihash_add_del_24_8 (ht, &kv, 0); |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Register transport virtual function table. |
| 211 | * |
Florin Coras | 561af9b | 2017-12-09 10:19:43 -0800 | [diff] [blame] | 212 | * @param transport_proto - transport protocol type (i.e., TCP, UDP ..) |
| 213 | * @param vft - virtual function table for transport proto |
| 214 | * @param fib_proto - network layer protocol |
| 215 | * @param output_node - output node index that session layer will hand off |
| 216 | * buffers to, for requested fib proto |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 217 | */ |
| 218 | void |
Florin Coras | 561af9b | 2017-12-09 10:19:43 -0800 | [diff] [blame] | 219 | transport_register_protocol (transport_proto_t transport_proto, |
| 220 | const transport_proto_vft_t * vft, |
| 221 | fib_protocol_t fib_proto, u32 output_node) |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 222 | { |
Florin Coras | 561af9b | 2017-12-09 10:19:43 -0800 | [diff] [blame] | 223 | u8 is_ip4 = fib_proto == FIB_PROTOCOL_IP4; |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 224 | |
Florin Coras | 561af9b | 2017-12-09 10:19:43 -0800 | [diff] [blame] | 225 | vec_validate (tp_vfts, transport_proto); |
| 226 | tp_vfts[transport_proto] = *vft; |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 227 | |
Florin Coras | 561af9b | 2017-12-09 10:19:43 -0800 | [diff] [blame] | 228 | session_register_transport (transport_proto, vft, is_ip4, output_node); |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | /** |
| 232 | * Get transport virtual function table |
| 233 | * |
| 234 | * @param type - session type (not protocol type) |
| 235 | */ |
| 236 | transport_proto_vft_t * |
Florin Coras | 561af9b | 2017-12-09 10:19:43 -0800 | [diff] [blame] | 237 | transport_protocol_get_vft (transport_proto_t transport_proto) |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 238 | { |
Florin Coras | 561af9b | 2017-12-09 10:19:43 -0800 | [diff] [blame] | 239 | if (transport_proto >= vec_len (tp_vfts)) |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 240 | return 0; |
Florin Coras | 561af9b | 2017-12-09 10:19:43 -0800 | [diff] [blame] | 241 | return &tp_vfts[transport_proto]; |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 242 | } |
| 243 | |
Nathan Skrzypczak | a26349d | 2019-06-26 13:53:08 +0200 | [diff] [blame] | 244 | u8 |
| 245 | transport_half_open_has_fifos (transport_proto_t tp) |
| 246 | { |
| 247 | return tp_vfts[tp].transport_options.half_open_has_fifos; |
| 248 | } |
| 249 | |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 250 | transport_service_type_t |
| 251 | transport_protocol_service_type (transport_proto_t tp) |
| 252 | { |
Nathan Skrzypczak | e971bc9 | 2019-06-19 13:42:37 +0200 | [diff] [blame] | 253 | return tp_vfts[tp].transport_options.service_type; |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 254 | } |
| 255 | |
Florin Coras | f08f26d | 2018-05-10 13:20:47 -0700 | [diff] [blame] | 256 | transport_tx_fn_type_t |
| 257 | transport_protocol_tx_fn_type (transport_proto_t tp) |
| 258 | { |
Nathan Skrzypczak | e971bc9 | 2019-06-19 13:42:37 +0200 | [diff] [blame] | 259 | return tp_vfts[tp].transport_options.tx_type; |
Florin Coras | f08f26d | 2018-05-10 13:20:47 -0700 | [diff] [blame] | 260 | } |
| 261 | |
Florin Coras | 1ee7830 | 2019-02-05 15:51:15 -0800 | [diff] [blame] | 262 | void |
| 263 | transport_cleanup (transport_proto_t tp, u32 conn_index, u8 thread_index) |
Florin Coras | 4edc37e | 2019-02-04 23:01:34 -0800 | [diff] [blame] | 264 | { |
Florin Coras | 1ee7830 | 2019-02-05 15:51:15 -0800 | [diff] [blame] | 265 | tp_vfts[tp].cleanup (conn_index, thread_index); |
Florin Coras | 4edc37e | 2019-02-04 23:01:34 -0800 | [diff] [blame] | 266 | } |
| 267 | |
Florin Coras | 1ee7830 | 2019-02-05 15:51:15 -0800 | [diff] [blame] | 268 | int |
| 269 | transport_connect (transport_proto_t tp, transport_endpoint_cfg_t * tep) |
Florin Coras | 4edc37e | 2019-02-04 23:01:34 -0800 | [diff] [blame] | 270 | { |
Florin Coras | 1ee7830 | 2019-02-05 15:51:15 -0800 | [diff] [blame] | 271 | return tp_vfts[tp].connect (tep); |
| 272 | } |
| 273 | |
| 274 | void |
| 275 | transport_close (transport_proto_t tp, u32 conn_index, u8 thread_index) |
| 276 | { |
| 277 | tp_vfts[tp].close (conn_index, thread_index); |
| 278 | } |
| 279 | |
Florin Coras | dfb3b87 | 2019-08-16 17:48:44 -0700 | [diff] [blame] | 280 | void |
| 281 | transport_reset (transport_proto_t tp, u32 conn_index, u8 thread_index) |
| 282 | { |
| 283 | if (tp_vfts[tp].reset) |
| 284 | tp_vfts[tp].reset (conn_index, thread_index); |
| 285 | else |
| 286 | tp_vfts[tp].close (conn_index, thread_index); |
| 287 | } |
| 288 | |
Florin Coras | 1ee7830 | 2019-02-05 15:51:15 -0800 | [diff] [blame] | 289 | u32 |
| 290 | transport_start_listen (transport_proto_t tp, u32 session_index, |
| 291 | transport_endpoint_t * tep) |
| 292 | { |
| 293 | return tp_vfts[tp].start_listen (session_index, tep); |
| 294 | } |
| 295 | |
| 296 | u32 |
| 297 | transport_stop_listen (transport_proto_t tp, u32 conn_index) |
| 298 | { |
| 299 | return tp_vfts[tp].stop_listen (conn_index); |
Florin Coras | 4edc37e | 2019-02-04 23:01:34 -0800 | [diff] [blame] | 300 | } |
| 301 | |
Florin Coras | 40903ac | 2018-06-10 14:41:23 -0700 | [diff] [blame] | 302 | u8 |
| 303 | transport_protocol_is_cl (transport_proto_t tp) |
| 304 | { |
Nathan Skrzypczak | e971bc9 | 2019-06-19 13:42:37 +0200 | [diff] [blame] | 305 | return (tp_vfts[tp].transport_options.service_type == TRANSPORT_SERVICE_CL); |
Florin Coras | 40903ac | 2018-06-10 14:41:23 -0700 | [diff] [blame] | 306 | } |
| 307 | |
Aloys Augustin | cdb7170 | 2019-04-08 17:54:39 +0200 | [diff] [blame] | 308 | always_inline void |
| 309 | default_get_transport_endpoint (transport_connection_t * tc, |
Florin Coras | 09d18c2 | 2019-04-24 11:10:02 -0700 | [diff] [blame] | 310 | transport_endpoint_t * tep, u8 is_lcl) |
Aloys Augustin | cdb7170 | 2019-04-08 17:54:39 +0200 | [diff] [blame] | 311 | { |
| 312 | if (is_lcl) |
| 313 | { |
Florin Coras | 09d18c2 | 2019-04-24 11:10:02 -0700 | [diff] [blame] | 314 | tep->port = tc->lcl_port; |
| 315 | tep->is_ip4 = tc->is_ip4; |
| 316 | clib_memcpy_fast (&tep->ip, &tc->lcl_ip, sizeof (tc->lcl_ip)); |
Aloys Augustin | cdb7170 | 2019-04-08 17:54:39 +0200 | [diff] [blame] | 317 | } |
| 318 | else |
| 319 | { |
Florin Coras | 09d18c2 | 2019-04-24 11:10:02 -0700 | [diff] [blame] | 320 | tep->port = tc->rmt_port; |
| 321 | tep->is_ip4 = tc->is_ip4; |
| 322 | clib_memcpy_fast (&tep->ip, &tc->rmt_ip, sizeof (tc->rmt_ip)); |
Aloys Augustin | cdb7170 | 2019-04-08 17:54:39 +0200 | [diff] [blame] | 323 | } |
| 324 | } |
| 325 | |
| 326 | void |
| 327 | transport_get_endpoint (transport_proto_t tp, u32 conn_index, |
Florin Coras | 09d18c2 | 2019-04-24 11:10:02 -0700 | [diff] [blame] | 328 | u32 thread_index, transport_endpoint_t * tep, |
| 329 | u8 is_lcl) |
Aloys Augustin | cdb7170 | 2019-04-08 17:54:39 +0200 | [diff] [blame] | 330 | { |
| 331 | if (tp_vfts[tp].get_transport_endpoint) |
Florin Coras | 09d18c2 | 2019-04-24 11:10:02 -0700 | [diff] [blame] | 332 | tp_vfts[tp].get_transport_endpoint (conn_index, thread_index, tep, |
| 333 | is_lcl); |
Aloys Augustin | cdb7170 | 2019-04-08 17:54:39 +0200 | [diff] [blame] | 334 | else |
| 335 | { |
| 336 | transport_connection_t *tc; |
| 337 | tc = transport_get_connection (tp, conn_index, thread_index); |
Florin Coras | 09d18c2 | 2019-04-24 11:10:02 -0700 | [diff] [blame] | 338 | default_get_transport_endpoint (tc, tep, is_lcl); |
Aloys Augustin | cdb7170 | 2019-04-08 17:54:39 +0200 | [diff] [blame] | 339 | } |
| 340 | } |
| 341 | |
| 342 | void |
| 343 | transport_get_listener_endpoint (transport_proto_t tp, u32 conn_index, |
Florin Coras | 09d18c2 | 2019-04-24 11:10:02 -0700 | [diff] [blame] | 344 | transport_endpoint_t * tep, u8 is_lcl) |
Aloys Augustin | cdb7170 | 2019-04-08 17:54:39 +0200 | [diff] [blame] | 345 | { |
| 346 | if (tp_vfts[tp].get_transport_listener_endpoint) |
Florin Coras | 09d18c2 | 2019-04-24 11:10:02 -0700 | [diff] [blame] | 347 | tp_vfts[tp].get_transport_listener_endpoint (conn_index, tep, is_lcl); |
Aloys Augustin | cdb7170 | 2019-04-08 17:54:39 +0200 | [diff] [blame] | 348 | else |
| 349 | { |
| 350 | transport_connection_t *tc; |
| 351 | tc = transport_get_listener (tp, conn_index); |
Florin Coras | 09d18c2 | 2019-04-24 11:10:02 -0700 | [diff] [blame] | 352 | default_get_transport_endpoint (tc, tep, is_lcl); |
Aloys Augustin | cdb7170 | 2019-04-08 17:54:39 +0200 | [diff] [blame] | 353 | } |
| 354 | } |
| 355 | |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 356 | #define PORT_MASK ((1 << 16)- 1) |
| 357 | |
| 358 | void |
| 359 | transport_endpoint_del (u32 tepi) |
| 360 | { |
| 361 | clib_spinlock_lock_if_init (&local_endpoints_lock); |
| 362 | pool_put_index (local_endpoints, tepi); |
| 363 | clib_spinlock_unlock_if_init (&local_endpoints_lock); |
| 364 | } |
| 365 | |
| 366 | always_inline transport_endpoint_t * |
| 367 | transport_endpoint_new (void) |
| 368 | { |
| 369 | transport_endpoint_t *tep; |
Florin Coras | 5665ced | 2018-10-25 18:03:45 -0700 | [diff] [blame] | 370 | pool_get_zero (local_endpoints, tep); |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 371 | return tep; |
| 372 | } |
| 373 | |
| 374 | void |
| 375 | transport_endpoint_cleanup (u8 proto, ip46_address_t * lcl_ip, u16 port) |
| 376 | { |
| 377 | u32 tepi; |
| 378 | transport_endpoint_t *tep; |
| 379 | |
| 380 | /* Cleanup local endpoint if this was an active connect */ |
| 381 | tepi = transport_endpoint_lookup (&local_endpoints_table, proto, lcl_ip, |
| 382 | clib_net_to_host_u16 (port)); |
| 383 | if (tepi != ENDPOINT_INVALID_INDEX) |
| 384 | { |
| 385 | tep = pool_elt_at_index (local_endpoints, tepi); |
| 386 | transport_endpoint_table_del (&local_endpoints_table, proto, tep); |
| 387 | transport_endpoint_del (tepi); |
| 388 | } |
| 389 | } |
| 390 | |
Florin Coras | 5665ced | 2018-10-25 18:03:45 -0700 | [diff] [blame] | 391 | static void |
| 392 | transport_endpoint_mark_used (u8 proto, ip46_address_t * ip, u16 port) |
| 393 | { |
| 394 | transport_endpoint_t *tep; |
| 395 | clib_spinlock_lock_if_init (&local_endpoints_lock); |
| 396 | tep = transport_endpoint_new (); |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 397 | clib_memcpy_fast (&tep->ip, ip, sizeof (*ip)); |
Florin Coras | 5665ced | 2018-10-25 18:03:45 -0700 | [diff] [blame] | 398 | tep->port = port; |
| 399 | transport_endpoint_table_add (&local_endpoints_table, proto, tep, |
| 400 | tep - local_endpoints); |
| 401 | clib_spinlock_unlock_if_init (&local_endpoints_lock); |
| 402 | } |
| 403 | |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 404 | /** |
| 405 | * Allocate local port and add if successful add entry to local endpoint |
| 406 | * table to mark the pair as used. |
| 407 | */ |
| 408 | int |
| 409 | transport_alloc_local_port (u8 proto, ip46_address_t * ip) |
| 410 | { |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 411 | u16 min = 1024, max = 65535; /* XXX configurable ? */ |
| 412 | int tries, limit; |
Florin Coras | 5665ced | 2018-10-25 18:03:45 -0700 | [diff] [blame] | 413 | u32 tei; |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 414 | |
| 415 | limit = max - min; |
| 416 | |
| 417 | /* Only support active opens from thread 0 */ |
| 418 | ASSERT (vlib_get_thread_index () == 0); |
| 419 | |
| 420 | /* Search for first free slot */ |
| 421 | for (tries = 0; tries < limit; tries++) |
| 422 | { |
| 423 | u16 port = 0; |
| 424 | |
| 425 | /* Find a port in the specified range */ |
| 426 | while (1) |
| 427 | { |
| 428 | port = random_u32 (&port_allocator_seed) & PORT_MASK; |
| 429 | if (PREDICT_TRUE (port >= min && port < max)) |
| 430 | break; |
| 431 | } |
| 432 | |
| 433 | /* Look it up. If not found, we're done */ |
| 434 | tei = transport_endpoint_lookup (&local_endpoints_table, proto, ip, |
| 435 | port); |
| 436 | if (tei == ENDPOINT_INVALID_INDEX) |
| 437 | { |
Florin Coras | 5665ced | 2018-10-25 18:03:45 -0700 | [diff] [blame] | 438 | transport_endpoint_mark_used (proto, ip, port); |
| 439 | return port; |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 440 | } |
| 441 | } |
| 442 | return -1; |
| 443 | } |
| 444 | |
Florin Coras | 5665ced | 2018-10-25 18:03:45 -0700 | [diff] [blame] | 445 | static clib_error_t * |
| 446 | transport_get_interface_ip (u32 sw_if_index, u8 is_ip4, ip46_address_t * addr) |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 447 | { |
Florin Coras | 5665ced | 2018-10-25 18:03:45 -0700 | [diff] [blame] | 448 | if (is_ip4) |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 449 | { |
| 450 | ip4_address_t *ip4; |
| 451 | ip4 = ip_interface_get_first_ip (sw_if_index, 1); |
Florin Coras | fc804d9 | 2018-01-26 01:27:01 -0800 | [diff] [blame] | 452 | if (!ip4) |
Florin Coras | 5665ced | 2018-10-25 18:03:45 -0700 | [diff] [blame] | 453 | return clib_error_return (0, "no routable ip4 address on %U", |
| 454 | format_vnet_sw_if_index_name, |
| 455 | vnet_get_main (), sw_if_index); |
| 456 | addr->ip4.as_u32 = ip4->as_u32; |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 457 | } |
| 458 | else |
| 459 | { |
| 460 | ip6_address_t *ip6; |
| 461 | ip6 = ip_interface_get_first_ip (sw_if_index, 0); |
| 462 | if (ip6 == 0) |
Florin Coras | 5665ced | 2018-10-25 18:03:45 -0700 | [diff] [blame] | 463 | return clib_error_return (0, "no routable ip6 addresses on %U", |
| 464 | format_vnet_sw_if_index_name, |
| 465 | vnet_get_main (), sw_if_index); |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 466 | clib_memcpy_fast (&addr->ip6, ip6, sizeof (*ip6)); |
Florin Coras | 5665ced | 2018-10-25 18:03:45 -0700 | [diff] [blame] | 467 | } |
| 468 | return 0; |
| 469 | } |
| 470 | |
| 471 | static clib_error_t * |
| 472 | transport_find_local_ip_for_remote (u32 sw_if_index, |
| 473 | transport_endpoint_t * rmt, |
| 474 | ip46_address_t * lcl_addr) |
| 475 | { |
| 476 | fib_node_index_t fei; |
| 477 | fib_prefix_t prefix; |
| 478 | |
| 479 | if (sw_if_index == ENDPOINT_INVALID_INDEX) |
| 480 | { |
| 481 | /* Find a FIB path to the destination */ |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 482 | clib_memcpy_fast (&prefix.fp_addr, &rmt->ip, sizeof (rmt->ip)); |
Florin Coras | 5665ced | 2018-10-25 18:03:45 -0700 | [diff] [blame] | 483 | prefix.fp_proto = rmt->is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6; |
| 484 | prefix.fp_len = rmt->is_ip4 ? 32 : 128; |
| 485 | |
| 486 | ASSERT (rmt->fib_index != ENDPOINT_INVALID_INDEX); |
| 487 | fei = fib_table_lookup (rmt->fib_index, &prefix); |
| 488 | |
| 489 | /* Couldn't find route to destination. Bail out. */ |
| 490 | if (fei == FIB_NODE_INDEX_INVALID) |
| 491 | return clib_error_return (0, "no route to %U", format_ip46_address, |
| 492 | &rmt->ip, (rmt->is_ip4 == 0) + 1); |
| 493 | |
| 494 | sw_if_index = fib_entry_get_resolving_interface (fei); |
| 495 | if (sw_if_index == ENDPOINT_INVALID_INDEX) |
| 496 | return clib_error_return (0, "no resolving interface for %U", |
| 497 | format_ip46_address, &rmt->ip, |
| 498 | (rmt->is_ip4 == 0) + 1); |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 499 | } |
| 500 | |
Florin Coras | 5665ced | 2018-10-25 18:03:45 -0700 | [diff] [blame] | 501 | clib_memset (lcl_addr, 0, sizeof (*lcl_addr)); |
| 502 | return transport_get_interface_ip (sw_if_index, rmt->is_ip4, lcl_addr); |
| 503 | } |
| 504 | |
| 505 | int |
| 506 | transport_alloc_local_endpoint (u8 proto, transport_endpoint_cfg_t * rmt_cfg, |
| 507 | ip46_address_t * lcl_addr, u16 * lcl_port) |
| 508 | { |
| 509 | transport_endpoint_t *rmt = (transport_endpoint_t *) rmt_cfg; |
| 510 | clib_error_t *error; |
| 511 | int port; |
| 512 | u32 tei; |
| 513 | |
| 514 | /* |
| 515 | * Find the local address |
| 516 | */ |
| 517 | if (ip_is_zero (&rmt_cfg->peer.ip, rmt_cfg->peer.is_ip4)) |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 518 | { |
Florin Coras | 5665ced | 2018-10-25 18:03:45 -0700 | [diff] [blame] | 519 | error = transport_find_local_ip_for_remote (rmt_cfg->peer.sw_if_index, |
| 520 | rmt, lcl_addr); |
| 521 | if (error) |
Florin Coras | dc2e251 | 2018-12-03 17:47:26 -0800 | [diff] [blame] | 522 | { |
| 523 | clib_error_report (error); |
| 524 | return -1; |
| 525 | } |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 526 | } |
Florin Coras | 5665ced | 2018-10-25 18:03:45 -0700 | [diff] [blame] | 527 | else |
| 528 | { |
| 529 | /* Assume session layer vetted this address */ |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 530 | clib_memcpy_fast (lcl_addr, &rmt_cfg->peer.ip, |
| 531 | sizeof (rmt_cfg->peer.ip)); |
Florin Coras | 5665ced | 2018-10-25 18:03:45 -0700 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | /* |
| 535 | * Allocate source port |
| 536 | */ |
| 537 | if (rmt_cfg->peer.port == 0) |
| 538 | { |
| 539 | port = transport_alloc_local_port (proto, lcl_addr); |
| 540 | if (port < 1) |
| 541 | { |
| 542 | clib_warning ("Failed to allocate src port"); |
| 543 | return -1; |
| 544 | } |
| 545 | *lcl_port = port; |
| 546 | } |
| 547 | else |
| 548 | { |
| 549 | port = clib_net_to_host_u16 (rmt_cfg->peer.port); |
| 550 | tei = transport_endpoint_lookup (&local_endpoints_table, proto, |
| 551 | lcl_addr, port); |
| 552 | if (tei != ENDPOINT_INVALID_INDEX) |
| 553 | return -1; |
| 554 | |
| 555 | transport_endpoint_mark_used (proto, lcl_addr, port); |
| 556 | *lcl_port = port; |
| 557 | } |
| 558 | |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 559 | return 0; |
| 560 | } |
| 561 | |
Florin Coras | d67f112 | 2018-05-21 17:47:40 -0700 | [diff] [blame] | 562 | #define SPACER_CPU_TICKS_PER_PERIOD_SHIFT 10 |
| 563 | #define SPACER_CPU_TICKS_PER_PERIOD (1 << SPACER_CPU_TICKS_PER_PERIOD_SHIFT) |
| 564 | |
| 565 | u8 * |
| 566 | format_transport_pacer (u8 * s, va_list * args) |
| 567 | { |
| 568 | spacer_t *pacer = va_arg (*args, spacer_t *); |
| 569 | |
Florin Coras | 0048223 | 2019-08-02 12:52:00 -0700 | [diff] [blame] | 570 | s = format (s, "bucket %u tokens/period %.3f last_update %x", |
| 571 | pacer->bucket, pacer->tokens_per_period, pacer->last_update); |
Florin Coras | d67f112 | 2018-05-21 17:47:40 -0700 | [diff] [blame] | 572 | return s; |
| 573 | } |
| 574 | |
| 575 | static inline u32 |
| 576 | spacer_max_burst (spacer_t * pacer, u64 norm_time_now) |
| 577 | { |
| 578 | u64 n_periods = norm_time_now - pacer->last_update; |
Florin Coras | e55a6d7 | 2018-10-31 23:09:22 -0700 | [diff] [blame] | 579 | u64 inc; |
Florin Coras | d67f112 | 2018-05-21 17:47:40 -0700 | [diff] [blame] | 580 | |
Florin Coras | e55a6d7 | 2018-10-31 23:09:22 -0700 | [diff] [blame] | 581 | if (n_periods > 0 && (inc = n_periods * pacer->tokens_per_period) > 10) |
| 582 | { |
| 583 | pacer->last_update = norm_time_now; |
| 584 | pacer->bucket += inc; |
| 585 | } |
| 586 | |
Florin Coras | 6bc6fd0 | 2019-03-27 18:55:11 -0700 | [diff] [blame] | 587 | return clib_min (pacer->bucket, TRANSPORT_PACER_MAX_BURST); |
Florin Coras | d67f112 | 2018-05-21 17:47:40 -0700 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | static inline void |
| 591 | spacer_update_bucket (spacer_t * pacer, u32 bytes) |
| 592 | { |
| 593 | ASSERT (pacer->bucket >= bytes); |
| 594 | pacer->bucket -= bytes; |
| 595 | } |
| 596 | |
| 597 | static inline void |
Florin Coras | d67f112 | 2018-05-21 17:47:40 -0700 | [diff] [blame] | 598 | spacer_set_pace_rate (spacer_t * pacer, u64 rate_bytes_per_sec) |
| 599 | { |
| 600 | ASSERT (rate_bytes_per_sec != 0); |
| 601 | pacer->tokens_per_period = rate_bytes_per_sec / transport_pacer_period; |
| 602 | } |
| 603 | |
Florin Coras | 5281473 | 2019-06-12 15:38:19 -0700 | [diff] [blame] | 604 | static inline u64 |
| 605 | spacer_pace_rate (spacer_t * pacer) |
| 606 | { |
| 607 | return pacer->tokens_per_period * transport_pacer_period; |
| 608 | } |
| 609 | |
Florin Coras | d67f112 | 2018-05-21 17:47:40 -0700 | [diff] [blame] | 610 | void |
Florin Coras | c44a558 | 2018-11-01 16:30:54 -0700 | [diff] [blame] | 611 | transport_connection_tx_pacer_reset (transport_connection_t * tc, |
| 612 | u32 rate_bytes_per_sec, |
| 613 | u32 start_bucket, u64 time_now) |
Florin Coras | d67f112 | 2018-05-21 17:47:40 -0700 | [diff] [blame] | 614 | { |
Florin Coras | d67f112 | 2018-05-21 17:47:40 -0700 | [diff] [blame] | 615 | spacer_t *pacer = &tc->pacer; |
Florin Coras | d67f112 | 2018-05-21 17:47:40 -0700 | [diff] [blame] | 616 | spacer_set_pace_rate (&tc->pacer, rate_bytes_per_sec); |
| 617 | pacer->last_update = time_now >> SPACER_CPU_TICKS_PER_PERIOD_SHIFT; |
Florin Coras | c44a558 | 2018-11-01 16:30:54 -0700 | [diff] [blame] | 618 | pacer->bucket = start_bucket; |
| 619 | } |
| 620 | |
| 621 | void |
| 622 | transport_connection_tx_pacer_init (transport_connection_t * tc, |
| 623 | u32 rate_bytes_per_sec, |
| 624 | u32 initial_bucket) |
| 625 | { |
| 626 | vlib_main_t *vm = vlib_get_main (); |
| 627 | tc->flags |= TRANSPORT_CONNECTION_F_IS_TX_PACED; |
| 628 | transport_connection_tx_pacer_reset (tc, rate_bytes_per_sec, |
| 629 | initial_bucket, |
| 630 | vm->clib_time.last_cpu_time); |
Florin Coras | d67f112 | 2018-05-21 17:47:40 -0700 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | void |
| 634 | transport_connection_tx_pacer_update (transport_connection_t * tc, |
| 635 | u64 bytes_per_sec) |
| 636 | { |
Florin Coras | d67f112 | 2018-05-21 17:47:40 -0700 | [diff] [blame] | 637 | spacer_set_pace_rate (&tc->pacer, bytes_per_sec); |
Florin Coras | d67f112 | 2018-05-21 17:47:40 -0700 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | u32 |
Florin Coras | e55a6d7 | 2018-10-31 23:09:22 -0700 | [diff] [blame] | 641 | transport_connection_tx_pacer_burst (transport_connection_t * tc, |
| 642 | u64 time_now) |
| 643 | { |
| 644 | time_now >>= SPACER_CPU_TICKS_PER_PERIOD_SHIFT; |
| 645 | return spacer_max_burst (&tc->pacer, time_now); |
| 646 | } |
| 647 | |
| 648 | u32 |
| 649 | transport_connection_snd_space (transport_connection_t * tc, u64 time_now, |
| 650 | u16 mss) |
Florin Coras | d67f112 | 2018-05-21 17:47:40 -0700 | [diff] [blame] | 651 | { |
| 652 | u32 snd_space, max_paced_burst; |
Florin Coras | d67f112 | 2018-05-21 17:47:40 -0700 | [diff] [blame] | 653 | |
| 654 | snd_space = tp_vfts[tc->proto].send_space (tc); |
| 655 | if (transport_connection_is_tx_paced (tc)) |
| 656 | { |
| 657 | time_now >>= SPACER_CPU_TICKS_PER_PERIOD_SHIFT; |
| 658 | max_paced_burst = spacer_max_burst (&tc->pacer, time_now); |
Simon Zhang | 1146ff4 | 2019-09-02 22:54:00 +0800 | [diff] [blame^] | 659 | max_paced_burst = |
| 660 | (max_paced_burst < TRANSPORT_PACER_MIN_BURST) ? 0 : max_paced_burst; |
Florin Coras | d67f112 | 2018-05-21 17:47:40 -0700 | [diff] [blame] | 661 | snd_space = clib_min (snd_space, max_paced_burst); |
Simon Zhang | 1146ff4 | 2019-09-02 22:54:00 +0800 | [diff] [blame^] | 662 | return snd_space >= mss ? snd_space - snd_space % mss : snd_space; |
Florin Coras | d67f112 | 2018-05-21 17:47:40 -0700 | [diff] [blame] | 663 | } |
| 664 | return snd_space; |
| 665 | } |
| 666 | |
Florin Coras | 5281473 | 2019-06-12 15:38:19 -0700 | [diff] [blame] | 667 | u64 |
| 668 | transport_connection_tx_pacer_rate (transport_connection_t * tc) |
| 669 | { |
| 670 | return spacer_pace_rate (&tc->pacer); |
| 671 | } |
| 672 | |
Florin Coras | d67f112 | 2018-05-21 17:47:40 -0700 | [diff] [blame] | 673 | void |
Florin Coras | 0048223 | 2019-08-02 12:52:00 -0700 | [diff] [blame] | 674 | transport_connection_update_tx_bytes (transport_connection_t * tc, u32 bytes) |
Florin Coras | d67f112 | 2018-05-21 17:47:40 -0700 | [diff] [blame] | 675 | { |
Florin Coras | d67f112 | 2018-05-21 17:47:40 -0700 | [diff] [blame] | 676 | if (transport_connection_is_tx_paced (tc)) |
| 677 | spacer_update_bucket (&tc->pacer, bytes); |
| 678 | } |
| 679 | |
| 680 | void |
Florin Coras | e55a6d7 | 2018-10-31 23:09:22 -0700 | [diff] [blame] | 681 | transport_connection_tx_pacer_update_bytes (transport_connection_t * tc, |
| 682 | u32 bytes) |
| 683 | { |
| 684 | spacer_update_bucket (&tc->pacer, bytes); |
| 685 | } |
| 686 | |
| 687 | void |
Florin Coras | d67f112 | 2018-05-21 17:47:40 -0700 | [diff] [blame] | 688 | transport_init_tx_pacers_period (void) |
| 689 | { |
| 690 | f64 cpu_freq = os_cpu_clock_frequency (); |
| 691 | transport_pacer_period = cpu_freq / SPACER_CPU_TICKS_PER_PERIOD; |
| 692 | } |
| 693 | |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 694 | void |
Florin Coras | 561af9b | 2017-12-09 10:19:43 -0800 | [diff] [blame] | 695 | transport_update_time (f64 time_now, u8 thread_index) |
| 696 | { |
| 697 | transport_proto_vft_t *vft; |
| 698 | vec_foreach (vft, tp_vfts) |
| 699 | { |
| 700 | if (vft->update_time) |
| 701 | (vft->update_time) (time_now, thread_index); |
| 702 | } |
| 703 | } |
| 704 | |
| 705 | void |
| 706 | transport_enable_disable (vlib_main_t * vm, u8 is_en) |
| 707 | { |
| 708 | transport_proto_vft_t *vft; |
| 709 | vec_foreach (vft, tp_vfts) |
| 710 | { |
| 711 | if (vft->enable) |
| 712 | (vft->enable) (vm, is_en); |
| 713 | } |
| 714 | } |
| 715 | |
| 716 | void |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 717 | transport_init (void) |
| 718 | { |
| 719 | vlib_thread_main_t *vtm = vlib_get_thread_main (); |
Florin Coras | 31c9955 | 2019-03-01 13:00:58 -0800 | [diff] [blame] | 720 | session_main_t *smm = vnet_get_session_main (); |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 721 | u32 num_threads; |
| 722 | |
Florin Coras | 93e6580 | 2017-11-29 00:07:11 -0500 | [diff] [blame] | 723 | if (smm->local_endpoints_table_buckets == 0) |
| 724 | smm->local_endpoints_table_buckets = 250000; |
| 725 | if (smm->local_endpoints_table_memory == 0) |
| 726 | smm->local_endpoints_table_memory = 512 << 20; |
| 727 | |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 728 | /* Initialize [port-allocator] random number seed */ |
| 729 | port_allocator_seed = (u32) clib_cpu_time_now (); |
| 730 | |
| 731 | clib_bihash_init_24_8 (&local_endpoints_table, "local endpoints table", |
Florin Coras | 93e6580 | 2017-11-29 00:07:11 -0500 | [diff] [blame] | 732 | smm->local_endpoints_table_buckets, |
| 733 | smm->local_endpoints_table_memory); |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 734 | num_threads = 1 /* main thread */ + vtm->n_threads; |
| 735 | if (num_threads > 1) |
| 736 | clib_spinlock_init (&local_endpoints_lock); |
| 737 | } |
| 738 | |
| 739 | /* |
| 740 | * fd.io coding-style-patch-verification: ON |
| 741 | * |
| 742 | * Local Variables: |
| 743 | * eval: (c-set-style "gnu") |
| 744 | * End: |
| 745 | */ |