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_debug.h>
21 #include <vcl/vcl_private.h>
23 __thread uword __vcl_worker_index = ~0;
28 sigsegv_signal (int signum)
34 vcl_wait_for_memory (void *mem)
36 u8 __clib_unused test;
37 if (vcm->mounting_segment)
39 while (vcm->mounting_segment)
48 if (signal (SIGSEGV, sigsegv_signal))
64 signal (SIGSEGV, SIG_DFL);
68 vppcom_session_state_str (session_state_t state)
90 case STATE_CLOSE_ON_EMPTY:
91 st = "STATE_CLOSE_ON_EMPTY";
94 case STATE_DISCONNECT:
95 st = "STATE_DISCONNECT";
103 st = "UNKNOWN_STATE";
111 format_ip4_address (u8 * s, va_list * args)
113 u8 *a = va_arg (*args, u8 *);
114 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
118 format_ip6_address (u8 * s, va_list * args)
120 ip6_address_t *a = va_arg (*args, ip6_address_t *);
121 u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
123 i_max_n_zero = ARRAY_LEN (a->as_u16);
125 i_first_zero = i_max_n_zero;
127 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
129 u32 is_zero = a->as_u16[i] == 0;
130 if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
136 if ((!is_zero && n_zeros > max_n_zeros)
137 || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
139 i_max_n_zero = i_first_zero;
140 max_n_zeros = n_zeros;
141 i_first_zero = ARRAY_LEN (a->as_u16);
146 last_double_colon = 0;
147 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
149 if (i == i_max_n_zero && max_n_zeros > 1)
151 s = format (s, "::");
152 i += max_n_zeros - 1;
153 last_double_colon = 1;
157 s = format (s, "%s%x",
158 (last_double_colon || i == 0) ? "" : ":",
159 clib_net_to_host_u16 (a->as_u16[i]));
160 last_double_colon = 0;
167 /* Format an IP46 address. */
169 format_ip46_address (u8 * s, va_list * args)
171 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
172 ip46_type_t type = va_arg (*args, ip46_type_t);
178 is_ip4 = ip46_address_is_ip4 (ip46);
189 format (s, "%U", format_ip4_address, &ip46->ip4) :
190 format (s, "%U", format_ip6_address, &ip46->ip6);
194 * VPPCOM Utility Functions
199 vcl_session_vpp_evt_q (vcl_worker_t * wrk, vcl_session_t * s)
201 if (vcl_session_is_ct (s))
202 return wrk->vpp_event_queues[0];
204 return wrk->vpp_event_queues[s->tx_fifo->master_thread_index];
208 vcl_send_session_accepted_reply (svm_msg_q_t * mq, u32 context,
209 session_handle_t handle, int retval)
211 app_session_evt_t _app_evt, *app_evt = &_app_evt;
212 session_accepted_reply_msg_t *rmp;
213 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_ACCEPTED_REPLY);
214 rmp = (session_accepted_reply_msg_t *) app_evt->evt->data;
215 rmp->handle = handle;
216 rmp->context = context;
217 rmp->retval = retval;
218 app_send_ctrl_evt_to_vpp (mq, app_evt);
222 vcl_send_session_disconnected_reply (svm_msg_q_t * mq, u32 context,
223 session_handle_t handle, int retval)
225 app_session_evt_t _app_evt, *app_evt = &_app_evt;
226 session_disconnected_reply_msg_t *rmp;
227 app_alloc_ctrl_evt_to_vpp (mq, app_evt,
228 SESSION_CTRL_EVT_DISCONNECTED_REPLY);
229 rmp = (session_disconnected_reply_msg_t *) app_evt->evt->data;
230 rmp->handle = handle;
231 rmp->context = context;
232 rmp->retval = retval;
233 app_send_ctrl_evt_to_vpp (mq, app_evt);
237 vcl_send_session_reset_reply (svm_msg_q_t * mq, u32 context,
238 session_handle_t handle, int retval)
240 app_session_evt_t _app_evt, *app_evt = &_app_evt;
241 session_reset_reply_msg_t *rmp;
242 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_RESET_REPLY);
243 rmp = (session_reset_reply_msg_t *) app_evt->evt->data;
244 rmp->handle = handle;
245 rmp->context = context;
246 rmp->retval = retval;
247 app_send_ctrl_evt_to_vpp (mq, app_evt);
251 vcl_session_accepted_handler (vcl_worker_t * wrk, session_accepted_msg_t * mp)
253 vcl_session_t *session, *listen_session;
254 svm_fifo_t *rx_fifo, *tx_fifo;
258 session = vcl_session_alloc (wrk);
260 listen_session = vcl_session_table_lookup_listener (wrk,
261 mp->listener_handle);
265 evt_q = uword_to_pointer (mp->vpp_event_queue_address, svm_msg_q_t *);
266 clib_warning ("VCL<%d>: ERROR: couldn't find listen session: "
267 "unknown vpp listener handle %llx",
268 getpid (), mp->listener_handle);
269 vcl_send_session_accepted_reply (evt_q, mp->context, mp->handle,
270 VNET_API_ERROR_INVALID_ARGUMENT);
271 vcl_session_free (wrk, session);
272 return VCL_INVALID_SESSION_INDEX;
275 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
276 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
278 if (mp->server_event_queue_address)
280 session->vpp_evt_q = uword_to_pointer (mp->client_event_queue_address,
282 session->our_evt_q = uword_to_pointer (mp->server_event_queue_address,
284 vcl_wait_for_memory (session->vpp_evt_q);
285 rx_fifo->master_session_index = session->session_index;
286 tx_fifo->master_session_index = session->session_index;
287 rx_fifo->master_thread_index = vcl_get_worker_index ();
288 tx_fifo->master_thread_index = vcl_get_worker_index ();
289 vec_validate (wrk->vpp_event_queues, 0);
290 evt_q = uword_to_pointer (mp->vpp_event_queue_address, svm_msg_q_t *);
291 wrk->vpp_event_queues[0] = evt_q;
295 session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address,
297 rx_fifo->client_session_index = session->session_index;
298 tx_fifo->client_session_index = session->session_index;
299 rx_fifo->client_thread_index = vcl_get_worker_index ();
300 tx_fifo->client_thread_index = vcl_get_worker_index ();
301 vpp_wrk_index = tx_fifo->master_thread_index;
302 vec_validate (wrk->vpp_event_queues, vpp_wrk_index);
303 wrk->vpp_event_queues[vpp_wrk_index] = session->vpp_evt_q;
306 session->vpp_handle = mp->handle;
307 session->client_context = mp->context;
308 session->rx_fifo = rx_fifo;
309 session->tx_fifo = tx_fifo;
311 session->session_state = STATE_ACCEPT;
312 session->transport.rmt_port = mp->port;
313 session->transport.is_ip4 = mp->is_ip4;
314 clib_memcpy_fast (&session->transport.rmt_ip, mp->ip,
315 sizeof (ip46_address_t));
317 vcl_session_table_add_vpp_handle (wrk, mp->handle, session->session_index);
318 session->transport.lcl_port = listen_session->transport.lcl_port;
319 session->transport.lcl_ip = listen_session->transport.lcl_ip;
320 session->session_type = listen_session->session_type;
321 session->is_dgram = session->session_type == VPPCOM_PROTO_UDP;
323 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: client accept request from %s"
324 " address %U port %d queue %p!", getpid (), mp->handle,
325 session->session_index,
326 mp->is_ip4 ? "IPv4" : "IPv6", format_ip46_address, &mp->ip,
327 mp->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
328 clib_net_to_host_u16 (mp->port), session->vpp_evt_q);
329 vcl_evt (VCL_EVT_ACCEPT, session, listen_session, session_index);
331 return session->session_index;
335 vcl_session_connected_handler (vcl_worker_t * wrk,
336 session_connected_msg_t * mp)
338 u32 session_index, vpp_wrk_index;
339 svm_fifo_t *rx_fifo, *tx_fifo;
340 vcl_session_t *session = 0;
343 session_index = mp->context;
344 session = vcl_session_get (wrk, session_index);
347 clib_warning ("[%s] ERROR: vpp handle 0x%llx, sid %u: "
348 "Invalid session index (%u)!",
349 getpid (), mp->handle, session_index);
350 return VCL_INVALID_SESSION_INDEX;
354 clib_warning ("VCL<%d>: ERROR: sid %u: connect failed! %U", getpid (),
355 session_index, format_api_error, ntohl (mp->retval));
356 session->session_state = STATE_FAILED;
357 session->vpp_handle = mp->handle;
358 return session_index;
361 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
362 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
363 vcl_wait_for_memory (rx_fifo);
364 rx_fifo->client_session_index = session_index;
365 tx_fifo->client_session_index = session_index;
366 rx_fifo->client_thread_index = vcl_get_worker_index ();
367 tx_fifo->client_thread_index = vcl_get_worker_index ();
369 if (mp->client_event_queue_address)
371 session->vpp_evt_q = uword_to_pointer (mp->server_event_queue_address,
373 session->our_evt_q = uword_to_pointer (mp->client_event_queue_address,
376 vec_validate (wrk->vpp_event_queues, 0);
377 evt_q = uword_to_pointer (mp->vpp_event_queue_address, svm_msg_q_t *);
378 wrk->vpp_event_queues[0] = evt_q;
382 session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address,
384 vpp_wrk_index = tx_fifo->master_thread_index;
385 vec_validate (wrk->vpp_event_queues, vpp_wrk_index);
386 wrk->vpp_event_queues[vpp_wrk_index] = session->vpp_evt_q;
389 session->rx_fifo = rx_fifo;
390 session->tx_fifo = tx_fifo;
391 session->vpp_handle = mp->handle;
392 session->transport.is_ip4 = mp->is_ip4;
393 clib_memcpy_fast (&session->transport.lcl_ip, mp->lcl_ip,
394 sizeof (session->transport.lcl_ip));
395 session->transport.lcl_port = mp->lcl_port;
396 session->session_state = STATE_CONNECT;
398 /* Add it to lookup table */
399 hash_set (wrk->session_index_by_vpp_handles, mp->handle, session_index);
401 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: connect succeeded! "
402 "session_rx_fifo %p, refcnt %d, session_tx_fifo %p, refcnt %d",
403 getpid (), mp->handle, session_index, session->rx_fifo,
404 session->rx_fifo->refcnt, session->tx_fifo, session->tx_fifo->refcnt);
406 return session_index;
410 vcl_session_reset_handler (vcl_worker_t * wrk,
411 session_reset_msg_t * reset_msg)
413 vcl_session_t *session;
416 sid = vcl_session_index_from_vpp_handle (wrk, reset_msg->handle);
417 session = vcl_session_get (wrk, sid);
420 VDBG (0, "request to reset unknown handle 0x%llx", reset_msg->handle);
421 return VCL_INVALID_SESSION_INDEX;
423 session->session_state = STATE_CLOSE_ON_EMPTY;
424 VDBG (0, "reset handle 0x%llx, sid %u ", reset_msg->handle, sid);
425 vcl_send_session_reset_reply (vcl_session_vpp_evt_q (wrk, session),
426 vcm->my_client_index, reset_msg->handle, 0);
431 vcl_session_bound_handler (vcl_worker_t * wrk, session_bound_msg_t * mp)
433 vcl_session_t *session;
434 u32 sid = mp->context;
436 session = vcl_session_get (wrk, sid);
439 VDBG (0, "VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: bind failed: %U",
440 getpid (), mp->handle, sid, format_api_error, ntohl (mp->retval));
443 session->session_state = STATE_FAILED;
444 session->vpp_handle = mp->handle;
449 clib_warning ("[%s] ERROR: vpp handle 0x%llx, sid %u: "
450 "Invalid session index (%u)!",
451 getpid (), mp->handle, sid);
452 return VCL_INVALID_SESSION_INDEX;
456 session->vpp_handle = mp->handle;
457 session->transport.is_ip4 = mp->lcl_is_ip4;
458 clib_memcpy_fast (&session->transport.lcl_ip, mp->lcl_ip,
459 sizeof (ip46_address_t));
460 session->transport.lcl_port = mp->lcl_port;
461 vcl_session_table_add_listener (wrk, mp->handle, sid);
462 session->session_state = STATE_LISTEN;
464 if (session->is_dgram)
466 svm_fifo_t *rx_fifo, *tx_fifo;
467 session->vpp_evt_q = uword_to_pointer (mp->vpp_evt_q, svm_msg_q_t *);
468 rx_fifo = uword_to_pointer (mp->rx_fifo, svm_fifo_t *);
469 rx_fifo->client_session_index = sid;
470 tx_fifo = uword_to_pointer (mp->tx_fifo, svm_fifo_t *);
471 tx_fifo->client_session_index = sid;
472 session->rx_fifo = rx_fifo;
473 session->tx_fifo = tx_fifo;
476 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: bind succeeded!",
477 getpid (), mp->handle, sid);
482 vcl_handle_mq_event (vcl_worker_t * wrk, session_event_t * e)
484 session_accepted_msg_t *accepted_msg;
485 session_disconnected_msg_t *disconnected_msg;
486 vcl_session_msg_t *vcl_msg;
487 vcl_session_t *session;
491 switch (e->event_type)
493 case FIFO_EVENT_APP_RX:
494 case FIFO_EVENT_APP_TX:
495 case SESSION_IO_EVT_CT_RX:
496 case SESSION_IO_EVT_CT_TX:
497 vec_add1 (wrk->unhandled_evts_vector, *e);
499 case SESSION_CTRL_EVT_ACCEPTED:
500 accepted_msg = (session_accepted_msg_t *) e->data;
501 handle = accepted_msg->listener_handle;
502 session = vcl_session_table_lookup_listener (wrk, handle);
505 clib_warning ("VCL<%d>: ERROR: couldn't find listen session:"
506 "listener handle %llx", getpid (), handle);
510 clib_fifo_add2 (session->accept_evts_fifo, vcl_msg);
511 vcl_msg->accepted_msg = *accepted_msg;
513 case SESSION_CTRL_EVT_CONNECTED:
514 vcl_session_connected_handler (wrk,
515 (session_connected_msg_t *) e->data);
517 case SESSION_CTRL_EVT_DISCONNECTED:
518 disconnected_msg = (session_disconnected_msg_t *) e->data;
519 sid = vcl_session_index_from_vpp_handle (wrk, disconnected_msg->handle);
520 session = vcl_session_get (wrk, sid);
523 VDBG (0, "request to disconnect unknown handle 0x%llx",
524 disconnected_msg->handle);
527 session->session_state = STATE_DISCONNECT;
528 VDBG (0, "disconnected handle 0x%llx, sid %u", disconnected_msg->handle,
531 case SESSION_CTRL_EVT_RESET:
532 vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
534 case SESSION_CTRL_EVT_BOUND:
535 vcl_session_bound_handler (wrk, (session_bound_msg_t *) e->data);
538 clib_warning ("unhandled %u", e->event_type);
544 vppcom_wait_for_session_state_change (u32 session_index,
545 session_state_t state,
548 vcl_worker_t *wrk = vcl_worker_get_current ();
549 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
550 vcl_session_t *volatile session;
556 session = vcl_session_get (wrk, session_index);
557 if (PREDICT_FALSE (!session))
559 return VPPCOM_EBADFD;
561 if (session->session_state & state)
565 if (session->session_state & STATE_FAILED)
567 return VPPCOM_ECONNREFUSED;
570 if (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0))
572 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
573 vcl_handle_mq_event (wrk, e);
574 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
576 while (clib_time_now (&wrk->clib_time) < timeout);
578 VDBG (0, "VCL<%d>: timeout waiting for state 0x%x (%s)", getpid (), state,
579 vppcom_session_state_str (state));
580 vcl_evt (VCL_EVT_SESSION_TIMEOUT, session, session_state);
582 return VPPCOM_ETIMEDOUT;
586 vppcom_app_session_enable (void)
590 if (vcm->app_state != STATE_APP_ENABLED)
592 vppcom_send_session_enable_disable (1 /* is_enabled == TRUE */ );
593 rv = vcl_wait_for_app_state_change (STATE_APP_ENABLED);
594 if (PREDICT_FALSE (rv))
596 VDBG (0, "VCL<%d>: application session enable timed out! "
597 "returning %d (%s)", getpid (), rv, vppcom_retval_str (rv));
605 vppcom_app_attach (void)
609 vppcom_app_send_attach ();
610 rv = vcl_wait_for_app_state_change (STATE_APP_ATTACHED);
611 if (PREDICT_FALSE (rv))
613 VDBG (0, "VCL<%d>: application attach timed out! returning %d (%s)",
614 getpid (), rv, vppcom_retval_str (rv));
622 vppcom_session_unbind (u32 session_handle)
624 vcl_worker_t *wrk = vcl_worker_get_current ();
625 vcl_session_t *session = 0;
628 session = vcl_session_get_w_handle (wrk, session_handle);
630 return VPPCOM_EBADFD;
632 vpp_handle = session->vpp_handle;
633 vcl_session_table_del_listener (wrk, vpp_handle);
634 session->vpp_handle = ~0;
635 session->session_state = STATE_DISCONNECT;
637 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: sending unbind msg! new state"
638 " 0x%x (%s)", getpid (), vpp_handle, session_handle, STATE_DISCONNECT,
639 vppcom_session_state_str (STATE_DISCONNECT));
640 vcl_evt (VCL_EVT_UNBIND, session);
641 vppcom_send_unbind_sock (vpp_handle);
647 vppcom_session_disconnect (u32 session_handle)
649 vcl_worker_t *wrk = vcl_worker_get_current ();
650 svm_msg_q_t *vpp_evt_q;
651 vcl_session_t *session;
652 session_state_t state;
655 session = vcl_session_get_w_handle (wrk, session_handle);
657 return VPPCOM_EBADFD;
659 vpp_handle = session->vpp_handle;
660 state = session->session_state;
662 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u state 0x%x (%s)", getpid (),
663 vpp_handle, session_handle, state, vppcom_session_state_str (state));
665 if (PREDICT_FALSE (state & STATE_LISTEN))
667 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
668 "Cannot disconnect a listen socket!",
669 getpid (), vpp_handle, session_handle);
670 return VPPCOM_EBADFD;
673 if (state & STATE_CLOSE_ON_EMPTY)
675 vpp_evt_q = vcl_session_vpp_evt_q (wrk, session);
676 vcl_send_session_disconnected_reply (vpp_evt_q, vcm->my_client_index,
678 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: sending disconnect "
679 "REPLY...", getpid (), vpp_handle, session_handle);
683 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: sending disconnect...",
684 getpid (), vpp_handle, session_handle);
685 vppcom_send_disconnect_session (vpp_handle);
692 vcl_cleanup_bapi (void)
694 api_main_t *am = &api_main;
696 am->my_client_index = ~0;
697 am->my_registration = 0;
698 am->vl_input_queue = 0;
699 am->msg_index_by_name_and_crc = 0;
701 vl_client_api_unmap ();
705 vcl_app_fork_child_handler (void)
710 vcm->current_pid = getpid ();
711 vcl_set_worker_index (0);
713 VDBG (0, "initializing forked child");
714 child_name = format (0, "%v-child-%u%c", vcm->app_name, getpid (), 0);
717 vppcom_api_hookup ();
718 vcm->app_state = STATE_APP_START;
719 rv = vppcom_connect_to_vpp ((char *) child_name);
720 vec_free (child_name);
723 VERR ("couldn't connect to VPP!");
727 vcm->app_state = STATE_APP_ADDING_WORKER;
728 vcl_send_app_worker_add_del (1 /* is_add */ );
729 if (vcl_wait_for_app_state_change (STATE_APP_READY))
731 VERR ("failed to add worker to vpp");
734 VDBG (0, "forked child main worker initialized");
738 * VPPCOM Public API functions
741 vppcom_app_create (char *app_name)
743 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
749 vppcom_cfg (&vcm->cfg);
752 vcm->main_cpu = pthread_self ();
753 vcm->main_pid = vcm->current_pid = getpid ();
754 vcm->app_name = format (0, "%s", app_name);
755 vppcom_init_error_string_table ();
756 svm_fifo_segment_main_init (vcl_cfg->segment_baseva,
757 20 /* timeout in secs */ );
758 pool_init_fixed (vcm->workers, vcl_cfg->max_workers);
759 clib_spinlock_init (&vcm->workers_lock);
760 vcl_worker_alloc_and_init ();
761 pthread_atfork (NULL, NULL, vcl_app_fork_child_handler);
764 if (vcm->my_client_index == ~0)
766 /* API hookup and connect to VPP */
767 vppcom_api_hookup ();
769 vcm->app_state = STATE_APP_START;
770 rv = vppcom_connect_to_vpp (app_name);
773 VERR ("couldn't connect to VPP!");
776 VDBG (0, "sending session enable");
777 rv = vppcom_app_session_enable ();
780 VERR ("vppcom_app_session_enable() failed!");
784 VDBG (0, "sending app attach");
785 rv = vppcom_app_attach ();
788 VERR ("vppcom_app_attach() failed!");
792 VDBG (0, "app_name '%s', my_client_index %d (0x%x)",
793 app_name, vcm->my_client_index, vcm->my_client_index);
800 vppcom_app_destroy (void)
803 f64 orig_app_timeout;
805 if (vcm->my_client_index == ~0)
808 VDBG (0, "detaching from VPP, my_client_index %d (0x%x)",
809 vcm->my_client_index, vcm->my_client_index);
810 vcl_evt (VCL_EVT_DETACH, vcm);
812 vppcom_app_send_detach ();
813 orig_app_timeout = vcm->cfg.app_timeout;
814 vcm->cfg.app_timeout = 2.0;
815 rv = vcl_wait_for_app_state_change (STATE_APP_ENABLED);
816 vcm->cfg.app_timeout = orig_app_timeout;
817 if (PREDICT_FALSE (rv))
818 VDBG (0, "application detach timed out! returning %d (%s)",
819 rv, vppcom_retval_str (rv));
822 vl_client_disconnect_from_vlib ();
823 vec_free (vcm->app_name);
824 vcm->my_client_index = ~0;
825 vcm->app_state = STATE_APP_START;
829 vppcom_session_create (u8 proto, u8 is_nonblocking)
831 vcl_worker_t *wrk = vcl_worker_get_current ();
832 vcl_session_t *session;
834 session = vcl_session_alloc (wrk);
836 session->session_type = proto;
837 session->session_state = STATE_START;
838 session->vpp_handle = ~0;
839 session->is_dgram = proto == VPPCOM_PROTO_UDP;
842 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK);
844 vcl_evt (VCL_EVT_CREATE, session, session_type, session->session_state,
845 is_nonblocking, session_index);
847 VDBG (0, "created sid %u", session->session_index);
849 return vcl_session_handle (session);
853 vppcom_session_close (uint32_t session_handle)
855 vcl_worker_t *wrk = vcl_worker_get_current ();
856 vcl_session_t *session = 0;
857 u8 is_vep, is_vep_session;
858 session_state_t state;
863 session = vcl_session_get_w_handle (wrk, session_handle);
865 return VPPCOM_EBADFD;
867 is_vep = session->is_vep;
868 is_vep_session = session->is_vep_session;
869 next_sh = session->vep.next_sh;
870 vep_sh = session->vep.vep_sh;
871 state = session->session_state;
872 vpp_handle = session->vpp_handle;
874 if (VPPCOM_DEBUG > 0)
877 clib_warning ("VCL<%d>: vep_idx %u / sid %u: "
878 "closing epoll session...",
879 getpid (), session_handle, session_handle);
881 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %d: "
882 "closing session...",
883 getpid (), vpp_handle, session_handle);
888 while (next_sh != ~0)
890 rv = vppcom_epoll_ctl (session_handle, EPOLL_CTL_DEL, next_sh, 0);
891 if (PREDICT_FALSE (rv < 0))
892 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: EPOLL_CTL_DEL "
893 "vep_idx %u failed! rv %d (%s)",
894 getpid (), vpp_handle, next_sh, vep_sh,
895 rv, vppcom_retval_str (rv));
897 next_sh = session->vep.next_sh;
904 rv = vppcom_epoll_ctl (vep_sh, EPOLL_CTL_DEL, session_handle, 0);
906 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: EPOLL_CTL_DEL "
907 "vep_idx %u failed! rv %d (%s)",
908 getpid (), vpp_handle, session_handle,
909 vep_sh, rv, vppcom_retval_str (rv));
912 if (state & STATE_LISTEN)
914 rv = vppcom_session_unbind (session_handle);
915 if (PREDICT_FALSE (rv < 0))
916 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: listener unbind "
917 "failed! rv %d (%s)",
918 getpid (), vpp_handle, session_handle,
919 rv, vppcom_retval_str (rv));
921 else if (state & STATE_OPEN)
923 rv = vppcom_session_disconnect (session_handle);
924 if (PREDICT_FALSE (rv < 0))
925 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
926 "session disconnect failed! rv %d (%s)",
927 getpid (), vpp_handle, session_handle,
928 rv, vppcom_retval_str (rv));
932 if (vcl_session_is_ct (session))
934 vcl_cut_through_registration_t *ctr;
937 mq_addr = pointer_to_uword (session->our_evt_q);
938 ctr = vcl_ct_registration_lock_and_lookup (wrk, mq_addr);
940 if (ctr->epoll_evt_conn_index != ~0)
941 vcl_mq_epoll_del_evfd (wrk, ctr->epoll_evt_conn_index);
942 VDBG (0, "Removing ct registration %u",
943 vcl_ct_registration_index (wrk, ctr));
944 vcl_ct_registration_del (wrk, ctr);
945 vcl_ct_registration_lookup_del (wrk, mq_addr);
946 vcl_ct_registration_unlock (wrk);
949 if (vpp_handle != ~0)
951 vcl_session_table_del_vpp_handle (wrk, vpp_handle);
953 vcl_session_free (wrk, session);
955 if (VPPCOM_DEBUG > 0)
958 clib_warning ("VCL<%d>: vep_idx %u / sid %u: epoll session removed.",
959 getpid (), session_handle, session_handle);
961 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: session removed.",
962 getpid (), vpp_handle, session_handle);
965 vcl_evt (VCL_EVT_CLOSE, session, rv);
971 vppcom_session_bind (uint32_t session_handle, vppcom_endpt_t * ep)
973 vcl_worker_t *wrk = vcl_worker_get_current ();
974 vcl_session_t *session = 0;
977 return VPPCOM_EINVAL;
979 session = vcl_session_get_w_handle (wrk, session_handle);
981 return VPPCOM_EBADFD;
985 clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
986 "bind to an epoll session!", getpid (), session_handle);
987 return VPPCOM_EBADFD;
990 session->transport.is_ip4 = ep->is_ip4;
992 clib_memcpy_fast (&session->transport.lcl_ip.ip4, ep->ip,
993 sizeof (ip4_address_t));
995 clib_memcpy_fast (&session->transport.lcl_ip.ip6, ep->ip,
996 sizeof (ip6_address_t));
997 session->transport.lcl_port = ep->port;
999 VDBG (0, "VCL<%d>: sid %u: binding to local %s address %U port %u, "
1000 "proto %s", getpid (), session_handle,
1001 session->transport.is_ip4 ? "IPv4" : "IPv6",
1002 format_ip46_address, &session->transport.lcl_ip,
1003 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
1004 clib_net_to_host_u16 (session->transport.lcl_port),
1005 session->session_type ? "UDP" : "TCP");
1006 vcl_evt (VCL_EVT_BIND, session);
1008 if (session->session_type == VPPCOM_PROTO_UDP)
1009 vppcom_session_listen (session_handle, 10);
1015 vppcom_session_listen (uint32_t listen_sh, uint32_t q_len)
1017 vcl_worker_t *wrk = vcl_worker_get_current ();
1018 vcl_session_t *listen_session = 0;
1019 u64 listen_vpp_handle;
1022 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
1023 if (!listen_session)
1024 return VPPCOM_EBADFD;
1026 if (q_len == 0 || q_len == ~0)
1027 q_len = vcm->cfg.listen_queue_size;
1029 if (listen_session->is_vep)
1031 clib_warning ("VCL<%d>: ERROR: sid %u: cannot listen on an "
1032 "epoll session!", getpid (), listen_sh);
1033 return VPPCOM_EBADFD;
1036 listen_vpp_handle = listen_session->vpp_handle;
1037 if (listen_session->session_state & STATE_LISTEN)
1039 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: already in listen state!",
1040 getpid (), listen_vpp_handle, listen_sh);
1044 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: sending VPP bind+listen "
1045 "request...", getpid (), listen_vpp_handle, listen_sh);
1048 * Send listen request to vpp and wait for reply
1050 vppcom_send_bind_sock (listen_session);
1051 rv = vppcom_wait_for_session_state_change (listen_session->session_index,
1053 vcm->cfg.session_timeout);
1055 if (PREDICT_FALSE (rv))
1057 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
1058 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: bind+listen failed! "
1059 "returning %d (%s)", getpid (), listen_session->vpp_handle,
1060 listen_sh, rv, vppcom_retval_str (rv));
1068 validate_args_session_accept_ (vcl_worker_t * wrk,
1069 vcl_session_t * listen_session)
1071 /* Input validation - expects spinlock on sessions_lockp */
1072 if (listen_session->is_vep)
1074 clib_warning ("VCL<%d>: ERROR: sid %u: cannot accept on an "
1075 "epoll session!", getpid (),
1076 listen_session->session_index);
1077 return VPPCOM_EBADFD;
1080 if (listen_session->session_state != STATE_LISTEN)
1082 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
1083 "not in listen state! state 0x%x (%s)", getpid (),
1084 listen_session->vpp_handle, listen_session->session_index,
1085 listen_session->session_state,
1086 vppcom_session_state_str (listen_session->session_state));
1087 return VPPCOM_EBADFD;
1093 vppcom_session_accept (uint32_t listen_session_handle, vppcom_endpt_t * ep,
1096 u32 client_session_index = ~0, listen_session_index;
1097 vcl_worker_t *wrk = vcl_worker_get_current ();
1098 session_accepted_msg_t accepted_msg;
1099 vcl_session_t *listen_session = 0;
1100 vcl_session_t *client_session = 0;
1101 svm_msg_q_t *vpp_evt_q;
1102 vcl_session_msg_t *evt;
1103 u64 listen_vpp_handle;
1104 svm_msg_q_msg_t msg;
1109 listen_session = vcl_session_get_w_handle (wrk, listen_session_handle);
1110 if (!listen_session)
1111 return VPPCOM_EBADFD;
1113 listen_session_index = listen_session->session_index;
1114 if ((rv = validate_args_session_accept_ (wrk, listen_session)))
1117 if (clib_fifo_elts (listen_session->accept_evts_fifo))
1119 clib_fifo_sub2 (listen_session->accept_evts_fifo, evt);
1120 accepted_msg = evt->accepted_msg;
1124 is_nonblocking = VCL_SESS_ATTR_TEST (listen_session->attr,
1125 VCL_SESS_ATTR_NONBLOCK);
1126 if (svm_msg_q_is_empty (wrk->app_event_queue) && is_nonblocking)
1127 return VPPCOM_EAGAIN;
1131 if (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_WAIT, 0))
1132 return VPPCOM_EAGAIN;
1134 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
1135 if (e->event_type != SESSION_CTRL_EVT_ACCEPTED)
1137 clib_warning ("discarded event: %u", e->event_type);
1138 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
1141 clib_memcpy_fast (&accepted_msg, e->data, sizeof (accepted_msg));
1142 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
1148 client_session_index = vcl_session_accepted_handler (wrk, &accepted_msg);
1149 listen_session = vcl_session_get (wrk, listen_session_index);
1150 client_session = vcl_session_get (wrk, client_session_index);
1152 if (flags & O_NONBLOCK)
1153 VCL_SESS_ATTR_SET (client_session->attr, VCL_SESS_ATTR_NONBLOCK);
1155 listen_vpp_handle = listen_session->vpp_handle;
1156 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: Got a client request! "
1157 "vpp handle 0x%llx, sid %u, flags %d, is_nonblocking %u",
1158 getpid (), listen_vpp_handle, listen_session_handle,
1159 client_session->vpp_handle, client_session_index,
1160 flags, VCL_SESS_ATTR_TEST (client_session->attr,
1161 VCL_SESS_ATTR_NONBLOCK));
1165 ep->is_ip4 = client_session->transport.is_ip4;
1166 ep->port = client_session->transport.rmt_port;
1167 if (client_session->transport.is_ip4)
1168 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip4,
1169 sizeof (ip4_address_t));
1171 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip6,
1172 sizeof (ip6_address_t));
1175 if (accepted_msg.server_event_queue_address)
1176 vpp_evt_q = uword_to_pointer (accepted_msg.vpp_event_queue_address,
1179 vpp_evt_q = client_session->vpp_evt_q;
1181 vcl_send_session_accepted_reply (vpp_evt_q, client_session->client_context,
1182 client_session->vpp_handle, 0);
1184 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: accepted vpp handle 0x%llx, "
1185 "sid %u connection from peer %s address %U port %u to local %s "
1186 "address %U port %u", getpid (), listen_vpp_handle,
1187 listen_session_handle, client_session->vpp_handle,
1188 client_session_index,
1189 client_session->transport.is_ip4 ? "IPv4" : "IPv6",
1190 format_ip46_address, &client_session->transport.rmt_ip,
1191 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
1192 clib_net_to_host_u16 (client_session->transport.rmt_port),
1193 client_session->transport.is_ip4 ? "IPv4" : "IPv6",
1194 format_ip46_address, &client_session->transport.lcl_ip,
1195 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
1196 clib_net_to_host_u16 (client_session->transport.lcl_port));
1197 vcl_evt (VCL_EVT_ACCEPT, client_session, listen_session,
1198 client_session_index);
1200 return vcl_session_handle (client_session);
1204 vppcom_session_connect (uint32_t session_handle, vppcom_endpt_t * server_ep)
1206 vcl_worker_t *wrk = vcl_worker_get_current ();
1207 vcl_session_t *session = 0;
1211 session = vcl_session_get_w_handle (wrk, session_handle);
1213 return VPPCOM_EBADFD;
1214 session_index = session->session_index;
1216 if (PREDICT_FALSE (session->is_vep))
1218 clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
1219 "connect on an epoll session!", getpid (),
1221 return VPPCOM_EBADFD;
1224 if (PREDICT_FALSE (session->session_state & CLIENT_STATE_OPEN))
1226 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: session already "
1227 "connected to %s %U port %d proto %s, state 0x%x (%s)",
1228 getpid (), session->vpp_handle, session_handle,
1229 session->transport.is_ip4 ? "IPv4" : "IPv6",
1230 format_ip46_address,
1231 &session->transport.rmt_ip, session->transport.is_ip4 ?
1232 IP46_TYPE_IP4 : IP46_TYPE_IP6,
1233 clib_net_to_host_u16 (session->transport.rmt_port),
1234 session->session_type ? "UDP" : "TCP", session->session_state,
1235 vppcom_session_state_str (session->session_state));
1239 session->transport.is_ip4 = server_ep->is_ip4;
1240 if (session->transport.is_ip4)
1241 clib_memcpy_fast (&session->transport.rmt_ip.ip4, server_ep->ip,
1242 sizeof (ip4_address_t));
1244 clib_memcpy_fast (&session->transport.rmt_ip.ip6, server_ep->ip,
1245 sizeof (ip6_address_t));
1246 session->transport.rmt_port = server_ep->port;
1248 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: connecting to server %s %U "
1250 getpid (), session->vpp_handle, session_handle,
1251 session->transport.is_ip4 ? "IPv4" : "IPv6",
1252 format_ip46_address,
1253 &session->transport.rmt_ip, session->transport.is_ip4 ?
1254 IP46_TYPE_IP4 : IP46_TYPE_IP6,
1255 clib_net_to_host_u16 (session->transport.rmt_port),
1256 session->session_type ? "UDP" : "TCP");
1259 * Send connect request and wait for reply from vpp
1261 vppcom_send_connect_sock (session);
1262 rv = vppcom_wait_for_session_state_change (session_index, STATE_CONNECT,
1263 vcm->cfg.session_timeout);
1265 session = vcl_session_get (wrk, session_index);
1267 if (PREDICT_FALSE (rv))
1269 if (VPPCOM_DEBUG > 0)
1272 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: connect "
1273 "failed! returning %d (%s)", getpid (),
1274 session->vpp_handle, session_handle, rv,
1275 vppcom_retval_str (rv));
1277 clib_warning ("VCL<%d>: no session for sid %u: connect failed! "
1278 "returning %d (%s)", getpid (),
1279 session_handle, rv, vppcom_retval_str (rv));
1283 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: connected!",
1284 getpid (), session->vpp_handle, session_handle);
1290 vcl_is_rx_evt_for_session (session_event_t * e, u32 sid, u8 is_ct)
1293 return (e->event_type == FIFO_EVENT_APP_RX
1294 && e->fifo->client_session_index == sid);
1296 return (e->event_type == SESSION_IO_EVT_CT_TX);
1300 vcl_session_is_readable (vcl_session_t * s)
1302 return ((s->session_state & STATE_OPEN)
1303 || (s->session_state == STATE_LISTEN
1304 && s->session_type == VPPCOM_PROTO_UDP));
1308 vppcom_session_read_internal (uint32_t session_handle, void *buf, int n,
1311 vcl_worker_t *wrk = vcl_worker_get_current ();
1312 int n_read = 0, rv, is_nonblocking;
1313 vcl_session_t *s = 0;
1314 svm_fifo_t *rx_fifo;
1315 svm_msg_q_msg_t msg;
1320 if (PREDICT_FALSE (!buf))
1321 return VPPCOM_EINVAL;
1323 s = vcl_session_get_w_handle (wrk, session_handle);
1324 if (PREDICT_FALSE (!s || s->is_vep))
1325 return VPPCOM_EBADFD;
1327 if (PREDICT_FALSE (!vcl_session_is_readable (s)))
1329 session_state_t state = s->session_state;
1330 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
1332 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: %s session is not open! "
1333 "state 0x%x (%s), returning %d (%s)",
1334 getpid (), s->vpp_handle, session_handle, state,
1335 vppcom_session_state_str (state), rv, vppcom_retval_str (rv));
1339 is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
1340 is_ct = vcl_session_is_ct (s);
1341 mq = is_ct ? s->our_evt_q : wrk->app_event_queue;
1342 rx_fifo = s->rx_fifo;
1345 if (svm_fifo_is_empty (rx_fifo))
1349 svm_fifo_unset_event (rx_fifo);
1350 return VPPCOM_EWOULDBLOCK;
1352 while (svm_fifo_is_empty (rx_fifo))
1354 svm_fifo_unset_event (rx_fifo);
1355 svm_msg_q_lock (mq);
1356 if (svm_msg_q_is_empty (mq))
1357 svm_msg_q_wait (mq);
1359 svm_msg_q_sub_w_lock (mq, &msg);
1360 e = svm_msg_q_msg_data (mq, &msg);
1361 svm_msg_q_unlock (mq);
1362 if (!vcl_is_rx_evt_for_session (e, s->session_index, is_ct))
1364 vcl_handle_mq_event (wrk, e);
1365 svm_msg_q_free_msg (mq, &msg);
1368 svm_msg_q_free_msg (mq, &msg);
1370 if (PREDICT_FALSE (s->session_state == STATE_CLOSE_ON_EMPTY))
1376 n_read = app_recv_dgram_raw (rx_fifo, buf, n, &s->transport, 0, peek);
1378 n_read = app_recv_stream_raw (rx_fifo, buf, n, 0, peek);
1380 if (svm_fifo_is_empty (rx_fifo))
1381 svm_fifo_unset_event (rx_fifo);
1383 if (is_ct && svm_fifo_want_tx_evt (rx_fifo))
1385 svm_fifo_set_want_tx_evt (s->rx_fifo, 0);
1386 app_send_io_evt_to_vpp (s->vpp_evt_q, s->rx_fifo, SESSION_IO_EVT_CT_RX,
1390 VDBG (2, "VCL<%d>: vpp handle 0x%llx, sid %u: read %d bytes from (%p)",
1391 getpid (), s->vpp_handle, session_handle, n_read, rx_fifo);
1397 vppcom_session_read (uint32_t session_handle, void *buf, size_t n)
1399 return (vppcom_session_read_internal (session_handle, buf, n, 0));
1403 vppcom_session_peek (uint32_t session_handle, void *buf, int n)
1405 return (vppcom_session_read_internal (session_handle, buf, n, 1));
1409 vppcom_session_read_segments (uint32_t session_handle,
1410 vppcom_data_segments_t ds)
1412 vcl_worker_t *wrk = vcl_worker_get_current ();
1413 int n_read = 0, rv, is_nonblocking;
1414 vcl_session_t *s = 0;
1415 svm_fifo_t *rx_fifo;
1416 svm_msg_q_msg_t msg;
1421 s = vcl_session_get_w_handle (wrk, session_handle);
1422 if (PREDICT_FALSE (!s || s->is_vep))
1423 return VPPCOM_EBADFD;
1425 if (PREDICT_FALSE (!vcl_session_is_readable (s)))
1427 session_state_t state = s->session_state;
1428 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
1432 is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
1433 is_ct = vcl_session_is_ct (s);
1434 mq = is_ct ? s->our_evt_q : wrk->app_event_queue;
1435 rx_fifo = s->rx_fifo;
1438 if (svm_fifo_is_empty (rx_fifo))
1442 svm_fifo_unset_event (rx_fifo);
1443 return VPPCOM_EWOULDBLOCK;
1445 while (svm_fifo_is_empty (rx_fifo))
1447 svm_fifo_unset_event (rx_fifo);
1448 svm_msg_q_lock (mq);
1449 if (svm_msg_q_is_empty (mq))
1450 svm_msg_q_wait (mq);
1452 svm_msg_q_sub_w_lock (mq, &msg);
1453 e = svm_msg_q_msg_data (mq, &msg);
1454 svm_msg_q_unlock (mq);
1455 if (!vcl_is_rx_evt_for_session (e, s->session_index, is_ct))
1457 vcl_handle_mq_event (wrk, e);
1458 svm_msg_q_free_msg (mq, &msg);
1461 svm_msg_q_free_msg (mq, &msg);
1463 if (PREDICT_FALSE (s->session_state == STATE_CLOSE_ON_EMPTY))
1468 n_read = svm_fifo_segments (rx_fifo, (svm_fifo_segment_t *) ds);
1469 svm_fifo_unset_event (rx_fifo);
1471 if (is_ct && n_read + svm_fifo_max_dequeue (rx_fifo) == rx_fifo->nitems)
1473 /* If the peer is not polling send notification */
1474 if (!svm_fifo_has_event (s->rx_fifo))
1475 app_send_io_evt_to_vpp (s->vpp_evt_q, s->rx_fifo,
1476 SESSION_IO_EVT_CT_RX, SVM_Q_WAIT);
1483 vppcom_session_free_segments (uint32_t session_handle,
1484 vppcom_data_segments_t ds)
1486 vcl_worker_t *wrk = vcl_worker_get_current ();
1489 s = vcl_session_get_w_handle (wrk, session_handle);
1490 if (PREDICT_FALSE (!s || s->is_vep))
1493 svm_fifo_segments_free (s->rx_fifo, (svm_fifo_segment_t *) ds);
1497 vppcom_session_read_ready (vcl_session_t * session)
1499 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
1500 if (PREDICT_FALSE (session->is_vep))
1502 clib_warning ("VCL<%d>: ERROR: sid %u: cannot read from an "
1503 "epoll session!", getpid (), session->session_index);
1504 return VPPCOM_EBADFD;
1507 if (PREDICT_FALSE (!(session->session_state & (STATE_OPEN | STATE_LISTEN))))
1509 session_state_t state = session->session_state;
1512 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
1514 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: session is not open!"
1515 " state 0x%x (%s), returning %d (%s)", getpid (),
1516 session->vpp_handle, session->session_index, state,
1517 vppcom_session_state_str (state), rv, vppcom_retval_str (rv));
1521 if (session->session_state & STATE_LISTEN)
1522 return clib_fifo_elts (session->accept_evts_fifo);
1524 return svm_fifo_max_dequeue (session->rx_fifo);
1528 vppcom_data_segment_copy (void *buf, vppcom_data_segments_t ds, u32 max_bytes)
1530 u32 first_copy = clib_min (ds[0].len, max_bytes);
1531 clib_memcpy_fast (buf, ds[0].data, first_copy);
1532 if (first_copy < max_bytes)
1534 clib_memcpy_fast (buf + first_copy, ds[1].data,
1535 clib_min (ds[1].len, max_bytes - first_copy));
1541 vcl_is_tx_evt_for_session (session_event_t * e, u32 sid, u8 is_ct)
1544 return (e->event_type == FIFO_EVENT_APP_TX
1545 && e->fifo->client_session_index == sid);
1547 return (e->event_type == SESSION_IO_EVT_CT_RX);
1551 vppcom_session_write (uint32_t session_handle, void *buf, size_t n)
1553 vcl_worker_t *wrk = vcl_worker_get_current ();
1554 int rv, n_write, is_nonblocking;
1555 vcl_session_t *s = 0;
1556 svm_fifo_t *tx_fifo = 0;
1557 session_evt_type_t et;
1558 svm_msg_q_msg_t msg;
1563 if (PREDICT_FALSE (!buf))
1564 return VPPCOM_EINVAL;
1566 s = vcl_session_get_w_handle (wrk, session_handle);
1567 if (PREDICT_FALSE (!s))
1568 return VPPCOM_EBADFD;
1570 if (PREDICT_FALSE (s->is_vep))
1572 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
1573 "cannot write to an epoll session!",
1574 getpid (), s->vpp_handle, session_handle);
1576 return VPPCOM_EBADFD;
1579 if (PREDICT_FALSE (!(s->session_state & STATE_OPEN)))
1581 session_state_t state = s->session_state;
1582 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
1583 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: session is not open! "
1584 "state 0x%x (%s)", getpid (), s->vpp_handle, session_handle,
1585 state, vppcom_session_state_str (state));
1589 tx_fifo = s->tx_fifo;
1590 is_ct = vcl_session_is_ct (s);
1591 is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
1592 mq = is_ct ? s->our_evt_q : wrk->app_event_queue;
1593 if (svm_fifo_is_full (tx_fifo))
1597 return VPPCOM_EWOULDBLOCK;
1599 while (svm_fifo_is_full (tx_fifo))
1601 svm_fifo_set_want_tx_evt (tx_fifo, 1);
1602 svm_msg_q_lock (mq);
1603 if (svm_msg_q_is_empty (mq))
1604 svm_msg_q_wait (mq);
1606 svm_msg_q_sub_w_lock (mq, &msg);
1607 e = svm_msg_q_msg_data (mq, &msg);
1608 svm_msg_q_unlock (mq);
1610 if (!vcl_is_tx_evt_for_session (e, s->session_index, is_ct))
1611 vcl_handle_mq_event (wrk, e);
1612 svm_msg_q_free_msg (mq, &msg);
1616 ASSERT (FIFO_EVENT_APP_TX + 1 == SESSION_IO_EVT_CT_TX);
1617 et = FIFO_EVENT_APP_TX + vcl_session_is_ct (s);
1619 n_write = app_send_dgram_raw (tx_fifo, &s->transport,
1620 s->vpp_evt_q, buf, n, et, SVM_Q_WAIT);
1622 n_write = app_send_stream_raw (tx_fifo, s->vpp_evt_q, buf, n, et,
1625 ASSERT (n_write > 0);
1627 VDBG (2, "VCL<%d>: vpp handle 0x%llx, sid %u: wrote %d bytes", getpid (),
1628 s->vpp_handle, session_handle, n_write);
1633 static vcl_session_t *
1634 vcl_ct_session_get_from_fifo (vcl_worker_t * wrk, svm_fifo_t * f, u8 type)
1637 s = vcl_session_get (wrk, f->client_session_index);
1641 if (type == 0 && s->rx_fifo == f)
1644 if (type == 1 && s->tx_fifo == f)
1647 s = vcl_session_get (wrk, f->master_session_index);
1650 if (type == 0 && s->rx_fifo == f)
1652 if (type == 1 && s->tx_fifo == f)
1659 vppcom_session_write_ready (vcl_session_t * session)
1661 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
1662 if (PREDICT_FALSE (session->is_vep))
1664 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
1665 "cannot write to an epoll session!",
1666 getpid (), session->vpp_handle, session->session_index);
1667 return VPPCOM_EBADFD;
1670 if (PREDICT_FALSE (session->session_state & STATE_LISTEN))
1672 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
1673 "cannot write to a listen session!",
1674 getpid (), session->vpp_handle, session->session_index);
1675 return VPPCOM_EBADFD;
1678 if (PREDICT_FALSE (!(session->session_state & STATE_OPEN)))
1680 session_state_t state = session->session_state;
1683 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
1684 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
1685 "session is not open! state 0x%x (%s), "
1686 "returning %d (%s)", getpid (), session->vpp_handle,
1687 session->session_index,
1688 state, vppcom_session_state_str (state),
1689 rv, vppcom_retval_str (rv));
1693 VDBG (3, "VCL<%d>: vpp handle 0x%llx, sid %u: peek %s (%p), ready = %d",
1694 getpid (), session->vpp_handle, session->session_index,
1695 session->tx_fifo, svm_fifo_max_enqueue (session->tx_fifo));
1697 return svm_fifo_max_enqueue (session->tx_fifo);
1701 vcl_mq_dequeue_batch (vcl_worker_t * wrk, svm_msg_q_t * mq)
1703 svm_msg_q_msg_t *msg;
1707 n_msgs = svm_msg_q_size (mq);
1708 for (i = 0; i < n_msgs; i++)
1710 vec_add2 (wrk->mq_msg_vector, msg, 1);
1711 svm_msg_q_sub_w_lock (mq, msg);
1716 #define vcl_fifo_rx_evt_valid_or_break(_fifo) \
1717 if (PREDICT_FALSE (svm_fifo_is_empty (_fifo))) \
1719 svm_fifo_unset_event (_fifo); \
1720 if (svm_fifo_is_empty (_fifo)) \
1725 vcl_select_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
1726 unsigned long n_bits, unsigned long *read_map,
1727 unsigned long *write_map,
1728 unsigned long *except_map, u32 * bits_set)
1730 session_disconnected_msg_t *disconnected_msg;
1731 session_connected_msg_t *connected_msg;
1732 session_accepted_msg_t *accepted_msg;
1733 vcl_session_msg_t *vcl_msg;
1734 vcl_session_t *session;
1738 switch (e->event_type)
1740 case FIFO_EVENT_APP_RX:
1741 vcl_fifo_rx_evt_valid_or_break (e->fifo);
1742 sid = e->fifo->client_session_index;
1743 session = vcl_session_get (wrk, sid);
1746 if (sid < n_bits && read_map)
1748 clib_bitmap_set_no_check (read_map, sid, 1);
1752 case FIFO_EVENT_APP_TX:
1753 sid = e->fifo->client_session_index;
1754 session = vcl_session_get (wrk, sid);
1757 if (sid < n_bits && write_map)
1759 clib_bitmap_set_no_check (write_map, sid, 1);
1763 case SESSION_IO_EVT_CT_TX:
1764 vcl_fifo_rx_evt_valid_or_break (e->fifo);
1765 session = vcl_ct_session_get_from_fifo (wrk, e->fifo, 0);
1768 sid = session->session_index;
1769 if (sid < n_bits && read_map)
1771 clib_bitmap_set_no_check (read_map, sid, 1);
1775 case SESSION_IO_EVT_CT_RX:
1776 session = vcl_ct_session_get_from_fifo (wrk, e->fifo, 1);
1779 sid = session->session_index;
1780 if (sid < n_bits && write_map)
1782 clib_bitmap_set_no_check (write_map, sid, 1);
1786 case SESSION_CTRL_EVT_ACCEPTED:
1787 accepted_msg = (session_accepted_msg_t *) e->data;
1788 handle = accepted_msg->listener_handle;
1789 session = vcl_session_table_lookup_listener (wrk, handle);
1792 clib_warning ("VCL<%d>: ERROR: couldn't find listen session:"
1793 "listener handle %llx", getpid (), handle);
1797 clib_fifo_add2 (session->accept_evts_fifo, vcl_msg);
1798 vcl_msg->accepted_msg = *accepted_msg;
1799 sid = session->session_index;
1800 if (sid < n_bits && read_map)
1802 clib_bitmap_set_no_check (read_map, sid, 1);
1806 case SESSION_CTRL_EVT_CONNECTED:
1807 connected_msg = (session_connected_msg_t *) e->data;
1808 vcl_session_connected_handler (wrk, connected_msg);
1810 case SESSION_CTRL_EVT_DISCONNECTED:
1811 disconnected_msg = (session_disconnected_msg_t *) e->data;
1812 sid = vcl_session_index_from_vpp_handle (wrk, disconnected_msg->handle);
1813 if (sid < n_bits && except_map)
1815 clib_bitmap_set_no_check (except_map, sid, 1);
1819 case SESSION_CTRL_EVT_RESET:
1820 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
1821 if (sid < n_bits && except_map)
1823 clib_bitmap_set_no_check (except_map, sid, 1);
1828 clib_warning ("unhandled: %u", e->event_type);
1834 vcl_select_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
1835 unsigned long n_bits, unsigned long *read_map,
1836 unsigned long *write_map, unsigned long *except_map,
1837 double time_to_wait, u32 * bits_set)
1839 svm_msg_q_msg_t *msg;
1843 svm_msg_q_lock (mq);
1844 if (svm_msg_q_is_empty (mq))
1848 svm_msg_q_unlock (mq);
1854 svm_msg_q_unlock (mq);
1857 else if (time_to_wait < 0)
1859 svm_msg_q_wait (mq);
1863 if (svm_msg_q_timedwait (mq, time_to_wait))
1865 svm_msg_q_unlock (mq);
1870 vcl_mq_dequeue_batch (wrk, mq);
1871 svm_msg_q_unlock (mq);
1873 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
1875 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
1876 e = svm_msg_q_msg_data (mq, msg);
1877 vcl_select_handle_mq_event (wrk, e, n_bits, read_map, write_map,
1878 except_map, bits_set);
1879 svm_msg_q_free_msg (mq, msg);
1881 vec_reset_length (wrk->mq_msg_vector);
1886 vppcom_select_condvar (vcl_worker_t * wrk, unsigned long n_bits,
1887 unsigned long *read_map, unsigned long *write_map,
1888 unsigned long *except_map, double time_to_wait,
1891 double total_wait = 0, wait_slice;
1892 vcl_cut_through_registration_t *cr;
1894 time_to_wait = (time_to_wait == -1) ? 10e9 : time_to_wait;
1895 wait_slice = wrk->cut_through_registrations ? 10e-6 : time_to_wait;
1898 vcl_ct_registration_lock (wrk);
1900 pool_foreach (cr, wrk->cut_through_registrations, ({
1901 vcl_select_handle_mq (wrk, cr->mq, n_bits, read_map, write_map, except_map,
1905 vcl_ct_registration_unlock (wrk);
1907 vcl_select_handle_mq (wrk, wrk->app_event_queue, n_bits, read_map,
1908 write_map, except_map, time_to_wait, bits_set);
1909 total_wait += wait_slice;
1913 while (total_wait < time_to_wait);
1919 vppcom_select_eventfd (vcl_worker_t * wrk, unsigned long n_bits,
1920 unsigned long *read_map, unsigned long *write_map,
1921 unsigned long *except_map, double time_to_wait,
1924 vcl_mq_evt_conn_t *mqc;
1925 int __clib_unused n_read;
1929 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
1930 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
1931 vec_len (wrk->mq_events), time_to_wait);
1932 for (i = 0; i < n_mq_evts; i++)
1934 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
1935 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
1936 vcl_select_handle_mq (wrk, mqc->mq, n_bits, read_map, write_map,
1937 except_map, 0, bits_set);
1940 return (n_mq_evts > 0 ? (int) *bits_set : 0);
1944 vppcom_select (unsigned long n_bits, unsigned long *read_map,
1945 unsigned long *write_map, unsigned long *except_map,
1946 double time_to_wait)
1948 u32 sid, minbits = clib_max (n_bits, BITS (uword)), bits_set = 0;
1949 vcl_worker_t *wrk = vcl_worker_get_current ();
1950 vcl_session_t *session = 0;
1953 ASSERT (sizeof (clib_bitmap_t) == sizeof (long int));
1955 if (n_bits && read_map)
1957 clib_bitmap_validate (wrk->rd_bitmap, minbits);
1958 clib_memcpy_fast (wrk->rd_bitmap, read_map,
1959 vec_len (wrk->rd_bitmap) * sizeof (clib_bitmap_t));
1960 memset (read_map, 0, vec_len (wrk->rd_bitmap) * sizeof (clib_bitmap_t));
1962 if (n_bits && write_map)
1964 clib_bitmap_validate (wrk->wr_bitmap, minbits);
1965 clib_memcpy_fast (wrk->wr_bitmap, write_map,
1966 vec_len (wrk->wr_bitmap) * sizeof (clib_bitmap_t));
1967 memset (write_map, 0,
1968 vec_len (wrk->wr_bitmap) * sizeof (clib_bitmap_t));
1970 if (n_bits && except_map)
1972 clib_bitmap_validate (wrk->ex_bitmap, minbits);
1973 clib_memcpy_fast (wrk->ex_bitmap, except_map,
1974 vec_len (wrk->ex_bitmap) * sizeof (clib_bitmap_t));
1975 memset (except_map, 0,
1976 vec_len (wrk->ex_bitmap) * sizeof (clib_bitmap_t));
1986 clib_bitmap_foreach (sid, wrk->wr_bitmap, ({
1987 if (!(session = vcl_session_get (wrk, sid)))
1989 VDBG (0, "VCL<%d>: session %d specified in write_map is closed.",
1991 return VPPCOM_EBADFD;
1994 rv = svm_fifo_is_full (session->tx_fifo);
1997 clib_bitmap_set_no_check (write_map, sid, 1);
2006 clib_bitmap_foreach (sid, wrk->rd_bitmap, ({
2007 if (!(session = vcl_session_get (wrk, sid)))
2009 VDBG (0, "VCL<%d>: session %d specified in write_map is closed.",
2011 return VPPCOM_EBADFD;
2014 rv = vppcom_session_read_ready (session);
2017 clib_bitmap_set_no_check (read_map, sid, 1);
2025 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
2027 vcl_select_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i], n_bits,
2028 read_map, write_map, except_map, &bits_set);
2030 vec_reset_length (wrk->unhandled_evts_vector);
2032 if (vcm->cfg.use_mq_eventfd)
2033 vppcom_select_eventfd (wrk, n_bits, read_map, write_map, except_map,
2034 time_to_wait, &bits_set);
2036 vppcom_select_condvar (wrk, n_bits, read_map, write_map, except_map,
2037 time_to_wait, &bits_set);
2043 vep_verify_epoll_chain (vcl_worker_t * wrk, u32 vep_idx)
2045 vcl_session_t *session;
2046 vppcom_epoll_t *vep;
2049 if (VPPCOM_DEBUG <= 1)
2052 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
2053 session = vcl_session_get (wrk, vep_idx);
2054 if (PREDICT_FALSE (!session))
2056 clib_warning ("VCL<%d>: ERROR: Invalid vep_idx (%u)!",
2057 getpid (), vep_idx);
2060 if (PREDICT_FALSE (!session->is_vep))
2062 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
2063 getpid (), vep_idx);
2066 vep = &session->vep;
2067 clib_warning ("VCL<%d>: vep_idx (%u): Dumping epoll chain\n"
2070 " is_vep_session = %u\n"
2071 " next_sid = 0x%x (%u)\n"
2072 " wait_cont_idx = 0x%x (%u)\n"
2073 "}\n", getpid (), vep_idx,
2074 session->is_vep, session->is_vep_session,
2075 vep->next_sh, vep->next_sh,
2076 session->wait_cont_idx, session->wait_cont_idx);
2078 for (sid = vep->next_sh; sid != ~0; sid = vep->next_sh)
2080 session = vcl_session_get (wrk, sid);
2081 if (PREDICT_FALSE (!session))
2083 clib_warning ("VCL<%d>: ERROR: Invalid sid (%u)!", getpid (), sid);
2086 if (PREDICT_FALSE (session->is_vep))
2087 clib_warning ("VCL<%d>: ERROR: sid (%u) is a vep!",
2088 getpid (), vep_idx);
2089 else if (PREDICT_FALSE (!session->is_vep_session))
2091 clib_warning ("VCL<%d>: ERROR: session (%u) "
2092 "is not a vep session!", getpid (), sid);
2095 vep = &session->vep;
2096 if (PREDICT_FALSE (vep->vep_sh != vep_idx))
2097 clib_warning ("VCL<%d>: ERROR: session (%u) vep_idx (%u) != "
2098 "vep_idx (%u)!", getpid (),
2099 sid, session->vep.vep_sh, vep_idx);
2100 if (session->is_vep_session)
2102 clib_warning ("vep_idx[%u]: sid 0x%x (%u)\n"
2104 " next_sid = 0x%x (%u)\n"
2105 " prev_sid = 0x%x (%u)\n"
2106 " vep_idx = 0x%x (%u)\n"
2107 " ev.events = 0x%x\n"
2108 " ev.data.u64 = 0x%llx\n"
2112 vep->next_sh, vep->next_sh,
2113 vep->prev_sh, vep->prev_sh,
2114 vep->vep_sh, vep->vep_sh,
2115 vep->ev.events, vep->ev.data.u64, vep->et_mask);
2120 clib_warning ("VCL<%d>: vep_idx (%u): Dump complete!\n",
2121 getpid (), vep_idx);
2125 vppcom_epoll_create (void)
2127 vcl_worker_t *wrk = vcl_worker_get_current ();
2128 vcl_session_t *vep_session;
2130 vep_session = vcl_session_alloc (wrk);
2132 vep_session->is_vep = 1;
2133 vep_session->vep.vep_sh = ~0;
2134 vep_session->vep.next_sh = ~0;
2135 vep_session->vep.prev_sh = ~0;
2136 vep_session->wait_cont_idx = ~0;
2137 vep_session->vpp_handle = ~0;
2139 vcl_evt (VCL_EVT_EPOLL_CREATE, vep_session, vep_sh);
2140 VDBG (0, "VCL<%d>: Created vep_idx %u / sid %u!",
2141 getpid (), vep_session->session_index, vep_session->session_index);
2143 return vcl_session_handle (vep_session);
2147 vppcom_epoll_ctl (uint32_t vep_handle, int op, uint32_t session_handle,
2148 struct epoll_event *event)
2150 vcl_worker_t *wrk = vcl_worker_get_current ();
2151 vcl_session_t *vep_session;
2152 vcl_session_t *session;
2155 if (vep_handle == session_handle)
2157 clib_warning ("VCL<%d>: ERROR: vep_idx == session_index (%u)!",
2158 getpid (), vep_handle);
2159 return VPPCOM_EINVAL;
2162 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
2163 if (PREDICT_FALSE (!vep_session))
2165 clib_warning ("VCL<%d>: ERROR: Invalid vep_idx (%u)!", vep_handle);
2166 return VPPCOM_EBADFD;
2168 if (PREDICT_FALSE (!vep_session->is_vep))
2170 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
2171 getpid (), vep_handle);
2172 return VPPCOM_EINVAL;
2175 ASSERT (vep_session->vep.vep_sh == ~0);
2176 ASSERT (vep_session->vep.prev_sh == ~0);
2178 session = vcl_session_get_w_handle (wrk, session_handle);
2179 if (PREDICT_FALSE (!session))
2181 VDBG (0, "VCL<%d>: ERROR: Invalid session_handle (%u)!",
2182 getpid (), session_handle);
2183 return VPPCOM_EBADFD;
2185 if (PREDICT_FALSE (session->is_vep))
2187 clib_warning ("ERROR: session_handle (%u) is a vep!", vep_handle);
2188 return VPPCOM_EINVAL;
2194 if (PREDICT_FALSE (!event))
2196 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_ADD: NULL pointer to "
2197 "epoll_event structure!", getpid ());
2198 return VPPCOM_EINVAL;
2200 if (vep_session->vep.next_sh != ~0)
2202 vcl_session_t *next_session;
2203 next_session = vcl_session_get_w_handle (wrk,
2204 vep_session->vep.next_sh);
2205 if (PREDICT_FALSE (!next_session))
2207 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_ADD: Invalid "
2208 "vep.next_sid (%u) on vep_idx (%u)!",
2209 getpid (), vep_session->vep.next_sh, vep_handle);
2210 return VPPCOM_EBADFD;
2212 ASSERT (next_session->vep.prev_sh == vep_handle);
2213 next_session->vep.prev_sh = session_handle;
2215 session->vep.next_sh = vep_session->vep.next_sh;
2216 session->vep.prev_sh = vep_handle;
2217 session->vep.vep_sh = vep_handle;
2218 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
2219 session->vep.ev = *event;
2220 session->is_vep = 0;
2221 session->is_vep_session = 1;
2222 vep_session->vep.next_sh = session_handle;
2224 VDBG (1, "VCL<%d>: EPOLL_CTL_ADD: vep_idx %u, sid %u, events 0x%x, "
2225 "data 0x%llx!", getpid (), vep_handle, session_handle,
2226 event->events, event->data.u64);
2227 vcl_evt (VCL_EVT_EPOLL_CTLADD, session, event->events, event->data.u64);
2231 if (PREDICT_FALSE (!event))
2233 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_MOD: NULL pointer to "
2234 "epoll_event structure!", getpid ());
2238 else if (PREDICT_FALSE (!session->is_vep_session))
2240 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_MOD: "
2241 "not a vep session!", getpid (), session_handle);
2245 else if (PREDICT_FALSE (session->vep.vep_sh != vep_handle))
2247 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_MOD: "
2248 "vep_idx (%u) != vep_idx (%u)!",
2249 getpid (), session_handle,
2250 session->vep.vep_sh, vep_handle);
2254 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
2255 session->vep.ev = *event;
2256 VDBG (1, "VCL<%d>: EPOLL_CTL_MOD: vep_idx %u, sid %u, events 0x%x,"
2257 " data 0x%llx!", getpid (), vep_handle, session_handle,
2258 event->events, event->data.u64);
2262 if (PREDICT_FALSE (!session->is_vep_session))
2264 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_DEL: "
2265 "not a vep session!", getpid (), session_handle);
2269 else if (PREDICT_FALSE (session->vep.vep_sh != vep_handle))
2271 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_DEL: "
2272 "vep_idx (%u) != vep_idx (%u)!",
2273 getpid (), session_handle,
2274 session->vep.vep_sh, vep_handle);
2279 vep_session->wait_cont_idx =
2280 (vep_session->wait_cont_idx == session_handle) ?
2281 session->vep.next_sh : vep_session->wait_cont_idx;
2283 if (session->vep.prev_sh == vep_handle)
2284 vep_session->vep.next_sh = session->vep.next_sh;
2287 vcl_session_t *prev_session;
2288 prev_session = vcl_session_get_w_handle (wrk, session->vep.prev_sh);
2289 if (PREDICT_FALSE (!prev_session))
2291 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_DEL: Invalid "
2292 "vep.prev_sid (%u) on sid (%u)!",
2293 getpid (), session->vep.prev_sh, session_handle);
2294 return VPPCOM_EBADFD;
2296 ASSERT (prev_session->vep.next_sh == session_handle);
2297 prev_session->vep.next_sh = session->vep.next_sh;
2299 if (session->vep.next_sh != ~0)
2301 vcl_session_t *next_session;
2302 next_session = vcl_session_get_w_handle (wrk, session->vep.next_sh);
2303 if (PREDICT_FALSE (!next_session))
2305 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_DEL: Invalid "
2306 "vep.next_sid (%u) on sid (%u)!",
2307 getpid (), session->vep.next_sh, session_handle);
2308 return VPPCOM_EBADFD;
2310 ASSERT (next_session->vep.prev_sh == session_handle);
2311 next_session->vep.prev_sh = session->vep.prev_sh;
2314 memset (&session->vep, 0, sizeof (session->vep));
2315 session->vep.next_sh = ~0;
2316 session->vep.prev_sh = ~0;
2317 session->vep.vep_sh = ~0;
2318 session->is_vep_session = 0;
2319 VDBG (1, "VCL<%d>: EPOLL_CTL_DEL: vep_idx %u, sid %u!",
2320 getpid (), vep_handle, session_handle);
2321 vcl_evt (VCL_EVT_EPOLL_CTLDEL, session, vep_sh);
2325 clib_warning ("VCL<%d>: ERROR: Invalid operation (%d)!", getpid (), op);
2329 vep_verify_epoll_chain (wrk, vep_handle);
2336 vcl_epoll_wait_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
2337 struct epoll_event *events, u32 * num_ev)
2339 session_disconnected_msg_t *disconnected_msg;
2340 session_connected_msg_t *connected_msg;
2341 session_accepted_msg_t *accepted_msg;
2342 u64 session_evt_data = ~0, handle;
2343 u32 sid = ~0, session_events;
2344 vcl_session_msg_t *vcl_msg;
2345 vcl_session_t *session;
2348 switch (e->event_type)
2350 case FIFO_EVENT_APP_RX:
2351 ASSERT (e->fifo->client_thread_index == vcl_get_worker_index ());
2352 vcl_fifo_rx_evt_valid_or_break (e->fifo);
2353 sid = e->fifo->client_session_index;
2354 session = vcl_session_get (wrk, sid);
2355 session_events = session->vep.ev.events;
2356 if (!(EPOLLIN & session->vep.ev.events) || session->has_rx_evt)
2359 events[*num_ev].events |= EPOLLIN;
2360 session_evt_data = session->vep.ev.data.u64;
2361 session->has_rx_evt = 1;
2363 case FIFO_EVENT_APP_TX:
2364 sid = e->fifo->client_session_index;
2365 session = vcl_session_get (wrk, sid);
2366 session_events = session->vep.ev.events;
2367 if (!(EPOLLOUT & session_events))
2370 events[*num_ev].events |= EPOLLOUT;
2371 session_evt_data = session->vep.ev.data.u64;
2373 case SESSION_IO_EVT_CT_TX:
2374 vcl_fifo_rx_evt_valid_or_break (e->fifo);
2375 session = vcl_ct_session_get_from_fifo (wrk, e->fifo, 0);
2376 sid = session->session_index;
2377 session_events = session->vep.ev.events;
2378 if (!(EPOLLIN & session->vep.ev.events) || session->has_rx_evt)
2381 events[*num_ev].events |= EPOLLIN;
2382 session_evt_data = session->vep.ev.data.u64;
2383 session->has_rx_evt = 1;
2385 case SESSION_IO_EVT_CT_RX:
2386 session = vcl_ct_session_get_from_fifo (wrk, e->fifo, 1);
2387 sid = session->session_index;
2388 session_events = session->vep.ev.events;
2389 if (!(EPOLLOUT & session_events))
2392 events[*num_ev].events |= EPOLLOUT;
2393 session_evt_data = session->vep.ev.data.u64;
2395 case SESSION_CTRL_EVT_ACCEPTED:
2396 accepted_msg = (session_accepted_msg_t *) e->data;
2397 handle = accepted_msg->listener_handle;
2398 session = vcl_session_table_lookup_listener (wrk, handle);
2401 clib_warning ("VCL<%d>: ERROR: couldn't find listen session:"
2402 "listener handle %llx", getpid (), handle);
2406 clib_fifo_add2 (session->accept_evts_fifo, vcl_msg);
2407 vcl_msg->accepted_msg = *accepted_msg;
2408 session_events = session->vep.ev.events;
2409 if (!(EPOLLIN & session_events))
2413 events[*num_ev].events |= EPOLLIN;
2414 session_evt_data = session->vep.ev.data.u64;
2416 case SESSION_CTRL_EVT_CONNECTED:
2417 connected_msg = (session_connected_msg_t *) e->data;
2418 vcl_session_connected_handler (wrk, connected_msg);
2419 /* Generate EPOLLOUT because there's no connected event */
2420 sid = vcl_session_index_from_vpp_handle (wrk, connected_msg->handle);
2421 session = vcl_session_get (wrk, sid);
2422 session_events = session->vep.ev.events;
2423 if (EPOLLOUT & session_events)
2426 events[*num_ev].events |= EPOLLOUT;
2427 session_evt_data = session->vep.ev.data.u64;
2430 case SESSION_CTRL_EVT_DISCONNECTED:
2431 disconnected_msg = (session_disconnected_msg_t *) e->data;
2432 sid = vcl_session_index_from_vpp_handle (wrk, disconnected_msg->handle);
2433 if (!(session = vcl_session_get (wrk, sid)))
2436 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
2437 session_evt_data = session->vep.ev.data.u64;
2438 session_events = session->vep.ev.events;
2440 case SESSION_CTRL_EVT_RESET:
2441 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
2442 if (!(session = vcl_session_get (wrk, sid)))
2445 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
2446 session_evt_data = session->vep.ev.data.u64;
2447 session_events = session->vep.ev.events;
2450 VDBG (0, "unhandled: %u", e->event_type);
2456 events[*num_ev].data.u64 = session_evt_data;
2457 if (EPOLLONESHOT & session_events)
2459 session = vcl_session_get (wrk, sid);
2460 session->vep.ev.events = 0;
2467 vcl_epoll_wait_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
2468 struct epoll_event *events, u32 maxevents,
2469 double wait_for_time, u32 * num_ev)
2471 svm_msg_q_msg_t *msg;
2475 if (vec_len (wrk->mq_msg_vector) && svm_msg_q_is_empty (mq))
2476 goto handle_dequeued;
2478 svm_msg_q_lock (mq);
2479 if (svm_msg_q_is_empty (mq))
2483 svm_msg_q_unlock (mq);
2486 else if (wait_for_time < 0)
2488 svm_msg_q_wait (mq);
2492 if (svm_msg_q_timedwait (mq, wait_for_time / 1e3))
2494 svm_msg_q_unlock (mq);
2499 vcl_mq_dequeue_batch (wrk, mq);
2500 svm_msg_q_unlock (mq);
2503 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
2505 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
2506 e = svm_msg_q_msg_data (mq, msg);
2507 if (*num_ev < maxevents)
2508 vcl_epoll_wait_handle_mq_event (wrk, e, events, num_ev);
2510 vec_add1 (wrk->unhandled_evts_vector, *e);
2511 svm_msg_q_free_msg (mq, msg);
2513 vec_reset_length (wrk->mq_msg_vector);
2519 vppcom_epoll_wait_condvar (vcl_worker_t * wrk, struct epoll_event *events,
2520 int maxevents, u32 n_evts, double wait_for_time)
2522 vcl_cut_through_registration_t *cr;
2523 double total_wait = 0, wait_slice;
2526 wait_for_time = (wait_for_time == -1) ? (double) 10e9 : wait_for_time;
2527 wait_slice = wrk->cut_through_registrations ? 10e-6 : wait_for_time;
2531 vcl_ct_registration_lock (wrk);
2533 pool_foreach (cr, wrk->cut_through_registrations, ({
2534 vcl_epoll_wait_handle_mq (wrk, cr->mq, events, maxevents, 0, &n_evts);
2537 vcl_ct_registration_unlock (wrk);
2539 rv = vcl_epoll_wait_handle_mq (wrk, wrk->app_event_queue, events,
2540 maxevents, n_evts ? 0 : wait_slice,
2543 total_wait += wait_slice;
2547 while (total_wait < wait_for_time);
2552 vppcom_epoll_wait_eventfd (vcl_worker_t * wrk, struct epoll_event *events,
2553 int maxevents, u32 n_evts, double wait_for_time)
2555 vcl_mq_evt_conn_t *mqc;
2556 int __clib_unused n_read;
2560 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
2562 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
2563 vec_len (wrk->mq_events), wait_for_time);
2564 for (i = 0; i < n_mq_evts; i++)
2566 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
2567 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
2568 vcl_epoll_wait_handle_mq (wrk, mqc->mq, events, maxevents, 0, &n_evts);
2570 if (!n_evts && n_mq_evts > 0)
2573 return (int) n_evts;
2577 vppcom_epoll_wait (uint32_t vep_handle, struct epoll_event *events,
2578 int maxevents, double wait_for_time)
2580 vcl_worker_t *wrk = vcl_worker_get_current ();
2581 vcl_session_t *vep_session;
2585 if (PREDICT_FALSE (maxevents <= 0))
2587 clib_warning ("VCL<%d>: ERROR: Invalid maxevents (%d)!",
2588 getpid (), maxevents);
2589 return VPPCOM_EINVAL;
2592 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
2594 return VPPCOM_EBADFD;
2596 if (PREDICT_FALSE (!vep_session->is_vep))
2598 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
2599 getpid (), vep_handle);
2600 return VPPCOM_EINVAL;
2603 memset (events, 0, sizeof (*events) * maxevents);
2605 if (vec_len (wrk->unhandled_evts_vector))
2607 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
2609 vcl_epoll_wait_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i],
2611 if (n_evts == maxevents)
2618 vec_delete (wrk->unhandled_evts_vector, i, 0);
2621 if (vcm->cfg.use_mq_eventfd)
2622 return vppcom_epoll_wait_eventfd (wrk, events, maxevents, n_evts,
2625 return vppcom_epoll_wait_condvar (wrk, events, maxevents, n_evts,
2630 vppcom_session_attr (uint32_t session_handle, uint32_t op,
2631 void *buffer, uint32_t * buflen)
2633 vcl_worker_t *wrk = vcl_worker_get_current ();
2634 vcl_session_t *session;
2636 u32 *flags = buffer;
2637 vppcom_endpt_t *ep = buffer;
2639 session = vcl_session_get_w_handle (wrk, session_handle);
2641 return VPPCOM_EBADFD;
2645 case VPPCOM_ATTR_GET_NREAD:
2646 rv = vppcom_session_read_ready (session);
2647 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_NREAD: sid %u, nread = %d",
2651 case VPPCOM_ATTR_GET_NWRITE:
2652 rv = vppcom_session_write_ready (session);
2653 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_NWRITE: sid %u, nwrite = %d",
2654 getpid (), session_handle, rv);
2657 case VPPCOM_ATTR_GET_FLAGS:
2658 if (PREDICT_TRUE (buffer && buflen && (*buflen >= sizeof (*flags))))
2660 *flags = O_RDWR | (VCL_SESS_ATTR_TEST (session->attr,
2661 VCL_SESS_ATTR_NONBLOCK));
2662 *buflen = sizeof (*flags);
2663 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_FLAGS: sid %u, flags = 0x%08x, "
2664 "is_nonblocking = %u", getpid (),
2665 session_handle, *flags,
2666 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK));
2672 case VPPCOM_ATTR_SET_FLAGS:
2673 if (PREDICT_TRUE (buffer && buflen && (*buflen == sizeof (*flags))))
2675 if (*flags & O_NONBLOCK)
2676 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK);
2678 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_NONBLOCK);
2680 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_FLAGS: sid %u, flags = 0x%08x,"
2681 " is_nonblocking = %u",
2682 getpid (), session_handle, *flags,
2683 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK));
2689 case VPPCOM_ATTR_GET_PEER_ADDR:
2690 if (PREDICT_TRUE (buffer && buflen &&
2691 (*buflen >= sizeof (*ep)) && ep->ip))
2693 ep->is_ip4 = session->transport.is_ip4;
2694 ep->port = session->transport.rmt_port;
2695 if (session->transport.is_ip4)
2696 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
2697 sizeof (ip4_address_t));
2699 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
2700 sizeof (ip6_address_t));
2701 *buflen = sizeof (*ep);
2702 VDBG (1, "VCL<%d>: VPPCOM_ATTR_GET_PEER_ADDR: sid %u, is_ip4 = %u, "
2703 "addr = %U, port %u", getpid (),
2704 session_handle, ep->is_ip4, format_ip46_address,
2705 &session->transport.rmt_ip,
2706 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
2707 clib_net_to_host_u16 (ep->port));
2713 case VPPCOM_ATTR_GET_LCL_ADDR:
2714 if (PREDICT_TRUE (buffer && buflen &&
2715 (*buflen >= sizeof (*ep)) && ep->ip))
2717 ep->is_ip4 = session->transport.is_ip4;
2718 ep->port = session->transport.lcl_port;
2719 if (session->transport.is_ip4)
2720 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip4,
2721 sizeof (ip4_address_t));
2723 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip6,
2724 sizeof (ip6_address_t));
2725 *buflen = sizeof (*ep);
2726 VDBG (1, "VCL<%d>: VPPCOM_ATTR_GET_LCL_ADDR: sid %u, is_ip4 = %u,"
2727 " addr = %U port %d", getpid (),
2728 session_handle, ep->is_ip4, format_ip46_address,
2729 &session->transport.lcl_ip,
2730 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
2731 clib_net_to_host_u16 (ep->port));
2737 case VPPCOM_ATTR_GET_LIBC_EPFD:
2738 rv = session->libc_epfd;
2739 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd %d",
2743 case VPPCOM_ATTR_SET_LIBC_EPFD:
2744 if (PREDICT_TRUE (buffer && buflen &&
2745 (*buflen == sizeof (session->libc_epfd))))
2747 session->libc_epfd = *(int *) buffer;
2748 *buflen = sizeof (session->libc_epfd);
2750 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd %d, "
2751 "buflen %d", getpid (), session->libc_epfd, *buflen);
2757 case VPPCOM_ATTR_GET_PROTOCOL:
2758 if (buffer && buflen && (*buflen >= sizeof (int)))
2760 *(int *) buffer = session->session_type;
2761 *buflen = sizeof (int);
2763 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_PROTOCOL: %d (%s), buflen %d",
2764 getpid (), *(int *) buffer, *(int *) buffer ? "UDP" : "TCP",
2771 case VPPCOM_ATTR_GET_LISTEN:
2772 if (buffer && buflen && (*buflen >= sizeof (int)))
2774 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2775 VCL_SESS_ATTR_LISTEN);
2776 *buflen = sizeof (int);
2778 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_LISTEN: %d, buflen %d",
2779 getpid (), *(int *) buffer, *buflen);
2785 case VPPCOM_ATTR_GET_ERROR:
2786 if (buffer && buflen && (*buflen >= sizeof (int)))
2788 *(int *) buffer = 0;
2789 *buflen = sizeof (int);
2791 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_ERROR: %d, buflen %d, #VPP-TBD#",
2792 getpid (), *(int *) buffer, *buflen);
2798 case VPPCOM_ATTR_GET_TX_FIFO_LEN:
2799 if (buffer && buflen && (*buflen >= sizeof (u32)))
2803 *(size_t *) buffer = (session->sndbuf_size ? session->sndbuf_size :
2804 session->tx_fifo ? session->tx_fifo->nitems :
2805 vcm->cfg.tx_fifo_size);
2806 *buflen = sizeof (u32);
2808 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TX_FIFO_LEN: %u (0x%x), "
2809 "buflen %d, #VPP-TBD#", getpid (),
2810 *(size_t *) buffer, *(size_t *) buffer, *buflen);
2816 case VPPCOM_ATTR_SET_TX_FIFO_LEN:
2817 if (buffer && buflen && (*buflen == sizeof (u32)))
2820 session->sndbuf_size = *(u32 *) buffer;
2821 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TX_FIFO_LEN: %u (0x%x), "
2822 "buflen %d, #VPP-TBD#", getpid (),
2823 session->sndbuf_size, session->sndbuf_size, *buflen);
2829 case VPPCOM_ATTR_GET_RX_FIFO_LEN:
2830 if (buffer && buflen && (*buflen >= sizeof (u32)))
2834 *(size_t *) buffer = (session->rcvbuf_size ? session->rcvbuf_size :
2835 session->rx_fifo ? session->rx_fifo->nitems :
2836 vcm->cfg.rx_fifo_size);
2837 *buflen = sizeof (u32);
2839 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_RX_FIFO_LEN: %u (0x%x), "
2840 "buflen %d, #VPP-TBD#", getpid (),
2841 *(size_t *) buffer, *(size_t *) buffer, *buflen);
2847 case VPPCOM_ATTR_SET_RX_FIFO_LEN:
2848 if (buffer && buflen && (*buflen == sizeof (u32)))
2851 session->rcvbuf_size = *(u32 *) buffer;
2852 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_RX_FIFO_LEN: %u (0x%x), "
2853 "buflen %d, #VPP-TBD#", getpid (),
2854 session->sndbuf_size, session->sndbuf_size, *buflen);
2860 case VPPCOM_ATTR_GET_REUSEADDR:
2861 if (buffer && buflen && (*buflen >= sizeof (int)))
2864 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2865 VCL_SESS_ATTR_REUSEADDR);
2866 *buflen = sizeof (int);
2868 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_REUSEADDR: %d, "
2869 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
2875 case VPPCOM_ATTR_SET_REUSEADDR:
2876 if (buffer && buflen && (*buflen == sizeof (int)) &&
2877 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
2880 if (*(int *) buffer)
2881 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEADDR);
2883 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEADDR);
2885 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_REUSEADDR: %d, buflen %d,"
2886 " #VPP-TBD#", getpid (),
2887 VCL_SESS_ATTR_TEST (session->attr,
2888 VCL_SESS_ATTR_REUSEADDR), *buflen);
2894 case VPPCOM_ATTR_GET_REUSEPORT:
2895 if (buffer && buflen && (*buflen >= sizeof (int)))
2898 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2899 VCL_SESS_ATTR_REUSEPORT);
2900 *buflen = sizeof (int);
2902 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_REUSEPORT: %d, buflen %d,"
2903 " #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
2909 case VPPCOM_ATTR_SET_REUSEPORT:
2910 if (buffer && buflen && (*buflen == sizeof (int)) &&
2911 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
2914 if (*(int *) buffer)
2915 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEPORT);
2917 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEPORT);
2919 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_REUSEPORT: %d, buflen %d,"
2920 " #VPP-TBD#", getpid (),
2921 VCL_SESS_ATTR_TEST (session->attr,
2922 VCL_SESS_ATTR_REUSEPORT), *buflen);
2928 case VPPCOM_ATTR_GET_BROADCAST:
2929 if (buffer && buflen && (*buflen >= sizeof (int)))
2932 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2933 VCL_SESS_ATTR_BROADCAST);
2934 *buflen = sizeof (int);
2936 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_BROADCAST: %d, buflen %d,"
2937 " #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
2943 case VPPCOM_ATTR_SET_BROADCAST:
2944 if (buffer && buflen && (*buflen == sizeof (int)))
2947 if (*(int *) buffer)
2948 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_BROADCAST);
2950 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_BROADCAST);
2952 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_BROADCAST: %d, buflen %d, "
2953 "#VPP-TBD#", getpid (),
2954 VCL_SESS_ATTR_TEST (session->attr,
2955 VCL_SESS_ATTR_BROADCAST), *buflen);
2961 case VPPCOM_ATTR_GET_V6ONLY:
2962 if (buffer && buflen && (*buflen >= sizeof (int)))
2965 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2966 VCL_SESS_ATTR_V6ONLY);
2967 *buflen = sizeof (int);
2969 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_V6ONLY: %d, buflen %d, "
2970 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
2976 case VPPCOM_ATTR_SET_V6ONLY:
2977 if (buffer && buflen && (*buflen == sizeof (int)))
2980 if (*(int *) buffer)
2981 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_V6ONLY);
2983 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_V6ONLY);
2985 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_V6ONLY: %d, buflen %d, "
2986 "#VPP-TBD#", getpid (),
2987 VCL_SESS_ATTR_TEST (session->attr,
2988 VCL_SESS_ATTR_V6ONLY), *buflen);
2994 case VPPCOM_ATTR_GET_KEEPALIVE:
2995 if (buffer && buflen && (*buflen >= sizeof (int)))
2998 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2999 VCL_SESS_ATTR_KEEPALIVE);
3000 *buflen = sizeof (int);
3002 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_KEEPALIVE: %d, buflen %d, "
3003 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
3009 case VPPCOM_ATTR_SET_KEEPALIVE:
3010 if (buffer && buflen && (*buflen == sizeof (int)))
3013 if (*(int *) buffer)
3014 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_KEEPALIVE);
3016 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_KEEPALIVE);
3018 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_KEEPALIVE: %d, buflen %d, "
3019 "#VPP-TBD#", getpid (),
3020 VCL_SESS_ATTR_TEST (session->attr,
3021 VCL_SESS_ATTR_KEEPALIVE), *buflen);
3027 case VPPCOM_ATTR_GET_TCP_NODELAY:
3028 if (buffer && buflen && (*buflen >= sizeof (int)))
3031 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3032 VCL_SESS_ATTR_TCP_NODELAY);
3033 *buflen = sizeof (int);
3035 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_NODELAY: %d, buflen %d, "
3036 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
3042 case VPPCOM_ATTR_SET_TCP_NODELAY:
3043 if (buffer && buflen && (*buflen == sizeof (int)))
3046 if (*(int *) buffer)
3047 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
3049 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
3051 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_NODELAY: %d, buflen %d, "
3052 "#VPP-TBD#", getpid (),
3053 VCL_SESS_ATTR_TEST (session->attr,
3054 VCL_SESS_ATTR_TCP_NODELAY), *buflen);
3060 case VPPCOM_ATTR_GET_TCP_KEEPIDLE:
3061 if (buffer && buflen && (*buflen >= sizeof (int)))
3064 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3065 VCL_SESS_ATTR_TCP_KEEPIDLE);
3066 *buflen = sizeof (int);
3068 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d, buflen %d, "
3069 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
3075 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
3076 if (buffer && buflen && (*buflen == sizeof (int)))
3079 if (*(int *) buffer)
3080 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
3082 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
3084 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d, buflen %d, "
3085 "#VPP-TBD#", getpid (),
3086 VCL_SESS_ATTR_TEST (session->attr,
3087 VCL_SESS_ATTR_TCP_KEEPIDLE), *buflen);
3093 case VPPCOM_ATTR_GET_TCP_KEEPINTVL:
3094 if (buffer && buflen && (*buflen >= sizeof (int)))
3097 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3098 VCL_SESS_ATTR_TCP_KEEPINTVL);
3099 *buflen = sizeof (int);
3101 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_KEEPINTVL: %d, buflen %d, "
3102 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
3108 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
3109 if (buffer && buflen && (*buflen == sizeof (int)))
3112 if (*(int *) buffer)
3113 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
3115 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
3117 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d, buflen %d, "
3118 "#VPP-TBD#", getpid (),
3119 VCL_SESS_ATTR_TEST (session->attr,
3120 VCL_SESS_ATTR_TCP_KEEPINTVL), *buflen);
3126 case VPPCOM_ATTR_GET_TCP_USER_MSS:
3127 if (buffer && buflen && (*buflen >= sizeof (u32)))
3130 *(u32 *) buffer = session->user_mss;
3131 *buflen = sizeof (int);
3133 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_USER_MSS: %d, buflen %d,"
3134 " #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
3140 case VPPCOM_ATTR_SET_TCP_USER_MSS:
3141 if (buffer && buflen && (*buflen == sizeof (u32)))
3144 session->user_mss = *(u32 *) buffer;
3146 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_USER_MSS: %u, buflen %d, "
3147 "#VPP-TBD#", getpid (), session->user_mss, *buflen);
3162 vppcom_session_recvfrom (uint32_t session_handle, void *buffer,
3163 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3165 vcl_worker_t *wrk = vcl_worker_get_current ();
3167 vcl_session_t *session = 0;
3171 session = vcl_session_get_w_handle (wrk, session_handle);
3172 if (PREDICT_FALSE (!session))
3174 VDBG (0, "VCL<%d>: invalid session, sid (%u) has been closed!",
3175 getpid (), session_handle);
3176 return VPPCOM_EBADFD;
3178 ep->is_ip4 = session->transport.is_ip4;
3179 ep->port = session->transport.rmt_port;
3183 rv = vppcom_session_read (session_handle, buffer, buflen);
3184 else if (flags & MSG_PEEK)
3185 rv = vppcom_session_peek (session_handle, buffer, buflen);
3188 clib_warning ("VCL<%d>: Unsupport flags for recvfrom %d",
3190 return VPPCOM_EAFNOSUPPORT;
3195 if (session->transport.is_ip4)
3196 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
3197 sizeof (ip4_address_t));
3199 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
3200 sizeof (ip6_address_t));
3207 vppcom_session_sendto (uint32_t session_handle, void *buffer,
3208 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3211 return VPPCOM_EINVAL;
3216 return VPPCOM_EINVAL;
3221 // TBD check the flags and do the right thing
3222 VDBG (2, "VCL<%d>: handling flags 0x%u (%d) not implemented yet.",
3223 getpid (), flags, flags);
3226 return (vppcom_session_write (session_handle, buffer, buflen));
3230 vppcom_poll (vcl_poll_t * vp, uint32_t n_sids, double wait_for_time)
3232 vcl_worker_t *wrk = vcl_worker_get_current ();
3233 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
3234 u32 i, keep_trying = 1;
3235 svm_msg_q_msg_t msg;
3239 VDBG (3, "VCL<%d>: vp %p, nsids %u, wait_for_time %f",
3240 getpid (), vp, n_sids, wait_for_time);
3243 return VPPCOM_EFAULT;
3247 vcl_session_t *session;
3249 /* Dequeue all events and drop all unhandled io events */
3250 while (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0) == 0)
3252 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
3253 vcl_handle_mq_event (wrk, e);
3254 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
3256 vec_reset_length (wrk->unhandled_evts_vector);
3258 for (i = 0; i < n_sids; i++)
3260 session = vcl_session_get (wrk, vp[i].sid);
3263 vp[i].revents = POLLHUP;
3270 if (POLLIN & vp[i].events)
3272 rv = vppcom_session_read_ready (session);
3275 vp[i].revents |= POLLIN;
3282 case VPPCOM_ECONNRESET:
3283 vp[i].revents = POLLHUP;
3287 vp[i].revents = POLLERR;
3294 if (POLLOUT & vp[i].events)
3296 rv = vppcom_session_write_ready (session);
3299 vp[i].revents |= POLLOUT;
3306 case VPPCOM_ECONNRESET:
3307 vp[i].revents = POLLHUP;
3311 vp[i].revents = POLLERR;
3318 if (0) // Note "done:" label used by VCL_SESSION_LOCK_AND_GET()
3320 vp[i].revents = POLLNVAL;
3324 if (wait_for_time != -1)
3325 keep_trying = (clib_time_now (&wrk->clib_time) <= timeout) ? 1 : 0;
3327 while ((num_ev == 0) && keep_trying);
3329 if (VPPCOM_DEBUG > 3)
3331 clib_warning ("VCL<%d>: returning %d", getpid (), num_ev);
3332 for (i = 0; i < n_sids; i++)
3334 clib_warning ("VCL<%d>: vp[%d].sid %d (0x%x), .events 0x%x, "
3335 ".revents 0x%x", getpid (), i, vp[i].sid, vp[i].sid,
3336 vp[i].events, vp[i].revents);
3343 vppcom_mq_epoll_fd (void)
3345 vcl_worker_t *wrk = vcl_worker_get_current ();
3346 return wrk->mqs_epfd;
3350 vppcom_session_index (uint32_t session_handle)
3352 return session_handle & 0xFFFFFF;
3356 vppcom_worker_register (void)
3358 if (vcl_worker_alloc_and_init ())
3360 return VPPCOM_EEXIST;
3364 * fd.io coding-style-patch-verification: ON
3367 * eval: (c-set-style "gnu")