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");
60 case TRANSPORT_PROTO_UDPC:
61 s = format (s, "UDPC");
68 format_transport_proto_short (u8 * s, va_list * args)
70 u32 transport_proto = va_arg (*args, u32);
71 switch (transport_proto)
73 case TRANSPORT_PROTO_TCP:
76 case TRANSPORT_PROTO_UDP:
79 case TRANSPORT_PROTO_SCTP:
82 case TRANSPORT_PROTO_UDPC:
90 unformat_transport_proto (unformat_input_t * input, va_list * args)
92 u32 *proto = va_arg (*args, u32 *);
93 if (unformat (input, "tcp"))
94 *proto = TRANSPORT_PROTO_TCP;
95 else if (unformat (input, "TCP"))
96 *proto = TRANSPORT_PROTO_TCP;
97 else if (unformat (input, "udp"))
98 *proto = TRANSPORT_PROTO_UDP;
99 else if (unformat (input, "UDP"))
100 *proto = TRANSPORT_PROTO_UDP;
101 else if (unformat (input, "sctp"))
102 *proto = TRANSPORT_PROTO_SCTP;
103 else if (unformat (input, "SCTP"))
104 *proto = TRANSPORT_PROTO_SCTP;
105 else if (unformat (input, "tls"))
106 *proto = TRANSPORT_PROTO_TLS;
107 else if (unformat (input, "TLS"))
108 *proto = TRANSPORT_PROTO_TLS;
109 else if (unformat (input, "udpc"))
110 *proto = TRANSPORT_PROTO_UDPC;
111 else if (unformat (input, "UDPC"))
112 *proto = TRANSPORT_PROTO_UDPC;
119 transport_endpoint_lookup (transport_endpoint_table_t * ht, u8 proto,
120 ip46_address_t * ip, u16 port)
122 clib_bihash_kv_24_8_t kv;
125 kv.key[0] = ip->as_u64[0];
126 kv.key[1] = ip->as_u64[1];
127 kv.key[2] = (u64) port << 8 | (u64) proto;
129 rv = clib_bihash_search_inline_24_8 (ht, &kv);
133 return ENDPOINT_INVALID_INDEX;
137 transport_endpoint_table_add (transport_endpoint_table_t * ht, u8 proto,
138 transport_endpoint_t * te, u32 value)
140 clib_bihash_kv_24_8_t kv;
142 kv.key[0] = te->ip.as_u64[0];
143 kv.key[1] = te->ip.as_u64[1];
144 kv.key[2] = (u64) te->port << 8 | (u64) proto;
147 clib_bihash_add_del_24_8 (ht, &kv, 1);
151 transport_endpoint_table_del (transport_endpoint_table_t * ht, u8 proto,
152 transport_endpoint_t * te)
154 clib_bihash_kv_24_8_t kv;
156 kv.key[0] = te->ip.as_u64[0];
157 kv.key[1] = te->ip.as_u64[1];
158 kv.key[2] = (u64) te->port << 8 | (u64) proto;
160 clib_bihash_add_del_24_8 (ht, &kv, 0);
164 * Register transport virtual function table.
166 * @param transport_proto - transport protocol type (i.e., TCP, UDP ..)
167 * @param vft - virtual function table for transport proto
168 * @param fib_proto - network layer protocol
169 * @param output_node - output node index that session layer will hand off
170 * buffers to, for requested fib proto
173 transport_register_protocol (transport_proto_t transport_proto,
174 const transport_proto_vft_t * vft,
175 fib_protocol_t fib_proto, u32 output_node)
177 u8 is_ip4 = fib_proto == FIB_PROTOCOL_IP4;
179 vec_validate (tp_vfts, transport_proto);
180 tp_vfts[transport_proto] = *vft;
182 session_register_transport (transport_proto, vft, is_ip4, output_node);
186 * Get transport virtual function table
188 * @param type - session type (not protocol type)
190 transport_proto_vft_t *
191 transport_protocol_get_vft (transport_proto_t transport_proto)
193 if (transport_proto >= vec_len (tp_vfts))
195 return &tp_vfts[transport_proto];
198 transport_service_type_t
199 transport_protocol_service_type (transport_proto_t tp)
201 return tp_vfts[tp].service_type;
204 transport_tx_fn_type_t
205 transport_protocol_tx_fn_type (transport_proto_t tp)
207 return tp_vfts[tp].tx_type;
211 transport_protocol_is_cl (transport_proto_t tp)
213 return (tp_vfts[tp].service_type == TRANSPORT_SERVICE_CL);
216 #define PORT_MASK ((1 << 16)- 1)
219 transport_endpoint_del (u32 tepi)
221 clib_spinlock_lock_if_init (&local_endpoints_lock);
222 pool_put_index (local_endpoints, tepi);
223 clib_spinlock_unlock_if_init (&local_endpoints_lock);
226 always_inline transport_endpoint_t *
227 transport_endpoint_new (void)
229 transport_endpoint_t *tep;
230 pool_get (local_endpoints, tep);
235 transport_endpoint_cleanup (u8 proto, ip46_address_t * lcl_ip, u16 port)
238 transport_endpoint_t *tep;
240 /* Cleanup local endpoint if this was an active connect */
241 tepi = transport_endpoint_lookup (&local_endpoints_table, proto, lcl_ip,
242 clib_net_to_host_u16 (port));
243 if (tepi != ENDPOINT_INVALID_INDEX)
245 tep = pool_elt_at_index (local_endpoints, tepi);
246 transport_endpoint_table_del (&local_endpoints_table, proto, tep);
247 transport_endpoint_del (tepi);
252 * Allocate local port and add if successful add entry to local endpoint
253 * table to mark the pair as used.
256 transport_alloc_local_port (u8 proto, ip46_address_t * ip)
258 transport_endpoint_t *tep;
260 u16 min = 1024, max = 65535; /* XXX configurable ? */
265 /* Only support active opens from thread 0 */
266 ASSERT (vlib_get_thread_index () == 0);
268 /* Search for first free slot */
269 for (tries = 0; tries < limit; tries++)
273 /* Find a port in the specified range */
276 port = random_u32 (&port_allocator_seed) & PORT_MASK;
277 if (PREDICT_TRUE (port >= min && port < max))
281 /* Look it up. If not found, we're done */
282 tei = transport_endpoint_lookup (&local_endpoints_table, proto, ip,
284 if (tei == ENDPOINT_INVALID_INDEX)
286 clib_spinlock_lock_if_init (&local_endpoints_lock);
287 tep = transport_endpoint_new ();
288 clib_memcpy (&tep->ip, ip, sizeof (*ip));
290 transport_endpoint_table_add (&local_endpoints_table, proto, tep,
291 tep - local_endpoints);
292 clib_spinlock_unlock_if_init (&local_endpoints_lock);
301 transport_alloc_local_endpoint (u8 proto, transport_endpoint_t * rmt,
302 ip46_address_t * lcl_addr, u16 * lcl_port)
305 fib_node_index_t fei;
310 * Find the local address and allocate port
313 /* Find a FIB path to the destination */
314 clib_memcpy (&prefix.fp_addr, &rmt->ip, sizeof (rmt->ip));
315 prefix.fp_proto = rmt->is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
316 prefix.fp_len = rmt->is_ip4 ? 32 : 128;
318 ASSERT (rmt->fib_index != ENDPOINT_INVALID_INDEX);
319 fei = fib_table_lookup (rmt->fib_index, &prefix);
321 /* Couldn't find route to destination. Bail out. */
322 if (fei == FIB_NODE_INDEX_INVALID)
324 clib_warning ("no route to destination");
328 sw_if_index = rmt->sw_if_index;
329 if (sw_if_index == ENDPOINT_INVALID_INDEX)
330 sw_if_index = fib_entry_get_resolving_interface (fei);
332 if (sw_if_index == ENDPOINT_INVALID_INDEX)
334 clib_warning ("no resolving interface for %U", format_ip46_address,
335 &rmt->ip, (rmt->is_ip4 == 0) + 1);
339 memset (lcl_addr, 0, sizeof (*lcl_addr));
344 ip4 = ip_interface_get_first_ip (sw_if_index, 1);
347 clib_warning ("no routable ip4 address on %U",
348 format_vnet_sw_if_index_name, vnet_get_main (),
352 lcl_addr->ip4.as_u32 = ip4->as_u32;
357 ip6 = ip_interface_get_first_ip (sw_if_index, 0);
360 clib_warning ("no routable ip6 addresses on %U",
361 format_vnet_sw_if_index_name, vnet_get_main (),
365 clib_memcpy (&lcl_addr->ip6, ip6, sizeof (*ip6));
368 /* Allocate source port */
369 port = transport_alloc_local_port (proto, lcl_addr);
372 clib_warning ("Failed to allocate src port");
380 transport_update_time (f64 time_now, u8 thread_index)
382 transport_proto_vft_t *vft;
383 vec_foreach (vft, tp_vfts)
385 if (vft->update_time)
386 (vft->update_time) (time_now, thread_index);
391 transport_enable_disable (vlib_main_t * vm, u8 is_en)
393 transport_proto_vft_t *vft;
394 vec_foreach (vft, tp_vfts)
397 (vft->enable) (vm, is_en);
402 transport_init (void)
404 vlib_thread_main_t *vtm = vlib_get_thread_main ();
405 session_manager_main_t *smm = vnet_get_session_manager_main ();
408 if (smm->local_endpoints_table_buckets == 0)
409 smm->local_endpoints_table_buckets = 250000;
410 if (smm->local_endpoints_table_memory == 0)
411 smm->local_endpoints_table_memory = 512 << 20;
413 /* Initialize [port-allocator] random number seed */
414 port_allocator_seed = (u32) clib_cpu_time_now ();
416 clib_bihash_init_24_8 (&local_endpoints_table, "local endpoints table",
417 smm->local_endpoints_table_buckets,
418 smm->local_endpoints_table_memory);
419 num_threads = 1 /* main thread */ + vtm->n_threads;
421 clib_spinlock_init (&local_endpoints_lock);
425 * fd.io coding-style-patch-verification: ON
428 * eval: (c-set-style "gnu")