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 vcm->main_api_client_index = vcm->my_client_index;
777 VDBG (0, "sending session enable");
778 rv = vppcom_app_session_enable ();
781 VERR ("vppcom_app_session_enable() failed!");
785 VDBG (0, "sending app attach");
786 rv = vppcom_app_attach ();
789 VERR ("vppcom_app_attach() failed!");
793 VDBG (0, "app_name '%s', my_client_index %d (0x%x)",
794 app_name, vcm->my_client_index, vcm->my_client_index);
801 vppcom_app_destroy (void)
804 f64 orig_app_timeout;
806 if (vcm->my_client_index == ~0)
809 VDBG (0, "detaching from VPP, my_client_index %d (0x%x)",
810 vcm->my_client_index, vcm->my_client_index);
811 vcl_evt (VCL_EVT_DETACH, vcm);
813 vppcom_app_send_detach ();
814 orig_app_timeout = vcm->cfg.app_timeout;
815 vcm->cfg.app_timeout = 2.0;
816 rv = vcl_wait_for_app_state_change (STATE_APP_ENABLED);
817 vcm->cfg.app_timeout = orig_app_timeout;
818 if (PREDICT_FALSE (rv))
819 VDBG (0, "application detach timed out! returning %d (%s)",
820 rv, vppcom_retval_str (rv));
823 vl_client_disconnect_from_vlib ();
824 vec_free (vcm->app_name);
825 vcm->my_client_index = ~0;
826 vcm->app_state = STATE_APP_START;
830 vppcom_session_create (u8 proto, u8 is_nonblocking)
832 vcl_worker_t *wrk = vcl_worker_get_current ();
833 vcl_session_t *session;
835 session = vcl_session_alloc (wrk);
837 session->session_type = proto;
838 session->session_state = STATE_START;
839 session->vpp_handle = ~0;
840 session->is_dgram = proto == VPPCOM_PROTO_UDP;
843 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK);
845 vcl_evt (VCL_EVT_CREATE, session, session_type, session->session_state,
846 is_nonblocking, session_index);
848 VDBG (0, "created sid %u", session->session_index);
850 return vcl_session_handle (session);
854 vppcom_session_close (uint32_t session_handle)
856 vcl_worker_t *wrk = vcl_worker_get_current ();
857 vcl_session_t *session = 0;
858 u8 is_vep, is_vep_session;
859 session_state_t state;
864 session = vcl_session_get_w_handle (wrk, session_handle);
866 return VPPCOM_EBADFD;
868 is_vep = session->is_vep;
869 is_vep_session = session->is_vep_session;
870 next_sh = session->vep.next_sh;
871 vep_sh = session->vep.vep_sh;
872 state = session->session_state;
873 vpp_handle = session->vpp_handle;
875 if (VPPCOM_DEBUG > 0)
878 clib_warning ("VCL<%d>: vep_idx %u / sid %u: "
879 "closing epoll session...",
880 getpid (), session_handle, session_handle);
882 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %d: "
883 "closing session...",
884 getpid (), vpp_handle, session_handle);
889 while (next_sh != ~0)
891 rv = vppcom_epoll_ctl (session_handle, EPOLL_CTL_DEL, next_sh, 0);
892 if (PREDICT_FALSE (rv < 0))
893 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: EPOLL_CTL_DEL "
894 "vep_idx %u failed! rv %d (%s)",
895 getpid (), vpp_handle, next_sh, vep_sh,
896 rv, vppcom_retval_str (rv));
898 next_sh = session->vep.next_sh;
905 rv = vppcom_epoll_ctl (vep_sh, EPOLL_CTL_DEL, session_handle, 0);
907 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: EPOLL_CTL_DEL "
908 "vep_idx %u failed! rv %d (%s)",
909 getpid (), vpp_handle, session_handle,
910 vep_sh, rv, vppcom_retval_str (rv));
913 if (state & STATE_LISTEN)
915 rv = vppcom_session_unbind (session_handle);
916 if (PREDICT_FALSE (rv < 0))
917 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: listener unbind "
918 "failed! rv %d (%s)",
919 getpid (), vpp_handle, session_handle,
920 rv, vppcom_retval_str (rv));
922 else if (state & STATE_OPEN)
924 rv = vppcom_session_disconnect (session_handle);
925 if (PREDICT_FALSE (rv < 0))
926 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
927 "session disconnect failed! rv %d (%s)",
928 getpid (), vpp_handle, session_handle,
929 rv, vppcom_retval_str (rv));
933 if (vcl_session_is_ct (session))
935 vcl_cut_through_registration_t *ctr;
938 mq_addr = pointer_to_uword (session->our_evt_q);
939 ctr = vcl_ct_registration_lock_and_lookup (wrk, mq_addr);
941 if (ctr->epoll_evt_conn_index != ~0)
942 vcl_mq_epoll_del_evfd (wrk, ctr->epoll_evt_conn_index);
943 VDBG (0, "Removing ct registration %u",
944 vcl_ct_registration_index (wrk, ctr));
945 vcl_ct_registration_del (wrk, ctr);
946 vcl_ct_registration_lookup_del (wrk, mq_addr);
947 vcl_ct_registration_unlock (wrk);
950 if (vpp_handle != ~0)
952 vcl_session_table_del_vpp_handle (wrk, vpp_handle);
954 vcl_session_free (wrk, session);
956 if (VPPCOM_DEBUG > 0)
959 clib_warning ("VCL<%d>: vep_idx %u / sid %u: epoll session removed.",
960 getpid (), session_handle, session_handle);
962 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: session removed.",
963 getpid (), vpp_handle, session_handle);
966 vcl_evt (VCL_EVT_CLOSE, session, rv);
972 vppcom_session_bind (uint32_t session_handle, vppcom_endpt_t * ep)
974 vcl_worker_t *wrk = vcl_worker_get_current ();
975 vcl_session_t *session = 0;
978 return VPPCOM_EINVAL;
980 session = vcl_session_get_w_handle (wrk, session_handle);
982 return VPPCOM_EBADFD;
986 clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
987 "bind to an epoll session!", getpid (), session_handle);
988 return VPPCOM_EBADFD;
991 session->transport.is_ip4 = ep->is_ip4;
993 clib_memcpy_fast (&session->transport.lcl_ip.ip4, ep->ip,
994 sizeof (ip4_address_t));
996 clib_memcpy_fast (&session->transport.lcl_ip.ip6, ep->ip,
997 sizeof (ip6_address_t));
998 session->transport.lcl_port = ep->port;
1000 VDBG (0, "VCL<%d>: sid %u: binding to local %s address %U port %u, "
1001 "proto %s", getpid (), session_handle,
1002 session->transport.is_ip4 ? "IPv4" : "IPv6",
1003 format_ip46_address, &session->transport.lcl_ip,
1004 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
1005 clib_net_to_host_u16 (session->transport.lcl_port),
1006 session->session_type ? "UDP" : "TCP");
1007 vcl_evt (VCL_EVT_BIND, session);
1009 if (session->session_type == VPPCOM_PROTO_UDP)
1010 vppcom_session_listen (session_handle, 10);
1016 vppcom_session_listen (uint32_t listen_sh, uint32_t q_len)
1018 vcl_worker_t *wrk = vcl_worker_get_current ();
1019 vcl_session_t *listen_session = 0;
1020 u64 listen_vpp_handle;
1023 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
1024 if (!listen_session)
1025 return VPPCOM_EBADFD;
1027 if (q_len == 0 || q_len == ~0)
1028 q_len = vcm->cfg.listen_queue_size;
1030 if (listen_session->is_vep)
1032 clib_warning ("VCL<%d>: ERROR: sid %u: cannot listen on an "
1033 "epoll session!", getpid (), listen_sh);
1034 return VPPCOM_EBADFD;
1037 listen_vpp_handle = listen_session->vpp_handle;
1038 if (listen_session->session_state & STATE_LISTEN)
1040 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: already in listen state!",
1041 getpid (), listen_vpp_handle, listen_sh);
1045 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: sending VPP bind+listen "
1046 "request...", getpid (), listen_vpp_handle, listen_sh);
1049 * Send listen request to vpp and wait for reply
1051 vppcom_send_bind_sock (listen_session);
1052 rv = vppcom_wait_for_session_state_change (listen_session->session_index,
1054 vcm->cfg.session_timeout);
1056 if (PREDICT_FALSE (rv))
1058 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
1059 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: bind+listen failed! "
1060 "returning %d (%s)", getpid (), listen_session->vpp_handle,
1061 listen_sh, rv, vppcom_retval_str (rv));
1069 validate_args_session_accept_ (vcl_worker_t * wrk,
1070 vcl_session_t * listen_session)
1072 /* Input validation - expects spinlock on sessions_lockp */
1073 if (listen_session->is_vep)
1075 clib_warning ("VCL<%d>: ERROR: sid %u: cannot accept on an "
1076 "epoll session!", getpid (),
1077 listen_session->session_index);
1078 return VPPCOM_EBADFD;
1081 if (listen_session->session_state != STATE_LISTEN)
1083 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
1084 "not in listen state! state 0x%x (%s)", getpid (),
1085 listen_session->vpp_handle, listen_session->session_index,
1086 listen_session->session_state,
1087 vppcom_session_state_str (listen_session->session_state));
1088 return VPPCOM_EBADFD;
1094 vppcom_session_accept (uint32_t listen_session_handle, vppcom_endpt_t * ep,
1097 u32 client_session_index = ~0, listen_session_index;
1098 vcl_worker_t *wrk = vcl_worker_get_current ();
1099 session_accepted_msg_t accepted_msg;
1100 vcl_session_t *listen_session = 0;
1101 vcl_session_t *client_session = 0;
1102 svm_msg_q_t *vpp_evt_q;
1103 vcl_session_msg_t *evt;
1104 u64 listen_vpp_handle;
1105 svm_msg_q_msg_t msg;
1110 listen_session = vcl_session_get_w_handle (wrk, listen_session_handle);
1111 if (!listen_session)
1112 return VPPCOM_EBADFD;
1114 listen_session_index = listen_session->session_index;
1115 if ((rv = validate_args_session_accept_ (wrk, listen_session)))
1118 if (clib_fifo_elts (listen_session->accept_evts_fifo))
1120 clib_fifo_sub2 (listen_session->accept_evts_fifo, evt);
1121 accepted_msg = evt->accepted_msg;
1125 is_nonblocking = VCL_SESS_ATTR_TEST (listen_session->attr,
1126 VCL_SESS_ATTR_NONBLOCK);
1127 if (svm_msg_q_is_empty (wrk->app_event_queue) && is_nonblocking)
1128 return VPPCOM_EAGAIN;
1132 if (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_WAIT, 0))
1133 return VPPCOM_EAGAIN;
1135 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
1136 if (e->event_type != SESSION_CTRL_EVT_ACCEPTED)
1138 clib_warning ("discarded event: %u", e->event_type);
1139 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
1142 clib_memcpy_fast (&accepted_msg, e->data, sizeof (accepted_msg));
1143 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
1149 client_session_index = vcl_session_accepted_handler (wrk, &accepted_msg);
1150 listen_session = vcl_session_get (wrk, listen_session_index);
1151 client_session = vcl_session_get (wrk, client_session_index);
1153 if (flags & O_NONBLOCK)
1154 VCL_SESS_ATTR_SET (client_session->attr, VCL_SESS_ATTR_NONBLOCK);
1156 listen_vpp_handle = listen_session->vpp_handle;
1157 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: Got a client request! "
1158 "vpp handle 0x%llx, sid %u, flags %d, is_nonblocking %u",
1159 getpid (), listen_vpp_handle, listen_session_handle,
1160 client_session->vpp_handle, client_session_index,
1161 flags, VCL_SESS_ATTR_TEST (client_session->attr,
1162 VCL_SESS_ATTR_NONBLOCK));
1166 ep->is_ip4 = client_session->transport.is_ip4;
1167 ep->port = client_session->transport.rmt_port;
1168 if (client_session->transport.is_ip4)
1169 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip4,
1170 sizeof (ip4_address_t));
1172 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip6,
1173 sizeof (ip6_address_t));
1176 if (accepted_msg.server_event_queue_address)
1177 vpp_evt_q = uword_to_pointer (accepted_msg.vpp_event_queue_address,
1180 vpp_evt_q = client_session->vpp_evt_q;
1182 vcl_send_session_accepted_reply (vpp_evt_q, client_session->client_context,
1183 client_session->vpp_handle, 0);
1185 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: accepted vpp handle 0x%llx, "
1186 "sid %u connection from peer %s address %U port %u to local %s "
1187 "address %U port %u", getpid (), listen_vpp_handle,
1188 listen_session_handle, client_session->vpp_handle,
1189 client_session_index,
1190 client_session->transport.is_ip4 ? "IPv4" : "IPv6",
1191 format_ip46_address, &client_session->transport.rmt_ip,
1192 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
1193 clib_net_to_host_u16 (client_session->transport.rmt_port),
1194 client_session->transport.is_ip4 ? "IPv4" : "IPv6",
1195 format_ip46_address, &client_session->transport.lcl_ip,
1196 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
1197 clib_net_to_host_u16 (client_session->transport.lcl_port));
1198 vcl_evt (VCL_EVT_ACCEPT, client_session, listen_session,
1199 client_session_index);
1201 return vcl_session_handle (client_session);
1205 vppcom_session_connect (uint32_t session_handle, vppcom_endpt_t * server_ep)
1207 vcl_worker_t *wrk = vcl_worker_get_current ();
1208 vcl_session_t *session = 0;
1212 session = vcl_session_get_w_handle (wrk, session_handle);
1214 return VPPCOM_EBADFD;
1215 session_index = session->session_index;
1217 if (PREDICT_FALSE (session->is_vep))
1219 clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
1220 "connect on an epoll session!", getpid (),
1222 return VPPCOM_EBADFD;
1225 if (PREDICT_FALSE (session->session_state & CLIENT_STATE_OPEN))
1227 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: session already "
1228 "connected to %s %U port %d proto %s, state 0x%x (%s)",
1229 getpid (), session->vpp_handle, session_handle,
1230 session->transport.is_ip4 ? "IPv4" : "IPv6",
1231 format_ip46_address,
1232 &session->transport.rmt_ip, session->transport.is_ip4 ?
1233 IP46_TYPE_IP4 : IP46_TYPE_IP6,
1234 clib_net_to_host_u16 (session->transport.rmt_port),
1235 session->session_type ? "UDP" : "TCP", session->session_state,
1236 vppcom_session_state_str (session->session_state));
1240 session->transport.is_ip4 = server_ep->is_ip4;
1241 if (session->transport.is_ip4)
1242 clib_memcpy_fast (&session->transport.rmt_ip.ip4, server_ep->ip,
1243 sizeof (ip4_address_t));
1245 clib_memcpy_fast (&session->transport.rmt_ip.ip6, server_ep->ip,
1246 sizeof (ip6_address_t));
1247 session->transport.rmt_port = server_ep->port;
1249 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: connecting to server %s %U "
1251 getpid (), session->vpp_handle, session_handle,
1252 session->transport.is_ip4 ? "IPv4" : "IPv6",
1253 format_ip46_address,
1254 &session->transport.rmt_ip, session->transport.is_ip4 ?
1255 IP46_TYPE_IP4 : IP46_TYPE_IP6,
1256 clib_net_to_host_u16 (session->transport.rmt_port),
1257 session->session_type ? "UDP" : "TCP");
1260 * Send connect request and wait for reply from vpp
1262 vppcom_send_connect_sock (session);
1263 rv = vppcom_wait_for_session_state_change (session_index, STATE_CONNECT,
1264 vcm->cfg.session_timeout);
1266 session = vcl_session_get (wrk, session_index);
1268 if (PREDICT_FALSE (rv))
1270 if (VPPCOM_DEBUG > 0)
1273 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: connect "
1274 "failed! returning %d (%s)", getpid (),
1275 session->vpp_handle, session_handle, rv,
1276 vppcom_retval_str (rv));
1278 clib_warning ("VCL<%d>: no session for sid %u: connect failed! "
1279 "returning %d (%s)", getpid (),
1280 session_handle, rv, vppcom_retval_str (rv));
1284 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: connected!",
1285 getpid (), session->vpp_handle, session_handle);
1291 vcl_is_rx_evt_for_session (session_event_t * e, u32 sid, u8 is_ct)
1294 return (e->event_type == FIFO_EVENT_APP_RX
1295 && e->fifo->client_session_index == sid);
1297 return (e->event_type == SESSION_IO_EVT_CT_TX);
1301 vcl_session_is_readable (vcl_session_t * s)
1303 return ((s->session_state & STATE_OPEN)
1304 || (s->session_state == STATE_LISTEN
1305 && s->session_type == VPPCOM_PROTO_UDP));
1309 vppcom_session_read_internal (uint32_t session_handle, void *buf, int n,
1312 vcl_worker_t *wrk = vcl_worker_get_current ();
1313 int n_read = 0, rv, is_nonblocking;
1314 vcl_session_t *s = 0;
1315 svm_fifo_t *rx_fifo;
1316 svm_msg_q_msg_t msg;
1321 if (PREDICT_FALSE (!buf))
1322 return VPPCOM_EINVAL;
1324 s = vcl_session_get_w_handle (wrk, session_handle);
1325 if (PREDICT_FALSE (!s || s->is_vep))
1326 return VPPCOM_EBADFD;
1328 if (PREDICT_FALSE (!vcl_session_is_readable (s)))
1330 session_state_t state = s->session_state;
1331 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
1333 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: %s session is not open! "
1334 "state 0x%x (%s), returning %d (%s)",
1335 getpid (), s->vpp_handle, session_handle, state,
1336 vppcom_session_state_str (state), rv, vppcom_retval_str (rv));
1340 is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
1341 is_ct = vcl_session_is_ct (s);
1342 mq = is_ct ? s->our_evt_q : wrk->app_event_queue;
1343 rx_fifo = s->rx_fifo;
1346 if (svm_fifo_is_empty (rx_fifo))
1350 svm_fifo_unset_event (rx_fifo);
1351 return VPPCOM_EWOULDBLOCK;
1353 while (svm_fifo_is_empty (rx_fifo))
1355 svm_fifo_unset_event (rx_fifo);
1356 svm_msg_q_lock (mq);
1357 if (svm_msg_q_is_empty (mq))
1358 svm_msg_q_wait (mq);
1360 svm_msg_q_sub_w_lock (mq, &msg);
1361 e = svm_msg_q_msg_data (mq, &msg);
1362 svm_msg_q_unlock (mq);
1363 if (!vcl_is_rx_evt_for_session (e, s->session_index, is_ct))
1365 vcl_handle_mq_event (wrk, e);
1366 svm_msg_q_free_msg (mq, &msg);
1369 svm_msg_q_free_msg (mq, &msg);
1371 if (PREDICT_FALSE (s->session_state == STATE_CLOSE_ON_EMPTY))
1377 n_read = app_recv_dgram_raw (rx_fifo, buf, n, &s->transport, 0, peek);
1379 n_read = app_recv_stream_raw (rx_fifo, buf, n, 0, peek);
1381 if (svm_fifo_is_empty (rx_fifo))
1382 svm_fifo_unset_event (rx_fifo);
1384 if (is_ct && svm_fifo_want_tx_evt (rx_fifo))
1386 svm_fifo_set_want_tx_evt (s->rx_fifo, 0);
1387 app_send_io_evt_to_vpp (s->vpp_evt_q, s->rx_fifo, SESSION_IO_EVT_CT_RX,
1391 VDBG (2, "VCL<%d>: vpp handle 0x%llx, sid %u: read %d bytes from (%p)",
1392 getpid (), s->vpp_handle, session_handle, n_read, rx_fifo);
1398 vppcom_session_read (uint32_t session_handle, void *buf, size_t n)
1400 return (vppcom_session_read_internal (session_handle, buf, n, 0));
1404 vppcom_session_peek (uint32_t session_handle, void *buf, int n)
1406 return (vppcom_session_read_internal (session_handle, buf, n, 1));
1410 vppcom_session_read_segments (uint32_t session_handle,
1411 vppcom_data_segments_t ds)
1413 vcl_worker_t *wrk = vcl_worker_get_current ();
1414 int n_read = 0, rv, is_nonblocking;
1415 vcl_session_t *s = 0;
1416 svm_fifo_t *rx_fifo;
1417 svm_msg_q_msg_t msg;
1422 s = vcl_session_get_w_handle (wrk, session_handle);
1423 if (PREDICT_FALSE (!s || s->is_vep))
1424 return VPPCOM_EBADFD;
1426 if (PREDICT_FALSE (!vcl_session_is_readable (s)))
1428 session_state_t state = s->session_state;
1429 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
1433 is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
1434 is_ct = vcl_session_is_ct (s);
1435 mq = is_ct ? s->our_evt_q : wrk->app_event_queue;
1436 rx_fifo = s->rx_fifo;
1439 if (svm_fifo_is_empty (rx_fifo))
1443 svm_fifo_unset_event (rx_fifo);
1444 return VPPCOM_EWOULDBLOCK;
1446 while (svm_fifo_is_empty (rx_fifo))
1448 svm_fifo_unset_event (rx_fifo);
1449 svm_msg_q_lock (mq);
1450 if (svm_msg_q_is_empty (mq))
1451 svm_msg_q_wait (mq);
1453 svm_msg_q_sub_w_lock (mq, &msg);
1454 e = svm_msg_q_msg_data (mq, &msg);
1455 svm_msg_q_unlock (mq);
1456 if (!vcl_is_rx_evt_for_session (e, s->session_index, is_ct))
1458 vcl_handle_mq_event (wrk, e);
1459 svm_msg_q_free_msg (mq, &msg);
1462 svm_msg_q_free_msg (mq, &msg);
1464 if (PREDICT_FALSE (s->session_state == STATE_CLOSE_ON_EMPTY))
1469 n_read = svm_fifo_segments (rx_fifo, (svm_fifo_segment_t *) ds);
1470 svm_fifo_unset_event (rx_fifo);
1472 if (is_ct && n_read + svm_fifo_max_dequeue (rx_fifo) == rx_fifo->nitems)
1474 /* If the peer is not polling send notification */
1475 if (!svm_fifo_has_event (s->rx_fifo))
1476 app_send_io_evt_to_vpp (s->vpp_evt_q, s->rx_fifo,
1477 SESSION_IO_EVT_CT_RX, SVM_Q_WAIT);
1484 vppcom_session_free_segments (uint32_t session_handle,
1485 vppcom_data_segments_t ds)
1487 vcl_worker_t *wrk = vcl_worker_get_current ();
1490 s = vcl_session_get_w_handle (wrk, session_handle);
1491 if (PREDICT_FALSE (!s || s->is_vep))
1494 svm_fifo_segments_free (s->rx_fifo, (svm_fifo_segment_t *) ds);
1498 vppcom_session_read_ready (vcl_session_t * session)
1500 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
1501 if (PREDICT_FALSE (session->is_vep))
1503 clib_warning ("VCL<%d>: ERROR: sid %u: cannot read from an "
1504 "epoll session!", getpid (), session->session_index);
1505 return VPPCOM_EBADFD;
1508 if (PREDICT_FALSE (!(session->session_state & (STATE_OPEN | STATE_LISTEN))))
1510 session_state_t state = session->session_state;
1513 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
1515 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: session is not open!"
1516 " state 0x%x (%s), returning %d (%s)", getpid (),
1517 session->vpp_handle, session->session_index, state,
1518 vppcom_session_state_str (state), rv, vppcom_retval_str (rv));
1522 if (session->session_state & STATE_LISTEN)
1523 return clib_fifo_elts (session->accept_evts_fifo);
1525 return svm_fifo_max_dequeue (session->rx_fifo);
1529 vppcom_data_segment_copy (void *buf, vppcom_data_segments_t ds, u32 max_bytes)
1531 u32 first_copy = clib_min (ds[0].len, max_bytes);
1532 clib_memcpy_fast (buf, ds[0].data, first_copy);
1533 if (first_copy < max_bytes)
1535 clib_memcpy_fast (buf + first_copy, ds[1].data,
1536 clib_min (ds[1].len, max_bytes - first_copy));
1542 vcl_is_tx_evt_for_session (session_event_t * e, u32 sid, u8 is_ct)
1545 return (e->event_type == FIFO_EVENT_APP_TX
1546 && e->fifo->client_session_index == sid);
1548 return (e->event_type == SESSION_IO_EVT_CT_RX);
1552 vppcom_session_write (uint32_t session_handle, void *buf, size_t n)
1554 vcl_worker_t *wrk = vcl_worker_get_current ();
1555 int rv, n_write, is_nonblocking;
1556 vcl_session_t *s = 0;
1557 svm_fifo_t *tx_fifo = 0;
1558 session_evt_type_t et;
1559 svm_msg_q_msg_t msg;
1564 if (PREDICT_FALSE (!buf))
1565 return VPPCOM_EINVAL;
1567 s = vcl_session_get_w_handle (wrk, session_handle);
1568 if (PREDICT_FALSE (!s))
1569 return VPPCOM_EBADFD;
1571 if (PREDICT_FALSE (s->is_vep))
1573 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
1574 "cannot write to an epoll session!",
1575 getpid (), s->vpp_handle, session_handle);
1577 return VPPCOM_EBADFD;
1580 if (PREDICT_FALSE (!(s->session_state & STATE_OPEN)))
1582 session_state_t state = s->session_state;
1583 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
1584 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: session is not open! "
1585 "state 0x%x (%s)", getpid (), s->vpp_handle, session_handle,
1586 state, vppcom_session_state_str (state));
1590 tx_fifo = s->tx_fifo;
1591 is_ct = vcl_session_is_ct (s);
1592 is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
1593 mq = is_ct ? s->our_evt_q : wrk->app_event_queue;
1594 if (svm_fifo_is_full (tx_fifo))
1598 return VPPCOM_EWOULDBLOCK;
1600 while (svm_fifo_is_full (tx_fifo))
1602 svm_fifo_set_want_tx_evt (tx_fifo, 1);
1603 svm_msg_q_lock (mq);
1604 if (svm_msg_q_is_empty (mq))
1605 svm_msg_q_wait (mq);
1607 svm_msg_q_sub_w_lock (mq, &msg);
1608 e = svm_msg_q_msg_data (mq, &msg);
1609 svm_msg_q_unlock (mq);
1611 if (!vcl_is_tx_evt_for_session (e, s->session_index, is_ct))
1612 vcl_handle_mq_event (wrk, e);
1613 svm_msg_q_free_msg (mq, &msg);
1617 ASSERT (FIFO_EVENT_APP_TX + 1 == SESSION_IO_EVT_CT_TX);
1618 et = FIFO_EVENT_APP_TX + vcl_session_is_ct (s);
1620 n_write = app_send_dgram_raw (tx_fifo, &s->transport,
1621 s->vpp_evt_q, buf, n, et, SVM_Q_WAIT);
1623 n_write = app_send_stream_raw (tx_fifo, s->vpp_evt_q, buf, n, et,
1626 ASSERT (n_write > 0);
1628 VDBG (2, "VCL<%d>: vpp handle 0x%llx, sid %u: wrote %d bytes", getpid (),
1629 s->vpp_handle, session_handle, n_write);
1634 static vcl_session_t *
1635 vcl_ct_session_get_from_fifo (vcl_worker_t * wrk, svm_fifo_t * f, u8 type)
1638 s = vcl_session_get (wrk, f->client_session_index);
1642 if (type == 0 && s->rx_fifo == f)
1645 if (type == 1 && s->tx_fifo == f)
1648 s = vcl_session_get (wrk, f->master_session_index);
1651 if (type == 0 && s->rx_fifo == f)
1653 if (type == 1 && s->tx_fifo == f)
1660 vppcom_session_write_ready (vcl_session_t * session)
1662 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
1663 if (PREDICT_FALSE (session->is_vep))
1665 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
1666 "cannot write to an epoll session!",
1667 getpid (), session->vpp_handle, session->session_index);
1668 return VPPCOM_EBADFD;
1671 if (PREDICT_FALSE (session->session_state & STATE_LISTEN))
1673 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
1674 "cannot write to a listen session!",
1675 getpid (), session->vpp_handle, session->session_index);
1676 return VPPCOM_EBADFD;
1679 if (PREDICT_FALSE (!(session->session_state & STATE_OPEN)))
1681 session_state_t state = session->session_state;
1684 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
1685 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
1686 "session is not open! state 0x%x (%s), "
1687 "returning %d (%s)", getpid (), session->vpp_handle,
1688 session->session_index,
1689 state, vppcom_session_state_str (state),
1690 rv, vppcom_retval_str (rv));
1694 VDBG (3, "VCL<%d>: vpp handle 0x%llx, sid %u: peek %s (%p), ready = %d",
1695 getpid (), session->vpp_handle, session->session_index,
1696 session->tx_fifo, svm_fifo_max_enqueue (session->tx_fifo));
1698 return svm_fifo_max_enqueue (session->tx_fifo);
1702 vcl_mq_dequeue_batch (vcl_worker_t * wrk, svm_msg_q_t * mq)
1704 svm_msg_q_msg_t *msg;
1708 n_msgs = svm_msg_q_size (mq);
1709 for (i = 0; i < n_msgs; i++)
1711 vec_add2 (wrk->mq_msg_vector, msg, 1);
1712 svm_msg_q_sub_w_lock (mq, msg);
1717 #define vcl_fifo_rx_evt_valid_or_break(_fifo) \
1718 if (PREDICT_FALSE (svm_fifo_is_empty (_fifo))) \
1720 svm_fifo_unset_event (_fifo); \
1721 if (svm_fifo_is_empty (_fifo)) \
1726 vcl_select_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
1727 unsigned long n_bits, unsigned long *read_map,
1728 unsigned long *write_map,
1729 unsigned long *except_map, u32 * bits_set)
1731 session_disconnected_msg_t *disconnected_msg;
1732 session_connected_msg_t *connected_msg;
1733 session_accepted_msg_t *accepted_msg;
1734 vcl_session_msg_t *vcl_msg;
1735 vcl_session_t *session;
1739 switch (e->event_type)
1741 case FIFO_EVENT_APP_RX:
1742 vcl_fifo_rx_evt_valid_or_break (e->fifo);
1743 sid = e->fifo->client_session_index;
1744 session = vcl_session_get (wrk, sid);
1747 if (sid < n_bits && read_map)
1749 clib_bitmap_set_no_check (read_map, sid, 1);
1753 case FIFO_EVENT_APP_TX:
1754 sid = e->fifo->client_session_index;
1755 session = vcl_session_get (wrk, sid);
1758 if (sid < n_bits && write_map)
1760 clib_bitmap_set_no_check (write_map, sid, 1);
1764 case SESSION_IO_EVT_CT_TX:
1765 vcl_fifo_rx_evt_valid_or_break (e->fifo);
1766 session = vcl_ct_session_get_from_fifo (wrk, e->fifo, 0);
1769 sid = session->session_index;
1770 if (sid < n_bits && read_map)
1772 clib_bitmap_set_no_check (read_map, sid, 1);
1776 case SESSION_IO_EVT_CT_RX:
1777 session = vcl_ct_session_get_from_fifo (wrk, e->fifo, 1);
1780 sid = session->session_index;
1781 if (sid < n_bits && write_map)
1783 clib_bitmap_set_no_check (write_map, sid, 1);
1787 case SESSION_CTRL_EVT_ACCEPTED:
1788 accepted_msg = (session_accepted_msg_t *) e->data;
1789 handle = accepted_msg->listener_handle;
1790 session = vcl_session_table_lookup_listener (wrk, handle);
1793 clib_warning ("VCL<%d>: ERROR: couldn't find listen session:"
1794 "listener handle %llx", getpid (), handle);
1798 clib_fifo_add2 (session->accept_evts_fifo, vcl_msg);
1799 vcl_msg->accepted_msg = *accepted_msg;
1800 sid = session->session_index;
1801 if (sid < n_bits && read_map)
1803 clib_bitmap_set_no_check (read_map, sid, 1);
1807 case SESSION_CTRL_EVT_CONNECTED:
1808 connected_msg = (session_connected_msg_t *) e->data;
1809 vcl_session_connected_handler (wrk, connected_msg);
1811 case SESSION_CTRL_EVT_DISCONNECTED:
1812 disconnected_msg = (session_disconnected_msg_t *) e->data;
1813 sid = vcl_session_index_from_vpp_handle (wrk, disconnected_msg->handle);
1814 if (sid < n_bits && except_map)
1816 clib_bitmap_set_no_check (except_map, sid, 1);
1820 case SESSION_CTRL_EVT_RESET:
1821 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
1822 if (sid < n_bits && except_map)
1824 clib_bitmap_set_no_check (except_map, sid, 1);
1829 clib_warning ("unhandled: %u", e->event_type);
1835 vcl_select_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
1836 unsigned long n_bits, unsigned long *read_map,
1837 unsigned long *write_map, unsigned long *except_map,
1838 double time_to_wait, u32 * bits_set)
1840 svm_msg_q_msg_t *msg;
1844 svm_msg_q_lock (mq);
1845 if (svm_msg_q_is_empty (mq))
1849 svm_msg_q_unlock (mq);
1855 svm_msg_q_unlock (mq);
1858 else if (time_to_wait < 0)
1860 svm_msg_q_wait (mq);
1864 if (svm_msg_q_timedwait (mq, time_to_wait))
1866 svm_msg_q_unlock (mq);
1871 vcl_mq_dequeue_batch (wrk, mq);
1872 svm_msg_q_unlock (mq);
1874 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
1876 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
1877 e = svm_msg_q_msg_data (mq, msg);
1878 vcl_select_handle_mq_event (wrk, e, n_bits, read_map, write_map,
1879 except_map, bits_set);
1880 svm_msg_q_free_msg (mq, msg);
1882 vec_reset_length (wrk->mq_msg_vector);
1887 vppcom_select_condvar (vcl_worker_t * wrk, unsigned long n_bits,
1888 unsigned long *read_map, unsigned long *write_map,
1889 unsigned long *except_map, double time_to_wait,
1892 double total_wait = 0, wait_slice;
1893 vcl_cut_through_registration_t *cr;
1895 time_to_wait = (time_to_wait == -1) ? 10e9 : time_to_wait;
1896 wait_slice = wrk->cut_through_registrations ? 10e-6 : time_to_wait;
1899 vcl_ct_registration_lock (wrk);
1901 pool_foreach (cr, wrk->cut_through_registrations, ({
1902 vcl_select_handle_mq (wrk, cr->mq, n_bits, read_map, write_map, except_map,
1906 vcl_ct_registration_unlock (wrk);
1908 vcl_select_handle_mq (wrk, wrk->app_event_queue, n_bits, read_map,
1909 write_map, except_map, time_to_wait, bits_set);
1910 total_wait += wait_slice;
1914 while (total_wait < time_to_wait);
1920 vppcom_select_eventfd (vcl_worker_t * wrk, unsigned long n_bits,
1921 unsigned long *read_map, unsigned long *write_map,
1922 unsigned long *except_map, double time_to_wait,
1925 vcl_mq_evt_conn_t *mqc;
1926 int __clib_unused n_read;
1930 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
1931 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
1932 vec_len (wrk->mq_events), time_to_wait);
1933 for (i = 0; i < n_mq_evts; i++)
1935 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
1936 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
1937 vcl_select_handle_mq (wrk, mqc->mq, n_bits, read_map, write_map,
1938 except_map, 0, bits_set);
1941 return (n_mq_evts > 0 ? (int) *bits_set : 0);
1945 vppcom_select (unsigned long n_bits, unsigned long *read_map,
1946 unsigned long *write_map, unsigned long *except_map,
1947 double time_to_wait)
1949 u32 sid, minbits = clib_max (n_bits, BITS (uword)), bits_set = 0;
1950 vcl_worker_t *wrk = vcl_worker_get_current ();
1951 vcl_session_t *session = 0;
1954 ASSERT (sizeof (clib_bitmap_t) == sizeof (long int));
1956 if (n_bits && read_map)
1958 clib_bitmap_validate (wrk->rd_bitmap, minbits);
1959 clib_memcpy_fast (wrk->rd_bitmap, read_map,
1960 vec_len (wrk->rd_bitmap) * sizeof (clib_bitmap_t));
1961 memset (read_map, 0, vec_len (wrk->rd_bitmap) * sizeof (clib_bitmap_t));
1963 if (n_bits && write_map)
1965 clib_bitmap_validate (wrk->wr_bitmap, minbits);
1966 clib_memcpy_fast (wrk->wr_bitmap, write_map,
1967 vec_len (wrk->wr_bitmap) * sizeof (clib_bitmap_t));
1968 memset (write_map, 0,
1969 vec_len (wrk->wr_bitmap) * sizeof (clib_bitmap_t));
1971 if (n_bits && except_map)
1973 clib_bitmap_validate (wrk->ex_bitmap, minbits);
1974 clib_memcpy_fast (wrk->ex_bitmap, except_map,
1975 vec_len (wrk->ex_bitmap) * sizeof (clib_bitmap_t));
1976 memset (except_map, 0,
1977 vec_len (wrk->ex_bitmap) * sizeof (clib_bitmap_t));
1987 clib_bitmap_foreach (sid, wrk->wr_bitmap, ({
1988 if (!(session = vcl_session_get (wrk, sid)))
1990 VDBG (0, "VCL<%d>: session %d specified in write_map is closed.",
1992 return VPPCOM_EBADFD;
1995 rv = svm_fifo_is_full (session->tx_fifo);
1998 clib_bitmap_set_no_check (write_map, sid, 1);
2007 clib_bitmap_foreach (sid, wrk->rd_bitmap, ({
2008 if (!(session = vcl_session_get (wrk, sid)))
2010 VDBG (0, "VCL<%d>: session %d specified in write_map is closed.",
2012 return VPPCOM_EBADFD;
2015 rv = vppcom_session_read_ready (session);
2018 clib_bitmap_set_no_check (read_map, sid, 1);
2026 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
2028 vcl_select_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i], n_bits,
2029 read_map, write_map, except_map, &bits_set);
2031 vec_reset_length (wrk->unhandled_evts_vector);
2033 if (vcm->cfg.use_mq_eventfd)
2034 vppcom_select_eventfd (wrk, n_bits, read_map, write_map, except_map,
2035 time_to_wait, &bits_set);
2037 vppcom_select_condvar (wrk, n_bits, read_map, write_map, except_map,
2038 time_to_wait, &bits_set);
2044 vep_verify_epoll_chain (vcl_worker_t * wrk, u32 vep_idx)
2046 vcl_session_t *session;
2047 vppcom_epoll_t *vep;
2050 if (VPPCOM_DEBUG <= 1)
2053 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
2054 session = vcl_session_get (wrk, vep_idx);
2055 if (PREDICT_FALSE (!session))
2057 clib_warning ("VCL<%d>: ERROR: Invalid vep_idx (%u)!",
2058 getpid (), vep_idx);
2061 if (PREDICT_FALSE (!session->is_vep))
2063 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
2064 getpid (), vep_idx);
2067 vep = &session->vep;
2068 clib_warning ("VCL<%d>: vep_idx (%u): Dumping epoll chain\n"
2071 " is_vep_session = %u\n"
2072 " next_sid = 0x%x (%u)\n"
2073 " wait_cont_idx = 0x%x (%u)\n"
2074 "}\n", getpid (), vep_idx,
2075 session->is_vep, session->is_vep_session,
2076 vep->next_sh, vep->next_sh,
2077 session->wait_cont_idx, session->wait_cont_idx);
2079 for (sid = vep->next_sh; sid != ~0; sid = vep->next_sh)
2081 session = vcl_session_get (wrk, sid);
2082 if (PREDICT_FALSE (!session))
2084 clib_warning ("VCL<%d>: ERROR: Invalid sid (%u)!", getpid (), sid);
2087 if (PREDICT_FALSE (session->is_vep))
2088 clib_warning ("VCL<%d>: ERROR: sid (%u) is a vep!",
2089 getpid (), vep_idx);
2090 else if (PREDICT_FALSE (!session->is_vep_session))
2092 clib_warning ("VCL<%d>: ERROR: session (%u) "
2093 "is not a vep session!", getpid (), sid);
2096 vep = &session->vep;
2097 if (PREDICT_FALSE (vep->vep_sh != vep_idx))
2098 clib_warning ("VCL<%d>: ERROR: session (%u) vep_idx (%u) != "
2099 "vep_idx (%u)!", getpid (),
2100 sid, session->vep.vep_sh, vep_idx);
2101 if (session->is_vep_session)
2103 clib_warning ("vep_idx[%u]: sid 0x%x (%u)\n"
2105 " next_sid = 0x%x (%u)\n"
2106 " prev_sid = 0x%x (%u)\n"
2107 " vep_idx = 0x%x (%u)\n"
2108 " ev.events = 0x%x\n"
2109 " ev.data.u64 = 0x%llx\n"
2113 vep->next_sh, vep->next_sh,
2114 vep->prev_sh, vep->prev_sh,
2115 vep->vep_sh, vep->vep_sh,
2116 vep->ev.events, vep->ev.data.u64, vep->et_mask);
2121 clib_warning ("VCL<%d>: vep_idx (%u): Dump complete!\n",
2122 getpid (), vep_idx);
2126 vppcom_epoll_create (void)
2128 vcl_worker_t *wrk = vcl_worker_get_current ();
2129 vcl_session_t *vep_session;
2131 vep_session = vcl_session_alloc (wrk);
2133 vep_session->is_vep = 1;
2134 vep_session->vep.vep_sh = ~0;
2135 vep_session->vep.next_sh = ~0;
2136 vep_session->vep.prev_sh = ~0;
2137 vep_session->wait_cont_idx = ~0;
2138 vep_session->vpp_handle = ~0;
2140 vcl_evt (VCL_EVT_EPOLL_CREATE, vep_session, vep_sh);
2141 VDBG (0, "VCL<%d>: Created vep_idx %u / sid %u!",
2142 getpid (), vep_session->session_index, vep_session->session_index);
2144 return vcl_session_handle (vep_session);
2148 vppcom_epoll_ctl (uint32_t vep_handle, int op, uint32_t session_handle,
2149 struct epoll_event *event)
2151 vcl_worker_t *wrk = vcl_worker_get_current ();
2152 vcl_session_t *vep_session;
2153 vcl_session_t *session;
2156 if (vep_handle == session_handle)
2158 clib_warning ("VCL<%d>: ERROR: vep_idx == session_index (%u)!",
2159 getpid (), vep_handle);
2160 return VPPCOM_EINVAL;
2163 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
2164 if (PREDICT_FALSE (!vep_session))
2166 clib_warning ("VCL<%d>: ERROR: Invalid vep_idx (%u)!", vep_handle);
2167 return VPPCOM_EBADFD;
2169 if (PREDICT_FALSE (!vep_session->is_vep))
2171 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
2172 getpid (), vep_handle);
2173 return VPPCOM_EINVAL;
2176 ASSERT (vep_session->vep.vep_sh == ~0);
2177 ASSERT (vep_session->vep.prev_sh == ~0);
2179 session = vcl_session_get_w_handle (wrk, session_handle);
2180 if (PREDICT_FALSE (!session))
2182 VDBG (0, "VCL<%d>: ERROR: Invalid session_handle (%u)!",
2183 getpid (), session_handle);
2184 return VPPCOM_EBADFD;
2186 if (PREDICT_FALSE (session->is_vep))
2188 clib_warning ("ERROR: session_handle (%u) is a vep!", vep_handle);
2189 return VPPCOM_EINVAL;
2195 if (PREDICT_FALSE (!event))
2197 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_ADD: NULL pointer to "
2198 "epoll_event structure!", getpid ());
2199 return VPPCOM_EINVAL;
2201 if (vep_session->vep.next_sh != ~0)
2203 vcl_session_t *next_session;
2204 next_session = vcl_session_get_w_handle (wrk,
2205 vep_session->vep.next_sh);
2206 if (PREDICT_FALSE (!next_session))
2208 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_ADD: Invalid "
2209 "vep.next_sid (%u) on vep_idx (%u)!",
2210 getpid (), vep_session->vep.next_sh, vep_handle);
2211 return VPPCOM_EBADFD;
2213 ASSERT (next_session->vep.prev_sh == vep_handle);
2214 next_session->vep.prev_sh = session_handle;
2216 session->vep.next_sh = vep_session->vep.next_sh;
2217 session->vep.prev_sh = vep_handle;
2218 session->vep.vep_sh = vep_handle;
2219 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
2220 session->vep.ev = *event;
2221 session->is_vep = 0;
2222 session->is_vep_session = 1;
2223 vep_session->vep.next_sh = session_handle;
2225 VDBG (1, "VCL<%d>: EPOLL_CTL_ADD: vep_idx %u, sid %u, events 0x%x, "
2226 "data 0x%llx!", getpid (), vep_handle, session_handle,
2227 event->events, event->data.u64);
2228 vcl_evt (VCL_EVT_EPOLL_CTLADD, session, event->events, event->data.u64);
2232 if (PREDICT_FALSE (!event))
2234 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_MOD: NULL pointer to "
2235 "epoll_event structure!", getpid ());
2239 else if (PREDICT_FALSE (!session->is_vep_session))
2241 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_MOD: "
2242 "not a vep session!", getpid (), session_handle);
2246 else if (PREDICT_FALSE (session->vep.vep_sh != vep_handle))
2248 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_MOD: "
2249 "vep_idx (%u) != vep_idx (%u)!",
2250 getpid (), session_handle,
2251 session->vep.vep_sh, vep_handle);
2255 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
2256 session->vep.ev = *event;
2257 VDBG (1, "VCL<%d>: EPOLL_CTL_MOD: vep_idx %u, sid %u, events 0x%x,"
2258 " data 0x%llx!", getpid (), vep_handle, session_handle,
2259 event->events, event->data.u64);
2263 if (PREDICT_FALSE (!session->is_vep_session))
2265 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_DEL: "
2266 "not a vep session!", getpid (), session_handle);
2270 else if (PREDICT_FALSE (session->vep.vep_sh != vep_handle))
2272 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_DEL: "
2273 "vep_idx (%u) != vep_idx (%u)!",
2274 getpid (), session_handle,
2275 session->vep.vep_sh, vep_handle);
2280 vep_session->wait_cont_idx =
2281 (vep_session->wait_cont_idx == session_handle) ?
2282 session->vep.next_sh : vep_session->wait_cont_idx;
2284 if (session->vep.prev_sh == vep_handle)
2285 vep_session->vep.next_sh = session->vep.next_sh;
2288 vcl_session_t *prev_session;
2289 prev_session = vcl_session_get_w_handle (wrk, session->vep.prev_sh);
2290 if (PREDICT_FALSE (!prev_session))
2292 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_DEL: Invalid "
2293 "vep.prev_sid (%u) on sid (%u)!",
2294 getpid (), session->vep.prev_sh, session_handle);
2295 return VPPCOM_EBADFD;
2297 ASSERT (prev_session->vep.next_sh == session_handle);
2298 prev_session->vep.next_sh = session->vep.next_sh;
2300 if (session->vep.next_sh != ~0)
2302 vcl_session_t *next_session;
2303 next_session = vcl_session_get_w_handle (wrk, session->vep.next_sh);
2304 if (PREDICT_FALSE (!next_session))
2306 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_DEL: Invalid "
2307 "vep.next_sid (%u) on sid (%u)!",
2308 getpid (), session->vep.next_sh, session_handle);
2309 return VPPCOM_EBADFD;
2311 ASSERT (next_session->vep.prev_sh == session_handle);
2312 next_session->vep.prev_sh = session->vep.prev_sh;
2315 memset (&session->vep, 0, sizeof (session->vep));
2316 session->vep.next_sh = ~0;
2317 session->vep.prev_sh = ~0;
2318 session->vep.vep_sh = ~0;
2319 session->is_vep_session = 0;
2320 VDBG (1, "VCL<%d>: EPOLL_CTL_DEL: vep_idx %u, sid %u!",
2321 getpid (), vep_handle, session_handle);
2322 vcl_evt (VCL_EVT_EPOLL_CTLDEL, session, vep_sh);
2326 clib_warning ("VCL<%d>: ERROR: Invalid operation (%d)!", getpid (), op);
2330 vep_verify_epoll_chain (wrk, vep_handle);
2337 vcl_epoll_wait_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
2338 struct epoll_event *events, u32 * num_ev)
2340 session_disconnected_msg_t *disconnected_msg;
2341 session_connected_msg_t *connected_msg;
2342 session_accepted_msg_t *accepted_msg;
2343 u64 session_evt_data = ~0, handle;
2344 u32 sid = ~0, session_events;
2345 vcl_session_msg_t *vcl_msg;
2346 vcl_session_t *session;
2349 switch (e->event_type)
2351 case FIFO_EVENT_APP_RX:
2352 ASSERT (e->fifo->client_thread_index == vcl_get_worker_index ());
2353 vcl_fifo_rx_evt_valid_or_break (e->fifo);
2354 sid = e->fifo->client_session_index;
2355 session = vcl_session_get (wrk, sid);
2356 session_events = session->vep.ev.events;
2357 if (!(EPOLLIN & session->vep.ev.events) || session->has_rx_evt)
2360 events[*num_ev].events |= EPOLLIN;
2361 session_evt_data = session->vep.ev.data.u64;
2362 session->has_rx_evt = 1;
2364 case FIFO_EVENT_APP_TX:
2365 sid = e->fifo->client_session_index;
2366 session = vcl_session_get (wrk, sid);
2367 session_events = session->vep.ev.events;
2368 if (!(EPOLLOUT & session_events))
2371 events[*num_ev].events |= EPOLLOUT;
2372 session_evt_data = session->vep.ev.data.u64;
2374 case SESSION_IO_EVT_CT_TX:
2375 vcl_fifo_rx_evt_valid_or_break (e->fifo);
2376 session = vcl_ct_session_get_from_fifo (wrk, e->fifo, 0);
2377 sid = session->session_index;
2378 session_events = session->vep.ev.events;
2379 if (!(EPOLLIN & session->vep.ev.events) || session->has_rx_evt)
2382 events[*num_ev].events |= EPOLLIN;
2383 session_evt_data = session->vep.ev.data.u64;
2384 session->has_rx_evt = 1;
2386 case SESSION_IO_EVT_CT_RX:
2387 session = vcl_ct_session_get_from_fifo (wrk, e->fifo, 1);
2388 sid = session->session_index;
2389 session_events = session->vep.ev.events;
2390 if (!(EPOLLOUT & session_events))
2393 events[*num_ev].events |= EPOLLOUT;
2394 session_evt_data = session->vep.ev.data.u64;
2396 case SESSION_CTRL_EVT_ACCEPTED:
2397 accepted_msg = (session_accepted_msg_t *) e->data;
2398 handle = accepted_msg->listener_handle;
2399 session = vcl_session_table_lookup_listener (wrk, handle);
2402 clib_warning ("VCL<%d>: ERROR: couldn't find listen session:"
2403 "listener handle %llx", getpid (), handle);
2407 clib_fifo_add2 (session->accept_evts_fifo, vcl_msg);
2408 vcl_msg->accepted_msg = *accepted_msg;
2409 session_events = session->vep.ev.events;
2410 if (!(EPOLLIN & session_events))
2414 events[*num_ev].events |= EPOLLIN;
2415 session_evt_data = session->vep.ev.data.u64;
2417 case SESSION_CTRL_EVT_CONNECTED:
2418 connected_msg = (session_connected_msg_t *) e->data;
2419 vcl_session_connected_handler (wrk, connected_msg);
2420 /* Generate EPOLLOUT because there's no connected event */
2421 sid = vcl_session_index_from_vpp_handle (wrk, connected_msg->handle);
2422 session = vcl_session_get (wrk, sid);
2423 session_events = session->vep.ev.events;
2424 if (EPOLLOUT & session_events)
2427 events[*num_ev].events |= EPOLLOUT;
2428 session_evt_data = session->vep.ev.data.u64;
2431 case SESSION_CTRL_EVT_DISCONNECTED:
2432 disconnected_msg = (session_disconnected_msg_t *) e->data;
2433 sid = vcl_session_index_from_vpp_handle (wrk, disconnected_msg->handle);
2434 if (!(session = vcl_session_get (wrk, sid)))
2437 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
2438 session_evt_data = session->vep.ev.data.u64;
2439 session_events = session->vep.ev.events;
2441 case SESSION_CTRL_EVT_RESET:
2442 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
2443 if (!(session = vcl_session_get (wrk, sid)))
2446 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
2447 session_evt_data = session->vep.ev.data.u64;
2448 session_events = session->vep.ev.events;
2451 VDBG (0, "unhandled: %u", e->event_type);
2457 events[*num_ev].data.u64 = session_evt_data;
2458 if (EPOLLONESHOT & session_events)
2460 session = vcl_session_get (wrk, sid);
2461 session->vep.ev.events = 0;
2468 vcl_epoll_wait_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
2469 struct epoll_event *events, u32 maxevents,
2470 double wait_for_time, u32 * num_ev)
2472 svm_msg_q_msg_t *msg;
2476 if (vec_len (wrk->mq_msg_vector) && svm_msg_q_is_empty (mq))
2477 goto handle_dequeued;
2479 svm_msg_q_lock (mq);
2480 if (svm_msg_q_is_empty (mq))
2484 svm_msg_q_unlock (mq);
2487 else if (wait_for_time < 0)
2489 svm_msg_q_wait (mq);
2493 if (svm_msg_q_timedwait (mq, wait_for_time / 1e3))
2495 svm_msg_q_unlock (mq);
2500 vcl_mq_dequeue_batch (wrk, mq);
2501 svm_msg_q_unlock (mq);
2504 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
2506 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
2507 e = svm_msg_q_msg_data (mq, msg);
2508 if (*num_ev < maxevents)
2509 vcl_epoll_wait_handle_mq_event (wrk, e, events, num_ev);
2511 vec_add1 (wrk->unhandled_evts_vector, *e);
2512 svm_msg_q_free_msg (mq, msg);
2514 vec_reset_length (wrk->mq_msg_vector);
2520 vppcom_epoll_wait_condvar (vcl_worker_t * wrk, struct epoll_event *events,
2521 int maxevents, u32 n_evts, double wait_for_time)
2523 vcl_cut_through_registration_t *cr;
2524 double total_wait = 0, wait_slice;
2527 wait_for_time = (wait_for_time == -1) ? (double) 10e9 : wait_for_time;
2528 wait_slice = wrk->cut_through_registrations ? 10e-6 : wait_for_time;
2532 vcl_ct_registration_lock (wrk);
2534 pool_foreach (cr, wrk->cut_through_registrations, ({
2535 vcl_epoll_wait_handle_mq (wrk, cr->mq, events, maxevents, 0, &n_evts);
2538 vcl_ct_registration_unlock (wrk);
2540 rv = vcl_epoll_wait_handle_mq (wrk, wrk->app_event_queue, events,
2541 maxevents, n_evts ? 0 : wait_slice,
2544 total_wait += wait_slice;
2548 while (total_wait < wait_for_time);
2553 vppcom_epoll_wait_eventfd (vcl_worker_t * wrk, struct epoll_event *events,
2554 int maxevents, u32 n_evts, double wait_for_time)
2556 vcl_mq_evt_conn_t *mqc;
2557 int __clib_unused n_read;
2561 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
2563 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
2564 vec_len (wrk->mq_events), wait_for_time);
2565 for (i = 0; i < n_mq_evts; i++)
2567 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
2568 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
2569 vcl_epoll_wait_handle_mq (wrk, mqc->mq, events, maxevents, 0, &n_evts);
2571 if (!n_evts && n_mq_evts > 0)
2574 return (int) n_evts;
2578 vppcom_epoll_wait (uint32_t vep_handle, struct epoll_event *events,
2579 int maxevents, double wait_for_time)
2581 vcl_worker_t *wrk = vcl_worker_get_current ();
2582 vcl_session_t *vep_session;
2586 if (PREDICT_FALSE (maxevents <= 0))
2588 clib_warning ("VCL<%d>: ERROR: Invalid maxevents (%d)!",
2589 getpid (), maxevents);
2590 return VPPCOM_EINVAL;
2593 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
2595 return VPPCOM_EBADFD;
2597 if (PREDICT_FALSE (!vep_session->is_vep))
2599 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
2600 getpid (), vep_handle);
2601 return VPPCOM_EINVAL;
2604 memset (events, 0, sizeof (*events) * maxevents);
2606 if (vec_len (wrk->unhandled_evts_vector))
2608 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
2610 vcl_epoll_wait_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i],
2612 if (n_evts == maxevents)
2619 vec_delete (wrk->unhandled_evts_vector, i, 0);
2622 if (vcm->cfg.use_mq_eventfd)
2623 return vppcom_epoll_wait_eventfd (wrk, events, maxevents, n_evts,
2626 return vppcom_epoll_wait_condvar (wrk, events, maxevents, n_evts,
2631 vppcom_session_attr (uint32_t session_handle, uint32_t op,
2632 void *buffer, uint32_t * buflen)
2634 vcl_worker_t *wrk = vcl_worker_get_current ();
2635 vcl_session_t *session;
2637 u32 *flags = buffer;
2638 vppcom_endpt_t *ep = buffer;
2640 session = vcl_session_get_w_handle (wrk, session_handle);
2642 return VPPCOM_EBADFD;
2646 case VPPCOM_ATTR_GET_NREAD:
2647 rv = vppcom_session_read_ready (session);
2648 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_NREAD: sid %u, nread = %d",
2652 case VPPCOM_ATTR_GET_NWRITE:
2653 rv = vppcom_session_write_ready (session);
2654 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_NWRITE: sid %u, nwrite = %d",
2655 getpid (), session_handle, rv);
2658 case VPPCOM_ATTR_GET_FLAGS:
2659 if (PREDICT_TRUE (buffer && buflen && (*buflen >= sizeof (*flags))))
2661 *flags = O_RDWR | (VCL_SESS_ATTR_TEST (session->attr,
2662 VCL_SESS_ATTR_NONBLOCK));
2663 *buflen = sizeof (*flags);
2664 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_FLAGS: sid %u, flags = 0x%08x, "
2665 "is_nonblocking = %u", getpid (),
2666 session_handle, *flags,
2667 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK));
2673 case VPPCOM_ATTR_SET_FLAGS:
2674 if (PREDICT_TRUE (buffer && buflen && (*buflen == sizeof (*flags))))
2676 if (*flags & O_NONBLOCK)
2677 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK);
2679 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_NONBLOCK);
2681 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_FLAGS: sid %u, flags = 0x%08x,"
2682 " is_nonblocking = %u",
2683 getpid (), session_handle, *flags,
2684 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK));
2690 case VPPCOM_ATTR_GET_PEER_ADDR:
2691 if (PREDICT_TRUE (buffer && buflen &&
2692 (*buflen >= sizeof (*ep)) && ep->ip))
2694 ep->is_ip4 = session->transport.is_ip4;
2695 ep->port = session->transport.rmt_port;
2696 if (session->transport.is_ip4)
2697 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
2698 sizeof (ip4_address_t));
2700 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
2701 sizeof (ip6_address_t));
2702 *buflen = sizeof (*ep);
2703 VDBG (1, "VCL<%d>: VPPCOM_ATTR_GET_PEER_ADDR: sid %u, is_ip4 = %u, "
2704 "addr = %U, port %u", getpid (),
2705 session_handle, ep->is_ip4, format_ip46_address,
2706 &session->transport.rmt_ip,
2707 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
2708 clib_net_to_host_u16 (ep->port));
2714 case VPPCOM_ATTR_GET_LCL_ADDR:
2715 if (PREDICT_TRUE (buffer && buflen &&
2716 (*buflen >= sizeof (*ep)) && ep->ip))
2718 ep->is_ip4 = session->transport.is_ip4;
2719 ep->port = session->transport.lcl_port;
2720 if (session->transport.is_ip4)
2721 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip4,
2722 sizeof (ip4_address_t));
2724 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip6,
2725 sizeof (ip6_address_t));
2726 *buflen = sizeof (*ep);
2727 VDBG (1, "VCL<%d>: VPPCOM_ATTR_GET_LCL_ADDR: sid %u, is_ip4 = %u,"
2728 " addr = %U port %d", getpid (),
2729 session_handle, ep->is_ip4, format_ip46_address,
2730 &session->transport.lcl_ip,
2731 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
2732 clib_net_to_host_u16 (ep->port));
2738 case VPPCOM_ATTR_GET_LIBC_EPFD:
2739 rv = session->libc_epfd;
2740 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd %d",
2744 case VPPCOM_ATTR_SET_LIBC_EPFD:
2745 if (PREDICT_TRUE (buffer && buflen &&
2746 (*buflen == sizeof (session->libc_epfd))))
2748 session->libc_epfd = *(int *) buffer;
2749 *buflen = sizeof (session->libc_epfd);
2751 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd %d, "
2752 "buflen %d", getpid (), session->libc_epfd, *buflen);
2758 case VPPCOM_ATTR_GET_PROTOCOL:
2759 if (buffer && buflen && (*buflen >= sizeof (int)))
2761 *(int *) buffer = session->session_type;
2762 *buflen = sizeof (int);
2764 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_PROTOCOL: %d (%s), buflen %d",
2765 getpid (), *(int *) buffer, *(int *) buffer ? "UDP" : "TCP",
2772 case VPPCOM_ATTR_GET_LISTEN:
2773 if (buffer && buflen && (*buflen >= sizeof (int)))
2775 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2776 VCL_SESS_ATTR_LISTEN);
2777 *buflen = sizeof (int);
2779 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_LISTEN: %d, buflen %d",
2780 getpid (), *(int *) buffer, *buflen);
2786 case VPPCOM_ATTR_GET_ERROR:
2787 if (buffer && buflen && (*buflen >= sizeof (int)))
2789 *(int *) buffer = 0;
2790 *buflen = sizeof (int);
2792 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_ERROR: %d, buflen %d, #VPP-TBD#",
2793 getpid (), *(int *) buffer, *buflen);
2799 case VPPCOM_ATTR_GET_TX_FIFO_LEN:
2800 if (buffer && buflen && (*buflen >= sizeof (u32)))
2804 *(size_t *) buffer = (session->sndbuf_size ? session->sndbuf_size :
2805 session->tx_fifo ? session->tx_fifo->nitems :
2806 vcm->cfg.tx_fifo_size);
2807 *buflen = sizeof (u32);
2809 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TX_FIFO_LEN: %u (0x%x), "
2810 "buflen %d, #VPP-TBD#", getpid (),
2811 *(size_t *) buffer, *(size_t *) buffer, *buflen);
2817 case VPPCOM_ATTR_SET_TX_FIFO_LEN:
2818 if (buffer && buflen && (*buflen == sizeof (u32)))
2821 session->sndbuf_size = *(u32 *) buffer;
2822 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TX_FIFO_LEN: %u (0x%x), "
2823 "buflen %d, #VPP-TBD#", getpid (),
2824 session->sndbuf_size, session->sndbuf_size, *buflen);
2830 case VPPCOM_ATTR_GET_RX_FIFO_LEN:
2831 if (buffer && buflen && (*buflen >= sizeof (u32)))
2835 *(size_t *) buffer = (session->rcvbuf_size ? session->rcvbuf_size :
2836 session->rx_fifo ? session->rx_fifo->nitems :
2837 vcm->cfg.rx_fifo_size);
2838 *buflen = sizeof (u32);
2840 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_RX_FIFO_LEN: %u (0x%x), "
2841 "buflen %d, #VPP-TBD#", getpid (),
2842 *(size_t *) buffer, *(size_t *) buffer, *buflen);
2848 case VPPCOM_ATTR_SET_RX_FIFO_LEN:
2849 if (buffer && buflen && (*buflen == sizeof (u32)))
2852 session->rcvbuf_size = *(u32 *) buffer;
2853 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_RX_FIFO_LEN: %u (0x%x), "
2854 "buflen %d, #VPP-TBD#", getpid (),
2855 session->sndbuf_size, session->sndbuf_size, *buflen);
2861 case VPPCOM_ATTR_GET_REUSEADDR:
2862 if (buffer && buflen && (*buflen >= sizeof (int)))
2865 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2866 VCL_SESS_ATTR_REUSEADDR);
2867 *buflen = sizeof (int);
2869 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_REUSEADDR: %d, "
2870 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
2876 case VPPCOM_ATTR_SET_REUSEADDR:
2877 if (buffer && buflen && (*buflen == sizeof (int)) &&
2878 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
2881 if (*(int *) buffer)
2882 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEADDR);
2884 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEADDR);
2886 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_REUSEADDR: %d, buflen %d,"
2887 " #VPP-TBD#", getpid (),
2888 VCL_SESS_ATTR_TEST (session->attr,
2889 VCL_SESS_ATTR_REUSEADDR), *buflen);
2895 case VPPCOM_ATTR_GET_REUSEPORT:
2896 if (buffer && buflen && (*buflen >= sizeof (int)))
2899 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2900 VCL_SESS_ATTR_REUSEPORT);
2901 *buflen = sizeof (int);
2903 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_REUSEPORT: %d, buflen %d,"
2904 " #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
2910 case VPPCOM_ATTR_SET_REUSEPORT:
2911 if (buffer && buflen && (*buflen == sizeof (int)) &&
2912 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
2915 if (*(int *) buffer)
2916 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEPORT);
2918 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEPORT);
2920 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_REUSEPORT: %d, buflen %d,"
2921 " #VPP-TBD#", getpid (),
2922 VCL_SESS_ATTR_TEST (session->attr,
2923 VCL_SESS_ATTR_REUSEPORT), *buflen);
2929 case VPPCOM_ATTR_GET_BROADCAST:
2930 if (buffer && buflen && (*buflen >= sizeof (int)))
2933 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2934 VCL_SESS_ATTR_BROADCAST);
2935 *buflen = sizeof (int);
2937 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_BROADCAST: %d, buflen %d,"
2938 " #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
2944 case VPPCOM_ATTR_SET_BROADCAST:
2945 if (buffer && buflen && (*buflen == sizeof (int)))
2948 if (*(int *) buffer)
2949 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_BROADCAST);
2951 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_BROADCAST);
2953 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_BROADCAST: %d, buflen %d, "
2954 "#VPP-TBD#", getpid (),
2955 VCL_SESS_ATTR_TEST (session->attr,
2956 VCL_SESS_ATTR_BROADCAST), *buflen);
2962 case VPPCOM_ATTR_GET_V6ONLY:
2963 if (buffer && buflen && (*buflen >= sizeof (int)))
2966 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2967 VCL_SESS_ATTR_V6ONLY);
2968 *buflen = sizeof (int);
2970 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_V6ONLY: %d, buflen %d, "
2971 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
2977 case VPPCOM_ATTR_SET_V6ONLY:
2978 if (buffer && buflen && (*buflen == sizeof (int)))
2981 if (*(int *) buffer)
2982 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_V6ONLY);
2984 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_V6ONLY);
2986 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_V6ONLY: %d, buflen %d, "
2987 "#VPP-TBD#", getpid (),
2988 VCL_SESS_ATTR_TEST (session->attr,
2989 VCL_SESS_ATTR_V6ONLY), *buflen);
2995 case VPPCOM_ATTR_GET_KEEPALIVE:
2996 if (buffer && buflen && (*buflen >= sizeof (int)))
2999 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3000 VCL_SESS_ATTR_KEEPALIVE);
3001 *buflen = sizeof (int);
3003 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_KEEPALIVE: %d, buflen %d, "
3004 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
3010 case VPPCOM_ATTR_SET_KEEPALIVE:
3011 if (buffer && buflen && (*buflen == sizeof (int)))
3014 if (*(int *) buffer)
3015 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_KEEPALIVE);
3017 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_KEEPALIVE);
3019 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_KEEPALIVE: %d, buflen %d, "
3020 "#VPP-TBD#", getpid (),
3021 VCL_SESS_ATTR_TEST (session->attr,
3022 VCL_SESS_ATTR_KEEPALIVE), *buflen);
3028 case VPPCOM_ATTR_GET_TCP_NODELAY:
3029 if (buffer && buflen && (*buflen >= sizeof (int)))
3032 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3033 VCL_SESS_ATTR_TCP_NODELAY);
3034 *buflen = sizeof (int);
3036 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_NODELAY: %d, buflen %d, "
3037 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
3043 case VPPCOM_ATTR_SET_TCP_NODELAY:
3044 if (buffer && buflen && (*buflen == sizeof (int)))
3047 if (*(int *) buffer)
3048 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
3050 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
3052 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_NODELAY: %d, buflen %d, "
3053 "#VPP-TBD#", getpid (),
3054 VCL_SESS_ATTR_TEST (session->attr,
3055 VCL_SESS_ATTR_TCP_NODELAY), *buflen);
3061 case VPPCOM_ATTR_GET_TCP_KEEPIDLE:
3062 if (buffer && buflen && (*buflen >= sizeof (int)))
3065 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3066 VCL_SESS_ATTR_TCP_KEEPIDLE);
3067 *buflen = sizeof (int);
3069 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d, buflen %d, "
3070 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
3076 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
3077 if (buffer && buflen && (*buflen == sizeof (int)))
3080 if (*(int *) buffer)
3081 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
3083 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
3085 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d, buflen %d, "
3086 "#VPP-TBD#", getpid (),
3087 VCL_SESS_ATTR_TEST (session->attr,
3088 VCL_SESS_ATTR_TCP_KEEPIDLE), *buflen);
3094 case VPPCOM_ATTR_GET_TCP_KEEPINTVL:
3095 if (buffer && buflen && (*buflen >= sizeof (int)))
3098 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3099 VCL_SESS_ATTR_TCP_KEEPINTVL);
3100 *buflen = sizeof (int);
3102 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_KEEPINTVL: %d, buflen %d, "
3103 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
3109 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
3110 if (buffer && buflen && (*buflen == sizeof (int)))
3113 if (*(int *) buffer)
3114 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
3116 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
3118 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d, buflen %d, "
3119 "#VPP-TBD#", getpid (),
3120 VCL_SESS_ATTR_TEST (session->attr,
3121 VCL_SESS_ATTR_TCP_KEEPINTVL), *buflen);
3127 case VPPCOM_ATTR_GET_TCP_USER_MSS:
3128 if (buffer && buflen && (*buflen >= sizeof (u32)))
3131 *(u32 *) buffer = session->user_mss;
3132 *buflen = sizeof (int);
3134 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_USER_MSS: %d, buflen %d,"
3135 " #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
3141 case VPPCOM_ATTR_SET_TCP_USER_MSS:
3142 if (buffer && buflen && (*buflen == sizeof (u32)))
3145 session->user_mss = *(u32 *) buffer;
3147 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_USER_MSS: %u, buflen %d, "
3148 "#VPP-TBD#", getpid (), session->user_mss, *buflen);
3163 vppcom_session_recvfrom (uint32_t session_handle, void *buffer,
3164 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3166 vcl_worker_t *wrk = vcl_worker_get_current ();
3168 vcl_session_t *session = 0;
3172 session = vcl_session_get_w_handle (wrk, session_handle);
3173 if (PREDICT_FALSE (!session))
3175 VDBG (0, "VCL<%d>: invalid session, sid (%u) has been closed!",
3176 getpid (), session_handle);
3177 return VPPCOM_EBADFD;
3179 ep->is_ip4 = session->transport.is_ip4;
3180 ep->port = session->transport.rmt_port;
3184 rv = vppcom_session_read (session_handle, buffer, buflen);
3185 else if (flags & MSG_PEEK)
3186 rv = vppcom_session_peek (session_handle, buffer, buflen);
3189 clib_warning ("VCL<%d>: Unsupport flags for recvfrom %d",
3191 return VPPCOM_EAFNOSUPPORT;
3196 if (session->transport.is_ip4)
3197 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
3198 sizeof (ip4_address_t));
3200 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
3201 sizeof (ip6_address_t));
3208 vppcom_session_sendto (uint32_t session_handle, void *buffer,
3209 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3212 return VPPCOM_EINVAL;
3217 return VPPCOM_EINVAL;
3222 // TBD check the flags and do the right thing
3223 VDBG (2, "VCL<%d>: handling flags 0x%u (%d) not implemented yet.",
3224 getpid (), flags, flags);
3227 return (vppcom_session_write (session_handle, buffer, buflen));
3231 vppcom_poll (vcl_poll_t * vp, uint32_t n_sids, double wait_for_time)
3233 vcl_worker_t *wrk = vcl_worker_get_current ();
3234 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
3235 u32 i, keep_trying = 1;
3236 svm_msg_q_msg_t msg;
3240 VDBG (3, "VCL<%d>: vp %p, nsids %u, wait_for_time %f",
3241 getpid (), vp, n_sids, wait_for_time);
3244 return VPPCOM_EFAULT;
3248 vcl_session_t *session;
3250 /* Dequeue all events and drop all unhandled io events */
3251 while (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0) == 0)
3253 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
3254 vcl_handle_mq_event (wrk, e);
3255 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
3257 vec_reset_length (wrk->unhandled_evts_vector);
3259 for (i = 0; i < n_sids; i++)
3261 session = vcl_session_get (wrk, vp[i].sid);
3264 vp[i].revents = POLLHUP;
3271 if (POLLIN & vp[i].events)
3273 rv = vppcom_session_read_ready (session);
3276 vp[i].revents |= POLLIN;
3283 case VPPCOM_ECONNRESET:
3284 vp[i].revents = POLLHUP;
3288 vp[i].revents = POLLERR;
3295 if (POLLOUT & vp[i].events)
3297 rv = vppcom_session_write_ready (session);
3300 vp[i].revents |= POLLOUT;
3307 case VPPCOM_ECONNRESET:
3308 vp[i].revents = POLLHUP;
3312 vp[i].revents = POLLERR;
3319 if (0) // Note "done:" label used by VCL_SESSION_LOCK_AND_GET()
3321 vp[i].revents = POLLNVAL;
3325 if (wait_for_time != -1)
3326 keep_trying = (clib_time_now (&wrk->clib_time) <= timeout) ? 1 : 0;
3328 while ((num_ev == 0) && keep_trying);
3330 if (VPPCOM_DEBUG > 3)
3332 clib_warning ("VCL<%d>: returning %d", getpid (), num_ev);
3333 for (i = 0; i < n_sids; i++)
3335 clib_warning ("VCL<%d>: vp[%d].sid %d (0x%x), .events 0x%x, "
3336 ".revents 0x%x", getpid (), i, vp[i].sid, vp[i].sid,
3337 vp[i].events, vp[i].revents);
3344 vppcom_mq_epoll_fd (void)
3346 vcl_worker_t *wrk = vcl_worker_get_current ();
3347 return wrk->mqs_epfd;
3351 vppcom_session_index (uint32_t session_handle)
3353 return session_handle & 0xFFFFFF;
3357 vppcom_worker_register (void)
3359 if (vcl_worker_alloc_and_init ())
3361 return VPPCOM_EEXIST;
3365 * fd.io coding-style-patch-verification: ON
3368 * eval: (c-set-style "gnu")