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