FD.io VPP  v18.07-rc0-415-g6c78436
Vector Packet Processing
transport.c
Go to the documentation of this file.
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 
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  */
24 
25 /*
26  * Port allocator seed
27  */
29 
30 /*
31  * Local endpoints table
32  */
34 
35 /*
36  * Pool of local endpoints
37  */
39 
40 /*
41  * Local endpoints pool lock
42  */
44 
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  {
52  s = format (s, "TCP");
53  break;
55  s = format (s, "UDP");
56  break;
58  s = format (s, "SCTP");
59  break;
61  s = format (s, "UDPC");
62  break;
63  }
64  return s;
65 }
66 
67 u8 *
68 format_transport_proto_short (u8 * s, va_list * args)
69 {
70  u32 transport_proto = va_arg (*args, u32);
71  switch (transport_proto)
72  {
74  s = format (s, "T");
75  break;
77  s = format (s, "U");
78  break;
80  s = format (s, "S");
81  break;
83  s = format (s, "U");
84  break;
85  }
86  return s;
87 }
88 
89 uword
90 unformat_transport_proto (unformat_input_t * input, va_list * args)
91 {
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;
113  else
114  return 0;
115  return 1;
116 }
117 
118 u32
120  ip46_address_t * ip, u16 port)
121 {
123  int rv;
124 
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;
128 
129  rv = clib_bihash_search_inline_24_8 (ht, &kv);
130  if (rv == 0)
131  return kv.value;
132 
133  return ENDPOINT_INVALID_INDEX;
134 }
135 
136 void
138  transport_endpoint_t * te, u32 value)
139 {
141 
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;
145  kv.value = value;
146 
147  clib_bihash_add_del_24_8 (ht, &kv, 1);
148 }
149 
150 void
153 {
155 
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;
159 
160  clib_bihash_add_del_24_8 (ht, &kv, 0);
161 }
162 
163 /**
164  * Register transport virtual function table.
165  *
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
171  */
172 void
174  const transport_proto_vft_t * vft,
175  fib_protocol_t fib_proto, u32 output_node)
176 {
177  u8 is_ip4 = fib_proto == FIB_PROTOCOL_IP4;
178 
179  vec_validate (tp_vfts, transport_proto);
180  tp_vfts[transport_proto] = *vft;
181 
182  session_register_transport (transport_proto, vft, is_ip4, output_node);
183 }
184 
185 /**
186  * Get transport virtual function table
187  *
188  * @param type - session type (not protocol type)
189  */
192 {
193  if (transport_proto >= vec_len (tp_vfts))
194  return 0;
195  return &tp_vfts[transport_proto];
196 }
197 
200 {
201  return tp_vfts[tp].service_type;
202 }
203 
206 {
207  return tp_vfts[tp].tx_type;
208 }
209 
210 u8
212 {
213  return (tp_vfts[tp].service_type == TRANSPORT_SERVICE_CL);
214 }
215 
216 #define PORT_MASK ((1 << 16)- 1)
217 
218 void
220 {
221  clib_spinlock_lock_if_init (&local_endpoints_lock);
223  clib_spinlock_unlock_if_init (&local_endpoints_lock);
224 }
225 
228 {
230  pool_get (local_endpoints, tep);
231  return tep;
232 }
233 
234 void
235 transport_endpoint_cleanup (u8 proto, ip46_address_t * lcl_ip, u16 port)
236 {
237  u32 tepi;
239 
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)
244  {
245  tep = pool_elt_at_index (local_endpoints, tepi);
247  transport_endpoint_del (tepi);
248  }
249 }
250 
251 /**
252  * Allocate local port and add if successful add entry to local endpoint
253  * table to mark the pair as used.
254  */
255 int
256 transport_alloc_local_port (u8 proto, ip46_address_t * ip)
257 {
259  u32 tei;
260  u16 min = 1024, max = 65535; /* XXX configurable ? */
261  int tries, limit;
262 
263  limit = max - min;
264 
265  /* Only support active opens from thread 0 */
266  ASSERT (vlib_get_thread_index () == 0);
267 
268  /* Search for first free slot */
269  for (tries = 0; tries < limit; tries++)
270  {
271  u16 port = 0;
272 
273  /* Find a port in the specified range */
274  while (1)
275  {
277  if (PREDICT_TRUE (port >= min && port < max))
278  break;
279  }
280 
281  /* Look it up. If not found, we're done */
283  port);
284  if (tei == ENDPOINT_INVALID_INDEX)
285  {
286  clib_spinlock_lock_if_init (&local_endpoints_lock);
287  tep = transport_endpoint_new ();
288  clib_memcpy (&tep->ip, ip, sizeof (*ip));
289  tep->port = port;
291  tep - local_endpoints);
292  clib_spinlock_unlock_if_init (&local_endpoints_lock);
293 
294  return tep->port;
295  }
296  }
297  return -1;
298 }
299 
300 int
302  ip46_address_t * lcl_addr, u16 * lcl_port)
303 {
304  fib_prefix_t prefix;
305  fib_node_index_t fei;
306  u32 sw_if_index;
307  int port;
308 
309  /*
310  * Find the local address and allocate port
311  */
312 
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;
317 
318  ASSERT (rmt->fib_index != ENDPOINT_INVALID_INDEX);
319  fei = fib_table_lookup (rmt->fib_index, &prefix);
320 
321  /* Couldn't find route to destination. Bail out. */
322  if (fei == FIB_NODE_INDEX_INVALID)
323  {
324  clib_warning ("no route to destination");
325  return -1;
326  }
327 
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);
331 
332  if (sw_if_index == ENDPOINT_INVALID_INDEX)
333  {
334  clib_warning ("no resolving interface for %U", format_ip46_address,
335  &rmt->ip, (rmt->is_ip4 == 0) + 1);
336  return -1;
337  }
338 
339  memset (lcl_addr, 0, sizeof (*lcl_addr));
340 
341  if (rmt->is_ip4)
342  {
343  ip4_address_t *ip4;
344  ip4 = ip_interface_get_first_ip (sw_if_index, 1);
345  if (!ip4)
346  {
347  clib_warning ("no routable ip4 address on %U",
349  sw_if_index);
350  return -1;
351  }
352  lcl_addr->ip4.as_u32 = ip4->as_u32;
353  }
354  else
355  {
356  ip6_address_t *ip6;
357  ip6 = ip_interface_get_first_ip (sw_if_index, 0);
358  if (ip6 == 0)
359  {
360  clib_warning ("no routable ip6 addresses on %U",
362  sw_if_index);
363  return -1;
364  }
365  clib_memcpy (&lcl_addr->ip6, ip6, sizeof (*ip6));
366  }
367 
368  /* Allocate source port */
369  port = transport_alloc_local_port (proto, lcl_addr);
370  if (port < 1)
371  {
372  clib_warning ("Failed to allocate src port");
373  return -1;
374  }
375  *lcl_port = port;
376  return 0;
377 }
378 
379 void
380 transport_update_time (f64 time_now, u8 thread_index)
381 {
383  vec_foreach (vft, tp_vfts)
384  {
385  if (vft->update_time)
386  (vft->update_time) (time_now, thread_index);
387  }
388 }
389 
390 void
392 {
394  vec_foreach (vft, tp_vfts)
395  {
396  if (vft->enable)
397  (vft->enable) (vm, is_en);
398  }
399 }
400 
401 void
403 {
406  u32 num_threads;
407 
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;
412 
413  /* Initialize [port-allocator] random number seed */
415 
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;
420  if (num_threads > 1)
421  clib_spinlock_init (&local_endpoints_lock);
422 }
423 
424 /*
425  * fd.io coding-style-patch-verification: ON
426  *
427  * Local Variables:
428  * eval: (c-set-style "gnu")
429  * End:
430  */
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:437
fib_protocol_t fp_proto
protocol type
Definition: fib_types.h:197
u32 transport_endpoint_lookup(transport_endpoint_table_t *ht, u8 proto, ip46_address_t *ip, u16 port)
Definition: transport.c:119
u8 * format_transport_proto_short(u8 *s, va_list *args)
Definition: transport.c:68
vnet_main_t * vnet_get_main(void)
Definition: misc.c:47
void transport_endpoint_table_add(transport_endpoint_table_t *ht, u8 proto, transport_endpoint_t *te, u32 value)
Definition: transport.c:137
#define PREDICT_TRUE(x)
Definition: clib.h:106
unsigned long u64
Definition: types.h:89
static transport_endpoint_table_t local_endpoints_table
Definition: transport.c:33
static_always_inline void clib_spinlock_unlock_if_init(clib_spinlock_t *p)
Definition: lock.h:98
struct _transport_proto_vft transport_proto_vft_t
static u64 clib_cpu_time_now(void)
Definition: time.h:73
format_function_t format_ip46_address
Definition: format.h:61
transport_tx_fn_type_t transport_protocol_tx_fn_type(transport_proto_t tp)
Definition: transport.c:205
void * ip_interface_get_first_ip(u32 sw_if_index, u8 is_ip4)
Definition: ip.c:133
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:419
#define pool_get(P, E)
Allocate an object E from a pool P (unspecified alignment).
Definition: pool.h:227
void transport_endpoint_del(u32 tepi)
Definition: transport.c:219
enum transport_service_type_ transport_service_type_t
format_function_t format_vnet_sw_if_index_name
unsigned char u8
Definition: types.h:56
void transport_update_time(f64 time_now, u8 thread_index)
Definition: transport.c:380
enum fib_protocol_t_ fib_protocol_t
Protocol Type.
double f64
Definition: types.h:142
connectionless service
#define always_inline
Definition: clib.h:92
Aggregrate type for a prefix.
Definition: fib_types.h:188
unsigned int u32
Definition: types.h:88
u16 fp_len
The mask length.
Definition: fib_types.h:192
static clib_spinlock_t local_endpoints_lock
Definition: transport.c:43
enum transport_dequeue_type_ transport_tx_fn_type_t
fib_node_index_t fib_table_lookup(u32 fib_index, const fib_prefix_t *prefix)
Perfom a longest prefix match in the non-forwarding table.
Definition: fib_table.c:66
static void clib_spinlock_init(clib_spinlock_t *p)
Definition: lock.h:57
transport_proto_vft_t * transport_protocol_get_vft(transport_proto_t transport_proto)
Get transport virtual function table.
Definition: transport.c:191
static session_manager_main_t * vnet_get_session_manager_main()
Definition: session.h:266
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:461
void transport_endpoint_table_del(transport_endpoint_table_t *ht, u8 proto, transport_endpoint_t *te)
Definition: transport.c:151
transport_service_type_t transport_protocol_service_type(transport_proto_t tp)
Definition: transport.c:199
ip46_address_t fp_addr
The address type is not deriveable from the fp_addr member.
Definition: fib_types.h:211
struct _unformat_input_t unformat_input_t
unsigned short u16
Definition: types.h:57
static transport_endpoint_t * local_endpoints
Definition: transport.c:38
transport_proto_vft_t * tp_vfts
Per-type vector of transport protocol virtual function tables.
Definition: transport.c:23
struct _session_manager_main session_manager_main_t
Definition: session.h:153
u32 fib_entry_get_resolving_interface(fib_node_index_t entry_index)
Definition: fib_entry.c:1409
void transport_init(void)
Definition: transport.c:402
static_always_inline uword vlib_get_thread_index(void)
Definition: threads.h:221
vlib_main_t * vm
Definition: buffer.c:294
clib_bihash_24_8_t transport_endpoint_table_t
Definition: transport.h:114
#define ENDPOINT_INVALID_INDEX
Definition: transport.h:116
#define clib_warning(format, args...)
Definition: error.h:59
#define PORT_MASK
Definition: transport.c:216
#define clib_memcpy(a, b, c)
Definition: string.h:75
u32 fib_node_index_t
A typedef of a node index.
Definition: fib_types.h:30
void transport_enable_disable(vlib_main_t *vm, u8 is_en)
Definition: transport.c:391
void transport_endpoint_cleanup(u8 proto, ip46_address_t *lcl_ip, u16 port)
Definition: transport.c:235
void transport_register_protocol(transport_proto_t transport_proto, const transport_proto_vft_t *vft, fib_protocol_t fib_proto, u32 output_node)
Register transport virtual function table.
Definition: transport.c:173
#define pool_put_index(p, i)
Free pool element with given index.
Definition: pool.h:296
#define ASSERT(truth)
uword unformat_transport_proto(unformat_input_t *input, va_list *args)
Definition: transport.c:90
static transport_endpoint_t * transport_endpoint_new(void)
Definition: transport.c:227
static u32 port_allocator_seed
Definition: transport.c:28
#define FIB_NODE_INDEX_INVALID
Definition: fib_types.h:31
enum _transport_proto transport_proto_t
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
struct _transport_endpoint transport_endpoint_t
u64 uword
Definition: types.h:112
static u32 random_u32(u32 *seed)
32-bit random number generator
Definition: random.h:69
static vlib_thread_main_t * vlib_get_thread_main()
Definition: global_funcs.h:32
#define vec_foreach(var, vec)
Vector iterator.
int transport_alloc_local_endpoint(u8 proto, transport_endpoint_t *rmt, ip46_address_t *lcl_addr, u16 *lcl_port)
Definition: transport.c:301
int transport_alloc_local_port(u8 proto, ip46_address_t *ip)
Allocate local port and add if successful add entry to local endpoint table to mark the pair as used...
Definition: transport.c:256
void session_register_transport(transport_proto_t transport_proto, const transport_proto_vft_t *vft, u8 is_ip4, u32 output_node)
Initialize session layer for given transport proto and ip version.
Definition: session.c:1238
static_always_inline void clib_spinlock_lock_if_init(clib_spinlock_t *p)
Definition: lock.h:82
u8 transport_protocol_is_cl(transport_proto_t tp)
Definition: transport.c:211
u8 * format_transport_proto(u8 *s, va_list *args)
Definition: transport.c:46
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:972