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