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