blob: b25911eb3063da5f0df4f6cc4fb078fdc1ae57d3 [file] [log] [blame]
Dave Barach68b0fb02017-02-28 15:15:56 -05001/*
2 * Copyright (c) 2015-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
16#include <vnet/vnet.h>
17#include <vlibmemory/api.h>
18#include <vnet/session/application.h>
Florin Coras1c710452017-10-17 00:03:13 -070019#include <vnet/session/application_interface.h>
20#include <vnet/session/session_rules_table.h>
Florin Coras6c36f532017-11-03 18:32:34 -070021#include <vnet/session/session_table.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050022
23#include <vnet/vnet_msg_enum.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050024
25#define vl_typedefs /* define message structures */
26#include <vnet/vnet_all_api_h.h>
27#undef vl_typedefs
28
29#define vl_endianfun /* define message structures */
30#include <vnet/vnet_all_api_h.h>
31#undef vl_endianfun
32
33/* instantiate all the print functions we know about */
34#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
35#define vl_printfun
36#include <vnet/vnet_all_api_h.h>
37#undef vl_printfun
38
39#include <vlibapi/api_helper_macros.h>
40
41#define foreach_session_api_msg \
42_(MAP_ANOTHER_SEGMENT_REPLY, map_another_segment_reply) \
Florin Coras6cf30ad2017-04-04 23:08:23 -070043_(APPLICATION_ATTACH, application_attach) \
44_(APPLICATION_DETACH, application_detach) \
Dave Barach68b0fb02017-02-28 15:15:56 -050045_(BIND_URI, bind_uri) \
46_(UNBIND_URI, unbind_uri) \
47_(CONNECT_URI, connect_uri) \
48_(DISCONNECT_SESSION, disconnect_session) \
49_(DISCONNECT_SESSION_REPLY, disconnect_session_reply) \
50_(ACCEPT_SESSION_REPLY, accept_session_reply) \
51_(RESET_SESSION_REPLY, reset_session_reply) \
Florin Corascea194d2017-10-02 00:18:51 -070052_(BIND_SOCK, bind_sock) \
Dave Barach68b0fb02017-02-28 15:15:56 -050053_(UNBIND_SOCK, unbind_sock) \
54_(CONNECT_SOCK, connect_sock) \
Florin Corase04c2992017-03-01 08:17:34 -080055_(SESSION_ENABLE_DISABLE, session_enable_disable) \
Florin Corascea194d2017-10-02 00:18:51 -070056_(APP_NAMESPACE_ADD_DEL, app_namespace_add_del) \
Florin Coras1c710452017-10-17 00:03:13 -070057_(SESSION_RULE_ADD_DEL, session_rule_add_del) \
Florin Coras6c36f532017-11-03 18:32:34 -070058_(SESSION_RULES_DUMP, session_rules_dump) \
Florin Coras371ca502018-02-21 12:07:41 -080059_(APPLICATION_TLS_CERT_ADD, application_tls_cert_add) \
60_(APPLICATION_TLS_KEY_ADD, application_tls_key_add) \
Florin Corase04c2992017-03-01 08:17:34 -080061
Dave Barach68b0fb02017-02-28 15:15:56 -050062static int
Florin Corasb384b542018-01-15 01:08:33 -080063session_send_memfd_fd (vl_api_registration_t * reg, const ssvm_private_t * sp)
64{
65 clib_error_t *error;
66 if (vl_api_registration_file_index (reg) == VL_API_INVALID_FI)
67 {
68 clib_warning ("can't send memfd fd");
69 return -1;
70 }
71 error = vl_api_send_fd_msg (reg, sp->fd);
72 if (error)
73 {
74 clib_error_report (error);
75 return -1;
76 }
77 return 0;
78}
79
80static int
81send_add_segment_callback (u32 api_client_index, const ssvm_private_t * sp)
Dave Barach68b0fb02017-02-28 15:15:56 -050082{
83 vl_api_map_another_segment_t *mp;
Florin Corasb384b542018-01-15 01:08:33 -080084 vl_api_registration_t *reg;
Dave Barach68b0fb02017-02-28 15:15:56 -050085
Florin Corasb384b542018-01-15 01:08:33 -080086 reg = vl_mem_api_client_index_to_registration (api_client_index);
87 if (!reg)
88 {
89 clib_warning ("no registration: %u", api_client_index);
90 return -1;
91 }
Dave Barach68b0fb02017-02-28 15:15:56 -050092
Florin Corasb384b542018-01-15 01:08:33 -080093 if (ssvm_type (sp) == SSVM_SEGMENT_MEMFD
94 && vl_api_registration_file_index (reg) == VL_API_INVALID_FI)
95 {
96 clib_warning ("can't send memfd fd");
97 return -1;
98 }
Dave Barach68b0fb02017-02-28 15:15:56 -050099
Florin Coras7a2e1bd2017-11-30 16:07:39 -0500100 mp = vl_msg_api_alloc_as_if_client (sizeof (*mp));
Dave Barach68b0fb02017-02-28 15:15:56 -0500101 memset (mp, 0, sizeof (*mp));
102 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_MAP_ANOTHER_SEGMENT);
Florin Corasb384b542018-01-15 01:08:33 -0800103 mp->segment_size = sp->ssvm_size;
104 strncpy ((char *) mp->segment_name, (char *) sp->name,
Dave Barach68b0fb02017-02-28 15:15:56 -0500105 sizeof (mp->segment_name) - 1);
106
Florin Corasb384b542018-01-15 01:08:33 -0800107 vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp);
108
109 if (ssvm_type (sp) == SSVM_SEGMENT_MEMFD)
110 return session_send_memfd_fd (reg, sp);
Dave Barach68b0fb02017-02-28 15:15:56 -0500111
112 return 0;
113}
114
115static int
Florin Corasf8f516a2018-02-08 15:10:09 -0800116send_del_segment_callback (u32 api_client_index, const ssvm_private_t * fs)
117{
118 vl_api_unmap_segment_t *mp;
119 vl_api_registration_t *reg;
120
121 reg = vl_mem_api_client_index_to_registration (api_client_index);
122 if (!reg)
123 {
124 clib_warning ("no registration: %u", api_client_index);
125 return -1;
126 }
127
128 if (ssvm_type (fs) == SSVM_SEGMENT_MEMFD
129 && vl_api_registration_file_index (reg) == VL_API_INVALID_FI)
130 {
131 clib_warning ("can't send memfd fd");
132 return -1;
133 }
134
135 mp = vl_msg_api_alloc_as_if_client (sizeof (*mp));
136 memset (mp, 0, sizeof (*mp));
137 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_UNMAP_SEGMENT);
138 strncpy ((char *) mp->segment_name, (char *) fs->name,
139 sizeof (mp->segment_name) - 1);
140
141 vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp);
142
143 if (ssvm_type (fs) == SSVM_SEGMENT_MEMFD)
144 return session_send_memfd_fd (reg, fs);
145
146 return 0;
147}
148
149static int
Florin Coras6cf30ad2017-04-04 23:08:23 -0700150send_session_accept_callback (stream_session_t * s)
Dave Barach68b0fb02017-02-28 15:15:56 -0500151{
Dave Barach68b0fb02017-02-28 15:15:56 -0500152 application_t *server = application_get (s->app_index);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700153 transport_proto_vft_t *tp_vft;
Florin Corasb384b542018-01-15 01:08:33 -0800154 vl_api_accept_session_t *mp;
155 vl_api_registration_t *reg;
156 transport_connection_t *tc;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700157 stream_session_t *listener;
Florin Corasb384b542018-01-15 01:08:33 -0800158 svm_queue_t *vpp_queue;
Dave Barach68b0fb02017-02-28 15:15:56 -0500159
Florin Corasb384b542018-01-15 01:08:33 -0800160 reg = vl_mem_api_client_index_to_registration (server->api_client_index);
161 if (!reg)
162 {
163 clib_warning ("no registration: %u", server->api_client_index);
164 return -1;
165 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500166
Florin Corasb384b542018-01-15 01:08:33 -0800167 mp = vl_mem_api_alloc_as_if_client_w_reg (reg, sizeof (*mp));
Florin Coras6cf30ad2017-04-04 23:08:23 -0700168 memset (mp, 0, sizeof (*mp));
169
Dave Barach68b0fb02017-02-28 15:15:56 -0500170 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_ACCEPT_SESSION);
Florin Corasa5464812017-04-19 13:00:05 -0700171 mp->context = server->index;
Damjan Marion7bee80c2017-04-26 15:32:12 +0200172 mp->server_rx_fifo = pointer_to_uword (s->server_rx_fifo);
173 mp->server_tx_fifo = pointer_to_uword (s->server_tx_fifo);
Florin Corasf8f516a2018-02-08 15:10:09 -0800174
175 if (session_has_transport (s))
176 {
177 listener = listen_session_get (s->session_type, s->listener_index);
178 mp->listener_handle = listen_session_get_handle (listener);
179 if (application_is_proxy (server))
180 {
181 listener =
182 application_first_listener (server, session_get_fib_proto (s),
183 session_get_transport_proto (s));
184 if (listener)
185 mp->listener_handle = listen_session_get_handle (listener);
186 }
187 vpp_queue = session_manager_get_vpp_event_queue (s->thread_index);
188 mp->vpp_event_queue_address = pointer_to_uword (vpp_queue);
189 mp->handle = session_handle (s);
190 tp_vft = transport_protocol_get_vft (session_get_transport_proto (s));
191 tc = tp_vft->get_connection (s->connection_index, s->thread_index);
192 mp->port = tc->rmt_port;
193 mp->is_ip4 = tc->is_ip4;
194 clib_memcpy (&mp->ip, &tc->rmt_ip, sizeof (tc->rmt_ip));
195 }
196 else
197 {
198 local_session_t *ls = (local_session_t *) s;
199 local_session_t *ll;
200 if (application_local_session_listener_has_transport (ls))
201 {
202 listener = listen_session_get (ls->listener_session_type,
203 ls->listener_index);
204 mp->listener_handle = listen_session_get_handle (listener);
Florin Coras5fda7a32018-02-14 08:04:31 -0800205 mp->is_ip4 = session_type_is_ip4 (listener->session_type);
Florin Corasf8f516a2018-02-08 15:10:09 -0800206 }
207 else
208 {
209 ll = application_get_local_listen_session (server,
210 ls->listener_index);
Florin Coras5fda7a32018-02-14 08:04:31 -0800211 if (ll->transport_listener_index != ~0)
212 {
213 listener = listen_session_get (ll->listener_session_type,
214 ll->transport_listener_index);
215 mp->listener_handle = listen_session_get_handle (listener);
216 }
217 else
218 {
219 mp->listener_handle = application_local_session_handle (ll);
220 }
221 mp->is_ip4 = session_type_is_ip4 (ll->listener_session_type);
Florin Corasf8f516a2018-02-08 15:10:09 -0800222 }
223 mp->handle = application_local_session_handle (ls);
224 mp->port = ls->port;
225 mp->vpp_event_queue_address = ls->client_evt_q;
226 mp->server_event_queue_address = ls->server_evt_q;
227 }
Florin Corasb384b542018-01-15 01:08:33 -0800228 vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp);
Dave Barach68b0fb02017-02-28 15:15:56 -0500229
230 return 0;
231}
232
Florin Corasf8f516a2018-02-08 15:10:09 -0800233void
234send_local_session_disconnect_callback (u32 app_index, local_session_t * ls)
235{
236 application_t *app = application_get (app_index);
237 vl_api_disconnect_session_t *mp;
238 vl_api_registration_t *reg;
239
240 reg = vl_mem_api_client_index_to_registration (app->api_client_index);
241 if (!reg)
242 {
243 clib_warning ("no registration: %u", app->api_client_index);
244 return;
245 }
246
247 mp = vl_mem_api_alloc_as_if_client_w_reg (reg, sizeof (*mp));
248 memset (mp, 0, sizeof (*mp));
249 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_DISCONNECT_SESSION);
250 mp->handle = application_local_session_handle (ls);
251 mp->context = app->api_client_index;
252 vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp);
253}
254
Dave Barach68b0fb02017-02-28 15:15:56 -0500255static void
Florin Coras6cf30ad2017-04-04 23:08:23 -0700256send_session_disconnect_callback (stream_session_t * s)
Dave Barach68b0fb02017-02-28 15:15:56 -0500257{
Dave Barach68b0fb02017-02-28 15:15:56 -0500258 application_t *app = application_get (s->app_index);
Florin Corasb384b542018-01-15 01:08:33 -0800259 vl_api_disconnect_session_t *mp;
260 vl_api_registration_t *reg;
Dave Barach68b0fb02017-02-28 15:15:56 -0500261
Florin Corasb384b542018-01-15 01:08:33 -0800262 reg = vl_mem_api_client_index_to_registration (app->api_client_index);
263 if (!reg)
264 {
265 clib_warning ("no registration: %u", app->api_client_index);
266 return;
267 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500268
Florin Corasb384b542018-01-15 01:08:33 -0800269 mp = vl_mem_api_alloc_as_if_client_w_reg (reg, sizeof (*mp));
Dave Barach68b0fb02017-02-28 15:15:56 -0500270 memset (mp, 0, sizeof (*mp));
271 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_DISCONNECT_SESSION);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700272 mp->handle = session_handle (s);
Florin Corasf8f516a2018-02-08 15:10:09 -0800273 mp->context = app->api_client_index;
Florin Corasb384b542018-01-15 01:08:33 -0800274 vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp);
Dave Barach68b0fb02017-02-28 15:15:56 -0500275}
276
Florin Corasd79b41e2017-03-04 05:37:52 -0800277static void
Florin Coras6cf30ad2017-04-04 23:08:23 -0700278send_session_reset_callback (stream_session_t * s)
Florin Corasd79b41e2017-03-04 05:37:52 -0800279{
Florin Corasd79b41e2017-03-04 05:37:52 -0800280 application_t *app = application_get (s->app_index);
Florin Corasb384b542018-01-15 01:08:33 -0800281 vl_api_registration_t *reg;
282 vl_api_reset_session_t *mp;
Florin Corasd79b41e2017-03-04 05:37:52 -0800283
Florin Corasb384b542018-01-15 01:08:33 -0800284 reg = vl_mem_api_client_index_to_registration (app->api_client_index);
285 if (!reg)
286 {
287 clib_warning ("no registration: %u", app->api_client_index);
288 return;
289 }
Florin Corasd79b41e2017-03-04 05:37:52 -0800290
Florin Corasb384b542018-01-15 01:08:33 -0800291 mp = vl_mem_api_alloc_as_if_client_w_reg (reg, sizeof (*mp));
Florin Corasd79b41e2017-03-04 05:37:52 -0800292 memset (mp, 0, sizeof (*mp));
293 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_RESET_SESSION);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700294 mp->handle = session_handle (s);
Florin Corasb384b542018-01-15 01:08:33 -0800295 vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp);
Florin Corasd79b41e2017-03-04 05:37:52 -0800296}
297
Dave Barach0194f1a2017-05-15 10:11:39 -0400298int
Florin Coras6cf30ad2017-04-04 23:08:23 -0700299send_session_connected_callback (u32 app_index, u32 api_context,
300 stream_session_t * s, u8 is_fail)
Dave Barach68b0fb02017-02-28 15:15:56 -0500301{
Dave Wallace33e002b2017-09-06 01:20:02 -0400302 vl_api_connect_session_reply_t *mp;
Florin Corasade70e42017-10-14 18:56:41 -0700303 transport_connection_t *tc;
Florin Corasb384b542018-01-15 01:08:33 -0800304 vl_api_registration_t *reg;
305 svm_queue_t *vpp_queue;
306 application_t *app;
Dave Barach68b0fb02017-02-28 15:15:56 -0500307
Florin Coras6cf30ad2017-04-04 23:08:23 -0700308 app = application_get (app_index);
Florin Corasb384b542018-01-15 01:08:33 -0800309 reg = vl_mem_api_client_index_to_registration (app->api_client_index);
310 if (!reg)
311 {
312 clib_warning ("no registration: %u", app->api_client_index);
313 return -1;
314 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500315
Florin Corasb384b542018-01-15 01:08:33 -0800316 mp = vl_mem_api_alloc_as_if_client_w_reg (reg, sizeof (*mp));
Dave Wallace33e002b2017-09-06 01:20:02 -0400317 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_CONNECT_SESSION_REPLY);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700318 mp->context = api_context;
Dave Wallace965fec92017-10-17 16:19:41 -0400319
320 if (is_fail)
321 goto done;
322
Florin Corasf8f516a2018-02-08 15:10:09 -0800323 if (session_has_transport (s))
Dave Barach68b0fb02017-02-28 15:15:56 -0500324 {
Florin Corasf8f516a2018-02-08 15:10:09 -0800325 tc = session_get_transport (s);
326 if (!tc)
327 {
328 is_fail = 1;
329 goto done;
330 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500331
Florin Corasf8f516a2018-02-08 15:10:09 -0800332 vpp_queue = session_manager_get_vpp_event_queue (s->thread_index);
333 mp->handle = session_handle (s);
334 mp->vpp_event_queue_address = pointer_to_uword (vpp_queue);
335 clib_memcpy (mp->lcl_ip, &tc->lcl_ip, sizeof (tc->lcl_ip));
336 mp->is_ip4 = tc->is_ip4;
337 mp->lcl_port = tc->lcl_port;
338 mp->server_rx_fifo = pointer_to_uword (s->server_rx_fifo);
339 mp->server_tx_fifo = pointer_to_uword (s->server_tx_fifo);
340 }
341 else
342 {
343 local_session_t *ls = (local_session_t *) s;
344 mp->handle = application_local_session_handle (ls);
345 mp->lcl_port = ls->port;
346 mp->vpp_event_queue_address = ls->server_evt_q;
347 mp->client_event_queue_address = ls->client_evt_q;
348 mp->server_rx_fifo = pointer_to_uword (s->server_tx_fifo);
349 mp->server_tx_fifo = pointer_to_uword (s->server_rx_fifo);
350 }
Dave Wallace965fec92017-10-17 16:19:41 -0400351
352done:
353 mp->retval = is_fail ?
354 clib_host_to_net_u32 (VNET_API_ERROR_SESSION_CONNECT) : 0;
Florin Corasb384b542018-01-15 01:08:33 -0800355 vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp);
Dave Barach68b0fb02017-02-28 15:15:56 -0500356 return 0;
357}
358
Florin Corascea194d2017-10-02 00:18:51 -0700359static session_cb_vft_t session_cb_vft = {
Dave Barach68b0fb02017-02-28 15:15:56 -0500360 .session_accept_callback = send_session_accept_callback,
361 .session_disconnect_callback = send_session_disconnect_callback,
362 .session_connected_callback = send_session_connected_callback,
Florin Corasd79b41e2017-03-04 05:37:52 -0800363 .session_reset_callback = send_session_reset_callback,
Dave Barach68b0fb02017-02-28 15:15:56 -0500364 .add_segment_callback = send_add_segment_callback,
Florin Corasf8f516a2018-02-08 15:10:09 -0800365 .del_segment_callback = send_del_segment_callback,
Dave Barach68b0fb02017-02-28 15:15:56 -0500366};
367
Dave Barach68b0fb02017-02-28 15:15:56 -0500368static void
Florin Corase04c2992017-03-01 08:17:34 -0800369vl_api_session_enable_disable_t_handler (vl_api_session_enable_disable_t * mp)
370{
371 vl_api_session_enable_disable_reply_t *rmp;
372 vlib_main_t *vm = vlib_get_main ();
373 int rv = 0;
374
375 vnet_session_enable_disable (vm, mp->is_enable);
376 REPLY_MACRO (VL_API_SESSION_ENABLE_DISABLE_REPLY);
377}
378
379static void
Florin Coras6cf30ad2017-04-04 23:08:23 -0700380vl_api_application_attach_t_handler (vl_api_application_attach_t * mp)
Dave Barach68b0fb02017-02-28 15:15:56 -0500381{
Florin Coras6cf30ad2017-04-04 23:08:23 -0700382 vl_api_application_attach_reply_t *rmp;
Florin Corasb384b542018-01-15 01:08:33 -0800383 ssvm_private_t *segp, *evt_q_segment;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700384 vnet_app_attach_args_t _a, *a = &_a;
Florin Corasb384b542018-01-15 01:08:33 -0800385 vl_api_registration_t *reg;
Florin Corascea194d2017-10-02 00:18:51 -0700386 clib_error_t *error = 0;
387 int rv = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500388
Florin Corasfc804d92018-01-26 01:27:01 -0800389 reg = vl_api_client_index_to_registration (mp->client_index);
390 if (!reg)
391 return;
392
Florin Coras6cf30ad2017-04-04 23:08:23 -0700393 if (session_manager_is_enabled () == 0)
394 {
395 rv = VNET_API_ERROR_FEATURE_DISABLED;
396 goto done;
397 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500398
Florin Corasff6e7692017-12-11 04:59:01 -0800399 STATIC_ASSERT (sizeof (u64) * APP_OPTIONS_N_OPTIONS <=
Florin Coras6cf30ad2017-04-04 23:08:23 -0700400 sizeof (mp->options),
401 "Out of options, fix api message definition");
Dave Barach68b0fb02017-02-28 15:15:56 -0500402
403 memset (a, 0, sizeof (*a));
Dave Barach68b0fb02017-02-28 15:15:56 -0500404 a->api_client_index = mp->client_index;
405 a->options = mp->options;
Florin Corascea194d2017-10-02 00:18:51 -0700406 a->session_cb_vft = &session_cb_vft;
Dave Barach68b0fb02017-02-28 15:15:56 -0500407
Florin Corascea194d2017-10-02 00:18:51 -0700408 if (mp->namespace_id_len > 64)
409 {
410 rv = VNET_API_ERROR_INVALID_VALUE;
411 goto done;
412 }
413
414 if (mp->namespace_id_len)
415 {
Dave Wallace8af20542017-10-26 03:29:30 -0400416 vec_validate (a->namespace_id, mp->namespace_id_len - 1);
Florin Corascea194d2017-10-02 00:18:51 -0700417 clib_memcpy (a->namespace_id, mp->namespace_id, mp->namespace_id_len);
418 }
419
420 if ((error = vnet_application_attach (a)))
421 {
422 rv = clib_error_get_code (error);
423 clib_error_report (error);
424 }
425 vec_free (a->namespace_id);
Dave Barach68b0fb02017-02-28 15:15:56 -0500426
Florin Coras6cf30ad2017-04-04 23:08:23 -0700427done:
Florin Corasa5464812017-04-19 13:00:05 -0700428
Dave Barach68b0fb02017-02-28 15:15:56 -0500429 /* *INDENT-OFF* */
Florin Coras6cf30ad2017-04-04 23:08:23 -0700430 REPLY_MACRO2 (VL_API_APPLICATION_ATTACH_REPLY, ({
Dave Barach68b0fb02017-02-28 15:15:56 -0500431 if (!rv)
432 {
Florin Corasb384b542018-01-15 01:08:33 -0800433 segp = a->segment;
Dave Barach68b0fb02017-02-28 15:15:56 -0500434 rmp->segment_name_length = 0;
Florin Corasb384b542018-01-15 01:08:33 -0800435 rmp->segment_size = segp->ssvm_size;
436 if (vec_len (segp->name))
Dave Barach68b0fb02017-02-28 15:15:56 -0500437 {
Florin Corasb384b542018-01-15 01:08:33 -0800438 memcpy (rmp->segment_name, segp->name, vec_len (segp->name));
439 rmp->segment_name_length = vec_len (segp->name);
Dave Barach68b0fb02017-02-28 15:15:56 -0500440 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700441 rmp->app_event_queue_address = a->app_event_queue_address;
Dave Barach68b0fb02017-02-28 15:15:56 -0500442 }
443 }));
444 /* *INDENT-ON* */
Florin Corasb384b542018-01-15 01:08:33 -0800445
446 if (rv)
447 return;
448
Florin Corasb384b542018-01-15 01:08:33 -0800449 /* Send fifo segment fd if needed */
450 if (ssvm_type (a->segment) == SSVM_SEGMENT_MEMFD)
451 session_send_memfd_fd (reg, a->segment);
452 /* Send event queues segment */
453 if ((evt_q_segment = session_manager_get_evt_q_segment ()))
454 session_send_memfd_fd (reg, evt_q_segment);
Dave Barach68b0fb02017-02-28 15:15:56 -0500455}
456
457static void
Florin Coras6cf30ad2017-04-04 23:08:23 -0700458vl_api_application_detach_t_handler (vl_api_application_detach_t * mp)
459{
460 vl_api_application_detach_reply_t *rmp;
461 int rv = VNET_API_ERROR_INVALID_VALUE_2;
462 vnet_app_detach_args_t _a, *a = &_a;
463 application_t *app;
464
465 if (session_manager_is_enabled () == 0)
466 {
467 rv = VNET_API_ERROR_FEATURE_DISABLED;
468 goto done;
469 }
470
471 app = application_lookup (mp->client_index);
472 if (app)
473 {
474 a->app_index = app->index;
475 rv = vnet_application_detach (a);
476 }
477
478done:
479 REPLY_MACRO (VL_API_APPLICATION_DETACH_REPLY);
480}
481
482static void
483vl_api_bind_uri_t_handler (vl_api_bind_uri_t * mp)
484{
485 vl_api_bind_uri_reply_t *rmp;
486 vnet_bind_args_t _a, *a = &_a;
487 application_t *app;
488 int rv;
489
490 if (session_manager_is_enabled () == 0)
491 {
492 rv = VNET_API_ERROR_FEATURE_DISABLED;
493 goto done;
494 }
495
496 app = application_lookup (mp->client_index);
497 if (app)
498 {
499 memset (a, 0, sizeof (*a));
500 a->uri = (char *) mp->uri;
501 a->app_index = app->index;
502 rv = vnet_bind_uri (a);
503 }
504 else
505 {
506 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
507 }
508
509done:
510 REPLY_MACRO (VL_API_BIND_URI_REPLY);
511}
512
513static void
Dave Barach68b0fb02017-02-28 15:15:56 -0500514vl_api_unbind_uri_t_handler (vl_api_unbind_uri_t * mp)
515{
516 vl_api_unbind_uri_reply_t *rmp;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700517 application_t *app;
518 vnet_unbind_args_t _a, *a = &_a;
Dave Barach68b0fb02017-02-28 15:15:56 -0500519 int rv;
520
Florin Coras6cf30ad2017-04-04 23:08:23 -0700521 if (session_manager_is_enabled () == 0)
522 {
523 rv = VNET_API_ERROR_FEATURE_DISABLED;
524 goto done;
525 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500526
Florin Coras6cf30ad2017-04-04 23:08:23 -0700527 app = application_lookup (mp->client_index);
528 if (app)
529 {
530 a->uri = (char *) mp->uri;
531 a->app_index = app->index;
532 rv = vnet_unbind_uri (a);
533 }
534 else
535 {
536 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
537 }
538
539done:
Dave Barach68b0fb02017-02-28 15:15:56 -0500540 REPLY_MACRO (VL_API_UNBIND_URI_REPLY);
541}
542
Florin Corasf03a59a2017-06-09 21:07:32 -0700543static void
Dave Barach68b0fb02017-02-28 15:15:56 -0500544vl_api_connect_uri_t_handler (vl_api_connect_uri_t * mp)
545{
Dave Wallace33e002b2017-09-06 01:20:02 -0400546 vl_api_connect_session_reply_t *rmp;
Dave Barach68b0fb02017-02-28 15:15:56 -0500547 vnet_connect_args_t _a, *a = &_a;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700548 application_t *app;
Florin Corascea194d2017-10-02 00:18:51 -0700549 clib_error_t *error = 0;
550 int rv = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500551
Florin Coras6cf30ad2017-04-04 23:08:23 -0700552 if (session_manager_is_enabled () == 0)
553 {
554 rv = VNET_API_ERROR_FEATURE_DISABLED;
555 goto done;
556 }
Florin Corase04c2992017-03-01 08:17:34 -0800557
Florin Coras6cf30ad2017-04-04 23:08:23 -0700558 app = application_lookup (mp->client_index);
559 if (app)
560 {
561 a->uri = (char *) mp->uri;
562 a->api_context = mp->context;
563 a->app_index = app->index;
Florin Corascea194d2017-10-02 00:18:51 -0700564 if ((error = vnet_connect_uri (a)))
565 {
566 rv = clib_error_get_code (error);
Florin Coras8f89dd02018-03-05 16:53:07 -0800567 clib_error_report (error);
Florin Corascea194d2017-10-02 00:18:51 -0700568 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700569 }
570 else
571 {
572 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
573 }
Florin Corase04c2992017-03-01 08:17:34 -0800574
Florin Coras3cbc04b2017-10-02 00:18:51 -0700575 /*
576 * Don't reply to stream (tcp) connects. The reply will come once
577 * the connection is established. In case of the redirects, the reply
578 * will come from the server app.
579 */
Florin Coras8f89dd02018-03-05 16:53:07 -0800580 if (rv == 0)
Florin Corase04c2992017-03-01 08:17:34 -0800581 return;
582
Florin Coras6cf30ad2017-04-04 23:08:23 -0700583done:
Florin Corase04c2992017-03-01 08:17:34 -0800584 /* *INDENT-OFF* */
Dave Wallace33e002b2017-09-06 01:20:02 -0400585 REPLY_MACRO (VL_API_CONNECT_SESSION_REPLY);
Florin Corase04c2992017-03-01 08:17:34 -0800586 /* *INDENT-ON* */
Dave Barach68b0fb02017-02-28 15:15:56 -0500587}
588
589static void
590vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
591{
592 vl_api_disconnect_session_reply_t *rmp;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700593 vnet_disconnect_args_t _a, *a = &_a;
594 application_t *app;
595 int rv = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500596
Florin Coras6cf30ad2017-04-04 23:08:23 -0700597 if (session_manager_is_enabled () == 0)
598 {
599 rv = VNET_API_ERROR_FEATURE_DISABLED;
600 goto done;
601 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500602
Florin Coras6cf30ad2017-04-04 23:08:23 -0700603 app = application_lookup (mp->client_index);
604 if (app)
605 {
606 a->handle = mp->handle;
607 a->app_index = app->index;
608 rv = vnet_disconnect_session (a);
609 }
610 else
611 {
612 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
613 }
614
615done:
Dave Wallacede5fec92017-11-11 22:41:34 -0500616 REPLY_MACRO2 (VL_API_DISCONNECT_SESSION_REPLY, rmp->handle = mp->handle);
Dave Barach68b0fb02017-02-28 15:15:56 -0500617}
618
619static void
620vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
621 mp)
622{
Florin Coras6cf30ad2017-04-04 23:08:23 -0700623 vnet_disconnect_args_t _a, *a = &_a;
624 application_t *app;
Dave Barach68b0fb02017-02-28 15:15:56 -0500625
626 /* Client objected to disconnecting the session, log and continue */
627 if (mp->retval)
628 {
629 clib_warning ("client retval %d", mp->retval);
630 return;
631 }
632
633 /* Disconnect has been confirmed. Confirm close to transport */
Florin Corasf8f516a2018-02-08 15:10:09 -0800634 app = application_lookup (mp->context);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700635 if (app)
636 {
637 a->handle = mp->handle;
638 a->app_index = app->index;
639 vnet_disconnect_session (a);
640 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500641}
642
643static void
644vl_api_reset_session_reply_t_handler (vl_api_reset_session_reply_t * mp)
645{
Florin Coras6cf30ad2017-04-04 23:08:23 -0700646 application_t *app;
Dave Barach68b0fb02017-02-28 15:15:56 -0500647 stream_session_t *s;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700648 u32 index, thread_index;
Dave Barach68b0fb02017-02-28 15:15:56 -0500649
Florin Coras6cf30ad2017-04-04 23:08:23 -0700650 app = application_lookup (mp->client_index);
651 if (!app)
652 return;
653
Florin Corascea194d2017-10-02 00:18:51 -0700654 session_parse_handle (mp->handle, &index, &thread_index);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700655 s = session_get_if_valid (index, thread_index);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700656 if (s == 0 || app->index != s->app_index)
Dave Barach68b0fb02017-02-28 15:15:56 -0500657 {
658 clib_warning ("Invalid session!");
659 return;
660 }
661
662 /* Client objected to resetting the session, log and continue */
663 if (mp->retval)
664 {
665 clib_warning ("client retval %d", mp->retval);
666 return;
667 }
668
Dave Barach68b0fb02017-02-28 15:15:56 -0500669 /* This comes as a response to a reset, transport only waiting for
670 * confirmation to remove connection state, no need to disconnect */
671 stream_session_cleanup (s);
672}
673
674static void
675vl_api_accept_session_reply_t_handler (vl_api_accept_session_reply_t * mp)
676{
Florin Corasf8f516a2018-02-08 15:10:09 -0800677 vnet_disconnect_args_t _a = { 0 }, *a = &_a;
678 local_session_t *ls;
Dave Barach68b0fb02017-02-28 15:15:56 -0500679 stream_session_t *s;
Dave Barach68b0fb02017-02-28 15:15:56 -0500680
Florin Corasa5464812017-04-19 13:00:05 -0700681 /* Server isn't interested, kill the session */
682 if (mp->retval)
Dave Barach68b0fb02017-02-28 15:15:56 -0500683 {
Florin Corasa5464812017-04-19 13:00:05 -0700684 a->app_index = mp->context;
685 a->handle = mp->handle;
686 vnet_disconnect_session (a);
Florin Corasf8f516a2018-02-08 15:10:09 -0800687 return;
688 }
689
690 if (session_handle_is_local (mp->handle))
691 {
692 ls = application_get_local_session_from_handle (mp->handle);
693 if (!ls || ls->app_index != mp->context)
694 {
695 clib_warning ("server %u doesn't own local handle %llu",
696 mp->context, mp->handle);
697 return;
698 }
699 if (application_local_session_connect_notify (ls))
700 return;
701 ls->session_state = SESSION_STATE_READY;
Dave Barach68b0fb02017-02-28 15:15:56 -0500702 }
Florin Corasa5464812017-04-19 13:00:05 -0700703 else
704 {
Florin Corasf8f516a2018-02-08 15:10:09 -0800705 s = session_get_from_handle_if_valid (mp->handle);
Florin Corasa5464812017-04-19 13:00:05 -0700706 if (!s)
707 {
708 clib_warning ("session doesn't exist");
709 return;
710 }
711 if (s->app_index != mp->context)
712 {
713 clib_warning ("app doesn't own session");
714 return;
715 }
Florin Corasa5464812017-04-19 13:00:05 -0700716 s->session_state = SESSION_STATE_READY;
717 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500718}
719
720static void
721vl_api_map_another_segment_reply_t_handler (vl_api_map_another_segment_reply_t
722 * mp)
723{
724 clib_warning ("not implemented");
725}
726
727static void
728vl_api_bind_sock_t_handler (vl_api_bind_sock_t * mp)
729{
730 vl_api_bind_sock_reply_t *rmp;
731 vnet_bind_args_t _a, *a = &_a;
Florin Corascea194d2017-10-02 00:18:51 -0700732 int rv = 0;
733 clib_error_t *error;
Florin Coraseb2945c2017-11-22 10:39:09 -0800734 application_t *app = 0;
Florin Corasdcf55ce2017-11-16 15:32:50 -0800735 stream_session_t *s;
736 transport_connection_t *tc = 0;
737 ip46_address_t *ip46;
Dave Barach68b0fb02017-02-28 15:15:56 -0500738
Florin Coras6cf30ad2017-04-04 23:08:23 -0700739 if (session_manager_is_enabled () == 0)
740 {
741 rv = VNET_API_ERROR_FEATURE_DISABLED;
742 goto done;
743 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500744
Florin Coras6cf30ad2017-04-04 23:08:23 -0700745 app = application_lookup (mp->client_index);
Florin Corasdcf55ce2017-11-16 15:32:50 -0800746 if (!app)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700747 {
Florin Corasdcf55ce2017-11-16 15:32:50 -0800748 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
749 goto done;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700750 }
Florin Corasdcf55ce2017-11-16 15:32:50 -0800751
752 ip46 = (ip46_address_t *) mp->ip;
753 memset (a, 0, sizeof (*a));
754 a->sep.is_ip4 = mp->is_ip4;
755 a->sep.ip = *ip46;
756 a->sep.port = mp->port;
757 a->sep.fib_index = mp->vrf;
758 a->sep.sw_if_index = ENDPOINT_INVALID_INDEX;
759 a->sep.transport_proto = mp->proto;
760 a->app_index = app->index;
761
762 if ((error = vnet_bind (a)))
763 {
764 rv = clib_error_get_code (error);
765 clib_error_report (error);
766 }
Florin Corasdcf55ce2017-11-16 15:32:50 -0800767
Florin Coras6cf30ad2017-04-04 23:08:23 -0700768done:
Florin Corascea194d2017-10-02 00:18:51 -0700769 /* *INDENT-OFF* */
770 REPLY_MACRO2 (VL_API_BIND_SOCK_REPLY,({
771 if (!rv)
Florin Corasdcf55ce2017-11-16 15:32:50 -0800772 {
773 rmp->handle = a->handle;
Florin Coraseb2945c2017-11-22 10:39:09 -0800774 rmp->lcl_port = mp->port;
Dave Wallace97b1a272017-12-18 13:51:59 -0500775 if (app && application_has_global_scope (app))
Florin Coraseb2945c2017-11-22 10:39:09 -0800776 {
777 s = listen_session_get_from_handle (a->handle);
778 tc = listen_session_get_transport (s);
779 rmp->lcl_is_ip4 = tc->is_ip4;
780 clib_memcpy (rmp->lcl_ip, &tc->lcl_ip, sizeof (tc->lcl_ip));
781 }
Florin Corasdcf55ce2017-11-16 15:32:50 -0800782 }
Florin Corascea194d2017-10-02 00:18:51 -0700783 }));
784 /* *INDENT-ON* */
Dave Barach68b0fb02017-02-28 15:15:56 -0500785}
786
787static void
788vl_api_unbind_sock_t_handler (vl_api_unbind_sock_t * mp)
789{
790 vl_api_unbind_sock_reply_t *rmp;
791 vnet_unbind_args_t _a, *a = &_a;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700792 application_t *app;
Florin Corascea194d2017-10-02 00:18:51 -0700793 clib_error_t *error;
794 int rv = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500795
Florin Coras6cf30ad2017-04-04 23:08:23 -0700796 if (session_manager_is_enabled () == 0)
797 {
798 rv = VNET_API_ERROR_FEATURE_DISABLED;
799 goto done;
800 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500801
Florin Coras6cf30ad2017-04-04 23:08:23 -0700802 app = application_lookup (mp->client_index);
803 if (app)
804 {
Florin Corasef24d422017-11-09 10:05:42 -0800805 a->app_index = app->index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700806 a->handle = mp->handle;
Florin Corascea194d2017-10-02 00:18:51 -0700807 if ((error = vnet_unbind (a)))
808 {
809 rv = clib_error_get_code (error);
810 clib_error_report (error);
811 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700812 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500813
Florin Coras6cf30ad2017-04-04 23:08:23 -0700814done:
Dave Barach68b0fb02017-02-28 15:15:56 -0500815 REPLY_MACRO (VL_API_UNBIND_SOCK_REPLY);
816}
817
818static void
819vl_api_connect_sock_t_handler (vl_api_connect_sock_t * mp)
820{
Dave Wallace33e002b2017-09-06 01:20:02 -0400821 vl_api_connect_session_reply_t *rmp;
Dave Barach68b0fb02017-02-28 15:15:56 -0500822 vnet_connect_args_t _a, *a = &_a;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700823 application_t *app;
Florin Corascea194d2017-10-02 00:18:51 -0700824 clib_error_t *error = 0;
825 int rv = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500826
Florin Coras6cf30ad2017-04-04 23:08:23 -0700827 if (session_manager_is_enabled () == 0)
828 {
829 rv = VNET_API_ERROR_FEATURE_DISABLED;
830 goto done;
831 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500832
Florin Coras6cf30ad2017-04-04 23:08:23 -0700833 app = application_lookup (mp->client_index);
834 if (app)
835 {
Florin Corase86a8ed2018-01-05 03:20:25 -0800836 svm_queue_t *client_q;
Dave Wallace33e002b2017-09-06 01:20:02 -0400837 ip46_address_t *ip46 = (ip46_address_t *) mp->ip;
Dave Wallaceb2d5ff32017-06-14 12:38:28 -0400838
Florin Coras8f89dd02018-03-05 16:53:07 -0800839 memset (a, 0, sizeof (*a));
Dave Wallaceb2d5ff32017-06-14 12:38:28 -0400840 client_q = vl_api_client_index_to_input_queue (mp->client_index);
841 mp->client_queue_address = pointer_to_uword (client_q);
Florin Corascea194d2017-10-02 00:18:51 -0700842 a->sep.is_ip4 = mp->is_ip4;
843 a->sep.ip = *ip46;
844 a->sep.port = mp->port;
845 a->sep.transport_proto = mp->proto;
846 a->sep.fib_index = mp->vrf;
847 a->sep.sw_if_index = ENDPOINT_INVALID_INDEX;
Florin Coras8f89dd02018-03-05 16:53:07 -0800848 if (mp->hostname_len)
849 {
850 vec_validate (a->sep.hostname, mp->hostname_len - 1);
851 clib_memcpy (a->sep.hostname, mp->hostname, mp->hostname_len);
852 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700853 a->api_context = mp->context;
854 a->app_index = app->index;
Florin Corascea194d2017-10-02 00:18:51 -0700855 if ((error = vnet_connect (a)))
856 {
857 rv = clib_error_get_code (error);
Florin Coras8f89dd02018-03-05 16:53:07 -0800858 clib_error_report (error);
Florin Corascea194d2017-10-02 00:18:51 -0700859 }
Florin Coras8f89dd02018-03-05 16:53:07 -0800860 vec_free (a->sep.hostname);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700861 }
862 else
863 {
864 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
865 }
Florin Corase04c2992017-03-01 08:17:34 -0800866
Florin Coras8f89dd02018-03-05 16:53:07 -0800867 if (rv == 0)
Florin Corase04c2992017-03-01 08:17:34 -0800868 return;
869
870 /* Got some error, relay it */
871
Florin Coras6cf30ad2017-04-04 23:08:23 -0700872done:
Dave Wallace33e002b2017-09-06 01:20:02 -0400873 REPLY_MACRO (VL_API_CONNECT_SESSION_REPLY);
Dave Barach68b0fb02017-02-28 15:15:56 -0500874}
875
Florin Corascea194d2017-10-02 00:18:51 -0700876static void
877vl_api_app_namespace_add_del_t_handler (vl_api_app_namespace_add_del_t * mp)
878{
879 vl_api_app_namespace_add_del_reply_t *rmp;
Florin Corascea194d2017-10-02 00:18:51 -0700880 clib_error_t *error = 0;
Florin Coras6e8c6672017-11-10 09:03:54 -0800881 u32 appns_index = 0;
882 u8 *ns_id = 0;
Florin Corascea194d2017-10-02 00:18:51 -0700883 int rv = 0;
884 if (!session_manager_is_enabled ())
885 {
886 rv = VNET_API_ERROR_FEATURE_DISABLED;
887 goto done;
888 }
889
890 if (mp->namespace_id_len > ARRAY_LEN (mp->namespace_id))
891 {
892 rv = VNET_API_ERROR_INVALID_VALUE;
893 goto done;
894 }
895
896 vec_validate (ns_id, mp->namespace_id_len - 1);
897 clib_memcpy (ns_id, mp->namespace_id, mp->namespace_id_len);
898 vnet_app_namespace_add_del_args_t args = {
899 .ns_id = ns_id,
Florin Coras9a9adb22017-10-26 08:16:59 -0700900 .secret = clib_net_to_host_u64 (mp->secret),
Florin Corascea194d2017-10-02 00:18:51 -0700901 .sw_if_index = clib_net_to_host_u32 (mp->sw_if_index),
902 .ip4_fib_id = clib_net_to_host_u32 (mp->ip4_fib_id),
903 .ip6_fib_id = clib_net_to_host_u32 (mp->ip6_fib_id),
904 .is_add = 1
905 };
906 error = vnet_app_namespace_add_del (&args);
907 if (error)
908 {
909 rv = clib_error_get_code (error);
910 clib_error_report (error);
911 }
Florin Coras6e8c6672017-11-10 09:03:54 -0800912 else
913 {
914 appns_index = app_namespace_index_from_id (ns_id);
915 if (appns_index == APP_NAMESPACE_INVALID_INDEX)
916 {
917 clib_warning ("app ns lookup failed");
918 rv = VNET_API_ERROR_UNSPECIFIED;
919 }
920 }
Florin Corascea194d2017-10-02 00:18:51 -0700921 vec_free (ns_id);
Florin Coras6e8c6672017-11-10 09:03:54 -0800922
923 /* *INDENT-OFF* */
Florin Corascea194d2017-10-02 00:18:51 -0700924done:
Florin Coras6e8c6672017-11-10 09:03:54 -0800925 REPLY_MACRO2 (VL_API_APP_NAMESPACE_ADD_DEL_REPLY, ({
926 if (!rv)
927 rmp->appns_index = clib_host_to_net_u32 (appns_index);
928 }));
929 /* *INDENT-ON* */
Florin Corascea194d2017-10-02 00:18:51 -0700930}
931
Florin Coras1c710452017-10-17 00:03:13 -0700932static void
933vl_api_session_rule_add_del_t_handler (vl_api_session_rule_add_del_t * mp)
934{
935 vl_api_session_rule_add_del_reply_t *rmp;
936 session_rule_add_del_args_t args;
937 session_rule_table_add_del_args_t *table_args = &args.table_args;
938 clib_error_t *error;
939 u8 fib_proto;
940 int rv = 0;
941
Florin Corasc97a7392017-11-05 23:07:07 -0800942 memset (&args, 0, sizeof (args));
Florin Coras1c710452017-10-17 00:03:13 -0700943 fib_proto = mp->is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
944
945 table_args->lcl.fp_len = mp->lcl_plen;
946 table_args->lcl.fp_proto = fib_proto;
947 table_args->rmt.fp_len = mp->rmt_plen;
948 table_args->rmt.fp_proto = fib_proto;
Milan Lenco8b9a5d12017-11-24 17:12:33 +0100949 table_args->lcl_port = mp->lcl_port;
950 table_args->rmt_port = mp->rmt_port;
Florin Coras1c710452017-10-17 00:03:13 -0700951 table_args->action_index = clib_net_to_host_u32 (mp->action_index);
952 table_args->is_add = mp->is_add;
Florin Corasc97a7392017-11-05 23:07:07 -0800953 mp->tag[sizeof (mp->tag) - 1] = 0;
954 table_args->tag = format (0, "%s", mp->tag);
Florin Coras1c710452017-10-17 00:03:13 -0700955 args.appns_index = clib_net_to_host_u32 (mp->appns_index);
956 args.scope = mp->scope;
Florin Coras42324ad2017-11-18 18:45:20 -0800957 args.transport_proto = mp->transport_proto;
Florin Coras1c710452017-10-17 00:03:13 -0700958
959 memset (&table_args->lcl.fp_addr, 0, sizeof (table_args->lcl.fp_addr));
960 memset (&table_args->rmt.fp_addr, 0, sizeof (table_args->rmt.fp_addr));
961 ip_set (&table_args->lcl.fp_addr, mp->lcl_ip, mp->is_ip4);
962 ip_set (&table_args->rmt.fp_addr, mp->rmt_ip, mp->is_ip4);
963 error = vnet_session_rule_add_del (&args);
964 if (error)
965 {
966 rv = clib_error_get_code (error);
967 clib_error_report (error);
968 }
Florin Corasc97a7392017-11-05 23:07:07 -0800969 vec_free (table_args->tag);
Florin Coras1c710452017-10-17 00:03:13 -0700970 REPLY_MACRO (VL_API_SESSION_RULE_ADD_DEL_REPLY);
971}
972
Florin Coras6c36f532017-11-03 18:32:34 -0700973static void
974send_session_rule_details4 (mma_rule_16_t * rule, u8 is_local,
Florin Corasc97a7392017-11-05 23:07:07 -0800975 u8 transport_proto, u32 appns_index, u8 * tag,
Florin Coras6c4dae22018-01-09 06:39:23 -0800976 vl_api_registration_t * reg, u32 context)
Florin Coras6c36f532017-11-03 18:32:34 -0700977{
978 vl_api_session_rules_details_t *rmp = 0;
979 session_mask_or_match_4_t *match =
980 (session_mask_or_match_4_t *) & rule->match;
981 session_mask_or_match_4_t *mask =
982 (session_mask_or_match_4_t *) & rule->mask;
983
984 rmp = vl_msg_api_alloc (sizeof (*rmp));
985 memset (rmp, 0, sizeof (*rmp));
986 rmp->_vl_msg_id = ntohs (VL_API_SESSION_RULES_DETAILS);
987 rmp->context = context;
988
989 rmp->is_ip4 = 1;
990 clib_memcpy (rmp->lcl_ip, &match->lcl_ip, sizeof (match->lcl_ip));
991 clib_memcpy (rmp->rmt_ip, &match->rmt_ip, sizeof (match->rmt_ip));
992 rmp->lcl_plen = ip4_mask_to_preflen (&mask->lcl_ip);
993 rmp->rmt_plen = ip4_mask_to_preflen (&mask->rmt_ip);
Milan Lenco8b9a5d12017-11-24 17:12:33 +0100994 rmp->lcl_port = match->lcl_port;
995 rmp->rmt_port = match->rmt_port;
Florin Coras6c36f532017-11-03 18:32:34 -0700996 rmp->action_index = clib_host_to_net_u32 (rule->action_index);
997 rmp->scope =
998 is_local ? SESSION_RULE_SCOPE_LOCAL : SESSION_RULE_SCOPE_GLOBAL;
999 rmp->transport_proto = transport_proto;
1000 rmp->appns_index = clib_host_to_net_u32 (appns_index);
Florin Corasc97a7392017-11-05 23:07:07 -08001001 if (tag)
1002 {
1003 clib_memcpy (rmp->tag, tag, vec_len (tag));
1004 rmp->tag[vec_len (tag)] = 0;
1005 }
Florin Coras6c36f532017-11-03 18:32:34 -07001006
Florin Coras6c4dae22018-01-09 06:39:23 -08001007 vl_api_send_msg (reg, (u8 *) rmp);
Florin Coras6c36f532017-11-03 18:32:34 -07001008}
1009
1010static void
Florin Corasc97a7392017-11-05 23:07:07 -08001011send_session_rule_details6 (mma_rule_40_t * rule, u8 is_local,
1012 u8 transport_proto, u32 appns_index, u8 * tag,
Florin Coras6c4dae22018-01-09 06:39:23 -08001013 vl_api_registration_t * reg, u32 context)
Florin Coras6c36f532017-11-03 18:32:34 -07001014{
1015 vl_api_session_rules_details_t *rmp = 0;
1016 session_mask_or_match_6_t *match =
1017 (session_mask_or_match_6_t *) & rule->match;
1018 session_mask_or_match_6_t *mask =
1019 (session_mask_or_match_6_t *) & rule->mask;
1020
1021 rmp = vl_msg_api_alloc (sizeof (*rmp));
1022 memset (rmp, 0, sizeof (*rmp));
1023 rmp->_vl_msg_id = ntohs (VL_API_SESSION_RULES_DETAILS);
1024 rmp->context = context;
1025
1026 rmp->is_ip4 = 0;
1027 clib_memcpy (rmp->lcl_ip, &match->lcl_ip, sizeof (match->lcl_ip));
1028 clib_memcpy (rmp->rmt_ip, &match->rmt_ip, sizeof (match->rmt_ip));
1029 rmp->lcl_plen = ip6_mask_to_preflen (&mask->lcl_ip);
1030 rmp->rmt_plen = ip6_mask_to_preflen (&mask->rmt_ip);
Milan Lenco8b9a5d12017-11-24 17:12:33 +01001031 rmp->lcl_port = match->lcl_port;
1032 rmp->rmt_port = match->rmt_port;
Florin Coras6c36f532017-11-03 18:32:34 -07001033 rmp->action_index = clib_host_to_net_u32 (rule->action_index);
Florin Corasc97a7392017-11-05 23:07:07 -08001034 rmp->scope =
1035 is_local ? SESSION_RULE_SCOPE_LOCAL : SESSION_RULE_SCOPE_GLOBAL;
Florin Coras6c36f532017-11-03 18:32:34 -07001036 rmp->transport_proto = transport_proto;
1037 rmp->appns_index = clib_host_to_net_u32 (appns_index);
Florin Corasc97a7392017-11-05 23:07:07 -08001038 if (tag)
1039 {
1040 clib_memcpy (rmp->tag, tag, vec_len (tag));
1041 rmp->tag[vec_len (tag)] = 0;
1042 }
Florin Coras6c36f532017-11-03 18:32:34 -07001043
Florin Coras6c4dae22018-01-09 06:39:23 -08001044 vl_api_send_msg (reg, (u8 *) rmp);
Florin Coras6c36f532017-11-03 18:32:34 -07001045}
1046
1047static void
1048send_session_rules_table_details (session_rules_table_t * srt, u8 fib_proto,
Florin Corasc97a7392017-11-05 23:07:07 -08001049 u8 tp, u8 is_local, u32 appns_index,
Florin Coras6c4dae22018-01-09 06:39:23 -08001050 vl_api_registration_t * reg, u32 context)
Florin Coras6c36f532017-11-03 18:32:34 -07001051{
1052 mma_rule_16_t *rule16;
1053 mma_rule_40_t *rule40;
1054 mma_rules_table_16_t *srt16;
1055 mma_rules_table_40_t *srt40;
Florin Corasc97a7392017-11-05 23:07:07 -08001056 u32 ri;
Florin Coras6c36f532017-11-03 18:32:34 -07001057
Florin Corasc97a7392017-11-05 23:07:07 -08001058 if (is_local || fib_proto == FIB_PROTOCOL_IP4)
Florin Coras6c36f532017-11-03 18:32:34 -07001059 {
Florin Corasc97a7392017-11-05 23:07:07 -08001060 u8 *tag = 0;
1061 /* *INDENT-OFF* */
1062 srt16 = &srt->session_rules_tables_16;
1063 pool_foreach (rule16, srt16->rules, ({
1064 ri = mma_rules_table_rule_index_16 (srt16, rule16);
1065 tag = session_rules_table_rule_tag (srt, ri, 1);
1066 send_session_rule_details4 (rule16, is_local, tp, appns_index, tag,
Florin Coras6c4dae22018-01-09 06:39:23 -08001067 reg, context);
Florin Corasc97a7392017-11-05 23:07:07 -08001068 }));
1069 /* *INDENT-ON* */
1070 }
1071 if (is_local || fib_proto == FIB_PROTOCOL_IP6)
1072 {
1073 u8 *tag = 0;
1074 /* *INDENT-OFF* */
1075 srt40 = &srt->session_rules_tables_40;
1076 pool_foreach (rule40, srt40->rules, ({
1077 ri = mma_rules_table_rule_index_40 (srt40, rule40);
1078 tag = session_rules_table_rule_tag (srt, ri, 1);
1079 send_session_rule_details6 (rule40, is_local, tp, appns_index, tag,
Florin Coras6c4dae22018-01-09 06:39:23 -08001080 reg, context);
Florin Corasc97a7392017-11-05 23:07:07 -08001081 }));
1082 /* *INDENT-ON* */
Florin Coras6c36f532017-11-03 18:32:34 -07001083 }
1084}
1085
1086static void
1087vl_api_session_rules_dump_t_handler (vl_api_one_map_server_dump_t * mp)
1088{
Florin Coras6c4dae22018-01-09 06:39:23 -08001089 vl_api_registration_t *reg;
Florin Coras6c36f532017-11-03 18:32:34 -07001090 session_table_t *st;
Florin Corasc97a7392017-11-05 23:07:07 -08001091 u8 tp;
Florin Coras6c36f532017-11-03 18:32:34 -07001092
Florin Coras6c4dae22018-01-09 06:39:23 -08001093 reg = vl_api_client_index_to_registration (mp->client_index);
1094 if (!reg)
Florin Coras6c36f532017-11-03 18:32:34 -07001095 return;
1096
1097 /* *INDENT-OFF* */
1098 session_table_foreach (st, ({
Florin Corasc97a7392017-11-05 23:07:07 -08001099 for (tp = 0; tp < TRANSPORT_N_PROTO; tp++)
1100 {
1101 send_session_rules_table_details (&st->session_rules[tp],
1102 st->active_fib_proto, tp,
Florin Coras6c4dae22018-01-09 06:39:23 -08001103 st->is_local, st->appns_index, reg,
Florin Corasc97a7392017-11-05 23:07:07 -08001104 mp->context);
1105 }
Florin Coras6c36f532017-11-03 18:32:34 -07001106 }));
1107 /* *INDENT-ON* */
1108}
1109
Florin Coras371ca502018-02-21 12:07:41 -08001110static void
1111vl_api_application_tls_cert_add_t_handler (vl_api_application_tls_cert_add_t *
1112 mp)
1113{
1114 vl_api_app_namespace_add_del_reply_t *rmp;
1115 vnet_app_add_tls_cert_args_t _a, *a = &_a;
1116 clib_error_t *error;
Florin Corase3e2f072018-03-04 07:24:30 -08001117 application_t *app;
Florin Coras371ca502018-02-21 12:07:41 -08001118 u32 cert_len;
1119 int rv = 0;
1120 if (!session_manager_is_enabled ())
1121 {
1122 rv = VNET_API_ERROR_FEATURE_DISABLED;
1123 goto done;
1124 }
Florin Corase3e2f072018-03-04 07:24:30 -08001125 if (!(app = application_lookup (mp->client_index)))
1126 {
1127 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
1128 goto done;
1129 }
Florin Coras371ca502018-02-21 12:07:41 -08001130 memset (a, 0, sizeof (*a));
Florin Corase3e2f072018-03-04 07:24:30 -08001131 a->app_index = app->index;
Florin Coras371ca502018-02-21 12:07:41 -08001132 cert_len = clib_net_to_host_u16 (mp->cert_len);
1133 vec_validate (a->cert, cert_len);
1134 clib_memcpy (a->cert, mp->cert, cert_len);
1135 if ((error = vnet_app_add_tls_cert (a)))
1136 {
1137 rv = clib_error_get_code (error);
1138 clib_error_report (error);
1139 }
1140 vec_free (a->cert);
1141done:
1142 REPLY_MACRO (VL_API_APPLICATION_TLS_CERT_ADD_REPLY);
1143}
1144
1145static void
1146vl_api_application_tls_key_add_t_handler (vl_api_application_tls_key_add_t *
1147 mp)
1148{
1149 vl_api_app_namespace_add_del_reply_t *rmp;
1150 vnet_app_add_tls_key_args_t _a, *a = &_a;
1151 clib_error_t *error;
Florin Corase3e2f072018-03-04 07:24:30 -08001152 application_t *app;
Florin Coras371ca502018-02-21 12:07:41 -08001153 u32 key_len;
1154 int rv = 0;
1155 if (!session_manager_is_enabled ())
1156 {
1157 rv = VNET_API_ERROR_FEATURE_DISABLED;
1158 goto done;
1159 }
Florin Corase3e2f072018-03-04 07:24:30 -08001160 if (!(app = application_lookup (mp->client_index)))
1161 {
1162 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
1163 goto done;
1164 }
Florin Coras371ca502018-02-21 12:07:41 -08001165 memset (a, 0, sizeof (*a));
Florin Corase3e2f072018-03-04 07:24:30 -08001166 a->app_index = app->index;
Florin Coras371ca502018-02-21 12:07:41 -08001167 key_len = clib_net_to_host_u16 (mp->key_len);
1168 vec_validate (a->key, key_len);
1169 clib_memcpy (a->key, mp->key, key_len);
1170 if ((error = vnet_app_add_tls_key (a)))
1171 {
1172 rv = clib_error_get_code (error);
1173 clib_error_report (error);
1174 }
1175 vec_free (a->key);
1176done:
1177 REPLY_MACRO (VL_API_APPLICATION_TLS_KEY_ADD_REPLY);
1178}
1179
Florin Coras6cf30ad2017-04-04 23:08:23 -07001180static clib_error_t *
1181application_reaper_cb (u32 client_index)
Dave Barach68b0fb02017-02-28 15:15:56 -05001182{
Florin Coras6cf30ad2017-04-04 23:08:23 -07001183 application_t *app = application_lookup (client_index);
1184 vnet_app_detach_args_t _a, *a = &_a;
1185 if (app)
1186 {
1187 a->app_index = app->index;
1188 vnet_application_detach (a);
1189 }
1190 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001191}
1192
Florin Coras6cf30ad2017-04-04 23:08:23 -07001193VL_MSG_API_REAPER_FUNCTION (application_reaper_cb);
Dave Barach68b0fb02017-02-28 15:15:56 -05001194
1195#define vl_msg_name_crc_list
1196#include <vnet/vnet_all_api_h.h>
1197#undef vl_msg_name_crc_list
1198
1199static void
1200setup_message_id_table (api_main_t * am)
1201{
1202#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
1203 foreach_vl_msg_name_crc_session;
1204#undef _
1205}
1206
1207/*
1208 * session_api_hookup
1209 * Add uri's API message handlers to the table.
1210 * vlib has alread mapped shared memory and
1211 * added the client registration handlers.
1212 * See .../open-repo/vlib/memclnt_vlib.c:memclnt_process()
1213 */
1214static clib_error_t *
1215session_api_hookup (vlib_main_t * vm)
1216{
1217 api_main_t *am = &api_main;
1218
1219#define _(N,n) \
1220 vl_msg_api_set_handlers(VL_API_##N, #n, \
1221 vl_api_##n##_t_handler, \
1222 vl_noop_handler, \
1223 vl_api_##n##_t_endian, \
1224 vl_api_##n##_t_print, \
1225 sizeof(vl_api_##n##_t), 1);
1226 foreach_session_api_msg;
1227#undef _
1228
1229 /*
1230 * Messages which bounce off the data-plane to
1231 * an API client. Simply tells the message handling infra not
1232 * to free the message.
1233 *
1234 * Bounced message handlers MUST NOT block the data plane
1235 */
1236 am->message_bounce[VL_API_CONNECT_URI] = 1;
1237 am->message_bounce[VL_API_CONNECT_SOCK] = 1;
1238
1239 /*
1240 * Set up the (msg_name, crc, message-id) table
1241 */
1242 setup_message_id_table (am);
1243
1244 return 0;
1245}
1246
1247VLIB_API_INIT_FUNCTION (session_api_hookup);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001248
Dave Barach68b0fb02017-02-28 15:15:56 -05001249/*
1250 * fd.io coding-style-patch-verification: ON
1251 *
1252 * Local Variables:
1253 * eval: (c-set-style "gnu")
1254 * End:
1255 */