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