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