FD.io VPP  v18.10-34-gcce845e
Vector Packet Processing
application_interface.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016 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  */
16 
17 #include <vnet/session/session.h>
18 #include <vlibmemory/api.h>
19 #include <vnet/dpo/load_balance.h>
20 
21 /** @file
22  VPP's application/session API bind/unbind/connect/disconnect calls
23 */
24 
25 /*
26  * TLS server cert and keys to be used for testing only
27  */
28 const char test_srv_crt_rsa[] =
29  "-----BEGIN CERTIFICATE-----\r\n"
30  "MIID5zCCAs+gAwIBAgIJALeMYCEHrTtJMA0GCSqGSIb3DQEBCwUAMIGJMQswCQYD\r\n"
31  "VQQGEwJVUzELMAkGA1UECAwCQ0ExETAPBgNVBAcMCFNhbiBKb3NlMQ4wDAYDVQQK\r\n"
32  "DAVDaXNjbzEOMAwGA1UECwwFZmQuaW8xFjAUBgNVBAMMDXRlc3R0bHMuZmQuaW8x\r\n"
33  "IjAgBgkqhkiG9w0BCQEWE3ZwcC1kZXZAbGlzdHMuZmQuaW8wHhcNMTgwMzA1MjEx\r\n"
34  "NTEyWhcNMjgwMzAyMjExNTEyWjCBiTELMAkGA1UEBhMCVVMxCzAJBgNVBAgMAkNB\r\n"
35  "MREwDwYDVQQHDAhTYW4gSm9zZTEOMAwGA1UECgwFQ2lzY28xDjAMBgNVBAsMBWZk\r\n"
36  "LmlvMRYwFAYDVQQDDA10ZXN0dGxzLmZkLmlvMSIwIAYJKoZIhvcNAQkBFhN2cHAt\r\n"
37  "ZGV2QGxpc3RzLmZkLmlvMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA\r\n"
38  "4C1k8a1DuStgggqT4o09fP9sJ2dC54bxhS/Xk2VEfaIZ222WSo4X/syRVfVy9Yah\r\n"
39  "cpI1zJ/RDxaZSFhgA+nPZBrFMsrULkrdAOpOVj8eDEp9JuWdO2ODSoFnCvLxcYWB\r\n"
40  "Yc5kHryJpEaGJl1sFQSesnzMFty/59ta0stk0Fp8r5NhIjWvSovGzPo6Bhz+VS2c\r\n"
41  "ebIZh4x1t2hHaFcgm0qJoJ6DceReWCW8w+yOVovTolGGq+bpb2Hn7MnRSZ2K2NdL\r\n"
42  "+aLXpkZbS/AODP1FF2vTO1mYL290LO7/51vJmPXNKSDYMy5EvILr5/VqtjsFCwRL\r\n"
43  "Q4jcM/+GeHSAFWx4qIv0BwIDAQABo1AwTjAdBgNVHQ4EFgQUWa1SOB37xmT53tZQ\r\n"
44  "aXuLLhRI7U8wHwYDVR0jBBgwFoAUWa1SOB37xmT53tZQaXuLLhRI7U8wDAYDVR0T\r\n"
45  "BAUwAwEB/zANBgkqhkiG9w0BAQsFAAOCAQEAoUht13W4ya27NVzQuCMvqPWL3VM4\r\n"
46  "3xbPFk02FaGz/WupPu276zGlzJAZrbuDcQowwwU1Ni1Yygxl96s1c2M5rHDTrOKG\r\n"
47  "rK0hbkSFBo+i6I8u4HiiQ4rYmG0Hv6+sXn3of0HsbtDPGgWZoipPWDljPYEURu3e\r\n"
48  "3HRe/Dtsj9CakBoSDzs8ndWaBR+f4sM9Tk1cjD46Gq2T/qpSPXqKxEUXlzhdCAn4\r\n"
49  "twub17Bq2kykHpppCwPg5M+v30tHG/R2Go15MeFWbEJthFk3TZMjKL7UFs7fH+x2\r\n"
50  "wSonXb++jY+KmCb93C+soABBizE57g/KmiR2IxQ/LMjDik01RSUIaM0lLA==\r\n"
51  "-----END CERTIFICATE-----\r\n";
53 
54 const char test_srv_key_rsa[] =
55  "-----BEGIN PRIVATE KEY-----\r\n"
56  "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDgLWTxrUO5K2CC\r\n"
57  "CpPijT18/2wnZ0LnhvGFL9eTZUR9ohnbbZZKjhf+zJFV9XL1hqFykjXMn9EPFplI\r\n"
58  "WGAD6c9kGsUyytQuSt0A6k5WPx4MSn0m5Z07Y4NKgWcK8vFxhYFhzmQevImkRoYm\r\n"
59  "XWwVBJ6yfMwW3L/n21rSy2TQWnyvk2EiNa9Ki8bM+joGHP5VLZx5shmHjHW3aEdo\r\n"
60  "VyCbSomgnoNx5F5YJbzD7I5Wi9OiUYar5ulvYefsydFJnYrY10v5otemRltL8A4M\r\n"
61  "/UUXa9M7WZgvb3Qs7v/nW8mY9c0pINgzLkS8guvn9Wq2OwULBEtDiNwz/4Z4dIAV\r\n"
62  "bHioi/QHAgMBAAECggEBAMzGipP8+oT166U+NlJXRFifFVN1DvdhG9PWnOxGL+c3\r\n"
63  "ILmBBC08WQzmHshPemBvR6DZkA1H23cV5JTiLWrFtC00CvhXsLRMrE5+uWotI6yE\r\n"
64  "iofybMroHvD6/X5R510UX9hQ6MHu5ShLR5VZ9zXHz5MpTmB/60jG5dLx+jgcwBK8\r\n"
65  "LuGv2YB/WCUwT9QJ3YU2eaingnXtz/MrFbkbltrqlnBdlD+kTtw6Yac9y1XuuQXc\r\n"
66  "BPeulLNDuPolJVWbUvDBZrpt2dXTgz8ws1sv+wCNE0xwQJsqW4Nx3QkpibUL9RUr\r\n"
67  "CVbKlNfa9lopT6nGKlgX69R/uH35yh9AOsfasro6w0ECgYEA82UJ8u/+ORah+0sF\r\n"
68  "Q0FfW5MTdi7OAUHOz16pUsGlaEv0ERrjZxmAkHA/VRwpvDBpx4alCv0Hc39PFLIk\r\n"
69  "nhSsM2BEuBkTAs6/GaoNAiBtQVE/hN7awNRWVmlieS0go3Y3dzaE9IUMyj8sPOFT\r\n"
70  "5JdJ6BM69PHKCkY3dKdnnfpFEuECgYEA68mRpteunF1mdZgXs+WrN+uLlRrQR20F\r\n"
71  "ZyMYiUCH2Dtn26EzA2moy7FipIIrQcX/j+KhYNGM3e7MU4LymIO29E18mn8JODnH\r\n"
72  "sQOXzBTsf8A4yIVMkcuQD3bfb0JiUGYUPOidTp2N7IJA7+6Yc3vQOyb74lnKnJoO\r\n"
73  "gougPT2wS+cCgYAn7muzb6xFsXDhyW0Tm6YJYBfRS9yAWEuVufINobeBZPSl2cN1\r\n"
74  "Jrnw+HlrfTNbrJWuJmjtZJXUXQ6cVp2rUbjutNyRV4vG6iRwEXYQ40EJdkr1gZpi\r\n"
75  "CHQhuShuuPih2MNAy7EEbM+sXrDjTBR3bFqzuHPzu7dp+BshCFX3lRfAAQKBgGQt\r\n"
76  "K5i7IhCFDjb/+3IPLgOAK7mZvsvZ4eXD33TQ2eZgtut1PXtBtNl17/b85uv293Fm\r\n"
77  "VDISVcsk3eLNS8zIiT6afUoWlxAwXEs0v5WRfjl4radkGvgGiJpJYvyeM67877RB\r\n"
78  "EDSKc/X8ESLfOB44iGvZUEMG6zJFscx9DgN25iQZAoGAbyd+JEWwdVH9/K3IH1t2\r\n"
79  "PBkZX17kNWv+iVM1WyFjbe++vfKZCrOJiyiqhDeEqgrP3AuNMlaaduC3VRC3G5oV\r\n"
80  "Mj1tlhDWQ/qhvKdCKNdIVQYDE75nw+FRWV8yYkHAnXYW3tNoweDIwixE0hkPR1bc\r\n"
81  "oEjPLVNtx8SOj/M4rhaPT3I=\r\n" "-----END PRIVATE KEY-----\r\n";
83 
84 #define app_interface_check_thread_and_barrier(_fn, _arg) \
85  if (PREDICT_FALSE (!vlib_thread_is_main_w_barrier ())) \
86  { \
87  vlib_rpc_call_main_thread (_fn, (u8 *) _arg, sizeof(*_arg)); \
88  return 0; \
89  }
90 
91 static u8
93 {
94  return (ip_is_zero (&sep->ip, sep->is_ip4)
95  || ip_is_local_host (&sep->ip, sep->is_ip4));
96 }
97 
98 static u8
100 {
101  return ip_is_zero (&sep->ip, sep->is_ip4);
102 }
103 
104 u8
106 {
107  u8 is_lep = session_endpoint_is_local (sep);
108  if (!is_lep && sep->sw_if_index != ENDPOINT_INVALID_INDEX
109  && !ip_interface_has_address (sep->sw_if_index, &sep->ip, sep->is_ip4))
110  {
111  clib_warning ("sw_if_index %u not configured with ip %U",
112  sep->sw_if_index, format_ip46_address, &sep->ip,
113  sep->is_ip4);
114  return 0;
115  }
116  return (is_lep || ip_is_local (sep->fib_index, &sep->ip, sep->is_ip4));
117 }
118 
119 int
120 api_parse_session_handle (u64 handle, u32 * session_index, u32 * thread_index)
121 {
123  stream_session_t *pool;
124 
125  *thread_index = handle & 0xFFFFFFFF;
126  *session_index = handle >> 32;
127 
128  if (*thread_index >= vec_len (smm->sessions))
129  return VNET_API_ERROR_INVALID_VALUE;
130 
131  pool = smm->sessions[*thread_index];
132 
133  if (pool_is_free_index (pool, *session_index))
134  return VNET_API_ERROR_INVALID_VALUE_2;
135 
136  return 0;
137 }
138 
139 static void
141  application_t * app)
142 {
143  app_namespace_t *app_ns;
144  u32 ns_index;
145 
146  ns_index = app->ns_index;
147 
148  /* App is a transport proto, so fetch the calling app's ns */
149  if (app->flags & APP_OPTIONS_FLAGS_IS_TRANSPORT_APP)
150  {
151  app_worker_t *owner_wrk;
152  application_t *owner_app;
153 
154  owner_wrk = app_worker_get (sep->app_wrk_index);
155  owner_app = application_get (owner_wrk->app_index);
156  ns_index = owner_app->ns_index;
157  }
158  app_ns = app_namespace_get (ns_index);
159  if (app_ns)
160  {
161  /* Ask transport and network to bind to/connect using local interface
162  * that "supports" app's namespace. This will fix our local connection
163  * endpoint.
164  */
165  sep->sw_if_index = app_ns->sw_if_index;
166  sep->fib_index =
167  sep->is_ip4 ? app_ns->ip4_fib_index : app_ns->ip6_fib_index;
168  }
169 }
170 
171 static inline int
173 {
174  u64 ll_handle = SESSION_INVALID_HANDLE;
175  app_worker_t *app_wrk;
176  application_t *app;
177  int rv;
178 
179  app = application_get_if_valid (a->app_index);
180  if (!app)
181  {
182  SESSION_DBG ("app not attached");
183  return VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
184  }
185  app_wrk = application_get_worker (app, a->wrk_map_index);
186  a->sep_ext.app_wrk_index = app_wrk->wrk_index;
187 
188  session_endpoint_update_for_app (&a->sep_ext, app);
189  if (!session_endpoint_in_ns (&a->sep))
190  return VNET_API_ERROR_INVALID_VALUE_2;
191 
192  /*
193  * Add session endpoint to local session table. Only binds to "inaddr_any"
194  * (i.e., zero address) are added to local scope table.
195  */
197  && session_endpoint_is_local (&a->sep))
198  {
199  if ((rv = application_start_local_listen (app, &a->sep_ext,
200  &a->handle)))
201  return rv;
202  ll_handle = a->handle;
203  }
204 
205  if (!application_has_global_scope (app))
206  return (ll_handle == SESSION_INVALID_HANDLE ? -1 : 0);
207 
208  /*
209  * Add session endpoint to global session table
210  */
211 
212  /* Setup listen path down to transport */
213  rv = application_start_listen (app, &a->sep_ext, &a->handle);
214  if (rv && ll_handle != SESSION_INVALID_HANDLE)
215  {
216  application_stop_local_listen (a->app_index, a->wrk_map_index,
217  ll_handle);
218  return rv;
219  }
220 
221  /*
222  * Store in local table listener the index of the transport layer
223  * listener. We'll need if if local listeners are hit and we need to
224  * return global handle
225  */
226  if (ll_handle != SESSION_INVALID_HANDLE)
227  {
228  local_session_t *ll;
229  stream_session_t *tl;
231  tl = listen_session_get_from_handle (a->handle);
232  if (ll->transport_listener_index == ~0)
233  ll->transport_listener_index = tl->session_index;
234  }
235  return rv;
236 }
237 
238 static inline int
240 {
241  application_t *app;
242  int rv;
243 
244  if (!(app = application_get_if_valid (a->app_index)))
245  {
246  SESSION_DBG ("app (%d) not attached", wrk_map_index);
247  return VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
248  }
249 
250  if (application_has_local_scope (app))
251  {
252  if ((rv = application_stop_local_listen (a->app_index,
253  a->wrk_map_index, a->handle)))
254  return rv;
255  }
256 
257  /*
258  * Clear the global scope table of the listener
259  */
261  return application_stop_listen (a->app_index, a->wrk_map_index,
262  a->handle);
263  return 0;
264 }
265 
266 static int
268 {
269  app_worker_t *server_wrk, *client_wrk;
270  u32 table_index, server_index, li;
271  stream_session_t *listener;
272  application_t *client, *server;
273  local_session_t *ll;
274  u8 fib_proto;
275  u64 lh;
276 
277  if (session_endpoint_is_zero (&a->sep))
278  return VNET_API_ERROR_INVALID_VALUE;
279 
280  client = application_get (a->app_index);
281  session_endpoint_update_for_app (&a->sep_ext, client);
282  client_wrk = application_get_worker (client, a->wrk_map_index);
283 
284  /*
285  * First check the local scope for locally attached destinations.
286  * If we have local scope, we pass *all* connects through it since we may
287  * have special policy rules even for non-local destinations, think proxy.
288  */
289  if (application_has_local_scope (client))
290  {
291  table_index = application_local_session_table (client);
292  lh = session_lookup_local_endpoint (table_index, &a->sep);
293  if (lh == SESSION_DROP_HANDLE)
294  return VNET_API_ERROR_APP_CONNECT_FILTERED;
295 
296  if (lh == SESSION_INVALID_HANDLE)
297  goto global_scope;
298 
299  local_session_parse_handle (lh, &server_index, &li);
300 
301  /*
302  * Break loop if rule in local table points to connecting app. This
303  * can happen if client is a generic proxy. Route connect through
304  * global table instead.
305  */
306  if (server_index != a->app_index)
307  {
308  server = application_get (server_index);
309  ll = application_get_local_listen_session (server, li);
310  listener = (stream_session_t *) ll;
311  server_wrk = application_listener_select_worker (listener,
312  1 /* is_local */ );
313  return application_local_session_connect (client_wrk,
314  server_wrk, ll,
315  a->api_context);
316  }
317  }
318 
319  /*
320  * If nothing found, check the global scope for locally attached
321  * destinations. Make sure first that we're allowed to.
322  */
323 
324 global_scope:
325  if (session_endpoint_is_local (&a->sep))
326  return VNET_API_ERROR_SESSION_CONNECT;
327 
328  if (!application_has_global_scope (client))
329  return VNET_API_ERROR_APP_CONNECT_SCOPE;
330 
331  fib_proto = session_endpoint_fib_proto (&a->sep);
332  table_index = application_session_table (client, fib_proto);
333  listener = session_lookup_listener (table_index, &a->sep);
334  if (listener)
335  {
336  server_wrk = application_listener_select_worker (listener,
337  0 /* is_local */ );
338  ll = (local_session_t *) listener;
339  return application_local_session_connect (client_wrk, server_wrk, ll,
340  a->api_context);
341  }
342 
343  /*
344  * Not connecting to a local server, propagate to transport
345  */
346  if (app_worker_open_session (client_wrk, &a->sep, a->api_context))
347  return VNET_API_ERROR_SESSION_CONNECT;
348  return 0;
349 }
350 
351 /**
352  * unformat a vnet URI
353  *
354  * transport-proto://[hostname]ip46-addr:port
355  * eg. tcp://ip46-addr:port
356  * tls://[testtsl.fd.io]ip46-addr:port
357  *
358  * u8 ip46_address[16];
359  * u16 port_in_host_byte_order;
360  * stream_session_type_t sst;
361  * u8 *fifo_name;
362  *
363  * if (unformat (input, "%U", unformat_vnet_uri, &ip46_address,
364  * &sst, &port, &fifo_name))
365  * etc...
366  *
367  */
368 uword
369 unformat_vnet_uri (unformat_input_t * input, va_list * args)
370 {
371  session_endpoint_extended_t *sep = va_arg (*args,
373  u32 transport_proto = 0, port;
374 
375  if (unformat (input, "%U://%U/%d", unformat_transport_proto,
376  &transport_proto, unformat_ip4_address, &sep->ip.ip4, &port))
377  {
378  sep->transport_proto = transport_proto;
379  sep->port = clib_host_to_net_u16 (port);
380  sep->is_ip4 = 1;
381  return 1;
382  }
383  else if (unformat (input, "%U://[%s]%U/%d", unformat_transport_proto,
384  &transport_proto, &sep->hostname, unformat_ip4_address,
385  &sep->ip.ip4, &port))
386  {
387  sep->transport_proto = transport_proto;
388  sep->port = clib_host_to_net_u16 (port);
389  sep->is_ip4 = 1;
390  return 1;
391  }
392  else if (unformat (input, "%U://%U/%d", unformat_transport_proto,
393  &transport_proto, unformat_ip6_address, &sep->ip.ip6,
394  &port))
395  {
396  sep->transport_proto = transport_proto;
397  sep->port = clib_host_to_net_u16 (port);
398  sep->is_ip4 = 0;
399  return 1;
400  }
401  else if (unformat (input, "%U://[%s]%U/%d", unformat_transport_proto,
402  &transport_proto, &sep->hostname, unformat_ip6_address,
403  &sep->ip.ip6, &port))
404  {
405  sep->transport_proto = transport_proto;
406  sep->port = clib_host_to_net_u16 (port);
407  sep->is_ip4 = 0;
408  return 1;
409  }
410  return 0;
411 }
412 
413 static u8 *cache_uri;
415 
416 int
418 {
419  unformat_input_t _input, *input = &_input;
420 
421  if (cache_uri && !strncmp (uri, (char *) cache_uri, vec_len (cache_uri)))
422  {
423  *sep = *cache_sep;
424  return 0;
425  }
426 
427  /* Make sure */
428  uri = (char *) format (0, "%s%c", uri, 0);
429 
430  /* Parse uri */
431  unformat_init_string (input, uri, strlen (uri));
432  if (!unformat (input, "%U", unformat_vnet_uri, sep))
433  {
434  unformat_free (input);
435  return VNET_API_ERROR_INVALID_VALUE;
436  }
437  unformat_free (input);
438 
440  cache_uri = (u8 *) uri;
441  if (cache_sep)
443  cache_sep = clib_mem_alloc (sizeof (*sep));
444  *cache_sep = *sep;
445 
446  return 0;
447 }
448 
449 static int
450 app_validate_namespace (u8 * namespace_id, u64 secret, u32 * app_ns_index)
451 {
452  app_namespace_t *app_ns;
453  if (vec_len (namespace_id) == 0)
454  {
455  /* Use default namespace */
456  *app_ns_index = 0;
457  return 0;
458  }
459 
460  *app_ns_index = app_namespace_index_from_id (namespace_id);
461  if (*app_ns_index == APP_NAMESPACE_INVALID_INDEX)
462  return VNET_API_ERROR_APP_INVALID_NS;
463  app_ns = app_namespace_get (*app_ns_index);
464  if (!app_ns)
465  return VNET_API_ERROR_APP_INVALID_NS;
466  if (app_ns->ns_secret != secret)
467  return VNET_API_ERROR_APP_WRONG_NS_SECRET;
468  return 0;
469 }
470 
471 /**
472  * Attach application to vpp
473  *
474  * Allocates a vpp app, i.e., a structure that keeps back pointers
475  * to external app and a segment manager for shared memory fifo based
476  * communication with the external app.
477  */
478 clib_error_t *
480 {
482  application_t *app = 0;
483  app_worker_t *app_wrk;
484  segment_manager_t *sm;
485  u32 app_ns_index = 0;
486  u64 secret;
487  int rv;
488 
489  if (a->api_client_index != APP_INVALID_INDEX)
490  app = application_lookup (a->api_client_index);
491  else if (a->name)
492  app = application_lookup_name (a->name);
493  else
494  return clib_error_return_code (0, VNET_API_ERROR_INVALID_VALUE, 0,
495  "api index or name must be provided");
496 
497  if (app)
498  return clib_error_return_code (0, VNET_API_ERROR_APP_ALREADY_ATTACHED, 0,
499  "app already attached");
500 
501  secret = a->options[APP_OPTIONS_NAMESPACE_SECRET];
502  if ((rv = app_validate_namespace (a->namespace_id, secret, &app_ns_index)))
503  return clib_error_return_code (0, rv, 0, "namespace validation: %d", rv);
504  a->options[APP_OPTIONS_NAMESPACE] = app_ns_index;
505 
506  if ((rv = application_alloc_and_init ((app_init_args_t *) a)))
507  return clib_error_return_code (0, rv, 0, "app init: %d", rv);
508 
509  app = application_get (a->app_index);
510  if ((rv = app_worker_alloc_and_init (app, &app_wrk)))
511  return clib_error_return_code (0, rv, 0, "app default wrk init: %d", rv);
512 
513  a->app_evt_q = app_wrk->event_queue;
516 
517  if (application_is_proxy (app))
519 
520  ASSERT (vec_len (fs->ssvm.name) <= 128);
521  a->segment = &fs->ssvm;
522 
524 
525  return 0;
526 }
527 
528 /**
529  * Detach application from vpp
530  */
531 int
533 {
534  application_t *app;
535 
536  app = application_get_if_valid (a->app_index);
537  if (!app)
538  {
539  clib_warning ("app not attached");
540  return VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
541  }
542 
544  application_free (app);
545  return 0;
546 }
547 
548 int
550 {
552  int rv;
553 
554  rv = parse_uri (a->uri, &sep);
555  if (rv)
556  return rv;
557  sep.app_wrk_index = 0;
558  clib_memcpy (&a->sep_ext, &sep, sizeof (sep));
559  return vnet_bind_inline (a);
560 }
561 
562 int
564 {
566  stream_session_t *listener;
567  u32 table_index;
568  int rv;
569 
570  rv = parse_uri (a->uri, &sep);
571  if (rv)
572  return rv;
573 
574  /* NOTE: only default fib tables supported for uri apis */
575  table_index = session_lookup_get_index_for_fib (fib_ip_proto (!sep.is_ip4),
576  0);
577  listener = session_lookup_listener (table_index,
578  (session_endpoint_t *) & sep);
579  if (!listener)
580  return VNET_API_ERROR_ADDRESS_NOT_IN_USE;
581  a->handle = listen_session_get_handle (listener);
582  return vnet_unbind_inline (a);
583 }
584 
585 clib_error_t *
587 {
589  int rv;
590 
591  /* Parse uri */
592  rv = parse_uri (a->uri, &sep);
593  if (rv)
594  return clib_error_return_code (0, rv, 0, "app init: %d", rv);
595 
596  clib_memcpy (&a->sep_ext, &sep, sizeof (sep));
597  if ((rv = application_connect (a)))
598  return clib_error_return_code (0, rv, 0, "connect failed");
599  return 0;
600 }
601 
602 int
604 {
605  if (session_handle_is_local (a->handle))
606  {
607  local_session_t *ls;
608 
609  /* Disconnect reply came to worker 1 not main thread */
611 
612  if (!(ls = application_get_local_session_from_handle (a->handle)))
613  return 0;
614 
615  return application_local_session_disconnect (a->app_index, ls);
616  }
617  else
618  {
619  app_worker_t *app_wrk;
620  stream_session_t *s;
621 
622  s = session_get_from_handle_if_valid (a->handle);
623  if (!s)
624  return VNET_API_ERROR_INVALID_VALUE;
625  app_wrk = app_worker_get (s->app_wrk_index);
626  if (app_wrk->app_index != a->app_index)
627  return VNET_API_ERROR_INVALID_VALUE;
628 
629  /* We're peeking into another's thread pool. Make sure */
630  ASSERT (s->session_index == session_index_from_handle (a->handle));
631 
633  }
634  return 0;
635 }
636 
637 clib_error_t *
639 {
640  int rv;
641  if ((rv = vnet_bind_inline (a)))
642  return clib_error_return_code (0, rv, 0, "bind failed: %d", rv);
643  return 0;
644 }
645 
646 clib_error_t *
648 {
649  int rv;
650  if ((rv = vnet_unbind_inline (a)))
651  return clib_error_return_code (0, rv, 0, "unbind failed: %d", rv);
652  return 0;
653 }
654 
655 clib_error_t *
657 {
658  int rv;
659 
660  if ((rv = application_connect (a)))
661  return clib_error_return_code (0, rv, 0, "connect failed: %d", rv);
662  return 0;
663 }
664 
665 /*
666  * fd.io coding-style-patch-verification: ON
667  *
668  * Local Variables:
669  * eval: (c-set-style "gnu")
670  * End:
671  */
#define SESSION_DROP_HANDLE
Definition: session_table.h:60
u32 flags
Flags.
Definition: application.h:128
u8 session_endpoint_in_ns(session_endpoint_t *sep)
int application_stop_listen(u32 app_index, u32 app_wrk_index, session_handle_t handle)
Stop listening on session associated to handle.
Definition: application.c:799
app_worker_t * app_worker_get(u32 wrk_index)
Definition: application.c:499
#define APP_NAMESPACE_INVALID_INDEX
void segment_manager_segment_reader_unlock(segment_manager_t *sm)
static int app_validate_namespace(u8 *namespace_id, u64 secret, u32 *app_ns_index)
static session_endpoint_extended_t * cache_sep
int app_worker_alloc_and_init(application_t *app, app_worker_t **wrk)
Definition: application.c:579
static int application_connect(vnet_connect_args_t *a)
a
Definition: bitmap.h:538
const u32 test_srv_crt_rsa_len
static int vnet_unbind_inline(vnet_unbind_args_t *a)
u32 ns_index
Namespace the application belongs to.
Definition: application.h:143
static u8 session_endpoint_is_local(session_endpoint_t *sep)
u8 application_has_global_scope(application_t *app)
Definition: application.c:970
struct _vnet_connect_args vnet_connect_args_t
int vnet_bind_uri(vnet_bind_args_t *a)
const u32 test_srv_key_rsa_len
u8 ip_is_local(u32 fib_index, ip46_address_t *ip46_address, u8 is_ip4)
Checks that an ip is local to the requested fib.
Definition: ip.c:55
unsigned long u64
Definition: types.h:89
clib_error_t * vnet_unbind(vnet_unbind_args_t *a)
app_worker_t * application_listener_select_worker(stream_session_t *ls, u8 is_local)
Definition: application.c:460
application_t * application_lookup_name(const u8 *name)
Definition: application.c:222
u8 ip_interface_has_address(u32 sw_if_index, ip46_address_t *ip, u8 is_ip4)
Definition: ip.c:99
u32 session_lookup_get_index_for_fib(u32 fib_proto, u32 fib_index)
local_session_t * application_get_local_session_from_handle(session_handle_t handle)
Definition: application.c:1374
int application_local_session_connect(app_worker_t *client_wrk, app_worker_t *server_wrk, local_session_t *ll, u32 opaque)
Definition: application.c:1562
application_t * application_lookup(u32 api_client_index)
Definition: application.c:211
void application_free(application_t *app)
Definition: application.c:388
format_function_t format_ip46_address
Definition: format.h:61
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:419
static u8 * cache_uri
static u32 session_index_from_handle(session_handle_t handle)
Definition: session.h:340
#define SESSION_ENDPOINT_EXT_NULL
unsigned char u8
Definition: types.h:56
stream_session_t * session_lookup_listener(u32 table_index, session_endpoint_t *sep)
Lookup listener, exact or proxy (inaddr_any:0) match.
static stream_session_t * listen_session_get_from_handle(session_handle_t handle)
Definition: session.h:606
app_worker_t * application_get_worker(application_t *app, u32 wrk_map_index)
Definition: application.c:444
application_t * application_get_if_valid(u32 app_index)
Definition: application.c:251
int application_is_proxy(application_t *app)
Definition: application.c:946
int vnet_unbind_uri(vnet_unbind_args_t *a)
unformat_function_t unformat_ip4_address
Definition: format.h:70
u32 first_segment_manager
First segment manager has in the the first segment the application&#39;s event fifo.
Definition: application.h:88
struct _vnet_disconnect_args_t vnet_disconnect_args_t
u32 transport_listener_index
Transport listener index.
struct _vnet_unbind_args_t vnet_unbind_args_t
svm_fifo_segment_private_t * segment_manager_get_segment_w_lock(segment_manager_t *sm, u32 segment_index)
Reads a segment from the segment manager&#39;s pool and acquires reader lock.
unsigned int u32
Definition: types.h:88
static local_session_t * application_get_local_listen_session(application_t *app, u32 session_index)
Definition: application.h:357
u32 app_namespace_index_from_id(const u8 *ns_id)
struct _stream_session_t stream_session_t
void unformat_init_string(unformat_input_t *input, char *string, int string_len)
Definition: unformat.c:1023
struct _vnet_app_attach_args_t vnet_app_attach_args_t
static session_manager_main_t * vnet_get_session_manager_main()
Definition: session.h:289
struct _session_endpoint session_endpoint_t
static void session_endpoint_update_for_app(session_endpoint_extended_t *sep, application_t *app)
int application_start_listen(application_t *app, session_endpoint_extended_t *sep_ext, session_handle_t *res)
Start listening local transport endpoint for requested transport.
Definition: application.c:717
static u8 session_handle_is_local(session_handle_t handle)
Definition: session.h:377
struct _unformat_input_t unformat_input_t
static int vnet_bind_inline(vnet_bind_args_t *a)
#define APP_INVALID_INDEX
Definition: application.h:218
app_namespace_t * app_namespace_get(u32 index)
clib_error_t * vnet_connect(vnet_connect_args_t *a)
u32 wrk_index
Worker index in global worker pool.
Definition: application.h:66
u32 application_session_table(application_t *app, u8 fib_proto)
Definition: application.c:124
struct _session_manager_main session_manager_main_t
Definition: session.h:176
int parse_uri(char *uri, session_endpoint_extended_t *sep)
const char test_srv_crt_rsa[]
unformat_function_t unformat_ip6_address
Definition: format.h:91
#define SESSION_DBG(_fmt, _args...)
u64 session_lookup_local_endpoint(u32 table_index, session_endpoint_t *sep)
Look up endpoint in local session table.
u32 application_local_session_table(application_t *app)
Definition: application.c:139
#define app_interface_check_thread_and_barrier(_fn, _arg)
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:339
#define ENDPOINT_INVALID_INDEX
Definition: transport.h:116
#define clib_warning(format, args...)
Definition: error.h:59
#define SESSION_INVALID_HANDLE
Definition: session_table.h:59
#define clib_memcpy(a, b, c)
Definition: string.h:75
#define pool_is_free_index(P, I)
Use free bitmap to query whether given index is free.
Definition: pool.h:271
int vnet_disconnect_session(vnet_disconnect_args_t *a)
int api_parse_session_handle(u64 handle, u32 *session_index, u32 *thread_index)
struct _app_namespace app_namespace_t
application_t * application_get(u32 app_index)
Definition: application.c:243
static stream_session_t * session_get_from_handle_if_valid(session_handle_t handle)
Definition: session.h:369
#define ASSERT(truth)
clib_error_t * vnet_bind(vnet_bind_args_t *a)
uword unformat_transport_proto(unformat_input_t *input, va_list *args)
Definition: transport.c:90
static local_session_t * application_get_local_listener_w_handle(session_handle_t handle)
Definition: application.h:363
static u8 session_endpoint_fib_proto(session_endpoint_t *sep)
int application_start_local_listen(application_t *app, session_endpoint_extended_t *sep_ext, session_handle_t *handle)
Definition: application.c:1410
static void clib_mem_free(void *p)
Definition: mem.h:205
static u8 session_endpoint_is_zero(session_endpoint_t *sep)
void stream_session_disconnect(stream_session_t *s)
Initialize session disconnect.
Definition: session.c:1066
int application_local_session_disconnect(u32 app_index, local_session_t *ls)
Definition: application.c:1765
struct _vnet_app_detach_args_t vnet_app_detach_args_t
static void * clib_mem_alloc(uword size)
Definition: mem.h:132
int application_alloc_and_init(app_init_args_t *a)
Definition: application.c:306
u8 * name
Definition: ssvm.h:86
u8 ip_is_zero(ip46_address_t *ip46_address, u8 is_ip4)
Definition: ip.c:20
uword unformat_vnet_uri(unformat_input_t *input, va_list *args)
unformat a vnet URI
clib_error_t * vnet_connect_uri(vnet_connect_args_t *a)
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
clib_error_t * vnet_application_attach(vnet_app_attach_args_t *a)
Attach application to vpp.
static fib_protocol_t fib_ip_proto(bool is_ip6)
Convert from boolean is_ip6 to FIB protocol.
Definition: fib_types.h:80
u64 uword
Definition: types.h:112
static void unformat_free(unformat_input_t *i)
Definition: format.h:162
u8 ip_is_local_host(ip46_address_t *ip46_address, u8 is_ip4)
Definition: ip.c:29
int app_worker_open_session(app_worker_t *app, session_endpoint_t *sep, u32 api_context)
Definition: application.c:837
struct _segment_manager segment_manager_t
u32 app_index
Index of owning app.
Definition: application.h:72
int application_stop_local_listen(u32 app_index, u32 wrk_map_index, session_handle_t lh)
Clean up local session table.
Definition: application.c:1472
#define clib_error_return_code(e, code, flags, args...)
Definition: error.h:93
void application_setup_proxy(application_t *app)
Definition: application.c:1153
svm_msg_q_t * event_queue
Application listens for events on this svm queue.
Definition: application.h:75
int vnet_application_detach(vnet_app_detach_args_t *a)
Detach application from vpp.
struct _vnet_bind_args_t vnet_bind_args_t
static segment_manager_t * segment_manager_get(u32 index)
static u64 listen_session_get_handle(stream_session_t *s)
Definition: session.h:599
struct _session_endpoint_extended session_endpoint_extended_t
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:972
static void local_session_parse_handle(session_handle_t handle, u32 *app_or_wrk_index, u32 *session_index)
Definition: application.h:340
const char test_srv_key_rsa[]
u8 application_has_local_scope(application_t *app)
Definition: application.c:964