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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
15 #include <vnet/session/application_interface.h>
17 #include <vnet/session/session.h>
18 #include <vlibmemory/api.h>
19 #include <vnet/dpo/load_balance.h>
22 VPP's application/session API bind/unbind/connect/disconnect calls
26 session_endpoint_is_local (session_endpoint_t * sep)
28 return (ip_is_zero (&sep->ip, sep->is_ip4)
29 || ip_is_local_host (&sep->ip, sep->is_ip4));
33 session_endpoint_is_zero (session_endpoint_t * sep)
35 return ip_is_zero (&sep->ip, sep->is_ip4);
39 session_endpoint_in_ns (session_endpoint_t * sep)
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))
45 clib_warning ("sw_if_index %u not configured with ip %U",
46 sep->sw_if_index, format_ip46_address, &sep->ip,
50 return (is_zero || ip_is_local (sep->fib_index, &sep->ip, sep->is_ip4));
54 api_parse_session_handle (u64 handle, u32 * session_index, u32 * thread_index)
56 session_manager_main_t *smm = vnet_get_session_manager_main ();
57 stream_session_t *pool;
59 *thread_index = handle & 0xFFFFFFFF;
60 *session_index = handle >> 32;
62 if (*thread_index >= vec_len (smm->sessions))
63 return VNET_API_ERROR_INVALID_VALUE;
65 pool = smm->sessions[*thread_index];
67 if (pool_is_free_index (pool, *session_index))
68 return VNET_API_ERROR_INVALID_VALUE_2;
74 session_endpoint_update_for_app (session_endpoint_t * sep,
77 app_namespace_t *app_ns;
78 app_ns = app_namespace_get (app->ns_index);
81 /* Ask transport and network to bind to/connect using local interface
82 * that "supports" app's namespace. This will fix our local connection
85 sep->sw_if_index = app_ns->sw_if_index;
87 sep->is_ip4 ? app_ns->ip4_fib_index : app_ns->ip6_fib_index;
92 vnet_bind_i (u32 app_index, session_endpoint_t * sep, u64 * handle)
97 int rv, have_local = 0;
99 app = application_get_if_valid (app_index);
102 SESSION_DBG ("app not attached");
103 return VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
106 session_endpoint_update_for_app (sep, app);
107 if (!session_endpoint_in_ns (sep))
108 return VNET_API_ERROR_INVALID_VALUE_2;
110 table_index = application_session_table (app,
111 session_endpoint_fib_proto (sep));
112 listener = session_lookup_session_endpoint (table_index, sep);
113 if (listener != SESSION_INVALID_HANDLE)
114 return VNET_API_ERROR_ADDRESS_IN_USE;
117 * Add session endpoint to local session table. Only binds to "inaddr_any"
118 * (i.e., zero address) are added to local scope table.
120 if (application_has_local_scope (app) && session_endpoint_is_zero (sep))
122 table_index = application_local_session_table (app);
123 listener = session_lookup_session_endpoint (table_index, sep);
124 if (listener != SESSION_INVALID_HANDLE)
125 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);
131 if (!application_has_global_scope (app))
132 return (have_local - 1);
135 * Add session endpoint to global session table
138 /* Setup listen path down to transport */
139 rv = application_start_listen (app, sep, handle);
140 if (rv && have_local)
141 session_lookup_del_session_endpoint (table_index, sep);
146 vnet_unbind_i (u32 app_index, u64 handle)
148 application_t *app = application_get_if_valid (app_index);
149 stream_session_t *listener = 0;
154 SESSION_DBG ("app (%d) not attached", app_index);
155 return VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
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
164 if (application_has_local_scope (app))
166 session_endpoint_t sep = SESSION_ENDPOINT_NULL;
167 if (!session_lookup_local_is_handle (handle))
168 listener = listen_session_get_from_handle (handle);
171 if (listen_session_get_local_session_endpoint (listener, &sep))
173 clib_warning ("broken listener");
179 if (session_lookup_local_listener_parse_handle (handle, &sep))
181 clib_warning ("can't parse handle");
185 table_index = application_local_session_table (app);
186 session_lookup_del_session_endpoint (table_index, &sep);
190 * Clear the global scope table of the listener
192 if (application_has_global_scope (app))
193 return application_stop_listen (app, handle);
198 app_connect_redirect (application_t * server, void *mp)
200 return server->cb_fns.redirect_connect_callback (server->api_client_index,
205 vnet_connect_i (u32 app_index, u32 api_context, session_endpoint_t * sep,
208 application_t *server, *app;
210 stream_session_t *listener;
212 if (session_endpoint_is_zero (sep))
213 return VNET_API_ERROR_INVALID_VALUE;
215 app = application_get (app_index);
216 session_endpoint_update_for_app (sep, app);
219 * 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.
223 if (application_has_local_scope (app))
225 table_index = application_local_session_table (app);
226 app_index = session_lookup_local_session_endpoint (table_index, sep);
227 server = application_get (app_index);
229 * Server is willing to have a direct fifo connection created
230 * instead of going through the state machine, etc.
232 if (server && (server->flags & APP_OPTIONS_FLAGS_ACCEPT_REDIRECT))
233 return app_connect_redirect (server, mp);
237 * If nothing found, check the global scope for locally attached
238 * destinations. Make sure first that we're allowed to.
240 if (session_endpoint_is_local (sep))
241 return VNET_API_ERROR_SESSION_CONNECT;
243 if (!application_has_global_scope (app))
244 return VNET_API_ERROR_APP_CONNECT_SCOPE;
246 table_index = application_session_table (app,
247 session_endpoint_fib_proto (sep));
248 listener = session_lookup_listener (table_index, sep);
251 server = application_get (listener->app_index);
252 if (server && (server->flags & APP_OPTIONS_FLAGS_ACCEPT_REDIRECT))
253 return app_connect_redirect (server, mp);
257 * Not connecting to a local server, propagate to transport
259 if (application_open_session (app, sep, api_context))
260 return VNET_API_ERROR_SESSION_CONNECT;
265 * unformat a vnet URI
268 * tcp://ip46-addr:port
269 * udp://ip46-addr:port
271 * u8 ip46_address[16];
272 * u16 port_in_host_byte_order;
273 * stream_session_type_t sst;
276 * if (unformat (input, "%U", unformat_vnet_uri, &ip46_address,
277 * &sst, &port, &fifo_name))
282 unformat_vnet_uri (unformat_input_t * input, va_list * args)
284 session_endpoint_t *sep = va_arg (*args, session_endpoint_t *);
286 if (unformat (input, "tcp://%U/%d", unformat_ip4_address, &sep->ip.ip4,
289 sep->transport_proto = TRANSPORT_PROTO_TCP;
290 sep->port = clib_host_to_net_u16 (sep->port);
294 if (unformat (input, "udp://%U/%d", unformat_ip4_address, &sep->ip.ip4,
297 sep->transport_proto = TRANSPORT_PROTO_UDP;
298 sep->port = clib_host_to_net_u16 (sep->port);
302 if (unformat (input, "udp://%U/%d", unformat_ip6_address, &sep->ip.ip6,
305 sep->transport_proto = TRANSPORT_PROTO_UDP;
306 sep->port = clib_host_to_net_u16 (sep->port);
310 if (unformat (input, "tcp://%U/%d", unformat_ip6_address, &sep->ip.ip6,
313 sep->transport_proto = TRANSPORT_PROTO_TCP;
314 sep->port = clib_host_to_net_u16 (sep->port);
322 static u8 *cache_uri;
323 static session_endpoint_t *cache_sep;
326 parse_uri (char *uri, session_endpoint_t * sep)
328 unformat_input_t _input, *input = &_input;
330 if (cache_uri && !strncmp (uri, (char *) cache_uri, vec_len (cache_uri)))
337 uri = (char *) format (0, "%s%c", uri, 0);
340 unformat_init_string (input, uri, strlen (uri));
341 if (!unformat (input, "%U", unformat_vnet_uri, sep))
343 unformat_free (input);
344 return VNET_API_ERROR_INVALID_VALUE;
346 unformat_free (input);
348 vec_free (cache_uri);
349 cache_uri = (u8 *) uri;
351 clib_mem_free (cache_sep);
352 cache_sep = clib_mem_alloc (sizeof (*sep));
359 session_validate_namespace (u8 * namespace_id, u64 secret, u32 * app_ns_index)
361 app_namespace_t *app_ns;
362 if (vec_len (namespace_id) == 0)
364 /* Use default namespace */
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);
374 return VNET_API_ERROR_APP_INVALID_NS;
375 if (app_ns->ns_secret != secret)
376 return VNET_API_ERROR_APP_WRONG_NS_SECRET;
381 * Attach application to vpp
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.
388 vnet_application_attach (vnet_app_attach_args_t * a)
390 application_t *app = 0;
391 segment_manager_t *sm;
394 u32 app_ns_index = 0;
397 app = application_lookup (a->api_client_index);
399 return clib_error_return_code (0, VNET_API_ERROR_APP_ALREADY_ATTACHED,
400 0, "app already attached");
402 secret = a->options[APP_OPTIONS_NAMESPACE_SECRET];
403 if ((rv = session_validate_namespace (a->namespace_id, secret,
405 return clib_error_return_code (0, rv, 0, "namespace validation: %d", rv);
406 a->options[APP_OPTIONS_NAMESPACE] = app_ns_index;
407 app = application_new ();
408 if ((rv = application_init (app, a->api_client_index, a->options,
410 return clib_error_return_code (0, rv, 0, "app init: %d", rv);
412 a->app_event_queue_address = pointer_to_uword (app->event_queue);
413 sm = segment_manager_get (app->first_segment_manager);
414 segment_manager_get_segment_info (sm->segment_indices[0],
415 &seg_name, &a->segment_size);
417 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;
425 * Detach application from vpp
428 vnet_application_detach (vnet_app_detach_args_t * a)
431 app = application_get_if_valid (a->app_index);
435 clib_warning ("app not attached");
436 return VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
439 application_del (app);
444 vnet_bind_uri (vnet_bind_args_t * a)
446 session_endpoint_t sep = SESSION_ENDPOINT_NULL;
449 rv = parse_uri (a->uri, &sep);
453 return vnet_bind_i (a->app_index, &sep, &a->handle);
457 vnet_unbind_uri (vnet_unbind_args_t * a)
459 stream_session_t *listener;
460 session_endpoint_t sep = SESSION_ENDPOINT_NULL;
463 rv = parse_uri (a->uri, &sep);
467 /* NOTE: only default table supported for uri */
468 listener = session_lookup_listener (0, &sep);
470 return VNET_API_ERROR_ADDRESS_NOT_IN_USE;
472 return vnet_unbind_i (a->app_index, listen_session_get_handle (listener));
476 vnet_connect_uri (vnet_connect_args_t * a)
478 session_endpoint_t sep_null = SESSION_ENDPOINT_NULL;
483 rv = parse_uri (a->uri, &a->sep);
485 return clib_error_return_code (0, rv, 0, "app init: %d", rv);
486 if ((rv = vnet_connect_i (a->app_index, a->api_context, &a->sep, a->mp)))
487 return clib_error_return_code (0, rv, 0, "connect failed");
492 vnet_disconnect_session (vnet_disconnect_args_t * a)
494 u32 index, thread_index;
497 session_parse_handle (a->handle, &index, &thread_index);
498 s = session_get_if_valid (index, thread_index);
500 if (!s || s->app_index != a->app_index)
501 return VNET_API_ERROR_INVALID_VALUE;
503 /* We're peeking into another's thread pool. Make sure */
504 ASSERT (s->session_index == index);
506 session_send_session_evt_to_thread (a->handle, FIFO_EVENT_DISCONNECT,
512 vnet_bind (vnet_bind_args_t * a)
515 if ((rv = vnet_bind_i (a->app_index, &a->sep, &a->handle)))
516 return clib_error_return_code (0, rv, 0, "bind failed");
521 vnet_unbind (vnet_unbind_args_t * a)
524 if ((rv = vnet_unbind_i (a->app_index, a->handle)))
525 return clib_error_return_code (0, rv, 0, "unbind failed");
530 vnet_connect (vnet_connect_args_t * a)
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");
539 * fd.io coding-style-patch-verification: ON
542 * eval: (c-set-style "gnu")