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