blob: 8852fc6ee08bdced50d57d60a2dcee9ca53e55c6 [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) \
41_(BIND_URI, bind_uri) \
42_(UNBIND_URI, unbind_uri) \
43_(CONNECT_URI, connect_uri) \
44_(DISCONNECT_SESSION, disconnect_session) \
45_(DISCONNECT_SESSION_REPLY, disconnect_session_reply) \
46_(ACCEPT_SESSION_REPLY, accept_session_reply) \
47_(RESET_SESSION_REPLY, reset_session_reply) \
48_(BIND_SOCK, bind_sock) \
49_(UNBIND_SOCK, unbind_sock) \
50_(CONNECT_SOCK, connect_sock) \
51_(DISCONNECT_SOCK, disconnect_sock) \
52_(DISCONNECT_SOCK_REPLY, disconnect_sock_reply) \
53_(ACCEPT_SOCK_REPLY, accept_sock_reply) \
54_(RESET_SOCK_REPLY, reset_sock_reply) \
Florin Corase04c2992017-03-01 08:17:34 -080055_(SESSION_ENABLE_DISABLE, session_enable_disable) \
56
Dave Barach68b0fb02017-02-28 15:15:56 -050057
58static int
59send_add_segment_callback (u32 api_client_index, const u8 * segment_name,
60 u32 segment_size)
61{
62 vl_api_map_another_segment_t *mp;
63 unix_shared_memory_queue_t *q;
64
65 q = vl_api_client_index_to_input_queue (api_client_index);
66
67 if (!q)
68 return -1;
69
70 mp = vl_msg_api_alloc (sizeof (*mp));
71 memset (mp, 0, sizeof (*mp));
72 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_MAP_ANOTHER_SEGMENT);
73 mp->segment_size = segment_size;
74 strncpy ((char *) mp->segment_name, (char *) segment_name,
75 sizeof (mp->segment_name) - 1);
76
77 vl_msg_api_send_shmem (q, (u8 *) & mp);
78
79 return 0;
80}
81
82static int
83send_session_accept_uri_callback (stream_session_t * s)
84{
85 vl_api_accept_session_t *mp;
86 unix_shared_memory_queue_t *q, *vpp_queue;
87 application_t *server = application_get (s->app_index);
88
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));
96 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_ACCEPT_SESSION);
97
98 /* Note: session_type is the first octet in all types of sessions */
99
100 mp->accept_cookie = server->accept_cookie;
101 mp->server_rx_fifo = (u64) s->server_rx_fifo;
102 mp->server_tx_fifo = (u64) s->server_tx_fifo;
103 mp->session_thread_index = s->thread_index;
104 mp->session_index = s->session_index;
105 mp->session_type = s->session_type;
106 mp->vpp_event_queue_address = (u64) vpp_queue;
107 vl_msg_api_send_shmem (q, (u8 *) & mp);
108
109 return 0;
110}
111
112static void
113send_session_disconnect_uri_callback (stream_session_t * s)
114{
115 vl_api_disconnect_session_t *mp;
116 unix_shared_memory_queue_t *q;
117 application_t *app = application_get (s->app_index);
118
119 q = vl_api_client_index_to_input_queue (app->api_client_index);
120
121 if (!q)
122 return;
123
124 mp = vl_msg_api_alloc (sizeof (*mp));
125 memset (mp, 0, sizeof (*mp));
126 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_DISCONNECT_SESSION);
127
128 mp->session_thread_index = s->thread_index;
129 mp->session_index = s->session_index;
130 vl_msg_api_send_shmem (q, (u8 *) & mp);
131}
132
133static int
134send_session_connected_uri_callback (u32 api_client_index,
135 stream_session_t * s, u8 is_fail)
136{
137 vl_api_connect_uri_reply_t *mp;
138 unix_shared_memory_queue_t *q;
139 application_t *app = application_lookup (api_client_index);
140 u8 *seg_name;
141 unix_shared_memory_queue_t *vpp_queue;
142
143 q = vl_api_client_index_to_input_queue (app->api_client_index);
144
145 if (!q)
146 return -1;
147
148 mp = vl_msg_api_alloc (sizeof (*mp));
149 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_CONNECT_URI_REPLY);
150 mp->context = app->api_context;
Dave Barach68b0fb02017-02-28 15:15:56 -0500151 if (!is_fail)
152 {
153 vpp_queue = session_manager_get_vpp_event_queue (s->thread_index);
154 mp->server_rx_fifo = (u64) s->server_rx_fifo;
155 mp->server_tx_fifo = (u64) s->server_tx_fifo;
156 mp->session_thread_index = s->thread_index;
157 mp->session_index = s->session_index;
158 mp->session_type = s->session_type;
159 mp->vpp_event_queue_address = (u64) vpp_queue;
160 mp->client_event_queue_address = (u64) app->event_queue;
Florin Corase04c2992017-03-01 08:17:34 -0800161 mp->retval = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500162
163 session_manager_get_segment_info (s->server_segment_index, &seg_name,
164 &mp->segment_size);
165 mp->segment_name_length = vec_len (seg_name);
166 if (mp->segment_name_length)
167 clib_memcpy (mp->segment_name, seg_name, mp->segment_name_length);
168 }
Florin Corase04c2992017-03-01 08:17:34 -0800169 else
170 {
171 mp->retval = VNET_API_ERROR_SESSION_CONNECT_FAIL;
172 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500173
174 vl_msg_api_send_shmem (q, (u8 *) & mp);
175
176 /* Remove client if connect failed */
177 if (is_fail)
Florin Corase04c2992017-03-01 08:17:34 -0800178 {
179 application_del (app);
180 }
181 else
182 {
183 s->session_state = SESSION_STATE_READY;
184 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500185
186 return 0;
187}
188
189/**
190 * Redirect a connect_uri message to the indicated server.
191 * Only sent if the server has bound the related port with
192 * URI_OPTIONS_FLAGS_USE_FIFO
193 */
194static int
195redirect_connect_uri_callback (u32 server_api_client_index, void *mp_arg)
196{
197 vl_api_connect_uri_t *mp = mp_arg;
198 unix_shared_memory_queue_t *server_q, *client_q;
199 vlib_main_t *vm = vlib_get_main ();
200 f64 timeout = vlib_time_now (vm) + 0.5;
201 int rv = 0;
202
203 server_q = vl_api_client_index_to_input_queue (server_api_client_index);
204
205 if (!server_q)
206 {
207 rv = VNET_API_ERROR_INVALID_VALUE;
208 goto out;
209 }
210
211 client_q = vl_api_client_index_to_input_queue (mp->client_index);
212 if (!client_q)
213 {
214 rv = VNET_API_ERROR_INVALID_VALUE_2;
215 goto out;
216 }
217
218 /* Tell the server the client's API queue address, so it can reply */
219 mp->client_queue_address = (u64) client_q;
220
221 /*
222 * Bounce message handlers MUST NOT block the data-plane.
223 * Spin waiting for the queue lock, but
224 */
225
226 while (vlib_time_now (vm) < timeout)
227 {
228 rv =
229 unix_shared_memory_queue_add (server_q, (u8 *) & mp, 1 /*nowait */ );
230 switch (rv)
231 {
232 /* correctly enqueued */
233 case 0:
234 return VNET_CONNECT_REDIRECTED;
235
236 /* continue spinning, wait for pthread_mutex_trylock to work */
237 case -1:
238 continue;
239
240 /* queue stuffed, drop the msg */
241 case -2:
242 rv = VNET_API_ERROR_QUEUE_FULL;
243 goto out;
244 }
245 }
246out:
247 /* Dispose of the message */
248 vl_msg_api_free (mp);
249 return rv;
250}
251
252static u64
253make_session_handle (stream_session_t * s)
254{
255 return (u64) s->session_index << 32 | (u64) s->thread_index;
256}
257
258static int
259send_session_accept_callback (stream_session_t * s)
260{
261 vl_api_accept_sock_t *mp;
262 unix_shared_memory_queue_t *q, *vpp_queue;
263 application_t *server = application_get (s->app_index);
264
265 q = vl_api_client_index_to_input_queue (server->api_client_index);
266 vpp_queue = session_manager_get_vpp_event_queue (s->thread_index);
267
268 if (!q)
269 return -1;
270
271 mp = vl_msg_api_alloc (sizeof (*mp));
272 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_ACCEPT_SOCK);
273
274 /* Note: session_type is the first octet in all types of sessions */
275
276 mp->accept_cookie = server->accept_cookie;
277 mp->server_rx_fifo = (u64) s->server_rx_fifo;
278 mp->server_tx_fifo = (u64) s->server_tx_fifo;
279 mp->handle = make_session_handle (s);
280 mp->vpp_event_queue_address = (u64) vpp_queue;
281 vl_msg_api_send_shmem (q, (u8 *) & mp);
282
283 return 0;
284}
285
286static int
287send_session_connected_callback (u32 api_client_index, stream_session_t * s,
288 u8 is_fail)
289{
290 vl_api_connect_sock_reply_t *mp;
291 unix_shared_memory_queue_t *q;
292 application_t *app = application_lookup (api_client_index);
293 u8 *seg_name;
294 unix_shared_memory_queue_t *vpp_queue;
295
296 q = vl_api_client_index_to_input_queue (app->api_client_index);
297
298 if (!q)
299 return -1;
300
301 mp = vl_msg_api_alloc (sizeof (*mp));
302 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_CONNECT_SOCK_REPLY);
303 mp->context = app->api_context;
304 mp->retval = is_fail;
305 if (!is_fail)
306 {
307 vpp_queue = session_manager_get_vpp_event_queue (s->thread_index);
308 mp->server_rx_fifo = (u64) s->server_rx_fifo;
309 mp->server_tx_fifo = (u64) s->server_tx_fifo;
310 mp->handle = make_session_handle (s);
311 mp->vpp_event_queue_address = (u64) vpp_queue;
312 mp->client_event_queue_address = (u64) app->event_queue;
313
314 session_manager_get_segment_info (s->server_segment_index, &seg_name,
315 &mp->segment_size);
316 mp->segment_name_length = vec_len (seg_name);
317 if (mp->segment_name_length)
318 clib_memcpy (mp->segment_name, seg_name, mp->segment_name_length);
319 }
320
321 vl_msg_api_send_shmem (q, (u8 *) & mp);
322
323 /* Remove client if connect failed */
324 if (is_fail)
325 application_del (app);
326
327 return 0;
328}
329
330static void
331send_session_disconnect_callback (stream_session_t * s)
332{
333 vl_api_disconnect_sock_t *mp;
334 unix_shared_memory_queue_t *q;
335 application_t *app = application_get (s->app_index);
336
337 q = vl_api_client_index_to_input_queue (app->api_client_index);
338
339 if (!q)
340 return;
341
342 mp = vl_msg_api_alloc (sizeof (*mp));
343 memset (mp, 0, sizeof (*mp));
344 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_DISCONNECT_SOCK);
345
346 mp->handle = make_session_handle (s);
347 vl_msg_api_send_shmem (q, (u8 *) & mp);
348}
349
350/**
351 * Redirect a connect_uri message to the indicated server.
352 * Only sent if the server has bound the related port with
353 * URI_OPTIONS_FLAGS_USE_FIFO
354 */
355static int
356redirect_connect_callback (u32 server_api_client_index, void *mp_arg)
357{
358 vl_api_connect_sock_t *mp = mp_arg;
359 unix_shared_memory_queue_t *server_q, *client_q;
360 vlib_main_t *vm = vlib_get_main ();
361 f64 timeout = vlib_time_now (vm) + 0.5;
362 int rv = 0;
363
364 server_q = vl_api_client_index_to_input_queue (server_api_client_index);
365
366 if (!server_q)
367 {
368 rv = VNET_API_ERROR_INVALID_VALUE;
369 goto out;
370 }
371
372 client_q = vl_api_client_index_to_input_queue (mp->client_index);
373 if (!client_q)
374 {
375 rv = VNET_API_ERROR_INVALID_VALUE_2;
376 goto out;
377 }
378
379 /* Tell the server the client's API queue address, so it can reply */
380 mp->client_queue_address = (u64) client_q;
381
382 /*
383 * Bounce message handlers MUST NOT block the data-plane.
384 * Spin waiting for the queue lock, but
385 */
386
387 while (vlib_time_now (vm) < timeout)
388 {
389 rv =
390 unix_shared_memory_queue_add (server_q, (u8 *) & mp, 1 /*nowait */ );
391 switch (rv)
392 {
393 /* correctly enqueued */
394 case 0:
395 return VNET_CONNECT_REDIRECTED;
396
397 /* continue spinning, wait for pthread_mutex_trylock to work */
398 case -1:
399 continue;
400
401 /* queue stuffed, drop the msg */
402 case -2:
403 rv = VNET_API_ERROR_QUEUE_FULL;
404 goto out;
405 }
406 }
407out:
408 /* Dispose of the message */
409 vl_msg_api_free (mp);
410 return rv;
411}
412
413static session_cb_vft_t uri_session_cb_vft = {
414 .session_accept_callback = send_session_accept_uri_callback,
415 .session_disconnect_callback = send_session_disconnect_uri_callback,
416 .session_connected_callback = send_session_connected_uri_callback,
417 .add_segment_callback = send_add_segment_callback,
418 .redirect_connect_callback = redirect_connect_uri_callback
419};
420
421static session_cb_vft_t session_cb_vft = {
422 .session_accept_callback = send_session_accept_callback,
423 .session_disconnect_callback = send_session_disconnect_callback,
424 .session_connected_callback = send_session_connected_callback,
425 .add_segment_callback = send_add_segment_callback,
426 .redirect_connect_callback = redirect_connect_callback
427};
428
429static int
430api_session_not_valid (u32 session_index, u32 thread_index)
431{
432 session_manager_main_t *smm = vnet_get_session_manager_main ();
433 stream_session_t *pool;
434
435 if (thread_index >= vec_len (smm->sessions))
436 return VNET_API_ERROR_INVALID_VALUE;
437
438 pool = smm->sessions[thread_index];
439
440 if (pool_is_free_index (pool, session_index))
441 return VNET_API_ERROR_INVALID_VALUE_2;
442
443 return 0;
444}
445
446static void
Florin Corase04c2992017-03-01 08:17:34 -0800447vl_api_session_enable_disable_t_handler (vl_api_session_enable_disable_t * mp)
448{
449 vl_api_session_enable_disable_reply_t *rmp;
450 vlib_main_t *vm = vlib_get_main ();
451 int rv = 0;
452
453 vnet_session_enable_disable (vm, mp->is_enable);
454 REPLY_MACRO (VL_API_SESSION_ENABLE_DISABLE_REPLY);
455}
456
457static void
Dave Barach68b0fb02017-02-28 15:15:56 -0500458vl_api_bind_uri_t_handler (vl_api_bind_uri_t * mp)
459{
460 vl_api_bind_uri_reply_t *rmp;
461 vnet_bind_args_t _a, *a = &_a;
462 char segment_name[128];
463 u32 segment_name_length;
464 int rv;
465
466 _Static_assert (sizeof (u64) * SESSION_OPTIONS_N_OPTIONS <=
467 sizeof (mp->options),
468 "Out of options, fix api message definition");
469
470 segment_name_length = ARRAY_LEN (segment_name);
471
472 memset (a, 0, sizeof (*a));
473
474 a->uri = (char *) mp->uri;
475 a->api_client_index = mp->client_index;
476 a->options = mp->options;
477 a->segment_name = segment_name;
478 a->segment_name_length = segment_name_length;
479 a->session_cb_vft = &uri_session_cb_vft;
480
481 a->options[SESSION_OPTIONS_SEGMENT_SIZE] = mp->initial_segment_size;
482 a->options[SESSION_OPTIONS_ACCEPT_COOKIE] = mp->accept_cookie;
483 rv = vnet_bind_uri (a);
484
485 /* *INDENT-OFF* */
486 REPLY_MACRO2 (VL_API_BIND_URI_REPLY, ({
487 rmp->retval = rv;
488 if (!rv)
489 {
490 rmp->segment_name_length = 0;
491 /* $$$$ policy? */
492 rmp->segment_size = mp->initial_segment_size;
493 if (segment_name_length)
494 {
495 memcpy (rmp->segment_name, segment_name, segment_name_length);
496 rmp->segment_name_length = segment_name_length;
497 }
498 rmp->server_event_queue_address = a->server_event_queue_address;
499 }
500 }));
501 /* *INDENT-ON* */
Dave Barach68b0fb02017-02-28 15:15:56 -0500502}
503
504static void
505vl_api_unbind_uri_t_handler (vl_api_unbind_uri_t * mp)
506{
507 vl_api_unbind_uri_reply_t *rmp;
508 int rv;
509
510 rv = vnet_unbind_uri ((char *) mp->uri, mp->client_index);
511
512 REPLY_MACRO (VL_API_UNBIND_URI_REPLY);
513}
514
515static void
516vl_api_connect_uri_t_handler (vl_api_connect_uri_t * mp)
517{
Florin Corase04c2992017-03-01 08:17:34 -0800518 vl_api_connect_uri_reply_t *rmp;
Dave Barach68b0fb02017-02-28 15:15:56 -0500519 vnet_connect_args_t _a, *a = &_a;
Florin Corase04c2992017-03-01 08:17:34 -0800520 int rv;
Dave Barach68b0fb02017-02-28 15:15:56 -0500521
522 a->uri = (char *) mp->uri;
523 a->api_client_index = mp->client_index;
524 a->api_context = mp->context;
525 a->options = mp->options;
526 a->session_cb_vft = &uri_session_cb_vft;
527 a->mp = mp;
Florin Corase04c2992017-03-01 08:17:34 -0800528
529 rv = vnet_connect_uri (a);
530
531 if (rv == 0 || rv == VNET_CONNECT_REDIRECTED)
532 return;
533
534 /* Got some error, relay it */
535
536 /* *INDENT-OFF* */
537 REPLY_MACRO2 (VL_API_CONNECT_URI_REPLY, ({
538 rmp->retval = rv;
539 }));
540 /* *INDENT-ON* */
Dave Barach68b0fb02017-02-28 15:15:56 -0500541}
542
543static void
544vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
545{
546 vl_api_disconnect_session_reply_t *rmp;
547 int rv;
548
549 rv = api_session_not_valid (mp->session_index, mp->session_thread_index);
550 if (!rv)
551 rv = vnet_disconnect_session (mp->client_index, mp->session_index,
552 mp->session_thread_index);
553
554 REPLY_MACRO (VL_API_DISCONNECT_SESSION_REPLY);
555}
556
557static void
558vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
559 mp)
560{
561 if (api_session_not_valid (mp->session_index, mp->session_thread_index))
562 {
563 clib_warning ("Invalid session!");
564 return;
565 }
566
567 /* Client objected to disconnecting the session, log and continue */
568 if (mp->retval)
569 {
570 clib_warning ("client retval %d", mp->retval);
571 return;
572 }
573
574 /* Disconnect has been confirmed. Confirm close to transport */
575 vnet_disconnect_session (mp->client_index, mp->session_index,
576 mp->session_thread_index);
577}
578
579static void
580vl_api_reset_session_reply_t_handler (vl_api_reset_session_reply_t * mp)
581{
582 stream_session_t *s;
583
584 if (api_session_not_valid (mp->session_index, mp->session_thread_index))
585 {
586 clib_warning ("Invalid session!");
587 return;
588 }
589
590 /* Client objected to resetting the session, log and continue */
591 if (mp->retval)
592 {
593 clib_warning ("client retval %d", mp->retval);
594 return;
595 }
596
597 s = stream_session_get (mp->session_index, mp->session_thread_index);
598
599 /* This comes as a response to a reset, transport only waiting for
600 * confirmation to remove connection state, no need to disconnect */
601 stream_session_cleanup (s);
602}
603
604static void
605vl_api_accept_session_reply_t_handler (vl_api_accept_session_reply_t * mp)
606{
607 stream_session_t *s;
608 int rv;
609
610 if (api_session_not_valid (mp->session_index, mp->session_thread_index))
611 return;
612
613 s = stream_session_get (mp->session_index, mp->session_thread_index);
614 rv = mp->retval;
615
616 if (rv)
617 {
618 /* Server isn't interested, kill the session */
619 stream_session_disconnect (s);
620 return;
621 }
622
623 s->session_state = SESSION_STATE_READY;
624}
625
626static void
627vl_api_map_another_segment_reply_t_handler (vl_api_map_another_segment_reply_t
628 * mp)
629{
630 clib_warning ("not implemented");
631}
632
633static void
634vl_api_bind_sock_t_handler (vl_api_bind_sock_t * mp)
635{
636 vl_api_bind_sock_reply_t *rmp;
637 vnet_bind_args_t _a, *a = &_a;
638 char segment_name[128];
639 u32 segment_name_length;
640 int rv;
641
642 STATIC_ASSERT (sizeof (u64) * SESSION_OPTIONS_N_OPTIONS <=
643 sizeof (mp->options),
644 "Out of options, fix api message definition");
645
646 segment_name_length = ARRAY_LEN (segment_name);
647
648 memset (a, 0, sizeof (*a));
649
650 clib_memcpy (&a->tep.ip, mp->ip,
651 (mp->is_ip4 ? sizeof (ip4_address_t) :
652 sizeof (ip6_address_t)));
653 a->tep.is_ip4 = mp->is_ip4;
654 a->tep.port = mp->port;
655 a->tep.vrf = mp->vrf;
656
657 a->api_client_index = mp->client_index;
658 a->options = mp->options;
659 a->segment_name = segment_name;
660 a->segment_name_length = segment_name_length;
661 a->session_cb_vft = &session_cb_vft;
662
663 rv = vnet_bind_uri (a);
664
665 /* *INDENT-OFF* */
666 REPLY_MACRO2 (VL_API_BIND_SOCK_REPLY, ({
667 rmp->retval = rv;
668 if (!rv)
669 {
670 rmp->segment_name_length = 0;
671 rmp->segment_size = mp->options[SESSION_OPTIONS_SEGMENT_SIZE];
672 if (segment_name_length)
673 {
674 memcpy(rmp->segment_name, segment_name, segment_name_length);
675 rmp->segment_name_length = segment_name_length;
676 }
677 rmp->server_event_queue_address = a->server_event_queue_address;
678 }
679 }));
680 /* *INDENT-ON* */
681}
682
683static void
684vl_api_unbind_sock_t_handler (vl_api_unbind_sock_t * mp)
685{
686 vl_api_unbind_sock_reply_t *rmp;
687 vnet_unbind_args_t _a, *a = &_a;
688 int rv;
689
690 a->api_client_index = mp->client_index;
691 a->handle = mp->handle;
692
693 rv = vnet_unbind (a);
694
695 REPLY_MACRO (VL_API_UNBIND_SOCK_REPLY);
696}
697
698static void
699vl_api_connect_sock_t_handler (vl_api_connect_sock_t * mp)
700{
Florin Corase04c2992017-03-01 08:17:34 -0800701 vl_api_connect_sock_reply_t *rmp;
Dave Barach68b0fb02017-02-28 15:15:56 -0500702 vnet_connect_args_t _a, *a = &_a;
Florin Corase04c2992017-03-01 08:17:34 -0800703 int rv;
Dave Barach68b0fb02017-02-28 15:15:56 -0500704
705 clib_memcpy (&a->tep.ip, mp->ip,
706 (mp->is_ip4 ? sizeof (ip4_address_t) :
707 sizeof (ip6_address_t)));
708 a->tep.is_ip4 = mp->is_ip4;
709 a->tep.port = mp->port;
710 a->tep.vrf = mp->vrf;
711 a->options = mp->options;
712 a->session_cb_vft = &session_cb_vft;
713 a->api_context = mp->context;
714 a->mp = mp;
715
Florin Corase04c2992017-03-01 08:17:34 -0800716 rv = vnet_connect (a);
717
718 if (rv == 0 || rv == VNET_CONNECT_REDIRECTED)
719 return;
720
721 /* Got some error, relay it */
722
723 /* *INDENT-OFF* */
724 REPLY_MACRO2 (VL_API_CONNECT_URI_REPLY, ({
725 rmp->retval = rv;
726 }));
727 /* *INDENT-ON* */
Dave Barach68b0fb02017-02-28 15:15:56 -0500728}
729
730static void
731vl_api_disconnect_sock_t_handler (vl_api_disconnect_sock_t * mp)
732{
733 vnet_disconnect_args_t _a, *a = &_a;
734 vl_api_disconnect_sock_reply_t *rmp;
735 int rv;
736
737 a->api_client_index = mp->client_index;
738 a->handle = mp->handle;
739 rv = vnet_disconnect (a);
740
741 REPLY_MACRO (VL_API_DISCONNECT_SOCK_REPLY);
742}
743
744static void
745vl_api_disconnect_sock_reply_t_handler (vl_api_disconnect_sock_reply_t * mp)
746{
747 vnet_disconnect_args_t _a, *a = &_a;
748
749 /* Client objected to disconnecting the session, log and continue */
750 if (mp->retval)
751 {
752 clib_warning ("client retval %d", mp->retval);
753 return;
754 }
755
756 a->api_client_index = mp->client_index;
757 a->handle = mp->handle;
758
759 vnet_disconnect (a);
760}
761
762static void
763vl_api_reset_sock_reply_t_handler (vl_api_reset_sock_reply_t * mp)
764{
765 stream_session_t *s;
766 u32 session_index, thread_index;
767
768 /* Client objected to resetting the session, log and continue */
769 if (mp->retval)
770 {
771 clib_warning ("client retval %d", mp->retval);
772 return;
773 }
774
775 if (api_parse_session_handle (mp->handle, &session_index, &thread_index))
776 {
777 clib_warning ("Invalid handle");
778 return;
779 }
780
781 s = stream_session_get (session_index, thread_index);
782
783 /* This comes as a response to a reset, transport only waiting for
784 * confirmation to remove connection state, no need to disconnect */
785 stream_session_cleanup (s);
786}
787
788static void
789vl_api_accept_sock_reply_t_handler (vl_api_accept_sock_reply_t * mp)
790{
791 stream_session_t *s;
792 u32 session_index, thread_index;
793
794 if (api_parse_session_handle (mp->handle, &session_index, &thread_index))
795 {
796 clib_warning ("Invalid handle");
797 return;
798 }
799 s = stream_session_get (session_index, thread_index);
800
801 if (mp->retval)
802 {
803 /* Server isn't interested, kill the session */
804 stream_session_disconnect (s);
805 return;
806 }
807
808 s->session_state = SESSION_STATE_READY;
809}
810
811#define vl_msg_name_crc_list
812#include <vnet/vnet_all_api_h.h>
813#undef vl_msg_name_crc_list
814
815static void
816setup_message_id_table (api_main_t * am)
817{
818#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
819 foreach_vl_msg_name_crc_session;
820#undef _
821}
822
823/*
824 * session_api_hookup
825 * Add uri's API message handlers to the table.
826 * vlib has alread mapped shared memory and
827 * added the client registration handlers.
828 * See .../open-repo/vlib/memclnt_vlib.c:memclnt_process()
829 */
830static clib_error_t *
831session_api_hookup (vlib_main_t * vm)
832{
833 api_main_t *am = &api_main;
834
835#define _(N,n) \
836 vl_msg_api_set_handlers(VL_API_##N, #n, \
837 vl_api_##n##_t_handler, \
838 vl_noop_handler, \
839 vl_api_##n##_t_endian, \
840 vl_api_##n##_t_print, \
841 sizeof(vl_api_##n##_t), 1);
842 foreach_session_api_msg;
843#undef _
844
845 /*
846 * Messages which bounce off the data-plane to
847 * an API client. Simply tells the message handling infra not
848 * to free the message.
849 *
850 * Bounced message handlers MUST NOT block the data plane
851 */
852 am->message_bounce[VL_API_CONNECT_URI] = 1;
853 am->message_bounce[VL_API_CONNECT_SOCK] = 1;
854
855 /*
856 * Set up the (msg_name, crc, message-id) table
857 */
858 setup_message_id_table (am);
859
860 return 0;
861}
862
863VLIB_API_INIT_FUNCTION (session_api_hookup);
864/*
865 * fd.io coding-style-patch-verification: ON
866 *
867 * Local Variables:
868 * eval: (c-set-style "gnu")
869 * End:
870 */