2 * Copyright (c) 2017 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 <svm/svm_fifo_segment.h>
19 #include <vcl/vppcom.h>
20 #include <vcl/vcl_event.h>
21 #include <vcl/vcl_debug.h>
22 #include <vcl/vcl_private.h>
27 sigsegv_signal (int signum)
33 vcl_wait_for_memory (void *mem)
35 u8 __clib_unused test;
36 if (vcm->mounting_segment)
38 while (vcm->mounting_segment)
47 if (signal (SIGSEGV, sigsegv_signal))
63 signal (SIGSEGV, SIG_DFL);
67 vppcom_app_state_str (app_state_t state)
74 st = "STATE_APP_START";
77 case STATE_APP_CONN_VPP:
78 st = "STATE_APP_CONN_VPP";
81 case STATE_APP_ENABLED:
82 st = "STATE_APP_ENABLED";
85 case STATE_APP_ATTACHED:
86 st = "STATE_APP_ATTACHED";
90 st = "UNKNOWN_APP_STATE";
98 vppcom_session_state_str (session_state_t state)
109 st = "STATE_CONNECT";
120 case STATE_CLOSE_ON_EMPTY:
121 st = "STATE_CLOSE_ON_EMPTY";
124 case STATE_DISCONNECT:
125 st = "STATE_DISCONNECT";
133 st = "UNKNOWN_STATE";
141 format_ip4_address (u8 * s, va_list * args)
143 u8 *a = va_arg (*args, u8 *);
144 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
148 format_ip6_address (u8 * s, va_list * args)
150 ip6_address_t *a = va_arg (*args, ip6_address_t *);
151 u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
153 i_max_n_zero = ARRAY_LEN (a->as_u16);
155 i_first_zero = i_max_n_zero;
157 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
159 u32 is_zero = a->as_u16[i] == 0;
160 if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
166 if ((!is_zero && n_zeros > max_n_zeros)
167 || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
169 i_max_n_zero = i_first_zero;
170 max_n_zeros = n_zeros;
171 i_first_zero = ARRAY_LEN (a->as_u16);
176 last_double_colon = 0;
177 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
179 if (i == i_max_n_zero && max_n_zeros > 1)
181 s = format (s, "::");
182 i += max_n_zeros - 1;
183 last_double_colon = 1;
187 s = format (s, "%s%x",
188 (last_double_colon || i == 0) ? "" : ":",
189 clib_net_to_host_u16 (a->as_u16[i]));
190 last_double_colon = 0;
197 /* Format an IP46 address. */
199 format_ip46_address (u8 * s, va_list * args)
201 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
202 ip46_type_t type = va_arg (*args, ip46_type_t);
208 is_ip4 = ip46_address_is_ip4 (ip46);
219 format (s, "%U", format_ip4_address, &ip46->ip4) :
220 format (s, "%U", format_ip6_address, &ip46->ip6);
224 * VPPCOM Utility Functions
228 vppcom_session_table_del_listener (u64 listener_handle)
230 listener_handle |= 1ULL << 63;
231 hash_unset (vcm->session_index_by_vpp_handles, listener_handle);
235 vppcom_wait_for_app_state_change (app_state_t app_state)
237 f64 timeout = clib_time_now (&vcm->clib_time) + vcm->cfg.app_timeout;
239 while (clib_time_now (&vcm->clib_time) < timeout)
241 if (vcm->app_state == app_state)
244 VDBG (0, "VCL<%d>: timeout waiting for state %s (%d)", getpid (),
245 vppcom_app_state_str (app_state), app_state);
246 vcl_evt (VCL_EVT_SESSION_TIMEOUT, vcm, app_state);
248 return VPPCOM_ETIMEDOUT;
252 vcl_session_vpp_evt_q (vcl_session_t * s)
254 if (vcl_session_is_ct (s))
255 return vcm->vpp_event_queues[0];
257 return vcm->vpp_event_queues[s->tx_fifo->master_thread_index];
261 vcl_send_session_accepted_reply (svm_msg_q_t * mq, u32 context,
262 session_handle_t handle, int retval)
264 app_session_evt_t _app_evt, *app_evt = &_app_evt;
265 session_accepted_reply_msg_t *rmp;
266 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_ACCEPTED_REPLY);
267 rmp = (session_accepted_reply_msg_t *) app_evt->evt->data;
268 rmp->handle = handle;
269 rmp->context = context;
270 rmp->retval = retval;
271 app_send_ctrl_evt_to_vpp (mq, app_evt);
275 vcl_send_session_disconnected_reply (svm_msg_q_t * mq, u32 context,
276 session_handle_t handle, int retval)
278 app_session_evt_t _app_evt, *app_evt = &_app_evt;
279 session_disconnected_reply_msg_t *rmp;
280 app_alloc_ctrl_evt_to_vpp (mq, app_evt,
281 SESSION_CTRL_EVT_DISCONNECTED_REPLY);
282 rmp = (session_disconnected_reply_msg_t *) app_evt->evt->data;
283 rmp->handle = handle;
284 rmp->context = context;
285 rmp->retval = retval;
286 app_send_ctrl_evt_to_vpp (mq, app_evt);
290 vcl_send_session_reset_reply (svm_msg_q_t * mq, u32 context,
291 session_handle_t handle, int retval)
293 app_session_evt_t _app_evt, *app_evt = &_app_evt;
294 session_reset_reply_msg_t *rmp;
295 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_RESET_REPLY);
296 rmp = (session_reset_reply_msg_t *) app_evt->evt->data;
297 rmp->handle = handle;
298 rmp->context = context;
299 rmp->retval = retval;
300 app_send_ctrl_evt_to_vpp (mq, app_evt);
304 vcl_session_accepted_handler (session_accepted_msg_t * mp)
306 vcl_session_t *session, *listen_session;
307 svm_fifo_t *rx_fifo, *tx_fifo;
308 u32 session_index, vpp_wrk_index;
313 session = vcl_session_alloc ();
314 session_index = vcl_session_index (session);
316 listen_session = vppcom_session_table_lookup_listener (mp->listener_handle);
320 evt_q = uword_to_pointer (mp->vpp_event_queue_address, svm_msg_q_t *);
321 clib_warning ("VCL<%d>: ERROR: couldn't find listen session: "
322 "unknown vpp listener handle %llx",
323 getpid (), mp->listener_handle);
324 vcl_send_session_accepted_reply (evt_q, mp->context, mp->handle,
325 VNET_API_ERROR_INVALID_ARGUMENT);
326 vcl_session_free (session);
327 VCL_SESSION_UNLOCK ();
328 return VCL_INVALID_SESSION_INDEX;
331 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
332 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
334 if (mp->server_event_queue_address)
336 session->vpp_evt_q = uword_to_pointer (mp->client_event_queue_address,
338 session->our_evt_q = uword_to_pointer (mp->server_event_queue_address,
340 vcl_wait_for_memory (session->vpp_evt_q);
341 rx_fifo->master_session_index = session_index;
342 tx_fifo->master_session_index = session_index;
343 vec_validate (vcm->vpp_event_queues, 0);
344 evt_q = uword_to_pointer (mp->vpp_event_queue_address, svm_msg_q_t *);
345 vcm->vpp_event_queues[0] = evt_q;
349 session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address,
351 rx_fifo->client_session_index = session_index;
352 tx_fifo->client_session_index = session_index;
354 vpp_wrk_index = tx_fifo->master_thread_index;
355 vec_validate (vcm->vpp_event_queues, vpp_wrk_index);
356 vcm->vpp_event_queues[vpp_wrk_index] = session->vpp_evt_q;
359 session->vpp_handle = mp->handle;
360 session->client_context = mp->context;
361 session->rx_fifo = rx_fifo;
362 session->tx_fifo = tx_fifo;
364 session->session_state = STATE_ACCEPT;
365 session->transport.rmt_port = mp->port;
366 session->transport.is_ip4 = mp->is_ip4;
367 clib_memcpy (&session->transport.rmt_ip, mp->ip, sizeof (ip46_address_t));
369 hash_set (vcm->session_index_by_vpp_handles, mp->handle, session_index);
370 session->transport.lcl_port = listen_session->transport.lcl_port;
371 session->transport.lcl_ip = listen_session->transport.lcl_ip;
372 session->session_type = listen_session->session_type;
373 session->is_dgram = session->session_type == VPPCOM_PROTO_UDP;
375 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: client accept request from %s"
376 " address %U port %d queue %p!", getpid (), mp->handle, session_index,
377 mp->is_ip4 ? "IPv4" : "IPv6", format_ip46_address, &mp->ip,
378 mp->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
379 clib_net_to_host_u16 (mp->port), session->vpp_evt_q);
380 vcl_evt (VCL_EVT_ACCEPT, session, listen_session, session_index);
382 VCL_SESSION_UNLOCK ();
383 return session_index;
387 vcl_session_connected_handler (session_connected_msg_t * mp)
389 u32 session_index, vpp_wrk_index;
390 svm_fifo_t *rx_fifo, *tx_fifo;
391 vcl_session_t *session = 0;
395 session_index = mp->context;
396 VCL_SESSION_LOCK_AND_GET (session_index, &session);
400 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
401 "connect failed! %U",
402 getpid (), mp->handle, session_index,
403 format_api_error, ntohl (mp->retval));
406 session->session_state = STATE_FAILED;
407 session->vpp_handle = mp->handle;
411 clib_warning ("[%s] ERROR: vpp handle 0x%llx, sid %u: "
412 "Invalid session index (%u)!",
413 getpid (), mp->handle, session_index);
421 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
422 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
423 vcl_wait_for_memory (rx_fifo);
424 rx_fifo->client_session_index = session_index;
425 tx_fifo->client_session_index = session_index;
427 if (mp->client_event_queue_address)
429 session->vpp_evt_q = uword_to_pointer (mp->server_event_queue_address,
431 session->our_evt_q = uword_to_pointer (mp->client_event_queue_address,
434 vec_validate (vcm->vpp_event_queues, 0);
435 evt_q = uword_to_pointer (mp->vpp_event_queue_address, svm_msg_q_t *);
436 vcm->vpp_event_queues[0] = evt_q;
440 session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address,
442 vpp_wrk_index = tx_fifo->master_thread_index;
443 vec_validate (vcm->vpp_event_queues, vpp_wrk_index);
444 vcm->vpp_event_queues[vpp_wrk_index] = session->vpp_evt_q;
447 session->rx_fifo = rx_fifo;
448 session->tx_fifo = tx_fifo;
449 session->vpp_handle = mp->handle;
450 session->transport.is_ip4 = mp->is_ip4;
451 clib_memcpy (&session->transport.lcl_ip, mp->lcl_ip,
452 sizeof (session->transport.lcl_ip));
453 session->transport.lcl_port = mp->lcl_port;
454 session->session_state = STATE_CONNECT;
456 /* Add it to lookup table */
457 hash_set (vcm->session_index_by_vpp_handles, mp->handle, session_index);
459 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: connect succeeded! "
460 "session_rx_fifo %p, refcnt %d, session_tx_fifo %p, refcnt %d",
461 getpid (), mp->handle, session_index, session->rx_fifo,
462 session->rx_fifo->refcnt, session->tx_fifo, session->tx_fifo->refcnt);
464 VCL_SESSION_UNLOCK ();
465 return session_index;
469 vcl_session_reset_handler (session_reset_msg_t * reset_msg)
471 vcl_session_t *session;
474 sid = vcl_session_get_index_from_handle (reset_msg->handle);
475 session = vcl_session_get (sid);
478 VDBG (0, "request to reset unknown handle 0x%llx", reset_msg->handle);
479 return VCL_INVALID_SESSION_INDEX;
481 session->session_state = STATE_CLOSE_ON_EMPTY;
482 VDBG (0, "reset handle 0x%llx, sid %u ", reset_msg->handle, sid);
483 vcl_send_session_reset_reply (vcl_session_vpp_evt_q (session),
484 vcm->my_client_index, reset_msg->handle, 0);
489 vcl_session_bound_handler (session_bound_msg_t * mp)
491 vcl_session_t *session;
492 u32 sid = mp->context;
494 session = vcl_session_get (sid);
497 VDBG (0, "VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: bind failed: %U",
498 getpid (), mp->handle, sid, format_api_error, ntohl (mp->retval));
501 session->session_state = STATE_FAILED;
502 session->vpp_handle = mp->handle;
507 clib_warning ("[%s] ERROR: vpp handle 0x%llx, sid %u: "
508 "Invalid session index (%u)!",
509 getpid (), mp->handle, sid);
510 return VCL_INVALID_SESSION_INDEX;
514 session->vpp_handle = mp->handle;
515 session->transport.is_ip4 = mp->lcl_is_ip4;
516 clib_memcpy (&session->transport.lcl_ip, mp->lcl_ip,
517 sizeof (ip46_address_t));
518 session->transport.lcl_port = mp->lcl_port;
519 vppcom_session_table_add_listener (mp->handle, sid);
520 session->session_state = STATE_LISTEN;
522 if (session->is_dgram)
524 svm_fifo_t *rx_fifo, *tx_fifo;
525 session->vpp_evt_q = uword_to_pointer (mp->vpp_evt_q, svm_msg_q_t *);
526 rx_fifo = uword_to_pointer (mp->rx_fifo, svm_fifo_t *);
527 rx_fifo->client_session_index = sid;
528 tx_fifo = uword_to_pointer (mp->tx_fifo, svm_fifo_t *);
529 tx_fifo->client_session_index = sid;
530 session->rx_fifo = rx_fifo;
531 session->tx_fifo = tx_fifo;
534 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: bind succeeded!",
535 getpid (), mp->handle, sid);
540 vcl_handle_mq_ctrl_event (session_event_t * e)
542 session_accepted_msg_t *accepted_msg;
543 session_disconnected_msg_t *disconnected_msg;
544 vcl_session_msg_t *vcl_msg;
545 vcl_session_t *session;
549 switch (e->event_type)
551 case FIFO_EVENT_APP_RX:
552 clib_warning ("unhandled rx: sid %u (0x%x)",
553 e->fifo->client_session_index,
554 e->fifo->client_session_index);
556 case SESSION_CTRL_EVT_ACCEPTED:
557 accepted_msg = (session_accepted_msg_t *) e->data;
558 handle = accepted_msg->listener_handle;
559 session = vppcom_session_table_lookup_listener (handle);
562 clib_warning ("VCL<%d>: ERROR: couldn't find listen session:"
563 "listener handle %llx", getpid (), handle);
567 clib_fifo_add2 (session->accept_evts_fifo, vcl_msg);
568 vcl_msg->accepted_msg = *accepted_msg;
570 case SESSION_CTRL_EVT_CONNECTED:
571 vcl_session_connected_handler ((session_connected_msg_t *) e->data);
573 case SESSION_CTRL_EVT_DISCONNECTED:
574 disconnected_msg = (session_disconnected_msg_t *) e->data;
575 sid = vcl_session_get_index_from_handle (disconnected_msg->handle);
576 session = vcl_session_get (sid);
579 VDBG (0, "request to disconnect unknown handle 0x%llx",
580 disconnected_msg->handle);
583 session->session_state = STATE_DISCONNECT;
584 VDBG (0, "disconnected handle 0xllx, sid %u", disconnected_msg->handle,
587 case SESSION_CTRL_EVT_RESET:
588 vcl_session_reset_handler ((session_reset_msg_t *) e->data);
590 case SESSION_CTRL_EVT_BOUND:
591 vcl_session_bound_handler ((session_bound_msg_t *) e->data);
594 clib_warning ("unhandled %u", e->event_type);
600 vppcom_wait_for_session_state_change (u32 session_index,
601 session_state_t state,
604 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
605 vcl_session_t *volatile session;
613 rv = vppcom_session_at_index (session_index, &session);
614 if (PREDICT_FALSE (rv))
616 VCL_SESSION_UNLOCK ();
619 if (session->session_state & state)
621 VCL_SESSION_UNLOCK ();
624 if (session->session_state & STATE_FAILED)
626 VCL_SESSION_UNLOCK ();
627 return VPPCOM_ECONNREFUSED;
629 VCL_SESSION_UNLOCK ();
631 if (svm_msg_q_sub (vcm->app_event_queue, &msg, SVM_Q_NOWAIT, 0))
633 e = svm_msg_q_msg_data (vcm->app_event_queue, &msg);
634 vcl_handle_mq_ctrl_event (e);
635 svm_msg_q_free_msg (vcm->app_event_queue, &msg);
637 while (clib_time_now (&vcm->clib_time) < timeout);
639 VDBG (0, "VCL<%d>: timeout waiting for state 0x%x (%s)", getpid (), state,
640 vppcom_session_state_str (state));
641 vcl_evt (VCL_EVT_SESSION_TIMEOUT, session, session_state);
643 return VPPCOM_ETIMEDOUT;
647 vppcom_app_session_enable (void)
651 if (vcm->app_state != STATE_APP_ENABLED)
653 vppcom_send_session_enable_disable (1 /* is_enabled == TRUE */ );
654 rv = vppcom_wait_for_app_state_change (STATE_APP_ENABLED);
655 if (PREDICT_FALSE (rv))
657 VDBG (0, "VCL<%d>: application session enable timed out! "
658 "returning %d (%s)", getpid (), rv, vppcom_retval_str (rv));
666 vppcom_app_attach (void)
670 vppcom_app_send_attach ();
671 rv = vppcom_wait_for_app_state_change (STATE_APP_ATTACHED);
672 if (PREDICT_FALSE (rv))
674 VDBG (0, "VCL<%d>: application attach timed out! returning %d (%s)",
675 getpid (), rv, vppcom_retval_str (rv));
683 vppcom_session_unbind (u32 session_index)
685 vcl_session_t *session = 0;
689 VCL_SESSION_LOCK_AND_GET (session_index, &session);
691 vpp_handle = session->vpp_handle;
692 vppcom_session_table_del_listener (vpp_handle);
693 session->vpp_handle = ~0;
694 session->session_state = STATE_DISCONNECT;
696 VCL_SESSION_UNLOCK ();
698 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: sending unbind msg! new state"
699 " 0x%x (%s)", getpid (), vpp_handle, session_index, STATE_DISCONNECT,
700 vppcom_session_state_str (STATE_DISCONNECT));
701 vcl_evt (VCL_EVT_UNBIND, session);
702 vppcom_send_unbind_sock (vpp_handle);
709 vppcom_session_disconnect (u32 session_index)
711 svm_msg_q_t *vpp_evt_q;
712 vcl_session_t *session;
713 session_state_t state;
717 VCL_SESSION_LOCK_AND_GET (session_index, &session);
719 vpp_handle = session->vpp_handle;
720 state = session->session_state;
721 VCL_SESSION_UNLOCK ();
723 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u state 0x%x (%s)", getpid (),
724 vpp_handle, session_index, state, vppcom_session_state_str (state));
726 if (PREDICT_FALSE (state & STATE_LISTEN))
728 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
729 "Cannot disconnect a listen socket!",
730 getpid (), vpp_handle, session_index);
735 if (state & STATE_CLOSE_ON_EMPTY)
737 vpp_evt_q = vcl_session_vpp_evt_q (session);
738 vcl_send_session_disconnected_reply (vpp_evt_q, vcm->my_client_index,
740 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: sending disconnect "
741 "REPLY...", getpid (), vpp_handle, session_index);
745 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: sending disconnect...",
746 getpid (), vpp_handle, session_index);
747 vppcom_send_disconnect_session (vpp_handle, session_index);
755 * VPPCOM Public API functions
758 vppcom_app_create (char *app_name)
760 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
766 vppcom_cfg (&vcm->cfg);
770 if (vcl_cfg->use_mq_eventfd)
771 vcm->mqs_epfd = epoll_create (1);
773 clib_spinlock_init (&vcm->session_fifo_lockp);
774 clib_fifo_validate (vcm->client_session_index_fifo,
775 vcm->cfg.listen_queue_size);
776 clib_spinlock_init (&vcm->sessions_lockp);
779 vcm->main_cpu = os_get_thread_index ();
781 vcm->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
782 vcm->ct_registration_by_mq = hash_create (0, sizeof (uword));
783 clib_spinlock_init (&vcm->ct_registration_lock);
785 clib_time_init (&vcm->clib_time);
786 vppcom_init_error_string_table ();
787 svm_fifo_segment_main_init (vcl_cfg->segment_baseva,
788 20 /* timeout in secs */ );
789 vec_validate (vcm->mq_events, 64);
790 vec_validate (vcm->mq_msg_vector, 128);
791 vec_reset_length (vcm->mq_msg_vector);
794 if (vcm->my_client_index == ~0)
796 /* API hookup and connect to VPP */
797 vppcom_api_hookup ();
799 vcm->app_state = STATE_APP_START;
800 rv = vppcom_connect_to_vpp (app_name);
803 clib_warning ("VCL<%d>: ERROR: couldn't connect to VPP!",
808 /* State event handling thread */
810 rv = vce_start_event_thread (&(vcm->event_thread), 20);
812 VDBG (0, "VCL<%d>: sending session enable", getpid ());
814 rv = vppcom_app_session_enable ();
817 clib_warning ("VCL<%d>: ERROR: vppcom_app_session_enable() "
818 "failed!", getpid ());
822 VDBG (0, "VCL<%d>: sending app attach", getpid ());
824 rv = vppcom_app_attach ();
827 clib_warning ("VCL<%d>: ERROR: vppcom_app_attach() failed!",
832 VDBG (0, "VCL<%d>: app_name '%s', my_client_index %d (0x%x)",
833 getpid (), app_name, vcm->my_client_index, vcm->my_client_index);
840 vppcom_app_destroy (void)
843 f64 orig_app_timeout;
845 if (vcm->my_client_index == ~0)
848 VDBG (0, "VCL<%d>: detaching from VPP, my_client_index %d (0x%x)",
849 getpid (), vcm->my_client_index, vcm->my_client_index);
850 vcl_evt (VCL_EVT_DETACH, vcm);
852 vppcom_app_send_detach ();
853 orig_app_timeout = vcm->cfg.app_timeout;
854 vcm->cfg.app_timeout = 2.0;
855 rv = vppcom_wait_for_app_state_change (STATE_APP_ENABLED);
856 vcm->cfg.app_timeout = orig_app_timeout;
857 if (PREDICT_FALSE (rv))
858 VDBG (0, "VCL<%d>: application detach timed out! returning %d (%s)",
859 getpid (), rv, vppcom_retval_str (rv));
862 vl_client_disconnect_from_vlib ();
863 vcm->my_client_index = ~0;
864 vcm->app_state = STATE_APP_START;
868 vppcom_session_create (u8 proto, u8 is_nonblocking)
870 vcl_session_t *session;
874 pool_get (vcm->sessions, session);
875 memset (session, 0, sizeof (*session));
876 session_index = session - vcm->sessions;
878 session->session_type = proto;
879 session->session_state = STATE_START;
880 session->vpp_handle = ~0;
881 session->is_dgram = proto == VPPCOM_PROTO_UDP;
884 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK);
886 vcl_evt (VCL_EVT_CREATE, session, session_type, session->session_state,
887 is_nonblocking, session_index);
889 VCL_SESSION_UNLOCK ();
891 VDBG (0, "VCL<%d>: sid %u", getpid (), session_index);
893 return (int) session_index;
897 vppcom_session_close (uint32_t session_index)
899 vcl_session_t *session = 0;
907 session_state_t state;
909 VCL_SESSION_LOCK_AND_GET (session_index, &session);
910 is_vep = session->is_vep;
911 is_vep_session = session->is_vep_session;
912 next_sid = session->vep.next_sid;
913 vep_idx = session->vep.vep_idx;
914 state = session->session_state;
915 vpp_handle = session->vpp_handle;
916 VCL_SESSION_UNLOCK ();
918 if (VPPCOM_DEBUG > 0)
921 clib_warning ("VCL<%d>: vep_idx %u / sid %u: "
922 "closing epoll session...",
923 getpid (), session_index, session_index);
925 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %d: "
926 "closing session...",
927 getpid (), vpp_handle, session_index);
932 while (next_sid != ~0)
934 rv = vppcom_epoll_ctl (session_index, EPOLL_CTL_DEL, next_sid, 0);
935 if (PREDICT_FALSE (rv < 0))
936 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: EPOLL_CTL_DEL "
937 "vep_idx %u failed! rv %d (%s)",
938 getpid (), vpp_handle, next_sid, vep_idx,
939 rv, vppcom_retval_str (rv));
941 VCL_SESSION_LOCK_AND_GET (session_index, &session);
942 next_sid = session->vep.next_sid;
943 VCL_SESSION_UNLOCK ();
950 rv = vppcom_epoll_ctl (vep_idx, EPOLL_CTL_DEL, session_index, 0);
952 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: EPOLL_CTL_DEL "
953 "vep_idx %u failed! rv %d (%s)",
954 getpid (), vpp_handle, session_index,
955 vep_idx, rv, vppcom_retval_str (rv));
958 if (state & STATE_LISTEN)
960 rv = vppcom_session_unbind (session_index);
961 if (PREDICT_FALSE (rv < 0))
962 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: listener unbind "
963 "failed! rv %d (%s)",
964 getpid (), vpp_handle, session_index,
965 rv, vppcom_retval_str (rv));
967 else if (state & (CLIENT_STATE_OPEN | SERVER_STATE_OPEN))
969 rv = vppcom_session_disconnect (session_index);
970 if (PREDICT_FALSE (rv < 0))
971 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
972 "session disconnect failed! rv %d (%s)",
973 getpid (), vpp_handle, session_index,
974 rv, vppcom_retval_str (rv));
978 VCL_SESSION_LOCK_AND_GET (session_index, &session);
979 if (vcl_session_is_ct (session))
981 vcl_cut_through_registration_t *ctr;
984 mq_addr = pointer_to_uword (session->our_evt_q);
985 ctr = vcl_ct_registration_lock_and_lookup (mq_addr);
987 if (ctr->epoll_evt_conn_index != ~0)
988 vcl_mq_epoll_del_evfd (ctr->epoll_evt_conn_index);
989 VDBG (0, "Removing ct registration %u",
990 vcl_ct_registration_index (ctr));
991 vcl_ct_registration_del (ctr);
992 vcl_ct_registration_unlock ();
995 vpp_handle = session->vpp_handle;
996 if (vpp_handle != ~0)
998 p = hash_get (vcm->session_index_by_vpp_handles, vpp_handle);
1000 hash_unset (vcm->session_index_by_vpp_handles, vpp_handle);
1002 pool_put_index (vcm->sessions, session_index);
1004 VCL_SESSION_UNLOCK ();
1006 if (VPPCOM_DEBUG > 0)
1009 clib_warning ("VCL<%d>: vep_idx %u / sid %u: epoll session removed.",
1010 getpid (), session_index, session_index);
1012 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: session removed.",
1013 getpid (), vpp_handle, session_index);
1017 vcl_evt (VCL_EVT_CLOSE, session, rv);
1023 vppcom_session_bind (uint32_t session_index, vppcom_endpt_t * ep)
1025 vcl_session_t *session = 0;
1029 return VPPCOM_EINVAL;
1031 VCL_SESSION_LOCK_AND_GET (session_index, &session);
1033 if (session->is_vep)
1035 VCL_SESSION_UNLOCK ();
1036 clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
1037 "bind to an epoll session!", getpid (), session_index);
1042 session->transport.is_ip4 = ep->is_ip4;
1044 clib_memcpy (&session->transport.lcl_ip.ip4, ep->ip,
1045 sizeof (ip4_address_t));
1047 clib_memcpy (&session->transport.lcl_ip.ip6, ep->ip,
1048 sizeof (ip6_address_t));
1049 session->transport.lcl_port = ep->port;
1051 VDBG (0, "VCL<%d>: sid %u: binding to local %s address %U port %u, "
1052 "proto %s", getpid (), session_index,
1053 session->transport.is_ip4 ? "IPv4" : "IPv6",
1054 format_ip46_address, &session->transport.lcl_ip,
1055 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
1056 clib_net_to_host_u16 (session->transport.lcl_port),
1057 session->session_type ? "UDP" : "TCP");
1058 vcl_evt (VCL_EVT_BIND, session);
1059 VCL_SESSION_UNLOCK ();
1061 if (session->session_type == VPPCOM_PROTO_UDP)
1062 vppcom_session_listen (session_index, 10);
1069 vppcom_session_listen (uint32_t listen_session_index, uint32_t q_len)
1071 vcl_session_t *listen_session = 0;
1072 u64 listen_vpp_handle;
1075 if (q_len == 0 || q_len == ~0)
1076 q_len = vcm->cfg.listen_queue_size;
1078 VCL_SESSION_LOCK_AND_GET (listen_session_index, &listen_session);
1080 if (listen_session->is_vep)
1082 VCL_SESSION_UNLOCK ();
1083 clib_warning ("VCL<%d>: ERROR: sid %u: cannot listen on an "
1084 "epoll session!", getpid (), listen_session_index);
1089 listen_vpp_handle = listen_session->vpp_handle;
1090 if (listen_session->session_state & STATE_LISTEN)
1092 VCL_SESSION_UNLOCK ();
1093 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: already in listen state!",
1094 getpid (), listen_vpp_handle, listen_session_index);
1099 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: sending VPP bind+listen "
1100 "request...", getpid (), listen_vpp_handle, listen_session_index);
1102 vppcom_send_bind_sock (listen_session, listen_session_index);
1103 VCL_SESSION_UNLOCK ();
1104 retval = vppcom_wait_for_session_state_change (listen_session_index,
1106 vcm->cfg.session_timeout);
1108 VCL_SESSION_LOCK_AND_GET (listen_session_index, &listen_session);
1109 if (PREDICT_FALSE (retval))
1111 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: bind+listen failed! "
1112 "returning %d (%s)", getpid (), listen_session->vpp_handle,
1113 listen_session_index, retval, vppcom_retval_str (retval));
1114 VCL_SESSION_UNLOCK ();
1119 VCL_SESSION_UNLOCK ();
1126 validate_args_session_accept_ (vcl_session_t * listen_session)
1128 u32 listen_session_index = listen_session - vcm->sessions;
1130 /* Input validation - expects spinlock on sessions_lockp */
1131 if (listen_session->is_vep)
1133 clib_warning ("VCL<%d>: ERROR: sid %u: cannot accept on an "
1134 "epoll session!", getpid (), listen_session_index);
1135 return VPPCOM_EBADFD;
1138 if (listen_session->session_state != STATE_LISTEN)
1140 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
1141 "not in listen state! state 0x%x (%s)", getpid (),
1142 listen_session->vpp_handle, listen_session_index,
1143 listen_session->session_state,
1144 vppcom_session_state_str (listen_session->session_state));
1145 return VPPCOM_EBADFD;
1151 vppcom_session_accept (uint32_t listen_session_index, vppcom_endpt_t * ep,
1154 session_accepted_msg_t accepted_msg;
1155 vcl_session_t *listen_session = 0;
1156 vcl_session_t *client_session = 0;
1157 u32 client_session_index = ~0;
1158 svm_msg_q_t *vpp_evt_q;
1159 vcl_session_msg_t *evt;
1160 u64 listen_vpp_handle;
1161 svm_msg_q_msg_t msg;
1166 VCL_SESSION_LOCK_AND_GET (listen_session_index, &listen_session);
1168 if (validate_args_session_accept_ (listen_session))
1170 VCL_SESSION_UNLOCK ();
1174 VCL_SESSION_UNLOCK ();
1176 if (clib_fifo_elts (listen_session->accept_evts_fifo))
1178 clib_fifo_sub2 (listen_session->accept_evts_fifo, evt);
1179 accepted_msg = evt->accepted_msg;
1183 is_nonblocking = VCL_SESS_ATTR_TEST (listen_session->attr,
1184 VCL_SESS_ATTR_NONBLOCK);
1185 if (svm_msg_q_is_empty (vcm->app_event_queue) && is_nonblocking)
1186 return VPPCOM_EAGAIN;
1190 if (svm_msg_q_sub (vcm->app_event_queue, &msg, SVM_Q_WAIT, 0))
1191 return VPPCOM_EAGAIN;
1193 e = svm_msg_q_msg_data (vcm->app_event_queue, &msg);
1194 if (e->event_type != SESSION_CTRL_EVT_ACCEPTED)
1196 clib_warning ("discarded event: %u", e->event_type);
1197 svm_msg_q_free_msg (vcm->app_event_queue, &msg);
1200 clib_memcpy (&accepted_msg, e->data, sizeof (accepted_msg));
1201 svm_msg_q_free_msg (vcm->app_event_queue, &msg);
1207 client_session_index = vcl_session_accepted_handler (&accepted_msg);
1208 listen_session = vcl_session_get (listen_session_index);
1209 VCL_SESSION_LOCK_AND_GET (client_session_index, &client_session);
1210 rv = client_session_index;
1212 if (flags & O_NONBLOCK)
1213 VCL_SESS_ATTR_SET (client_session->attr, VCL_SESS_ATTR_NONBLOCK);
1215 listen_vpp_handle = listen_session->vpp_handle;
1216 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: Got a client request! "
1217 "vpp handle 0x%llx, sid %u, flags %d, is_nonblocking %u",
1218 getpid (), listen_vpp_handle, listen_session_index,
1219 client_session->vpp_handle, client_session_index,
1220 flags, VCL_SESS_ATTR_TEST (client_session->attr,
1221 VCL_SESS_ATTR_NONBLOCK));
1225 ep->is_ip4 = client_session->transport.is_ip4;
1226 ep->port = client_session->transport.rmt_port;
1227 if (client_session->transport.is_ip4)
1228 clib_memcpy (ep->ip, &client_session->transport.rmt_ip.ip4,
1229 sizeof (ip4_address_t));
1231 clib_memcpy (ep->ip, &client_session->transport.rmt_ip.ip6,
1232 sizeof (ip6_address_t));
1235 if (accepted_msg.server_event_queue_address)
1236 vpp_evt_q = uword_to_pointer (accepted_msg.vpp_event_queue_address,
1239 vpp_evt_q = client_session->vpp_evt_q;
1241 vcl_send_session_accepted_reply (vpp_evt_q, client_session->client_context,
1242 client_session->vpp_handle, 0);
1244 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: accepted vpp handle 0x%llx, "
1245 "sid %u connection from peer %s address %U port %u to local %s "
1246 "address %U port %u", getpid (), listen_vpp_handle,
1247 listen_session_index, client_session->vpp_handle,
1248 client_session_index,
1249 client_session->transport.is_ip4 ? "IPv4" : "IPv6",
1250 format_ip46_address, &client_session->transport.rmt_ip,
1251 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
1252 clib_net_to_host_u16 (client_session->transport.rmt_port),
1253 client_session->transport.is_ip4 ? "IPv4" : "IPv6",
1254 format_ip46_address, &client_session->transport.lcl_ip,
1255 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
1256 clib_net_to_host_u16 (client_session->transport.lcl_port));
1257 vcl_evt (VCL_EVT_ACCEPT, client_session, listen_session,
1258 client_session_index);
1259 VCL_SESSION_UNLOCK ();
1266 vppcom_session_connect (uint32_t session_index, vppcom_endpt_t * server_ep)
1268 vcl_session_t *session = 0;
1270 int rv, retval = VPPCOM_OK;
1272 VCL_SESSION_LOCK_AND_GET (session_index, &session);
1274 if (PREDICT_FALSE (session->is_vep))
1276 VCL_SESSION_UNLOCK ();
1277 clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
1278 "connect on an epoll session!", getpid (), session_index);
1283 if (PREDICT_FALSE (session->session_state & CLIENT_STATE_OPEN))
1285 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: session already "
1286 "connected to %s %U port %d proto %s, state 0x%x (%s)",
1287 getpid (), session->vpp_handle, session_index,
1288 session->transport.is_ip4 ? "IPv4" : "IPv6",
1289 format_ip46_address,
1290 &session->transport.rmt_ip, session->transport.is_ip4 ?
1291 IP46_TYPE_IP4 : IP46_TYPE_IP6,
1292 clib_net_to_host_u16 (session->transport.rmt_port),
1293 session->session_type ? "UDP" : "TCP", session->session_state,
1294 vppcom_session_state_str (session->session_state));
1296 VCL_SESSION_UNLOCK ();
1300 session->transport.is_ip4 = server_ep->is_ip4;
1301 if (session->transport.is_ip4)
1302 clib_memcpy (&session->transport.rmt_ip.ip4, server_ep->ip,
1303 sizeof (ip4_address_t));
1305 clib_memcpy (&session->transport.rmt_ip.ip6, server_ep->ip,
1306 sizeof (ip6_address_t));
1307 session->transport.rmt_port = server_ep->port;
1309 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: connecting to server %s %U "
1311 getpid (), session->vpp_handle, session_index,
1312 session->transport.is_ip4 ? "IPv4" : "IPv6",
1313 format_ip46_address,
1314 &session->transport.rmt_ip, session->transport.is_ip4 ?
1315 IP46_TYPE_IP4 : IP46_TYPE_IP6,
1316 clib_net_to_host_u16 (session->transport.rmt_port),
1317 session->session_type ? "UDP" : "TCP");
1319 vppcom_send_connect_sock (session, session_index);
1320 VCL_SESSION_UNLOCK ();
1322 retval = vppcom_wait_for_session_state_change (session_index, STATE_CONNECT,
1323 vcm->cfg.session_timeout);
1325 VCL_SESSION_LOCK_AND_GET (session_index, &session);
1326 vpp_handle = session->vpp_handle;
1327 VCL_SESSION_UNLOCK ();
1330 if (PREDICT_FALSE (retval))
1333 if (VPPCOM_DEBUG > 0)
1336 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: connect "
1337 "failed! returning %d (%s)", getpid (), vpp_handle,
1338 session_index, rv, vppcom_retval_str (rv));
1340 clib_warning ("VCL<%d>: no session for sid %u: connect failed! "
1341 "returning %d (%s)", getpid (),
1342 session_index, rv, vppcom_retval_str (rv));
1346 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: connected!",
1347 getpid (), vpp_handle, session_index);
1353 vcl_is_rx_evt_for_session (session_event_t * e, u32 sid, u8 is_ct)
1356 return (e->event_type == FIFO_EVENT_APP_RX
1357 && e->fifo->client_session_index == sid);
1359 return (e->event_type == SESSION_IO_EVT_CT_TX);
1363 vcl_session_is_readable (vcl_session_t * s)
1365 return ((s->session_state & STATE_OPEN)
1366 || (s->session_state == STATE_LISTEN
1367 && s->session_type == VPPCOM_PROTO_UDP));
1371 vppcom_session_read_internal (uint32_t session_index, void *buf, int n,
1374 int n_read = 0, rv, is_nonblocking;
1375 vcl_session_t *s = 0;
1376 svm_fifo_t *rx_fifo;
1377 svm_msg_q_msg_t msg;
1384 VCL_SESSION_LOCK_AND_GET (session_index, &s);
1386 if (PREDICT_FALSE (s->is_vep))
1388 VCL_SESSION_UNLOCK ();
1389 clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
1390 "read from an epoll session!", getpid (), session_index);
1395 is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
1396 rx_fifo = s->rx_fifo;
1398 if (PREDICT_FALSE (!vcl_session_is_readable (s)))
1400 session_state_t state = s->session_state;
1401 VCL_SESSION_UNLOCK ();
1402 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
1404 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: %s session is not open! "
1405 "state 0x%x (%s), returning %d (%s)",
1406 getpid (), s->vpp_handle, session_index, state,
1407 vppcom_session_state_str (state), rv, vppcom_retval_str (rv));
1411 VCL_SESSION_UNLOCK ();
1412 mq = vcl_session_is_ct (s) ? s->our_evt_q : vcm->app_event_queue;
1413 svm_fifo_unset_event (rx_fifo);
1414 is_full = svm_fifo_is_full (rx_fifo);
1416 if (svm_fifo_is_empty (rx_fifo))
1425 svm_msg_q_lock (mq);
1426 if (svm_msg_q_is_empty (mq))
1427 svm_msg_q_wait (mq);
1429 svm_msg_q_sub_w_lock (mq, &msg);
1430 e = svm_msg_q_msg_data (mq, &msg);
1431 svm_msg_q_unlock (mq);
1432 if (!vcl_is_rx_evt_for_session (e, session_index,
1435 vcl_handle_mq_ctrl_event (e);
1436 svm_msg_q_free_msg (mq, &msg);
1439 svm_fifo_unset_event (rx_fifo);
1440 svm_msg_q_free_msg (mq, &msg);
1441 if (PREDICT_FALSE (s->session_state == STATE_CLOSE_ON_EMPTY))
1443 if (svm_fifo_is_empty (rx_fifo))
1450 n_read = app_recv_dgram_raw (rx_fifo, buf, n, &s->transport, 0, peek);
1452 n_read = app_recv_stream_raw (rx_fifo, buf, n, 0, peek);
1454 if (vcl_session_is_ct (s) && is_full)
1456 /* If the peer is not polling send notification */
1457 if (!svm_fifo_has_event (s->rx_fifo))
1458 app_send_io_evt_to_vpp (s->vpp_evt_q, s->rx_fifo,
1459 SESSION_IO_EVT_CT_RX, SVM_Q_WAIT);
1462 if (VPPCOM_DEBUG > 2)
1465 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: read %d bytes "
1466 "from (%p)", getpid (), s->vpp_handle,
1467 session_index, n_read, rx_fifo);
1469 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: nothing read! "
1470 "returning %d (%s)", getpid (), s->vpp_handle,
1471 session_index, rv, vppcom_retval_str (rv));
1480 vppcom_session_read (uint32_t session_index, void *buf, size_t n)
1482 return (vppcom_session_read_internal (session_index, buf, n, 0));
1486 vppcom_session_peek (uint32_t session_index, void *buf, int n)
1488 return (vppcom_session_read_internal (session_index, buf, n, 1));
1492 vppcom_session_read_ready (vcl_session_t * session)
1494 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
1495 if (PREDICT_FALSE (session->is_vep))
1497 clib_warning ("VCL<%d>: ERROR: sid %u: cannot read from an "
1498 "epoll session!", getpid (), vcl_session_index (session));
1499 return VPPCOM_EBADFD;
1502 if (PREDICT_FALSE (!(session->session_state & (STATE_OPEN | STATE_LISTEN))))
1504 session_state_t state = session->session_state;
1507 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
1509 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: session is not open!"
1510 " state 0x%x (%s), returning %d (%s)", getpid (),
1511 session->vpp_handle, vcl_session_index (session), state,
1512 vppcom_session_state_str (state), rv, vppcom_retval_str (rv));
1516 if (session->session_state & STATE_LISTEN)
1517 return clib_fifo_elts (session->accept_evts_fifo);
1519 return svm_fifo_max_dequeue (session->rx_fifo);
1523 vcl_is_tx_evt_for_session (session_event_t * e, u32 sid, u8 is_ct)
1526 return (e->event_type == FIFO_EVENT_APP_TX
1527 && e->fifo->client_session_index == sid);
1529 return (e->event_type == SESSION_IO_EVT_CT_RX);
1533 vppcom_session_write (uint32_t session_index, void *buf, size_t n)
1535 int rv, n_write, is_nonblocking;
1536 vcl_session_t *s = 0;
1537 svm_fifo_t *tx_fifo = 0;
1538 session_evt_type_t et;
1539 svm_msg_q_msg_t msg;
1545 VCL_SESSION_LOCK_AND_GET (session_index, &s);
1547 tx_fifo = s->tx_fifo;
1548 is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
1550 if (PREDICT_FALSE (s->is_vep))
1552 VCL_SESSION_UNLOCK ();
1553 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
1554 "cannot write to an epoll session!",
1555 getpid (), s->vpp_handle, session_index);
1561 if (!(s->session_state & STATE_OPEN))
1563 session_state_t state = s->session_state;
1564 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
1565 VCL_SESSION_UNLOCK ();
1566 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: session is not open! "
1568 getpid (), s->vpp_handle, session_index,
1569 state, vppcom_session_state_str (state));
1573 VCL_SESSION_UNLOCK ();
1575 mq = vcl_session_is_ct (s) ? s->our_evt_q : vcm->app_event_queue;
1576 if (svm_fifo_is_full (tx_fifo))
1580 rv = VPPCOM_EWOULDBLOCK;
1583 while (svm_fifo_is_full (tx_fifo))
1585 svm_msg_q_lock (mq);
1586 while (svm_msg_q_is_empty (mq) && svm_msg_q_timedwait (mq, 10e-6))
1588 svm_msg_q_sub_w_lock (mq, &msg);
1589 e = svm_msg_q_msg_data (mq, &msg);
1590 svm_msg_q_unlock (mq);
1592 if (!vcl_is_tx_evt_for_session (e, session_index,
1594 vcl_handle_mq_ctrl_event (e);
1595 svm_msg_q_free_msg (mq, &msg);
1599 ASSERT (FIFO_EVENT_APP_TX + 1 == SESSION_IO_EVT_CT_TX);
1600 et = FIFO_EVENT_APP_TX + vcl_session_is_ct (s);
1602 n_write = app_send_dgram_raw (tx_fifo, &s->transport,
1603 s->vpp_evt_q, buf, n, et, SVM_Q_WAIT);
1605 n_write = app_send_stream_raw (tx_fifo, s->vpp_evt_q, buf, n, et,
1608 ASSERT (n_write > 0);
1610 if (VPPCOM_DEBUG > 2)
1613 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
1614 "FIFO-FULL (%p)", getpid (), s->vpp_handle,
1615 session_index, tx_fifo);
1617 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
1618 "wrote %d bytes tx-fifo: (%p)", getpid (),
1619 s->vpp_handle, session_index, n_write, tx_fifo);
1627 static vcl_session_t *
1628 vcl_ct_session_get_from_fifo (svm_fifo_t * f, u8 type)
1631 s = vcl_session_get (f->client_session_index);
1635 if (type == 0 && s->rx_fifo == f)
1638 if (type == 1 && s->tx_fifo == f)
1641 s = vcl_session_get (f->master_session_index);
1644 if (type == 0 && s->rx_fifo == f)
1646 if (type == 1 && s->tx_fifo == f)
1653 vppcom_session_write_ready (vcl_session_t * session, u32 session_index)
1655 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
1656 if (PREDICT_FALSE (session->is_vep))
1658 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
1659 "cannot write to an epoll session!",
1660 getpid (), session->vpp_handle, session_index);
1661 return VPPCOM_EBADFD;
1664 if (PREDICT_FALSE (session->session_state & STATE_LISTEN))
1666 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
1667 "cannot write to a listen session!",
1668 getpid (), session->vpp_handle, session_index);
1669 return VPPCOM_EBADFD;
1672 if (PREDICT_FALSE (!(session->session_state & STATE_OPEN)))
1674 session_state_t state = session->session_state;
1677 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
1678 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
1679 "session is not open! state 0x%x (%s), "
1680 "returning %d (%s)", getpid (), session->vpp_handle,
1682 state, vppcom_session_state_str (state),
1683 rv, vppcom_retval_str (rv));
1687 VDBG (3, "VCL<%d>: vpp handle 0x%llx, sid %u: peek %s (%p), ready = %d",
1688 getpid (), session->vpp_handle, session_index, session->tx_fifo,
1689 svm_fifo_max_enqueue (session->tx_fifo));
1691 return svm_fifo_max_enqueue (session->tx_fifo);
1695 vcl_mq_dequeue_batch (svm_msg_q_t * mq)
1697 svm_msg_q_msg_t *msg;
1701 n_msgs = svm_msg_q_size (mq);
1702 for (i = 0; i < n_msgs; i++)
1704 vec_add2 (vcm->mq_msg_vector, msg, 1);
1705 svm_msg_q_sub_w_lock (mq, msg);
1711 vcl_select_handle_mq (svm_msg_q_t * mq, unsigned long n_bits,
1712 unsigned long *read_map, unsigned long *write_map,
1713 unsigned long *except_map, double time_to_wait,
1716 session_disconnected_msg_t *disconnected_msg;
1717 session_connected_msg_t *connected_msg;
1718 session_accepted_msg_t *accepted_msg;
1719 vcl_session_msg_t *vcl_msg;
1720 vcl_session_t *session;
1721 svm_msg_q_msg_t *msg;
1726 svm_msg_q_lock (mq);
1727 if (svm_msg_q_is_empty (mq))
1731 svm_msg_q_unlock (mq);
1737 svm_msg_q_unlock (mq);
1740 else if (time_to_wait < 0)
1742 svm_msg_q_wait (mq);
1746 if (svm_msg_q_timedwait (mq, time_to_wait))
1748 svm_msg_q_unlock (mq);
1753 vcl_mq_dequeue_batch (mq);
1754 svm_msg_q_unlock (mq);
1756 for (i = 0; i < vec_len (vcm->mq_msg_vector); i++)
1758 msg = vec_elt_at_index (vcm->mq_msg_vector, i);
1759 e = svm_msg_q_msg_data (mq, msg);
1760 switch (e->event_type)
1762 case FIFO_EVENT_APP_RX:
1763 sid = e->fifo->client_session_index;
1764 session = vcl_session_get (sid);
1765 if (sid < n_bits && read_map)
1767 clib_bitmap_set_no_check (read_map, sid, 1);
1771 case FIFO_EVENT_APP_TX:
1772 sid = e->fifo->client_session_index;
1773 session = vcl_session_get (sid);
1776 if (sid < n_bits && write_map)
1778 clib_bitmap_set_no_check (write_map, sid, 1);
1782 case SESSION_IO_EVT_CT_TX:
1783 session = vcl_ct_session_get_from_fifo (e->fifo, 0);
1784 sid = vcl_session_index (session);
1785 if (sid < n_bits && read_map)
1787 clib_bitmap_set_no_check (read_map, sid, 1);
1792 case SESSION_IO_EVT_CT_RX:
1793 session = vcl_ct_session_get_from_fifo (e->fifo, 1);
1794 sid = vcl_session_index (session);
1797 if (sid < n_bits && write_map)
1799 clib_bitmap_set_no_check (write_map, sid, 1);
1803 case SESSION_CTRL_EVT_ACCEPTED:
1804 accepted_msg = (session_accepted_msg_t *) e->data;
1805 handle = accepted_msg->listener_handle;
1806 session = vppcom_session_table_lookup_listener (handle);
1809 clib_warning ("VCL<%d>: ERROR: couldn't find listen session:"
1810 "listener handle %llx", getpid (), handle);
1814 clib_fifo_add2 (session->accept_evts_fifo, vcl_msg);
1815 vcl_msg->accepted_msg = *accepted_msg;
1816 sid = session - vcm->sessions;
1817 if (sid < n_bits && read_map)
1819 clib_bitmap_set_no_check (read_map, sid, 1);
1823 case SESSION_CTRL_EVT_CONNECTED:
1824 connected_msg = (session_connected_msg_t *) e->data;
1825 vcl_session_connected_handler (connected_msg);
1827 case SESSION_CTRL_EVT_DISCONNECTED:
1828 disconnected_msg = (session_disconnected_msg_t *) e->data;
1829 sid = vcl_session_get_index_from_handle (disconnected_msg->handle);
1830 if (sid < n_bits && except_map)
1832 clib_bitmap_set_no_check (except_map, sid, 1);
1836 case SESSION_CTRL_EVT_RESET:
1837 sid = vcl_session_reset_handler ((session_reset_msg_t *) e->data);
1838 if (sid < n_bits && except_map)
1840 clib_bitmap_set_no_check (except_map, sid, 1);
1845 clib_warning ("unhandled: %u", e->event_type);
1848 svm_msg_q_free_msg (mq, msg);
1851 vec_reset_length (vcm->mq_msg_vector);
1856 vppcom_select_condvar (unsigned long n_bits, unsigned long *read_map,
1857 unsigned long *write_map, unsigned long *except_map,
1858 double time_to_wait, u32 * bits_set)
1860 double total_wait = 0, wait_slice;
1861 vcl_cut_through_registration_t *cr;
1863 time_to_wait = (time_to_wait == -1) ? 10e9 : time_to_wait;
1864 wait_slice = vcm->cut_through_registrations ? 10e-6 : time_to_wait;
1868 pool_foreach (cr, vcm->cut_through_registrations, ({
1869 vcl_select_handle_mq (cr->mq, n_bits, read_map, write_map, except_map,
1874 vcl_select_handle_mq (vcm->app_event_queue, n_bits, read_map, write_map,
1875 except_map, time_to_wait, bits_set);
1876 total_wait += wait_slice;
1880 while (total_wait < time_to_wait);
1886 vppcom_select_eventfd (unsigned long n_bits, unsigned long *read_map,
1887 unsigned long *write_map, unsigned long *except_map,
1888 double time_to_wait, u32 * bits_set)
1890 vcl_mq_evt_conn_t *mqc;
1891 int __clib_unused n_read;
1895 vec_validate (vcm->mq_events, pool_elts (vcm->mq_evt_conns));
1896 n_mq_evts = epoll_wait (vcm->mqs_epfd, vcm->mq_events,
1897 vec_len (vcm->mq_events), time_to_wait);
1898 for (i = 0; i < n_mq_evts; i++)
1900 mqc = vcl_mq_evt_conn_get (vcm->mq_events[i].data.u32);
1901 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
1902 vcl_select_handle_mq (mqc->mq, n_bits, read_map, write_map,
1903 except_map, 0, bits_set);
1906 return (n_mq_evts > 0 ? (int) *bits_set : 0);
1910 vppcom_select (unsigned long n_bits, unsigned long *read_map,
1911 unsigned long *write_map, unsigned long *except_map,
1912 double time_to_wait)
1914 u32 sid, minbits = clib_max (n_bits, BITS (uword)), bits_set = 0;
1915 vcl_session_t *session = 0;
1918 ASSERT (sizeof (clib_bitmap_t) == sizeof (long int));
1920 if (n_bits && read_map)
1922 clib_bitmap_validate (vcm->rd_bitmap, minbits);
1923 clib_memcpy (vcm->rd_bitmap, read_map,
1924 vec_len (vcm->rd_bitmap) * sizeof (clib_bitmap_t));
1925 memset (read_map, 0, vec_len (vcm->rd_bitmap) * sizeof (clib_bitmap_t));
1927 if (n_bits && write_map)
1929 clib_bitmap_validate (vcm->wr_bitmap, minbits);
1930 clib_memcpy (vcm->wr_bitmap, write_map,
1931 vec_len (vcm->wr_bitmap) * sizeof (clib_bitmap_t));
1932 memset (write_map, 0,
1933 vec_len (vcm->wr_bitmap) * sizeof (clib_bitmap_t));
1935 if (n_bits && except_map)
1937 clib_bitmap_validate (vcm->ex_bitmap, minbits);
1938 clib_memcpy (vcm->ex_bitmap, except_map,
1939 vec_len (vcm->ex_bitmap) * sizeof (clib_bitmap_t));
1940 memset (except_map, 0,
1941 vec_len (vcm->ex_bitmap) * sizeof (clib_bitmap_t));
1951 clib_bitmap_foreach (sid, vcm->wr_bitmap, ({
1952 if (!(session = vcl_session_get (sid)))
1954 VDBG (0, "VCL<%d>: session %d specified in write_map is closed.",
1956 return VPPCOM_EBADFD;
1959 rv = svm_fifo_is_full (session->tx_fifo);
1962 clib_bitmap_set_no_check (write_map, sid, 1);
1971 clib_bitmap_foreach (sid, vcm->rd_bitmap, ({
1972 if (!(session = vcl_session_get (sid)))
1974 VDBG (0, "VCL<%d>: session %d specified in write_map is closed.",
1976 return VPPCOM_EBADFD;
1979 rv = vppcom_session_read_ready (session);
1982 clib_bitmap_set_no_check (read_map, sid, 1);
1990 if (vcm->cfg.use_mq_eventfd)
1991 vppcom_select_eventfd (n_bits, read_map, write_map, except_map,
1992 time_to_wait, &bits_set);
1994 vppcom_select_condvar (n_bits, read_map, write_map, except_map,
1995 time_to_wait, &bits_set);
2001 vep_verify_epoll_chain (u32 vep_idx)
2003 vcl_session_t *session;
2004 vppcom_epoll_t *vep;
2008 if (VPPCOM_DEBUG <= 1)
2011 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
2012 rv = vppcom_session_at_index (vep_idx, &session);
2013 if (PREDICT_FALSE (rv))
2015 clib_warning ("VCL<%d>: ERROR: Invalid vep_idx (%u)!",
2016 getpid (), vep_idx);
2019 if (PREDICT_FALSE (!session->is_vep))
2021 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
2022 getpid (), vep_idx);
2025 vep = &session->vep;
2026 clib_warning ("VCL<%d>: vep_idx (%u): Dumping epoll chain\n"
2029 " is_vep_session = %u\n"
2030 " next_sid = 0x%x (%u)\n"
2031 " wait_cont_idx = 0x%x (%u)\n"
2032 "}\n", getpid (), vep_idx,
2033 session->is_vep, session->is_vep_session,
2034 vep->next_sid, vep->next_sid,
2035 session->wait_cont_idx, session->wait_cont_idx);
2037 for (sid = vep->next_sid; sid != ~0; sid = vep->next_sid)
2039 rv = vppcom_session_at_index (sid, &session);
2040 if (PREDICT_FALSE (rv))
2042 clib_warning ("VCL<%d>: ERROR: Invalid sid (%u)!", getpid (), sid);
2045 if (PREDICT_FALSE (session->is_vep))
2046 clib_warning ("VCL<%d>: ERROR: sid (%u) is a vep!",
2047 getpid (), vep_idx);
2048 else if (PREDICT_FALSE (!session->is_vep_session))
2050 clib_warning ("VCL<%d>: ERROR: session (%u) "
2051 "is not a vep session!", getpid (), sid);
2054 vep = &session->vep;
2055 if (PREDICT_FALSE (vep->vep_idx != vep_idx))
2056 clib_warning ("VCL<%d>: ERROR: session (%u) vep_idx (%u) != "
2057 "vep_idx (%u)!", getpid (),
2058 sid, session->vep.vep_idx, vep_idx);
2059 if (session->is_vep_session)
2061 clib_warning ("vep_idx[%u]: sid 0x%x (%u)\n"
2063 " next_sid = 0x%x (%u)\n"
2064 " prev_sid = 0x%x (%u)\n"
2065 " vep_idx = 0x%x (%u)\n"
2066 " ev.events = 0x%x\n"
2067 " ev.data.u64 = 0x%llx\n"
2071 vep->next_sid, vep->next_sid,
2072 vep->prev_sid, vep->prev_sid,
2073 vep->vep_idx, vep->vep_idx,
2074 vep->ev.events, vep->ev.data.u64, vep->et_mask);
2079 clib_warning ("VCL<%d>: vep_idx (%u): Dump complete!\n",
2080 getpid (), vep_idx);
2084 vppcom_epoll_create (void)
2086 vcl_session_t *vep_session;
2089 VCL_SESSION_LOCK ();
2090 pool_get (vcm->sessions, vep_session);
2091 memset (vep_session, 0, sizeof (*vep_session));
2092 vep_idx = vep_session - vcm->sessions;
2094 vep_session->is_vep = 1;
2095 vep_session->vep.vep_idx = ~0;
2096 vep_session->vep.next_sid = ~0;
2097 vep_session->vep.prev_sid = ~0;
2098 vep_session->wait_cont_idx = ~0;
2099 vep_session->vpp_handle = ~0;
2100 vep_session->poll_reg = 0;
2102 vcl_evt (VCL_EVT_EPOLL_CREATE, vep_session, vep_idx);
2103 VCL_SESSION_UNLOCK ();
2105 VDBG (0, "VCL<%d>: Created vep_idx %u / sid %u!",
2106 getpid (), vep_idx, vep_idx);
2112 vppcom_epoll_ctl (uint32_t vep_idx, int op, uint32_t session_index,
2113 struct epoll_event *event)
2115 vcl_session_t *vep_session;
2116 vcl_session_t *session;
2119 if (vep_idx == session_index)
2121 clib_warning ("VCL<%d>: ERROR: vep_idx == session_index (%u)!",
2122 getpid (), vep_idx);
2123 return VPPCOM_EINVAL;
2126 VCL_SESSION_LOCK ();
2127 rv = vppcom_session_at_index (vep_idx, &vep_session);
2128 if (PREDICT_FALSE (rv))
2130 clib_warning ("VCL<%d>: ERROR: Invalid vep_idx (%u)!", vep_idx);
2133 if (PREDICT_FALSE (!vep_session->is_vep))
2135 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
2136 getpid (), vep_idx);
2141 ASSERT (vep_session->vep.vep_idx == ~0);
2142 ASSERT (vep_session->vep.prev_sid == ~0);
2144 rv = vppcom_session_at_index (session_index, &session);
2145 if (PREDICT_FALSE (rv))
2147 VDBG (0, "VCL<%d>: ERROR: Invalid session_index (%u)!",
2148 getpid (), session_index);
2151 if (PREDICT_FALSE (session->is_vep))
2153 clib_warning ("ERROR: session_index (%u) is a vep!", vep_idx);
2161 if (PREDICT_FALSE (!event))
2163 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_ADD: NULL pointer to "
2164 "epoll_event structure!", getpid ());
2168 if (vep_session->vep.next_sid != ~0)
2170 vcl_session_t *next_session;
2171 rv = vppcom_session_at_index (vep_session->vep.next_sid,
2173 if (PREDICT_FALSE (rv))
2175 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_ADD: Invalid "
2176 "vep.next_sid (%u) on vep_idx (%u)!",
2177 getpid (), vep_session->vep.next_sid, vep_idx);
2180 ASSERT (next_session->vep.prev_sid == vep_idx);
2181 next_session->vep.prev_sid = session_index;
2183 session->vep.next_sid = vep_session->vep.next_sid;
2184 session->vep.prev_sid = vep_idx;
2185 session->vep.vep_idx = vep_idx;
2186 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
2187 session->vep.ev = *event;
2188 session->is_vep = 0;
2189 session->is_vep_session = 1;
2190 vep_session->vep.next_sid = session_index;
2192 /* VCL Event Register handler */
2193 if (session->session_state & STATE_LISTEN)
2195 /* Register handler for connect_request event on listen_session_index */
2196 vce_event_key_t evk;
2197 evk.session_index = session_index;
2198 evk.eid = VCL_EVENT_CONNECT_REQ_ACCEPTED;
2199 vep_session->poll_reg =
2200 vce_register_handler (&vcm->event_thread, &evk,
2201 vce_poll_wait_connect_request_handler_fn,
2202 0 /* No callback args */ );
2204 VDBG (1, "VCL<%d>: EPOLL_CTL_ADD: vep_idx %u, "
2205 "sid %u, events 0x%x, data 0x%llx!",
2206 getpid (), vep_idx, session_index,
2207 event->events, event->data.u64);
2208 vcl_evt (VCL_EVT_EPOLL_CTLADD, session, event->events, event->data.u64);
2212 if (PREDICT_FALSE (!event))
2214 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_MOD: NULL pointer to "
2215 "epoll_event structure!", getpid ());
2219 else if (PREDICT_FALSE (!session->is_vep_session))
2221 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_MOD: "
2222 "not a vep session!", getpid (), session_index);
2226 else if (PREDICT_FALSE (session->vep.vep_idx != vep_idx))
2228 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_MOD: "
2229 "vep_idx (%u) != vep_idx (%u)!",
2230 getpid (), session_index,
2231 session->vep.vep_idx, vep_idx);
2235 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
2236 session->vep.ev = *event;
2237 VDBG (1, "VCL<%d>: EPOLL_CTL_MOD: vep_idx %u, sid %u, events 0x%x,"
2238 " data 0x%llx!", getpid (), vep_idx, session_index, event->events,
2243 if (PREDICT_FALSE (!session->is_vep_session))
2245 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_DEL: "
2246 "not a vep session!", getpid (), session_index);
2250 else if (PREDICT_FALSE (session->vep.vep_idx != vep_idx))
2252 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_DEL: "
2253 "vep_idx (%u) != vep_idx (%u)!",
2254 getpid (), session_index,
2255 session->vep.vep_idx, vep_idx);
2260 /* VCL Event Un-register handler */
2261 if ((session->session_state & STATE_LISTEN) && vep_session->poll_reg)
2263 (void) vce_unregister_handler (&vcm->event_thread,
2264 vep_session->poll_reg);
2267 vep_session->wait_cont_idx =
2268 (vep_session->wait_cont_idx == session_index) ?
2269 session->vep.next_sid : vep_session->wait_cont_idx;
2271 if (session->vep.prev_sid == vep_idx)
2272 vep_session->vep.next_sid = session->vep.next_sid;
2275 vcl_session_t *prev_session;
2276 rv = vppcom_session_at_index (session->vep.prev_sid, &prev_session);
2277 if (PREDICT_FALSE (rv))
2279 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_DEL: Invalid "
2280 "vep.prev_sid (%u) on sid (%u)!",
2281 getpid (), session->vep.prev_sid, session_index);
2284 ASSERT (prev_session->vep.next_sid == session_index);
2285 prev_session->vep.next_sid = session->vep.next_sid;
2287 if (session->vep.next_sid != ~0)
2289 vcl_session_t *next_session;
2290 rv = vppcom_session_at_index (session->vep.next_sid, &next_session);
2291 if (PREDICT_FALSE (rv))
2293 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_DEL: Invalid "
2294 "vep.next_sid (%u) on sid (%u)!",
2295 getpid (), session->vep.next_sid, session_index);
2298 ASSERT (next_session->vep.prev_sid == session_index);
2299 next_session->vep.prev_sid = session->vep.prev_sid;
2302 memset (&session->vep, 0, sizeof (session->vep));
2303 session->vep.next_sid = ~0;
2304 session->vep.prev_sid = ~0;
2305 session->vep.vep_idx = ~0;
2306 session->is_vep_session = 0;
2307 VDBG (1, "VCL<%d>: EPOLL_CTL_DEL: vep_idx %u, sid %u!",
2308 getpid (), vep_idx, session_index);
2309 vcl_evt (VCL_EVT_EPOLL_CTLDEL, session, vep_idx);
2313 clib_warning ("VCL<%d>: ERROR: Invalid operation (%d)!", getpid (), op);
2317 vep_verify_epoll_chain (vep_idx);
2320 VCL_SESSION_UNLOCK ();
2325 vcl_epoll_wait_handle_mq (svm_msg_q_t * mq, struct epoll_event *events,
2326 u32 maxevents, double wait_for_time, u32 * num_ev)
2328 session_disconnected_msg_t *disconnected_msg;
2329 session_connected_msg_t *connected_msg;
2330 session_accepted_msg_t *accepted_msg;
2331 u64 session_evt_data = ~0, handle;
2332 u32 sid = ~0, session_events;
2333 vcl_session_msg_t *vcl_msg;
2334 vcl_session_t *session;
2335 svm_msg_q_msg_t *msg;
2340 svm_msg_q_lock (mq);
2341 if (svm_msg_q_is_empty (mq))
2345 svm_msg_q_unlock (mq);
2348 else if (wait_for_time < 0)
2350 svm_msg_q_wait (mq);
2354 if (svm_msg_q_timedwait (mq, wait_for_time / 1e3))
2356 svm_msg_q_unlock (mq);
2361 vcl_mq_dequeue_batch (mq);
2362 svm_msg_q_unlock (mq);
2364 for (i = 0; i < vec_len (vcm->mq_msg_vector); i++)
2366 msg = vec_elt_at_index (vcm->mq_msg_vector, i);
2367 e = svm_msg_q_msg_data (mq, msg);
2369 switch (e->event_type)
2371 case FIFO_EVENT_APP_RX:
2372 sid = e->fifo->client_session_index;
2373 session = vcl_session_get (sid);
2374 session_events = session->vep.ev.events;
2375 if (!(EPOLLIN & session->vep.ev.events))
2378 events[*num_ev].events |= EPOLLIN;
2379 session_evt_data = session->vep.ev.data.u64;
2381 case FIFO_EVENT_APP_TX:
2382 sid = e->fifo->client_session_index;
2383 session = vcl_session_get (sid);
2384 session_events = session->vep.ev.events;
2385 if (!(EPOLLOUT & session_events))
2388 events[*num_ev].events |= EPOLLOUT;
2389 session_evt_data = session->vep.ev.data.u64;
2391 case SESSION_IO_EVT_CT_TX:
2392 session = vcl_ct_session_get_from_fifo (e->fifo, 0);
2393 sid = vcl_session_index (session);
2394 session_events = session->vep.ev.events;
2395 if (!(EPOLLIN & session->vep.ev.events))
2398 events[*num_ev].events |= EPOLLIN;
2399 session_evt_data = session->vep.ev.data.u64;
2401 case SESSION_IO_EVT_CT_RX:
2402 session = vcl_ct_session_get_from_fifo (e->fifo, 1);
2403 sid = vcl_session_index (session);
2404 session_events = session->vep.ev.events;
2405 if (!(EPOLLOUT & session_events))
2408 events[*num_ev].events |= EPOLLOUT;
2409 session_evt_data = session->vep.ev.data.u64;
2411 case SESSION_CTRL_EVT_ACCEPTED:
2412 accepted_msg = (session_accepted_msg_t *) e->data;
2413 handle = accepted_msg->listener_handle;
2414 session = vppcom_session_table_lookup_listener (handle);
2417 clib_warning ("VCL<%d>: ERROR: couldn't find listen session:"
2418 "listener handle %llx", getpid (), handle);
2422 clib_fifo_add2 (session->accept_evts_fifo, vcl_msg);
2423 vcl_msg->accepted_msg = *accepted_msg;
2424 session_events = session->vep.ev.events;
2425 if (!(EPOLLIN & session_events))
2429 events[*num_ev].events |= EPOLLIN;
2430 session_evt_data = session->vep.ev.data.u64;
2432 case SESSION_CTRL_EVT_CONNECTED:
2433 connected_msg = (session_connected_msg_t *) e->data;
2434 vcl_session_connected_handler (connected_msg);
2435 /* Generate EPOLLOUT because there's no connected event */
2436 sid = vcl_session_get_index_from_handle (connected_msg->handle);
2437 clib_spinlock_lock (&vcm->sessions_lockp);
2438 session = vcl_session_get (sid);
2439 session_events = session->vep.ev.events;
2440 if (EPOLLOUT & session_events)
2443 events[*num_ev].events |= EPOLLOUT;
2444 session_evt_data = session->vep.ev.data.u64;
2446 clib_spinlock_unlock (&vcm->sessions_lockp);
2448 case SESSION_CTRL_EVT_DISCONNECTED:
2449 disconnected_msg = (session_disconnected_msg_t *) e->data;
2450 sid = vcl_session_get_index_from_handle (disconnected_msg->handle);
2451 clib_spinlock_lock (&vcm->sessions_lockp);
2452 if (!(session = vcl_session_get (sid)))
2455 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
2456 session_evt_data = session->vep.ev.data.u64;
2457 session_events = session->vep.ev.events;
2458 clib_spinlock_unlock (&vcm->sessions_lockp);
2460 case SESSION_CTRL_EVT_RESET:
2461 sid = vcl_session_reset_handler ((session_reset_msg_t *) e->data);
2462 if (!(session = vcl_session_get (sid)))
2465 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
2466 session_evt_data = session->vep.ev.data.u64;
2467 session_events = session->vep.ev.events;
2470 clib_warning ("unhandled: %u", e->event_type);
2471 svm_msg_q_free_msg (mq, msg);
2474 svm_msg_q_free_msg (mq, msg);
2478 events[*num_ev].data.u64 = session_evt_data;
2479 if (EPOLLONESHOT & session_events)
2481 clib_spinlock_lock (&vcm->sessions_lockp);
2482 session = vcl_session_get (sid);
2483 session->vep.ev.events = 0;
2484 clib_spinlock_unlock (&vcm->sessions_lockp);
2487 if (*num_ev == maxevents)
2492 vec_reset_length (vcm->mq_msg_vector);
2497 vppcom_epoll_wait_condvar (struct epoll_event *events, int maxevents,
2498 double wait_for_time)
2500 vcl_cut_through_registration_t *cr;
2501 double total_wait = 0, wait_slice;
2505 wait_for_time = (wait_for_time == -1) ? (double) 10e9 : wait_for_time;
2506 wait_slice = vcm->cut_through_registrations ? 10e-6 : wait_for_time;
2511 pool_foreach (cr, vcm->cut_through_registrations, ({
2512 vcl_epoll_wait_handle_mq (cr->mq, events, maxevents, 0, &num_ev);
2516 rv = vcl_epoll_wait_handle_mq (vcm->app_event_queue, events, maxevents,
2517 num_ev ? 0 : wait_slice, &num_ev);
2519 total_wait += wait_slice;
2523 while (total_wait < wait_for_time);
2524 return (int) num_ev;
2528 vppcom_epoll_wait_eventfd (struct epoll_event *events, int maxevents,
2529 double wait_for_time)
2531 vcl_mq_evt_conn_t *mqc;
2532 int __clib_unused n_read;
2537 vec_validate (vcm->mq_events, pool_elts (vcm->mq_evt_conns));
2538 n_mq_evts = epoll_wait (vcm->mqs_epfd, vcm->mq_events,
2539 vec_len (vcm->mq_events), wait_for_time);
2540 for (i = 0; i < n_mq_evts; i++)
2542 mqc = vcl_mq_evt_conn_get (vcm->mq_events[i].data.u32);
2543 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
2544 vcl_epoll_wait_handle_mq (mqc->mq, events, maxevents, 0, &n_evts);
2547 return (int) n_evts;
2551 vppcom_epoll_wait (uint32_t vep_idx, struct epoll_event *events,
2552 int maxevents, double wait_for_time)
2554 vcl_session_t *vep_session;
2556 if (PREDICT_FALSE (maxevents <= 0))
2558 clib_warning ("VCL<%d>: ERROR: Invalid maxevents (%d)!",
2559 getpid (), maxevents);
2560 return VPPCOM_EINVAL;
2563 clib_spinlock_lock (&vcm->sessions_lockp);
2564 vep_session = vcl_session_get (vep_idx);
2565 if (PREDICT_FALSE (!vep_session->is_vep))
2567 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
2568 getpid (), vep_idx);
2569 clib_spinlock_unlock (&vcm->sessions_lockp);
2570 return VPPCOM_EINVAL;
2572 clib_spinlock_unlock (&vcm->sessions_lockp);
2574 memset (events, 0, sizeof (*events) * maxevents);
2576 if (vcm->cfg.use_mq_eventfd)
2577 return vppcom_epoll_wait_eventfd (events, maxevents, wait_for_time);
2579 return vppcom_epoll_wait_condvar (events, maxevents, wait_for_time);
2583 vppcom_session_attr (uint32_t session_index, uint32_t op,
2584 void *buffer, uint32_t * buflen)
2586 vcl_session_t *session;
2588 u32 *flags = buffer;
2589 vppcom_endpt_t *ep = buffer;
2591 VCL_SESSION_LOCK_AND_GET (session_index, &session);
2597 case VPPCOM_ATTR_GET_NREAD:
2598 rv = vppcom_session_read_ready (session);
2599 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_NREAD: sid %u, nread = %d",
2603 case VPPCOM_ATTR_GET_NWRITE:
2604 rv = vppcom_session_write_ready (session, session_index);
2605 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_NWRITE: sid %u, nwrite = %d",
2606 getpid (), session_index, rv);
2609 case VPPCOM_ATTR_GET_FLAGS:
2610 if (PREDICT_TRUE (buffer && buflen && (*buflen >= sizeof (*flags))))
2612 *flags = O_RDWR | (VCL_SESS_ATTR_TEST (session->attr,
2613 VCL_SESS_ATTR_NONBLOCK));
2614 *buflen = sizeof (*flags);
2615 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_FLAGS: sid %u, flags = 0x%08x, "
2616 "is_nonblocking = %u", getpid (),
2617 session_index, *flags,
2618 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK));
2624 case VPPCOM_ATTR_SET_FLAGS:
2625 if (PREDICT_TRUE (buffer && buflen && (*buflen == sizeof (*flags))))
2627 if (*flags & O_NONBLOCK)
2628 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK);
2630 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_NONBLOCK);
2632 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_FLAGS: sid %u, flags = 0x%08x,"
2633 " is_nonblocking = %u",
2634 getpid (), session_index, *flags,
2635 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK));
2641 case VPPCOM_ATTR_GET_PEER_ADDR:
2642 if (PREDICT_TRUE (buffer && buflen &&
2643 (*buflen >= sizeof (*ep)) && ep->ip))
2645 ep->is_ip4 = session->transport.is_ip4;
2646 ep->port = session->transport.rmt_port;
2647 if (session->transport.is_ip4)
2648 clib_memcpy (ep->ip, &session->transport.rmt_ip.ip4,
2649 sizeof (ip4_address_t));
2651 clib_memcpy (ep->ip, &session->transport.rmt_ip.ip6,
2652 sizeof (ip6_address_t));
2653 *buflen = sizeof (*ep);
2654 VDBG (1, "VCL<%d>: VPPCOM_ATTR_GET_PEER_ADDR: sid %u, is_ip4 = %u, "
2655 "addr = %U, port %u", getpid (),
2656 session_index, ep->is_ip4, format_ip46_address,
2657 &session->transport.rmt_ip,
2658 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
2659 clib_net_to_host_u16 (ep->port));
2665 case VPPCOM_ATTR_GET_LCL_ADDR:
2666 if (PREDICT_TRUE (buffer && buflen &&
2667 (*buflen >= sizeof (*ep)) && ep->ip))
2669 ep->is_ip4 = session->transport.is_ip4;
2670 ep->port = session->transport.lcl_port;
2671 if (session->transport.is_ip4)
2672 clib_memcpy (ep->ip, &session->transport.lcl_ip.ip4,
2673 sizeof (ip4_address_t));
2675 clib_memcpy (ep->ip, &session->transport.lcl_ip.ip6,
2676 sizeof (ip6_address_t));
2677 *buflen = sizeof (*ep);
2678 VDBG (1, "VCL<%d>: VPPCOM_ATTR_GET_LCL_ADDR: sid %u, is_ip4 = %u,"
2679 " addr = %U port %d", getpid (),
2680 session_index, ep->is_ip4, format_ip46_address,
2681 &session->transport.lcl_ip,
2682 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
2683 clib_net_to_host_u16 (ep->port));
2689 case VPPCOM_ATTR_GET_LIBC_EPFD:
2690 rv = session->libc_epfd;
2691 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd %d",
2695 case VPPCOM_ATTR_SET_LIBC_EPFD:
2696 if (PREDICT_TRUE (buffer && buflen &&
2697 (*buflen == sizeof (session->libc_epfd))))
2699 session->libc_epfd = *(int *) buffer;
2700 *buflen = sizeof (session->libc_epfd);
2702 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd %d, "
2703 "buflen %d", getpid (), session->libc_epfd, *buflen);
2709 case VPPCOM_ATTR_GET_PROTOCOL:
2710 if (buffer && buflen && (*buflen >= sizeof (int)))
2712 *(int *) buffer = session->session_type;
2713 *buflen = sizeof (int);
2715 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_PROTOCOL: %d (%s), buflen %d",
2716 getpid (), *(int *) buffer, *(int *) buffer ? "UDP" : "TCP",
2723 case VPPCOM_ATTR_GET_LISTEN:
2724 if (buffer && buflen && (*buflen >= sizeof (int)))
2726 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2727 VCL_SESS_ATTR_LISTEN);
2728 *buflen = sizeof (int);
2730 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_LISTEN: %d, buflen %d",
2731 getpid (), *(int *) buffer, *buflen);
2737 case VPPCOM_ATTR_GET_ERROR:
2738 if (buffer && buflen && (*buflen >= sizeof (int)))
2740 *(int *) buffer = 0;
2741 *buflen = sizeof (int);
2743 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_ERROR: %d, buflen %d, #VPP-TBD#",
2744 getpid (), *(int *) buffer, *buflen);
2750 case VPPCOM_ATTR_GET_TX_FIFO_LEN:
2751 if (buffer && buflen && (*buflen >= sizeof (u32)))
2755 *(size_t *) buffer = (session->sndbuf_size ? session->sndbuf_size :
2756 session->tx_fifo ? session->tx_fifo->nitems :
2757 vcm->cfg.tx_fifo_size);
2758 *buflen = sizeof (u32);
2760 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TX_FIFO_LEN: %u (0x%x), "
2761 "buflen %d, #VPP-TBD#", getpid (),
2762 *(size_t *) buffer, *(size_t *) buffer, *buflen);
2768 case VPPCOM_ATTR_SET_TX_FIFO_LEN:
2769 if (buffer && buflen && (*buflen == sizeof (u32)))
2772 session->sndbuf_size = *(u32 *) buffer;
2773 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TX_FIFO_LEN: %u (0x%x), "
2774 "buflen %d, #VPP-TBD#", getpid (),
2775 session->sndbuf_size, session->sndbuf_size, *buflen);
2781 case VPPCOM_ATTR_GET_RX_FIFO_LEN:
2782 if (buffer && buflen && (*buflen >= sizeof (u32)))
2786 *(size_t *) buffer = (session->rcvbuf_size ? session->rcvbuf_size :
2787 session->rx_fifo ? session->rx_fifo->nitems :
2788 vcm->cfg.rx_fifo_size);
2789 *buflen = sizeof (u32);
2791 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_RX_FIFO_LEN: %u (0x%x), "
2792 "buflen %d, #VPP-TBD#", getpid (),
2793 *(size_t *) buffer, *(size_t *) buffer, *buflen);
2799 case VPPCOM_ATTR_SET_RX_FIFO_LEN:
2800 if (buffer && buflen && (*buflen == sizeof (u32)))
2803 session->rcvbuf_size = *(u32 *) buffer;
2804 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_RX_FIFO_LEN: %u (0x%x), "
2805 "buflen %d, #VPP-TBD#", getpid (),
2806 session->sndbuf_size, session->sndbuf_size, *buflen);
2812 case VPPCOM_ATTR_GET_REUSEADDR:
2813 if (buffer && buflen && (*buflen >= sizeof (int)))
2816 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2817 VCL_SESS_ATTR_REUSEADDR);
2818 *buflen = sizeof (int);
2820 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_REUSEADDR: %d, "
2821 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
2827 case VPPCOM_ATTR_SET_REUSEADDR:
2828 if (buffer && buflen && (*buflen == sizeof (int)) &&
2829 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
2832 if (*(int *) buffer)
2833 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEADDR);
2835 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEADDR);
2837 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_REUSEADDR: %d, buflen %d,"
2838 " #VPP-TBD#", getpid (),
2839 VCL_SESS_ATTR_TEST (session->attr,
2840 VCL_SESS_ATTR_REUSEADDR), *buflen);
2846 case VPPCOM_ATTR_GET_REUSEPORT:
2847 if (buffer && buflen && (*buflen >= sizeof (int)))
2850 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2851 VCL_SESS_ATTR_REUSEPORT);
2852 *buflen = sizeof (int);
2854 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_REUSEPORT: %d, buflen %d,"
2855 " #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
2861 case VPPCOM_ATTR_SET_REUSEPORT:
2862 if (buffer && buflen && (*buflen == sizeof (int)) &&
2863 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
2866 if (*(int *) buffer)
2867 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEPORT);
2869 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEPORT);
2871 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_REUSEPORT: %d, buflen %d,"
2872 " #VPP-TBD#", getpid (),
2873 VCL_SESS_ATTR_TEST (session->attr,
2874 VCL_SESS_ATTR_REUSEPORT), *buflen);
2880 case VPPCOM_ATTR_GET_BROADCAST:
2881 if (buffer && buflen && (*buflen >= sizeof (int)))
2884 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2885 VCL_SESS_ATTR_BROADCAST);
2886 *buflen = sizeof (int);
2888 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_BROADCAST: %d, buflen %d,"
2889 " #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
2895 case VPPCOM_ATTR_SET_BROADCAST:
2896 if (buffer && buflen && (*buflen == sizeof (int)))
2899 if (*(int *) buffer)
2900 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_BROADCAST);
2902 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_BROADCAST);
2904 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_BROADCAST: %d, buflen %d, "
2905 "#VPP-TBD#", getpid (),
2906 VCL_SESS_ATTR_TEST (session->attr,
2907 VCL_SESS_ATTR_BROADCAST), *buflen);
2913 case VPPCOM_ATTR_GET_V6ONLY:
2914 if (buffer && buflen && (*buflen >= sizeof (int)))
2917 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2918 VCL_SESS_ATTR_V6ONLY);
2919 *buflen = sizeof (int);
2921 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_V6ONLY: %d, buflen %d, "
2922 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
2928 case VPPCOM_ATTR_SET_V6ONLY:
2929 if (buffer && buflen && (*buflen == sizeof (int)))
2932 if (*(int *) buffer)
2933 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_V6ONLY);
2935 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_V6ONLY);
2937 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_V6ONLY: %d, buflen %d, "
2938 "#VPP-TBD#", getpid (),
2939 VCL_SESS_ATTR_TEST (session->attr,
2940 VCL_SESS_ATTR_V6ONLY), *buflen);
2946 case VPPCOM_ATTR_GET_KEEPALIVE:
2947 if (buffer && buflen && (*buflen >= sizeof (int)))
2950 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2951 VCL_SESS_ATTR_KEEPALIVE);
2952 *buflen = sizeof (int);
2954 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_KEEPALIVE: %d, buflen %d, "
2955 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
2961 case VPPCOM_ATTR_SET_KEEPALIVE:
2962 if (buffer && buflen && (*buflen == sizeof (int)))
2965 if (*(int *) buffer)
2966 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_KEEPALIVE);
2968 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_KEEPALIVE);
2970 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_KEEPALIVE: %d, buflen %d, "
2971 "#VPP-TBD#", getpid (),
2972 VCL_SESS_ATTR_TEST (session->attr,
2973 VCL_SESS_ATTR_KEEPALIVE), *buflen);
2979 case VPPCOM_ATTR_GET_TCP_NODELAY:
2980 if (buffer && buflen && (*buflen >= sizeof (int)))
2983 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2984 VCL_SESS_ATTR_TCP_NODELAY);
2985 *buflen = sizeof (int);
2987 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_NODELAY: %d, buflen %d, "
2988 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
2994 case VPPCOM_ATTR_SET_TCP_NODELAY:
2995 if (buffer && buflen && (*buflen == sizeof (int)))
2998 if (*(int *) buffer)
2999 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
3001 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
3003 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_NODELAY: %d, buflen %d, "
3004 "#VPP-TBD#", getpid (),
3005 VCL_SESS_ATTR_TEST (session->attr,
3006 VCL_SESS_ATTR_TCP_NODELAY), *buflen);
3012 case VPPCOM_ATTR_GET_TCP_KEEPIDLE:
3013 if (buffer && buflen && (*buflen >= sizeof (int)))
3016 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3017 VCL_SESS_ATTR_TCP_KEEPIDLE);
3018 *buflen = sizeof (int);
3020 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d, buflen %d, "
3021 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
3027 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
3028 if (buffer && buflen && (*buflen == sizeof (int)))
3031 if (*(int *) buffer)
3032 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
3034 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
3036 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d, buflen %d, "
3037 "#VPP-TBD#", getpid (),
3038 VCL_SESS_ATTR_TEST (session->attr,
3039 VCL_SESS_ATTR_TCP_KEEPIDLE), *buflen);
3045 case VPPCOM_ATTR_GET_TCP_KEEPINTVL:
3046 if (buffer && buflen && (*buflen >= sizeof (int)))
3049 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3050 VCL_SESS_ATTR_TCP_KEEPINTVL);
3051 *buflen = sizeof (int);
3053 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_KEEPINTVL: %d, buflen %d, "
3054 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
3060 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
3061 if (buffer && buflen && (*buflen == sizeof (int)))
3064 if (*(int *) buffer)
3065 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
3067 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
3069 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d, buflen %d, "
3070 "#VPP-TBD#", getpid (),
3071 VCL_SESS_ATTR_TEST (session->attr,
3072 VCL_SESS_ATTR_TCP_KEEPINTVL), *buflen);
3078 case VPPCOM_ATTR_GET_TCP_USER_MSS:
3079 if (buffer && buflen && (*buflen >= sizeof (u32)))
3082 *(u32 *) buffer = session->user_mss;
3083 *buflen = sizeof (int);
3085 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_USER_MSS: %d, buflen %d,"
3086 " #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
3092 case VPPCOM_ATTR_SET_TCP_USER_MSS:
3093 if (buffer && buflen && (*buflen == sizeof (u32)))
3096 session->user_mss = *(u32 *) buffer;
3098 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_USER_MSS: %u, buflen %d, "
3099 "#VPP-TBD#", getpid (), session->user_mss, *buflen);
3111 VCL_SESSION_UNLOCK ();
3116 vppcom_session_recvfrom (uint32_t session_index, void *buffer,
3117 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3120 vcl_session_t *session = 0;
3124 VCL_SESSION_LOCK ();
3125 rv = vppcom_session_at_index (session_index, &session);
3126 if (PREDICT_FALSE (rv))
3128 VCL_SESSION_UNLOCK ();
3129 VDBG (0, "VCL<%d>: invalid session, sid (%u) has been closed!",
3130 getpid (), session_index);
3131 VCL_SESSION_UNLOCK ();
3132 return VPPCOM_EBADFD;
3134 ep->is_ip4 = session->transport.is_ip4;
3135 ep->port = session->transport.rmt_port;
3136 VCL_SESSION_UNLOCK ();
3140 rv = vppcom_session_read (session_index, buffer, buflen);
3141 else if (flags & MSG_PEEK)
3142 rv = vppcom_session_peek (session_index, buffer, buflen);
3145 clib_warning ("VCL<%d>: Unsupport flags for recvfrom %d",
3147 return VPPCOM_EAFNOSUPPORT;
3152 if (session->transport.is_ip4)
3153 clib_memcpy (ep->ip, &session->transport.rmt_ip.ip4,
3154 sizeof (ip4_address_t));
3156 clib_memcpy (ep->ip, &session->transport.rmt_ip.ip6,
3157 sizeof (ip6_address_t));
3164 vppcom_session_sendto (uint32_t session_index, void *buffer,
3165 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3168 return VPPCOM_EINVAL;
3173 return VPPCOM_EINVAL;
3178 // TBD check the flags and do the right thing
3179 VDBG (2, "VCL<%d>: handling flags 0x%u (%d) not implemented yet.",
3180 getpid (), flags, flags);
3183 return (vppcom_session_write (session_index, buffer, buflen));
3187 vppcom_poll (vcl_poll_t * vp, uint32_t n_sids, double wait_for_time)
3189 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
3190 u32 i, keep_trying = 1;
3193 VDBG (3, "VCL<%d>: vp %p, nsids %u, wait_for_time %f",
3194 getpid (), vp, n_sids, wait_for_time);
3197 return VPPCOM_EFAULT;
3201 vcl_session_t *session;
3203 for (i = 0; i < n_sids; i++)
3205 ASSERT (vp[i].revents);
3207 VCL_SESSION_LOCK_AND_GET (vp[i].sid, &session);
3208 VCL_SESSION_UNLOCK ();
3213 if (POLLIN & vp[i].events)
3215 VCL_SESSION_LOCK_AND_GET (vp[i].sid, &session);
3216 rv = vppcom_session_read_ready (session);
3217 VCL_SESSION_UNLOCK ();
3220 *vp[i].revents |= POLLIN;
3227 case VPPCOM_ECONNRESET:
3228 *vp[i].revents = POLLHUP;
3232 *vp[i].revents = POLLERR;
3239 if (POLLOUT & vp[i].events)
3241 VCL_SESSION_LOCK_AND_GET (vp[i].sid, &session);
3242 rv = vppcom_session_write_ready (session, vp[i].sid);
3243 VCL_SESSION_UNLOCK ();
3246 *vp[i].revents |= POLLOUT;
3253 case VPPCOM_ECONNRESET:
3254 *vp[i].revents = POLLHUP;
3258 *vp[i].revents = POLLERR;
3265 if (0) // Note "done:" label used by VCL_SESSION_LOCK_AND_GET()
3268 *vp[i].revents = POLLNVAL;
3272 if (wait_for_time != -1)
3273 keep_trying = (clib_time_now (&vcm->clib_time) <= timeout) ? 1 : 0;
3275 while ((num_ev == 0) && keep_trying);
3277 if (VPPCOM_DEBUG > 3)
3279 clib_warning ("VCL<%d>: returning %d", getpid (), num_ev);
3280 for (i = 0; i < n_sids; i++)
3282 clib_warning ("VCL<%d>: vp[%d].sid %d (0x%x), .events 0x%x, "
3283 ".revents 0x%x", getpid (), i, vp[i].sid, vp[i].sid,
3284 vp[i].events, *vp[i].revents);
3291 vppcom_mq_epoll_fd (void)
3293 return vcm->mqs_epfd;
3297 * fd.io coding-style-patch-verification: ON
3300 * eval: (c-set-style "gnu")