blob: 8599c74fe46ee0eaed0754a7fa3063dcc1a74fb8 [file] [log] [blame]
Dave Barach68b0fb02017-02-28 15:15:56 -05001/*
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>
Dave Barach68b0fb02017-02-28 15:15:56 -050020
21/** @file
22 VPP's application/session API bind/unbind/connect/disconnect calls
23*/
24
25static u8
Florin Corascea194d2017-10-02 00:18:51 -070026session_endpoint_is_local (session_endpoint_t * sep)
Dave Barach68b0fb02017-02-28 15:15:56 -050027{
Florin Corascea194d2017-10-02 00:18:51 -070028 return (ip_is_zero (&sep->ip, sep->is_ip4)
29 || ip_is_local_host (&sep->ip, sep->is_ip4));
Dave Barach68b0fb02017-02-28 15:15:56 -050030}
31
32static u8
Florin Corascea194d2017-10-02 00:18:51 -070033session_endpoint_is_zero (session_endpoint_t * sep)
Dave Barach68b0fb02017-02-28 15:15:56 -050034{
Florin Corascea194d2017-10-02 00:18:51 -070035 return ip_is_zero (&sep->ip, sep->is_ip4);
36}
Dave Barach68b0fb02017-02-28 15:15:56 -050037
Florin Corascea194d2017-10-02 00:18:51 -070038u8
39session_endpoint_in_ns (session_endpoint_t * sep)
40{
41 u8 is_zero = ip_is_zero (&sep->ip, sep->is_ip4);
42 if (!is_zero && sep->sw_if_index != ENDPOINT_INVALID_INDEX
43 && !ip_interface_has_address (sep->sw_if_index, &sep->ip, sep->is_ip4))
Dave Barach68b0fb02017-02-28 15:15:56 -050044 {
Florin Corascea194d2017-10-02 00:18:51 -070045 clib_warning ("sw_if_index %u not configured with ip %U",
46 sep->sw_if_index, format_ip46_address, &sep->ip,
47 sep->is_ip4);
48 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -050049 }
Florin Corascea194d2017-10-02 00:18:51 -070050 return (is_zero || ip_is_local (sep->fib_index, &sep->ip, sep->is_ip4));
Dave Barach68b0fb02017-02-28 15:15:56 -050051}
52
53int
54api_parse_session_handle (u64 handle, u32 * session_index, u32 * thread_index)
55{
56 session_manager_main_t *smm = vnet_get_session_manager_main ();
57 stream_session_t *pool;
58
59 *thread_index = handle & 0xFFFFFFFF;
60 *session_index = handle >> 32;
61
62 if (*thread_index >= vec_len (smm->sessions))
63 return VNET_API_ERROR_INVALID_VALUE;
64
65 pool = smm->sessions[*thread_index];
66
67 if (pool_is_free_index (pool, *session_index))
68 return VNET_API_ERROR_INVALID_VALUE_2;
69
70 return 0;
71}
72
Florin Corascea194d2017-10-02 00:18:51 -070073static void
74session_endpoint_update_for_app (session_endpoint_t * sep,
75 application_t * app)
76{
77 app_namespace_t *app_ns;
78 app_ns = app_namespace_get (app->ns_index);
79 if (app_ns)
80 {
81 /* Ask transport and network to bind to/connect using local interface
82 * that "supports" app's namespace. This will fix our local connection
83 * endpoint.
84 */
85 sep->sw_if_index = app_ns->sw_if_index;
86 sep->fib_index =
87 sep->is_ip4 ? app_ns->ip4_fib_index : app_ns->ip6_fib_index;
88 }
89}
90
91static int
92vnet_bind_i (u32 app_index, session_endpoint_t * sep, u64 * handle)
Dave Barach68b0fb02017-02-28 15:15:56 -050093{
Florin Coras6cf30ad2017-04-04 23:08:23 -070094 application_t *app;
Florin Coras3cbc04b2017-10-02 00:18:51 -070095 u32 table_index;
96 u64 listener;
Florin Corascea194d2017-10-02 00:18:51 -070097 int rv, have_local = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -050098
Florin Coras6cf30ad2017-04-04 23:08:23 -070099 app = application_get_if_valid (app_index);
100 if (!app)
101 {
Florin Corascea194d2017-10-02 00:18:51 -0700102 SESSION_DBG ("app not attached");
Florin Coras6cf30ad2017-04-04 23:08:23 -0700103 return VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
104 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500105
Florin Corascea194d2017-10-02 00:18:51 -0700106 session_endpoint_update_for_app (sep, app);
107 if (!session_endpoint_in_ns (sep))
Dave Barach68b0fb02017-02-28 15:15:56 -0500108 return VNET_API_ERROR_INVALID_VALUE_2;
109
Florin Corascea194d2017-10-02 00:18:51 -0700110 table_index = application_session_table (app,
111 session_endpoint_fib_proto (sep));
Florin Coras3cbc04b2017-10-02 00:18:51 -0700112 listener = session_lookup_session_endpoint (table_index, sep);
113 if (listener != SESSION_INVALID_HANDLE)
Florin Corascea194d2017-10-02 00:18:51 -0700114 return VNET_API_ERROR_ADDRESS_IN_USE;
115
116 /*
117 * Add session endpoint to local session table. Only binds to "inaddr_any"
118 * (i.e., zero address) are added to local scope table.
119 */
120 if (application_has_local_scope (app) && session_endpoint_is_zero (sep))
121 {
122 table_index = application_local_session_table (app);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700123 listener = session_lookup_session_endpoint (table_index, sep);
124 if (listener != SESSION_INVALID_HANDLE)
Florin Corascea194d2017-10-02 00:18:51 -0700125 return VNET_API_ERROR_ADDRESS_IN_USE;
126 session_lookup_add_session_endpoint (table_index, sep, app->index);
127 *handle = session_lookup_local_listener_make_handle (sep);
128 have_local = 1;
129 }
130
131 if (!application_has_global_scope (app))
132 return (have_local - 1);
133
134 /*
135 * Add session endpoint to global session table
136 */
137
Florin Coras6cf30ad2017-04-04 23:08:23 -0700138 /* Setup listen path down to transport */
Florin Corascea194d2017-10-02 00:18:51 -0700139 rv = application_start_listen (app, sep, handle);
140 if (rv && have_local)
141 session_lookup_del_session_endpoint (table_index, sep);
142 return rv;
Dave Barach68b0fb02017-02-28 15:15:56 -0500143}
144
145int
Florin Coras6cf30ad2017-04-04 23:08:23 -0700146vnet_unbind_i (u32 app_index, u64 handle)
147{
148 application_t *app = application_get_if_valid (app_index);
Florin Corascea194d2017-10-02 00:18:51 -0700149 stream_session_t *listener = 0;
150 u32 table_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700151
152 if (!app)
153 {
Florin Corascea194d2017-10-02 00:18:51 -0700154 SESSION_DBG ("app (%d) not attached", app_index);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700155 return VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
156 }
157
Florin Corascea194d2017-10-02 00:18:51 -0700158 /*
159 * Clean up local session table. If we have a listener session use it to
160 * find the port and proto. If not, the handle must be a local table handle
161 * so parse it.
162 */
163
164 if (application_has_local_scope (app))
165 {
166 session_endpoint_t sep = SESSION_ENDPOINT_NULL;
167 if (!session_lookup_local_is_handle (handle))
168 listener = listen_session_get_from_handle (handle);
169 if (listener)
170 {
171 if (listen_session_get_local_session_endpoint (listener, &sep))
172 {
173 clib_warning ("broken listener");
174 return -1;
175 }
176 }
177 else
178 {
179 if (session_lookup_local_listener_parse_handle (handle, &sep))
180 {
181 clib_warning ("can't parse handle");
182 return -1;
183 }
184 }
185 table_index = application_local_session_table (app);
186 session_lookup_del_session_endpoint (table_index, &sep);
187 }
188
189 /*
190 * Clear the global scope table of the listener
191 */
192 if (application_has_global_scope (app))
193 return application_stop_listen (app, handle);
194 return 0;
195}
196
197static int
198app_connect_redirect (application_t * server, void *mp)
199{
200 return server->cb_fns.redirect_connect_callback (server->api_client_index,
201 mp);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700202}
203
204int
Florin Corascea194d2017-10-02 00:18:51 -0700205vnet_connect_i (u32 app_index, u32 api_context, session_endpoint_t * sep,
206 void *mp)
Dave Barach68b0fb02017-02-28 15:15:56 -0500207{
Dave Barach68b0fb02017-02-28 15:15:56 -0500208 application_t *server, *app;
Florin Corascea194d2017-10-02 00:18:51 -0700209 u32 table_index;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700210 stream_session_t *listener;
Florin Corascea194d2017-10-02 00:18:51 -0700211
212 if (session_endpoint_is_zero (sep))
213 return VNET_API_ERROR_INVALID_VALUE;
214
215 app = application_get (app_index);
216 session_endpoint_update_for_app (sep, app);
Dave Barach68b0fb02017-02-28 15:15:56 -0500217
218 /*
Florin Corascea194d2017-10-02 00:18:51 -0700219 * First check the the local scope for locally attached destinations.
220 * If we have local scope, we pass *all* connects through it since we may
221 * have special policy rules even for non-local destinations, think proxy.
Dave Barach68b0fb02017-02-28 15:15:56 -0500222 */
Florin Corascea194d2017-10-02 00:18:51 -0700223 if (application_has_local_scope (app))
Dave Barach68b0fb02017-02-28 15:15:56 -0500224 {
Florin Corascea194d2017-10-02 00:18:51 -0700225 table_index = application_local_session_table (app);
226 app_index = session_lookup_local_session_endpoint (table_index, sep);
227 server = application_get (app_index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500228 /*
229 * Server is willing to have a direct fifo connection created
230 * instead of going through the state machine, etc.
231 */
Florin Corascea194d2017-10-02 00:18:51 -0700232 if (server && (server->flags & APP_OPTIONS_FLAGS_ACCEPT_REDIRECT))
233 return app_connect_redirect (server, mp);
Dave Barach68b0fb02017-02-28 15:15:56 -0500234 }
235
Dave Barach68b0fb02017-02-28 15:15:56 -0500236 /*
Florin Corascea194d2017-10-02 00:18:51 -0700237 * If nothing found, check the global scope for locally attached
238 * destinations. Make sure first that we're allowed to.
Dave Barach68b0fb02017-02-28 15:15:56 -0500239 */
Florin Corascea194d2017-10-02 00:18:51 -0700240 if (session_endpoint_is_local (sep))
241 return VNET_API_ERROR_SESSION_CONNECT;
242
243 if (!application_has_global_scope (app))
244 return VNET_API_ERROR_APP_CONNECT_SCOPE;
245
246 table_index = application_session_table (app,
247 session_endpoint_fib_proto (sep));
Florin Coras3cbc04b2017-10-02 00:18:51 -0700248 listener = session_lookup_listener (table_index, sep);
249 if (listener)
250 {
251 server = application_get (listener->app_index);
252 if (server && (server->flags & APP_OPTIONS_FLAGS_ACCEPT_REDIRECT))
253 return app_connect_redirect (server, mp);
254 }
Florin Corascea194d2017-10-02 00:18:51 -0700255
256 /*
257 * Not connecting to a local server, propagate to transport
258 */
259 if (application_open_session (app, sep, api_context))
260 return VNET_API_ERROR_SESSION_CONNECT;
261 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500262}
263
264/**
265 * unformat a vnet URI
266 *
267 * fifo://name
268 * tcp://ip46-addr:port
269 * udp://ip46-addr:port
270 *
271 * u8 ip46_address[16];
272 * u16 port_in_host_byte_order;
273 * stream_session_type_t sst;
274 * u8 *fifo_name;
275 *
276 * if (unformat (input, "%U", unformat_vnet_uri, &ip46_address,
277 * &sst, &port, &fifo_name))
278 * etc...
279 *
280 */
281uword
282unformat_vnet_uri (unformat_input_t * input, va_list * args)
283{
Florin Corascea194d2017-10-02 00:18:51 -0700284 session_endpoint_t *sep = va_arg (*args, session_endpoint_t *);
Dave Barach68b0fb02017-02-28 15:15:56 -0500285
Florin Corascea194d2017-10-02 00:18:51 -0700286 if (unformat (input, "tcp://%U/%d", unformat_ip4_address, &sep->ip.ip4,
287 &sep->port))
Dave Barach68b0fb02017-02-28 15:15:56 -0500288 {
Florin Corascea194d2017-10-02 00:18:51 -0700289 sep->transport_proto = TRANSPORT_PROTO_TCP;
290 sep->port = clib_host_to_net_u16 (sep->port);
291 sep->is_ip4 = 1;
Dave Barach68b0fb02017-02-28 15:15:56 -0500292 return 1;
293 }
Florin Corascea194d2017-10-02 00:18:51 -0700294 if (unformat (input, "udp://%U/%d", unformat_ip4_address, &sep->ip.ip4,
295 &sep->port))
Dave Barach68b0fb02017-02-28 15:15:56 -0500296 {
Florin Corascea194d2017-10-02 00:18:51 -0700297 sep->transport_proto = TRANSPORT_PROTO_UDP;
298 sep->port = clib_host_to_net_u16 (sep->port);
299 sep->is_ip4 = 1;
Dave Barach68b0fb02017-02-28 15:15:56 -0500300 return 1;
301 }
Florin Corascea194d2017-10-02 00:18:51 -0700302 if (unformat (input, "udp://%U/%d", unformat_ip6_address, &sep->ip.ip6,
303 &sep->port))
Dave Barach68b0fb02017-02-28 15:15:56 -0500304 {
Florin Corascea194d2017-10-02 00:18:51 -0700305 sep->transport_proto = TRANSPORT_PROTO_UDP;
306 sep->port = clib_host_to_net_u16 (sep->port);
307 sep->is_ip4 = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500308 return 1;
309 }
Florin Corascea194d2017-10-02 00:18:51 -0700310 if (unformat (input, "tcp://%U/%d", unformat_ip6_address, &sep->ip.ip6,
311 &sep->port))
Dave Barach68b0fb02017-02-28 15:15:56 -0500312 {
Florin Corascea194d2017-10-02 00:18:51 -0700313 sep->transport_proto = TRANSPORT_PROTO_TCP;
314 sep->port = clib_host_to_net_u16 (sep->port);
315 sep->is_ip4 = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500316 return 1;
317 }
318
319 return 0;
320}
321
Dave Barachb7f1faa2017-08-29 11:43:37 -0400322static u8 *cache_uri;
Florin Corascea194d2017-10-02 00:18:51 -0700323static session_endpoint_t *cache_sep;
Dave Barachb7f1faa2017-08-29 11:43:37 -0400324
Dave Barach68b0fb02017-02-28 15:15:56 -0500325int
Florin Corascea194d2017-10-02 00:18:51 -0700326parse_uri (char *uri, session_endpoint_t * sep)
Dave Barach68b0fb02017-02-28 15:15:56 -0500327{
328 unformat_input_t _input, *input = &_input;
329
Dave Barachb7f1faa2017-08-29 11:43:37 -0400330 if (cache_uri && !strncmp (uri, (char *) cache_uri, vec_len (cache_uri)))
331 {
Florin Corascea194d2017-10-02 00:18:51 -0700332 *sep = *cache_sep;
Dave Barachb7f1faa2017-08-29 11:43:37 -0400333 return 0;
334 }
335
Dave Barach68b0fb02017-02-28 15:15:56 -0500336 /* Make sure */
337 uri = (char *) format (0, "%s%c", uri, 0);
338
339 /* Parse uri */
340 unformat_init_string (input, uri, strlen (uri));
Florin Corascea194d2017-10-02 00:18:51 -0700341 if (!unformat (input, "%U", unformat_vnet_uri, sep))
Dave Barach68b0fb02017-02-28 15:15:56 -0500342 {
343 unformat_free (input);
344 return VNET_API_ERROR_INVALID_VALUE;
345 }
346 unformat_free (input);
347
Dave Barachb7f1faa2017-08-29 11:43:37 -0400348 vec_free (cache_uri);
349 cache_uri = (u8 *) uri;
Florin Corascea194d2017-10-02 00:18:51 -0700350 if (cache_sep)
351 clib_mem_free (cache_sep);
352 cache_sep = clib_mem_alloc (sizeof (*sep));
353 *cache_sep = *sep;
Dave Barachb7f1faa2017-08-29 11:43:37 -0400354
Dave Barach68b0fb02017-02-28 15:15:56 -0500355 return 0;
356}
357
Florin Corascea194d2017-10-02 00:18:51 -0700358static int
359session_validate_namespace (u8 * namespace_id, u64 secret, u32 * app_ns_index)
360{
361 app_namespace_t *app_ns;
362 if (vec_len (namespace_id) == 0)
363 {
364 /* Use default namespace */
365 *app_ns_index = 0;
366 return 0;
367 }
368
369 *app_ns_index = app_namespace_index_from_id (namespace_id);
370 if (*app_ns_index == APP_NAMESPACE_INVALID_INDEX)
371 return VNET_API_ERROR_APP_INVALID_NS;
372 app_ns = app_namespace_get (*app_ns_index);
373 if (!app_ns)
374 return VNET_API_ERROR_APP_INVALID_NS;
375 if (app_ns->ns_secret != secret)
376 return VNET_API_ERROR_APP_WRONG_NS_SECRET;
377 return 0;
378}
379
Florin Coras6cf30ad2017-04-04 23:08:23 -0700380/**
Florin Corascea194d2017-10-02 00:18:51 -0700381 * Attach application to vpp
Florin Coras6cf30ad2017-04-04 23:08:23 -0700382 *
383 * Allocates a vpp app, i.e., a structure that keeps back pointers
384 * to external app and a segment manager for shared memory fifo based
385 * communication with the external app.
386 */
Florin Corascea194d2017-10-02 00:18:51 -0700387clib_error_t *
Florin Coras6cf30ad2017-04-04 23:08:23 -0700388vnet_application_attach (vnet_app_attach_args_t * a)
Dave Barach68b0fb02017-02-28 15:15:56 -0500389{
Florin Coras6cf30ad2017-04-04 23:08:23 -0700390 application_t *app = 0;
391 segment_manager_t *sm;
392 u8 *seg_name;
Florin Corascea194d2017-10-02 00:18:51 -0700393 u64 secret;
394 u32 app_ns_index = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500395 int rv;
396
Florin Corascea194d2017-10-02 00:18:51 -0700397 app = application_lookup (a->api_client_index);
398 if (app)
399 return clib_error_return_code (0, VNET_API_ERROR_APP_ALREADY_ATTACHED,
400 0, "app already attached");
401
402 secret = a->options[APP_OPTIONS_NAMESPACE_SECRET];
403 if ((rv = session_validate_namespace (a->namespace_id, secret,
404 &app_ns_index)))
405 return clib_error_return_code (0, rv, 0, "namespace validation: %d", rv);
406 a->options[APP_OPTIONS_NAMESPACE] = app_ns_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700407 app = application_new ();
408 if ((rv = application_init (app, a->api_client_index, a->options,
409 a->session_cb_vft)))
Florin Corascea194d2017-10-02 00:18:51 -0700410 return clib_error_return_code (0, rv, 0, "app init: %d", rv);
Dave Barach68b0fb02017-02-28 15:15:56 -0500411
Damjan Marion7bee80c2017-04-26 15:32:12 +0200412 a->app_event_queue_address = pointer_to_uword (app->event_queue);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700413 sm = segment_manager_get (app->first_segment_manager);
414 segment_manager_get_segment_info (sm->segment_indices[0],
415 &seg_name, &a->segment_size);
Dave Barach68b0fb02017-02-28 15:15:56 -0500416
Florin Coras6cf30ad2017-04-04 23:08:23 -0700417 a->segment_name_length = vec_len (seg_name);
418 a->segment_name = seg_name;
419 ASSERT (vec_len (a->segment_name) <= 128);
420 a->app_index = app->index;
421 return 0;
422}
423
Florin Corascea194d2017-10-02 00:18:51 -0700424/**
425 * Detach application from vpp
426 */
Florin Coras6cf30ad2017-04-04 23:08:23 -0700427int
428vnet_application_detach (vnet_app_detach_args_t * a)
429{
430 application_t *app;
431 app = application_get_if_valid (a->app_index);
432
433 if (!app)
434 {
435 clib_warning ("app not attached");
436 return VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
437 }
438
439 application_del (app);
Dave Barach68b0fb02017-02-28 15:15:56 -0500440 return 0;
441}
442
Dave Barach68b0fb02017-02-28 15:15:56 -0500443int
Florin Coras6cf30ad2017-04-04 23:08:23 -0700444vnet_bind_uri (vnet_bind_args_t * a)
Dave Barach68b0fb02017-02-28 15:15:56 -0500445{
Florin Corascea194d2017-10-02 00:18:51 -0700446 session_endpoint_t sep = SESSION_ENDPOINT_NULL;
Dave Barach68b0fb02017-02-28 15:15:56 -0500447 int rv;
448
Florin Corascea194d2017-10-02 00:18:51 -0700449 rv = parse_uri (a->uri, &sep);
Dave Barach68b0fb02017-02-28 15:15:56 -0500450 if (rv)
451 return rv;
452
Florin Corascea194d2017-10-02 00:18:51 -0700453 return vnet_bind_i (a->app_index, &sep, &a->handle);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700454}
455
456int
457vnet_unbind_uri (vnet_unbind_args_t * a)
458{
Florin Coras6cf30ad2017-04-04 23:08:23 -0700459 stream_session_t *listener;
Florin Corascea194d2017-10-02 00:18:51 -0700460 session_endpoint_t sep = SESSION_ENDPOINT_NULL;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700461 int rv;
462
Florin Corascea194d2017-10-02 00:18:51 -0700463 rv = parse_uri (a->uri, &sep);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700464 if (rv)
465 return rv;
466
Florin Corascea194d2017-10-02 00:18:51 -0700467 /* NOTE: only default table supported for uri */
468 listener = session_lookup_listener (0, &sep);
Dave Barach68b0fb02017-02-28 15:15:56 -0500469 if (!listener)
470 return VNET_API_ERROR_ADDRESS_NOT_IN_USE;
471
Florin Coras6cf30ad2017-04-04 23:08:23 -0700472 return vnet_unbind_i (a->app_index, listen_session_get_handle (listener));
Dave Barach68b0fb02017-02-28 15:15:56 -0500473}
474
Florin Corascea194d2017-10-02 00:18:51 -0700475clib_error_t *
Dave Barach68b0fb02017-02-28 15:15:56 -0500476vnet_connect_uri (vnet_connect_args_t * a)
477{
Florin Coras3cbc04b2017-10-02 00:18:51 -0700478 session_endpoint_t sep_null = SESSION_ENDPOINT_NULL;
Dave Barach68b0fb02017-02-28 15:15:56 -0500479 int rv;
480
Dave Barach68b0fb02017-02-28 15:15:56 -0500481 /* Parse uri */
Florin Coras3cbc04b2017-10-02 00:18:51 -0700482 a->sep = sep_null;
483 rv = parse_uri (a->uri, &a->sep);
Dave Barach68b0fb02017-02-28 15:15:56 -0500484 if (rv)
Florin Corascea194d2017-10-02 00:18:51 -0700485 return clib_error_return_code (0, rv, 0, "app init: %d", rv);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700486 if ((rv = vnet_connect_i (a->app_index, a->api_context, &a->sep, a->mp)))
Florin Corascea194d2017-10-02 00:18:51 -0700487 return clib_error_return_code (0, rv, 0, "connect failed");
488 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500489}
490
491int
Florin Coras6cf30ad2017-04-04 23:08:23 -0700492vnet_disconnect_session (vnet_disconnect_args_t * a)
Dave Barach68b0fb02017-02-28 15:15:56 -0500493{
Florin Coras6cf30ad2017-04-04 23:08:23 -0700494 u32 index, thread_index;
495 stream_session_t *s;
Dave Barach68b0fb02017-02-28 15:15:56 -0500496
Florin Corascea194d2017-10-02 00:18:51 -0700497 session_parse_handle (a->handle, &index, &thread_index);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700498 s = session_get_if_valid (index, thread_index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500499
Florin Coras6cf30ad2017-04-04 23:08:23 -0700500 if (!s || s->app_index != a->app_index)
501 return VNET_API_ERROR_INVALID_VALUE;
502
Florin Corasa5464812017-04-19 13:00:05 -0700503 /* We're peeking into another's thread pool. Make sure */
504 ASSERT (s->session_index == index);
505
506 session_send_session_evt_to_thread (a->handle, FIFO_EVENT_DISCONNECT,
507 thread_index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500508 return 0;
509}
510
Florin Corascea194d2017-10-02 00:18:51 -0700511clib_error_t *
Dave Barach68b0fb02017-02-28 15:15:56 -0500512vnet_bind (vnet_bind_args_t * a)
513{
Dave Barach68b0fb02017-02-28 15:15:56 -0500514 int rv;
Florin Corascea194d2017-10-02 00:18:51 -0700515 if ((rv = vnet_bind_i (a->app_index, &a->sep, &a->handle)))
516 return clib_error_return_code (0, rv, 0, "bind failed");
Dave Barach68b0fb02017-02-28 15:15:56 -0500517 return 0;
518}
519
Florin Corascea194d2017-10-02 00:18:51 -0700520clib_error_t *
Dave Barach68b0fb02017-02-28 15:15:56 -0500521vnet_unbind (vnet_unbind_args_t * a)
522{
Florin Corascea194d2017-10-02 00:18:51 -0700523 int rv;
524 if ((rv = vnet_unbind_i (a->app_index, a->handle)))
525 return clib_error_return_code (0, rv, 0, "unbind failed");
526 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500527}
528
Florin Corascea194d2017-10-02 00:18:51 -0700529clib_error_t *
Dave Barach68b0fb02017-02-28 15:15:56 -0500530vnet_connect (vnet_connect_args_t * a)
531{
Florin Corascea194d2017-10-02 00:18:51 -0700532 int rv;
533 if ((rv = vnet_connect_i (a->app_index, a->api_context, &a->sep, a->mp)))
534 return clib_error_return_code (0, rv, 0, "connect failed");
535 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500536}
537
Dave Barach68b0fb02017-02-28 15:15:56 -0500538/*
539 * fd.io coding-style-patch-verification: ON
540 *
541 * Local Variables:
542 * eval: (c-set-style "gnu")
543 * End:
544 */