blob: 74f456a1eabbe0f631655755d77b71b9f1ae4b59 [file] [log] [blame]
Dave Barach68b0fb02017-02-28 15:15:56 -05001/*
Florin Coras288eaab2019-02-03 15:26:14 -08002 * Copyright (c) 2016-2019 Cisco and/or its affiliates.
Dave Barach68b0fb02017-02-28 15:15:56 -05003 * 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 Corasc1a42652019-02-08 18:27:29 -080016#include <vnet/session/application.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050017#include <vnet/session/session.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050018
19/** @file
20 VPP's application/session API bind/unbind/connect/disconnect calls
21*/
22
Dave Barach68b0fb02017-02-28 15:15:56 -050023/**
24 * unformat a vnet URI
25 *
Florin Coras8f89dd02018-03-05 16:53:07 -080026 * transport-proto://[hostname]ip46-addr:port
27 * eg. tcp://ip46-addr:port
28 * tls://[testtsl.fd.io]ip46-addr:port
Dave Barach68b0fb02017-02-28 15:15:56 -050029 *
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 */
40uword
41unformat_vnet_uri (unformat_input_t * input, va_list * args)
42{
Florin Coras5665ced2018-10-25 18:03:45 -070043 session_endpoint_cfg_t *sep = va_arg (*args, session_endpoint_cfg_t *);
Florin Coras371ca502018-02-21 12:07:41 -080044 u32 transport_proto = 0, port;
45
Florin Coras8f89dd02018-03-05 16:53:07 -080046 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 }
Florin Coras371ca502018-02-21 12:07:41 -080054 else if (unformat (input, "%U://%U/%d", unformat_transport_proto,
55 &transport_proto, unformat_ip6_address, &sep->ip.ip6,
56 &port))
Dave Barach68b0fb02017-02-28 15:15:56 -050057 {
Florin Coras4399c2e2018-01-25 06:34:42 -080058 sep->transport_proto = transport_proto;
Florin Coras371ca502018-02-21 12:07:41 -080059 sep->port = clib_host_to_net_u16 (port);
Marco Varlese191a5942017-10-30 18:17:21 +010060 sep->is_ip4 = 0;
61 return 1;
62 }
Aloys Augustinba123e12019-05-14 14:13:51 +020063 else if (unformat (input, "%U://session/%lu", unformat_transport_proto,
Nathan Skrzypczak8ac1d6d2019-07-17 11:02:20 +020064 &transport_proto, &sep->parent_handle))
Aloys Augustin502785b2019-04-09 11:40:57 +020065 {
66 sep->transport_proto = transport_proto;
Aloys Augustin502785b2019-04-09 11:40:57 +020067 sep->ip.ip4.as_u32 = 1; /* ip need to be non zero in vnet */
68 return 1;
69 }
Dave Barach68b0fb02017-02-28 15:15:56 -050070 return 0;
71}
72
Dave Barachb7f1faa2017-08-29 11:43:37 -040073static u8 *cache_uri;
Florin Coras5665ced2018-10-25 18:03:45 -070074static session_endpoint_cfg_t *cache_sep;
Dave Barachb7f1faa2017-08-29 11:43:37 -040075
Dave Barach68b0fb02017-02-28 15:15:56 -050076int
Florin Coras5665ced2018-10-25 18:03:45 -070077parse_uri (char *uri, session_endpoint_cfg_t * sep)
Dave Barach68b0fb02017-02-28 15:15:56 -050078{
79 unformat_input_t _input, *input = &_input;
80
Dave Barachb7f1faa2017-08-29 11:43:37 -040081 if (cache_uri && !strncmp (uri, (char *) cache_uri, vec_len (cache_uri)))
82 {
Florin Corascea194d2017-10-02 00:18:51 -070083 *sep = *cache_sep;
Dave Barachb7f1faa2017-08-29 11:43:37 -040084 return 0;
85 }
86
Dave Barach68b0fb02017-02-28 15:15:56 -050087 /* Make sure */
88 uri = (char *) format (0, "%s%c", uri, 0);
89
90 /* Parse uri */
91 unformat_init_string (input, uri, strlen (uri));
Florin Corascea194d2017-10-02 00:18:51 -070092 if (!unformat (input, "%U", unformat_vnet_uri, sep))
Dave Barach68b0fb02017-02-28 15:15:56 -050093 {
94 unformat_free (input);
95 return VNET_API_ERROR_INVALID_VALUE;
96 }
97 unformat_free (input);
98
Dave Barachb7f1faa2017-08-29 11:43:37 -040099 vec_free (cache_uri);
100 cache_uri = (u8 *) uri;
Florin Corascea194d2017-10-02 00:18:51 -0700101 if (cache_sep)
102 clib_mem_free (cache_sep);
103 cache_sep = clib_mem_alloc (sizeof (*sep));
104 *cache_sep = *sep;
Dave Barachb7f1faa2017-08-29 11:43:37 -0400105
Dave Barach68b0fb02017-02-28 15:15:56 -0500106 return 0;
107}
108
Dave Barach68b0fb02017-02-28 15:15:56 -0500109int
Florin Corasc9940fc2019-02-05 20:55:11 -0800110vnet_bind_uri (vnet_listen_args_t * a)
Dave Barach68b0fb02017-02-28 15:15:56 -0500111{
Florin Coras5665ced2018-10-25 18:03:45 -0700112 session_endpoint_cfg_t sep = SESSION_ENDPOINT_CFG_NULL;
Dave Barach68b0fb02017-02-28 15:15:56 -0500113 int rv;
114
Florin Corascea194d2017-10-02 00:18:51 -0700115 rv = parse_uri (a->uri, &sep);
Dave Barach68b0fb02017-02-28 15:15:56 -0500116 if (rv)
117 return rv;
Florin Coras41c9e042018-09-11 00:10:41 -0700118 sep.app_wrk_index = 0;
Florin Corasc3638fe2018-08-24 13:58:49 -0700119 clib_memcpy (&a->sep_ext, &sep, sizeof (sep));
Florin Corasc1a42652019-02-08 18:27:29 -0800120 return vnet_listen (a);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700121}
122
123int
Florin Corasc1a42652019-02-08 18:27:29 -0800124vnet_unbind_uri (vnet_unlisten_args_t * a)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700125{
Florin Coras5665ced2018-10-25 18:03:45 -0700126 session_endpoint_cfg_t sep = SESSION_ENDPOINT_CFG_NULL;
Nathan Skrzypczak61ad5502019-07-05 11:58:22 +0200127 application_t *app;
Florin Coras288eaab2019-02-03 15:26:14 -0800128 session_t *listener;
Florin Coras36ec1f52018-05-29 21:15:38 -0700129 u32 table_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700130 int rv;
131
Florin Corasc1a42652019-02-08 18:27:29 -0800132 if ((rv = parse_uri (a->uri, &sep)))
Florin Coras6cf30ad2017-04-04 23:08:23 -0700133 return rv;
134
Nathan Skrzypczak61ad5502019-07-05 11:58:22 +0200135 app = application_get (a->app_index);
136 if (!app)
137 return VNET_API_ERROR_INVALID_VALUE;
138
139 table_index = application_session_table (app, fib_ip_proto (!sep.is_ip4));
Florin Coras36ec1f52018-05-29 21:15:38 -0700140 listener = session_lookup_listener (table_index,
141 (session_endpoint_t *) & sep);
Dave Barach68b0fb02017-02-28 15:15:56 -0500142 if (!listener)
143 return VNET_API_ERROR_ADDRESS_NOT_IN_USE;
Florin Corasab2f6db2018-08-31 14:31:41 -0700144 a->handle = listen_session_get_handle (listener);
Florin Corasc1a42652019-02-08 18:27:29 -0800145 return vnet_unlisten (a);
Dave Barach68b0fb02017-02-28 15:15:56 -0500146}
147
Florin Corasc1a42652019-02-08 18:27:29 -0800148int
Dave Barach68b0fb02017-02-28 15:15:56 -0500149vnet_connect_uri (vnet_connect_args_t * a)
150{
Florin Coras5665ced2018-10-25 18:03:45 -0700151 session_endpoint_cfg_t sep = SESSION_ENDPOINT_CFG_NULL;
Dave Barach68b0fb02017-02-28 15:15:56 -0500152 int rv;
153
Florin Corasc1a42652019-02-08 18:27:29 -0800154 if ((rv = parse_uri (a->uri, &sep)))
155 return rv;
Florin Coras371ca502018-02-21 12:07:41 -0800156
Florin Corasc3638fe2018-08-24 13:58:49 -0700157 clib_memcpy (&a->sep_ext, &sep, sizeof (sep));
Florin Corasc1a42652019-02-08 18:27:29 -0800158 if ((rv = vnet_connect (a)))
159 return rv;
Florin Corascea194d2017-10-02 00:18:51 -0700160 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500161}
162
Dave Barach68b0fb02017-02-28 15:15:56 -0500163/*
164 * fd.io coding-style-patch-verification: ON
165 *
166 * Local Variables:
167 * eval: (c-set-style "gnu")
168 * End:
169 */