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