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