blob: a82dfe0b9011204f09afe7125a3f61b1684f7e35 [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);
99
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 {
183 mp->retval = VNET_API_ERROR_SESSION_CONNECT_FAIL;
184 }
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
273static int
274api_session_not_valid (u32 session_index, u32 thread_index)
275{
276 session_manager_main_t *smm = vnet_get_session_manager_main ();
277 stream_session_t *pool;
278
279 if (thread_index >= vec_len (smm->sessions))
280 return VNET_API_ERROR_INVALID_VALUE;
281
282 pool = smm->sessions[thread_index];
283
284 if (pool_is_free_index (pool, session_index))
285 return VNET_API_ERROR_INVALID_VALUE_2;
286
287 return 0;
288}
289
290static void
Florin Corase04c2992017-03-01 08:17:34 -0800291vl_api_session_enable_disable_t_handler (vl_api_session_enable_disable_t * mp)
292{
293 vl_api_session_enable_disable_reply_t *rmp;
294 vlib_main_t *vm = vlib_get_main ();
295 int rv = 0;
296
297 vnet_session_enable_disable (vm, mp->is_enable);
298 REPLY_MACRO (VL_API_SESSION_ENABLE_DISABLE_REPLY);
299}
300
301static void
Florin Coras6cf30ad2017-04-04 23:08:23 -0700302vl_api_application_attach_t_handler (vl_api_application_attach_t * mp)
Dave Barach68b0fb02017-02-28 15:15:56 -0500303{
Florin Coras6cf30ad2017-04-04 23:08:23 -0700304 vl_api_application_attach_reply_t *rmp;
305 vnet_app_attach_args_t _a, *a = &_a;
Dave Barach68b0fb02017-02-28 15:15:56 -0500306 int rv;
307
Florin Coras6cf30ad2017-04-04 23:08:23 -0700308 if (session_manager_is_enabled () == 0)
309 {
310 rv = VNET_API_ERROR_FEATURE_DISABLED;
311 goto done;
312 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500313
Florin Coras6cf30ad2017-04-04 23:08:23 -0700314 STATIC_ASSERT (sizeof (u64) * SESSION_OPTIONS_N_OPTIONS <=
315 sizeof (mp->options),
316 "Out of options, fix api message definition");
Dave Barach68b0fb02017-02-28 15:15:56 -0500317
318 memset (a, 0, sizeof (*a));
319
Dave Barach68b0fb02017-02-28 15:15:56 -0500320 a->api_client_index = mp->client_index;
321 a->options = mp->options;
Dave Barach68b0fb02017-02-28 15:15:56 -0500322 a->session_cb_vft = &uri_session_cb_vft;
323
Florin Coras6cf30ad2017-04-04 23:08:23 -0700324 rv = vnet_application_attach (a);
Dave Barach68b0fb02017-02-28 15:15:56 -0500325
Florin Coras6cf30ad2017-04-04 23:08:23 -0700326done:
Dave Barach68b0fb02017-02-28 15:15:56 -0500327 /* *INDENT-OFF* */
Florin Coras6cf30ad2017-04-04 23:08:23 -0700328 REPLY_MACRO2 (VL_API_APPLICATION_ATTACH_REPLY, ({
Dave Barach68b0fb02017-02-28 15:15:56 -0500329 rmp->retval = rv;
330 if (!rv)
331 {
332 rmp->segment_name_length = 0;
333 /* $$$$ policy? */
Florin Coras6cf30ad2017-04-04 23:08:23 -0700334 rmp->segment_size = a->segment_size;
335 if (a->segment_name_length)
Dave Barach68b0fb02017-02-28 15:15:56 -0500336 {
Florin Coras6cf30ad2017-04-04 23:08:23 -0700337 memcpy (rmp->segment_name, a->segment_name,
338 a->segment_name_length);
339 rmp->segment_name_length = a->segment_name_length;
Dave Barach68b0fb02017-02-28 15:15:56 -0500340 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700341 rmp->app_event_queue_address = a->app_event_queue_address;
Dave Barach68b0fb02017-02-28 15:15:56 -0500342 }
343 }));
344 /* *INDENT-ON* */
Dave Barach68b0fb02017-02-28 15:15:56 -0500345}
346
347static void
Florin Coras6cf30ad2017-04-04 23:08:23 -0700348vl_api_application_detach_t_handler (vl_api_application_detach_t * mp)
349{
350 vl_api_application_detach_reply_t *rmp;
351 int rv = VNET_API_ERROR_INVALID_VALUE_2;
352 vnet_app_detach_args_t _a, *a = &_a;
353 application_t *app;
354
355 if (session_manager_is_enabled () == 0)
356 {
357 rv = VNET_API_ERROR_FEATURE_DISABLED;
358 goto done;
359 }
360
361 app = application_lookup (mp->client_index);
362 if (app)
363 {
364 a->app_index = app->index;
365 rv = vnet_application_detach (a);
366 }
367
368done:
369 REPLY_MACRO (VL_API_APPLICATION_DETACH_REPLY);
370}
371
372static void
373vl_api_bind_uri_t_handler (vl_api_bind_uri_t * mp)
374{
375 vl_api_bind_uri_reply_t *rmp;
376 vnet_bind_args_t _a, *a = &_a;
377 application_t *app;
378 int rv;
379
380 if (session_manager_is_enabled () == 0)
381 {
382 rv = VNET_API_ERROR_FEATURE_DISABLED;
383 goto done;
384 }
385
386 app = application_lookup (mp->client_index);
387 if (app)
388 {
389 memset (a, 0, sizeof (*a));
390 a->uri = (char *) mp->uri;
391 a->app_index = app->index;
392 rv = vnet_bind_uri (a);
393 }
394 else
395 {
396 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
397 }
398
399done:
400 REPLY_MACRO (VL_API_BIND_URI_REPLY);
401}
402
403static void
Dave Barach68b0fb02017-02-28 15:15:56 -0500404vl_api_unbind_uri_t_handler (vl_api_unbind_uri_t * mp)
405{
406 vl_api_unbind_uri_reply_t *rmp;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700407 application_t *app;
408 vnet_unbind_args_t _a, *a = &_a;
Dave Barach68b0fb02017-02-28 15:15:56 -0500409 int rv;
410
Florin Coras6cf30ad2017-04-04 23:08:23 -0700411 if (session_manager_is_enabled () == 0)
412 {
413 rv = VNET_API_ERROR_FEATURE_DISABLED;
414 goto done;
415 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500416
Florin Coras6cf30ad2017-04-04 23:08:23 -0700417 app = application_lookup (mp->client_index);
418 if (app)
419 {
420 a->uri = (char *) mp->uri;
421 a->app_index = app->index;
422 rv = vnet_unbind_uri (a);
423 }
424 else
425 {
426 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
427 }
428
429done:
Dave Barach68b0fb02017-02-28 15:15:56 -0500430 REPLY_MACRO (VL_API_UNBIND_URI_REPLY);
431}
432
433static void
434vl_api_connect_uri_t_handler (vl_api_connect_uri_t * mp)
435{
Florin Corase04c2992017-03-01 08:17:34 -0800436 vl_api_connect_uri_reply_t *rmp;
Dave Barach68b0fb02017-02-28 15:15:56 -0500437 vnet_connect_args_t _a, *a = &_a;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700438 application_t *app;
Florin Corase04c2992017-03-01 08:17:34 -0800439 int rv;
Dave Barach68b0fb02017-02-28 15:15:56 -0500440
Florin Coras6cf30ad2017-04-04 23:08:23 -0700441 if (session_manager_is_enabled () == 0)
442 {
443 rv = VNET_API_ERROR_FEATURE_DISABLED;
444 goto done;
445 }
Florin Corase04c2992017-03-01 08:17:34 -0800446
Florin Coras6cf30ad2017-04-04 23:08:23 -0700447 app = application_lookup (mp->client_index);
448 if (app)
449 {
450 a->uri = (char *) mp->uri;
451 a->api_context = mp->context;
452 a->app_index = app->index;
453 a->mp = mp;
454 rv = vnet_connect_uri (a);
455 }
456 else
457 {
458 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
459 }
Florin Corase04c2992017-03-01 08:17:34 -0800460
461 if (rv == 0 || rv == VNET_CONNECT_REDIRECTED)
462 return;
463
464 /* Got some error, relay it */
465
Florin Coras6cf30ad2017-04-04 23:08:23 -0700466done:
Florin Corase04c2992017-03-01 08:17:34 -0800467 /* *INDENT-OFF* */
Florin Coras6cf30ad2017-04-04 23:08:23 -0700468 REPLY_MACRO (VL_API_CONNECT_URI_REPLY);
Florin Corase04c2992017-03-01 08:17:34 -0800469 /* *INDENT-ON* */
Dave Barach68b0fb02017-02-28 15:15:56 -0500470}
471
472static void
473vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
474{
475 vl_api_disconnect_session_reply_t *rmp;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700476 vnet_disconnect_args_t _a, *a = &_a;
477 application_t *app;
478 int rv = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500479
Florin Coras6cf30ad2017-04-04 23:08:23 -0700480 if (session_manager_is_enabled () == 0)
481 {
482 rv = VNET_API_ERROR_FEATURE_DISABLED;
483 goto done;
484 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500485
Florin Coras6cf30ad2017-04-04 23:08:23 -0700486 app = application_lookup (mp->client_index);
487 if (app)
488 {
489 a->handle = mp->handle;
490 a->app_index = app->index;
491 rv = vnet_disconnect_session (a);
492 }
493 else
494 {
495 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
496 }
497
498done:
Dave Barach68b0fb02017-02-28 15:15:56 -0500499 REPLY_MACRO (VL_API_DISCONNECT_SESSION_REPLY);
500}
501
502static void
503vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
504 mp)
505{
Florin Coras6cf30ad2017-04-04 23:08:23 -0700506 vnet_disconnect_args_t _a, *a = &_a;
507 application_t *app;
Dave Barach68b0fb02017-02-28 15:15:56 -0500508
509 /* Client objected to disconnecting the session, log and continue */
510 if (mp->retval)
511 {
512 clib_warning ("client retval %d", mp->retval);
513 return;
514 }
515
516 /* Disconnect has been confirmed. Confirm close to transport */
Florin Coras6cf30ad2017-04-04 23:08:23 -0700517 app = application_lookup (mp->client_index);
518 if (app)
519 {
520 a->handle = mp->handle;
521 a->app_index = app->index;
522 vnet_disconnect_session (a);
523 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500524}
525
526static void
527vl_api_reset_session_reply_t_handler (vl_api_reset_session_reply_t * mp)
528{
Florin Coras6cf30ad2017-04-04 23:08:23 -0700529 application_t *app;
Dave Barach68b0fb02017-02-28 15:15:56 -0500530 stream_session_t *s;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700531 u32 index, thread_index;
Dave Barach68b0fb02017-02-28 15:15:56 -0500532
Florin Coras6cf30ad2017-04-04 23:08:23 -0700533 app = application_lookup (mp->client_index);
534 if (!app)
535 return;
536
537 stream_session_parse_handle (mp->handle, &index, &thread_index);
538 s = stream_session_get_if_valid (index, thread_index);
539 if (s == 0 || app->index != s->app_index)
Dave Barach68b0fb02017-02-28 15:15:56 -0500540 {
541 clib_warning ("Invalid session!");
542 return;
543 }
544
545 /* Client objected to resetting the session, log and continue */
546 if (mp->retval)
547 {
548 clib_warning ("client retval %d", mp->retval);
549 return;
550 }
551
Dave Barach68b0fb02017-02-28 15:15:56 -0500552 /* This comes as a response to a reset, transport only waiting for
553 * confirmation to remove connection state, no need to disconnect */
554 stream_session_cleanup (s);
555}
556
557static void
558vl_api_accept_session_reply_t_handler (vl_api_accept_session_reply_t * mp)
559{
560 stream_session_t *s;
561 int rv;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700562 u32 session_index, thread_index;
563 session_index = stream_session_index_from_handle (mp->handle);
564 thread_index = stream_session_thread_from_handle (mp->handle);
565 if (api_session_not_valid (session_index, thread_index))
Dave Barach68b0fb02017-02-28 15:15:56 -0500566 return;
567
Florin Coras6cf30ad2017-04-04 23:08:23 -0700568 s = stream_session_get (session_index, thread_index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500569 rv = mp->retval;
570
571 if (rv)
572 {
573 /* Server isn't interested, kill the session */
574 stream_session_disconnect (s);
575 return;
576 }
577
578 s->session_state = SESSION_STATE_READY;
579}
580
581static void
582vl_api_map_another_segment_reply_t_handler (vl_api_map_another_segment_reply_t
583 * mp)
584{
585 clib_warning ("not implemented");
586}
587
588static void
589vl_api_bind_sock_t_handler (vl_api_bind_sock_t * mp)
590{
591 vl_api_bind_sock_reply_t *rmp;
592 vnet_bind_args_t _a, *a = &_a;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700593 int rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
594 application_t *app;
Dave Barach68b0fb02017-02-28 15:15:56 -0500595
Florin Coras6cf30ad2017-04-04 23:08:23 -0700596 if (session_manager_is_enabled () == 0)
597 {
598 rv = VNET_API_ERROR_FEATURE_DISABLED;
599 goto done;
600 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500601
Florin Coras6cf30ad2017-04-04 23:08:23 -0700602 app = application_lookup (mp->client_index);
603 if (app)
604 {
605 memset (a, 0, sizeof (*a));
606 clib_memcpy (&a->tep.ip, mp->ip, (mp->is_ip4 ?
607 sizeof (ip4_address_t) :
608 sizeof (ip6_address_t)));
609 a->tep.is_ip4 = mp->is_ip4;
610 a->tep.port = mp->port;
611 a->tep.vrf = mp->vrf;
612 a->app_index = app->index;
Dave Barach68b0fb02017-02-28 15:15:56 -0500613
Florin Coras6cf30ad2017-04-04 23:08:23 -0700614 rv = vnet_bind (a);
615 }
616done:
617 REPLY_MACRO (VL_API_BIND_SOCK_REPLY);
Dave Barach68b0fb02017-02-28 15:15:56 -0500618}
619
620static void
621vl_api_unbind_sock_t_handler (vl_api_unbind_sock_t * mp)
622{
623 vl_api_unbind_sock_reply_t *rmp;
624 vnet_unbind_args_t _a, *a = &_a;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700625 application_t *app;
626 int rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
Dave Barach68b0fb02017-02-28 15:15:56 -0500627
Florin Coras6cf30ad2017-04-04 23:08:23 -0700628 if (session_manager_is_enabled () == 0)
629 {
630 rv = VNET_API_ERROR_FEATURE_DISABLED;
631 goto done;
632 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500633
Florin Coras6cf30ad2017-04-04 23:08:23 -0700634 app = application_lookup (mp->client_index);
635 if (app)
636 {
637 a->app_index = mp->client_index;
638 a->handle = mp->handle;
639 rv = vnet_unbind (a);
640 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500641
Florin Coras6cf30ad2017-04-04 23:08:23 -0700642done:
Dave Barach68b0fb02017-02-28 15:15:56 -0500643 REPLY_MACRO (VL_API_UNBIND_SOCK_REPLY);
644}
645
646static void
647vl_api_connect_sock_t_handler (vl_api_connect_sock_t * mp)
648{
Florin Corase04c2992017-03-01 08:17:34 -0800649 vl_api_connect_sock_reply_t *rmp;
Dave Barach68b0fb02017-02-28 15:15:56 -0500650 vnet_connect_args_t _a, *a = &_a;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700651 application_t *app;
Florin Corase04c2992017-03-01 08:17:34 -0800652 int rv;
Dave Barach68b0fb02017-02-28 15:15:56 -0500653
Florin Coras6cf30ad2017-04-04 23:08:23 -0700654 if (session_manager_is_enabled () == 0)
655 {
656 rv = VNET_API_ERROR_FEATURE_DISABLED;
657 goto done;
658 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500659
Florin Coras6cf30ad2017-04-04 23:08:23 -0700660 app = application_lookup (mp->client_index);
661 if (app)
662 {
663 clib_memcpy (&a->tep.ip, mp->ip,
664 (mp->is_ip4 ? sizeof (ip4_address_t) :
665 sizeof (ip6_address_t)));
666 a->api_context = mp->context;
667 a->app_index = app->index;
668 a->mp = mp;
669 rv = vnet_connect (a);
670 }
671 else
672 {
673 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
674 }
Florin Corase04c2992017-03-01 08:17:34 -0800675
676 if (rv == 0 || rv == VNET_CONNECT_REDIRECTED)
677 return;
678
679 /* Got some error, relay it */
680
Florin Coras6cf30ad2017-04-04 23:08:23 -0700681done:
682 REPLY_MACRO (VL_API_CONNECT_URI_REPLY);
Dave Barach68b0fb02017-02-28 15:15:56 -0500683}
684
Florin Coras6cf30ad2017-04-04 23:08:23 -0700685static clib_error_t *
686application_reaper_cb (u32 client_index)
Dave Barach68b0fb02017-02-28 15:15:56 -0500687{
Florin Coras6cf30ad2017-04-04 23:08:23 -0700688 application_t *app = application_lookup (client_index);
689 vnet_app_detach_args_t _a, *a = &_a;
690 if (app)
691 {
692 a->app_index = app->index;
693 vnet_application_detach (a);
694 }
695 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500696}
697
Florin Coras6cf30ad2017-04-04 23:08:23 -0700698VL_MSG_API_REAPER_FUNCTION (application_reaper_cb);
Dave Barach68b0fb02017-02-28 15:15:56 -0500699
700#define vl_msg_name_crc_list
701#include <vnet/vnet_all_api_h.h>
702#undef vl_msg_name_crc_list
703
704static void
705setup_message_id_table (api_main_t * am)
706{
707#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
708 foreach_vl_msg_name_crc_session;
709#undef _
710}
711
712/*
713 * session_api_hookup
714 * Add uri's API message handlers to the table.
715 * vlib has alread mapped shared memory and
716 * added the client registration handlers.
717 * See .../open-repo/vlib/memclnt_vlib.c:memclnt_process()
718 */
719static clib_error_t *
720session_api_hookup (vlib_main_t * vm)
721{
722 api_main_t *am = &api_main;
723
724#define _(N,n) \
725 vl_msg_api_set_handlers(VL_API_##N, #n, \
726 vl_api_##n##_t_handler, \
727 vl_noop_handler, \
728 vl_api_##n##_t_endian, \
729 vl_api_##n##_t_print, \
730 sizeof(vl_api_##n##_t), 1);
731 foreach_session_api_msg;
732#undef _
733
734 /*
735 * Messages which bounce off the data-plane to
736 * an API client. Simply tells the message handling infra not
737 * to free the message.
738 *
739 * Bounced message handlers MUST NOT block the data plane
740 */
741 am->message_bounce[VL_API_CONNECT_URI] = 1;
742 am->message_bounce[VL_API_CONNECT_SOCK] = 1;
743
744 /*
745 * Set up the (msg_name, crc, message-id) table
746 */
747 setup_message_id_table (am);
748
749 return 0;
750}
751
752VLIB_API_INIT_FUNCTION (session_api_hookup);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700753
Dave Barach68b0fb02017-02-28 15:15:56 -0500754/*
755 * fd.io coding-style-patch-verification: ON
756 *
757 * Local Variables:
758 * eval: (c-set-style "gnu")
759 * End:
760 */