Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1 | /* |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 2 | * Copyright (c) 2016-2019 Cisco and/or its affiliates. |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 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> |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 16 | #include <vnet/session/application.h> |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 17 | #include <vnet/session/session.h> |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 18 | |
| 19 | /** @file |
| 20 | VPP's application/session API bind/unbind/connect/disconnect calls |
| 21 | */ |
| 22 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 23 | /** |
| 24 | * unformat a vnet URI |
| 25 | * |
Florin Coras | 8f89dd0 | 2018-03-05 16:53:07 -0800 | [diff] [blame] | 26 | * transport-proto://[hostname]ip46-addr:port |
| 27 | * eg. tcp://ip46-addr:port |
| 28 | * tls://[testtsl.fd.io]ip46-addr:port |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 29 | * |
| 30 | * u8 ip46_address[16]; |
| 31 | * u16 port_in_host_byte_order; |
| 32 | * stream_session_type_t sst; |
| 33 | * u8 *fifo_name; |
| 34 | * |
| 35 | * if (unformat (input, "%U", unformat_vnet_uri, &ip46_address, |
| 36 | * &sst, &port, &fifo_name)) |
| 37 | * etc... |
| 38 | * |
| 39 | */ |
| 40 | uword |
| 41 | unformat_vnet_uri (unformat_input_t * input, va_list * args) |
| 42 | { |
Florin Coras | 5665ced | 2018-10-25 18:03:45 -0700 | [diff] [blame] | 43 | session_endpoint_cfg_t *sep = va_arg (*args, session_endpoint_cfg_t *); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 44 | u32 transport_proto = 0, port; |
| 45 | |
Florin Coras | 8f89dd0 | 2018-03-05 16:53:07 -0800 | [diff] [blame] | 46 | if (unformat (input, "%U://%U/%d", unformat_transport_proto, |
| 47 | &transport_proto, unformat_ip4_address, &sep->ip.ip4, &port)) |
| 48 | { |
| 49 | sep->transport_proto = transport_proto; |
| 50 | sep->port = clib_host_to_net_u16 (port); |
| 51 | sep->is_ip4 = 1; |
| 52 | return 1; |
| 53 | } |
| 54 | else if (unformat (input, "%U://[%s]%U/%d", unformat_transport_proto, |
| 55 | &transport_proto, &sep->hostname, unformat_ip4_address, |
| 56 | &sep->ip.ip4, &port)) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 57 | { |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 58 | sep->transport_proto = transport_proto; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 59 | sep->port = clib_host_to_net_u16 (port); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 60 | sep->is_ip4 = 1; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 61 | return 1; |
| 62 | } |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 63 | else if (unformat (input, "%U://%U/%d", unformat_transport_proto, |
| 64 | &transport_proto, unformat_ip6_address, &sep->ip.ip6, |
| 65 | &port)) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 66 | { |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 67 | sep->transport_proto = transport_proto; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 68 | sep->port = clib_host_to_net_u16 (port); |
Marco Varlese | 191a594 | 2017-10-30 18:17:21 +0100 | [diff] [blame] | 69 | sep->is_ip4 = 0; |
| 70 | return 1; |
| 71 | } |
Florin Coras | 8f89dd0 | 2018-03-05 16:53:07 -0800 | [diff] [blame] | 72 | else if (unformat (input, "%U://[%s]%U/%d", unformat_transport_proto, |
| 73 | &transport_proto, &sep->hostname, unformat_ip6_address, |
| 74 | &sep->ip.ip6, &port)) |
| 75 | { |
| 76 | sep->transport_proto = transport_proto; |
| 77 | sep->port = clib_host_to_net_u16 (port); |
| 78 | sep->is_ip4 = 0; |
| 79 | return 1; |
| 80 | } |
Aloys Augustin | ba123e1 | 2019-05-14 14:13:51 +0200 | [diff] [blame] | 81 | else if (unformat (input, "%U://session/%lu", unformat_transport_proto, |
Nathan Skrzypczak | 8ac1d6d | 2019-07-17 11:02:20 +0200 | [diff] [blame] | 82 | &transport_proto, &sep->parent_handle)) |
Aloys Augustin | 502785b | 2019-04-09 11:40:57 +0200 | [diff] [blame] | 83 | { |
| 84 | sep->transport_proto = transport_proto; |
Aloys Augustin | 502785b | 2019-04-09 11:40:57 +0200 | [diff] [blame] | 85 | sep->ip.ip4.as_u32 = 1; /* ip need to be non zero in vnet */ |
| 86 | return 1; |
| 87 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 88 | return 0; |
| 89 | } |
| 90 | |
Dave Barach | b7f1faa | 2017-08-29 11:43:37 -0400 | [diff] [blame] | 91 | static u8 *cache_uri; |
Florin Coras | 5665ced | 2018-10-25 18:03:45 -0700 | [diff] [blame] | 92 | static session_endpoint_cfg_t *cache_sep; |
Dave Barach | b7f1faa | 2017-08-29 11:43:37 -0400 | [diff] [blame] | 93 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 94 | int |
Florin Coras | 5665ced | 2018-10-25 18:03:45 -0700 | [diff] [blame] | 95 | parse_uri (char *uri, session_endpoint_cfg_t * sep) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 96 | { |
| 97 | unformat_input_t _input, *input = &_input; |
| 98 | |
Dave Barach | b7f1faa | 2017-08-29 11:43:37 -0400 | [diff] [blame] | 99 | if (cache_uri && !strncmp (uri, (char *) cache_uri, vec_len (cache_uri))) |
| 100 | { |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 101 | *sep = *cache_sep; |
Dave Barach | b7f1faa | 2017-08-29 11:43:37 -0400 | [diff] [blame] | 102 | return 0; |
| 103 | } |
| 104 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 105 | /* Make sure */ |
| 106 | uri = (char *) format (0, "%s%c", uri, 0); |
| 107 | |
| 108 | /* Parse uri */ |
| 109 | unformat_init_string (input, uri, strlen (uri)); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 110 | if (!unformat (input, "%U", unformat_vnet_uri, sep)) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 111 | { |
| 112 | unformat_free (input); |
| 113 | return VNET_API_ERROR_INVALID_VALUE; |
| 114 | } |
| 115 | unformat_free (input); |
| 116 | |
Dave Barach | b7f1faa | 2017-08-29 11:43:37 -0400 | [diff] [blame] | 117 | vec_free (cache_uri); |
| 118 | cache_uri = (u8 *) uri; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 119 | if (cache_sep) |
| 120 | clib_mem_free (cache_sep); |
| 121 | cache_sep = clib_mem_alloc (sizeof (*sep)); |
| 122 | *cache_sep = *sep; |
Dave Barach | b7f1faa | 2017-08-29 11:43:37 -0400 | [diff] [blame] | 123 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 124 | return 0; |
| 125 | } |
| 126 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 127 | int |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 128 | vnet_bind_uri (vnet_listen_args_t * a) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 129 | { |
Florin Coras | 5665ced | 2018-10-25 18:03:45 -0700 | [diff] [blame] | 130 | session_endpoint_cfg_t sep = SESSION_ENDPOINT_CFG_NULL; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 131 | int rv; |
| 132 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 133 | rv = parse_uri (a->uri, &sep); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 134 | if (rv) |
| 135 | return rv; |
Florin Coras | 41c9e04 | 2018-09-11 00:10:41 -0700 | [diff] [blame] | 136 | sep.app_wrk_index = 0; |
Florin Coras | c3638fe | 2018-08-24 13:58:49 -0700 | [diff] [blame] | 137 | clib_memcpy (&a->sep_ext, &sep, sizeof (sep)); |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 138 | return vnet_listen (a); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | int |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 142 | vnet_unbind_uri (vnet_unlisten_args_t * a) |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 143 | { |
Florin Coras | 5665ced | 2018-10-25 18:03:45 -0700 | [diff] [blame] | 144 | session_endpoint_cfg_t sep = SESSION_ENDPOINT_CFG_NULL; |
Nathan Skrzypczak | 61ad550 | 2019-07-05 11:58:22 +0200 | [diff] [blame] | 145 | application_t *app; |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 146 | session_t *listener; |
Florin Coras | 36ec1f5 | 2018-05-29 21:15:38 -0700 | [diff] [blame] | 147 | u32 table_index; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 148 | int rv; |
| 149 | |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 150 | if ((rv = parse_uri (a->uri, &sep))) |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 151 | return rv; |
| 152 | |
Nathan Skrzypczak | 61ad550 | 2019-07-05 11:58:22 +0200 | [diff] [blame] | 153 | app = application_get (a->app_index); |
| 154 | if (!app) |
| 155 | return VNET_API_ERROR_INVALID_VALUE; |
| 156 | |
| 157 | table_index = application_session_table (app, fib_ip_proto (!sep.is_ip4)); |
Florin Coras | 36ec1f5 | 2018-05-29 21:15:38 -0700 | [diff] [blame] | 158 | listener = session_lookup_listener (table_index, |
| 159 | (session_endpoint_t *) & sep); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 160 | if (!listener) |
| 161 | return VNET_API_ERROR_ADDRESS_NOT_IN_USE; |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 162 | a->handle = listen_session_get_handle (listener); |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 163 | return vnet_unlisten (a); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 164 | } |
| 165 | |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 166 | int |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 167 | vnet_connect_uri (vnet_connect_args_t * a) |
| 168 | { |
Florin Coras | 5665ced | 2018-10-25 18:03:45 -0700 | [diff] [blame] | 169 | session_endpoint_cfg_t sep = SESSION_ENDPOINT_CFG_NULL; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 170 | int rv; |
| 171 | |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 172 | if ((rv = parse_uri (a->uri, &sep))) |
| 173 | return rv; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 174 | |
Florin Coras | c3638fe | 2018-08-24 13:58:49 -0700 | [diff] [blame] | 175 | clib_memcpy (&a->sep_ext, &sep, sizeof (sep)); |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 176 | if ((rv = vnet_connect (a))) |
| 177 | return rv; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 178 | return 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 179 | } |
| 180 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 181 | /* |
| 182 | * fd.io coding-style-patch-verification: ON |
| 183 | * |
| 184 | * Local Variables: |
| 185 | * eval: (c-set-style "gnu") |
| 186 | * End: |
| 187 | */ |