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