blob: e9ddbce7f94b713a0eb52d43d6c809fb345cef5a [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>
19
20#include <vnet/vnet_msg_enum.h>
21#include "application_interface.h"
22
23#define vl_typedefs /* define message structures */
24#include <vnet/vnet_all_api_h.h>
25#undef vl_typedefs
26
27#define vl_endianfun /* define message structures */
28#include <vnet/vnet_all_api_h.h>
29#undef vl_endianfun
30
31/* instantiate all the print functions we know about */
32#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
33#define vl_printfun
34#include <vnet/vnet_all_api_h.h>
35#undef vl_printfun
36
37#include <vlibapi/api_helper_macros.h>
38
39#define foreach_session_api_msg \
40_(MAP_ANOTHER_SEGMENT_REPLY, map_another_segment_reply) \
Florin Coras6cf30ad2017-04-04 23:08:23 -070041_(APPLICATION_ATTACH, application_attach) \
42_(APPLICATION_DETACH, application_detach) \
Dave Barach68b0fb02017-02-28 15:15:56 -050043_(BIND_URI, bind_uri) \
44_(UNBIND_URI, unbind_uri) \
45_(CONNECT_URI, connect_uri) \
46_(DISCONNECT_SESSION, disconnect_session) \
47_(DISCONNECT_SESSION_REPLY, disconnect_session_reply) \
48_(ACCEPT_SESSION_REPLY, accept_session_reply) \
49_(RESET_SESSION_REPLY, reset_session_reply) \
Florin Corascea194d2017-10-02 00:18:51 -070050_(BIND_SOCK, bind_sock) \
Dave Barach68b0fb02017-02-28 15:15:56 -050051_(UNBIND_SOCK, unbind_sock) \
52_(CONNECT_SOCK, connect_sock) \
Florin Corase04c2992017-03-01 08:17:34 -080053_(SESSION_ENABLE_DISABLE, session_enable_disable) \
Florin Corascea194d2017-10-02 00:18:51 -070054_(APP_NAMESPACE_ADD_DEL, app_namespace_add_del) \
Florin Corase04c2992017-03-01 08:17:34 -080055
Dave Barach68b0fb02017-02-28 15:15:56 -050056static int
57send_add_segment_callback (u32 api_client_index, const u8 * segment_name,
58 u32 segment_size)
59{
60 vl_api_map_another_segment_t *mp;
61 unix_shared_memory_queue_t *q;
62
63 q = vl_api_client_index_to_input_queue (api_client_index);
64
65 if (!q)
66 return -1;
67
68 mp = vl_msg_api_alloc (sizeof (*mp));
69 memset (mp, 0, sizeof (*mp));
70 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_MAP_ANOTHER_SEGMENT);
71 mp->segment_size = segment_size;
72 strncpy ((char *) mp->segment_name, (char *) segment_name,
73 sizeof (mp->segment_name) - 1);
74
75 vl_msg_api_send_shmem (q, (u8 *) & mp);
76
77 return 0;
78}
79
80static int
Florin Coras6cf30ad2017-04-04 23:08:23 -070081send_session_accept_callback (stream_session_t * s)
Dave Barach68b0fb02017-02-28 15:15:56 -050082{
83 vl_api_accept_session_t *mp;
84 unix_shared_memory_queue_t *q, *vpp_queue;
85 application_t *server = application_get (s->app_index);
Florin Coras6cf30ad2017-04-04 23:08:23 -070086 transport_connection_t *tc;
87 transport_proto_vft_t *tp_vft;
88 stream_session_t *listener;
Dave Barach68b0fb02017-02-28 15:15:56 -050089
90 q = vl_api_client_index_to_input_queue (server->api_client_index);
91 vpp_queue = session_manager_get_vpp_event_queue (s->thread_index);
92
93 if (!q)
94 return -1;
95
96 mp = vl_msg_api_alloc (sizeof (*mp));
Florin Coras6cf30ad2017-04-04 23:08:23 -070097 memset (mp, 0, sizeof (*mp));
98
Dave Barach68b0fb02017-02-28 15:15:56 -050099 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_ACCEPT_SESSION);
Florin Corasa5464812017-04-19 13:00:05 -0700100 mp->context = server->index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700101 listener = listen_session_get (s->session_type, s->listener_index);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700102 tp_vft = transport_protocol_get_vft (s->session_type);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700103 tc = tp_vft->get_connection (s->connection_index, s->thread_index);
104 mp->listener_handle = listen_session_get_handle (listener);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700105 mp->handle = session_handle (s);
Damjan Marion7bee80c2017-04-26 15:32:12 +0200106 mp->server_rx_fifo = pointer_to_uword (s->server_rx_fifo);
107 mp->server_tx_fifo = pointer_to_uword (s->server_tx_fifo);
108 mp->vpp_event_queue_address = pointer_to_uword (vpp_queue);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700109 mp->port = tc->rmt_port;
110 mp->is_ip4 = tc->is_ip4;
111 clib_memcpy (&mp->ip, &tc->rmt_ip, sizeof (tc->rmt_ip));
Dave Barach68b0fb02017-02-28 15:15:56 -0500112 vl_msg_api_send_shmem (q, (u8 *) & mp);
113
114 return 0;
115}
116
117static void
Florin Coras6cf30ad2017-04-04 23:08:23 -0700118send_session_disconnect_callback (stream_session_t * s)
Dave Barach68b0fb02017-02-28 15:15:56 -0500119{
120 vl_api_disconnect_session_t *mp;
121 unix_shared_memory_queue_t *q;
122 application_t *app = application_get (s->app_index);
123
124 q = vl_api_client_index_to_input_queue (app->api_client_index);
125
126 if (!q)
127 return;
128
129 mp = vl_msg_api_alloc (sizeof (*mp));
130 memset (mp, 0, sizeof (*mp));
131 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_DISCONNECT_SESSION);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700132 mp->handle = session_handle (s);
Dave Barach68b0fb02017-02-28 15:15:56 -0500133 vl_msg_api_send_shmem (q, (u8 *) & mp);
134}
135
Florin Corasd79b41e2017-03-04 05:37:52 -0800136static void
Florin Coras6cf30ad2017-04-04 23:08:23 -0700137send_session_reset_callback (stream_session_t * s)
Florin Corasd79b41e2017-03-04 05:37:52 -0800138{
139 vl_api_reset_session_t *mp;
140 unix_shared_memory_queue_t *q;
141 application_t *app = application_get (s->app_index);
142
143 q = vl_api_client_index_to_input_queue (app->api_client_index);
144
145 if (!q)
146 return;
147
148 mp = vl_msg_api_alloc (sizeof (*mp));
149 memset (mp, 0, sizeof (*mp));
150 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_RESET_SESSION);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700151 mp->handle = session_handle (s);
Florin Corasd79b41e2017-03-04 05:37:52 -0800152 vl_msg_api_send_shmem (q, (u8 *) & mp);
153}
154
Dave Barach0194f1a2017-05-15 10:11:39 -0400155int
Florin Coras6cf30ad2017-04-04 23:08:23 -0700156send_session_connected_callback (u32 app_index, u32 api_context,
157 stream_session_t * s, u8 is_fail)
Dave Barach68b0fb02017-02-28 15:15:56 -0500158{
Dave Wallace33e002b2017-09-06 01:20:02 -0400159 vl_api_connect_session_reply_t *mp;
Dave Barach68b0fb02017-02-28 15:15:56 -0500160 unix_shared_memory_queue_t *q;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700161 application_t *app;
Dave Barach68b0fb02017-02-28 15:15:56 -0500162 unix_shared_memory_queue_t *vpp_queue;
Florin Corasade70e42017-10-14 18:56:41 -0700163 transport_connection_t *tc;
Dave Barach68b0fb02017-02-28 15:15:56 -0500164
Florin Coras6cf30ad2017-04-04 23:08:23 -0700165 app = application_get (app_index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500166 q = vl_api_client_index_to_input_queue (app->api_client_index);
167
168 if (!q)
169 return -1;
170
Florin Corasade70e42017-10-14 18:56:41 -0700171 tc = session_get_transport (s);
172 if (!tc)
173 is_fail = 1;
Dave Barach68b0fb02017-02-28 15:15:56 -0500174 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Wallace33e002b2017-09-06 01:20:02 -0400175 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_CONNECT_SESSION_REPLY);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700176 mp->context = api_context;
Dave Barach68b0fb02017-02-28 15:15:56 -0500177 if (!is_fail)
178 {
179 vpp_queue = session_manager_get_vpp_event_queue (s->thread_index);
Damjan Marion7bee80c2017-04-26 15:32:12 +0200180 mp->server_rx_fifo = pointer_to_uword (s->server_rx_fifo);
181 mp->server_tx_fifo = pointer_to_uword (s->server_tx_fifo);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700182 mp->handle = session_handle (s);
Damjan Marion7bee80c2017-04-26 15:32:12 +0200183 mp->vpp_event_queue_address = pointer_to_uword (vpp_queue);
Florin Corasade70e42017-10-14 18:56:41 -0700184 clib_memcpy (mp->lcl_ip, &tc->lcl_ip, sizeof (tc->lcl_ip));
185 mp->is_ip4 = tc->is_ip4;
186 mp->lcl_port = tc->lcl_port;
Florin Corase04c2992017-03-01 08:17:34 -0800187 mp->retval = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500188 }
Florin Corase04c2992017-03-01 08:17:34 -0800189 else
190 {
Florin Corascea194d2017-10-02 00:18:51 -0700191 mp->retval = clib_host_to_net_u32 (VNET_API_ERROR_SESSION_CONNECT);
Florin Corase04c2992017-03-01 08:17:34 -0800192 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500193
194 vl_msg_api_send_shmem (q, (u8 *) & mp);
Dave Barach68b0fb02017-02-28 15:15:56 -0500195 return 0;
196}
197
198/**
199 * Redirect a connect_uri message to the indicated server.
200 * Only sent if the server has bound the related port with
201 * URI_OPTIONS_FLAGS_USE_FIFO
202 */
203static int
Florin Coras6cf30ad2017-04-04 23:08:23 -0700204redirect_connect_callback (u32 server_api_client_index, void *mp_arg)
Dave Barach68b0fb02017-02-28 15:15:56 -0500205{
Florin Corascea194d2017-10-02 00:18:51 -0700206 vl_api_connect_sock_t *mp = mp_arg;
Dave Barach68b0fb02017-02-28 15:15:56 -0500207 unix_shared_memory_queue_t *server_q, *client_q;
208 vlib_main_t *vm = vlib_get_main ();
209 f64 timeout = vlib_time_now (vm) + 0.5;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700210 application_t *app;
Dave Barach68b0fb02017-02-28 15:15:56 -0500211 int rv = 0;
212
213 server_q = vl_api_client_index_to_input_queue (server_api_client_index);
214
215 if (!server_q)
216 {
217 rv = VNET_API_ERROR_INVALID_VALUE;
218 goto out;
219 }
220
221 client_q = vl_api_client_index_to_input_queue (mp->client_index);
222 if (!client_q)
223 {
224 rv = VNET_API_ERROR_INVALID_VALUE_2;
225 goto out;
226 }
227
228 /* Tell the server the client's API queue address, so it can reply */
Damjan Marion7bee80c2017-04-26 15:32:12 +0200229 mp->client_queue_address = pointer_to_uword (client_q);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700230 app = application_lookup (mp->client_index);
Florin Coras82b13a82017-04-25 11:58:06 -0700231 if (!app)
232 {
233 clib_warning ("no client application");
234 return -1;
235 }
236
Florin Coras6cf30ad2017-04-04 23:08:23 -0700237 mp->options[SESSION_OPTIONS_RX_FIFO_SIZE] = app->sm_properties.rx_fifo_size;
238 mp->options[SESSION_OPTIONS_TX_FIFO_SIZE] = app->sm_properties.tx_fifo_size;
Dave Barach68b0fb02017-02-28 15:15:56 -0500239
240 /*
241 * Bounce message handlers MUST NOT block the data-plane.
242 * Spin waiting for the queue lock, but
243 */
244
245 while (vlib_time_now (vm) < timeout)
246 {
247 rv =
248 unix_shared_memory_queue_add (server_q, (u8 *) & mp, 1 /*nowait */ );
249 switch (rv)
250 {
251 /* correctly enqueued */
252 case 0:
Florin Corascea194d2017-10-02 00:18:51 -0700253 return VNET_API_ERROR_SESSION_REDIRECT;
Dave Barach68b0fb02017-02-28 15:15:56 -0500254
255 /* continue spinning, wait for pthread_mutex_trylock to work */
256 case -1:
257 continue;
258
259 /* queue stuffed, drop the msg */
260 case -2:
261 rv = VNET_API_ERROR_QUEUE_FULL;
262 goto out;
263 }
264 }
265out:
266 /* Dispose of the message */
267 vl_msg_api_free (mp);
268 return rv;
269}
270
Florin Corascea194d2017-10-02 00:18:51 -0700271static session_cb_vft_t session_cb_vft = {
Dave Barach68b0fb02017-02-28 15:15:56 -0500272 .session_accept_callback = send_session_accept_callback,
273 .session_disconnect_callback = send_session_disconnect_callback,
274 .session_connected_callback = send_session_connected_callback,
Florin Corasd79b41e2017-03-04 05:37:52 -0800275 .session_reset_callback = send_session_reset_callback,
Dave Barach68b0fb02017-02-28 15:15:56 -0500276 .add_segment_callback = send_add_segment_callback,
277 .redirect_connect_callback = redirect_connect_callback
278};
279
Dave Barach68b0fb02017-02-28 15:15:56 -0500280static void
Florin Corase04c2992017-03-01 08:17:34 -0800281vl_api_session_enable_disable_t_handler (vl_api_session_enable_disable_t * mp)
282{
283 vl_api_session_enable_disable_reply_t *rmp;
284 vlib_main_t *vm = vlib_get_main ();
285 int rv = 0;
286
287 vnet_session_enable_disable (vm, mp->is_enable);
288 REPLY_MACRO (VL_API_SESSION_ENABLE_DISABLE_REPLY);
289}
290
291static void
Florin Coras6cf30ad2017-04-04 23:08:23 -0700292vl_api_application_attach_t_handler (vl_api_application_attach_t * mp)
Dave Barach68b0fb02017-02-28 15:15:56 -0500293{
Florin Coras6cf30ad2017-04-04 23:08:23 -0700294 vl_api_application_attach_reply_t *rmp;
295 vnet_app_attach_args_t _a, *a = &_a;
Florin Corascea194d2017-10-02 00:18:51 -0700296 clib_error_t *error = 0;
297 int rv = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500298
Florin Coras6cf30ad2017-04-04 23:08:23 -0700299 if (session_manager_is_enabled () == 0)
300 {
301 rv = VNET_API_ERROR_FEATURE_DISABLED;
302 goto done;
303 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500304
Florin Coras6cf30ad2017-04-04 23:08:23 -0700305 STATIC_ASSERT (sizeof (u64) * SESSION_OPTIONS_N_OPTIONS <=
306 sizeof (mp->options),
307 "Out of options, fix api message definition");
Dave Barach68b0fb02017-02-28 15:15:56 -0500308
309 memset (a, 0, sizeof (*a));
Dave Barach68b0fb02017-02-28 15:15:56 -0500310 a->api_client_index = mp->client_index;
311 a->options = mp->options;
Florin Corascea194d2017-10-02 00:18:51 -0700312 a->session_cb_vft = &session_cb_vft;
Dave Barach68b0fb02017-02-28 15:15:56 -0500313
Florin Corascea194d2017-10-02 00:18:51 -0700314 if (mp->namespace_id_len > 64)
315 {
316 rv = VNET_API_ERROR_INVALID_VALUE;
317 goto done;
318 }
319
320 if (mp->namespace_id_len)
321 {
322 vec_validate (a->namespace_id, mp->namespace_id_len);
323 clib_memcpy (a->namespace_id, mp->namespace_id, mp->namespace_id_len);
324 }
325
326 if ((error = vnet_application_attach (a)))
327 {
328 rv = clib_error_get_code (error);
329 clib_error_report (error);
330 }
331 vec_free (a->namespace_id);
Dave Barach68b0fb02017-02-28 15:15:56 -0500332
Florin Coras6cf30ad2017-04-04 23:08:23 -0700333done:
Florin Corasa5464812017-04-19 13:00:05 -0700334
Dave Barach68b0fb02017-02-28 15:15:56 -0500335 /* *INDENT-OFF* */
Florin Coras6cf30ad2017-04-04 23:08:23 -0700336 REPLY_MACRO2 (VL_API_APPLICATION_ATTACH_REPLY, ({
Dave Barach68b0fb02017-02-28 15:15:56 -0500337 if (!rv)
338 {
339 rmp->segment_name_length = 0;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700340 rmp->segment_size = a->segment_size;
341 if (a->segment_name_length)
Dave Barach68b0fb02017-02-28 15:15:56 -0500342 {
Florin Coras6cf30ad2017-04-04 23:08:23 -0700343 memcpy (rmp->segment_name, a->segment_name,
344 a->segment_name_length);
345 rmp->segment_name_length = a->segment_name_length;
Dave Barach68b0fb02017-02-28 15:15:56 -0500346 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700347 rmp->app_event_queue_address = a->app_event_queue_address;
Dave Barach68b0fb02017-02-28 15:15:56 -0500348 }
349 }));
350 /* *INDENT-ON* */
Dave Barach68b0fb02017-02-28 15:15:56 -0500351}
352
353static void
Florin Coras6cf30ad2017-04-04 23:08:23 -0700354vl_api_application_detach_t_handler (vl_api_application_detach_t * mp)
355{
356 vl_api_application_detach_reply_t *rmp;
357 int rv = VNET_API_ERROR_INVALID_VALUE_2;
358 vnet_app_detach_args_t _a, *a = &_a;
359 application_t *app;
360
361 if (session_manager_is_enabled () == 0)
362 {
363 rv = VNET_API_ERROR_FEATURE_DISABLED;
364 goto done;
365 }
366
367 app = application_lookup (mp->client_index);
368 if (app)
369 {
370 a->app_index = app->index;
371 rv = vnet_application_detach (a);
372 }
373
374done:
375 REPLY_MACRO (VL_API_APPLICATION_DETACH_REPLY);
376}
377
378static void
379vl_api_bind_uri_t_handler (vl_api_bind_uri_t * mp)
380{
381 vl_api_bind_uri_reply_t *rmp;
382 vnet_bind_args_t _a, *a = &_a;
383 application_t *app;
384 int rv;
385
386 if (session_manager_is_enabled () == 0)
387 {
388 rv = VNET_API_ERROR_FEATURE_DISABLED;
389 goto done;
390 }
391
392 app = application_lookup (mp->client_index);
393 if (app)
394 {
395 memset (a, 0, sizeof (*a));
396 a->uri = (char *) mp->uri;
397 a->app_index = app->index;
398 rv = vnet_bind_uri (a);
399 }
400 else
401 {
402 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
403 }
404
405done:
406 REPLY_MACRO (VL_API_BIND_URI_REPLY);
407}
408
409static void
Dave Barach68b0fb02017-02-28 15:15:56 -0500410vl_api_unbind_uri_t_handler (vl_api_unbind_uri_t * mp)
411{
412 vl_api_unbind_uri_reply_t *rmp;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700413 application_t *app;
414 vnet_unbind_args_t _a, *a = &_a;
Dave Barach68b0fb02017-02-28 15:15:56 -0500415 int rv;
416
Florin Coras6cf30ad2017-04-04 23:08:23 -0700417 if (session_manager_is_enabled () == 0)
418 {
419 rv = VNET_API_ERROR_FEATURE_DISABLED;
420 goto done;
421 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500422
Florin Coras6cf30ad2017-04-04 23:08:23 -0700423 app = application_lookup (mp->client_index);
424 if (app)
425 {
426 a->uri = (char *) mp->uri;
427 a->app_index = app->index;
428 rv = vnet_unbind_uri (a);
429 }
430 else
431 {
432 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
433 }
434
435done:
Dave Barach68b0fb02017-02-28 15:15:56 -0500436 REPLY_MACRO (VL_API_UNBIND_URI_REPLY);
437}
438
Florin Corasf03a59a2017-06-09 21:07:32 -0700439static void
Dave Barach68b0fb02017-02-28 15:15:56 -0500440vl_api_connect_uri_t_handler (vl_api_connect_uri_t * mp)
441{
Dave Wallace33e002b2017-09-06 01:20:02 -0400442 vl_api_connect_session_reply_t *rmp;
Dave Barach68b0fb02017-02-28 15:15:56 -0500443 vnet_connect_args_t _a, *a = &_a;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700444 application_t *app;
Florin Corascea194d2017-10-02 00:18:51 -0700445 clib_error_t *error = 0;
446 int rv = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500447
Florin Coras6cf30ad2017-04-04 23:08:23 -0700448 if (session_manager_is_enabled () == 0)
449 {
450 rv = VNET_API_ERROR_FEATURE_DISABLED;
451 goto done;
452 }
Florin Corase04c2992017-03-01 08:17:34 -0800453
Florin Coras6cf30ad2017-04-04 23:08:23 -0700454 app = application_lookup (mp->client_index);
455 if (app)
456 {
457 a->uri = (char *) mp->uri;
458 a->api_context = mp->context;
459 a->app_index = app->index;
460 a->mp = mp;
Florin Corascea194d2017-10-02 00:18:51 -0700461 if ((error = vnet_connect_uri (a)))
462 {
463 rv = clib_error_get_code (error);
464 if (rv != VNET_API_ERROR_SESSION_REDIRECT)
465 clib_error_report (error);
466 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700467 }
468 else
469 {
470 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
471 }
Florin Corase04c2992017-03-01 08:17:34 -0800472
Florin Coras3cbc04b2017-10-02 00:18:51 -0700473 /*
474 * Don't reply to stream (tcp) connects. The reply will come once
475 * the connection is established. In case of the redirects, the reply
476 * will come from the server app.
477 */
Florin Corascea194d2017-10-02 00:18:51 -0700478 if (rv == 0 || rv == VNET_API_ERROR_SESSION_REDIRECT)
Florin Corase04c2992017-03-01 08:17:34 -0800479 return;
480
Florin Coras6cf30ad2017-04-04 23:08:23 -0700481done:
Florin Corase04c2992017-03-01 08:17:34 -0800482 /* *INDENT-OFF* */
Dave Wallace33e002b2017-09-06 01:20:02 -0400483 REPLY_MACRO (VL_API_CONNECT_SESSION_REPLY);
Florin Corase04c2992017-03-01 08:17:34 -0800484 /* *INDENT-ON* */
Dave Barach68b0fb02017-02-28 15:15:56 -0500485}
486
487static void
488vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
489{
490 vl_api_disconnect_session_reply_t *rmp;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700491 vnet_disconnect_args_t _a, *a = &_a;
492 application_t *app;
493 int rv = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500494
Florin Coras6cf30ad2017-04-04 23:08:23 -0700495 if (session_manager_is_enabled () == 0)
496 {
497 rv = VNET_API_ERROR_FEATURE_DISABLED;
498 goto done;
499 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500500
Florin Coras6cf30ad2017-04-04 23:08:23 -0700501 app = application_lookup (mp->client_index);
502 if (app)
503 {
504 a->handle = mp->handle;
505 a->app_index = app->index;
506 rv = vnet_disconnect_session (a);
507 }
508 else
509 {
510 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
511 }
512
513done:
Dave Barach68b0fb02017-02-28 15:15:56 -0500514 REPLY_MACRO (VL_API_DISCONNECT_SESSION_REPLY);
515}
516
517static void
518vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
519 mp)
520{
Florin Coras6cf30ad2017-04-04 23:08:23 -0700521 vnet_disconnect_args_t _a, *a = &_a;
522 application_t *app;
Dave Barach68b0fb02017-02-28 15:15:56 -0500523
524 /* Client objected to disconnecting the session, log and continue */
525 if (mp->retval)
526 {
527 clib_warning ("client retval %d", mp->retval);
528 return;
529 }
530
531 /* Disconnect has been confirmed. Confirm close to transport */
Florin Coras6cf30ad2017-04-04 23:08:23 -0700532 app = application_lookup (mp->client_index);
533 if (app)
534 {
535 a->handle = mp->handle;
536 a->app_index = app->index;
537 vnet_disconnect_session (a);
538 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500539}
540
541static void
542vl_api_reset_session_reply_t_handler (vl_api_reset_session_reply_t * mp)
543{
Florin Coras6cf30ad2017-04-04 23:08:23 -0700544 application_t *app;
Dave Barach68b0fb02017-02-28 15:15:56 -0500545 stream_session_t *s;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700546 u32 index, thread_index;
Dave Barach68b0fb02017-02-28 15:15:56 -0500547
Florin Coras6cf30ad2017-04-04 23:08:23 -0700548 app = application_lookup (mp->client_index);
549 if (!app)
550 return;
551
Florin Corascea194d2017-10-02 00:18:51 -0700552 session_parse_handle (mp->handle, &index, &thread_index);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700553 s = session_get_if_valid (index, thread_index);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700554 if (s == 0 || app->index != s->app_index)
Dave Barach68b0fb02017-02-28 15:15:56 -0500555 {
556 clib_warning ("Invalid session!");
557 return;
558 }
559
560 /* Client objected to resetting the session, log and continue */
561 if (mp->retval)
562 {
563 clib_warning ("client retval %d", mp->retval);
564 return;
565 }
566
Dave Barach68b0fb02017-02-28 15:15:56 -0500567 /* This comes as a response to a reset, transport only waiting for
568 * confirmation to remove connection state, no need to disconnect */
569 stream_session_cleanup (s);
570}
571
572static void
573vl_api_accept_session_reply_t_handler (vl_api_accept_session_reply_t * mp)
574{
575 stream_session_t *s;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700576 u32 session_index, thread_index;
Florin Corasa5464812017-04-19 13:00:05 -0700577 vnet_disconnect_args_t _a, *a = &_a;
Dave Barach68b0fb02017-02-28 15:15:56 -0500578
Florin Corasa5464812017-04-19 13:00:05 -0700579 /* Server isn't interested, kill the session */
580 if (mp->retval)
Dave Barach68b0fb02017-02-28 15:15:56 -0500581 {
Florin Corasa5464812017-04-19 13:00:05 -0700582 a->app_index = mp->context;
583 a->handle = mp->handle;
584 vnet_disconnect_session (a);
Dave Barach68b0fb02017-02-28 15:15:56 -0500585 }
Florin Corasa5464812017-04-19 13:00:05 -0700586 else
587 {
Florin Corascea194d2017-10-02 00:18:51 -0700588 session_parse_handle (mp->handle, &session_index, &thread_index);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700589 s = session_get_if_valid (session_index, thread_index);
Florin Corasa5464812017-04-19 13:00:05 -0700590 if (!s)
591 {
592 clib_warning ("session doesn't exist");
593 return;
594 }
595 if (s->app_index != mp->context)
596 {
597 clib_warning ("app doesn't own session");
598 return;
599 }
Florin Corasa5464812017-04-19 13:00:05 -0700600 s->session_state = SESSION_STATE_READY;
601 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500602}
603
604static void
605vl_api_map_another_segment_reply_t_handler (vl_api_map_another_segment_reply_t
606 * mp)
607{
608 clib_warning ("not implemented");
609}
610
611static void
612vl_api_bind_sock_t_handler (vl_api_bind_sock_t * mp)
613{
614 vl_api_bind_sock_reply_t *rmp;
615 vnet_bind_args_t _a, *a = &_a;
Florin Corascea194d2017-10-02 00:18:51 -0700616 int rv = 0;
617 clib_error_t *error;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700618 application_t *app;
Dave Barach68b0fb02017-02-28 15:15:56 -0500619
Florin Coras6cf30ad2017-04-04 23:08:23 -0700620 if (session_manager_is_enabled () == 0)
621 {
622 rv = VNET_API_ERROR_FEATURE_DISABLED;
623 goto done;
624 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500625
Florin Coras6cf30ad2017-04-04 23:08:23 -0700626 app = application_lookup (mp->client_index);
627 if (app)
628 {
Dave Wallace33e002b2017-09-06 01:20:02 -0400629 ip46_address_t *ip46 = (ip46_address_t *) mp->ip;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700630 memset (a, 0, sizeof (*a));
Florin Corascea194d2017-10-02 00:18:51 -0700631 a->sep.is_ip4 = mp->is_ip4;
632 a->sep.ip = *ip46;
633 a->sep.port = mp->port;
634 a->sep.fib_index = mp->vrf;
635 a->sep.sw_if_index = ENDPOINT_INVALID_INDEX;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700636 a->sep.transport_proto = mp->proto;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700637 a->app_index = app->index;
Dave Barach68b0fb02017-02-28 15:15:56 -0500638
Florin Corascea194d2017-10-02 00:18:51 -0700639 if ((error = vnet_bind (a)))
640 {
641 rv = clib_error_get_code (error);
642 clib_error_report (error);
643 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700644 }
645done:
Florin Corascea194d2017-10-02 00:18:51 -0700646 /* *INDENT-OFF* */
647 REPLY_MACRO2 (VL_API_BIND_SOCK_REPLY,({
648 if (!rv)
649 rmp->handle = a->handle;
650 }));
651 /* *INDENT-ON* */
Dave Barach68b0fb02017-02-28 15:15:56 -0500652}
653
654static void
655vl_api_unbind_sock_t_handler (vl_api_unbind_sock_t * mp)
656{
657 vl_api_unbind_sock_reply_t *rmp;
658 vnet_unbind_args_t _a, *a = &_a;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700659 application_t *app;
Florin Corascea194d2017-10-02 00:18:51 -0700660 clib_error_t *error;
661 int rv = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500662
Florin Coras6cf30ad2017-04-04 23:08:23 -0700663 if (session_manager_is_enabled () == 0)
664 {
665 rv = VNET_API_ERROR_FEATURE_DISABLED;
666 goto done;
667 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500668
Florin Coras6cf30ad2017-04-04 23:08:23 -0700669 app = application_lookup (mp->client_index);
670 if (app)
671 {
672 a->app_index = mp->client_index;
673 a->handle = mp->handle;
Florin Corascea194d2017-10-02 00:18:51 -0700674 if ((error = vnet_unbind (a)))
675 {
676 rv = clib_error_get_code (error);
677 clib_error_report (error);
678 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700679 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500680
Florin Coras6cf30ad2017-04-04 23:08:23 -0700681done:
Dave Barach68b0fb02017-02-28 15:15:56 -0500682 REPLY_MACRO (VL_API_UNBIND_SOCK_REPLY);
683}
684
685static void
686vl_api_connect_sock_t_handler (vl_api_connect_sock_t * mp)
687{
Dave Wallace33e002b2017-09-06 01:20:02 -0400688 vl_api_connect_session_reply_t *rmp;
Dave Barach68b0fb02017-02-28 15:15:56 -0500689 vnet_connect_args_t _a, *a = &_a;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700690 application_t *app;
Florin Corascea194d2017-10-02 00:18:51 -0700691 clib_error_t *error = 0;
692 int rv = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500693
Florin Coras6cf30ad2017-04-04 23:08:23 -0700694 if (session_manager_is_enabled () == 0)
695 {
696 rv = VNET_API_ERROR_FEATURE_DISABLED;
697 goto done;
698 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500699
Florin Coras6cf30ad2017-04-04 23:08:23 -0700700 app = application_lookup (mp->client_index);
701 if (app)
702 {
Dave Wallaceb2d5ff32017-06-14 12:38:28 -0400703 unix_shared_memory_queue_t *client_q;
Dave Wallace33e002b2017-09-06 01:20:02 -0400704 ip46_address_t *ip46 = (ip46_address_t *) mp->ip;
Dave Wallaceb2d5ff32017-06-14 12:38:28 -0400705
706 client_q = vl_api_client_index_to_input_queue (mp->client_index);
707 mp->client_queue_address = pointer_to_uword (client_q);
Florin Corascea194d2017-10-02 00:18:51 -0700708 a->sep.is_ip4 = mp->is_ip4;
709 a->sep.ip = *ip46;
710 a->sep.port = mp->port;
711 a->sep.transport_proto = mp->proto;
712 a->sep.fib_index = mp->vrf;
713 a->sep.sw_if_index = ENDPOINT_INVALID_INDEX;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700714 a->api_context = mp->context;
715 a->app_index = app->index;
716 a->mp = mp;
Florin Corascea194d2017-10-02 00:18:51 -0700717 if ((error = vnet_connect (a)))
718 {
719 rv = clib_error_get_code (error);
720 if (rv != VNET_API_ERROR_SESSION_REDIRECT)
721 clib_error_report (error);
722 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700723 }
724 else
725 {
726 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
727 }
Florin Corase04c2992017-03-01 08:17:34 -0800728
Florin Corascea194d2017-10-02 00:18:51 -0700729 if (rv == 0 || rv == VNET_API_ERROR_SESSION_REDIRECT)
Florin Corase04c2992017-03-01 08:17:34 -0800730 return;
731
732 /* Got some error, relay it */
733
Florin Coras6cf30ad2017-04-04 23:08:23 -0700734done:
Dave Wallace33e002b2017-09-06 01:20:02 -0400735 REPLY_MACRO (VL_API_CONNECT_SESSION_REPLY);
Dave Barach68b0fb02017-02-28 15:15:56 -0500736}
737
Florin Corascea194d2017-10-02 00:18:51 -0700738static void
739vl_api_app_namespace_add_del_t_handler (vl_api_app_namespace_add_del_t * mp)
740{
741 vl_api_app_namespace_add_del_reply_t *rmp;
742 u8 *ns_id = 0;
743 clib_error_t *error = 0;
744 int rv = 0;
745 if (!session_manager_is_enabled ())
746 {
747 rv = VNET_API_ERROR_FEATURE_DISABLED;
748 goto done;
749 }
750
751 if (mp->namespace_id_len > ARRAY_LEN (mp->namespace_id))
752 {
753 rv = VNET_API_ERROR_INVALID_VALUE;
754 goto done;
755 }
756
757 vec_validate (ns_id, mp->namespace_id_len - 1);
758 clib_memcpy (ns_id, mp->namespace_id, mp->namespace_id_len);
759 vnet_app_namespace_add_del_args_t args = {
760 .ns_id = ns_id,
761 .secret = mp->secret,
762 .sw_if_index = clib_net_to_host_u32 (mp->sw_if_index),
763 .ip4_fib_id = clib_net_to_host_u32 (mp->ip4_fib_id),
764 .ip6_fib_id = clib_net_to_host_u32 (mp->ip6_fib_id),
765 .is_add = 1
766 };
767 error = vnet_app_namespace_add_del (&args);
768 if (error)
769 {
770 rv = clib_error_get_code (error);
771 clib_error_report (error);
772 }
773 vec_free (ns_id);
774done:
775 REPLY_MACRO (VL_API_APP_NAMESPACE_ADD_DEL_REPLY);
776}
777
Florin Coras6cf30ad2017-04-04 23:08:23 -0700778static clib_error_t *
779application_reaper_cb (u32 client_index)
Dave Barach68b0fb02017-02-28 15:15:56 -0500780{
Florin Coras6cf30ad2017-04-04 23:08:23 -0700781 application_t *app = application_lookup (client_index);
782 vnet_app_detach_args_t _a, *a = &_a;
783 if (app)
784 {
785 a->app_index = app->index;
786 vnet_application_detach (a);
787 }
788 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500789}
790
Florin Coras6cf30ad2017-04-04 23:08:23 -0700791VL_MSG_API_REAPER_FUNCTION (application_reaper_cb);
Dave Barach68b0fb02017-02-28 15:15:56 -0500792
793#define vl_msg_name_crc_list
794#include <vnet/vnet_all_api_h.h>
795#undef vl_msg_name_crc_list
796
797static void
798setup_message_id_table (api_main_t * am)
799{
800#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
801 foreach_vl_msg_name_crc_session;
802#undef _
803}
804
805/*
806 * session_api_hookup
807 * Add uri's API message handlers to the table.
808 * vlib has alread mapped shared memory and
809 * added the client registration handlers.
810 * See .../open-repo/vlib/memclnt_vlib.c:memclnt_process()
811 */
812static clib_error_t *
813session_api_hookup (vlib_main_t * vm)
814{
815 api_main_t *am = &api_main;
816
817#define _(N,n) \
818 vl_msg_api_set_handlers(VL_API_##N, #n, \
819 vl_api_##n##_t_handler, \
820 vl_noop_handler, \
821 vl_api_##n##_t_endian, \
822 vl_api_##n##_t_print, \
823 sizeof(vl_api_##n##_t), 1);
824 foreach_session_api_msg;
825#undef _
826
827 /*
828 * Messages which bounce off the data-plane to
829 * an API client. Simply tells the message handling infra not
830 * to free the message.
831 *
832 * Bounced message handlers MUST NOT block the data plane
833 */
834 am->message_bounce[VL_API_CONNECT_URI] = 1;
835 am->message_bounce[VL_API_CONNECT_SOCK] = 1;
836
837 /*
838 * Set up the (msg_name, crc, message-id) table
839 */
840 setup_message_id_table (am);
841
842 return 0;
843}
844
845VLIB_API_INIT_FUNCTION (session_api_hookup);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700846
Dave Barach68b0fb02017-02-28 15:15:56 -0500847/*
848 * fd.io coding-style-patch-verification: ON
849 *
850 * Local Variables:
851 * eval: (c-set-style "gnu")
852 * End:
853 */