VCL: Fix for vppcom_session_listen() q_len <= 0 crash
[vpp.git] / src / vcl / 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 <vcl/vppcom.h>
24 #include <vlib/unix/unix.h>
25 #include <vppinfra/vec_bootstrap.h>
26 #include <vppinfra/elog.h>
27
28 #define vl_typedefs             /* define message structures */
29 #include <vpp/api/vpe_all_api_h.h>
30 #undef vl_typedefs
31
32 /* declare message handlers for each api */
33
34 #define vl_endianfun            /* define message structures */
35 #include <vpp/api/vpe_all_api_h.h>
36 #undef vl_endianfun
37
38 /* instantiate all the print functions we know about */
39 #define vl_print(handle, ...)
40 #define vl_printfun
41 #include <vpp/api/vpe_all_api_h.h>
42 #undef vl_printfun
43
44 #if (CLIB_DEBUG > 0)
45 /* Set VPPCOM_DEBUG_INIT 2 for connection debug,
46  *                       3 for read/write debug output
47  * or
48  *    export VCL_DEBUG=<#> to set dynamically.
49  */
50 #define VPPCOM_DEBUG_INIT 1
51 #else
52 #define VPPCOM_DEBUG_INIT 0
53 #endif
54
55 #define VPPCOM_DEBUG vcm->debug
56
57 /*
58  * VPPCOM Private definitions and functions.
59  */
60 typedef enum
61 {
62   STATE_APP_START,
63   STATE_APP_CONN_VPP,
64   STATE_APP_ENABLED,
65   STATE_APP_ATTACHED,
66 } app_state_t;
67
68 typedef enum
69 {
70   STATE_START = 0x01,
71   STATE_CONNECT = 0x02,
72   STATE_LISTEN = 0x04,
73   STATE_ACCEPT = 0x08,
74   STATE_CLOSE_ON_EMPTY = 0x10,
75   STATE_DISCONNECT = 0x20,
76   STATE_FAILED = 0x40
77 } session_state_t;
78
79 #define SERVER_STATE_OPEN  (STATE_ACCEPT|STATE_CLOSE_ON_EMPTY)
80 #define CLIENT_STATE_OPEN  (STATE_CONNECT|STATE_CLOSE_ON_EMPTY)
81
82 typedef struct epoll_event vppcom_epoll_event_t;
83
84 typedef struct
85 {
86   u32 next_sid;
87   u32 prev_sid;
88   u32 vep_idx;
89   vppcom_epoll_event_t ev;
90 #define VEP_DEFAULT_ET_MASK  (EPOLLIN|EPOLLOUT)
91 #define VEP_UNSUPPORTED_EVENTS (EPOLLONESHOT|EPOLLEXCLUSIVE)
92   u32 et_mask;
93 } vppcom_epoll_t;
94
95 typedef struct
96 {
97   u8 is_ip4;
98   ip46_address_t ip46;
99 } vppcom_ip46_t;
100
101 enum
102 {
103   VCL_SESS_ATTR_SERVER,
104   VCL_SESS_ATTR_CUT_THRU,
105   VCL_SESS_ATTR_VEP,
106   VCL_SESS_ATTR_VEP_SESSION,
107   VCL_SESS_ATTR_LISTEN,         // SOL_SOCKET,SO_ACCEPTCONN
108   VCL_SESS_ATTR_NONBLOCK,       // fcntl,O_NONBLOCK
109   VCL_SESS_ATTR_REUSEADDR,      // SOL_SOCKET,SO_REUSEADDR
110   VCL_SESS_ATTR_REUSEPORT,      // SOL_SOCKET,SO_REUSEPORT
111   VCL_SESS_ATTR_BROADCAST,      // SOL_SOCKET,SO_BROADCAST
112   VCL_SESS_ATTR_V6ONLY,         // SOL_TCP,IPV6_V6ONLY
113   VCL_SESS_ATTR_KEEPALIVE,      // SOL_SOCKET,SO_KEEPALIVE
114   VCL_SESS_ATTR_TCP_NODELAY,    // SOL_TCP,TCP_NODELAY
115   VCL_SESS_ATTR_TCP_KEEPIDLE,   // SOL_TCP,TCP_KEEPIDLE
116   VCL_SESS_ATTR_TCP_KEEPINTVL,  // SOL_TCP,TCP_KEEPINTVL
117   VCL_SESS_ATTR_MAX
118 } vppcom_session_attr_t;
119
120 #define VCL_SESS_ATTR_SET(ATTR, VAL)            \
121 do {                                            \
122   (ATTR) |= 1 << (VAL);                         \
123  } while (0)
124
125 #define VCL_SESS_ATTR_CLR(ATTR, VAL)            \
126 do {                                            \
127   (ATTR) &= ~(1 << (VAL));                      \
128  } while (0)
129
130 #define VCL_SESS_ATTR_TEST(ATTR, VAL)           \
131   ((ATTR) & (1 << (VAL)) ? 1 : 0)
132
133 typedef struct
134 {
135   volatile session_state_t state;
136
137   svm_fifo_t *rx_fifo;
138   svm_fifo_t *tx_fifo;
139   u32 sndbuf_size;              // VPP-TBD: Hack until support setsockopt(SO_SNDBUF)
140   u32 rcvbuf_size;              // VPP-TBD: Hack until support setsockopt(SO_RCVBUF)
141   u32 user_mss;                 // VPP-TBD: Hack until support setsockopt(TCP_MAXSEG)
142   u8 *segment_name;
143   u32 sm_seg_index;
144   u32 client_context;
145   u64 vpp_handle;
146   svm_queue_t *vpp_event_queue;
147
148   /* Socket configuration state */
149   u8 is_vep;
150   u8 is_vep_session;
151   u32 attr;
152   u32 wait_cont_idx;
153   vppcom_epoll_t vep;
154   int libc_epfd;
155   vppcom_ip46_t lcl_addr;
156   vppcom_ip46_t peer_addr;
157   u16 lcl_port;                 // network order
158   u16 peer_port;                // network order
159   u8 proto;
160   u64 client_queue_address;
161   u64 options[16];
162   elog_track_t elog_track;
163 } session_t;
164
165 typedef struct vppcom_cfg_t_
166 {
167   u64 heapsize;
168   u32 vpp_api_q_length;
169   u64 segment_baseva;
170   u32 segment_size;
171   u32 add_segment_size;
172   u32 preallocated_fifo_pairs;
173   u32 rx_fifo_size;
174   u32 tx_fifo_size;
175   u32 event_queue_size;
176   u32 listen_queue_size;
177   u8 app_proxy_transport_tcp;
178   u8 app_proxy_transport_udp;
179   u8 app_scope_local;
180   u8 app_scope_global;
181   u8 *namespace_id;
182   u64 namespace_secret;
183   f64 app_timeout;
184   f64 session_timeout;
185   f64 accept_timeout;
186   u32 event_ring_size;
187   char *event_log_path;
188   u8 *vpp_api_filename;
189 } vppcom_cfg_t;
190
191 typedef struct vppcom_main_t_
192 {
193   u8 init;
194   u32 debug;
195   u32 *client_session_index_fifo;
196   int main_cpu;
197
198   /* vpp input queue */
199   svm_queue_t *vl_input_queue;
200
201   /* API client handle */
202   u32 my_client_index;
203
204   /* Session pool */
205   clib_spinlock_t sessions_lockp;
206   session_t *sessions;
207
208   /* Hash table for disconnect processing */
209   uword *session_index_by_vpp_handles;
210
211   /* Select bitmaps */
212   clib_bitmap_t *rd_bitmap;
213   clib_bitmap_t *wr_bitmap;
214   clib_bitmap_t *ex_bitmap;
215
216   /* Our event queue */
217   svm_queue_t *app_event_queue;
218
219   /* unique segment name counter */
220   u32 unique_segment_index;
221
222   /* For deadman timers */
223   clib_time_t clib_time;
224
225   /* State of the connection, shared between msg RX thread and main thread */
226   volatile app_state_t app_state;
227
228   vppcom_cfg_t cfg;
229
230   /* Event logging */
231   elog_main_t elog_main;
232   elog_track_t elog_track;
233
234   /* VNET_API_ERROR_FOO -> "Foo" hash table */
235   uword *error_string_by_error_number;
236 } vppcom_main_t;
237
238 /* NOTE: _vppcom_main is only used until the heap is allocated.
239  *       Do not access it directly -- use vcm which will point to
240  *       the heap allocated copy after init.
241  */
242 static vppcom_main_t _vppcom_main = {
243   .debug = VPPCOM_DEBUG_INIT,
244   .my_client_index = ~0
245 };
246
247 static vppcom_main_t *vcm = &_vppcom_main;
248
249 #define VCL_LOCK_AND_GET_SESSION(I, S)                          \
250 do {                                                            \
251   clib_spinlock_lock (&vcm->sessions_lockp);                    \
252   rv = vppcom_session_at_index (I, S);                          \
253   if (PREDICT_FALSE (rv))                                       \
254     {                                                           \
255       clib_spinlock_unlock (&vcm->sessions_lockp);              \
256       clib_warning ("VCL<%d>: ERROR: Invalid ##I (%u)!",        \
257                     getpid (), I);                              \
258       goto done;                                                \
259     }                                                           \
260 } while (0)
261
262 static const char *
263 vppcom_app_state_str (app_state_t state)
264 {
265   char *st;
266
267   switch (state)
268     {
269     case STATE_APP_START:
270       st = "STATE_APP_START";
271       break;
272
273     case STATE_APP_CONN_VPP:
274       st = "STATE_APP_CONN_VPP";
275       break;
276
277     case STATE_APP_ENABLED:
278       st = "STATE_APP_ENABLED";
279       break;
280
281     case STATE_APP_ATTACHED:
282       st = "STATE_APP_ATTACHED";
283       break;
284
285     default:
286       st = "UNKNOWN_APP_STATE";
287       break;
288     }
289
290   return st;
291 }
292
293 static const char *
294 vppcom_session_state_str (session_state_t state)
295 {
296   char *st;
297
298   switch (state)
299     {
300     case STATE_START:
301       st = "STATE_START";
302       break;
303
304     case STATE_CONNECT:
305       st = "STATE_CONNECT";
306       break;
307
308     case STATE_LISTEN:
309       st = "STATE_LISTEN";
310       break;
311
312     case STATE_ACCEPT:
313       st = "STATE_ACCEPT";
314       break;
315
316     case STATE_CLOSE_ON_EMPTY:
317       st = "STATE_CLOSE_ON_EMPTY";
318       break;
319
320     case STATE_DISCONNECT:
321       st = "STATE_DISCONNECT";
322       break;
323
324     case STATE_FAILED:
325       st = "STATE_FAILED";
326       break;
327
328     default:
329       st = "UNKNOWN_STATE";
330       break;
331     }
332
333   return st;
334 }
335
336 /*
337  * VPPCOM Utility Functions
338  */
339 static inline int
340 vppcom_session_at_index (u32 session_index, session_t * volatile *sess)
341 {
342   /* Assumes that caller has acquired spinlock: vcm->sessions_lockp */
343   if (PREDICT_FALSE ((session_index == ~0) ||
344                      pool_is_free_index (vcm->sessions, session_index)))
345     {
346       clib_warning ("VCL<%d>: invalid session, sid (%u) has been closed!",
347                     getpid (), session_index);
348       return VPPCOM_EBADFD;
349     }
350   *sess = pool_elt_at_index (vcm->sessions, session_index);
351   return VPPCOM_OK;
352 }
353
354 static inline void
355 vppcom_session_table_add_listener (u64 listener_handle, u32 value)
356 {
357   /* Session and listener handles have different formats. The latter has
358    * the thread index in the upper 32 bits while the former has the session
359    * type. Knowing that, for listeners we just flip the MSB to 1 */
360   listener_handle |= 1ULL << 63;
361   hash_set (vcm->session_index_by_vpp_handles, listener_handle, value);
362 }
363
364 static inline session_t *
365 vppcom_session_table_lookup_listener (u64 listener_handle)
366 {
367   uword *p;
368   u64 handle = listener_handle | (1ULL << 63);
369   session_t *session;
370
371   p = hash_get (vcm->session_index_by_vpp_handles, handle);
372   if (!p)
373     {
374       clib_warning ("VCL<%d>: couldn't find listen session: unknown vpp "
375                     "listener handle %llx", getpid (), listener_handle);
376       return 0;
377     }
378   if (pool_is_free_index (vcm->sessions, p[0]))
379     {
380       if (VPPCOM_DEBUG > 1)
381         clib_warning ("VCL<%d>: invalid listen session, sid (%u)",
382                       getpid (), p[0]);
383       return 0;
384     }
385
386   session = pool_elt_at_index (vcm->sessions, p[0]);
387   ASSERT (session->state & STATE_LISTEN);
388   return session;
389 }
390
391 static inline void
392 vppcom_session_table_del_listener (u64 listener_handle)
393 {
394   listener_handle |= 1ULL << 63;
395   hash_unset (vcm->session_index_by_vpp_handles, listener_handle);
396 }
397
398 static void
399 write_elog (void)
400 {
401   elog_main_t *em = &vcm->elog_main;
402   char *chroot_file;
403   clib_error_t *error = 0;
404
405   chroot_file =
406     (char *) format (0, "%s/%d-%d-vcl-elog%c", vcm->cfg.event_log_path,
407                      vcm->my_client_index, getpid (), 0);
408   error = elog_write_file (em, chroot_file, 1 /* flush ring */ );
409   if (error)
410     {
411       clib_error_report (error);
412     }
413   if (VPPCOM_DEBUG > 0)
414     clib_warning ("[%d] Event Log:'%s' ", getpid (), chroot_file);
415
416 }
417
418 static int
419 vppcom_connect_to_vpp (char *app_name)
420 {
421   api_main_t *am = &api_main;
422   vppcom_cfg_t *vcl_cfg = &vcm->cfg;
423   int rv = VPPCOM_OK;
424
425   if (!vcl_cfg->vpp_api_filename)
426     vcl_cfg->vpp_api_filename = format (0, "/vpe-api%c", 0);
427
428   if (VPPCOM_DEBUG > 0)
429     clib_warning ("VCL<%d>: app (%s) connecting to VPP api (%s)...",
430                   getpid (), app_name, vcl_cfg->vpp_api_filename);
431
432   if (vl_client_connect_to_vlib ((char *) vcl_cfg->vpp_api_filename, app_name,
433                                  vcm->cfg.vpp_api_q_length) < 0)
434     {
435       clib_warning ("VCL<%d>: app (%s) connect failed!", getpid (), app_name);
436       rv = VPPCOM_ECONNREFUSED;
437     }
438   else
439     {
440       vcm->vl_input_queue = am->shmem_hdr->vl_input_queue;
441       vcm->my_client_index = am->my_client_index;
442       vcm->app_state = STATE_APP_CONN_VPP;
443
444       if (VPPCOM_DEBUG > 0)
445         clib_warning ("VCL<%d>: app (%s) is connected to VPP!",
446                       getpid (), app_name);
447     }
448
449   if (VPPCOM_DEBUG > 0)
450     {
451       vcm->elog_main.lock =
452         clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, CLIB_CACHE_LINE_BYTES);
453       vcm->elog_main.lock[0] = 0;
454       vcm->elog_main.event_ring_size = vcm->cfg.event_ring_size;
455       elog_init (&vcm->elog_main, vcm->elog_main.event_ring_size);
456       elog_enable_disable (&vcm->elog_main, 1);
457
458       vcm->elog_track.name =
459         (char *) format (0, "P:%d:C:%d%c", getpid (),
460                          vcm->my_client_index, 0);
461       elog_track_register (&vcm->elog_main, &vcm->elog_track);
462
463       /* *INDENT-OFF* */
464       ELOG_TYPE_DECLARE (e) =
465       {
466         .format = "connect_vpp:rv:%d",
467         .format_args = "i4",
468       };
469       struct
470       {
471         u32 data;
472       } *ed;
473       ed = ELOG_TRACK_DATA (&vcm->elog_main, e, vcm->elog_track);
474       ed->data = rv;
475       /* *INDENT-ON* */
476     }
477   return rv;
478 }
479
480 static u8 *
481 format_api_error (u8 * s, va_list * args)
482 {
483   i32 error = va_arg (*args, u32);
484   uword *p;
485
486   p = hash_get (vcm->error_string_by_error_number, -error);
487
488   if (p)
489     s = format (s, "%s (%d)", p[0], error);
490   else
491     s = format (s, "%d", error);
492   return s;
493 }
494
495 static void
496 vppcom_init_error_string_table (void)
497 {
498   vcm->error_string_by_error_number = hash_create (0, sizeof (uword));
499
500 #define _(n, v, s) hash_set (vcm->error_string_by_error_number, -v, s);
501   foreach_vnet_api_error;
502 #undef _
503
504   hash_set (vcm->error_string_by_error_number, 99, "Misc");
505 }
506
507 static inline int
508 vppcom_wait_for_app_state_change (app_state_t app_state)
509 {
510   f64 timeout = clib_time_now (&vcm->clib_time) + vcm->cfg.app_timeout;
511
512   while (clib_time_now (&vcm->clib_time) < timeout)
513     {
514       if (vcm->app_state == app_state)
515         return VPPCOM_OK;
516     }
517   if (VPPCOM_DEBUG > 0)
518     clib_warning ("VCL<%d>: timeout waiting for state %s (%d)", getpid (),
519                   vppcom_app_state_str (app_state), app_state);
520
521   if (VPPCOM_DEBUG > 0)
522     {
523       /* *INDENT-OFF* */
524       ELOG_TYPE_DECLARE (e) =
525         {
526           .format = "ERR: timeout state:%d",
527           .format_args = "i4",
528         };
529       struct
530       {
531         u32 data;
532       } *ed;
533
534       ed = ELOG_TRACK_DATA (&vcm->elog_main, e, vcm->elog_track);
535
536       ed->data = app_state;
537       /* *INDENT-ON* */
538     }
539
540   return VPPCOM_ETIMEDOUT;
541 }
542
543 static inline int
544 vppcom_wait_for_session_state_change (u32 session_index,
545                                       session_state_t state,
546                                       f64 wait_for_time)
547 {
548   f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
549   session_t *volatile session;
550   int rv;
551
552   do
553     {
554       clib_spinlock_lock (&vcm->sessions_lockp);
555       rv = vppcom_session_at_index (session_index, &session);
556       if (PREDICT_FALSE (rv))
557         {
558           clib_spinlock_unlock (&vcm->sessions_lockp);
559           return rv;
560         }
561       if (session->state & state)
562         {
563           clib_spinlock_unlock (&vcm->sessions_lockp);
564           return VPPCOM_OK;
565         }
566       if (session->state & STATE_FAILED)
567         {
568           clib_spinlock_unlock (&vcm->sessions_lockp);
569           return VPPCOM_ECONNREFUSED;
570         }
571
572       clib_spinlock_unlock (&vcm->sessions_lockp);
573     }
574   while (clib_time_now (&vcm->clib_time) < timeout);
575
576   if (VPPCOM_DEBUG > 0)
577     clib_warning ("VCL<%d>: timeout waiting for state 0x%x (%s)", getpid (),
578                   state, vppcom_session_state_str (state));
579
580   if (VPPCOM_DEBUG > 0)
581     {
582       /* *INDENT-OFF* */
583       ELOG_TYPE_DECLARE (e) =
584         {
585           .format = "ERR: timeout state:%d",
586           .format_args = "i4",
587         };
588       struct
589       {
590         u32 data;
591       } *ed;
592
593       ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
594
595       ed->data = state;
596       /* *INDENT-ON* */
597     }
598
599   return VPPCOM_ETIMEDOUT;
600 }
601
602 static inline int
603 vppcom_wait_for_client_session_index (f64 wait_for_time)
604 {
605   f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
606
607   do
608     {
609       if (clib_fifo_elts (vcm->client_session_index_fifo))
610         return VPPCOM_OK;
611     }
612   while (clib_time_now (&vcm->clib_time) < timeout);
613
614   if (wait_for_time == 0)
615     return VPPCOM_EAGAIN;
616
617   if (VPPCOM_DEBUG > 0)
618     clib_warning ("VCL<%d>: timeout waiting for client_session_index",
619                   getpid ());
620
621   if (VPPCOM_DEBUG > 0)
622     {
623       /* *INDENT-OFF* */
624       ELOG_TYPE_DECLARE (e) =
625         {
626           .format = "ERR: timeout waiting for session index :%d",
627           .format_args = "i4",
628         };
629       struct
630       {
631         u32 data;
632       } *ed;
633
634       ed = ELOG_TRACK_DATA (&vcm->elog_main, e, vcm->elog_track);
635
636       ed->data = getpid();
637       /* *INDENT-ON* */
638     }
639
640   return VPPCOM_ETIMEDOUT;
641 }
642
643 /*
644  * VPP-API message functions
645  */
646 static void
647 vppcom_send_session_enable_disable (u8 is_enable)
648 {
649   vl_api_session_enable_disable_t *bmp;
650   bmp = vl_msg_api_alloc (sizeof (*bmp));
651   memset (bmp, 0, sizeof (*bmp));
652
653   bmp->_vl_msg_id = ntohs (VL_API_SESSION_ENABLE_DISABLE);
654   bmp->client_index = vcm->my_client_index;
655   bmp->context = htonl (0xfeedface);
656   bmp->is_enable = is_enable;
657   vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
658 }
659
660 static int
661 vppcom_app_session_enable (void)
662 {
663   int rv;
664
665   if (vcm->app_state != STATE_APP_ENABLED)
666     {
667       vppcom_send_session_enable_disable (1 /* is_enabled == TRUE */ );
668       rv = vppcom_wait_for_app_state_change (STATE_APP_ENABLED);
669       if (PREDICT_FALSE (rv))
670         {
671           if (VPPCOM_DEBUG > 0)
672             clib_warning ("VCL<%d>: application session enable timed out! "
673                           "returning %d (%s)",
674                           getpid (), rv, vppcom_retval_str (rv));
675           return rv;
676         }
677     }
678   return VPPCOM_OK;
679 }
680
681 static void
682   vl_api_session_enable_disable_reply_t_handler
683   (vl_api_session_enable_disable_reply_t * mp)
684 {
685   if (mp->retval)
686     {
687       clib_warning ("VCL<%d>: session_enable_disable failed: %U", getpid (),
688                     format_api_error, ntohl (mp->retval));
689     }
690   else
691     vcm->app_state = STATE_APP_ENABLED;
692 }
693
694 static void
695 vppcom_app_send_attach (void)
696 {
697   vl_api_application_attach_t *bmp;
698   u8 nsid_len = vec_len (vcm->cfg.namespace_id);
699   u8 app_is_proxy = (vcm->cfg.app_proxy_transport_tcp ||
700                      vcm->cfg.app_proxy_transport_udp);
701
702   bmp = vl_msg_api_alloc (sizeof (*bmp));
703   memset (bmp, 0, sizeof (*bmp));
704
705   bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_ATTACH);
706   bmp->client_index = vcm->my_client_index;
707   bmp->context = htonl (0xfeedface);
708   bmp->options[APP_OPTIONS_FLAGS] =
709     APP_OPTIONS_FLAGS_ACCEPT_REDIRECT | APP_OPTIONS_FLAGS_ADD_SEGMENT |
710     (vcm->cfg.app_scope_local ? APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE : 0) |
711     (vcm->cfg.app_scope_global ? APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE : 0) |
712     (app_is_proxy ? APP_OPTIONS_FLAGS_IS_PROXY : 0);
713   bmp->options[APP_OPTIONS_PROXY_TRANSPORT] =
714     (vcm->cfg.app_proxy_transport_tcp ? 1 << TRANSPORT_PROTO_TCP : 0) |
715     (vcm->cfg.app_proxy_transport_udp ? 1 << TRANSPORT_PROTO_UDP : 0);
716   bmp->options[APP_OPTIONS_SEGMENT_SIZE] = vcm->cfg.segment_size;
717   bmp->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = vcm->cfg.add_segment_size;
718   bmp->options[APP_OPTIONS_RX_FIFO_SIZE] = vcm->cfg.rx_fifo_size;
719   bmp->options[APP_OPTIONS_TX_FIFO_SIZE] = vcm->cfg.tx_fifo_size;
720   bmp->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] =
721     vcm->cfg.preallocated_fifo_pairs;
722   bmp->options[APP_OPTIONS_EVT_QUEUE_SIZE] = vcm->cfg.event_queue_size;
723   if (nsid_len)
724     {
725       bmp->namespace_id_len = nsid_len;
726       clib_memcpy (bmp->namespace_id, vcm->cfg.namespace_id, nsid_len);
727       bmp->options[APP_OPTIONS_NAMESPACE_SECRET] = vcm->cfg.namespace_secret;
728     }
729   vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
730 }
731
732 static int
733 vppcom_app_attach (void)
734 {
735   int rv;
736
737   vppcom_app_send_attach ();
738   rv = vppcom_wait_for_app_state_change (STATE_APP_ATTACHED);
739   if (PREDICT_FALSE (rv))
740     {
741       if (VPPCOM_DEBUG > 0)
742         clib_warning ("VCL<%d>: application attach timed out! "
743                       "returning %d (%s)",
744                       getpid (), rv, vppcom_retval_str (rv));
745       return rv;
746     }
747   return VPPCOM_OK;
748 }
749
750 static void
751 vppcom_app_detach (void)
752 {
753   vl_api_application_detach_t *bmp;
754   bmp = vl_msg_api_alloc (sizeof (*bmp));
755   memset (bmp, 0, sizeof (*bmp));
756
757   bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH);
758   bmp->client_index = vcm->my_client_index;
759   bmp->context = htonl (0xfeedface);
760   vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
761 }
762
763 static void
764 vl_api_application_attach_reply_t_handler (vl_api_application_attach_reply_t *
765                                            mp)
766 {
767   static svm_fifo_segment_create_args_t _a;
768   svm_fifo_segment_create_args_t *a = &_a;
769   int rv;
770
771   memset (a, 0, sizeof (*a));
772   if (mp->retval)
773     {
774       clib_warning ("VCL<%d>: attach failed: %U", getpid (),
775                     format_api_error, ntohl (mp->retval));
776       return;
777     }
778
779   if (mp->segment_name_length == 0)
780     {
781       clib_warning ("VCL<%d>: segment_name_length zero", getpid ());
782       return;
783     }
784
785   a->segment_name = (char *) mp->segment_name;
786   a->segment_size = mp->segment_size;
787
788   ASSERT (mp->app_event_queue_address);
789
790   /* Attach to the segment vpp created */
791   rv = svm_fifo_segment_attach (a);
792   vec_reset_length (a->new_segment_indices);
793   if (PREDICT_FALSE (rv))
794     {
795       clib_warning ("VCL<%d>: svm_fifo_segment_attach ('%s') failed",
796                     getpid (), mp->segment_name);
797       return;
798     }
799
800   vcm->app_event_queue =
801     uword_to_pointer (mp->app_event_queue_address, svm_queue_t *);
802
803   vcm->app_state = STATE_APP_ATTACHED;
804 }
805
806 static void
807 vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t *
808                                            mp)
809 {
810   if (mp->retval)
811     clib_warning ("VCL<%d>: detach failed: %U", getpid (), format_api_error,
812                   ntohl (mp->retval));
813
814   vcm->app_state = STATE_APP_ENABLED;
815 }
816
817 static void
818 vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
819                                            mp)
820 {
821   if (mp->retval)
822     clib_warning ("VCL<%d>: vpp handle 0x%llx: disconnect session failed: %U",
823                   getpid (), mp->handle, format_api_error,
824                   ntohl (mp->retval));
825 }
826
827 static void
828 vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp)
829 {
830   static svm_fifo_segment_create_args_t _a;
831   svm_fifo_segment_create_args_t *a = &_a;
832   int rv;
833
834   memset (a, 0, sizeof (*a));
835   a->segment_name = (char *) mp->segment_name;
836   a->segment_size = mp->segment_size;
837   /* Attach to the segment vpp created */
838   rv = svm_fifo_segment_attach (a);
839   vec_reset_length (a->new_segment_indices);
840   if (PREDICT_FALSE (rv))
841     {
842       clib_warning ("VCL<%d>: svm_fifo_segment_attach ('%s') failed",
843                     getpid (), mp->segment_name);
844       return;
845     }
846   if (VPPCOM_DEBUG > 1)
847     clib_warning ("VCL<%d>: mapped new segment '%s' size %d", getpid (),
848                   mp->segment_name, mp->segment_size);
849 }
850
851 static void
852 vl_api_unmap_segment_t_handler (vl_api_unmap_segment_t * mp)
853 {
854
855 /*
856  * XXX Need segment_name to session_id hash,
857  * XXX - have sessionID by handle hash currently
858  */
859   clib_warning ("Unmapped segment '%s'", mp->segment_name);
860 }
861
862 static void
863 vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
864 {
865   uword *p;
866
867   p = hash_get (vcm->session_index_by_vpp_handles, mp->handle);
868   if (p)
869     {
870       int rv;
871       session_t *session = 0;
872       u32 session_index = p[0];
873
874       VCL_LOCK_AND_GET_SESSION (session_index, &session);
875       session->state = STATE_CLOSE_ON_EMPTY;
876
877       if (VPPCOM_DEBUG > 1)
878         clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
879                       "setting state to 0x%x (%s)",
880                       getpid (), mp->handle, session_index, session->state,
881                       vppcom_session_state_str (session->state));
882       clib_spinlock_unlock (&vcm->sessions_lockp);
883       return;
884
885     done:
886       if (VPPCOM_DEBUG > 1)
887         clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
888                       "session lookup failed!",
889                       getpid (), mp->handle, session_index);
890     }
891   else
892     clib_warning ("VCL<%d>: vpp handle 0x%llx: session lookup by "
893                   "handle failed!", getpid (), mp->handle);
894 }
895
896 static void
897 vl_api_reset_session_t_handler (vl_api_reset_session_t * mp)
898 {
899   session_t *session = 0;
900   vl_api_reset_session_reply_t *rmp;
901   uword *p;
902   int rv = 0;
903
904   p = hash_get (vcm->session_index_by_vpp_handles, mp->handle);
905   if (p)
906     {
907       int rval;
908       clib_spinlock_lock (&vcm->sessions_lockp);
909       rval = vppcom_session_at_index (p[0], &session);
910       if (PREDICT_FALSE (rval))
911         {
912           rv = VNET_API_ERROR_INVALID_VALUE_2;
913           clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
914                         "session lookup failed! returning %d %U",
915                         getpid (), mp->handle, p[0],
916                         rv, format_api_error, rv);
917         }
918       else
919         {
920           /* TBD: should this disconnect immediately and
921            * flush the fifos?
922            */
923           session->state = STATE_CLOSE_ON_EMPTY;
924
925           if (VPPCOM_DEBUG > 1)
926             clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
927                           "state set to %d (%s)!", getpid (),
928                           mp->handle, p[0], session->state,
929                           vppcom_session_state_str (session->state));
930         }
931       clib_spinlock_unlock (&vcm->sessions_lockp);
932     }
933   else
934     {
935       rv = VNET_API_ERROR_INVALID_VALUE;
936       clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx: session lookup "
937                     "failed! returning %d %U",
938                     getpid (), mp->handle, rv, format_api_error, rv);
939     }
940
941   rmp = vl_msg_api_alloc (sizeof (*rmp));
942   memset (rmp, 0, sizeof (*rmp));
943   rmp->_vl_msg_id = ntohs (VL_API_RESET_SESSION_REPLY);
944   rmp->retval = htonl (rv);
945   rmp->handle = mp->handle;
946   vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
947 }
948
949 static void
950 vl_api_connect_session_reply_t_handler (vl_api_connect_session_reply_t * mp)
951 {
952   session_t *session = 0;
953   u32 session_index;
954   svm_fifo_t *rx_fifo, *tx_fifo;
955   int rv = VPPCOM_OK;
956
957   session_index = mp->context;
958   VCL_LOCK_AND_GET_SESSION (session_index, &session);
959 done:
960   if (mp->retval)
961     {
962       clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
963                     "connect failed! %U",
964                     getpid (), mp->handle, session_index,
965                     format_api_error, ntohl (mp->retval));
966       if (session)
967         {
968           session->state = STATE_FAILED;
969           session->vpp_handle = mp->handle;
970         }
971       else
972         {
973           clib_warning ("[%s] ERROR: vpp handle 0x%llx, sid %u: "
974                         "Invalid session index (%u)!",
975                         getpid (), mp->handle, session_index);
976         }
977       goto done_unlock;
978     }
979
980   if (rv)
981     goto done_unlock;
982
983   /*
984    * Setup session
985    */
986   session->vpp_event_queue = uword_to_pointer (mp->vpp_event_queue_address,
987                                                svm_queue_t *);
988
989   rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
990   rx_fifo->client_session_index = session_index;
991   tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
992   tx_fifo->client_session_index = session_index;
993
994   session->rx_fifo = rx_fifo;
995   session->tx_fifo = tx_fifo;
996   session->vpp_handle = mp->handle;
997   session->lcl_addr.is_ip4 = mp->is_ip4;
998   clib_memcpy (&session->lcl_addr.ip46, mp->lcl_ip,
999                sizeof (session->peer_addr.ip46));
1000   session->lcl_port = mp->lcl_port;
1001   session->state = STATE_CONNECT;
1002
1003   /* Add it to lookup table */
1004   hash_set (vcm->session_index_by_vpp_handles, mp->handle, session_index);
1005
1006   if (VPPCOM_DEBUG > 1)
1007     clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: connect succeeded!"
1008                   " session_rx_fifo %p, refcnt %d,"
1009                   " session_tx_fifo %p, refcnt %d",
1010                   getpid (), mp->handle, session_index,
1011                   session->rx_fifo,
1012                   session->rx_fifo->refcnt,
1013                   session->tx_fifo, session->tx_fifo->refcnt);
1014 done_unlock:
1015   clib_spinlock_unlock (&vcm->sessions_lockp);
1016 }
1017
1018 static void
1019 vppcom_send_connect_sock (session_t * session, u32 session_index)
1020 {
1021   vl_api_connect_sock_t *cmp;
1022
1023   /* Assumes caller as acquired the spinlock: vcm->sessions_lockp */
1024   cmp = vl_msg_api_alloc (sizeof (*cmp));
1025   memset (cmp, 0, sizeof (*cmp));
1026   cmp->_vl_msg_id = ntohs (VL_API_CONNECT_SOCK);
1027   cmp->client_index = vcm->my_client_index;
1028   cmp->context = session_index;
1029
1030   cmp->is_ip4 = session->peer_addr.is_ip4;
1031   clib_memcpy (cmp->ip, &session->peer_addr.ip46, sizeof (cmp->ip));
1032   cmp->port = session->peer_port;
1033   cmp->proto = session->proto;
1034   clib_memcpy (cmp->options, session->options, sizeof (cmp->options));
1035   vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & cmp);
1036 }
1037
1038 static inline void
1039 vppcom_send_disconnect_session_reply (u64 vpp_handle, u32 session_index,
1040                                       int rv)
1041 {
1042   vl_api_disconnect_session_reply_t *rmp;
1043
1044   if (VPPCOM_DEBUG > 1)
1045     clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
1046                   "sending disconnect msg",
1047                   getpid (), vpp_handle, session_index);
1048
1049   rmp = vl_msg_api_alloc (sizeof (*rmp));
1050   memset (rmp, 0, sizeof (*rmp));
1051
1052   rmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION_REPLY);
1053   rmp->retval = htonl (rv);
1054   rmp->handle = vpp_handle;
1055   vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
1056 }
1057
1058 static inline void
1059 vppcom_send_disconnect_session (u64 vpp_handle, u32 session_index)
1060 {
1061   vl_api_disconnect_session_t *dmp;
1062
1063   if (VPPCOM_DEBUG > 1)
1064     clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
1065                   "sending disconnect msg",
1066                   getpid (), vpp_handle, session_index);
1067
1068   dmp = vl_msg_api_alloc (sizeof (*dmp));
1069   memset (dmp, 0, sizeof (*dmp));
1070   dmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION);
1071   dmp->client_index = vcm->my_client_index;
1072   dmp->handle = vpp_handle;
1073   vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & dmp);
1074 }
1075
1076 static void
1077 vl_api_bind_sock_reply_t_handler (vl_api_bind_sock_reply_t * mp)
1078 {
1079   session_t *session = 0;
1080   u32 session_index = mp->context;
1081   int rv;
1082
1083   VCL_LOCK_AND_GET_SESSION (session_index, &session);
1084 done:
1085   if (mp->retval)
1086     {
1087       clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, "
1088                     "sid %u: bind failed: %U",
1089                     getpid (), mp->handle, session_index,
1090                     format_api_error, ntohl (mp->retval));
1091       rv = vppcom_session_at_index (session_index, &session);
1092       if (rv == VPPCOM_OK)
1093         {
1094           session->state = STATE_FAILED;
1095           session->vpp_handle = mp->handle;
1096         }
1097       else
1098         {
1099           clib_warning ("[%s] ERROR: vpp handle 0x%llx, sid %u: "
1100                         "Invalid session index (%u)!",
1101                         getpid (), mp->handle, session_index);
1102         }
1103       goto done_unlock;
1104     }
1105
1106   session->vpp_handle = mp->handle;
1107   session->lcl_addr.is_ip4 = mp->lcl_is_ip4;
1108   clib_memcpy (&session->lcl_addr.ip46, mp->lcl_ip,
1109                sizeof (session->peer_addr.ip46));
1110   session->lcl_port = mp->lcl_port;
1111   vppcom_session_table_add_listener (mp->handle, session_index);
1112   session->state = STATE_LISTEN;
1113
1114   if (VPPCOM_DEBUG > 1)
1115     clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: bind succeeded!",
1116                   getpid (), mp->handle, mp->context);
1117 done_unlock:
1118   clib_spinlock_unlock (&vcm->sessions_lockp);
1119 }
1120
1121 static void
1122 vl_api_unbind_sock_reply_t_handler (vl_api_unbind_sock_reply_t * mp)
1123 {
1124   if (mp->retval)
1125     clib_warning ("VCL<%d>: ERROR: sid %u: unbind failed: %U",
1126                   getpid (), mp->context, format_api_error,
1127                   ntohl (mp->retval));
1128
1129   else if (VPPCOM_DEBUG > 1)
1130     clib_warning ("VCL<%d>: sid %u: unbind succeeded!",
1131                   getpid (), mp->context);
1132 }
1133
1134 u8 *
1135 format_ip4_address (u8 * s, va_list * args)
1136 {
1137   u8 *a = va_arg (*args, u8 *);
1138   return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
1139 }
1140
1141 u8 *
1142 format_ip6_address (u8 * s, va_list * args)
1143 {
1144   ip6_address_t *a = va_arg (*args, ip6_address_t *);
1145   u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
1146
1147   i_max_n_zero = ARRAY_LEN (a->as_u16);
1148   max_n_zeros = 0;
1149   i_first_zero = i_max_n_zero;
1150   n_zeros = 0;
1151   for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
1152     {
1153       u32 is_zero = a->as_u16[i] == 0;
1154       if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
1155         {
1156           i_first_zero = i;
1157           n_zeros = 0;
1158         }
1159       n_zeros += is_zero;
1160       if ((!is_zero && n_zeros > max_n_zeros)
1161           || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
1162         {
1163           i_max_n_zero = i_first_zero;
1164           max_n_zeros = n_zeros;
1165           i_first_zero = ARRAY_LEN (a->as_u16);
1166           n_zeros = 0;
1167         }
1168     }
1169
1170   last_double_colon = 0;
1171   for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
1172     {
1173       if (i == i_max_n_zero && max_n_zeros > 1)
1174         {
1175           s = format (s, "::");
1176           i += max_n_zeros - 1;
1177           last_double_colon = 1;
1178         }
1179       else
1180         {
1181           s = format (s, "%s%x",
1182                       (last_double_colon || i == 0) ? "" : ":",
1183                       clib_net_to_host_u16 (a->as_u16[i]));
1184           last_double_colon = 0;
1185         }
1186     }
1187
1188   return s;
1189 }
1190
1191 /* Format an IP46 address. */
1192 u8 *
1193 format_ip46_address (u8 * s, va_list * args)
1194 {
1195   ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
1196   ip46_type_t type = va_arg (*args, ip46_type_t);
1197   int is_ip4 = 1;
1198
1199   switch (type)
1200     {
1201     case IP46_TYPE_ANY:
1202       is_ip4 = ip46_address_is_ip4 (ip46);
1203       break;
1204     case IP46_TYPE_IP4:
1205       is_ip4 = 1;
1206       break;
1207     case IP46_TYPE_IP6:
1208       is_ip4 = 0;
1209       break;
1210     }
1211
1212   return is_ip4 ?
1213     format (s, "%U", format_ip4_address, &ip46->ip4) :
1214     format (s, "%U", format_ip6_address, &ip46->ip6);
1215 }
1216
1217 static inline void
1218 vppcom_send_accept_session_reply (u64 handle, u32 context, int retval)
1219 {
1220   vl_api_accept_session_reply_t *rmp;
1221
1222   rmp = vl_msg_api_alloc (sizeof (*rmp));
1223   memset (rmp, 0, sizeof (*rmp));
1224   rmp->_vl_msg_id = ntohs (VL_API_ACCEPT_SESSION_REPLY);
1225   rmp->retval = htonl (retval);
1226   rmp->context = context;
1227   rmp->handle = handle;
1228   vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
1229 }
1230
1231 static void
1232 vl_api_accept_session_t_handler (vl_api_accept_session_t * mp)
1233 {
1234   svm_fifo_t *rx_fifo, *tx_fifo;
1235   session_t *session, *listen_session;
1236   u32 session_index;
1237
1238   clib_spinlock_lock (&vcm->sessions_lockp);
1239   if (!clib_fifo_free_elts (vcm->client_session_index_fifo))
1240     {
1241       clib_warning ("VCL<%d>: client session queue is full!", getpid ());
1242       vppcom_send_accept_session_reply (mp->handle, mp->context,
1243                                         VNET_API_ERROR_QUEUE_FULL);
1244       clib_spinlock_unlock (&vcm->sessions_lockp);
1245       return;
1246     }
1247
1248   listen_session = vppcom_session_table_lookup_listener (mp->listener_handle);
1249   if (!listen_session)
1250     {
1251       clib_warning ("VCL<%d>: ERROR: couldn't find listen session: "
1252                     "unknown vpp listener handle %llx",
1253                     getpid (), mp->listener_handle);
1254       clib_spinlock_unlock (&vcm->sessions_lockp);
1255       return;
1256     }
1257
1258   /* Allocate local session and set it up */
1259   pool_get (vcm->sessions, session);
1260   memset (session, 0, sizeof (*session));
1261   session_index = session - vcm->sessions;
1262
1263   rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
1264   rx_fifo->client_session_index = session_index;
1265   tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
1266   tx_fifo->client_session_index = session_index;
1267
1268   session->vpp_handle = mp->handle;
1269   session->client_context = mp->context;
1270   session->rx_fifo = rx_fifo;
1271   session->tx_fifo = tx_fifo;
1272   session->vpp_event_queue = uword_to_pointer (mp->vpp_event_queue_address,
1273                                                svm_queue_t *);
1274   session->state = STATE_ACCEPT;
1275   session->peer_port = mp->port;
1276   session->peer_addr.is_ip4 = mp->is_ip4;
1277   clib_memcpy (&session->peer_addr.ip46, mp->ip,
1278                sizeof (session->peer_addr.ip46));
1279
1280   /* Add it to lookup table */
1281   hash_set (vcm->session_index_by_vpp_handles, mp->handle, session_index);
1282   session->lcl_port = listen_session->lcl_port;
1283   session->lcl_addr = listen_session->lcl_addr;
1284
1285   /* TBD: move client_session_index_fifo into listener session */
1286   clib_fifo_add1 (vcm->client_session_index_fifo, session_index);
1287
1288   if (VPPCOM_DEBUG > 1)
1289     clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: client accept "
1290                   "request from %s address %U port %d queue %p!", getpid (),
1291                   mp->handle, session_index, mp->is_ip4 ? "IPv4" : "IPv6",
1292                   format_ip46_address, &mp->ip, mp->is_ip4,
1293                   clib_net_to_host_u16 (mp->port), session->vpp_event_queue);
1294
1295   if (VPPCOM_DEBUG > 0)
1296     {
1297       session->elog_track.name =
1298         (char *) format (0, "C:%d:S:%d%c", vcm->my_client_index,
1299                          session_index, 0);
1300       elog_track_register (&vcm->elog_main, &session->elog_track);
1301
1302       if (session->peer_addr.is_ip4)
1303         {
1304           /* *INDENT-OFF* */
1305           ELOG_TYPE_DECLARE (e) =
1306           {
1307             .format =
1308             "client_accept:handle:%x addr:%d.%d.%d.%d:%d",
1309             .format_args = "i8i1i1i1i1i2",
1310           };
1311
1312           CLIB_PACKED (struct {
1313             u64 handle; //8
1314             u8 addr[4]; //4
1315             u16 port;   //2
1316           }) * ed;
1317
1318           ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
1319
1320           ed->handle = mp->handle;
1321           ed->addr[0] = session->peer_addr.ip46.ip4.as_u8[0];
1322           ed->addr[1] = session->peer_addr.ip46.ip4.as_u8[1];
1323           ed->addr[2] = session->peer_addr.ip46.ip4.as_u8[2];
1324           ed->addr[3] = session->peer_addr.ip46.ip4.as_u8[3];
1325           ed->port = clib_net_to_host_u16 (session->peer_port);
1326           /* *INDENT-ON* */
1327         }
1328       else
1329         {
1330           clib_warning ("ip6");
1331         }
1332     }
1333   clib_spinlock_unlock (&vcm->sessions_lockp);
1334
1335 }
1336
1337 static void
1338 vppcom_send_connect_session_reply (session_t * session, u32 session_index,
1339                                    u64 vpp_handle, u32 context, int retval)
1340 {
1341   vl_api_connect_session_reply_t *rmp;
1342   u32 len;
1343   svm_queue_t *client_q;
1344
1345   rmp = vl_msg_api_alloc (sizeof (*rmp));
1346   memset (rmp, 0, sizeof (*rmp));
1347   rmp->_vl_msg_id = ntohs (VL_API_CONNECT_SESSION_REPLY);
1348
1349   if (!session)
1350     {
1351       rmp->context = context;
1352       rmp->handle = vpp_handle;
1353       rmp->retval = htonl (retval);
1354       vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
1355       return;
1356     }
1357
1358   rmp->context = session->client_context;
1359   rmp->retval = htonl (retval);
1360   rmp->handle = session->vpp_handle;
1361   rmp->server_rx_fifo = pointer_to_uword (session->rx_fifo);
1362   rmp->server_tx_fifo = pointer_to_uword (session->tx_fifo);
1363   rmp->vpp_event_queue_address = pointer_to_uword (session->vpp_event_queue);
1364   rmp->segment_size = vcm->cfg.segment_size;
1365   len = vec_len (session->segment_name);
1366   rmp->segment_name_length = clib_min (len, sizeof (rmp->segment_name));
1367   clib_memcpy (rmp->segment_name, session->segment_name,
1368                rmp->segment_name_length - 1);
1369   clib_memcpy (rmp->lcl_ip, session->peer_addr.ip46.as_u8,
1370                sizeof (rmp->lcl_ip));
1371   rmp->is_ip4 = session->peer_addr.is_ip4;
1372   rmp->lcl_port = session->peer_port;
1373   client_q = uword_to_pointer (session->client_queue_address, svm_queue_t *);
1374   ASSERT (client_q);
1375   vl_msg_api_send_shmem (client_q, (u8 *) & rmp);
1376 }
1377
1378 /*
1379  * Acting as server for redirected connect requests
1380  */
1381 static void
1382 vl_api_connect_sock_t_handler (vl_api_connect_sock_t * mp)
1383 {
1384   u32 session_index;
1385   session_t *session = 0;
1386
1387   clib_spinlock_lock (&vcm->sessions_lockp);
1388   if (!clib_fifo_free_elts (vcm->client_session_index_fifo))
1389     {
1390       clib_spinlock_unlock (&vcm->sessions_lockp);
1391
1392       if (VPPCOM_DEBUG > 1)
1393         clib_warning ("VCL<%d>: client session queue is full!", getpid ());
1394
1395       /* TBD: Fix api to include vpp handle */
1396       vppcom_send_connect_session_reply (0 /* session */ , 0 /* sid */ ,
1397                                          0 /* handle */ , mp->context,
1398                                          VNET_API_ERROR_QUEUE_FULL);
1399       return;
1400     }
1401
1402   pool_get (vcm->sessions, session);
1403   memset (session, 0, sizeof (*session));
1404   session_index = session - vcm->sessions;
1405
1406   session->client_context = mp->context;
1407   session->vpp_handle = session_index;
1408   session->client_queue_address = mp->client_queue_address;
1409   session->lcl_port = mp->port;
1410   session->lcl_addr.is_ip4 = mp->is_ip4;
1411   clib_memcpy (&session->lcl_addr.ip46, mp->ip,
1412                sizeof (session->lcl_addr.ip46));
1413
1414   /* TBD: missing peer info in api msg.
1415    */
1416   session->peer_addr.is_ip4 = mp->is_ip4;
1417   ASSERT (session->lcl_addr.is_ip4 == session->peer_addr.is_ip4);
1418
1419   session->state = STATE_ACCEPT;
1420   clib_fifo_add1 (vcm->client_session_index_fifo, session_index);
1421   if (VPPCOM_DEBUG > 1)
1422     clib_warning ("VCL<%d>: sid %u: Got a cut-thru connect request! "
1423                   "clib_fifo_elts %u!\n", getpid (), session_index,
1424                   clib_fifo_elts (vcm->client_session_index_fifo));
1425
1426   if (VPPCOM_DEBUG > 0)
1427     {
1428       session->elog_track.name =
1429         (char *) format (0, "C:%d:S:%d%c", vcm->my_client_index,
1430                          session_index, 0);
1431       elog_track_register (&vcm->elog_main, &session->elog_track);
1432
1433       /* *INDENT-OFF* */
1434       ELOG_TYPE_DECLARE (e) =
1435       {
1436         .format = "cut-thru-connect:S:%d clib_fifo_elts:%d",
1437         .format_args = "i4i4",
1438       };
1439
1440       struct
1441       {
1442         u32 data[2];
1443       } *ed;
1444
1445       ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
1446
1447       ed->data[0] = session_index;
1448       ed->data[1] = clib_fifo_elts (vcm->client_session_index_fifo);
1449       /* *INDENT-ON* */
1450     }
1451
1452   clib_spinlock_unlock (&vcm->sessions_lockp);
1453 }
1454
1455 static void
1456 vppcom_send_bind_sock (session_t * session, u32 session_index)
1457 {
1458   vl_api_bind_sock_t *bmp;
1459
1460   /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
1461   bmp = vl_msg_api_alloc (sizeof (*bmp));
1462   memset (bmp, 0, sizeof (*bmp));
1463
1464   bmp->_vl_msg_id = ntohs (VL_API_BIND_SOCK);
1465   bmp->client_index = vcm->my_client_index;
1466   bmp->context = session_index;
1467   bmp->is_ip4 = session->lcl_addr.is_ip4;
1468   clib_memcpy (bmp->ip, &session->lcl_addr.ip46, sizeof (bmp->ip));
1469   bmp->port = session->lcl_port;
1470   bmp->proto = session->proto;
1471   clib_memcpy (bmp->options, session->options, sizeof (bmp->options));
1472   vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
1473 }
1474
1475 static void
1476 vppcom_send_unbind_sock (u64 vpp_handle)
1477 {
1478   vl_api_unbind_sock_t *ump;
1479
1480   ump = vl_msg_api_alloc (sizeof (*ump));
1481   memset (ump, 0, sizeof (*ump));
1482
1483   ump->_vl_msg_id = ntohs (VL_API_UNBIND_SOCK);
1484   ump->client_index = vcm->my_client_index;
1485   ump->handle = vpp_handle;
1486   vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & ump);
1487 }
1488
1489 static int
1490 vppcom_session_unbind (u32 session_index)
1491 {
1492   session_t *session = 0;
1493   int rv;
1494   u64 vpp_handle;
1495   elog_track_t session_elog_track;
1496
1497   VCL_LOCK_AND_GET_SESSION (session_index, &session);
1498
1499   vpp_handle = session->vpp_handle;
1500   vppcom_session_table_del_listener (vpp_handle);
1501   session->vpp_handle = ~0;
1502   session->state = STATE_DISCONNECT;
1503   session_elog_track = session->elog_track;
1504
1505   clib_spinlock_unlock (&vcm->sessions_lockp);
1506
1507   if (VPPCOM_DEBUG > 1)
1508     clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
1509                   "sending unbind msg! new state 0x%x (%s)",
1510                   getpid (), vpp_handle, session_index,
1511                   STATE_DISCONNECT,
1512                   vppcom_session_state_str (STATE_DISCONNECT));
1513
1514   if (VPPCOM_DEBUG > 0)
1515     {
1516       /* *INDENT-OFF* */
1517       ELOG_TYPE_DECLARE (e) =
1518       {
1519         .format = "unbind: handle:%x",
1520         .format_args = "i8",
1521       };
1522
1523       struct
1524       {
1525         u64 handle;
1526       } *ed;
1527
1528       ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session_elog_track);
1529       ed->handle = vpp_handle;
1530       /* *INDENT-ON* */
1531     }
1532
1533   vppcom_send_unbind_sock (vpp_handle);
1534
1535 done:
1536   return rv;
1537 }
1538
1539 static inline int
1540 vppcom_session_disconnect (u32 session_index)
1541 {
1542   int rv;
1543   session_t *session;
1544   u64 vpp_handle;
1545   session_state_t state;
1546
1547   VCL_LOCK_AND_GET_SESSION (session_index, &session);
1548
1549   vpp_handle = session->vpp_handle;
1550   state = session->state;
1551   clib_spinlock_unlock (&vcm->sessions_lockp);
1552
1553   if (VPPCOM_DEBUG > 1)
1554     {
1555       clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u "
1556                     "state 0x%x (%s)",
1557                     getpid (), vpp_handle, session_index,
1558                     state, vppcom_session_state_str (state));
1559     }
1560
1561   if (PREDICT_FALSE (state & STATE_LISTEN))
1562     {
1563       clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
1564                     "Cannot disconnect a listen socket!",
1565                     getpid (), vpp_handle, session_index);
1566       rv = VPPCOM_EBADFD;
1567       goto done;
1568     }
1569
1570   /* The peer has already initiated the close,
1571    * so send the disconnect session reply.
1572    */
1573   if (state & STATE_CLOSE_ON_EMPTY)
1574     {
1575       //XXX alagalah - Check and drain here?
1576       vppcom_send_disconnect_session_reply (vpp_handle,
1577                                             session_index, 0 /* rv */ );
1578       if (VPPCOM_DEBUG > 1)
1579         clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
1580                       "sending disconnect REPLY...",
1581                       getpid (), vpp_handle, session_index);
1582     }
1583
1584   /* Otherwise, send a disconnect session msg...
1585    */
1586   else
1587     {
1588       if (VPPCOM_DEBUG > 1)
1589         clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
1590                       "sending disconnect...",
1591                       getpid (), vpp_handle, session_index);
1592
1593       vppcom_send_disconnect_session (vpp_handle, session_index);
1594     }
1595
1596 done:
1597   return rv;
1598 }
1599
1600 #define foreach_sock_msg                                        \
1601 _(SESSION_ENABLE_DISABLE_REPLY, session_enable_disable_reply)   \
1602 _(BIND_SOCK_REPLY, bind_sock_reply)                             \
1603 _(UNBIND_SOCK_REPLY, unbind_sock_reply)                         \
1604 _(ACCEPT_SESSION, accept_session)                               \
1605 _(CONNECT_SOCK, connect_sock)                                   \
1606 _(CONNECT_SESSION_REPLY, connect_session_reply)                 \
1607 _(DISCONNECT_SESSION, disconnect_session)                       \
1608 _(DISCONNECT_SESSION_REPLY, disconnect_session_reply)           \
1609 _(RESET_SESSION, reset_session)                                 \
1610 _(APPLICATION_ATTACH_REPLY, application_attach_reply)           \
1611 _(APPLICATION_DETACH_REPLY, application_detach_reply)           \
1612 _(MAP_ANOTHER_SEGMENT, map_another_segment)                     \
1613 _(UNMAP_SEGMENT, unmap_segment)
1614
1615 static void
1616 vppcom_api_hookup (void)
1617 {
1618 #define _(N, n)                                                  \
1619     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
1620                            vl_api_##n##_t_handler,              \
1621                            vl_noop_handler,                     \
1622                            vl_api_##n##_t_endian,               \
1623                            vl_api_##n##_t_print,                \
1624                            sizeof(vl_api_##n##_t), 1);
1625   foreach_sock_msg;
1626 #undef _
1627 }
1628
1629 static void
1630 vppcom_cfg_init (vppcom_cfg_t * vcl_cfg)
1631 {
1632   ASSERT (vcl_cfg);
1633
1634   vcl_cfg->heapsize = (256ULL << 20);
1635   vcl_cfg->vpp_api_q_length = 1024;
1636   vcl_cfg->segment_baseva = 0x200000000ULL;
1637   vcl_cfg->segment_size = (256 << 20);
1638   vcl_cfg->add_segment_size = (128 << 20);
1639   vcl_cfg->preallocated_fifo_pairs = 8;
1640   vcl_cfg->rx_fifo_size = (1 << 20);
1641   vcl_cfg->tx_fifo_size = (1 << 20);
1642   vcl_cfg->event_queue_size = 2048;
1643   vcl_cfg->listen_queue_size = CLIB_CACHE_LINE_BYTES / sizeof (u32);
1644   vcl_cfg->app_timeout = 10 * 60.0;
1645   vcl_cfg->session_timeout = 10 * 60.0;
1646   vcl_cfg->accept_timeout = 60.0;
1647   vcl_cfg->event_ring_size = (128 << 10);
1648   vcl_cfg->event_log_path = "/dev/shm";
1649 }
1650
1651 static void
1652 vppcom_cfg_heapsize (char *conf_fname)
1653 {
1654   vppcom_cfg_t *vcl_cfg = &vcm->cfg;
1655   FILE *fp;
1656   char inbuf[4096];
1657   int argc = 1;
1658   char **argv = NULL;
1659   char *arg = NULL;
1660   char *p;
1661   int i;
1662   u8 *sizep;
1663   u32 size;
1664   void *vcl_mem;
1665   void *heap;
1666
1667   fp = fopen (conf_fname, "r");
1668   if (fp == NULL)
1669     {
1670       if (VPPCOM_DEBUG > 0)
1671         clib_warning ("VCL<%d>: using default heapsize %lld (0x%llx)",
1672                       getpid (), vcl_cfg->heapsize, vcl_cfg->heapsize);
1673       goto defaulted;
1674     }
1675
1676   argv = calloc (1, sizeof (char *));
1677   if (argv == NULL)
1678     {
1679       if (VPPCOM_DEBUG > 0)
1680         clib_warning ("VCL<%d>: calloc failed, using default "
1681                       "heapsize %lld (0x%llx)",
1682                       getpid (), vcl_cfg->heapsize, vcl_cfg->heapsize);
1683       goto defaulted;
1684     }
1685
1686   while (1)
1687     {
1688       if (fgets (inbuf, 4096, fp) == 0)
1689         break;
1690       p = strtok (inbuf, " \t\n");
1691       while (p != NULL)
1692         {
1693           if (*p == '#')
1694             break;
1695           argc++;
1696           char **tmp = realloc (argv, argc * sizeof (char *));
1697           if (tmp == NULL)
1698             {
1699               if (VPPCOM_DEBUG > 0)
1700                 clib_warning ("VCL<%d>: realloc failed, "
1701                               "using default heapsize %lld (0x%llx)",
1702                               getpid (), vcl_cfg->heapsize,
1703                               vcl_cfg->heapsize);
1704               goto defaulted;
1705             }
1706           argv = tmp;
1707           arg = strndup (p, 1024);
1708           if (arg == NULL)
1709             {
1710               if (VPPCOM_DEBUG > 0)
1711                 clib_warning ("VCL<%d>: strndup failed, "
1712                               "using default heapsize %lld (0x%llx)",
1713                               getpid (), vcl_cfg->heapsize,
1714                               vcl_cfg->heapsize);
1715               goto defaulted;
1716             }
1717           argv[argc - 1] = arg;
1718           p = strtok (NULL, " \t\n");
1719         }
1720     }
1721
1722   fclose (fp);
1723   fp = NULL;
1724
1725   char **tmp = realloc (argv, (argc + 1) * sizeof (char *));
1726   if (tmp == NULL)
1727     {
1728       if (VPPCOM_DEBUG > 0)
1729         clib_warning ("VCL<%d>: realloc failed, "
1730                       "using default heapsize %lld (0x%llx)",
1731                       getpid (), vcl_cfg->heapsize, vcl_cfg->heapsize);
1732       goto defaulted;
1733     }
1734   argv = tmp;
1735   argv[argc] = NULL;
1736
1737   /*
1738    * Look for and parse the "heapsize" config parameter.
1739    * Manual since none of the clib infra has been bootstrapped yet.
1740    *
1741    * Format: heapsize <nn>[mM][gG]
1742    */
1743
1744   for (i = 1; i < (argc - 1); i++)
1745     {
1746       if (!strncmp (argv[i], "heapsize", 8))
1747         {
1748           sizep = (u8 *) argv[i + 1];
1749           size = 0;
1750           while (*sizep >= '0' && *sizep <= '9')
1751             {
1752               size *= 10;
1753               size += *sizep++ - '0';
1754             }
1755           if (size == 0)
1756             {
1757               if (VPPCOM_DEBUG > 0)
1758                 clib_warning ("VCL<%d>: parse error '%s %s', "
1759                               "using default heapsize %lld (0x%llx)",
1760                               getpid (), argv[i], argv[i + 1],
1761                               vcl_cfg->heapsize, vcl_cfg->heapsize);
1762               goto defaulted;
1763             }
1764
1765           if (*sizep == 'g' || *sizep == 'G')
1766             vcl_cfg->heapsize = size << 30;
1767           else if (*sizep == 'm' || *sizep == 'M')
1768             vcl_cfg->heapsize = size << 20;
1769           else
1770             {
1771               if (VPPCOM_DEBUG > 0)
1772                 clib_warning ("VCL<%d>: parse error '%s %s', "
1773                               "using default heapsize %lld (0x%llx)",
1774                               getpid (), argv[i], argv[i + 1],
1775                               vcl_cfg->heapsize, vcl_cfg->heapsize);
1776               goto defaulted;
1777             }
1778         }
1779     }
1780
1781 defaulted:
1782   if (fp != NULL)
1783     fclose (fp);
1784   if (argv != NULL)
1785     free (argv);
1786
1787   vcl_mem = mmap (0, vcl_cfg->heapsize, PROT_READ | PROT_WRITE,
1788                   MAP_SHARED | MAP_ANONYMOUS, -1, 0);
1789   if (vcl_mem == MAP_FAILED)
1790     {
1791       clib_unix_error ("VCL<%d>: ERROR: mmap(0, %lld == 0x%llx, "
1792                        "PROT_READ | PROT_WRITE,MAP_SHARED | MAP_ANONYMOUS, "
1793                        "-1, 0) failed!",
1794                        getpid (), vcl_cfg->heapsize, vcl_cfg->heapsize);
1795       ASSERT (vcl_mem != MAP_FAILED);
1796       return;
1797     }
1798   heap = clib_mem_init (vcl_mem, vcl_cfg->heapsize);
1799   if (!heap)
1800     {
1801       clib_warning ("VCL<%d>: ERROR: clib_mem_init() failed!", getpid ());
1802       ASSERT (heap);
1803       return;
1804     }
1805   vcl_mem = clib_mem_alloc (sizeof (_vppcom_main));
1806   if (!vcl_mem)
1807     {
1808       clib_warning ("VCL<%d>: ERROR: clib_mem_alloc() failed!", getpid ());
1809       ASSERT (vcl_mem);
1810       return;
1811     }
1812
1813   clib_memcpy (vcl_mem, &_vppcom_main, sizeof (_vppcom_main));
1814   vcm = vcl_mem;
1815
1816   if (VPPCOM_DEBUG > 0)
1817     clib_warning ("VCL<%d>: allocated VCL heap = %p, size %lld (0x%llx)",
1818                   getpid (), heap, vcl_cfg->heapsize, vcl_cfg->heapsize);
1819 }
1820
1821 static void
1822 vppcom_cfg_read (char *conf_fname)
1823 {
1824   vppcom_cfg_t *vcl_cfg = &vcm->cfg;
1825   int fd;
1826   unformat_input_t _input, *input = &_input;
1827   unformat_input_t _line_input, *line_input = &_line_input;
1828   u8 vc_cfg_input = 0;
1829   u8 *chroot_path;
1830   struct stat s;
1831   u32 uid, gid, q_len;
1832
1833   fd = open (conf_fname, O_RDONLY);
1834   if (fd < 0)
1835     {
1836       if (VPPCOM_DEBUG > 0)
1837         clib_warning ("VCL<%d>: using default configuration.",
1838                       getpid (), conf_fname);
1839       goto file_done;
1840     }
1841
1842   if (fstat (fd, &s) < 0)
1843     {
1844       if (VPPCOM_DEBUG > 0)
1845         clib_warning ("VCL<%d>: failed to stat `%s', "
1846                       "using default configuration", getpid (), conf_fname);
1847       goto file_done;
1848     }
1849
1850   if (!(S_ISREG (s.st_mode) || S_ISLNK (s.st_mode)))
1851     {
1852       if (VPPCOM_DEBUG > 0)
1853         clib_warning ("VCL<%d>: not a regular file `%s', "
1854                       "using default configuration", getpid (), conf_fname);
1855       goto file_done;
1856     }
1857
1858   unformat_init_clib_file (input, fd);
1859
1860   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1861     {
1862       (void) unformat_user (input, unformat_line_input, line_input);
1863       unformat_skip_white_space (line_input);
1864
1865       if (unformat (line_input, "vcl {"))
1866         {
1867           vc_cfg_input = 1;
1868           continue;
1869         }
1870
1871       if (vc_cfg_input)
1872         {
1873           if (unformat (line_input, "heapsize %s", &chroot_path))
1874             {
1875               vec_terminate_c_string (chroot_path);
1876               if (VPPCOM_DEBUG > 0)
1877                 clib_warning ("VCL<%d>: configured heapsize %s, "
1878                               "actual heapsize %lld (0x%llx)",
1879                               getpid (), chroot_path, vcl_cfg->heapsize,
1880                               vcl_cfg->heapsize);
1881               vec_free (chroot_path);
1882             }
1883           else if (unformat (line_input, "api-prefix %s", &chroot_path))
1884             {
1885               vec_terminate_c_string (chroot_path);
1886               if (vcl_cfg->vpp_api_filename)
1887                 vec_free (vcl_cfg->vpp_api_filename);
1888               vcl_cfg->vpp_api_filename = format (0, "/%s-vpe-api%c",
1889                                                   chroot_path, 0);
1890               vl_set_memory_root_path ((char *) chroot_path);
1891
1892               if (VPPCOM_DEBUG > 0)
1893                 clib_warning ("VCL<%d>: configured api-prefix (%s) and "
1894                               "api filename (%s)", getpid (), chroot_path,
1895                               vcl_cfg->vpp_api_filename);
1896               chroot_path = 0;  /* Don't vec_free() it! */
1897             }
1898           else if (unformat (line_input, "vpp-api-q-length %d", &q_len))
1899             {
1900               if (q_len < vcl_cfg->vpp_api_q_length)
1901                 {
1902                   clib_warning ("VCL<%d>: ERROR: configured vpp-api-q-length "
1903                                 "(%u) is too small! Using default: %u ",
1904                                 getpid (), q_len, vcl_cfg->vpp_api_q_length);
1905                 }
1906               else
1907                 {
1908                   vcl_cfg->vpp_api_q_length = q_len;
1909
1910                   if (VPPCOM_DEBUG > 0)
1911                     clib_warning ("VCL<%d>: configured vpp-api-q-length %u",
1912                                   getpid (), vcl_cfg->vpp_api_q_length);
1913                 }
1914             }
1915           else if (unformat (line_input, "uid %d", &uid))
1916             {
1917               vl_set_memory_uid (uid);
1918               if (VPPCOM_DEBUG > 0)
1919                 clib_warning ("VCL<%d>: configured uid %d", getpid (), uid);
1920             }
1921           else if (unformat (line_input, "gid %d", &gid))
1922             {
1923               vl_set_memory_gid (gid);
1924               if (VPPCOM_DEBUG > 0)
1925                 clib_warning ("VCL<%d>: configured gid %d", getpid (), gid);
1926             }
1927           else if (unformat (line_input, "segment-baseva 0x%lx",
1928                              &vcl_cfg->segment_baseva))
1929             {
1930               if (VPPCOM_DEBUG > 0)
1931                 clib_warning ("VCL<%d>: configured segment_baseva 0x%lx",
1932                               getpid (), vcl_cfg->segment_baseva);
1933             }
1934           else if (unformat (line_input, "segment-size 0x%lx",
1935                              &vcl_cfg->segment_size))
1936             {
1937               if (VPPCOM_DEBUG > 0)
1938                 clib_warning ("VCL<%d>: configured segment_size 0x%lx (%ld)",
1939                               getpid (), vcl_cfg->segment_size,
1940                               vcl_cfg->segment_size);
1941             }
1942           else if (unformat (line_input, "segment-size %ld",
1943                              &vcl_cfg->segment_size))
1944             {
1945               if (VPPCOM_DEBUG > 0)
1946                 clib_warning ("VCL<%d>: configured segment_size %ld (0x%lx)",
1947                               getpid (), vcl_cfg->segment_size,
1948                               vcl_cfg->segment_size);
1949             }
1950           else if (unformat (line_input, "add-segment-size 0x%lx",
1951                              &vcl_cfg->add_segment_size))
1952             {
1953               if (VPPCOM_DEBUG > 0)
1954                 clib_warning
1955                   ("VCL<%d>: configured add_segment_size 0x%lx (%ld)",
1956                    getpid (), vcl_cfg->add_segment_size,
1957                    vcl_cfg->add_segment_size);
1958             }
1959           else if (unformat (line_input, "add-segment-size %ld",
1960                              &vcl_cfg->add_segment_size))
1961             {
1962               if (VPPCOM_DEBUG > 0)
1963                 clib_warning
1964                   ("VCL<%d>: configured add_segment_size %ld (0x%lx)",
1965                    getpid (), vcl_cfg->add_segment_size,
1966                    vcl_cfg->add_segment_size);
1967             }
1968           else if (unformat (line_input, "preallocated-fifo-pairs %d",
1969                              &vcl_cfg->preallocated_fifo_pairs))
1970             {
1971               if (VPPCOM_DEBUG > 0)
1972                 clib_warning ("VCL<%d>: configured preallocated_fifo_pairs "
1973                               "%d (0x%x)", getpid (),
1974                               vcl_cfg->preallocated_fifo_pairs,
1975                               vcl_cfg->preallocated_fifo_pairs);
1976             }
1977           else if (unformat (line_input, "rx-fifo-size 0x%lx",
1978                              &vcl_cfg->rx_fifo_size))
1979             {
1980               if (VPPCOM_DEBUG > 0)
1981                 clib_warning ("VCL<%d>: configured rx_fifo_size 0x%lx (%ld)",
1982                               getpid (), vcl_cfg->rx_fifo_size,
1983                               vcl_cfg->rx_fifo_size);
1984             }
1985           else if (unformat (line_input, "rx-fifo-size %ld",
1986                              &vcl_cfg->rx_fifo_size))
1987             {
1988               if (VPPCOM_DEBUG > 0)
1989                 clib_warning ("VCL<%d>: configured rx_fifo_size %ld (0x%lx)",
1990                               getpid (), vcl_cfg->rx_fifo_size,
1991                               vcl_cfg->rx_fifo_size);
1992             }
1993           else if (unformat (line_input, "tx-fifo-size 0x%lx",
1994                              &vcl_cfg->tx_fifo_size))
1995             {
1996               if (VPPCOM_DEBUG > 0)
1997                 clib_warning ("VCL<%d>: configured tx_fifo_size 0x%lx (%ld)",
1998                               getpid (), vcl_cfg->tx_fifo_size,
1999                               vcl_cfg->tx_fifo_size);
2000             }
2001           else if (unformat (line_input, "tx-fifo-size %ld",
2002                              &vcl_cfg->tx_fifo_size))
2003             {
2004               if (VPPCOM_DEBUG > 0)
2005                 clib_warning ("VCL<%d>: configured tx_fifo_size %ld (0x%lx)",
2006                               getpid (), vcl_cfg->tx_fifo_size,
2007                               vcl_cfg->tx_fifo_size);
2008             }
2009           else if (unformat (line_input, "event-queue-size 0x%lx",
2010                              &vcl_cfg->event_queue_size))
2011             {
2012               if (VPPCOM_DEBUG > 0)
2013                 clib_warning ("VCL<%d>: configured event_queue_size "
2014                               "0x%lx (%ld)",
2015                               getpid (), vcl_cfg->event_queue_size,
2016                               vcl_cfg->event_queue_size);
2017             }
2018           else if (unformat (line_input, "event-queue-size %ld",
2019                              &vcl_cfg->event_queue_size))
2020             {
2021               if (VPPCOM_DEBUG > 0)
2022                 clib_warning ("VCL<%d>: configured event_queue_size "
2023                               "%ld (0x%lx)",
2024                               getpid (), vcl_cfg->event_queue_size,
2025                               vcl_cfg->event_queue_size);
2026             }
2027           else if (unformat (line_input, "listen-queue-size 0x%lx",
2028                              &vcl_cfg->listen_queue_size))
2029             {
2030               if (VPPCOM_DEBUG > 0)
2031                 clib_warning ("VCL<%d>: configured listen_queue_size "
2032                               "0x%lx (%ld)",
2033                               getpid (), vcl_cfg->listen_queue_size,
2034                               vcl_cfg->listen_queue_size);
2035             }
2036           else if (unformat (line_input, "listen-queue-size %ld",
2037                              &vcl_cfg->listen_queue_size))
2038             {
2039               if (VPPCOM_DEBUG > 0)
2040                 clib_warning ("VCL<%d>: configured listen_queue_size "
2041                               "%ld (0x%lx)",
2042                               getpid (), vcl_cfg->listen_queue_size,
2043                               vcl_cfg->listen_queue_size);
2044             }
2045           else if (unformat (line_input, "app-timeout %f",
2046                              &vcl_cfg->app_timeout))
2047             {
2048               if (VPPCOM_DEBUG > 0)
2049                 clib_warning ("VCL<%d>: configured app_timeout %f",
2050                               getpid (), vcl_cfg->app_timeout);
2051             }
2052           else if (unformat (line_input, "session-timeout %f",
2053                              &vcl_cfg->session_timeout))
2054             {
2055               if (VPPCOM_DEBUG > 0)
2056                 clib_warning ("VCL<%d>: configured session_timeout %f",
2057                               getpid (), vcl_cfg->session_timeout);
2058             }
2059           else if (unformat (line_input, "accept-timeout %f",
2060                              &vcl_cfg->accept_timeout))
2061             {
2062               if (VPPCOM_DEBUG > 0)
2063                 clib_warning ("VCL<%d>: configured accept_timeout %f",
2064                               getpid (), vcl_cfg->accept_timeout);
2065             }
2066           else if (unformat (line_input, "app-proxy-transport-tcp"))
2067             {
2068               vcl_cfg->app_proxy_transport_tcp = 1;
2069               if (VPPCOM_DEBUG > 0)
2070                 clib_warning ("VCL<%d>: configured "
2071                               "app_proxy_transport_tcp (%d)",
2072                               getpid (), vcl_cfg->app_proxy_transport_tcp);
2073             }
2074           else if (unformat (line_input, "app-proxy-transport-udp"))
2075             {
2076               vcl_cfg->app_proxy_transport_udp = 1;
2077               if (VPPCOM_DEBUG > 0)
2078                 clib_warning ("VCL<%d>: configured "
2079                               "app_proxy_transport_udp (%d)",
2080                               getpid (), vcl_cfg->app_proxy_transport_udp);
2081             }
2082           else if (unformat (line_input, "app-scope-local"))
2083             {
2084               vcl_cfg->app_scope_local = 1;
2085               if (VPPCOM_DEBUG > 0)
2086                 clib_warning ("VCL<%d>: configured app_scope_local (%d)",
2087                               getpid (), vcl_cfg->app_scope_local);
2088             }
2089           else if (unformat (line_input, "app-scope-global"))
2090             {
2091               vcl_cfg->app_scope_global = 1;
2092               if (VPPCOM_DEBUG > 0)
2093                 clib_warning ("VCL<%d>: configured app_scope_global (%d)",
2094                               getpid (), vcl_cfg->app_scope_global);
2095             }
2096           else if (unformat (line_input, "namespace-secret %lu",
2097                              &vcl_cfg->namespace_secret))
2098             {
2099               if (VPPCOM_DEBUG > 0)
2100                 clib_warning
2101                   ("VCL<%d>: configured namespace_secret %lu (0x%lx)",
2102                    getpid (), vcl_cfg->namespace_secret,
2103                    vcl_cfg->namespace_secret);
2104             }
2105           else if (unformat (line_input, "namespace-id %v",
2106                              &vcl_cfg->namespace_id))
2107             {
2108               vl_api_application_attach_t *mp;
2109               u32 max_nsid_vec_len = sizeof (mp->namespace_id) - 1;
2110               u32 nsid_vec_len = vec_len (vcl_cfg->namespace_id);
2111               if (nsid_vec_len > max_nsid_vec_len)
2112                 {
2113                   _vec_len (vcl_cfg->namespace_id) = max_nsid_vec_len;
2114                   if (VPPCOM_DEBUG > 0)
2115                     clib_warning ("VCL<%d>: configured namespace_id is "
2116                                   "too long, truncated to %d characters!",
2117                                   getpid (), max_nsid_vec_len);
2118                 }
2119
2120               if (VPPCOM_DEBUG > 0)
2121                 clib_warning ("VCL<%d>: configured namespace_id %v",
2122                               getpid (), vcl_cfg->namespace_id);
2123             }
2124           else if (unformat (line_input, "}"))
2125             {
2126               vc_cfg_input = 0;
2127               if (VPPCOM_DEBUG > 0)
2128                 clib_warning ("VCL<%d>: completed parsing vppcom config!",
2129                               getpid ());
2130               goto input_done;
2131             }
2132           else
2133             {
2134               if (line_input->buffer[line_input->index] != '#')
2135                 {
2136                   clib_warning ("VCL<%d>: Unknown vppcom config option: '%s'",
2137                                 getpid (), (char *)
2138                                 &line_input->buffer[line_input->index]);
2139                 }
2140             }
2141         }
2142     }
2143
2144 input_done:
2145   unformat_free (input);
2146
2147 file_done:
2148   if (fd >= 0)
2149     close (fd);
2150 }
2151
2152 /*
2153  * VPPCOM Public API functions
2154  */
2155 int
2156 vppcom_app_create (char *app_name)
2157 {
2158   vppcom_cfg_t *vcl_cfg = &vcm->cfg;
2159   u8 *heap;
2160   mheap_t *h;
2161   int rv;
2162
2163   if (!vcm->init)
2164     {
2165       char *conf_fname;
2166       char *env_var_str;
2167
2168       vcm->init = 1;
2169       vppcom_cfg_init (vcl_cfg);
2170       env_var_str = getenv (VPPCOM_ENV_DEBUG);
2171       if (env_var_str)
2172         {
2173           u32 tmp;
2174           if (sscanf (env_var_str, "%u", &tmp) != 1)
2175             clib_warning ("VCL<%d>: Invalid debug level specified in "
2176                           "the environment variable "
2177                           VPPCOM_ENV_DEBUG
2178                           " (%s)!\n", getpid (), env_var_str);
2179           else
2180             {
2181               vcm->debug = tmp;
2182               clib_warning ("VCL<%d>: configured VCL debug level (%u) from "
2183                             VPPCOM_ENV_DEBUG "!", getpid (), vcm->debug);
2184             }
2185         }
2186       conf_fname = getenv (VPPCOM_ENV_CONF);
2187       if (!conf_fname)
2188         conf_fname = VPPCOM_CONF_DEFAULT;
2189       vppcom_cfg_heapsize (conf_fname);
2190       vcl_cfg = &vcm->cfg;
2191       clib_fifo_validate (vcm->client_session_index_fifo,
2192                           vcm->cfg.listen_queue_size);
2193       vppcom_cfg_read (conf_fname);
2194
2195       env_var_str = getenv (VPPCOM_ENV_API_PREFIX);
2196       if (env_var_str)
2197         {
2198           if (vcl_cfg->vpp_api_filename)
2199             vec_free (vcl_cfg->vpp_api_filename);
2200           vcl_cfg->vpp_api_filename = format (0, "/%s-vpe-api%c",
2201                                               env_var_str, 0);
2202           vl_set_memory_root_path ((char *) env_var_str);
2203
2204           if (VPPCOM_DEBUG > 0)
2205             clib_warning ("VCL<%d>: configured api prefix (%s) and "
2206                           "filename (%s) from " VPPCOM_ENV_API_PREFIX "!",
2207                           getpid (), env_var_str, vcl_cfg->vpp_api_filename);
2208         }
2209
2210       env_var_str = getenv (VPPCOM_ENV_APP_NAMESPACE_SECRET);
2211       if (env_var_str)
2212         {
2213           u64 tmp;
2214           if (sscanf (env_var_str, "%lu", &tmp) != 1)
2215             clib_warning ("VCL<%d>: Invalid namespace secret specified in "
2216                           "the environment variable "
2217                           VPPCOM_ENV_APP_NAMESPACE_SECRET
2218                           " (%s)!\n", getpid (), env_var_str);
2219           else
2220             {
2221               vcm->cfg.namespace_secret = tmp;
2222               if (VPPCOM_DEBUG > 0)
2223                 clib_warning ("VCL<%d>: configured namespace secret "
2224                               "(%lu) from " VPPCOM_ENV_APP_NAMESPACE_ID "!",
2225                               getpid (), vcm->cfg.namespace_secret);
2226             }
2227         }
2228       env_var_str = getenv (VPPCOM_ENV_APP_NAMESPACE_ID);
2229       if (env_var_str)
2230         {
2231           u32 ns_id_vec_len = strlen (env_var_str);
2232
2233           vec_reset_length (vcm->cfg.namespace_id);
2234           vec_validate (vcm->cfg.namespace_id, ns_id_vec_len - 1);
2235           clib_memcpy (vcm->cfg.namespace_id, env_var_str, ns_id_vec_len);
2236
2237           if (VPPCOM_DEBUG > 0)
2238             clib_warning ("VCL<%d>: configured namespace_id (%v) from "
2239                           VPPCOM_ENV_APP_NAMESPACE_ID
2240                           "!", getpid (), vcm->cfg.namespace_id);
2241         }
2242       env_var_str = getenv (VPPCOM_ENV_APP_NAMESPACE_SECRET);
2243       if (env_var_str)
2244         {
2245           u64 tmp;
2246           if (sscanf (env_var_str, "%lu", &tmp) != 1)
2247             clib_warning ("VCL<%d>: Invalid namespace secret specified in "
2248                           "the environment variable "
2249                           VPPCOM_ENV_APP_NAMESPACE_SECRET
2250                           " (%s)!\n", getpid (), env_var_str);
2251           else
2252             {
2253               vcm->cfg.namespace_secret = tmp;
2254               if (VPPCOM_DEBUG > 0)
2255                 clib_warning ("VCL<%d>: configured namespace secret "
2256                               "(%lu) from "
2257                               VPPCOM_ENV_APP_NAMESPACE_ID
2258                               "!", getpid (), vcm->cfg.namespace_secret);
2259             }
2260         }
2261       if (getenv (VPPCOM_ENV_APP_PROXY_TRANSPORT_TCP))
2262         {
2263           vcm->cfg.app_proxy_transport_tcp = 1;
2264           if (VPPCOM_DEBUG > 0)
2265             clib_warning ("VCL<%d>: configured app_proxy_transport_tcp "
2266                           "(%u) from "
2267                           VPPCOM_ENV_APP_PROXY_TRANSPORT_TCP
2268                           "!", getpid (), vcm->cfg.app_proxy_transport_tcp);
2269         }
2270       if (getenv (VPPCOM_ENV_APP_PROXY_TRANSPORT_UDP))
2271         {
2272           vcm->cfg.app_proxy_transport_udp = 1;
2273           if (VPPCOM_DEBUG > 0)
2274             clib_warning ("VCL<%d>: configured app_proxy_transport_udp "
2275                           "(%u) from "
2276                           VPPCOM_ENV_APP_PROXY_TRANSPORT_UDP
2277                           "!", getpid (), vcm->cfg.app_proxy_transport_udp);
2278         }
2279       if (getenv (VPPCOM_ENV_APP_SCOPE_LOCAL))
2280         {
2281           vcm->cfg.app_scope_local = 1;
2282           if (VPPCOM_DEBUG > 0)
2283             clib_warning ("VCL<%d>: configured app_scope_local (%u) from "
2284                           VPPCOM_ENV_APP_SCOPE_LOCAL
2285                           "!", getpid (), vcm->cfg.app_scope_local);
2286         }
2287       if (getenv (VPPCOM_ENV_APP_SCOPE_GLOBAL))
2288         {
2289           vcm->cfg.app_scope_global = 1;
2290           if (VPPCOM_DEBUG > 0)
2291             clib_warning ("VCL<%d>: configured app_scope_global (%u) from "
2292                           VPPCOM_ENV_APP_SCOPE_GLOBAL
2293                           "!", getpid (), vcm->cfg.app_scope_global);
2294         }
2295
2296       vcm->main_cpu = os_get_thread_index ();
2297       heap = clib_mem_get_per_cpu_heap ();
2298       h = mheap_header (heap);
2299
2300       /* make the main heap thread-safe */
2301       h->flags |= MHEAP_FLAG_THREAD_SAFE;
2302
2303       vcm->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
2304
2305       clib_time_init (&vcm->clib_time);
2306       vppcom_init_error_string_table ();
2307       svm_fifo_segment_main_init (vcl_cfg->segment_baseva,
2308                                   20 /* timeout in secs */ );
2309       clib_spinlock_init (&vcm->sessions_lockp);
2310     }
2311
2312   if (vcm->my_client_index == ~0)
2313     {
2314       vppcom_api_hookup ();
2315       vcm->app_state = STATE_APP_START;
2316       rv = vppcom_connect_to_vpp (app_name);
2317       if (rv)
2318         {
2319           clib_warning ("VCL<%d>: ERROR: couldn't connect to VPP!",
2320                         getpid ());
2321           return rv;
2322         }
2323
2324       if (VPPCOM_DEBUG > 0)
2325         clib_warning ("VCL<%d>: sending session enable", getpid ());
2326
2327       rv = vppcom_app_session_enable ();
2328       if (rv)
2329         {
2330           clib_warning ("VCL<%d>: ERROR: vppcom_app_session_enable() "
2331                         "failed!", getpid ());
2332           return rv;
2333         }
2334
2335       if (VPPCOM_DEBUG > 0)
2336         clib_warning ("VCL<%d>: sending app attach", getpid ());
2337
2338       rv = vppcom_app_attach ();
2339       if (rv)
2340         {
2341           clib_warning ("VCL<%d>: ERROR: vppcom_app_attach() failed!",
2342                         getpid ());
2343           return rv;
2344         }
2345
2346       if (VPPCOM_DEBUG > 0)
2347         clib_warning ("VCL<%d>: app_name '%s', my_client_index %d (0x%x)",
2348                       getpid (), app_name, vcm->my_client_index,
2349                       vcm->my_client_index);
2350     }
2351
2352   return VPPCOM_OK;
2353 }
2354
2355 void
2356 vppcom_app_destroy (void)
2357 {
2358   int rv;
2359
2360   if (vcm->my_client_index == ~0)
2361     return;
2362
2363   if (VPPCOM_DEBUG > 0)
2364     clib_warning ("VCL<%d>: detaching from VPP, my_client_index %d (0x%x)",
2365                   getpid (), vcm->my_client_index, vcm->my_client_index);
2366
2367   if (VPPCOM_DEBUG > 0)
2368     {
2369       /* *INDENT-OFF* */
2370       ELOG_TYPE_DECLARE (e) =
2371       {
2372         .format = "app_detach:C:%d",
2373         .format_args = "i4",
2374       };
2375
2376       struct
2377       {
2378         u32 data;
2379       } *ed;
2380       ed = ELOG_TRACK_DATA (&vcm->elog_main, e, vcm->elog_track);
2381       ed->data = vcm->my_client_index;
2382       /* *INDENT-ON* */
2383     }
2384
2385   vppcom_app_detach ();
2386   rv = vppcom_wait_for_app_state_change (STATE_APP_ENABLED);
2387   if (PREDICT_FALSE (rv))
2388     {
2389       if (VPPCOM_DEBUG > 0)
2390         clib_warning ("VCL<%d>: application detach timed out! "
2391                       "returning %d (%s)",
2392                       getpid (), rv, vppcom_retval_str (rv));
2393     }
2394
2395   /* Finished with logging before client gets reset to ~0 */
2396   if (VPPCOM_DEBUG > 0)
2397     write_elog ();
2398
2399   vl_client_disconnect_from_vlib ();
2400   vcm->my_client_index = ~0;
2401   vcm->app_state = STATE_APP_START;
2402 }
2403
2404 int
2405 vppcom_session_create (u8 proto, u8 is_nonblocking)
2406 {
2407   session_t *session;
2408   u32 session_index;
2409   session_state_t state;
2410   elog_track_t session_elog_track;
2411
2412   clib_spinlock_lock (&vcm->sessions_lockp);
2413   pool_get (vcm->sessions, session);
2414   memset (session, 0, sizeof (*session));
2415   session_index = session - vcm->sessions;
2416
2417   session->proto = proto;
2418   session->state = STATE_START;
2419   state = session->state;
2420   session->vpp_handle = ~0;
2421
2422   if (is_nonblocking)
2423     VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK);
2424   else
2425     VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_NONBLOCK);
2426
2427   if (VPPCOM_DEBUG > 0)
2428     {
2429       session->elog_track.name =
2430         (char *) format (0, "C:%d:S:%d%c", vcm->my_client_index,
2431                          session_index, 0);
2432       elog_track_register (&vcm->elog_main, &session->elog_track);
2433       session_elog_track = session->elog_track;
2434     }
2435
2436   clib_spinlock_unlock (&vcm->sessions_lockp);
2437
2438   if (VPPCOM_DEBUG > 0)
2439     clib_warning ("VCL<%d>: sid %u", getpid (), session_index);
2440
2441   if (VPPCOM_DEBUG > 0)
2442     {
2443       /* *INDENT-OFF* */
2444       ELOG_TYPE_DECLARE (e) =
2445       {
2446         .format = "session_create:proto:%d state:%d is_nonblocking:%d",
2447         .format_args = "i4i4i4",
2448       };
2449
2450       struct
2451       {
2452         u32 data[3];
2453       } *ed;
2454
2455       ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session_elog_track);
2456       ed->data[0] = proto;
2457       ed->data[1] = state;
2458       ed->data[2] = is_nonblocking;
2459       /* *INDENT-ON* */
2460     }
2461
2462   return (int) session_index;
2463 }
2464
2465 int
2466 vppcom_session_close (uint32_t session_index)
2467 {
2468   session_t *session = 0;
2469   int rv;
2470   u8 is_vep;
2471   u8 is_vep_session;
2472   u32 next_sid;
2473   u32 vep_idx;
2474   u64 vpp_handle;
2475   uword *p;
2476   session_state_t state;
2477   elog_track_t session_elog_track;
2478
2479   VCL_LOCK_AND_GET_SESSION (session_index, &session);
2480   is_vep = session->is_vep;
2481   is_vep_session = session->is_vep_session;
2482   next_sid = session->vep.next_sid;
2483   vep_idx = session->vep.vep_idx;
2484   state = session->state;
2485   vpp_handle = session->vpp_handle;
2486   clib_spinlock_unlock (&vcm->sessions_lockp);
2487
2488   /*
2489    * Why two if(VPPCOM_DEBUG) checks?
2490    *
2491    * Eventually all clib_warnings need their own way of being
2492    * logged and signalled (like severity) where event logging
2493    * is a separate debugging tool. It will make the separation
2494    * easier. ... parting is such sweet sorrow ...
2495    */
2496   if (VPPCOM_DEBUG > 0)
2497     {
2498       session_elog_track = session->elog_track;
2499     }
2500
2501   if (VPPCOM_DEBUG > 0)
2502     {
2503       if (is_vep)
2504         clib_warning ("VCL<%d>: vep_idx %u / sid %u: "
2505                       "closing epoll session...",
2506                       getpid (), session_index, session_index);
2507       else
2508         clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %d: "
2509                       "closing session...",
2510                       getpid (), vpp_handle, session_index);
2511     }
2512
2513   if (is_vep)
2514     {
2515       while (next_sid != ~0)
2516         {
2517           rv = vppcom_epoll_ctl (session_index, EPOLL_CTL_DEL, next_sid, 0);
2518           if ((VPPCOM_DEBUG > 0) && PREDICT_FALSE (rv < 0))
2519             clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
2520                           "EPOLL_CTL_DEL vep_idx %u failed! rv %d (%s)",
2521                           getpid (), vpp_handle, next_sid, vep_idx,
2522                           rv, vppcom_retval_str (rv));
2523
2524           VCL_LOCK_AND_GET_SESSION (session_index, &session);
2525           next_sid = session->vep.next_sid;
2526           clib_spinlock_unlock (&vcm->sessions_lockp);
2527         }
2528     }
2529   else
2530     {
2531       if (is_vep_session)
2532         {
2533           rv = vppcom_epoll_ctl (vep_idx, EPOLL_CTL_DEL, session_index, 0);
2534           if ((VPPCOM_DEBUG > 0) && (rv < 0))
2535             clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
2536                           "EPOLL_CTL_DEL vep_idx %u failed! rv %d (%s)",
2537                           getpid (), vpp_handle, session_index,
2538                           vep_idx, rv, vppcom_retval_str (rv));
2539         }
2540
2541       if (state & STATE_LISTEN)
2542         {
2543           rv = vppcom_session_unbind (session_index);
2544           if (PREDICT_FALSE (rv < 0))
2545             {
2546               if (VPPCOM_DEBUG > 0)
2547                 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
2548                               "listener unbind failed! rv %d (%s)",
2549                               getpid (), vpp_handle, session_index,
2550                               rv, vppcom_retval_str (rv));
2551             }
2552         }
2553
2554       else if (state & (CLIENT_STATE_OPEN | SERVER_STATE_OPEN))
2555         {
2556           rv = vppcom_session_disconnect (session_index);
2557           if (PREDICT_FALSE (rv < 0))
2558             clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
2559                           "session disconnect failed! rv %d (%s)",
2560                           getpid (), vpp_handle, session_index,
2561                           rv, vppcom_retval_str (rv));
2562         }
2563     }
2564
2565   VCL_LOCK_AND_GET_SESSION (session_index, &session);
2566   vpp_handle = session->vpp_handle;
2567   if (vpp_handle != ~0)
2568     {
2569       p = hash_get (vcm->session_index_by_vpp_handles, vpp_handle);
2570       if (p)
2571         hash_unset (vcm->session_index_by_vpp_handles, vpp_handle);
2572     }
2573   pool_put_index (vcm->sessions, session_index);
2574
2575   clib_spinlock_unlock (&vcm->sessions_lockp);
2576
2577   if (VPPCOM_DEBUG > 0)
2578     {
2579       if (is_vep)
2580         clib_warning ("VCL<%d>: vep_idx %u / sid %u: epoll session removed.",
2581                       getpid (), session_index, session_index);
2582       else
2583         clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: session removed.",
2584                       getpid (), vpp_handle, session_index);
2585     }
2586 done:
2587
2588   if (VPPCOM_DEBUG > 0)
2589     {
2590       /* *INDENT-OFF* */
2591       ELOG_TYPE_DECLARE (e) =
2592       {
2593         .format = "session_close:rv:%d",
2594         .format_args = "i4",
2595       };
2596
2597       struct
2598       {
2599         u32 data;
2600       } *ed;
2601
2602       ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session_elog_track);
2603       ed->data = rv;
2604       /* *INDENT-ON* */
2605     }
2606
2607   return rv;
2608 }
2609
2610 int
2611 vppcom_session_bind (uint32_t session_index, vppcom_endpt_t * ep)
2612 {
2613   session_t *session = 0;
2614   int rv;
2615
2616   if (!ep || !ep->ip)
2617     return VPPCOM_EINVAL;
2618
2619   VCL_LOCK_AND_GET_SESSION (session_index, &session);
2620
2621   if (session->is_vep)
2622     {
2623       clib_spinlock_unlock (&vcm->sessions_lockp);
2624       clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
2625                     "bind to an epoll session!", getpid (), session_index);
2626       rv = VPPCOM_EBADFD;
2627       goto done;
2628     }
2629
2630   session->lcl_addr.is_ip4 = ep->is_ip4;
2631   session->lcl_addr.ip46 = to_ip46 (!ep->is_ip4, ep->ip);
2632   session->lcl_port = ep->port;
2633
2634   if (VPPCOM_DEBUG > 0)
2635     clib_warning ("VCL<%d>: sid %u: binding to local %s address %U "
2636                   "port %u, proto %s", getpid (), session_index,
2637                   session->lcl_addr.is_ip4 ? "IPv4" : "IPv6",
2638                   format_ip46_address, &session->lcl_addr.ip46,
2639                   session->lcl_addr.is_ip4,
2640                   clib_net_to_host_u16 (session->lcl_port),
2641                   session->proto ? "UDP" : "TCP");
2642
2643   if (VPPCOM_DEBUG > 0)
2644     {
2645       if (session->lcl_addr.is_ip4)
2646         {
2647           /* *INDENT-OFF* */
2648           ELOG_TYPE_DECLARE (e) =
2649           {
2650             .format = "bind local:%s:%d.%d.%d.%d:%d ",
2651             .format_args = "t1i1i1i1i1i2",
2652             .n_enum_strings = 2,
2653             .enum_strings = {"TCP", "UDP",},
2654           };
2655
2656           CLIB_PACKED (struct {
2657             u8 proto;
2658             u8 addr[4];
2659             u16 port;
2660           }) * ed;
2661
2662           ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
2663           ed->proto = session->proto;
2664           ed->addr[0] = session->lcl_addr.ip46.ip4.as_u8[0];
2665           ed->addr[1] = session->lcl_addr.ip46.ip4.as_u8[1];
2666           ed->addr[2] = session->lcl_addr.ip46.ip4.as_u8[2];
2667           ed->addr[3] = session->lcl_addr.ip46.ip4.as_u8[3];
2668           ed->port = clib_net_to_host_u16 (session->lcl_port);
2669           /* *INDENT-ON* */
2670         }
2671     }
2672
2673   clib_spinlock_unlock (&vcm->sessions_lockp);
2674 done:
2675   return rv;
2676 }
2677
2678 int
2679 vppcom_session_listen (uint32_t listen_session_index, uint32_t q_len)
2680 {
2681   session_t *listen_session = 0;
2682   u64 listen_vpp_handle;
2683   int rv, retval;
2684
2685   if (q_len == 0 || q_len == ~0)
2686     q_len = vcm->cfg.listen_queue_size;
2687
2688   VCL_LOCK_AND_GET_SESSION (listen_session_index, &listen_session);
2689
2690   if (listen_session->is_vep)
2691     {
2692       clib_spinlock_unlock (&vcm->sessions_lockp);
2693       clib_warning ("VCL<%d>: ERROR: sid %u: cannot listen on an "
2694                     "epoll session!", getpid (), listen_session_index);
2695       rv = VPPCOM_EBADFD;
2696       goto done;
2697     }
2698
2699   listen_vpp_handle = listen_session->vpp_handle;
2700   if (listen_session->state & STATE_LISTEN)
2701     {
2702       clib_spinlock_unlock (&vcm->sessions_lockp);
2703       if (VPPCOM_DEBUG > 0)
2704         clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
2705                       "already in listen state!",
2706                       getpid (), listen_vpp_handle, listen_session_index);
2707       rv = VPPCOM_OK;
2708       goto done;
2709     }
2710
2711   if (VPPCOM_DEBUG > 0)
2712     clib_warning ("VCL<%d>: vpp handle 0x%llx, "
2713                   "sid %u: sending bind request...",
2714                   getpid (), listen_vpp_handle, listen_session_index);
2715
2716   vppcom_send_bind_sock (listen_session, listen_session_index);
2717   clib_spinlock_unlock (&vcm->sessions_lockp);
2718   retval =
2719     vppcom_wait_for_session_state_change (listen_session_index, STATE_LISTEN,
2720                                           vcm->cfg.session_timeout);
2721
2722   VCL_LOCK_AND_GET_SESSION (listen_session_index, &listen_session);
2723   if (PREDICT_FALSE (retval))
2724     {
2725       if (VPPCOM_DEBUG > 0)
2726         clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: bind failed! "
2727                       "returning %d (%s)", getpid (),
2728                       listen_session->vpp_handle, listen_session_index,
2729                       retval, vppcom_retval_str (retval));
2730       clib_spinlock_unlock (&vcm->sessions_lockp);
2731       rv = retval;
2732       goto done;
2733     }
2734
2735   clib_fifo_validate (vcm->client_session_index_fifo, q_len);
2736   clib_spinlock_unlock (&vcm->sessions_lockp);
2737 done:
2738   return rv;
2739 }
2740
2741 int
2742 vppcom_session_accept (uint32_t listen_session_index, vppcom_endpt_t * ep,
2743                        uint32_t flags)
2744 {
2745   session_t *listen_session = 0;
2746   session_t *client_session = 0;
2747   u32 client_session_index = ~0;
2748   int rv;
2749   f64 wait_for;
2750   u64 listen_vpp_handle;
2751
2752   VCL_LOCK_AND_GET_SESSION (listen_session_index, &listen_session);
2753
2754   if (listen_session->is_vep)
2755     {
2756       clib_spinlock_unlock (&vcm->sessions_lockp);
2757       clib_warning ("VCL<%d>: ERROR: sid %u: cannot accept on an "
2758                     "epoll session!", getpid (), listen_session_index);
2759       rv = VPPCOM_EBADFD;
2760       goto done;
2761     }
2762
2763   listen_vpp_handle = listen_session->vpp_handle;
2764   if (listen_session->state != STATE_LISTEN)
2765     {
2766       clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
2767                     "not in listen state! state 0x%x (%s)", getpid (),
2768                     listen_vpp_handle, listen_session_index,
2769                     listen_session->state,
2770                     vppcom_session_state_str (listen_session->state));
2771       clib_spinlock_unlock (&vcm->sessions_lockp);
2772       rv = VPPCOM_EBADFD;
2773       goto done;
2774     }
2775   wait_for = (VCL_SESS_ATTR_TEST (listen_session->attr,
2776                                   VCL_SESS_ATTR_NONBLOCK))
2777     ? 0 : vcm->cfg.accept_timeout;
2778
2779   clib_spinlock_unlock (&vcm->sessions_lockp);
2780
2781   while (1)
2782     {
2783       rv = vppcom_wait_for_client_session_index (wait_for);
2784       if (rv)
2785         {
2786           if ((VPPCOM_DEBUG > 0))
2787             clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
2788                           "accept failed! returning %d (%s)", getpid (),
2789                           listen_vpp_handle, listen_session_index,
2790                           rv, vppcom_retval_str (rv));
2791           if (wait_for == 0)
2792             goto done;
2793         }
2794       else
2795         break;
2796     }
2797
2798   clib_spinlock_lock (&vcm->sessions_lockp);
2799   clib_fifo_sub1 (vcm->client_session_index_fifo, client_session_index);
2800   rv = vppcom_session_at_index (client_session_index, &client_session);
2801   if (PREDICT_FALSE (rv))
2802     {
2803       rv = VPPCOM_ECONNABORTED;
2804       clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: client sid %u "
2805                     "lookup failed! returning %d (%s)", getpid (),
2806                     listen_vpp_handle, listen_session_index,
2807                     client_session_index, rv, vppcom_retval_str (rv));
2808       goto done;
2809     }
2810
2811   if (flags & O_NONBLOCK)
2812     VCL_SESS_ATTR_SET (client_session->attr, VCL_SESS_ATTR_NONBLOCK);
2813   else
2814     VCL_SESS_ATTR_CLR (client_session->attr, VCL_SESS_ATTR_NONBLOCK);
2815
2816   if (VPPCOM_DEBUG > 0)
2817     clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: Got a client request! "
2818                   "vpp handle 0x%llx, sid %u, flags %d, is_nonblocking %u",
2819                   getpid (), listen_vpp_handle, listen_session_index,
2820                   client_session->vpp_handle, client_session_index,
2821                   flags, VCL_SESS_ATTR_TEST (client_session->attr,
2822                                              VCL_SESS_ATTR_NONBLOCK));
2823
2824   if (ep)
2825     {
2826       ep->is_ip4 = client_session->peer_addr.is_ip4;
2827       ep->port = client_session->peer_port;
2828       if (client_session->peer_addr.is_ip4)
2829         clib_memcpy (ep->ip, &client_session->peer_addr.ip46.ip4,
2830                      sizeof (ip4_address_t));
2831       else
2832         clib_memcpy (ep->ip, &client_session->peer_addr.ip46.ip6,
2833                      sizeof (ip6_address_t));
2834     }
2835
2836   vppcom_send_accept_session_reply (client_session->vpp_handle,
2837                                     client_session->client_context,
2838                                     0 /* retval OK */ );
2839
2840   if (VPPCOM_DEBUG > 0)
2841     clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: accepted vpp handle "
2842                   "0x%llx, sid %u connection to local %s address "
2843                   "%U port %u", getpid (), listen_vpp_handle,
2844                   listen_session_index, client_session->vpp_handle,
2845                   client_session_index,
2846                   client_session->lcl_addr.is_ip4 ? "IPv4" : "IPv6",
2847                   format_ip46_address, &client_session->lcl_addr.ip46,
2848                   client_session->lcl_addr.is_ip4,
2849                   clib_net_to_host_u16 (client_session->lcl_port));
2850
2851   if (VPPCOM_DEBUG > 0)
2852     {
2853       client_session->elog_track.name =
2854         (char *) format (0, "C:%d:S:%d%c", vcm->my_client_index,
2855                          client_session_index, 0);
2856       elog_track_register (&vcm->elog_main, &client_session->elog_track);
2857
2858       // Two elog entries due to 20-byte per entry constraint.
2859       /* *INDENT-OFF* */
2860       ELOG_TYPE_DECLARE (e) =
2861       {
2862         .format = "accept: listen_handle:%x from_handle:%x",
2863         .format_args = "i8i8",
2864       };
2865
2866       struct
2867       {
2868         u64 handle[2];
2869       } *ed;
2870
2871       ed = ELOG_TRACK_DATA (&vcm->elog_main, e, client_session->elog_track);
2872       ed->handle[0] = listen_vpp_handle;
2873       ed->handle[1] = client_session->vpp_handle;
2874       /* *INDENT-ON* */
2875
2876       if (client_session->lcl_addr.is_ip4)
2877         {
2878           /* *INDENT-OFF* */
2879           ELOG_TYPE_DECLARE (e2) =
2880           {
2881             .format = "accept: S:%d %d.%d.%d.%d:%d ",
2882             .format_args = "i4i1i1i1i1i2",
2883           };
2884
2885           CLIB_PACKED (struct {
2886             u32 session;
2887             u8 addr[4];
2888             u16 port;
2889           }) * ed2;
2890
2891           ed2 =
2892             ELOG_TRACK_DATA (&vcm->elog_main, e2, client_session->elog_track);
2893           ed2->session = client_session_index;
2894           ed2->addr[0] = client_session->lcl_addr.ip46.ip4.as_u8[0];
2895           ed2->addr[1] = client_session->lcl_addr.ip46.ip4.as_u8[1];
2896           ed2->addr[2] = client_session->lcl_addr.ip46.ip4.as_u8[2];
2897           ed2->addr[3] = client_session->lcl_addr.ip46.ip4.as_u8[3];
2898           ed2->port = clib_net_to_host_u16 (client_session->lcl_port);
2899           /* *INDENT-ON* */
2900         }
2901     }
2902
2903   clib_spinlock_unlock (&vcm->sessions_lockp);
2904   rv = (int) client_session_index;
2905 done:
2906   return rv;
2907 }
2908
2909 int
2910 vppcom_session_connect (uint32_t session_index, vppcom_endpt_t * server_ep)
2911 {
2912   session_t *session = 0;
2913   u64 vpp_handle = 0;
2914   int rv, retval = VPPCOM_OK;
2915
2916   VCL_LOCK_AND_GET_SESSION (session_index, &session);
2917
2918   if (PREDICT_FALSE (session->is_vep))
2919     {
2920       clib_spinlock_unlock (&vcm->sessions_lockp);
2921       clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
2922                     "connect on an epoll session!", getpid (), session_index);
2923       rv = VPPCOM_EBADFD;
2924       goto done;
2925     }
2926
2927   if (PREDICT_FALSE (session->state & CLIENT_STATE_OPEN))
2928     {
2929       if (VPPCOM_DEBUG > 0)
2930         clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: session already "
2931                       "connected to %s %U port %d proto %s, state 0x%x (%s)",
2932                       getpid (), session->vpp_handle, session_index,
2933                       session->peer_addr.is_ip4 ? "IPv4" : "IPv6",
2934                       format_ip46_address,
2935                       &session->peer_addr.ip46, session->peer_addr.is_ip4,
2936                       clib_net_to_host_u16 (session->peer_port),
2937                       session->proto ? "UDP" : "TCP", session->state,
2938                       vppcom_session_state_str (session->state));
2939
2940       clib_spinlock_unlock (&vcm->sessions_lockp);
2941       goto done;
2942     }
2943
2944   session->peer_addr.is_ip4 = server_ep->is_ip4;
2945   session->peer_addr.ip46 = to_ip46 (!server_ep->is_ip4, server_ep->ip);
2946   session->peer_port = server_ep->port;
2947
2948   if (VPPCOM_DEBUG > 0)
2949     clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: connecting to server "
2950                   "%s %U port %d proto %s",
2951                   getpid (), session->vpp_handle, session_index,
2952                   session->peer_addr.is_ip4 ? "IPv4" : "IPv6",
2953                   format_ip46_address,
2954                   &session->peer_addr.ip46, session->peer_addr.is_ip4,
2955                   clib_net_to_host_u16 (session->peer_port),
2956                   session->proto ? "UDP" : "TCP");
2957
2958   vppcom_send_connect_sock (session, session_index);
2959   clib_spinlock_unlock (&vcm->sessions_lockp);
2960
2961   retval =
2962     vppcom_wait_for_session_state_change (session_index, STATE_CONNECT,
2963                                           vcm->cfg.session_timeout);
2964
2965   VCL_LOCK_AND_GET_SESSION (session_index, &session);
2966   vpp_handle = session->vpp_handle;
2967   clib_spinlock_unlock (&vcm->sessions_lockp);
2968
2969 done:
2970   if (PREDICT_FALSE (retval))
2971     {
2972       rv = retval;
2973       if (VPPCOM_DEBUG > 0)
2974         {
2975           if (session)
2976             clib_warning
2977               ("VCL<%d>: vpp handle 0x%llx, sid %u: connect failed! "
2978                "returning %d (%s)", getpid (), vpp_handle,
2979                session_index, rv, vppcom_retval_str (rv));
2980           else
2981             clib_warning ("VCL<%d>: no session for sid %u: connect failed! "
2982                           "returning %d (%s)", getpid (),
2983                           session_index, rv, vppcom_retval_str (rv));
2984         }
2985     }
2986   else if (VPPCOM_DEBUG > 0)
2987     clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: connected!",
2988                   getpid (), vpp_handle, session_index);
2989
2990   return rv;
2991 }
2992
2993 static inline int
2994 vppcom_session_read_internal (uint32_t session_index, void *buf, int n,
2995                               u8 peek)
2996 {
2997   session_t *session = 0;
2998   svm_fifo_t *rx_fifo;
2999   int n_read = 0;
3000   int rv;
3001   int is_nonblocking;
3002
3003   u64 vpp_handle;
3004   u32 poll_et;
3005   session_state_t state;
3006
3007   ASSERT (buf);
3008
3009   VCL_LOCK_AND_GET_SESSION (session_index, &session);
3010
3011   is_nonblocking = VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK);
3012   rx_fifo = session->rx_fifo;
3013   state = session->state;
3014   vpp_handle = session->vpp_handle;
3015
3016   if (PREDICT_FALSE (session->is_vep))
3017     {
3018       clib_spinlock_unlock (&vcm->sessions_lockp);
3019       clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
3020                     "read from an epoll session!", getpid (), session_index);
3021       rv = VPPCOM_EBADFD;
3022       goto done;
3023     }
3024
3025   if (PREDICT_FALSE (!(state & (SERVER_STATE_OPEN | CLIENT_STATE_OPEN))))
3026     {
3027       clib_spinlock_unlock (&vcm->sessions_lockp);
3028       rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
3029
3030       if (VPPCOM_DEBUG > 0)
3031         clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: %s session is "
3032                       "not open! state 0x%x (%s), returning %d (%s)",
3033                       getpid (), vpp_handle, session_index, state,
3034                       vppcom_session_state_str (state),
3035                       rv, vppcom_retval_str (rv));
3036       goto done;
3037     }
3038
3039   clib_spinlock_unlock (&vcm->sessions_lockp);
3040
3041   do
3042     {
3043       if (peek)
3044         n_read = svm_fifo_peek (rx_fifo, 0, n, buf);
3045       else
3046         n_read = svm_fifo_dequeue_nowait (rx_fifo, n, buf);
3047     }
3048   while (!is_nonblocking && (n_read <= 0));
3049
3050   if (n_read <= 0)
3051     {
3052       VCL_LOCK_AND_GET_SESSION (session_index, &session);
3053
3054       poll_et = (((EPOLLET | EPOLLIN) & session->vep.ev.events) ==
3055                  (EPOLLET | EPOLLIN));
3056       if (poll_et)
3057         session->vep.et_mask |= EPOLLIN;
3058
3059       if (state & STATE_CLOSE_ON_EMPTY)
3060         {
3061           rv = VPPCOM_ECONNRESET;
3062
3063           if (VPPCOM_DEBUG > 1)
3064             {
3065               clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: Empty fifo "
3066                             "with session state 0x%x (%s)!"
3067                             "  Setting state to 0x%x (%s), returning %d (%s)",
3068                             getpid (), session->vpp_handle, session_index,
3069                             state, vppcom_session_state_str (state),
3070                             STATE_DISCONNECT,
3071                             vppcom_session_state_str (STATE_DISCONNECT), rv,
3072                             vppcom_retval_str (rv));
3073             }
3074
3075           session->state = STATE_DISCONNECT;
3076         }
3077       else
3078         rv = VPPCOM_EAGAIN;
3079
3080       clib_spinlock_unlock (&vcm->sessions_lockp);
3081     }
3082   else
3083     rv = n_read;
3084
3085   if (VPPCOM_DEBUG > 2)
3086     {
3087       if (rv > 0)
3088         clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: read %d bytes "
3089                       "from (%p)", getpid (), vpp_handle,
3090                       session_index, n_read, rx_fifo);
3091       else
3092         clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: nothing read! "
3093                       "returning %d (%s)", getpid (), vpp_handle,
3094                       session_index, rv, vppcom_retval_str (rv));
3095     }
3096 done:
3097   return rv;
3098 }
3099
3100 int
3101 vppcom_session_read (uint32_t session_index, void *buf, size_t n)
3102 {
3103   return (vppcom_session_read_internal (session_index, buf, n, 0));
3104 }
3105
3106 static int
3107 vppcom_session_peek (uint32_t session_index, void *buf, int n)
3108 {
3109   return (vppcom_session_read_internal (session_index, buf, n, 1));
3110 }
3111
3112 static inline int
3113 vppcom_session_read_ready (session_t * session, u32 session_index)
3114 {
3115   int ready = 0;
3116   u32 poll_et;
3117   int rv;
3118   session_state_t state = session->state;
3119   u64 vpp_handle = session->vpp_handle;
3120
3121   /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
3122   if (PREDICT_FALSE (session->is_vep))
3123     {
3124       clib_warning ("VCL<%d>: ERROR: sid %u: cannot read from an "
3125                     "epoll session!", getpid (), session_index);
3126       rv = VPPCOM_EBADFD;
3127       goto done;
3128     }
3129
3130   if (session->state & STATE_LISTEN)
3131     ready = clib_fifo_elts (vcm->client_session_index_fifo);
3132   else
3133     {
3134       if (!(state & (SERVER_STATE_OPEN | CLIENT_STATE_OPEN | STATE_LISTEN)))
3135         {
3136           rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET :
3137                 VPPCOM_ENOTCONN);
3138
3139           if (VPPCOM_DEBUG > 1)
3140             clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: session is "
3141                           "not open! state 0x%x (%s), returning %d (%s)",
3142                           getpid (), vpp_handle, session_index,
3143                           state, vppcom_session_state_str (state),
3144                           rv, vppcom_retval_str (rv));
3145           goto done;
3146         }
3147
3148       ready = svm_fifo_max_dequeue (session->rx_fifo);
3149     }
3150
3151   if (ready == 0)
3152     {
3153       poll_et =
3154         ((EPOLLET | EPOLLIN) & session->vep.ev.events) == (EPOLLET | EPOLLIN);
3155       if (poll_et)
3156         session->vep.et_mask |= EPOLLIN;
3157
3158       if (state & STATE_CLOSE_ON_EMPTY)
3159         {
3160           rv = VPPCOM_ECONNRESET;
3161
3162           if (VPPCOM_DEBUG > 1)
3163             {
3164               clib_warning ("VCL<%d>: vpp handle 0x%llx, "
3165                             "sid %u: Empty fifo with"
3166                             " session state 0x%x (%s)! Setting state to "
3167                             "0x%x (%s), returning %d (%s)",
3168                             getpid (), session_index, vpp_handle,
3169                             state, vppcom_session_state_str (state),
3170                             STATE_DISCONNECT,
3171                             vppcom_session_state_str (STATE_DISCONNECT), rv,
3172                             vppcom_retval_str (rv));
3173             }
3174           session->state = STATE_DISCONNECT;
3175           goto done;
3176         }
3177     }
3178   rv = ready;
3179
3180   if (vcm->app_event_queue->cursize &&
3181       !pthread_mutex_trylock (&vcm->app_event_queue->mutex))
3182     {
3183       u32 i, n_to_dequeue = vcm->app_event_queue->cursize;
3184       session_fifo_event_t e;
3185
3186       for (i = 0; i < n_to_dequeue; i++)
3187         svm_queue_sub_raw (vcm->app_event_queue, (u8 *) & e);
3188
3189       pthread_mutex_unlock (&vcm->app_event_queue->mutex);
3190     }
3191 done:
3192   return rv;
3193 }
3194
3195 int
3196 vppcom_session_write (uint32_t session_index, void *buf, size_t n)
3197 {
3198   session_t *session = 0;
3199   svm_fifo_t *tx_fifo = 0;
3200   svm_queue_t *q;
3201   session_fifo_event_t evt;
3202   session_state_t state;
3203   int rv, n_write, is_nonblocking;
3204   u32 poll_et;
3205   u64 vpp_handle;
3206
3207   ASSERT (buf);
3208
3209   VCL_LOCK_AND_GET_SESSION (session_index, &session);
3210
3211   tx_fifo = session->tx_fifo;
3212   is_nonblocking = VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK);
3213   vpp_handle = session->vpp_handle;
3214   state = session->state;
3215
3216   if (PREDICT_FALSE (session->is_vep))
3217     {
3218       clib_spinlock_unlock (&vcm->sessions_lockp);
3219       clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
3220                     "cannot write to an epoll session!",
3221                     getpid (), vpp_handle, session_index);
3222
3223       rv = VPPCOM_EBADFD;
3224       goto done;
3225     }
3226
3227   if (!(session->state & (SERVER_STATE_OPEN | CLIENT_STATE_OPEN)))
3228     {
3229       rv =
3230         ((session->state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET :
3231          VPPCOM_ENOTCONN);
3232
3233       clib_spinlock_unlock (&vcm->sessions_lockp);
3234       if (VPPCOM_DEBUG > 1)
3235         clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
3236                       "session is not open! state 0x%x (%s)",
3237                       getpid (), vpp_handle, session_index,
3238                       state, vppcom_session_state_str (state));
3239       goto done;
3240     }
3241
3242   clib_spinlock_unlock (&vcm->sessions_lockp);
3243
3244   do
3245     {
3246       n_write = svm_fifo_enqueue_nowait (tx_fifo, n, (void *) buf);
3247     }
3248   while (!is_nonblocking && (n_write <= 0));
3249
3250   /* If event wasn't set, add one */
3251   if ((n_write > 0) && svm_fifo_set_event (tx_fifo))
3252     {
3253       /* Fabricate TX event, send to vpp */
3254       evt.fifo = tx_fifo;
3255       evt.event_type = FIFO_EVENT_APP_TX;
3256
3257       VCL_LOCK_AND_GET_SESSION (session_index, &session);
3258       q = session->vpp_event_queue;
3259       ASSERT (q);
3260       svm_queue_add (q, (u8 *) & evt, 0 /* do wait for mutex */ );
3261       clib_spinlock_unlock (&vcm->sessions_lockp);
3262       if (VPPCOM_DEBUG > 1)
3263         clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
3264                       "added FIFO_EVENT_APP_TX to "
3265                       "vpp_event_q %p, n_write %d", getpid (),
3266                       vpp_handle, session_index, q, n_write);
3267     }
3268
3269   if (n_write <= 0)
3270     {
3271       VCL_LOCK_AND_GET_SESSION (session_index, &session);
3272
3273       poll_et = (((EPOLLET | EPOLLOUT) & session->vep.ev.events) ==
3274                  (EPOLLET | EPOLLOUT));
3275       if (poll_et)
3276         session->vep.et_mask |= EPOLLOUT;
3277
3278       if (session->state & STATE_CLOSE_ON_EMPTY)
3279         {
3280           rv = VPPCOM_ECONNRESET;
3281
3282           if (VPPCOM_DEBUG > 1)
3283             {
3284               clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
3285                             "Empty fifo with session state 0x%x (%s)!"
3286                             "  Setting state to 0x%x (%s), returning %d (%s)",
3287                             getpid (), session->vpp_handle, session_index,
3288                             session->state,
3289                             vppcom_session_state_str (session->state),
3290                             STATE_DISCONNECT,
3291                             vppcom_session_state_str (STATE_DISCONNECT), rv,
3292                             vppcom_retval_str (rv));
3293             }
3294
3295           session->state = STATE_DISCONNECT;
3296         }
3297       else
3298         rv = VPPCOM_EAGAIN;
3299
3300       clib_spinlock_unlock (&vcm->sessions_lockp);
3301     }
3302   else
3303     rv = n_write;
3304
3305   if (VPPCOM_DEBUG > 2)
3306     {
3307       if (n_write <= 0)
3308         clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
3309                       "FIFO-FULL (%p)", getpid (), vpp_handle,
3310                       session_index, tx_fifo);
3311       else
3312         clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
3313                       "wrote %d bytes tx-fifo: (%p)", getpid (),
3314                       vpp_handle, session_index, n_write, tx_fifo);
3315     }
3316 done:
3317   return rv;
3318 }
3319
3320 static inline int
3321 vppcom_session_write_ready (session_t * session, u32 session_index)
3322 {
3323   int ready;
3324   u32 poll_et;
3325   int rv;
3326
3327   ASSERT (session);
3328
3329   /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
3330   if (PREDICT_FALSE (session->is_vep))
3331     {
3332       clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
3333                     "cannot write to an epoll session!",
3334                     getpid (), session->vpp_handle, session_index);
3335       rv = VPPCOM_EBADFD;
3336       goto done;
3337     }
3338
3339   if (PREDICT_FALSE (session->state & STATE_LISTEN))
3340     {
3341       clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
3342                     "cannot write to a listen session!",
3343                     getpid (), session->vpp_handle, session_index);
3344       rv = VPPCOM_EBADFD;
3345       goto done;
3346     }
3347
3348   if (!(session->state & (SERVER_STATE_OPEN | CLIENT_STATE_OPEN)))
3349     {
3350       session_state_t state = session->state;
3351
3352       rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
3353
3354       clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
3355                     "session is not open! state 0x%x (%s), "
3356                     "returning %d (%s)", getpid (), session->vpp_handle,
3357                     session_index,
3358                     state, vppcom_session_state_str (state),
3359                     rv, vppcom_retval_str (rv));
3360       goto done;
3361     }
3362
3363   ready = svm_fifo_max_enqueue (session->tx_fifo);
3364
3365   if (VPPCOM_DEBUG > 3)
3366     clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
3367                   "peek %s (%p), ready = %d", getpid (),
3368                   session->vpp_handle, session_index,
3369                   session->tx_fifo, ready);
3370
3371   if (ready == 0)
3372     {
3373       poll_et = (((EPOLLET | EPOLLOUT) & session->vep.ev.events) ==
3374                  (EPOLLET | EPOLLOUT));
3375       if (poll_et)
3376         session->vep.et_mask |= EPOLLOUT;
3377
3378       if (session->state & STATE_CLOSE_ON_EMPTY)
3379         {
3380           rv = VPPCOM_ECONNRESET;
3381
3382           if (VPPCOM_DEBUG > 1)
3383             {
3384               clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
3385                             "Empty fifo with session "
3386                             "state 0x%x (%s)! Setting state to 0x%x (%s), "
3387                             "returning %d (%s)", getpid (),
3388                             session->vpp_handle, session_index,
3389                             session->state,
3390                             vppcom_session_state_str (session->state),
3391                             STATE_DISCONNECT,
3392                             vppcom_session_state_str (STATE_DISCONNECT), rv,
3393                             vppcom_retval_str (rv));
3394             }
3395           session->state = STATE_DISCONNECT;
3396           goto done;
3397         }
3398     }
3399   rv = ready;
3400 done:
3401   return rv;
3402 }
3403
3404 int
3405 vppcom_select (unsigned long n_bits, unsigned long *read_map,
3406                unsigned long *write_map, unsigned long *except_map,
3407                double time_to_wait)
3408 {
3409   u32 session_index;
3410   session_t *session = 0;
3411   int rv, bits_set = 0;
3412   f64 timeout = clib_time_now (&vcm->clib_time) + time_to_wait;
3413   u32 minbits = clib_max (n_bits, BITS (uword));
3414
3415   ASSERT (sizeof (clib_bitmap_t) == sizeof (long int));
3416
3417   if (n_bits && read_map)
3418     {
3419       clib_bitmap_validate (vcm->rd_bitmap, minbits);
3420       clib_memcpy (vcm->rd_bitmap, read_map,
3421                    vec_len (vcm->rd_bitmap) * sizeof (clib_bitmap_t));
3422       memset (read_map, 0, vec_len (vcm->rd_bitmap) * sizeof (clib_bitmap_t));
3423     }
3424   if (n_bits && write_map)
3425     {
3426       clib_bitmap_validate (vcm->wr_bitmap, minbits);
3427       clib_memcpy (vcm->wr_bitmap, write_map,
3428                    vec_len (vcm->wr_bitmap) * sizeof (clib_bitmap_t));
3429       memset (write_map, 0,
3430               vec_len (vcm->wr_bitmap) * sizeof (clib_bitmap_t));
3431     }
3432   if (n_bits && except_map)
3433     {
3434       clib_bitmap_validate (vcm->ex_bitmap, minbits);
3435       clib_memcpy (vcm->ex_bitmap, except_map,
3436                    vec_len (vcm->ex_bitmap) * sizeof (clib_bitmap_t));
3437       memset (except_map, 0,
3438               vec_len (vcm->ex_bitmap) * sizeof (clib_bitmap_t));
3439     }
3440
3441   do
3442     {
3443       /* *INDENT-OFF* */
3444       if (n_bits)
3445         {
3446           if (read_map)
3447             {
3448               clib_bitmap_foreach (session_index, vcm->rd_bitmap,
3449                 ({
3450                   clib_spinlock_lock (&vcm->sessions_lockp);
3451                   rv = vppcom_session_at_index (session_index, &session);
3452                   if (rv < 0)
3453                     {
3454                       clib_spinlock_unlock (&vcm->sessions_lockp);
3455                       if (VPPCOM_DEBUG > 1)
3456                         clib_warning ("VCL<%d>: session %d specified in "
3457                                       "read_map is closed.", getpid (),
3458                                       session_index);
3459                       bits_set = VPPCOM_EBADFD;
3460                       goto select_done;
3461                     }
3462
3463                   rv = vppcom_session_read_ready (session, session_index);
3464                   clib_spinlock_unlock (&vcm->sessions_lockp);
3465                   if (except_map && vcm->ex_bitmap &&
3466                       clib_bitmap_get (vcm->ex_bitmap, session_index) &&
3467                       (rv < 0))
3468                     {
3469                       clib_bitmap_set_no_check (except_map, session_index, 1);
3470                       bits_set++;
3471                     }
3472                   else if (rv > 0)
3473                     {
3474                       clib_bitmap_set_no_check (read_map, session_index, 1);
3475                       bits_set++;
3476                     }
3477                 }));
3478             }
3479
3480           if (write_map)
3481             {
3482               clib_bitmap_foreach (session_index, vcm->wr_bitmap,
3483                 ({
3484                   clib_spinlock_lock (&vcm->sessions_lockp);
3485                   rv = vppcom_session_at_index (session_index, &session);
3486                   if (rv < 0)
3487                     {
3488                       clib_spinlock_unlock (&vcm->sessions_lockp);
3489                       if (VPPCOM_DEBUG > 0)
3490                         clib_warning ("VCL<%d>: session %d specified in "
3491                                       "write_map is closed.", getpid (),
3492                                       session_index);
3493                       bits_set = VPPCOM_EBADFD;
3494                       goto select_done;
3495                     }
3496
3497                   rv = vppcom_session_write_ready (session, session_index);
3498                   clib_spinlock_unlock (&vcm->sessions_lockp);
3499                   if (write_map && (rv > 0))
3500                     {
3501                       clib_bitmap_set_no_check (write_map, session_index, 1);
3502                       bits_set++;
3503                     }
3504                 }));
3505             }
3506
3507           if (except_map)
3508             {
3509               clib_bitmap_foreach (session_index, vcm->ex_bitmap,
3510                 ({
3511                   clib_spinlock_lock (&vcm->sessions_lockp);
3512                   rv = vppcom_session_at_index (session_index, &session);
3513                   if (rv < 0)
3514                     {
3515                       clib_spinlock_unlock (&vcm->sessions_lockp);
3516                       if (VPPCOM_DEBUG > 1)
3517                         clib_warning ("VCL<%d>: session %d specified in "
3518                                       "except_map is closed.", getpid (),
3519                                       session_index);
3520                       bits_set = VPPCOM_EBADFD;
3521                       goto select_done;
3522                     }
3523
3524                   rv = vppcom_session_read_ready (session, session_index);
3525                   clib_spinlock_unlock (&vcm->sessions_lockp);
3526                   if (rv < 0)
3527                     {
3528                       clib_bitmap_set_no_check (except_map, session_index, 1);
3529                       bits_set++;
3530                     }
3531                 }));
3532             }
3533         }
3534       /* *INDENT-ON* */
3535     }
3536   while ((time_to_wait == -1) || (clib_time_now (&vcm->clib_time) < timeout));
3537
3538 select_done:
3539   return (bits_set);
3540 }
3541
3542 static inline void
3543 vep_verify_epoll_chain (u32 vep_idx)
3544 {
3545   session_t *session;
3546   vppcom_epoll_t *vep;
3547   int rv;
3548   u32 sid = vep_idx;
3549
3550   if (VPPCOM_DEBUG <= 1)
3551     return;
3552
3553   /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
3554   rv = vppcom_session_at_index (vep_idx, &session);
3555   if (PREDICT_FALSE (rv))
3556     {
3557       clib_warning ("VCL<%d>: ERROR: Invalid vep_idx (%u)!",
3558                     getpid (), vep_idx);
3559       goto done;
3560     }
3561   if (PREDICT_FALSE (!session->is_vep))
3562     {
3563       clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
3564                     getpid (), vep_idx);
3565       goto done;
3566     }
3567   vep = &session->vep;
3568   clib_warning ("VCL<%d>: vep_idx (%u): Dumping epoll chain\n"
3569                 "{\n"
3570                 "   is_vep         = %u\n"
3571                 "   is_vep_session = %u\n"
3572                 "   next_sid       = 0x%x (%u)\n"
3573                 "   wait_cont_idx  = 0x%x (%u)\n"
3574                 "}\n", getpid (), vep_idx,
3575                 session->is_vep, session->is_vep_session,
3576                 vep->next_sid, vep->next_sid,
3577                 session->wait_cont_idx, session->wait_cont_idx);
3578
3579   for (sid = vep->next_sid; sid != ~0; sid = vep->next_sid)
3580     {
3581       rv = vppcom_session_at_index (sid, &session);
3582       if (PREDICT_FALSE (rv))
3583         {
3584           clib_warning ("VCL<%d>: ERROR: Invalid sid (%u)!", getpid (), sid);
3585           goto done;
3586         }
3587       if (PREDICT_FALSE (session->is_vep))
3588         clib_warning ("VCL<%d>: ERROR: sid (%u) is a vep!",
3589                       getpid (), vep_idx);
3590       else if (PREDICT_FALSE (!session->is_vep_session))
3591         {
3592           clib_warning ("VCL<%d>: ERROR: session (%u) "
3593                         "is not a vep session!", getpid (), sid);
3594           goto done;
3595         }
3596       vep = &session->vep;
3597       if (PREDICT_FALSE (vep->vep_idx != vep_idx))
3598         clib_warning ("VCL<%d>: ERROR: session (%u) vep_idx (%u) != "
3599                       "vep_idx (%u)!", getpid (),
3600                       sid, session->vep.vep_idx, vep_idx);
3601       if (session->is_vep_session)
3602         {
3603           clib_warning ("vep_idx[%u]: sid 0x%x (%u)\n"
3604                         "{\n"
3605                         "   next_sid       = 0x%x (%u)\n"
3606                         "   prev_sid       = 0x%x (%u)\n"
3607                         "   vep_idx        = 0x%x (%u)\n"
3608                         "   ev.events      = 0x%x\n"
3609                         "   ev.data.u64    = 0x%llx\n"
3610                         "   et_mask        = 0x%x\n"
3611                         "}\n",
3612                         vep_idx, sid, sid,
3613                         vep->next_sid, vep->next_sid,
3614                         vep->prev_sid, vep->prev_sid,
3615                         vep->vep_idx, vep->vep_idx,
3616                         vep->ev.events, vep->ev.data.u64, vep->et_mask);
3617         }
3618     }
3619
3620 done:
3621   clib_warning ("VCL<%d>: vep_idx (%u): Dump complete!\n",
3622                 getpid (), vep_idx);
3623 }
3624
3625 int
3626 vppcom_epoll_create (void)
3627 {
3628   session_t *vep_session;
3629   u32 vep_idx;
3630   elog_track_t vep_elog_track;
3631
3632   clib_spinlock_lock (&vcm->sessions_lockp);
3633   pool_get (vcm->sessions, vep_session);
3634   memset (vep_session, 0, sizeof (*vep_session));
3635   vep_idx = vep_session - vcm->sessions;
3636
3637   vep_session->is_vep = 1;
3638   vep_session->vep.vep_idx = ~0;
3639   vep_session->vep.next_sid = ~0;
3640   vep_session->vep.prev_sid = ~0;
3641   vep_session->wait_cont_idx = ~0;
3642   vep_session->vpp_handle = ~0;
3643
3644   if (VPPCOM_DEBUG > 0)
3645     {
3646       vep_session->elog_track.name =
3647         (char *) format (0, "C:%d:VEP:%d%c", vcm->my_client_index,
3648                          vep_idx, 0);
3649       elog_track_register (&vcm->elog_main, &vep_session->elog_track);
3650       vep_elog_track = vep_session->elog_track;
3651     }
3652
3653   clib_spinlock_unlock (&vcm->sessions_lockp);
3654
3655   if (VPPCOM_DEBUG > 0)
3656     clib_warning ("VCL<%d>: Created vep_idx %u / sid %u!",
3657                   getpid (), vep_idx, vep_idx);
3658
3659   if (VPPCOM_DEBUG > 0)
3660     {
3661
3662       /* *INDENT-OFF* */
3663       ELOG_TYPE_DECLARE (e) =
3664       {
3665         .format = "created epoll session:%d",
3666         .format_args = "i4",
3667       };
3668
3669       struct
3670       {
3671         u32 data;
3672       } *ed;
3673
3674       ed = ELOG_TRACK_DATA (&vcm->elog_main, e, vep_elog_track);
3675       ed->data = vep_idx;
3676       /* *INDENT-ON* */
3677     }
3678
3679   return (vep_idx);
3680 }
3681
3682 int
3683 vppcom_epoll_ctl (uint32_t vep_idx, int op, uint32_t session_index,
3684                   struct epoll_event *event)
3685 {
3686   session_t *vep_session;
3687   session_t *session;
3688   int rv;
3689
3690   if (vep_idx == session_index)
3691     {
3692       clib_warning ("VCL<%d>: ERROR: vep_idx == session_index (%u)!",
3693                     getpid (), vep_idx);
3694       return VPPCOM_EINVAL;
3695     }
3696
3697   clib_spinlock_lock (&vcm->sessions_lockp);
3698   rv = vppcom_session_at_index (vep_idx, &vep_session);
3699   if (PREDICT_FALSE (rv))
3700     {
3701       clib_warning ("VCL<%d>: ERROR: Invalid vep_idx (%u)!", vep_idx);
3702       goto done;
3703     }
3704   if (PREDICT_FALSE (!vep_session->is_vep))
3705     {
3706       clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
3707                     getpid (), vep_idx);
3708       rv = VPPCOM_EINVAL;
3709       goto done;
3710     }
3711
3712   ASSERT (vep_session->vep.vep_idx == ~0);
3713   ASSERT (vep_session->vep.prev_sid == ~0);
3714
3715   rv = vppcom_session_at_index (session_index, &session);
3716   if (PREDICT_FALSE (rv))
3717     {
3718       if (VPPCOM_DEBUG > 0)
3719         clib_warning ("VCL<%d>: ERROR: Invalid session_index (%u)!",
3720                       getpid (), session_index);
3721       goto done;
3722     }
3723   if (PREDICT_FALSE (session->is_vep))
3724     {
3725       clib_warning ("ERROR: session_index (%u) is a vep!", vep_idx);
3726       rv = VPPCOM_EINVAL;
3727       goto done;
3728     }
3729
3730   switch (op)
3731     {
3732     case EPOLL_CTL_ADD:
3733       if (PREDICT_FALSE (!event))
3734         {
3735           clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_ADD: NULL pointer to "
3736                         "epoll_event structure!", getpid ());
3737           rv = VPPCOM_EINVAL;
3738           goto done;
3739         }
3740       if (vep_session->vep.next_sid != ~0)
3741         {
3742           session_t *next_session;
3743           rv = vppcom_session_at_index (vep_session->vep.next_sid,
3744                                         &next_session);
3745           if (PREDICT_FALSE (rv))
3746             {
3747               clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_ADD: Invalid "
3748                             "vep.next_sid (%u) on vep_idx (%u)!",
3749                             getpid (), vep_session->vep.next_sid, vep_idx);
3750               goto done;
3751             }
3752           ASSERT (next_session->vep.prev_sid == vep_idx);
3753           next_session->vep.prev_sid = session_index;
3754         }
3755       session->vep.next_sid = vep_session->vep.next_sid;
3756       session->vep.prev_sid = vep_idx;
3757       session->vep.vep_idx = vep_idx;
3758       session->vep.et_mask = VEP_DEFAULT_ET_MASK;
3759       session->vep.ev = *event;
3760       session->is_vep = 0;
3761       session->is_vep_session = 1;
3762       vep_session->vep.next_sid = session_index;
3763       if (VPPCOM_DEBUG > 1)
3764         clib_warning ("VCL<%d>: EPOLL_CTL_ADD: vep_idx %u, "
3765                       "sid %u, events 0x%x, data 0x%llx!",
3766                       getpid (), vep_idx, session_index,
3767                       event->events, event->data.u64);
3768       if (VPPCOM_DEBUG > 0)
3769         {
3770           /* *INDENT-OFF* */
3771           ELOG_TYPE_DECLARE (e) =
3772             {
3773               .format = "epoll_ctladd: events:%x data:%x",
3774               .format_args = "i4i4i8",
3775             };
3776           struct
3777           {
3778             u32 events;
3779             u64 event_data;
3780           } *ed;
3781
3782           ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
3783
3784           ed->events = event->events;
3785           ed->event_data = event->data.u64;
3786           /* *INDENT-ON* */
3787         }
3788       break;
3789
3790     case EPOLL_CTL_MOD:
3791       if (PREDICT_FALSE (!event))
3792         {
3793           clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_MOD: NULL pointer to "
3794                         "epoll_event structure!", getpid ());
3795           rv = VPPCOM_EINVAL;
3796           goto done;
3797         }
3798       else if (PREDICT_FALSE (!session->is_vep_session))
3799         {
3800           clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_MOD: "
3801                         "not a vep session!", getpid (), session_index);
3802           rv = VPPCOM_EINVAL;
3803           goto done;
3804         }
3805       else if (PREDICT_FALSE (session->vep.vep_idx != vep_idx))
3806         {
3807           clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_MOD: "
3808                         "vep_idx (%u) != vep_idx (%u)!",
3809                         getpid (), session_index,
3810                         session->vep.vep_idx, vep_idx);
3811           rv = VPPCOM_EINVAL;
3812           goto done;
3813         }
3814       session->vep.et_mask = VEP_DEFAULT_ET_MASK;
3815       session->vep.ev = *event;
3816       if (VPPCOM_DEBUG > 1)
3817         clib_warning
3818           ("VCL<%d>: EPOLL_CTL_MOD: vep_idx %u, sid %u, events 0x%x,"
3819            " data 0x%llx!", getpid (), vep_idx, session_index, event->events,
3820            event->data.u64);
3821       break;
3822
3823     case EPOLL_CTL_DEL:
3824       if (PREDICT_FALSE (!session->is_vep_session))
3825         {
3826           clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_DEL: "
3827                         "not a vep session!", getpid (), session_index);
3828           rv = VPPCOM_EINVAL;
3829           goto done;
3830         }
3831       else if (PREDICT_FALSE (session->vep.vep_idx != vep_idx))
3832         {
3833           clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_DEL: "
3834                         "vep_idx (%u) != vep_idx (%u)!",
3835                         getpid (), session_index,
3836                         session->vep.vep_idx, vep_idx);
3837           rv = VPPCOM_EINVAL;
3838           goto done;
3839         }
3840
3841       vep_session->wait_cont_idx =
3842         (vep_session->wait_cont_idx == session_index) ?
3843         session->vep.next_sid : vep_session->wait_cont_idx;
3844
3845       if (session->vep.prev_sid == vep_idx)
3846         vep_session->vep.next_sid = session->vep.next_sid;
3847       else
3848         {
3849           session_t *prev_session;
3850           rv = vppcom_session_at_index (session->vep.prev_sid, &prev_session);
3851           if (PREDICT_FALSE (rv))
3852             {
3853               clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_DEL: Invalid "
3854                             "vep.prev_sid (%u) on sid (%u)!",
3855                             getpid (), session->vep.prev_sid, session_index);
3856               goto done;
3857             }
3858           ASSERT (prev_session->vep.next_sid == session_index);
3859           prev_session->vep.next_sid = session->vep.next_sid;
3860         }
3861       if (session->vep.next_sid != ~0)
3862         {
3863           session_t *next_session;
3864           rv = vppcom_session_at_index (session->vep.next_sid, &next_session);
3865           if (PREDICT_FALSE (rv))
3866             {
3867               clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_DEL: Invalid "
3868                             "vep.next_sid (%u) on sid (%u)!",
3869                             getpid (), session->vep.next_sid, session_index);
3870               goto done;
3871             }
3872           ASSERT (next_session->vep.prev_sid == session_index);
3873           next_session->vep.prev_sid = session->vep.prev_sid;
3874         }
3875
3876       memset (&session->vep, 0, sizeof (session->vep));
3877       session->vep.next_sid = ~0;
3878       session->vep.prev_sid = ~0;
3879       session->vep.vep_idx = ~0;
3880       session->is_vep_session = 0;
3881       if (VPPCOM_DEBUG > 1)
3882         clib_warning ("VCL<%d>: EPOLL_CTL_DEL: vep_idx %u, sid %u!",
3883                       getpid (), vep_idx, session_index);
3884       if (VPPCOM_DEBUG > 0)
3885         {
3886           /* *INDENT-OFF* */
3887           ELOG_TYPE_DECLARE (e) =
3888             {
3889               .format = "epoll_ctldel: vep:%d",
3890               .format_args = "i4",
3891             };
3892           struct
3893           {
3894             u32 data;
3895           } *ed;
3896
3897           ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
3898
3899           ed->data = vep_idx;
3900           /* *INDENT-ON* */
3901         }
3902       break;
3903
3904     default:
3905       clib_warning ("VCL<%d>: ERROR: Invalid operation (%d)!", getpid (), op);
3906       rv = VPPCOM_EINVAL;
3907     }
3908
3909   vep_verify_epoll_chain (vep_idx);
3910
3911 done:
3912   clib_spinlock_unlock (&vcm->sessions_lockp);
3913   return rv;
3914 }
3915
3916 int
3917 vppcom_epoll_wait (uint32_t vep_idx, struct epoll_event *events,
3918                    int maxevents, double wait_for_time)
3919 {
3920   session_t *vep_session;
3921   elog_track_t vep_elog_track;
3922   int rv;
3923   f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
3924   u32 keep_trying = 1;
3925   int num_ev = 0;
3926   u32 vep_next_sid, wait_cont_idx;
3927   u8 is_vep;
3928
3929   if (PREDICT_FALSE (maxevents <= 0))
3930     {
3931       clib_warning ("VCL<%d>: ERROR: Invalid maxevents (%d)!",
3932                     getpid (), maxevents);
3933       return VPPCOM_EINVAL;
3934     }
3935   memset (events, 0, sizeof (*events) * maxevents);
3936
3937   VCL_LOCK_AND_GET_SESSION (vep_idx, &vep_session);
3938   vep_next_sid = vep_session->vep.next_sid;
3939   is_vep = vep_session->is_vep;
3940   wait_cont_idx = vep_session->wait_cont_idx;
3941   vep_elog_track = vep_session->elog_track;
3942   clib_spinlock_unlock (&vcm->sessions_lockp);
3943
3944   if (PREDICT_FALSE (!is_vep))
3945     {
3946       clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
3947                     getpid (), vep_idx);
3948       rv = VPPCOM_EINVAL;
3949       goto done;
3950     }
3951   if (PREDICT_FALSE (vep_next_sid == ~0))
3952     {
3953       if (VPPCOM_DEBUG > 0)
3954         clib_warning ("VCL<%d>: WARNING: vep_idx (%u) is empty!",
3955                       getpid (), vep_idx);
3956       if (VPPCOM_DEBUG > 0)
3957         {
3958           /* *INDENT-OFF* */
3959           ELOG_TYPE_DECLARE (e) =
3960             {
3961               .format = "WRN: vep_idx:%d empty",
3962               .format_args = "i4",
3963             };
3964           struct
3965           {
3966             u32 data;
3967           } *ed;
3968
3969           ed = ELOG_TRACK_DATA (&vcm->elog_main, e, vep_elog_track);
3970
3971           ed->data = vep_idx;
3972           /* *INDENT-ON* */
3973         }
3974       goto done;
3975     }
3976
3977   do
3978     {
3979       u32 sid;
3980       u32 next_sid = ~0;
3981       session_t *session;
3982       elog_track_t session_elog_track;
3983
3984       for (sid = (wait_cont_idx == ~0) ? vep_next_sid : wait_cont_idx;
3985            sid != ~0; sid = next_sid)
3986         {
3987           u32 session_events, et_mask, clear_et_mask, session_vep_idx;
3988           u8 add_event, is_vep_session;
3989           int ready;
3990           u64 session_ev_data;
3991
3992           VCL_LOCK_AND_GET_SESSION (sid, &session);
3993           next_sid = session->vep.next_sid;
3994           session_events = session->vep.ev.events;
3995           et_mask = session->vep.et_mask;
3996           is_vep = session->is_vep;
3997           is_vep_session = session->is_vep_session;
3998           session_vep_idx = session->vep.vep_idx;
3999           session_ev_data = session->vep.ev.data.u64;
4000
4001           if (VPPCOM_DEBUG > 0)
4002             {
4003               session_elog_track = session->elog_track;
4004             }
4005
4006           clib_spinlock_unlock (&vcm->sessions_lockp);
4007
4008           if (PREDICT_FALSE (is_vep))
4009             {
4010               if (VPPCOM_DEBUG > 0)
4011                 clib_warning ("VCL<%d>: ERROR: sid (%u) is a vep!",
4012                               getpid (), vep_idx);
4013               if (VPPCOM_DEBUG > 0)
4014                 {
4015                   /* *INDENT-OFF* */
4016                   ELOG_TYPE_DECLARE (e) =
4017                     {
4018                       .format = "ERR:vep_idx:%d is vep",
4019                       .format_args = "i4",
4020                     };
4021                   struct
4022                   {
4023                     u32 data;
4024                   } *ed;
4025
4026                   ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session_elog_track);
4027
4028                   ed->data = vep_idx;
4029                   /* *INDENT-ON* */
4030                 }
4031
4032               rv = VPPCOM_EINVAL;
4033               goto done;
4034             }
4035           if (PREDICT_FALSE (!is_vep_session))
4036             {
4037               if (VPPCOM_DEBUG > 0)
4038                 clib_warning ("VCL<%d>: ERROR: session (%u) is not "
4039                               "a vep session!", getpid (), sid);
4040               if (VPPCOM_DEBUG > 0)
4041                 {
4042                   /* *INDENT-OFF* */
4043                   ELOG_TYPE_DECLARE (e) =
4044                     {
4045                       .format = "ERR:SID:%d not vep",
4046                       .format_args = "i4",
4047                     };
4048                   struct
4049                   {
4050                     u32 data;
4051                   } *ed;
4052
4053                   ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session_elog_track);
4054
4055                   ed->data = sid;
4056                   /* *INDENT-ON* */
4057                 }
4058
4059               rv = VPPCOM_EINVAL;
4060               goto done;
4061             }
4062           if (PREDICT_FALSE (session_vep_idx != vep_idx))
4063             {
4064               clib_warning ("VCL<%d>: ERROR: session (%u) "
4065                             "vep_idx (%u) != vep_idx (%u)!",
4066                             getpid (), sid, session_vep_idx, vep_idx);
4067               rv = VPPCOM_EINVAL;
4068               goto done;
4069             }
4070
4071           add_event = clear_et_mask = 0;
4072
4073           if (EPOLLIN & session_events)
4074             {
4075               VCL_LOCK_AND_GET_SESSION (sid, &session);
4076               ready = vppcom_session_read_ready (session, sid);
4077               clib_spinlock_unlock (&vcm->sessions_lockp);
4078               if ((ready > 0) && (EPOLLIN & et_mask))
4079                 {
4080                   add_event = 1;
4081                   events[num_ev].events |= EPOLLIN;
4082                   if (((EPOLLET | EPOLLIN) & session_events) ==
4083                       (EPOLLET | EPOLLIN))
4084                     clear_et_mask |= EPOLLIN;
4085                 }
4086               else if (ready < 0)
4087                 {
4088                   add_event = 1;
4089                   switch (ready)
4090                     {
4091                     case VPPCOM_ECONNRESET:
4092                       events[num_ev].events |= EPOLLHUP | EPOLLRDHUP;
4093                       break;
4094
4095                     default:
4096                       events[num_ev].events |= EPOLLERR;
4097                       break;
4098                     }
4099                 }
4100             }
4101
4102           if (EPOLLOUT & session_events)
4103             {
4104               VCL_LOCK_AND_GET_SESSION (sid, &session);
4105               ready = vppcom_session_write_ready (session, sid);
4106               clib_spinlock_unlock (&vcm->sessions_lockp);
4107               if ((ready > 0) && (EPOLLOUT & et_mask))
4108                 {
4109                   add_event = 1;
4110                   events[num_ev].events |= EPOLLOUT;
4111                   if (((EPOLLET | EPOLLOUT) & session_events) ==
4112                       (EPOLLET | EPOLLOUT))
4113                     clear_et_mask |= EPOLLOUT;
4114                 }
4115               else if (ready < 0)
4116                 {
4117                   add_event = 1;
4118                   switch (ready)
4119                     {
4120                     case VPPCOM_ECONNRESET:
4121                       events[num_ev].events |= EPOLLHUP;
4122                       break;
4123
4124                     default:
4125                       events[num_ev].events |= EPOLLERR;
4126                       break;
4127                     }
4128                 }
4129             }
4130
4131           if (add_event)
4132             {
4133               events[num_ev].data.u64 = session_ev_data;
4134               if (EPOLLONESHOT & session_events)
4135                 {
4136                   VCL_LOCK_AND_GET_SESSION (sid, &session);
4137                   session->vep.ev.events = 0;
4138                   clib_spinlock_unlock (&vcm->sessions_lockp);
4139                 }
4140               num_ev++;
4141               if (num_ev == maxevents)
4142                 {
4143                   VCL_LOCK_AND_GET_SESSION (vep_idx, &vep_session);
4144                   vep_session->wait_cont_idx = next_sid;
4145                   clib_spinlock_unlock (&vcm->sessions_lockp);
4146                   goto done;
4147                 }
4148             }
4149           if (wait_cont_idx != ~0)
4150             {
4151               if (next_sid == ~0)
4152                 next_sid = vep_next_sid;
4153               else if (next_sid == wait_cont_idx)
4154                 next_sid = ~0;
4155             }
4156         }
4157       if (wait_for_time != -1)
4158         keep_trying = (clib_time_now (&vcm->clib_time) <= timeout) ? 1 : 0;
4159     }
4160   while ((num_ev == 0) && keep_trying);
4161
4162   if (wait_cont_idx != ~0)
4163     {
4164       VCL_LOCK_AND_GET_SESSION (vep_idx, &vep_session);
4165       vep_session->wait_cont_idx = ~0;
4166       clib_spinlock_unlock (&vcm->sessions_lockp);
4167     }
4168 done:
4169   return (rv != VPPCOM_OK) ? rv : num_ev;
4170 }
4171
4172 int
4173 vppcom_session_attr (uint32_t session_index, uint32_t op,
4174                      void *buffer, uint32_t * buflen)
4175 {
4176   session_t *session;
4177   int rv = VPPCOM_OK;
4178   u32 *flags = buffer;
4179   vppcom_endpt_t *ep = buffer;
4180
4181   VCL_LOCK_AND_GET_SESSION (session_index, &session);
4182
4183   ASSERT (session);
4184
4185   switch (op)
4186     {
4187     case VPPCOM_ATTR_GET_NREAD:
4188       rv = vppcom_session_read_ready (session, session_index);
4189       if (VPPCOM_DEBUG > 2)
4190         clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_NREAD: sid %u, nread = %d",
4191                       getpid (), rv);
4192       if (VPPCOM_DEBUG > 0)
4193         {
4194           /* *INDENT-OFF* */
4195           ELOG_TYPE_DECLARE (e) =
4196             {
4197               .format = "VPPCOM_ATTR_GET_NREAD: nread=%d",
4198               .format_args = "i4",
4199             };
4200           struct
4201           {
4202             u32 data;
4203           } *ed;
4204
4205           ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4206
4207           ed->data = rv;
4208           /* *INDENT-ON* */
4209         }
4210
4211       break;
4212
4213     case VPPCOM_ATTR_GET_NWRITE:
4214       rv = vppcom_session_write_ready (session, session_index);
4215       if (VPPCOM_DEBUG > 2)
4216         clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_NWRITE: sid %u, nwrite = %d",
4217                       getpid (), session_index, rv);
4218       if (VPPCOM_DEBUG > 0)
4219         {
4220           /* *INDENT-OFF* */
4221           ELOG_TYPE_DECLARE (e) =
4222             {
4223               .format = "VPPCOM_ATTR_GET_NWRITE: nwrite=%d",
4224               .format_args = "i4",
4225             };
4226           struct
4227           {
4228             u32 data;
4229           } *ed;
4230
4231           ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4232
4233           ed->data = rv;
4234           /* *INDENT-ON* */
4235         }
4236       break;
4237
4238     case VPPCOM_ATTR_GET_FLAGS:
4239       if (PREDICT_TRUE (buffer && buflen && (*buflen >= sizeof (*flags))))
4240         {
4241           *flags = O_RDWR | (VCL_SESS_ATTR_TEST (session->attr,
4242                                                  VCL_SESS_ATTR_NONBLOCK));
4243           *buflen = sizeof (*flags);
4244           if (VPPCOM_DEBUG > 2)
4245             clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_FLAGS: sid %u, "
4246                           "flags = 0x%08x, is_nonblocking = %u", getpid (),
4247                           session_index, *flags,
4248                           VCL_SESS_ATTR_TEST (session->attr,
4249                                               VCL_SESS_ATTR_NONBLOCK));
4250           if (VPPCOM_DEBUG > 0)
4251             {
4252                 /* *INDENT-OFF* */
4253               ELOG_TYPE_DECLARE (e) =
4254                 {
4255                   .format = "VPPCOM_ATTR_GET_FLAGS: flags=%x is_nonblk=%d",
4256                   .format_args = "i4i4",
4257                 };
4258               struct
4259               {
4260                 u32 flags;
4261                 u32 is_nonblk;
4262               } *ed;
4263
4264               ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4265
4266               ed->flags = *flags;
4267               ed->is_nonblk = VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK);
4268               /* *INDENT-ON* */
4269             }
4270
4271         }
4272       else
4273         rv = VPPCOM_EINVAL;
4274       break;
4275
4276     case VPPCOM_ATTR_SET_FLAGS:
4277       if (PREDICT_TRUE (buffer && buflen && (*buflen == sizeof (*flags))))
4278         {
4279           if (*flags & O_NONBLOCK)
4280             VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK);
4281           else
4282             VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_NONBLOCK);
4283
4284           if (VPPCOM_DEBUG > 2)
4285             clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_FLAGS: sid %u, "
4286                           "flags = 0x%08x, is_nonblocking = %u",
4287                           getpid (), session_index, *flags,
4288                           VCL_SESS_ATTR_TEST (session->attr,
4289                                               VCL_SESS_ATTR_NONBLOCK));
4290           if (VPPCOM_DEBUG > 0)
4291             {
4292                 /* *INDENT-OFF* */
4293               ELOG_TYPE_DECLARE (e) =
4294                 {
4295                   .format = "VPPCOM_ATTR_SET_FLAGS: flags=%x is_nonblk=%d",
4296                   .format_args = "i4i4",
4297                 };
4298               struct
4299               {
4300                 u32 flags;
4301                 u32 is_nonblk;
4302               } *ed;
4303
4304               ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4305
4306               ed->flags = *flags;
4307               ed->is_nonblk = VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK);
4308               /* *INDENT-ON* */
4309             }
4310         }
4311       else
4312         rv = VPPCOM_EINVAL;
4313       break;
4314
4315     case VPPCOM_ATTR_GET_PEER_ADDR:
4316       if (PREDICT_TRUE (buffer && buflen &&
4317                         (*buflen >= sizeof (*ep)) && ep->ip))
4318         {
4319           ep->is_ip4 = session->peer_addr.is_ip4;
4320           ep->port = session->peer_port;
4321           if (session->peer_addr.is_ip4)
4322             clib_memcpy (ep->ip, &session->peer_addr.ip46.ip4,
4323                          sizeof (ip4_address_t));
4324           else
4325             clib_memcpy (ep->ip, &session->peer_addr.ip46.ip6,
4326                          sizeof (ip6_address_t));
4327           *buflen = sizeof (*ep);
4328           if (VPPCOM_DEBUG > 1)
4329             clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_PEER_ADDR: sid %u, "
4330                           "is_ip4 = %u, addr = %U, port %u", getpid (),
4331                           session_index, ep->is_ip4, format_ip46_address,
4332                           &session->peer_addr.ip46, ep->is_ip4,
4333                           clib_net_to_host_u16 (ep->port));
4334           if (VPPCOM_DEBUG > 0)
4335             {
4336               if (ep->is_ip4)
4337                 {
4338                     /* *INDENT-OFF* */
4339                   ELOG_TYPE_DECLARE (e) =
4340                     {
4341                       .format = "VPPCOM_ATTR_GET_PEER_ADDR: addr:%d.%d.%d.%d:%d",
4342                       .format_args = "i1i1i1i1i2",
4343                     };
4344                   CLIB_PACKED (struct {
4345                     u8 addr[4]; //4
4346                     u16 port;   //2
4347                   }) * ed;
4348
4349                   ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4350
4351                   ed->addr[0] = session->peer_addr.ip46.ip4.as_u8[0];
4352                   ed->addr[1] = session->peer_addr.ip46.ip4.as_u8[1];
4353                   ed->addr[2] = session->peer_addr.ip46.ip4.as_u8[2];
4354                   ed->addr[3] = session->peer_addr.ip46.ip4.as_u8[3];
4355                   ed->port = clib_net_to_host_u16 (session->peer_port);
4356                   /* *INDENT-ON* */
4357                 }
4358               else
4359                 {
4360                     /* *INDENT-OFF* */
4361                   ELOG_TYPE_DECLARE (e) =
4362                     {
4363                       .format = "VPPCOM_ATTR_GET_PEER_ADDR: addr:IP6:%d",
4364                       .format_args = "i2",
4365                     };
4366                   CLIB_PACKED (struct {
4367                     u16 port;   //2
4368                   }) * ed;
4369
4370                   ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4371
4372                   ed->port = clib_net_to_host_u16 (session->peer_port);
4373                   /* *INDENT-ON* */
4374                 }
4375             }
4376         }
4377       else
4378         rv = VPPCOM_EINVAL;
4379       break;
4380
4381     case VPPCOM_ATTR_GET_LCL_ADDR:
4382       if (PREDICT_TRUE (buffer && buflen &&
4383                         (*buflen >= sizeof (*ep)) && ep->ip))
4384         {
4385           ep->is_ip4 = session->lcl_addr.is_ip4;
4386           ep->port = session->lcl_port;
4387           if (session->lcl_addr.is_ip4)
4388             clib_memcpy (ep->ip, &session->lcl_addr.ip46.ip4,
4389                          sizeof (ip4_address_t));
4390           else
4391             clib_memcpy (ep->ip, &session->lcl_addr.ip46.ip6,
4392                          sizeof (ip6_address_t));
4393           *buflen = sizeof (*ep);
4394           if (VPPCOM_DEBUG > 1)
4395             clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_LCL_ADDR: sid %u, "
4396                           "is_ip4 = %u, addr = %U port %d", getpid (),
4397                           session_index, ep->is_ip4, format_ip46_address,
4398                           &session->lcl_addr.ip46, ep->is_ip4,
4399                           clib_net_to_host_u16 (ep->port));
4400           if (VPPCOM_DEBUG > 0)
4401             {
4402               if (ep->is_ip4)
4403                 {
4404                     /* *INDENT-OFF* */
4405                   ELOG_TYPE_DECLARE (e) =
4406                     {
4407                       .format = "VPPCOM_ATTR_GET_LCL_ADDR: addr:%d.%d.%d.%d:%d",
4408                       .format_args = "i1i1i1i1i2",
4409                     };
4410                   CLIB_PACKED (struct {
4411                     u8 addr[4]; //4
4412                     u16 port;   //2
4413                   }) * ed;
4414
4415                   ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4416
4417                   ed->addr[0] = session->lcl_addr.ip46.ip4.as_u8[0];
4418                   ed->addr[1] = session->lcl_addr.ip46.ip4.as_u8[1];
4419                   ed->addr[2] = session->lcl_addr.ip46.ip4.as_u8[2];
4420                   ed->addr[3] = session->lcl_addr.ip46.ip4.as_u8[3];
4421                   ed->port = clib_net_to_host_u16 (session->peer_port);
4422                   /* *INDENT-ON* */
4423                 }
4424               else
4425                 {
4426                     /* *INDENT-OFF* */
4427                   ELOG_TYPE_DECLARE (e) =
4428                     {
4429                       .format = "VPPCOM_ATTR_GET_LCL_ADDR: addr:IP6:%d",
4430                       .format_args = "i2",
4431                     };
4432                   CLIB_PACKED (struct {
4433                     u16 port;   //2
4434                   }) * ed;
4435
4436                   ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4437
4438                   ed->port = clib_net_to_host_u16 (session->peer_port);
4439                   /* *INDENT-ON* */
4440                 }
4441             }
4442         }
4443       else
4444         rv = VPPCOM_EINVAL;
4445       break;
4446
4447     case VPPCOM_ATTR_GET_LIBC_EPFD:
4448       rv = session->libc_epfd;
4449       if (VPPCOM_DEBUG > 2)
4450         clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd %d",
4451                       getpid (), rv);
4452       if (VPPCOM_DEBUG > 0)
4453         {
4454           /* *INDENT-OFF* */
4455           ELOG_TYPE_DECLARE (e) =
4456             {
4457               .format = "VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd=%d",
4458               .format_args = "i4",
4459             };
4460           CLIB_PACKED (struct {
4461             i32 data;
4462           }) * ed;
4463
4464           ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4465           ed->data = session->libc_epfd;
4466           /* *INDENT-ON* */
4467         }
4468
4469       break;
4470
4471     case VPPCOM_ATTR_SET_LIBC_EPFD:
4472       if (PREDICT_TRUE (buffer && buflen &&
4473                         (*buflen == sizeof (session->libc_epfd))))
4474         {
4475           session->libc_epfd = *(int *) buffer;
4476           *buflen = sizeof (session->libc_epfd);
4477
4478           if (VPPCOM_DEBUG > 2)
4479             clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd %d, "
4480                           "buflen %d", getpid (), session->libc_epfd,
4481                           *buflen);
4482           if (VPPCOM_DEBUG > 0)
4483             {
4484                 /* *INDENT-OFF* */
4485               ELOG_TYPE_DECLARE (e) =
4486                 {
4487                   .format = "VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd=%s%d buflen=%d",
4488                   .format_args = "t1i4i4",
4489                   .n_enum_strings = 2,
4490                   .enum_strings = {"", "-",},
4491                 };
4492               CLIB_PACKED (struct {
4493                 u8 sign;
4494                 u32 data[2];
4495               }) * ed;
4496
4497               ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4498
4499               ed->sign = (session->libc_epfd < 0);
4500               ed->data[0] = abs(session->libc_epfd);
4501               ed->data[1] = *buflen;
4502               /* *INDENT-ON* */
4503             }
4504         }
4505       else
4506         rv = VPPCOM_EINVAL;
4507       break;
4508
4509     case VPPCOM_ATTR_GET_PROTOCOL:
4510       if (buffer && buflen && (*buflen >= sizeof (int)))
4511         {
4512           *(int *) buffer = session->proto;
4513           *buflen = sizeof (int);
4514
4515           if (VPPCOM_DEBUG > 2)
4516             clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_PROTOCOL: %d (%s), "
4517                           "buflen %d", getpid (), *(int *) buffer,
4518                           *(int *) buffer ? "UDP" : "TCP", *buflen);
4519           if (VPPCOM_DEBUG > 0)
4520             {
4521                 /* *INDENT-OFF* */
4522               ELOG_TYPE_DECLARE (e) =
4523                 {
4524                   .format = "VPPCOM_ATTR_GET_PROTOCOL: %s buflen=%d",
4525                   .format_args = "t1i4",
4526                   .n_enum_strings = 2,
4527                   .enum_strings = {"TCP", "UDP",},
4528                 };
4529
4530               CLIB_PACKED (struct {
4531                 u8 proto;
4532                 u32 buflen;
4533               }) * ed;
4534
4535               ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4536               ed->proto = session->proto;
4537               ed->buflen = *(int *) buffer;
4538               /* *INDENT-ON* */
4539             }
4540         }
4541       else
4542         rv = VPPCOM_EINVAL;
4543       break;
4544
4545     case VPPCOM_ATTR_GET_LISTEN:
4546       if (buffer && buflen && (*buflen >= sizeof (int)))
4547         {
4548           *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
4549                                                 VCL_SESS_ATTR_LISTEN);
4550           *buflen = sizeof (int);
4551
4552           if (VPPCOM_DEBUG > 2)
4553             clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_LISTEN: %d, "
4554                           "buflen %d", getpid (), *(int *) buffer, *buflen);
4555           if (VPPCOM_DEBUG > 0)
4556             {
4557                 /* *INDENT-OFF* */
4558               ELOG_TYPE_DECLARE (e) =
4559                 {
4560                   .format = "VPPCOM_ATTR_GET_LISTEN: %d buflen=%d",
4561                   .format_args = "i4i4",
4562                 };
4563
4564               struct {
4565                 u32 data[2];
4566               } * ed;
4567
4568               ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4569               ed->data[0] = *(int *) buffer;
4570               ed->data[1] = *buflen;
4571               /* *INDENT-ON* */
4572             }
4573         }
4574       else
4575         rv = VPPCOM_EINVAL;
4576       break;
4577
4578     case VPPCOM_ATTR_GET_ERROR:
4579       if (buffer && buflen && (*buflen >= sizeof (int)))
4580         {
4581           *(int *) buffer = 0;
4582           *buflen = sizeof (int);
4583
4584           if (VPPCOM_DEBUG > 2)
4585             clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_ERROR: %d, "
4586                           "buflen %d, #VPP-TBD#", getpid (),
4587                           *(int *) buffer, *buflen);
4588           if (VPPCOM_DEBUG > 0)
4589             {
4590                 /* *INDENT-OFF* */
4591               ELOG_TYPE_DECLARE (e) =
4592                 {
4593                   .format = "VPPCOM_ATTR_GET_ERROR: %d buflen=%d",
4594                   .format_args = "i4i4",
4595                 };
4596
4597               struct {
4598                 u32 data[2];
4599               } * ed;
4600
4601               ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4602               ed->data[0] = *(int *) buffer;
4603               ed->data[1] = *buflen;
4604               /* *INDENT-ON* */
4605             }
4606         }
4607       else
4608         rv = VPPCOM_EINVAL;
4609       break;
4610
4611     case VPPCOM_ATTR_GET_TX_FIFO_LEN:
4612       if (buffer && buflen && (*buflen >= sizeof (u32)))
4613         {
4614
4615           /* VPP-TBD */
4616           *(size_t *) buffer = (session->sndbuf_size ? session->sndbuf_size :
4617                                 session->tx_fifo ? session->tx_fifo->nitems :
4618                                 vcm->cfg.tx_fifo_size);
4619           *buflen = sizeof (u32);
4620
4621           if (VPPCOM_DEBUG > 2)
4622             clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_TX_FIFO_LEN: %u (0x%x), "
4623                           "buflen %d, #VPP-TBD#", getpid (),
4624                           *(size_t *) buffer, *(size_t *) buffer, *buflen);
4625           if (VPPCOM_DEBUG > 0)
4626             {
4627                 /* *INDENT-OFF* */
4628               ELOG_TYPE_DECLARE (e) =
4629                 {
4630                   .format = "VPPCOM_ATTR_GET_TX_FIFO_LEN: 0x%x buflen=%d",
4631                   .format_args = "i4i4",
4632                 };
4633
4634               struct {
4635                 u32 data[2];
4636               } * ed;
4637
4638               ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4639               ed->data[0] = *(size_t *) buffer;
4640               ed->data[1] = *buflen;
4641               /* *INDENT-ON* */
4642             }
4643         }
4644       else
4645         rv = VPPCOM_EINVAL;
4646       break;
4647
4648     case VPPCOM_ATTR_SET_TX_FIFO_LEN:
4649       if (buffer && buflen && (*buflen == sizeof (u32)))
4650         {
4651           /* VPP-TBD */
4652           session->sndbuf_size = *(u32 *) buffer;
4653           if (VPPCOM_DEBUG > 2)
4654             clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_TX_FIFO_LEN: %u (0x%x), "
4655                           "buflen %d, #VPP-TBD#", getpid (),
4656                           session->sndbuf_size, session->sndbuf_size,
4657                           *buflen);
4658           if (VPPCOM_DEBUG > 0)
4659             {
4660                 /* *INDENT-OFF* */
4661               ELOG_TYPE_DECLARE (e) =
4662                 {
4663                   .format = "VPPCOM_ATTR_SET_TX_FIFO_LEN: 0x%x buflen=%d",
4664                   .format_args = "i4i4",
4665                 };
4666
4667               struct {
4668                 u32 data[2];
4669               } * ed;
4670
4671               ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4672               ed->data[0] = session->sndbuf_size;
4673               ed->data[1] = *buflen;
4674               /* *INDENT-ON* */
4675             }
4676         }
4677       else
4678         rv = VPPCOM_EINVAL;
4679       break;
4680
4681     case VPPCOM_ATTR_GET_RX_FIFO_LEN:
4682       if (buffer && buflen && (*buflen >= sizeof (u32)))
4683         {
4684
4685           /* VPP-TBD */
4686           *(size_t *) buffer = (session->rcvbuf_size ? session->rcvbuf_size :
4687                                 session->rx_fifo ? session->rx_fifo->nitems :
4688                                 vcm->cfg.rx_fifo_size);
4689           *buflen = sizeof (u32);
4690
4691           if (VPPCOM_DEBUG > 2)
4692             clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_RX_FIFO_LEN: %u (0x%x), "
4693                           "buflen %d, #VPP-TBD#", getpid (),
4694                           *(size_t *) buffer, *(size_t *) buffer, *buflen);
4695           if (VPPCOM_DEBUG > 0)
4696             {
4697                 /* *INDENT-OFF* */
4698               ELOG_TYPE_DECLARE (e) =
4699                 {
4700                   .format = "VPPCOM_ATTR_GET_RX_FIFO_LEN: 0x%x buflen=%d",
4701                   .format_args = "i4i4",
4702                 };
4703
4704               struct {
4705                 u32 data[2];
4706               } * ed;
4707
4708               ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4709               ed->data[0] = *(size_t *) buffer;
4710               ed->data[1] = *buflen;
4711               /* *INDENT-ON* */
4712             }
4713         }
4714       else
4715         rv = VPPCOM_EINVAL;
4716       break;
4717
4718     case VPPCOM_ATTR_SET_RX_FIFO_LEN:
4719       if (buffer && buflen && (*buflen == sizeof (u32)))
4720         {
4721           /* VPP-TBD */
4722           session->rcvbuf_size = *(u32 *) buffer;
4723           if (VPPCOM_DEBUG > 2)
4724             clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_RX_FIFO_LEN: %u (0x%x), "
4725                           "buflen %d, #VPP-TBD#", getpid (),
4726                           session->sndbuf_size, session->sndbuf_size,
4727                           *buflen);
4728           if (VPPCOM_DEBUG > 0)
4729             {
4730                 /* *INDENT-OFF* */
4731               ELOG_TYPE_DECLARE (e) =
4732                 {
4733                   .format = "VPPCOM_ATTR_SET_RX_FIFO_LEN: 0x%x buflen=%d",
4734                   .format_args = "i4i4",
4735                 };
4736
4737               struct {
4738                 u32 data[2];
4739               } * ed;
4740
4741               ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4742               ed->data[0] = session->sndbuf_size;
4743               ed->data[1] = *buflen;
4744               /* *INDENT-ON* */
4745             }
4746         }
4747       else
4748         rv = VPPCOM_EINVAL;
4749       break;
4750
4751     case VPPCOM_ATTR_GET_REUSEADDR:
4752       if (buffer && buflen && (*buflen >= sizeof (int)))
4753         {
4754           /* VPP-TBD */
4755           *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
4756                                                 VCL_SESS_ATTR_REUSEADDR);
4757           *buflen = sizeof (int);
4758
4759           if (VPPCOM_DEBUG > 2)
4760             clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_REUSEADDR: %d, "
4761                           "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
4762                           *buflen);
4763           if (VPPCOM_DEBUG > 0)
4764             {
4765                 /* *INDENT-OFF* */
4766               ELOG_TYPE_DECLARE (e) =
4767                 {
4768                   .format = "VPPCOM_ATTR_GET_REUSEADDR: %d buflen=%d",
4769                   .format_args = "i4i4",
4770                 };
4771
4772               struct {
4773                 u32 data[2];
4774               } * ed;
4775
4776               ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4777               ed->data[0] = *(int *) buffer;
4778               ed->data[1] = *buflen;
4779               /* *INDENT-ON* */
4780             }
4781         }
4782       else
4783         rv = VPPCOM_EINVAL;
4784       break;
4785
4786     case VPPCOM_ATTR_SET_REUSEADDR:
4787       if (buffer && buflen && (*buflen == sizeof (int)) &&
4788           !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
4789         {
4790           /* VPP-TBD */
4791           if (*(int *) buffer)
4792             VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEADDR);
4793           else
4794             VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEADDR);
4795
4796           if (VPPCOM_DEBUG > 2)
4797             clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_REUSEADDR: %d, "
4798                           "buflen %d, #VPP-TBD#", getpid (),
4799                           VCL_SESS_ATTR_TEST (session->attr,
4800                                               VCL_SESS_ATTR_REUSEADDR),
4801                           *buflen);
4802           if (VPPCOM_DEBUG > 0)
4803             {
4804                 /* *INDENT-OFF* */
4805               ELOG_TYPE_DECLARE (e) =
4806                 {
4807                   .format = "VPPCOM_ATTR_SET_REUSEADDR: %d buflen=%d",
4808                   .format_args = "i4i4",
4809                 };
4810
4811               struct {
4812                 u32 data[2];
4813               } * ed;
4814
4815               ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4816               ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
4817                                                 VCL_SESS_ATTR_REUSEADDR);
4818               ed->data[1] = *buflen;
4819               /* *INDENT-ON* */
4820             }
4821         }
4822       else
4823         rv = VPPCOM_EINVAL;
4824       break;
4825
4826     case VPPCOM_ATTR_GET_REUSEPORT:
4827       if (buffer && buflen && (*buflen >= sizeof (int)))
4828         {
4829           /* VPP-TBD */
4830           *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
4831                                                 VCL_SESS_ATTR_REUSEPORT);
4832           *buflen = sizeof (int);
4833
4834           if (VPPCOM_DEBUG > 2)
4835             clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_REUSEPORT: %d, "
4836                           "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
4837                           *buflen);
4838           if (VPPCOM_DEBUG > 0)
4839             {
4840                 /* *INDENT-OFF* */
4841               ELOG_TYPE_DECLARE (e) =
4842                 {
4843                   .format = "VPPCOM_ATTR_GET_REUSEPORT: %d buflen=%d",
4844                   .format_args = "i4i4",
4845                 };
4846
4847               struct {
4848                 u32 data[2];
4849               } * ed;
4850
4851               ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4852               ed->data[0] = *(int *) buffer;
4853               ed->data[1] = *buflen;
4854               /* *INDENT-ON* */
4855             }
4856         }
4857       else
4858         rv = VPPCOM_EINVAL;
4859       break;
4860
4861     case VPPCOM_ATTR_SET_REUSEPORT:
4862       if (buffer && buflen && (*buflen == sizeof (int)) &&
4863           !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
4864         {
4865           /* VPP-TBD */
4866           if (*(int *) buffer)
4867             VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEPORT);
4868           else
4869             VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEPORT);
4870
4871           if (VPPCOM_DEBUG > 2)
4872             clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_REUSEPORT: %d, "
4873                           "buflen %d, #VPP-TBD#", getpid (),
4874                           VCL_SESS_ATTR_TEST (session->attr,
4875                                               VCL_SESS_ATTR_REUSEPORT),
4876                           *buflen);
4877           if (VPPCOM_DEBUG > 0)
4878             {
4879                 /* *INDENT-OFF* */
4880               ELOG_TYPE_DECLARE (e) =
4881                 {
4882                   .format = "VPPCOM_ATTR_SET_REUSEPORT: %d buflen=%d",
4883                   .format_args = "i4i4",
4884                 };
4885
4886               struct {
4887                 u32 data[2];
4888               } * ed;
4889
4890               ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4891               ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
4892                                                 VCL_SESS_ATTR_REUSEPORT);
4893               ed->data[1] = *buflen;
4894               /* *INDENT-ON* */
4895             }
4896         }
4897       else
4898         rv = VPPCOM_EINVAL;
4899       break;
4900
4901     case VPPCOM_ATTR_GET_BROADCAST:
4902       if (buffer && buflen && (*buflen >= sizeof (int)))
4903         {
4904           /* VPP-TBD */
4905           *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
4906                                                 VCL_SESS_ATTR_BROADCAST);
4907           *buflen = sizeof (int);
4908
4909           if (VPPCOM_DEBUG > 2)
4910             clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_BROADCAST: %d, "
4911                           "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
4912                           *buflen);
4913           if (VPPCOM_DEBUG > 0)
4914             {
4915                 /* *INDENT-OFF* */
4916               ELOG_TYPE_DECLARE (e) =
4917                 {
4918                   .format = "VPPCOM_ATTR_GET_BROADCAST: %d buflen=%d",
4919                   .format_args = "i4i4",
4920                 };
4921
4922               struct {
4923                 u32 data[2];
4924               } * ed;
4925
4926               ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4927               ed->data[0] = *(int *) buffer;
4928               ed->data[1] = *buflen;
4929               /* *INDENT-ON* */
4930             }
4931         }
4932       else
4933         rv = VPPCOM_EINVAL;
4934       break;
4935
4936     case VPPCOM_ATTR_SET_BROADCAST:
4937       if (buffer && buflen && (*buflen == sizeof (int)))
4938         {
4939           /* VPP-TBD */
4940           if (*(int *) buffer)
4941             VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_BROADCAST);
4942           else
4943             VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_BROADCAST);
4944
4945           if (VPPCOM_DEBUG > 2)
4946             clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_BROADCAST: %d, "
4947                           "buflen %d, #VPP-TBD#", getpid (),
4948                           VCL_SESS_ATTR_TEST (session->attr,
4949                                               VCL_SESS_ATTR_BROADCAST),
4950                           *buflen);
4951           if (VPPCOM_DEBUG > 0)
4952             {
4953                 /* *INDENT-OFF* */
4954               ELOG_TYPE_DECLARE (e) =
4955                 {
4956                   .format = "VPPCOM_ATTR_SET_BROADCAST: %d buflen=%d",
4957                   .format_args = "i4i4",
4958                 };
4959
4960               struct {
4961                 u32 data[2];
4962               } * ed;
4963
4964               ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4965               ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
4966                                                 VCL_SESS_ATTR_BROADCAST);
4967               ed->data[1] = *buflen;
4968               /* *INDENT-ON* */
4969             }
4970         }
4971       else
4972         rv = VPPCOM_EINVAL;
4973       break;
4974
4975     case VPPCOM_ATTR_GET_V6ONLY:
4976       if (buffer && buflen && (*buflen >= sizeof (int)))
4977         {
4978           /* VPP-TBD */
4979           *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
4980                                                 VCL_SESS_ATTR_V6ONLY);
4981           *buflen = sizeof (int);
4982
4983           if (VPPCOM_DEBUG > 2)
4984             clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_V6ONLY: %d, "
4985                           "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
4986                           *buflen);
4987           if (VPPCOM_DEBUG > 0)
4988             {
4989                 /* *INDENT-OFF* */
4990               ELOG_TYPE_DECLARE (e) =
4991                 {
4992                   .format = "VPPCOM_ATTR_GET_V6ONLY: %d buflen=%d",
4993                   .format_args = "i4i4",
4994                 };
4995
4996               struct {
4997                 u32 data[2];
4998               } * ed;
4999
5000               ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5001               ed->data[0] = *(int *) buffer;
5002               ed->data[1] = *buflen;
5003               /* *INDENT-ON* */
5004             }
5005         }
5006       else
5007         rv = VPPCOM_EINVAL;
5008       break;
5009
5010     case VPPCOM_ATTR_SET_V6ONLY:
5011       if (buffer && buflen && (*buflen == sizeof (int)))
5012         {
5013           /* VPP-TBD */
5014           if (*(int *) buffer)
5015             VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_V6ONLY);
5016           else
5017             VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_V6ONLY);
5018
5019           if (VPPCOM_DEBUG > 2)
5020             clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_V6ONLY: %d, "
5021                           "buflen %d, #VPP-TBD#", getpid (),
5022                           VCL_SESS_ATTR_TEST (session->attr,
5023                                               VCL_SESS_ATTR_V6ONLY), *buflen);
5024           if (VPPCOM_DEBUG > 0)
5025             {
5026                 /* *INDENT-OFF* */
5027               ELOG_TYPE_DECLARE (e) =
5028                 {
5029                   .format = "VPPCOM_ATTR_SET_V6ONLY: %d buflen=%d",
5030                   .format_args = "i4i4",
5031                 };
5032
5033               struct {
5034                 u32 data[2];
5035               } * ed;
5036
5037               ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5038               ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
5039                                                 VCL_SESS_ATTR_V6ONLY);
5040               ed->data[1] = *buflen;
5041               /* *INDENT-ON* */
5042             }
5043         }
5044       else
5045         rv = VPPCOM_EINVAL;
5046       break;
5047
5048     case VPPCOM_ATTR_GET_KEEPALIVE:
5049       if (buffer && buflen && (*buflen >= sizeof (int)))
5050         {
5051           /* VPP-TBD */
5052           *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
5053                                                 VCL_SESS_ATTR_KEEPALIVE);
5054           *buflen = sizeof (int);
5055
5056           if (VPPCOM_DEBUG > 2)
5057             clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_KEEPALIVE: %d, "
5058                           "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
5059                           *buflen);
5060           if (VPPCOM_DEBUG > 0)
5061             {
5062                 /* *INDENT-OFF* */
5063               ELOG_TYPE_DECLARE (e) =
5064                 {
5065                   .format = "VPPCOM_ATTR_GET_KEEPALIVE: %d buflen=%d",
5066                   .format_args = "i4i4",
5067                 };
5068
5069               struct {
5070                 u32 data[2];
5071               } * ed;
5072
5073               ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5074               ed->data[0] = *(int *) buffer;
5075               ed->data[1] = *buflen;
5076               /* *INDENT-ON* */
5077             }
5078         }
5079       else
5080         rv = VPPCOM_EINVAL;
5081       break;
5082
5083     case VPPCOM_ATTR_SET_KEEPALIVE:
5084       if (buffer && buflen && (*buflen == sizeof (int)))
5085         {
5086           /* VPP-TBD */
5087           if (*(int *) buffer)
5088             VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_KEEPALIVE);
5089           else
5090             VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_KEEPALIVE);
5091
5092           if (VPPCOM_DEBUG > 2)
5093             clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_KEEPALIVE: %d, "
5094                           "buflen %d, #VPP-TBD#", getpid (),
5095                           VCL_SESS_ATTR_TEST (session->attr,
5096                                               VCL_SESS_ATTR_KEEPALIVE),
5097                           *buflen);
5098           if (VPPCOM_DEBUG > 0)
5099             {
5100                 /* *INDENT-OFF* */
5101               ELOG_TYPE_DECLARE (e) =
5102                 {
5103                   .format = "VPPCOM_ATTR_SET_KEEPALIVE: %d buflen=%d",
5104                   .format_args = "i4i4",
5105                 };
5106
5107               struct {
5108                 u32 data[2];
5109               } * ed;
5110
5111               ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5112               ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
5113                                                 VCL_SESS_ATTR_KEEPALIVE);
5114               ed->data[1] = *buflen;
5115               /* *INDENT-ON* */
5116             }
5117         }
5118       else
5119         rv = VPPCOM_EINVAL;
5120       break;
5121
5122     case VPPCOM_ATTR_GET_TCP_NODELAY:
5123       if (buffer && buflen && (*buflen >= sizeof (int)))
5124         {
5125           /* VPP-TBD */
5126           *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
5127                                                 VCL_SESS_ATTR_TCP_NODELAY);
5128           *buflen = sizeof (int);
5129
5130           if (VPPCOM_DEBUG > 2)
5131             clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_TCP_NODELAY: %d, "
5132                           "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
5133                           *buflen);
5134           if (VPPCOM_DEBUG > 0)
5135             {
5136                 /* *INDENT-OFF* */
5137               ELOG_TYPE_DECLARE (e) =
5138                 {
5139                   .format = "VPPCOM_ATTR_GET_TCP_NODELAY: %d buflen=%d",
5140                   .format_args = "i4i4",
5141                 };
5142
5143               struct {
5144                 u32 data[2];
5145               } * ed;
5146
5147               ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5148               ed->data[0] = *(int *) buffer;
5149               ed->data[1] = *buflen;
5150               /* *INDENT-ON* */
5151             }
5152         }
5153       else
5154         rv = VPPCOM_EINVAL;
5155       break;
5156
5157     case VPPCOM_ATTR_SET_TCP_NODELAY:
5158       if (buffer && buflen && (*buflen == sizeof (int)))
5159         {
5160           /* VPP-TBD */
5161           if (*(int *) buffer)
5162             VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
5163           else
5164             VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
5165
5166           if (VPPCOM_DEBUG > 2)
5167             clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_TCP_NODELAY: %d, "
5168                           "buflen %d, #VPP-TBD#", getpid (),
5169                           VCL_SESS_ATTR_TEST (session->attr,
5170                                               VCL_SESS_ATTR_TCP_NODELAY),
5171                           *buflen);
5172           if (VPPCOM_DEBUG > 0)
5173             {
5174                 /* *INDENT-OFF* */
5175               ELOG_TYPE_DECLARE (e) =
5176                 {
5177                   .format = "VPPCOM_ATTR_SET_TCP_NODELAY: %d buflen=%d",
5178                   .format_args = "i4i4",
5179                 };
5180
5181               struct {
5182                 u32 data[2];
5183               } * ed;
5184
5185               ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5186               ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
5187                                                 VCL_SESS_ATTR_TCP_NODELAY);
5188               ed->data[1] = *buflen;
5189               /* *INDENT-ON* */
5190             }
5191         }
5192       else
5193         rv = VPPCOM_EINVAL;
5194       break;
5195
5196     case VPPCOM_ATTR_GET_TCP_KEEPIDLE:
5197       if (buffer && buflen && (*buflen >= sizeof (int)))
5198         {
5199           /* VPP-TBD */
5200           *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
5201                                                 VCL_SESS_ATTR_TCP_KEEPIDLE);
5202           *buflen = sizeof (int);
5203
5204           if (VPPCOM_DEBUG > 2)
5205             clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d, "
5206                           "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
5207                           *buflen);
5208           if (VPPCOM_DEBUG > 0)
5209             {
5210                 /* *INDENT-OFF* */
5211               ELOG_TYPE_DECLARE (e) =
5212                 {
5213                   .format = "VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d buflen=%d",
5214                   .format_args = "i4i4",
5215                 };
5216
5217               struct {
5218                 u32 data[2];
5219               } * ed;
5220
5221               ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5222               ed->data[0] = *(int *) buffer;
5223               ed->data[1] = *buflen;
5224               /* *INDENT-ON* */
5225             }
5226         }
5227       else
5228         rv = VPPCOM_EINVAL;
5229       break;
5230
5231     case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
5232       if (buffer && buflen && (*buflen == sizeof (int)))
5233         {
5234           /* VPP-TBD */
5235           if (*(int *) buffer)
5236             VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
5237           else
5238             VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
5239
5240           if (VPPCOM_DEBUG > 2)
5241             clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d, "
5242                           "buflen %d, #VPP-TBD#", getpid (),
5243                           VCL_SESS_ATTR_TEST (session->attr,
5244                                               VCL_SESS_ATTR_TCP_KEEPIDLE),
5245                           *buflen);
5246           if (VPPCOM_DEBUG > 0)
5247             {
5248                 /* *INDENT-OFF* */
5249               ELOG_TYPE_DECLARE (e) =
5250                 {
5251                   .format = "VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d buflen=%d",
5252                   .format_args = "i4i4",
5253                 };
5254
5255               struct {
5256                 u32 data[2];
5257               } * ed;
5258
5259               ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5260               ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
5261                                                 VCL_SESS_ATTR_TCP_KEEPIDLE);
5262               ed->data[1] = *buflen;
5263               /* *INDENT-ON* */
5264             }
5265         }
5266       else
5267         rv = VPPCOM_EINVAL;
5268       break;
5269
5270     case VPPCOM_ATTR_GET_TCP_KEEPINTVL:
5271       if (buffer && buflen && (*buflen >= sizeof (int)))
5272         {
5273           /* VPP-TBD */
5274           *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
5275                                                 VCL_SESS_ATTR_TCP_KEEPINTVL);
5276           *buflen = sizeof (int);
5277
5278           if (VPPCOM_DEBUG > 2)
5279             clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_TCP_KEEPINTVL: %d, "
5280                           "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
5281                           *buflen);
5282           if (VPPCOM_DEBUG > 0)
5283             {
5284                 /* *INDENT-OFF* */
5285               ELOG_TYPE_DECLARE (e) =
5286                 {
5287                   .format = "VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d buflen=%d",
5288                   .format_args = "i4i4",
5289                 };
5290
5291               struct {
5292                 u32 data[2];
5293               } * ed;
5294
5295               ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5296               ed->data[0] = *(int *) buffer;
5297               ed->data[1] = *buflen;
5298               /* *INDENT-ON* */
5299             }
5300         }
5301       else
5302         rv = VPPCOM_EINVAL;
5303       break;
5304
5305     case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
5306       if (buffer && buflen && (*buflen == sizeof (int)))
5307         {
5308           /* VPP-TBD */
5309           if (*(int *) buffer)
5310             VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
5311           else
5312             VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
5313
5314           if (VPPCOM_DEBUG > 2)
5315             clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d, "
5316                           "buflen %d, #VPP-TBD#", getpid (),
5317                           VCL_SESS_ATTR_TEST (session->attr,
5318                                               VCL_SESS_ATTR_TCP_KEEPINTVL),
5319                           *buflen);
5320           if (VPPCOM_DEBUG > 0)
5321             {
5322                 /* *INDENT-OFF* */
5323               ELOG_TYPE_DECLARE (e) =
5324                 {
5325                   .format = "VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d buflen=%d",
5326                   .format_args = "i4i4",
5327                 };
5328
5329               struct {
5330                 u32 data[2];
5331               } * ed;
5332
5333               ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5334               ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
5335                                                 VCL_SESS_ATTR_TCP_KEEPINTVL);
5336               ed->data[1] = *buflen;
5337               /* *INDENT-ON* */
5338             }
5339         }
5340       else
5341         rv = VPPCOM_EINVAL;
5342       break;
5343
5344     case VPPCOM_ATTR_GET_TCP_USER_MSS:
5345       if (buffer && buflen && (*buflen >= sizeof (u32)))
5346         {
5347           /* VPP-TBD */
5348           *(u32 *) buffer = session->user_mss;
5349           *buflen = sizeof (int);
5350
5351           if (VPPCOM_DEBUG > 2)
5352             clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_TCP_USER_MSS: %d, "
5353                           "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
5354                           *buflen);
5355           if (VPPCOM_DEBUG > 0)
5356             {
5357                 /* *INDENT-OFF* */
5358               ELOG_TYPE_DECLARE (e) =
5359                 {
5360                   .format = "VPPCOM_ATTR_GET_TCP_USER_MSS: %d buflen=%d",
5361                   .format_args = "i4i4",
5362                 };
5363
5364               struct {
5365                 u32 data[2];
5366               } * ed;
5367
5368               ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5369               ed->data[0] = *(int *) buffer;
5370               ed->data[1] = *buflen;
5371               /* *INDENT-ON* */
5372             }
5373         }
5374       else
5375         rv = VPPCOM_EINVAL;
5376       break;
5377
5378     case VPPCOM_ATTR_SET_TCP_USER_MSS:
5379       if (buffer && buflen && (*buflen == sizeof (u32)))
5380         {
5381           /* VPP-TBD */
5382           session->user_mss = *(u32 *) buffer;
5383
5384           if (VPPCOM_DEBUG > 2)
5385             clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_TCP_USER_MSS: %u, "
5386                           "buflen %d, #VPP-TBD#", getpid (),
5387                           session->user_mss, *buflen);
5388           if (VPPCOM_DEBUG > 0)
5389             {
5390                 /* *INDENT-OFF* */
5391               ELOG_TYPE_DECLARE (e) =
5392                 {
5393                   .format = "VPPCOM_ATTR_SET_TCP_USER_MSS: %d buflen=%d",
5394                   .format_args = "i4i4",
5395                 };
5396
5397               struct {
5398                 u32 data[2];
5399               } * ed;
5400
5401               ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5402               ed->data[0] = session->user_mss;
5403               ed->data[1] = *buflen;
5404               /* *INDENT-ON* */
5405             }
5406         }
5407       else
5408         rv = VPPCOM_EINVAL;
5409       break;
5410
5411     default:
5412       rv = VPPCOM_EINVAL;
5413       break;
5414     }
5415
5416 done:
5417   clib_spinlock_unlock (&vcm->sessions_lockp);
5418   return rv;
5419 }
5420
5421 int
5422 vppcom_session_recvfrom (uint32_t session_index, void *buffer,
5423                          uint32_t buflen, int flags, vppcom_endpt_t * ep)
5424 {
5425   int rv = VPPCOM_OK;
5426   session_t *session = 0;
5427
5428   if (ep)
5429     {
5430       clib_spinlock_lock (&vcm->sessions_lockp);
5431       rv = vppcom_session_at_index (session_index, &session);
5432       if (PREDICT_FALSE (rv))
5433         {
5434           clib_spinlock_unlock (&vcm->sessions_lockp);
5435           if (VPPCOM_DEBUG > 0)
5436             clib_warning ("VCL<%d>: invalid session, "
5437                           "sid (%u) has been closed!",
5438                           getpid (), session_index);
5439           if (VPPCOM_DEBUG > 0)
5440             {
5441               /* *INDENT-OFF* */
5442               ELOG_TYPE_DECLARE (e) =
5443                 {
5444                   .format = "invalid session: %d closed",
5445                   .format_args = "i4",
5446                 };
5447
5448               struct {
5449                 u32 data;
5450               } * ed;
5451
5452               ed = ELOG_TRACK_DATA (&vcm->elog_main, e, vcm->elog_track);
5453               ed->data = session_index;
5454               /* *INDENT-ON* */
5455             }
5456           rv = VPPCOM_EBADFD;
5457           clib_spinlock_unlock (&vcm->sessions_lockp);
5458           goto done;
5459         }
5460       ep->is_ip4 = session->peer_addr.is_ip4;
5461       ep->port = session->peer_port;
5462       if (session->peer_addr.is_ip4)
5463         clib_memcpy (ep->ip, &session->peer_addr.ip46.ip4,
5464                      sizeof (ip4_address_t));
5465       else
5466         clib_memcpy (ep->ip, &session->peer_addr.ip46.ip6,
5467                      sizeof (ip6_address_t));
5468       clib_spinlock_unlock (&vcm->sessions_lockp);
5469     }
5470
5471   if (flags == 0)
5472     rv = vppcom_session_read (session_index, buffer, buflen);
5473   else if (flags & MSG_PEEK)
5474     rv = vppcom_session_peek (session_index, buffer, buflen);
5475   else
5476     {
5477       clib_warning ("VCL<%d>: Unsupport flags for recvfrom %d",
5478                     getpid (), flags);
5479       rv = VPPCOM_EAFNOSUPPORT;
5480     }
5481
5482 done:
5483   return rv;
5484 }
5485
5486 int
5487 vppcom_session_sendto (uint32_t session_index, void *buffer,
5488                        uint32_t buflen, int flags, vppcom_endpt_t * ep)
5489 {
5490   if (!buffer)
5491     return VPPCOM_EINVAL;
5492
5493   if (ep)
5494     {
5495       // TBD
5496       return VPPCOM_EINVAL;
5497     }
5498
5499   if (flags)
5500     {
5501       // TBD check the flags and do the right thing
5502       if (VPPCOM_DEBUG > 2)
5503         clib_warning ("VCL<%d>: handling flags 0x%u (%d) "
5504                       "not implemented yet.", getpid (), flags, flags);
5505     }
5506
5507   return (vppcom_session_write (session_index, buffer, buflen));
5508 }
5509
5510 int
5511 vppcom_poll (vcl_poll_t * vp, uint32_t n_sids, double wait_for_time)
5512 {
5513   f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
5514   u32 i, keep_trying = 1;
5515   int rv, num_ev = 0;
5516
5517   if (VPPCOM_DEBUG > 3)
5518     clib_warning ("VCL<%d>: vp %p, nsids %u, wait_for_time %f",
5519                   getpid (), vp, n_sids, wait_for_time);
5520
5521   if (!vp)
5522     return VPPCOM_EFAULT;
5523
5524   do
5525     {
5526       session_t *session;
5527
5528       for (i = 0; i < n_sids; i++)
5529         {
5530           ASSERT (vp[i].revents);
5531
5532           VCL_LOCK_AND_GET_SESSION (vp[i].sid, &session);
5533           clib_spinlock_unlock (&vcm->sessions_lockp);
5534
5535           if (*vp[i].revents)
5536             *vp[i].revents = 0;
5537
5538           if (POLLIN & vp[i].events)
5539             {
5540               VCL_LOCK_AND_GET_SESSION (vp[i].sid, &session);
5541               rv = vppcom_session_read_ready (session, vp[i].sid);
5542               clib_spinlock_unlock (&vcm->sessions_lockp);
5543               if (rv > 0)
5544                 {
5545                   *vp[i].revents |= POLLIN;
5546                   num_ev++;
5547                 }
5548               else if (rv < 0)
5549                 {
5550                   switch (rv)
5551                     {
5552                     case VPPCOM_ECONNRESET:
5553                       *vp[i].revents = POLLHUP;
5554                       break;
5555
5556                     default:
5557                       *vp[i].revents = POLLERR;
5558                       break;
5559                     }
5560                   num_ev++;
5561                 }
5562             }
5563
5564           if (POLLOUT & vp[i].events)
5565             {
5566               VCL_LOCK_AND_GET_SESSION (vp[i].sid, &session);
5567               rv = vppcom_session_write_ready (session, vp[i].sid);
5568               clib_spinlock_unlock (&vcm->sessions_lockp);
5569               if (rv > 0)
5570                 {
5571                   *vp[i].revents |= POLLOUT;
5572                   num_ev++;
5573                 }
5574               else if (rv < 0)
5575                 {
5576                   switch (rv)
5577                     {
5578                     case VPPCOM_ECONNRESET:
5579                       *vp[i].revents = POLLHUP;
5580                       break;
5581
5582                     default:
5583                       *vp[i].revents = POLLERR;
5584                       break;
5585                     }
5586                   num_ev++;
5587                 }
5588             }
5589
5590           if (0)                // Note "done:" label used by VCL_LOCK_AND_GET_SESSION()
5591             {
5592             done:
5593               *vp[i].revents = POLLNVAL;
5594               num_ev++;
5595             }
5596         }
5597       if (wait_for_time != -1)
5598         keep_trying = (clib_time_now (&vcm->clib_time) <= timeout) ? 1 : 0;
5599     }
5600   while ((num_ev == 0) && keep_trying);
5601
5602   if (VPPCOM_DEBUG > 3)
5603     {
5604       clib_warning ("VCL<%d>: returning %d", getpid (), num_ev);
5605       for (i = 0; i < n_sids; i++)
5606         {
5607           clib_warning ("VCL<%d>: vp[%d].sid %d (0x%x), .events 0x%x, "
5608                         ".revents 0x%x", getpid (), i, vp[i].sid, vp[i].sid,
5609                         vp[i].events, *vp[i].revents);
5610         }
5611     }
5612   return num_ev;
5613 }
5614
5615 /*
5616  * fd.io coding-style-patch-verification: ON
5617  *
5618  * Local Variables:
5619  * eval: (c-set-style "gnu")
5620  * End:
5621  */