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_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_handle_mq_ctrl_event (session_event_t * e)
491 session_accepted_msg_t *accepted_msg;
492 session_disconnected_msg_t *disconnected_msg;
493 vcl_session_msg_t *vcl_msg;
494 vcl_session_t *session;
498 switch (e->event_type)
500 case FIFO_EVENT_APP_RX:
501 clib_warning ("unhandled rx: sid %u (0x%x)",
502 e->fifo->client_session_index,
503 e->fifo->client_session_index);
505 case SESSION_CTRL_EVT_ACCEPTED:
506 accepted_msg = (session_accepted_msg_t *) e->data;
507 handle = accepted_msg->listener_handle;
508 session = vppcom_session_table_lookup_listener (handle);
511 clib_warning ("VCL<%d>: ERROR: couldn't find listen session:"
512 "listener handle %llx", getpid (), handle);
516 clib_fifo_add2 (session->accept_evts_fifo, vcl_msg);
517 vcl_msg->accepted_msg = *accepted_msg;
519 case SESSION_CTRL_EVT_CONNECTED:
520 vcl_session_connected_handler ((session_connected_msg_t *) e->data);
522 case SESSION_CTRL_EVT_DISCONNECTED:
523 disconnected_msg = (session_disconnected_msg_t *) e->data;
524 sid = vcl_session_get_index_from_handle (disconnected_msg->handle);
525 session = vcl_session_get (sid);
528 VDBG (0, "request to disconnect unknown handle 0x%llx",
529 disconnected_msg->handle);
532 session->session_state = STATE_DISCONNECT;
533 VDBG (0, "disconnected handle 0xllx, sid %u", disconnected_msg->handle,
536 case SESSION_CTRL_EVT_RESET:
537 vcl_reset_handler ((session_reset_msg_t *) e->data);
540 clib_warning ("unhandled %u", e->event_type);
546 vppcom_wait_for_session_state_change (u32 session_index,
547 session_state_t state,
550 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
551 vcl_session_t *volatile session;
559 rv = vppcom_session_at_index (session_index, &session);
560 if (PREDICT_FALSE (rv))
562 VCL_SESSION_UNLOCK ();
565 if (session->session_state & state)
567 VCL_SESSION_UNLOCK ();
570 if (session->session_state & STATE_FAILED)
572 VCL_SESSION_UNLOCK ();
573 return VPPCOM_ECONNREFUSED;
575 VCL_SESSION_UNLOCK ();
577 if (svm_msg_q_sub (vcm->app_event_queue, &msg, SVM_Q_NOWAIT, 0))
579 e = svm_msg_q_msg_data (vcm->app_event_queue, &msg);
580 vcl_handle_mq_ctrl_event (e);
581 svm_msg_q_free_msg (vcm->app_event_queue, &msg);
583 while (clib_time_now (&vcm->clib_time) < timeout);
585 VDBG (0, "VCL<%d>: timeout waiting for state 0x%x (%s)", getpid (), state,
586 vppcom_session_state_str (state));
587 vcl_evt (VCL_EVT_SESSION_TIMEOUT, session, session_state);
589 return VPPCOM_ETIMEDOUT;
593 vppcom_app_session_enable (void)
597 if (vcm->app_state != STATE_APP_ENABLED)
599 vppcom_send_session_enable_disable (1 /* is_enabled == TRUE */ );
600 rv = vppcom_wait_for_app_state_change (STATE_APP_ENABLED);
601 if (PREDICT_FALSE (rv))
603 VDBG (0, "VCL<%d>: application session enable timed out! "
604 "returning %d (%s)", getpid (), rv, vppcom_retval_str (rv));
612 vppcom_app_attach (void)
616 vppcom_app_send_attach ();
617 rv = vppcom_wait_for_app_state_change (STATE_APP_ATTACHED);
618 if (PREDICT_FALSE (rv))
620 VDBG (0, "VCL<%d>: application attach timed out! returning %d (%s)",
621 getpid (), rv, vppcom_retval_str (rv));
629 vppcom_session_unbind (u32 session_index)
631 vcl_session_t *session = 0;
635 VCL_SESSION_LOCK_AND_GET (session_index, &session);
637 vpp_handle = session->vpp_handle;
638 vppcom_session_table_del_listener (vpp_handle);
639 session->vpp_handle = ~0;
640 session->session_state = STATE_DISCONNECT;
642 VCL_SESSION_UNLOCK ();
644 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: sending unbind msg! new state"
645 " 0x%x (%s)", getpid (), vpp_handle, session_index, STATE_DISCONNECT,
646 vppcom_session_state_str (STATE_DISCONNECT));
647 vcl_evt (VCL_EVT_UNBIND, session);
648 vppcom_send_unbind_sock (vpp_handle);
655 vppcom_session_disconnect (u32 session_index)
657 svm_msg_q_t *vpp_evt_q;
658 vcl_session_t *session;
659 session_state_t state;
663 VCL_SESSION_LOCK_AND_GET (session_index, &session);
665 vpp_handle = session->vpp_handle;
666 state = session->session_state;
667 VCL_SESSION_UNLOCK ();
669 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u state 0x%x (%s)", getpid (),
670 vpp_handle, session_index, state, vppcom_session_state_str (state));
672 if (PREDICT_FALSE (state & STATE_LISTEN))
674 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
675 "Cannot disconnect a listen socket!",
676 getpid (), vpp_handle, session_index);
681 if (state & STATE_CLOSE_ON_EMPTY)
683 vpp_evt_q = vcl_session_vpp_evt_q (session);
684 vcl_send_session_disconnected_reply (vpp_evt_q, vcm->my_client_index,
686 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: sending disconnect "
687 "REPLY...", getpid (), vpp_handle, session_index);
691 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: sending disconnect...",
692 getpid (), vpp_handle, session_index);
693 vppcom_send_disconnect_session (vpp_handle, session_index);
701 * VPPCOM Public API functions
704 vppcom_app_create (char *app_name)
706 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
712 vppcom_cfg (&vcm->cfg);
716 if (vcl_cfg->use_mq_eventfd)
717 vcm->mqs_epfd = epoll_create (1);
719 clib_spinlock_init (&vcm->session_fifo_lockp);
720 clib_fifo_validate (vcm->client_session_index_fifo,
721 vcm->cfg.listen_queue_size);
722 clib_spinlock_init (&vcm->sessions_lockp);
725 vcm->main_cpu = os_get_thread_index ();
727 vcm->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
728 vcm->ct_registration_by_mq = hash_create (0, sizeof (uword));
729 clib_spinlock_init (&vcm->ct_registration_lock);
731 clib_time_init (&vcm->clib_time);
732 vppcom_init_error_string_table ();
733 svm_fifo_segment_main_init (vcl_cfg->segment_baseva,
734 20 /* timeout in secs */ );
735 vec_validate (vcm->mq_events, 64);
736 vec_validate (vcm->mq_msg_vector, 128);
737 vec_reset_length (vcm->mq_msg_vector);
740 if (vcm->my_client_index == ~0)
742 /* API hookup and connect to VPP */
743 vppcom_api_hookup ();
745 vcm->app_state = STATE_APP_START;
746 rv = vppcom_connect_to_vpp (app_name);
749 clib_warning ("VCL<%d>: ERROR: couldn't connect to VPP!",
754 /* State event handling thread */
756 rv = vce_start_event_thread (&(vcm->event_thread), 20);
758 VDBG (0, "VCL<%d>: sending session enable", getpid ());
760 rv = vppcom_app_session_enable ();
763 clib_warning ("VCL<%d>: ERROR: vppcom_app_session_enable() "
764 "failed!", getpid ());
768 VDBG (0, "VCL<%d>: sending app attach", getpid ());
770 rv = vppcom_app_attach ();
773 clib_warning ("VCL<%d>: ERROR: vppcom_app_attach() failed!",
778 VDBG (0, "VCL<%d>: app_name '%s', my_client_index %d (0x%x)",
779 getpid (), app_name, vcm->my_client_index, vcm->my_client_index);
786 vppcom_app_destroy (void)
789 f64 orig_app_timeout;
791 if (vcm->my_client_index == ~0)
794 VDBG (0, "VCL<%d>: detaching from VPP, my_client_index %d (0x%x)",
795 getpid (), vcm->my_client_index, vcm->my_client_index);
796 vcl_evt (VCL_EVT_DETACH, vcm);
798 vppcom_app_send_detach ();
799 orig_app_timeout = vcm->cfg.app_timeout;
800 vcm->cfg.app_timeout = 2.0;
801 rv = vppcom_wait_for_app_state_change (STATE_APP_ENABLED);
802 vcm->cfg.app_timeout = orig_app_timeout;
803 if (PREDICT_FALSE (rv))
804 VDBG (0, "VCL<%d>: application detach timed out! returning %d (%s)",
805 getpid (), rv, vppcom_retval_str (rv));
808 vl_client_disconnect_from_vlib ();
809 vcm->my_client_index = ~0;
810 vcm->app_state = STATE_APP_START;
814 vppcom_session_create (u8 proto, u8 is_nonblocking)
816 vcl_session_t *session;
820 pool_get (vcm->sessions, session);
821 memset (session, 0, sizeof (*session));
822 session_index = session - vcm->sessions;
824 session->session_type = proto;
825 session->session_state = STATE_START;
826 session->vpp_handle = ~0;
827 session->is_dgram = proto == VPPCOM_PROTO_UDP;
830 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK);
832 vcl_evt (VCL_EVT_CREATE, session, session_type, session->session_state,
833 is_nonblocking, session_index);
835 VCL_SESSION_UNLOCK ();
837 VDBG (0, "VCL<%d>: sid %u", getpid (), session_index);
839 return (int) session_index;
843 vppcom_session_close (uint32_t session_index)
845 vcl_session_t *session = 0;
853 session_state_t state;
855 VCL_SESSION_LOCK_AND_GET (session_index, &session);
856 is_vep = session->is_vep;
857 is_vep_session = session->is_vep_session;
858 next_sid = session->vep.next_sid;
859 vep_idx = session->vep.vep_idx;
860 state = session->session_state;
861 vpp_handle = session->vpp_handle;
862 VCL_SESSION_UNLOCK ();
864 if (VPPCOM_DEBUG > 0)
867 clib_warning ("VCL<%d>: vep_idx %u / sid %u: "
868 "closing epoll session...",
869 getpid (), session_index, session_index);
871 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %d: "
872 "closing session...",
873 getpid (), vpp_handle, session_index);
878 while (next_sid != ~0)
880 rv = vppcom_epoll_ctl (session_index, EPOLL_CTL_DEL, next_sid, 0);
881 if (PREDICT_FALSE (rv < 0))
882 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: EPOLL_CTL_DEL "
883 "vep_idx %u failed! rv %d (%s)",
884 getpid (), vpp_handle, next_sid, vep_idx,
885 rv, vppcom_retval_str (rv));
887 VCL_SESSION_LOCK_AND_GET (session_index, &session);
888 next_sid = session->vep.next_sid;
889 VCL_SESSION_UNLOCK ();
896 rv = vppcom_epoll_ctl (vep_idx, EPOLL_CTL_DEL, session_index, 0);
898 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: EPOLL_CTL_DEL "
899 "vep_idx %u failed! rv %d (%s)",
900 getpid (), vpp_handle, session_index,
901 vep_idx, rv, vppcom_retval_str (rv));
904 if (state & STATE_LISTEN)
906 rv = vppcom_session_unbind (session_index);
907 if (PREDICT_FALSE (rv < 0))
908 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: listener unbind "
909 "failed! rv %d (%s)",
910 getpid (), vpp_handle, session_index,
911 rv, vppcom_retval_str (rv));
913 else if (state & (CLIENT_STATE_OPEN | SERVER_STATE_OPEN))
915 rv = vppcom_session_disconnect (session_index);
916 if (PREDICT_FALSE (rv < 0))
917 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
918 "session disconnect failed! rv %d (%s)",
919 getpid (), vpp_handle, session_index,
920 rv, vppcom_retval_str (rv));
924 VCL_SESSION_LOCK_AND_GET (session_index, &session);
925 if (vcl_session_is_ct (session))
927 vcl_cut_through_registration_t *ctr;
930 mq_addr = pointer_to_uword (session->our_evt_q);
931 ctr = vcl_ct_registration_lock_and_lookup (mq_addr);
933 if (ctr->epoll_evt_conn_index != ~0)
934 vcl_mq_epoll_del_evfd (ctr->epoll_evt_conn_index);
935 VDBG (0, "Removing ct registration %u",
936 vcl_ct_registration_index (ctr));
937 vcl_ct_registration_del (ctr);
938 vcl_ct_registration_unlock ();
941 vpp_handle = session->vpp_handle;
942 if (vpp_handle != ~0)
944 p = hash_get (vcm->session_index_by_vpp_handles, vpp_handle);
946 hash_unset (vcm->session_index_by_vpp_handles, vpp_handle);
948 pool_put_index (vcm->sessions, session_index);
950 VCL_SESSION_UNLOCK ();
952 if (VPPCOM_DEBUG > 0)
955 clib_warning ("VCL<%d>: vep_idx %u / sid %u: epoll session removed.",
956 getpid (), session_index, session_index);
958 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: session removed.",
959 getpid (), vpp_handle, session_index);
963 vcl_evt (VCL_EVT_CLOSE, session, rv);
969 vppcom_session_bind (uint32_t session_index, vppcom_endpt_t * ep)
971 vcl_session_t *session = 0;
975 return VPPCOM_EINVAL;
977 VCL_SESSION_LOCK_AND_GET (session_index, &session);
981 VCL_SESSION_UNLOCK ();
982 clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
983 "bind to an epoll session!", getpid (), session_index);
988 session->transport.is_ip4 = ep->is_ip4;
990 clib_memcpy (&session->transport.lcl_ip.ip4, ep->ip,
991 sizeof (ip4_address_t));
993 clib_memcpy (&session->transport.lcl_ip.ip6, ep->ip,
994 sizeof (ip6_address_t));
995 session->transport.lcl_port = ep->port;
997 VDBG (0, "VCL<%d>: sid %u: binding to local %s address %U port %u, "
998 "proto %s", getpid (), session_index,
999 session->transport.is_ip4 ? "IPv4" : "IPv6",
1000 format_ip46_address, &session->transport.lcl_ip,
1001 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
1002 clib_net_to_host_u16 (session->transport.lcl_port),
1003 session->session_type ? "UDP" : "TCP");
1004 vcl_evt (VCL_EVT_BIND, session);
1005 VCL_SESSION_UNLOCK ();
1007 if (session->session_type == VPPCOM_PROTO_UDP)
1008 vppcom_session_listen (session_index, 10);
1015 vppcom_session_listen (uint32_t listen_session_index, uint32_t q_len)
1017 vcl_session_t *listen_session = 0;
1018 u64 listen_vpp_handle;
1021 if (q_len == 0 || q_len == ~0)
1022 q_len = vcm->cfg.listen_queue_size;
1024 VCL_SESSION_LOCK_AND_GET (listen_session_index, &listen_session);
1026 if (listen_session->is_vep)
1028 VCL_SESSION_UNLOCK ();
1029 clib_warning ("VCL<%d>: ERROR: sid %u: cannot listen on an "
1030 "epoll session!", getpid (), listen_session_index);
1035 listen_vpp_handle = listen_session->vpp_handle;
1036 if (listen_session->session_state & STATE_LISTEN)
1038 VCL_SESSION_UNLOCK ();
1039 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: already in listen state!",
1040 getpid (), listen_vpp_handle, listen_session_index);
1045 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: sending VPP bind+listen "
1046 "request...", getpid (), listen_vpp_handle, listen_session_index);
1048 vppcom_send_bind_sock (listen_session, listen_session_index);
1049 VCL_SESSION_UNLOCK ();
1050 retval = vppcom_wait_for_session_state_change (listen_session_index,
1052 vcm->cfg.session_timeout);
1054 VCL_SESSION_LOCK_AND_GET (listen_session_index, &listen_session);
1055 if (PREDICT_FALSE (retval))
1057 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: bind+listen failed! "
1058 "returning %d (%s)", getpid (), listen_session->vpp_handle,
1059 listen_session_index, retval, vppcom_retval_str (retval));
1060 VCL_SESSION_UNLOCK ();
1065 VCL_SESSION_UNLOCK ();
1072 validate_args_session_accept_ (vcl_session_t * listen_session)
1074 u32 listen_session_index = listen_session - vcm->sessions;
1076 /* Input validation - expects spinlock on sessions_lockp */
1077 if (listen_session->is_vep)
1079 clib_warning ("VCL<%d>: ERROR: sid %u: cannot accept on an "
1080 "epoll session!", getpid (), listen_session_index);
1081 return VPPCOM_EBADFD;
1084 if (listen_session->session_state != STATE_LISTEN)
1086 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
1087 "not in listen state! state 0x%x (%s)", getpid (),
1088 listen_session->vpp_handle, listen_session_index,
1089 listen_session->session_state,
1090 vppcom_session_state_str (listen_session->session_state));
1091 return VPPCOM_EBADFD;
1097 vppcom_session_accept (uint32_t listen_session_index, vppcom_endpt_t * ep,
1100 session_accepted_msg_t accepted_msg;
1101 vcl_session_t *listen_session = 0;
1102 vcl_session_t *client_session = 0;
1103 u32 client_session_index = ~0;
1104 svm_msg_q_t *vpp_evt_q;
1105 vcl_session_msg_t *evt;
1106 u64 listen_vpp_handle;
1107 svm_msg_q_msg_t msg;
1112 VCL_SESSION_LOCK_AND_GET (listen_session_index, &listen_session);
1114 if (validate_args_session_accept_ (listen_session))
1116 VCL_SESSION_UNLOCK ();
1120 VCL_SESSION_UNLOCK ();
1122 if (clib_fifo_elts (listen_session->accept_evts_fifo))
1124 clib_fifo_sub2 (listen_session->accept_evts_fifo, evt);
1125 accepted_msg = evt->accepted_msg;
1129 is_nonblocking = VCL_SESS_ATTR_TEST (listen_session->attr,
1130 VCL_SESS_ATTR_NONBLOCK);
1131 if (svm_msg_q_is_empty (vcm->app_event_queue) && is_nonblocking)
1132 return VPPCOM_EAGAIN;
1136 if (svm_msg_q_sub (vcm->app_event_queue, &msg, SVM_Q_WAIT, 0))
1137 return VPPCOM_EAGAIN;
1139 e = svm_msg_q_msg_data (vcm->app_event_queue, &msg);
1140 if (e->event_type != SESSION_CTRL_EVT_ACCEPTED)
1142 clib_warning ("discarded event: %u", e->event_type);
1143 svm_msg_q_free_msg (vcm->app_event_queue, &msg);
1146 clib_memcpy (&accepted_msg, e->data, sizeof (accepted_msg));
1147 svm_msg_q_free_msg (vcm->app_event_queue, &msg);
1153 client_session_index = vcl_session_accepted_handler (&accepted_msg);
1154 listen_session = vcl_session_get (listen_session_index);
1155 VCL_SESSION_LOCK_AND_GET (client_session_index, &client_session);
1156 rv = client_session_index;
1158 if (flags & O_NONBLOCK)
1159 VCL_SESS_ATTR_SET (client_session->attr, VCL_SESS_ATTR_NONBLOCK);
1161 listen_vpp_handle = listen_session->vpp_handle;
1162 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: Got a client request! "
1163 "vpp handle 0x%llx, sid %u, flags %d, is_nonblocking %u",
1164 getpid (), listen_vpp_handle, listen_session_index,
1165 client_session->vpp_handle, client_session_index,
1166 flags, VCL_SESS_ATTR_TEST (client_session->attr,
1167 VCL_SESS_ATTR_NONBLOCK));
1171 ep->is_ip4 = client_session->transport.is_ip4;
1172 ep->port = client_session->transport.rmt_port;
1173 if (client_session->transport.is_ip4)
1174 clib_memcpy (ep->ip, &client_session->transport.rmt_ip.ip4,
1175 sizeof (ip4_address_t));
1177 clib_memcpy (ep->ip, &client_session->transport.rmt_ip.ip6,
1178 sizeof (ip6_address_t));
1181 if (accepted_msg.server_event_queue_address)
1182 vpp_evt_q = uword_to_pointer (accepted_msg.vpp_event_queue_address,
1185 vpp_evt_q = client_session->vpp_evt_q;
1187 vcl_send_session_accepted_reply (vpp_evt_q, client_session->client_context,
1188 client_session->vpp_handle, 0);
1190 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: accepted vpp handle 0x%llx, "
1191 "sid %u connection from peer %s address %U port %u to local %s "
1192 "address %U port %u", getpid (), listen_vpp_handle,
1193 listen_session_index, client_session->vpp_handle,
1194 client_session_index,
1195 client_session->transport.is_ip4 ? "IPv4" : "IPv6",
1196 format_ip46_address, &client_session->transport.rmt_ip,
1197 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
1198 clib_net_to_host_u16 (client_session->transport.rmt_port),
1199 client_session->transport.is_ip4 ? "IPv4" : "IPv6",
1200 format_ip46_address, &client_session->transport.lcl_ip,
1201 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
1202 clib_net_to_host_u16 (client_session->transport.lcl_port));
1203 vcl_evt (VCL_EVT_ACCEPT, client_session, listen_session,
1204 client_session_index);
1205 VCL_SESSION_UNLOCK ();
1212 vppcom_session_connect (uint32_t session_index, vppcom_endpt_t * server_ep)
1214 vcl_session_t *session = 0;
1216 int rv, retval = VPPCOM_OK;
1218 VCL_SESSION_LOCK_AND_GET (session_index, &session);
1220 if (PREDICT_FALSE (session->is_vep))
1222 VCL_SESSION_UNLOCK ();
1223 clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
1224 "connect on an epoll session!", getpid (), session_index);
1229 if (PREDICT_FALSE (session->session_state & CLIENT_STATE_OPEN))
1231 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: session already "
1232 "connected to %s %U port %d proto %s, state 0x%x (%s)",
1233 getpid (), session->vpp_handle, session_index,
1234 session->transport.is_ip4 ? "IPv4" : "IPv6",
1235 format_ip46_address,
1236 &session->transport.rmt_ip, session->transport.is_ip4 ?
1237 IP46_TYPE_IP4 : IP46_TYPE_IP6,
1238 clib_net_to_host_u16 (session->transport.rmt_port),
1239 session->session_type ? "UDP" : "TCP", session->session_state,
1240 vppcom_session_state_str (session->session_state));
1242 VCL_SESSION_UNLOCK ();
1246 session->transport.is_ip4 = server_ep->is_ip4;
1247 if (session->transport.is_ip4)
1248 clib_memcpy (&session->transport.rmt_ip.ip4, server_ep->ip,
1249 sizeof (ip4_address_t));
1251 clib_memcpy (&session->transport.rmt_ip.ip6, server_ep->ip,
1252 sizeof (ip6_address_t));
1253 session->transport.rmt_port = server_ep->port;
1255 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: connecting to server %s %U "
1257 getpid (), session->vpp_handle, session_index,
1258 session->transport.is_ip4 ? "IPv4" : "IPv6",
1259 format_ip46_address,
1260 &session->transport.rmt_ip, session->transport.is_ip4 ?
1261 IP46_TYPE_IP4 : IP46_TYPE_IP6,
1262 clib_net_to_host_u16 (session->transport.rmt_port),
1263 session->session_type ? "UDP" : "TCP");
1265 vppcom_send_connect_sock (session, session_index);
1266 VCL_SESSION_UNLOCK ();
1268 retval = vppcom_wait_for_session_state_change (session_index, STATE_CONNECT,
1269 vcm->cfg.session_timeout);
1271 VCL_SESSION_LOCK_AND_GET (session_index, &session);
1272 vpp_handle = session->vpp_handle;
1273 VCL_SESSION_UNLOCK ();
1276 if (PREDICT_FALSE (retval))
1279 if (VPPCOM_DEBUG > 0)
1282 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: connect "
1283 "failed! returning %d (%s)", getpid (), vpp_handle,
1284 session_index, rv, vppcom_retval_str (rv));
1286 clib_warning ("VCL<%d>: no session for sid %u: connect failed! "
1287 "returning %d (%s)", getpid (),
1288 session_index, rv, vppcom_retval_str (rv));
1292 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: connected!",
1293 getpid (), vpp_handle, session_index);
1299 vcl_is_rx_evt_for_session (session_event_t * e, u32 sid, u8 is_ct)
1302 return (e->event_type == FIFO_EVENT_APP_RX
1303 && e->fifo->client_session_index == sid);
1305 return (e->event_type == SESSION_IO_EVT_CT_TX);
1309 vcl_session_is_readable (vcl_session_t * s)
1311 return ((s->session_state & STATE_OPEN)
1312 || (s->session_state == STATE_LISTEN
1313 && s->session_type == VPPCOM_PROTO_UDP));
1317 vppcom_session_read_internal (uint32_t session_index, void *buf, int n,
1320 int n_read = 0, rv, is_nonblocking;
1321 vcl_session_t *s = 0;
1322 svm_fifo_t *rx_fifo;
1323 svm_msg_q_msg_t msg;
1330 VCL_SESSION_LOCK_AND_GET (session_index, &s);
1332 if (PREDICT_FALSE (s->is_vep))
1334 VCL_SESSION_UNLOCK ();
1335 clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
1336 "read from an epoll session!", getpid (), session_index);
1341 is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
1342 rx_fifo = s->rx_fifo;
1344 if (PREDICT_FALSE (!vcl_session_is_readable (s)))
1346 session_state_t state = s->session_state;
1347 VCL_SESSION_UNLOCK ();
1348 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
1350 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: %s session is not open! "
1351 "state 0x%x (%s), returning %d (%s)",
1352 getpid (), s->vpp_handle, session_index, state,
1353 vppcom_session_state_str (state), rv, vppcom_retval_str (rv));
1357 VCL_SESSION_UNLOCK ();
1358 mq = vcl_session_is_ct (s) ? s->our_evt_q : vcm->app_event_queue;
1359 svm_fifo_unset_event (rx_fifo);
1360 is_full = svm_fifo_is_full (rx_fifo);
1362 if (svm_fifo_is_empty (rx_fifo))
1371 svm_msg_q_lock (mq);
1372 if (svm_msg_q_is_empty (mq))
1373 svm_msg_q_wait (mq);
1375 svm_msg_q_sub_w_lock (mq, &msg);
1376 e = svm_msg_q_msg_data (mq, &msg);
1377 svm_msg_q_unlock (mq);
1378 if (!vcl_is_rx_evt_for_session (e, session_index,
1381 vcl_handle_mq_ctrl_event (e);
1382 svm_msg_q_free_msg (mq, &msg);
1385 svm_fifo_unset_event (rx_fifo);
1386 svm_msg_q_free_msg (mq, &msg);
1387 if (PREDICT_FALSE (s->session_state == STATE_CLOSE_ON_EMPTY))
1389 if (svm_fifo_is_empty (rx_fifo))
1396 n_read = app_recv_dgram_raw (rx_fifo, buf, n, &s->transport, 0, peek);
1398 n_read = app_recv_stream_raw (rx_fifo, buf, n, 0, peek);
1400 if (vcl_session_is_ct (s) && is_full)
1402 /* If the peer is not polling send notification */
1403 if (!svm_fifo_has_event (s->rx_fifo))
1404 app_send_io_evt_to_vpp (s->vpp_evt_q, s->rx_fifo,
1405 SESSION_IO_EVT_CT_RX, SVM_Q_WAIT);
1408 if (VPPCOM_DEBUG > 2)
1411 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: read %d bytes "
1412 "from (%p)", getpid (), s->vpp_handle,
1413 session_index, n_read, rx_fifo);
1415 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: nothing read! "
1416 "returning %d (%s)", getpid (), s->vpp_handle,
1417 session_index, rv, vppcom_retval_str (rv));
1426 vppcom_session_read (uint32_t session_index, void *buf, size_t n)
1428 return (vppcom_session_read_internal (session_index, buf, n, 0));
1432 vppcom_session_peek (uint32_t session_index, void *buf, int n)
1434 return (vppcom_session_read_internal (session_index, buf, n, 1));
1438 vppcom_session_read_ready (vcl_session_t * session)
1440 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
1441 if (PREDICT_FALSE (session->is_vep))
1443 clib_warning ("VCL<%d>: ERROR: sid %u: cannot read from an "
1444 "epoll session!", getpid (), vcl_session_index (session));
1445 return VPPCOM_EBADFD;
1448 if (PREDICT_FALSE (!(session->session_state & (STATE_OPEN | STATE_LISTEN))))
1450 session_state_t state = session->session_state;
1453 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
1455 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: session is not open!"
1456 " state 0x%x (%s), returning %d (%s)", getpid (),
1457 session->vpp_handle, vcl_session_index (session), state,
1458 vppcom_session_state_str (state), rv, vppcom_retval_str (rv));
1462 if (session->session_state & STATE_LISTEN)
1463 return clib_fifo_elts (session->accept_evts_fifo);
1465 return svm_fifo_max_dequeue (session->rx_fifo);
1469 vcl_is_tx_evt_for_session (session_event_t * e, u32 sid, u8 is_ct)
1472 return (e->event_type == FIFO_EVENT_APP_TX
1473 && e->fifo->client_session_index == sid);
1475 return (e->event_type == SESSION_IO_EVT_CT_RX);
1479 vppcom_session_write (uint32_t session_index, void *buf, size_t n)
1481 int rv, n_write, is_nonblocking;
1482 vcl_session_t *s = 0;
1483 svm_fifo_t *tx_fifo = 0;
1484 session_evt_type_t et;
1485 svm_msg_q_msg_t msg;
1491 VCL_SESSION_LOCK_AND_GET (session_index, &s);
1493 tx_fifo = s->tx_fifo;
1494 is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
1496 if (PREDICT_FALSE (s->is_vep))
1498 VCL_SESSION_UNLOCK ();
1499 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
1500 "cannot write to an epoll session!",
1501 getpid (), s->vpp_handle, session_index);
1507 if (!(s->session_state & STATE_OPEN))
1509 session_state_t state = s->session_state;
1510 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
1511 VCL_SESSION_UNLOCK ();
1512 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: session is not open! "
1514 getpid (), s->vpp_handle, session_index,
1515 state, vppcom_session_state_str (state));
1519 VCL_SESSION_UNLOCK ();
1521 mq = vcl_session_is_ct (s) ? s->our_evt_q : vcm->app_event_queue;
1522 if (svm_fifo_is_full (tx_fifo))
1526 rv = VPPCOM_EWOULDBLOCK;
1529 while (svm_fifo_is_full (tx_fifo))
1531 svm_msg_q_lock (mq);
1532 while (svm_msg_q_is_empty (mq) && svm_msg_q_timedwait (mq, 10e-6))
1534 svm_msg_q_sub_w_lock (mq, &msg);
1535 e = svm_msg_q_msg_data (mq, &msg);
1536 svm_msg_q_unlock (mq);
1538 if (!vcl_is_tx_evt_for_session (e, session_index,
1540 vcl_handle_mq_ctrl_event (e);
1541 svm_msg_q_free_msg (mq, &msg);
1545 ASSERT (FIFO_EVENT_APP_TX + 1 == SESSION_IO_EVT_CT_TX);
1546 et = FIFO_EVENT_APP_TX + vcl_session_is_ct (s);
1548 n_write = app_send_dgram_raw (tx_fifo, &s->transport,
1549 s->vpp_evt_q, buf, n, et, SVM_Q_WAIT);
1551 n_write = app_send_stream_raw (tx_fifo, s->vpp_evt_q, buf, n, et,
1554 ASSERT (n_write > 0);
1556 if (VPPCOM_DEBUG > 2)
1559 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
1560 "FIFO-FULL (%p)", getpid (), s->vpp_handle,
1561 session_index, tx_fifo);
1563 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
1564 "wrote %d bytes tx-fifo: (%p)", getpid (),
1565 s->vpp_handle, session_index, n_write, tx_fifo);
1573 static vcl_session_t *
1574 vcl_ct_session_get_from_fifo (svm_fifo_t * f, u8 type)
1577 s = vcl_session_get (f->client_session_index);
1581 if (type == 0 && s->rx_fifo == f)
1584 if (type == 1 && s->tx_fifo == f)
1587 s = vcl_session_get (f->master_session_index);
1590 if (type == 0 && s->rx_fifo == f)
1592 if (type == 1 && s->tx_fifo == f)
1599 vppcom_session_write_ready (vcl_session_t * session, u32 session_index)
1601 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
1602 if (PREDICT_FALSE (session->is_vep))
1604 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
1605 "cannot write to an epoll session!",
1606 getpid (), session->vpp_handle, session_index);
1607 return VPPCOM_EBADFD;
1610 if (PREDICT_FALSE (session->session_state & STATE_LISTEN))
1612 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
1613 "cannot write to a listen session!",
1614 getpid (), session->vpp_handle, session_index);
1615 return VPPCOM_EBADFD;
1618 if (PREDICT_FALSE (!(session->session_state & STATE_OPEN)))
1620 session_state_t state = session->session_state;
1623 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
1624 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
1625 "session is not open! state 0x%x (%s), "
1626 "returning %d (%s)", getpid (), session->vpp_handle,
1628 state, vppcom_session_state_str (state),
1629 rv, vppcom_retval_str (rv));
1633 VDBG (3, "VCL<%d>: vpp handle 0x%llx, sid %u: peek %s (%p), ready = %d",
1634 getpid (), session->vpp_handle, session_index, session->tx_fifo,
1635 svm_fifo_max_enqueue (session->tx_fifo));
1637 return svm_fifo_max_enqueue (session->tx_fifo);
1641 vcl_mq_dequeue_batch (svm_msg_q_t * mq)
1643 svm_msg_q_msg_t *msg;
1647 n_msgs = svm_msg_q_size (mq);
1648 for (i = 0; i < n_msgs; i++)
1650 vec_add2 (vcm->mq_msg_vector, msg, 1);
1651 svm_msg_q_sub_w_lock (mq, msg);
1657 vcl_select_handle_mq (svm_msg_q_t * mq, unsigned long n_bits,
1658 unsigned long *read_map, unsigned long *write_map,
1659 unsigned long *except_map, double time_to_wait,
1662 session_disconnected_msg_t *disconnected_msg;
1663 session_connected_msg_t *connected_msg;
1664 session_accepted_msg_t *accepted_msg;
1665 vcl_session_msg_t *vcl_msg;
1666 vcl_session_t *session;
1667 svm_msg_q_msg_t *msg;
1672 svm_msg_q_lock (mq);
1673 if (svm_msg_q_is_empty (mq))
1677 svm_msg_q_unlock (mq);
1683 svm_msg_q_unlock (mq);
1686 else if (time_to_wait < 0)
1688 svm_msg_q_wait (mq);
1692 if (svm_msg_q_timedwait (mq, time_to_wait))
1694 svm_msg_q_unlock (mq);
1699 vcl_mq_dequeue_batch (mq);
1700 svm_msg_q_unlock (mq);
1702 for (i = 0; i < vec_len (vcm->mq_msg_vector); i++)
1704 msg = vec_elt_at_index (vcm->mq_msg_vector, i);
1705 e = svm_msg_q_msg_data (mq, msg);
1706 switch (e->event_type)
1708 case FIFO_EVENT_APP_RX:
1709 sid = e->fifo->client_session_index;
1710 session = vcl_session_get (sid);
1711 if (sid < n_bits && read_map)
1713 clib_bitmap_set_no_check (read_map, sid, 1);
1717 case FIFO_EVENT_APP_TX:
1718 sid = e->fifo->client_session_index;
1719 session = vcl_session_get (sid);
1722 if (sid < n_bits && write_map)
1724 clib_bitmap_set_no_check (write_map, sid, 1);
1728 case SESSION_IO_EVT_CT_TX:
1729 session = vcl_ct_session_get_from_fifo (e->fifo, 0);
1730 sid = vcl_session_index (session);
1731 if (sid < n_bits && read_map)
1733 clib_bitmap_set_no_check (read_map, sid, 1);
1738 case SESSION_IO_EVT_CT_RX:
1739 session = vcl_ct_session_get_from_fifo (e->fifo, 1);
1740 sid = vcl_session_index (session);
1743 if (sid < n_bits && write_map)
1745 clib_bitmap_set_no_check (write_map, sid, 1);
1749 case SESSION_CTRL_EVT_ACCEPTED:
1750 accepted_msg = (session_accepted_msg_t *) e->data;
1751 handle = accepted_msg->listener_handle;
1752 session = vppcom_session_table_lookup_listener (handle);
1755 clib_warning ("VCL<%d>: ERROR: couldn't find listen session:"
1756 "listener handle %llx", getpid (), handle);
1760 clib_fifo_add2 (session->accept_evts_fifo, vcl_msg);
1761 vcl_msg->accepted_msg = *accepted_msg;
1762 sid = session - vcm->sessions;
1763 if (sid < n_bits && read_map)
1765 clib_bitmap_set_no_check (read_map, sid, 1);
1769 case SESSION_CTRL_EVT_CONNECTED:
1770 connected_msg = (session_connected_msg_t *) e->data;
1771 vcl_session_connected_handler (connected_msg);
1773 case SESSION_CTRL_EVT_DISCONNECTED:
1774 disconnected_msg = (session_disconnected_msg_t *) e->data;
1775 sid = vcl_session_get_index_from_handle (disconnected_msg->handle);
1776 if (sid < n_bits && except_map)
1778 clib_bitmap_set_no_check (except_map, sid, 1);
1782 case SESSION_CTRL_EVT_RESET:
1783 sid = vcl_reset_handler ((session_reset_msg_t *) e->data);
1784 if (sid < n_bits && except_map)
1786 clib_bitmap_set_no_check (except_map, sid, 1);
1791 clib_warning ("unhandled: %u", e->event_type);
1794 svm_msg_q_free_msg (mq, msg);
1797 vec_reset_length (vcm->mq_msg_vector);
1802 vppcom_select_condvar (unsigned long n_bits, unsigned long *read_map,
1803 unsigned long *write_map, unsigned long *except_map,
1804 double time_to_wait, u32 * bits_set)
1806 double total_wait = 0, wait_slice;
1807 vcl_cut_through_registration_t *cr;
1809 time_to_wait = (time_to_wait == -1) ? 10e9 : time_to_wait;
1810 wait_slice = vcm->cut_through_registrations ? 10e-6 : time_to_wait;
1814 pool_foreach (cr, vcm->cut_through_registrations, ({
1815 vcl_select_handle_mq (cr->mq, n_bits, read_map, write_map, except_map,
1820 vcl_select_handle_mq (vcm->app_event_queue, n_bits, read_map, write_map,
1821 except_map, time_to_wait, bits_set);
1822 total_wait += wait_slice;
1826 while (total_wait < time_to_wait);
1832 vppcom_select_eventfd (unsigned long n_bits, unsigned long *read_map,
1833 unsigned long *write_map, unsigned long *except_map,
1834 double time_to_wait, u32 * bits_set)
1836 vcl_mq_evt_conn_t *mqc;
1837 int __clib_unused n_read;
1841 vec_validate (vcm->mq_events, pool_elts (vcm->mq_evt_conns));
1842 n_mq_evts = epoll_wait (vcm->mqs_epfd, vcm->mq_events,
1843 vec_len (vcm->mq_events), time_to_wait);
1844 for (i = 0; i < n_mq_evts; i++)
1846 mqc = vcl_mq_evt_conn_get (vcm->mq_events[i].data.u32);
1847 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
1848 vcl_select_handle_mq (mqc->mq, n_bits, read_map, write_map,
1849 except_map, 0, bits_set);
1852 return (n_mq_evts > 0 ? (int) *bits_set : 0);
1856 vppcom_select (unsigned long n_bits, unsigned long *read_map,
1857 unsigned long *write_map, unsigned long *except_map,
1858 double time_to_wait)
1860 u32 sid, minbits = clib_max (n_bits, BITS (uword)), bits_set = 0;
1861 vcl_session_t *session = 0;
1864 ASSERT (sizeof (clib_bitmap_t) == sizeof (long int));
1866 if (n_bits && read_map)
1868 clib_bitmap_validate (vcm->rd_bitmap, minbits);
1869 clib_memcpy (vcm->rd_bitmap, read_map,
1870 vec_len (vcm->rd_bitmap) * sizeof (clib_bitmap_t));
1871 memset (read_map, 0, vec_len (vcm->rd_bitmap) * sizeof (clib_bitmap_t));
1873 if (n_bits && write_map)
1875 clib_bitmap_validate (vcm->wr_bitmap, minbits);
1876 clib_memcpy (vcm->wr_bitmap, write_map,
1877 vec_len (vcm->wr_bitmap) * sizeof (clib_bitmap_t));
1878 memset (write_map, 0,
1879 vec_len (vcm->wr_bitmap) * sizeof (clib_bitmap_t));
1881 if (n_bits && except_map)
1883 clib_bitmap_validate (vcm->ex_bitmap, minbits);
1884 clib_memcpy (vcm->ex_bitmap, except_map,
1885 vec_len (vcm->ex_bitmap) * sizeof (clib_bitmap_t));
1886 memset (except_map, 0,
1887 vec_len (vcm->ex_bitmap) * sizeof (clib_bitmap_t));
1897 clib_bitmap_foreach (sid, vcm->wr_bitmap, ({
1898 if (!(session = vcl_session_get (sid)))
1900 VDBG (0, "VCL<%d>: session %d specified in write_map is closed.",
1902 return VPPCOM_EBADFD;
1905 rv = svm_fifo_is_full (session->tx_fifo);
1908 clib_bitmap_set_no_check (write_map, sid, 1);
1917 clib_bitmap_foreach (sid, vcm->rd_bitmap, ({
1918 if (!(session = vcl_session_get (sid)))
1920 VDBG (0, "VCL<%d>: session %d specified in write_map is closed.",
1922 return VPPCOM_EBADFD;
1925 rv = vppcom_session_read_ready (session);
1928 clib_bitmap_set_no_check (read_map, sid, 1);
1936 if (vcm->cfg.use_mq_eventfd)
1937 vppcom_select_eventfd (n_bits, read_map, write_map, except_map,
1938 time_to_wait, &bits_set);
1940 vppcom_select_condvar (n_bits, read_map, write_map, except_map,
1941 time_to_wait, &bits_set);
1947 vep_verify_epoll_chain (u32 vep_idx)
1949 vcl_session_t *session;
1950 vppcom_epoll_t *vep;
1954 if (VPPCOM_DEBUG <= 1)
1957 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
1958 rv = vppcom_session_at_index (vep_idx, &session);
1959 if (PREDICT_FALSE (rv))
1961 clib_warning ("VCL<%d>: ERROR: Invalid vep_idx (%u)!",
1962 getpid (), vep_idx);
1965 if (PREDICT_FALSE (!session->is_vep))
1967 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
1968 getpid (), vep_idx);
1971 vep = &session->vep;
1972 clib_warning ("VCL<%d>: vep_idx (%u): Dumping epoll chain\n"
1975 " is_vep_session = %u\n"
1976 " next_sid = 0x%x (%u)\n"
1977 " wait_cont_idx = 0x%x (%u)\n"
1978 "}\n", getpid (), vep_idx,
1979 session->is_vep, session->is_vep_session,
1980 vep->next_sid, vep->next_sid,
1981 session->wait_cont_idx, session->wait_cont_idx);
1983 for (sid = vep->next_sid; sid != ~0; sid = vep->next_sid)
1985 rv = vppcom_session_at_index (sid, &session);
1986 if (PREDICT_FALSE (rv))
1988 clib_warning ("VCL<%d>: ERROR: Invalid sid (%u)!", getpid (), sid);
1991 if (PREDICT_FALSE (session->is_vep))
1992 clib_warning ("VCL<%d>: ERROR: sid (%u) is a vep!",
1993 getpid (), vep_idx);
1994 else if (PREDICT_FALSE (!session->is_vep_session))
1996 clib_warning ("VCL<%d>: ERROR: session (%u) "
1997 "is not a vep session!", getpid (), sid);
2000 vep = &session->vep;
2001 if (PREDICT_FALSE (vep->vep_idx != vep_idx))
2002 clib_warning ("VCL<%d>: ERROR: session (%u) vep_idx (%u) != "
2003 "vep_idx (%u)!", getpid (),
2004 sid, session->vep.vep_idx, vep_idx);
2005 if (session->is_vep_session)
2007 clib_warning ("vep_idx[%u]: sid 0x%x (%u)\n"
2009 " next_sid = 0x%x (%u)\n"
2010 " prev_sid = 0x%x (%u)\n"
2011 " vep_idx = 0x%x (%u)\n"
2012 " ev.events = 0x%x\n"
2013 " ev.data.u64 = 0x%llx\n"
2017 vep->next_sid, vep->next_sid,
2018 vep->prev_sid, vep->prev_sid,
2019 vep->vep_idx, vep->vep_idx,
2020 vep->ev.events, vep->ev.data.u64, vep->et_mask);
2025 clib_warning ("VCL<%d>: vep_idx (%u): Dump complete!\n",
2026 getpid (), vep_idx);
2030 vppcom_epoll_create (void)
2032 vcl_session_t *vep_session;
2035 VCL_SESSION_LOCK ();
2036 pool_get (vcm->sessions, vep_session);
2037 memset (vep_session, 0, sizeof (*vep_session));
2038 vep_idx = vep_session - vcm->sessions;
2040 vep_session->is_vep = 1;
2041 vep_session->vep.vep_idx = ~0;
2042 vep_session->vep.next_sid = ~0;
2043 vep_session->vep.prev_sid = ~0;
2044 vep_session->wait_cont_idx = ~0;
2045 vep_session->vpp_handle = ~0;
2046 vep_session->poll_reg = 0;
2048 vcl_evt (VCL_EVT_EPOLL_CREATE, vep_session, vep_idx);
2049 VCL_SESSION_UNLOCK ();
2051 VDBG (0, "VCL<%d>: Created vep_idx %u / sid %u!",
2052 getpid (), vep_idx, vep_idx);
2058 vppcom_epoll_ctl (uint32_t vep_idx, int op, uint32_t session_index,
2059 struct epoll_event *event)
2061 vcl_session_t *vep_session;
2062 vcl_session_t *session;
2065 if (vep_idx == session_index)
2067 clib_warning ("VCL<%d>: ERROR: vep_idx == session_index (%u)!",
2068 getpid (), vep_idx);
2069 return VPPCOM_EINVAL;
2072 VCL_SESSION_LOCK ();
2073 rv = vppcom_session_at_index (vep_idx, &vep_session);
2074 if (PREDICT_FALSE (rv))
2076 clib_warning ("VCL<%d>: ERROR: Invalid vep_idx (%u)!", vep_idx);
2079 if (PREDICT_FALSE (!vep_session->is_vep))
2081 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
2082 getpid (), vep_idx);
2087 ASSERT (vep_session->vep.vep_idx == ~0);
2088 ASSERT (vep_session->vep.prev_sid == ~0);
2090 rv = vppcom_session_at_index (session_index, &session);
2091 if (PREDICT_FALSE (rv))
2093 VDBG (0, "VCL<%d>: ERROR: Invalid session_index (%u)!",
2094 getpid (), session_index);
2097 if (PREDICT_FALSE (session->is_vep))
2099 clib_warning ("ERROR: session_index (%u) is a vep!", vep_idx);
2107 if (PREDICT_FALSE (!event))
2109 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_ADD: NULL pointer to "
2110 "epoll_event structure!", getpid ());
2114 if (vep_session->vep.next_sid != ~0)
2116 vcl_session_t *next_session;
2117 rv = vppcom_session_at_index (vep_session->vep.next_sid,
2119 if (PREDICT_FALSE (rv))
2121 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_ADD: Invalid "
2122 "vep.next_sid (%u) on vep_idx (%u)!",
2123 getpid (), vep_session->vep.next_sid, vep_idx);
2126 ASSERT (next_session->vep.prev_sid == vep_idx);
2127 next_session->vep.prev_sid = session_index;
2129 session->vep.next_sid = vep_session->vep.next_sid;
2130 session->vep.prev_sid = vep_idx;
2131 session->vep.vep_idx = vep_idx;
2132 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
2133 session->vep.ev = *event;
2134 session->is_vep = 0;
2135 session->is_vep_session = 1;
2136 vep_session->vep.next_sid = session_index;
2138 /* VCL Event Register handler */
2139 if (session->session_state & STATE_LISTEN)
2141 /* Register handler for connect_request event on listen_session_index */
2142 vce_event_key_t evk;
2143 evk.session_index = session_index;
2144 evk.eid = VCL_EVENT_CONNECT_REQ_ACCEPTED;
2145 vep_session->poll_reg =
2146 vce_register_handler (&vcm->event_thread, &evk,
2147 vce_poll_wait_connect_request_handler_fn,
2148 0 /* No callback args */ );
2150 VDBG (1, "VCL<%d>: EPOLL_CTL_ADD: vep_idx %u, "
2151 "sid %u, events 0x%x, data 0x%llx!",
2152 getpid (), vep_idx, session_index,
2153 event->events, event->data.u64);
2154 vcl_evt (VCL_EVT_EPOLL_CTLADD, session, event->events, event->data.u64);
2158 if (PREDICT_FALSE (!event))
2160 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_MOD: NULL pointer to "
2161 "epoll_event structure!", getpid ());
2165 else if (PREDICT_FALSE (!session->is_vep_session))
2167 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_MOD: "
2168 "not a vep session!", getpid (), session_index);
2172 else if (PREDICT_FALSE (session->vep.vep_idx != vep_idx))
2174 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_MOD: "
2175 "vep_idx (%u) != vep_idx (%u)!",
2176 getpid (), session_index,
2177 session->vep.vep_idx, vep_idx);
2181 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
2182 session->vep.ev = *event;
2183 VDBG (1, "VCL<%d>: EPOLL_CTL_MOD: vep_idx %u, sid %u, events 0x%x,"
2184 " data 0x%llx!", getpid (), vep_idx, session_index, event->events,
2189 if (PREDICT_FALSE (!session->is_vep_session))
2191 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_DEL: "
2192 "not a vep session!", getpid (), session_index);
2196 else if (PREDICT_FALSE (session->vep.vep_idx != vep_idx))
2198 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_DEL: "
2199 "vep_idx (%u) != vep_idx (%u)!",
2200 getpid (), session_index,
2201 session->vep.vep_idx, vep_idx);
2206 /* VCL Event Un-register handler */
2207 if ((session->session_state & STATE_LISTEN) && vep_session->poll_reg)
2209 (void) vce_unregister_handler (&vcm->event_thread,
2210 vep_session->poll_reg);
2213 vep_session->wait_cont_idx =
2214 (vep_session->wait_cont_idx == session_index) ?
2215 session->vep.next_sid : vep_session->wait_cont_idx;
2217 if (session->vep.prev_sid == vep_idx)
2218 vep_session->vep.next_sid = session->vep.next_sid;
2221 vcl_session_t *prev_session;
2222 rv = vppcom_session_at_index (session->vep.prev_sid, &prev_session);
2223 if (PREDICT_FALSE (rv))
2225 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_DEL: Invalid "
2226 "vep.prev_sid (%u) on sid (%u)!",
2227 getpid (), session->vep.prev_sid, session_index);
2230 ASSERT (prev_session->vep.next_sid == session_index);
2231 prev_session->vep.next_sid = session->vep.next_sid;
2233 if (session->vep.next_sid != ~0)
2235 vcl_session_t *next_session;
2236 rv = vppcom_session_at_index (session->vep.next_sid, &next_session);
2237 if (PREDICT_FALSE (rv))
2239 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_DEL: Invalid "
2240 "vep.next_sid (%u) on sid (%u)!",
2241 getpid (), session->vep.next_sid, session_index);
2244 ASSERT (next_session->vep.prev_sid == session_index);
2245 next_session->vep.prev_sid = session->vep.prev_sid;
2248 memset (&session->vep, 0, sizeof (session->vep));
2249 session->vep.next_sid = ~0;
2250 session->vep.prev_sid = ~0;
2251 session->vep.vep_idx = ~0;
2252 session->is_vep_session = 0;
2253 VDBG (1, "VCL<%d>: EPOLL_CTL_DEL: vep_idx %u, sid %u!",
2254 getpid (), vep_idx, session_index);
2255 vcl_evt (VCL_EVT_EPOLL_CTLDEL, session, vep_idx);
2259 clib_warning ("VCL<%d>: ERROR: Invalid operation (%d)!", getpid (), op);
2263 vep_verify_epoll_chain (vep_idx);
2266 VCL_SESSION_UNLOCK ();
2271 vcl_epoll_wait_handle_mq (svm_msg_q_t * mq, struct epoll_event *events,
2272 u32 maxevents, double wait_for_time, u32 * num_ev)
2274 session_disconnected_msg_t *disconnected_msg;
2275 session_connected_msg_t *connected_msg;
2276 session_accepted_msg_t *accepted_msg;
2277 u64 session_evt_data = ~0, handle;
2278 u32 sid = ~0, session_events;
2279 vcl_session_msg_t *vcl_msg;
2280 vcl_session_t *session;
2281 svm_msg_q_msg_t *msg;
2286 svm_msg_q_lock (mq);
2287 if (svm_msg_q_is_empty (mq))
2291 svm_msg_q_unlock (mq);
2294 else if (wait_for_time < 0)
2296 svm_msg_q_wait (mq);
2300 if (svm_msg_q_timedwait (mq, wait_for_time / 1e3))
2302 svm_msg_q_unlock (mq);
2307 vcl_mq_dequeue_batch (mq);
2308 svm_msg_q_unlock (mq);
2310 for (i = 0; i < vec_len (vcm->mq_msg_vector); i++)
2312 msg = vec_elt_at_index (vcm->mq_msg_vector, i);
2313 e = svm_msg_q_msg_data (mq, msg);
2315 switch (e->event_type)
2317 case FIFO_EVENT_APP_RX:
2318 sid = e->fifo->client_session_index;
2319 session = vcl_session_get (sid);
2320 session_events = session->vep.ev.events;
2321 if (!(EPOLLIN & session->vep.ev.events))
2324 events[*num_ev].events |= EPOLLIN;
2325 session_evt_data = session->vep.ev.data.u64;
2327 case FIFO_EVENT_APP_TX:
2328 sid = e->fifo->client_session_index;
2329 session = vcl_session_get (sid);
2330 session_events = session->vep.ev.events;
2331 if (!(EPOLLOUT & session_events))
2334 events[*num_ev].events |= EPOLLOUT;
2335 session_evt_data = session->vep.ev.data.u64;
2337 case SESSION_IO_EVT_CT_TX:
2338 session = vcl_ct_session_get_from_fifo (e->fifo, 0);
2339 sid = vcl_session_index (session);
2340 session_events = session->vep.ev.events;
2341 if (!(EPOLLIN & session->vep.ev.events))
2344 events[*num_ev].events |= EPOLLIN;
2345 session_evt_data = session->vep.ev.data.u64;
2347 case SESSION_IO_EVT_CT_RX:
2348 session = vcl_ct_session_get_from_fifo (e->fifo, 1);
2349 sid = vcl_session_index (session);
2350 session_events = session->vep.ev.events;
2351 if (!(EPOLLOUT & session_events))
2354 events[*num_ev].events |= EPOLLOUT;
2355 session_evt_data = session->vep.ev.data.u64;
2357 case SESSION_CTRL_EVT_ACCEPTED:
2358 accepted_msg = (session_accepted_msg_t *) e->data;
2359 handle = accepted_msg->listener_handle;
2360 session = vppcom_session_table_lookup_listener (handle);
2363 clib_warning ("VCL<%d>: ERROR: couldn't find listen session:"
2364 "listener handle %llx", getpid (), handle);
2368 clib_fifo_add2 (session->accept_evts_fifo, vcl_msg);
2369 vcl_msg->accepted_msg = *accepted_msg;
2370 session_events = session->vep.ev.events;
2371 if (!(EPOLLIN & session_events))
2375 events[*num_ev].events |= EPOLLIN;
2376 session_evt_data = session->vep.ev.data.u64;
2378 case SESSION_CTRL_EVT_CONNECTED:
2379 connected_msg = (session_connected_msg_t *) e->data;
2380 vcl_session_connected_handler (connected_msg);
2381 /* Generate EPOLLOUT because there's no connected event */
2382 sid = vcl_session_get_index_from_handle (connected_msg->handle);
2383 clib_spinlock_lock (&vcm->sessions_lockp);
2384 session = vcl_session_get (sid);
2385 session_events = session->vep.ev.events;
2386 if (EPOLLOUT & session_events)
2389 events[*num_ev].events |= EPOLLOUT;
2390 session_evt_data = session->vep.ev.data.u64;
2392 clib_spinlock_unlock (&vcm->sessions_lockp);
2394 case SESSION_CTRL_EVT_DISCONNECTED:
2395 disconnected_msg = (session_disconnected_msg_t *) e->data;
2396 sid = vcl_session_get_index_from_handle (disconnected_msg->handle);
2397 clib_spinlock_lock (&vcm->sessions_lockp);
2398 if (!(session = vcl_session_get (sid)))
2401 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
2402 session_evt_data = session->vep.ev.data.u64;
2403 session_events = session->vep.ev.events;
2404 clib_spinlock_unlock (&vcm->sessions_lockp);
2406 case SESSION_CTRL_EVT_RESET:
2407 sid = vcl_reset_handler ((session_reset_msg_t *) e->data);
2408 if (!(session = vcl_session_get (sid)))
2411 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
2412 session_evt_data = session->vep.ev.data.u64;
2413 session_events = session->vep.ev.events;
2416 clib_warning ("unhandled: %u", e->event_type);
2417 svm_msg_q_free_msg (mq, msg);
2420 svm_msg_q_free_msg (mq, msg);
2424 events[*num_ev].data.u64 = session_evt_data;
2425 if (EPOLLONESHOT & session_events)
2427 clib_spinlock_lock (&vcm->sessions_lockp);
2428 session = vcl_session_get (sid);
2429 session->vep.ev.events = 0;
2430 clib_spinlock_unlock (&vcm->sessions_lockp);
2433 if (*num_ev == maxevents)
2438 vec_reset_length (vcm->mq_msg_vector);
2443 vppcom_epoll_wait_condvar (struct epoll_event *events, int maxevents,
2444 double wait_for_time)
2446 vcl_cut_through_registration_t *cr;
2447 double total_wait = 0, wait_slice;
2451 wait_for_time = (wait_for_time == -1) ? (double) 10e9 : wait_for_time;
2452 wait_slice = vcm->cut_through_registrations ? 10e-6 : wait_for_time;
2457 pool_foreach (cr, vcm->cut_through_registrations, ({
2458 vcl_epoll_wait_handle_mq (cr->mq, events, maxevents, 0, &num_ev);
2462 rv = vcl_epoll_wait_handle_mq (vcm->app_event_queue, events, maxevents,
2463 num_ev ? 0 : wait_slice, &num_ev);
2465 total_wait += wait_slice;
2469 while (total_wait < wait_for_time);
2470 return (int) num_ev;
2474 vppcom_epoll_wait_eventfd (struct epoll_event *events, int maxevents,
2475 double wait_for_time)
2477 vcl_mq_evt_conn_t *mqc;
2478 int __clib_unused n_read;
2483 vec_validate (vcm->mq_events, pool_elts (vcm->mq_evt_conns));
2484 n_mq_evts = epoll_wait (vcm->mqs_epfd, vcm->mq_events,
2485 vec_len (vcm->mq_events), wait_for_time);
2486 for (i = 0; i < n_mq_evts; i++)
2488 mqc = vcl_mq_evt_conn_get (vcm->mq_events[i].data.u32);
2489 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
2490 vcl_epoll_wait_handle_mq (mqc->mq, events, maxevents, 0, &n_evts);
2493 return (int) n_evts;
2497 vppcom_epoll_wait (uint32_t vep_idx, struct epoll_event *events,
2498 int maxevents, double wait_for_time)
2500 vcl_session_t *vep_session;
2502 if (PREDICT_FALSE (maxevents <= 0))
2504 clib_warning ("VCL<%d>: ERROR: Invalid maxevents (%d)!",
2505 getpid (), maxevents);
2506 return VPPCOM_EINVAL;
2509 clib_spinlock_lock (&vcm->sessions_lockp);
2510 vep_session = vcl_session_get (vep_idx);
2511 if (PREDICT_FALSE (!vep_session->is_vep))
2513 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
2514 getpid (), vep_idx);
2515 clib_spinlock_unlock (&vcm->sessions_lockp);
2516 return VPPCOM_EINVAL;
2518 clib_spinlock_unlock (&vcm->sessions_lockp);
2520 memset (events, 0, sizeof (*events) * maxevents);
2522 if (vcm->cfg.use_mq_eventfd)
2523 return vppcom_epoll_wait_eventfd (events, maxevents, wait_for_time);
2525 return vppcom_epoll_wait_condvar (events, maxevents, wait_for_time);
2529 vppcom_session_attr (uint32_t session_index, uint32_t op,
2530 void *buffer, uint32_t * buflen)
2532 vcl_session_t *session;
2534 u32 *flags = buffer;
2535 vppcom_endpt_t *ep = buffer;
2537 VCL_SESSION_LOCK_AND_GET (session_index, &session);
2543 case VPPCOM_ATTR_GET_NREAD:
2544 rv = vppcom_session_read_ready (session);
2545 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_NREAD: sid %u, nread = %d",
2549 case VPPCOM_ATTR_GET_NWRITE:
2550 rv = vppcom_session_write_ready (session, session_index);
2551 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_NWRITE: sid %u, nwrite = %d",
2552 getpid (), session_index, rv);
2555 case VPPCOM_ATTR_GET_FLAGS:
2556 if (PREDICT_TRUE (buffer && buflen && (*buflen >= sizeof (*flags))))
2558 *flags = O_RDWR | (VCL_SESS_ATTR_TEST (session->attr,
2559 VCL_SESS_ATTR_NONBLOCK));
2560 *buflen = sizeof (*flags);
2561 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_FLAGS: sid %u, flags = 0x%08x, "
2562 "is_nonblocking = %u", getpid (),
2563 session_index, *flags,
2564 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK));
2570 case VPPCOM_ATTR_SET_FLAGS:
2571 if (PREDICT_TRUE (buffer && buflen && (*buflen == sizeof (*flags))))
2573 if (*flags & O_NONBLOCK)
2574 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK);
2576 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_NONBLOCK);
2578 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_FLAGS: sid %u, flags = 0x%08x,"
2579 " is_nonblocking = %u",
2580 getpid (), session_index, *flags,
2581 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK));
2587 case VPPCOM_ATTR_GET_PEER_ADDR:
2588 if (PREDICT_TRUE (buffer && buflen &&
2589 (*buflen >= sizeof (*ep)) && ep->ip))
2591 ep->is_ip4 = session->transport.is_ip4;
2592 ep->port = session->transport.rmt_port;
2593 if (session->transport.is_ip4)
2594 clib_memcpy (ep->ip, &session->transport.rmt_ip.ip4,
2595 sizeof (ip4_address_t));
2597 clib_memcpy (ep->ip, &session->transport.rmt_ip.ip6,
2598 sizeof (ip6_address_t));
2599 *buflen = sizeof (*ep);
2600 VDBG (1, "VCL<%d>: VPPCOM_ATTR_GET_PEER_ADDR: sid %u, is_ip4 = %u, "
2601 "addr = %U, port %u", getpid (),
2602 session_index, ep->is_ip4, format_ip46_address,
2603 &session->transport.rmt_ip,
2604 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
2605 clib_net_to_host_u16 (ep->port));
2611 case VPPCOM_ATTR_GET_LCL_ADDR:
2612 if (PREDICT_TRUE (buffer && buflen &&
2613 (*buflen >= sizeof (*ep)) && ep->ip))
2615 ep->is_ip4 = session->transport.is_ip4;
2616 ep->port = session->transport.lcl_port;
2617 if (session->transport.is_ip4)
2618 clib_memcpy (ep->ip, &session->transport.lcl_ip.ip4,
2619 sizeof (ip4_address_t));
2621 clib_memcpy (ep->ip, &session->transport.lcl_ip.ip6,
2622 sizeof (ip6_address_t));
2623 *buflen = sizeof (*ep);
2624 VDBG (1, "VCL<%d>: VPPCOM_ATTR_GET_LCL_ADDR: sid %u, is_ip4 = %u,"
2625 " addr = %U port %d", getpid (),
2626 session_index, ep->is_ip4, format_ip46_address,
2627 &session->transport.lcl_ip,
2628 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
2629 clib_net_to_host_u16 (ep->port));
2635 case VPPCOM_ATTR_GET_LIBC_EPFD:
2636 rv = session->libc_epfd;
2637 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd %d",
2641 case VPPCOM_ATTR_SET_LIBC_EPFD:
2642 if (PREDICT_TRUE (buffer && buflen &&
2643 (*buflen == sizeof (session->libc_epfd))))
2645 session->libc_epfd = *(int *) buffer;
2646 *buflen = sizeof (session->libc_epfd);
2648 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd %d, "
2649 "buflen %d", getpid (), session->libc_epfd, *buflen);
2655 case VPPCOM_ATTR_GET_PROTOCOL:
2656 if (buffer && buflen && (*buflen >= sizeof (int)))
2658 *(int *) buffer = session->session_type;
2659 *buflen = sizeof (int);
2661 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_PROTOCOL: %d (%s), buflen %d",
2662 getpid (), *(int *) buffer, *(int *) buffer ? "UDP" : "TCP",
2669 case VPPCOM_ATTR_GET_LISTEN:
2670 if (buffer && buflen && (*buflen >= sizeof (int)))
2672 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2673 VCL_SESS_ATTR_LISTEN);
2674 *buflen = sizeof (int);
2676 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_LISTEN: %d, buflen %d",
2677 getpid (), *(int *) buffer, *buflen);
2683 case VPPCOM_ATTR_GET_ERROR:
2684 if (buffer && buflen && (*buflen >= sizeof (int)))
2686 *(int *) buffer = 0;
2687 *buflen = sizeof (int);
2689 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_ERROR: %d, buflen %d, #VPP-TBD#",
2690 getpid (), *(int *) buffer, *buflen);
2696 case VPPCOM_ATTR_GET_TX_FIFO_LEN:
2697 if (buffer && buflen && (*buflen >= sizeof (u32)))
2701 *(size_t *) buffer = (session->sndbuf_size ? session->sndbuf_size :
2702 session->tx_fifo ? session->tx_fifo->nitems :
2703 vcm->cfg.tx_fifo_size);
2704 *buflen = sizeof (u32);
2706 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TX_FIFO_LEN: %u (0x%x), "
2707 "buflen %d, #VPP-TBD#", getpid (),
2708 *(size_t *) buffer, *(size_t *) buffer, *buflen);
2714 case VPPCOM_ATTR_SET_TX_FIFO_LEN:
2715 if (buffer && buflen && (*buflen == sizeof (u32)))
2718 session->sndbuf_size = *(u32 *) buffer;
2719 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TX_FIFO_LEN: %u (0x%x), "
2720 "buflen %d, #VPP-TBD#", getpid (),
2721 session->sndbuf_size, session->sndbuf_size, *buflen);
2727 case VPPCOM_ATTR_GET_RX_FIFO_LEN:
2728 if (buffer && buflen && (*buflen >= sizeof (u32)))
2732 *(size_t *) buffer = (session->rcvbuf_size ? session->rcvbuf_size :
2733 session->rx_fifo ? session->rx_fifo->nitems :
2734 vcm->cfg.rx_fifo_size);
2735 *buflen = sizeof (u32);
2737 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_RX_FIFO_LEN: %u (0x%x), "
2738 "buflen %d, #VPP-TBD#", getpid (),
2739 *(size_t *) buffer, *(size_t *) buffer, *buflen);
2745 case VPPCOM_ATTR_SET_RX_FIFO_LEN:
2746 if (buffer && buflen && (*buflen == sizeof (u32)))
2749 session->rcvbuf_size = *(u32 *) buffer;
2750 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_RX_FIFO_LEN: %u (0x%x), "
2751 "buflen %d, #VPP-TBD#", getpid (),
2752 session->sndbuf_size, session->sndbuf_size, *buflen);
2758 case VPPCOM_ATTR_GET_REUSEADDR:
2759 if (buffer && buflen && (*buflen >= sizeof (int)))
2762 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2763 VCL_SESS_ATTR_REUSEADDR);
2764 *buflen = sizeof (int);
2766 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_REUSEADDR: %d, "
2767 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
2773 case VPPCOM_ATTR_SET_REUSEADDR:
2774 if (buffer && buflen && (*buflen == sizeof (int)) &&
2775 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
2778 if (*(int *) buffer)
2779 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEADDR);
2781 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEADDR);
2783 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_REUSEADDR: %d, buflen %d,"
2784 " #VPP-TBD#", getpid (),
2785 VCL_SESS_ATTR_TEST (session->attr,
2786 VCL_SESS_ATTR_REUSEADDR), *buflen);
2792 case VPPCOM_ATTR_GET_REUSEPORT:
2793 if (buffer && buflen && (*buflen >= sizeof (int)))
2796 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2797 VCL_SESS_ATTR_REUSEPORT);
2798 *buflen = sizeof (int);
2800 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_REUSEPORT: %d, buflen %d,"
2801 " #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
2807 case VPPCOM_ATTR_SET_REUSEPORT:
2808 if (buffer && buflen && (*buflen == sizeof (int)) &&
2809 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
2812 if (*(int *) buffer)
2813 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEPORT);
2815 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEPORT);
2817 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_REUSEPORT: %d, buflen %d,"
2818 " #VPP-TBD#", getpid (),
2819 VCL_SESS_ATTR_TEST (session->attr,
2820 VCL_SESS_ATTR_REUSEPORT), *buflen);
2826 case VPPCOM_ATTR_GET_BROADCAST:
2827 if (buffer && buflen && (*buflen >= sizeof (int)))
2830 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2831 VCL_SESS_ATTR_BROADCAST);
2832 *buflen = sizeof (int);
2834 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_BROADCAST: %d, buflen %d,"
2835 " #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
2841 case VPPCOM_ATTR_SET_BROADCAST:
2842 if (buffer && buflen && (*buflen == sizeof (int)))
2845 if (*(int *) buffer)
2846 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_BROADCAST);
2848 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_BROADCAST);
2850 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_BROADCAST: %d, buflen %d, "
2851 "#VPP-TBD#", getpid (),
2852 VCL_SESS_ATTR_TEST (session->attr,
2853 VCL_SESS_ATTR_BROADCAST), *buflen);
2859 case VPPCOM_ATTR_GET_V6ONLY:
2860 if (buffer && buflen && (*buflen >= sizeof (int)))
2863 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2864 VCL_SESS_ATTR_V6ONLY);
2865 *buflen = sizeof (int);
2867 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_V6ONLY: %d, buflen %d, "
2868 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
2874 case VPPCOM_ATTR_SET_V6ONLY:
2875 if (buffer && buflen && (*buflen == sizeof (int)))
2878 if (*(int *) buffer)
2879 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_V6ONLY);
2881 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_V6ONLY);
2883 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_V6ONLY: %d, buflen %d, "
2884 "#VPP-TBD#", getpid (),
2885 VCL_SESS_ATTR_TEST (session->attr,
2886 VCL_SESS_ATTR_V6ONLY), *buflen);
2892 case VPPCOM_ATTR_GET_KEEPALIVE:
2893 if (buffer && buflen && (*buflen >= sizeof (int)))
2896 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2897 VCL_SESS_ATTR_KEEPALIVE);
2898 *buflen = sizeof (int);
2900 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_KEEPALIVE: %d, buflen %d, "
2901 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
2907 case VPPCOM_ATTR_SET_KEEPALIVE:
2908 if (buffer && buflen && (*buflen == sizeof (int)))
2911 if (*(int *) buffer)
2912 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_KEEPALIVE);
2914 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_KEEPALIVE);
2916 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_KEEPALIVE: %d, buflen %d, "
2917 "#VPP-TBD#", getpid (),
2918 VCL_SESS_ATTR_TEST (session->attr,
2919 VCL_SESS_ATTR_KEEPALIVE), *buflen);
2925 case VPPCOM_ATTR_GET_TCP_NODELAY:
2926 if (buffer && buflen && (*buflen >= sizeof (int)))
2929 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2930 VCL_SESS_ATTR_TCP_NODELAY);
2931 *buflen = sizeof (int);
2933 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_NODELAY: %d, buflen %d, "
2934 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
2940 case VPPCOM_ATTR_SET_TCP_NODELAY:
2941 if (buffer && buflen && (*buflen == sizeof (int)))
2944 if (*(int *) buffer)
2945 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
2947 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
2949 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_NODELAY: %d, buflen %d, "
2950 "#VPP-TBD#", getpid (),
2951 VCL_SESS_ATTR_TEST (session->attr,
2952 VCL_SESS_ATTR_TCP_NODELAY), *buflen);
2958 case VPPCOM_ATTR_GET_TCP_KEEPIDLE:
2959 if (buffer && buflen && (*buflen >= sizeof (int)))
2962 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2963 VCL_SESS_ATTR_TCP_KEEPIDLE);
2964 *buflen = sizeof (int);
2966 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d, buflen %d, "
2967 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
2973 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
2974 if (buffer && buflen && (*buflen == sizeof (int)))
2977 if (*(int *) buffer)
2978 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
2980 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
2982 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d, buflen %d, "
2983 "#VPP-TBD#", getpid (),
2984 VCL_SESS_ATTR_TEST (session->attr,
2985 VCL_SESS_ATTR_TCP_KEEPIDLE), *buflen);
2991 case VPPCOM_ATTR_GET_TCP_KEEPINTVL:
2992 if (buffer && buflen && (*buflen >= sizeof (int)))
2995 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2996 VCL_SESS_ATTR_TCP_KEEPINTVL);
2997 *buflen = sizeof (int);
2999 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_KEEPINTVL: %d, buflen %d, "
3000 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
3006 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
3007 if (buffer && buflen && (*buflen == sizeof (int)))
3010 if (*(int *) buffer)
3011 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
3013 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
3015 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d, buflen %d, "
3016 "#VPP-TBD#", getpid (),
3017 VCL_SESS_ATTR_TEST (session->attr,
3018 VCL_SESS_ATTR_TCP_KEEPINTVL), *buflen);
3024 case VPPCOM_ATTR_GET_TCP_USER_MSS:
3025 if (buffer && buflen && (*buflen >= sizeof (u32)))
3028 *(u32 *) buffer = session->user_mss;
3029 *buflen = sizeof (int);
3031 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_USER_MSS: %d, buflen %d,"
3032 " #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
3038 case VPPCOM_ATTR_SET_TCP_USER_MSS:
3039 if (buffer && buflen && (*buflen == sizeof (u32)))
3042 session->user_mss = *(u32 *) buffer;
3044 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_USER_MSS: %u, buflen %d, "
3045 "#VPP-TBD#", getpid (), session->user_mss, *buflen);
3057 VCL_SESSION_UNLOCK ();
3062 vppcom_session_recvfrom (uint32_t session_index, void *buffer,
3063 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3066 vcl_session_t *session = 0;
3070 VCL_SESSION_LOCK ();
3071 rv = vppcom_session_at_index (session_index, &session);
3072 if (PREDICT_FALSE (rv))
3074 VCL_SESSION_UNLOCK ();
3075 VDBG (0, "VCL<%d>: invalid session, sid (%u) has been closed!",
3076 getpid (), session_index);
3077 VCL_SESSION_UNLOCK ();
3078 return VPPCOM_EBADFD;
3080 ep->is_ip4 = session->transport.is_ip4;
3081 ep->port = session->transport.rmt_port;
3082 VCL_SESSION_UNLOCK ();
3086 rv = vppcom_session_read (session_index, buffer, buflen);
3087 else if (flags & MSG_PEEK)
3088 rv = vppcom_session_peek (session_index, buffer, buflen);
3091 clib_warning ("VCL<%d>: Unsupport flags for recvfrom %d",
3093 return VPPCOM_EAFNOSUPPORT;
3098 if (session->transport.is_ip4)
3099 clib_memcpy (ep->ip, &session->transport.rmt_ip.ip4,
3100 sizeof (ip4_address_t));
3102 clib_memcpy (ep->ip, &session->transport.rmt_ip.ip6,
3103 sizeof (ip6_address_t));
3110 vppcom_session_sendto (uint32_t session_index, void *buffer,
3111 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3114 return VPPCOM_EINVAL;
3119 return VPPCOM_EINVAL;
3124 // TBD check the flags and do the right thing
3125 VDBG (2, "VCL<%d>: handling flags 0x%u (%d) not implemented yet.",
3126 getpid (), flags, flags);
3129 return (vppcom_session_write (session_index, buffer, buflen));
3133 vppcom_poll (vcl_poll_t * vp, uint32_t n_sids, double wait_for_time)
3135 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
3136 u32 i, keep_trying = 1;
3139 VDBG (3, "VCL<%d>: vp %p, nsids %u, wait_for_time %f",
3140 getpid (), vp, n_sids, wait_for_time);
3143 return VPPCOM_EFAULT;
3147 vcl_session_t *session;
3149 for (i = 0; i < n_sids; i++)
3151 ASSERT (vp[i].revents);
3153 VCL_SESSION_LOCK_AND_GET (vp[i].sid, &session);
3154 VCL_SESSION_UNLOCK ();
3159 if (POLLIN & vp[i].events)
3161 VCL_SESSION_LOCK_AND_GET (vp[i].sid, &session);
3162 rv = vppcom_session_read_ready (session);
3163 VCL_SESSION_UNLOCK ();
3166 *vp[i].revents |= POLLIN;
3173 case VPPCOM_ECONNRESET:
3174 *vp[i].revents = POLLHUP;
3178 *vp[i].revents = POLLERR;
3185 if (POLLOUT & vp[i].events)
3187 VCL_SESSION_LOCK_AND_GET (vp[i].sid, &session);
3188 rv = vppcom_session_write_ready (session, vp[i].sid);
3189 VCL_SESSION_UNLOCK ();
3192 *vp[i].revents |= POLLOUT;
3199 case VPPCOM_ECONNRESET:
3200 *vp[i].revents = POLLHUP;
3204 *vp[i].revents = POLLERR;
3211 if (0) // Note "done:" label used by VCL_SESSION_LOCK_AND_GET()
3214 *vp[i].revents = POLLNVAL;
3218 if (wait_for_time != -1)
3219 keep_trying = (clib_time_now (&vcm->clib_time) <= timeout) ? 1 : 0;
3221 while ((num_ev == 0) && keep_trying);
3223 if (VPPCOM_DEBUG > 3)
3225 clib_warning ("VCL<%d>: returning %d", getpid (), num_ev);
3226 for (i = 0; i < n_sids; i++)
3228 clib_warning ("VCL<%d>: vp[%d].sid %d (0x%x), .events 0x%x, "
3229 ".revents 0x%x", getpid (), i, vp[i].sid, vp[i].sid,
3230 vp[i].events, *vp[i].revents);
3237 vppcom_mq_epoll_fd (void)
3239 return vcm->mqs_epfd;
3243 * fd.io coding-style-patch-verification: ON
3246 * eval: (c-set-style "gnu")