X-Git-Url: https://gerrit.fd.io/r/gitweb?a=blobdiff_plain;f=src%2Fvnet%2Fsession%2Fapplication_interface.c;h=12a5701fdf35375cfbe7671368be5cb32443a0e2;hb=371ca50a74a9c4f1b74c4c1b65c6fdec610fcfc3;hp=4b30bd876a784c1abf2fd8bf8337ee0dabb63f2a;hpb=d79b41e993981df80245b0e6d90eb691bdaae648;p=vpp.git diff --git a/src/vnet/session/application_interface.c b/src/vnet/session/application_interface.c index 4b30bd876a7..12a5701fdf3 100644 --- a/src/vnet/session/application_interface.c +++ b/src/vnet/session/application_interface.c @@ -17,45 +17,92 @@ #include #include #include -#include /** @file VPP's application/session API bind/unbind/connect/disconnect calls */ +/* + * TLS server cert and keys to be used for testing only + */ +const char test_srv_crt_rsa[] = + "-----BEGIN CERTIFICATE-----\r\n" + "MIIDNzCCAh+gAwIBAgIBAjANBgkqhkiG9w0BAQUFADA7MQswCQYDVQQGEwJOTDER\r\n" + "MA8GA1UEChMIUG9sYXJTU0wxGTAXBgNVBAMTEFBvbGFyU1NMIFRlc3QgQ0EwHhcN\r\n" + "MTEwMjEyMTQ0NDA2WhcNMjEwMjEyMTQ0NDA2WjA0MQswCQYDVQQGEwJOTDERMA8G\r\n" + "A1UEChMIUG9sYXJTU0wxEjAQBgNVBAMTCWxvY2FsaG9zdDCCASIwDQYJKoZIhvcN\r\n" + "AQEBBQADggEPADCCAQoCggEBAMFNo93nzR3RBNdJcriZrA545Do8Ss86ExbQWuTN\r\n" + "owCIp+4ea5anUrSQ7y1yej4kmvy2NKwk9XfgJmSMnLAofaHa6ozmyRyWvP7BBFKz\r\n" + "NtSj+uGxdtiQwWG0ZlI2oiZTqqt0Xgd9GYLbKtgfoNkNHC1JZvdbJXNG6AuKT2kM\r\n" + "tQCQ4dqCEGZ9rlQri2V5kaHiYcPNQEkI7mgM8YuG0ka/0LiqEQMef1aoGh5EGA8P\r\n" + "hYvai0Re4hjGYi/HZo36Xdh98yeJKQHFkA4/J/EwyEoO79bex8cna8cFPXrEAjya\r\n" + "HT4P6DSYW8tzS1KW2BGiLICIaTla0w+w3lkvEcf36hIBMJcCAwEAAaNNMEswCQYD\r\n" + "VR0TBAIwADAdBgNVHQ4EFgQUpQXoZLjc32APUBJNYKhkr02LQ5MwHwYDVR0jBBgw\r\n" + "FoAUtFrkpbPe0lL2udWmlQ/rPrzH/f8wDQYJKoZIhvcNAQEFBQADggEBAJxnXClY\r\n" + "oHkbp70cqBrsGXLybA74czbO5RdLEgFs7rHVS9r+c293luS/KdliLScZqAzYVylw\r\n" + "UfRWvKMoWhHYKp3dEIS4xTXk6/5zXxhv9Rw8SGc8qn6vITHk1S1mPevtekgasY5Y\r\n" + "iWQuM3h4YVlRH3HHEMAD1TnAexfXHHDFQGe+Bd1iAbz1/sH9H8l4StwX6egvTK3M\r\n" + "wXRwkKkvjKaEDA9ATbZx0mI8LGsxSuCqe9r9dyjmttd47J1p1Rulz3CLzaRcVIuS\r\n" + "RRQfaD8neM9c1S/iJ/amTVqJxA1KOdOS5780WhPfSArA+g4qAmSjelc3p4wWpha8\r\n" + "zhuYwjVuX6JHG0c=\r\n" "-----END CERTIFICATE-----\r\n"; +const u32 test_srv_crt_rsa_len = sizeof (test_srv_crt_rsa); + +const char test_srv_key_rsa[] = + "-----BEGIN RSA PRIVATE KEY-----\r\n" + "MIIEpAIBAAKCAQEAwU2j3efNHdEE10lyuJmsDnjkOjxKzzoTFtBa5M2jAIin7h5r\r\n" + "lqdStJDvLXJ6PiSa/LY0rCT1d+AmZIycsCh9odrqjObJHJa8/sEEUrM21KP64bF2\r\n" + "2JDBYbRmUjaiJlOqq3ReB30Zgtsq2B+g2Q0cLUlm91slc0boC4pPaQy1AJDh2oIQ\r\n" + "Zn2uVCuLZXmRoeJhw81ASQjuaAzxi4bSRr/QuKoRAx5/VqgaHkQYDw+Fi9qLRF7i\r\n" + "GMZiL8dmjfpd2H3zJ4kpAcWQDj8n8TDISg7v1t7HxydrxwU9esQCPJodPg/oNJhb\r\n" + "y3NLUpbYEaIsgIhpOVrTD7DeWS8Rx/fqEgEwlwIDAQABAoIBAQCXR0S8EIHFGORZ\r\n" + "++AtOg6eENxD+xVs0f1IeGz57Tjo3QnXX7VBZNdj+p1ECvhCE/G7XnkgU5hLZX+G\r\n" + "Z0jkz/tqJOI0vRSdLBbipHnWouyBQ4e/A1yIJdlBtqXxJ1KE/ituHRbNc4j4kL8Z\r\n" + "/r6pvwnTI0PSx2Eqs048YdS92LT6qAv4flbNDxMn2uY7s4ycS4Q8w1JXnCeaAnYm\r\n" + "WYI5wxO+bvRELR2Mcz5DmVnL8jRyml6l6582bSv5oufReFIbyPZbQWlXgYnpu6He\r\n" + "GTc7E1zKYQGG/9+DQUl/1vQuCPqQwny0tQoX2w5tdYpdMdVm+zkLtbajzdTviJJa\r\n" + "TWzL6lt5AoGBAN86+SVeJDcmQJcv4Eq6UhtRr4QGMiQMz0Sod6ettYxYzMgxtw28\r\n" + "CIrgpozCc+UaZJLo7UxvC6an85r1b2nKPCLQFaggJ0H4Q0J/sZOhBIXaoBzWxveK\r\n" + "nupceKdVxGsFi8CDy86DBfiyFivfBj+47BbaQzPBj7C4rK7UlLjab2rDAoGBAN2u\r\n" + "AM2gchoFiu4v1HFL8D7lweEpi6ZnMJjnEu/dEgGQJFjwdpLnPbsj4c75odQ4Gz8g\r\n" + "sw9lao9VVzbusoRE/JGI4aTdO0pATXyG7eG1Qu+5Yc1YGXcCrliA2xM9xx+d7f+s\r\n" + "mPzN+WIEg5GJDYZDjAzHG5BNvi/FfM1C9dOtjv2dAoGAF0t5KmwbjWHBhcVqO4Ic\r\n" + "BVvN3BIlc1ue2YRXEDlxY5b0r8N4XceMgKmW18OHApZxfl8uPDauWZLXOgl4uepv\r\n" + "whZC3EuWrSyyICNhLY21Ah7hbIEBPF3L3ZsOwC+UErL+dXWLdB56Jgy3gZaBeW7b\r\n" + "vDrEnocJbqCm7IukhXHOBK8CgYEAwqdHB0hqyNSzIOGY7v9abzB6pUdA3BZiQvEs\r\n" + "3LjHVd4HPJ2x0N8CgrBIWOE0q8+0hSMmeE96WW/7jD3fPWwCR5zlXknxBQsfv0gP\r\n" + "3BC5PR0Qdypz+d+9zfMf625kyit4T/hzwhDveZUzHnk1Cf+IG7Q+TOEnLnWAWBED\r\n" + "ISOWmrUCgYAFEmRxgwAc/u+D6t0syCwAYh6POtscq9Y0i9GyWk89NzgC4NdwwbBH\r\n" + "4AgahOxIxXx2gxJnq3yfkJfIjwf0s2DyP0kY2y6Ua1OeomPeY9mrIS4tCuDQ6LrE\r\n" + "TB6l9VGoxJL4fyHnZb8L5gGvnB1bbD8cL6YPaDiOhcRseC9vBiEuVg==\r\n" + "-----END RSA PRIVATE KEY-----\r\n"; +const u32 test_srv_key_rsa_len = sizeof (test_srv_key_rsa); + static u8 -ip_is_zero (ip46_address_t * ip46_address, u8 is_ip4) +session_endpoint_is_local (session_endpoint_t * sep) { - if (is_ip4) - return (ip46_address->ip4.as_u32 == 0); - else - return (ip46_address->as_u64[0] == 0 && ip46_address->as_u64[1] == 0); + return (ip_is_zero (&sep->ip, sep->is_ip4) + || ip_is_local_host (&sep->ip, sep->is_ip4)); } static u8 -ip_is_local (ip46_address_t * ip46_address, u8 is_ip4) +session_endpoint_is_zero (session_endpoint_t * sep) { - fib_node_index_t fei; - fib_entry_flag_t flags; - fib_prefix_t prefix; + return ip_is_zero (&sep->ip, sep->is_ip4); +} - /* Check if requester is local */ - if (is_ip4) - { - prefix.fp_len = 32; - prefix.fp_proto = FIB_PROTOCOL_IP4; - } - else +u8 +session_endpoint_in_ns (session_endpoint_t * sep) +{ + u8 is_zero = ip_is_zero (&sep->ip, sep->is_ip4); + if (!is_zero && sep->sw_if_index != ENDPOINT_INVALID_INDEX + && !ip_interface_has_address (sep->sw_if_index, &sep->ip, sep->is_ip4)) { - prefix.fp_len = 128; - prefix.fp_proto = FIB_PROTOCOL_IP6; + clib_warning ("sw_if_index %u not configured with ip %U", + sep->sw_if_index, format_ip46_address, &sep->ip, + sep->is_ip4); + return 0; } - - clib_memcpy (&prefix.fp_addr, ip46_address, sizeof (ip46_address_t)); - fei = fib_table_lookup (0, &prefix); - flags = fib_entry_get_flags (fei); - - return (flags & FIB_ENTRY_FLAG_LOCAL); + return (is_zero || ip_is_local (sep->fib_index, &sep->ip, sep->is_ip4)); } int @@ -78,123 +125,198 @@ api_parse_session_handle (u64 handle, u32 * session_index, u32 * thread_index) return 0; } -int -vnet_bind_i (u32 api_client_index, ip46_address_t * ip46, u16 port_host_order, - session_type_t sst, u64 * options, session_cb_vft_t * cb_fns, - application_t ** app, u32 * len_seg_name, char *seg_name) +static void +session_endpoint_update_for_app (session_endpoint_t * sep, + application_t * app) { - u8 *segment_name = 0; - application_t *server = 0; - stream_session_t *listener; - u8 is_ip4; - - listener = - stream_session_lookup_listener (ip46, - clib_host_to_net_u16 (port_host_order), - sst); + app_namespace_t *app_ns; + app_ns = app_namespace_get (app->ns_index); + if (app_ns) + { + /* Ask transport and network to bind to/connect using local interface + * that "supports" app's namespace. This will fix our local connection + * endpoint. + */ + sep->sw_if_index = app_ns->sw_if_index; + sep->fib_index = + sep->is_ip4 ? app_ns->ip4_fib_index : app_ns->ip6_fib_index; + } +} - if (listener) - return VNET_API_ERROR_ADDRESS_IN_USE; +static int +vnet_bind_i (u32 app_index, session_endpoint_t * sep, u64 * handle) +{ + u64 lh, ll_handle = SESSION_INVALID_HANDLE; + application_t *app; + u32 table_index; + int rv; - if (application_lookup (api_client_index)) + app = application_get_if_valid (app_index); + if (!app) { - clib_warning ("Only one connection supported for now"); - return VNET_API_ERROR_ADDRESS_IN_USE; + SESSION_DBG ("app not attached"); + return VNET_API_ERROR_APPLICATION_NOT_ATTACHED; } - is_ip4 = SESSION_TYPE_IP4_UDP == sst || SESSION_TYPE_IP4_TCP == sst; - if (!ip_is_zero (ip46, is_ip4) && !ip_is_local (ip46, is_ip4)) - return VNET_API_ERROR_INVALID_VALUE; + session_endpoint_update_for_app (sep, app); + if (!session_endpoint_in_ns (sep)) + return VNET_API_ERROR_INVALID_VALUE_2; - /* Allocate and initialize stream server */ - server = application_new (APP_SERVER, sst, api_client_index, - options[SESSION_OPTIONS_FLAGS], cb_fns); + table_index = application_session_table (app, + session_endpoint_fib_proto (sep)); + lh = session_lookup_endpoint_listener (table_index, sep, 1); + if (lh != SESSION_INVALID_HANDLE) + return VNET_API_ERROR_ADDRESS_IN_USE; - application_server_init (server, options[SESSION_OPTIONS_SEGMENT_SIZE], - options[SESSION_OPTIONS_ADD_SEGMENT_SIZE], - options[SESSION_OPTIONS_RX_FIFO_SIZE], - options[SESSION_OPTIONS_TX_FIFO_SIZE], - &segment_name); + /* + * Add session endpoint to local session table. Only binds to "inaddr_any" + * (i.e., zero address) are added to local scope table. + */ + if (application_has_local_scope (app) && session_endpoint_is_zero (sep)) + { + if ((rv = application_start_local_listen (app, sep, handle))) + return rv; + ll_handle = *handle; + } - /* Setup listen path down to transport */ - stream_session_start_listen (server->index, ip46, port_host_order); + if (!application_has_global_scope (app)) + return (ll_handle == SESSION_INVALID_HANDLE ? -1 : 0); /* - * Return values + * Add session endpoint to global session table */ - ASSERT (vec_len (segment_name) <= 128); - *len_seg_name = vec_len (segment_name); - memcpy (seg_name, segment_name, *len_seg_name); - *app = server; + /* Setup listen path down to transport */ + rv = application_start_listen (app, sep, handle); + if (rv && ll_handle != SESSION_INVALID_HANDLE) + session_lookup_del_session_endpoint (table_index, sep); - return 0; + /* + * Store in local table listener the index of the transport layer + * listener. We'll need local listeners are hit and we need to + * return global handle + */ + if (ll_handle != SESSION_INVALID_HANDLE) + { + local_session_t *ll; + stream_session_t *tl; + ll = application_get_local_listener_w_handle (ll_handle); + tl = listen_session_get_from_handle (*handle); + ll->transport_listener_index = tl->session_index; + } + return rv; } int -vnet_unbind_i (u32 api_client_index) +vnet_unbind_i (u32 app_index, session_handle_t handle) { - application_t *server; + application_t *app; + int rv; - /* - * Find the stream_server_t corresponding to the api client - */ - server = application_lookup (api_client_index); - if (!server) - return VNET_API_ERROR_INVALID_VALUE_2; + if (!(app = application_get_if_valid (app_index))) + { + SESSION_DBG ("app (%d) not attached", app_index); + return VNET_API_ERROR_APPLICATION_NOT_ATTACHED; + } - /* Clear the listener */ - stream_session_stop_listen (server->index); - application_del (server); + if (application_has_local_scope (app)) + { + if ((rv = application_stop_local_listen (app, handle))) + return rv; + } + /* + * Clear the global scope table of the listener + */ + if (application_has_global_scope (app)) + return application_stop_listen (app, handle); return 0; } int -vnet_connect_i (u32 api_client_index, u32 api_context, session_type_t sst, - ip46_address_t * ip46, u16 port, u64 * options, void *mp, - session_cb_vft_t * cb_fns) +application_connect (u32 client_index, u32 api_context, + session_endpoint_t * sep) { + application_t *server, *client; + u32 table_index, server_index, li; stream_session_t *listener; - application_t *server, *app; + local_session_t *ll; + u64 lh; + + if (session_endpoint_is_zero (sep)) + return VNET_API_ERROR_INVALID_VALUE; + + client = application_get (client_index); + session_endpoint_update_for_app (sep, client); /* - * Figure out if connecting to a local server + * First check the local scope for locally attached destinations. + * If we have local scope, we pass *all* connects through it since we may + * have special policy rules even for non-local destinations, think proxy. */ - listener = stream_session_lookup_listener (ip46, - clib_host_to_net_u16 (port), - sst); - if (listener) + if (application_has_local_scope (client)) { - server = application_get (listener->app_index); + table_index = application_local_session_table (client); + lh = session_lookup_local_endpoint (table_index, sep); + if (lh == SESSION_DROP_HANDLE) + return VNET_API_ERROR_APP_CONNECT_FILTERED; + + if (lh == SESSION_INVALID_HANDLE) + goto global_scope; + + local_session_parse_handle (lh, &server_index, &li); /* - * Server is willing to have a direct fifo connection created - * instead of going through the state machine, etc. + * Break loop if rule in local table points to connecting app. This + * can happen if client is a generic proxy. Route connect through + * global table instead. */ - if (server->flags & SESSION_OPTIONS_FLAGS_USE_FIFO) - return server->cb_fns. - redirect_connect_callback (server->api_client_index, mp); + if (server_index != client_index) + { + server = application_get (server_index); + ll = application_get_local_listen_session (server, li); + return application_local_session_connect (table_index, client, + server, ll, api_context); + } } - /* Create client app */ - app = application_new (APP_CLIENT, sst, api_client_index, - options[SESSION_OPTIONS_FLAGS], cb_fns); + /* + * If nothing found, check the global scope for locally attached + * destinations. Make sure first that we're allowed to. + */ + +global_scope: + if (session_endpoint_is_local (sep)) + return VNET_API_ERROR_SESSION_CONNECT; - app->api_context = api_context; + if (!application_has_global_scope (client)) + return VNET_API_ERROR_APP_CONNECT_SCOPE; + + table_index = application_session_table (client, + session_endpoint_fib_proto (sep)); + listener = session_lookup_listener (table_index, sep); + if (listener) + { + server = application_get (listener->app_index); + if (server) + return application_local_session_connect (table_index, client, server, + (local_session_t *) + listener, api_context); + } /* - * Not connecting to a local server. Create regular session + * Not connecting to a local server, propagate to transport */ - return stream_session_open (sst, ip46, port, app->index); + if (application_open_session (client, sep, api_context)) + return VNET_API_ERROR_SESSION_CONNECT; + return 0; } /** * unformat a vnet URI * - * fifo://name - * tcp://ip46-addr:port - * udp://ip46-addr:port + * transport-proto://ip46-addr:port + * eg. tcp://ip46-addr:port * * u8 ip46_address[16]; * u16 port_in_host_byte_order; @@ -209,241 +331,259 @@ vnet_connect_i (u32 api_client_index, u32 api_context, session_type_t sst, uword unformat_vnet_uri (unformat_input_t * input, va_list * args) { - ip46_address_t *address = va_arg (*args, ip46_address_t *); - session_type_t *sst = va_arg (*args, session_type_t *); - u16 *port = va_arg (*args, u16 *); + session_endpoint_t *sep = va_arg (*args, session_endpoint_t *); + u32 transport_proto = 0, port; - if (unformat (input, "tcp://%U/%d", unformat_ip4_address, &address->ip4, - port)) - { - *sst = SESSION_TYPE_IP4_TCP; - return 1; - } - if (unformat (input, "udp://%U/%d", unformat_ip4_address, &address->ip4, - port)) + if (unformat + (input, "%U://%U/%d", unformat_transport_proto, &transport_proto, + unformat_ip4_address, &sep->ip.ip4, &port)) { - *sst = SESSION_TYPE_IP4_UDP; + sep->transport_proto = transport_proto; + sep->port = clib_host_to_net_u16 (port); + sep->is_ip4 = 1; return 1; } - if (unformat (input, "udp://%U/%d", unformat_ip6_address, &address->ip6, - port)) + else if (unformat (input, "%U://%U/%d", unformat_transport_proto, + &transport_proto, unformat_ip6_address, &sep->ip.ip6, + &port)) { - *sst = SESSION_TYPE_IP6_UDP; + sep->transport_proto = transport_proto; + sep->port = clib_host_to_net_u16 (port); + sep->is_ip4 = 0; return 1; } - if (unformat (input, "tcp://%U/%d", unformat_ip6_address, &address->ip6, - port)) - { - *sst = SESSION_TYPE_IP6_TCP; - return 1; - } - return 0; } +static u8 *cache_uri; +static session_endpoint_t *cache_sep; + int -parse_uri (char *uri, session_type_t * sst, ip46_address_t * addr, - u16 * port_number_host_byte_order) +parse_uri (char *uri, session_endpoint_t * sep) { unformat_input_t _input, *input = &_input; + if (cache_uri && !strncmp (uri, (char *) cache_uri, vec_len (cache_uri))) + { + *sep = *cache_sep; + return 0; + } + /* Make sure */ uri = (char *) format (0, "%s%c", uri, 0); /* Parse uri */ unformat_init_string (input, uri, strlen (uri)); - if (!unformat (input, "%U", unformat_vnet_uri, addr, sst, - port_number_host_byte_order)) + if (!unformat (input, "%U", unformat_vnet_uri, sep)) { unformat_free (input); return VNET_API_ERROR_INVALID_VALUE; } unformat_free (input); + vec_free (cache_uri); + cache_uri = (u8 *) uri; + if (cache_sep) + clib_mem_free (cache_sep); + cache_sep = clib_mem_alloc (sizeof (*sep)); + *cache_sep = *sep; + return 0; } -int -vnet_bind_uri (vnet_bind_args_t * a) +static int +session_validate_namespace (u8 * namespace_id, u64 secret, u32 * app_ns_index) +{ + app_namespace_t *app_ns; + if (vec_len (namespace_id) == 0) + { + /* Use default namespace */ + *app_ns_index = 0; + return 0; + } + + *app_ns_index = app_namespace_index_from_id (namespace_id); + if (*app_ns_index == APP_NAMESPACE_INVALID_INDEX) + return VNET_API_ERROR_APP_INVALID_NS; + app_ns = app_namespace_get (*app_ns_index); + if (!app_ns) + return VNET_API_ERROR_APP_INVALID_NS; + if (app_ns->ns_secret != secret) + return VNET_API_ERROR_APP_WRONG_NS_SECRET; + return 0; +} + +/** + * Attach application to vpp + * + * Allocates a vpp app, i.e., a structure that keeps back pointers + * to external app and a segment manager for shared memory fifo based + * communication with the external app. + */ +clib_error_t * +vnet_application_attach (vnet_app_attach_args_t * a) { - application_t *server = 0; - u16 port_host_order; - session_type_t sst = SESSION_N_TYPES; - ip46_address_t ip46; + svm_fifo_segment_private_t *fs; + application_t *app = 0; + segment_manager_t *sm; + u32 app_ns_index = 0; + u64 secret; int rv; - memset (&ip46, 0, sizeof (ip46)); - rv = parse_uri (a->uri, &sst, &ip46, &port_host_order); - if (rv) - return rv; + app = application_lookup (a->api_client_index); + if (app) + return clib_error_return_code (0, VNET_API_ERROR_APP_ALREADY_ATTACHED, + 0, "app already attached"); - if ((rv = vnet_bind_i (a->api_client_index, &ip46, port_host_order, sst, - a->options, a->session_cb_vft, &server, - &a->segment_name_length, a->segment_name))) - return rv; + secret = a->options[APP_OPTIONS_NAMESPACE_SECRET]; + if ((rv = session_validate_namespace (a->namespace_id, secret, + &app_ns_index))) + return clib_error_return_code (0, rv, 0, "namespace validation: %d", rv); + a->options[APP_OPTIONS_NAMESPACE] = app_ns_index; + app = application_new (); + if ((rv = application_init (app, a->api_client_index, a->options, + a->session_cb_vft))) + return clib_error_return_code (0, rv, 0, "app init: %d", rv); + + a->app_event_queue_address = pointer_to_uword (app->event_queue); + sm = segment_manager_get (app->first_segment_manager); + fs = segment_manager_get_segment_w_lock (sm, 0); + + if (application_is_proxy (app)) + application_setup_proxy (app); + + ASSERT (vec_len (fs->ssvm.name) <= 128); + a->segment = &fs->ssvm; + a->app_index = app->index; + + segment_manager_segment_reader_unlock (sm); - a->server_event_queue_address = (u64) server->event_queue; return 0; } -session_type_t -session_type_from_proto_and_ip (session_api_proto_t proto, u8 is_ip4) +/** + * Detach application from vpp + */ +int +vnet_application_detach (vnet_app_detach_args_t * a) { - if (proto == SESSION_PROTO_TCP) - { - if (is_ip4) - return SESSION_TYPE_IP4_TCP; - else - return SESSION_TYPE_IP6_TCP; - } - else + application_t *app; + app = application_get_if_valid (a->app_index); + + if (!app) { - if (is_ip4) - return SESSION_TYPE_IP4_UDP; - else - return SESSION_TYPE_IP6_UDP; + clib_warning ("app not attached"); + return VNET_API_ERROR_APPLICATION_NOT_ATTACHED; } - return SESSION_N_TYPES; + application_del (app); + return 0; } int -vnet_unbind_uri (char *uri, u32 api_client_index) +vnet_bind_uri (vnet_bind_args_t * a) { - u16 port_number_host_byte_order; - session_type_t sst = SESSION_N_TYPES; - ip46_address_t ip46_address; - stream_session_t *listener; + session_endpoint_t sep = SESSION_ENDPOINT_NULL; int rv; - rv = parse_uri (uri, &sst, &ip46_address, &port_number_host_byte_order); + rv = parse_uri (a->uri, &sep); if (rv) return rv; - listener = - stream_session_lookup_listener (&ip46_address, - clib_host_to_net_u16 - (port_number_host_byte_order), sst); + return vnet_bind_i (a->app_index, &sep, &a->handle); +} + +int +vnet_unbind_uri (vnet_unbind_args_t * a) +{ + session_endpoint_t sep = SESSION_ENDPOINT_NULL; + stream_session_t *listener; + int rv; + + rv = parse_uri (a->uri, &sep); + if (rv) + return rv; + /* NOTE: only default table supported for uri */ + listener = session_lookup_listener (0, &sep); if (!listener) return VNET_API_ERROR_ADDRESS_NOT_IN_USE; - /* External client? */ - if (api_client_index != ~0) - { - ASSERT (vl_api_client_index_to_registration (api_client_index)); - } - - return vnet_unbind_i (api_client_index); + return vnet_unbind_i (a->app_index, listen_session_get_handle (listener)); } -int +clib_error_t * vnet_connect_uri (vnet_connect_args_t * a) { - ip46_address_t ip46_address; - u16 port; - session_type_t sst; - application_t *app; + session_endpoint_t sep = SESSION_ENDPOINT_NULL; int rv; - app = application_lookup (a->api_client_index); - if (app) - { - clib_warning ("Already have a connect from this app"); - return VNET_API_ERROR_INVALID_VALUE_2; - } - /* Parse uri */ - rv = parse_uri (a->uri, &sst, &ip46_address, &port); + rv = parse_uri (a->uri, &sep); if (rv) - return rv; + return clib_error_return_code (0, rv, 0, "app init: %d", rv); - return vnet_connect_i (a->api_client_index, a->api_context, sst, - &ip46_address, port, a->options, a->mp, - a->session_cb_vft); + if ((rv = application_connect (a->app_index, a->api_context, &sep))) + return clib_error_return_code (0, rv, 0, "connect failed"); + return 0; } int -vnet_disconnect_session (u32 session_index, u32 thread_index) +vnet_disconnect_session (vnet_disconnect_args_t * a) { - stream_session_t *session; + if (session_handle_is_local (a->handle)) + { + local_session_t *ls; + ls = application_get_local_session_from_handle (a->handle); + if (ls->app_index != a->app_index && ls->client_index != a->app_index) + { + clib_warning ("app %u is neither client nor server for session %u", + a->app_index, a->app_index); + return VNET_API_ERROR_INVALID_VALUE; + } + return application_local_session_disconnect (a->app_index, ls); + } + else + { + stream_session_t *s; + s = session_get_from_handle_if_valid (a->handle); + if (!s || s->app_index != a->app_index) + return VNET_API_ERROR_INVALID_VALUE; - session = stream_session_get (session_index, thread_index); - stream_session_disconnect (session); + /* We're peeking into another's thread pool. Make sure */ + ASSERT (s->session_index == session_index_from_handle (a->handle)); + stream_session_disconnect (s); + } return 0; } - -int +clib_error_t * vnet_bind (vnet_bind_args_t * a) { - application_t *server = 0; - session_type_t sst = SESSION_N_TYPES; int rv; - - sst = session_type_from_proto_and_ip (a->proto, a->tep.is_ip4); - if ((rv = vnet_bind_i (a->api_client_index, &a->tep.ip, a->tep.port, sst, - a->options, a->session_cb_vft, &server, - &a->segment_name_length, a->segment_name))) - return rv; - - a->server_event_queue_address = (u64) server->event_queue; - a->handle = (u64) a->tep.vrf << 32 | (u64) server->session_index; + if ((rv = vnet_bind_i (a->app_index, &a->sep, &a->handle))) + return clib_error_return_code (0, rv, 0, "bind failed"); return 0; } -int +clib_error_t * vnet_unbind (vnet_unbind_args_t * a) { - application_t *server; - - if (a->api_client_index != ~0) - { - ASSERT (vl_api_client_index_to_registration (a->api_client_index)); - } - - /* Make sure this is the right one */ - server = application_lookup (a->api_client_index); - ASSERT (server->session_index == (0xFFFFFFFF & a->handle)); - - /* TODO use handle to disambiguate namespaces/vrfs */ - return vnet_unbind_i (a->api_client_index); + int rv; + if ((rv = vnet_unbind_i (a->app_index, a->handle))) + return clib_error_return_code (0, rv, 0, "unbind failed"); + return 0; } -int +clib_error_t * vnet_connect (vnet_connect_args_t * a) { - session_type_t sst; - application_t *app; - - app = application_lookup (a->api_client_index); - if (app) - { - clib_warning ("Already have a connect from this app"); - return VNET_API_ERROR_INVALID_VALUE_2; - } - - sst = session_type_from_proto_and_ip (a->proto, a->tep.is_ip4); - return vnet_connect_i (a->api_client_index, a->api_context, sst, &a->tep.ip, - a->tep.port, a->options, a->mp, a->session_cb_vft); -} - -int -vnet_disconnect (vnet_disconnect_args_t * a) -{ - stream_session_t *session; - u32 session_index, thread_index; - - if (api_parse_session_handle (a->handle, &session_index, &thread_index)) - { - clib_warning ("Invalid handle"); - return -1; - } - - session = stream_session_get (session_index, thread_index); - stream_session_disconnect (session); + session_endpoint_t *sep = &a->sep; + int rv; + if ((rv = application_connect (a->app_index, a->api_context, sep))) + return clib_error_return_code (0, rv, 0, "connect failed"); return 0; }