Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 Cisco and/or its affiliates. |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | |
| 16 | #include <vnet/session/transport_interface.h> |
| 17 | #include <vnet/session/session.h> |
| 18 | #include <vnet/fib/fib.h> |
| 19 | |
| 20 | /** |
| 21 | * Per-type vector of transport protocol virtual function tables |
| 22 | */ |
| 23 | transport_proto_vft_t *tp_vfts; |
| 24 | |
| 25 | /* |
| 26 | * Port allocator seed |
| 27 | */ |
| 28 | static u32 port_allocator_seed; |
| 29 | |
| 30 | /* |
| 31 | * Local endpoints table |
| 32 | */ |
| 33 | static transport_endpoint_table_t local_endpoints_table; |
| 34 | |
| 35 | /* |
| 36 | * Pool of local endpoints |
| 37 | */ |
| 38 | static transport_endpoint_t *local_endpoints; |
| 39 | |
| 40 | /* |
| 41 | * Local endpoints pool lock |
| 42 | */ |
| 43 | static clib_spinlock_t local_endpoints_lock; |
| 44 | |
Florin Coras | 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 | { |
| 51 | case TRANSPORT_PROTO_TCP: |
| 52 | s = format (s, "TCP"); |
| 53 | break; |
| 54 | case TRANSPORT_PROTO_UDP: |
| 55 | s = format (s, "UDP"); |
| 56 | break; |
| 57 | } |
| 58 | return s; |
| 59 | } |
| 60 | |
Florin Coras | 561af9b | 2017-12-09 10:19:43 -0800 | [diff] [blame] | 61 | u8 * |
| 62 | format_transport_proto_short (u8 * s, va_list * args) |
| 63 | { |
| 64 | u32 transport_proto = va_arg (*args, u32); |
| 65 | switch (transport_proto) |
| 66 | { |
| 67 | case TRANSPORT_PROTO_TCP: |
| 68 | s = format (s, "T"); |
| 69 | break; |
| 70 | case TRANSPORT_PROTO_UDP: |
| 71 | s = format (s, "U"); |
| 72 | break; |
| 73 | } |
| 74 | return s; |
| 75 | } |
| 76 | |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 77 | uword |
| 78 | unformat_transport_proto (unformat_input_t * input, va_list * args) |
| 79 | { |
| 80 | u32 *proto = va_arg (*args, u32 *); |
| 81 | if (unformat (input, "tcp")) |
| 82 | *proto = TRANSPORT_PROTO_TCP; |
| 83 | else if (unformat (input, "TCP")) |
| 84 | *proto = TRANSPORT_PROTO_TCP; |
| 85 | else if (unformat (input, "udp")) |
| 86 | *proto = TRANSPORT_PROTO_UDP; |
| 87 | else if (unformat (input, "UDP")) |
| 88 | *proto = TRANSPORT_PROTO_UDP; |
| 89 | else |
| 90 | return 0; |
| 91 | return 1; |
| 92 | } |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 93 | |
| 94 | u32 |
| 95 | transport_endpoint_lookup (transport_endpoint_table_t * ht, u8 proto, |
| 96 | ip46_address_t * ip, u16 port) |
| 97 | { |
| 98 | clib_bihash_kv_24_8_t kv; |
| 99 | int rv; |
| 100 | |
| 101 | kv.key[0] = ip->as_u64[0]; |
| 102 | kv.key[1] = ip->as_u64[1]; |
| 103 | kv.key[2] = (u64) port << 8 | (u64) proto; |
| 104 | |
| 105 | rv = clib_bihash_search_inline_24_8 (ht, &kv); |
| 106 | if (rv == 0) |
| 107 | return kv.value; |
| 108 | |
| 109 | return ENDPOINT_INVALID_INDEX; |
| 110 | } |
| 111 | |
| 112 | void |
| 113 | transport_endpoint_table_add (transport_endpoint_table_t * ht, u8 proto, |
| 114 | transport_endpoint_t * te, u32 value) |
| 115 | { |
| 116 | clib_bihash_kv_24_8_t kv; |
| 117 | |
| 118 | kv.key[0] = te->ip.as_u64[0]; |
| 119 | kv.key[1] = te->ip.as_u64[1]; |
| 120 | kv.key[2] = (u64) te->port << 8 | (u64) proto; |
| 121 | kv.value = value; |
| 122 | |
| 123 | clib_bihash_add_del_24_8 (ht, &kv, 1); |
| 124 | } |
| 125 | |
| 126 | void |
| 127 | transport_endpoint_table_del (transport_endpoint_table_t * ht, u8 proto, |
| 128 | transport_endpoint_t * te) |
| 129 | { |
| 130 | clib_bihash_kv_24_8_t kv; |
| 131 | |
| 132 | kv.key[0] = te->ip.as_u64[0]; |
| 133 | kv.key[1] = te->ip.as_u64[1]; |
| 134 | kv.key[2] = (u64) te->port << 8 | (u64) proto; |
| 135 | |
| 136 | clib_bihash_add_del_24_8 (ht, &kv, 0); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Register transport virtual function table. |
| 141 | * |
Florin Coras | 561af9b | 2017-12-09 10:19:43 -0800 | [diff] [blame] | 142 | * @param transport_proto - transport protocol type (i.e., TCP, UDP ..) |
| 143 | * @param vft - virtual function table for transport proto |
| 144 | * @param fib_proto - network layer protocol |
| 145 | * @param output_node - output node index that session layer will hand off |
| 146 | * buffers to, for requested fib proto |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 147 | */ |
| 148 | void |
Florin Coras | 561af9b | 2017-12-09 10:19:43 -0800 | [diff] [blame] | 149 | transport_register_protocol (transport_proto_t transport_proto, |
| 150 | const transport_proto_vft_t * vft, |
| 151 | fib_protocol_t fib_proto, u32 output_node) |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 152 | { |
Florin Coras | 561af9b | 2017-12-09 10:19:43 -0800 | [diff] [blame] | 153 | u8 is_ip4 = fib_proto == FIB_PROTOCOL_IP4; |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 154 | |
Florin Coras | 561af9b | 2017-12-09 10:19:43 -0800 | [diff] [blame] | 155 | vec_validate (tp_vfts, transport_proto); |
| 156 | tp_vfts[transport_proto] = *vft; |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 157 | |
Florin Coras | 561af9b | 2017-12-09 10:19:43 -0800 | [diff] [blame] | 158 | session_register_transport (transport_proto, vft, is_ip4, output_node); |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Get transport virtual function table |
| 163 | * |
| 164 | * @param type - session type (not protocol type) |
| 165 | */ |
| 166 | transport_proto_vft_t * |
Florin Coras | 561af9b | 2017-12-09 10:19:43 -0800 | [diff] [blame] | 167 | transport_protocol_get_vft (transport_proto_t transport_proto) |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 168 | { |
Florin Coras | 561af9b | 2017-12-09 10:19:43 -0800 | [diff] [blame] | 169 | if (transport_proto >= vec_len (tp_vfts)) |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 170 | return 0; |
Florin Coras | 561af9b | 2017-12-09 10:19:43 -0800 | [diff] [blame] | 171 | return &tp_vfts[transport_proto]; |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | #define PORT_MASK ((1 << 16)- 1) |
| 175 | |
| 176 | void |
| 177 | transport_endpoint_del (u32 tepi) |
| 178 | { |
| 179 | clib_spinlock_lock_if_init (&local_endpoints_lock); |
| 180 | pool_put_index (local_endpoints, tepi); |
| 181 | clib_spinlock_unlock_if_init (&local_endpoints_lock); |
| 182 | } |
| 183 | |
| 184 | always_inline transport_endpoint_t * |
| 185 | transport_endpoint_new (void) |
| 186 | { |
| 187 | transport_endpoint_t *tep; |
| 188 | pool_get (local_endpoints, tep); |
| 189 | return tep; |
| 190 | } |
| 191 | |
| 192 | void |
| 193 | transport_endpoint_cleanup (u8 proto, ip46_address_t * lcl_ip, u16 port) |
| 194 | { |
| 195 | u32 tepi; |
| 196 | transport_endpoint_t *tep; |
| 197 | |
| 198 | /* Cleanup local endpoint if this was an active connect */ |
| 199 | tepi = transport_endpoint_lookup (&local_endpoints_table, proto, lcl_ip, |
| 200 | clib_net_to_host_u16 (port)); |
| 201 | if (tepi != ENDPOINT_INVALID_INDEX) |
| 202 | { |
| 203 | tep = pool_elt_at_index (local_endpoints, tepi); |
| 204 | transport_endpoint_table_del (&local_endpoints_table, proto, tep); |
| 205 | transport_endpoint_del (tepi); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Allocate local port and add if successful add entry to local endpoint |
| 211 | * table to mark the pair as used. |
| 212 | */ |
| 213 | int |
| 214 | transport_alloc_local_port (u8 proto, ip46_address_t * ip) |
| 215 | { |
| 216 | transport_endpoint_t *tep; |
| 217 | u32 tei; |
| 218 | u16 min = 1024, max = 65535; /* XXX configurable ? */ |
| 219 | int tries, limit; |
| 220 | |
| 221 | limit = max - min; |
| 222 | |
| 223 | /* Only support active opens from thread 0 */ |
| 224 | ASSERT (vlib_get_thread_index () == 0); |
| 225 | |
| 226 | /* Search for first free slot */ |
| 227 | for (tries = 0; tries < limit; tries++) |
| 228 | { |
| 229 | u16 port = 0; |
| 230 | |
| 231 | /* Find a port in the specified range */ |
| 232 | while (1) |
| 233 | { |
| 234 | port = random_u32 (&port_allocator_seed) & PORT_MASK; |
| 235 | if (PREDICT_TRUE (port >= min && port < max)) |
| 236 | break; |
| 237 | } |
| 238 | |
| 239 | /* Look it up. If not found, we're done */ |
| 240 | tei = transport_endpoint_lookup (&local_endpoints_table, proto, ip, |
| 241 | port); |
| 242 | if (tei == ENDPOINT_INVALID_INDEX) |
| 243 | { |
| 244 | clib_spinlock_lock_if_init (&local_endpoints_lock); |
| 245 | tep = transport_endpoint_new (); |
| 246 | clib_memcpy (&tep->ip, ip, sizeof (*ip)); |
| 247 | tep->port = port; |
| 248 | transport_endpoint_table_add (&local_endpoints_table, proto, tep, |
| 249 | tep - local_endpoints); |
| 250 | clib_spinlock_unlock_if_init (&local_endpoints_lock); |
| 251 | |
| 252 | return tep->port; |
| 253 | } |
| 254 | } |
| 255 | return -1; |
| 256 | } |
| 257 | |
| 258 | int |
| 259 | transport_alloc_local_endpoint (u8 proto, transport_endpoint_t * rmt, |
| 260 | ip46_address_t * lcl_addr, u16 * lcl_port) |
| 261 | { |
| 262 | fib_prefix_t prefix; |
| 263 | fib_node_index_t fei; |
| 264 | u32 sw_if_index; |
| 265 | int port; |
| 266 | |
| 267 | /* |
| 268 | * Find the local address and allocate port |
| 269 | */ |
| 270 | |
| 271 | /* Find a FIB path to the destination */ |
| 272 | clib_memcpy (&prefix.fp_addr, &rmt->ip, sizeof (rmt->ip)); |
| 273 | prefix.fp_proto = rmt->is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6; |
| 274 | prefix.fp_len = rmt->is_ip4 ? 32 : 128; |
| 275 | |
| 276 | ASSERT (rmt->fib_index != ENDPOINT_INVALID_INDEX); |
| 277 | fei = fib_table_lookup (rmt->fib_index, &prefix); |
| 278 | |
| 279 | /* Couldn't find route to destination. Bail out. */ |
| 280 | if (fei == FIB_NODE_INDEX_INVALID) |
| 281 | { |
| 282 | clib_warning ("no route to destination"); |
| 283 | return -1; |
| 284 | } |
| 285 | |
| 286 | sw_if_index = rmt->sw_if_index; |
| 287 | if (sw_if_index == ENDPOINT_INVALID_INDEX) |
| 288 | sw_if_index = fib_entry_get_resolving_interface (fei); |
| 289 | |
| 290 | if (sw_if_index == ENDPOINT_INVALID_INDEX) |
| 291 | { |
| 292 | clib_warning ("no resolving interface for %U", format_ip46_address, |
| 293 | &rmt->ip, (rmt->is_ip4 == 0) + 1); |
| 294 | return -1; |
| 295 | } |
| 296 | |
| 297 | memset (lcl_addr, 0, sizeof (*lcl_addr)); |
| 298 | |
| 299 | if (rmt->is_ip4) |
| 300 | { |
| 301 | ip4_address_t *ip4; |
| 302 | ip4 = ip_interface_get_first_ip (sw_if_index, 1); |
| 303 | lcl_addr->ip4.as_u32 = ip4->as_u32; |
| 304 | } |
| 305 | else |
| 306 | { |
| 307 | ip6_address_t *ip6; |
| 308 | ip6 = ip_interface_get_first_ip (sw_if_index, 0); |
| 309 | if (ip6 == 0) |
| 310 | { |
| 311 | clib_warning ("no routable ip6 addresses on %U", |
| 312 | format_vnet_sw_if_index_name, vnet_get_main (), |
| 313 | sw_if_index); |
| 314 | return -1; |
| 315 | } |
| 316 | clib_memcpy (&lcl_addr->ip6, ip6, sizeof (*ip6)); |
| 317 | } |
| 318 | |
| 319 | /* Allocate source port */ |
| 320 | port = transport_alloc_local_port (proto, lcl_addr); |
| 321 | if (port < 1) |
| 322 | { |
| 323 | clib_warning ("Failed to allocate src port"); |
| 324 | return -1; |
| 325 | } |
| 326 | *lcl_port = port; |
| 327 | return 0; |
| 328 | } |
| 329 | |
| 330 | void |
Florin Coras | 561af9b | 2017-12-09 10:19:43 -0800 | [diff] [blame] | 331 | transport_update_time (f64 time_now, u8 thread_index) |
| 332 | { |
| 333 | transport_proto_vft_t *vft; |
| 334 | vec_foreach (vft, tp_vfts) |
| 335 | { |
| 336 | if (vft->update_time) |
| 337 | (vft->update_time) (time_now, thread_index); |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | void |
| 342 | transport_enable_disable (vlib_main_t * vm, u8 is_en) |
| 343 | { |
| 344 | transport_proto_vft_t *vft; |
| 345 | vec_foreach (vft, tp_vfts) |
| 346 | { |
| 347 | if (vft->enable) |
| 348 | (vft->enable) (vm, is_en); |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | void |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 353 | transport_init (void) |
| 354 | { |
| 355 | vlib_thread_main_t *vtm = vlib_get_thread_main (); |
Florin Coras | 93e6580 | 2017-11-29 00:07:11 -0500 | [diff] [blame] | 356 | session_manager_main_t *smm = vnet_get_session_manager_main (); |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 357 | u32 num_threads; |
| 358 | |
Florin Coras | 93e6580 | 2017-11-29 00:07:11 -0500 | [diff] [blame] | 359 | if (smm->local_endpoints_table_buckets == 0) |
| 360 | smm->local_endpoints_table_buckets = 250000; |
| 361 | if (smm->local_endpoints_table_memory == 0) |
| 362 | smm->local_endpoints_table_memory = 512 << 20; |
| 363 | |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 364 | /* Initialize [port-allocator] random number seed */ |
| 365 | port_allocator_seed = (u32) clib_cpu_time_now (); |
| 366 | |
| 367 | clib_bihash_init_24_8 (&local_endpoints_table, "local endpoints table", |
Florin Coras | 93e6580 | 2017-11-29 00:07:11 -0500 | [diff] [blame] | 368 | smm->local_endpoints_table_buckets, |
| 369 | smm->local_endpoints_table_memory); |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 370 | num_threads = 1 /* main thread */ + vtm->n_threads; |
| 371 | if (num_threads > 1) |
| 372 | clib_spinlock_init (&local_endpoints_lock); |
| 373 | } |
| 374 | |
| 375 | /* |
| 376 | * fd.io coding-style-patch-verification: ON |
| 377 | * |
| 378 | * Local Variables: |
| 379 | * eval: (c-set-style "gnu") |
| 380 | * End: |
| 381 | */ |