5d1373fb0eaabf36b1222946a99562475195d8fd
[vpp.git] / src / uri / vppcom.c
1 /*
2  * Copyright (c) 2017 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this
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 <stdio.h>
17 #include <stdlib.h>
18 #include <signal.h>
19 #include <svm/svm_fifo_segment.h>
20 #include <vlibmemory/api.h>
21 #include <vpp/api/vpe_msg_enum.h>
22 #include <vnet/session/application_interface.h>
23 #include <uri/vppcom.h>
24 #include <vlib/unix/unix.h>
25 #include <vppinfra/vec_bootstrap.h>
26
27 #define vl_typedefs             /* define message structures */
28 #include <vpp/api/vpe_all_api_h.h>
29 #undef vl_typedefs
30
31 /* declare message handlers for each api */
32
33 #define vl_endianfun            /* define message structures */
34 #include <vpp/api/vpe_all_api_h.h>
35 #undef vl_endianfun
36
37 /* instantiate all the print functions we know about */
38 #define vl_print(handle, ...)
39 #define vl_printfun
40 #include <vpp/api/vpe_all_api_h.h>
41 #undef vl_printfun
42
43 #if (CLIB_DEBUG > 0)
44 /* Set VPPCOM_DEBUG 2 for connection debug, 3 for read/write debug output */
45 #define VPPCOM_DEBUG 1
46 #else
47 #define VPPCOM_DEBUG 0
48 #endif
49
50 /*
51  * VPPCOM Private definitions and functions.
52  */
53 typedef enum
54 {
55   STATE_APP_START,
56   STATE_APP_CONN_VPP,
57   STATE_APP_ENABLED,
58   STATE_APP_ATTACHED,
59 } app_state_t;
60
61 typedef enum
62 {
63   STATE_START,
64   STATE_CONNECT,
65   STATE_LISTEN,
66   STATE_ACCEPT,
67   STATE_DISCONNECT,
68   STATE_FAILED
69 } session_state_t;
70
71 typedef struct epoll_event vppcom_epoll_event_t;
72
73 typedef struct
74 {
75   u32 next_sid;
76   u32 prev_sid;
77   u32 vep_idx;
78   vppcom_epoll_event_t ev;
79 #define VEP_DEFAULT_ET_MASK  (EPOLLIN|EPOLLOUT)
80   u32 et_mask;
81 } vppcom_epoll_t;
82
83 typedef struct
84 {
85   u8 is_ip4;
86   ip46_address_t ip46;
87 } vppcom_ip46_t;
88
89 typedef struct
90 {
91   volatile session_state_t state;
92
93   svm_fifo_t *server_rx_fifo;
94   svm_fifo_t *server_tx_fifo;
95   u32 sm_seg_index;
96   u64 vpp_session_handle;
97   unix_shared_memory_queue_t *vpp_event_queue;
98
99   /* Socket configuration state */
100   /* TBD: covert 'is_*' vars to bit in u8 flags; */
101   u8 is_server;
102   u8 is_listen;
103   u8 is_cut_thru;
104   u8 is_nonblocking;
105   u8 is_vep;
106   u8 is_vep_session;
107   u32 wait_cont_idx;
108   vppcom_epoll_t vep;
109   u32 vrf;
110   vppcom_ip46_t lcl_addr;
111   vppcom_ip46_t peer_addr;
112   u16 port;
113   u8 proto;
114   u64 client_queue_address;
115   u64 options[16];
116 } session_t;
117
118 typedef struct vppcom_cfg_t_
119 {
120   u64 heapsize;
121   u64 segment_baseva;
122   u32 segment_size;
123   u32 add_segment_size;
124   u32 preallocated_fifo_pairs;
125   u32 rx_fifo_size;
126   u32 tx_fifo_size;
127   u32 event_queue_size;
128   u32 listen_queue_size;
129   f64 app_timeout;
130   f64 session_timeout;
131   f64 accept_timeout;
132 } vppcom_cfg_t;
133
134 typedef struct vppcom_main_t_
135 {
136   u8 init;
137   u32 *client_session_index_fifo;
138   volatile u32 bind_session_index;
139   int main_cpu;
140
141   /* vpe input queue */
142   unix_shared_memory_queue_t *vl_input_queue;
143
144   /* API client handle */
145   u32 my_client_index;
146
147   /* Session pool */
148   clib_spinlock_t sessions_lockp;
149   session_t *sessions;
150
151   /* Hash table for disconnect processing */
152   uword *session_index_by_vpp_handles;
153
154   /* Select bitmaps */
155   clib_bitmap_t *rd_bitmap;
156   clib_bitmap_t *wr_bitmap;
157   clib_bitmap_t *ex_bitmap;
158
159   /* Our event queue */
160   unix_shared_memory_queue_t *app_event_queue;
161
162   /* unique segment name counter */
163   u32 unique_segment_index;
164
165   pid_t my_pid;
166
167   /* For deadman timers */
168   clib_time_t clib_time;
169
170   /* State of the connection, shared between msg RX thread and main thread */
171   volatile app_state_t app_state;
172
173   vppcom_cfg_t cfg;
174
175   /* VNET_API_ERROR_FOO -> "Foo" hash table */
176   uword *error_string_by_error_number;
177 } vppcom_main_t;
178
179 vppcom_main_t vppcom_main = {.my_client_index = ~0 };
180
181 static const char *
182 vppcom_app_state_str (app_state_t state)
183 {
184   char *st;
185
186   switch (state)
187     {
188     case STATE_APP_START:
189       st = "STATE_APP_START";
190       break;
191
192     case STATE_APP_CONN_VPP:
193       st = "STATE_APP_CONN_VPP";
194       break;
195
196     case STATE_APP_ENABLED:
197       st = "STATE_APP_ENABLED";
198       break;
199
200     case STATE_APP_ATTACHED:
201       st = "STATE_APP_ATTACHED";
202       break;
203
204     default:
205       st = "UNKNOWN_APP_STATE";
206       break;
207     }
208
209   return st;
210 }
211
212 static const char *
213 vppcom_session_state_str (session_state_t state)
214 {
215   char *st;
216
217   switch (state)
218     {
219     case STATE_START:
220       st = "STATE_START";
221       break;
222
223     case STATE_CONNECT:
224       st = "STATE_CONNECT";
225       break;
226
227     case STATE_LISTEN:
228       st = "STATE_LISTEN";
229       break;
230
231     case STATE_ACCEPT:
232       st = "STATE_ACCEPT";
233       break;
234
235     case STATE_DISCONNECT:
236       st = "STATE_DISCONNECT";
237       break;
238
239     case STATE_FAILED:
240       st = "STATE_FAILED";
241       break;
242
243     default:
244       st = "UNKNOWN_STATE";
245       break;
246     }
247
248   return st;
249 }
250
251 /*
252  * VPPCOM Utility Functions
253  */
254 static inline int
255 vppcom_session_at_index (u32 session_index, session_t * volatile *sess)
256 {
257   vppcom_main_t *vcm = &vppcom_main;
258
259   /* Assumes that caller has acquired spinlock: vcm->sessions_lockp */
260   if (PREDICT_FALSE ((session_index == ~0) ||
261                      pool_is_free_index (vcm->sessions, session_index)))
262     {
263       clib_warning ("[%d] invalid session, sid (%u) has been closed!",
264                     vcm->my_pid, session_index);
265       return VPPCOM_EBADFD;
266     }
267   *sess = pool_elt_at_index (vcm->sessions, session_index);
268   return VPPCOM_OK;
269 }
270
271 static int
272 vppcom_connect_to_vpp (char *app_name)
273 {
274   api_main_t *am = &api_main;
275   vppcom_main_t *vcm = &vppcom_main;
276
277   if (VPPCOM_DEBUG > 0)
278     printf ("\nConnecting to VPP api...");
279   if (vl_client_connect_to_vlib ("/vpe-api", app_name, 32) < 0)
280     {
281       clib_warning ("[%d] connect to vpp (%s) failed!",
282                     vcm->my_pid, app_name);
283       return VPPCOM_ECONNREFUSED;
284     }
285
286   vcm->vl_input_queue = am->shmem_hdr->vl_input_queue;
287   vcm->my_client_index = am->my_client_index;
288   if (VPPCOM_DEBUG > 0)
289     printf (" connected!\n");
290
291   vcm->app_state = STATE_APP_CONN_VPP;
292   return VPPCOM_OK;
293 }
294
295 static u8 *
296 format_api_error (u8 * s, va_list * args)
297 {
298   vppcom_main_t *vcm = &vppcom_main;
299   i32 error = va_arg (*args, u32);
300   uword *p;
301
302   p = hash_get (vcm->error_string_by_error_number, -error);
303
304   if (p)
305     s = format (s, "%s (%d)", p[0], error);
306   else
307     s = format (s, "%d", error);
308   return s;
309 }
310
311 static void
312 vppcom_init_error_string_table (void)
313 {
314   vppcom_main_t *vcm = &vppcom_main;
315
316   vcm->error_string_by_error_number = hash_create (0, sizeof (uword));
317
318 #define _(n,v,s) hash_set (vcm->error_string_by_error_number, -v, s);
319   foreach_vnet_api_error;
320 #undef _
321
322   hash_set (vcm->error_string_by_error_number, 99, "Misc");
323 }
324
325 static inline int
326 vppcom_wait_for_app_state_change (app_state_t app_state)
327 {
328   vppcom_main_t *vcm = &vppcom_main;
329   f64 timeout = clib_time_now (&vcm->clib_time) + vcm->cfg.app_timeout;
330
331   while (clib_time_now (&vcm->clib_time) < timeout)
332     {
333       if (vcm->app_state == app_state)
334         return VPPCOM_OK;
335     }
336   if (VPPCOM_DEBUG > 0)
337     clib_warning ("[%d] timeout waiting for state %s (%d)", vcm->my_pid,
338                   vppcom_app_state_str (app_state), app_state);
339   return VPPCOM_ETIMEDOUT;
340 }
341
342 static inline int
343 vppcom_wait_for_session_state_change (u32 session_index,
344                                       session_state_t state,
345                                       f64 wait_for_time)
346 {
347   vppcom_main_t *vcm = &vppcom_main;
348   f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
349   session_t *volatile session;
350   int rv;
351
352   do
353     {
354       clib_spinlock_lock (&vcm->sessions_lockp);
355       rv = vppcom_session_at_index (session_index, &session);
356       if (PREDICT_FALSE (rv))
357         {
358           clib_spinlock_unlock (&vcm->sessions_lockp);
359           return rv;
360         }
361       if (session->state == state)
362         {
363           clib_spinlock_unlock (&vcm->sessions_lockp);
364           return VPPCOM_OK;
365         }
366       clib_spinlock_unlock (&vcm->sessions_lockp);
367     }
368   while (clib_time_now (&vcm->clib_time) < timeout);
369
370   if (VPPCOM_DEBUG > 0)
371     clib_warning ("[%d] timeout waiting for state %s (%d)", vcm->my_pid,
372                   vppcom_session_state_str (state), state);
373   return VPPCOM_ETIMEDOUT;
374 }
375
376 static inline int
377 vppcom_wait_for_client_session_index (f64 wait_for_time)
378 {
379   vppcom_main_t *vcm = &vppcom_main;
380   f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
381
382   do
383     {
384       if (clib_fifo_elts (vcm->client_session_index_fifo))
385         return VPPCOM_OK;
386     }
387   while (clib_time_now (&vcm->clib_time) < timeout);
388
389   if (wait_for_time == 0)
390     return VPPCOM_EAGAIN;
391
392   if (VPPCOM_DEBUG > 0)
393     clib_warning ("[%d] timeout waiting for client_session_index",
394                   vcm->my_pid);
395   return VPPCOM_ETIMEDOUT;
396 }
397
398 /*
399  * VPP-API message functions
400  */
401 static void
402 vppcom_send_session_enable_disable (u8 is_enable)
403 {
404   vppcom_main_t *vcm = &vppcom_main;
405   vl_api_session_enable_disable_t *bmp;
406   bmp = vl_msg_api_alloc (sizeof (*bmp));
407   memset (bmp, 0, sizeof (*bmp));
408
409   bmp->_vl_msg_id = ntohs (VL_API_SESSION_ENABLE_DISABLE);
410   bmp->client_index = vcm->my_client_index;
411   bmp->context = htonl (0xfeedface);
412   bmp->is_enable = is_enable;
413   vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
414 }
415
416 static int
417 vppcom_app_session_enable (void)
418 {
419   vppcom_main_t *vcm = &vppcom_main;
420   int rv;
421
422   if (vcm->app_state != STATE_APP_ENABLED)
423     {
424       vppcom_send_session_enable_disable (1 /* is_enabled == TRUE */ );
425       rv = vppcom_wait_for_app_state_change (STATE_APP_ENABLED);
426       if (PREDICT_FALSE (rv))
427         {
428           if (VPPCOM_DEBUG > 0)
429             clib_warning ("[%d] Session enable timed out, rv = %s (%d)",
430                           vcm->my_pid, vppcom_retval_str (rv), rv);
431           return rv;
432         }
433     }
434   return VPPCOM_OK;
435 }
436
437 static void
438   vl_api_session_enable_disable_reply_t_handler
439   (vl_api_session_enable_disable_reply_t * mp)
440 {
441   vppcom_main_t *vcm = &vppcom_main;
442
443   if (mp->retval)
444     {
445       clib_warning ("[%d] session_enable_disable failed: %U", vcm->my_pid,
446                     format_api_error, ntohl (mp->retval));
447     }
448   else
449     vcm->app_state = STATE_APP_ENABLED;
450 }
451
452 static void
453 vppcom_app_send_attach (void)
454 {
455   vppcom_main_t *vcm = &vppcom_main;
456   vl_api_application_attach_t *bmp;
457   bmp = vl_msg_api_alloc (sizeof (*bmp));
458   memset (bmp, 0, sizeof (*bmp));
459
460   bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_ATTACH);
461   bmp->client_index = vcm->my_client_index;
462   bmp->context = htonl (0xfeedface);
463   bmp->options[APP_OPTIONS_FLAGS] =
464     APP_OPTIONS_FLAGS_ACCEPT_REDIRECT | APP_OPTIONS_FLAGS_ADD_SEGMENT |
465     APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE | APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
466   bmp->options[SESSION_OPTIONS_SEGMENT_SIZE] = vcm->cfg.segment_size;
467   bmp->options[SESSION_OPTIONS_ADD_SEGMENT_SIZE] = vcm->cfg.add_segment_size;
468   bmp->options[SESSION_OPTIONS_RX_FIFO_SIZE] = vcm->cfg.rx_fifo_size;
469   bmp->options[SESSION_OPTIONS_TX_FIFO_SIZE] = vcm->cfg.tx_fifo_size;
470   vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
471 }
472
473 static int
474 vppcom_app_attach (void)
475 {
476   vppcom_main_t *vcm = &vppcom_main;
477   int rv;
478
479   vppcom_app_send_attach ();
480   rv = vppcom_wait_for_app_state_change (STATE_APP_ATTACHED);
481   if (PREDICT_FALSE (rv))
482     {
483       if (VPPCOM_DEBUG > 0)
484         clib_warning ("[%d] application attach timed out, rv = %s (%d)",
485                       vcm->my_pid, vppcom_retval_str (rv), rv);
486       return rv;
487     }
488   return VPPCOM_OK;
489 }
490
491 static void
492 vppcom_app_detach (void)
493 {
494   vppcom_main_t *vcm = &vppcom_main;
495   vl_api_application_detach_t *bmp;
496   bmp = vl_msg_api_alloc (sizeof (*bmp));
497   memset (bmp, 0, sizeof (*bmp));
498
499   bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH);
500   bmp->client_index = vcm->my_client_index;
501   bmp->context = htonl (0xfeedface);
502   vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
503 }
504
505 static void
506 vl_api_application_attach_reply_t_handler (vl_api_application_attach_reply_t *
507                                            mp)
508 {
509   vppcom_main_t *vcm = &vppcom_main;
510   static svm_fifo_segment_create_args_t _a;
511   svm_fifo_segment_create_args_t *a = &_a;
512   int rv;
513
514   memset (a, 0, sizeof (*a));
515   if (mp->retval)
516     {
517       clib_warning ("[%d] attach failed: %U", vcm->my_pid,
518                     format_api_error, ntohl (mp->retval));
519       return;
520     }
521
522   if (mp->segment_name_length == 0)
523     {
524       clib_warning ("[%d] segment_name_length zero", vcm->my_pid);
525       return;
526     }
527
528   a->segment_name = (char *) mp->segment_name;
529   a->segment_size = mp->segment_size;
530
531   ASSERT (mp->app_event_queue_address);
532
533   /* Attach to the segment vpp created */
534   rv = svm_fifo_segment_attach (a);
535   vec_reset_length (a->new_segment_indices);
536   if (PREDICT_FALSE (rv))
537     {
538       clib_warning ("[%d] svm_fifo_segment_attach ('%s') failed", vcm->my_pid,
539                     mp->segment_name);
540       return;
541     }
542
543   vcm->app_event_queue =
544     uword_to_pointer (mp->app_event_queue_address,
545                       unix_shared_memory_queue_t *);
546
547   vcm->app_state = STATE_APP_ATTACHED;
548 }
549
550 static void
551 vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t *
552                                            mp)
553 {
554   vppcom_main_t *vcm = &vppcom_main;
555
556   if (mp->retval)
557     clib_warning ("[%d] detach failed: %U", vcm->my_pid, format_api_error,
558                   ntohl (mp->retval));
559
560   vcm->app_state = STATE_APP_ENABLED;
561 }
562
563 static void
564 vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
565                                            mp)
566 {
567   vppcom_main_t *vcm = &vppcom_main;
568   uword *p;
569
570   p = hash_get (vcm->session_index_by_vpp_handles, mp->handle);
571   if (p)
572     {
573       session_t *session = 0;
574       int rv;
575       clib_spinlock_lock (&vcm->sessions_lockp);
576       rv = vppcom_session_at_index (p[0], &session);
577       if (PREDICT_FALSE (rv))
578         {
579           if (VPPCOM_DEBUG > 1)
580             clib_warning ("[%d] invalid session, sid (%u) has been closed!",
581                           vcm->my_pid, p[0]);
582         }
583       hash_unset (vcm->session_index_by_vpp_handles, mp->handle);
584       session->state = STATE_DISCONNECT;
585       clib_spinlock_unlock (&vcm->sessions_lockp);
586     }
587   else
588     {
589       if (VPPCOM_DEBUG > 1)
590         clib_warning ("[%d] couldn't find session key %llx", vcm->my_pid,
591                       mp->handle);
592     }
593
594   if (mp->retval)
595     clib_warning ("[%d] disconnect_session failed: %U", vcm->my_pid,
596                   format_api_error, ntohl (mp->retval));
597 }
598
599 static void
600 vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp)
601 {
602   vppcom_main_t *vcm = &vppcom_main;
603   static svm_fifo_segment_create_args_t _a;
604   svm_fifo_segment_create_args_t *a = &_a;
605   int rv;
606
607   memset (a, 0, sizeof (*a));
608   a->segment_name = (char *) mp->segment_name;
609   a->segment_size = mp->segment_size;
610   /* Attach to the segment vpp created */
611   rv = svm_fifo_segment_attach (a);
612   vec_reset_length (a->new_segment_indices);
613   if (PREDICT_FALSE (rv))
614     {
615       clib_warning ("[%d] svm_fifo_segment_attach ('%s') failed",
616                     vcm->my_pid, mp->segment_name);
617       return;
618     }
619   if (VPPCOM_DEBUG > 1)
620     clib_warning ("[%d] mapped new segment '%s' size %d", vcm->my_pid,
621                   mp->segment_name, mp->segment_size);
622 }
623
624 static void
625 vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
626 {
627   vppcom_main_t *vcm = &vppcom_main;
628   session_t *session = 0;
629   vl_api_disconnect_session_reply_t *rmp;
630   uword *p;
631   int rv = 0;
632
633   p = hash_get (vcm->session_index_by_vpp_handles, mp->handle);
634   if (p)
635     {
636       int rval;
637       clib_spinlock_lock (&vcm->sessions_lockp);
638       rval = vppcom_session_at_index (p[0], &session);
639       if (PREDICT_FALSE (rval))
640         {
641           if (VPPCOM_DEBUG > 1)
642             clib_warning ("[%d] invalid session, sid (%u) has been closed!",
643                           vcm->my_pid, p[0]);
644         }
645       else
646         pool_put (vcm->sessions, session);
647       clib_spinlock_unlock (&vcm->sessions_lockp);
648       hash_unset (vcm->session_index_by_vpp_handles, mp->handle);
649     }
650   else
651     {
652       clib_warning ("[%d] couldn't find session key %llx", vcm->my_pid,
653                     mp->handle);
654       rv = -11;
655     }
656
657   rmp = vl_msg_api_alloc (sizeof (*rmp));
658   memset (rmp, 0, sizeof (*rmp));
659
660   rmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION_REPLY);
661   rmp->retval = htonl (rv);
662   rmp->handle = mp->handle;
663   vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
664 }
665
666 static void
667 vl_api_reset_session_t_handler (vl_api_reset_session_t * mp)
668 {
669   vppcom_main_t *vcm = &vppcom_main;
670   session_t *session = 0;
671   vl_api_reset_session_reply_t *rmp;
672   uword *p;
673   int rv = 0;
674
675   p = hash_get (vcm->session_index_by_vpp_handles, mp->handle);
676   if (p)
677     {
678       int rval;
679       clib_spinlock_lock (&vcm->sessions_lockp);
680       rval = vppcom_session_at_index (p[0], &session);
681       if (PREDICT_FALSE (rval))
682         {
683           if (VPPCOM_DEBUG > 1)
684             clib_warning ("[%d] invalid session, sid (%u) has been closed!",
685                           vcm->my_pid, p[0]);
686         }
687       else
688         pool_put (vcm->sessions, session);
689       clib_spinlock_unlock (&vcm->sessions_lockp);
690       hash_unset (vcm->session_index_by_vpp_handles, mp->handle);
691     }
692   else
693     {
694       clib_warning ("[%d] couldn't find session key %llx", vcm->my_pid,
695                     mp->handle);
696       rv = -11;
697     }
698
699   rmp = vl_msg_api_alloc (sizeof (*rmp));
700   memset (rmp, 0, sizeof (*rmp));
701   rmp->_vl_msg_id = ntohs (VL_API_RESET_SESSION_REPLY);
702   rmp->retval = htonl (rv);
703   rmp->handle = mp->handle;
704   vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
705 }
706
707 static void
708 vl_api_connect_session_reply_t_handler (vl_api_connect_session_reply_t * mp)
709 {
710   vppcom_main_t *vcm = &vppcom_main;
711   session_t *session;
712   u32 session_index;
713   svm_fifo_t *rx_fifo, *tx_fifo;
714   u8 is_cut_thru = 0;
715   int rv;
716
717   if (mp->retval)
718     {
719       clib_warning ("[%d] connect failed: %U", vcm->my_pid, format_api_error,
720                     ntohl (mp->retval));
721       return;
722     }
723
724   session_index = mp->context;
725   if (VPPCOM_DEBUG > 1)
726     clib_warning ("[%d] session_index = %d 0x%08x", vcm->my_pid,
727                   session_index, session_index);
728
729   clib_spinlock_lock (&vcm->sessions_lockp);
730   if (pool_is_free_index (vcm->sessions, session_index))
731     {
732       clib_spinlock_unlock (&vcm->sessions_lockp);
733       if (VPPCOM_DEBUG > 1)
734         clib_warning ("[%d] invalid session, sid %d is closed!",
735                       vcm->my_pid, session_index);
736       return;
737     }
738
739   /* We've been redirected */
740   if (mp->segment_name_length > 0)
741     {
742       static svm_fifo_segment_create_args_t _a;
743       svm_fifo_segment_create_args_t *a = &_a;
744
745       is_cut_thru = 1;
746       memset (a, 0, sizeof (*a));
747       a->segment_name = (char *) mp->segment_name;
748       if (VPPCOM_DEBUG > 1)
749         clib_warning ("[%d] cut-thru segment: %s", vcm->my_pid,
750                       a->segment_name);
751       rv = svm_fifo_segment_attach (a);
752       vec_reset_length (a->new_segment_indices);
753       if (PREDICT_FALSE (rv))
754         {
755           clib_spinlock_unlock (&vcm->sessions_lockp);
756           clib_warning ("[%d] sm_fifo_segment_attach ('%s') failed",
757                         vcm->my_pid, a->segment_name);
758           return;
759         }
760     }
761
762   /*
763    * Setup session
764    */
765   if (VPPCOM_DEBUG > 1)
766     clib_warning ("[%d] client sid %d", vcm->my_pid, session_index);
767
768   session = pool_elt_at_index (vcm->sessions, session_index);
769   session->is_cut_thru = is_cut_thru;
770   session->vpp_event_queue = uword_to_pointer (mp->vpp_event_queue_address,
771                                                unix_shared_memory_queue_t *);
772
773   rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
774   rx_fifo->client_session_index = session_index;
775   tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
776   tx_fifo->client_session_index = session_index;
777
778   session->server_rx_fifo = rx_fifo;
779   session->server_tx_fifo = tx_fifo;
780   session->vpp_session_handle = mp->handle;
781   session->state = STATE_CONNECT;
782
783   /* Add it to lookup table */
784   hash_set (vcm->session_index_by_vpp_handles, mp->handle, session_index);
785   clib_spinlock_unlock (&vcm->sessions_lockp);
786 }
787
788 static void
789 vppcom_send_connect_sock (session_t * session, u32 session_index)
790 {
791   vppcom_main_t *vcm = &vppcom_main;
792   vl_api_connect_sock_t *cmp;
793
794   /* Assumes caller as acquired the spinlock: vcm->sessions_lockp */
795   session->is_server = 0;
796   cmp = vl_msg_api_alloc (sizeof (*cmp));
797   memset (cmp, 0, sizeof (*cmp));
798   cmp->_vl_msg_id = ntohs (VL_API_CONNECT_SOCK);
799   cmp->client_index = vcm->my_client_index;
800   cmp->context = session_index;
801
802   if (VPPCOM_DEBUG > 1)
803     clib_warning ("[%d] session_index = %d 0x%08x",
804                   vcm->my_pid, session_index, session_index);
805
806   cmp->vrf = session->vrf;
807   cmp->is_ip4 = session->peer_addr.is_ip4;
808   clib_memcpy (cmp->ip, &session->peer_addr.ip46, sizeof (cmp->ip));
809   cmp->port = session->port;
810   cmp->proto = session->proto;
811   clib_memcpy (cmp->options, session->options, sizeof (cmp->options));
812   vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & cmp);
813 }
814
815 static int
816 vppcom_send_disconnect (u32 session_index)
817 {
818   vppcom_main_t *vcm = &vppcom_main;
819   vl_api_disconnect_session_t *dmp;
820   session_t *session = 0;
821   int rv;
822
823   clib_spinlock_lock (&vcm->sessions_lockp);
824   rv = vppcom_session_at_index (session_index, &session);
825   if (PREDICT_FALSE (rv))
826     {
827       clib_spinlock_unlock (&vcm->sessions_lockp);
828       if (VPPCOM_DEBUG > 1)
829         clib_warning ("[%d] invalid session, sid (%u) has been closed!",
830                       vcm->my_pid, session_index);
831       return rv;
832     }
833
834   dmp = vl_msg_api_alloc (sizeof (*dmp));
835   memset (dmp, 0, sizeof (*dmp));
836   dmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION);
837   dmp->client_index = vcm->my_client_index;
838   dmp->handle = session->vpp_session_handle;
839   clib_spinlock_unlock (&vcm->sessions_lockp);
840   vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & dmp);
841   return VPPCOM_OK;
842 }
843
844 static void
845 vl_api_bind_sock_reply_t_handler (vl_api_bind_sock_reply_t * mp)
846 {
847   vppcom_main_t *vcm = &vppcom_main;
848   session_t *session = 0;
849   int rv;
850
851   if (mp->retval)
852     clib_warning ("[%d] bind failed: %U", vcm->my_pid, format_api_error,
853                   ntohl (mp->retval));
854
855   ASSERT (vcm->bind_session_index != ~0);
856
857   clib_spinlock_lock (&vcm->sessions_lockp);
858   rv = vppcom_session_at_index (vcm->bind_session_index, &session);
859   if (rv == VPPCOM_OK)
860     {
861       session->vpp_session_handle = mp->handle;
862       hash_set (vcm->session_index_by_vpp_handles, mp->handle,
863                 vcm->bind_session_index);
864       session->state = mp->retval ? STATE_FAILED : STATE_LISTEN;
865       vcm->bind_session_index = ~0;
866     }
867   clib_spinlock_unlock (&vcm->sessions_lockp);
868 }
869
870 static void
871 vl_api_unbind_sock_reply_t_handler (vl_api_unbind_sock_reply_t * mp)
872 {
873   vppcom_main_t *vcm = &vppcom_main;
874   session_t *session = 0;
875   int rv;
876
877   clib_spinlock_lock (&vcm->sessions_lockp);
878   rv = vppcom_session_at_index (vcm->bind_session_index, &session);
879   if (rv == VPPCOM_OK)
880     {
881       if ((VPPCOM_DEBUG > 1) && (mp->retval))
882         clib_warning ("[%d] unbind failed: %U", vcm->my_pid, format_api_error,
883                       ntohl (mp->retval));
884
885       vcm->bind_session_index = ~0;
886       session->state = STATE_START;
887     }
888   clib_spinlock_unlock (&vcm->sessions_lockp);
889 }
890
891 u8 *
892 format_ip4_address (u8 * s, va_list * args)
893 {
894   u8 *a = va_arg (*args, u8 *);
895   return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
896 }
897
898 u8 *
899 format_ip6_address (u8 * s, va_list * args)
900 {
901   ip6_address_t *a = va_arg (*args, ip6_address_t *);
902   u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
903
904   i_max_n_zero = ARRAY_LEN (a->as_u16);
905   max_n_zeros = 0;
906   i_first_zero = i_max_n_zero;
907   n_zeros = 0;
908   for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
909     {
910       u32 is_zero = a->as_u16[i] == 0;
911       if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
912         {
913           i_first_zero = i;
914           n_zeros = 0;
915         }
916       n_zeros += is_zero;
917       if ((!is_zero && n_zeros > max_n_zeros)
918           || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
919         {
920           i_max_n_zero = i_first_zero;
921           max_n_zeros = n_zeros;
922           i_first_zero = ARRAY_LEN (a->as_u16);
923           n_zeros = 0;
924         }
925     }
926
927   last_double_colon = 0;
928   for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
929     {
930       if (i == i_max_n_zero && max_n_zeros > 1)
931         {
932           s = format (s, "::");
933           i += max_n_zeros - 1;
934           last_double_colon = 1;
935         }
936       else
937         {
938           s = format (s, "%s%x",
939                       (last_double_colon || i == 0) ? "" : ":",
940                       clib_net_to_host_u16 (a->as_u16[i]));
941           last_double_colon = 0;
942         }
943     }
944
945   return s;
946 }
947
948 /* Format an IP46 address. */
949 u8 *
950 format_ip46_address (u8 * s, va_list * args)
951 {
952   ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
953   ip46_type_t type = va_arg (*args, ip46_type_t);
954   int is_ip4 = 1;
955
956   switch (type)
957     {
958     case IP46_TYPE_ANY:
959       is_ip4 = ip46_address_is_ip4 (ip46);
960       break;
961     case IP46_TYPE_IP4:
962       is_ip4 = 1;
963       break;
964     case IP46_TYPE_IP6:
965       is_ip4 = 0;
966       break;
967     }
968
969   return is_ip4 ?
970     format (s, "%U", format_ip4_address, &ip46->ip4) :
971     format (s, "%U", format_ip6_address, &ip46->ip6);
972 }
973
974 static void
975 vl_api_accept_session_t_handler (vl_api_accept_session_t * mp)
976 {
977   vppcom_main_t *vcm = &vppcom_main;
978   vl_api_accept_session_reply_t *rmp;
979   svm_fifo_t *rx_fifo, *tx_fifo;
980   session_t *session;
981   u32 session_index;
982   int rv = 0;
983
984   if (!clib_fifo_free_elts (vcm->client_session_index_fifo))
985     {
986       clib_warning ("[%d] client session queue is full!", vcm->my_pid);
987       rv = VNET_API_ERROR_QUEUE_FULL;
988       goto send_reply;
989     }
990
991   if (VPPCOM_DEBUG > 1)
992     {
993       u8 *ip_str = format (0, "%U", format_ip46_address, &mp->ip, mp->is_ip4);
994       clib_warning ("[%d] accepted session from: %s:%d", vcm->my_pid, ip_str,
995                     clib_net_to_host_u16 (mp->port));
996       vec_free (ip_str);
997     }
998
999   clib_spinlock_lock (&vcm->sessions_lockp);
1000   /* Allocate local session and set it up */
1001   pool_get (vcm->sessions, session);
1002   memset (session, 0, sizeof (*session));
1003   session_index = session - vcm->sessions;
1004
1005   rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
1006   rx_fifo->client_session_index = session_index;
1007   tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
1008   tx_fifo->client_session_index = session_index;
1009
1010   session->server_rx_fifo = rx_fifo;
1011   session->server_tx_fifo = tx_fifo;
1012   session->vpp_event_queue = uword_to_pointer (mp->vpp_event_queue_address,
1013                                                unix_shared_memory_queue_t *);
1014   session->state = STATE_ACCEPT;
1015   session->is_cut_thru = 0;
1016   session->is_server = 1;
1017   session->port = ntohs (mp->port);
1018   session->peer_addr.is_ip4 = mp->is_ip4;
1019   clib_memcpy (&session->peer_addr.ip46, mp->ip,
1020                sizeof (session->peer_addr.ip46));
1021
1022   /* Add it to lookup table */
1023   hash_set (vcm->session_index_by_vpp_handles, mp->handle, session_index);
1024
1025   clib_fifo_add1 (vcm->client_session_index_fifo, session_index);
1026   clib_spinlock_unlock (&vcm->sessions_lockp);
1027
1028   /*
1029    * Send accept reply to vpp
1030    */
1031 send_reply:
1032   rmp = vl_msg_api_alloc (sizeof (*rmp));
1033   memset (rmp, 0, sizeof (*rmp));
1034   rmp->_vl_msg_id = ntohs (VL_API_ACCEPT_SESSION_REPLY);
1035   rmp->retval = htonl (rv);
1036   rmp->handle = mp->handle;
1037   vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
1038 }
1039
1040 /*
1041  * Acting as server for redirected connect requests
1042  */
1043 static void
1044 vl_api_connect_sock_t_handler (vl_api_connect_sock_t * mp)
1045 {
1046   static svm_fifo_segment_create_args_t _a;
1047   svm_fifo_segment_create_args_t *a = &_a;
1048   vppcom_main_t *vcm = &vppcom_main;
1049   u32 session_index;
1050   svm_fifo_segment_private_t *seg;
1051   unix_shared_memory_queue_t *client_q;
1052   vl_api_connect_session_reply_t *rmp;
1053   session_t *session = 0;
1054   int rv = 0;
1055   svm_fifo_t *rx_fifo;
1056   svm_fifo_t *tx_fifo;
1057   unix_shared_memory_queue_t *event_q = 0;
1058
1059   clib_spinlock_lock (&vcm->sessions_lockp);
1060   if (!clib_fifo_free_elts (vcm->client_session_index_fifo))
1061     {
1062       if (VPPCOM_DEBUG > 1)
1063         clib_warning ("[%d] client session queue is full!", vcm->my_pid);
1064       rv = VNET_API_ERROR_QUEUE_FULL;
1065       clib_spinlock_unlock (&vcm->sessions_lockp);
1066       goto send_reply;
1067     }
1068
1069   /* Create the segment */
1070   memset (a, 0, sizeof (*a));
1071   a->segment_name = (char *) format ((u8 *) a->segment_name, "%d:segment%d%c",
1072                                      vcm->my_pid, vcm->unique_segment_index++,
1073                                      0);
1074   a->segment_size = vcm->cfg.segment_size;
1075   a->preallocated_fifo_pairs = vcm->cfg.preallocated_fifo_pairs;
1076   a->rx_fifo_size = vcm->cfg.rx_fifo_size;
1077   a->tx_fifo_size = vcm->cfg.tx_fifo_size;
1078
1079   rv = svm_fifo_segment_create (a);
1080   if (PREDICT_FALSE (rv))
1081     {
1082       if (VPPCOM_DEBUG > 1)
1083         clib_warning ("[%d] svm_fifo_segment_create ('%s') failed",
1084                       vcm->my_pid, a->segment_name);
1085       vec_reset_length (a->new_segment_indices);
1086       rv = VNET_API_ERROR_URI_FIFO_CREATE_FAILED;
1087       goto send_reply;
1088     }
1089
1090   if (VPPCOM_DEBUG > 1)
1091     clib_warning ("[%d] created segment '%s'", vcm->my_pid, a->segment_name);
1092
1093   pool_get (vcm->sessions, session);
1094   memset (session, 0, sizeof (*session));
1095   session_index = session - vcm->sessions;
1096
1097   session->sm_seg_index = a->new_segment_indices[0];
1098   vec_reset_length (a->new_segment_indices);
1099
1100   seg = svm_fifo_segment_get_segment (session->sm_seg_index);
1101   rx_fifo = session->server_rx_fifo =
1102     svm_fifo_segment_alloc_fifo (seg, vcm->cfg.rx_fifo_size,
1103                                  FIFO_SEGMENT_RX_FREELIST);
1104   if (PREDICT_FALSE (!session->server_rx_fifo))
1105     {
1106       svm_fifo_segment_delete (seg);
1107       clib_warning ("[%d] rx fifo alloc failed, size %ld (0x%lx)",
1108                     vcm->my_pid, vcm->cfg.rx_fifo_size,
1109                     vcm->cfg.rx_fifo_size);
1110       rv = VNET_API_ERROR_URI_FIFO_CREATE_FAILED;
1111       clib_spinlock_unlock (&vcm->sessions_lockp);
1112       goto send_reply;
1113     }
1114
1115   tx_fifo = session->server_tx_fifo =
1116     svm_fifo_segment_alloc_fifo (seg, vcm->cfg.tx_fifo_size,
1117                                  FIFO_SEGMENT_TX_FREELIST);
1118   if (PREDICT_FALSE (!session->server_tx_fifo))
1119     {
1120       svm_fifo_segment_delete (seg);
1121       if (VPPCOM_DEBUG > 1)
1122         clib_warning ("[%d] tx fifo alloc failed, size %ld (0x%lx)",
1123                       vcm->my_pid, vcm->cfg.tx_fifo_size,
1124                       vcm->cfg.tx_fifo_size);
1125       rv = VNET_API_ERROR_URI_FIFO_CREATE_FAILED;
1126       clib_spinlock_unlock (&vcm->sessions_lockp);
1127       goto send_reply;
1128     }
1129
1130   session->server_rx_fifo->master_session_index = session_index;
1131   session->server_tx_fifo->master_session_index = session_index;
1132   session->client_queue_address = mp->client_queue_address;
1133   session->is_cut_thru = 1;
1134   session->is_server = 1;
1135   session->port = mp->port;
1136   session->peer_addr.is_ip4 = mp->is_ip4;
1137   clib_memcpy (&session->peer_addr.ip46, mp->ip,
1138                sizeof (session->peer_addr.ip46));
1139   {
1140     void *oldheap;
1141     ssvm_shared_header_t *sh = seg->ssvm.sh;
1142
1143     ssvm_lock_non_recursive (sh, 1);
1144     oldheap = ssvm_push_heap (sh);
1145     event_q = session->vpp_event_queue =
1146       unix_shared_memory_queue_init (vcm->cfg.event_queue_size,
1147                                      sizeof (session_fifo_event_t),
1148                                      vcm->my_pid, 0 /* signal not sent */ );
1149     ssvm_pop_heap (oldheap);
1150     ssvm_unlock_non_recursive (sh);
1151   }
1152
1153   session->state = STATE_ACCEPT;
1154   if (VPPCOM_DEBUG > 1)
1155     clib_warning ("[%d] Connected cut-thru to client: sid %d",
1156                   vcm->my_pid, session_index);
1157   clib_fifo_add1 (vcm->client_session_index_fifo, session_index);
1158   clib_spinlock_unlock (&vcm->sessions_lockp);
1159
1160 send_reply:
1161   rmp = vl_msg_api_alloc (sizeof (*rmp));
1162   memset (rmp, 0, sizeof (*rmp));
1163
1164   rmp->_vl_msg_id = ntohs (VL_API_CONNECT_SESSION_REPLY);
1165   rmp->context = mp->context;
1166   rmp->retval = htonl (rv);
1167   rmp->segment_name_length = vec_len (a->segment_name);
1168   clib_memcpy (rmp->segment_name, a->segment_name, vec_len (a->segment_name));
1169   vec_reset_length (a->segment_name);
1170
1171   if (event_q)
1172     {
1173       rmp->vpp_event_queue_address = pointer_to_uword (event_q);
1174       rmp->server_rx_fifo = pointer_to_uword (rx_fifo);
1175       rmp->server_tx_fifo = pointer_to_uword (tx_fifo);
1176     }
1177   client_q =
1178     uword_to_pointer (mp->client_queue_address, unix_shared_memory_queue_t *);
1179
1180   ASSERT (client_q);
1181   vl_msg_api_send_shmem (client_q, (u8 *) & rmp);
1182 }
1183
1184 static void
1185 vppcom_send_bind_sock (session_t * session)
1186 {
1187   vppcom_main_t *vcm = &vppcom_main;
1188   vl_api_bind_sock_t *bmp;
1189
1190   /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
1191   session->is_server = 1;
1192   bmp = vl_msg_api_alloc (sizeof (*bmp));
1193   memset (bmp, 0, sizeof (*bmp));
1194
1195   bmp->_vl_msg_id = ntohs (VL_API_BIND_SOCK);
1196   bmp->client_index = vcm->my_client_index;
1197   bmp->context = htonl (0xfeedface);
1198   bmp->vrf = session->vrf;
1199   bmp->is_ip4 = session->lcl_addr.is_ip4;
1200   clib_memcpy (bmp->ip, &session->lcl_addr.ip46, sizeof (bmp->ip));
1201   bmp->port = session->port;
1202   bmp->proto = session->proto;
1203   clib_memcpy (bmp->options, session->options, sizeof (bmp->options));
1204   vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
1205 }
1206
1207 static void
1208 vppcom_send_unbind_sock (u32 session_index)
1209 {
1210   vppcom_main_t *vcm = &vppcom_main;
1211   vl_api_unbind_sock_t *ump;
1212   session_t *session = 0;
1213   int rv;
1214
1215   clib_spinlock_lock (&vcm->sessions_lockp);
1216   rv = vppcom_session_at_index (session_index, &session);
1217   if (PREDICT_FALSE (rv))
1218     {
1219       clib_spinlock_unlock (&vcm->sessions_lockp);
1220       if (VPPCOM_DEBUG > 0)
1221         clib_warning ("[%d] invalid session, sid (%u) has been closed!",
1222                       vcm->my_pid, session_index);
1223       return;
1224     }
1225
1226   ump = vl_msg_api_alloc (sizeof (*ump));
1227   memset (ump, 0, sizeof (*ump));
1228
1229   ump->_vl_msg_id = ntohs (VL_API_UNBIND_SOCK);
1230   ump->client_index = vcm->my_client_index;
1231   ump->handle = session->vpp_session_handle;
1232   clib_spinlock_unlock (&vcm->sessions_lockp);
1233   vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & ump);
1234 }
1235
1236 static int
1237 vppcom_session_unbind_cut_thru (session_t * session)
1238 {
1239   svm_fifo_segment_main_t *sm = &svm_fifo_segment_main;
1240   svm_fifo_segment_private_t *seg;
1241   int rv = VPPCOM_OK;
1242
1243   seg = vec_elt_at_index (sm->segments, session->sm_seg_index);
1244   svm_fifo_segment_free_fifo (seg, session->server_rx_fifo,
1245                               FIFO_SEGMENT_RX_FREELIST);
1246   svm_fifo_segment_free_fifo (seg, session->server_tx_fifo,
1247                               FIFO_SEGMENT_TX_FREELIST);
1248   svm_fifo_segment_delete (seg);
1249
1250   return rv;
1251 }
1252
1253 static int
1254 vppcom_session_unbind (u32 session_index)
1255 {
1256   vppcom_main_t *vcm = &vppcom_main;
1257   int rv;
1258
1259   clib_spinlock_lock (&vcm->sessions_lockp);
1260   if (PREDICT_FALSE (pool_is_free_index (vcm->sessions, session_index)))
1261     {
1262       clib_spinlock_unlock (&vcm->sessions_lockp);
1263       if (VPPCOM_DEBUG > 1)
1264         clib_warning ("[%d] invalid session, sid (%u) has been closed!",
1265                       vcm->my_pid, session_index);
1266       return VPPCOM_EBADFD;
1267     }
1268   clib_spinlock_unlock (&vcm->sessions_lockp);
1269
1270   vcm->bind_session_index = session_index;
1271   vppcom_send_unbind_sock (session_index);
1272   rv = vppcom_wait_for_session_state_change (session_index, STATE_START,
1273                                              vcm->cfg.session_timeout);
1274   if (PREDICT_FALSE (rv))
1275     {
1276       vcm->bind_session_index = ~0;
1277       if (VPPCOM_DEBUG > 0)
1278         clib_warning ("[%d] server unbind timed out, rv = %s (%d)",
1279                       vcm->my_pid, vppcom_retval_str (rv), rv);
1280       return rv;
1281     }
1282   return VPPCOM_OK;
1283 }
1284
1285 static int
1286 vppcom_session_disconnect (u32 session_index)
1287 {
1288   vppcom_main_t *vcm = &vppcom_main;
1289   int rv;
1290
1291   rv = vppcom_send_disconnect (session_index);
1292   if (PREDICT_FALSE (rv))
1293     return rv;
1294
1295   rv = vppcom_wait_for_session_state_change (session_index, STATE_DISCONNECT,
1296                                              vcm->cfg.session_timeout);
1297   if (PREDICT_FALSE (rv))
1298     {
1299       if (VPPCOM_DEBUG > 0)
1300         clib_warning ("[%d] client disconnect timed out, rv = %s (%d)",
1301                       vcm->my_pid, vppcom_retval_str (rv), rv);
1302       return rv;
1303     }
1304   return VPPCOM_OK;
1305 }
1306
1307 #define foreach_sock_msg                                        \
1308 _(SESSION_ENABLE_DISABLE_REPLY, session_enable_disable_reply)   \
1309 _(BIND_SOCK_REPLY, bind_sock_reply)                             \
1310 _(UNBIND_SOCK_REPLY, unbind_sock_reply)                         \
1311 _(ACCEPT_SESSION, accept_session)                               \
1312 _(CONNECT_SOCK, connect_sock)                                   \
1313 _(CONNECT_SESSION_REPLY, connect_session_reply)                 \
1314 _(DISCONNECT_SESSION, disconnect_session)                       \
1315 _(DISCONNECT_SESSION_REPLY, disconnect_session_reply)           \
1316 _(RESET_SESSION, reset_session)                                 \
1317 _(APPLICATION_ATTACH_REPLY, application_attach_reply)           \
1318 _(APPLICATION_DETACH_REPLY, application_detach_reply)           \
1319 _(MAP_ANOTHER_SEGMENT, map_another_segment)
1320
1321 static void
1322 vppcom_api_hookup (void)
1323 {
1324 #define _(N,n)                                                  \
1325     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
1326                            vl_api_##n##_t_handler,              \
1327                            vl_noop_handler,                     \
1328                            vl_api_##n##_t_endian,               \
1329                            vl_api_##n##_t_print,                \
1330                            sizeof(vl_api_##n##_t), 1);
1331   foreach_sock_msg;
1332 #undef _
1333 }
1334
1335 static void
1336 vppcom_cfg_init (vppcom_cfg_t * vcl_cfg)
1337 {
1338   ASSERT (vcl_cfg);
1339
1340   vcl_cfg->heapsize = (256ULL << 20);
1341   vcl_cfg->segment_baseva = 0x200000000ULL;
1342   vcl_cfg->segment_size = (256 << 20);
1343   vcl_cfg->add_segment_size = (128 << 20);
1344   vcl_cfg->preallocated_fifo_pairs = 8;
1345   vcl_cfg->rx_fifo_size = (1 << 20);
1346   vcl_cfg->tx_fifo_size = (1 << 20);
1347   vcl_cfg->event_queue_size = 2048;
1348   vcl_cfg->listen_queue_size = CLIB_CACHE_LINE_BYTES / sizeof (u32);
1349   vcl_cfg->app_timeout = 10 * 60.0;
1350   vcl_cfg->session_timeout = 10 * 60.0;
1351   vcl_cfg->accept_timeout = 60.0;
1352 }
1353
1354 static void
1355 vppcom_cfg_heapsize (char *conf_fname)
1356 {
1357   vppcom_main_t *vcm = &vppcom_main;
1358   vppcom_cfg_t *vcl_cfg = &vcm->cfg;
1359   FILE *fp;
1360   char inbuf[4096];
1361   int argc = 1;
1362   char **argv = NULL;
1363   char *arg = NULL;
1364   char *p;
1365   int i;
1366   u8 *sizep;
1367   u32 size;
1368
1369   fp = fopen (conf_fname, "r");
1370   if (fp == NULL)
1371     {
1372       if (VPPCOM_DEBUG > 0)
1373         fprintf (stderr, "open configuration file '%s' failed\n", conf_fname);
1374       goto defaulted;
1375     }
1376   argv = calloc (1, sizeof (char *));
1377   if (argv == NULL)
1378     goto defaulted;
1379
1380   while (1)
1381     {
1382       if (fgets (inbuf, 4096, fp) == 0)
1383         break;
1384       p = strtok (inbuf, " \t\n");
1385       while (p != NULL)
1386         {
1387           if (*p == '#')
1388             break;
1389           argc++;
1390           char **tmp = realloc (argv, argc * sizeof (char *));
1391           if (tmp == NULL)
1392             goto defaulted;
1393           argv = tmp;
1394           arg = strndup (p, 1024);
1395           if (arg == NULL)
1396             goto defaulted;
1397           argv[argc - 1] = arg;
1398           p = strtok (NULL, " \t\n");
1399         }
1400     }
1401
1402   fclose (fp);
1403   fp = NULL;
1404
1405   char **tmp = realloc (argv, (argc + 1) * sizeof (char *));
1406   if (tmp == NULL)
1407     goto defaulted;
1408   argv = tmp;
1409   argv[argc] = NULL;
1410
1411   /*
1412    * Look for and parse the "heapsize" config parameter.
1413    * Manual since none of the clib infra has been bootstrapped yet.
1414    *
1415    * Format: heapsize <nn>[mM][gG]
1416    */
1417
1418   for (i = 1; i < (argc - 1); i++)
1419     {
1420       if (!strncmp (argv[i], "heapsize", 8))
1421         {
1422           sizep = (u8 *) argv[i + 1];
1423           size = 0;
1424           while (*sizep >= '0' && *sizep <= '9')
1425             {
1426               size *= 10;
1427               size += *sizep++ - '0';
1428             }
1429           if (size == 0)
1430             {
1431               if (VPPCOM_DEBUG > 0)
1432                 clib_warning ("[%d] parse error '%s %s', "
1433                               "using default heapsize %lld (0x%llx)",
1434                               vcm->my_pid, argv[i], argv[i + 1],
1435                               vcl_cfg->heapsize, vcl_cfg->heapsize);
1436               goto defaulted;
1437             }
1438
1439           if (*sizep == 'g' || *sizep == 'G')
1440             vcl_cfg->heapsize = size << 30;
1441           else if (*sizep == 'm' || *sizep == 'M')
1442             vcl_cfg->heapsize = size << 20;
1443           else
1444             {
1445               if (VPPCOM_DEBUG > 0)
1446                 clib_warning ("[%d] parse error '%s %s', "
1447                               "using default heapsize %lld (0x%llx)",
1448                               vcm->my_pid, argv[i], argv[i + 1],
1449                               vcl_cfg->heapsize, vcl_cfg->heapsize);
1450               goto defaulted;
1451             }
1452         }
1453     }
1454
1455 defaulted:
1456   if (fp != NULL)
1457     fclose (fp);
1458   if (argv != NULL)
1459     free (argv);
1460   if (!clib_mem_init (0, vcl_cfg->heapsize))
1461     clib_warning ("[%d] vppcom heap allocation failure!", vcm->my_pid);
1462   else if (VPPCOM_DEBUG > 0)
1463     clib_warning ("[%d] allocated vppcom heapsize %lld (0x%llx)",
1464                   vcm->my_pid, vcl_cfg->heapsize, vcl_cfg->heapsize);
1465 }
1466
1467 static void
1468 vppcom_cfg_read (char *conf_fname)
1469 {
1470   vppcom_main_t *vcm = &vppcom_main;
1471   vppcom_cfg_t *vcl_cfg = &vcm->cfg;
1472   int fd;
1473   unformat_input_t _input, *input = &_input;
1474   unformat_input_t _line_input, *line_input = &_line_input;
1475   u8 vc_cfg_input = 0;
1476   u8 *chroot_path;
1477   struct stat s;
1478   u32 uid, gid;
1479
1480   fd = open (conf_fname, O_RDONLY);
1481   if (fd < 0)
1482     {
1483       if (VPPCOM_DEBUG > 0)
1484         clib_warning ("[%d] open configuration file '%s' failed!",
1485                       vcm->my_pid, conf_fname);
1486       goto file_done;
1487     }
1488
1489   if (fstat (fd, &s) < 0)
1490     {
1491       if (VPPCOM_DEBUG > 0)
1492         clib_warning ("[%d] failed to stat `%s'", vcm->my_pid, conf_fname);
1493       goto file_done;
1494     }
1495
1496   if (!(S_ISREG (s.st_mode) || S_ISLNK (s.st_mode)))
1497     {
1498       if (VPPCOM_DEBUG > 0)
1499         clib_warning ("[%d] not a regular file `%s'", vcm->my_pid,
1500                       conf_fname);
1501       goto file_done;
1502     }
1503
1504   unformat_init_clib_file (input, fd);
1505
1506   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1507     {
1508       (void) unformat_user (input, unformat_line_input, line_input);
1509       unformat_skip_white_space (line_input);
1510
1511       if (unformat (line_input, "vppcom {"))
1512         {
1513           vc_cfg_input = 1;
1514           continue;
1515         }
1516
1517       if (vc_cfg_input)
1518         {
1519           if (unformat (line_input, "heapsize %s", &chroot_path))
1520             {
1521               vec_terminate_c_string (chroot_path);
1522               if (VPPCOM_DEBUG > 0)
1523                 clib_warning ("[%d] configured heapsize %s, "
1524                               "actual heapsize %lld (0x%llx)",
1525                               vcm->my_pid, chroot_path, vcl_cfg->heapsize,
1526                               vcl_cfg->heapsize);
1527               vec_free (chroot_path);
1528             }
1529           else if (unformat (line_input, "api-prefix %s", &chroot_path))
1530             {
1531               vec_terminate_c_string (chroot_path);
1532               vl_set_memory_root_path ((char *) chroot_path);
1533               if (VPPCOM_DEBUG > 0)
1534                 clib_warning ("[%d] configured api-prefix %s",
1535                               vcm->my_pid, chroot_path);
1536               chroot_path = 0;  /* Don't vec_free() it! */
1537             }
1538           else if (unformat (line_input, "uid %d", &uid))
1539             {
1540               vl_set_memory_uid (uid);
1541               if (VPPCOM_DEBUG > 0)
1542                 clib_warning ("[%d] configured uid %d", vcm->my_pid, uid);
1543             }
1544           else if (unformat (line_input, "gid %d", &gid))
1545             {
1546               vl_set_memory_gid (gid);
1547               if (VPPCOM_DEBUG > 0)
1548                 clib_warning ("[%d] configured gid %d", vcm->my_pid, gid);
1549             }
1550           else if (unformat (line_input, "segment-baseva 0x%llx",
1551                              &vcl_cfg->segment_baseva))
1552             {
1553               if (VPPCOM_DEBUG > 0)
1554                 clib_warning ("[%d] configured segment_baseva 0x%llx",
1555                               vcm->my_pid, vcl_cfg->segment_baseva);
1556             }
1557           else if (unformat (line_input, "segment-size 0x%lx",
1558                              &vcl_cfg->segment_size))
1559             {
1560               if (VPPCOM_DEBUG > 0)
1561                 clib_warning ("[%d] configured segment_size 0x%lx (%ld)",
1562                               vcm->my_pid, vcl_cfg->segment_size,
1563                               vcl_cfg->segment_size);
1564             }
1565           else if (unformat (line_input, "segment-size %ld",
1566                              &vcl_cfg->segment_size))
1567             {
1568               if (VPPCOM_DEBUG > 0)
1569                 clib_warning ("[%d] configured segment_size %ld (0x%lx)",
1570                               vcm->my_pid, vcl_cfg->segment_size,
1571                               vcl_cfg->segment_size);
1572             }
1573           else if (unformat (line_input, "add-segment-size 0x%lx",
1574                              &vcl_cfg->add_segment_size))
1575             {
1576               if (VPPCOM_DEBUG > 0)
1577                 clib_warning
1578                   ("[%d] configured add_segment_size 0x%lx (%ld)",
1579                    vcm->my_pid, vcl_cfg->add_segment_size,
1580                    vcl_cfg->add_segment_size);
1581             }
1582           else if (unformat (line_input, "add-segment-size %ld",
1583                              &vcl_cfg->add_segment_size))
1584             {
1585               if (VPPCOM_DEBUG > 0)
1586                 clib_warning
1587                   ("[%d] configured add_segment_size %ld (0x%lx)",
1588                    vcm->my_pid, vcl_cfg->add_segment_size,
1589                    vcl_cfg->add_segment_size);
1590             }
1591           else if (unformat (line_input, "preallocated-fifo-pairs %d",
1592                              &vcl_cfg->preallocated_fifo_pairs))
1593             {
1594               if (VPPCOM_DEBUG > 0)
1595                 clib_warning ("[%d] configured preallocated_fifo_pairs "
1596                               "%d (0x%x)", vcm->my_pid,
1597                               vcl_cfg->preallocated_fifo_pairs,
1598                               vcl_cfg->preallocated_fifo_pairs);
1599             }
1600           else if (unformat (line_input, "rx-fifo-size 0x%lx",
1601                              &vcl_cfg->rx_fifo_size))
1602             {
1603               if (VPPCOM_DEBUG > 0)
1604                 clib_warning ("[%d] configured rx_fifo_size 0x%lx (%ld)",
1605                               vcm->my_pid, vcl_cfg->rx_fifo_size,
1606                               vcl_cfg->rx_fifo_size);
1607             }
1608           else if (unformat (line_input, "rx-fifo-size %ld",
1609                              &vcl_cfg->rx_fifo_size))
1610             {
1611               if (VPPCOM_DEBUG > 0)
1612                 clib_warning ("[%d] configured rx_fifo_size %ld (0x%lx)",
1613                               vcm->my_pid, vcl_cfg->rx_fifo_size,
1614                               vcl_cfg->rx_fifo_size);
1615             }
1616           else if (unformat (line_input, "tx-fifo-size 0x%lx",
1617                              &vcl_cfg->tx_fifo_size))
1618             {
1619               if (VPPCOM_DEBUG > 0)
1620                 clib_warning ("[%d] configured tx_fifo_size 0x%lx (%ld)",
1621                               vcm->my_pid, vcl_cfg->tx_fifo_size,
1622                               vcl_cfg->tx_fifo_size);
1623             }
1624           else if (unformat (line_input, "tx-fifo-size %ld",
1625                              &vcl_cfg->tx_fifo_size))
1626             {
1627               if (VPPCOM_DEBUG > 0)
1628                 clib_warning ("[%d] configured tx_fifo_size %ld (0x%lx)",
1629                               vcm->my_pid, vcl_cfg->tx_fifo_size,
1630                               vcl_cfg->tx_fifo_size);
1631             }
1632           else if (unformat (line_input, "event-queue-size 0x%lx",
1633                              &vcl_cfg->event_queue_size))
1634             {
1635               if (VPPCOM_DEBUG > 0)
1636                 clib_warning ("[%d] configured event_queue_size 0x%lx (%ld)",
1637                               vcm->my_pid, vcl_cfg->event_queue_size,
1638                               vcl_cfg->event_queue_size);
1639             }
1640           else if (unformat (line_input, "event-queue-size %ld",
1641                              &vcl_cfg->event_queue_size))
1642             {
1643               if (VPPCOM_DEBUG > 0)
1644                 clib_warning ("[%d] configured event_queue_size %ld (0x%lx)",
1645                               vcm->my_pid, vcl_cfg->event_queue_size,
1646                               vcl_cfg->event_queue_size);
1647             }
1648           else if (unformat (line_input, "listen-queue-size 0x%lx",
1649                              &vcl_cfg->listen_queue_size))
1650             {
1651               if (VPPCOM_DEBUG > 0)
1652                 clib_warning ("[%d] configured listen_queue_size 0x%lx (%ld)",
1653                               vcm->my_pid, vcl_cfg->listen_queue_size,
1654                               vcl_cfg->listen_queue_size);
1655             }
1656           else if (unformat (line_input, "listen-queue-size %ld",
1657                              &vcl_cfg->listen_queue_size))
1658             {
1659               if (VPPCOM_DEBUG > 0)
1660                 clib_warning ("[%d] configured listen_queue_size %ld (0x%lx)",
1661                               vcm->my_pid, vcl_cfg->listen_queue_size,
1662                               vcl_cfg->listen_queue_size);
1663             }
1664           else if (unformat (line_input, "app-timeout %f",
1665                              &vcl_cfg->app_timeout))
1666             {
1667               if (VPPCOM_DEBUG > 0)
1668                 clib_warning ("[%d] configured app_timeout %f",
1669                               vcm->my_pid, vcl_cfg->app_timeout);
1670             }
1671           else if (unformat (line_input, "session-timeout %f",
1672                              &vcl_cfg->session_timeout))
1673             {
1674               if (VPPCOM_DEBUG > 0)
1675                 clib_warning ("[%d] configured session_timeout %f",
1676                               vcm->my_pid, vcl_cfg->session_timeout);
1677             }
1678           else if (unformat (line_input, "accept-timeout %f",
1679                              &vcl_cfg->accept_timeout))
1680             {
1681               if (VPPCOM_DEBUG > 0)
1682                 clib_warning ("[%d] configured accept_timeout %f",
1683                               vcm->my_pid, vcl_cfg->accept_timeout);
1684             }
1685           else if (unformat (line_input, "}"))
1686             {
1687               vc_cfg_input = 0;
1688               if (VPPCOM_DEBUG > 0)
1689                 clib_warning ("[%d] completed parsing vppcom config!",
1690                               vcm->my_pid);
1691               goto input_done;
1692             }
1693           else
1694             {
1695               if (line_input->buffer[line_input->index] != '#')
1696                 {
1697                   clib_warning ("[%d] Unknown vppcom config option: '%s'",
1698                                 vcm->my_pid, (char *)
1699                                 &line_input->buffer[line_input->index]);
1700                 }
1701             }
1702         }
1703     }
1704
1705 input_done:
1706   unformat_free (input);
1707
1708 file_done:
1709   if (fd >= 0)
1710     close (fd);
1711 }
1712
1713 /*
1714  * VPPCOM Public API functions
1715  */
1716 int
1717 vppcom_app_create (char *app_name)
1718 {
1719   vppcom_main_t *vcm = &vppcom_main;
1720   vppcom_cfg_t *vcl_cfg = &vcm->cfg;
1721   u8 *heap;
1722   mheap_t *h;
1723   int rv;
1724
1725   if (!vcm->init)
1726     {
1727       char *conf_fname;
1728
1729       vcm->init = 1;
1730       vcm->my_pid = getpid ();
1731       clib_fifo_validate (vcm->client_session_index_fifo,
1732                           vcm->cfg.listen_queue_size);
1733       vppcom_cfg_init (vcl_cfg);
1734       conf_fname = getenv (VPPCOM_CONF_ENV);
1735       if (!conf_fname)
1736         {
1737           conf_fname = VPPCOM_CONF_DEFAULT;
1738           if (VPPCOM_DEBUG > 0)
1739             clib_warning ("[%d] getenv '%s' failed!", vcm->my_pid,
1740                           VPPCOM_CONF_ENV);
1741         }
1742       vppcom_cfg_heapsize (conf_fname);
1743       vppcom_cfg_read (conf_fname);
1744       vcm->bind_session_index = ~0;
1745       vcm->main_cpu = os_get_thread_index ();
1746       heap = clib_mem_get_per_cpu_heap ();
1747       h = mheap_header (heap);
1748
1749       /* make the main heap thread-safe */
1750       h->flags |= MHEAP_FLAG_THREAD_SAFE;
1751
1752       vcm->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
1753
1754       clib_time_init (&vcm->clib_time);
1755       vppcom_init_error_string_table ();
1756       svm_fifo_segment_init (vcl_cfg->segment_baseva,
1757                              20 /* timeout in secs */ );
1758       clib_spinlock_init (&vcm->sessions_lockp);
1759       vppcom_api_hookup ();
1760     }
1761
1762   if (vcm->my_client_index == ~0)
1763     {
1764       vcm->app_state = STATE_APP_START;
1765       rv = vppcom_connect_to_vpp (app_name);
1766       if (rv)
1767         {
1768           clib_warning ("[%d] couldn't connect to VPP.", vcm->my_pid);
1769           return rv;
1770         }
1771
1772       if (VPPCOM_DEBUG > 0)
1773         clib_warning ("[%d] sending session enable", vcm->my_pid);
1774
1775       rv = vppcom_app_session_enable ();
1776       if (rv)
1777         {
1778           clib_warning ("[%d] vppcom_app_session_enable() failed!",
1779                         vcm->my_pid);
1780           return rv;
1781         }
1782
1783       if (VPPCOM_DEBUG > 0)
1784         clib_warning ("[%d] sending app attach", vcm->my_pid);
1785
1786       rv = vppcom_app_attach ();
1787       if (rv)
1788         {
1789           clib_warning ("[%d] vppcom_app_attach() failed!", vcm->my_pid);
1790           return rv;
1791         }
1792     }
1793
1794   if (VPPCOM_DEBUG > 0)
1795     clib_warning ("[%d] app_name '%s', my_client_index %d (0x%x)",
1796                   vcm->my_pid, app_name, vcm->my_client_index,
1797                   vcm->my_client_index);
1798
1799   return VPPCOM_OK;
1800 }
1801
1802 void
1803 vppcom_app_destroy (void)
1804 {
1805   vppcom_main_t *vcm = &vppcom_main;
1806   int rv;
1807
1808   if (vcm->my_client_index == ~0)
1809     return;
1810
1811   if (VPPCOM_DEBUG > 0)
1812     clib_warning ("[%d] detaching from VPP, my_client_index %d (0x%x)",
1813                   vcm->my_pid, vcm->my_client_index, vcm->my_client_index);
1814
1815   vppcom_app_detach ();
1816   rv = vppcom_wait_for_app_state_change (STATE_APP_ENABLED);
1817   if (PREDICT_FALSE (rv))
1818     {
1819       if (VPPCOM_DEBUG > 0)
1820         clib_warning ("[%d] application detach timed out, rv = %s (%d)",
1821                       vcm->my_pid, vppcom_retval_str (rv), rv);
1822     }
1823   vl_client_disconnect_from_vlib ();
1824   vcm->my_client_index = ~0;
1825   vcm->app_state = STATE_APP_START;
1826 }
1827
1828 int
1829 vppcom_session_create (u32 vrf, u8 proto, u8 is_nonblocking)
1830 {
1831   vppcom_main_t *vcm = &vppcom_main;
1832   session_t *session;
1833   u32 session_index;
1834
1835   clib_spinlock_lock (&vcm->sessions_lockp);
1836   pool_get (vcm->sessions, session);
1837   memset (session, 0, sizeof (*session));
1838   session_index = session - vcm->sessions;
1839
1840   session->vrf = vrf;
1841   session->proto = proto;
1842   session->state = STATE_START;
1843   session->is_nonblocking = is_nonblocking ? 1 : 0;
1844   clib_spinlock_unlock (&vcm->sessions_lockp);
1845
1846   if (VPPCOM_DEBUG > 0)
1847     clib_warning ("[%d] sid %d", vcm->my_pid, session_index);
1848
1849   return (int) session_index;
1850 }
1851
1852 int
1853 vppcom_session_close (uint32_t session_index)
1854 {
1855   vppcom_main_t *vcm = &vppcom_main;
1856   session_t *session = 0;
1857   int rv;
1858
1859   clib_spinlock_lock (&vcm->sessions_lockp);
1860   rv = vppcom_session_at_index (session_index, &session);
1861   if (PREDICT_FALSE (rv))
1862     {
1863       if (VPPCOM_DEBUG > 0)
1864         clib_warning ("[%d] invalid session, sid (%u) has been closed!",
1865                       vcm->my_pid, session_index);
1866       clib_spinlock_unlock (&vcm->sessions_lockp);
1867       goto done;
1868     }
1869   clib_spinlock_unlock (&vcm->sessions_lockp);
1870
1871   if (VPPCOM_DEBUG > 0)
1872     clib_warning ("[%d] sid %d", vcm->my_pid, session_index);
1873
1874   if (session->is_vep)
1875     {
1876       u32 next_sid;
1877       for (next_sid = session->vep.next_sid; next_sid != ~0;
1878            next_sid = session->vep.next_sid)
1879         {
1880           rv = vppcom_epoll_ctl (session_index, EPOLL_CTL_DEL, next_sid, 0);
1881           if ((VPPCOM_DEBUG > 0) && (rv < 0))
1882             clib_warning ("[%d] EPOLL_CTL_DEL vep_idx %u, sid %u failed, "
1883                           "rv = %s (%d)", session_index, next_sid,
1884                           vcm->my_pid, session_index,
1885                           vppcom_retval_str (rv), rv);
1886
1887           clib_spinlock_lock (&vcm->sessions_lockp);
1888           rv = vppcom_session_at_index (session_index, &session);
1889           if (PREDICT_FALSE (rv))
1890             {
1891               if (VPPCOM_DEBUG > 0)
1892                 clib_warning
1893                   ("[%d] invalid session, sid (%u) has been closed!",
1894                    vcm->my_pid, session_index);
1895               clib_spinlock_unlock (&vcm->sessions_lockp);
1896               goto done;
1897             }
1898           clib_spinlock_unlock (&vcm->sessions_lockp);
1899         }
1900     }
1901   else
1902     {
1903       if (session->is_vep_session)
1904         {
1905           u32 vep_idx = session->vep.vep_idx;
1906           rv = vppcom_epoll_ctl (vep_idx, EPOLL_CTL_DEL, session_index, 0);
1907           if ((VPPCOM_DEBUG > 0) && (rv < 0))
1908             clib_warning ("[%d] EPOLL_CTL_DEL vep_idx %u, sid %u failed, "
1909                           "rv = %s (%d)", vep_idx, session_index,
1910                           vcm->my_pid, session_index,
1911                           vppcom_retval_str (rv), rv);
1912
1913           clib_spinlock_lock (&vcm->sessions_lockp);
1914           rv = vppcom_session_at_index (session_index, &session);
1915           if (PREDICT_FALSE (rv))
1916             {
1917               if (VPPCOM_DEBUG > 0)
1918                 clib_warning
1919                   ("[%d] invalid session, sid (%u) has been closed!",
1920                    vcm->my_pid, session_index);
1921               clib_spinlock_unlock (&vcm->sessions_lockp);
1922               goto done;
1923             }
1924           clib_spinlock_unlock (&vcm->sessions_lockp);
1925         }
1926
1927       if (session->is_cut_thru && session->is_server &&
1928           (session->state == STATE_ACCEPT))
1929         {
1930           rv = vppcom_session_unbind_cut_thru (session);
1931           if ((VPPCOM_DEBUG > 0) && (rv < 0))
1932             clib_warning ("[%d] unbind cut-thru (session %d) failed, "
1933                           "rv = %s (%d)",
1934                           vcm->my_pid, session_index,
1935                           vppcom_retval_str (rv), rv);
1936         }
1937       else if (session->is_server && session->is_listen)
1938         {
1939           rv = vppcom_session_unbind (session_index);
1940           if ((VPPCOM_DEBUG > 0) && (rv < 0))
1941             clib_warning ("[%d] unbind (session %d) failed, rv = %s (%d)",
1942                           vcm->my_pid, session_index,
1943                           vppcom_retval_str (rv), rv);
1944         }
1945       else if (session->state == STATE_CONNECT)
1946         {
1947           rv = vppcom_session_disconnect (session_index);
1948           if ((VPPCOM_DEBUG > 0) && (rv < 0))
1949             clib_warning ("[%d] disconnect (session %d) failed, rv = %s (%d)",
1950                           vcm->my_pid, session_index,
1951                           vppcom_retval_str (rv), rv);
1952         }
1953     }
1954   pool_put_index (vcm->sessions, session_index);
1955 done:
1956   return rv;
1957 }
1958
1959 int
1960 vppcom_session_bind (uint32_t session_index, vppcom_endpt_t * ep)
1961 {
1962   vppcom_main_t *vcm = &vppcom_main;
1963   session_t *session = 0;
1964   int rv;
1965
1966   if (!ep || !ep->ip)
1967     return VPPCOM_EINVAL;
1968
1969   clib_spinlock_lock (&vcm->sessions_lockp);
1970   rv = vppcom_session_at_index (session_index, &session);
1971   if (PREDICT_FALSE (rv))
1972     {
1973       clib_spinlock_unlock (&vcm->sessions_lockp);
1974       if (VPPCOM_DEBUG > 0)
1975         clib_warning ("[%d] invalid session, sid (%u) has been closed!",
1976                       vcm->my_pid, session_index);
1977       return rv;
1978     }
1979
1980   if (session->is_vep)
1981     {
1982       clib_spinlock_unlock (&vcm->sessions_lockp);
1983       if (VPPCOM_DEBUG > 0)
1984         clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
1985                       vcm->my_pid, session_index);
1986       return VPPCOM_EBADFD;
1987     }
1988
1989   if (VPPCOM_DEBUG > 0)
1990     clib_warning ("[%d] sid %d", vcm->my_pid, session_index);
1991
1992   session->vrf = ep->vrf;
1993   session->lcl_addr.is_ip4 = ep->is_ip4;
1994   session->lcl_addr.ip46 = to_ip46 (!ep->is_ip4, ep->ip);
1995   session->port = ep->port;
1996
1997   clib_spinlock_unlock (&vcm->sessions_lockp);
1998   return VPPCOM_OK;
1999 }
2000
2001 int
2002 vppcom_session_listen (uint32_t listen_session_index, uint32_t q_len)
2003 {
2004   vppcom_main_t *vcm = &vppcom_main;
2005   session_t *listen_session = 0;
2006   int rv;
2007
2008   clib_spinlock_lock (&vcm->sessions_lockp);
2009   rv = vppcom_session_at_index (listen_session_index, &listen_session);
2010   if (PREDICT_FALSE (rv))
2011     {
2012       clib_spinlock_unlock (&vcm->sessions_lockp);
2013       if (VPPCOM_DEBUG > 0)
2014         clib_warning ("[%d] invalid session, sid (%u) has been closed!",
2015                       vcm->my_pid, listen_session_index);
2016       return rv;
2017     }
2018
2019   if (listen_session->is_vep)
2020     {
2021       clib_spinlock_unlock (&vcm->sessions_lockp);
2022       if (VPPCOM_DEBUG > 0)
2023         clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
2024                       vcm->my_pid, listen_session_index);
2025       return VPPCOM_EBADFD;
2026     }
2027
2028   if (VPPCOM_DEBUG > 0)
2029     clib_warning ("[%d] sid %d", vcm->my_pid, listen_session_index);
2030
2031   ASSERT (vcm->bind_session_index == ~0);
2032   vcm->bind_session_index = listen_session_index;
2033   vppcom_send_bind_sock (listen_session);
2034   clib_spinlock_unlock (&vcm->sessions_lockp);
2035   rv =
2036     vppcom_wait_for_session_state_change (listen_session_index, STATE_LISTEN,
2037                                           vcm->cfg.session_timeout);
2038   if (PREDICT_FALSE (rv))
2039     {
2040       vcm->bind_session_index = ~0;
2041       if (VPPCOM_DEBUG > 0)
2042         clib_warning ("[%d] server listen timed out, rv = %d (%d)",
2043                       vcm->my_pid, vppcom_retval_str (rv), rv);
2044       return rv;
2045     }
2046
2047   clib_spinlock_lock (&vcm->sessions_lockp);
2048   rv = vppcom_session_at_index (listen_session_index, &listen_session);
2049   if (PREDICT_FALSE (rv))
2050     {
2051       clib_spinlock_unlock (&vcm->sessions_lockp);
2052       if (VPPCOM_DEBUG > 0)
2053         clib_warning ("[%d] invalid session, sid (%u) has been closed!",
2054                       vcm->my_pid, listen_session_index);
2055       return rv;
2056     }
2057   listen_session->is_listen = 1;
2058   clib_fifo_validate (vcm->client_session_index_fifo, q_len);
2059   clib_spinlock_unlock (&vcm->sessions_lockp);
2060
2061   return VPPCOM_OK;
2062 }
2063
2064 int
2065 vppcom_session_accept (uint32_t listen_session_index, vppcom_endpt_t * ep,
2066                        double wait_for_time)
2067 {
2068   vppcom_main_t *vcm = &vppcom_main;
2069   session_t *listen_session = 0;
2070   session_t *client_session = 0;
2071   u32 client_session_index;
2072   int rv;
2073   f64 wait_for;
2074
2075   clib_spinlock_lock (&vcm->sessions_lockp);
2076   rv = vppcom_session_at_index (listen_session_index, &listen_session);
2077   if (PREDICT_FALSE (rv))
2078     {
2079       clib_spinlock_unlock (&vcm->sessions_lockp);
2080       if (VPPCOM_DEBUG > 0)
2081         clib_warning ("[%d] invalid session, sid (%u) has been closed!",
2082                       vcm->my_pid, listen_session_index);
2083       return rv;
2084     }
2085
2086   if (listen_session->is_vep)
2087     {
2088       clib_spinlock_unlock (&vcm->sessions_lockp);
2089       if (VPPCOM_DEBUG > 0)
2090         clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
2091                       vcm->my_pid, listen_session_index);
2092       return VPPCOM_EBADFD;
2093     }
2094
2095   if (listen_session->state != STATE_LISTEN)
2096     {
2097       clib_spinlock_unlock (&vcm->sessions_lockp);
2098       if (VPPCOM_DEBUG > 0)
2099         clib_warning ("[%d] session not in listen state, state = %s",
2100                       vcm->my_pid,
2101                       vppcom_session_state_str (listen_session->state));
2102       return VPPCOM_EBADFD;
2103     }
2104   wait_for = listen_session->is_nonblocking ? 0 :
2105     (wait_for_time < 0) ? vcm->cfg.accept_timeout : wait_for_time;
2106
2107   if (VPPCOM_DEBUG > 0)
2108     clib_warning ("[%d] sid %d: %s (%d)", vcm->my_pid,
2109                   listen_session_index,
2110                   vppcom_session_state_str (listen_session->state),
2111                   listen_session->state);
2112   clib_spinlock_unlock (&vcm->sessions_lockp);
2113
2114   while (1)
2115     {
2116       rv = vppcom_wait_for_client_session_index (wait_for);
2117       if (rv)
2118         {
2119           if ((VPPCOM_DEBUG > 0))
2120             clib_warning ("[%d] sid %d, accept timed out, rv = %s (%d)",
2121                           vcm->my_pid, listen_session_index,
2122                           vppcom_retval_str (rv), rv);
2123           if ((wait_for == 0) || (wait_for_time > 0))
2124             return rv;
2125         }
2126       else
2127         break;
2128     }
2129
2130   clib_spinlock_lock (&vcm->sessions_lockp);
2131   clib_fifo_sub1 (vcm->client_session_index_fifo, client_session_index);
2132   rv = vppcom_session_at_index (client_session_index, &client_session);
2133   ASSERT (rv == VPPCOM_OK);
2134   ASSERT (client_session->peer_addr.is_ip4 ==
2135           listen_session->lcl_addr.is_ip4);
2136
2137   if (VPPCOM_DEBUG > 0)
2138     clib_warning ("[%d] Got a request: client sid %d", vcm->my_pid,
2139                   client_session_index);
2140
2141   ep->vrf = client_session->vrf;
2142   ep->is_cut_thru = client_session->is_cut_thru;
2143   ep->is_ip4 = client_session->peer_addr.is_ip4;
2144   ep->port = client_session->port;
2145   if (client_session->peer_addr.is_ip4)
2146     clib_memcpy (ep->ip, &client_session->peer_addr.ip46.ip4,
2147                  sizeof (ip4_address_t));
2148   else
2149     clib_memcpy (ep->ip, &client_session->peer_addr.ip46.ip6,
2150                  sizeof (ip6_address_t));
2151   clib_spinlock_unlock (&vcm->sessions_lockp);
2152   return (int) client_session_index;
2153 }
2154
2155 int
2156 vppcom_session_connect (uint32_t session_index, vppcom_endpt_t * server_ep)
2157 {
2158   vppcom_main_t *vcm = &vppcom_main;
2159   session_t *session = 0;
2160   int rv;
2161
2162   clib_spinlock_lock (&vcm->sessions_lockp);
2163   rv = vppcom_session_at_index (session_index, &session);
2164   if (PREDICT_FALSE (rv))
2165     {
2166       clib_spinlock_unlock (&vcm->sessions_lockp);
2167       if (VPPCOM_DEBUG > 0)
2168         clib_warning ("[%d] invalid session, sid (%u) has been closed!",
2169                       vcm->my_pid, session_index);
2170       return rv;
2171     }
2172
2173   if (session->is_vep)
2174     {
2175       clib_spinlock_unlock (&vcm->sessions_lockp);
2176       if (VPPCOM_DEBUG > 0)
2177         clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
2178                       vcm->my_pid, session_index);
2179       return VPPCOM_EBADFD;
2180     }
2181
2182   if (session->state == STATE_CONNECT)
2183     {
2184       clib_spinlock_unlock (&vcm->sessions_lockp);
2185       if (VPPCOM_DEBUG > 0)
2186         clib_warning ("[%d] session, sid (%u) already connected!",
2187                       vcm->my_pid, session_index);
2188       return VPPCOM_OK;
2189     }
2190
2191   session->vrf = server_ep->vrf;
2192   session->peer_addr.is_ip4 = server_ep->is_ip4;
2193   session->peer_addr.ip46 = to_ip46 (!server_ep->is_ip4, server_ep->ip);
2194   session->port = server_ep->port;
2195
2196   if (VPPCOM_DEBUG > 0)
2197     {
2198       u8 *ip_str = format (0, "%U", format_ip46_address,
2199                            &session->peer_addr.ip46,
2200                            session->peer_addr.is_ip4);
2201       clib_warning ("[%d] connect sid %d to %s server port %d",
2202                     vcm->my_pid, session_index, ip_str,
2203                     clib_net_to_host_u16 (session->port));
2204       vec_free (ip_str);
2205     }
2206
2207   vppcom_send_connect_sock (session, session_index);
2208   clib_spinlock_unlock (&vcm->sessions_lockp);
2209   rv = vppcom_wait_for_session_state_change (session_index, STATE_CONNECT,
2210                                              vcm->cfg.session_timeout);
2211   if (PREDICT_FALSE (rv))
2212     {
2213       if (VPPCOM_DEBUG > 0)
2214         clib_warning ("[%d] connect timed out, rv = %s (%d)",
2215                       vcm->my_pid, vppcom_retval_str (rv), rv);
2216       return rv;
2217     }
2218   return VPPCOM_OK;
2219 }
2220
2221 int
2222 vppcom_session_read (uint32_t session_index, void *buf, int n)
2223 {
2224   vppcom_main_t *vcm = &vppcom_main;
2225   session_t *session = 0;
2226   svm_fifo_t *rx_fifo;
2227   int n_read = 0;
2228   int rv;
2229   char *fifo_str;
2230   u32 poll_et;
2231
2232   ASSERT (buf);
2233
2234   clib_spinlock_lock (&vcm->sessions_lockp);
2235   rv = vppcom_session_at_index (session_index, &session);
2236   if (PREDICT_FALSE (rv))
2237     {
2238       clib_spinlock_unlock (&vcm->sessions_lockp);
2239       if (VPPCOM_DEBUG > 0)
2240         clib_warning ("[%d] invalid session, sid (%u) has been closed!",
2241                       vcm->my_pid, session_index);
2242       return rv;
2243     }
2244
2245   if (session->is_vep)
2246     {
2247       clib_spinlock_unlock (&vcm->sessions_lockp);
2248       if (VPPCOM_DEBUG > 0)
2249         clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
2250                       vcm->my_pid, session_index);
2251       return VPPCOM_EBADFD;
2252     }
2253
2254   if (session->state == STATE_DISCONNECT)
2255     {
2256       clib_spinlock_unlock (&vcm->sessions_lockp);
2257       if (VPPCOM_DEBUG > 0)
2258         clib_warning ("[%d] sid (%u) has been closed by remote peer!",
2259                       vcm->my_pid, session_index);
2260       return VPPCOM_ECONNRESET;
2261     }
2262
2263   rx_fifo = ((!session->is_cut_thru || session->is_server) ?
2264              session->server_rx_fifo : session->server_tx_fifo);
2265   fifo_str = ((!session->is_cut_thru || session->is_server) ?
2266               "server_rx_fifo" : "server_tx_fifo");
2267   poll_et = EPOLLET & session->vep.ev.events;
2268   clib_spinlock_unlock (&vcm->sessions_lockp);
2269
2270   do
2271     {
2272       n_read = svm_fifo_dequeue_nowait (rx_fifo, n, buf);
2273     }
2274   while (!session->is_nonblocking && (n_read <= 0));
2275
2276   if (poll_et && (n_read <= 0))
2277     {
2278       clib_spinlock_lock (&vcm->sessions_lockp);
2279       session->vep.et_mask |= EPOLLIN;
2280       clib_spinlock_unlock (&vcm->sessions_lockp);
2281     }
2282
2283   if ((VPPCOM_DEBUG > 2) && (n_read > 0))
2284     clib_warning ("[%d] sid %d, read %d bytes from %s (%p)", vcm->my_pid,
2285                   session_index, n_read, fifo_str, rx_fifo);
2286
2287   return (n_read <= 0) ? VPPCOM_EAGAIN : n_read;
2288 }
2289
2290 static inline int
2291 vppcom_session_read_ready (session_t * session, u32 session_index)
2292 {
2293   vppcom_main_t *vcm = &vppcom_main;
2294   svm_fifo_t *rx_fifo;
2295   int ready = 0;
2296
2297   /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
2298   if (session->is_vep)
2299     {
2300       clib_spinlock_unlock (&vcm->sessions_lockp);
2301       if (VPPCOM_DEBUG > 0)
2302         clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
2303                       vcm->my_pid, session_index);
2304       return VPPCOM_EBADFD;
2305     }
2306
2307   if (session->state == STATE_DISCONNECT)
2308     {
2309       if (VPPCOM_DEBUG > 0)
2310         clib_warning ("[%d] sid (%u) has been closed by remote peer!",
2311                       vcm->my_pid, session_index);
2312       return VPPCOM_ECONNRESET;
2313     }
2314
2315   if (session->is_listen)
2316     ready = clib_fifo_elts (vcm->client_session_index_fifo);
2317   else
2318     {
2319       rx_fifo = ((!session->is_cut_thru || session->is_server) ?
2320                  session->server_rx_fifo : session->server_tx_fifo);
2321
2322       ready = svm_fifo_max_dequeue (rx_fifo);
2323     }
2324
2325   if (VPPCOM_DEBUG > 3)
2326     clib_warning ("[%d] sid %d, peek %s (%p), ready = %d", vcm->my_pid,
2327                   session_index,
2328                   session->is_server ? "server_rx_fifo" : "server_tx_fifo",
2329                   rx_fifo, ready);
2330   if ((session->vep.ev.events & EPOLLET) && (ready == 0))
2331     session->vep.et_mask |= EPOLLIN;
2332
2333   return ready;
2334 }
2335
2336 int
2337 vppcom_session_write (uint32_t session_index, void *buf, int n)
2338 {
2339   vppcom_main_t *vcm = &vppcom_main;
2340   session_t *session = 0;
2341   svm_fifo_t *tx_fifo;
2342   unix_shared_memory_queue_t *q;
2343   session_fifo_event_t evt;
2344   int rv, n_write;
2345   char *fifo_str;
2346   u32 poll_et;
2347
2348   ASSERT (buf);
2349
2350   clib_spinlock_lock (&vcm->sessions_lockp);
2351   rv = vppcom_session_at_index (session_index, &session);
2352   if (PREDICT_FALSE (rv))
2353     {
2354       clib_spinlock_unlock (&vcm->sessions_lockp);
2355       if (VPPCOM_DEBUG > 0)
2356         clib_warning ("[%d] invalid session, sid (%u) has been closed!",
2357                       vcm->my_pid, session_index);
2358       return rv;
2359     }
2360
2361   if (session->is_vep)
2362     {
2363       clib_spinlock_unlock (&vcm->sessions_lockp);
2364       if (VPPCOM_DEBUG > 0)
2365         clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
2366                       vcm->my_pid, session_index);
2367       return VPPCOM_EBADFD;
2368     }
2369
2370   if (session->state == STATE_DISCONNECT)
2371     {
2372       clib_spinlock_unlock (&vcm->sessions_lockp);
2373       if (VPPCOM_DEBUG > 0)
2374         clib_warning ("[%d] sid (%u) has been closed by remote peer!",
2375                       vcm->my_pid, session_index);
2376       return VPPCOM_ECONNRESET;
2377     }
2378
2379   tx_fifo = ((!session->is_cut_thru || session->is_server) ?
2380              session->server_tx_fifo : session->server_rx_fifo);
2381   fifo_str = ((!session->is_cut_thru || session->is_server) ?
2382               "server_tx_fifo" : "server_rx_fifo");
2383   q = session->vpp_event_queue;
2384   poll_et = EPOLLET & session->vep.ev.events;
2385   clib_spinlock_unlock (&vcm->sessions_lockp);
2386
2387   do
2388     {
2389       n_write = svm_fifo_enqueue_nowait (tx_fifo, n, buf);
2390     }
2391   while (!session->is_nonblocking && (n_write <= 0));
2392
2393   /* If event wasn't set, add one */
2394   if (!session->is_cut_thru && (n_write > 0) && svm_fifo_set_event (tx_fifo))
2395     {
2396       int rval;
2397
2398       /* Fabricate TX event, send to vpp */
2399       evt.fifo = tx_fifo;
2400       evt.event_type = FIFO_EVENT_APP_TX;
2401
2402       rval = vppcom_session_at_index (session_index, &session);
2403       if (PREDICT_FALSE (rval))
2404         {
2405           if (VPPCOM_DEBUG > 1)
2406             clib_warning ("[%d] invalid session, sid (%u) has been closed!",
2407                           vcm->my_pid, session_index);
2408           return rval;
2409         }
2410       ASSERT (q);
2411       unix_shared_memory_queue_add (q, (u8 *) & evt,
2412                                     0 /* do wait for mutex */ );
2413     }
2414
2415   if (poll_et && (n_write <= 0))
2416     {
2417       clib_spinlock_lock (&vcm->sessions_lockp);
2418       session->vep.et_mask |= EPOLLOUT;
2419       clib_spinlock_unlock (&vcm->sessions_lockp);
2420     }
2421
2422   if (VPPCOM_DEBUG > 2)
2423     {
2424       if (n_write == -2)
2425         clib_warning ("[%d] sid %d, FIFO-FULL %s (%p)", vcm->my_pid,
2426                       session_index, fifo_str, tx_fifo);
2427       else
2428         clib_warning ("[%d] sid %d, wrote %d bytes to %s (%p)", vcm->my_pid,
2429                       session_index, n_write, fifo_str, tx_fifo);
2430     }
2431   return (n_write < 0) ? VPPCOM_EAGAIN : n_write;
2432 }
2433
2434 static inline int
2435 vppcom_session_write_ready (session_t * session, u32 session_index)
2436 {
2437   vppcom_main_t *vcm = &vppcom_main;
2438   svm_fifo_t *tx_fifo;
2439   char *fifo_str;
2440   int ready;
2441
2442   /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
2443   if (session->is_vep)
2444     {
2445       clib_spinlock_unlock (&vcm->sessions_lockp);
2446       if (VPPCOM_DEBUG > 0)
2447         clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
2448                       vcm->my_pid, session_index);
2449       return VPPCOM_EBADFD;
2450     }
2451
2452   if (session->state == STATE_DISCONNECT)
2453     {
2454       if (VPPCOM_DEBUG > 0)
2455         clib_warning ("[%d] sid (%u) has been closed by remote peer!",
2456                       vcm->my_pid, session_index);
2457       return VPPCOM_ECONNRESET;
2458     }
2459
2460   tx_fifo = ((!session->is_cut_thru || session->is_server) ?
2461              session->server_tx_fifo : session->server_rx_fifo);
2462   fifo_str = ((!session->is_cut_thru || session->is_server) ?
2463               "server_tx_fifo" : "server_rx_fifo");
2464
2465   ready = svm_fifo_max_enqueue (tx_fifo);
2466
2467   if (VPPCOM_DEBUG > 3)
2468     clib_warning ("[%d] sid %d, peek %s (%p), ready = %d", vcm->my_pid,
2469                   session_index, fifo_str, tx_fifo, ready);
2470   if ((session->vep.ev.events & EPOLLET) && (ready == 0))
2471     session->vep.et_mask |= EPOLLOUT;
2472
2473   return ready;
2474 }
2475
2476 int
2477 vppcom_select (unsigned long n_bits, unsigned long *read_map,
2478                unsigned long *write_map, unsigned long *except_map,
2479                double time_to_wait)
2480 {
2481   vppcom_main_t *vcm = &vppcom_main;
2482   u32 session_index;
2483   session_t *session = 0;
2484   int rv, bits_set = 0;
2485   f64 timeout = clib_time_now (&vcm->clib_time) + time_to_wait;
2486   u32 minbits = clib_max (n_bits, BITS (uword));
2487
2488   ASSERT (sizeof (clib_bitmap_t) == sizeof (long int));
2489
2490   if (read_map)
2491     {
2492       clib_bitmap_validate (vcm->rd_bitmap, minbits);
2493       clib_memcpy (vcm->rd_bitmap, read_map, vec_len (vcm->rd_bitmap));
2494       memset (read_map, 0, vec_len (vcm->rd_bitmap));
2495     }
2496   if (write_map)
2497     {
2498       clib_bitmap_validate (vcm->wr_bitmap, minbits);
2499       clib_memcpy (vcm->wr_bitmap, write_map, vec_len (vcm->wr_bitmap));
2500       memset (write_map, 0, vec_len (vcm->wr_bitmap));
2501     }
2502   if (except_map)
2503     {
2504       clib_bitmap_validate (vcm->ex_bitmap, minbits);
2505       clib_memcpy (vcm->ex_bitmap, except_map, vec_len (vcm->ex_bitmap));
2506       memset (except_map, 0, vec_len (vcm->ex_bitmap));
2507     }
2508
2509   do
2510     {
2511       /* *INDENT-OFF* */
2512       clib_bitmap_foreach (session_index, vcm->rd_bitmap,
2513         ({
2514           clib_spinlock_lock (&vcm->sessions_lockp);
2515           rv = vppcom_session_at_index (session_index, &session);
2516           if (rv < 0)
2517             {
2518               clib_spinlock_unlock (&vcm->sessions_lockp);
2519               if (VPPCOM_DEBUG > 1)
2520                 clib_warning ("[%d] session %d specified in "
2521                               "read_map is closed.", vcm->my_pid,
2522                               session_index);
2523               bits_set = VPPCOM_EBADFD;
2524               goto select_done;
2525             }
2526
2527           rv = vppcom_session_read_ready (session, session_index);
2528           clib_spinlock_unlock (&vcm->sessions_lockp);
2529           if (vcm->ex_bitmap &&
2530               clib_bitmap_get (vcm->ex_bitmap, session_index) && (rv < 0))
2531             {
2532               // TBD: clib_warning
2533               /* coverity[FORWARD_NULL] */
2534               clib_bitmap_set_no_check (except_map, session_index, 1);
2535               bits_set++;
2536             }
2537           else if (rv > 0)
2538             {
2539               // TBD: clib_warning
2540               /* coverity[FORWARD_NULL] */
2541               clib_bitmap_set_no_check (read_map, session_index, 1);
2542               bits_set++;
2543             }
2544         }));
2545
2546       clib_bitmap_foreach (session_index, vcm->wr_bitmap,
2547         ({
2548           clib_spinlock_lock (&vcm->sessions_lockp);
2549           rv = vppcom_session_at_index (session_index, &session);
2550           if (rv < 0)
2551             {
2552               clib_spinlock_unlock (&vcm->sessions_lockp);
2553               if (VPPCOM_DEBUG > 0)
2554                 clib_warning ("[%d] session %d specified in "
2555                               "write_map is closed.", vcm->my_pid,
2556                               session_index);
2557               bits_set = VPPCOM_EBADFD;
2558               goto select_done;
2559             }
2560
2561           rv = vppcom_session_write_ready (session, session_index);
2562           clib_spinlock_unlock (&vcm->sessions_lockp);
2563           if (rv > 0 )
2564             {
2565               // TBD: clib_warning
2566               /* coverity[FORWARD_NULL] */
2567               clib_bitmap_set_no_check (write_map, session_index, 1);
2568               bits_set++;
2569             }
2570         }));
2571
2572       clib_bitmap_foreach (session_index, vcm->ex_bitmap,
2573         ({
2574           clib_spinlock_lock (&vcm->sessions_lockp);
2575           rv = vppcom_session_at_index (session_index, &session);
2576           if (rv < 0)
2577             {
2578               clib_spinlock_unlock (&vcm->sessions_lockp);
2579               if (VPPCOM_DEBUG > 1)
2580                 clib_warning ("[%d] session %d specified in "
2581                               "except_map is closed.", vcm->my_pid,
2582                               session_index);
2583               bits_set = VPPCOM_EBADFD;
2584               goto select_done;
2585             }
2586
2587           rv = vppcom_session_read_ready (session, session_index);
2588           clib_spinlock_unlock (&vcm->sessions_lockp);
2589           if (rv < 0)
2590             {
2591               // TBD: clib_warning
2592               /* coverity[FORWARD_NULL] */
2593               clib_bitmap_set_no_check (except_map, session_index, 1);
2594               bits_set++;
2595             }
2596         }));
2597       /* *INDENT-ON* */
2598     }
2599   while (clib_time_now (&vcm->clib_time) < timeout);
2600
2601 select_done:
2602   return (bits_set);
2603 }
2604
2605 static inline void
2606 vep_verify_epoll_chain (u32 vep_idx)
2607 {
2608   session_t *session;
2609   vppcom_epoll_t *vep;
2610   int rv;
2611   u32 sid;
2612
2613   if (VPPCOM_DEBUG < 1)
2614     return;
2615
2616   /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
2617   rv = vppcom_session_at_index (vep_idx, &session);
2618   if (PREDICT_FALSE (rv))
2619     {
2620       clib_warning ("ERROR: Invalid vep_idx (%u)!", vep_idx);
2621       goto done;
2622     }
2623   if (PREDICT_FALSE (!session->is_vep))
2624     {
2625       clib_warning ("ERROR: vep_idx (%u) is not a vep!", vep_idx);
2626       goto done;
2627     }
2628   if (VPPCOM_DEBUG > 1)
2629     clib_warning ("vep_idx (%u): Dumping epoll chain\n"
2630                   "{\n"
2631                   "   is_vep         = %u\n"
2632                   "   is_vep_session = %u\n"
2633                   "   wait_cont_idx  = 0x%x (%u)\n"
2634                   "}\n",
2635                   vep_idx, session->is_vep, session->is_vep_session,
2636                   session->wait_cont_idx, session->wait_cont_idx);
2637   do
2638     {
2639       vep = &session->vep;
2640       if (session->is_vep_session)
2641         {
2642           if (VPPCOM_DEBUG > 1)
2643             clib_warning ("vep_idx[%u]: sid 0x%x (%u)\n"
2644                           "{\n"
2645                           "   next_sid       = 0x%x (%u)\n"
2646                           "   prev_sid       = 0x%x (%u)\n"
2647                           "   vep_idx        = 0x%x (%u)\n"
2648                           "   ev.events      = 0x%x\n"
2649                           "   ev.data.u64    = 0x%llx\n"
2650                           "   et_mask        = 0x%x\n"
2651                           "}\n",
2652                           vep_idx, sid, sid,
2653                           vep->next_sid, vep->next_sid,
2654                           vep->prev_sid, vep->prev_sid,
2655                           vep->vep_idx, vep->vep_idx,
2656                           vep->ev.events, vep->ev.data.u64, vep->et_mask);
2657         }
2658       sid = vep->next_sid;
2659       if (sid != ~0)
2660         {
2661           rv = vppcom_session_at_index (sid, &session);
2662           if (PREDICT_FALSE (rv))
2663             {
2664               clib_warning ("ERROR: Invalid sid (%u)!", sid);
2665               goto done;
2666             }
2667           if (PREDICT_FALSE (session->is_vep))
2668             clib_warning ("ERROR: sid (%u) is a vep!", vep_idx);
2669           else if (PREDICT_FALSE (!session->is_vep_session))
2670             {
2671               clib_warning ("ERROR: session (%u) is not a vep session!", sid);
2672               goto done;
2673             }
2674           if (PREDICT_FALSE (session->vep.vep_idx != vep_idx))
2675             clib_warning ("ERROR: session (%u) vep_idx (%u) != "
2676                           "vep_idx (%u)!",
2677                           sid, session->vep.vep_idx, vep_idx);
2678         }
2679     }
2680   while (sid != ~0);
2681
2682 done:
2683   if (VPPCOM_DEBUG > 1)
2684     clib_warning ("vep_idx (%u): Dump complete!", vep_idx);
2685 }
2686
2687 int
2688 vppcom_epoll_create (void)
2689 {
2690   vppcom_main_t *vcm = &vppcom_main;
2691   session_t *vep_session;
2692   u32 vep_idx;
2693
2694   clib_spinlock_lock (&vcm->sessions_lockp);
2695   pool_get (vcm->sessions, vep_session);
2696   memset (vep_session, 0, sizeof (*vep_session));
2697   vep_idx = vep_session - vcm->sessions;
2698
2699   vep_session->is_vep = 1;
2700   vep_session->vep.vep_idx = ~0;
2701   vep_session->vep.next_sid = ~0;
2702   vep_session->vep.prev_sid = ~0;
2703   vep_session->wait_cont_idx = ~0;
2704   clib_spinlock_unlock (&vcm->sessions_lockp);
2705
2706   if (VPPCOM_DEBUG > 0)
2707     clib_warning ("Created vep_idx %u!", vep_idx);
2708
2709   return (vep_idx);
2710 }
2711
2712 int
2713 vppcom_epoll_ctl (uint32_t vep_idx, int op, uint32_t session_index,
2714                   struct epoll_event *event)
2715 {
2716   vppcom_main_t *vcm = &vppcom_main;
2717   session_t *vep_session;
2718   session_t *session;
2719   int rv;
2720
2721   if (vep_idx == session_index)
2722     {
2723       if (VPPCOM_DEBUG > 0)
2724         clib_warning ("ERROR: vep_idx == session_index (%u)!", vep_idx);
2725       return VPPCOM_EINVAL;
2726     }
2727
2728   clib_spinlock_lock (&vcm->sessions_lockp);
2729   rv = vppcom_session_at_index (vep_idx, &vep_session);
2730   if (PREDICT_FALSE (rv))
2731     {
2732       if (VPPCOM_DEBUG > 0)
2733         clib_warning ("ERROR: Invalid vep_idx (%u)!", vep_idx);
2734       goto done;
2735     }
2736   if (PREDICT_FALSE (!vep_session->is_vep))
2737     {
2738       if (VPPCOM_DEBUG > 0)
2739         clib_warning ("ERROR: vep_idx (%u) is not a vep!", vep_idx);
2740       rv = VPPCOM_EINVAL;
2741       goto done;
2742     }
2743
2744   ASSERT (vep_session->vep.vep_idx == ~0);
2745   ASSERT (vep_session->vep.prev_sid == ~0);
2746
2747   rv = vppcom_session_at_index (session_index, &session);
2748   if (PREDICT_FALSE (rv))
2749     {
2750       if (VPPCOM_DEBUG > 0)
2751         clib_warning ("ERROR: Invalid session_index (%u)!", session_index);
2752       goto done;
2753     }
2754   if (PREDICT_FALSE (session->is_vep))
2755     {
2756       if (VPPCOM_DEBUG > 0)
2757         clib_warning ("ERROR: session_index (%u) is a vep!", vep_idx);
2758       rv = VPPCOM_EINVAL;
2759       goto done;
2760     }
2761
2762   switch (op)
2763     {
2764     case EPOLL_CTL_ADD:
2765       if (PREDICT_FALSE (!event))
2766         {
2767           clib_warning ("NULL pointer to epoll_event structure!");
2768           rv = VPPCOM_EINVAL;
2769           goto done;
2770         }
2771       if (vep_session->vep.next_sid != ~0)
2772         {
2773           session_t *next_session;
2774           rv = vppcom_session_at_index (vep_session->vep.next_sid,
2775                                         &next_session);
2776           if (PREDICT_FALSE (rv))
2777             {
2778               if (VPPCOM_DEBUG > 0)
2779                 clib_warning ("EPOLL_CTL_ADD: Invalid vep.next_sid (%u) on"
2780                               " vep_idx (%u)!", vep_session->vep.next_sid,
2781                               vep_idx);
2782               goto done;
2783             }
2784           ASSERT (next_session->vep.prev_sid == vep_idx);
2785           next_session->vep.prev_sid = session_index;
2786         }
2787       session->vep.next_sid = vep_session->vep.next_sid;
2788       session->vep.prev_sid = vep_idx;
2789       session->vep.vep_idx = vep_idx;
2790       session->vep.et_mask = VEP_DEFAULT_ET_MASK;
2791       session->vep.ev = *event;
2792       session->is_vep_session = 1;
2793       vep_session->vep.next_sid = session_index;
2794       if (VPPCOM_DEBUG > 1)
2795         clib_warning ("EPOLL_CTL_ADD: vep_idx %u, sid %u, events 0x%x,"
2796                       " data 0x%llx!", vep_idx, session_index,
2797                       event->events, event->data.u64);
2798       break;
2799
2800     case EPOLL_CTL_MOD:
2801       if (PREDICT_FALSE (!event))
2802         {
2803           clib_warning ("NULL pointer to epoll_event structure!");
2804           rv = VPPCOM_EINVAL;
2805           goto done;
2806         }
2807       if (PREDICT_FALSE (!session->is_vep_session &&
2808                          (session->vep.vep_idx != vep_idx)))
2809         {
2810           if (VPPCOM_DEBUG > 0)
2811             {
2812               if (!session->is_vep_session)
2813                 clib_warning ("EPOLL_CTL_MOD: session (%u) is not "
2814                               "a vep session!", session_index);
2815               else
2816                 clib_warning ("EPOLL_CTL_MOD: session (%u) vep_idx (%u) != "
2817                               "vep_idx (%u)!", session_index,
2818                               session->vep.vep_idx, vep_idx);
2819             }
2820           rv = VPPCOM_EINVAL;
2821           goto done;
2822         }
2823       session->vep.et_mask = VEP_DEFAULT_ET_MASK;
2824       session->vep.ev = *event;
2825       if (VPPCOM_DEBUG > 1)
2826         clib_warning ("EPOLL_CTL_MOD: vep_idx %u, sid %u, events 0x%x,"
2827                       " data 0x%llx!", vep_idx, session_index,
2828                       event->events, event->data.u64);
2829       break;
2830
2831     case EPOLL_CTL_DEL:
2832       if (PREDICT_FALSE (!session->is_vep_session &&
2833                          (session->vep.vep_idx != vep_idx)))
2834         {
2835           if (VPPCOM_DEBUG > 0)
2836             {
2837               if (!session->is_vep_session)
2838                 clib_warning ("EPOLL_CTL_DEL: session (%u) is not "
2839                               "a vep session!", session_index);
2840               else
2841                 clib_warning ("EPOLL_CTL_DEL: session (%u) vep_idx (%u) != "
2842                               "vep_idx (%u)!", session_index,
2843                               session->vep.vep_idx, vep_idx);
2844             }
2845           rv = VPPCOM_EINVAL;
2846           goto done;
2847         }
2848
2849       vep_session->wait_cont_idx =
2850         (vep_session->wait_cont_idx == session_index) ?
2851         session->vep.next_sid : vep_session->wait_cont_idx;
2852
2853       if (session->vep.prev_sid == vep_idx)
2854         vep_session->vep.next_sid = session->vep.next_sid;
2855       else
2856         {
2857           session_t *prev_session;
2858           rv = vppcom_session_at_index (session->vep.prev_sid, &prev_session);
2859           if (PREDICT_FALSE (rv))
2860             {
2861               if (VPPCOM_DEBUG > 0)
2862                 clib_warning ("EPOLL_CTL_DEL: Invalid vep.prev_sid (%u) on"
2863                               " sid (%u)!", session->vep.prev_sid,
2864                               session_index);
2865               goto done;
2866             }
2867           ASSERT (prev_session->vep.next_sid == session_index);
2868           prev_session->vep.next_sid = session->vep.next_sid;
2869         }
2870       if (session->vep.next_sid != ~0)
2871         {
2872           session_t *next_session;
2873           rv = vppcom_session_at_index (session->vep.next_sid, &next_session);
2874           if (PREDICT_FALSE (rv))
2875             {
2876               if (VPPCOM_DEBUG > 0)
2877                 clib_warning ("EPOLL_CTL_DEL: Invalid vep.next_sid (%u) on"
2878                               " sid (%u)!", session->vep.next_sid,
2879                               session_index);
2880               goto done;
2881             }
2882           ASSERT (next_session->vep.prev_sid == session_index);
2883           next_session->vep.prev_sid = session->vep.prev_sid;
2884         }
2885
2886       memset (&session->vep, 0, sizeof (session->vep));
2887       session->vep.next_sid = ~0;
2888       session->vep.prev_sid = ~0;
2889       session->vep.vep_idx = ~0;
2890       session->is_vep_session = 0;
2891       if (VPPCOM_DEBUG > 1)
2892         clib_warning ("EPOLL_CTL_DEL: vep_idx %u, sid %u!", vep_idx,
2893                       session_index);
2894       break;
2895
2896     default:
2897       clib_warning ("Invalid operation (%d)!", op);
2898       rv = VPPCOM_EINVAL;
2899     }
2900
2901   vep_verify_epoll_chain (vep_idx);
2902
2903 done:
2904   clib_spinlock_unlock (&vcm->sessions_lockp);
2905   return rv;
2906 }
2907
2908 #define VCL_LOCK_AND_GET_SESSION(I, S)                  \
2909 do {                                                    \
2910   vppcom_main_t *vcm = &vppcom_main;                    \
2911                                                         \
2912   clib_spinlock_lock (&vcm->sessions_lockp);            \
2913   rv = vppcom_session_at_index (I, S);                  \
2914   if (PREDICT_FALSE (rv))                               \
2915     {                                                   \
2916       clib_spinlock_unlock (&vcm->sessions_lockp);      \
2917                                                         \
2918       if (VPPCOM_DEBUG > 0)                             \
2919         clib_warning ("ERROR: Invalid ##I (%u)!", I);   \
2920                                                         \
2921       goto done;                                        \
2922     }                                                   \
2923 } while (0)
2924
2925 int
2926 vppcom_epoll_wait (uint32_t vep_idx, struct epoll_event *events,
2927                    int maxevents, double wait_for_time)
2928 {
2929   vppcom_main_t *vcm = &vppcom_main;
2930   session_t *vep_session;
2931   int rv;
2932   f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
2933   int num_ev = 0;
2934   u32 vep_next_sid, wait_cont_idx;
2935   u8 is_vep;
2936
2937   if (PREDICT_FALSE (maxevents <= 0))
2938     {
2939       if (VPPCOM_DEBUG > 0)
2940         clib_warning ("ERROR: Invalid maxevents (%d)!", maxevents);
2941       return VPPCOM_EINVAL;
2942     }
2943   if (PREDICT_FALSE (wait_for_time < 0))
2944     {
2945       if (VPPCOM_DEBUG > 0)
2946         clib_warning ("ERROR: Invalid wait_for_time (%f)!", wait_for_time);
2947       return VPPCOM_EINVAL;
2948     }
2949   memset (events, 0, sizeof (*events) * maxevents);
2950
2951   VCL_LOCK_AND_GET_SESSION (vep_idx, &vep_session);
2952   vep_next_sid = vep_session->vep.next_sid;
2953   is_vep = vep_session->is_vep;
2954   wait_cont_idx = vep_session->wait_cont_idx;
2955   clib_spinlock_unlock (&vcm->sessions_lockp);
2956
2957   if (PREDICT_FALSE (!is_vep))
2958     {
2959       if (VPPCOM_DEBUG > 0)
2960         clib_warning ("ERROR: vep_idx (%u) is not a vep!", vep_idx);
2961       rv = VPPCOM_EINVAL;
2962       goto done;
2963     }
2964   if ((VPPCOM_DEBUG > 0) && (PREDICT_FALSE (vep_next_sid == ~0)))
2965     {
2966       clib_warning ("WARNING: vep_idx (%u) is empty!", vep_idx);
2967       goto done;
2968     }
2969
2970   do
2971     {
2972       u32 sid;
2973       u32 next_sid = ~0;
2974       session_t *session;
2975
2976       for (sid = (wait_cont_idx == ~0) ? vep_next_sid : wait_cont_idx;
2977            sid != ~0; sid = next_sid)
2978         {
2979           u32 session_events, et_mask, clear_et_mask, session_vep_idx;
2980           u8 add_event, is_vep_session;
2981           int ready;
2982           u64 session_ev_data;
2983
2984           VCL_LOCK_AND_GET_SESSION (sid, &session);
2985           next_sid = session->vep.next_sid;
2986           session_events = session->vep.ev.events;
2987           et_mask = session->vep.et_mask;
2988           is_vep = session->is_vep;
2989           is_vep_session = session->is_vep_session;
2990           session_vep_idx = session->vep.vep_idx;
2991           session_ev_data = session->vep.ev.data.u64;
2992           clib_spinlock_unlock (&vcm->sessions_lockp);
2993
2994           if (PREDICT_FALSE (is_vep))
2995             {
2996               if (VPPCOM_DEBUG > 0)
2997                 clib_warning ("ERROR: sid (%u) is a vep!", vep_idx);
2998               rv = VPPCOM_EINVAL;
2999               goto done;
3000             }
3001           if (PREDICT_FALSE (!is_vep_session))
3002             {
3003               if (VPPCOM_DEBUG > 0)
3004                 clib_warning ("EPOLL_CTL_MOD: session (%u) is not "
3005                               "a vep session!", sid);
3006               rv = VPPCOM_EINVAL;
3007               goto done;
3008             }
3009           if (PREDICT_FALSE (session_vep_idx != vep_idx))
3010             {
3011               clib_warning ("EPOLL_CTL_MOD: session (%u) "
3012                             "vep_idx (%u) != vep_idx (%u)!",
3013                             sid, session->vep.vep_idx, vep_idx);
3014               rv = VPPCOM_EINVAL;
3015               goto done;
3016             }
3017
3018           add_event = clear_et_mask = 0;
3019
3020           if ((EPOLLIN & session_events) && (EPOLLIN & et_mask))
3021             {
3022               VCL_LOCK_AND_GET_SESSION (sid, &session);
3023               ready = vppcom_session_read_ready (session, sid);
3024               clib_spinlock_unlock (&vcm->sessions_lockp);
3025               if (ready > 0)
3026                 {
3027                   add_event = 1;
3028                   events[num_ev].events |= EPOLLIN;
3029                   if (EPOLLET & session_events)
3030                     clear_et_mask |= EPOLLIN;
3031                 }
3032               else if (ready < 0)
3033                 {
3034                   add_event = 1;
3035                   switch (ready)
3036                     {
3037                     case VPPCOM_ECONNRESET:
3038                       events[num_ev].events |= EPOLLHUP | EPOLLRDHUP;
3039                       break;
3040
3041                     default:
3042                       events[num_ev].events |= EPOLLERR;
3043                       break;
3044                     }
3045                 }
3046             }
3047
3048           if ((EPOLLOUT & session_events) && (EPOLLOUT & et_mask))
3049             {
3050               VCL_LOCK_AND_GET_SESSION (sid, &session);
3051               ready = vppcom_session_write_ready (session, sid);
3052               clib_spinlock_unlock (&vcm->sessions_lockp);
3053               if (ready > 0)
3054                 {
3055                   add_event = 1;
3056                   events[num_ev].events |= EPOLLOUT;
3057                   if (EPOLLET & session_events)
3058                     clear_et_mask |= EPOLLOUT;
3059                 }
3060               else if (ready < 0)
3061                 {
3062                   add_event = 1;
3063                   switch (ready)
3064                     {
3065                     case VPPCOM_ECONNRESET:
3066                       events[num_ev].events |= EPOLLHUP;
3067                       break;
3068
3069                     default:
3070                       events[num_ev].events |= EPOLLERR;
3071                       break;
3072                     }
3073                 }
3074             }
3075
3076           if (add_event)
3077             {
3078               events[num_ev].data.u64 = session_ev_data;
3079               if (EPOLLONESHOT & session_events)
3080                 {
3081                   VCL_LOCK_AND_GET_SESSION (sid, &session);
3082                   session->vep.ev.events = 0;
3083                   clib_spinlock_unlock (&vcm->sessions_lockp);
3084                 }
3085               num_ev++;
3086               if (num_ev == maxevents)
3087                 {
3088                   VCL_LOCK_AND_GET_SESSION (vep_idx, &vep_session);
3089                   vep_session->wait_cont_idx = next_sid;
3090                   clib_spinlock_unlock (&vcm->sessions_lockp);
3091                   goto done;
3092                 }
3093             }
3094           if (wait_cont_idx != ~0)
3095             {
3096               if (next_sid == ~0)
3097                 next_sid = vep_next_sid;
3098               else if (next_sid == wait_cont_idx)
3099                 next_sid = ~0;
3100             }
3101         }
3102     }
3103   while ((num_ev == 0) && (clib_time_now (&vcm->clib_time) <= timeout));
3104
3105   if (wait_cont_idx != ~0)
3106     {
3107       VCL_LOCK_AND_GET_SESSION (vep_idx, &vep_session);
3108       vep_session->wait_cont_idx = ~0;
3109       clib_spinlock_unlock (&vcm->sessions_lockp);
3110     }
3111 done:
3112   return (rv != VPPCOM_OK) ? rv : num_ev;
3113 }
3114
3115 int
3116 vppcom_session_attr (uint32_t session_index, uint32_t op,
3117                      void *buffer, uint32_t * buflen)
3118 {
3119   vppcom_main_t *vcm = &vppcom_main;
3120   session_t *session;
3121   int rv = VPPCOM_OK;
3122   u32 *flags = buffer;
3123   vppcom_endpt_t *ep = buffer;
3124
3125   VCL_LOCK_AND_GET_SESSION (session_index, &session);
3126   switch (op)
3127     {
3128     case VPPCOM_ATTR_GET_NREAD:
3129       rv = vppcom_session_read_ready (session, session_index);
3130       if (VPPCOM_DEBUG > 0)
3131         clib_warning ("VPPCOM_ATTR_GET_NREAD: nread = %d", rv);
3132
3133       break;
3134
3135     case VPPCOM_ATTR_PEEK_NREAD:
3136       /* TBD */
3137       break;
3138
3139     case VPPCOM_ATTR_GET_FLAGS:
3140       if (buffer && buflen && (*buflen >= sizeof (*flags)))
3141         {
3142           *flags = O_RDWR | ((session->is_nonblocking) ? O_NONBLOCK : 0);
3143           *buflen = sizeof (*flags);
3144           if (VPPCOM_DEBUG > 0)
3145             clib_warning ("VPPCOM_ATTR_GET_FLAGS: flags = 0x%08x, "
3146                           "is_nonblocking = %u", *flags,
3147                           session->is_nonblocking);
3148         }
3149       else
3150         rv = VPPCOM_EINVAL;
3151       break;
3152
3153     case VPPCOM_ATTR_SET_FLAGS:
3154       if (buffer && buflen && (*buflen >= sizeof (*flags)))
3155         {
3156           session->is_nonblocking = (*flags & O_NONBLOCK) ? 1 : 0;
3157           if (VPPCOM_DEBUG > 0)
3158             clib_warning ("VPPCOM_ATTR_SET_FLAGS: flags = 0x%08x, "
3159                           "is_nonblocking = %u", *flags,
3160                           session->is_nonblocking);
3161         }
3162       else
3163         rv = VPPCOM_EINVAL;
3164       break;
3165
3166     case VPPCOM_ATTR_GET_PEER_ADDR:
3167       if (buffer && buflen && (*buflen >= sizeof (*ep)))
3168         {
3169           ep->vrf = session->vrf;
3170           ep->is_ip4 = session->peer_addr.is_ip4;
3171           ep->port = session->port;
3172           if (session->peer_addr.is_ip4)
3173             clib_memcpy (ep->ip, &session->peer_addr.ip46.ip4,
3174                          sizeof (ip4_address_t));
3175           else
3176             clib_memcpy (ep->ip, &session->peer_addr.ip46.ip6,
3177                          sizeof (ip6_address_t));
3178           *buflen = sizeof (*ep);
3179           if (VPPCOM_DEBUG > 0)
3180             clib_warning ("VPPCOM_ATTR_GET_PEER_ADDR: is_ip4 = %u, "
3181                           "addr = %U", ep->is_ip4, format_ip46_address,
3182                           &session->peer_addr.ip46, ep->is_ip4);
3183         }
3184       else
3185         rv = VPPCOM_EINVAL;
3186       break;
3187
3188     case VPPCOM_ATTR_GET_LCL_ADDR:
3189       if (buffer && buflen && (*buflen >= sizeof (*ep)))
3190         {
3191           ep->vrf = session->vrf;
3192           ep->is_ip4 = session->lcl_addr.is_ip4;
3193           ep->port = session->port;
3194           if (session->lcl_addr.is_ip4)
3195             clib_memcpy (ep->ip, &session->lcl_addr.ip46.ip4,
3196                          sizeof (ip4_address_t));
3197           else
3198             clib_memcpy (ep->ip, &session->lcl_addr.ip46.ip6,
3199                          sizeof (ip6_address_t));
3200           *buflen = sizeof (*ep);
3201           if (VPPCOM_DEBUG > 0)
3202             if (VPPCOM_DEBUG > 0)
3203               clib_warning ("VPPCOM_ATTR_GET_LCL_ADDR: is_ip4 = %u, "
3204                             "addr = %U", ep->is_ip4, format_ip46_address,
3205                             &session->lcl_addr.ip46, ep->is_ip4);
3206         }
3207       else
3208         rv = VPPCOM_EINVAL;
3209       break;
3210
3211     case VPPCOM_ATTR_SET_REUSEADDR:
3212       break;
3213
3214     case VPPCOM_ATTR_SET_BROADCAST:
3215       break;
3216
3217     case VPPCOM_ATTR_SET_V6ONLY:
3218       break;
3219
3220     case VPPCOM_ATTR_SET_KEEPALIVE:
3221       break;
3222
3223     case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
3224       break;
3225
3226     case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
3227       break;
3228     }
3229
3230 done:
3231   clib_spinlock_unlock (&vcm->sessions_lockp);
3232   return rv;
3233 }
3234
3235   /*
3236    * fd.io coding-style-patch-verification: ON
3237    *
3238    * Local Variables:
3239    * eval: (c-set-style "gnu")
3240    * End:
3241    */