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