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