2 * Copyright (c) 2017-2019 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <vcl/vppcom.h>
19 #include <vcl/vcl_debug.h>
20 #include <vcl/vcl_private.h>
21 #include <svm/fifo_segment.h>
23 __thread uword __vcl_worker_index = ~0;
26 vcl_segment_is_not_mounted (vcl_worker_t * wrk, u64 segment_handle)
30 if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
33 segment_index = vcl_segment_table_lookup (segment_handle);
34 if (segment_index != VCL_INVALID_SEGMENT_INDEX)
41 vcl_mq_dequeue_batch (vcl_worker_t * wrk, svm_msg_q_t * mq, u32 n_max_msg)
47 n_msgs = clib_min (svm_msg_q_size (mq), n_max_msg);
48 for (i = 0; i < n_msgs; i++)
50 vec_add2 (wrk->mq_msg_vector, msg, 1);
51 svm_msg_q_sub_w_lock (mq, msg);
57 vppcom_session_state_str (vcl_session_state_t state)
79 case STATE_VPP_CLOSING:
80 st = "STATE_VPP_CLOSING";
83 case STATE_DISCONNECT:
84 st = "STATE_DISCONNECT";
88 st = "STATE_DETACHED";
95 case STATE_LISTEN_NO_MQ:
96 st = "STATE_LISTEN_NO_MQ";
100 st = "UNKNOWN_STATE";
108 format_ip4_address (u8 * s, va_list * args)
110 u8 *a = va_arg (*args, u8 *);
111 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
115 format_ip6_address (u8 * s, va_list * args)
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;
120 i_max_n_zero = ARRAY_LEN (a->as_u16);
122 i_first_zero = i_max_n_zero;
124 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
126 u32 is_zero = a->as_u16[i] == 0;
127 if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
133 if ((!is_zero && n_zeros > max_n_zeros)
134 || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
136 i_max_n_zero = i_first_zero;
137 max_n_zeros = n_zeros;
138 i_first_zero = ARRAY_LEN (a->as_u16);
143 last_double_colon = 0;
144 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
146 if (i == i_max_n_zero && max_n_zeros > 1)
148 s = format (s, "::");
149 i += max_n_zeros - 1;
150 last_double_colon = 1;
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;
164 /* Format an IP46 address. */
166 format_ip46_address (u8 * s, va_list * args)
168 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
169 ip46_type_t type = va_arg (*args, ip46_type_t);
175 is_ip4 = ip46_address_is_ip4 (ip46);
186 format (s, "%U", format_ip4_address, &ip46->ip4) :
187 format (s, "%U", format_ip6_address, &ip46->ip6);
191 * VPPCOM Utility Functions
195 vcl_send_session_listen (vcl_worker_t * wrk, vcl_session_t * s)
197 app_session_evt_t _app_evt, *app_evt = &_app_evt;
198 session_listen_msg_t *mp;
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);
216 vcl_send_session_connect (vcl_worker_t * wrk, vcl_session_t * s)
218 app_session_evt_t _app_evt, *app_evt = &_app_evt;
219 session_connect_msg_t *mp;
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);
242 vcl_send_session_unlisten (vcl_worker_t * wrk, vcl_session_t * s)
244 app_session_evt_t _app_evt, *app_evt = &_app_evt;
245 session_unlisten_msg_t *mp;
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);
260 vcl_send_session_disconnect (vcl_worker_t * wrk, vcl_session_t * s)
262 app_session_evt_t _app_evt, *app_evt = &_app_evt;
263 session_disconnect_msg_t *mp;
266 /* Send to thread that owns the session */
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);
277 vcl_send_app_detach (vcl_worker_t * wrk)
279 app_session_evt_t _app_evt, *app_evt = &_app_evt;
280 session_app_detach_msg_t *mp;
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);
292 vcl_send_session_accepted_reply (svm_msg_q_t * mq, u32 context,
293 session_handle_t handle, int retval)
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);
306 vcl_send_session_disconnected_reply (svm_msg_q_t * mq, u32 context,
307 session_handle_t handle, int retval)
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);
321 vcl_send_session_reset_reply (svm_msg_q_t * mq, u32 context,
322 session_handle_t handle, int retval)
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);
335 vcl_send_session_worker_update (vcl_worker_t * wrk, vcl_session_t * s,
338 app_session_evt_t _app_evt, *app_evt = &_app_evt;
339 session_worker_update_msg_t *mp;
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);
353 vcl_session_accepted_handler (vcl_worker_t * wrk, session_accepted_msg_t * mp,
356 vcl_session_t *session, *listen_session;
357 svm_fifo_t *rx_fifo, *tx_fifo;
361 session = vcl_session_alloc (wrk);
363 listen_session = vcl_session_get (wrk, ls_index);
364 if (listen_session->vpp_handle != mp->listener_handle)
366 VDBG (0, "ERROR: listener handle %lu does not match session %u",
367 mp->listener_handle, ls_index);
371 if (vcl_segment_is_not_mounted (wrk, mp->segment_handle))
373 VDBG (0, "ERROR: segment for session %u is not mounted!",
374 session->session_index);
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,
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;
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;
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));
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++;
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);
416 vcl_send_session_accepted_reply (session->vpp_evt_q, mp->context,
417 session->vpp_handle, 0);
419 return session->session_index;
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;
430 vcl_session_connected_handler (vcl_worker_t * wrk,
431 session_connected_msg_t * mp)
433 u32 session_index, vpp_wrk_index;
434 svm_fifo_t *rx_fifo, *tx_fifo;
435 vcl_session_t *session = 0;
437 session_index = mp->context;
438 session = vcl_session_get (wrk, session_index);
441 VDBG (0, "ERROR: vpp handle 0x%llx has no session index (%u)!",
442 mp->handle, session_index);
443 return VCL_INVALID_SESSION_INDEX;
447 VDBG (0, "ERROR: session index %u: connect failed! %U",
448 session_index, format_api_error, ntohl (mp->retval));
449 session->session_state = STATE_DETACHED | STATE_DISCONNECT;
450 session->vpp_handle = mp->handle;
451 return session_index;
454 session->vpp_handle = mp->handle;
455 session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address,
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))
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;
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 ();
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;
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))
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;
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;
500 /* Add it to lookup table */
501 vcl_session_table_add_vpp_handle (wrk, mp->handle, session_index);
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);
507 return session_index;
511 vcl_flag_accepted_session (vcl_session_t * session, u64 handle, u32 flags)
513 vcl_session_msg_t *accepted_msg;
516 for (i = 0; i < vec_len (session->accept_evts_fifo); i++)
518 accepted_msg = &session->accept_evts_fifo[i];
519 if (accepted_msg->accepted_msg.handle == handle)
521 accepted_msg->flags |= flags;
529 vcl_session_reset_handler (vcl_worker_t * wrk,
530 session_reset_msg_t * reset_msg)
532 vcl_session_t *session;
535 sid = vcl_session_index_from_vpp_handle (wrk, reset_msg->handle);
536 session = vcl_session_get (wrk, sid);
539 VDBG (0, "request to reset unknown handle 0x%llx", reset_msg->handle);
540 return VCL_INVALID_SESSION_INDEX;
543 /* Caught a reset before actually accepting the session */
544 if (session->session_state == STATE_LISTEN)
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;
553 session->session_state = STATE_DISCONNECT;
554 VDBG (0, "reset session %u [0x%llx]", sid, reset_msg->handle);
559 vcl_session_bound_handler (vcl_worker_t * wrk, session_bound_msg_t * mp)
561 vcl_session_t *session;
562 u32 sid = mp->context;
564 session = vcl_session_get (wrk, sid);
567 VERR ("session %u [0x%llx]: bind failed: %U", sid, mp->handle,
568 format_api_error, mp->retval);
571 session->session_state = STATE_DETACHED;
572 session->vpp_handle = mp->handle;
577 VDBG (0, "ERROR: session %u [0x%llx]: Invalid session index!",
579 return VCL_INVALID_SESSION_INDEX;
583 session->vpp_handle = mp->handle;
584 session->transport.is_ip4 = mp->lcl_is_ip4;
585 clib_memcpy_fast (&session->transport.lcl_ip, mp->lcl_ip,
586 sizeof (ip46_address_t));
587 session->transport.lcl_port = mp->lcl_port;
588 vcl_session_table_add_listener (wrk, mp->handle, sid);
589 session->session_state = STATE_LISTEN;
591 session->vpp_evt_q = uword_to_pointer (mp->vpp_evt_q, svm_msg_q_t *);
592 vec_validate (wrk->vpp_event_queues, 0);
593 wrk->vpp_event_queues[0] = session->vpp_evt_q;
595 if (vcl_session_is_cl (session))
597 svm_fifo_t *rx_fifo, *tx_fifo;
598 session->vpp_evt_q = uword_to_pointer (mp->vpp_evt_q, svm_msg_q_t *);
599 rx_fifo = uword_to_pointer (mp->rx_fifo, svm_fifo_t *);
600 rx_fifo->client_session_index = sid;
601 tx_fifo = uword_to_pointer (mp->tx_fifo, svm_fifo_t *);
602 tx_fifo->client_session_index = sid;
603 session->rx_fifo = rx_fifo;
604 session->tx_fifo = tx_fifo;
607 VDBG (0, "session %u [0x%llx]: listen succeeded!", sid, mp->handle);
612 vcl_session_unlisten_reply_handler (vcl_worker_t * wrk, void *data)
614 session_unlisten_reply_msg_t *mp = (session_unlisten_reply_msg_t *) data;
617 s = vcl_session_get_w_vpp_handle (wrk, mp->handle);
620 VDBG (0, "Unlisten reply with wrong handle %llx", mp->handle);
623 if (s->session_state != STATE_DISCONNECT)
625 /* Connected udp listener */
626 if (s->session_type == VPPCOM_PROTO_UDP
627 && s->session_state == STATE_CLOSED)
630 VDBG (0, "Unlisten session in wrong state %llx", mp->handle);
635 VDBG (0, "ERROR: session %u [0xllx]: unlisten failed: %U",
636 s->session_index, mp->handle, format_api_error, ntohl (mp->retval));
638 if (mp->context != wrk->wrk_index)
639 VDBG (0, "wrong context");
641 vcl_session_table_del_vpp_handle (wrk, mp->handle);
642 vcl_session_free (wrk, s);
646 vcl_session_migrated_handler (vcl_worker_t * wrk, void *data)
648 session_migrated_msg_t *mp = (session_migrated_msg_t *) data;
651 s = vcl_session_get_w_vpp_handle (wrk, mp->handle);
654 VDBG (0, "Migrated notification with wrong handle %llx", mp->handle);
658 s->vpp_thread_index = mp->vpp_thread_index;
659 s->vpp_evt_q = uword_to_pointer (mp->vpp_evt_q, svm_msg_q_t *);
661 vec_validate (wrk->vpp_event_queues, s->vpp_thread_index);
662 wrk->vpp_event_queues[s->vpp_thread_index] = s->vpp_evt_q;
664 vcl_session_table_del_vpp_handle (wrk, mp->handle);
665 vcl_session_table_add_vpp_handle (wrk, mp->new_handle, s->session_index);
667 /* Generate new tx event if we have outstanding data */
668 if (svm_fifo_has_event (s->tx_fifo))
669 app_send_io_evt_to_vpp (s->vpp_evt_q, s->tx_fifo->master_session_index,
670 SESSION_IO_EVT_TX, SVM_Q_WAIT);
672 VDBG (0, "Migrated 0x%x to thread %u", mp->handle, s->vpp_thread_index);
675 static vcl_session_t *
676 vcl_session_accepted (vcl_worker_t * wrk, session_accepted_msg_t * msg)
678 vcl_session_msg_t *vcl_msg;
679 vcl_session_t *session;
681 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
682 if (PREDICT_FALSE (session != 0))
683 VWRN ("session overlap handle %lu state %u!", msg->handle,
684 session->session_state);
686 session = vcl_session_table_lookup_listener (wrk, msg->listener_handle);
689 VERR ("couldn't find listen session: listener handle %llx",
690 msg->listener_handle);
694 clib_fifo_add2 (session->accept_evts_fifo, vcl_msg);
696 vcl_msg->accepted_msg = *msg;
697 /* Session handle points to listener until fully accepted by app */
698 vcl_session_table_add_vpp_handle (wrk, msg->handle, session->session_index);
703 static vcl_session_t *
704 vcl_session_disconnected_handler (vcl_worker_t * wrk,
705 session_disconnected_msg_t * msg)
707 vcl_session_t *session;
709 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
712 VDBG (0, "request to disconnect unknown handle 0x%llx", msg->handle);
716 /* Late disconnect notification on a session that has been closed */
717 if (session->session_state == STATE_CLOSED)
720 /* Caught a disconnect before actually accepting the session */
721 if (session->session_state == STATE_LISTEN)
723 if (!vcl_flag_accepted_session (session, msg->handle,
724 VCL_ACCEPTED_F_CLOSED))
725 VDBG (0, "session was not accepted!");
729 /* If not already reset change state */
730 if (session->session_state != STATE_DISCONNECT)
731 session->session_state = STATE_VPP_CLOSING;
737 vcl_session_cleanup_handler (vcl_worker_t * wrk, void *data)
739 session_cleanup_msg_t *msg;
740 vcl_session_t *session;
742 msg = (session_cleanup_msg_t *) data;
743 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
746 VDBG (0, "disconnect confirmed for unknown handle 0x%llx", msg->handle);
750 vcl_session_table_del_vpp_handle (wrk, msg->handle);
751 /* Should not happen. App did not close the connection so don't free it. */
752 if (session->session_state != STATE_CLOSED)
754 VDBG (0, "app did not close session %d", session->session_index);
755 session->session_state = STATE_DETACHED;
756 session->vpp_handle = VCL_INVALID_SESSION_HANDLE;
759 vcl_session_free (wrk, session);
763 vcl_session_req_worker_update_handler (vcl_worker_t * wrk, void *data)
765 session_req_worker_update_msg_t *msg;
768 msg = (session_req_worker_update_msg_t *) data;
769 s = vcl_session_get_w_vpp_handle (wrk, msg->session_handle);
773 vec_add1 (wrk->pending_session_wrk_updates, s->session_index);
777 vcl_session_worker_update_reply_handler (vcl_worker_t * wrk, void *data)
779 session_worker_update_reply_msg_t *msg;
782 msg = (session_worker_update_reply_msg_t *) data;
783 s = vcl_session_get_w_vpp_handle (wrk, msg->handle);
786 VDBG (0, "unknown handle 0x%llx", msg->handle);
789 if (vcl_segment_is_not_mounted (wrk, msg->segment_handle))
791 clib_warning ("segment for session %u is not mounted!",
798 s->rx_fifo = uword_to_pointer (msg->rx_fifo, svm_fifo_t *);
799 s->tx_fifo = uword_to_pointer (msg->tx_fifo, svm_fifo_t *);
800 s->rx_fifo->client_session_index = s->session_index;
801 s->tx_fifo->client_session_index = s->session_index;
802 s->rx_fifo->client_thread_index = wrk->wrk_index;
803 s->tx_fifo->client_thread_index = wrk->wrk_index;
805 s->session_state = STATE_UPDATED;
807 VDBG (0, "session %u[0x%llx] moved to worker %u", s->session_index,
808 s->vpp_handle, wrk->wrk_index);
812 vcl_session_app_add_segment_handler (vcl_worker_t * wrk, void *data)
814 ssvm_segment_type_t seg_type = SSVM_SEGMENT_SHM;
815 session_app_add_segment_msg_t *msg;
819 msg = (session_app_add_segment_msg_t *) data;
823 vl_socket_client_recv_fd_msg2 (&wrk->bapi_sock_ctx, &fd, 1, 5);
824 seg_type = SSVM_SEGMENT_MEMFD;
827 segment_handle = msg->segment_handle;
828 if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
830 clib_warning ("invalid segment handle");
834 if (vcl_segment_attach (segment_handle, (char *) msg->segment_name,
837 VDBG (0, "vcl_segment_attach ('%s') failed", msg->segment_name);
841 VDBG (1, "mapped new segment '%s' size %d", msg->segment_name,
846 vcl_session_app_del_segment_handler (vcl_worker_t * wrk, void *data)
848 session_app_del_segment_msg_t *msg = (session_app_del_segment_msg_t *) data;
849 vcl_segment_detach (msg->segment_handle);
850 VDBG (1, "Unmapped segment: %d", msg->segment_handle);
854 vcl_handle_mq_event (vcl_worker_t * wrk, session_event_t * e)
856 session_disconnected_msg_t *disconnected_msg;
857 vcl_session_t *session;
859 switch (e->event_type)
861 case SESSION_IO_EVT_RX:
862 case SESSION_IO_EVT_TX:
863 session = vcl_session_get (wrk, e->session_index);
864 if (!session || !(session->session_state & STATE_OPEN))
866 vec_add1 (wrk->unhandled_evts_vector, *e);
868 case SESSION_CTRL_EVT_ACCEPTED:
869 vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
871 case SESSION_CTRL_EVT_CONNECTED:
872 vcl_session_connected_handler (wrk,
873 (session_connected_msg_t *) e->data);
875 case SESSION_CTRL_EVT_DISCONNECTED:
876 disconnected_msg = (session_disconnected_msg_t *) e->data;
877 session = vcl_session_disconnected_handler (wrk, disconnected_msg);
880 VDBG (0, "disconnected session %u [0x%llx]", session->session_index,
881 session->vpp_handle);
883 case SESSION_CTRL_EVT_RESET:
884 vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
886 case SESSION_CTRL_EVT_BOUND:
887 vcl_session_bound_handler (wrk, (session_bound_msg_t *) e->data);
889 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
890 vcl_session_unlisten_reply_handler (wrk, e->data);
892 case SESSION_CTRL_EVT_MIGRATED:
893 vcl_session_migrated_handler (wrk, e->data);
895 case SESSION_CTRL_EVT_CLEANUP:
896 vcl_session_cleanup_handler (wrk, e->data);
898 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
899 vcl_session_req_worker_update_handler (wrk, e->data);
901 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
902 vcl_session_worker_update_reply_handler (wrk, e->data);
904 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
905 vcl_session_app_add_segment_handler (wrk, e->data);
907 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
908 vcl_session_app_del_segment_handler (wrk, e->data);
911 clib_warning ("unhandled %u", e->event_type);
917 vppcom_wait_for_session_state_change (u32 session_index,
918 vcl_session_state_t state,
921 vcl_worker_t *wrk = vcl_worker_get_current ();
922 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
923 vcl_session_t *volatile session;
929 session = vcl_session_get (wrk, session_index);
930 if (PREDICT_FALSE (!session))
932 return VPPCOM_EBADFD;
934 if (session->session_state & state)
938 if (session->session_state & STATE_DETACHED)
940 return VPPCOM_ECONNREFUSED;
943 if (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0))
948 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
949 vcl_handle_mq_event (wrk, e);
950 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
952 while (clib_time_now (&wrk->clib_time) < timeout);
954 VDBG (0, "timeout waiting for state 0x%x (%s)", state,
955 vppcom_session_state_str (state));
956 vcl_evt (VCL_EVT_SESSION_TIMEOUT, session, session_state);
958 return VPPCOM_ETIMEDOUT;
962 vcl_handle_pending_wrk_updates (vcl_worker_t * wrk)
964 vcl_session_state_t state;
968 if (PREDICT_TRUE (vec_len (wrk->pending_session_wrk_updates) == 0))
971 vec_foreach (sip, wrk->pending_session_wrk_updates)
973 s = vcl_session_get (wrk, *sip);
974 vcl_send_session_worker_update (wrk, s, wrk->wrk_index);
975 state = s->session_state;
976 vppcom_wait_for_session_state_change (s->session_index, STATE_UPDATED, 5);
977 s->session_state = state;
979 vec_reset_length (wrk->pending_session_wrk_updates);
983 vcl_flush_mq_events (void)
985 vcl_worker_t *wrk = vcl_worker_get_current ();
986 svm_msg_q_msg_t *msg;
991 mq = wrk->app_event_queue;
993 vcl_mq_dequeue_batch (wrk, mq, ~0);
994 svm_msg_q_unlock (mq);
996 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
998 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
999 e = svm_msg_q_msg_data (mq, msg);
1000 vcl_handle_mq_event (wrk, e);
1001 svm_msg_q_free_msg (mq, msg);
1003 vec_reset_length (wrk->mq_msg_vector);
1004 vcl_handle_pending_wrk_updates (wrk);
1008 vppcom_app_session_enable (void)
1012 if (vcm->app_state != STATE_APP_ENABLED)
1014 vppcom_send_session_enable_disable (1 /* is_enabled == TRUE */ );
1015 rv = vcl_wait_for_app_state_change (STATE_APP_ENABLED);
1016 if (PREDICT_FALSE (rv))
1018 VDBG (0, "application session enable timed out! returning %d (%s)",
1019 rv, vppcom_retval_str (rv));
1027 vppcom_app_attach (void)
1031 vppcom_app_send_attach ();
1032 rv = vcl_wait_for_app_state_change (STATE_APP_ATTACHED);
1033 if (PREDICT_FALSE (rv))
1035 VDBG (0, "application attach timed out! returning %d (%s)", rv,
1036 vppcom_retval_str (rv));
1044 vppcom_session_unbind (u32 session_handle)
1046 vcl_worker_t *wrk = vcl_worker_get_current ();
1047 session_accepted_msg_t *accepted_msg;
1048 vcl_session_t *session = 0;
1049 vcl_session_msg_t *evt;
1051 session = vcl_session_get_w_handle (wrk, session_handle);
1053 return VPPCOM_EBADFD;
1055 /* Flush pending accept events, if any */
1056 while (clib_fifo_elts (session->accept_evts_fifo))
1058 clib_fifo_sub2 (session->accept_evts_fifo, evt);
1059 accepted_msg = &evt->accepted_msg;
1060 vcl_session_table_del_vpp_handle (wrk, accepted_msg->handle);
1061 vcl_send_session_accepted_reply (session->vpp_evt_q,
1062 accepted_msg->context,
1063 session->vpp_handle, -1);
1065 clib_fifo_free (session->accept_evts_fifo);
1067 vcl_send_session_unlisten (wrk, session);
1069 VDBG (1, "session %u [0x%llx]: sending unbind!", session->session_index,
1070 session->vpp_handle);
1071 vcl_evt (VCL_EVT_UNBIND, session);
1073 session->vpp_handle = ~0;
1074 session->session_state = STATE_DISCONNECT;
1080 vppcom_session_disconnect (u32 session_handle)
1082 vcl_worker_t *wrk = vcl_worker_get_current ();
1083 svm_msg_q_t *vpp_evt_q;
1084 vcl_session_t *session, *listen_session;
1085 vcl_session_state_t state;
1088 session = vcl_session_get_w_handle (wrk, session_handle);
1090 return VPPCOM_EBADFD;
1092 vpp_handle = session->vpp_handle;
1093 state = session->session_state;
1095 VDBG (1, "session %u [0x%llx] state 0x%x (%s)", session->session_index,
1096 vpp_handle, state, vppcom_session_state_str (state));
1098 if (PREDICT_FALSE (state & STATE_LISTEN))
1100 VDBG (0, "ERROR: Cannot disconnect a listen socket!");
1101 return VPPCOM_EBADFD;
1104 if (state & STATE_VPP_CLOSING)
1106 vpp_evt_q = vcl_session_vpp_evt_q (wrk, session);
1107 vcl_send_session_disconnected_reply (vpp_evt_q, wrk->my_client_index,
1109 VDBG (1, "session %u [0x%llx]: sending disconnect REPLY...",
1110 session->session_index, vpp_handle);
1114 VDBG (1, "session %u [0x%llx]: sending disconnect...",
1115 session->session_index, vpp_handle);
1116 vcl_send_session_disconnect (wrk, session);
1119 if (session->listener_index != VCL_INVALID_SESSION_INDEX)
1121 listen_session = vcl_session_get (wrk, session->listener_index);
1122 listen_session->n_accepted_sessions--;
1131 * Notify vpp of the disconnect and mark the worker as free. If we're the
1132 * last worker, do a full cleanup otherwise, since we're probably a forked
1133 * child, avoid syscalls as much as possible. We might've lost privileges.
1136 vppcom_app_exit (void)
1138 if (!pool_elts (vcm->workers))
1140 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
1141 vcl_set_worker_index (~0);
1142 vcl_elog_stop (vcm);
1146 * VPPCOM Public API functions
1149 vppcom_app_create (char *app_name)
1151 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
1156 VDBG (1, "already initialized");
1157 return VPPCOM_EEXIST;
1161 vppcom_cfg (&vcm->cfg);
1162 vcl_cfg = &vcm->cfg;
1164 vcm->main_cpu = pthread_self ();
1165 vcm->main_pid = getpid ();
1166 vcm->app_name = format (0, "%s", app_name);
1167 vppcom_init_error_string_table ();
1168 fifo_segment_main_init (&vcm->segment_main, vcl_cfg->segment_baseva,
1169 20 /* timeout in secs */ );
1170 pool_alloc (vcm->workers, vcl_cfg->max_workers);
1171 clib_spinlock_init (&vcm->workers_lock);
1172 clib_rwlock_init (&vcm->segment_table_lock);
1173 atexit (vppcom_app_exit);
1175 /* Allocate default worker */
1176 vcl_worker_alloc_and_init ();
1178 /* API hookup and connect to VPP */
1179 vcl_elog_init (vcm);
1180 vcm->app_state = STATE_APP_START;
1181 rv = vppcom_connect_to_vpp (app_name);
1184 VERR ("couldn't connect to VPP!");
1187 VDBG (0, "sending session enable");
1188 rv = vppcom_app_session_enable ();
1191 VERR ("vppcom_app_session_enable() failed!");
1195 VDBG (0, "sending app attach");
1196 rv = vppcom_app_attach ();
1199 VERR ("vppcom_app_attach() failed!");
1203 VDBG (0, "app_name '%s', my_client_index %d (0x%x)", app_name,
1204 vcm->workers[0].my_client_index, vcm->workers[0].my_client_index);
1210 vppcom_app_destroy (void)
1213 f64 orig_app_timeout;
1215 if (!pool_elts (vcm->workers))
1218 vcl_evt (VCL_EVT_DETACH, vcm);
1220 if (pool_elts (vcm->workers) == 1)
1222 vcl_send_app_detach (vcl_worker_get_current ());
1223 orig_app_timeout = vcm->cfg.app_timeout;
1224 vcm->cfg.app_timeout = 2.0;
1225 rv = vcl_wait_for_app_state_change (STATE_APP_ENABLED);
1226 vcm->cfg.app_timeout = orig_app_timeout;
1227 if (PREDICT_FALSE (rv))
1228 VDBG (0, "application detach timed out! returning %d (%s)", rv,
1229 vppcom_retval_str (rv));
1230 vec_free (vcm->app_name);
1231 vcl_worker_cleanup (vcl_worker_get_current (), 0 /* notify vpp */ );
1235 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
1238 vcl_set_worker_index (~0);
1239 vcl_elog_stop (vcm);
1240 vl_client_disconnect_from_vlib ();
1244 vppcom_session_create (u8 proto, u8 is_nonblocking)
1246 vcl_worker_t *wrk = vcl_worker_get_current ();
1247 vcl_session_t *session;
1249 session = vcl_session_alloc (wrk);
1251 session->session_type = proto;
1252 session->session_state = STATE_CLOSED;
1253 session->vpp_handle = ~0;
1254 session->is_dgram = vcl_proto_is_dgram (proto);
1257 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK);
1259 vcl_evt (VCL_EVT_CREATE, session, session_type, session->session_state,
1260 is_nonblocking, session_index);
1262 VDBG (0, "created session %u", session->session_index);
1264 return vcl_session_handle (session);
1268 vcl_session_cleanup (vcl_worker_t * wrk, vcl_session_t * session,
1269 vcl_session_handle_t sh, u8 do_disconnect)
1271 vcl_session_state_t state;
1272 u32 next_sh, vep_sh;
1277 is_vep = session->is_vep;
1278 next_sh = session->vep.next_sh;
1279 vep_sh = session->vep.vep_sh;
1280 state = session->session_state;
1281 vpp_handle = session->vpp_handle;
1283 VDBG (1, "session %u [0x%llx] closing", session->session_index, vpp_handle);
1287 while (next_sh != ~0)
1289 rv = vppcom_epoll_ctl (sh, EPOLL_CTL_DEL, next_sh, 0);
1290 if (PREDICT_FALSE (rv < 0))
1291 VDBG (0, "vpp handle 0x%llx, sh %u: EPOLL_CTL_DEL vep_idx %u"
1292 " failed! rv %d (%s)", vpp_handle, next_sh, vep_sh, rv,
1293 vppcom_retval_str (rv));
1295 next_sh = session->vep.next_sh;
1300 if (session->is_vep_session)
1302 rv = vppcom_epoll_ctl (vep_sh, EPOLL_CTL_DEL, sh, 0);
1304 VDBG (0, "session %u [0x%llx]: EPOLL_CTL_DEL vep_idx %u "
1305 "failed! rv %d (%s)", session->session_index, vpp_handle,
1306 vep_sh, rv, vppcom_retval_str (rv));
1311 VDBG (1, "session %u [0x%llx] disconnect skipped",
1312 session->session_index, vpp_handle);
1316 if (state & STATE_LISTEN)
1318 rv = vppcom_session_unbind (sh);
1319 if (PREDICT_FALSE (rv < 0))
1320 VDBG (0, "session %u [0x%llx]: listener unbind failed! "
1321 "rv %d (%s)", session->session_index, vpp_handle, rv,
1322 vppcom_retval_str (rv));
1325 else if ((state & STATE_OPEN)
1326 || (vcl_session_is_connectable_listener (wrk, session)))
1328 rv = vppcom_session_disconnect (sh);
1329 if (PREDICT_FALSE (rv < 0))
1330 VDBG (0, "ERROR: session %u [0x%llx]: disconnect failed!"
1331 " rv %d (%s)", session->session_index, vpp_handle,
1332 rv, vppcom_retval_str (rv));
1334 else if (state == STATE_DISCONNECT)
1336 svm_msg_q_t *mq = vcl_session_vpp_evt_q (wrk, session);
1337 vcl_send_session_reset_reply (mq, wrk->my_client_index,
1338 session->vpp_handle, 0);
1340 else if (state == STATE_DETACHED)
1342 /* Should not happen. VPP cleaned up before app confirmed close */
1343 VDBG (0, "vpp freed session %d before close", session->session_index);
1347 session->session_state = STATE_CLOSED;
1349 /* Session is removed only after vpp confirms the disconnect */
1353 vcl_session_table_del_vpp_handle (wrk, vpp_handle);
1355 vcl_session_free (wrk, session);
1356 vcl_evt (VCL_EVT_CLOSE, session, rv);
1362 vppcom_session_close (uint32_t session_handle)
1364 vcl_worker_t *wrk = vcl_worker_get_current ();
1365 vcl_session_t *session;
1367 session = vcl_session_get_w_handle (wrk, session_handle);
1369 return VPPCOM_EBADFD;
1370 return vcl_session_cleanup (wrk, session, session_handle,
1371 1 /* do_disconnect */ );
1375 vppcom_session_bind (uint32_t session_handle, vppcom_endpt_t * ep)
1377 vcl_worker_t *wrk = vcl_worker_get_current ();
1378 vcl_session_t *session = 0;
1381 return VPPCOM_EINVAL;
1383 session = vcl_session_get_w_handle (wrk, session_handle);
1385 return VPPCOM_EBADFD;
1387 if (session->is_vep)
1389 VDBG (0, "ERROR: cannot bind to epoll session %u!",
1390 session->session_index);
1391 return VPPCOM_EBADFD;
1394 session->transport.is_ip4 = ep->is_ip4;
1396 clib_memcpy_fast (&session->transport.lcl_ip.ip4, ep->ip,
1397 sizeof (ip4_address_t));
1399 clib_memcpy_fast (&session->transport.lcl_ip.ip6, ep->ip,
1400 sizeof (ip6_address_t));
1401 session->transport.lcl_port = ep->port;
1403 VDBG (0, "session %u handle %u: binding to local %s address %U port %u, "
1404 "proto %s", session->session_index, session_handle,
1405 session->transport.is_ip4 ? "IPv4" : "IPv6",
1406 format_ip46_address, &session->transport.lcl_ip,
1407 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
1408 clib_net_to_host_u16 (session->transport.lcl_port),
1409 vppcom_proto_str (session->session_type));
1410 vcl_evt (VCL_EVT_BIND, session);
1412 if (session->session_type == VPPCOM_PROTO_UDP)
1413 vppcom_session_listen (session_handle, 10);
1419 vppcom_session_listen (uint32_t listen_sh, uint32_t q_len)
1421 vcl_worker_t *wrk = vcl_worker_get_current ();
1422 vcl_session_t *listen_session = 0;
1423 u64 listen_vpp_handle;
1426 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
1427 if (!listen_session || listen_session->is_vep)
1428 return VPPCOM_EBADFD;
1430 if (q_len == 0 || q_len == ~0)
1431 q_len = vcm->cfg.listen_queue_size;
1433 listen_vpp_handle = listen_session->vpp_handle;
1434 if (listen_session->session_state & STATE_LISTEN)
1436 VDBG (0, "session %u [0x%llx]: already in listen state!",
1437 listen_sh, listen_vpp_handle);
1441 VDBG (0, "session %u: sending vpp listen request...", listen_sh);
1444 * Send listen request to vpp and wait for reply
1446 vcl_send_session_listen (wrk, listen_session);
1447 rv = vppcom_wait_for_session_state_change (listen_session->session_index,
1449 vcm->cfg.session_timeout);
1451 if (PREDICT_FALSE (rv))
1453 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
1454 VDBG (0, "session %u [0x%llx]: listen failed! returning %d (%s)",
1455 listen_sh, listen_session->vpp_handle, rv,
1456 vppcom_retval_str (rv));
1464 vppcom_session_tls_add_cert (uint32_t session_handle, char *cert,
1468 vcl_worker_t *wrk = vcl_worker_get_current ();
1469 vcl_session_t *session = 0;
1471 session = vcl_session_get_w_handle (wrk, session_handle);
1473 return VPPCOM_EBADFD;
1475 if (cert_len == 0 || cert_len == ~0)
1476 return VPPCOM_EBADFD;
1479 * Send listen request to vpp and wait for reply
1481 vppcom_send_application_tls_cert_add (session, cert, cert_len);
1482 vcm->app_state = STATE_APP_ADDING_TLS_DATA;
1483 vcl_wait_for_app_state_change (STATE_APP_READY);
1489 vppcom_session_tls_add_key (uint32_t session_handle, char *key,
1493 vcl_worker_t *wrk = vcl_worker_get_current ();
1494 vcl_session_t *session = 0;
1496 session = vcl_session_get_w_handle (wrk, session_handle);
1498 return VPPCOM_EBADFD;
1500 if (key_len == 0 || key_len == ~0)
1501 return VPPCOM_EBADFD;
1503 vppcom_send_application_tls_key_add (session, key, key_len);
1504 vcm->app_state = STATE_APP_ADDING_TLS_DATA;
1505 vcl_wait_for_app_state_change (STATE_APP_READY);
1510 validate_args_session_accept_ (vcl_worker_t * wrk, vcl_session_t * ls)
1514 VDBG (0, "ERROR: cannot accept on epoll session %u!",
1516 return VPPCOM_EBADFD;
1519 if ((ls->session_state != STATE_LISTEN)
1520 && (!vcl_session_is_connectable_listener (wrk, ls)))
1523 "ERROR: session [0x%llx]: not in listen state! state 0x%x"
1524 " (%s)", ls->vpp_handle, ls->session_state,
1525 vppcom_session_state_str (ls->session_state));
1526 return VPPCOM_EBADFD;
1532 vppcom_unformat_proto (uint8_t * proto, char *proto_str)
1534 if (!strcmp (proto_str, "TCP"))
1535 *proto = VPPCOM_PROTO_TCP;
1536 else if (!strcmp (proto_str, "tcp"))
1537 *proto = VPPCOM_PROTO_TCP;
1538 else if (!strcmp (proto_str, "UDP"))
1539 *proto = VPPCOM_PROTO_UDP;
1540 else if (!strcmp (proto_str, "udp"))
1541 *proto = VPPCOM_PROTO_UDP;
1542 else if (!strcmp (proto_str, "UDPC"))
1543 *proto = VPPCOM_PROTO_UDPC;
1544 else if (!strcmp (proto_str, "udpc"))
1545 *proto = VPPCOM_PROTO_UDPC;
1546 else if (!strcmp (proto_str, "TLS"))
1547 *proto = VPPCOM_PROTO_TLS;
1548 else if (!strcmp (proto_str, "tls"))
1549 *proto = VPPCOM_PROTO_TLS;
1550 else if (!strcmp (proto_str, "QUIC"))
1551 *proto = VPPCOM_PROTO_QUIC;
1552 else if (!strcmp (proto_str, "quic"))
1553 *proto = VPPCOM_PROTO_QUIC;
1560 vppcom_session_accept (uint32_t listen_session_handle, vppcom_endpt_t * ep,
1563 u32 client_session_index = ~0, listen_session_index, accept_flags = 0;
1564 vcl_worker_t *wrk = vcl_worker_get_current ();
1565 session_accepted_msg_t accepted_msg;
1566 vcl_session_t *listen_session = 0;
1567 vcl_session_t *client_session = 0;
1568 vcl_session_msg_t *evt;
1569 svm_msg_q_msg_t msg;
1574 listen_session = vcl_session_get_w_handle (wrk, listen_session_handle);
1575 if (!listen_session)
1576 return VPPCOM_EBADFD;
1578 listen_session_index = listen_session->session_index;
1579 if ((rv = validate_args_session_accept_ (wrk, listen_session)))
1582 if (clib_fifo_elts (listen_session->accept_evts_fifo))
1584 clib_fifo_sub2 (listen_session->accept_evts_fifo, evt);
1585 accept_flags = evt->flags;
1586 accepted_msg = evt->accepted_msg;
1590 is_nonblocking = VCL_SESS_ATTR_TEST (listen_session->attr,
1591 VCL_SESS_ATTR_NONBLOCK);
1594 if (svm_msg_q_is_empty (wrk->app_event_queue) && is_nonblocking)
1595 return VPPCOM_EAGAIN;
1597 if (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_WAIT, 0))
1598 return VPPCOM_EAGAIN;
1600 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
1601 if (e->event_type != SESSION_CTRL_EVT_ACCEPTED)
1603 vcl_handle_mq_event (wrk, e);
1604 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
1607 clib_memcpy_fast (&accepted_msg, e->data, sizeof (accepted_msg));
1608 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
1614 client_session_index = vcl_session_accepted_handler (wrk, &accepted_msg,
1615 listen_session_index);
1616 if (client_session_index == VCL_INVALID_SESSION_INDEX)
1617 return VPPCOM_ECONNABORTED;
1619 listen_session = vcl_session_get (wrk, listen_session_index);
1620 client_session = vcl_session_get (wrk, client_session_index);
1622 if (flags & O_NONBLOCK)
1623 VCL_SESS_ATTR_SET (client_session->attr, VCL_SESS_ATTR_NONBLOCK);
1625 VDBG (1, "listener %u [0x%llx]: Got a connect request! session %u [0x%llx],"
1626 " flags %d, is_nonblocking %u", listen_session->session_index,
1627 listen_session->vpp_handle, client_session_index,
1628 client_session->vpp_handle, flags,
1629 VCL_SESS_ATTR_TEST (client_session->attr, VCL_SESS_ATTR_NONBLOCK));
1633 ep->is_ip4 = client_session->transport.is_ip4;
1634 ep->port = client_session->transport.rmt_port;
1635 if (client_session->transport.is_ip4)
1636 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip4,
1637 sizeof (ip4_address_t));
1639 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip6,
1640 sizeof (ip6_address_t));
1643 VDBG (0, "listener %u [0x%llx] accepted %u [0x%llx] peer: %U:%u "
1644 "local: %U:%u", listen_session_handle, listen_session->vpp_handle,
1645 client_session_index, client_session->vpp_handle,
1646 format_ip46_address, &client_session->transport.rmt_ip,
1647 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
1648 clib_net_to_host_u16 (client_session->transport.rmt_port),
1649 format_ip46_address, &client_session->transport.lcl_ip,
1650 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
1651 clib_net_to_host_u16 (client_session->transport.lcl_port));
1652 vcl_evt (VCL_EVT_ACCEPT, client_session, listen_session,
1653 client_session_index);
1656 * Session might have been closed already
1660 if (accept_flags & VCL_ACCEPTED_F_CLOSED)
1661 client_session->session_state = STATE_VPP_CLOSING;
1662 else if (accept_flags & VCL_ACCEPTED_F_RESET)
1663 client_session->session_state = STATE_DISCONNECT;
1665 return vcl_session_handle (client_session);
1669 vppcom_session_connect (uint32_t session_handle, vppcom_endpt_t * server_ep)
1671 vcl_worker_t *wrk = vcl_worker_get_current ();
1672 vcl_session_t *session = 0;
1676 session = vcl_session_get_w_handle (wrk, session_handle);
1678 return VPPCOM_EBADFD;
1679 session_index = session->session_index;
1681 if (PREDICT_FALSE (session->is_vep))
1683 VDBG (0, "ERROR: cannot connect epoll session %u!",
1684 session->session_index);
1685 return VPPCOM_EBADFD;
1688 if (PREDICT_FALSE (session->session_state & CLIENT_STATE_OPEN))
1690 VDBG (0, "session handle %u [0x%llx]: session already "
1691 "connected to %s %U port %d proto %s, state 0x%x (%s)",
1692 session_handle, session->vpp_handle,
1693 session->transport.is_ip4 ? "IPv4" : "IPv6", format_ip46_address,
1694 &session->transport.rmt_ip, session->transport.is_ip4 ?
1695 IP46_TYPE_IP4 : IP46_TYPE_IP6,
1696 clib_net_to_host_u16 (session->transport.rmt_port),
1697 vppcom_proto_str (session->session_type), session->session_state,
1698 vppcom_session_state_str (session->session_state));
1702 /* Attempt to connect a connectionless listener */
1703 if (PREDICT_FALSE (session->session_state & STATE_LISTEN))
1705 if (session->session_type != VPPCOM_PROTO_UDP)
1706 return VPPCOM_EINVAL;
1707 vcl_send_session_unlisten (wrk, session);
1708 session->session_state = STATE_CLOSED;
1711 session->transport.is_ip4 = server_ep->is_ip4;
1712 vcl_ip_copy_from_ep (&session->transport.rmt_ip, server_ep);
1713 session->transport.rmt_port = server_ep->port;
1714 session->parent_handle = VCL_INVALID_SESSION_HANDLE;
1715 session->flags |= VCL_SESSION_F_CONNECTED;
1717 VDBG (0, "session handle %u (%s): connecting to peer %s %U "
1718 "port %d proto %s", session_handle,
1719 vppcom_session_state_str (session->session_state),
1720 session->transport.is_ip4 ? "IPv4" : "IPv6",
1721 format_ip46_address,
1722 &session->transport.rmt_ip, session->transport.is_ip4 ?
1723 IP46_TYPE_IP4 : IP46_TYPE_IP6,
1724 clib_net_to_host_u16 (session->transport.rmt_port),
1725 vppcom_proto_str (session->session_type));
1727 vcl_send_session_connect (wrk, session);
1729 if (VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK))
1730 return VPPCOM_EINPROGRESS;
1733 * Wait for reply from vpp if blocking
1735 rv = vppcom_wait_for_session_state_change (session_index, STATE_CONNECT,
1736 vcm->cfg.session_timeout);
1738 session = vcl_session_get (wrk, session_index);
1739 VDBG (0, "session %u [0x%llx]: connect %s!", session->session_index,
1740 session->vpp_handle, rv ? "failed" : "succeeded");
1746 vppcom_session_stream_connect (uint32_t session_handle,
1747 uint32_t parent_session_handle)
1749 vcl_worker_t *wrk = vcl_worker_get_current ();
1750 vcl_session_t *session, *parent_session;
1751 u32 session_index, parent_session_index;
1754 session = vcl_session_get_w_handle (wrk, session_handle);
1756 return VPPCOM_EBADFD;
1757 parent_session = vcl_session_get_w_handle (wrk, parent_session_handle);
1758 if (!parent_session)
1759 return VPPCOM_EBADFD;
1761 session_index = session->session_index;
1762 parent_session_index = parent_session->session_index;
1763 if (PREDICT_FALSE (session->is_vep))
1765 VDBG (0, "ERROR: cannot connect epoll session %u!",
1766 session->session_index);
1767 return VPPCOM_EBADFD;
1770 if (PREDICT_FALSE (session->session_state & CLIENT_STATE_OPEN))
1772 VDBG (0, "session handle %u [0x%llx]: session already "
1773 "connected to session %u [0x%llx] proto %s, state 0x%x (%s)",
1774 session_handle, session->vpp_handle,
1775 parent_session_handle, parent_session->vpp_handle,
1776 vppcom_proto_str (session->session_type), session->session_state,
1777 vppcom_session_state_str (session->session_state));
1781 /* Connect to quic session specifics */
1782 session->transport.is_ip4 = parent_session->transport.is_ip4;
1783 session->transport.rmt_ip.ip4.as_u32 = (uint32_t) 1;
1784 session->transport.rmt_port = 0;
1785 session->parent_handle = parent_session->vpp_handle;
1787 VDBG (0, "session handle %u: connecting to session %u [0x%llx]",
1788 session_handle, parent_session_handle, parent_session->vpp_handle);
1791 * Send connect request and wait for reply from vpp
1793 vcl_send_session_connect (wrk, session);
1794 rv = vppcom_wait_for_session_state_change (session_index, STATE_CONNECT,
1795 vcm->cfg.session_timeout);
1797 session->listener_index = parent_session_index;
1798 parent_session = vcl_session_get_w_handle (wrk, parent_session_handle);
1800 parent_session->n_accepted_sessions++;
1802 session = vcl_session_get (wrk, session_index);
1803 VDBG (0, "session %u [0x%llx]: connect %s!", session->session_index,
1804 session->vpp_handle, rv ? "failed" : "succeeded");
1810 vcl_is_rx_evt_for_session (session_event_t * e, u32 sid, u8 is_ct)
1812 return (e->event_type == SESSION_IO_EVT_RX && e->session_index == sid);
1816 vppcom_session_read_internal (uint32_t session_handle, void *buf, int n,
1819 vcl_worker_t *wrk = vcl_worker_get_current ();
1820 int n_read = 0, is_nonblocking;
1821 vcl_session_t *s = 0;
1822 svm_fifo_t *rx_fifo;
1823 svm_msg_q_msg_t msg;
1828 if (PREDICT_FALSE (!buf))
1829 return VPPCOM_EINVAL;
1831 s = vcl_session_get_w_handle (wrk, session_handle);
1832 if (PREDICT_FALSE (!s || s->is_vep))
1833 return VPPCOM_EBADFD;
1835 if (PREDICT_FALSE (!vcl_session_is_open (s)))
1837 VDBG (0, "session %u[0x%llx] is not open! state 0x%x (%s)",
1838 s->session_index, s->vpp_handle, s->session_state,
1839 vppcom_session_state_str (s->session_state));
1840 return vcl_session_closed_error (s);
1843 is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
1844 is_ct = vcl_session_is_ct (s);
1845 mq = wrk->app_event_queue;
1846 rx_fifo = is_ct ? s->ct_rx_fifo : s->rx_fifo;
1849 if (svm_fifo_is_empty_cons (rx_fifo))
1853 if (vcl_session_is_closing (s))
1854 return vcl_session_closing_error (s);
1855 svm_fifo_unset_event (s->rx_fifo);
1856 return VPPCOM_EWOULDBLOCK;
1858 while (svm_fifo_is_empty_cons (rx_fifo))
1860 if (vcl_session_is_closing (s))
1861 return vcl_session_closing_error (s);
1863 svm_fifo_unset_event (s->rx_fifo);
1864 svm_msg_q_lock (mq);
1865 if (svm_msg_q_is_empty (mq))
1866 svm_msg_q_wait (mq);
1868 svm_msg_q_sub_w_lock (mq, &msg);
1869 e = svm_msg_q_msg_data (mq, &msg);
1870 svm_msg_q_unlock (mq);
1871 if (!vcl_is_rx_evt_for_session (e, s->session_index, is_ct))
1872 vcl_handle_mq_event (wrk, e);
1873 svm_msg_q_free_msg (mq, &msg);
1878 n_read = app_recv_dgram_raw (rx_fifo, buf, n, &s->transport, 0, peek);
1880 n_read = app_recv_stream_raw (rx_fifo, buf, n, 0, peek);
1882 if (svm_fifo_is_empty_cons (rx_fifo))
1883 svm_fifo_unset_event (s->rx_fifo);
1885 /* Cut-through sessions might request tx notifications on rx fifos */
1886 if (PREDICT_FALSE (rx_fifo->want_deq_ntf))
1888 app_send_io_evt_to_vpp (s->vpp_evt_q, s->rx_fifo->master_session_index,
1889 SESSION_IO_EVT_RX, SVM_Q_WAIT);
1890 svm_fifo_reset_has_deq_ntf (s->rx_fifo);
1893 VDBG (2, "session %u[0x%llx]: read %d bytes from (%p)", s->session_index,
1894 s->vpp_handle, n_read, rx_fifo);
1900 vppcom_session_read (uint32_t session_handle, void *buf, size_t n)
1902 return (vppcom_session_read_internal (session_handle, buf, n, 0));
1906 vppcom_session_peek (uint32_t session_handle, void *buf, int n)
1908 return (vppcom_session_read_internal (session_handle, buf, n, 1));
1912 vppcom_session_read_segments (uint32_t session_handle,
1913 vppcom_data_segments_t ds)
1915 vcl_worker_t *wrk = vcl_worker_get_current ();
1916 int n_read = 0, is_nonblocking;
1917 vcl_session_t *s = 0;
1918 svm_fifo_t *rx_fifo;
1919 svm_msg_q_msg_t msg;
1924 s = vcl_session_get_w_handle (wrk, session_handle);
1925 if (PREDICT_FALSE (!s || s->is_vep))
1926 return VPPCOM_EBADFD;
1928 if (PREDICT_FALSE (!vcl_session_is_open (s)))
1929 return vcl_session_closed_error (s);
1931 is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
1932 is_ct = vcl_session_is_ct (s);
1933 mq = is_ct ? s->our_evt_q : wrk->app_event_queue;
1934 rx_fifo = s->rx_fifo;
1938 svm_fifo_unset_event (s->rx_fifo);
1940 if (svm_fifo_is_empty_cons (rx_fifo))
1944 svm_fifo_unset_event (rx_fifo);
1945 return VPPCOM_EWOULDBLOCK;
1947 while (svm_fifo_is_empty_cons (rx_fifo))
1949 if (vcl_session_is_closing (s))
1950 return vcl_session_closing_error (s);
1952 svm_fifo_unset_event (rx_fifo);
1953 svm_msg_q_lock (mq);
1954 if (svm_msg_q_is_empty (mq))
1955 svm_msg_q_wait (mq);
1957 svm_msg_q_sub_w_lock (mq, &msg);
1958 e = svm_msg_q_msg_data (mq, &msg);
1959 svm_msg_q_unlock (mq);
1960 if (!vcl_is_rx_evt_for_session (e, s->session_index, is_ct))
1961 vcl_handle_mq_event (wrk, e);
1962 svm_msg_q_free_msg (mq, &msg);
1966 n_read = svm_fifo_segments (rx_fifo, (svm_fifo_seg_t *) ds);
1967 svm_fifo_unset_event (rx_fifo);
1973 vppcom_session_free_segments (uint32_t session_handle,
1974 vppcom_data_segments_t ds)
1976 vcl_worker_t *wrk = vcl_worker_get_current ();
1979 s = vcl_session_get_w_handle (wrk, session_handle);
1980 if (PREDICT_FALSE (!s || s->is_vep))
1983 svm_fifo_segments_free (s->rx_fifo, (svm_fifo_seg_t *) ds);
1987 vppcom_data_segment_copy (void *buf, vppcom_data_segments_t ds, u32 max_bytes)
1989 u32 first_copy = clib_min (ds[0].len, max_bytes);
1990 clib_memcpy_fast (buf, ds[0].data, first_copy);
1991 if (first_copy < max_bytes)
1993 clib_memcpy_fast (buf + first_copy, ds[1].data,
1994 clib_min (ds[1].len, max_bytes - first_copy));
2000 vcl_is_tx_evt_for_session (session_event_t * e, u32 sid, u8 is_ct)
2002 return (e->event_type == SESSION_IO_EVT_TX && e->session_index == sid);
2006 vppcom_session_write_inline (uint32_t session_handle, void *buf, size_t n,
2009 vcl_worker_t *wrk = vcl_worker_get_current ();
2010 int n_write, is_nonblocking;
2011 vcl_session_t *s = 0;
2012 session_evt_type_t et;
2013 svm_msg_q_msg_t msg;
2014 svm_fifo_t *tx_fifo;
2019 if (PREDICT_FALSE (!buf || n == 0))
2020 return VPPCOM_EINVAL;
2022 s = vcl_session_get_w_handle (wrk, session_handle);
2023 if (PREDICT_FALSE (!s))
2024 return VPPCOM_EBADFD;
2026 if (PREDICT_FALSE (s->is_vep))
2028 VDBG (0, "ERROR: session %u [0x%llx]: cannot write to an epoll"
2029 " session!", s->session_index, s->vpp_handle);
2030 return VPPCOM_EBADFD;
2033 if (PREDICT_FALSE (!vcl_session_is_open (s)))
2035 VDBG (1, "session %u [0x%llx]: is not open! state 0x%x (%s)",
2036 s->session_index, s->vpp_handle, s->session_state,
2037 vppcom_session_state_str (s->session_state));
2038 return vcl_session_closed_error (s);;
2041 is_ct = vcl_session_is_ct (s);
2042 tx_fifo = is_ct ? s->ct_tx_fifo : s->tx_fifo;
2043 is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
2045 mq = wrk->app_event_queue;
2046 if (svm_fifo_is_full_prod (tx_fifo))
2050 return VPPCOM_EWOULDBLOCK;
2052 while (svm_fifo_is_full_prod (tx_fifo))
2054 svm_fifo_add_want_deq_ntf (tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
2055 if (vcl_session_is_closing (s))
2056 return vcl_session_closing_error (s);
2057 svm_msg_q_lock (mq);
2058 if (svm_msg_q_is_empty (mq))
2059 svm_msg_q_wait (mq);
2061 svm_msg_q_sub_w_lock (mq, &msg);
2062 e = svm_msg_q_msg_data (mq, &msg);
2063 svm_msg_q_unlock (mq);
2065 if (!vcl_is_tx_evt_for_session (e, s->session_index, is_ct))
2066 vcl_handle_mq_event (wrk, e);
2067 svm_msg_q_free_msg (mq, &msg);
2071 et = SESSION_IO_EVT_TX;
2072 if (is_flush && !is_ct)
2073 et = SESSION_IO_EVT_TX_FLUSH;
2076 n_write = app_send_dgram_raw (tx_fifo, &s->transport,
2077 s->vpp_evt_q, buf, n, et,
2078 0 /* do_evt */ , SVM_Q_WAIT);
2080 n_write = app_send_stream_raw (tx_fifo, s->vpp_evt_q, buf, n, et,
2081 0 /* do_evt */ , SVM_Q_WAIT);
2083 if (svm_fifo_set_event (s->tx_fifo))
2084 app_send_io_evt_to_vpp (s->vpp_evt_q, s->tx_fifo->master_session_index,
2087 ASSERT (n_write > 0);
2089 VDBG (2, "session %u [0x%llx]: wrote %d bytes", s->session_index,
2090 s->vpp_handle, n_write);
2096 vppcom_session_write (uint32_t session_handle, void *buf, size_t n)
2098 return vppcom_session_write_inline (session_handle, buf, n,
2103 vppcom_session_write_msg (uint32_t session_handle, void *buf, size_t n)
2105 return vppcom_session_write_inline (session_handle, buf, n,
2109 #define vcl_fifo_rx_evt_valid_or_break(_s) \
2110 if (PREDICT_FALSE (!_s->rx_fifo)) \
2112 if (PREDICT_FALSE (svm_fifo_is_empty (_s->rx_fifo))) \
2114 if (!vcl_session_is_ct (_s)) \
2116 svm_fifo_unset_event (_s->rx_fifo); \
2117 if (svm_fifo_is_empty (_s->rx_fifo)) \
2120 else if (svm_fifo_is_empty (_s->ct_rx_fifo)) \
2122 svm_fifo_unset_event (_s->ct_rx_fifo); \
2123 if (svm_fifo_is_empty (_s->ct_rx_fifo)) \
2129 vcl_select_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
2130 unsigned long n_bits, unsigned long *read_map,
2131 unsigned long *write_map,
2132 unsigned long *except_map, u32 * bits_set)
2134 session_disconnected_msg_t *disconnected_msg;
2135 session_connected_msg_t *connected_msg;
2136 vcl_session_t *session;
2139 switch (e->event_type)
2141 case SESSION_IO_EVT_RX:
2142 sid = e->session_index;
2143 session = vcl_session_get (wrk, sid);
2146 vcl_fifo_rx_evt_valid_or_break (session);
2147 if (sid < n_bits && read_map)
2149 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
2153 case SESSION_IO_EVT_TX:
2154 sid = e->session_index;
2155 session = vcl_session_get (wrk, sid);
2158 if (sid < n_bits && write_map)
2160 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
2164 case SESSION_CTRL_EVT_ACCEPTED:
2165 session = vcl_session_accepted (wrk,
2166 (session_accepted_msg_t *) e->data);
2169 sid = session->session_index;
2170 if (sid < n_bits && read_map)
2172 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
2176 case SESSION_CTRL_EVT_CONNECTED:
2177 connected_msg = (session_connected_msg_t *) e->data;
2178 sid = vcl_session_connected_handler (wrk, connected_msg);
2179 if (sid == VCL_INVALID_SESSION_INDEX)
2181 if (sid < n_bits && write_map)
2183 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
2187 case SESSION_CTRL_EVT_DISCONNECTED:
2188 disconnected_msg = (session_disconnected_msg_t *) e->data;
2189 session = vcl_session_disconnected_handler (wrk, disconnected_msg);
2192 sid = session->session_index;
2193 if (sid < n_bits && except_map)
2195 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
2199 case SESSION_CTRL_EVT_RESET:
2200 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
2201 if (sid < n_bits && except_map)
2203 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
2207 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
2208 vcl_session_unlisten_reply_handler (wrk, e->data);
2210 case SESSION_CTRL_EVT_MIGRATED:
2211 vcl_session_migrated_handler (wrk, e->data);
2213 case SESSION_CTRL_EVT_CLEANUP:
2214 vcl_session_cleanup_handler (wrk, e->data);
2216 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
2217 vcl_session_worker_update_reply_handler (wrk, e->data);
2219 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
2220 vcl_session_req_worker_update_handler (wrk, e->data);
2222 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
2223 vcl_session_app_add_segment_handler (wrk, e->data);
2225 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
2226 vcl_session_app_del_segment_handler (wrk, e->data);
2229 clib_warning ("unhandled: %u", e->event_type);
2235 vcl_select_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
2236 unsigned long n_bits, unsigned long *read_map,
2237 unsigned long *write_map, unsigned long *except_map,
2238 double time_to_wait, u32 * bits_set)
2240 svm_msg_q_msg_t *msg;
2244 svm_msg_q_lock (mq);
2245 if (svm_msg_q_is_empty (mq))
2249 svm_msg_q_unlock (mq);
2255 svm_msg_q_unlock (mq);
2258 else if (time_to_wait < 0)
2260 svm_msg_q_wait (mq);
2264 if (svm_msg_q_timedwait (mq, time_to_wait))
2266 svm_msg_q_unlock (mq);
2271 vcl_mq_dequeue_batch (wrk, mq, ~0);
2272 svm_msg_q_unlock (mq);
2274 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
2276 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
2277 e = svm_msg_q_msg_data (mq, msg);
2278 vcl_select_handle_mq_event (wrk, e, n_bits, read_map, write_map,
2279 except_map, bits_set);
2280 svm_msg_q_free_msg (mq, msg);
2282 vec_reset_length (wrk->mq_msg_vector);
2283 vcl_handle_pending_wrk_updates (wrk);
2288 vppcom_select_condvar (vcl_worker_t * wrk, int n_bits,
2289 vcl_si_set * read_map, vcl_si_set * write_map,
2290 vcl_si_set * except_map, double time_to_wait,
2293 double wait = 0, start = 0;
2297 wait = time_to_wait;
2298 start = clib_time_now (&wrk->clib_time);
2303 vcl_select_handle_mq (wrk, wrk->app_event_queue, n_bits, read_map,
2304 write_map, except_map, wait, bits_set);
2310 wait = wait - (clib_time_now (&wrk->clib_time) - start);
2318 vppcom_select_eventfd (vcl_worker_t * wrk, int n_bits,
2319 vcl_si_set * read_map, vcl_si_set * write_map,
2320 vcl_si_set * except_map, double time_to_wait,
2323 vcl_mq_evt_conn_t *mqc;
2324 int __clib_unused n_read;
2328 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
2329 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
2330 vec_len (wrk->mq_events), time_to_wait);
2331 for (i = 0; i < n_mq_evts; i++)
2333 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
2334 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
2335 vcl_select_handle_mq (wrk, mqc->mq, n_bits, read_map, write_map,
2336 except_map, 0, bits_set);
2339 return (n_mq_evts > 0 ? (int) *bits_set : 0);
2343 vppcom_select (int n_bits, vcl_si_set * read_map, vcl_si_set * write_map,
2344 vcl_si_set * except_map, double time_to_wait)
2346 u32 sid, minbits = clib_max (n_bits, BITS (uword)), bits_set = 0;
2347 vcl_worker_t *wrk = vcl_worker_get_current ();
2348 vcl_session_t *session = 0;
2351 if (n_bits && read_map)
2353 clib_bitmap_validate (wrk->rd_bitmap, minbits);
2354 clib_memcpy_fast (wrk->rd_bitmap, read_map,
2355 vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
2356 memset (read_map, 0, vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
2358 if (n_bits && write_map)
2360 clib_bitmap_validate (wrk->wr_bitmap, minbits);
2361 clib_memcpy_fast (wrk->wr_bitmap, write_map,
2362 vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
2363 memset (write_map, 0, vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
2365 if (n_bits && except_map)
2367 clib_bitmap_validate (wrk->ex_bitmap, minbits);
2368 clib_memcpy_fast (wrk->ex_bitmap, except_map,
2369 vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
2370 memset (except_map, 0, vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
2380 clib_bitmap_foreach (sid, wrk->wr_bitmap, ({
2381 if (!(session = vcl_session_get (wrk, sid)))
2383 if (except_map && sid < minbits)
2384 clib_bitmap_set_no_check (except_map, sid, 1);
2388 rv = svm_fifo_is_full_prod (session->tx_fifo);
2391 clib_bitmap_set_no_check ((uword*)write_map, sid, 1);
2395 svm_fifo_add_want_deq_ntf (session->tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
2402 clib_bitmap_foreach (sid, wrk->rd_bitmap, ({
2403 if (!(session = vcl_session_get (wrk, sid)))
2405 if (except_map && sid < minbits)
2406 clib_bitmap_set_no_check (except_map, sid, 1);
2410 rv = vcl_session_read_ready (session);
2413 clib_bitmap_set_no_check ((uword*)read_map, sid, 1);
2421 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
2423 vcl_select_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i], n_bits,
2424 read_map, write_map, except_map, &bits_set);
2426 vec_reset_length (wrk->unhandled_evts_vector);
2428 if (vcm->cfg.use_mq_eventfd)
2429 vppcom_select_eventfd (wrk, n_bits, read_map, write_map, except_map,
2430 time_to_wait, &bits_set);
2432 vppcom_select_condvar (wrk, n_bits, read_map, write_map, except_map,
2433 time_to_wait, &bits_set);
2439 vep_verify_epoll_chain (vcl_worker_t * wrk, u32 vep_handle)
2441 vcl_session_t *session;
2442 vppcom_epoll_t *vep;
2443 u32 sh = vep_handle;
2445 if (VPPCOM_DEBUG <= 2)
2448 session = vcl_session_get_w_handle (wrk, vep_handle);
2449 if (PREDICT_FALSE (!session))
2451 VDBG (0, "ERROR: Invalid vep_sh (%u)!", vep_handle);
2454 if (PREDICT_FALSE (!session->is_vep))
2456 VDBG (0, "ERROR: vep_sh (%u) is not a vep!", vep_handle);
2459 vep = &session->vep;
2460 VDBG (0, "vep_sh (%u): Dumping epoll chain\n"
2463 " is_vep_session = %u\n"
2464 " next_sh = 0x%x (%u)\n"
2465 "}\n", vep_handle, session->is_vep, session->is_vep_session,
2466 vep->next_sh, vep->next_sh);
2468 for (sh = vep->next_sh; sh != ~0; sh = vep->next_sh)
2470 session = vcl_session_get_w_handle (wrk, sh);
2471 if (PREDICT_FALSE (!session))
2473 VDBG (0, "ERROR: Invalid sh (%u)!", sh);
2476 if (PREDICT_FALSE (session->is_vep))
2478 VDBG (0, "ERROR: sh (%u) is a vep!", vep_handle);
2480 else if (PREDICT_FALSE (!session->is_vep_session))
2482 VDBG (0, "ERROR: sh (%u) is not a vep session handle!", sh);
2485 vep = &session->vep;
2486 if (PREDICT_FALSE (vep->vep_sh != vep_handle))
2487 VDBG (0, "ERROR: session (%u) vep_sh (%u) != vep_sh (%u)!",
2488 sh, session->vep.vep_sh, vep_handle);
2489 if (session->is_vep_session)
2491 VDBG (0, "vep_sh[%u]: sh 0x%x (%u)\n"
2493 " next_sh = 0x%x (%u)\n"
2494 " prev_sh = 0x%x (%u)\n"
2495 " vep_sh = 0x%x (%u)\n"
2496 " ev.events = 0x%x\n"
2497 " ev.data.u64 = 0x%llx\n"
2500 vep_handle, sh, sh, vep->next_sh, vep->next_sh, vep->prev_sh,
2501 vep->prev_sh, vep->vep_sh, vep->vep_sh, vep->ev.events,
2502 vep->ev.data.u64, vep->et_mask);
2507 VDBG (0, "vep_sh (%u): Dump complete!\n", vep_handle);
2511 vppcom_epoll_create (void)
2513 vcl_worker_t *wrk = vcl_worker_get_current ();
2514 vcl_session_t *vep_session;
2516 vep_session = vcl_session_alloc (wrk);
2518 vep_session->is_vep = 1;
2519 vep_session->vep.vep_sh = ~0;
2520 vep_session->vep.next_sh = ~0;
2521 vep_session->vep.prev_sh = ~0;
2522 vep_session->vpp_handle = ~0;
2524 vcl_evt (VCL_EVT_EPOLL_CREATE, vep_session, vep_session->session_index);
2525 VDBG (0, "Created vep_idx %u", vep_session->session_index);
2527 return vcl_session_handle (vep_session);
2531 vppcom_epoll_ctl (uint32_t vep_handle, int op, uint32_t session_handle,
2532 struct epoll_event *event)
2534 vcl_worker_t *wrk = vcl_worker_get_current ();
2535 vcl_session_t *vep_session;
2536 vcl_session_t *session;
2539 if (vep_handle == session_handle)
2541 VDBG (0, "vep_sh == session handle (%u)!", vep_handle);
2542 return VPPCOM_EINVAL;
2545 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
2546 if (PREDICT_FALSE (!vep_session))
2548 VDBG (0, "Invalid vep_sh (%u)!", vep_handle);
2549 return VPPCOM_EBADFD;
2551 if (PREDICT_FALSE (!vep_session->is_vep))
2553 VDBG (0, "vep_sh (%u) is not a vep!", vep_handle);
2554 return VPPCOM_EINVAL;
2557 ASSERT (vep_session->vep.vep_sh == ~0);
2558 ASSERT (vep_session->vep.prev_sh == ~0);
2560 session = vcl_session_get_w_handle (wrk, session_handle);
2561 if (PREDICT_FALSE (!session))
2563 VDBG (0, "Invalid session_handle (%u)!", session_handle);
2564 return VPPCOM_EBADFD;
2566 if (PREDICT_FALSE (session->is_vep))
2568 VDBG (0, "session_handle (%u) is a vep!", vep_handle);
2569 return VPPCOM_EINVAL;
2575 if (PREDICT_FALSE (!event))
2577 VDBG (0, "EPOLL_CTL_ADD: NULL pointer to epoll_event structure!");
2578 return VPPCOM_EINVAL;
2580 if (vep_session->vep.next_sh != ~0)
2582 vcl_session_t *next_session;
2583 next_session = vcl_session_get_w_handle (wrk,
2584 vep_session->vep.next_sh);
2585 if (PREDICT_FALSE (!next_session))
2587 VDBG (0, "EPOLL_CTL_ADD: Invalid vep.next_sh (%u) on "
2588 "vep_idx (%u)!", vep_session->vep.next_sh, vep_handle);
2589 return VPPCOM_EBADFD;
2591 ASSERT (next_session->vep.prev_sh == vep_handle);
2592 next_session->vep.prev_sh = session_handle;
2594 session->vep.next_sh = vep_session->vep.next_sh;
2595 session->vep.prev_sh = vep_handle;
2596 session->vep.vep_sh = vep_handle;
2597 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
2598 session->vep.ev = *event;
2599 session->is_vep = 0;
2600 session->is_vep_session = 1;
2601 vep_session->vep.next_sh = session_handle;
2603 if (session->tx_fifo)
2604 svm_fifo_add_want_deq_ntf (session->tx_fifo,
2605 SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
2607 /* Generate EPOLLOUT if tx fifo not full */
2608 if ((event->events & EPOLLOUT) &&
2609 (vcl_session_write_ready (session) > 0))
2611 session_event_t e = { 0 };
2612 e.event_type = SESSION_IO_EVT_TX;
2613 e.session_index = session->session_index;
2614 vec_add1 (wrk->unhandled_evts_vector, e);
2616 /* Generate EPOLLIN if rx fifo has data */
2617 if ((event->events & EPOLLIN) && (vcl_session_read_ready (session) > 0))
2619 session_event_t e = { 0 };
2620 e.event_type = SESSION_IO_EVT_RX;
2621 e.session_index = session->session_index;
2622 vec_add1 (wrk->unhandled_evts_vector, e);
2624 VDBG (1, "EPOLL_CTL_ADD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2625 vep_handle, session_handle, event->events, event->data.u64);
2626 vcl_evt (VCL_EVT_EPOLL_CTLADD, session, event->events, event->data.u64);
2630 if (PREDICT_FALSE (!event))
2632 VDBG (0, "EPOLL_CTL_MOD: NULL pointer to epoll_event structure!");
2636 else if (PREDICT_FALSE (!session->is_vep_session))
2638 VDBG (0, "sh %u EPOLL_CTL_MOD: not a vep session!", session_handle);
2642 else if (PREDICT_FALSE (session->vep.vep_sh != vep_handle))
2644 VDBG (0, "EPOLL_CTL_MOD: sh %u vep_sh (%u) != vep_sh (%u)!",
2645 session_handle, session->vep.vep_sh, vep_handle);
2650 /* Generate EPOLLOUT when tx_fifo/ct_tx_fifo not full */
2651 if ((event->events & EPOLLOUT) &&
2652 !(session->vep.ev.events & EPOLLOUT) &&
2653 (vcl_session_write_ready (session) > 0))
2655 session_event_t e = { 0 };
2656 e.event_type = SESSION_IO_EVT_TX;
2657 e.session_index = session->session_index;
2658 vec_add1 (wrk->unhandled_evts_vector, e);
2660 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
2661 session->vep.ev = *event;
2662 VDBG (1, "EPOLL_CTL_MOD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2663 vep_handle, session_handle, event->events, event->data.u64);
2667 if (PREDICT_FALSE (!session->is_vep_session))
2669 VDBG (0, "EPOLL_CTL_DEL: %u not a vep session!", session_handle);
2673 else if (PREDICT_FALSE (session->vep.vep_sh != vep_handle))
2675 VDBG (0, "EPOLL_CTL_DEL: sh %u vep_sh (%u) != vep_sh (%u)!",
2676 session_handle, session->vep.vep_sh, vep_handle);
2681 if (session->vep.prev_sh == vep_handle)
2682 vep_session->vep.next_sh = session->vep.next_sh;
2685 vcl_session_t *prev_session;
2686 prev_session = vcl_session_get_w_handle (wrk, session->vep.prev_sh);
2687 if (PREDICT_FALSE (!prev_session))
2689 VDBG (0, "EPOLL_CTL_DEL: Invalid prev_sh (%u) on sh (%u)!",
2690 session->vep.prev_sh, session_handle);
2691 return VPPCOM_EBADFD;
2693 ASSERT (prev_session->vep.next_sh == session_handle);
2694 prev_session->vep.next_sh = session->vep.next_sh;
2696 if (session->vep.next_sh != ~0)
2698 vcl_session_t *next_session;
2699 next_session = vcl_session_get_w_handle (wrk, session->vep.next_sh);
2700 if (PREDICT_FALSE (!next_session))
2702 VDBG (0, "EPOLL_CTL_DEL: Invalid next_sh (%u) on sh (%u)!",
2703 session->vep.next_sh, session_handle);
2704 return VPPCOM_EBADFD;
2706 ASSERT (next_session->vep.prev_sh == session_handle);
2707 next_session->vep.prev_sh = session->vep.prev_sh;
2710 memset (&session->vep, 0, sizeof (session->vep));
2711 session->vep.next_sh = ~0;
2712 session->vep.prev_sh = ~0;
2713 session->vep.vep_sh = ~0;
2714 session->is_vep_session = 0;
2716 if (session->tx_fifo)
2717 svm_fifo_del_want_deq_ntf (session->tx_fifo, SVM_FIFO_NO_DEQ_NOTIF);
2719 VDBG (1, "EPOLL_CTL_DEL: vep_idx %u, sh %u!", vep_handle,
2721 vcl_evt (VCL_EVT_EPOLL_CTLDEL, session, vep_sh);
2725 VDBG (0, "Invalid operation (%d)!", op);
2729 vep_verify_epoll_chain (wrk, vep_handle);
2736 vcl_epoll_wait_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
2737 struct epoll_event *events, u32 * num_ev)
2739 session_disconnected_msg_t *disconnected_msg;
2740 session_connected_msg_t *connected_msg;
2741 u32 sid = ~0, session_events;
2742 u64 session_evt_data = ~0;
2743 vcl_session_t *session;
2746 switch (e->event_type)
2748 case SESSION_IO_EVT_RX:
2749 sid = e->session_index;
2750 if (!(session = vcl_session_get (wrk, sid)))
2752 vcl_fifo_rx_evt_valid_or_break (session);
2753 session_events = session->vep.ev.events;
2754 if (!(EPOLLIN & session->vep.ev.events) || session->has_rx_evt)
2757 events[*num_ev].events |= EPOLLIN;
2758 session_evt_data = session->vep.ev.data.u64;
2759 session->has_rx_evt = 1;
2761 case SESSION_IO_EVT_TX:
2762 sid = e->session_index;
2763 if (!(session = vcl_session_get (wrk, sid)))
2765 session_events = session->vep.ev.events;
2766 if (!(EPOLLOUT & session_events))
2769 events[*num_ev].events |= EPOLLOUT;
2770 session_evt_data = session->vep.ev.data.u64;
2771 svm_fifo_reset_has_deq_ntf (session->tx_fifo);
2773 case SESSION_CTRL_EVT_ACCEPTED:
2774 session = vcl_session_accepted (wrk,
2775 (session_accepted_msg_t *) e->data);
2779 session_events = session->vep.ev.events;
2780 if (!(EPOLLIN & session_events))
2784 events[*num_ev].events |= EPOLLIN;
2785 session_evt_data = session->vep.ev.data.u64;
2787 case SESSION_CTRL_EVT_CONNECTED:
2788 connected_msg = (session_connected_msg_t *) e->data;
2789 sid = vcl_session_connected_handler (wrk, connected_msg);
2790 /* Generate EPOLLOUT because there's no connected event */
2791 if (!(session = vcl_session_get (wrk, sid)))
2793 session_events = session->vep.ev.events;
2794 if (!(EPOLLOUT & session_events))
2797 events[*num_ev].events |= EPOLLOUT;
2798 session_evt_data = session->vep.ev.data.u64;
2799 if (session->session_state & STATE_DETACHED)
2800 events[*num_ev].events |= EPOLLHUP;
2802 case SESSION_CTRL_EVT_DISCONNECTED:
2803 disconnected_msg = (session_disconnected_msg_t *) e->data;
2804 session = vcl_session_disconnected_handler (wrk, disconnected_msg);
2807 session_events = session->vep.ev.events;
2809 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
2810 session_evt_data = session->vep.ev.data.u64;
2812 case SESSION_CTRL_EVT_RESET:
2813 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
2814 if (!(session = vcl_session_get (wrk, sid)))
2816 session_events = session->vep.ev.events;
2818 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
2819 session_evt_data = session->vep.ev.data.u64;
2821 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
2822 vcl_session_unlisten_reply_handler (wrk, e->data);
2824 case SESSION_CTRL_EVT_MIGRATED:
2825 vcl_session_migrated_handler (wrk, e->data);
2827 case SESSION_CTRL_EVT_CLEANUP:
2828 vcl_session_cleanup_handler (wrk, e->data);
2830 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
2831 vcl_session_req_worker_update_handler (wrk, e->data);
2833 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
2834 vcl_session_worker_update_reply_handler (wrk, e->data);
2836 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
2837 vcl_session_app_add_segment_handler (wrk, e->data);
2839 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
2840 vcl_session_app_del_segment_handler (wrk, e->data);
2843 VDBG (0, "unhandled: %u", e->event_type);
2849 events[*num_ev].data.u64 = session_evt_data;
2850 if (EPOLLONESHOT & session_events)
2852 session = vcl_session_get (wrk, sid);
2853 session->vep.ev.events = 0;
2860 vcl_epoll_wait_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
2861 struct epoll_event *events, u32 maxevents,
2862 double wait_for_time, u32 * num_ev)
2864 svm_msg_q_msg_t *msg;
2868 if (vec_len (wrk->mq_msg_vector) && svm_msg_q_is_empty (mq))
2869 goto handle_dequeued;
2871 svm_msg_q_lock (mq);
2872 if (svm_msg_q_is_empty (mq))
2876 svm_msg_q_unlock (mq);
2879 else if (wait_for_time < 0)
2881 svm_msg_q_wait (mq);
2885 if (svm_msg_q_timedwait (mq, wait_for_time / 1e3))
2887 svm_msg_q_unlock (mq);
2892 ASSERT (maxevents > *num_ev);
2893 vcl_mq_dequeue_batch (wrk, mq, maxevents - *num_ev);
2894 svm_msg_q_unlock (mq);
2897 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
2899 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
2900 e = svm_msg_q_msg_data (mq, msg);
2901 vcl_epoll_wait_handle_mq_event (wrk, e, events, num_ev);
2902 svm_msg_q_free_msg (mq, msg);
2904 vec_reset_length (wrk->mq_msg_vector);
2905 vcl_handle_pending_wrk_updates (wrk);
2910 vppcom_epoll_wait_condvar (vcl_worker_t * wrk, struct epoll_event *events,
2911 int maxevents, u32 n_evts, double wait_for_time)
2913 double wait = 0, start = 0, now;
2917 wait = wait_for_time;
2918 start = clib_time_now (&wrk->clib_time);
2923 vcl_epoll_wait_handle_mq (wrk, wrk->app_event_queue, events, maxevents,
2930 now = clib_time_now (&wrk->clib_time);
2931 wait -= now - start;
2940 vppcom_epoll_wait_eventfd (vcl_worker_t * wrk, struct epoll_event *events,
2941 int maxevents, u32 n_evts, double wait_for_time)
2943 vcl_mq_evt_conn_t *mqc;
2944 int __clib_unused n_read;
2948 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
2950 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
2951 vec_len (wrk->mq_events), wait_for_time);
2952 for (i = 0; i < n_mq_evts; i++)
2954 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
2955 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
2956 vcl_epoll_wait_handle_mq (wrk, mqc->mq, events, maxevents, 0, &n_evts);
2958 if (!n_evts && n_mq_evts > 0)
2961 return (int) n_evts;
2965 vppcom_epoll_wait (uint32_t vep_handle, struct epoll_event *events,
2966 int maxevents, double wait_for_time)
2968 vcl_worker_t *wrk = vcl_worker_get_current ();
2969 vcl_session_t *vep_session;
2973 if (PREDICT_FALSE (maxevents <= 0))
2975 VDBG (0, "ERROR: Invalid maxevents (%d)!", maxevents);
2976 return VPPCOM_EINVAL;
2979 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
2981 return VPPCOM_EBADFD;
2983 if (PREDICT_FALSE (!vep_session->is_vep))
2985 VDBG (0, "ERROR: vep_idx (%u) is not a vep!", vep_handle);
2986 return VPPCOM_EINVAL;
2989 memset (events, 0, sizeof (*events) * maxevents);
2991 if (vec_len (wrk->unhandled_evts_vector))
2993 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
2995 vcl_epoll_wait_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i],
2997 if (n_evts == maxevents)
2999 vec_delete (wrk->unhandled_evts_vector, i + 1, 0);
3003 vec_reset_length (wrk->unhandled_evts_vector);
3006 if (vcm->cfg.use_mq_eventfd)
3007 return vppcom_epoll_wait_eventfd (wrk, events, maxevents, n_evts,
3010 return vppcom_epoll_wait_condvar (wrk, events, maxevents, n_evts,
3015 vppcom_session_attr (uint32_t session_handle, uint32_t op,
3016 void *buffer, uint32_t * buflen)
3018 vcl_worker_t *wrk = vcl_worker_get_current ();
3019 vcl_session_t *session;
3021 u32 *flags = buffer, tmp_flags = 0;
3022 vppcom_endpt_t *ep = buffer;
3024 session = vcl_session_get_w_handle (wrk, session_handle);
3026 return VPPCOM_EBADFD;
3030 case VPPCOM_ATTR_GET_NREAD:
3031 rv = vcl_session_read_ready (session);
3032 VDBG (2, "VPPCOM_ATTR_GET_NREAD: sh %u, nread = %d", session_handle,
3036 case VPPCOM_ATTR_GET_NWRITE:
3037 rv = vcl_session_write_ready (session);
3038 VDBG (2, "VPPCOM_ATTR_GET_NWRITE: sh %u, nwrite = %d", session_handle,
3042 case VPPCOM_ATTR_GET_FLAGS:
3043 if (PREDICT_TRUE (buffer && buflen && (*buflen >= sizeof (*flags))))
3045 *flags = O_RDWR | (VCL_SESS_ATTR_TEST (session->attr,
3046 VCL_SESS_ATTR_NONBLOCK));
3047 *buflen = sizeof (*flags);
3048 VDBG (2, "VPPCOM_ATTR_GET_FLAGS: sh %u, flags = 0x%08x, "
3049 "is_nonblocking = %u", session_handle, *flags,
3050 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK));
3056 case VPPCOM_ATTR_SET_FLAGS:
3057 if (PREDICT_TRUE (buffer && buflen && (*buflen == sizeof (*flags))))
3059 if (*flags & O_NONBLOCK)
3060 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK);
3062 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_NONBLOCK);
3064 VDBG (2, "VPPCOM_ATTR_SET_FLAGS: sh %u, flags = 0x%08x,"
3065 " is_nonblocking = %u", session_handle, *flags,
3066 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK));
3072 case VPPCOM_ATTR_GET_PEER_ADDR:
3073 if (PREDICT_TRUE (buffer && buflen &&
3074 (*buflen >= sizeof (*ep)) && ep->ip))
3076 ep->is_ip4 = session->transport.is_ip4;
3077 ep->port = session->transport.rmt_port;
3078 if (session->transport.is_ip4)
3079 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
3080 sizeof (ip4_address_t));
3082 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
3083 sizeof (ip6_address_t));
3084 *buflen = sizeof (*ep);
3085 VDBG (1, "VPPCOM_ATTR_GET_PEER_ADDR: sh %u, is_ip4 = %u, "
3086 "addr = %U, port %u", session_handle, ep->is_ip4,
3087 format_ip46_address, &session->transport.rmt_ip,
3088 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3089 clib_net_to_host_u16 (ep->port));
3095 case VPPCOM_ATTR_GET_LCL_ADDR:
3096 if (PREDICT_TRUE (buffer && buflen &&
3097 (*buflen >= sizeof (*ep)) && ep->ip))
3099 ep->is_ip4 = session->transport.is_ip4;
3100 ep->port = session->transport.lcl_port;
3101 if (session->transport.is_ip4)
3102 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip4,
3103 sizeof (ip4_address_t));
3105 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip6,
3106 sizeof (ip6_address_t));
3107 *buflen = sizeof (*ep);
3108 VDBG (1, "VPPCOM_ATTR_GET_LCL_ADDR: sh %u, is_ip4 = %u, addr = %U"
3109 " port %d", session_handle, ep->is_ip4, format_ip46_address,
3110 &session->transport.lcl_ip,
3111 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3112 clib_net_to_host_u16 (ep->port));
3118 case VPPCOM_ATTR_SET_LCL_ADDR:
3119 if (PREDICT_TRUE (buffer && buflen &&
3120 (*buflen >= sizeof (*ep)) && ep->ip))
3122 session->transport.is_ip4 = ep->is_ip4;
3123 session->transport.lcl_port = ep->port;
3124 vcl_ip_copy_from_ep (&session->transport.lcl_ip, ep);
3125 *buflen = sizeof (*ep);
3126 VDBG (1, "VPPCOM_ATTR_SET_LCL_ADDR: sh %u, is_ip4 = %u, addr = %U"
3127 " port %d", session_handle, ep->is_ip4, format_ip46_address,
3128 &session->transport.lcl_ip,
3129 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3130 clib_net_to_host_u16 (ep->port));
3136 case VPPCOM_ATTR_GET_LIBC_EPFD:
3137 rv = session->libc_epfd;
3138 VDBG (2, "VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd %d", rv);
3141 case VPPCOM_ATTR_SET_LIBC_EPFD:
3142 if (PREDICT_TRUE (buffer && buflen &&
3143 (*buflen == sizeof (session->libc_epfd))))
3145 session->libc_epfd = *(int *) buffer;
3146 *buflen = sizeof (session->libc_epfd);
3148 VDBG (2, "VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd %d, buflen %d",
3149 session->libc_epfd, *buflen);
3155 case VPPCOM_ATTR_GET_PROTOCOL:
3156 if (buffer && buflen && (*buflen >= sizeof (int)))
3158 *(int *) buffer = session->session_type;
3159 *buflen = sizeof (int);
3161 VDBG (2, "VPPCOM_ATTR_GET_PROTOCOL: %d (%s), buflen %d",
3162 *(int *) buffer, *(int *) buffer ? "UDP" : "TCP", *buflen);
3168 case VPPCOM_ATTR_GET_LISTEN:
3169 if (buffer && buflen && (*buflen >= sizeof (int)))
3171 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3172 VCL_SESS_ATTR_LISTEN);
3173 *buflen = sizeof (int);
3175 VDBG (2, "VPPCOM_ATTR_GET_LISTEN: %d, buflen %d", *(int *) buffer,
3182 case VPPCOM_ATTR_GET_ERROR:
3183 if (buffer && buflen && (*buflen >= sizeof (int)))
3185 *(int *) buffer = 0;
3186 *buflen = sizeof (int);
3188 VDBG (2, "VPPCOM_ATTR_GET_ERROR: %d, buflen %d, #VPP-TBD#",
3189 *(int *) buffer, *buflen);
3195 case VPPCOM_ATTR_GET_TX_FIFO_LEN:
3196 if (buffer && buflen && (*buflen >= sizeof (u32)))
3200 *(size_t *) buffer = (session->sndbuf_size ? session->sndbuf_size :
3202 svm_fifo_size (session->tx_fifo) :
3203 vcm->cfg.tx_fifo_size);
3204 *buflen = sizeof (u32);
3206 VDBG (2, "VPPCOM_ATTR_GET_TX_FIFO_LEN: %u (0x%x), buflen %d,"
3207 " #VPP-TBD#", *(size_t *) buffer, *(size_t *) buffer,
3214 case VPPCOM_ATTR_SET_TX_FIFO_LEN:
3215 if (buffer && buflen && (*buflen == sizeof (u32)))
3218 session->sndbuf_size = *(u32 *) buffer;
3219 VDBG (2, "VPPCOM_ATTR_SET_TX_FIFO_LEN: %u (0x%x), buflen %d,"
3220 " #VPP-TBD#", session->sndbuf_size, session->sndbuf_size,
3227 case VPPCOM_ATTR_GET_RX_FIFO_LEN:
3228 if (buffer && buflen && (*buflen >= sizeof (u32)))
3232 *(size_t *) buffer = (session->rcvbuf_size ? session->rcvbuf_size :
3234 svm_fifo_size (session->rx_fifo) :
3235 vcm->cfg.rx_fifo_size);
3236 *buflen = sizeof (u32);
3238 VDBG (2, "VPPCOM_ATTR_GET_RX_FIFO_LEN: %u (0x%x), buflen %d, "
3239 "#VPP-TBD#", *(size_t *) buffer, *(size_t *) buffer, *buflen);
3245 case VPPCOM_ATTR_SET_RX_FIFO_LEN:
3246 if (buffer && buflen && (*buflen == sizeof (u32)))
3249 session->rcvbuf_size = *(u32 *) buffer;
3250 VDBG (2, "VPPCOM_ATTR_SET_RX_FIFO_LEN: %u (0x%x), buflen %d,"
3251 " #VPP-TBD#", session->sndbuf_size, session->sndbuf_size,
3258 case VPPCOM_ATTR_GET_REUSEADDR:
3259 if (buffer && buflen && (*buflen >= sizeof (int)))
3262 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3263 VCL_SESS_ATTR_REUSEADDR);
3264 *buflen = sizeof (int);
3266 VDBG (2, "VPPCOM_ATTR_GET_REUSEADDR: %d, buflen %d, #VPP-TBD#",
3267 *(int *) buffer, *buflen);
3273 case VPPCOM_ATTR_SET_REUSEADDR:
3274 if (buffer && buflen && (*buflen == sizeof (int)) &&
3275 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
3278 if (*(int *) buffer)
3279 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEADDR);
3281 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEADDR);
3283 VDBG (2, "VPPCOM_ATTR_SET_REUSEADDR: %d, buflen %d, #VPP-TBD#",
3284 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_REUSEADDR),
3291 case VPPCOM_ATTR_GET_REUSEPORT:
3292 if (buffer && buflen && (*buflen >= sizeof (int)))
3295 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3296 VCL_SESS_ATTR_REUSEPORT);
3297 *buflen = sizeof (int);
3299 VDBG (2, "VPPCOM_ATTR_GET_REUSEPORT: %d, buflen %d, #VPP-TBD#",
3300 *(int *) buffer, *buflen);
3306 case VPPCOM_ATTR_SET_REUSEPORT:
3307 if (buffer && buflen && (*buflen == sizeof (int)) &&
3308 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
3311 if (*(int *) buffer)
3312 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEPORT);
3314 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEPORT);
3316 VDBG (2, "VPPCOM_ATTR_SET_REUSEPORT: %d, buflen %d, #VPP-TBD#",
3317 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_REUSEPORT),
3324 case VPPCOM_ATTR_GET_BROADCAST:
3325 if (buffer && buflen && (*buflen >= sizeof (int)))
3328 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3329 VCL_SESS_ATTR_BROADCAST);
3330 *buflen = sizeof (int);
3332 VDBG (2, "VPPCOM_ATTR_GET_BROADCAST: %d, buflen %d, #VPP-TBD#",
3333 *(int *) buffer, *buflen);
3339 case VPPCOM_ATTR_SET_BROADCAST:
3340 if (buffer && buflen && (*buflen == sizeof (int)))
3343 if (*(int *) buffer)
3344 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_BROADCAST);
3346 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_BROADCAST);
3348 VDBG (2, "VPPCOM_ATTR_SET_BROADCAST: %d, buflen %d, #VPP-TBD#",
3349 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_BROADCAST),
3356 case VPPCOM_ATTR_GET_V6ONLY:
3357 if (buffer && buflen && (*buflen >= sizeof (int)))
3360 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3361 VCL_SESS_ATTR_V6ONLY);
3362 *buflen = sizeof (int);
3364 VDBG (2, "VPPCOM_ATTR_GET_V6ONLY: %d, buflen %d, #VPP-TBD#",
3365 *(int *) buffer, *buflen);
3371 case VPPCOM_ATTR_SET_V6ONLY:
3372 if (buffer && buflen && (*buflen == sizeof (int)))
3375 if (*(int *) buffer)
3376 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_V6ONLY);
3378 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_V6ONLY);
3380 VDBG (2, "VPPCOM_ATTR_SET_V6ONLY: %d, buflen %d, #VPP-TBD#",
3381 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_V6ONLY),
3388 case VPPCOM_ATTR_GET_KEEPALIVE:
3389 if (buffer && buflen && (*buflen >= sizeof (int)))
3392 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3393 VCL_SESS_ATTR_KEEPALIVE);
3394 *buflen = sizeof (int);
3396 VDBG (2, "VPPCOM_ATTR_GET_KEEPALIVE: %d, buflen %d, #VPP-TBD#",
3397 *(int *) buffer, *buflen);
3403 case VPPCOM_ATTR_SET_KEEPALIVE:
3404 if (buffer && buflen && (*buflen == sizeof (int)))
3407 if (*(int *) buffer)
3408 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_KEEPALIVE);
3410 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_KEEPALIVE);
3412 VDBG (2, "VPPCOM_ATTR_SET_KEEPALIVE: %d, buflen %d, #VPP-TBD#",
3413 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_KEEPALIVE),
3420 case VPPCOM_ATTR_GET_TCP_NODELAY:
3421 if (buffer && buflen && (*buflen >= sizeof (int)))
3424 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3425 VCL_SESS_ATTR_TCP_NODELAY);
3426 *buflen = sizeof (int);
3428 VDBG (2, "VPPCOM_ATTR_GET_TCP_NODELAY: %d, buflen %d, #VPP-TBD#",
3429 *(int *) buffer, *buflen);
3435 case VPPCOM_ATTR_SET_TCP_NODELAY:
3436 if (buffer && buflen && (*buflen == sizeof (int)))
3439 if (*(int *) buffer)
3440 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
3442 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
3444 VDBG (2, "VPPCOM_ATTR_SET_TCP_NODELAY: %d, buflen %d, #VPP-TBD#",
3445 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_TCP_NODELAY),
3452 case VPPCOM_ATTR_GET_TCP_KEEPIDLE:
3453 if (buffer && buflen && (*buflen >= sizeof (int)))
3456 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3457 VCL_SESS_ATTR_TCP_KEEPIDLE);
3458 *buflen = sizeof (int);
3460 VDBG (2, "VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d, buflen %d, #VPP-TBD#",
3461 *(int *) buffer, *buflen);
3467 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
3468 if (buffer && buflen && (*buflen == sizeof (int)))
3471 if (*(int *) buffer)
3472 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
3474 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
3476 VDBG (2, "VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d, buflen %d, #VPP-TBD#",
3477 VCL_SESS_ATTR_TEST (session->attr,
3478 VCL_SESS_ATTR_TCP_KEEPIDLE), *buflen);
3484 case VPPCOM_ATTR_GET_TCP_KEEPINTVL:
3485 if (buffer && buflen && (*buflen >= sizeof (int)))
3488 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3489 VCL_SESS_ATTR_TCP_KEEPINTVL);
3490 *buflen = sizeof (int);
3492 VDBG (2, "VPPCOM_ATTR_GET_TCP_KEEPINTVL: %d, buflen %d, #VPP-TBD#",
3493 *(int *) buffer, *buflen);
3499 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
3500 if (buffer && buflen && (*buflen == sizeof (int)))
3503 if (*(int *) buffer)
3504 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
3506 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
3508 VDBG (2, "VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d, buflen %d, #VPP-TBD#",
3509 VCL_SESS_ATTR_TEST (session->attr,
3510 VCL_SESS_ATTR_TCP_KEEPINTVL), *buflen);
3516 case VPPCOM_ATTR_GET_TCP_USER_MSS:
3517 if (buffer && buflen && (*buflen >= sizeof (u32)))
3520 *(u32 *) buffer = session->user_mss;
3521 *buflen = sizeof (int);
3523 VDBG (2, "VPPCOM_ATTR_GET_TCP_USER_MSS: %d, buflen %d, #VPP-TBD#",
3524 *(int *) buffer, *buflen);
3530 case VPPCOM_ATTR_SET_TCP_USER_MSS:
3531 if (buffer && buflen && (*buflen == sizeof (u32)))
3534 session->user_mss = *(u32 *) buffer;
3536 VDBG (2, "VPPCOM_ATTR_SET_TCP_USER_MSS: %u, buflen %d, #VPP-TBD#",
3537 session->user_mss, *buflen);
3543 case VPPCOM_ATTR_SET_SHUT:
3544 if (*flags == SHUT_RD || *flags == SHUT_RDWR)
3545 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_SHUT_RD);
3546 if (*flags == SHUT_WR || *flags == SHUT_RDWR)
3547 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_SHUT_WR);
3550 case VPPCOM_ATTR_GET_SHUT:
3551 if (VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_SHUT_RD))
3553 if (VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_SHUT_WR))
3556 *(int *) buffer = SHUT_RD;
3557 else if (tmp_flags == 2)
3558 *(int *) buffer = SHUT_WR;
3559 else if (tmp_flags == 3)
3560 *(int *) buffer = SHUT_RDWR;
3561 *buflen = sizeof (int);
3572 vppcom_session_recvfrom (uint32_t session_handle, void *buffer,
3573 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3575 vcl_worker_t *wrk = vcl_worker_get_current ();
3576 vcl_session_t *session;
3580 rv = vppcom_session_read (session_handle, buffer, buflen);
3581 else if (flags & MSG_PEEK)
3582 rv = vppcom_session_peek (session_handle, buffer, buflen);
3585 VDBG (0, "Unsupport flags for recvfrom %d", flags);
3586 return VPPCOM_EAFNOSUPPORT;
3591 session = vcl_session_get_w_handle (wrk, session_handle);
3592 if (session->transport.is_ip4)
3593 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
3594 sizeof (ip4_address_t));
3596 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
3597 sizeof (ip6_address_t));
3598 ep->is_ip4 = session->transport.is_ip4;
3599 ep->port = session->transport.rmt_port;
3606 vppcom_session_sendto (uint32_t session_handle, void *buffer,
3607 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3610 return VPPCOM_EINVAL;
3614 vcl_worker_t *wrk = vcl_worker_get_current ();
3617 s = vcl_session_get_w_handle (wrk, session_handle);
3619 return VPPCOM_EBADFD;
3621 if (s->session_type != VPPCOM_PROTO_UDP
3622 || (s->flags & VCL_SESSION_F_CONNECTED))
3623 return VPPCOM_EINVAL;
3625 /* Session not connected/bound in vpp. Create it by 'connecting' it */
3626 if (PREDICT_FALSE (s->session_state == STATE_CLOSED))
3628 vcl_send_session_connect (wrk, s);
3632 s->transport.is_ip4 = ep->is_ip4;
3633 s->transport.rmt_port = ep->port;
3634 vcl_ip_copy_from_ep (&s->transport.rmt_ip, ep);
3640 // TBD check the flags and do the right thing
3641 VDBG (2, "handling flags 0x%u (%d) not implemented yet.", flags, flags);
3644 return (vppcom_session_write_inline (session_handle, buffer, buflen, 1));
3648 vppcom_poll (vcl_poll_t * vp, uint32_t n_sids, double wait_for_time)
3650 vcl_worker_t *wrk = vcl_worker_get_current ();
3651 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
3652 u32 i, keep_trying = 1;
3653 svm_msg_q_msg_t msg;
3657 VDBG (3, "vp %p, nsids %u, wait_for_time %f", vp, n_sids, wait_for_time);
3660 return VPPCOM_EFAULT;
3664 vcl_session_t *session;
3666 /* Dequeue all events and drop all unhandled io events */
3667 while (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0) == 0)
3669 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
3670 vcl_handle_mq_event (wrk, e);
3671 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
3673 vec_reset_length (wrk->unhandled_evts_vector);
3675 for (i = 0; i < n_sids; i++)
3677 session = vcl_session_get (wrk, vp[i].sh);
3680 vp[i].revents = POLLHUP;
3687 if (POLLIN & vp[i].events)
3689 rv = vcl_session_read_ready (session);
3692 vp[i].revents |= POLLIN;
3699 case VPPCOM_ECONNRESET:
3700 vp[i].revents = POLLHUP;
3704 vp[i].revents = POLLERR;
3711 if (POLLOUT & vp[i].events)
3713 rv = vcl_session_write_ready (session);
3716 vp[i].revents |= POLLOUT;
3723 case VPPCOM_ECONNRESET:
3724 vp[i].revents = POLLHUP;
3728 vp[i].revents = POLLERR;
3735 if (0) // Note "done:" label used by VCL_SESSION_LOCK_AND_GET()
3737 vp[i].revents = POLLNVAL;
3741 if (wait_for_time != -1)
3742 keep_trying = (clib_time_now (&wrk->clib_time) <= timeout) ? 1 : 0;
3744 while ((num_ev == 0) && keep_trying);
3750 vppcom_mq_epoll_fd (void)
3752 vcl_worker_t *wrk = vcl_worker_get_current ();
3753 return wrk->mqs_epfd;
3757 vppcom_session_index (vcl_session_handle_t session_handle)
3759 return session_handle & 0xFFFFFF;
3763 vppcom_session_worker (vcl_session_handle_t session_handle)
3765 return session_handle >> 24;
3769 vppcom_worker_register (void)
3775 if (!vcl_worker_alloc_and_init ())
3776 return VPPCOM_EEXIST;
3778 wrk = vcl_worker_get_current ();
3779 wrk_name = format (0, "%s-wrk-%u", vcm->app_name, wrk->wrk_index);
3781 rv = vppcom_connect_to_vpp ((char *) wrk_name);
3782 vec_free (wrk_name);
3785 return VPPCOM_EFAULT;
3787 if (vcl_worker_register_with_vpp ())
3788 return VPPCOM_EEXIST;
3794 vppcom_worker_unregister (void)
3796 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
3797 vcl_set_worker_index (~0);
3801 vppcom_worker_index (void)
3803 return vcl_get_worker_index ();
3807 vppcom_worker_mqs_epfd (void)
3809 vcl_worker_t *wrk = vcl_worker_get_current ();
3810 if (!vcm->cfg.use_mq_eventfd)
3812 return wrk->mqs_epfd;
3816 vppcom_session_is_connectable_listener (uint32_t session_handle)
3818 vcl_session_t *session;
3819 vcl_worker_t *wrk = vcl_worker_get_current ();
3820 session = vcl_session_get_w_handle (wrk, session_handle);
3822 return VPPCOM_EBADFD;
3823 return vcl_session_is_connectable_listener (wrk, session);
3827 vppcom_session_listener (uint32_t session_handle)
3829 vcl_worker_t *wrk = vcl_worker_get_current ();
3830 vcl_session_t *listen_session, *session;
3831 session = vcl_session_get_w_handle (wrk, session_handle);
3833 return VPPCOM_EBADFD;
3834 if (session->listener_index == VCL_INVALID_SESSION_INDEX)
3835 return VPPCOM_EBADFD;
3836 listen_session = vcl_session_get_w_handle (wrk, session->listener_index);
3837 if (!listen_session)
3838 return VPPCOM_EBADFD;
3839 return vcl_session_handle (listen_session);
3843 vppcom_session_n_accepted (uint32_t session_handle)
3845 vcl_worker_t *wrk = vcl_worker_get_current ();
3846 vcl_session_t *session = vcl_session_get_w_handle (wrk, session_handle);
3848 return VPPCOM_EBADFD;
3849 return session->n_accepted_sessions;
3853 * fd.io coding-style-patch-verification: ON
3856 * eval: (c-set-style "gnu")