vcl: separate binary api connections per thread worker
[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 (session->is_dgram)
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->accepted_msg = *msg;
659   /* Session handle points to listener until fully accepted by app */
660   vcl_session_table_add_vpp_handle (wrk, msg->handle, session->session_index);
661
662   return session;
663 }
664
665 static vcl_session_t *
666 vcl_session_disconnected_handler (vcl_worker_t * wrk,
667                                   session_disconnected_msg_t * msg)
668 {
669   vcl_session_t *session;
670
671   session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
672   if (!session)
673     {
674       VDBG (0, "request to disconnect unknown handle 0x%llx", msg->handle);
675       return 0;
676     }
677
678   /* Caught a disconnect before actually accepting the session */
679   if (session->session_state == STATE_LISTEN)
680     {
681       if (!vcl_flag_accepted_session (session, msg->handle,
682                                       VCL_ACCEPTED_F_CLOSED))
683         VDBG (0, "session was not accepted!");
684       return 0;
685     }
686
687   session->session_state = STATE_VPP_CLOSING;
688   return session;
689 }
690
691 static void
692 vcl_session_req_worker_update_handler (vcl_worker_t * wrk, void *data)
693 {
694   session_req_worker_update_msg_t *msg;
695   vcl_session_t *s;
696
697   msg = (session_req_worker_update_msg_t *) data;
698   s = vcl_session_get_w_vpp_handle (wrk, msg->session_handle);
699   if (!s)
700     return;
701
702   vec_add1 (wrk->pending_session_wrk_updates, s->session_index);
703 }
704
705 static void
706 vcl_session_worker_update_reply_handler (vcl_worker_t * wrk, void *data)
707 {
708   session_worker_update_reply_msg_t *msg;
709   vcl_session_t *s;
710
711   msg = (session_worker_update_reply_msg_t *) data;
712   s = vcl_session_get_w_vpp_handle (wrk, msg->handle);
713   if (!s)
714     {
715       VDBG (0, "unknown handle 0x%llx", msg->handle);
716       return;
717     }
718   if (vcl_wait_for_segment (msg->segment_handle))
719     {
720       clib_warning ("segment for session %u couldn't be mounted!",
721                     s->session_index);
722       return;
723     }
724
725   if (s->rx_fifo)
726     {
727       s->rx_fifo = uword_to_pointer (msg->rx_fifo, svm_fifo_t *);
728       s->tx_fifo = uword_to_pointer (msg->tx_fifo, svm_fifo_t *);
729       s->rx_fifo->client_session_index = s->session_index;
730       s->tx_fifo->client_session_index = s->session_index;
731       s->rx_fifo->client_thread_index = wrk->wrk_index;
732       s->tx_fifo->client_thread_index = wrk->wrk_index;
733     }
734   s->session_state = STATE_UPDATED;
735
736   VDBG (0, "session %u[0x%llx] moved to worker %u", s->session_index,
737         s->vpp_handle, wrk->wrk_index);
738 }
739
740 static int
741 vcl_handle_mq_event (vcl_worker_t * wrk, session_event_t * e)
742 {
743   session_disconnected_msg_t *disconnected_msg;
744   vcl_session_t *session;
745
746   switch (e->event_type)
747     {
748     case SESSION_IO_EVT_RX:
749     case SESSION_IO_EVT_TX:
750       session = vcl_session_get (wrk, e->session_index);
751       if (!session || !(session->session_state & STATE_OPEN))
752         break;
753       vec_add1 (wrk->unhandled_evts_vector, *e);
754       break;
755     case SESSION_CTRL_EVT_ACCEPTED:
756       vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
757       break;
758     case SESSION_CTRL_EVT_CONNECTED:
759       vcl_session_connected_handler (wrk,
760                                      (session_connected_msg_t *) e->data);
761       break;
762     case SESSION_CTRL_EVT_DISCONNECTED:
763       disconnected_msg = (session_disconnected_msg_t *) e->data;
764       session = vcl_session_disconnected_handler (wrk, disconnected_msg);
765       if (!session)
766         break;
767       VDBG (0, "disconnected session %u [0x%llx]", session->session_index,
768             session->vpp_handle);
769       break;
770     case SESSION_CTRL_EVT_RESET:
771       vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
772       break;
773     case SESSION_CTRL_EVT_BOUND:
774       vcl_session_bound_handler (wrk, (session_bound_msg_t *) e->data);
775       break;
776     case SESSION_CTRL_EVT_UNLISTEN_REPLY:
777       vcl_session_unlisten_reply_handler (wrk, e->data);
778       break;
779     case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
780       vcl_session_req_worker_update_handler (wrk, e->data);
781       break;
782     case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
783       vcl_session_worker_update_reply_handler (wrk, e->data);
784       break;
785     default:
786       clib_warning ("unhandled %u", e->event_type);
787     }
788   return VPPCOM_OK;
789 }
790
791 static int
792 vppcom_wait_for_session_state_change (u32 session_index,
793                                       vcl_session_state_t state,
794                                       f64 wait_for_time)
795 {
796   vcl_worker_t *wrk = vcl_worker_get_current ();
797   f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
798   vcl_session_t *volatile session;
799   svm_msg_q_msg_t msg;
800   session_event_t *e;
801
802   do
803     {
804       session = vcl_session_get (wrk, session_index);
805       if (PREDICT_FALSE (!session))
806         {
807           return VPPCOM_EBADFD;
808         }
809       if (session->session_state & state)
810         {
811           return VPPCOM_OK;
812         }
813       if (session->session_state & STATE_FAILED)
814         {
815           return VPPCOM_ECONNREFUSED;
816         }
817
818       if (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0))
819         {
820           usleep (100);
821           continue;
822         }
823       e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
824       vcl_handle_mq_event (wrk, e);
825       svm_msg_q_free_msg (wrk->app_event_queue, &msg);
826     }
827   while (clib_time_now (&wrk->clib_time) < timeout);
828
829   VDBG (0, "timeout waiting for state 0x%x (%s)", state,
830         vppcom_session_state_str (state));
831   vcl_evt (VCL_EVT_SESSION_TIMEOUT, session, session_state);
832
833   return VPPCOM_ETIMEDOUT;
834 }
835
836 static void
837 vcl_handle_pending_wrk_updates (vcl_worker_t * wrk)
838 {
839   vcl_session_state_t state;
840   vcl_session_t *s;
841   u32 *sip;
842
843   if (PREDICT_TRUE (vec_len (wrk->pending_session_wrk_updates) == 0))
844     return;
845
846   vec_foreach (sip, wrk->pending_session_wrk_updates)
847   {
848     s = vcl_session_get (wrk, *sip);
849     vcl_send_session_worker_update (wrk, s, wrk->wrk_index);
850     state = s->session_state;
851     vppcom_wait_for_session_state_change (s->session_index, STATE_UPDATED, 5);
852     s->session_state = state;
853   }
854   vec_reset_length (wrk->pending_session_wrk_updates);
855 }
856
857 void
858 vcl_flush_mq_events (void)
859 {
860   vcl_worker_t *wrk = vcl_worker_get_current ();
861   svm_msg_q_msg_t *msg;
862   session_event_t *e;
863   svm_msg_q_t *mq;
864   int i;
865
866   mq = wrk->app_event_queue;
867   svm_msg_q_lock (mq);
868   vcl_mq_dequeue_batch (wrk, mq, ~0);
869   svm_msg_q_unlock (mq);
870
871   for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
872     {
873       msg = vec_elt_at_index (wrk->mq_msg_vector, i);
874       e = svm_msg_q_msg_data (mq, msg);
875       vcl_handle_mq_event (wrk, e);
876       svm_msg_q_free_msg (mq, msg);
877     }
878   vec_reset_length (wrk->mq_msg_vector);
879   vcl_handle_pending_wrk_updates (wrk);
880 }
881
882 static int
883 vppcom_app_session_enable (void)
884 {
885   int rv;
886
887   if (vcm->app_state != STATE_APP_ENABLED)
888     {
889       vppcom_send_session_enable_disable (1 /* is_enabled == TRUE */ );
890       rv = vcl_wait_for_app_state_change (STATE_APP_ENABLED);
891       if (PREDICT_FALSE (rv))
892         {
893           VDBG (0, "application session enable timed out! returning %d (%s)",
894                 rv, vppcom_retval_str (rv));
895           return rv;
896         }
897     }
898   return VPPCOM_OK;
899 }
900
901 static int
902 vppcom_app_attach (void)
903 {
904   int rv;
905
906   vppcom_app_send_attach ();
907   rv = vcl_wait_for_app_state_change (STATE_APP_ATTACHED);
908   if (PREDICT_FALSE (rv))
909     {
910       VDBG (0, "application attach timed out! returning %d (%s)", rv,
911             vppcom_retval_str (rv));
912       return rv;
913     }
914
915   return VPPCOM_OK;
916 }
917
918 static int
919 vppcom_session_unbind (u32 session_handle)
920 {
921   vcl_worker_t *wrk = vcl_worker_get_current ();
922   session_accepted_msg_t *accepted_msg;
923   vcl_session_t *session = 0;
924   vcl_session_msg_t *evt;
925
926   session = vcl_session_get_w_handle (wrk, session_handle);
927   if (!session)
928     return VPPCOM_EBADFD;
929
930   /* Flush pending accept events, if any */
931   while (clib_fifo_elts (session->accept_evts_fifo))
932     {
933       clib_fifo_sub2 (session->accept_evts_fifo, evt);
934       accepted_msg = &evt->accepted_msg;
935       vcl_session_table_del_vpp_handle (wrk, accepted_msg->handle);
936       vcl_send_session_accepted_reply (session->vpp_evt_q,
937                                        accepted_msg->context,
938                                        session->vpp_handle, -1);
939     }
940   clib_fifo_free (session->accept_evts_fifo);
941
942   vcl_send_session_unlisten (wrk, session);
943
944   VDBG (1, "session %u [0x%llx]: sending unbind!", session->session_index,
945         session->vpp_handle);
946   vcl_evt (VCL_EVT_UNBIND, session);
947
948   session->vpp_handle = ~0;
949   session->session_state = STATE_DISCONNECT;
950
951   return VPPCOM_OK;
952 }
953
954 static int
955 vppcom_session_disconnect (u32 session_handle)
956 {
957   vcl_worker_t *wrk = vcl_worker_get_current ();
958   svm_msg_q_t *vpp_evt_q;
959   vcl_session_t *session, *listen_session;
960   vcl_session_state_t state;
961   u64 vpp_handle;
962
963   session = vcl_session_get_w_handle (wrk, session_handle);
964   if (!session)
965     return VPPCOM_EBADFD;
966
967   vpp_handle = session->vpp_handle;
968   state = session->session_state;
969
970   VDBG (1, "session %u [0x%llx] state 0x%x (%s)", session->session_index,
971         vpp_handle, state, vppcom_session_state_str (state));
972
973   if (PREDICT_FALSE (state & STATE_LISTEN))
974     {
975       VDBG (0, "ERROR: Cannot disconnect a listen socket!");
976       return VPPCOM_EBADFD;
977     }
978
979   if (state & STATE_VPP_CLOSING)
980     {
981       vpp_evt_q = vcl_session_vpp_evt_q (wrk, session);
982       vcl_send_session_disconnected_reply (vpp_evt_q, wrk->my_client_index,
983                                            vpp_handle, 0);
984       VDBG (1, "session %u [0x%llx]: sending disconnect REPLY...",
985             session->session_index, vpp_handle);
986     }
987   else
988     {
989       VDBG (1, "session %u [0x%llx]: sending disconnect...",
990             session->session_index, vpp_handle);
991       vcl_send_session_disconnect (wrk, session);
992     }
993
994   if (session->listener_index != VCL_INVALID_SESSION_INDEX)
995     {
996       listen_session = vcl_session_get (wrk, session->listener_index);
997       listen_session->n_accepted_sessions--;
998     }
999
1000   return VPPCOM_OK;
1001 }
1002
1003 /**
1004  * Handle app exit
1005  *
1006  * Notify vpp of the disconnect and mark the worker as free. If we're the
1007  * last worker, do a full cleanup otherwise, since we're probably a forked
1008  * child, avoid syscalls as much as possible. We might've lost privileges.
1009  */
1010 void
1011 vppcom_app_exit (void)
1012 {
1013   if (!pool_elts (vcm->workers))
1014     return;
1015   vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
1016   vcl_set_worker_index (~0);
1017   vcl_elog_stop (vcm);
1018   if (vec_len (vcm->workers) == 1)
1019     vppcom_disconnect_from_vpp ();
1020   else
1021     vl_client_send_disconnect (1 /* vpp should cleanup */ );
1022 }
1023
1024 /*
1025  * VPPCOM Public API functions
1026  */
1027 int
1028 vppcom_app_create (char *app_name)
1029 {
1030   vppcom_cfg_t *vcl_cfg = &vcm->cfg;
1031   int rv;
1032
1033   if (vcm->is_init)
1034     {
1035       VDBG (1, "already initialized");
1036       return VPPCOM_EEXIST;
1037     }
1038
1039   vcm->is_init = 1;
1040   vppcom_cfg (&vcm->cfg);
1041   vcl_cfg = &vcm->cfg;
1042
1043   vcm->main_cpu = pthread_self ();
1044   vcm->main_pid = getpid ();
1045   vcm->app_name = format (0, "%s", app_name);
1046   vppcom_init_error_string_table ();
1047   fifo_segment_main_init (&vcm->segment_main, vcl_cfg->segment_baseva,
1048                           20 /* timeout in secs */ );
1049   pool_alloc (vcm->workers, vcl_cfg->max_workers);
1050   clib_spinlock_init (&vcm->workers_lock);
1051   clib_rwlock_init (&vcm->segment_table_lock);
1052   atexit (vppcom_app_exit);
1053
1054   /* Allocate default worker */
1055   vcl_worker_alloc_and_init ();
1056
1057   /* API hookup and connect to VPP */
1058   vcl_elog_init (vcm);
1059   vcm->app_state = STATE_APP_START;
1060   rv = vppcom_connect_to_vpp (app_name);
1061   if (rv)
1062     {
1063       VERR ("couldn't connect to VPP!");
1064       return rv;
1065     }
1066   VDBG (0, "sending session enable");
1067   rv = vppcom_app_session_enable ();
1068   if (rv)
1069     {
1070       VERR ("vppcom_app_session_enable() failed!");
1071       return rv;
1072     }
1073
1074   VDBG (0, "sending app attach");
1075   rv = vppcom_app_attach ();
1076   if (rv)
1077     {
1078       VERR ("vppcom_app_attach() failed!");
1079       return rv;
1080     }
1081
1082   VDBG (0, "app_name '%s', my_client_index %d (0x%x)", app_name,
1083         vcm->workers[0].my_client_index, vcm->workers[0].my_client_index);
1084
1085   return VPPCOM_OK;
1086 }
1087
1088 void
1089 vppcom_app_destroy (void)
1090 {
1091   int rv;
1092   f64 orig_app_timeout;
1093
1094   if (!pool_elts (vcm->workers))
1095     return;
1096
1097   vcl_evt (VCL_EVT_DETACH, vcm);
1098
1099   if (pool_elts (vcm->workers) == 1)
1100     {
1101       vcl_send_app_detach (vcl_worker_get_current ());
1102       orig_app_timeout = vcm->cfg.app_timeout;
1103       vcm->cfg.app_timeout = 2.0;
1104       rv = vcl_wait_for_app_state_change (STATE_APP_ENABLED);
1105       vcm->cfg.app_timeout = orig_app_timeout;
1106       if (PREDICT_FALSE (rv))
1107         VDBG (0, "application detach timed out! returning %d (%s)", rv,
1108               vppcom_retval_str (rv));
1109       vec_free (vcm->app_name);
1110       vcl_worker_cleanup (vcl_worker_get_current (), 0 /* notify vpp */ );
1111     }
1112   else
1113     {
1114       vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
1115     }
1116
1117   vcl_set_worker_index (~0);
1118   vcl_elog_stop (vcm);
1119   vl_client_disconnect_from_vlib ();
1120 }
1121
1122 int
1123 vppcom_session_create (u8 proto, u8 is_nonblocking)
1124 {
1125   vcl_worker_t *wrk = vcl_worker_get_current ();
1126   vcl_session_t *session;
1127
1128   session = vcl_session_alloc (wrk);
1129
1130   session->session_type = proto;
1131   session->session_state = STATE_START;
1132   session->vpp_handle = ~0;
1133   session->is_dgram = vcl_proto_is_dgram (proto);
1134
1135   if (is_nonblocking)
1136     VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK);
1137
1138   vcl_evt (VCL_EVT_CREATE, session, session_type, session->session_state,
1139            is_nonblocking, session_index);
1140
1141   VDBG (0, "created session %u", session->session_index);
1142
1143   return vcl_session_handle (session);
1144 }
1145
1146 int
1147 vcl_session_cleanup (vcl_worker_t * wrk, vcl_session_t * session,
1148                      vcl_session_handle_t sh, u8 do_disconnect)
1149 {
1150   vcl_session_state_t state;
1151   u32 next_sh, vep_sh;
1152   int rv = VPPCOM_OK;
1153   u64 vpp_handle;
1154   u8 is_vep;
1155
1156   is_vep = session->is_vep;
1157   next_sh = session->vep.next_sh;
1158   vep_sh = session->vep.vep_sh;
1159   state = session->session_state;
1160   vpp_handle = session->vpp_handle;
1161
1162   VDBG (1, "session %u [0x%llx] closing", session->session_index, vpp_handle);
1163
1164   if (is_vep)
1165     {
1166       while (next_sh != ~0)
1167         {
1168           rv = vppcom_epoll_ctl (sh, EPOLL_CTL_DEL, next_sh, 0);
1169           if (PREDICT_FALSE (rv < 0))
1170             VDBG (0, "vpp handle 0x%llx, sh %u: EPOLL_CTL_DEL vep_idx %u"
1171                   " failed! rv %d (%s)", vpp_handle, next_sh, vep_sh, rv,
1172                   vppcom_retval_str (rv));
1173
1174           next_sh = session->vep.next_sh;
1175         }
1176     }
1177   else
1178     {
1179       if (session->is_vep_session)
1180         {
1181           rv = vppcom_epoll_ctl (vep_sh, EPOLL_CTL_DEL, sh, 0);
1182           if (rv < 0)
1183             VDBG (0, "session %u [0x%llx]: EPOLL_CTL_DEL vep_idx %u "
1184                   "failed! rv %d (%s)", session->session_index, vpp_handle,
1185                   vep_sh, rv, vppcom_retval_str (rv));
1186         }
1187
1188       if (!do_disconnect)
1189         {
1190           VDBG (1, "session %u [0x%llx] disconnect skipped",
1191                 session->session_index, vpp_handle);
1192           goto cleanup;
1193         }
1194
1195       if (state & STATE_LISTEN)
1196         {
1197           rv = vppcom_session_unbind (sh);
1198           if (PREDICT_FALSE (rv < 0))
1199             VDBG (0, "session %u [0x%llx]: listener unbind failed! "
1200                   "rv %d (%s)", session->session_index, vpp_handle, rv,
1201                   vppcom_retval_str (rv));
1202           return rv;
1203         }
1204       else if ((state & STATE_OPEN)
1205                || (vcl_session_is_connectable_listener (wrk, session)))
1206         {
1207           rv = vppcom_session_disconnect (sh);
1208           if (PREDICT_FALSE (rv < 0))
1209             VDBG (0, "ERROR: session %u [0x%llx]: disconnect failed!"
1210                   " rv %d (%s)", session->session_index, vpp_handle,
1211                   rv, vppcom_retval_str (rv));
1212         }
1213       else if (state == STATE_DISCONNECT)
1214         {
1215           svm_msg_q_t *mq = vcl_session_vpp_evt_q (wrk, session);
1216           vcl_send_session_reset_reply (mq, wrk->my_client_index,
1217                                         session->vpp_handle, 0);
1218         }
1219     }
1220
1221   VDBG (0, "session %u [0x%llx] removed", session->session_index, vpp_handle);
1222
1223 cleanup:
1224   vcl_session_table_del_vpp_handle (wrk, vpp_handle);
1225   vcl_session_free (wrk, session);
1226   vcl_evt (VCL_EVT_CLOSE, session, rv);
1227
1228   return rv;
1229 }
1230
1231 int
1232 vppcom_session_close (uint32_t session_handle)
1233 {
1234   vcl_worker_t *wrk = vcl_worker_get_current ();
1235   vcl_session_t *session;
1236
1237   session = vcl_session_get_w_handle (wrk, session_handle);
1238   if (!session)
1239     return VPPCOM_EBADFD;
1240   return vcl_session_cleanup (wrk, session, session_handle,
1241                               1 /* do_disconnect */ );
1242 }
1243
1244 int
1245 vppcom_session_bind (uint32_t session_handle, vppcom_endpt_t * ep)
1246 {
1247   vcl_worker_t *wrk = vcl_worker_get_current ();
1248   vcl_session_t *session = 0;
1249
1250   if (!ep || !ep->ip)
1251     return VPPCOM_EINVAL;
1252
1253   session = vcl_session_get_w_handle (wrk, session_handle);
1254   if (!session)
1255     return VPPCOM_EBADFD;
1256
1257   if (session->is_vep)
1258     {
1259       VDBG (0, "ERROR: cannot bind to epoll session %u!",
1260             session->session_index);
1261       return VPPCOM_EBADFD;
1262     }
1263
1264   session->transport.is_ip4 = ep->is_ip4;
1265   if (ep->is_ip4)
1266     clib_memcpy_fast (&session->transport.lcl_ip.ip4, ep->ip,
1267                       sizeof (ip4_address_t));
1268   else
1269     clib_memcpy_fast (&session->transport.lcl_ip.ip6, ep->ip,
1270                       sizeof (ip6_address_t));
1271   session->transport.lcl_port = ep->port;
1272
1273   VDBG (0, "session %u handle %u: binding to local %s address %U port %u, "
1274         "proto %s", session->session_index, session_handle,
1275         session->transport.is_ip4 ? "IPv4" : "IPv6",
1276         format_ip46_address, &session->transport.lcl_ip,
1277         session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
1278         clib_net_to_host_u16 (session->transport.lcl_port),
1279         vppcom_proto_str (session->session_type));
1280   vcl_evt (VCL_EVT_BIND, session);
1281
1282   if (session->session_type == VPPCOM_PROTO_UDP)
1283     vppcom_session_listen (session_handle, 10);
1284
1285   return VPPCOM_OK;
1286 }
1287
1288 int
1289 vppcom_session_listen (uint32_t listen_sh, uint32_t q_len)
1290 {
1291   vcl_worker_t *wrk = vcl_worker_get_current ();
1292   vcl_session_t *listen_session = 0;
1293   u64 listen_vpp_handle;
1294   int rv;
1295
1296   listen_session = vcl_session_get_w_handle (wrk, listen_sh);
1297   if (!listen_session || listen_session->is_vep)
1298     return VPPCOM_EBADFD;
1299
1300   if (q_len == 0 || q_len == ~0)
1301     q_len = vcm->cfg.listen_queue_size;
1302
1303   listen_vpp_handle = listen_session->vpp_handle;
1304   if (listen_session->session_state & STATE_LISTEN)
1305     {
1306       VDBG (0, "session %u [0x%llx]: already in listen state!",
1307             listen_sh, listen_vpp_handle);
1308       return VPPCOM_OK;
1309     }
1310
1311   VDBG (0, "session %u: sending vpp listen request...", listen_sh);
1312
1313   /*
1314    * Send listen request to vpp and wait for reply
1315    */
1316   vcl_send_session_listen (wrk, listen_session);
1317   rv = vppcom_wait_for_session_state_change (listen_session->session_index,
1318                                              STATE_LISTEN,
1319                                              vcm->cfg.session_timeout);
1320
1321   if (PREDICT_FALSE (rv))
1322     {
1323       listen_session = vcl_session_get_w_handle (wrk, listen_sh);
1324       VDBG (0, "session %u [0x%llx]: listen failed! returning %d (%s)",
1325             listen_sh, listen_session->vpp_handle, rv,
1326             vppcom_retval_str (rv));
1327       return rv;
1328     }
1329
1330   return VPPCOM_OK;
1331 }
1332
1333 int
1334 vppcom_session_tls_add_cert (uint32_t session_handle, char *cert,
1335                              uint32_t cert_len)
1336 {
1337
1338   vcl_worker_t *wrk = vcl_worker_get_current ();
1339   vcl_session_t *session = 0;
1340
1341   session = vcl_session_get_w_handle (wrk, session_handle);
1342   if (!session)
1343     return VPPCOM_EBADFD;
1344
1345   if (cert_len == 0 || cert_len == ~0)
1346     return VPPCOM_EBADFD;
1347
1348   /*
1349    * Send listen request to vpp and wait for reply
1350    */
1351   vppcom_send_application_tls_cert_add (session, cert, cert_len);
1352   vcm->app_state = STATE_APP_ADDING_TLS_DATA;
1353   vcl_wait_for_app_state_change (STATE_APP_READY);
1354   return VPPCOM_OK;
1355
1356 }
1357
1358 int
1359 vppcom_session_tls_add_key (uint32_t session_handle, char *key,
1360                             uint32_t key_len)
1361 {
1362
1363   vcl_worker_t *wrk = vcl_worker_get_current ();
1364   vcl_session_t *session = 0;
1365
1366   session = vcl_session_get_w_handle (wrk, session_handle);
1367   if (!session)
1368     return VPPCOM_EBADFD;
1369
1370   if (key_len == 0 || key_len == ~0)
1371     return VPPCOM_EBADFD;
1372
1373   vppcom_send_application_tls_key_add (session, key, key_len);
1374   vcm->app_state = STATE_APP_ADDING_TLS_DATA;
1375   vcl_wait_for_app_state_change (STATE_APP_READY);
1376   return VPPCOM_OK;
1377 }
1378
1379 static int
1380 validate_args_session_accept_ (vcl_worker_t * wrk, vcl_session_t * ls)
1381 {
1382   if (ls->is_vep)
1383     {
1384       VDBG (0, "ERROR: cannot accept on epoll session %u!",
1385             ls->session_index);
1386       return VPPCOM_EBADFD;
1387     }
1388
1389   if ((ls->session_state != STATE_LISTEN)
1390       && (!vcl_session_is_connectable_listener (wrk, ls)))
1391     {
1392       VDBG (0,
1393             "ERROR: session [0x%llx]: not in listen state! state 0x%x"
1394             " (%s)", ls->vpp_handle, ls->session_state,
1395             vppcom_session_state_str (ls->session_state));
1396       return VPPCOM_EBADFD;
1397     }
1398   return VPPCOM_OK;
1399 }
1400
1401 int
1402 vppcom_unformat_proto (uint8_t * proto, char *proto_str)
1403 {
1404   if (!strcmp (proto_str, "TCP"))
1405     *proto = VPPCOM_PROTO_TCP;
1406   else if (!strcmp (proto_str, "tcp"))
1407     *proto = VPPCOM_PROTO_TCP;
1408   else if (!strcmp (proto_str, "UDP"))
1409     *proto = VPPCOM_PROTO_UDP;
1410   else if (!strcmp (proto_str, "udp"))
1411     *proto = VPPCOM_PROTO_UDP;
1412   else if (!strcmp (proto_str, "UDPC"))
1413     *proto = VPPCOM_PROTO_UDPC;
1414   else if (!strcmp (proto_str, "udpc"))
1415     *proto = VPPCOM_PROTO_UDPC;
1416   else if (!strcmp (proto_str, "SCTP"))
1417     *proto = VPPCOM_PROTO_SCTP;
1418   else if (!strcmp (proto_str, "sctp"))
1419     *proto = VPPCOM_PROTO_SCTP;
1420   else if (!strcmp (proto_str, "TLS"))
1421     *proto = VPPCOM_PROTO_TLS;
1422   else if (!strcmp (proto_str, "tls"))
1423     *proto = VPPCOM_PROTO_TLS;
1424   else if (!strcmp (proto_str, "QUIC"))
1425     *proto = VPPCOM_PROTO_QUIC;
1426   else if (!strcmp (proto_str, "quic"))
1427     *proto = VPPCOM_PROTO_QUIC;
1428   else
1429     return 1;
1430   return 0;
1431 }
1432
1433 int
1434 vppcom_session_accept (uint32_t listen_session_handle, vppcom_endpt_t * ep,
1435                        uint32_t flags)
1436 {
1437   u32 client_session_index = ~0, listen_session_index, accept_flags = 0;
1438   vcl_worker_t *wrk = vcl_worker_get_current ();
1439   session_accepted_msg_t accepted_msg;
1440   vcl_session_t *listen_session = 0;
1441   vcl_session_t *client_session = 0;
1442   vcl_session_msg_t *evt;
1443   svm_msg_q_msg_t msg;
1444   session_event_t *e;
1445   u8 is_nonblocking;
1446   int rv;
1447
1448   listen_session = vcl_session_get_w_handle (wrk, listen_session_handle);
1449   if (!listen_session)
1450     return VPPCOM_EBADFD;
1451
1452   listen_session_index = listen_session->session_index;
1453   if ((rv = validate_args_session_accept_ (wrk, listen_session)))
1454     return rv;
1455
1456   if (clib_fifo_elts (listen_session->accept_evts_fifo))
1457     {
1458       clib_fifo_sub2 (listen_session->accept_evts_fifo, evt);
1459       accept_flags = evt->flags;
1460       accepted_msg = evt->accepted_msg;
1461       goto handle;
1462     }
1463
1464   is_nonblocking = VCL_SESS_ATTR_TEST (listen_session->attr,
1465                                        VCL_SESS_ATTR_NONBLOCK);
1466   while (1)
1467     {
1468       if (svm_msg_q_is_empty (wrk->app_event_queue) && is_nonblocking)
1469         return VPPCOM_EAGAIN;
1470
1471       if (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_WAIT, 0))
1472         return VPPCOM_EAGAIN;
1473
1474       e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
1475       if (e->event_type != SESSION_CTRL_EVT_ACCEPTED)
1476         {
1477           VDBG (0, "discarded event: %u", e->event_type);
1478           svm_msg_q_free_msg (wrk->app_event_queue, &msg);
1479           continue;
1480         }
1481       clib_memcpy_fast (&accepted_msg, e->data, sizeof (accepted_msg));
1482       svm_msg_q_free_msg (wrk->app_event_queue, &msg);
1483       break;
1484     }
1485
1486 handle:
1487
1488   client_session_index = vcl_session_accepted_handler (wrk, &accepted_msg,
1489                                                        listen_session_index);
1490   if (client_session_index == VCL_INVALID_SESSION_INDEX)
1491     return VPPCOM_ECONNABORTED;
1492
1493   listen_session = vcl_session_get (wrk, listen_session_index);
1494   client_session = vcl_session_get (wrk, client_session_index);
1495
1496   if (flags & O_NONBLOCK)
1497     VCL_SESS_ATTR_SET (client_session->attr, VCL_SESS_ATTR_NONBLOCK);
1498
1499   VDBG (1, "listener %u [0x%llx]: Got a connect request! session %u [0x%llx],"
1500         " flags %d, is_nonblocking %u", listen_session->session_index,
1501         listen_session->vpp_handle, client_session_index,
1502         client_session->vpp_handle, flags,
1503         VCL_SESS_ATTR_TEST (client_session->attr, VCL_SESS_ATTR_NONBLOCK));
1504
1505   if (ep)
1506     {
1507       ep->is_ip4 = client_session->transport.is_ip4;
1508       ep->port = client_session->transport.rmt_port;
1509       if (client_session->transport.is_ip4)
1510         clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip4,
1511                           sizeof (ip4_address_t));
1512       else
1513         clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip6,
1514                           sizeof (ip6_address_t));
1515     }
1516
1517   VDBG (0, "listener %u [0x%llx] accepted %u [0x%llx] peer: %U:%u "
1518         "local: %U:%u", listen_session_handle, listen_session->vpp_handle,
1519         client_session_index, client_session->vpp_handle,
1520         format_ip46_address, &client_session->transport.rmt_ip,
1521         client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
1522         clib_net_to_host_u16 (client_session->transport.rmt_port),
1523         format_ip46_address, &client_session->transport.lcl_ip,
1524         client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
1525         clib_net_to_host_u16 (client_session->transport.lcl_port));
1526   vcl_evt (VCL_EVT_ACCEPT, client_session, listen_session,
1527            client_session_index);
1528
1529   /*
1530    * Session might have been closed already
1531    */
1532   if (accept_flags)
1533     {
1534       if (accept_flags & VCL_ACCEPTED_F_CLOSED)
1535         client_session->session_state = STATE_VPP_CLOSING;
1536       else if (accept_flags & VCL_ACCEPTED_F_RESET)
1537         client_session->session_state = STATE_DISCONNECT;
1538     }
1539   return vcl_session_handle (client_session);
1540 }
1541
1542 static void
1543 vcl_ip_copy_from_ep (ip46_address_t * ip, vppcom_endpt_t * ep)
1544 {
1545   if (ep->is_ip4)
1546     clib_memcpy_fast (&ip->ip4, ep->ip, sizeof (ip4_address_t));
1547   else
1548     clib_memcpy_fast (&ip->ip6, ep->ip, sizeof (ip6_address_t));
1549 }
1550
1551 void
1552 vcl_ip_copy_to_ep (ip46_address_t * ip, vppcom_endpt_t * ep, u8 is_ip4)
1553 {
1554   ep->is_ip4 = is_ip4;
1555   if (is_ip4)
1556     clib_memcpy_fast (ep->ip, &ip->ip4, sizeof (ip4_address_t));
1557   else
1558     clib_memcpy_fast (ep->ip, &ip->ip6, sizeof (ip6_address_t));
1559 }
1560
1561 int
1562 vppcom_session_connect (uint32_t session_handle, vppcom_endpt_t * server_ep)
1563 {
1564   vcl_worker_t *wrk = vcl_worker_get_current ();
1565   vcl_session_t *session = 0;
1566   u32 session_index;
1567   int rv;
1568
1569   session = vcl_session_get_w_handle (wrk, session_handle);
1570   if (!session)
1571     return VPPCOM_EBADFD;
1572   session_index = session->session_index;
1573
1574   if (PREDICT_FALSE (session->is_vep))
1575     {
1576       VDBG (0, "ERROR: cannot connect epoll session %u!",
1577             session->session_index);
1578       return VPPCOM_EBADFD;
1579     }
1580
1581   if (PREDICT_FALSE (session->session_state & CLIENT_STATE_OPEN))
1582     {
1583       VDBG (0, "session handle %u [0x%llx]: session already "
1584             "connected to %s %U port %d proto %s, state 0x%x (%s)",
1585             session_handle, session->vpp_handle,
1586             session->transport.is_ip4 ? "IPv4" : "IPv6", format_ip46_address,
1587             &session->transport.rmt_ip, session->transport.is_ip4 ?
1588             IP46_TYPE_IP4 : IP46_TYPE_IP6,
1589             clib_net_to_host_u16 (session->transport.rmt_port),
1590             vppcom_proto_str (session->session_type), session->session_state,
1591             vppcom_session_state_str (session->session_state));
1592       return VPPCOM_OK;
1593     }
1594
1595   session->transport.is_ip4 = server_ep->is_ip4;
1596   vcl_ip_copy_from_ep (&session->transport.rmt_ip, server_ep);
1597   session->transport.rmt_port = server_ep->port;
1598   session->parent_handle = VCL_INVALID_SESSION_HANDLE;
1599
1600   VDBG (0, "session handle %u: connecting to server %s %U "
1601         "port %d proto %s", session_handle,
1602         session->transport.is_ip4 ? "IPv4" : "IPv6",
1603         format_ip46_address,
1604         &session->transport.rmt_ip, session->transport.is_ip4 ?
1605         IP46_TYPE_IP4 : IP46_TYPE_IP6,
1606         clib_net_to_host_u16 (session->transport.rmt_port),
1607         vppcom_proto_str (session->session_type));
1608
1609   vcl_send_session_connect (wrk, session);
1610
1611   if (VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK))
1612     return VPPCOM_EINPROGRESS;
1613
1614   /*
1615    * Wait for reply from vpp if blocking
1616    */
1617   rv = vppcom_wait_for_session_state_change (session_index, STATE_CONNECT,
1618                                              vcm->cfg.session_timeout);
1619
1620   session = vcl_session_get (wrk, session_index);
1621   VDBG (0, "session %u [0x%llx]: connect %s!", session->session_index,
1622         session->vpp_handle, rv ? "failed" : "succeeded");
1623
1624   return rv;
1625 }
1626
1627 int
1628 vppcom_session_stream_connect (uint32_t session_handle,
1629                                uint32_t parent_session_handle)
1630 {
1631   vcl_worker_t *wrk = vcl_worker_get_current ();
1632   vcl_session_t *session, *parent_session;
1633   u32 session_index, parent_session_index;
1634   int rv;
1635
1636   session = vcl_session_get_w_handle (wrk, session_handle);
1637   if (!session)
1638     return VPPCOM_EBADFD;
1639   parent_session = vcl_session_get_w_handle (wrk, parent_session_handle);
1640   if (!parent_session)
1641     return VPPCOM_EBADFD;
1642
1643   session_index = session->session_index;
1644   parent_session_index = parent_session->session_index;
1645   if (PREDICT_FALSE (session->is_vep))
1646     {
1647       VDBG (0, "ERROR: cannot connect epoll session %u!",
1648             session->session_index);
1649       return VPPCOM_EBADFD;
1650     }
1651
1652   if (PREDICT_FALSE (session->session_state & CLIENT_STATE_OPEN))
1653     {
1654       VDBG (0, "session handle %u [0x%llx]: session already "
1655             "connected to session %u [0x%llx] proto %s, state 0x%x (%s)",
1656             session_handle, session->vpp_handle,
1657             parent_session_handle, parent_session->vpp_handle,
1658             vppcom_proto_str (session->session_type), session->session_state,
1659             vppcom_session_state_str (session->session_state));
1660       return VPPCOM_OK;
1661     }
1662
1663   /* Connect to quic session specifics */
1664   session->transport.is_ip4 = parent_session->transport.is_ip4;
1665   session->transport.rmt_ip.ip4.as_u32 = (uint32_t) 1;
1666   session->transport.rmt_port = 0;
1667   session->parent_handle = parent_session->vpp_handle;
1668
1669   VDBG (0, "session handle %u: connecting to session %u [0x%llx]",
1670         session_handle, parent_session_handle, parent_session->vpp_handle);
1671
1672   /*
1673    * Send connect request and wait for reply from vpp
1674    */
1675   vcl_send_session_connect (wrk, session);
1676   rv = vppcom_wait_for_session_state_change (session_index, STATE_CONNECT,
1677                                              vcm->cfg.session_timeout);
1678
1679   session->listener_index = parent_session_index;
1680   parent_session = vcl_session_get_w_handle (wrk, parent_session_handle);
1681   if (parent_session)
1682     parent_session->n_accepted_sessions++;
1683
1684   session = vcl_session_get (wrk, session_index);
1685   VDBG (0, "session %u [0x%llx]: connect %s!", session->session_index,
1686         session->vpp_handle, rv ? "failed" : "succeeded");
1687
1688   return rv;
1689 }
1690
1691 static u8
1692 vcl_is_rx_evt_for_session (session_event_t * e, u32 sid, u8 is_ct)
1693 {
1694   return (e->event_type == SESSION_IO_EVT_RX && e->session_index == sid);
1695 }
1696
1697 static inline int
1698 vppcom_session_read_internal (uint32_t session_handle, void *buf, int n,
1699                               u8 peek)
1700 {
1701   vcl_worker_t *wrk = vcl_worker_get_current ();
1702   int n_read = 0, is_nonblocking;
1703   vcl_session_t *s = 0;
1704   svm_fifo_t *rx_fifo;
1705   svm_msg_q_msg_t msg;
1706   session_event_t *e;
1707   svm_msg_q_t *mq;
1708   u8 is_ct;
1709
1710   if (PREDICT_FALSE (!buf))
1711     return VPPCOM_EINVAL;
1712
1713   s = vcl_session_get_w_handle (wrk, session_handle);
1714   if (PREDICT_FALSE (!s || s->is_vep))
1715     return VPPCOM_EBADFD;
1716
1717   if (PREDICT_FALSE (!vcl_session_is_open (s)))
1718     {
1719       VDBG (0, "session %u[0x%llx] is not open! state 0x%x (%s)",
1720             s->session_index, s->vpp_handle, s->session_state,
1721             vppcom_session_state_str (s->session_state));
1722       return vcl_session_closed_error (s);
1723     }
1724
1725   is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
1726   is_ct = vcl_session_is_ct (s);
1727   mq = wrk->app_event_queue;
1728   rx_fifo = is_ct ? s->ct_rx_fifo : s->rx_fifo;
1729   s->has_rx_evt = 0;
1730
1731   if (svm_fifo_is_empty_cons (rx_fifo))
1732     {
1733       if (is_nonblocking)
1734         {
1735           if (vcl_session_is_closing (s))
1736             return vcl_session_closing_error (s);
1737           svm_fifo_unset_event (s->rx_fifo);
1738           return VPPCOM_EWOULDBLOCK;
1739         }
1740       while (svm_fifo_is_empty_cons (rx_fifo))
1741         {
1742           if (vcl_session_is_closing (s))
1743             return vcl_session_closing_error (s);
1744
1745           svm_fifo_unset_event (s->rx_fifo);
1746           svm_msg_q_lock (mq);
1747           if (svm_msg_q_is_empty (mq))
1748             svm_msg_q_wait (mq);
1749
1750           svm_msg_q_sub_w_lock (mq, &msg);
1751           e = svm_msg_q_msg_data (mq, &msg);
1752           svm_msg_q_unlock (mq);
1753           if (!vcl_is_rx_evt_for_session (e, s->session_index, is_ct))
1754             vcl_handle_mq_event (wrk, e);
1755           svm_msg_q_free_msg (mq, &msg);
1756         }
1757     }
1758
1759   if (s->is_dgram)
1760     n_read = app_recv_dgram_raw (rx_fifo, buf, n, &s->transport, 0, peek);
1761   else
1762     n_read = app_recv_stream_raw (rx_fifo, buf, n, 0, peek);
1763
1764   if (svm_fifo_is_empty_cons (rx_fifo))
1765     svm_fifo_unset_event (s->rx_fifo);
1766
1767   /* Cut-through sessions might request tx notifications on rx fifos */
1768   if (PREDICT_FALSE (rx_fifo->want_deq_ntf))
1769     {
1770       app_send_io_evt_to_vpp (s->vpp_evt_q, s->rx_fifo->master_session_index,
1771                               SESSION_IO_EVT_RX, SVM_Q_WAIT);
1772       svm_fifo_reset_has_deq_ntf (s->rx_fifo);
1773     }
1774
1775   VDBG (2, "session %u[0x%llx]: read %d bytes from (%p)", s->session_index,
1776         s->vpp_handle, n_read, rx_fifo);
1777
1778   return n_read;
1779 }
1780
1781 int
1782 vppcom_session_read (uint32_t session_handle, void *buf, size_t n)
1783 {
1784   return (vppcom_session_read_internal (session_handle, buf, n, 0));
1785 }
1786
1787 static int
1788 vppcom_session_peek (uint32_t session_handle, void *buf, int n)
1789 {
1790   return (vppcom_session_read_internal (session_handle, buf, n, 1));
1791 }
1792
1793 int
1794 vppcom_session_read_segments (uint32_t session_handle,
1795                               vppcom_data_segments_t ds)
1796 {
1797   vcl_worker_t *wrk = vcl_worker_get_current ();
1798   int n_read = 0, is_nonblocking;
1799   vcl_session_t *s = 0;
1800   svm_fifo_t *rx_fifo;
1801   svm_msg_q_msg_t msg;
1802   session_event_t *e;
1803   svm_msg_q_t *mq;
1804   u8 is_ct;
1805
1806   s = vcl_session_get_w_handle (wrk, session_handle);
1807   if (PREDICT_FALSE (!s || s->is_vep))
1808     return VPPCOM_EBADFD;
1809
1810   if (PREDICT_FALSE (!vcl_session_is_open (s)))
1811     return vcl_session_closed_error (s);
1812
1813   is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
1814   is_ct = vcl_session_is_ct (s);
1815   mq = is_ct ? s->our_evt_q : wrk->app_event_queue;
1816   rx_fifo = s->rx_fifo;
1817   s->has_rx_evt = 0;
1818
1819   if (is_ct)
1820     svm_fifo_unset_event (s->rx_fifo);
1821
1822   if (svm_fifo_is_empty_cons (rx_fifo))
1823     {
1824       if (is_nonblocking)
1825         {
1826           svm_fifo_unset_event (rx_fifo);
1827           return VPPCOM_EWOULDBLOCK;
1828         }
1829       while (svm_fifo_is_empty_cons (rx_fifo))
1830         {
1831           if (vcl_session_is_closing (s))
1832             return vcl_session_closing_error (s);
1833
1834           svm_fifo_unset_event (rx_fifo);
1835           svm_msg_q_lock (mq);
1836           if (svm_msg_q_is_empty (mq))
1837             svm_msg_q_wait (mq);
1838
1839           svm_msg_q_sub_w_lock (mq, &msg);
1840           e = svm_msg_q_msg_data (mq, &msg);
1841           svm_msg_q_unlock (mq);
1842           if (!vcl_is_rx_evt_for_session (e, s->session_index, is_ct))
1843             vcl_handle_mq_event (wrk, e);
1844           svm_msg_q_free_msg (mq, &msg);
1845         }
1846     }
1847
1848   n_read = svm_fifo_segments (rx_fifo, (svm_fifo_seg_t *) ds);
1849   svm_fifo_unset_event (rx_fifo);
1850
1851   return n_read;
1852 }
1853
1854 void
1855 vppcom_session_free_segments (uint32_t session_handle,
1856                               vppcom_data_segments_t ds)
1857 {
1858   vcl_worker_t *wrk = vcl_worker_get_current ();
1859   vcl_session_t *s;
1860
1861   s = vcl_session_get_w_handle (wrk, session_handle);
1862   if (PREDICT_FALSE (!s || s->is_vep))
1863     return;
1864
1865   svm_fifo_segments_free (s->rx_fifo, (svm_fifo_seg_t *) ds);
1866 }
1867
1868 int
1869 vppcom_data_segment_copy (void *buf, vppcom_data_segments_t ds, u32 max_bytes)
1870 {
1871   u32 first_copy = clib_min (ds[0].len, max_bytes);
1872   clib_memcpy_fast (buf, ds[0].data, first_copy);
1873   if (first_copy < max_bytes)
1874     {
1875       clib_memcpy_fast (buf + first_copy, ds[1].data,
1876                         clib_min (ds[1].len, max_bytes - first_copy));
1877     }
1878   return 0;
1879 }
1880
1881 static u8
1882 vcl_is_tx_evt_for_session (session_event_t * e, u32 sid, u8 is_ct)
1883 {
1884   return (e->event_type == SESSION_IO_EVT_TX && e->session_index == sid);
1885 }
1886
1887 static inline int
1888 vppcom_session_write_inline (uint32_t session_handle, void *buf, size_t n,
1889                              u8 is_flush)
1890 {
1891   vcl_worker_t *wrk = vcl_worker_get_current ();
1892   int n_write, is_nonblocking;
1893   vcl_session_t *s = 0;
1894   session_evt_type_t et;
1895   svm_msg_q_msg_t msg;
1896   svm_fifo_t *tx_fifo;
1897   session_event_t *e;
1898   svm_msg_q_t *mq;
1899   u8 is_ct;
1900
1901   if (PREDICT_FALSE (!buf || n == 0))
1902     return VPPCOM_EINVAL;
1903
1904   s = vcl_session_get_w_handle (wrk, session_handle);
1905   if (PREDICT_FALSE (!s))
1906     return VPPCOM_EBADFD;
1907
1908   if (PREDICT_FALSE (s->is_vep))
1909     {
1910       VDBG (0, "ERROR: session %u [0x%llx]: cannot write to an epoll"
1911             " session!", s->session_index, s->vpp_handle);
1912       return VPPCOM_EBADFD;
1913     }
1914
1915   if (PREDICT_FALSE (!vcl_session_is_open (s)))
1916     {
1917       VDBG (1, "session %u [0x%llx]: is not open! state 0x%x (%s)",
1918             s->session_index, s->vpp_handle, s->session_state,
1919             vppcom_session_state_str (s->session_state));
1920       return vcl_session_closed_error (s);;
1921     }
1922
1923   is_ct = vcl_session_is_ct (s);
1924   tx_fifo = is_ct ? s->ct_tx_fifo : s->tx_fifo;
1925   is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
1926
1927   mq = wrk->app_event_queue;
1928   if (svm_fifo_is_full_prod (tx_fifo))
1929     {
1930       if (is_nonblocking)
1931         {
1932           return VPPCOM_EWOULDBLOCK;
1933         }
1934       while (svm_fifo_is_full_prod (tx_fifo))
1935         {
1936           svm_fifo_add_want_deq_ntf (tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
1937           if (vcl_session_is_closing (s))
1938             return vcl_session_closing_error (s);
1939           svm_msg_q_lock (mq);
1940           if (svm_msg_q_is_empty (mq))
1941             svm_msg_q_wait (mq);
1942
1943           svm_msg_q_sub_w_lock (mq, &msg);
1944           e = svm_msg_q_msg_data (mq, &msg);
1945           svm_msg_q_unlock (mq);
1946
1947           if (!vcl_is_tx_evt_for_session (e, s->session_index, is_ct))
1948             vcl_handle_mq_event (wrk, e);
1949           svm_msg_q_free_msg (mq, &msg);
1950         }
1951     }
1952
1953   et = SESSION_IO_EVT_TX;
1954   if (is_flush && !is_ct)
1955     et = SESSION_IO_EVT_TX_FLUSH;
1956
1957   if (s->is_dgram)
1958     n_write = app_send_dgram_raw (tx_fifo, &s->transport,
1959                                   s->vpp_evt_q, buf, n, et,
1960                                   0 /* do_evt */ , SVM_Q_WAIT);
1961   else
1962     n_write = app_send_stream_raw (tx_fifo, s->vpp_evt_q, buf, n, et,
1963                                    0 /* do_evt */ , SVM_Q_WAIT);
1964
1965   if (svm_fifo_set_event (s->tx_fifo))
1966     app_send_io_evt_to_vpp (s->vpp_evt_q, s->tx_fifo->master_session_index,
1967                             et, SVM_Q_WAIT);
1968
1969   ASSERT (n_write > 0);
1970
1971   VDBG (2, "session %u [0x%llx]: wrote %d bytes", s->session_index,
1972         s->vpp_handle, n_write);
1973
1974   return n_write;
1975 }
1976
1977 int
1978 vppcom_session_write (uint32_t session_handle, void *buf, size_t n)
1979 {
1980   return vppcom_session_write_inline (session_handle, buf, n,
1981                                       0 /* is_flush */ );
1982 }
1983
1984 int
1985 vppcom_session_write_msg (uint32_t session_handle, void *buf, size_t n)
1986 {
1987   return vppcom_session_write_inline (session_handle, buf, n,
1988                                       1 /* is_flush */ );
1989 }
1990
1991 #define vcl_fifo_rx_evt_valid_or_break(_s)                              \
1992 if (PREDICT_FALSE (!_s->rx_fifo))                                       \
1993   break;                                                                \
1994 if (PREDICT_FALSE (svm_fifo_is_empty (_s->rx_fifo)))                    \
1995   {                                                                     \
1996     if (!vcl_session_is_ct (_s))                                        \
1997       {                                                                 \
1998         svm_fifo_unset_event (_s->rx_fifo);                             \
1999         if (svm_fifo_is_empty (_s->rx_fifo))                            \
2000           break;                                                        \
2001       }                                                                 \
2002     else if (svm_fifo_is_empty (_s->ct_rx_fifo))                        \
2003       {                                                                 \
2004         svm_fifo_unset_event (_s->ct_rx_fifo);                          \
2005         if (svm_fifo_is_empty (_s->ct_rx_fifo))                         \
2006           break;                                                        \
2007       }                                                                 \
2008   }                                                                     \
2009
2010 static void
2011 vcl_select_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
2012                             unsigned long n_bits, unsigned long *read_map,
2013                             unsigned long *write_map,
2014                             unsigned long *except_map, u32 * bits_set)
2015 {
2016   session_disconnected_msg_t *disconnected_msg;
2017   session_connected_msg_t *connected_msg;
2018   vcl_session_t *session;
2019   u32 sid;
2020
2021   switch (e->event_type)
2022     {
2023     case SESSION_IO_EVT_RX:
2024       sid = e->session_index;
2025       session = vcl_session_get (wrk, sid);
2026       if (!session)
2027         break;
2028       vcl_fifo_rx_evt_valid_or_break (session);
2029       if (sid < n_bits && read_map)
2030         {
2031           clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
2032           *bits_set += 1;
2033         }
2034       break;
2035     case SESSION_IO_EVT_TX:
2036       sid = e->session_index;
2037       session = vcl_session_get (wrk, sid);
2038       if (!session)
2039         break;
2040       if (sid < n_bits && write_map)
2041         {
2042           clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
2043           *bits_set += 1;
2044         }
2045       break;
2046     case SESSION_CTRL_EVT_ACCEPTED:
2047       session = vcl_session_accepted (wrk,
2048                                       (session_accepted_msg_t *) e->data);
2049       if (!session)
2050         break;
2051       sid = session->session_index;
2052       if (sid < n_bits && read_map)
2053         {
2054           clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
2055           *bits_set += 1;
2056         }
2057       break;
2058     case SESSION_CTRL_EVT_CONNECTED:
2059       connected_msg = (session_connected_msg_t *) e->data;
2060       sid = vcl_session_connected_handler (wrk, connected_msg);
2061       if (sid == VCL_INVALID_SESSION_INDEX)
2062         break;
2063       if (sid < n_bits && write_map)
2064         {
2065           clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
2066           *bits_set += 1;
2067         }
2068       break;
2069     case SESSION_CTRL_EVT_DISCONNECTED:
2070       disconnected_msg = (session_disconnected_msg_t *) e->data;
2071       session = vcl_session_disconnected_handler (wrk, disconnected_msg);
2072       if (!session)
2073         break;
2074       sid = session->session_index;
2075       if (sid < n_bits && except_map)
2076         {
2077           clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
2078           *bits_set += 1;
2079         }
2080       break;
2081     case SESSION_CTRL_EVT_RESET:
2082       sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
2083       if (sid < n_bits && except_map)
2084         {
2085           clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
2086           *bits_set += 1;
2087         }
2088       break;
2089     case SESSION_CTRL_EVT_UNLISTEN_REPLY:
2090       vcl_session_unlisten_reply_handler (wrk, e->data);
2091       break;
2092     case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
2093       vcl_session_worker_update_reply_handler (wrk, e->data);
2094       break;
2095     case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
2096       vcl_session_req_worker_update_handler (wrk, e->data);
2097       break;
2098     default:
2099       clib_warning ("unhandled: %u", e->event_type);
2100       break;
2101     }
2102 }
2103
2104 static int
2105 vcl_select_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
2106                       unsigned long n_bits, unsigned long *read_map,
2107                       unsigned long *write_map, unsigned long *except_map,
2108                       double time_to_wait, u32 * bits_set)
2109 {
2110   svm_msg_q_msg_t *msg;
2111   session_event_t *e;
2112   u32 i;
2113
2114   svm_msg_q_lock (mq);
2115   if (svm_msg_q_is_empty (mq))
2116     {
2117       if (*bits_set)
2118         {
2119           svm_msg_q_unlock (mq);
2120           return 0;
2121         }
2122
2123       if (!time_to_wait)
2124         {
2125           svm_msg_q_unlock (mq);
2126           return 0;
2127         }
2128       else if (time_to_wait < 0)
2129         {
2130           svm_msg_q_wait (mq);
2131         }
2132       else
2133         {
2134           if (svm_msg_q_timedwait (mq, time_to_wait))
2135             {
2136               svm_msg_q_unlock (mq);
2137               return 0;
2138             }
2139         }
2140     }
2141   vcl_mq_dequeue_batch (wrk, mq, ~0);
2142   svm_msg_q_unlock (mq);
2143
2144   for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
2145     {
2146       msg = vec_elt_at_index (wrk->mq_msg_vector, i);
2147       e = svm_msg_q_msg_data (mq, msg);
2148       vcl_select_handle_mq_event (wrk, e, n_bits, read_map, write_map,
2149                                   except_map, bits_set);
2150       svm_msg_q_free_msg (mq, msg);
2151     }
2152   vec_reset_length (wrk->mq_msg_vector);
2153   vcl_handle_pending_wrk_updates (wrk);
2154   return *bits_set;
2155 }
2156
2157 static int
2158 vppcom_select_condvar (vcl_worker_t * wrk, int n_bits,
2159                        vcl_si_set * read_map, vcl_si_set * write_map,
2160                        vcl_si_set * except_map, double time_to_wait,
2161                        u32 * bits_set)
2162 {
2163   double wait = 0, start = 0;
2164
2165   if (!*bits_set)
2166     {
2167       wait = time_to_wait;
2168       start = clib_time_now (&wrk->clib_time);
2169     }
2170
2171   do
2172     {
2173       vcl_select_handle_mq (wrk, wrk->app_event_queue, n_bits, read_map,
2174                             write_map, except_map, wait, bits_set);
2175       if (*bits_set)
2176         return *bits_set;
2177       if (wait == -1)
2178         continue;
2179
2180       wait = wait - (clib_time_now (&wrk->clib_time) - start);
2181     }
2182   while (wait > 0);
2183
2184   return 0;
2185 }
2186
2187 static int
2188 vppcom_select_eventfd (vcl_worker_t * wrk, int n_bits,
2189                        vcl_si_set * read_map, vcl_si_set * write_map,
2190                        vcl_si_set * except_map, double time_to_wait,
2191                        u32 * bits_set)
2192 {
2193   vcl_mq_evt_conn_t *mqc;
2194   int __clib_unused n_read;
2195   int n_mq_evts, i;
2196   u64 buf;
2197
2198   vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
2199   n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
2200                           vec_len (wrk->mq_events), time_to_wait);
2201   for (i = 0; i < n_mq_evts; i++)
2202     {
2203       mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
2204       n_read = read (mqc->mq_fd, &buf, sizeof (buf));
2205       vcl_select_handle_mq (wrk, mqc->mq, n_bits, read_map, write_map,
2206                             except_map, 0, bits_set);
2207     }
2208
2209   return (n_mq_evts > 0 ? (int) *bits_set : 0);
2210 }
2211
2212 int
2213 vppcom_select (int n_bits, vcl_si_set * read_map, vcl_si_set * write_map,
2214                vcl_si_set * except_map, double time_to_wait)
2215 {
2216   u32 sid, minbits = clib_max (n_bits, BITS (uword)), bits_set = 0;
2217   vcl_worker_t *wrk = vcl_worker_get_current ();
2218   vcl_session_t *session = 0;
2219   int rv, i;
2220
2221   if (n_bits && read_map)
2222     {
2223       clib_bitmap_validate (wrk->rd_bitmap, minbits);
2224       clib_memcpy_fast (wrk->rd_bitmap, read_map,
2225                         vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
2226       memset (read_map, 0, vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
2227     }
2228   if (n_bits && write_map)
2229     {
2230       clib_bitmap_validate (wrk->wr_bitmap, minbits);
2231       clib_memcpy_fast (wrk->wr_bitmap, write_map,
2232                         vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
2233       memset (write_map, 0, vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
2234     }
2235   if (n_bits && except_map)
2236     {
2237       clib_bitmap_validate (wrk->ex_bitmap, minbits);
2238       clib_memcpy_fast (wrk->ex_bitmap, except_map,
2239                         vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
2240       memset (except_map, 0, vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
2241     }
2242
2243   if (!n_bits)
2244     return 0;
2245
2246   if (!write_map)
2247     goto check_rd;
2248
2249   /* *INDENT-OFF* */
2250   clib_bitmap_foreach (sid, wrk->wr_bitmap, ({
2251     if (!(session = vcl_session_get (wrk, sid)))
2252       {
2253         if (except_map && sid < minbits)
2254           clib_bitmap_set_no_check (except_map, sid, 1);
2255         continue;
2256       }
2257
2258     rv = svm_fifo_is_full_prod (session->tx_fifo);
2259     if (!rv)
2260       {
2261         clib_bitmap_set_no_check ((uword*)write_map, sid, 1);
2262         bits_set++;
2263       }
2264     else
2265       svm_fifo_add_want_deq_ntf (session->tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
2266   }));
2267
2268 check_rd:
2269   if (!read_map)
2270     goto check_mq;
2271
2272   clib_bitmap_foreach (sid, wrk->rd_bitmap, ({
2273     if (!(session = vcl_session_get (wrk, sid)))
2274       {
2275         if (except_map && sid < minbits)
2276           clib_bitmap_set_no_check (except_map, sid, 1);
2277         continue;
2278       }
2279
2280     rv = vcl_session_read_ready (session);
2281     if (rv)
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       VDBG (1, "EPOLL_CTL_ADD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2478             vep_handle, session_handle, event->events, event->data.u64);
2479       vcl_evt (VCL_EVT_EPOLL_CTLADD, session, event->events, event->data.u64);
2480       break;
2481
2482     case EPOLL_CTL_MOD:
2483       if (PREDICT_FALSE (!event))
2484         {
2485           VDBG (0, "EPOLL_CTL_MOD: NULL pointer to epoll_event structure!");
2486           rv = VPPCOM_EINVAL;
2487           goto done;
2488         }
2489       else if (PREDICT_FALSE (!session->is_vep_session))
2490         {
2491           VDBG (0, "sh %u EPOLL_CTL_MOD: not a vep session!", session_handle);
2492           rv = VPPCOM_EINVAL;
2493           goto done;
2494         }
2495       else if (PREDICT_FALSE (session->vep.vep_sh != vep_handle))
2496         {
2497           VDBG (0, "EPOLL_CTL_MOD: sh %u vep_sh (%u) != vep_sh (%u)!",
2498                 session_handle, session->vep.vep_sh, vep_handle);
2499           rv = VPPCOM_EINVAL;
2500           goto done;
2501         }
2502       session->vep.et_mask = VEP_DEFAULT_ET_MASK;
2503       session->vep.ev = *event;
2504       VDBG (1, "EPOLL_CTL_MOD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2505             vep_handle, session_handle, event->events, event->data.u64);
2506       break;
2507
2508     case EPOLL_CTL_DEL:
2509       if (PREDICT_FALSE (!session->is_vep_session))
2510         {
2511           VDBG (0, "EPOLL_CTL_DEL: %u not a vep session!", session_handle);
2512           rv = VPPCOM_EINVAL;
2513           goto done;
2514         }
2515       else if (PREDICT_FALSE (session->vep.vep_sh != vep_handle))
2516         {
2517           VDBG (0, "EPOLL_CTL_DEL: sh %u vep_sh (%u) != vep_sh (%u)!",
2518                 session_handle, session->vep.vep_sh, vep_handle);
2519           rv = VPPCOM_EINVAL;
2520           goto done;
2521         }
2522
2523       if (session->vep.prev_sh == vep_handle)
2524         vep_session->vep.next_sh = session->vep.next_sh;
2525       else
2526         {
2527           vcl_session_t *prev_session;
2528           prev_session = vcl_session_get_w_handle (wrk, session->vep.prev_sh);
2529           if (PREDICT_FALSE (!prev_session))
2530             {
2531               VDBG (0, "EPOLL_CTL_DEL: Invalid prev_sh (%u) on sh (%u)!",
2532                     session->vep.prev_sh, session_handle);
2533               return VPPCOM_EBADFD;
2534             }
2535           ASSERT (prev_session->vep.next_sh == session_handle);
2536           prev_session->vep.next_sh = session->vep.next_sh;
2537         }
2538       if (session->vep.next_sh != ~0)
2539         {
2540           vcl_session_t *next_session;
2541           next_session = vcl_session_get_w_handle (wrk, session->vep.next_sh);
2542           if (PREDICT_FALSE (!next_session))
2543             {
2544               VDBG (0, "EPOLL_CTL_DEL: Invalid next_sh (%u) on sh (%u)!",
2545                     session->vep.next_sh, session_handle);
2546               return VPPCOM_EBADFD;
2547             }
2548           ASSERT (next_session->vep.prev_sh == session_handle);
2549           next_session->vep.prev_sh = session->vep.prev_sh;
2550         }
2551
2552       memset (&session->vep, 0, sizeof (session->vep));
2553       session->vep.next_sh = ~0;
2554       session->vep.prev_sh = ~0;
2555       session->vep.vep_sh = ~0;
2556       session->is_vep_session = 0;
2557
2558       if (session->tx_fifo)
2559         svm_fifo_del_want_deq_ntf (session->tx_fifo, SVM_FIFO_NO_DEQ_NOTIF);
2560
2561       VDBG (1, "EPOLL_CTL_DEL: vep_idx %u, sh %u!", vep_handle,
2562             session_handle);
2563       vcl_evt (VCL_EVT_EPOLL_CTLDEL, session, vep_sh);
2564       break;
2565
2566     default:
2567       VDBG (0, "Invalid operation (%d)!", op);
2568       rv = VPPCOM_EINVAL;
2569     }
2570
2571   vep_verify_epoll_chain (wrk, vep_handle);
2572
2573 done:
2574   return rv;
2575 }
2576
2577 static inline void
2578 vcl_epoll_wait_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
2579                                 struct epoll_event *events, u32 * num_ev)
2580 {
2581   session_disconnected_msg_t *disconnected_msg;
2582   session_connected_msg_t *connected_msg;
2583   u32 sid = ~0, session_events;
2584   u64 session_evt_data = ~0;
2585   vcl_session_t *session;
2586   u8 add_event = 0;
2587
2588   switch (e->event_type)
2589     {
2590     case SESSION_IO_EVT_RX:
2591       sid = e->session_index;
2592       if (!(session = vcl_session_get (wrk, sid)))
2593         break;
2594       vcl_fifo_rx_evt_valid_or_break (session);
2595       session_events = session->vep.ev.events;
2596       if (!(EPOLLIN & session->vep.ev.events) || session->has_rx_evt)
2597         break;
2598       add_event = 1;
2599       events[*num_ev].events |= EPOLLIN;
2600       session_evt_data = session->vep.ev.data.u64;
2601       session->has_rx_evt = 1;
2602       break;
2603     case SESSION_IO_EVT_TX:
2604       sid = e->session_index;
2605       if (!(session = vcl_session_get (wrk, sid)))
2606         break;
2607       session_events = session->vep.ev.events;
2608       if (!(EPOLLOUT & session_events))
2609         break;
2610       add_event = 1;
2611       events[*num_ev].events |= EPOLLOUT;
2612       session_evt_data = session->vep.ev.data.u64;
2613       svm_fifo_reset_has_deq_ntf (session->tx_fifo);
2614       break;
2615     case SESSION_CTRL_EVT_ACCEPTED:
2616       session = vcl_session_accepted (wrk,
2617                                       (session_accepted_msg_t *) e->data);
2618       if (!session)
2619         break;
2620
2621       session_events = session->vep.ev.events;
2622       if (!(EPOLLIN & session_events))
2623         break;
2624
2625       add_event = 1;
2626       events[*num_ev].events |= EPOLLIN;
2627       session_evt_data = session->vep.ev.data.u64;
2628       break;
2629     case SESSION_CTRL_EVT_CONNECTED:
2630       connected_msg = (session_connected_msg_t *) e->data;
2631       sid = vcl_session_connected_handler (wrk, connected_msg);
2632       /* Generate EPOLLOUT because there's no connected event */
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       if (session->session_state & STATE_FAILED)
2642         events[*num_ev].events |= EPOLLHUP;
2643       break;
2644     case SESSION_CTRL_EVT_DISCONNECTED:
2645       disconnected_msg = (session_disconnected_msg_t *) e->data;
2646       session = vcl_session_disconnected_handler (wrk, disconnected_msg);
2647       if (!session)
2648         break;
2649       session_events = session->vep.ev.events;
2650       if (!((EPOLLHUP | EPOLLRDHUP) & session_events))
2651         break;
2652       add_event = 1;
2653       events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
2654       session_evt_data = session->vep.ev.data.u64;
2655       break;
2656     case SESSION_CTRL_EVT_RESET:
2657       sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
2658       if (!(session = vcl_session_get (wrk, sid)))
2659         break;
2660       session_events = session->vep.ev.events;
2661       if (!((EPOLLHUP | EPOLLRDHUP) & session_events))
2662         break;
2663       add_event = 1;
2664       events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
2665       session_evt_data = session->vep.ev.data.u64;
2666       break;
2667     case SESSION_CTRL_EVT_UNLISTEN_REPLY:
2668       vcl_session_unlisten_reply_handler (wrk, e->data);
2669       break;
2670     case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
2671       vcl_session_req_worker_update_handler (wrk, e->data);
2672       break;
2673     case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
2674       vcl_session_worker_update_reply_handler (wrk, e->data);
2675       break;
2676     default:
2677       VDBG (0, "unhandled: %u", e->event_type);
2678       break;
2679     }
2680
2681   if (add_event)
2682     {
2683       events[*num_ev].data.u64 = session_evt_data;
2684       if (EPOLLONESHOT & session_events)
2685         {
2686           session = vcl_session_get (wrk, sid);
2687           session->vep.ev.events = 0;
2688         }
2689       *num_ev += 1;
2690     }
2691 }
2692
2693 static int
2694 vcl_epoll_wait_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
2695                           struct epoll_event *events, u32 maxevents,
2696                           double wait_for_time, u32 * num_ev)
2697 {
2698   svm_msg_q_msg_t *msg;
2699   session_event_t *e;
2700   int i;
2701
2702   if (vec_len (wrk->mq_msg_vector) && svm_msg_q_is_empty (mq))
2703     goto handle_dequeued;
2704
2705   svm_msg_q_lock (mq);
2706   if (svm_msg_q_is_empty (mq))
2707     {
2708       if (!wait_for_time)
2709         {
2710           svm_msg_q_unlock (mq);
2711           return 0;
2712         }
2713       else if (wait_for_time < 0)
2714         {
2715           svm_msg_q_wait (mq);
2716         }
2717       else
2718         {
2719           if (svm_msg_q_timedwait (mq, wait_for_time / 1e3))
2720             {
2721               svm_msg_q_unlock (mq);
2722               return 0;
2723             }
2724         }
2725     }
2726   ASSERT (maxevents > *num_ev);
2727   vcl_mq_dequeue_batch (wrk, mq, maxevents - *num_ev);
2728   svm_msg_q_unlock (mq);
2729
2730 handle_dequeued:
2731   for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
2732     {
2733       msg = vec_elt_at_index (wrk->mq_msg_vector, i);
2734       e = svm_msg_q_msg_data (mq, msg);
2735       vcl_epoll_wait_handle_mq_event (wrk, e, events, num_ev);
2736       svm_msg_q_free_msg (mq, msg);
2737     }
2738   vec_reset_length (wrk->mq_msg_vector);
2739   vcl_handle_pending_wrk_updates (wrk);
2740   return *num_ev;
2741 }
2742
2743 static int
2744 vppcom_epoll_wait_condvar (vcl_worker_t * wrk, struct epoll_event *events,
2745                            int maxevents, u32 n_evts, double wait_for_time)
2746 {
2747   double wait = 0, start = 0, now;
2748
2749   if (!n_evts)
2750     {
2751       wait = wait_for_time;
2752       start = clib_time_now (&wrk->clib_time);
2753     }
2754
2755   do
2756     {
2757       vcl_epoll_wait_handle_mq (wrk, wrk->app_event_queue, events, maxevents,
2758                                 wait, &n_evts);
2759       if (n_evts)
2760         return n_evts;
2761       if (wait == -1)
2762         continue;
2763
2764       now = clib_time_now (&wrk->clib_time);
2765       wait -= now - start;
2766       start = now;
2767     }
2768   while (wait > 0);
2769
2770   return 0;
2771 }
2772
2773 static int
2774 vppcom_epoll_wait_eventfd (vcl_worker_t * wrk, struct epoll_event *events,
2775                            int maxevents, u32 n_evts, double wait_for_time)
2776 {
2777   vcl_mq_evt_conn_t *mqc;
2778   int __clib_unused n_read;
2779   int n_mq_evts, i;
2780   u64 buf;
2781
2782   vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
2783 again:
2784   n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
2785                           vec_len (wrk->mq_events), wait_for_time);
2786   for (i = 0; i < n_mq_evts; i++)
2787     {
2788       mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
2789       n_read = read (mqc->mq_fd, &buf, sizeof (buf));
2790       vcl_epoll_wait_handle_mq (wrk, mqc->mq, events, maxevents, 0, &n_evts);
2791     }
2792   if (!n_evts && n_mq_evts > 0)
2793     goto again;
2794
2795   return (int) n_evts;
2796 }
2797
2798 int
2799 vppcom_epoll_wait (uint32_t vep_handle, struct epoll_event *events,
2800                    int maxevents, double wait_for_time)
2801 {
2802   vcl_worker_t *wrk = vcl_worker_get_current ();
2803   vcl_session_t *vep_session;
2804   u32 n_evts = 0;
2805   int i;
2806
2807   if (PREDICT_FALSE (maxevents <= 0))
2808     {
2809       VDBG (0, "ERROR: Invalid maxevents (%d)!", maxevents);
2810       return VPPCOM_EINVAL;
2811     }
2812
2813   vep_session = vcl_session_get_w_handle (wrk, vep_handle);
2814   if (!vep_session)
2815     return VPPCOM_EBADFD;
2816
2817   if (PREDICT_FALSE (!vep_session->is_vep))
2818     {
2819       VDBG (0, "ERROR: vep_idx (%u) is not a vep!", vep_handle);
2820       return VPPCOM_EINVAL;
2821     }
2822
2823   memset (events, 0, sizeof (*events) * maxevents);
2824
2825   if (vec_len (wrk->unhandled_evts_vector))
2826     {
2827       for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
2828         {
2829           vcl_epoll_wait_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i],
2830                                           events, &n_evts);
2831           if (n_evts == maxevents)
2832             {
2833               vec_delete (wrk->unhandled_evts_vector, i + 1, 0);
2834               return n_evts;
2835             }
2836         }
2837       vec_reset_length (wrk->unhandled_evts_vector);
2838     }
2839
2840   if (vcm->cfg.use_mq_eventfd)
2841     return vppcom_epoll_wait_eventfd (wrk, events, maxevents, n_evts,
2842                                       wait_for_time);
2843
2844   return vppcom_epoll_wait_condvar (wrk, events, maxevents, n_evts,
2845                                     wait_for_time);
2846 }
2847
2848 int
2849 vppcom_session_attr (uint32_t session_handle, uint32_t op,
2850                      void *buffer, uint32_t * buflen)
2851 {
2852   vcl_worker_t *wrk = vcl_worker_get_current ();
2853   vcl_session_t *session;
2854   int rv = VPPCOM_OK;
2855   u32 *flags = buffer, tmp_flags = 0;
2856   vppcom_endpt_t *ep = buffer;
2857
2858   session = vcl_session_get_w_handle (wrk, session_handle);
2859   if (!session)
2860     return VPPCOM_EBADFD;
2861
2862   switch (op)
2863     {
2864     case VPPCOM_ATTR_GET_NREAD:
2865       rv = vcl_session_read_ready (session);
2866       VDBG (2, "VPPCOM_ATTR_GET_NREAD: sh %u, nread = %d", session_handle,
2867             rv);
2868       break;
2869
2870     case VPPCOM_ATTR_GET_NWRITE:
2871       rv = vcl_session_write_ready (session);
2872       VDBG (2, "VPPCOM_ATTR_GET_NWRITE: sh %u, nwrite = %d", session_handle,
2873             rv);
2874       break;
2875
2876     case VPPCOM_ATTR_GET_FLAGS:
2877       if (PREDICT_TRUE (buffer && buflen && (*buflen >= sizeof (*flags))))
2878         {
2879           *flags = O_RDWR | (VCL_SESS_ATTR_TEST (session->attr,
2880                                                  VCL_SESS_ATTR_NONBLOCK));
2881           *buflen = sizeof (*flags);
2882           VDBG (2, "VPPCOM_ATTR_GET_FLAGS: sh %u, flags = 0x%08x, "
2883                 "is_nonblocking = %u", session_handle, *flags,
2884                 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK));
2885         }
2886       else
2887         rv = VPPCOM_EINVAL;
2888       break;
2889
2890     case VPPCOM_ATTR_SET_FLAGS:
2891       if (PREDICT_TRUE (buffer && buflen && (*buflen == sizeof (*flags))))
2892         {
2893           if (*flags & O_NONBLOCK)
2894             VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK);
2895           else
2896             VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_NONBLOCK);
2897
2898           VDBG (2, "VPPCOM_ATTR_SET_FLAGS: sh %u, flags = 0x%08x,"
2899                 " is_nonblocking = %u", session_handle, *flags,
2900                 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK));
2901         }
2902       else
2903         rv = VPPCOM_EINVAL;
2904       break;
2905
2906     case VPPCOM_ATTR_GET_PEER_ADDR:
2907       if (PREDICT_TRUE (buffer && buflen &&
2908                         (*buflen >= sizeof (*ep)) && ep->ip))
2909         {
2910           ep->is_ip4 = session->transport.is_ip4;
2911           ep->port = session->transport.rmt_port;
2912           if (session->transport.is_ip4)
2913             clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
2914                               sizeof (ip4_address_t));
2915           else
2916             clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
2917                               sizeof (ip6_address_t));
2918           *buflen = sizeof (*ep);
2919           VDBG (1, "VPPCOM_ATTR_GET_PEER_ADDR: sh %u, is_ip4 = %u, "
2920                 "addr = %U, port %u", session_handle, ep->is_ip4,
2921                 format_ip46_address, &session->transport.rmt_ip,
2922                 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
2923                 clib_net_to_host_u16 (ep->port));
2924         }
2925       else
2926         rv = VPPCOM_EINVAL;
2927       break;
2928
2929     case VPPCOM_ATTR_GET_LCL_ADDR:
2930       if (PREDICT_TRUE (buffer && buflen &&
2931                         (*buflen >= sizeof (*ep)) && ep->ip))
2932         {
2933           ep->is_ip4 = session->transport.is_ip4;
2934           ep->port = session->transport.lcl_port;
2935           if (session->transport.is_ip4)
2936             clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip4,
2937                               sizeof (ip4_address_t));
2938           else
2939             clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip6,
2940                               sizeof (ip6_address_t));
2941           *buflen = sizeof (*ep);
2942           VDBG (1, "VPPCOM_ATTR_GET_LCL_ADDR: sh %u, is_ip4 = %u, addr = %U"
2943                 " port %d", session_handle, ep->is_ip4, format_ip46_address,
2944                 &session->transport.lcl_ip,
2945                 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
2946                 clib_net_to_host_u16 (ep->port));
2947         }
2948       else
2949         rv = VPPCOM_EINVAL;
2950       break;
2951
2952     case VPPCOM_ATTR_SET_LCL_ADDR:
2953       if (PREDICT_TRUE (buffer && buflen &&
2954                         (*buflen >= sizeof (*ep)) && ep->ip))
2955         {
2956           session->transport.is_ip4 = ep->is_ip4;
2957           session->transport.lcl_port = ep->port;
2958           vcl_ip_copy_from_ep (&session->transport.lcl_ip, ep);
2959           *buflen = sizeof (*ep);
2960           VDBG (1, "VPPCOM_ATTR_SET_LCL_ADDR: sh %u, is_ip4 = %u, addr = %U"
2961                 " port %d", session_handle, ep->is_ip4, format_ip46_address,
2962                 &session->transport.lcl_ip,
2963                 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
2964                 clib_net_to_host_u16 (ep->port));
2965         }
2966       else
2967         rv = VPPCOM_EINVAL;
2968       break;
2969
2970     case VPPCOM_ATTR_GET_LIBC_EPFD:
2971       rv = session->libc_epfd;
2972       VDBG (2, "VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd %d", rv);
2973       break;
2974
2975     case VPPCOM_ATTR_SET_LIBC_EPFD:
2976       if (PREDICT_TRUE (buffer && buflen &&
2977                         (*buflen == sizeof (session->libc_epfd))))
2978         {
2979           session->libc_epfd = *(int *) buffer;
2980           *buflen = sizeof (session->libc_epfd);
2981
2982           VDBG (2, "VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd %d, buflen %d",
2983                 session->libc_epfd, *buflen);
2984         }
2985       else
2986         rv = VPPCOM_EINVAL;
2987       break;
2988
2989     case VPPCOM_ATTR_GET_PROTOCOL:
2990       if (buffer && buflen && (*buflen >= sizeof (int)))
2991         {
2992           *(int *) buffer = session->session_type;
2993           *buflen = sizeof (int);
2994
2995           VDBG (2, "VPPCOM_ATTR_GET_PROTOCOL: %d (%s), buflen %d",
2996                 *(int *) buffer, *(int *) buffer ? "UDP" : "TCP", *buflen);
2997         }
2998       else
2999         rv = VPPCOM_EINVAL;
3000       break;
3001
3002     case VPPCOM_ATTR_GET_LISTEN:
3003       if (buffer && buflen && (*buflen >= sizeof (int)))
3004         {
3005           *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3006                                                 VCL_SESS_ATTR_LISTEN);
3007           *buflen = sizeof (int);
3008
3009           VDBG (2, "VPPCOM_ATTR_GET_LISTEN: %d, buflen %d", *(int *) buffer,
3010                 *buflen);
3011         }
3012       else
3013         rv = VPPCOM_EINVAL;
3014       break;
3015
3016     case VPPCOM_ATTR_GET_ERROR:
3017       if (buffer && buflen && (*buflen >= sizeof (int)))
3018         {
3019           *(int *) buffer = 0;
3020           *buflen = sizeof (int);
3021
3022           VDBG (2, "VPPCOM_ATTR_GET_ERROR: %d, buflen %d, #VPP-TBD#",
3023                 *(int *) buffer, *buflen);
3024         }
3025       else
3026         rv = VPPCOM_EINVAL;
3027       break;
3028
3029     case VPPCOM_ATTR_GET_TX_FIFO_LEN:
3030       if (buffer && buflen && (*buflen >= sizeof (u32)))
3031         {
3032
3033           /* VPP-TBD */
3034           *(size_t *) buffer = (session->sndbuf_size ? session->sndbuf_size :
3035                                 session->tx_fifo ? session->tx_fifo->nitems :
3036                                 vcm->cfg.tx_fifo_size);
3037           *buflen = sizeof (u32);
3038
3039           VDBG (2, "VPPCOM_ATTR_GET_TX_FIFO_LEN: %u (0x%x), buflen %d,"
3040                 " #VPP-TBD#", *(size_t *) buffer, *(size_t *) buffer,
3041                 *buflen);
3042         }
3043       else
3044         rv = VPPCOM_EINVAL;
3045       break;
3046
3047     case VPPCOM_ATTR_SET_TX_FIFO_LEN:
3048       if (buffer && buflen && (*buflen == sizeof (u32)))
3049         {
3050           /* VPP-TBD */
3051           session->sndbuf_size = *(u32 *) buffer;
3052           VDBG (2, "VPPCOM_ATTR_SET_TX_FIFO_LEN: %u (0x%x), buflen %d,"
3053                 " #VPP-TBD#", session->sndbuf_size, session->sndbuf_size,
3054                 *buflen);
3055         }
3056       else
3057         rv = VPPCOM_EINVAL;
3058       break;
3059
3060     case VPPCOM_ATTR_GET_RX_FIFO_LEN:
3061       if (buffer && buflen && (*buflen >= sizeof (u32)))
3062         {
3063
3064           /* VPP-TBD */
3065           *(size_t *) buffer = (session->rcvbuf_size ? session->rcvbuf_size :
3066                                 session->rx_fifo ? session->rx_fifo->nitems :
3067                                 vcm->cfg.rx_fifo_size);
3068           *buflen = sizeof (u32);
3069
3070           VDBG (2, "VPPCOM_ATTR_GET_RX_FIFO_LEN: %u (0x%x), buflen %d, "
3071                 "#VPP-TBD#", *(size_t *) buffer, *(size_t *) buffer, *buflen);
3072         }
3073       else
3074         rv = VPPCOM_EINVAL;
3075       break;
3076
3077     case VPPCOM_ATTR_SET_RX_FIFO_LEN:
3078       if (buffer && buflen && (*buflen == sizeof (u32)))
3079         {
3080           /* VPP-TBD */
3081           session->rcvbuf_size = *(u32 *) buffer;
3082           VDBG (2, "VPPCOM_ATTR_SET_RX_FIFO_LEN: %u (0x%x), buflen %d,"
3083                 " #VPP-TBD#", session->sndbuf_size, session->sndbuf_size,
3084                 *buflen);
3085         }
3086       else
3087         rv = VPPCOM_EINVAL;
3088       break;
3089
3090     case VPPCOM_ATTR_GET_REUSEADDR:
3091       if (buffer && buflen && (*buflen >= sizeof (int)))
3092         {
3093           /* VPP-TBD */
3094           *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3095                                                 VCL_SESS_ATTR_REUSEADDR);
3096           *buflen = sizeof (int);
3097
3098           VDBG (2, "VPPCOM_ATTR_GET_REUSEADDR: %d, buflen %d, #VPP-TBD#",
3099                 *(int *) buffer, *buflen);
3100         }
3101       else
3102         rv = VPPCOM_EINVAL;
3103       break;
3104
3105     case VPPCOM_ATTR_SET_REUSEADDR:
3106       if (buffer && buflen && (*buflen == sizeof (int)) &&
3107           !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
3108         {
3109           /* VPP-TBD */
3110           if (*(int *) buffer)
3111             VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEADDR);
3112           else
3113             VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEADDR);
3114
3115           VDBG (2, "VPPCOM_ATTR_SET_REUSEADDR: %d, buflen %d, #VPP-TBD#",
3116                 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_REUSEADDR),
3117                 *buflen);
3118         }
3119       else
3120         rv = VPPCOM_EINVAL;
3121       break;
3122
3123     case VPPCOM_ATTR_GET_REUSEPORT:
3124       if (buffer && buflen && (*buflen >= sizeof (int)))
3125         {
3126           /* VPP-TBD */
3127           *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3128                                                 VCL_SESS_ATTR_REUSEPORT);
3129           *buflen = sizeof (int);
3130
3131           VDBG (2, "VPPCOM_ATTR_GET_REUSEPORT: %d, buflen %d, #VPP-TBD#",
3132                 *(int *) buffer, *buflen);
3133         }
3134       else
3135         rv = VPPCOM_EINVAL;
3136       break;
3137
3138     case VPPCOM_ATTR_SET_REUSEPORT:
3139       if (buffer && buflen && (*buflen == sizeof (int)) &&
3140           !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
3141         {
3142           /* VPP-TBD */
3143           if (*(int *) buffer)
3144             VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEPORT);
3145           else
3146             VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEPORT);
3147
3148           VDBG (2, "VPPCOM_ATTR_SET_REUSEPORT: %d, buflen %d, #VPP-TBD#",
3149                 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_REUSEPORT),
3150                 *buflen);
3151         }
3152       else
3153         rv = VPPCOM_EINVAL;
3154       break;
3155
3156     case VPPCOM_ATTR_GET_BROADCAST:
3157       if (buffer && buflen && (*buflen >= sizeof (int)))
3158         {
3159           /* VPP-TBD */
3160           *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3161                                                 VCL_SESS_ATTR_BROADCAST);
3162           *buflen = sizeof (int);
3163
3164           VDBG (2, "VPPCOM_ATTR_GET_BROADCAST: %d, buflen %d, #VPP-TBD#",
3165                 *(int *) buffer, *buflen);
3166         }
3167       else
3168         rv = VPPCOM_EINVAL;
3169       break;
3170
3171     case VPPCOM_ATTR_SET_BROADCAST:
3172       if (buffer && buflen && (*buflen == sizeof (int)))
3173         {
3174           /* VPP-TBD */
3175           if (*(int *) buffer)
3176             VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_BROADCAST);
3177           else
3178             VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_BROADCAST);
3179
3180           VDBG (2, "VPPCOM_ATTR_SET_BROADCAST: %d, buflen %d, #VPP-TBD#",
3181                 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_BROADCAST),
3182                 *buflen);
3183         }
3184       else
3185         rv = VPPCOM_EINVAL;
3186       break;
3187
3188     case VPPCOM_ATTR_GET_V6ONLY:
3189       if (buffer && buflen && (*buflen >= sizeof (int)))
3190         {
3191           /* VPP-TBD */
3192           *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3193                                                 VCL_SESS_ATTR_V6ONLY);
3194           *buflen = sizeof (int);
3195
3196           VDBG (2, "VPPCOM_ATTR_GET_V6ONLY: %d, buflen %d, #VPP-TBD#",
3197                 *(int *) buffer, *buflen);
3198         }
3199       else
3200         rv = VPPCOM_EINVAL;
3201       break;
3202
3203     case VPPCOM_ATTR_SET_V6ONLY:
3204       if (buffer && buflen && (*buflen == sizeof (int)))
3205         {
3206           /* VPP-TBD */
3207           if (*(int *) buffer)
3208             VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_V6ONLY);
3209           else
3210             VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_V6ONLY);
3211
3212           VDBG (2, "VPPCOM_ATTR_SET_V6ONLY: %d, buflen %d, #VPP-TBD#",
3213                 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_V6ONLY),
3214                 *buflen);
3215         }
3216       else
3217         rv = VPPCOM_EINVAL;
3218       break;
3219
3220     case VPPCOM_ATTR_GET_KEEPALIVE:
3221       if (buffer && buflen && (*buflen >= sizeof (int)))
3222         {
3223           /* VPP-TBD */
3224           *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3225                                                 VCL_SESS_ATTR_KEEPALIVE);
3226           *buflen = sizeof (int);
3227
3228           VDBG (2, "VPPCOM_ATTR_GET_KEEPALIVE: %d, buflen %d, #VPP-TBD#",
3229                 *(int *) buffer, *buflen);
3230         }
3231       else
3232         rv = VPPCOM_EINVAL;
3233       break;
3234
3235     case VPPCOM_ATTR_SET_KEEPALIVE:
3236       if (buffer && buflen && (*buflen == sizeof (int)))
3237         {
3238           /* VPP-TBD */
3239           if (*(int *) buffer)
3240             VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_KEEPALIVE);
3241           else
3242             VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_KEEPALIVE);
3243
3244           VDBG (2, "VPPCOM_ATTR_SET_KEEPALIVE: %d, buflen %d, #VPP-TBD#",
3245                 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_KEEPALIVE),
3246                 *buflen);
3247         }
3248       else
3249         rv = VPPCOM_EINVAL;
3250       break;
3251
3252     case VPPCOM_ATTR_GET_TCP_NODELAY:
3253       if (buffer && buflen && (*buflen >= sizeof (int)))
3254         {
3255           /* VPP-TBD */
3256           *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3257                                                 VCL_SESS_ATTR_TCP_NODELAY);
3258           *buflen = sizeof (int);
3259
3260           VDBG (2, "VPPCOM_ATTR_GET_TCP_NODELAY: %d, buflen %d, #VPP-TBD#",
3261                 *(int *) buffer, *buflen);
3262         }
3263       else
3264         rv = VPPCOM_EINVAL;
3265       break;
3266
3267     case VPPCOM_ATTR_SET_TCP_NODELAY:
3268       if (buffer && buflen && (*buflen == sizeof (int)))
3269         {
3270           /* VPP-TBD */
3271           if (*(int *) buffer)
3272             VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
3273           else
3274             VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
3275
3276           VDBG (2, "VPPCOM_ATTR_SET_TCP_NODELAY: %d, buflen %d, #VPP-TBD#",
3277                 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_TCP_NODELAY),
3278                 *buflen);
3279         }
3280       else
3281         rv = VPPCOM_EINVAL;
3282       break;
3283
3284     case VPPCOM_ATTR_GET_TCP_KEEPIDLE:
3285       if (buffer && buflen && (*buflen >= sizeof (int)))
3286         {
3287           /* VPP-TBD */
3288           *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3289                                                 VCL_SESS_ATTR_TCP_KEEPIDLE);
3290           *buflen = sizeof (int);
3291
3292           VDBG (2, "VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d, buflen %d, #VPP-TBD#",
3293                 *(int *) buffer, *buflen);
3294         }
3295       else
3296         rv = VPPCOM_EINVAL;
3297       break;
3298
3299     case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
3300       if (buffer && buflen && (*buflen == sizeof (int)))
3301         {
3302           /* VPP-TBD */
3303           if (*(int *) buffer)
3304             VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
3305           else
3306             VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
3307
3308           VDBG (2, "VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d, buflen %d, #VPP-TBD#",
3309                 VCL_SESS_ATTR_TEST (session->attr,
3310                                     VCL_SESS_ATTR_TCP_KEEPIDLE), *buflen);
3311         }
3312       else
3313         rv = VPPCOM_EINVAL;
3314       break;
3315
3316     case VPPCOM_ATTR_GET_TCP_KEEPINTVL:
3317       if (buffer && buflen && (*buflen >= sizeof (int)))
3318         {
3319           /* VPP-TBD */
3320           *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3321                                                 VCL_SESS_ATTR_TCP_KEEPINTVL);
3322           *buflen = sizeof (int);
3323
3324           VDBG (2, "VPPCOM_ATTR_GET_TCP_KEEPINTVL: %d, buflen %d, #VPP-TBD#",
3325                 *(int *) buffer, *buflen);
3326         }
3327       else
3328         rv = VPPCOM_EINVAL;
3329       break;
3330
3331     case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
3332       if (buffer && buflen && (*buflen == sizeof (int)))
3333         {
3334           /* VPP-TBD */
3335           if (*(int *) buffer)
3336             VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
3337           else
3338             VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
3339
3340           VDBG (2, "VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d, buflen %d, #VPP-TBD#",
3341                 VCL_SESS_ATTR_TEST (session->attr,
3342                                     VCL_SESS_ATTR_TCP_KEEPINTVL), *buflen);
3343         }
3344       else
3345         rv = VPPCOM_EINVAL;
3346       break;
3347
3348     case VPPCOM_ATTR_GET_TCP_USER_MSS:
3349       if (buffer && buflen && (*buflen >= sizeof (u32)))
3350         {
3351           /* VPP-TBD */
3352           *(u32 *) buffer = session->user_mss;
3353           *buflen = sizeof (int);
3354
3355           VDBG (2, "VPPCOM_ATTR_GET_TCP_USER_MSS: %d, buflen %d, #VPP-TBD#",
3356                 *(int *) buffer, *buflen);
3357         }
3358       else
3359         rv = VPPCOM_EINVAL;
3360       break;
3361
3362     case VPPCOM_ATTR_SET_TCP_USER_MSS:
3363       if (buffer && buflen && (*buflen == sizeof (u32)))
3364         {
3365           /* VPP-TBD */
3366           session->user_mss = *(u32 *) buffer;
3367
3368           VDBG (2, "VPPCOM_ATTR_SET_TCP_USER_MSS: %u, buflen %d, #VPP-TBD#",
3369                 session->user_mss, *buflen);
3370         }
3371       else
3372         rv = VPPCOM_EINVAL;
3373       break;
3374
3375     case VPPCOM_ATTR_SET_SHUT:
3376       if (*flags == SHUT_RD || *flags == SHUT_RDWR)
3377         VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_SHUT_RD);
3378       if (*flags == SHUT_WR || *flags == SHUT_RDWR)
3379         VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_SHUT_WR);
3380       break;
3381
3382     case VPPCOM_ATTR_GET_SHUT:
3383       if (VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_SHUT_RD))
3384         tmp_flags = 1;
3385       if (VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_SHUT_WR))
3386         tmp_flags |= 2;
3387       if (tmp_flags == 1)
3388         *(int *) buffer = SHUT_RD;
3389       else if (tmp_flags == 2)
3390         *(int *) buffer = SHUT_WR;
3391       else if (tmp_flags == 3)
3392         *(int *) buffer = SHUT_RDWR;
3393       *buflen = sizeof (int);
3394       break;
3395     default:
3396       rv = VPPCOM_EINVAL;
3397       break;
3398     }
3399
3400   return rv;
3401 }
3402
3403 int
3404 vppcom_session_recvfrom (uint32_t session_handle, void *buffer,
3405                          uint32_t buflen, int flags, vppcom_endpt_t * ep)
3406 {
3407   vcl_worker_t *wrk = vcl_worker_get_current ();
3408   int rv = VPPCOM_OK;
3409   vcl_session_t *session = 0;
3410
3411   if (ep)
3412     {
3413       session = vcl_session_get_w_handle (wrk, session_handle);
3414       if (PREDICT_FALSE (!session))
3415         {
3416           VDBG (0, "sh 0x%llx is closed!", session_handle);
3417           return VPPCOM_EBADFD;
3418         }
3419       ep->is_ip4 = session->transport.is_ip4;
3420       ep->port = session->transport.rmt_port;
3421     }
3422
3423   if (flags == 0)
3424     rv = vppcom_session_read (session_handle, buffer, buflen);
3425   else if (flags & MSG_PEEK)
3426     rv = vppcom_session_peek (session_handle, buffer, buflen);
3427   else
3428     {
3429       VDBG (0, "Unsupport flags for recvfrom %d", flags);
3430       return VPPCOM_EAFNOSUPPORT;
3431     }
3432
3433   if (ep)
3434     {
3435       if (session->transport.is_ip4)
3436         clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
3437                           sizeof (ip4_address_t));
3438       else
3439         clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
3440                           sizeof (ip6_address_t));
3441     }
3442
3443   return rv;
3444 }
3445
3446 int
3447 vppcom_session_sendto (uint32_t session_handle, void *buffer,
3448                        uint32_t buflen, int flags, vppcom_endpt_t * ep)
3449 {
3450   if (!buffer)
3451     return VPPCOM_EINVAL;
3452
3453   if (ep)
3454     {
3455       // TBD
3456       return VPPCOM_EINVAL;
3457     }
3458
3459   if (flags)
3460     {
3461       // TBD check the flags and do the right thing
3462       VDBG (2, "handling flags 0x%u (%d) not implemented yet.", flags, flags);
3463     }
3464
3465   return (vppcom_session_write_inline (session_handle, buffer, buflen, 1));
3466 }
3467
3468 int
3469 vppcom_poll (vcl_poll_t * vp, uint32_t n_sids, double wait_for_time)
3470 {
3471   vcl_worker_t *wrk = vcl_worker_get_current ();
3472   f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
3473   u32 i, keep_trying = 1;
3474   svm_msg_q_msg_t msg;
3475   session_event_t *e;
3476   int rv, num_ev = 0;
3477
3478   VDBG (3, "vp %p, nsids %u, wait_for_time %f", vp, n_sids, wait_for_time);
3479
3480   if (!vp)
3481     return VPPCOM_EFAULT;
3482
3483   do
3484     {
3485       vcl_session_t *session;
3486
3487       /* Dequeue all events and drop all unhandled io events */
3488       while (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0) == 0)
3489         {
3490           e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
3491           vcl_handle_mq_event (wrk, e);
3492           svm_msg_q_free_msg (wrk->app_event_queue, &msg);
3493         }
3494       vec_reset_length (wrk->unhandled_evts_vector);
3495
3496       for (i = 0; i < n_sids; i++)
3497         {
3498           session = vcl_session_get (wrk, vp[i].sh);
3499           if (!session)
3500             {
3501               vp[i].revents = POLLHUP;
3502               num_ev++;
3503               continue;
3504             }
3505
3506           vp[i].revents = 0;
3507
3508           if (POLLIN & vp[i].events)
3509             {
3510               rv = vcl_session_read_ready (session);
3511               if (rv > 0)
3512                 {
3513                   vp[i].revents |= POLLIN;
3514                   num_ev++;
3515                 }
3516               else if (rv < 0)
3517                 {
3518                   switch (rv)
3519                     {
3520                     case VPPCOM_ECONNRESET:
3521                       vp[i].revents = POLLHUP;
3522                       break;
3523
3524                     default:
3525                       vp[i].revents = POLLERR;
3526                       break;
3527                     }
3528                   num_ev++;
3529                 }
3530             }
3531
3532           if (POLLOUT & vp[i].events)
3533             {
3534               rv = vcl_session_write_ready (session);
3535               if (rv > 0)
3536                 {
3537                   vp[i].revents |= POLLOUT;
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 (0)                // Note "done:" label used by VCL_SESSION_LOCK_AND_GET()
3557             {
3558               vp[i].revents = POLLNVAL;
3559               num_ev++;
3560             }
3561         }
3562       if (wait_for_time != -1)
3563         keep_trying = (clib_time_now (&wrk->clib_time) <= timeout) ? 1 : 0;
3564     }
3565   while ((num_ev == 0) && keep_trying);
3566
3567   return num_ev;
3568 }
3569
3570 int
3571 vppcom_mq_epoll_fd (void)
3572 {
3573   vcl_worker_t *wrk = vcl_worker_get_current ();
3574   return wrk->mqs_epfd;
3575 }
3576
3577 int
3578 vppcom_session_index (vcl_session_handle_t session_handle)
3579 {
3580   return session_handle & 0xFFFFFF;
3581 }
3582
3583 int
3584 vppcom_session_worker (vcl_session_handle_t session_handle)
3585 {
3586   return session_handle >> 24;
3587 }
3588
3589 int
3590 vppcom_worker_register (void)
3591 {
3592   vcl_worker_t *wrk;
3593   u8 *wrk_name = 0;
3594   int rv;
3595
3596   if (!vcl_worker_alloc_and_init ())
3597     return VPPCOM_EEXIST;
3598
3599   wrk = vcl_worker_get_current ();
3600   wrk_name = format (0, "%s-wrk-%u", vcm->app_name, wrk->wrk_index);
3601
3602   rv = vppcom_connect_to_vpp ((char *) wrk_name);
3603   vec_free (wrk_name);
3604
3605   if (rv)
3606     return VPPCOM_EFAULT;
3607
3608   if (vcl_worker_register_with_vpp ())
3609     return VPPCOM_EEXIST;
3610
3611   return VPPCOM_OK;
3612 }
3613
3614 void
3615 vppcom_worker_unregister (void)
3616 {
3617   vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
3618   vcl_set_worker_index (~0);
3619 }
3620
3621 int
3622 vppcom_worker_index (void)
3623 {
3624   return vcl_get_worker_index ();
3625 }
3626
3627 int
3628 vppcom_worker_mqs_epfd (void)
3629 {
3630   vcl_worker_t *wrk = vcl_worker_get_current ();
3631   if (!vcm->cfg.use_mq_eventfd)
3632     return -1;
3633   return wrk->mqs_epfd;
3634 }
3635
3636 int
3637 vppcom_session_is_connectable_listener (uint32_t session_handle)
3638 {
3639   vcl_session_t *session;
3640   vcl_worker_t *wrk = vcl_worker_get_current ();
3641   session = vcl_session_get_w_handle (wrk, session_handle);
3642   if (!session)
3643     return VPPCOM_EBADFD;
3644   return vcl_session_is_connectable_listener (wrk, session);
3645 }
3646
3647 int
3648 vppcom_session_listener (uint32_t session_handle)
3649 {
3650   vcl_worker_t *wrk = vcl_worker_get_current ();
3651   vcl_session_t *listen_session, *session;
3652   session = vcl_session_get_w_handle (wrk, session_handle);
3653   if (!session)
3654     return VPPCOM_EBADFD;
3655   if (session->listener_index == VCL_INVALID_SESSION_INDEX)
3656     return VPPCOM_EBADFD;
3657   listen_session = vcl_session_get_w_handle (wrk, session->listener_index);
3658   if (!listen_session)
3659     return VPPCOM_EBADFD;
3660   return vcl_session_handle (listen_session);
3661 }
3662
3663 int
3664 vppcom_session_n_accepted (uint32_t session_handle)
3665 {
3666   vcl_worker_t *wrk = vcl_worker_get_current ();
3667   vcl_session_t *session = vcl_session_get_w_handle (wrk, session_handle);
3668   if (!session)
3669     return VPPCOM_EBADFD;
3670   return session->n_accepted_sessions;
3671 }
3672
3673 /*
3674  * fd.io coding-style-patch-verification: ON
3675  *
3676  * Local Variables:
3677  * eval: (c-set-style "gnu")
3678  * End:
3679  */