FD.io VPP  v18.04-17-g3a0d853
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;
60  }
61  return s;
62 }
63 
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  {
71  s = format (s, "T");
72  break;
74  s = format (s, "U");
75  break;
77  s = format (s, "S");
78  break;
79  }
80  return s;
81 }
82 
83 uword
84 unformat_transport_proto (unformat_input_t * input, va_list * args)
85 {
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;
103  else
104  return 0;
105  return 1;
106 }
107 
108 u32
110  ip46_address_t * ip, u16 port)
111 {
113  int rv;
114 
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;
118 
119  rv = clib_bihash_search_inline_24_8 (ht, &kv);
120  if (rv == 0)
121  return kv.value;
122 
123  return ENDPOINT_INVALID_INDEX;
124 }
125 
126 void
128  transport_endpoint_t * te, u32 value)
129 {
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  kv.value = value;
136 
137  clib_bihash_add_del_24_8 (ht, &kv, 1);
138 }
139 
140 void
143 {
145 
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;
149 
150  clib_bihash_add_del_24_8 (ht, &kv, 0);
151 }
152 
153 /**
154  * Register transport virtual function table.
155  *
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
161  */
162 void
164  const transport_proto_vft_t * vft,
165  fib_protocol_t fib_proto, u32 output_node)
166 {
167  u8 is_ip4 = fib_proto == FIB_PROTOCOL_IP4;
168 
169  vec_validate (tp_vfts, transport_proto);
170  tp_vfts[transport_proto] = *vft;
171 
172  session_register_transport (transport_proto, vft, is_ip4, output_node);
173 }
174 
175 /**
176  * Get transport virtual function table
177  *
178  * @param type - session type (not protocol type)
179  */
182 {
183  if (transport_proto >= vec_len (tp_vfts))
184  return 0;
185  return &tp_vfts[transport_proto];
186 }
187 
188 #define PORT_MASK ((1 << 16)- 1)
189 
190 void
192 {
193  clib_spinlock_lock_if_init (&local_endpoints_lock);
195  clib_spinlock_unlock_if_init (&local_endpoints_lock);
196 }
197 
200 {
202  pool_get (local_endpoints, tep);
203  return tep;
204 }
205 
206 void
207 transport_endpoint_cleanup (u8 proto, ip46_address_t * lcl_ip, u16 port)
208 {
209  u32 tepi;
211 
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)
216  {
217  tep = pool_elt_at_index (local_endpoints, tepi);
219  transport_endpoint_del (tepi);
220  }
221 }
222 
223 /**
224  * Allocate local port and add if successful add entry to local endpoint
225  * table to mark the pair as used.
226  */
227 int
228 transport_alloc_local_port (u8 proto, ip46_address_t * ip)
229 {
231  u32 tei;
232  u16 min = 1024, max = 65535; /* XXX configurable ? */
233  int tries, limit;
234 
235  limit = max - min;
236 
237  /* Only support active opens from thread 0 */
238  ASSERT (vlib_get_thread_index () == 0);
239 
240  /* Search for first free slot */
241  for (tries = 0; tries < limit; tries++)
242  {
243  u16 port = 0;
244 
245  /* Find a port in the specified range */
246  while (1)
247  {
249  if (PREDICT_TRUE (port >= min && port < max))
250  break;
251  }
252 
253  /* Look it up. If not found, we're done */
255  port);
256  if (tei == ENDPOINT_INVALID_INDEX)
257  {
258  clib_spinlock_lock_if_init (&local_endpoints_lock);
259  tep = transport_endpoint_new ();
260  clib_memcpy (&tep->ip, ip, sizeof (*ip));
261  tep->port = port;
263  tep - local_endpoints);
264  clib_spinlock_unlock_if_init (&local_endpoints_lock);
265 
266  return tep->port;
267  }
268  }
269  return -1;
270 }
271 
272 int
274  ip46_address_t * lcl_addr, u16 * lcl_port)
275 {
276  fib_prefix_t prefix;
277  fib_node_index_t fei;
278  u32 sw_if_index;
279  int port;
280 
281  /*
282  * Find the local address and allocate port
283  */
284 
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;
289 
290  ASSERT (rmt->fib_index != ENDPOINT_INVALID_INDEX);
291  fei = fib_table_lookup (rmt->fib_index, &prefix);
292 
293  /* Couldn't find route to destination. Bail out. */
294  if (fei == FIB_NODE_INDEX_INVALID)
295  {
296  clib_warning ("no route to destination");
297  return -1;
298  }
299 
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);
303 
304  if (sw_if_index == ENDPOINT_INVALID_INDEX)
305  {
306  clib_warning ("no resolving interface for %U", format_ip46_address,
307  &rmt->ip, (rmt->is_ip4 == 0) + 1);
308  return -1;
309  }
310 
311  memset (lcl_addr, 0, sizeof (*lcl_addr));
312 
313  if (rmt->is_ip4)
314  {
315  ip4_address_t *ip4;
316  ip4 = ip_interface_get_first_ip (sw_if_index, 1);
317  if (!ip4)
318  {
319  clib_warning ("no routable ip4 address on %U",
321  sw_if_index);
322  return -1;
323  }
324  lcl_addr->ip4.as_u32 = ip4->as_u32;
325  }
326  else
327  {
328  ip6_address_t *ip6;
329  ip6 = ip_interface_get_first_ip (sw_if_index, 0);
330  if (ip6 == 0)
331  {
332  clib_warning ("no routable ip6 addresses on %U",
334  sw_if_index);
335  return -1;
336  }
337  clib_memcpy (&lcl_addr->ip6, ip6, sizeof (*ip6));
338  }
339 
340  /* Allocate source port */
341  port = transport_alloc_local_port (proto, lcl_addr);
342  if (port < 1)
343  {
344  clib_warning ("Failed to allocate src port");
345  return -1;
346  }
347  *lcl_port = port;
348  return 0;
349 }
350 
351 void
352 transport_update_time (f64 time_now, u8 thread_index)
353 {
355  vec_foreach (vft, tp_vfts)
356  {
357  if (vft->update_time)
358  (vft->update_time) (time_now, thread_index);
359  }
360 }
361 
362 void
364 {
366  vec_foreach (vft, tp_vfts)
367  {
368  if (vft->enable)
369  (vft->enable) (vm, is_en);
370  }
371 }
372 
373 void
375 {
378  u32 num_threads;
379 
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;
384 
385  /* Initialize [port-allocator] random number seed */
387 
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;
392  if (num_threads > 1)
393  clib_spinlock_init (&local_endpoints_lock);
394 }
395 
396 /*
397  * fd.io coding-style-patch-verification: ON
398  *
399  * Local Variables:
400  * eval: (c-set-style "gnu")
401  * End:
402  */
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:434
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:109
u8 * format_transport_proto_short(u8 *s, va_list *args)
Definition: transport.c:65
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:127
#define PREDICT_TRUE(x)
Definition: clib.h:106
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
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:191
format_function_t format_vnet_sw_if_index_name
void transport_update_time(f64 time_now, u8 thread_index)
Definition: transport.c:352
enum fib_protocol_t_ fib_protocol_t
Protocol Type.
#define always_inline
Definition: clib.h:92
Aggregrate type for a prefix.
Definition: fib_types.h:188
unsigned long u64
Definition: types.h:89
u16 fp_len
The mask length.
Definition: fib_types.h:192
static clib_spinlock_t local_endpoints_lock
Definition: transport.c:43
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:181
static session_manager_main_t * vnet_get_session_manager_main()
Definition: session.h:217
#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:141
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
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:107
u32 fib_entry_get_resolving_interface(fib_node_index_t entry_index)
Definition: fib_entry.c:1409
void transport_init(void)
Definition: transport.c:374
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:113
#define ENDPOINT_INVALID_INDEX
Definition: transport.h:115
#define clib_warning(format, args...)
Definition: error.h:59
#define PORT_MASK
Definition: transport.c:188
#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:363
void transport_endpoint_cleanup(u8 proto, ip46_address_t *lcl_ip, u16 port)
Definition: transport.c:207
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:163
#define pool_put_index(p, i)
Free pool element with given index.
Definition: pool.h:296
#define ASSERT(truth)
unsigned int u32
Definition: types.h:88
uword unformat_transport_proto(unformat_input_t *input, va_list *args)
Definition: transport.c:84
static transport_endpoint_t * transport_endpoint_new(void)
Definition: transport.c:199
u64 uword
Definition: types.h:112
static u32 port_allocator_seed
Definition: transport.c:28
unsigned short u16
Definition: types.h:57
#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)
double f64
Definition: types.h:142
unsigned char u8
Definition: types.h:56
struct _transport_endpoint transport_endpoint_t
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:273
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:228
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:1142
static_always_inline void clib_spinlock_lock_if_init(clib_spinlock_t *p)
Definition: lock.h:82
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