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