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