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