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