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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <vnet/session/transport_interface.h>
17 #include <vnet/session/session.h>
18 #include <vnet/fib/fib.h>
21 * Per-type vector of transport protocol virtual function tables
23 transport_proto_vft_t *tp_vfts;
28 static u32 port_allocator_seed;
31 * Local endpoints table
33 static transport_endpoint_table_t local_endpoints_table;
36 * Pool of local endpoints
38 static transport_endpoint_t *local_endpoints;
41 * Local endpoints pool lock
43 static clib_spinlock_t local_endpoints_lock;
46 format_transport_proto (u8 * s, va_list * args)
48 u32 transport_proto = va_arg (*args, u32);
49 switch (transport_proto)
51 case TRANSPORT_PROTO_TCP:
52 s = format (s, "TCP");
54 case TRANSPORT_PROTO_UDP:
55 s = format (s, "UDP");
57 case TRANSPORT_PROTO_SCTP:
58 s = format (s, "SCTP");
65 format_transport_proto_short (u8 * s, va_list * args)
67 u32 transport_proto = va_arg (*args, u32);
68 switch (transport_proto)
70 case TRANSPORT_PROTO_TCP:
73 case TRANSPORT_PROTO_UDP:
76 case TRANSPORT_PROTO_SCTP:
84 unformat_transport_proto (unformat_input_t * input, va_list * args)
86 u32 *proto = va_arg (*args, u32 *);
87 if (unformat (input, "tcp"))
88 *proto = TRANSPORT_PROTO_TCP;
89 else if (unformat (input, "TCP"))
90 *proto = TRANSPORT_PROTO_TCP;
91 else if (unformat (input, "udp"))
92 *proto = TRANSPORT_PROTO_UDP;
93 else if (unformat (input, "UDP"))
94 *proto = TRANSPORT_PROTO_UDP;
95 else if (unformat (input, "sctp"))
96 *proto = TRANSPORT_PROTO_SCTP;
97 else if (unformat (input, "SCTP"))
98 *proto = TRANSPORT_PROTO_SCTP;
99 else if (unformat (input, "tls"))
100 *proto = TRANSPORT_PROTO_TLS;
101 else if (unformat (input, "TLS"))
102 *proto = TRANSPORT_PROTO_TLS;
109 transport_endpoint_lookup (transport_endpoint_table_t * ht, u8 proto,
110 ip46_address_t * ip, u16 port)
112 clib_bihash_kv_24_8_t kv;
115 kv.key[0] = ip->as_u64[0];
116 kv.key[1] = ip->as_u64[1];
117 kv.key[2] = (u64) port << 8 | (u64) proto;
119 rv = clib_bihash_search_inline_24_8 (ht, &kv);
123 return ENDPOINT_INVALID_INDEX;
127 transport_endpoint_table_add (transport_endpoint_table_t * ht, u8 proto,
128 transport_endpoint_t * te, u32 value)
130 clib_bihash_kv_24_8_t kv;
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;
137 clib_bihash_add_del_24_8 (ht, &kv, 1);
141 transport_endpoint_table_del (transport_endpoint_table_t * ht, u8 proto,
142 transport_endpoint_t * te)
144 clib_bihash_kv_24_8_t kv;
146 kv.key[0] = te->ip.as_u64[0];
147 kv.key[1] = te->ip.as_u64[1];
148 kv.key[2] = (u64) te->port << 8 | (u64) proto;
150 clib_bihash_add_del_24_8 (ht, &kv, 0);
154 * Register transport virtual function table.
156 * @param transport_proto - transport protocol type (i.e., TCP, UDP ..)
157 * @param vft - virtual function table for transport proto
158 * @param fib_proto - network layer protocol
159 * @param output_node - output node index that session layer will hand off
160 * buffers to, for requested fib proto
163 transport_register_protocol (transport_proto_t transport_proto,
164 const transport_proto_vft_t * vft,
165 fib_protocol_t fib_proto, u32 output_node)
167 u8 is_ip4 = fib_proto == FIB_PROTOCOL_IP4;
169 vec_validate (tp_vfts, transport_proto);
170 tp_vfts[transport_proto] = *vft;
172 session_register_transport (transport_proto, vft, is_ip4, output_node);
176 * Get transport virtual function table
178 * @param type - session type (not protocol type)
180 transport_proto_vft_t *
181 transport_protocol_get_vft (transport_proto_t transport_proto)
183 if (transport_proto >= vec_len (tp_vfts))
185 return &tp_vfts[transport_proto];
188 #define PORT_MASK ((1 << 16)- 1)
191 transport_endpoint_del (u32 tepi)
193 clib_spinlock_lock_if_init (&local_endpoints_lock);
194 pool_put_index (local_endpoints, tepi);
195 clib_spinlock_unlock_if_init (&local_endpoints_lock);
198 always_inline transport_endpoint_t *
199 transport_endpoint_new (void)
201 transport_endpoint_t *tep;
202 pool_get (local_endpoints, tep);
207 transport_endpoint_cleanup (u8 proto, ip46_address_t * lcl_ip, u16 port)
210 transport_endpoint_t *tep;
212 /* Cleanup local endpoint if this was an active connect */
213 tepi = transport_endpoint_lookup (&local_endpoints_table, proto, lcl_ip,
214 clib_net_to_host_u16 (port));
215 if (tepi != ENDPOINT_INVALID_INDEX)
217 tep = pool_elt_at_index (local_endpoints, tepi);
218 transport_endpoint_table_del (&local_endpoints_table, proto, tep);
219 transport_endpoint_del (tepi);
224 * Allocate local port and add if successful add entry to local endpoint
225 * table to mark the pair as used.
228 transport_alloc_local_port (u8 proto, ip46_address_t * ip)
230 transport_endpoint_t *tep;
232 u16 min = 1024, max = 65535; /* XXX configurable ? */
237 /* Only support active opens from thread 0 */
238 ASSERT (vlib_get_thread_index () == 0);
240 /* Search for first free slot */
241 for (tries = 0; tries < limit; tries++)
245 /* Find a port in the specified range */
248 port = random_u32 (&port_allocator_seed) & PORT_MASK;
249 if (PREDICT_TRUE (port >= min && port < max))
253 /* Look it up. If not found, we're done */
254 tei = transport_endpoint_lookup (&local_endpoints_table, proto, ip,
256 if (tei == ENDPOINT_INVALID_INDEX)
258 clib_spinlock_lock_if_init (&local_endpoints_lock);
259 tep = transport_endpoint_new ();
260 clib_memcpy (&tep->ip, ip, sizeof (*ip));
262 transport_endpoint_table_add (&local_endpoints_table, proto, tep,
263 tep - local_endpoints);
264 clib_spinlock_unlock_if_init (&local_endpoints_lock);
273 transport_alloc_local_endpoint (u8 proto, transport_endpoint_t * rmt,
274 ip46_address_t * lcl_addr, u16 * lcl_port)
277 fib_node_index_t fei;
282 * Find the local address and allocate port
285 /* Find a FIB path to the destination */
286 clib_memcpy (&prefix.fp_addr, &rmt->ip, sizeof (rmt->ip));
287 prefix.fp_proto = rmt->is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
288 prefix.fp_len = rmt->is_ip4 ? 32 : 128;
290 ASSERT (rmt->fib_index != ENDPOINT_INVALID_INDEX);
291 fei = fib_table_lookup (rmt->fib_index, &prefix);
293 /* Couldn't find route to destination. Bail out. */
294 if (fei == FIB_NODE_INDEX_INVALID)
296 clib_warning ("no route to destination");
300 sw_if_index = rmt->sw_if_index;
301 if (sw_if_index == ENDPOINT_INVALID_INDEX)
302 sw_if_index = fib_entry_get_resolving_interface (fei);
304 if (sw_if_index == ENDPOINT_INVALID_INDEX)
306 clib_warning ("no resolving interface for %U", format_ip46_address,
307 &rmt->ip, (rmt->is_ip4 == 0) + 1);
311 memset (lcl_addr, 0, sizeof (*lcl_addr));
316 ip4 = ip_interface_get_first_ip (sw_if_index, 1);
319 clib_warning ("no routable ip4 address on %U",
320 format_vnet_sw_if_index_name, vnet_get_main (),
324 lcl_addr->ip4.as_u32 = ip4->as_u32;
329 ip6 = ip_interface_get_first_ip (sw_if_index, 0);
332 clib_warning ("no routable ip6 addresses on %U",
333 format_vnet_sw_if_index_name, vnet_get_main (),
337 clib_memcpy (&lcl_addr->ip6, ip6, sizeof (*ip6));
340 /* Allocate source port */
341 port = transport_alloc_local_port (proto, lcl_addr);
344 clib_warning ("Failed to allocate src port");
352 transport_update_time (f64 time_now, u8 thread_index)
354 transport_proto_vft_t *vft;
355 vec_foreach (vft, tp_vfts)
357 if (vft->update_time)
358 (vft->update_time) (time_now, thread_index);
363 transport_enable_disable (vlib_main_t * vm, u8 is_en)
365 transport_proto_vft_t *vft;
366 vec_foreach (vft, tp_vfts)
369 (vft->enable) (vm, is_en);
374 transport_init (void)
376 vlib_thread_main_t *vtm = vlib_get_thread_main ();
377 session_manager_main_t *smm = vnet_get_session_manager_main ();
380 if (smm->local_endpoints_table_buckets == 0)
381 smm->local_endpoints_table_buckets = 250000;
382 if (smm->local_endpoints_table_memory == 0)
383 smm->local_endpoints_table_memory = 512 << 20;
385 /* Initialize [port-allocator] random number seed */
386 port_allocator_seed = (u32) clib_cpu_time_now ();
388 clib_bihash_init_24_8 (&local_endpoints_table, "local endpoints table",
389 smm->local_endpoints_table_buckets,
390 smm->local_endpoints_table_memory);
391 num_threads = 1 /* main thread */ + vtm->n_threads;
393 clib_spinlock_init (&local_endpoints_lock);
397 * fd.io coding-style-patch-verification: ON
400 * eval: (c-set-style "gnu")