vcl: wait for segments with segment handle
[vpp.git] / src / vnet / session / application_interface.c
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  */
15 #include <vnet/session/application_interface.h>
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";
52 const u32 test_srv_crt_rsa_len = sizeof (test_srv_crt_rsa);
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";
82 const u32 test_srv_key_rsa_len = sizeof (test_srv_key_rsa);
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
92 session_endpoint_is_local (session_endpoint_t * sep)
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
99 session_endpoint_is_zero (session_endpoint_t * sep)
100 {
101   return ip_is_zero (&sep->ip, sep->is_ip4);
102 }
103
104 u8
105 session_endpoint_in_ns (session_endpoint_t * sep)
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 {
122   session_manager_main_t *smm = vnet_get_session_manager_main ();
123   stream_session_t *pool;
124
125   *thread_index = handle & 0xFFFFFFFF;
126   *session_index = handle >> 32;
127
128   if (*thread_index >= vec_len (smm->wrk))
129     return VNET_API_ERROR_INVALID_VALUE;
130
131   pool = smm->wrk[*thread_index].sessions;
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
140 session_endpoint_update_for_app (session_endpoint_cfg_t * sep,
141                                  application_t * app, u8 is_connect)
142 {
143   app_namespace_t *app_ns;
144   u32 ns_index, fib_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     return;
161
162   /* Ask transport and network to bind to/connect using local interface
163    * that "supports" app's namespace. This will fix our local connection
164    * endpoint.
165    */
166
167   /* If in default namespace and user requested a fib index use it */
168   if (ns_index == 0 && sep->fib_index != ENDPOINT_INVALID_INDEX)
169     fib_index = sep->fib_index;
170   else
171     fib_index = sep->is_ip4 ? app_ns->ip4_fib_index : app_ns->ip6_fib_index;
172   sep->peer.fib_index = fib_index;
173   sep->fib_index = fib_index;
174
175   if (!is_connect)
176     {
177       sep->sw_if_index = app_ns->sw_if_index;
178     }
179   else
180     {
181       if (app_ns->sw_if_index != APP_NAMESPACE_INVALID_INDEX
182           && sep->peer.sw_if_index != ENDPOINT_INVALID_INDEX
183           && sep->peer.sw_if_index != app_ns->sw_if_index)
184         clib_warning ("Local sw_if_index different from app ns sw_if_index");
185
186       sep->peer.sw_if_index = app_ns->sw_if_index;
187     }
188 }
189
190 static inline int
191 vnet_bind_inline (vnet_bind_args_t * a)
192 {
193   u64 ll_handle = SESSION_INVALID_HANDLE;
194   app_worker_t *app_wrk;
195   application_t *app;
196   int rv;
197
198   app = application_get_if_valid (a->app_index);
199   if (!app)
200     {
201       SESSION_DBG ("app not attached");
202       return VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
203     }
204   app_wrk = application_get_worker (app, a->wrk_map_index);
205   a->sep_ext.app_wrk_index = app_wrk->wrk_index;
206
207   session_endpoint_update_for_app (&a->sep_ext, app, 0 /* is_connect */ );
208   if (!session_endpoint_in_ns (&a->sep))
209     return VNET_API_ERROR_INVALID_VALUE_2;
210
211   /*
212    * Add session endpoint to local session table. Only binds to "inaddr_any"
213    * (i.e., zero address) are added to local scope table.
214    */
215   if (application_has_local_scope (app)
216       && session_endpoint_is_local (&a->sep))
217     {
218       if ((rv = application_start_local_listen (app, &a->sep_ext,
219                                                 &a->handle)))
220         return rv;
221       ll_handle = a->handle;
222     }
223
224   if (!application_has_global_scope (app))
225     return (ll_handle == SESSION_INVALID_HANDLE ? -1 : 0);
226
227   /*
228    * Add session endpoint to global session table
229    */
230
231   /* Setup listen path down to transport */
232   rv = application_start_listen (app, &a->sep_ext, &a->handle);
233   if (rv && ll_handle != SESSION_INVALID_HANDLE)
234     {
235       application_stop_local_listen (a->app_index, a->wrk_map_index,
236                                      ll_handle);
237       return rv;
238     }
239
240   /*
241    * Store in local table listener the index of the transport layer
242    * listener. We'll need if if local listeners are hit and we need to
243    * return global handle
244    */
245   if (ll_handle != SESSION_INVALID_HANDLE)
246     {
247       local_session_t *ll;
248       stream_session_t *tl;
249       ll = application_get_local_listener_w_handle (ll_handle);
250       tl = listen_session_get_from_handle (a->handle);
251       if (ll->transport_listener_index == ~0)
252         ll->transport_listener_index = tl->session_index;
253     }
254   return rv;
255 }
256
257 static inline int
258 vnet_unbind_inline (vnet_unbind_args_t * a)
259 {
260   application_t *app;
261   int rv;
262
263   if (!(app = application_get_if_valid (a->app_index)))
264     {
265       SESSION_DBG ("app (%d) not attached", wrk_map_index);
266       return VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
267     }
268
269   if (application_has_local_scope (app))
270     {
271       if ((rv = application_stop_local_listen (a->app_index,
272                                                a->wrk_map_index, a->handle)))
273         return rv;
274     }
275
276   /*
277    * Clear the global scope table of the listener
278    */
279   if (application_has_global_scope (app))
280     return application_stop_listen (a->app_index, a->wrk_map_index,
281                                     a->handle);
282   return 0;
283 }
284
285 static int
286 application_connect (vnet_connect_args_t * a)
287 {
288   app_worker_t *server_wrk, *client_wrk;
289   u32 table_index, server_index, li;
290   stream_session_t *listener;
291   application_t *client, *server;
292   local_session_t *ll;
293   u8 fib_proto;
294   u64 lh;
295
296   if (session_endpoint_is_zero (&a->sep))
297     return VNET_API_ERROR_INVALID_VALUE;
298
299   client = application_get (a->app_index);
300   session_endpoint_update_for_app (&a->sep_ext, client, 1 /* is_connect */ );
301   client_wrk = application_get_worker (client, a->wrk_map_index);
302
303   /*
304    * First check the local scope for locally attached destinations.
305    * If we have local scope, we pass *all* connects through it since we may
306    * have special policy rules even for non-local destinations, think proxy.
307    */
308   if (application_has_local_scope (client))
309     {
310       table_index = application_local_session_table (client);
311       lh = session_lookup_local_endpoint (table_index, &a->sep);
312       if (lh == SESSION_DROP_HANDLE)
313         return VNET_API_ERROR_APP_CONNECT_FILTERED;
314
315       if (lh == SESSION_INVALID_HANDLE)
316         goto global_scope;
317
318       local_session_parse_handle (lh, &server_index, &li);
319
320       /*
321        * Break loop if rule in local table points to connecting app. This
322        * can happen if client is a generic proxy. Route connect through
323        * global table instead.
324        */
325       if (server_index != a->app_index)
326         {
327           server = application_get (server_index);
328           ll = application_get_local_listen_session (server, li);
329           listener = (stream_session_t *) ll;
330           server_wrk = application_listener_select_worker (listener,
331                                                            1 /* is_local */ );
332           return application_local_session_connect (client_wrk,
333                                                     server_wrk, ll,
334                                                     a->api_context);
335         }
336     }
337
338   /*
339    * If nothing found, check the global scope for locally attached
340    * destinations. Make sure first that we're allowed to.
341    */
342
343 global_scope:
344   if (session_endpoint_is_local (&a->sep))
345     return VNET_API_ERROR_SESSION_CONNECT;
346
347   if (!application_has_global_scope (client))
348     return VNET_API_ERROR_APP_CONNECT_SCOPE;
349
350   fib_proto = session_endpoint_fib_proto (&a->sep);
351   table_index = application_session_table (client, fib_proto);
352   listener = session_lookup_listener (table_index, &a->sep);
353   if (listener)
354     {
355       server_wrk = application_listener_select_worker (listener,
356                                                        0 /* is_local */ );
357       ll = (local_session_t *) listener;
358       return application_local_session_connect (client_wrk, server_wrk, ll,
359                                                 a->api_context);
360     }
361
362   /*
363    * Not connecting to a local server, propagate to transport
364    */
365   if (app_worker_open_session (client_wrk, &a->sep, a->api_context))
366     return VNET_API_ERROR_SESSION_CONNECT;
367   return 0;
368 }
369
370 /**
371  * unformat a vnet URI
372  *
373  * transport-proto://[hostname]ip46-addr:port
374  * eg.  tcp://ip46-addr:port
375  *      tls://[testtsl.fd.io]ip46-addr:port
376  *
377  * u8 ip46_address[16];
378  * u16  port_in_host_byte_order;
379  * stream_session_type_t sst;
380  * u8 *fifo_name;
381  *
382  * if (unformat (input, "%U", unformat_vnet_uri, &ip46_address,
383  *              &sst, &port, &fifo_name))
384  *  etc...
385  *
386  */
387 uword
388 unformat_vnet_uri (unformat_input_t * input, va_list * args)
389 {
390   session_endpoint_cfg_t *sep = va_arg (*args, session_endpoint_cfg_t *);
391   u32 transport_proto = 0, port;
392
393   if (unformat (input, "%U://%U/%d", unformat_transport_proto,
394                 &transport_proto, unformat_ip4_address, &sep->ip.ip4, &port))
395     {
396       sep->transport_proto = transport_proto;
397       sep->port = clib_host_to_net_u16 (port);
398       sep->is_ip4 = 1;
399       return 1;
400     }
401   else if (unformat (input, "%U://[%s]%U/%d", unformat_transport_proto,
402                      &transport_proto, &sep->hostname, unformat_ip4_address,
403                      &sep->ip.ip4, &port))
404     {
405       sep->transport_proto = transport_proto;
406       sep->port = clib_host_to_net_u16 (port);
407       sep->is_ip4 = 1;
408       return 1;
409     }
410   else if (unformat (input, "%U://%U/%d", unformat_transport_proto,
411                      &transport_proto, unformat_ip6_address, &sep->ip.ip6,
412                      &port))
413     {
414       sep->transport_proto = transport_proto;
415       sep->port = clib_host_to_net_u16 (port);
416       sep->is_ip4 = 0;
417       return 1;
418     }
419   else if (unformat (input, "%U://[%s]%U/%d", unformat_transport_proto,
420                      &transport_proto, &sep->hostname, unformat_ip6_address,
421                      &sep->ip.ip6, &port))
422     {
423       sep->transport_proto = transport_proto;
424       sep->port = clib_host_to_net_u16 (port);
425       sep->is_ip4 = 0;
426       return 1;
427     }
428   return 0;
429 }
430
431 static u8 *cache_uri;
432 static session_endpoint_cfg_t *cache_sep;
433
434 int
435 parse_uri (char *uri, session_endpoint_cfg_t * sep)
436 {
437   unformat_input_t _input, *input = &_input;
438
439   if (cache_uri && !strncmp (uri, (char *) cache_uri, vec_len (cache_uri)))
440     {
441       *sep = *cache_sep;
442       return 0;
443     }
444
445   /* Make sure */
446   uri = (char *) format (0, "%s%c", uri, 0);
447
448   /* Parse uri */
449   unformat_init_string (input, uri, strlen (uri));
450   if (!unformat (input, "%U", unformat_vnet_uri, sep))
451     {
452       unformat_free (input);
453       return VNET_API_ERROR_INVALID_VALUE;
454     }
455   unformat_free (input);
456
457   vec_free (cache_uri);
458   cache_uri = (u8 *) uri;
459   if (cache_sep)
460     clib_mem_free (cache_sep);
461   cache_sep = clib_mem_alloc (sizeof (*sep));
462   *cache_sep = *sep;
463
464   return 0;
465 }
466
467 static int
468 app_validate_namespace (u8 * namespace_id, u64 secret, u32 * app_ns_index)
469 {
470   app_namespace_t *app_ns;
471   if (vec_len (namespace_id) == 0)
472     {
473       /* Use default namespace */
474       *app_ns_index = 0;
475       return 0;
476     }
477
478   *app_ns_index = app_namespace_index_from_id (namespace_id);
479   if (*app_ns_index == APP_NAMESPACE_INVALID_INDEX)
480     return VNET_API_ERROR_APP_INVALID_NS;
481   app_ns = app_namespace_get (*app_ns_index);
482   if (!app_ns)
483     return VNET_API_ERROR_APP_INVALID_NS;
484   if (app_ns->ns_secret != secret)
485     return VNET_API_ERROR_APP_WRONG_NS_SECRET;
486   return 0;
487 }
488
489 static u8 *
490 app_name_from_api_index (u32 api_client_index)
491 {
492   vl_api_registration_t *regp;
493   regp = vl_api_client_index_to_registration (api_client_index);
494   if (regp)
495     return format (0, "%s%c", regp->name, 0);
496
497   clib_warning ("api client index %u does not have an api registration!",
498                 api_client_index);
499   return format (0, "unknown%c", 0);
500 }
501
502 /**
503  * Attach application to vpp
504  *
505  * Allocates a vpp app, i.e., a structure that keeps back pointers
506  * to external app and a segment manager for shared memory fifo based
507  * communication with the external app.
508  */
509 clib_error_t *
510 vnet_application_attach (vnet_app_attach_args_t * a)
511 {
512   svm_fifo_segment_private_t *fs;
513   application_t *app = 0;
514   app_worker_t *app_wrk;
515   segment_manager_t *sm;
516   u32 app_ns_index = 0;
517   u8 *app_name = 0;
518   u64 secret;
519   int rv;
520
521   if (a->api_client_index != APP_INVALID_INDEX)
522     app = application_lookup (a->api_client_index);
523   else if (a->name)
524     app = application_lookup_name (a->name);
525   else
526     return clib_error_return_code (0, VNET_API_ERROR_INVALID_VALUE, 0,
527                                    "api index or name must be provided");
528
529   if (app)
530     return clib_error_return_code (0, VNET_API_ERROR_APP_ALREADY_ATTACHED, 0,
531                                    "app already attached");
532
533   if (a->api_client_index != APP_INVALID_INDEX)
534     {
535       app_name = app_name_from_api_index (a->api_client_index);
536       a->name = app_name;
537     }
538
539   secret = a->options[APP_OPTIONS_NAMESPACE_SECRET];
540   if ((rv = app_validate_namespace (a->namespace_id, secret, &app_ns_index)))
541     return clib_error_return_code (0, rv, 0, "namespace validation: %d", rv);
542   a->options[APP_OPTIONS_NAMESPACE] = app_ns_index;
543
544   if ((rv = application_alloc_and_init ((app_init_args_t *) a)))
545     return clib_error_return_code (0, rv, 0, "app init: %d", rv);
546
547   app = application_get (a->app_index);
548   if ((rv = app_worker_alloc_and_init (app, &app_wrk)))
549     return clib_error_return_code (0, rv, 0, "app default wrk init: %d", rv);
550
551   a->app_evt_q = app_wrk->event_queue;
552   app_wrk->api_client_index = a->api_client_index;
553   sm = segment_manager_get (app_wrk->first_segment_manager);
554   fs = segment_manager_get_segment_w_lock (sm, 0);
555
556   if (application_is_proxy (app))
557     application_setup_proxy (app);
558
559   ASSERT (vec_len (fs->ssvm.name) <= 128);
560   a->segment = &fs->ssvm;
561   a->segment_handle = segment_manager_segment_handle (sm, fs);
562
563   segment_manager_segment_reader_unlock (sm);
564   vec_free (app_name);
565   return 0;
566 }
567
568 /**
569  * Detach application from vpp
570  */
571 int
572 vnet_application_detach (vnet_app_detach_args_t * a)
573 {
574   application_t *app;
575
576   app = application_get_if_valid (a->app_index);
577   if (!app)
578     {
579       clib_warning ("app not attached");
580       return VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
581     }
582
583   app_interface_check_thread_and_barrier (vnet_application_detach, a);
584   application_detach_process (app, a->api_client_index);
585   return 0;
586 }
587
588 int
589 vnet_bind_uri (vnet_bind_args_t * a)
590 {
591   session_endpoint_cfg_t sep = SESSION_ENDPOINT_CFG_NULL;
592   int rv;
593
594   rv = parse_uri (a->uri, &sep);
595   if (rv)
596     return rv;
597   sep.app_wrk_index = 0;
598   clib_memcpy (&a->sep_ext, &sep, sizeof (sep));
599   return vnet_bind_inline (a);
600 }
601
602 int
603 vnet_unbind_uri (vnet_unbind_args_t * a)
604 {
605   session_endpoint_cfg_t sep = SESSION_ENDPOINT_CFG_NULL;
606   stream_session_t *listener;
607   u32 table_index;
608   int rv;
609
610   rv = parse_uri (a->uri, &sep);
611   if (rv)
612     return rv;
613
614   /* NOTE: only default fib tables supported for uri apis */
615   table_index = session_lookup_get_index_for_fib (fib_ip_proto (!sep.is_ip4),
616                                                   0);
617   listener = session_lookup_listener (table_index,
618                                       (session_endpoint_t *) & sep);
619   if (!listener)
620     return VNET_API_ERROR_ADDRESS_NOT_IN_USE;
621   a->handle = listen_session_get_handle (listener);
622   return vnet_unbind_inline (a);
623 }
624
625 clib_error_t *
626 vnet_connect_uri (vnet_connect_args_t * a)
627 {
628   session_endpoint_cfg_t sep = SESSION_ENDPOINT_CFG_NULL;
629   int rv;
630
631   /* Parse uri */
632   rv = parse_uri (a->uri, &sep);
633   if (rv)
634     return clib_error_return_code (0, rv, 0, "app init: %d", rv);
635
636   clib_memcpy (&a->sep_ext, &sep, sizeof (sep));
637   if ((rv = application_connect (a)))
638     return clib_error_return_code (0, rv, 0, "connect failed");
639   return 0;
640 }
641
642 int
643 vnet_disconnect_session (vnet_disconnect_args_t * a)
644 {
645   if (session_handle_is_local (a->handle))
646     {
647       local_session_t *ls;
648
649       /* Disconnect reply came to worker 1 not main thread */
650       app_interface_check_thread_and_barrier (vnet_disconnect_session, a);
651
652       if (!(ls = application_get_local_session_from_handle (a->handle)))
653         return 0;
654
655       return application_local_session_disconnect (a->app_index, ls);
656     }
657   else
658     {
659       app_worker_t *app_wrk;
660       stream_session_t *s;
661
662       s = session_get_from_handle_if_valid (a->handle);
663       if (!s)
664         return VNET_API_ERROR_INVALID_VALUE;
665       app_wrk = app_worker_get (s->app_wrk_index);
666       if (app_wrk->app_index != a->app_index)
667         return VNET_API_ERROR_INVALID_VALUE;
668
669       /* We're peeking into another's thread pool. Make sure */
670       ASSERT (s->session_index == session_index_from_handle (a->handle));
671
672       stream_session_disconnect (s);
673     }
674   return 0;
675 }
676
677 clib_error_t *
678 vnet_bind (vnet_bind_args_t * a)
679 {
680   int rv;
681   if ((rv = vnet_bind_inline (a)))
682     return clib_error_return_code (0, rv, 0, "bind failed: %d", rv);
683   return 0;
684 }
685
686 clib_error_t *
687 vnet_unbind (vnet_unbind_args_t * a)
688 {
689   int rv;
690   if ((rv = vnet_unbind_inline (a)))
691     return clib_error_return_code (0, rv, 0, "unbind failed: %d", rv);
692   return 0;
693 }
694
695 clib_error_t *
696 vnet_connect (vnet_connect_args_t * a)
697 {
698   int rv;
699
700   if ((rv = application_connect (a)))
701     return clib_error_return_code (0, rv, 0, "connect failed: %d", rv);
702   return 0;
703 }
704
705 /*
706  * fd.io coding-style-patch-verification: ON
707  *
708  * Local Variables:
709  * eval: (c-set-style "gnu")
710  * End:
711  */