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