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