vppcomm refactor
[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   VCL_LOCK_AND_GET_SESSION (listen_session_index, &listen_session);
2686
2687   if (listen_session->is_vep)
2688     {
2689       clib_spinlock_unlock (&vcm->sessions_lockp);
2690       clib_warning ("VCL<%d>: ERROR: sid %u: cannot listen on an "
2691                     "epoll session!", getpid (), listen_session_index);
2692       rv = VPPCOM_EBADFD;
2693       goto done;
2694     }
2695
2696   listen_vpp_handle = listen_session->vpp_handle;
2697   if (listen_session->state & STATE_LISTEN)
2698     {
2699       clib_spinlock_unlock (&vcm->sessions_lockp);
2700       if (VPPCOM_DEBUG > 0)
2701         clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
2702                       "already in listen state!",
2703                       getpid (), listen_vpp_handle, listen_session_index);
2704       rv = VPPCOM_OK;
2705       goto done;
2706     }
2707
2708   if (VPPCOM_DEBUG > 0)
2709     clib_warning ("VCL<%d>: vpp handle 0x%llx, "
2710                   "sid %u: sending bind request...",
2711                   getpid (), listen_vpp_handle, listen_session_index);
2712
2713   vppcom_send_bind_sock (listen_session, listen_session_index);
2714   clib_spinlock_unlock (&vcm->sessions_lockp);
2715   retval =
2716     vppcom_wait_for_session_state_change (listen_session_index, STATE_LISTEN,
2717                                           vcm->cfg.session_timeout);
2718
2719   VCL_LOCK_AND_GET_SESSION (listen_session_index, &listen_session);
2720   if (PREDICT_FALSE (retval))
2721     {
2722       if (VPPCOM_DEBUG > 0)
2723         clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: bind failed! "
2724                       "returning %d (%s)", getpid (),
2725                       listen_session->vpp_handle, listen_session_index,
2726                       retval, vppcom_retval_str (retval));
2727       clib_spinlock_unlock (&vcm->sessions_lockp);
2728       rv = retval;
2729       goto done;
2730     }
2731
2732   clib_fifo_validate (vcm->client_session_index_fifo, q_len);
2733   clib_spinlock_unlock (&vcm->sessions_lockp);
2734 done:
2735   return rv;
2736 }
2737
2738 int
2739 vppcom_session_accept (uint32_t listen_session_index, vppcom_endpt_t * ep,
2740                        uint32_t flags)
2741 {
2742   session_t *listen_session = 0;
2743   session_t *client_session = 0;
2744   u32 client_session_index = ~0;
2745   int rv;
2746   f64 wait_for;
2747   u64 listen_vpp_handle;
2748
2749   VCL_LOCK_AND_GET_SESSION (listen_session_index, &listen_session);
2750
2751   if (listen_session->is_vep)
2752     {
2753       clib_spinlock_unlock (&vcm->sessions_lockp);
2754       clib_warning ("VCL<%d>: ERROR: sid %u: cannot accept on an "
2755                     "epoll session!", getpid (), listen_session_index);
2756       rv = VPPCOM_EBADFD;
2757       goto done;
2758     }
2759
2760   listen_vpp_handle = listen_session->vpp_handle;
2761   if (listen_session->state != STATE_LISTEN)
2762     {
2763       clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
2764                     "not in listen state! state 0x%x (%s)", getpid (),
2765                     listen_vpp_handle, listen_session_index,
2766                     listen_session->state,
2767                     vppcom_session_state_str (listen_session->state));
2768       clib_spinlock_unlock (&vcm->sessions_lockp);
2769       rv = VPPCOM_EBADFD;
2770       goto done;
2771     }
2772   wait_for = (VCL_SESS_ATTR_TEST (listen_session->attr,
2773                                   VCL_SESS_ATTR_NONBLOCK))
2774     ? 0 : vcm->cfg.accept_timeout;
2775
2776   clib_spinlock_unlock (&vcm->sessions_lockp);
2777
2778   while (1)
2779     {
2780       rv = vppcom_wait_for_client_session_index (wait_for);
2781       if (rv)
2782         {
2783           if ((VPPCOM_DEBUG > 0))
2784             clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
2785                           "accept failed! returning %d (%s)", getpid (),
2786                           listen_vpp_handle, listen_session_index,
2787                           rv, vppcom_retval_str (rv));
2788           if (wait_for == 0)
2789             goto done;
2790         }
2791       else
2792         break;
2793     }
2794
2795   clib_spinlock_lock (&vcm->sessions_lockp);
2796   clib_fifo_sub1 (vcm->client_session_index_fifo, client_session_index);
2797   rv = vppcom_session_at_index (client_session_index, &client_session);
2798   if (PREDICT_FALSE (rv))
2799     {
2800       rv = VPPCOM_ECONNABORTED;
2801       clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: client sid %u "
2802                     "lookup failed! returning %d (%s)", getpid (),
2803                     listen_vpp_handle, listen_session_index,
2804                     client_session_index, rv, vppcom_retval_str (rv));
2805       goto done;
2806     }
2807
2808   if (flags & O_NONBLOCK)
2809     VCL_SESS_ATTR_SET (client_session->attr, VCL_SESS_ATTR_NONBLOCK);
2810   else
2811     VCL_SESS_ATTR_CLR (client_session->attr, VCL_SESS_ATTR_NONBLOCK);
2812
2813   if (VPPCOM_DEBUG > 0)
2814     clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: Got a client request! "
2815                   "vpp handle 0x%llx, sid %u, flags %d, is_nonblocking %u",
2816                   getpid (), listen_vpp_handle, listen_session_index,
2817                   client_session->vpp_handle, client_session_index,
2818                   flags, VCL_SESS_ATTR_TEST (client_session->attr,
2819                                              VCL_SESS_ATTR_NONBLOCK));
2820
2821   if (ep)
2822     {
2823       ep->is_ip4 = client_session->peer_addr.is_ip4;
2824       ep->port = client_session->peer_port;
2825       if (client_session->peer_addr.is_ip4)
2826         clib_memcpy (ep->ip, &client_session->peer_addr.ip46.ip4,
2827                      sizeof (ip4_address_t));
2828       else
2829         clib_memcpy (ep->ip, &client_session->peer_addr.ip46.ip6,
2830                      sizeof (ip6_address_t));
2831     }
2832
2833   vppcom_send_accept_session_reply (client_session->vpp_handle,
2834                                     client_session->client_context,
2835                                     0 /* retval OK */ );
2836
2837   if (VPPCOM_DEBUG > 0)
2838     clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: accepted vpp handle "
2839                   "0x%llx, sid %u connection to local %s address "
2840                   "%U port %u", getpid (), listen_vpp_handle,
2841                   listen_session_index, client_session->vpp_handle,
2842                   client_session_index,
2843                   client_session->lcl_addr.is_ip4 ? "IPv4" : "IPv6",
2844                   format_ip46_address, &client_session->lcl_addr.ip46,
2845                   client_session->lcl_addr.is_ip4,
2846                   clib_net_to_host_u16 (client_session->lcl_port));
2847
2848   if (VPPCOM_DEBUG > 0)
2849     {
2850       client_session->elog_track.name =
2851         (char *) format (0, "C:%d:S:%d%c", vcm->my_client_index,
2852                          client_session_index, 0);
2853       elog_track_register (&vcm->elog_main, &client_session->elog_track);
2854
2855       // Two elog entries due to 20-byte per entry constraint.
2856       /* *INDENT-OFF* */
2857       ELOG_TYPE_DECLARE (e) =
2858       {
2859         .format = "accept: listen_handle:%x from_handle:%x",
2860         .format_args = "i8i8",
2861       };
2862
2863       struct
2864       {
2865         u64 handle[2];
2866       } *ed;
2867
2868       ed = ELOG_TRACK_DATA (&vcm->elog_main, e, client_session->elog_track);
2869       ed->handle[0] = listen_vpp_handle;
2870       ed->handle[1] = client_session->vpp_handle;
2871       /* *INDENT-ON* */
2872
2873       if (client_session->lcl_addr.is_ip4)
2874         {
2875           /* *INDENT-OFF* */
2876           ELOG_TYPE_DECLARE (e2) =
2877           {
2878             .format = "accept: S:%d %d.%d.%d.%d:%d ",
2879             .format_args = "i4i1i1i1i1i2",
2880           };
2881
2882           CLIB_PACKED (struct {
2883             u32 session;
2884             u8 addr[4];
2885             u16 port;
2886           }) * ed2;
2887
2888           ed2 =
2889             ELOG_TRACK_DATA (&vcm->elog_main, e2, client_session->elog_track);
2890           ed2->session = client_session_index;
2891           ed2->addr[0] = client_session->lcl_addr.ip46.ip4.as_u8[0];
2892           ed2->addr[1] = client_session->lcl_addr.ip46.ip4.as_u8[1];
2893           ed2->addr[2] = client_session->lcl_addr.ip46.ip4.as_u8[2];
2894           ed2->addr[3] = client_session->lcl_addr.ip46.ip4.as_u8[3];
2895           ed2->port = clib_net_to_host_u16 (client_session->lcl_port);
2896           /* *INDENT-ON* */
2897         }
2898     }
2899
2900   clib_spinlock_unlock (&vcm->sessions_lockp);
2901   rv = (int) client_session_index;
2902 done:
2903   return rv;
2904 }
2905
2906 int
2907 vppcom_session_connect (uint32_t session_index, vppcom_endpt_t * server_ep)
2908 {
2909   session_t *session = 0;
2910   u64 vpp_handle = 0;
2911   int rv, retval = VPPCOM_OK;
2912
2913   VCL_LOCK_AND_GET_SESSION (session_index, &session);
2914
2915   if (PREDICT_FALSE (session->is_vep))
2916     {
2917       clib_spinlock_unlock (&vcm->sessions_lockp);
2918       clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
2919                     "connect on an epoll session!", getpid (), session_index);
2920       rv = VPPCOM_EBADFD;
2921       goto done;
2922     }
2923
2924   if (PREDICT_FALSE (session->state & CLIENT_STATE_OPEN))
2925     {
2926       if (VPPCOM_DEBUG > 0)
2927         clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: session already "
2928                       "connected to %s %U port %d proto %s, state 0x%x (%s)",
2929                       getpid (), session->vpp_handle, session_index,
2930                       session->peer_addr.is_ip4 ? "IPv4" : "IPv6",
2931                       format_ip46_address,
2932                       &session->peer_addr.ip46, session->peer_addr.is_ip4,
2933                       clib_net_to_host_u16 (session->peer_port),
2934                       session->proto ? "UDP" : "TCP", session->state,
2935                       vppcom_session_state_str (session->state));
2936
2937       clib_spinlock_unlock (&vcm->sessions_lockp);
2938       goto done;
2939     }
2940
2941   session->peer_addr.is_ip4 = server_ep->is_ip4;
2942   session->peer_addr.ip46 = to_ip46 (!server_ep->is_ip4, server_ep->ip);
2943   session->peer_port = server_ep->port;
2944
2945   if (VPPCOM_DEBUG > 0)
2946     clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: connecting to server "
2947                   "%s %U port %d proto %s",
2948                   getpid (), session->vpp_handle, session_index,
2949                   session->peer_addr.is_ip4 ? "IPv4" : "IPv6",
2950                   format_ip46_address,
2951                   &session->peer_addr.ip46, session->peer_addr.is_ip4,
2952                   clib_net_to_host_u16 (session->peer_port),
2953                   session->proto ? "UDP" : "TCP");
2954
2955   vppcom_send_connect_sock (session, session_index);
2956   clib_spinlock_unlock (&vcm->sessions_lockp);
2957
2958   retval =
2959     vppcom_wait_for_session_state_change (session_index, STATE_CONNECT,
2960                                           vcm->cfg.session_timeout);
2961
2962   VCL_LOCK_AND_GET_SESSION (session_index, &session);
2963   vpp_handle = session->vpp_handle;
2964   clib_spinlock_unlock (&vcm->sessions_lockp);
2965
2966 done:
2967   if (PREDICT_FALSE (retval))
2968     {
2969       rv = retval;
2970       if (VPPCOM_DEBUG > 0)
2971         {
2972           if (session)
2973             clib_warning
2974               ("VCL<%d>: vpp handle 0x%llx, sid %u: connect failed! "
2975                "returning %d (%s)", getpid (), vpp_handle,
2976                session_index, rv, vppcom_retval_str (rv));
2977           else
2978             clib_warning ("VCL<%d>: no session for sid %u: connect failed! "
2979                           "returning %d (%s)", getpid (),
2980                           session_index, rv, vppcom_retval_str (rv));
2981         }
2982     }
2983   else if (VPPCOM_DEBUG > 0)
2984     clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: connected!",
2985                   getpid (), vpp_handle, session_index);
2986
2987   return rv;
2988 }
2989
2990 static inline int
2991 vppcom_session_read_internal (uint32_t session_index, void *buf, int n,
2992                               u8 peek)
2993 {
2994   session_t *session = 0;
2995   svm_fifo_t *rx_fifo;
2996   int n_read = 0;
2997   int rv;
2998   int is_nonblocking;
2999
3000   u64 vpp_handle;
3001   u32 poll_et;
3002   session_state_t state;
3003
3004   ASSERT (buf);
3005
3006   VCL_LOCK_AND_GET_SESSION (session_index, &session);
3007
3008   is_nonblocking = VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK);
3009   rx_fifo = session->rx_fifo;
3010   state = session->state;
3011   vpp_handle = session->vpp_handle;
3012
3013   if (PREDICT_FALSE (session->is_vep))
3014     {
3015       clib_spinlock_unlock (&vcm->sessions_lockp);
3016       clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
3017                     "read from an epoll session!", getpid (), session_index);
3018       rv = VPPCOM_EBADFD;
3019       goto done;
3020     }
3021
3022   if (PREDICT_FALSE (!(state & (SERVER_STATE_OPEN | CLIENT_STATE_OPEN))))
3023     {
3024       clib_spinlock_unlock (&vcm->sessions_lockp);
3025       rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
3026
3027       if (VPPCOM_DEBUG > 0)
3028         clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: %s session is "
3029                       "not open! state 0x%x (%s), returning %d (%s)",
3030                       getpid (), vpp_handle, session_index, state,
3031                       vppcom_session_state_str (state),
3032                       rv, vppcom_retval_str (rv));
3033       goto done;
3034     }
3035
3036   clib_spinlock_unlock (&vcm->sessions_lockp);
3037
3038   do
3039     {
3040       if (peek)
3041         n_read = svm_fifo_peek (rx_fifo, 0, n, buf);
3042       else
3043         n_read = svm_fifo_dequeue_nowait (rx_fifo, n, buf);
3044     }
3045   while (!is_nonblocking && (n_read <= 0));
3046
3047   if (n_read <= 0)
3048     {
3049       VCL_LOCK_AND_GET_SESSION (session_index, &session);
3050
3051       poll_et = (((EPOLLET | EPOLLIN) & session->vep.ev.events) ==
3052                  (EPOLLET | EPOLLIN));
3053       if (poll_et)
3054         session->vep.et_mask |= EPOLLIN;
3055
3056       if (state & STATE_CLOSE_ON_EMPTY)
3057         {
3058           rv = VPPCOM_ECONNRESET;
3059
3060           if (VPPCOM_DEBUG > 1)
3061             {
3062               clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: Empty fifo "
3063                             "with session state 0x%x (%s)!"
3064                             "  Setting state to 0x%x (%s), returning %d (%s)",
3065                             getpid (), session->vpp_handle, session_index,
3066                             state, vppcom_session_state_str (state),
3067                             STATE_DISCONNECT,
3068                             vppcom_session_state_str (STATE_DISCONNECT), rv,
3069                             vppcom_retval_str (rv));
3070             }
3071
3072           session->state = STATE_DISCONNECT;
3073         }
3074       else
3075         rv = VPPCOM_EAGAIN;
3076
3077       clib_spinlock_unlock (&vcm->sessions_lockp);
3078     }
3079   else
3080     rv = n_read;
3081
3082   if (VPPCOM_DEBUG > 2)
3083     {
3084       if (rv > 0)
3085         clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: read %d bytes "
3086                       "from (%p)", getpid (), vpp_handle,
3087                       session_index, n_read, rx_fifo);
3088       else
3089         clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: nothing read! "
3090                       "returning %d (%s)", getpid (), vpp_handle,
3091                       session_index, rv, vppcom_retval_str (rv));
3092     }
3093 done:
3094   return rv;
3095 }
3096
3097 int
3098 vppcom_session_read (uint32_t session_index, void *buf, size_t n)
3099 {
3100   return (vppcom_session_read_internal (session_index, buf, n, 0));
3101 }
3102
3103 static int
3104 vppcom_session_peek (uint32_t session_index, void *buf, int n)
3105 {
3106   return (vppcom_session_read_internal (session_index, buf, n, 1));
3107 }
3108
3109 static inline int
3110 vppcom_session_read_ready (session_t * session, u32 session_index)
3111 {
3112   int ready = 0;
3113   u32 poll_et;
3114   int rv;
3115   session_state_t state = session->state;
3116   u64 vpp_handle = session->vpp_handle;
3117
3118   /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
3119   if (PREDICT_FALSE (session->is_vep))
3120     {
3121       clib_warning ("VCL<%d>: ERROR: sid %u: cannot read from an "
3122                     "epoll session!", getpid (), session_index);
3123       rv = VPPCOM_EBADFD;
3124       goto done;
3125     }
3126
3127   if (session->state & STATE_LISTEN)
3128     ready = clib_fifo_elts (vcm->client_session_index_fifo);
3129   else
3130     {
3131       if (!(state & (SERVER_STATE_OPEN | CLIENT_STATE_OPEN | STATE_LISTEN)))
3132         {
3133           rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET :
3134                 VPPCOM_ENOTCONN);
3135
3136           if (VPPCOM_DEBUG > 1)
3137             clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: session is "
3138                           "not open! state 0x%x (%s), returning %d (%s)",
3139                           getpid (), vpp_handle, session_index,
3140                           state, vppcom_session_state_str (state),
3141                           rv, vppcom_retval_str (rv));
3142           goto done;
3143         }
3144
3145       ready = svm_fifo_max_dequeue (session->rx_fifo);
3146     }
3147
3148   if (ready == 0)
3149     {
3150       poll_et =
3151         ((EPOLLET | EPOLLIN) & session->vep.ev.events) == (EPOLLET | EPOLLIN);
3152       if (poll_et)
3153         session->vep.et_mask |= EPOLLIN;
3154
3155       if (state & STATE_CLOSE_ON_EMPTY)
3156         {
3157           rv = VPPCOM_ECONNRESET;
3158
3159           if (VPPCOM_DEBUG > 1)
3160             {
3161               clib_warning ("VCL<%d>: vpp handle 0x%llx, "
3162                             "sid %u: Empty fifo with"
3163                             " session state 0x%x (%s)! Setting state to "
3164                             "0x%x (%s), returning %d (%s)",
3165                             getpid (), session_index, vpp_handle,
3166                             state, vppcom_session_state_str (state),
3167                             STATE_DISCONNECT,
3168                             vppcom_session_state_str (STATE_DISCONNECT), rv,
3169                             vppcom_retval_str (rv));
3170             }
3171           session->state = STATE_DISCONNECT;
3172           goto done;
3173         }
3174     }
3175   rv = ready;
3176
3177   if (vcm->app_event_queue->cursize &&
3178       !pthread_mutex_trylock (&vcm->app_event_queue->mutex))
3179     {
3180       u32 i, n_to_dequeue = vcm->app_event_queue->cursize;
3181       session_fifo_event_t e;
3182
3183       for (i = 0; i < n_to_dequeue; i++)
3184         svm_queue_sub_raw (vcm->app_event_queue, (u8 *) & e);
3185
3186       pthread_mutex_unlock (&vcm->app_event_queue->mutex);
3187     }
3188 done:
3189   return rv;
3190 }
3191
3192 int
3193 vppcom_session_write (uint32_t session_index, void *buf, size_t n)
3194 {
3195   session_t *session = 0;
3196   svm_fifo_t *tx_fifo = 0;
3197   svm_queue_t *q;
3198   session_fifo_event_t evt;
3199   session_state_t state;
3200   int rv, n_write, is_nonblocking;
3201   u32 poll_et;
3202   u64 vpp_handle;
3203
3204   ASSERT (buf);
3205
3206   VCL_LOCK_AND_GET_SESSION (session_index, &session);
3207
3208   tx_fifo = session->tx_fifo;
3209   is_nonblocking = VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK);
3210   vpp_handle = session->vpp_handle;
3211   state = session->state;
3212
3213   if (PREDICT_FALSE (session->is_vep))
3214     {
3215       clib_spinlock_unlock (&vcm->sessions_lockp);
3216       clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
3217                     "cannot write to an epoll session!",
3218                     getpid (), vpp_handle, session_index);
3219
3220       rv = VPPCOM_EBADFD;
3221       goto done;
3222     }
3223
3224   if (!(session->state & (SERVER_STATE_OPEN | CLIENT_STATE_OPEN)))
3225     {
3226       rv =
3227         ((session->state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET :
3228          VPPCOM_ENOTCONN);
3229
3230       clib_spinlock_unlock (&vcm->sessions_lockp);
3231       if (VPPCOM_DEBUG > 1)
3232         clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
3233                       "session is not open! state 0x%x (%s)",
3234                       getpid (), vpp_handle, session_index,
3235                       state, vppcom_session_state_str (state));
3236       goto done;
3237     }
3238
3239   clib_spinlock_unlock (&vcm->sessions_lockp);
3240
3241   do
3242     {
3243       n_write = svm_fifo_enqueue_nowait (tx_fifo, n, (void *) buf);
3244     }
3245   while (!is_nonblocking && (n_write <= 0));
3246
3247   /* If event wasn't set, add one */
3248   if ((n_write > 0) && svm_fifo_set_event (tx_fifo))
3249     {
3250       /* Fabricate TX event, send to vpp */
3251       evt.fifo = tx_fifo;
3252       evt.event_type = FIFO_EVENT_APP_TX;
3253
3254       VCL_LOCK_AND_GET_SESSION (session_index, &session);
3255       q = session->vpp_event_queue;
3256       ASSERT (q);
3257       svm_queue_add (q, (u8 *) & evt, 0 /* do wait for mutex */ );
3258       clib_spinlock_unlock (&vcm->sessions_lockp);
3259       if (VPPCOM_DEBUG > 1)
3260         clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
3261                       "added FIFO_EVENT_APP_TX to "
3262                       "vpp_event_q %p, n_write %d", getpid (),
3263                       vpp_handle, session_index, q, n_write);
3264     }
3265
3266   if (n_write <= 0)
3267     {
3268       VCL_LOCK_AND_GET_SESSION (session_index, &session);
3269
3270       poll_et = (((EPOLLET | EPOLLOUT) & session->vep.ev.events) ==
3271                  (EPOLLET | EPOLLOUT));
3272       if (poll_et)
3273         session->vep.et_mask |= EPOLLOUT;
3274
3275       if (session->state & STATE_CLOSE_ON_EMPTY)
3276         {
3277           rv = VPPCOM_ECONNRESET;
3278
3279           if (VPPCOM_DEBUG > 1)
3280             {
3281               clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
3282                             "Empty fifo with session state 0x%x (%s)!"
3283                             "  Setting state to 0x%x (%s), returning %d (%s)",
3284                             getpid (), session->vpp_handle, session_index,
3285                             session->state,
3286                             vppcom_session_state_str (session->state),
3287                             STATE_DISCONNECT,
3288                             vppcom_session_state_str (STATE_DISCONNECT), rv,
3289                             vppcom_retval_str (rv));
3290             }
3291
3292           session->state = STATE_DISCONNECT;
3293         }
3294       else
3295         rv = VPPCOM_EAGAIN;
3296
3297       clib_spinlock_unlock (&vcm->sessions_lockp);
3298     }
3299   else
3300     rv = n_write;
3301
3302   if (VPPCOM_DEBUG > 2)
3303     {
3304       if (n_write <= 0)
3305         clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
3306                       "FIFO-FULL (%p)", getpid (), vpp_handle,
3307                       session_index, tx_fifo);
3308       else
3309         clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
3310                       "wrote %d bytes tx-fifo: (%p)", getpid (),
3311                       vpp_handle, session_index, n_write, tx_fifo);
3312     }
3313 done:
3314   return rv;
3315 }
3316
3317 static inline int
3318 vppcom_session_write_ready (session_t * session, u32 session_index)
3319 {
3320   int ready;
3321   u32 poll_et;
3322   int rv;
3323
3324   ASSERT (session);
3325
3326   /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
3327   if (PREDICT_FALSE (session->is_vep))
3328     {
3329       clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
3330                     "cannot write to an epoll session!",
3331                     getpid (), session->vpp_handle, session_index);
3332       rv = VPPCOM_EBADFD;
3333       goto done;
3334     }
3335
3336   if (PREDICT_FALSE (session->state & STATE_LISTEN))
3337     {
3338       clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
3339                     "cannot write to a listen session!",
3340                     getpid (), session->vpp_handle, session_index);
3341       rv = VPPCOM_EBADFD;
3342       goto done;
3343     }
3344
3345   if (!(session->state & (SERVER_STATE_OPEN | CLIENT_STATE_OPEN)))
3346     {
3347       session_state_t state = session->state;
3348
3349       rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
3350
3351       clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
3352                     "session is not open! state 0x%x (%s), "
3353                     "returning %d (%s)", getpid (), session->vpp_handle,
3354                     session_index,
3355                     state, vppcom_session_state_str (state),
3356                     rv, vppcom_retval_str (rv));
3357       goto done;
3358     }
3359
3360   ready = svm_fifo_max_enqueue (session->tx_fifo);
3361
3362   if (VPPCOM_DEBUG > 3)
3363     clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
3364                   "peek %s (%p), ready = %d", getpid (),
3365                   session->vpp_handle, session_index,
3366                   session->tx_fifo, ready);
3367
3368   if (ready == 0)
3369     {
3370       poll_et = (((EPOLLET | EPOLLOUT) & session->vep.ev.events) ==
3371                  (EPOLLET | EPOLLOUT));
3372       if (poll_et)
3373         session->vep.et_mask |= EPOLLOUT;
3374
3375       if (session->state & STATE_CLOSE_ON_EMPTY)
3376         {
3377           rv = VPPCOM_ECONNRESET;
3378
3379           if (VPPCOM_DEBUG > 1)
3380             {
3381               clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
3382                             "Empty fifo with session "
3383                             "state 0x%x (%s)! Setting state to 0x%x (%s), "
3384                             "returning %d (%s)", getpid (),
3385                             session->vpp_handle, session_index,
3386                             session->state,
3387                             vppcom_session_state_str (session->state),
3388                             STATE_DISCONNECT,
3389                             vppcom_session_state_str (STATE_DISCONNECT), rv,
3390                             vppcom_retval_str (rv));
3391             }
3392           session->state = STATE_DISCONNECT;
3393           goto done;
3394         }
3395     }
3396   rv = ready;
3397 done:
3398   return rv;
3399 }
3400
3401 int
3402 vppcom_select (unsigned long n_bits, unsigned long *read_map,
3403                unsigned long *write_map, unsigned long *except_map,
3404                double time_to_wait)
3405 {
3406   u32 session_index;
3407   session_t *session = 0;
3408   int rv, bits_set = 0;
3409   f64 timeout = clib_time_now (&vcm->clib_time) + time_to_wait;
3410   u32 minbits = clib_max (n_bits, BITS (uword));
3411
3412   ASSERT (sizeof (clib_bitmap_t) == sizeof (long int));
3413
3414   if (n_bits && read_map)
3415     {
3416       clib_bitmap_validate (vcm->rd_bitmap, minbits);
3417       clib_memcpy (vcm->rd_bitmap, read_map,
3418                    vec_len (vcm->rd_bitmap) * sizeof (clib_bitmap_t));
3419       memset (read_map, 0, vec_len (vcm->rd_bitmap) * sizeof (clib_bitmap_t));
3420     }
3421   if (n_bits && write_map)
3422     {
3423       clib_bitmap_validate (vcm->wr_bitmap, minbits);
3424       clib_memcpy (vcm->wr_bitmap, write_map,
3425                    vec_len (vcm->wr_bitmap) * sizeof (clib_bitmap_t));
3426       memset (write_map, 0,
3427               vec_len (vcm->wr_bitmap) * sizeof (clib_bitmap_t));
3428     }
3429   if (n_bits && except_map)
3430     {
3431       clib_bitmap_validate (vcm->ex_bitmap, minbits);
3432       clib_memcpy (vcm->ex_bitmap, except_map,
3433                    vec_len (vcm->ex_bitmap) * sizeof (clib_bitmap_t));
3434       memset (except_map, 0,
3435               vec_len (vcm->ex_bitmap) * sizeof (clib_bitmap_t));
3436     }
3437
3438   do
3439     {
3440       /* *INDENT-OFF* */
3441       if (n_bits)
3442         {
3443           if (read_map)
3444             {
3445               clib_bitmap_foreach (session_index, vcm->rd_bitmap,
3446                 ({
3447                   clib_spinlock_lock (&vcm->sessions_lockp);
3448                   rv = vppcom_session_at_index (session_index, &session);
3449                   if (rv < 0)
3450                     {
3451                       clib_spinlock_unlock (&vcm->sessions_lockp);
3452                       if (VPPCOM_DEBUG > 1)
3453                         clib_warning ("VCL<%d>: session %d specified in "
3454                                       "read_map is closed.", getpid (),
3455                                       session_index);
3456                       bits_set = VPPCOM_EBADFD;
3457                       goto select_done;
3458                     }
3459
3460                   rv = vppcom_session_read_ready (session, session_index);
3461                   clib_spinlock_unlock (&vcm->sessions_lockp);
3462                   if (except_map && vcm->ex_bitmap &&
3463                       clib_bitmap_get (vcm->ex_bitmap, session_index) &&
3464                       (rv < 0))
3465                     {
3466                       clib_bitmap_set_no_check (except_map, session_index, 1);
3467                       bits_set++;
3468                     }
3469                   else if (rv > 0)
3470                     {
3471                       clib_bitmap_set_no_check (read_map, session_index, 1);
3472                       bits_set++;
3473                     }
3474                 }));
3475             }
3476
3477           if (write_map)
3478             {
3479               clib_bitmap_foreach (session_index, vcm->wr_bitmap,
3480                 ({
3481                   clib_spinlock_lock (&vcm->sessions_lockp);
3482                   rv = vppcom_session_at_index (session_index, &session);
3483                   if (rv < 0)
3484                     {
3485                       clib_spinlock_unlock (&vcm->sessions_lockp);
3486                       if (VPPCOM_DEBUG > 0)
3487                         clib_warning ("VCL<%d>: session %d specified in "
3488                                       "write_map is closed.", getpid (),
3489                                       session_index);
3490                       bits_set = VPPCOM_EBADFD;
3491                       goto select_done;
3492                     }
3493
3494                   rv = vppcom_session_write_ready (session, session_index);
3495                   clib_spinlock_unlock (&vcm->sessions_lockp);
3496                   if (write_map && (rv > 0))
3497                     {
3498                       clib_bitmap_set_no_check (write_map, session_index, 1);
3499                       bits_set++;
3500                     }
3501                 }));
3502             }
3503
3504           if (except_map)
3505             {
3506               clib_bitmap_foreach (session_index, vcm->ex_bitmap,
3507                 ({
3508                   clib_spinlock_lock (&vcm->sessions_lockp);
3509                   rv = vppcom_session_at_index (session_index, &session);
3510                   if (rv < 0)
3511                     {
3512                       clib_spinlock_unlock (&vcm->sessions_lockp);
3513                       if (VPPCOM_DEBUG > 1)
3514                         clib_warning ("VCL<%d>: session %d specified in "
3515                                       "except_map is closed.", getpid (),
3516                                       session_index);
3517                       bits_set = VPPCOM_EBADFD;
3518                       goto select_done;
3519                     }
3520
3521                   rv = vppcom_session_read_ready (session, session_index);
3522                   clib_spinlock_unlock (&vcm->sessions_lockp);
3523                   if (rv < 0)
3524                     {
3525                       clib_bitmap_set_no_check (except_map, session_index, 1);
3526                       bits_set++;
3527                     }
3528                 }));
3529             }
3530         }
3531       /* *INDENT-ON* */
3532     }
3533   while ((time_to_wait == -1) || (clib_time_now (&vcm->clib_time) < timeout));
3534
3535 select_done:
3536   return (bits_set);
3537 }
3538
3539 static inline void
3540 vep_verify_epoll_chain (u32 vep_idx)
3541 {
3542   session_t *session;
3543   vppcom_epoll_t *vep;
3544   int rv;
3545   u32 sid = vep_idx;
3546
3547   if (VPPCOM_DEBUG <= 1)
3548     return;
3549
3550   /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
3551   rv = vppcom_session_at_index (vep_idx, &session);
3552   if (PREDICT_FALSE (rv))
3553     {
3554       clib_warning ("VCL<%d>: ERROR: Invalid vep_idx (%u)!",
3555                     getpid (), vep_idx);
3556       goto done;
3557     }
3558   if (PREDICT_FALSE (!session->is_vep))
3559     {
3560       clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
3561                     getpid (), vep_idx);
3562       goto done;
3563     }
3564   vep = &session->vep;
3565   clib_warning ("VCL<%d>: vep_idx (%u): Dumping epoll chain\n"
3566                 "{\n"
3567                 "   is_vep         = %u\n"
3568                 "   is_vep_session = %u\n"
3569                 "   next_sid       = 0x%x (%u)\n"
3570                 "   wait_cont_idx  = 0x%x (%u)\n"
3571                 "}\n", getpid (), vep_idx,
3572                 session->is_vep, session->is_vep_session,
3573                 vep->next_sid, vep->next_sid,
3574                 session->wait_cont_idx, session->wait_cont_idx);
3575
3576   for (sid = vep->next_sid; sid != ~0; sid = vep->next_sid)
3577     {
3578       rv = vppcom_session_at_index (sid, &session);
3579       if (PREDICT_FALSE (rv))
3580         {
3581           clib_warning ("VCL<%d>: ERROR: Invalid sid (%u)!", getpid (), sid);
3582           goto done;
3583         }
3584       if (PREDICT_FALSE (session->is_vep))
3585         clib_warning ("VCL<%d>: ERROR: sid (%u) is a vep!",
3586                       getpid (), vep_idx);
3587       else if (PREDICT_FALSE (!session->is_vep_session))
3588         {
3589           clib_warning ("VCL<%d>: ERROR: session (%u) "
3590                         "is not a vep session!", getpid (), sid);
3591           goto done;
3592         }
3593       vep = &session->vep;
3594       if (PREDICT_FALSE (vep->vep_idx != vep_idx))
3595         clib_warning ("VCL<%d>: ERROR: session (%u) vep_idx (%u) != "
3596                       "vep_idx (%u)!", getpid (),
3597                       sid, session->vep.vep_idx, vep_idx);
3598       if (session->is_vep_session)
3599         {
3600           clib_warning ("vep_idx[%u]: sid 0x%x (%u)\n"
3601                         "{\n"
3602                         "   next_sid       = 0x%x (%u)\n"
3603                         "   prev_sid       = 0x%x (%u)\n"
3604                         "   vep_idx        = 0x%x (%u)\n"
3605                         "   ev.events      = 0x%x\n"
3606                         "   ev.data.u64    = 0x%llx\n"
3607                         "   et_mask        = 0x%x\n"
3608                         "}\n",
3609                         vep_idx, sid, sid,
3610                         vep->next_sid, vep->next_sid,
3611                         vep->prev_sid, vep->prev_sid,
3612                         vep->vep_idx, vep->vep_idx,
3613                         vep->ev.events, vep->ev.data.u64, vep->et_mask);
3614         }
3615     }
3616
3617 done:
3618   clib_warning ("VCL<%d>: vep_idx (%u): Dump complete!\n",
3619                 getpid (), vep_idx);
3620 }
3621
3622 int
3623 vppcom_epoll_create (void)
3624 {
3625   session_t *vep_session;
3626   u32 vep_idx;
3627   elog_track_t vep_elog_track;
3628
3629   clib_spinlock_lock (&vcm->sessions_lockp);
3630   pool_get (vcm->sessions, vep_session);
3631   memset (vep_session, 0, sizeof (*vep_session));
3632   vep_idx = vep_session - vcm->sessions;
3633
3634   vep_session->is_vep = 1;
3635   vep_session->vep.vep_idx = ~0;
3636   vep_session->vep.next_sid = ~0;
3637   vep_session->vep.prev_sid = ~0;
3638   vep_session->wait_cont_idx = ~0;
3639   vep_session->vpp_handle = ~0;
3640
3641   if (VPPCOM_DEBUG > 0)
3642     {
3643       vep_session->elog_track.name =
3644         (char *) format (0, "C:%d:VEP:%d%c", vcm->my_client_index,
3645                          vep_idx, 0);
3646       elog_track_register (&vcm->elog_main, &vep_session->elog_track);
3647       vep_elog_track = vep_session->elog_track;
3648     }
3649
3650   clib_spinlock_unlock (&vcm->sessions_lockp);
3651
3652   if (VPPCOM_DEBUG > 0)
3653     clib_warning ("VCL<%d>: Created vep_idx %u / sid %u!",
3654                   getpid (), vep_idx, vep_idx);
3655
3656   if (VPPCOM_DEBUG > 0)
3657     {
3658
3659       /* *INDENT-OFF* */
3660       ELOG_TYPE_DECLARE (e) =
3661       {
3662         .format = "created epoll session:%d",
3663         .format_args = "i4",
3664       };
3665
3666       struct
3667       {
3668         u32 data;
3669       } *ed;
3670
3671       ed = ELOG_TRACK_DATA (&vcm->elog_main, e, vep_elog_track);
3672       ed->data = vep_idx;
3673       /* *INDENT-ON* */
3674     }
3675
3676   return (vep_idx);
3677 }
3678
3679 int
3680 vppcom_epoll_ctl (uint32_t vep_idx, int op, uint32_t session_index,
3681                   struct epoll_event *event)
3682 {
3683   session_t *vep_session;
3684   session_t *session;
3685   int rv;
3686
3687   if (vep_idx == session_index)
3688     {
3689       clib_warning ("VCL<%d>: ERROR: vep_idx == session_index (%u)!",
3690                     getpid (), vep_idx);
3691       return VPPCOM_EINVAL;
3692     }
3693
3694   clib_spinlock_lock (&vcm->sessions_lockp);
3695   rv = vppcom_session_at_index (vep_idx, &vep_session);
3696   if (PREDICT_FALSE (rv))
3697     {
3698       clib_warning ("VCL<%d>: ERROR: Invalid vep_idx (%u)!", vep_idx);
3699       goto done;
3700     }
3701   if (PREDICT_FALSE (!vep_session->is_vep))
3702     {
3703       clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
3704                     getpid (), vep_idx);
3705       rv = VPPCOM_EINVAL;
3706       goto done;
3707     }
3708
3709   ASSERT (vep_session->vep.vep_idx == ~0);
3710   ASSERT (vep_session->vep.prev_sid == ~0);
3711
3712   rv = vppcom_session_at_index (session_index, &session);
3713   if (PREDICT_FALSE (rv))
3714     {
3715       if (VPPCOM_DEBUG > 0)
3716         clib_warning ("VCL<%d>: ERROR: Invalid session_index (%u)!",
3717                       getpid (), session_index);
3718       goto done;
3719     }
3720   if (PREDICT_FALSE (session->is_vep))
3721     {
3722       clib_warning ("ERROR: session_index (%u) is a vep!", vep_idx);
3723       rv = VPPCOM_EINVAL;
3724       goto done;
3725     }
3726
3727   switch (op)
3728     {
3729     case EPOLL_CTL_ADD:
3730       if (PREDICT_FALSE (!event))
3731         {
3732           clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_ADD: NULL pointer to "
3733                         "epoll_event structure!", getpid ());
3734           rv = VPPCOM_EINVAL;
3735           goto done;
3736         }
3737       if (vep_session->vep.next_sid != ~0)
3738         {
3739           session_t *next_session;
3740           rv = vppcom_session_at_index (vep_session->vep.next_sid,
3741                                         &next_session);
3742           if (PREDICT_FALSE (rv))
3743             {
3744               clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_ADD: Invalid "
3745                             "vep.next_sid (%u) on vep_idx (%u)!",
3746                             getpid (), vep_session->vep.next_sid, vep_idx);
3747               goto done;
3748             }
3749           ASSERT (next_session->vep.prev_sid == vep_idx);
3750           next_session->vep.prev_sid = session_index;
3751         }
3752       session->vep.next_sid = vep_session->vep.next_sid;
3753       session->vep.prev_sid = vep_idx;
3754       session->vep.vep_idx = vep_idx;
3755       session->vep.et_mask = VEP_DEFAULT_ET_MASK;
3756       session->vep.ev = *event;
3757       session->is_vep = 0;
3758       session->is_vep_session = 1;
3759       vep_session->vep.next_sid = session_index;
3760       if (VPPCOM_DEBUG > 1)
3761         clib_warning ("VCL<%d>: EPOLL_CTL_ADD: vep_idx %u, "
3762                       "sid %u, events 0x%x, data 0x%llx!",
3763                       getpid (), vep_idx, session_index,
3764                       event->events, event->data.u64);
3765       if (VPPCOM_DEBUG > 0)
3766         {
3767           /* *INDENT-OFF* */
3768           ELOG_TYPE_DECLARE (e) =
3769             {
3770               .format = "epoll_ctladd: events:%x data:%x",
3771               .format_args = "i4i4i8",
3772             };
3773           struct
3774           {
3775             u32 events;
3776             u64 event_data;
3777           } *ed;
3778
3779           ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
3780
3781           ed->events = event->events;
3782           ed->event_data = event->data.u64;
3783           /* *INDENT-ON* */
3784         }
3785       break;
3786
3787     case EPOLL_CTL_MOD:
3788       if (PREDICT_FALSE (!event))
3789         {
3790           clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_MOD: NULL pointer to "
3791                         "epoll_event structure!", getpid ());
3792           rv = VPPCOM_EINVAL;
3793           goto done;
3794         }
3795       else if (PREDICT_FALSE (!session->is_vep_session))
3796         {
3797           clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_MOD: "
3798                         "not a vep session!", getpid (), session_index);
3799           rv = VPPCOM_EINVAL;
3800           goto done;
3801         }
3802       else if (PREDICT_FALSE (session->vep.vep_idx != vep_idx))
3803         {
3804           clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_MOD: "
3805                         "vep_idx (%u) != vep_idx (%u)!",
3806                         getpid (), session_index,
3807                         session->vep.vep_idx, vep_idx);
3808           rv = VPPCOM_EINVAL;
3809           goto done;
3810         }
3811       session->vep.et_mask = VEP_DEFAULT_ET_MASK;
3812       session->vep.ev = *event;
3813       if (VPPCOM_DEBUG > 1)
3814         clib_warning
3815           ("VCL<%d>: EPOLL_CTL_MOD: vep_idx %u, sid %u, events 0x%x,"
3816            " data 0x%llx!", getpid (), vep_idx, session_index, event->events,
3817            event->data.u64);
3818       break;
3819
3820     case EPOLL_CTL_DEL:
3821       if (PREDICT_FALSE (!session->is_vep_session))
3822         {
3823           clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_DEL: "
3824                         "not a vep session!", getpid (), session_index);
3825           rv = VPPCOM_EINVAL;
3826           goto done;
3827         }
3828       else if (PREDICT_FALSE (session->vep.vep_idx != vep_idx))
3829         {
3830           clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_DEL: "
3831                         "vep_idx (%u) != vep_idx (%u)!",
3832                         getpid (), session_index,
3833                         session->vep.vep_idx, vep_idx);
3834           rv = VPPCOM_EINVAL;
3835           goto done;
3836         }
3837
3838       vep_session->wait_cont_idx =
3839         (vep_session->wait_cont_idx == session_index) ?
3840         session->vep.next_sid : vep_session->wait_cont_idx;
3841
3842       if (session->vep.prev_sid == vep_idx)
3843         vep_session->vep.next_sid = session->vep.next_sid;
3844       else
3845         {
3846           session_t *prev_session;
3847           rv = vppcom_session_at_index (session->vep.prev_sid, &prev_session);
3848           if (PREDICT_FALSE (rv))
3849             {
3850               clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_DEL: Invalid "
3851                             "vep.prev_sid (%u) on sid (%u)!",
3852                             getpid (), session->vep.prev_sid, session_index);
3853               goto done;
3854             }
3855           ASSERT (prev_session->vep.next_sid == session_index);
3856           prev_session->vep.next_sid = session->vep.next_sid;
3857         }
3858       if (session->vep.next_sid != ~0)
3859         {
3860           session_t *next_session;
3861           rv = vppcom_session_at_index (session->vep.next_sid, &next_session);
3862           if (PREDICT_FALSE (rv))
3863             {
3864               clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_DEL: Invalid "
3865                             "vep.next_sid (%u) on sid (%u)!",
3866                             getpid (), session->vep.next_sid, session_index);
3867               goto done;
3868             }
3869           ASSERT (next_session->vep.prev_sid == session_index);
3870           next_session->vep.prev_sid = session->vep.prev_sid;
3871         }
3872
3873       memset (&session->vep, 0, sizeof (session->vep));
3874       session->vep.next_sid = ~0;
3875       session->vep.prev_sid = ~0;
3876       session->vep.vep_idx = ~0;
3877       session->is_vep_session = 0;
3878       if (VPPCOM_DEBUG > 1)
3879         clib_warning ("VCL<%d>: EPOLL_CTL_DEL: vep_idx %u, sid %u!",
3880                       getpid (), vep_idx, session_index);
3881       if (VPPCOM_DEBUG > 0)
3882         {
3883           /* *INDENT-OFF* */
3884           ELOG_TYPE_DECLARE (e) =
3885             {
3886               .format = "epoll_ctldel: vep:%d",
3887               .format_args = "i4",
3888             };
3889           struct
3890           {
3891             u32 data;
3892           } *ed;
3893
3894           ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
3895
3896           ed->data = vep_idx;
3897           /* *INDENT-ON* */
3898         }
3899       break;
3900
3901     default:
3902       clib_warning ("VCL<%d>: ERROR: Invalid operation (%d)!", getpid (), op);
3903       rv = VPPCOM_EINVAL;
3904     }
3905
3906   vep_verify_epoll_chain (vep_idx);
3907
3908 done:
3909   clib_spinlock_unlock (&vcm->sessions_lockp);
3910   return rv;
3911 }
3912
3913 int
3914 vppcom_epoll_wait (uint32_t vep_idx, struct epoll_event *events,
3915                    int maxevents, double wait_for_time)
3916 {
3917   session_t *vep_session;
3918   elog_track_t vep_elog_track;
3919   int rv;
3920   f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
3921   u32 keep_trying = 1;
3922   int num_ev = 0;
3923   u32 vep_next_sid, wait_cont_idx;
3924   u8 is_vep;
3925
3926   if (PREDICT_FALSE (maxevents <= 0))
3927     {
3928       clib_warning ("VCL<%d>: ERROR: Invalid maxevents (%d)!",
3929                     getpid (), maxevents);
3930       return VPPCOM_EINVAL;
3931     }
3932   memset (events, 0, sizeof (*events) * maxevents);
3933
3934   VCL_LOCK_AND_GET_SESSION (vep_idx, &vep_session);
3935   vep_next_sid = vep_session->vep.next_sid;
3936   is_vep = vep_session->is_vep;
3937   wait_cont_idx = vep_session->wait_cont_idx;
3938   vep_elog_track = vep_session->elog_track;
3939   clib_spinlock_unlock (&vcm->sessions_lockp);
3940
3941   if (PREDICT_FALSE (!is_vep))
3942     {
3943       clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
3944                     getpid (), vep_idx);
3945       rv = VPPCOM_EINVAL;
3946       goto done;
3947     }
3948   if (PREDICT_FALSE (vep_next_sid == ~0))
3949     {
3950       if (VPPCOM_DEBUG > 0)
3951         clib_warning ("VCL<%d>: WARNING: vep_idx (%u) is empty!",
3952                       getpid (), vep_idx);
3953       if (VPPCOM_DEBUG > 0)
3954         {
3955           /* *INDENT-OFF* */
3956           ELOG_TYPE_DECLARE (e) =
3957             {
3958               .format = "WRN: vep_idx:%d empty",
3959               .format_args = "i4",
3960             };
3961           struct
3962           {
3963             u32 data;
3964           } *ed;
3965
3966           ed = ELOG_TRACK_DATA (&vcm->elog_main, e, vep_elog_track);
3967
3968           ed->data = vep_idx;
3969           /* *INDENT-ON* */
3970         }
3971       goto done;
3972     }
3973
3974   do
3975     {
3976       u32 sid;
3977       u32 next_sid = ~0;
3978       session_t *session;
3979       elog_track_t session_elog_track;
3980
3981       for (sid = (wait_cont_idx == ~0) ? vep_next_sid : wait_cont_idx;
3982            sid != ~0; sid = next_sid)
3983         {
3984           u32 session_events, et_mask, clear_et_mask, session_vep_idx;
3985           u8 add_event, is_vep_session;
3986           int ready;
3987           u64 session_ev_data;
3988
3989           VCL_LOCK_AND_GET_SESSION (sid, &session);
3990           next_sid = session->vep.next_sid;
3991           session_events = session->vep.ev.events;
3992           et_mask = session->vep.et_mask;
3993           is_vep = session->is_vep;
3994           is_vep_session = session->is_vep_session;
3995           session_vep_idx = session->vep.vep_idx;
3996           session_ev_data = session->vep.ev.data.u64;
3997
3998           if (VPPCOM_DEBUG > 0)
3999             {
4000               session_elog_track = session->elog_track;
4001             }
4002
4003           clib_spinlock_unlock (&vcm->sessions_lockp);
4004
4005           if (PREDICT_FALSE (is_vep))
4006             {
4007               if (VPPCOM_DEBUG > 0)
4008                 clib_warning ("VCL<%d>: ERROR: sid (%u) is a vep!",
4009                               getpid (), vep_idx);
4010               if (VPPCOM_DEBUG > 0)
4011                 {
4012                   /* *INDENT-OFF* */
4013                   ELOG_TYPE_DECLARE (e) =
4014                     {
4015                       .format = "ERR:vep_idx:%d is vep",
4016                       .format_args = "i4",
4017                     };
4018                   struct
4019                   {
4020                     u32 data;
4021                   } *ed;
4022
4023                   ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session_elog_track);
4024
4025                   ed->data = vep_idx;
4026                   /* *INDENT-ON* */
4027                 }
4028
4029               rv = VPPCOM_EINVAL;
4030               goto done;
4031             }
4032           if (PREDICT_FALSE (!is_vep_session))
4033             {
4034               if (VPPCOM_DEBUG > 0)
4035                 clib_warning ("VCL<%d>: ERROR: session (%u) is not "
4036                               "a vep session!", getpid (), sid);
4037               if (VPPCOM_DEBUG > 0)
4038                 {
4039                   /* *INDENT-OFF* */
4040                   ELOG_TYPE_DECLARE (e) =
4041                     {
4042                       .format = "ERR:SID:%d not vep",
4043                       .format_args = "i4",
4044                     };
4045                   struct
4046                   {
4047                     u32 data;
4048                   } *ed;
4049
4050                   ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session_elog_track);
4051
4052                   ed->data = sid;
4053                   /* *INDENT-ON* */
4054                 }
4055
4056               rv = VPPCOM_EINVAL;
4057               goto done;
4058             }
4059           if (PREDICT_FALSE (session_vep_idx != vep_idx))
4060             {
4061               clib_warning ("VCL<%d>: ERROR: session (%u) "
4062                             "vep_idx (%u) != vep_idx (%u)!",
4063                             getpid (), sid, session_vep_idx, vep_idx);
4064               rv = VPPCOM_EINVAL;
4065               goto done;
4066             }
4067
4068           add_event = clear_et_mask = 0;
4069
4070           if (EPOLLIN & session_events)
4071             {
4072               VCL_LOCK_AND_GET_SESSION (sid, &session);
4073               ready = vppcom_session_read_ready (session, sid);
4074               clib_spinlock_unlock (&vcm->sessions_lockp);
4075               if ((ready > 0) && (EPOLLIN & et_mask))
4076                 {
4077                   add_event = 1;
4078                   events[num_ev].events |= EPOLLIN;
4079                   if (((EPOLLET | EPOLLIN) & session_events) ==
4080                       (EPOLLET | EPOLLIN))
4081                     clear_et_mask |= EPOLLIN;
4082                 }
4083               else if (ready < 0)
4084                 {
4085                   add_event = 1;
4086                   switch (ready)
4087                     {
4088                     case VPPCOM_ECONNRESET:
4089                       events[num_ev].events |= EPOLLHUP | EPOLLRDHUP;
4090                       break;
4091
4092                     default:
4093                       events[num_ev].events |= EPOLLERR;
4094                       break;
4095                     }
4096                 }
4097             }
4098
4099           if (EPOLLOUT & session_events)
4100             {
4101               VCL_LOCK_AND_GET_SESSION (sid, &session);
4102               ready = vppcom_session_write_ready (session, sid);
4103               clib_spinlock_unlock (&vcm->sessions_lockp);
4104               if ((ready > 0) && (EPOLLOUT & et_mask))
4105                 {
4106                   add_event = 1;
4107                   events[num_ev].events |= EPOLLOUT;
4108                   if (((EPOLLET | EPOLLOUT) & session_events) ==
4109                       (EPOLLET | EPOLLOUT))
4110                     clear_et_mask |= EPOLLOUT;
4111                 }
4112               else if (ready < 0)
4113                 {
4114                   add_event = 1;
4115                   switch (ready)
4116                     {
4117                     case VPPCOM_ECONNRESET:
4118                       events[num_ev].events |= EPOLLHUP;
4119                       break;
4120
4121                     default:
4122                       events[num_ev].events |= EPOLLERR;
4123                       break;
4124                     }
4125                 }
4126             }
4127
4128           if (add_event)
4129             {
4130               events[num_ev].data.u64 = session_ev_data;
4131               if (EPOLLONESHOT & session_events)
4132                 {
4133                   VCL_LOCK_AND_GET_SESSION (sid, &session);
4134                   session->vep.ev.events = 0;
4135                   clib_spinlock_unlock (&vcm->sessions_lockp);
4136                 }
4137               num_ev++;
4138               if (num_ev == maxevents)
4139                 {
4140                   VCL_LOCK_AND_GET_SESSION (vep_idx, &vep_session);
4141                   vep_session->wait_cont_idx = next_sid;
4142                   clib_spinlock_unlock (&vcm->sessions_lockp);
4143                   goto done;
4144                 }
4145             }
4146           if (wait_cont_idx != ~0)
4147             {
4148               if (next_sid == ~0)
4149                 next_sid = vep_next_sid;
4150               else if (next_sid == wait_cont_idx)
4151                 next_sid = ~0;
4152             }
4153         }
4154       if (wait_for_time != -1)
4155         keep_trying = (clib_time_now (&vcm->clib_time) <= timeout) ? 1 : 0;
4156     }
4157   while ((num_ev == 0) && keep_trying);
4158
4159   if (wait_cont_idx != ~0)
4160     {
4161       VCL_LOCK_AND_GET_SESSION (vep_idx, &vep_session);
4162       vep_session->wait_cont_idx = ~0;
4163       clib_spinlock_unlock (&vcm->sessions_lockp);
4164     }
4165 done:
4166   return (rv != VPPCOM_OK) ? rv : num_ev;
4167 }
4168
4169 int
4170 vppcom_session_attr (uint32_t session_index, uint32_t op,
4171                      void *buffer, uint32_t * buflen)
4172 {
4173   session_t *session;
4174   int rv = VPPCOM_OK;
4175   u32 *flags = buffer;
4176   vppcom_endpt_t *ep = buffer;
4177
4178   VCL_LOCK_AND_GET_SESSION (session_index, &session);
4179
4180   ASSERT (session);
4181
4182   switch (op)
4183     {
4184     case VPPCOM_ATTR_GET_NREAD:
4185       rv = vppcom_session_read_ready (session, session_index);
4186       if (VPPCOM_DEBUG > 2)
4187         clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_NREAD: sid %u, nread = %d",
4188                       getpid (), rv);
4189       if (VPPCOM_DEBUG > 0)
4190         {
4191           /* *INDENT-OFF* */
4192           ELOG_TYPE_DECLARE (e) =
4193             {
4194               .format = "VPPCOM_ATTR_GET_NREAD: nread=%d",
4195               .format_args = "i4",
4196             };
4197           struct
4198           {
4199             u32 data;
4200           } *ed;
4201
4202           ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4203
4204           ed->data = rv;
4205           /* *INDENT-ON* */
4206         }
4207
4208       break;
4209
4210     case VPPCOM_ATTR_GET_NWRITE:
4211       rv = vppcom_session_write_ready (session, session_index);
4212       if (VPPCOM_DEBUG > 2)
4213         clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_NWRITE: sid %u, nwrite = %d",
4214                       getpid (), session_index, rv);
4215       if (VPPCOM_DEBUG > 0)
4216         {
4217           /* *INDENT-OFF* */
4218           ELOG_TYPE_DECLARE (e) =
4219             {
4220               .format = "VPPCOM_ATTR_GET_NWRITE: nwrite=%d",
4221               .format_args = "i4",
4222             };
4223           struct
4224           {
4225             u32 data;
4226           } *ed;
4227
4228           ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4229
4230           ed->data = rv;
4231           /* *INDENT-ON* */
4232         }
4233       break;
4234
4235     case VPPCOM_ATTR_GET_FLAGS:
4236       if (PREDICT_TRUE (buffer && buflen && (*buflen >= sizeof (*flags))))
4237         {
4238           *flags = O_RDWR | (VCL_SESS_ATTR_TEST (session->attr,
4239                                                  VCL_SESS_ATTR_NONBLOCK));
4240           *buflen = sizeof (*flags);
4241           if (VPPCOM_DEBUG > 2)
4242             clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_FLAGS: sid %u, "
4243                           "flags = 0x%08x, is_nonblocking = %u", getpid (),
4244                           session_index, *flags,
4245                           VCL_SESS_ATTR_TEST (session->attr,
4246                                               VCL_SESS_ATTR_NONBLOCK));
4247           if (VPPCOM_DEBUG > 0)
4248             {
4249                 /* *INDENT-OFF* */
4250               ELOG_TYPE_DECLARE (e) =
4251                 {
4252                   .format = "VPPCOM_ATTR_GET_FLAGS: flags=%x is_nonblk=%d",
4253                   .format_args = "i4i4",
4254                 };
4255               struct
4256               {
4257                 u32 flags;
4258                 u32 is_nonblk;
4259               } *ed;
4260
4261               ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4262
4263               ed->flags = *flags;
4264               ed->is_nonblk = VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK);
4265               /* *INDENT-ON* */
4266             }
4267
4268         }
4269       else
4270         rv = VPPCOM_EINVAL;
4271       break;
4272
4273     case VPPCOM_ATTR_SET_FLAGS:
4274       if (PREDICT_TRUE (buffer && buflen && (*buflen == sizeof (*flags))))
4275         {
4276           if (*flags & O_NONBLOCK)
4277             VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK);
4278           else
4279             VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_NONBLOCK);
4280
4281           if (VPPCOM_DEBUG > 2)
4282             clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_FLAGS: sid %u, "
4283                           "flags = 0x%08x, is_nonblocking = %u",
4284                           getpid (), session_index, *flags,
4285                           VCL_SESS_ATTR_TEST (session->attr,
4286                                               VCL_SESS_ATTR_NONBLOCK));
4287           if (VPPCOM_DEBUG > 0)
4288             {
4289                 /* *INDENT-OFF* */
4290               ELOG_TYPE_DECLARE (e) =
4291                 {
4292                   .format = "VPPCOM_ATTR_SET_FLAGS: flags=%x is_nonblk=%d",
4293                   .format_args = "i4i4",
4294                 };
4295               struct
4296               {
4297                 u32 flags;
4298                 u32 is_nonblk;
4299               } *ed;
4300
4301               ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4302
4303               ed->flags = *flags;
4304               ed->is_nonblk = VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK);
4305               /* *INDENT-ON* */
4306             }
4307         }
4308       else
4309         rv = VPPCOM_EINVAL;
4310       break;
4311
4312     case VPPCOM_ATTR_GET_PEER_ADDR:
4313       if (PREDICT_TRUE (buffer && buflen &&
4314                         (*buflen >= sizeof (*ep)) && ep->ip))
4315         {
4316           ep->is_ip4 = session->peer_addr.is_ip4;
4317           ep->port = session->peer_port;
4318           if (session->peer_addr.is_ip4)
4319             clib_memcpy (ep->ip, &session->peer_addr.ip46.ip4,
4320                          sizeof (ip4_address_t));
4321           else
4322             clib_memcpy (ep->ip, &session->peer_addr.ip46.ip6,
4323                          sizeof (ip6_address_t));
4324           *buflen = sizeof (*ep);
4325           if (VPPCOM_DEBUG > 1)
4326             clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_PEER_ADDR: sid %u, "
4327                           "is_ip4 = %u, addr = %U, port %u", getpid (),
4328                           session_index, ep->is_ip4, format_ip46_address,
4329                           &session->peer_addr.ip46, ep->is_ip4,
4330                           clib_net_to_host_u16 (ep->port));
4331           if (VPPCOM_DEBUG > 0)
4332             {
4333               if (ep->is_ip4)
4334                 {
4335                     /* *INDENT-OFF* */
4336                   ELOG_TYPE_DECLARE (e) =
4337                     {
4338                       .format = "VPPCOM_ATTR_GET_PEER_ADDR: addr:%d.%d.%d.%d:%d",
4339                       .format_args = "i1i1i1i1i2",
4340                     };
4341                   CLIB_PACKED (struct {
4342                     u8 addr[4]; //4
4343                     u16 port;   //2
4344                   }) * ed;
4345
4346                   ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4347
4348                   ed->addr[0] = session->peer_addr.ip46.ip4.as_u8[0];
4349                   ed->addr[1] = session->peer_addr.ip46.ip4.as_u8[1];
4350                   ed->addr[2] = session->peer_addr.ip46.ip4.as_u8[2];
4351                   ed->addr[3] = session->peer_addr.ip46.ip4.as_u8[3];
4352                   ed->port = clib_net_to_host_u16 (session->peer_port);
4353                   /* *INDENT-ON* */
4354                 }
4355               else
4356                 {
4357                     /* *INDENT-OFF* */
4358                   ELOG_TYPE_DECLARE (e) =
4359                     {
4360                       .format = "VPPCOM_ATTR_GET_PEER_ADDR: addr:IP6:%d",
4361                       .format_args = "i2",
4362                     };
4363                   CLIB_PACKED (struct {
4364                     u16 port;   //2
4365                   }) * ed;
4366
4367                   ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4368
4369                   ed->port = clib_net_to_host_u16 (session->peer_port);
4370                   /* *INDENT-ON* */
4371                 }
4372             }
4373         }
4374       else
4375         rv = VPPCOM_EINVAL;
4376       break;
4377
4378     case VPPCOM_ATTR_GET_LCL_ADDR:
4379       if (PREDICT_TRUE (buffer && buflen &&
4380                         (*buflen >= sizeof (*ep)) && ep->ip))
4381         {
4382           ep->is_ip4 = session->lcl_addr.is_ip4;
4383           ep->port = session->lcl_port;
4384           if (session->lcl_addr.is_ip4)
4385             clib_memcpy (ep->ip, &session->lcl_addr.ip46.ip4,
4386                          sizeof (ip4_address_t));
4387           else
4388             clib_memcpy (ep->ip, &session->lcl_addr.ip46.ip6,
4389                          sizeof (ip6_address_t));
4390           *buflen = sizeof (*ep);
4391           if (VPPCOM_DEBUG > 1)
4392             clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_LCL_ADDR: sid %u, "
4393                           "is_ip4 = %u, addr = %U port %d", getpid (),
4394                           session_index, ep->is_ip4, format_ip46_address,
4395                           &session->lcl_addr.ip46, ep->is_ip4,
4396                           clib_net_to_host_u16 (ep->port));
4397           if (VPPCOM_DEBUG > 0)
4398             {
4399               if (ep->is_ip4)
4400                 {
4401                     /* *INDENT-OFF* */
4402                   ELOG_TYPE_DECLARE (e) =
4403                     {
4404                       .format = "VPPCOM_ATTR_GET_LCL_ADDR: addr:%d.%d.%d.%d:%d",
4405                       .format_args = "i1i1i1i1i2",
4406                     };
4407                   CLIB_PACKED (struct {
4408                     u8 addr[4]; //4
4409                     u16 port;   //2
4410                   }) * ed;
4411
4412                   ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4413
4414                   ed->addr[0] = session->lcl_addr.ip46.ip4.as_u8[0];
4415                   ed->addr[1] = session->lcl_addr.ip46.ip4.as_u8[1];
4416                   ed->addr[2] = session->lcl_addr.ip46.ip4.as_u8[2];
4417                   ed->addr[3] = session->lcl_addr.ip46.ip4.as_u8[3];
4418                   ed->port = clib_net_to_host_u16 (session->peer_port);
4419                   /* *INDENT-ON* */
4420                 }
4421               else
4422                 {
4423                     /* *INDENT-OFF* */
4424                   ELOG_TYPE_DECLARE (e) =
4425                     {
4426                       .format = "VPPCOM_ATTR_GET_LCL_ADDR: addr:IP6:%d",
4427                       .format_args = "i2",
4428                     };
4429                   CLIB_PACKED (struct {
4430                     u16 port;   //2
4431                   }) * ed;
4432
4433                   ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4434
4435                   ed->port = clib_net_to_host_u16 (session->peer_port);
4436                   /* *INDENT-ON* */
4437                 }
4438             }
4439         }
4440       else
4441         rv = VPPCOM_EINVAL;
4442       break;
4443
4444     case VPPCOM_ATTR_GET_LIBC_EPFD:
4445       rv = session->libc_epfd;
4446       if (VPPCOM_DEBUG > 2)
4447         clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd %d",
4448                       getpid (), rv);
4449       if (VPPCOM_DEBUG > 0)
4450         {
4451           /* *INDENT-OFF* */
4452           ELOG_TYPE_DECLARE (e) =
4453             {
4454               .format = "VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd=%d",
4455               .format_args = "i4",
4456             };
4457           CLIB_PACKED (struct {
4458             i32 data;
4459           }) * ed;
4460
4461           ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4462           ed->data = session->libc_epfd;
4463           /* *INDENT-ON* */
4464         }
4465
4466       break;
4467
4468     case VPPCOM_ATTR_SET_LIBC_EPFD:
4469       if (PREDICT_TRUE (buffer && buflen &&
4470                         (*buflen == sizeof (session->libc_epfd))))
4471         {
4472           session->libc_epfd = *(int *) buffer;
4473           *buflen = sizeof (session->libc_epfd);
4474
4475           if (VPPCOM_DEBUG > 2)
4476             clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd %d, "
4477                           "buflen %d", getpid (), session->libc_epfd,
4478                           *buflen);
4479           if (VPPCOM_DEBUG > 0)
4480             {
4481                 /* *INDENT-OFF* */
4482               ELOG_TYPE_DECLARE (e) =
4483                 {
4484                   .format = "VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd=%s%d buflen=%d",
4485                   .format_args = "t1i4i4",
4486                   .n_enum_strings = 2,
4487                   .enum_strings = {"", "-",},
4488                 };
4489               CLIB_PACKED (struct {
4490                 u8 sign;
4491                 u32 data[2];
4492               }) * ed;
4493
4494               ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4495
4496               ed->sign = (session->libc_epfd < 0);
4497               ed->data[0] = abs(session->libc_epfd);
4498               ed->data[1] = *buflen;
4499               /* *INDENT-ON* */
4500             }
4501         }
4502       else
4503         rv = VPPCOM_EINVAL;
4504       break;
4505
4506     case VPPCOM_ATTR_GET_PROTOCOL:
4507       if (buffer && buflen && (*buflen >= sizeof (int)))
4508         {
4509           *(int *) buffer = session->proto;
4510           *buflen = sizeof (int);
4511
4512           if (VPPCOM_DEBUG > 2)
4513             clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_PROTOCOL: %d (%s), "
4514                           "buflen %d", getpid (), *(int *) buffer,
4515                           *(int *) buffer ? "UDP" : "TCP", *buflen);
4516           if (VPPCOM_DEBUG > 0)
4517             {
4518                 /* *INDENT-OFF* */
4519               ELOG_TYPE_DECLARE (e) =
4520                 {
4521                   .format = "VPPCOM_ATTR_GET_PROTOCOL: %s buflen=%d",
4522                   .format_args = "t1i4",
4523                   .n_enum_strings = 2,
4524                   .enum_strings = {"TCP", "UDP",},
4525                 };
4526
4527               CLIB_PACKED (struct {
4528                 u8 proto;
4529                 u32 buflen;
4530               }) * ed;
4531
4532               ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4533               ed->proto = session->proto;
4534               ed->buflen = *(int *) buffer;
4535               /* *INDENT-ON* */
4536             }
4537         }
4538       else
4539         rv = VPPCOM_EINVAL;
4540       break;
4541
4542     case VPPCOM_ATTR_GET_LISTEN:
4543       if (buffer && buflen && (*buflen >= sizeof (int)))
4544         {
4545           *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
4546                                                 VCL_SESS_ATTR_LISTEN);
4547           *buflen = sizeof (int);
4548
4549           if (VPPCOM_DEBUG > 2)
4550             clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_LISTEN: %d, "
4551                           "buflen %d", getpid (), *(int *) buffer, *buflen);
4552           if (VPPCOM_DEBUG > 0)
4553             {
4554                 /* *INDENT-OFF* */
4555               ELOG_TYPE_DECLARE (e) =
4556                 {
4557                   .format = "VPPCOM_ATTR_GET_LISTEN: %d buflen=%d",
4558                   .format_args = "i4i4",
4559                 };
4560
4561               struct {
4562                 u32 data[2];
4563               } * ed;
4564
4565               ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4566               ed->data[0] = *(int *) buffer;
4567               ed->data[1] = *buflen;
4568               /* *INDENT-ON* */
4569             }
4570         }
4571       else
4572         rv = VPPCOM_EINVAL;
4573       break;
4574
4575     case VPPCOM_ATTR_GET_ERROR:
4576       if (buffer && buflen && (*buflen >= sizeof (int)))
4577         {
4578           *(int *) buffer = 0;
4579           *buflen = sizeof (int);
4580
4581           if (VPPCOM_DEBUG > 2)
4582             clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_ERROR: %d, "
4583                           "buflen %d, #VPP-TBD#", getpid (),
4584                           *(int *) buffer, *buflen);
4585           if (VPPCOM_DEBUG > 0)
4586             {
4587                 /* *INDENT-OFF* */
4588               ELOG_TYPE_DECLARE (e) =
4589                 {
4590                   .format = "VPPCOM_ATTR_GET_ERROR: %d buflen=%d",
4591                   .format_args = "i4i4",
4592                 };
4593
4594               struct {
4595                 u32 data[2];
4596               } * ed;
4597
4598               ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4599               ed->data[0] = *(int *) buffer;
4600               ed->data[1] = *buflen;
4601               /* *INDENT-ON* */
4602             }
4603         }
4604       else
4605         rv = VPPCOM_EINVAL;
4606       break;
4607
4608     case VPPCOM_ATTR_GET_TX_FIFO_LEN:
4609       if (buffer && buflen && (*buflen >= sizeof (u32)))
4610         {
4611
4612           /* VPP-TBD */
4613           *(size_t *) buffer = (session->sndbuf_size ? session->sndbuf_size :
4614                                 session->tx_fifo ? session->tx_fifo->nitems :
4615                                 vcm->cfg.tx_fifo_size);
4616           *buflen = sizeof (u32);
4617
4618           if (VPPCOM_DEBUG > 2)
4619             clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_TX_FIFO_LEN: %u (0x%x), "
4620                           "buflen %d, #VPP-TBD#", getpid (),
4621                           *(size_t *) buffer, *(size_t *) buffer, *buflen);
4622           if (VPPCOM_DEBUG > 0)
4623             {
4624                 /* *INDENT-OFF* */
4625               ELOG_TYPE_DECLARE (e) =
4626                 {
4627                   .format = "VPPCOM_ATTR_GET_TX_FIFO_LEN: 0x%x buflen=%d",
4628                   .format_args = "i4i4",
4629                 };
4630
4631               struct {
4632                 u32 data[2];
4633               } * ed;
4634
4635               ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4636               ed->data[0] = *(size_t *) buffer;
4637               ed->data[1] = *buflen;
4638               /* *INDENT-ON* */
4639             }
4640         }
4641       else
4642         rv = VPPCOM_EINVAL;
4643       break;
4644
4645     case VPPCOM_ATTR_SET_TX_FIFO_LEN:
4646       if (buffer && buflen && (*buflen == sizeof (u32)))
4647         {
4648           /* VPP-TBD */
4649           session->sndbuf_size = *(u32 *) buffer;
4650           if (VPPCOM_DEBUG > 2)
4651             clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_TX_FIFO_LEN: %u (0x%x), "
4652                           "buflen %d, #VPP-TBD#", getpid (),
4653                           session->sndbuf_size, session->sndbuf_size,
4654                           *buflen);
4655           if (VPPCOM_DEBUG > 0)
4656             {
4657                 /* *INDENT-OFF* */
4658               ELOG_TYPE_DECLARE (e) =
4659                 {
4660                   .format = "VPPCOM_ATTR_SET_TX_FIFO_LEN: 0x%x buflen=%d",
4661                   .format_args = "i4i4",
4662                 };
4663
4664               struct {
4665                 u32 data[2];
4666               } * ed;
4667
4668               ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4669               ed->data[0] = session->sndbuf_size;
4670               ed->data[1] = *buflen;
4671               /* *INDENT-ON* */
4672             }
4673         }
4674       else
4675         rv = VPPCOM_EINVAL;
4676       break;
4677
4678     case VPPCOM_ATTR_GET_RX_FIFO_LEN:
4679       if (buffer && buflen && (*buflen >= sizeof (u32)))
4680         {
4681
4682           /* VPP-TBD */
4683           *(size_t *) buffer = (session->rcvbuf_size ? session->rcvbuf_size :
4684                                 session->rx_fifo ? session->rx_fifo->nitems :
4685                                 vcm->cfg.rx_fifo_size);
4686           *buflen = sizeof (u32);
4687
4688           if (VPPCOM_DEBUG > 2)
4689             clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_RX_FIFO_LEN: %u (0x%x), "
4690                           "buflen %d, #VPP-TBD#", getpid (),
4691                           *(size_t *) buffer, *(size_t *) buffer, *buflen);
4692           if (VPPCOM_DEBUG > 0)
4693             {
4694                 /* *INDENT-OFF* */
4695               ELOG_TYPE_DECLARE (e) =
4696                 {
4697                   .format = "VPPCOM_ATTR_GET_RX_FIFO_LEN: 0x%x buflen=%d",
4698                   .format_args = "i4i4",
4699                 };
4700
4701               struct {
4702                 u32 data[2];
4703               } * ed;
4704
4705               ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4706               ed->data[0] = *(size_t *) buffer;
4707               ed->data[1] = *buflen;
4708               /* *INDENT-ON* */
4709             }
4710         }
4711       else
4712         rv = VPPCOM_EINVAL;
4713       break;
4714
4715     case VPPCOM_ATTR_SET_RX_FIFO_LEN:
4716       if (buffer && buflen && (*buflen == sizeof (u32)))
4717         {
4718           /* VPP-TBD */
4719           session->rcvbuf_size = *(u32 *) buffer;
4720           if (VPPCOM_DEBUG > 2)
4721             clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_RX_FIFO_LEN: %u (0x%x), "
4722                           "buflen %d, #VPP-TBD#", getpid (),
4723                           session->sndbuf_size, session->sndbuf_size,
4724                           *buflen);
4725           if (VPPCOM_DEBUG > 0)
4726             {
4727                 /* *INDENT-OFF* */
4728               ELOG_TYPE_DECLARE (e) =
4729                 {
4730                   .format = "VPPCOM_ATTR_SET_RX_FIFO_LEN: 0x%x buflen=%d",
4731                   .format_args = "i4i4",
4732                 };
4733
4734               struct {
4735                 u32 data[2];
4736               } * ed;
4737
4738               ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4739               ed->data[0] = session->sndbuf_size;
4740               ed->data[1] = *buflen;
4741               /* *INDENT-ON* */
4742             }
4743         }
4744       else
4745         rv = VPPCOM_EINVAL;
4746       break;
4747
4748     case VPPCOM_ATTR_GET_REUSEADDR:
4749       if (buffer && buflen && (*buflen >= sizeof (int)))
4750         {
4751           /* VPP-TBD */
4752           *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
4753                                                 VCL_SESS_ATTR_REUSEADDR);
4754           *buflen = sizeof (int);
4755
4756           if (VPPCOM_DEBUG > 2)
4757             clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_REUSEADDR: %d, "
4758                           "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
4759                           *buflen);
4760           if (VPPCOM_DEBUG > 0)
4761             {
4762                 /* *INDENT-OFF* */
4763               ELOG_TYPE_DECLARE (e) =
4764                 {
4765                   .format = "VPPCOM_ATTR_GET_REUSEADDR: %d buflen=%d",
4766                   .format_args = "i4i4",
4767                 };
4768
4769               struct {
4770                 u32 data[2];
4771               } * ed;
4772
4773               ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4774               ed->data[0] = *(int *) buffer;
4775               ed->data[1] = *buflen;
4776               /* *INDENT-ON* */
4777             }
4778         }
4779       else
4780         rv = VPPCOM_EINVAL;
4781       break;
4782
4783     case VPPCOM_ATTR_SET_REUSEADDR:
4784       if (buffer && buflen && (*buflen == sizeof (int)) &&
4785           !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
4786         {
4787           /* VPP-TBD */
4788           if (*(int *) buffer)
4789             VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEADDR);
4790           else
4791             VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEADDR);
4792
4793           if (VPPCOM_DEBUG > 2)
4794             clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_REUSEADDR: %d, "
4795                           "buflen %d, #VPP-TBD#", getpid (),
4796                           VCL_SESS_ATTR_TEST (session->attr,
4797                                               VCL_SESS_ATTR_REUSEADDR),
4798                           *buflen);
4799           if (VPPCOM_DEBUG > 0)
4800             {
4801                 /* *INDENT-OFF* */
4802               ELOG_TYPE_DECLARE (e) =
4803                 {
4804                   .format = "VPPCOM_ATTR_SET_REUSEADDR: %d buflen=%d",
4805                   .format_args = "i4i4",
4806                 };
4807
4808               struct {
4809                 u32 data[2];
4810               } * ed;
4811
4812               ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4813               ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
4814                                                 VCL_SESS_ATTR_REUSEADDR);
4815               ed->data[1] = *buflen;
4816               /* *INDENT-ON* */
4817             }
4818         }
4819       else
4820         rv = VPPCOM_EINVAL;
4821       break;
4822
4823     case VPPCOM_ATTR_GET_REUSEPORT:
4824       if (buffer && buflen && (*buflen >= sizeof (int)))
4825         {
4826           /* VPP-TBD */
4827           *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
4828                                                 VCL_SESS_ATTR_REUSEPORT);
4829           *buflen = sizeof (int);
4830
4831           if (VPPCOM_DEBUG > 2)
4832             clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_REUSEPORT: %d, "
4833                           "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
4834                           *buflen);
4835           if (VPPCOM_DEBUG > 0)
4836             {
4837                 /* *INDENT-OFF* */
4838               ELOG_TYPE_DECLARE (e) =
4839                 {
4840                   .format = "VPPCOM_ATTR_GET_REUSEPORT: %d buflen=%d",
4841                   .format_args = "i4i4",
4842                 };
4843
4844               struct {
4845                 u32 data[2];
4846               } * ed;
4847
4848               ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4849               ed->data[0] = *(int *) buffer;
4850               ed->data[1] = *buflen;
4851               /* *INDENT-ON* */
4852             }
4853         }
4854       else
4855         rv = VPPCOM_EINVAL;
4856       break;
4857
4858     case VPPCOM_ATTR_SET_REUSEPORT:
4859       if (buffer && buflen && (*buflen == sizeof (int)) &&
4860           !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
4861         {
4862           /* VPP-TBD */
4863           if (*(int *) buffer)
4864             VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEPORT);
4865           else
4866             VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEPORT);
4867
4868           if (VPPCOM_DEBUG > 2)
4869             clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_REUSEPORT: %d, "
4870                           "buflen %d, #VPP-TBD#", getpid (),
4871                           VCL_SESS_ATTR_TEST (session->attr,
4872                                               VCL_SESS_ATTR_REUSEPORT),
4873                           *buflen);
4874           if (VPPCOM_DEBUG > 0)
4875             {
4876                 /* *INDENT-OFF* */
4877               ELOG_TYPE_DECLARE (e) =
4878                 {
4879                   .format = "VPPCOM_ATTR_SET_REUSEPORT: %d buflen=%d",
4880                   .format_args = "i4i4",
4881                 };
4882
4883               struct {
4884                 u32 data[2];
4885               } * ed;
4886
4887               ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4888               ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
4889                                                 VCL_SESS_ATTR_REUSEPORT);
4890               ed->data[1] = *buflen;
4891               /* *INDENT-ON* */
4892             }
4893         }
4894       else
4895         rv = VPPCOM_EINVAL;
4896       break;
4897
4898     case VPPCOM_ATTR_GET_BROADCAST:
4899       if (buffer && buflen && (*buflen >= sizeof (int)))
4900         {
4901           /* VPP-TBD */
4902           *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
4903                                                 VCL_SESS_ATTR_BROADCAST);
4904           *buflen = sizeof (int);
4905
4906           if (VPPCOM_DEBUG > 2)
4907             clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_BROADCAST: %d, "
4908                           "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
4909                           *buflen);
4910           if (VPPCOM_DEBUG > 0)
4911             {
4912                 /* *INDENT-OFF* */
4913               ELOG_TYPE_DECLARE (e) =
4914                 {
4915                   .format = "VPPCOM_ATTR_GET_BROADCAST: %d buflen=%d",
4916                   .format_args = "i4i4",
4917                 };
4918
4919               struct {
4920                 u32 data[2];
4921               } * ed;
4922
4923               ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4924               ed->data[0] = *(int *) buffer;
4925               ed->data[1] = *buflen;
4926               /* *INDENT-ON* */
4927             }
4928         }
4929       else
4930         rv = VPPCOM_EINVAL;
4931       break;
4932
4933     case VPPCOM_ATTR_SET_BROADCAST:
4934       if (buffer && buflen && (*buflen == sizeof (int)))
4935         {
4936           /* VPP-TBD */
4937           if (*(int *) buffer)
4938             VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_BROADCAST);
4939           else
4940             VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_BROADCAST);
4941
4942           if (VPPCOM_DEBUG > 2)
4943             clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_BROADCAST: %d, "
4944                           "buflen %d, #VPP-TBD#", getpid (),
4945                           VCL_SESS_ATTR_TEST (session->attr,
4946                                               VCL_SESS_ATTR_BROADCAST),
4947                           *buflen);
4948           if (VPPCOM_DEBUG > 0)
4949             {
4950                 /* *INDENT-OFF* */
4951               ELOG_TYPE_DECLARE (e) =
4952                 {
4953                   .format = "VPPCOM_ATTR_SET_BROADCAST: %d buflen=%d",
4954                   .format_args = "i4i4",
4955                 };
4956
4957               struct {
4958                 u32 data[2];
4959               } * ed;
4960
4961               ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4962               ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
4963                                                 VCL_SESS_ATTR_BROADCAST);
4964               ed->data[1] = *buflen;
4965               /* *INDENT-ON* */
4966             }
4967         }
4968       else
4969         rv = VPPCOM_EINVAL;
4970       break;
4971
4972     case VPPCOM_ATTR_GET_V6ONLY:
4973       if (buffer && buflen && (*buflen >= sizeof (int)))
4974         {
4975           /* VPP-TBD */
4976           *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
4977                                                 VCL_SESS_ATTR_V6ONLY);
4978           *buflen = sizeof (int);
4979
4980           if (VPPCOM_DEBUG > 2)
4981             clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_V6ONLY: %d, "
4982                           "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
4983                           *buflen);
4984           if (VPPCOM_DEBUG > 0)
4985             {
4986                 /* *INDENT-OFF* */
4987               ELOG_TYPE_DECLARE (e) =
4988                 {
4989                   .format = "VPPCOM_ATTR_GET_V6ONLY: %d buflen=%d",
4990                   .format_args = "i4i4",
4991                 };
4992
4993               struct {
4994                 u32 data[2];
4995               } * ed;
4996
4997               ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4998               ed->data[0] = *(int *) buffer;
4999               ed->data[1] = *buflen;
5000               /* *INDENT-ON* */
5001             }
5002         }
5003       else
5004         rv = VPPCOM_EINVAL;
5005       break;
5006
5007     case VPPCOM_ATTR_SET_V6ONLY:
5008       if (buffer && buflen && (*buflen == sizeof (int)))
5009         {
5010           /* VPP-TBD */
5011           if (*(int *) buffer)
5012             VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_V6ONLY);
5013           else
5014             VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_V6ONLY);
5015
5016           if (VPPCOM_DEBUG > 2)
5017             clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_V6ONLY: %d, "
5018                           "buflen %d, #VPP-TBD#", getpid (),
5019                           VCL_SESS_ATTR_TEST (session->attr,
5020                                               VCL_SESS_ATTR_V6ONLY), *buflen);
5021           if (VPPCOM_DEBUG > 0)
5022             {
5023                 /* *INDENT-OFF* */
5024               ELOG_TYPE_DECLARE (e) =
5025                 {
5026                   .format = "VPPCOM_ATTR_SET_V6ONLY: %d buflen=%d",
5027                   .format_args = "i4i4",
5028                 };
5029
5030               struct {
5031                 u32 data[2];
5032               } * ed;
5033
5034               ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5035               ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
5036                                                 VCL_SESS_ATTR_V6ONLY);
5037               ed->data[1] = *buflen;
5038               /* *INDENT-ON* */
5039             }
5040         }
5041       else
5042         rv = VPPCOM_EINVAL;
5043       break;
5044
5045     case VPPCOM_ATTR_GET_KEEPALIVE:
5046       if (buffer && buflen && (*buflen >= sizeof (int)))
5047         {
5048           /* VPP-TBD */
5049           *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
5050                                                 VCL_SESS_ATTR_KEEPALIVE);
5051           *buflen = sizeof (int);
5052
5053           if (VPPCOM_DEBUG > 2)
5054             clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_KEEPALIVE: %d, "
5055                           "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
5056                           *buflen);
5057           if (VPPCOM_DEBUG > 0)
5058             {
5059                 /* *INDENT-OFF* */
5060               ELOG_TYPE_DECLARE (e) =
5061                 {
5062                   .format = "VPPCOM_ATTR_GET_KEEPALIVE: %d buflen=%d",
5063                   .format_args = "i4i4",
5064                 };
5065
5066               struct {
5067                 u32 data[2];
5068               } * ed;
5069
5070               ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5071               ed->data[0] = *(int *) buffer;
5072               ed->data[1] = *buflen;
5073               /* *INDENT-ON* */
5074             }
5075         }
5076       else
5077         rv = VPPCOM_EINVAL;
5078       break;
5079
5080     case VPPCOM_ATTR_SET_KEEPALIVE:
5081       if (buffer && buflen && (*buflen == sizeof (int)))
5082         {
5083           /* VPP-TBD */
5084           if (*(int *) buffer)
5085             VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_KEEPALIVE);
5086           else
5087             VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_KEEPALIVE);
5088
5089           if (VPPCOM_DEBUG > 2)
5090             clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_KEEPALIVE: %d, "
5091                           "buflen %d, #VPP-TBD#", getpid (),
5092                           VCL_SESS_ATTR_TEST (session->attr,
5093                                               VCL_SESS_ATTR_KEEPALIVE),
5094                           *buflen);
5095           if (VPPCOM_DEBUG > 0)
5096             {
5097                 /* *INDENT-OFF* */
5098               ELOG_TYPE_DECLARE (e) =
5099                 {
5100                   .format = "VPPCOM_ATTR_SET_KEEPALIVE: %d buflen=%d",
5101                   .format_args = "i4i4",
5102                 };
5103
5104               struct {
5105                 u32 data[2];
5106               } * ed;
5107
5108               ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5109               ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
5110                                                 VCL_SESS_ATTR_KEEPALIVE);
5111               ed->data[1] = *buflen;
5112               /* *INDENT-ON* */
5113             }
5114         }
5115       else
5116         rv = VPPCOM_EINVAL;
5117       break;
5118
5119     case VPPCOM_ATTR_GET_TCP_NODELAY:
5120       if (buffer && buflen && (*buflen >= sizeof (int)))
5121         {
5122           /* VPP-TBD */
5123           *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
5124                                                 VCL_SESS_ATTR_TCP_NODELAY);
5125           *buflen = sizeof (int);
5126
5127           if (VPPCOM_DEBUG > 2)
5128             clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_TCP_NODELAY: %d, "
5129                           "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
5130                           *buflen);
5131           if (VPPCOM_DEBUG > 0)
5132             {
5133                 /* *INDENT-OFF* */
5134               ELOG_TYPE_DECLARE (e) =
5135                 {
5136                   .format = "VPPCOM_ATTR_GET_TCP_NODELAY: %d buflen=%d",
5137                   .format_args = "i4i4",
5138                 };
5139
5140               struct {
5141                 u32 data[2];
5142               } * ed;
5143
5144               ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5145               ed->data[0] = *(int *) buffer;
5146               ed->data[1] = *buflen;
5147               /* *INDENT-ON* */
5148             }
5149         }
5150       else
5151         rv = VPPCOM_EINVAL;
5152       break;
5153
5154     case VPPCOM_ATTR_SET_TCP_NODELAY:
5155       if (buffer && buflen && (*buflen == sizeof (int)))
5156         {
5157           /* VPP-TBD */
5158           if (*(int *) buffer)
5159             VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
5160           else
5161             VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
5162
5163           if (VPPCOM_DEBUG > 2)
5164             clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_TCP_NODELAY: %d, "
5165                           "buflen %d, #VPP-TBD#", getpid (),
5166                           VCL_SESS_ATTR_TEST (session->attr,
5167                                               VCL_SESS_ATTR_TCP_NODELAY),
5168                           *buflen);
5169           if (VPPCOM_DEBUG > 0)
5170             {
5171                 /* *INDENT-OFF* */
5172               ELOG_TYPE_DECLARE (e) =
5173                 {
5174                   .format = "VPPCOM_ATTR_SET_TCP_NODELAY: %d buflen=%d",
5175                   .format_args = "i4i4",
5176                 };
5177
5178               struct {
5179                 u32 data[2];
5180               } * ed;
5181
5182               ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5183               ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
5184                                                 VCL_SESS_ATTR_TCP_NODELAY);
5185               ed->data[1] = *buflen;
5186               /* *INDENT-ON* */
5187             }
5188         }
5189       else
5190         rv = VPPCOM_EINVAL;
5191       break;
5192
5193     case VPPCOM_ATTR_GET_TCP_KEEPIDLE:
5194       if (buffer && buflen && (*buflen >= sizeof (int)))
5195         {
5196           /* VPP-TBD */
5197           *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
5198                                                 VCL_SESS_ATTR_TCP_KEEPIDLE);
5199           *buflen = sizeof (int);
5200
5201           if (VPPCOM_DEBUG > 2)
5202             clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d, "
5203                           "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
5204                           *buflen);
5205           if (VPPCOM_DEBUG > 0)
5206             {
5207                 /* *INDENT-OFF* */
5208               ELOG_TYPE_DECLARE (e) =
5209                 {
5210                   .format = "VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d buflen=%d",
5211                   .format_args = "i4i4",
5212                 };
5213
5214               struct {
5215                 u32 data[2];
5216               } * ed;
5217
5218               ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5219               ed->data[0] = *(int *) buffer;
5220               ed->data[1] = *buflen;
5221               /* *INDENT-ON* */
5222             }
5223         }
5224       else
5225         rv = VPPCOM_EINVAL;
5226       break;
5227
5228     case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
5229       if (buffer && buflen && (*buflen == sizeof (int)))
5230         {
5231           /* VPP-TBD */
5232           if (*(int *) buffer)
5233             VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
5234           else
5235             VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
5236
5237           if (VPPCOM_DEBUG > 2)
5238             clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d, "
5239                           "buflen %d, #VPP-TBD#", getpid (),
5240                           VCL_SESS_ATTR_TEST (session->attr,
5241                                               VCL_SESS_ATTR_TCP_KEEPIDLE),
5242                           *buflen);
5243           if (VPPCOM_DEBUG > 0)
5244             {
5245                 /* *INDENT-OFF* */
5246               ELOG_TYPE_DECLARE (e) =
5247                 {
5248                   .format = "VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d buflen=%d",
5249                   .format_args = "i4i4",
5250                 };
5251
5252               struct {
5253                 u32 data[2];
5254               } * ed;
5255
5256               ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5257               ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
5258                                                 VCL_SESS_ATTR_TCP_KEEPIDLE);
5259               ed->data[1] = *buflen;
5260               /* *INDENT-ON* */
5261             }
5262         }
5263       else
5264         rv = VPPCOM_EINVAL;
5265       break;
5266
5267     case VPPCOM_ATTR_GET_TCP_KEEPINTVL:
5268       if (buffer && buflen && (*buflen >= sizeof (int)))
5269         {
5270           /* VPP-TBD */
5271           *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
5272                                                 VCL_SESS_ATTR_TCP_KEEPINTVL);
5273           *buflen = sizeof (int);
5274
5275           if (VPPCOM_DEBUG > 2)
5276             clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_TCP_KEEPINTVL: %d, "
5277                           "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
5278                           *buflen);
5279           if (VPPCOM_DEBUG > 0)
5280             {
5281                 /* *INDENT-OFF* */
5282               ELOG_TYPE_DECLARE (e) =
5283                 {
5284                   .format = "VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d buflen=%d",
5285                   .format_args = "i4i4",
5286                 };
5287
5288               struct {
5289                 u32 data[2];
5290               } * ed;
5291
5292               ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5293               ed->data[0] = *(int *) buffer;
5294               ed->data[1] = *buflen;
5295               /* *INDENT-ON* */
5296             }
5297         }
5298       else
5299         rv = VPPCOM_EINVAL;
5300       break;
5301
5302     case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
5303       if (buffer && buflen && (*buflen == sizeof (int)))
5304         {
5305           /* VPP-TBD */
5306           if (*(int *) buffer)
5307             VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
5308           else
5309             VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
5310
5311           if (VPPCOM_DEBUG > 2)
5312             clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d, "
5313                           "buflen %d, #VPP-TBD#", getpid (),
5314                           VCL_SESS_ATTR_TEST (session->attr,
5315                                               VCL_SESS_ATTR_TCP_KEEPINTVL),
5316                           *buflen);
5317           if (VPPCOM_DEBUG > 0)
5318             {
5319                 /* *INDENT-OFF* */
5320               ELOG_TYPE_DECLARE (e) =
5321                 {
5322                   .format = "VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d buflen=%d",
5323                   .format_args = "i4i4",
5324                 };
5325
5326               struct {
5327                 u32 data[2];
5328               } * ed;
5329
5330               ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5331               ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
5332                                                 VCL_SESS_ATTR_TCP_KEEPINTVL);
5333               ed->data[1] = *buflen;
5334               /* *INDENT-ON* */
5335             }
5336         }
5337       else
5338         rv = VPPCOM_EINVAL;
5339       break;
5340
5341     case VPPCOM_ATTR_GET_TCP_USER_MSS:
5342       if (buffer && buflen && (*buflen >= sizeof (u32)))
5343         {
5344           /* VPP-TBD */
5345           *(u32 *) buffer = session->user_mss;
5346           *buflen = sizeof (int);
5347
5348           if (VPPCOM_DEBUG > 2)
5349             clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_TCP_USER_MSS: %d, "
5350                           "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
5351                           *buflen);
5352           if (VPPCOM_DEBUG > 0)
5353             {
5354                 /* *INDENT-OFF* */
5355               ELOG_TYPE_DECLARE (e) =
5356                 {
5357                   .format = "VPPCOM_ATTR_GET_TCP_USER_MSS: %d buflen=%d",
5358                   .format_args = "i4i4",
5359                 };
5360
5361               struct {
5362                 u32 data[2];
5363               } * ed;
5364
5365               ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5366               ed->data[0] = *(int *) buffer;
5367               ed->data[1] = *buflen;
5368               /* *INDENT-ON* */
5369             }
5370         }
5371       else
5372         rv = VPPCOM_EINVAL;
5373       break;
5374
5375     case VPPCOM_ATTR_SET_TCP_USER_MSS:
5376       if (buffer && buflen && (*buflen == sizeof (u32)))
5377         {
5378           /* VPP-TBD */
5379           session->user_mss = *(u32 *) buffer;
5380
5381           if (VPPCOM_DEBUG > 2)
5382             clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_TCP_USER_MSS: %u, "
5383                           "buflen %d, #VPP-TBD#", getpid (),
5384                           session->user_mss, *buflen);
5385           if (VPPCOM_DEBUG > 0)
5386             {
5387                 /* *INDENT-OFF* */
5388               ELOG_TYPE_DECLARE (e) =
5389                 {
5390                   .format = "VPPCOM_ATTR_SET_TCP_USER_MSS: %d buflen=%d",
5391                   .format_args = "i4i4",
5392                 };
5393
5394               struct {
5395                 u32 data[2];
5396               } * ed;
5397
5398               ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5399               ed->data[0] = session->user_mss;
5400               ed->data[1] = *buflen;
5401               /* *INDENT-ON* */
5402             }
5403         }
5404       else
5405         rv = VPPCOM_EINVAL;
5406       break;
5407
5408     default:
5409       rv = VPPCOM_EINVAL;
5410       break;
5411     }
5412
5413 done:
5414   clib_spinlock_unlock (&vcm->sessions_lockp);
5415   return rv;
5416 }
5417
5418 int
5419 vppcom_session_recvfrom (uint32_t session_index, void *buffer,
5420                          uint32_t buflen, int flags, vppcom_endpt_t * ep)
5421 {
5422   int rv = VPPCOM_OK;
5423   session_t *session = 0;
5424
5425   if (ep)
5426     {
5427       clib_spinlock_lock (&vcm->sessions_lockp);
5428       rv = vppcom_session_at_index (session_index, &session);
5429       if (PREDICT_FALSE (rv))
5430         {
5431           clib_spinlock_unlock (&vcm->sessions_lockp);
5432           if (VPPCOM_DEBUG > 0)
5433             clib_warning ("VCL<%d>: invalid session, "
5434                           "sid (%u) has been closed!",
5435                           getpid (), session_index);
5436           if (VPPCOM_DEBUG > 0)
5437             {
5438               /* *INDENT-OFF* */
5439               ELOG_TYPE_DECLARE (e) =
5440                 {
5441                   .format = "invalid session: %d closed",
5442                   .format_args = "i4",
5443                 };
5444
5445               struct {
5446                 u32 data;
5447               } * ed;
5448
5449               ed = ELOG_TRACK_DATA (&vcm->elog_main, e, vcm->elog_track);
5450               ed->data = session_index;
5451               /* *INDENT-ON* */
5452             }
5453           rv = VPPCOM_EBADFD;
5454           clib_spinlock_unlock (&vcm->sessions_lockp);
5455           goto done;
5456         }
5457       ep->is_ip4 = session->peer_addr.is_ip4;
5458       ep->port = session->peer_port;
5459       if (session->peer_addr.is_ip4)
5460         clib_memcpy (ep->ip, &session->peer_addr.ip46.ip4,
5461                      sizeof (ip4_address_t));
5462       else
5463         clib_memcpy (ep->ip, &session->peer_addr.ip46.ip6,
5464                      sizeof (ip6_address_t));
5465       clib_spinlock_unlock (&vcm->sessions_lockp);
5466     }
5467
5468   if (flags == 0)
5469     rv = vppcom_session_read (session_index, buffer, buflen);
5470   else if (flags & MSG_PEEK)
5471     rv = vppcom_session_peek (session_index, buffer, buflen);
5472   else
5473     {
5474       clib_warning ("VCL<%d>: Unsupport flags for recvfrom %d",
5475                     getpid (), flags);
5476       rv = VPPCOM_EAFNOSUPPORT;
5477     }
5478
5479 done:
5480   return rv;
5481 }
5482
5483 int
5484 vppcom_session_sendto (uint32_t session_index, void *buffer,
5485                        uint32_t buflen, int flags, vppcom_endpt_t * ep)
5486 {
5487   if (!buffer)
5488     return VPPCOM_EINVAL;
5489
5490   if (ep)
5491     {
5492       // TBD
5493       return VPPCOM_EINVAL;
5494     }
5495
5496   if (flags)
5497     {
5498       // TBD check the flags and do the right thing
5499       if (VPPCOM_DEBUG > 2)
5500         clib_warning ("VCL<%d>: handling flags 0x%u (%d) "
5501                       "not implemented yet.", getpid (), flags, flags);
5502     }
5503
5504   return (vppcom_session_write (session_index, buffer, buflen));
5505 }
5506
5507 int
5508 vppcom_poll (vcl_poll_t * vp, uint32_t n_sids, double wait_for_time)
5509 {
5510   f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
5511   u32 i, keep_trying = 1;
5512   int rv, num_ev = 0;
5513
5514   if (VPPCOM_DEBUG > 3)
5515     clib_warning ("VCL<%d>: vp %p, nsids %u, wait_for_time %f",
5516                   getpid (), vp, n_sids, wait_for_time);
5517
5518   if (!vp)
5519     return VPPCOM_EFAULT;
5520
5521   do
5522     {
5523       session_t *session;
5524
5525       for (i = 0; i < n_sids; i++)
5526         {
5527           ASSERT (vp[i].revents);
5528
5529           VCL_LOCK_AND_GET_SESSION (vp[i].sid, &session);
5530           clib_spinlock_unlock (&vcm->sessions_lockp);
5531
5532           if (*vp[i].revents)
5533             *vp[i].revents = 0;
5534
5535           if (POLLIN & vp[i].events)
5536             {
5537               VCL_LOCK_AND_GET_SESSION (vp[i].sid, &session);
5538               rv = vppcom_session_read_ready (session, vp[i].sid);
5539               clib_spinlock_unlock (&vcm->sessions_lockp);
5540               if (rv > 0)
5541                 {
5542                   *vp[i].revents |= POLLIN;
5543                   num_ev++;
5544                 }
5545               else if (rv < 0)
5546                 {
5547                   switch (rv)
5548                     {
5549                     case VPPCOM_ECONNRESET:
5550                       *vp[i].revents = POLLHUP;
5551                       break;
5552
5553                     default:
5554                       *vp[i].revents = POLLERR;
5555                       break;
5556                     }
5557                   num_ev++;
5558                 }
5559             }
5560
5561           if (POLLOUT & vp[i].events)
5562             {
5563               VCL_LOCK_AND_GET_SESSION (vp[i].sid, &session);
5564               rv = vppcom_session_write_ready (session, vp[i].sid);
5565               clib_spinlock_unlock (&vcm->sessions_lockp);
5566               if (rv > 0)
5567                 {
5568                   *vp[i].revents |= POLLOUT;
5569                   num_ev++;
5570                 }
5571               else if (rv < 0)
5572                 {
5573                   switch (rv)
5574                     {
5575                     case VPPCOM_ECONNRESET:
5576                       *vp[i].revents = POLLHUP;
5577                       break;
5578
5579                     default:
5580                       *vp[i].revents = POLLERR;
5581                       break;
5582                     }
5583                   num_ev++;
5584                 }
5585             }
5586
5587           if (0)                // Note "done:" label used by VCL_LOCK_AND_GET_SESSION()
5588             {
5589             done:
5590               *vp[i].revents = POLLNVAL;
5591               num_ev++;
5592             }
5593         }
5594       if (wait_for_time != -1)
5595         keep_trying = (clib_time_now (&vcm->clib_time) <= timeout) ? 1 : 0;
5596     }
5597   while ((num_ev == 0) && keep_trying);
5598
5599   if (VPPCOM_DEBUG > 3)
5600     {
5601       clib_warning ("VCL<%d>: returning %d", getpid (), num_ev);
5602       for (i = 0; i < n_sids; i++)
5603         {
5604           clib_warning ("VCL<%d>: vp[%d].sid %d (0x%x), .events 0x%x, "
5605                         ".revents 0x%x", getpid (), i, vp[i].sid, vp[i].sid,
5606                         vp[i].events, *vp[i].revents);
5607         }
5608     }
5609   return num_ev;
5610 }
5611
5612 /*
5613  * fd.io coding-style-patch-verification: ON
5614  *
5615  * Local Variables:
5616  * eval: (c-set-style "gnu")
5617  * End:
5618  */