blob: 79d67a2fece7c743c8dd88129dae8911df7e7831 [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) \
50_(BIND_SOCK, bind_sock) \
51_(UNBIND_SOCK, unbind_sock) \
52_(CONNECT_SOCK, connect_sock) \
Florin Corase04c2992017-03-01 08:17:34 -080053_(SESSION_ENABLE_DISABLE, session_enable_disable) \
54
Dave Barach68b0fb02017-02-28 15:15:56 -050055static int
56send_add_segment_callback (u32 api_client_index, const u8 * segment_name,
57 u32 segment_size)
58{
59 vl_api_map_another_segment_t *mp;
60 unix_shared_memory_queue_t *q;
61
62 q = vl_api_client_index_to_input_queue (api_client_index);
63
64 if (!q)
65 return -1;
66
67 mp = vl_msg_api_alloc (sizeof (*mp));
68 memset (mp, 0, sizeof (*mp));
69 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_MAP_ANOTHER_SEGMENT);
70 mp->segment_size = segment_size;
71 strncpy ((char *) mp->segment_name, (char *) segment_name,
72 sizeof (mp->segment_name) - 1);
73
74 vl_msg_api_send_shmem (q, (u8 *) & mp);
75
76 return 0;
77}
78
79static int
Florin Coras6cf30ad2017-04-04 23:08:23 -070080send_session_accept_callback (stream_session_t * s)
Dave Barach68b0fb02017-02-28 15:15:56 -050081{
82 vl_api_accept_session_t *mp;
83 unix_shared_memory_queue_t *q, *vpp_queue;
84 application_t *server = application_get (s->app_index);
Florin Coras6cf30ad2017-04-04 23:08:23 -070085 transport_connection_t *tc;
86 transport_proto_vft_t *tp_vft;
87 stream_session_t *listener;
Dave Barach68b0fb02017-02-28 15:15:56 -050088
89 q = vl_api_client_index_to_input_queue (server->api_client_index);
90 vpp_queue = session_manager_get_vpp_event_queue (s->thread_index);
91
92 if (!q)
93 return -1;
94
95 mp = vl_msg_api_alloc (sizeof (*mp));
Florin Coras6cf30ad2017-04-04 23:08:23 -070096 memset (mp, 0, sizeof (*mp));
97
Dave Barach68b0fb02017-02-28 15:15:56 -050098 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_ACCEPT_SESSION);
Florin Corasa5464812017-04-19 13:00:05 -070099 mp->context = server->index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700100 listener = listen_session_get (s->session_type, s->listener_index);
101 tp_vft = session_get_transport_vft (s->session_type);
102 tc = tp_vft->get_connection (s->connection_index, s->thread_index);
103 mp->listener_handle = listen_session_get_handle (listener);
104 mp->handle = stream_session_handle (s);
Dave Barach68b0fb02017-02-28 15:15:56 -0500105 mp->server_rx_fifo = (u64) s->server_rx_fifo;
106 mp->server_tx_fifo = (u64) s->server_tx_fifo;
Dave Barach68b0fb02017-02-28 15:15:56 -0500107 mp->vpp_event_queue_address = (u64) vpp_queue;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700108 mp->port = tc->rmt_port;
109 mp->is_ip4 = tc->is_ip4;
110 clib_memcpy (&mp->ip, &tc->rmt_ip, sizeof (tc->rmt_ip));
Dave Barach68b0fb02017-02-28 15:15:56 -0500111 vl_msg_api_send_shmem (q, (u8 *) & mp);
112
113 return 0;
114}
115
116static void
Florin Coras6cf30ad2017-04-04 23:08:23 -0700117send_session_disconnect_callback (stream_session_t * s)
Dave Barach68b0fb02017-02-28 15:15:56 -0500118{
119 vl_api_disconnect_session_t *mp;
120 unix_shared_memory_queue_t *q;
121 application_t *app = application_get (s->app_index);
122
123 q = vl_api_client_index_to_input_queue (app->api_client_index);
124
125 if (!q)
126 return;
127
128 mp = vl_msg_api_alloc (sizeof (*mp));
129 memset (mp, 0, sizeof (*mp));
130 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_DISCONNECT_SESSION);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700131 mp->handle = stream_session_handle (s);
Dave Barach68b0fb02017-02-28 15:15:56 -0500132 vl_msg_api_send_shmem (q, (u8 *) & mp);
133}
134
Florin Corasd79b41e2017-03-04 05:37:52 -0800135static void
Florin Coras6cf30ad2017-04-04 23:08:23 -0700136send_session_reset_callback (stream_session_t * s)
Florin Corasd79b41e2017-03-04 05:37:52 -0800137{
138 vl_api_reset_session_t *mp;
139 unix_shared_memory_queue_t *q;
140 application_t *app = application_get (s->app_index);
141
142 q = vl_api_client_index_to_input_queue (app->api_client_index);
143
144 if (!q)
145 return;
146
147 mp = vl_msg_api_alloc (sizeof (*mp));
148 memset (mp, 0, sizeof (*mp));
149 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_RESET_SESSION);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700150 mp->handle = stream_session_handle (s);
Florin Corasd79b41e2017-03-04 05:37:52 -0800151 vl_msg_api_send_shmem (q, (u8 *) & mp);
152}
153
Dave Barach68b0fb02017-02-28 15:15:56 -0500154static int
Florin Coras6cf30ad2017-04-04 23:08:23 -0700155send_session_connected_callback (u32 app_index, u32 api_context,
156 stream_session_t * s, u8 is_fail)
Dave Barach68b0fb02017-02-28 15:15:56 -0500157{
158 vl_api_connect_uri_reply_t *mp;
159 unix_shared_memory_queue_t *q;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700160 application_t *app;
Dave Barach68b0fb02017-02-28 15:15:56 -0500161 unix_shared_memory_queue_t *vpp_queue;
162
Florin Coras6cf30ad2017-04-04 23:08:23 -0700163 app = application_get (app_index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500164 q = vl_api_client_index_to_input_queue (app->api_client_index);
165
166 if (!q)
167 return -1;
168
169 mp = vl_msg_api_alloc (sizeof (*mp));
170 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_CONNECT_URI_REPLY);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700171 mp->context = api_context;
Dave Barach68b0fb02017-02-28 15:15:56 -0500172 if (!is_fail)
173 {
174 vpp_queue = session_manager_get_vpp_event_queue (s->thread_index);
175 mp->server_rx_fifo = (u64) s->server_rx_fifo;
176 mp->server_tx_fifo = (u64) s->server_tx_fifo;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700177 mp->handle = stream_session_handle (s);
Dave Barach68b0fb02017-02-28 15:15:56 -0500178 mp->vpp_event_queue_address = (u64) vpp_queue;
Florin Corase04c2992017-03-01 08:17:34 -0800179 mp->retval = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500180 }
Florin Corase04c2992017-03-01 08:17:34 -0800181 else
182 {
flyingeagle23db42b7b2017-04-20 18:38:48 +0800183 mp->retval = clib_host_to_net_u32 (VNET_API_ERROR_SESSION_CONNECT_FAIL);
Florin Corase04c2992017-03-01 08:17:34 -0800184 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500185
186 vl_msg_api_send_shmem (q, (u8 *) & mp);
187
188 /* Remove client if connect failed */
Florin Coras6cf30ad2017-04-04 23:08:23 -0700189 if (!is_fail)
Florin Corase04c2992017-03-01 08:17:34 -0800190 {
191 s->session_state = SESSION_STATE_READY;
192 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500193
194 return 0;
195}
196
197/**
198 * Redirect a connect_uri message to the indicated server.
199 * Only sent if the server has bound the related port with
200 * URI_OPTIONS_FLAGS_USE_FIFO
201 */
202static int
Florin Coras6cf30ad2017-04-04 23:08:23 -0700203redirect_connect_callback (u32 server_api_client_index, void *mp_arg)
Dave Barach68b0fb02017-02-28 15:15:56 -0500204{
205 vl_api_connect_uri_t *mp = mp_arg;
206 unix_shared_memory_queue_t *server_q, *client_q;
207 vlib_main_t *vm = vlib_get_main ();
208 f64 timeout = vlib_time_now (vm) + 0.5;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700209 application_t *app;
Dave Barach68b0fb02017-02-28 15:15:56 -0500210 int rv = 0;
211
212 server_q = vl_api_client_index_to_input_queue (server_api_client_index);
213
214 if (!server_q)
215 {
216 rv = VNET_API_ERROR_INVALID_VALUE;
217 goto out;
218 }
219
220 client_q = vl_api_client_index_to_input_queue (mp->client_index);
221 if (!client_q)
222 {
223 rv = VNET_API_ERROR_INVALID_VALUE_2;
224 goto out;
225 }
226
227 /* Tell the server the client's API queue address, so it can reply */
228 mp->client_queue_address = (u64) client_q;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700229 app = application_lookup (mp->client_index);
230 mp->options[SESSION_OPTIONS_RX_FIFO_SIZE] = app->sm_properties.rx_fifo_size;
231 mp->options[SESSION_OPTIONS_TX_FIFO_SIZE] = app->sm_properties.tx_fifo_size;
Dave Barach68b0fb02017-02-28 15:15:56 -0500232
233 /*
234 * Bounce message handlers MUST NOT block the data-plane.
235 * Spin waiting for the queue lock, but
236 */
237
238 while (vlib_time_now (vm) < timeout)
239 {
240 rv =
241 unix_shared_memory_queue_add (server_q, (u8 *) & mp, 1 /*nowait */ );
242 switch (rv)
243 {
244 /* correctly enqueued */
245 case 0:
246 return VNET_CONNECT_REDIRECTED;
247
248 /* continue spinning, wait for pthread_mutex_trylock to work */
249 case -1:
250 continue;
251
252 /* queue stuffed, drop the msg */
253 case -2:
254 rv = VNET_API_ERROR_QUEUE_FULL;
255 goto out;
256 }
257 }
258out:
259 /* Dispose of the message */
260 vl_msg_api_free (mp);
261 return rv;
262}
263
264static session_cb_vft_t uri_session_cb_vft = {
Dave Barach68b0fb02017-02-28 15:15:56 -0500265 .session_accept_callback = send_session_accept_callback,
266 .session_disconnect_callback = send_session_disconnect_callback,
267 .session_connected_callback = send_session_connected_callback,
Florin Corasd79b41e2017-03-04 05:37:52 -0800268 .session_reset_callback = send_session_reset_callback,
Dave Barach68b0fb02017-02-28 15:15:56 -0500269 .add_segment_callback = send_add_segment_callback,
270 .redirect_connect_callback = redirect_connect_callback
271};
272
Dave Barach68b0fb02017-02-28 15:15:56 -0500273static void
Florin Corase04c2992017-03-01 08:17:34 -0800274vl_api_session_enable_disable_t_handler (vl_api_session_enable_disable_t * mp)
275{
276 vl_api_session_enable_disable_reply_t *rmp;
277 vlib_main_t *vm = vlib_get_main ();
278 int rv = 0;
279
280 vnet_session_enable_disable (vm, mp->is_enable);
281 REPLY_MACRO (VL_API_SESSION_ENABLE_DISABLE_REPLY);
282}
283
284static void
Florin Coras6cf30ad2017-04-04 23:08:23 -0700285vl_api_application_attach_t_handler (vl_api_application_attach_t * mp)
Dave Barach68b0fb02017-02-28 15:15:56 -0500286{
Florin Coras6cf30ad2017-04-04 23:08:23 -0700287 vl_api_application_attach_reply_t *rmp;
288 vnet_app_attach_args_t _a, *a = &_a;
Dave Barach68b0fb02017-02-28 15:15:56 -0500289 int rv;
290
Florin Coras6cf30ad2017-04-04 23:08:23 -0700291 if (session_manager_is_enabled () == 0)
292 {
293 rv = VNET_API_ERROR_FEATURE_DISABLED;
294 goto done;
295 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500296
Florin Coras6cf30ad2017-04-04 23:08:23 -0700297 STATIC_ASSERT (sizeof (u64) * SESSION_OPTIONS_N_OPTIONS <=
298 sizeof (mp->options),
299 "Out of options, fix api message definition");
Dave Barach68b0fb02017-02-28 15:15:56 -0500300
301 memset (a, 0, sizeof (*a));
302
Dave Barach68b0fb02017-02-28 15:15:56 -0500303 a->api_client_index = mp->client_index;
304 a->options = mp->options;
Dave Barach68b0fb02017-02-28 15:15:56 -0500305 a->session_cb_vft = &uri_session_cb_vft;
306
Florin Coras6cf30ad2017-04-04 23:08:23 -0700307 rv = vnet_application_attach (a);
Dave Barach68b0fb02017-02-28 15:15:56 -0500308
Florin Coras6cf30ad2017-04-04 23:08:23 -0700309done:
Florin Corasa5464812017-04-19 13:00:05 -0700310
Dave Barach68b0fb02017-02-28 15:15:56 -0500311 /* *INDENT-OFF* */
Florin Coras6cf30ad2017-04-04 23:08:23 -0700312 REPLY_MACRO2 (VL_API_APPLICATION_ATTACH_REPLY, ({
Dave Barach68b0fb02017-02-28 15:15:56 -0500313 if (!rv)
314 {
315 rmp->segment_name_length = 0;
316 /* $$$$ policy? */
Florin Coras6cf30ad2017-04-04 23:08:23 -0700317 rmp->segment_size = a->segment_size;
318 if (a->segment_name_length)
Dave Barach68b0fb02017-02-28 15:15:56 -0500319 {
Florin Coras6cf30ad2017-04-04 23:08:23 -0700320 memcpy (rmp->segment_name, a->segment_name,
321 a->segment_name_length);
322 rmp->segment_name_length = a->segment_name_length;
Dave Barach68b0fb02017-02-28 15:15:56 -0500323 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700324 rmp->app_event_queue_address = a->app_event_queue_address;
Dave Barach68b0fb02017-02-28 15:15:56 -0500325 }
326 }));
327 /* *INDENT-ON* */
Dave Barach68b0fb02017-02-28 15:15:56 -0500328}
329
330static void
Florin Coras6cf30ad2017-04-04 23:08:23 -0700331vl_api_application_detach_t_handler (vl_api_application_detach_t * mp)
332{
333 vl_api_application_detach_reply_t *rmp;
334 int rv = VNET_API_ERROR_INVALID_VALUE_2;
335 vnet_app_detach_args_t _a, *a = &_a;
336 application_t *app;
337
338 if (session_manager_is_enabled () == 0)
339 {
340 rv = VNET_API_ERROR_FEATURE_DISABLED;
341 goto done;
342 }
343
344 app = application_lookup (mp->client_index);
345 if (app)
346 {
347 a->app_index = app->index;
348 rv = vnet_application_detach (a);
349 }
350
351done:
352 REPLY_MACRO (VL_API_APPLICATION_DETACH_REPLY);
353}
354
355static void
356vl_api_bind_uri_t_handler (vl_api_bind_uri_t * mp)
357{
358 vl_api_bind_uri_reply_t *rmp;
359 vnet_bind_args_t _a, *a = &_a;
360 application_t *app;
361 int rv;
362
363 if (session_manager_is_enabled () == 0)
364 {
365 rv = VNET_API_ERROR_FEATURE_DISABLED;
366 goto done;
367 }
368
369 app = application_lookup (mp->client_index);
370 if (app)
371 {
372 memset (a, 0, sizeof (*a));
373 a->uri = (char *) mp->uri;
374 a->app_index = app->index;
375 rv = vnet_bind_uri (a);
376 }
377 else
378 {
379 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
380 }
381
382done:
383 REPLY_MACRO (VL_API_BIND_URI_REPLY);
384}
385
386static void
Dave Barach68b0fb02017-02-28 15:15:56 -0500387vl_api_unbind_uri_t_handler (vl_api_unbind_uri_t * mp)
388{
389 vl_api_unbind_uri_reply_t *rmp;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700390 application_t *app;
391 vnet_unbind_args_t _a, *a = &_a;
Dave Barach68b0fb02017-02-28 15:15:56 -0500392 int rv;
393
Florin Coras6cf30ad2017-04-04 23:08:23 -0700394 if (session_manager_is_enabled () == 0)
395 {
396 rv = VNET_API_ERROR_FEATURE_DISABLED;
397 goto done;
398 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500399
Florin Coras6cf30ad2017-04-04 23:08:23 -0700400 app = application_lookup (mp->client_index);
401 if (app)
402 {
403 a->uri = (char *) mp->uri;
404 a->app_index = app->index;
405 rv = vnet_unbind_uri (a);
406 }
407 else
408 {
409 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
410 }
411
412done:
Dave Barach68b0fb02017-02-28 15:15:56 -0500413 REPLY_MACRO (VL_API_UNBIND_URI_REPLY);
414}
415
416static void
417vl_api_connect_uri_t_handler (vl_api_connect_uri_t * mp)
418{
Florin Corase04c2992017-03-01 08:17:34 -0800419 vl_api_connect_uri_reply_t *rmp;
Dave Barach68b0fb02017-02-28 15:15:56 -0500420 vnet_connect_args_t _a, *a = &_a;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700421 application_t *app;
Florin Corase04c2992017-03-01 08:17:34 -0800422 int rv;
Dave Barach68b0fb02017-02-28 15:15:56 -0500423
Florin Coras6cf30ad2017-04-04 23:08:23 -0700424 if (session_manager_is_enabled () == 0)
425 {
426 rv = VNET_API_ERROR_FEATURE_DISABLED;
427 goto done;
428 }
Florin Corase04c2992017-03-01 08:17:34 -0800429
Florin Coras6cf30ad2017-04-04 23:08:23 -0700430 app = application_lookup (mp->client_index);
431 if (app)
432 {
433 a->uri = (char *) mp->uri;
434 a->api_context = mp->context;
435 a->app_index = app->index;
436 a->mp = mp;
437 rv = vnet_connect_uri (a);
438 }
439 else
440 {
441 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
442 }
Florin Corase04c2992017-03-01 08:17:34 -0800443
444 if (rv == 0 || rv == VNET_CONNECT_REDIRECTED)
445 return;
446
447 /* Got some error, relay it */
448
Florin Coras6cf30ad2017-04-04 23:08:23 -0700449done:
Florin Corase04c2992017-03-01 08:17:34 -0800450 /* *INDENT-OFF* */
Florin Coras6cf30ad2017-04-04 23:08:23 -0700451 REPLY_MACRO (VL_API_CONNECT_URI_REPLY);
Florin Corase04c2992017-03-01 08:17:34 -0800452 /* *INDENT-ON* */
Dave Barach68b0fb02017-02-28 15:15:56 -0500453}
454
455static void
456vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
457{
458 vl_api_disconnect_session_reply_t *rmp;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700459 vnet_disconnect_args_t _a, *a = &_a;
460 application_t *app;
461 int rv = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500462
Florin Coras6cf30ad2017-04-04 23:08:23 -0700463 if (session_manager_is_enabled () == 0)
464 {
465 rv = VNET_API_ERROR_FEATURE_DISABLED;
466 goto done;
467 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500468
Florin Coras6cf30ad2017-04-04 23:08:23 -0700469 app = application_lookup (mp->client_index);
470 if (app)
471 {
472 a->handle = mp->handle;
473 a->app_index = app->index;
474 rv = vnet_disconnect_session (a);
475 }
476 else
477 {
478 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
479 }
480
481done:
Dave Barach68b0fb02017-02-28 15:15:56 -0500482 REPLY_MACRO (VL_API_DISCONNECT_SESSION_REPLY);
483}
484
485static void
486vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
487 mp)
488{
Florin Coras6cf30ad2017-04-04 23:08:23 -0700489 vnet_disconnect_args_t _a, *a = &_a;
490 application_t *app;
Dave Barach68b0fb02017-02-28 15:15:56 -0500491
492 /* Client objected to disconnecting the session, log and continue */
493 if (mp->retval)
494 {
495 clib_warning ("client retval %d", mp->retval);
496 return;
497 }
498
499 /* Disconnect has been confirmed. Confirm close to transport */
Florin Coras6cf30ad2017-04-04 23:08:23 -0700500 app = application_lookup (mp->client_index);
501 if (app)
502 {
503 a->handle = mp->handle;
504 a->app_index = app->index;
505 vnet_disconnect_session (a);
506 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500507}
508
509static void
510vl_api_reset_session_reply_t_handler (vl_api_reset_session_reply_t * mp)
511{
Florin Coras6cf30ad2017-04-04 23:08:23 -0700512 application_t *app;
Dave Barach68b0fb02017-02-28 15:15:56 -0500513 stream_session_t *s;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700514 u32 index, thread_index;
Dave Barach68b0fb02017-02-28 15:15:56 -0500515
Florin Coras6cf30ad2017-04-04 23:08:23 -0700516 app = application_lookup (mp->client_index);
517 if (!app)
518 return;
519
520 stream_session_parse_handle (mp->handle, &index, &thread_index);
521 s = stream_session_get_if_valid (index, thread_index);
522 if (s == 0 || app->index != s->app_index)
Dave Barach68b0fb02017-02-28 15:15:56 -0500523 {
524 clib_warning ("Invalid session!");
525 return;
526 }
527
528 /* Client objected to resetting the session, log and continue */
529 if (mp->retval)
530 {
531 clib_warning ("client retval %d", mp->retval);
532 return;
533 }
534
Dave Barach68b0fb02017-02-28 15:15:56 -0500535 /* This comes as a response to a reset, transport only waiting for
536 * confirmation to remove connection state, no need to disconnect */
537 stream_session_cleanup (s);
538}
539
540static void
541vl_api_accept_session_reply_t_handler (vl_api_accept_session_reply_t * mp)
542{
543 stream_session_t *s;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700544 u32 session_index, thread_index;
Florin Corasa5464812017-04-19 13:00:05 -0700545 vnet_disconnect_args_t _a, *a = &_a;
Dave Barach68b0fb02017-02-28 15:15:56 -0500546
Florin Corasa5464812017-04-19 13:00:05 -0700547 /* Server isn't interested, kill the session */
548 if (mp->retval)
Dave Barach68b0fb02017-02-28 15:15:56 -0500549 {
Florin Corasa5464812017-04-19 13:00:05 -0700550 a->app_index = mp->context;
551 a->handle = mp->handle;
552 vnet_disconnect_session (a);
Dave Barach68b0fb02017-02-28 15:15:56 -0500553 }
Florin Corasa5464812017-04-19 13:00:05 -0700554 else
555 {
556 stream_session_parse_handle (mp->handle, &session_index, &thread_index);
557 s = stream_session_get_if_valid (session_index, thread_index);
558 if (!s)
559 {
560 clib_warning ("session doesn't exist");
561 return;
562 }
563 if (s->app_index != mp->context)
564 {
565 clib_warning ("app doesn't own session");
566 return;
567 }
568 /* XXX volatile? */
569 s->session_state = SESSION_STATE_READY;
570 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500571}
572
573static void
574vl_api_map_another_segment_reply_t_handler (vl_api_map_another_segment_reply_t
575 * mp)
576{
577 clib_warning ("not implemented");
578}
579
580static void
581vl_api_bind_sock_t_handler (vl_api_bind_sock_t * mp)
582{
583 vl_api_bind_sock_reply_t *rmp;
584 vnet_bind_args_t _a, *a = &_a;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700585 int rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
586 application_t *app;
Dave Barach68b0fb02017-02-28 15:15:56 -0500587
Florin Coras6cf30ad2017-04-04 23:08:23 -0700588 if (session_manager_is_enabled () == 0)
589 {
590 rv = VNET_API_ERROR_FEATURE_DISABLED;
591 goto done;
592 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500593
Florin Coras6cf30ad2017-04-04 23:08:23 -0700594 app = application_lookup (mp->client_index);
595 if (app)
596 {
597 memset (a, 0, sizeof (*a));
598 clib_memcpy (&a->tep.ip, mp->ip, (mp->is_ip4 ?
599 sizeof (ip4_address_t) :
600 sizeof (ip6_address_t)));
601 a->tep.is_ip4 = mp->is_ip4;
602 a->tep.port = mp->port;
603 a->tep.vrf = mp->vrf;
604 a->app_index = app->index;
Dave Barach68b0fb02017-02-28 15:15:56 -0500605
Florin Coras6cf30ad2017-04-04 23:08:23 -0700606 rv = vnet_bind (a);
607 }
608done:
609 REPLY_MACRO (VL_API_BIND_SOCK_REPLY);
Dave Barach68b0fb02017-02-28 15:15:56 -0500610}
611
612static void
613vl_api_unbind_sock_t_handler (vl_api_unbind_sock_t * mp)
614{
615 vl_api_unbind_sock_reply_t *rmp;
616 vnet_unbind_args_t _a, *a = &_a;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700617 application_t *app;
618 int rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
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 {
629 a->app_index = mp->client_index;
630 a->handle = mp->handle;
631 rv = vnet_unbind (a);
632 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500633
Florin Coras6cf30ad2017-04-04 23:08:23 -0700634done:
Dave Barach68b0fb02017-02-28 15:15:56 -0500635 REPLY_MACRO (VL_API_UNBIND_SOCK_REPLY);
636}
637
638static void
639vl_api_connect_sock_t_handler (vl_api_connect_sock_t * mp)
640{
Florin Corase04c2992017-03-01 08:17:34 -0800641 vl_api_connect_sock_reply_t *rmp;
Dave Barach68b0fb02017-02-28 15:15:56 -0500642 vnet_connect_args_t _a, *a = &_a;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700643 application_t *app;
Florin Corase04c2992017-03-01 08:17:34 -0800644 int rv;
Dave Barach68b0fb02017-02-28 15:15:56 -0500645
Florin Coras6cf30ad2017-04-04 23:08:23 -0700646 if (session_manager_is_enabled () == 0)
647 {
648 rv = VNET_API_ERROR_FEATURE_DISABLED;
649 goto done;
650 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500651
Florin Coras6cf30ad2017-04-04 23:08:23 -0700652 app = application_lookup (mp->client_index);
653 if (app)
654 {
655 clib_memcpy (&a->tep.ip, mp->ip,
656 (mp->is_ip4 ? sizeof (ip4_address_t) :
657 sizeof (ip6_address_t)));
658 a->api_context = mp->context;
659 a->app_index = app->index;
660 a->mp = mp;
661 rv = vnet_connect (a);
662 }
663 else
664 {
665 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
666 }
Florin Corase04c2992017-03-01 08:17:34 -0800667
668 if (rv == 0 || rv == VNET_CONNECT_REDIRECTED)
669 return;
670
671 /* Got some error, relay it */
672
Florin Coras6cf30ad2017-04-04 23:08:23 -0700673done:
674 REPLY_MACRO (VL_API_CONNECT_URI_REPLY);
Dave Barach68b0fb02017-02-28 15:15:56 -0500675}
676
Florin Coras6cf30ad2017-04-04 23:08:23 -0700677static clib_error_t *
678application_reaper_cb (u32 client_index)
Dave Barach68b0fb02017-02-28 15:15:56 -0500679{
Florin Coras6cf30ad2017-04-04 23:08:23 -0700680 application_t *app = application_lookup (client_index);
681 vnet_app_detach_args_t _a, *a = &_a;
682 if (app)
683 {
684 a->app_index = app->index;
685 vnet_application_detach (a);
686 }
687 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500688}
689
Florin Coras6cf30ad2017-04-04 23:08:23 -0700690VL_MSG_API_REAPER_FUNCTION (application_reaper_cb);
Dave Barach68b0fb02017-02-28 15:15:56 -0500691
692#define vl_msg_name_crc_list
693#include <vnet/vnet_all_api_h.h>
694#undef vl_msg_name_crc_list
695
696static void
697setup_message_id_table (api_main_t * am)
698{
699#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
700 foreach_vl_msg_name_crc_session;
701#undef _
702}
703
704/*
705 * session_api_hookup
706 * Add uri's API message handlers to the table.
707 * vlib has alread mapped shared memory and
708 * added the client registration handlers.
709 * See .../open-repo/vlib/memclnt_vlib.c:memclnt_process()
710 */
711static clib_error_t *
712session_api_hookup (vlib_main_t * vm)
713{
714 api_main_t *am = &api_main;
715
716#define _(N,n) \
717 vl_msg_api_set_handlers(VL_API_##N, #n, \
718 vl_api_##n##_t_handler, \
719 vl_noop_handler, \
720 vl_api_##n##_t_endian, \
721 vl_api_##n##_t_print, \
722 sizeof(vl_api_##n##_t), 1);
723 foreach_session_api_msg;
724#undef _
725
726 /*
727 * Messages which bounce off the data-plane to
728 * an API client. Simply tells the message handling infra not
729 * to free the message.
730 *
731 * Bounced message handlers MUST NOT block the data plane
732 */
733 am->message_bounce[VL_API_CONNECT_URI] = 1;
734 am->message_bounce[VL_API_CONNECT_SOCK] = 1;
735
736 /*
737 * Set up the (msg_name, crc, message-id) table
738 */
739 setup_message_id_table (am);
740
741 return 0;
742}
743
744VLIB_API_INIT_FUNCTION (session_api_hookup);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700745
Dave Barach68b0fb02017-02-28 15:15:56 -0500746/*
747 * fd.io coding-style-patch-verification: ON
748 *
749 * Local Variables:
750 * eval: (c-set-style "gnu")
751 * End:
752 */