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 (&session->transport.rmt_ip, mp->ip, sizeof (ip46_address_t));
316 vcl_session_table_add_vpp_handle (wrk, mp->handle, session->session_index);
317 session->transport.lcl_port = listen_session->transport.lcl_port;
318 session->transport.lcl_ip = listen_session->transport.lcl_ip;
319 session->session_type = listen_session->session_type;
320 session->is_dgram = session->session_type == VPPCOM_PROTO_UDP;
322 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: client accept request from %s"
323 " address %U port %d queue %p!", getpid (), mp->handle,
324 session->session_index,
325 mp->is_ip4 ? "IPv4" : "IPv6", format_ip46_address, &mp->ip,
326 mp->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
327 clib_net_to_host_u16 (mp->port), session->vpp_evt_q);
328 vcl_evt (VCL_EVT_ACCEPT, session, listen_session, session_index);
330 return session->session_index;
334 vcl_session_connected_handler (vcl_worker_t * wrk,
335 session_connected_msg_t * mp)
337 u32 session_index, vpp_wrk_index;
338 svm_fifo_t *rx_fifo, *tx_fifo;
339 vcl_session_t *session = 0;
342 session_index = mp->context;
343 session = vcl_session_get (wrk, session_index);
346 clib_warning ("[%s] ERROR: vpp handle 0x%llx, sid %u: "
347 "Invalid session index (%u)!",
348 getpid (), mp->handle, session_index);
349 return VCL_INVALID_SESSION_INDEX;
353 clib_warning ("VCL<%d>: ERROR: sid %u: connect failed! %U", getpid (),
354 session_index, format_api_error, ntohl (mp->retval));
355 session->session_state = STATE_FAILED;
356 session->vpp_handle = mp->handle;
357 return session_index;
360 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
361 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
362 vcl_wait_for_memory (rx_fifo);
363 rx_fifo->client_session_index = session_index;
364 tx_fifo->client_session_index = session_index;
365 rx_fifo->client_thread_index = vcl_get_worker_index ();
366 tx_fifo->client_thread_index = vcl_get_worker_index ();
368 if (mp->client_event_queue_address)
370 session->vpp_evt_q = uword_to_pointer (mp->server_event_queue_address,
372 session->our_evt_q = uword_to_pointer (mp->client_event_queue_address,
375 vec_validate (wrk->vpp_event_queues, 0);
376 evt_q = uword_to_pointer (mp->vpp_event_queue_address, svm_msg_q_t *);
377 wrk->vpp_event_queues[0] = evt_q;
381 session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address,
383 vpp_wrk_index = tx_fifo->master_thread_index;
384 vec_validate (wrk->vpp_event_queues, vpp_wrk_index);
385 wrk->vpp_event_queues[vpp_wrk_index] = session->vpp_evt_q;
388 session->rx_fifo = rx_fifo;
389 session->tx_fifo = tx_fifo;
390 session->vpp_handle = mp->handle;
391 session->transport.is_ip4 = mp->is_ip4;
392 clib_memcpy (&session->transport.lcl_ip, mp->lcl_ip,
393 sizeof (session->transport.lcl_ip));
394 session->transport.lcl_port = mp->lcl_port;
395 session->session_state = STATE_CONNECT;
397 /* Add it to lookup table */
398 hash_set (wrk->session_index_by_vpp_handles, mp->handle, session_index);
400 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: connect succeeded! "
401 "session_rx_fifo %p, refcnt %d, session_tx_fifo %p, refcnt %d",
402 getpid (), mp->handle, session_index, session->rx_fifo,
403 session->rx_fifo->refcnt, session->tx_fifo, session->tx_fifo->refcnt);
405 return session_index;
409 vcl_session_reset_handler (vcl_worker_t * wrk,
410 session_reset_msg_t * reset_msg)
412 vcl_session_t *session;
415 sid = vcl_session_index_from_vpp_handle (wrk, reset_msg->handle);
416 session = vcl_session_get (wrk, sid);
419 VDBG (0, "request to reset unknown handle 0x%llx", reset_msg->handle);
420 return VCL_INVALID_SESSION_INDEX;
422 session->session_state = STATE_CLOSE_ON_EMPTY;
423 VDBG (0, "reset handle 0x%llx, sid %u ", reset_msg->handle, sid);
424 vcl_send_session_reset_reply (vcl_session_vpp_evt_q (wrk, session),
425 vcm->my_client_index, reset_msg->handle, 0);
430 vcl_session_bound_handler (vcl_worker_t * wrk, session_bound_msg_t * mp)
432 vcl_session_t *session;
433 u32 sid = mp->context;
435 session = vcl_session_get (wrk, sid);
438 VDBG (0, "VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: bind failed: %U",
439 getpid (), mp->handle, sid, format_api_error, ntohl (mp->retval));
442 session->session_state = STATE_FAILED;
443 session->vpp_handle = mp->handle;
448 clib_warning ("[%s] ERROR: vpp handle 0x%llx, sid %u: "
449 "Invalid session index (%u)!",
450 getpid (), mp->handle, sid);
451 return VCL_INVALID_SESSION_INDEX;
455 session->vpp_handle = mp->handle;
456 session->transport.is_ip4 = mp->lcl_is_ip4;
457 clib_memcpy (&session->transport.lcl_ip, mp->lcl_ip,
458 sizeof (ip46_address_t));
459 session->transport.lcl_port = mp->lcl_port;
460 vcl_session_table_add_listener (wrk, mp->handle, sid);
461 session->session_state = STATE_LISTEN;
463 if (session->is_dgram)
465 svm_fifo_t *rx_fifo, *tx_fifo;
466 session->vpp_evt_q = uword_to_pointer (mp->vpp_evt_q, svm_msg_q_t *);
467 rx_fifo = uword_to_pointer (mp->rx_fifo, svm_fifo_t *);
468 rx_fifo->client_session_index = sid;
469 tx_fifo = uword_to_pointer (mp->tx_fifo, svm_fifo_t *);
470 tx_fifo->client_session_index = sid;
471 session->rx_fifo = rx_fifo;
472 session->tx_fifo = tx_fifo;
475 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: bind succeeded!",
476 getpid (), mp->handle, sid);
481 vcl_handle_mq_event (vcl_worker_t * wrk, session_event_t * e)
483 session_accepted_msg_t *accepted_msg;
484 session_disconnected_msg_t *disconnected_msg;
485 vcl_session_msg_t *vcl_msg;
486 vcl_session_t *session;
490 switch (e->event_type)
492 case FIFO_EVENT_APP_RX:
493 case FIFO_EVENT_APP_TX:
494 case SESSION_IO_EVT_CT_RX:
495 case SESSION_IO_EVT_CT_TX:
496 vec_add1 (wrk->unhandled_evts_vector, *e);
498 case SESSION_CTRL_EVT_ACCEPTED:
499 accepted_msg = (session_accepted_msg_t *) e->data;
500 handle = accepted_msg->listener_handle;
501 session = vcl_session_table_lookup_listener (wrk, handle);
504 clib_warning ("VCL<%d>: ERROR: couldn't find listen session:"
505 "listener handle %llx", getpid (), handle);
509 clib_fifo_add2 (session->accept_evts_fifo, vcl_msg);
510 vcl_msg->accepted_msg = *accepted_msg;
512 case SESSION_CTRL_EVT_CONNECTED:
513 vcl_session_connected_handler (wrk,
514 (session_connected_msg_t *) e->data);
516 case SESSION_CTRL_EVT_DISCONNECTED:
517 disconnected_msg = (session_disconnected_msg_t *) e->data;
518 sid = vcl_session_index_from_vpp_handle (wrk, disconnected_msg->handle);
519 session = vcl_session_get (wrk, sid);
522 VDBG (0, "request to disconnect unknown handle 0x%llx",
523 disconnected_msg->handle);
526 session->session_state = STATE_DISCONNECT;
527 VDBG (0, "disconnected handle 0x%llx, sid %u", disconnected_msg->handle,
530 case SESSION_CTRL_EVT_RESET:
531 vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
533 case SESSION_CTRL_EVT_BOUND:
534 vcl_session_bound_handler (wrk, (session_bound_msg_t *) e->data);
537 clib_warning ("unhandled %u", e->event_type);
543 vppcom_wait_for_session_state_change (u32 session_index,
544 session_state_t state,
547 vcl_worker_t *wrk = vcl_worker_get_current ();
548 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
549 vcl_session_t *volatile session;
555 session = vcl_session_get (wrk, session_index);
556 if (PREDICT_FALSE (!session))
558 return VPPCOM_EBADFD;
560 if (session->session_state & state)
564 if (session->session_state & STATE_FAILED)
566 return VPPCOM_ECONNREFUSED;
569 if (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0))
571 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
572 vcl_handle_mq_event (wrk, e);
573 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
575 while (clib_time_now (&wrk->clib_time) < timeout);
577 VDBG (0, "VCL<%d>: timeout waiting for state 0x%x (%s)", getpid (), state,
578 vppcom_session_state_str (state));
579 vcl_evt (VCL_EVT_SESSION_TIMEOUT, session, session_state);
581 return VPPCOM_ETIMEDOUT;
585 vppcom_app_session_enable (void)
589 if (vcm->app_state != STATE_APP_ENABLED)
591 vppcom_send_session_enable_disable (1 /* is_enabled == TRUE */ );
592 rv = vcl_wait_for_app_state_change (STATE_APP_ENABLED);
593 if (PREDICT_FALSE (rv))
595 VDBG (0, "VCL<%d>: application session enable timed out! "
596 "returning %d (%s)", getpid (), rv, vppcom_retval_str (rv));
604 vppcom_app_attach (void)
608 vppcom_app_send_attach ();
609 rv = vcl_wait_for_app_state_change (STATE_APP_ATTACHED);
610 if (PREDICT_FALSE (rv))
612 VDBG (0, "VCL<%d>: application attach timed out! returning %d (%s)",
613 getpid (), rv, vppcom_retval_str (rv));
621 vppcom_session_unbind (u32 session_handle)
623 vcl_worker_t *wrk = vcl_worker_get_current ();
624 vcl_session_t *session = 0;
627 session = vcl_session_get_w_handle (wrk, session_handle);
629 return VPPCOM_EBADFD;
631 vpp_handle = session->vpp_handle;
632 vcl_session_table_del_listener (wrk, vpp_handle);
633 session->vpp_handle = ~0;
634 session->session_state = STATE_DISCONNECT;
636 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: sending unbind msg! new state"
637 " 0x%x (%s)", getpid (), vpp_handle, session_handle, STATE_DISCONNECT,
638 vppcom_session_state_str (STATE_DISCONNECT));
639 vcl_evt (VCL_EVT_UNBIND, session);
640 vppcom_send_unbind_sock (vpp_handle);
646 vppcom_session_disconnect (u32 session_handle)
648 vcl_worker_t *wrk = vcl_worker_get_current ();
649 svm_msg_q_t *vpp_evt_q;
650 vcl_session_t *session;
651 session_state_t state;
654 session = vcl_session_get_w_handle (wrk, session_handle);
656 return VPPCOM_EBADFD;
658 vpp_handle = session->vpp_handle;
659 state = session->session_state;
661 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u state 0x%x (%s)", getpid (),
662 vpp_handle, session_handle, state, vppcom_session_state_str (state));
664 if (PREDICT_FALSE (state & STATE_LISTEN))
666 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
667 "Cannot disconnect a listen socket!",
668 getpid (), vpp_handle, session_handle);
669 return VPPCOM_EBADFD;
672 if (state & STATE_CLOSE_ON_EMPTY)
674 vpp_evt_q = vcl_session_vpp_evt_q (wrk, session);
675 vcl_send_session_disconnected_reply (vpp_evt_q, vcm->my_client_index,
677 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: sending disconnect "
678 "REPLY...", getpid (), vpp_handle, session_handle);
682 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: sending disconnect...",
683 getpid (), vpp_handle, session_handle);
684 vppcom_send_disconnect_session (vpp_handle);
691 * VPPCOM Public API functions
694 vppcom_app_create (char *app_name)
696 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
702 vppcom_cfg (&vcm->cfg);
705 vcm->main_cpu = pthread_self ();
706 vppcom_init_error_string_table ();
707 svm_fifo_segment_main_init (vcl_cfg->segment_baseva,
708 20 /* timeout in secs */ );
709 pool_init_fixed (vcm->workers, vcl_cfg->max_workers);
710 clib_spinlock_init (&vcm->workers_lock);
711 vcl_worker_alloc_and_init ();
714 if (vcm->my_client_index == ~0)
716 /* API hookup and connect to VPP */
717 vppcom_api_hookup ();
719 vcm->app_state = STATE_APP_START;
720 rv = vppcom_connect_to_vpp (app_name);
723 clib_warning ("VCL<%d>: ERROR: couldn't connect to VPP!",
728 VDBG (0, "VCL<%d>: sending session enable", getpid ());
729 rv = vppcom_app_session_enable ();
732 clib_warning ("VCL<%d>: ERROR: vppcom_app_session_enable() "
733 "failed!", getpid ());
737 VDBG (0, "VCL<%d>: sending app attach", getpid ());
738 rv = vppcom_app_attach ();
741 clib_warning ("VCL<%d>: ERROR: vppcom_app_attach() failed!",
746 VDBG (0, "VCL<%d>: app_name '%s', my_client_index %d (0x%x)",
747 getpid (), app_name, vcm->my_client_index, vcm->my_client_index);
754 vppcom_app_destroy (void)
757 f64 orig_app_timeout;
759 if (vcm->my_client_index == ~0)
762 VDBG (0, "VCL<%d>: detaching from VPP, my_client_index %d (0x%x)",
763 getpid (), vcm->my_client_index, vcm->my_client_index);
764 vcl_evt (VCL_EVT_DETACH, vcm);
766 vppcom_app_send_detach ();
767 orig_app_timeout = vcm->cfg.app_timeout;
768 vcm->cfg.app_timeout = 2.0;
769 rv = vcl_wait_for_app_state_change (STATE_APP_ENABLED);
770 vcm->cfg.app_timeout = orig_app_timeout;
771 if (PREDICT_FALSE (rv))
772 VDBG (0, "VCL<%d>: application detach timed out! returning %d (%s)",
773 getpid (), rv, vppcom_retval_str (rv));
776 vl_client_disconnect_from_vlib ();
777 vcm->my_client_index = ~0;
778 vcm->app_state = STATE_APP_START;
782 vppcom_session_create (u8 proto, u8 is_nonblocking)
784 vcl_worker_t *wrk = vcl_worker_get_current ();
785 vcl_session_t *session;
787 session = vcl_session_alloc (wrk);
789 session->session_type = proto;
790 session->session_state = STATE_START;
791 session->vpp_handle = ~0;
792 session->is_dgram = proto == VPPCOM_PROTO_UDP;
795 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK);
797 vcl_evt (VCL_EVT_CREATE, session, session_type, session->session_state,
798 is_nonblocking, session_index);
800 VDBG (0, "VCL<%d>: sid %u", getpid (), session->session_index);
802 return vcl_session_handle (session);
806 vppcom_session_close (uint32_t session_handle)
808 vcl_worker_t *wrk = vcl_worker_get_current ();
809 vcl_session_t *session = 0;
810 u8 is_vep, is_vep_session;
811 session_state_t state;
816 session = vcl_session_get_w_handle (wrk, session_handle);
818 return VPPCOM_EBADFD;
820 is_vep = session->is_vep;
821 is_vep_session = session->is_vep_session;
822 next_sh = session->vep.next_sh;
823 vep_sh = session->vep.vep_sh;
824 state = session->session_state;
825 vpp_handle = session->vpp_handle;
827 if (VPPCOM_DEBUG > 0)
830 clib_warning ("VCL<%d>: vep_idx %u / sid %u: "
831 "closing epoll session...",
832 getpid (), session_handle, session_handle);
834 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %d: "
835 "closing session...",
836 getpid (), vpp_handle, session_handle);
841 while (next_sh != ~0)
843 rv = vppcom_epoll_ctl (session_handle, EPOLL_CTL_DEL, next_sh, 0);
844 if (PREDICT_FALSE (rv < 0))
845 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: EPOLL_CTL_DEL "
846 "vep_idx %u failed! rv %d (%s)",
847 getpid (), vpp_handle, next_sh, vep_sh,
848 rv, vppcom_retval_str (rv));
850 next_sh = session->vep.next_sh;
857 rv = vppcom_epoll_ctl (vep_sh, EPOLL_CTL_DEL, session_handle, 0);
859 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: EPOLL_CTL_DEL "
860 "vep_idx %u failed! rv %d (%s)",
861 getpid (), vpp_handle, session_handle,
862 vep_sh, rv, vppcom_retval_str (rv));
865 if (state & STATE_LISTEN)
867 rv = vppcom_session_unbind (session_handle);
868 if (PREDICT_FALSE (rv < 0))
869 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: listener unbind "
870 "failed! rv %d (%s)",
871 getpid (), vpp_handle, session_handle,
872 rv, vppcom_retval_str (rv));
874 else if (state & STATE_OPEN)
876 rv = vppcom_session_disconnect (session_handle);
877 if (PREDICT_FALSE (rv < 0))
878 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
879 "session disconnect failed! rv %d (%s)",
880 getpid (), vpp_handle, session_handle,
881 rv, vppcom_retval_str (rv));
885 if (vcl_session_is_ct (session))
887 vcl_cut_through_registration_t *ctr;
890 mq_addr = pointer_to_uword (session->our_evt_q);
891 ctr = vcl_ct_registration_lock_and_lookup (wrk, mq_addr);
893 if (ctr->epoll_evt_conn_index != ~0)
894 vcl_mq_epoll_del_evfd (wrk, ctr->epoll_evt_conn_index);
895 VDBG (0, "Removing ct registration %u",
896 vcl_ct_registration_index (wrk, ctr));
897 vcl_ct_registration_del (wrk, ctr);
898 vcl_ct_registration_lookup_del (wrk, mq_addr);
899 vcl_ct_registration_unlock (wrk);
902 if (vpp_handle != ~0)
904 vcl_session_table_del_vpp_handle (wrk, vpp_handle);
906 vcl_session_free (wrk, session);
908 if (VPPCOM_DEBUG > 0)
911 clib_warning ("VCL<%d>: vep_idx %u / sid %u: epoll session removed.",
912 getpid (), session_handle, session_handle);
914 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: session removed.",
915 getpid (), vpp_handle, session_handle);
918 vcl_evt (VCL_EVT_CLOSE, session, rv);
924 vppcom_session_bind (uint32_t session_handle, vppcom_endpt_t * ep)
926 vcl_worker_t *wrk = vcl_worker_get_current ();
927 vcl_session_t *session = 0;
930 return VPPCOM_EINVAL;
932 session = vcl_session_get_w_handle (wrk, session_handle);
934 return VPPCOM_EBADFD;
938 clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
939 "bind to an epoll session!", getpid (), session_handle);
940 return VPPCOM_EBADFD;
943 session->transport.is_ip4 = ep->is_ip4;
945 clib_memcpy (&session->transport.lcl_ip.ip4, ep->ip,
946 sizeof (ip4_address_t));
948 clib_memcpy (&session->transport.lcl_ip.ip6, ep->ip,
949 sizeof (ip6_address_t));
950 session->transport.lcl_port = ep->port;
952 VDBG (0, "VCL<%d>: sid %u: binding to local %s address %U port %u, "
953 "proto %s", getpid (), session_handle,
954 session->transport.is_ip4 ? "IPv4" : "IPv6",
955 format_ip46_address, &session->transport.lcl_ip,
956 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
957 clib_net_to_host_u16 (session->transport.lcl_port),
958 session->session_type ? "UDP" : "TCP");
959 vcl_evt (VCL_EVT_BIND, session);
961 if (session->session_type == VPPCOM_PROTO_UDP)
962 vppcom_session_listen (session_handle, 10);
968 vppcom_session_listen (uint32_t listen_sh, uint32_t q_len)
970 vcl_worker_t *wrk = vcl_worker_get_current ();
971 vcl_session_t *listen_session = 0;
972 u64 listen_vpp_handle;
975 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
977 return VPPCOM_EBADFD;
979 if (q_len == 0 || q_len == ~0)
980 q_len = vcm->cfg.listen_queue_size;
982 if (listen_session->is_vep)
984 clib_warning ("VCL<%d>: ERROR: sid %u: cannot listen on an "
985 "epoll session!", getpid (), listen_sh);
986 return VPPCOM_EBADFD;
989 listen_vpp_handle = listen_session->vpp_handle;
990 if (listen_session->session_state & STATE_LISTEN)
992 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: already in listen state!",
993 getpid (), listen_vpp_handle, listen_sh);
997 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: sending VPP bind+listen "
998 "request...", getpid (), listen_vpp_handle, listen_sh);
1001 * Send listen request to vpp and wait for reply
1003 vppcom_send_bind_sock (listen_session);
1004 rv = vppcom_wait_for_session_state_change (listen_session->session_index,
1006 vcm->cfg.session_timeout);
1008 if (PREDICT_FALSE (rv))
1010 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
1011 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: bind+listen failed! "
1012 "returning %d (%s)", getpid (), listen_session->vpp_handle,
1013 listen_sh, rv, vppcom_retval_str (rv));
1021 validate_args_session_accept_ (vcl_worker_t * wrk,
1022 vcl_session_t * listen_session)
1024 /* Input validation - expects spinlock on sessions_lockp */
1025 if (listen_session->is_vep)
1027 clib_warning ("VCL<%d>: ERROR: sid %u: cannot accept on an "
1028 "epoll session!", getpid (),
1029 listen_session->session_index);
1030 return VPPCOM_EBADFD;
1033 if (listen_session->session_state != STATE_LISTEN)
1035 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
1036 "not in listen state! state 0x%x (%s)", getpid (),
1037 listen_session->vpp_handle, listen_session->session_index,
1038 listen_session->session_state,
1039 vppcom_session_state_str (listen_session->session_state));
1040 return VPPCOM_EBADFD;
1046 vppcom_session_accept (uint32_t listen_session_handle, vppcom_endpt_t * ep,
1049 u32 client_session_index = ~0, listen_session_index;
1050 vcl_worker_t *wrk = vcl_worker_get_current ();
1051 session_accepted_msg_t accepted_msg;
1052 vcl_session_t *listen_session = 0;
1053 vcl_session_t *client_session = 0;
1054 svm_msg_q_t *vpp_evt_q;
1055 vcl_session_msg_t *evt;
1056 u64 listen_vpp_handle;
1057 svm_msg_q_msg_t msg;
1062 listen_session = vcl_session_get_w_handle (wrk, listen_session_handle);
1063 if (!listen_session)
1064 return VPPCOM_EBADFD;
1066 listen_session_index = listen_session->session_index;
1067 if ((rv = validate_args_session_accept_ (wrk, listen_session)))
1070 if (clib_fifo_elts (listen_session->accept_evts_fifo))
1072 clib_fifo_sub2 (listen_session->accept_evts_fifo, evt);
1073 accepted_msg = evt->accepted_msg;
1077 is_nonblocking = VCL_SESS_ATTR_TEST (listen_session->attr,
1078 VCL_SESS_ATTR_NONBLOCK);
1079 if (svm_msg_q_is_empty (wrk->app_event_queue) && is_nonblocking)
1080 return VPPCOM_EAGAIN;
1084 if (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_WAIT, 0))
1085 return VPPCOM_EAGAIN;
1087 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
1088 if (e->event_type != SESSION_CTRL_EVT_ACCEPTED)
1090 clib_warning ("discarded event: %u", e->event_type);
1091 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
1094 clib_memcpy (&accepted_msg, e->data, sizeof (accepted_msg));
1095 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
1101 client_session_index = vcl_session_accepted_handler (wrk, &accepted_msg);
1102 listen_session = vcl_session_get (wrk, listen_session_index);
1103 client_session = vcl_session_get (wrk, client_session_index);
1105 if (flags & O_NONBLOCK)
1106 VCL_SESS_ATTR_SET (client_session->attr, VCL_SESS_ATTR_NONBLOCK);
1108 listen_vpp_handle = listen_session->vpp_handle;
1109 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: Got a client request! "
1110 "vpp handle 0x%llx, sid %u, flags %d, is_nonblocking %u",
1111 getpid (), listen_vpp_handle, listen_session_handle,
1112 client_session->vpp_handle, client_session_index,
1113 flags, VCL_SESS_ATTR_TEST (client_session->attr,
1114 VCL_SESS_ATTR_NONBLOCK));
1118 ep->is_ip4 = client_session->transport.is_ip4;
1119 ep->port = client_session->transport.rmt_port;
1120 if (client_session->transport.is_ip4)
1121 clib_memcpy (ep->ip, &client_session->transport.rmt_ip.ip4,
1122 sizeof (ip4_address_t));
1124 clib_memcpy (ep->ip, &client_session->transport.rmt_ip.ip6,
1125 sizeof (ip6_address_t));
1128 if (accepted_msg.server_event_queue_address)
1129 vpp_evt_q = uword_to_pointer (accepted_msg.vpp_event_queue_address,
1132 vpp_evt_q = client_session->vpp_evt_q;
1134 vcl_send_session_accepted_reply (vpp_evt_q, client_session->client_context,
1135 client_session->vpp_handle, 0);
1137 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: accepted vpp handle 0x%llx, "
1138 "sid %u connection from peer %s address %U port %u to local %s "
1139 "address %U port %u", getpid (), listen_vpp_handle,
1140 listen_session_handle, client_session->vpp_handle,
1141 client_session_index,
1142 client_session->transport.is_ip4 ? "IPv4" : "IPv6",
1143 format_ip46_address, &client_session->transport.rmt_ip,
1144 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
1145 clib_net_to_host_u16 (client_session->transport.rmt_port),
1146 client_session->transport.is_ip4 ? "IPv4" : "IPv6",
1147 format_ip46_address, &client_session->transport.lcl_ip,
1148 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
1149 clib_net_to_host_u16 (client_session->transport.lcl_port));
1150 vcl_evt (VCL_EVT_ACCEPT, client_session, listen_session,
1151 client_session_index);
1153 return vcl_session_handle (client_session);
1157 vppcom_session_connect (uint32_t session_handle, vppcom_endpt_t * server_ep)
1159 vcl_worker_t *wrk = vcl_worker_get_current ();
1160 vcl_session_t *session = 0;
1164 session = vcl_session_get_w_handle (wrk, session_handle);
1166 return VPPCOM_EBADFD;
1167 session_index = session->session_index;
1169 if (PREDICT_FALSE (session->is_vep))
1171 clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
1172 "connect on an epoll session!", getpid (),
1174 return VPPCOM_EBADFD;
1177 if (PREDICT_FALSE (session->session_state & CLIENT_STATE_OPEN))
1179 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: session already "
1180 "connected to %s %U port %d proto %s, state 0x%x (%s)",
1181 getpid (), session->vpp_handle, session_handle,
1182 session->transport.is_ip4 ? "IPv4" : "IPv6",
1183 format_ip46_address,
1184 &session->transport.rmt_ip, session->transport.is_ip4 ?
1185 IP46_TYPE_IP4 : IP46_TYPE_IP6,
1186 clib_net_to_host_u16 (session->transport.rmt_port),
1187 session->session_type ? "UDP" : "TCP", session->session_state,
1188 vppcom_session_state_str (session->session_state));
1192 session->transport.is_ip4 = server_ep->is_ip4;
1193 if (session->transport.is_ip4)
1194 clib_memcpy (&session->transport.rmt_ip.ip4, server_ep->ip,
1195 sizeof (ip4_address_t));
1197 clib_memcpy (&session->transport.rmt_ip.ip6, server_ep->ip,
1198 sizeof (ip6_address_t));
1199 session->transport.rmt_port = server_ep->port;
1201 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: connecting to server %s %U "
1203 getpid (), session->vpp_handle, session_handle,
1204 session->transport.is_ip4 ? "IPv4" : "IPv6",
1205 format_ip46_address,
1206 &session->transport.rmt_ip, session->transport.is_ip4 ?
1207 IP46_TYPE_IP4 : IP46_TYPE_IP6,
1208 clib_net_to_host_u16 (session->transport.rmt_port),
1209 session->session_type ? "UDP" : "TCP");
1212 * Send connect request and wait for reply from vpp
1214 vppcom_send_connect_sock (session);
1215 rv = vppcom_wait_for_session_state_change (session_index, STATE_CONNECT,
1216 vcm->cfg.session_timeout);
1218 session = vcl_session_get (wrk, session_index);
1220 if (PREDICT_FALSE (rv))
1222 if (VPPCOM_DEBUG > 0)
1225 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: connect "
1226 "failed! returning %d (%s)", getpid (),
1227 session->vpp_handle, session_handle, rv,
1228 vppcom_retval_str (rv));
1230 clib_warning ("VCL<%d>: no session for sid %u: connect failed! "
1231 "returning %d (%s)", getpid (),
1232 session_handle, rv, vppcom_retval_str (rv));
1236 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: connected!",
1237 getpid (), session->vpp_handle, session_handle);
1243 vcl_is_rx_evt_for_session (session_event_t * e, u32 sid, u8 is_ct)
1246 return (e->event_type == FIFO_EVENT_APP_RX
1247 && e->fifo->client_session_index == sid);
1249 return (e->event_type == SESSION_IO_EVT_CT_TX);
1253 vcl_session_is_readable (vcl_session_t * s)
1255 return ((s->session_state & STATE_OPEN)
1256 || (s->session_state == STATE_LISTEN
1257 && s->session_type == VPPCOM_PROTO_UDP));
1261 vppcom_session_read_internal (uint32_t session_handle, void *buf, int n,
1264 vcl_worker_t *wrk = vcl_worker_get_current ();
1265 int n_read = 0, rv, is_nonblocking;
1266 vcl_session_t *s = 0;
1267 svm_fifo_t *rx_fifo;
1268 svm_msg_q_msg_t msg;
1273 if (PREDICT_FALSE (!buf))
1274 return VPPCOM_EINVAL;
1276 s = vcl_session_get_w_handle (wrk, session_handle);
1277 if (PREDICT_FALSE (!s || s->is_vep))
1278 return VPPCOM_EBADFD;
1280 if (PREDICT_FALSE (!vcl_session_is_readable (s)))
1282 session_state_t state = s->session_state;
1283 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
1285 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: %s session is not open! "
1286 "state 0x%x (%s), returning %d (%s)",
1287 getpid (), s->vpp_handle, session_handle, state,
1288 vppcom_session_state_str (state), rv, vppcom_retval_str (rv));
1292 is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
1293 is_ct = vcl_session_is_ct (s);
1294 mq = is_ct ? s->our_evt_q : wrk->app_event_queue;
1295 rx_fifo = s->rx_fifo;
1297 if (svm_fifo_is_empty (rx_fifo))
1301 svm_fifo_unset_event (rx_fifo);
1304 while (svm_fifo_is_empty (rx_fifo))
1306 svm_fifo_unset_event (rx_fifo);
1307 svm_msg_q_lock (mq);
1308 if (svm_msg_q_is_empty (mq))
1309 svm_msg_q_wait (mq);
1311 svm_msg_q_sub_w_lock (mq, &msg);
1312 e = svm_msg_q_msg_data (mq, &msg);
1313 svm_msg_q_unlock (mq);
1314 if (!vcl_is_rx_evt_for_session (e, s->session_index, is_ct))
1316 vcl_handle_mq_event (wrk, e);
1317 svm_msg_q_free_msg (mq, &msg);
1320 svm_msg_q_free_msg (mq, &msg);
1322 if (PREDICT_FALSE (s->session_state == STATE_CLOSE_ON_EMPTY))
1328 n_read = app_recv_dgram_raw (rx_fifo, buf, n, &s->transport, 0, peek);
1330 n_read = app_recv_stream_raw (rx_fifo, buf, n, 0, peek);
1332 if (svm_fifo_is_empty (rx_fifo))
1333 svm_fifo_unset_event (rx_fifo);
1335 if (is_ct && n_read + svm_fifo_max_dequeue (rx_fifo) == rx_fifo->nitems)
1337 /* If the peer is not polling send notification */
1338 if (!svm_fifo_has_event (s->rx_fifo))
1339 app_send_io_evt_to_vpp (s->vpp_evt_q, s->rx_fifo,
1340 SESSION_IO_EVT_CT_RX, SVM_Q_WAIT);
1343 VDBG (2, "VCL<%d>: vpp handle 0x%llx, sid %u: read %d bytes from (%p)",
1344 getpid (), s->vpp_handle, session_handle, n_read, rx_fifo);
1350 vppcom_session_read (uint32_t session_handle, void *buf, size_t n)
1352 return (vppcom_session_read_internal (session_handle, buf, n, 0));
1356 vppcom_session_peek (uint32_t session_handle, void *buf, int n)
1358 return (vppcom_session_read_internal (session_handle, buf, n, 1));
1362 vppcom_session_read_segments (uint32_t session_handle,
1363 vppcom_data_segments_t ds)
1365 vcl_worker_t *wrk = vcl_worker_get_current ();
1366 int n_read = 0, rv, is_nonblocking;
1367 vcl_session_t *s = 0;
1368 svm_fifo_t *rx_fifo;
1369 svm_msg_q_msg_t msg;
1374 s = vcl_session_get_w_handle (wrk, session_handle);
1375 if (PREDICT_FALSE (!s || s->is_vep))
1376 return VPPCOM_EBADFD;
1378 if (PREDICT_FALSE (!vcl_session_is_readable (s)))
1380 session_state_t state = s->session_state;
1381 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
1385 is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
1386 is_ct = vcl_session_is_ct (s);
1387 mq = is_ct ? s->our_evt_q : wrk->app_event_queue;
1388 rx_fifo = s->rx_fifo;
1390 if (svm_fifo_is_empty (rx_fifo))
1394 svm_fifo_unset_event (rx_fifo);
1397 while (svm_fifo_is_empty (rx_fifo))
1399 svm_fifo_unset_event (rx_fifo);
1400 svm_msg_q_lock (mq);
1401 if (svm_msg_q_is_empty (mq))
1402 svm_msg_q_wait (mq);
1404 svm_msg_q_sub_w_lock (mq, &msg);
1405 e = svm_msg_q_msg_data (mq, &msg);
1406 svm_msg_q_unlock (mq);
1407 if (!vcl_is_rx_evt_for_session (e, s->session_index, is_ct))
1409 vcl_handle_mq_event (wrk, e);
1410 svm_msg_q_free_msg (mq, &msg);
1413 svm_msg_q_free_msg (mq, &msg);
1415 if (PREDICT_FALSE (s->session_state == STATE_CLOSE_ON_EMPTY))
1420 n_read = svm_fifo_segments (rx_fifo, (svm_fifo_segment_t *) ds);
1421 svm_fifo_unset_event (rx_fifo);
1423 if (is_ct && n_read + svm_fifo_max_dequeue (rx_fifo) == rx_fifo->nitems)
1425 /* If the peer is not polling send notification */
1426 if (!svm_fifo_has_event (s->rx_fifo))
1427 app_send_io_evt_to_vpp (s->vpp_evt_q, s->rx_fifo,
1428 SESSION_IO_EVT_CT_RX, SVM_Q_WAIT);
1435 vppcom_session_free_segments (uint32_t session_handle,
1436 vppcom_data_segments_t ds)
1438 vcl_worker_t *wrk = vcl_worker_get_current ();
1441 s = vcl_session_get_w_handle (wrk, session_handle);
1442 if (PREDICT_FALSE (!s || s->is_vep))
1445 svm_fifo_segments_free (s->rx_fifo, (svm_fifo_segment_t *) ds);
1449 vppcom_session_read_ready (vcl_session_t * session)
1451 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
1452 if (PREDICT_FALSE (session->is_vep))
1454 clib_warning ("VCL<%d>: ERROR: sid %u: cannot read from an "
1455 "epoll session!", getpid (), session->session_index);
1456 return VPPCOM_EBADFD;
1459 if (PREDICT_FALSE (!(session->session_state & (STATE_OPEN | STATE_LISTEN))))
1461 session_state_t state = session->session_state;
1464 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
1466 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: session is not open!"
1467 " state 0x%x (%s), returning %d (%s)", getpid (),
1468 session->vpp_handle, session->session_index, state,
1469 vppcom_session_state_str (state), rv, vppcom_retval_str (rv));
1473 if (session->session_state & STATE_LISTEN)
1474 return clib_fifo_elts (session->accept_evts_fifo);
1476 return svm_fifo_max_dequeue (session->rx_fifo);
1480 vppcom_data_segment_copy (void *buf, vppcom_data_segments_t ds, u32 max_bytes)
1482 u32 first_copy = clib_min (ds[0].len, max_bytes);
1483 clib_memcpy (buf, ds[0].data, first_copy);
1484 if (first_copy < max_bytes)
1486 clib_memcpy (buf + first_copy, ds[1].data,
1487 clib_min (ds[1].len, max_bytes - first_copy));
1493 vcl_is_tx_evt_for_session (session_event_t * e, u32 sid, u8 is_ct)
1496 return (e->event_type == FIFO_EVENT_APP_TX
1497 && e->fifo->client_session_index == sid);
1499 return (e->event_type == SESSION_IO_EVT_CT_RX);
1503 vppcom_session_write (uint32_t session_handle, void *buf, size_t n)
1505 vcl_worker_t *wrk = vcl_worker_get_current ();
1506 int rv, n_write, is_nonblocking;
1507 vcl_session_t *s = 0;
1508 svm_fifo_t *tx_fifo = 0;
1509 session_evt_type_t et;
1510 svm_msg_q_msg_t msg;
1515 if (PREDICT_FALSE (!buf))
1516 return VPPCOM_EINVAL;
1518 s = vcl_session_get_w_handle (wrk, session_handle);
1519 if (PREDICT_FALSE (!s))
1520 return VPPCOM_EBADFD;
1522 if (PREDICT_FALSE (s->is_vep))
1524 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
1525 "cannot write to an epoll session!",
1526 getpid (), s->vpp_handle, session_handle);
1528 return VPPCOM_EBADFD;
1531 if (PREDICT_FALSE (!(s->session_state & STATE_OPEN)))
1533 session_state_t state = s->session_state;
1534 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
1535 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: session is not open! "
1536 "state 0x%x (%s)", getpid (), s->vpp_handle, session_handle,
1537 state, vppcom_session_state_str (state));
1541 tx_fifo = s->tx_fifo;
1542 is_ct = vcl_session_is_ct (s);
1543 is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
1544 mq = is_ct ? s->our_evt_q : wrk->app_event_queue;
1545 if (svm_fifo_is_full (tx_fifo))
1549 return VPPCOM_EWOULDBLOCK;
1551 while (svm_fifo_is_full (tx_fifo))
1553 svm_fifo_set_want_tx_evt (tx_fifo, 1);
1554 svm_msg_q_lock (mq);
1555 svm_msg_q_wait (mq);
1557 svm_msg_q_sub_w_lock (mq, &msg);
1558 e = svm_msg_q_msg_data (mq, &msg);
1559 svm_msg_q_unlock (mq);
1561 if (!vcl_is_tx_evt_for_session (e, s->session_index, is_ct))
1562 vcl_handle_mq_event (wrk, e);
1563 svm_msg_q_free_msg (mq, &msg);
1567 ASSERT (FIFO_EVENT_APP_TX + 1 == SESSION_IO_EVT_CT_TX);
1568 et = FIFO_EVENT_APP_TX + vcl_session_is_ct (s);
1570 n_write = app_send_dgram_raw (tx_fifo, &s->transport,
1571 s->vpp_evt_q, buf, n, et, SVM_Q_WAIT);
1573 n_write = app_send_stream_raw (tx_fifo, s->vpp_evt_q, buf, n, et,
1576 ASSERT (n_write > 0);
1578 VDBG (2, "VCL<%d>: vpp handle 0x%llx, sid %u: wrote %d bytes", getpid (),
1579 s->vpp_handle, session_handle, n_write);
1584 static vcl_session_t *
1585 vcl_ct_session_get_from_fifo (vcl_worker_t * wrk, svm_fifo_t * f, u8 type)
1588 s = vcl_session_get (wrk, f->client_session_index);
1592 if (type == 0 && s->rx_fifo == f)
1595 if (type == 1 && s->tx_fifo == f)
1598 s = vcl_session_get (wrk, f->master_session_index);
1601 if (type == 0 && s->rx_fifo == f)
1603 if (type == 1 && s->tx_fifo == f)
1610 vppcom_session_write_ready (vcl_session_t * session)
1612 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
1613 if (PREDICT_FALSE (session->is_vep))
1615 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
1616 "cannot write to an epoll session!",
1617 getpid (), session->vpp_handle, session->session_index);
1618 return VPPCOM_EBADFD;
1621 if (PREDICT_FALSE (session->session_state & STATE_LISTEN))
1623 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
1624 "cannot write to a listen session!",
1625 getpid (), session->vpp_handle, session->session_index);
1626 return VPPCOM_EBADFD;
1629 if (PREDICT_FALSE (!(session->session_state & STATE_OPEN)))
1631 session_state_t state = session->session_state;
1634 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
1635 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
1636 "session is not open! state 0x%x (%s), "
1637 "returning %d (%s)", getpid (), session->vpp_handle,
1638 session->session_index,
1639 state, vppcom_session_state_str (state),
1640 rv, vppcom_retval_str (rv));
1644 VDBG (3, "VCL<%d>: vpp handle 0x%llx, sid %u: peek %s (%p), ready = %d",
1645 getpid (), session->vpp_handle, session->session_index,
1646 session->tx_fifo, svm_fifo_max_enqueue (session->tx_fifo));
1648 return svm_fifo_max_enqueue (session->tx_fifo);
1652 vcl_mq_dequeue_batch (vcl_worker_t * wrk, svm_msg_q_t * mq)
1654 svm_msg_q_msg_t *msg;
1658 n_msgs = svm_msg_q_size (mq);
1659 for (i = 0; i < n_msgs; i++)
1661 vec_add2 (wrk->mq_msg_vector, msg, 1);
1662 svm_msg_q_sub_w_lock (mq, msg);
1667 #define vcl_fifo_rx_evt_valid_or_break(_fifo) \
1668 if (PREDICT_FALSE (svm_fifo_is_empty (_fifo))) \
1670 svm_fifo_unset_event (_fifo); \
1671 if (svm_fifo_is_empty (_fifo)) \
1676 vcl_select_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
1677 unsigned long n_bits, unsigned long *read_map,
1678 unsigned long *write_map,
1679 unsigned long *except_map, u32 * bits_set)
1681 session_disconnected_msg_t *disconnected_msg;
1682 session_connected_msg_t *connected_msg;
1683 session_accepted_msg_t *accepted_msg;
1684 vcl_session_msg_t *vcl_msg;
1685 vcl_session_t *session;
1689 switch (e->event_type)
1691 case FIFO_EVENT_APP_RX:
1692 vcl_fifo_rx_evt_valid_or_break (e->fifo);
1693 sid = e->fifo->client_session_index;
1694 session = vcl_session_get (wrk, sid);
1697 if (sid < n_bits && read_map)
1699 clib_bitmap_set_no_check (read_map, sid, 1);
1703 case FIFO_EVENT_APP_TX:
1704 sid = e->fifo->client_session_index;
1705 session = vcl_session_get (wrk, sid);
1708 if (sid < n_bits && write_map)
1710 clib_bitmap_set_no_check (write_map, sid, 1);
1714 case SESSION_IO_EVT_CT_TX:
1715 vcl_fifo_rx_evt_valid_or_break (e->fifo);
1716 session = vcl_ct_session_get_from_fifo (wrk, e->fifo, 0);
1719 sid = session->session_index;
1720 if (sid < n_bits && read_map)
1722 clib_bitmap_set_no_check (read_map, sid, 1);
1726 case SESSION_IO_EVT_CT_RX:
1727 session = vcl_ct_session_get_from_fifo (wrk, e->fifo, 1);
1730 sid = session->session_index;
1731 if (sid < n_bits && write_map)
1733 clib_bitmap_set_no_check (write_map, sid, 1);
1737 case SESSION_CTRL_EVT_ACCEPTED:
1738 accepted_msg = (session_accepted_msg_t *) e->data;
1739 handle = accepted_msg->listener_handle;
1740 session = vcl_session_table_lookup_listener (wrk, handle);
1743 clib_warning ("VCL<%d>: ERROR: couldn't find listen session:"
1744 "listener handle %llx", getpid (), handle);
1748 clib_fifo_add2 (session->accept_evts_fifo, vcl_msg);
1749 vcl_msg->accepted_msg = *accepted_msg;
1750 sid = session->session_index;
1751 if (sid < n_bits && read_map)
1753 clib_bitmap_set_no_check (read_map, sid, 1);
1757 case SESSION_CTRL_EVT_CONNECTED:
1758 connected_msg = (session_connected_msg_t *) e->data;
1759 vcl_session_connected_handler (wrk, connected_msg);
1761 case SESSION_CTRL_EVT_DISCONNECTED:
1762 disconnected_msg = (session_disconnected_msg_t *) e->data;
1763 sid = vcl_session_index_from_vpp_handle (wrk, disconnected_msg->handle);
1764 if (sid < n_bits && except_map)
1766 clib_bitmap_set_no_check (except_map, sid, 1);
1770 case SESSION_CTRL_EVT_RESET:
1771 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
1772 if (sid < n_bits && except_map)
1774 clib_bitmap_set_no_check (except_map, sid, 1);
1779 clib_warning ("unhandled: %u", e->event_type);
1785 vcl_select_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
1786 unsigned long n_bits, unsigned long *read_map,
1787 unsigned long *write_map, unsigned long *except_map,
1788 double time_to_wait, u32 * bits_set)
1790 svm_msg_q_msg_t *msg;
1794 svm_msg_q_lock (mq);
1795 if (svm_msg_q_is_empty (mq))
1799 svm_msg_q_unlock (mq);
1805 svm_msg_q_unlock (mq);
1808 else if (time_to_wait < 0)
1810 svm_msg_q_wait (mq);
1814 if (svm_msg_q_timedwait (mq, time_to_wait))
1816 svm_msg_q_unlock (mq);
1821 vcl_mq_dequeue_batch (wrk, mq);
1822 svm_msg_q_unlock (mq);
1824 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
1826 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
1827 e = svm_msg_q_msg_data (mq, msg);
1828 vcl_select_handle_mq_event (wrk, e, n_bits, read_map, write_map,
1829 except_map, bits_set);
1830 svm_msg_q_free_msg (mq, msg);
1832 vec_reset_length (wrk->mq_msg_vector);
1837 vppcom_select_condvar (vcl_worker_t * wrk, unsigned long n_bits,
1838 unsigned long *read_map, unsigned long *write_map,
1839 unsigned long *except_map, double time_to_wait,
1842 double total_wait = 0, wait_slice;
1843 vcl_cut_through_registration_t *cr;
1845 time_to_wait = (time_to_wait == -1) ? 10e9 : time_to_wait;
1846 wait_slice = wrk->cut_through_registrations ? 10e-6 : time_to_wait;
1849 vcl_ct_registration_lock (wrk);
1851 pool_foreach (cr, wrk->cut_through_registrations, ({
1852 vcl_select_handle_mq (wrk, cr->mq, n_bits, read_map, write_map, except_map,
1856 vcl_ct_registration_unlock (wrk);
1858 vcl_select_handle_mq (wrk, wrk->app_event_queue, n_bits, read_map,
1859 write_map, except_map, time_to_wait, bits_set);
1860 total_wait += wait_slice;
1864 while (total_wait < time_to_wait);
1870 vppcom_select_eventfd (vcl_worker_t * wrk, unsigned long n_bits,
1871 unsigned long *read_map, unsigned long *write_map,
1872 unsigned long *except_map, double time_to_wait,
1875 vcl_mq_evt_conn_t *mqc;
1876 int __clib_unused n_read;
1880 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
1881 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
1882 vec_len (wrk->mq_events), time_to_wait);
1883 for (i = 0; i < n_mq_evts; i++)
1885 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
1886 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
1887 vcl_select_handle_mq (wrk, mqc->mq, n_bits, read_map, write_map,
1888 except_map, 0, bits_set);
1891 return (n_mq_evts > 0 ? (int) *bits_set : 0);
1895 vppcom_select (unsigned long n_bits, unsigned long *read_map,
1896 unsigned long *write_map, unsigned long *except_map,
1897 double time_to_wait)
1899 u32 sid, minbits = clib_max (n_bits, BITS (uword)), bits_set = 0;
1900 vcl_worker_t *wrk = vcl_worker_get_current ();
1901 vcl_session_t *session = 0;
1904 ASSERT (sizeof (clib_bitmap_t) == sizeof (long int));
1906 if (n_bits && read_map)
1908 clib_bitmap_validate (wrk->rd_bitmap, minbits);
1909 clib_memcpy (wrk->rd_bitmap, read_map,
1910 vec_len (wrk->rd_bitmap) * sizeof (clib_bitmap_t));
1911 memset (read_map, 0, vec_len (wrk->rd_bitmap) * sizeof (clib_bitmap_t));
1913 if (n_bits && write_map)
1915 clib_bitmap_validate (wrk->wr_bitmap, minbits);
1916 clib_memcpy (wrk->wr_bitmap, write_map,
1917 vec_len (wrk->wr_bitmap) * sizeof (clib_bitmap_t));
1918 memset (write_map, 0,
1919 vec_len (wrk->wr_bitmap) * sizeof (clib_bitmap_t));
1921 if (n_bits && except_map)
1923 clib_bitmap_validate (wrk->ex_bitmap, minbits);
1924 clib_memcpy (wrk->ex_bitmap, except_map,
1925 vec_len (wrk->ex_bitmap) * sizeof (clib_bitmap_t));
1926 memset (except_map, 0,
1927 vec_len (wrk->ex_bitmap) * sizeof (clib_bitmap_t));
1937 clib_bitmap_foreach (sid, wrk->wr_bitmap, ({
1938 if (!(session = vcl_session_get (wrk, sid)))
1940 VDBG (0, "VCL<%d>: session %d specified in write_map is closed.",
1942 return VPPCOM_EBADFD;
1945 rv = svm_fifo_is_full (session->tx_fifo);
1948 clib_bitmap_set_no_check (write_map, sid, 1);
1957 clib_bitmap_foreach (sid, wrk->rd_bitmap, ({
1958 if (!(session = vcl_session_get (wrk, sid)))
1960 VDBG (0, "VCL<%d>: session %d specified in write_map is closed.",
1962 return VPPCOM_EBADFD;
1965 rv = vppcom_session_read_ready (session);
1968 clib_bitmap_set_no_check (read_map, sid, 1);
1976 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
1978 vcl_select_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i], n_bits,
1979 read_map, write_map, except_map, &bits_set);
1981 vec_reset_length (wrk->unhandled_evts_vector);
1983 if (vcm->cfg.use_mq_eventfd)
1984 vppcom_select_eventfd (wrk, n_bits, read_map, write_map, except_map,
1985 time_to_wait, &bits_set);
1987 vppcom_select_condvar (wrk, n_bits, read_map, write_map, except_map,
1988 time_to_wait, &bits_set);
1994 vep_verify_epoll_chain (vcl_worker_t * wrk, u32 vep_idx)
1996 vcl_session_t *session;
1997 vppcom_epoll_t *vep;
2000 if (VPPCOM_DEBUG <= 1)
2003 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
2004 session = vcl_session_get (wrk, vep_idx);
2005 if (PREDICT_FALSE (!session))
2007 clib_warning ("VCL<%d>: ERROR: Invalid vep_idx (%u)!",
2008 getpid (), vep_idx);
2011 if (PREDICT_FALSE (!session->is_vep))
2013 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
2014 getpid (), vep_idx);
2017 vep = &session->vep;
2018 clib_warning ("VCL<%d>: vep_idx (%u): Dumping epoll chain\n"
2021 " is_vep_session = %u\n"
2022 " next_sid = 0x%x (%u)\n"
2023 " wait_cont_idx = 0x%x (%u)\n"
2024 "}\n", getpid (), vep_idx,
2025 session->is_vep, session->is_vep_session,
2026 vep->next_sh, vep->next_sh,
2027 session->wait_cont_idx, session->wait_cont_idx);
2029 for (sid = vep->next_sh; sid != ~0; sid = vep->next_sh)
2031 session = vcl_session_get (wrk, sid);
2032 if (PREDICT_FALSE (!session))
2034 clib_warning ("VCL<%d>: ERROR: Invalid sid (%u)!", getpid (), sid);
2037 if (PREDICT_FALSE (session->is_vep))
2038 clib_warning ("VCL<%d>: ERROR: sid (%u) is a vep!",
2039 getpid (), vep_idx);
2040 else if (PREDICT_FALSE (!session->is_vep_session))
2042 clib_warning ("VCL<%d>: ERROR: session (%u) "
2043 "is not a vep session!", getpid (), sid);
2046 vep = &session->vep;
2047 if (PREDICT_FALSE (vep->vep_sh != vep_idx))
2048 clib_warning ("VCL<%d>: ERROR: session (%u) vep_idx (%u) != "
2049 "vep_idx (%u)!", getpid (),
2050 sid, session->vep.vep_sh, vep_idx);
2051 if (session->is_vep_session)
2053 clib_warning ("vep_idx[%u]: sid 0x%x (%u)\n"
2055 " next_sid = 0x%x (%u)\n"
2056 " prev_sid = 0x%x (%u)\n"
2057 " vep_idx = 0x%x (%u)\n"
2058 " ev.events = 0x%x\n"
2059 " ev.data.u64 = 0x%llx\n"
2063 vep->next_sh, vep->next_sh,
2064 vep->prev_sh, vep->prev_sh,
2065 vep->vep_sh, vep->vep_sh,
2066 vep->ev.events, vep->ev.data.u64, vep->et_mask);
2071 clib_warning ("VCL<%d>: vep_idx (%u): Dump complete!\n",
2072 getpid (), vep_idx);
2076 vppcom_epoll_create (void)
2078 vcl_worker_t *wrk = vcl_worker_get_current ();
2079 vcl_session_t *vep_session;
2081 vep_session = vcl_session_alloc (wrk);
2083 vep_session->is_vep = 1;
2084 vep_session->vep.vep_sh = ~0;
2085 vep_session->vep.next_sh = ~0;
2086 vep_session->vep.prev_sh = ~0;
2087 vep_session->wait_cont_idx = ~0;
2088 vep_session->vpp_handle = ~0;
2090 vcl_evt (VCL_EVT_EPOLL_CREATE, vep_session, vep_sh);
2091 VDBG (0, "VCL<%d>: Created vep_idx %u / sid %u!",
2092 getpid (), vep_session->session_index, vep_session->session_index);
2094 return vcl_session_handle (vep_session);
2098 vppcom_epoll_ctl (uint32_t vep_handle, int op, uint32_t session_handle,
2099 struct epoll_event *event)
2101 vcl_worker_t *wrk = vcl_worker_get_current ();
2102 vcl_session_t *vep_session;
2103 vcl_session_t *session;
2106 if (vep_handle == session_handle)
2108 clib_warning ("VCL<%d>: ERROR: vep_idx == session_index (%u)!",
2109 getpid (), vep_handle);
2110 return VPPCOM_EINVAL;
2113 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
2114 if (PREDICT_FALSE (!vep_session))
2116 clib_warning ("VCL<%d>: ERROR: Invalid vep_idx (%u)!", vep_handle);
2117 return VPPCOM_EBADFD;
2119 if (PREDICT_FALSE (!vep_session->is_vep))
2121 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
2122 getpid (), vep_handle);
2123 return VPPCOM_EINVAL;
2126 ASSERT (vep_session->vep.vep_sh == ~0);
2127 ASSERT (vep_session->vep.prev_sh == ~0);
2129 session = vcl_session_get_w_handle (wrk, session_handle);
2130 if (PREDICT_FALSE (!session))
2132 VDBG (0, "VCL<%d>: ERROR: Invalid session_handle (%u)!",
2133 getpid (), session_handle);
2134 return VPPCOM_EBADFD;
2136 if (PREDICT_FALSE (session->is_vep))
2138 clib_warning ("ERROR: session_handle (%u) is a vep!", vep_handle);
2139 return VPPCOM_EINVAL;
2145 if (PREDICT_FALSE (!event))
2147 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_ADD: NULL pointer to "
2148 "epoll_event structure!", getpid ());
2149 return VPPCOM_EINVAL;
2151 if (vep_session->vep.next_sh != ~0)
2153 vcl_session_t *next_session;
2154 next_session = vcl_session_get_w_handle (wrk,
2155 vep_session->vep.next_sh);
2156 if (PREDICT_FALSE (!next_session))
2158 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_ADD: Invalid "
2159 "vep.next_sid (%u) on vep_idx (%u)!",
2160 getpid (), vep_session->vep.next_sh, vep_handle);
2161 return VPPCOM_EBADFD;
2163 ASSERT (next_session->vep.prev_sh == vep_handle);
2164 next_session->vep.prev_sh = session_handle;
2166 session->vep.next_sh = vep_session->vep.next_sh;
2167 session->vep.prev_sh = vep_handle;
2168 session->vep.vep_sh = vep_handle;
2169 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
2170 session->vep.ev = *event;
2171 session->is_vep = 0;
2172 session->is_vep_session = 1;
2173 vep_session->vep.next_sh = session_handle;
2175 VDBG (1, "VCL<%d>: EPOLL_CTL_ADD: vep_idx %u, sid %u, events 0x%x, "
2176 "data 0x%llx!", getpid (), vep_handle, session_handle,
2177 event->events, event->data.u64);
2178 vcl_evt (VCL_EVT_EPOLL_CTLADD, session, event->events, event->data.u64);
2182 if (PREDICT_FALSE (!event))
2184 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_MOD: NULL pointer to "
2185 "epoll_event structure!", getpid ());
2189 else if (PREDICT_FALSE (!session->is_vep_session))
2191 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_MOD: "
2192 "not a vep session!", getpid (), session_handle);
2196 else if (PREDICT_FALSE (session->vep.vep_sh != vep_handle))
2198 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_MOD: "
2199 "vep_idx (%u) != vep_idx (%u)!",
2200 getpid (), session_handle,
2201 session->vep.vep_sh, vep_handle);
2205 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
2206 session->vep.ev = *event;
2207 VDBG (1, "VCL<%d>: EPOLL_CTL_MOD: vep_idx %u, sid %u, events 0x%x,"
2208 " data 0x%llx!", getpid (), vep_handle, session_handle,
2209 event->events, event->data.u64);
2213 if (PREDICT_FALSE (!session->is_vep_session))
2215 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_DEL: "
2216 "not a vep session!", getpid (), session_handle);
2220 else if (PREDICT_FALSE (session->vep.vep_sh != vep_handle))
2222 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_DEL: "
2223 "vep_idx (%u) != vep_idx (%u)!",
2224 getpid (), session_handle,
2225 session->vep.vep_sh, vep_handle);
2230 vep_session->wait_cont_idx =
2231 (vep_session->wait_cont_idx == session_handle) ?
2232 session->vep.next_sh : vep_session->wait_cont_idx;
2234 if (session->vep.prev_sh == vep_handle)
2235 vep_session->vep.next_sh = session->vep.next_sh;
2238 vcl_session_t *prev_session;
2239 prev_session = vcl_session_get_w_handle (wrk, session->vep.prev_sh);
2240 if (PREDICT_FALSE (!prev_session))
2242 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_DEL: Invalid "
2243 "vep.prev_sid (%u) on sid (%u)!",
2244 getpid (), session->vep.prev_sh, session_handle);
2245 return VPPCOM_EBADFD;
2247 ASSERT (prev_session->vep.next_sh == session_handle);
2248 prev_session->vep.next_sh = session->vep.next_sh;
2250 if (session->vep.next_sh != ~0)
2252 vcl_session_t *next_session;
2253 next_session = vcl_session_get_w_handle (wrk, session->vep.next_sh);
2254 if (PREDICT_FALSE (!next_session))
2256 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_DEL: Invalid "
2257 "vep.next_sid (%u) on sid (%u)!",
2258 getpid (), session->vep.next_sh, session_handle);
2259 return VPPCOM_EBADFD;
2261 ASSERT (next_session->vep.prev_sh == session_handle);
2262 next_session->vep.prev_sh = session->vep.prev_sh;
2265 memset (&session->vep, 0, sizeof (session->vep));
2266 session->vep.next_sh = ~0;
2267 session->vep.prev_sh = ~0;
2268 session->vep.vep_sh = ~0;
2269 session->is_vep_session = 0;
2270 VDBG (1, "VCL<%d>: EPOLL_CTL_DEL: vep_idx %u, sid %u!",
2271 getpid (), vep_handle, session_handle);
2272 vcl_evt (VCL_EVT_EPOLL_CTLDEL, session, vep_sh);
2276 clib_warning ("VCL<%d>: ERROR: Invalid operation (%d)!", getpid (), op);
2280 vep_verify_epoll_chain (wrk, vep_handle);
2287 vcl_epoll_wait_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
2288 struct epoll_event *events, u32 * num_ev)
2290 session_disconnected_msg_t *disconnected_msg;
2291 session_connected_msg_t *connected_msg;
2292 session_accepted_msg_t *accepted_msg;
2293 u64 session_evt_data = ~0, handle;
2294 u32 sid = ~0, session_events;
2295 vcl_session_msg_t *vcl_msg;
2296 vcl_session_t *session;
2299 switch (e->event_type)
2301 case FIFO_EVENT_APP_RX:
2302 ASSERT (e->fifo->client_thread_index == vcl_get_worker_index ());
2303 vcl_fifo_rx_evt_valid_or_break (e->fifo);
2304 sid = e->fifo->client_session_index;
2305 session = vcl_session_get (wrk, sid);
2306 session_events = session->vep.ev.events;
2307 if (!(EPOLLIN & session->vep.ev.events))
2310 events[*num_ev].events |= EPOLLIN;
2311 session_evt_data = session->vep.ev.data.u64;
2313 case FIFO_EVENT_APP_TX:
2314 sid = e->fifo->client_session_index;
2315 session = vcl_session_get (wrk, sid);
2316 session_events = session->vep.ev.events;
2317 if (!(EPOLLOUT & session_events))
2320 events[*num_ev].events |= EPOLLOUT;
2321 session_evt_data = session->vep.ev.data.u64;
2323 case SESSION_IO_EVT_CT_TX:
2324 vcl_fifo_rx_evt_valid_or_break (e->fifo);
2325 session = vcl_ct_session_get_from_fifo (wrk, e->fifo, 0);
2326 sid = session->session_index;
2327 session_events = session->vep.ev.events;
2328 if (!(EPOLLIN & session->vep.ev.events))
2331 events[*num_ev].events |= EPOLLIN;
2332 session_evt_data = session->vep.ev.data.u64;
2334 case SESSION_IO_EVT_CT_RX:
2335 session = vcl_ct_session_get_from_fifo (wrk, e->fifo, 1);
2336 sid = session->session_index;
2337 session_events = session->vep.ev.events;
2338 if (!(EPOLLOUT & session_events))
2341 events[*num_ev].events |= EPOLLOUT;
2342 session_evt_data = session->vep.ev.data.u64;
2344 case SESSION_CTRL_EVT_ACCEPTED:
2345 accepted_msg = (session_accepted_msg_t *) e->data;
2346 handle = accepted_msg->listener_handle;
2347 session = vcl_session_table_lookup_listener (wrk, handle);
2350 clib_warning ("VCL<%d>: ERROR: couldn't find listen session:"
2351 "listener handle %llx", getpid (), handle);
2355 clib_fifo_add2 (session->accept_evts_fifo, vcl_msg);
2356 vcl_msg->accepted_msg = *accepted_msg;
2357 session_events = session->vep.ev.events;
2358 if (!(EPOLLIN & session_events))
2362 events[*num_ev].events |= EPOLLIN;
2363 session_evt_data = session->vep.ev.data.u64;
2365 case SESSION_CTRL_EVT_CONNECTED:
2366 connected_msg = (session_connected_msg_t *) e->data;
2367 vcl_session_connected_handler (wrk, connected_msg);
2368 /* Generate EPOLLOUT because there's no connected event */
2369 sid = vcl_session_index_from_vpp_handle (wrk, connected_msg->handle);
2370 session = vcl_session_get (wrk, sid);
2371 session_events = session->vep.ev.events;
2372 if (EPOLLOUT & session_events)
2375 events[*num_ev].events |= EPOLLOUT;
2376 session_evt_data = session->vep.ev.data.u64;
2379 case SESSION_CTRL_EVT_DISCONNECTED:
2380 disconnected_msg = (session_disconnected_msg_t *) e->data;
2381 sid = vcl_session_index_from_vpp_handle (wrk, disconnected_msg->handle);
2382 if (!(session = vcl_session_get (wrk, sid)))
2385 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
2386 session_evt_data = session->vep.ev.data.u64;
2387 session_events = session->vep.ev.events;
2389 case SESSION_CTRL_EVT_RESET:
2390 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
2391 if (!(session = vcl_session_get (wrk, sid)))
2394 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
2395 session_evt_data = session->vep.ev.data.u64;
2396 session_events = session->vep.ev.events;
2399 VDBG (0, "unhandled: %u", e->event_type);
2405 events[*num_ev].data.u64 = session_evt_data;
2406 if (EPOLLONESHOT & session_events)
2408 session = vcl_session_get (wrk, sid);
2409 session->vep.ev.events = 0;
2416 vcl_epoll_wait_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
2417 struct epoll_event *events, u32 maxevents,
2418 double wait_for_time, u32 * num_ev)
2420 svm_msg_q_msg_t *msg;
2424 if (vec_len (wrk->mq_msg_vector) && svm_msg_q_is_empty (mq))
2425 goto handle_dequeued;
2427 svm_msg_q_lock (mq);
2428 if (svm_msg_q_is_empty (mq))
2432 svm_msg_q_unlock (mq);
2435 else if (wait_for_time < 0)
2437 svm_msg_q_wait (mq);
2441 if (svm_msg_q_timedwait (mq, wait_for_time / 1e3))
2443 svm_msg_q_unlock (mq);
2448 vcl_mq_dequeue_batch (wrk, mq);
2449 svm_msg_q_unlock (mq);
2452 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
2454 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
2455 e = svm_msg_q_msg_data (mq, msg);
2456 vcl_epoll_wait_handle_mq_event (wrk, e, events, num_ev);
2457 svm_msg_q_free_msg (mq, msg);
2458 if (*num_ev == maxevents)
2464 vec_delete (wrk->mq_msg_vector, i, 0);
2470 vppcom_epoll_wait_condvar (vcl_worker_t * wrk, struct epoll_event *events,
2471 int maxevents, u32 n_evts, double wait_for_time)
2473 vcl_cut_through_registration_t *cr;
2474 double total_wait = 0, wait_slice;
2477 wait_for_time = (wait_for_time == -1) ? (double) 10e9 : wait_for_time;
2478 wait_slice = wrk->cut_through_registrations ? 10e-6 : wait_for_time;
2482 vcl_ct_registration_lock (wrk);
2484 pool_foreach (cr, wrk->cut_through_registrations, ({
2485 vcl_epoll_wait_handle_mq (wrk, cr->mq, events, maxevents, 0, &n_evts);
2488 vcl_ct_registration_unlock (wrk);
2490 rv = vcl_epoll_wait_handle_mq (wrk, wrk->app_event_queue, events,
2491 maxevents, n_evts ? 0 : wait_slice,
2494 total_wait += wait_slice;
2498 while (total_wait < wait_for_time);
2503 vppcom_epoll_wait_eventfd (vcl_worker_t * wrk, struct epoll_event *events,
2504 int maxevents, u32 n_evts, double wait_for_time)
2506 vcl_mq_evt_conn_t *mqc;
2507 int __clib_unused n_read;
2511 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
2512 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
2513 vec_len (wrk->mq_events), wait_for_time);
2514 for (i = 0; i < n_mq_evts; i++)
2516 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
2517 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
2518 vcl_epoll_wait_handle_mq (wrk, mqc->mq, events, maxevents, 0, &n_evts);
2521 return (int) n_evts;
2525 vppcom_epoll_wait (uint32_t vep_handle, struct epoll_event *events,
2526 int maxevents, double wait_for_time)
2528 vcl_worker_t *wrk = vcl_worker_get_current ();
2529 vcl_session_t *vep_session;
2533 if (PREDICT_FALSE (maxevents <= 0))
2535 clib_warning ("VCL<%d>: ERROR: Invalid maxevents (%d)!",
2536 getpid (), maxevents);
2537 return VPPCOM_EINVAL;
2540 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
2542 return VPPCOM_EBADFD;
2544 if (PREDICT_FALSE (!vep_session->is_vep))
2546 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
2547 getpid (), vep_handle);
2548 return VPPCOM_EINVAL;
2551 memset (events, 0, sizeof (*events) * maxevents);
2553 if (vec_len (wrk->unhandled_evts_vector))
2555 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
2557 vcl_epoll_wait_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i],
2559 if (n_evts == maxevents)
2566 vec_delete (wrk->unhandled_evts_vector, i, 0);
2569 if (vcm->cfg.use_mq_eventfd)
2570 return vppcom_epoll_wait_eventfd (wrk, events, maxevents, n_evts,
2573 return vppcom_epoll_wait_condvar (wrk, events, maxevents, n_evts,
2578 vppcom_session_attr (uint32_t session_handle, uint32_t op,
2579 void *buffer, uint32_t * buflen)
2581 vcl_worker_t *wrk = vcl_worker_get_current ();
2582 vcl_session_t *session;
2584 u32 *flags = buffer;
2585 vppcom_endpt_t *ep = buffer;
2587 session = vcl_session_get_w_handle (wrk, session_handle);
2589 return VPPCOM_EBADFD;
2593 case VPPCOM_ATTR_GET_NREAD:
2594 rv = vppcom_session_read_ready (session);
2595 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_NREAD: sid %u, nread = %d",
2599 case VPPCOM_ATTR_GET_NWRITE:
2600 rv = vppcom_session_write_ready (session);
2601 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_NWRITE: sid %u, nwrite = %d",
2602 getpid (), session_handle, rv);
2605 case VPPCOM_ATTR_GET_FLAGS:
2606 if (PREDICT_TRUE (buffer && buflen && (*buflen >= sizeof (*flags))))
2608 *flags = O_RDWR | (VCL_SESS_ATTR_TEST (session->attr,
2609 VCL_SESS_ATTR_NONBLOCK));
2610 *buflen = sizeof (*flags);
2611 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_FLAGS: sid %u, flags = 0x%08x, "
2612 "is_nonblocking = %u", getpid (),
2613 session_handle, *flags,
2614 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK));
2620 case VPPCOM_ATTR_SET_FLAGS:
2621 if (PREDICT_TRUE (buffer && buflen && (*buflen == sizeof (*flags))))
2623 if (*flags & O_NONBLOCK)
2624 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK);
2626 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_NONBLOCK);
2628 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_FLAGS: sid %u, flags = 0x%08x,"
2629 " is_nonblocking = %u",
2630 getpid (), session_handle, *flags,
2631 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK));
2637 case VPPCOM_ATTR_GET_PEER_ADDR:
2638 if (PREDICT_TRUE (buffer && buflen &&
2639 (*buflen >= sizeof (*ep)) && ep->ip))
2641 ep->is_ip4 = session->transport.is_ip4;
2642 ep->port = session->transport.rmt_port;
2643 if (session->transport.is_ip4)
2644 clib_memcpy (ep->ip, &session->transport.rmt_ip.ip4,
2645 sizeof (ip4_address_t));
2647 clib_memcpy (ep->ip, &session->transport.rmt_ip.ip6,
2648 sizeof (ip6_address_t));
2649 *buflen = sizeof (*ep);
2650 VDBG (1, "VCL<%d>: VPPCOM_ATTR_GET_PEER_ADDR: sid %u, is_ip4 = %u, "
2651 "addr = %U, port %u", getpid (),
2652 session_handle, ep->is_ip4, format_ip46_address,
2653 &session->transport.rmt_ip,
2654 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
2655 clib_net_to_host_u16 (ep->port));
2661 case VPPCOM_ATTR_GET_LCL_ADDR:
2662 if (PREDICT_TRUE (buffer && buflen &&
2663 (*buflen >= sizeof (*ep)) && ep->ip))
2665 ep->is_ip4 = session->transport.is_ip4;
2666 ep->port = session->transport.lcl_port;
2667 if (session->transport.is_ip4)
2668 clib_memcpy (ep->ip, &session->transport.lcl_ip.ip4,
2669 sizeof (ip4_address_t));
2671 clib_memcpy (ep->ip, &session->transport.lcl_ip.ip6,
2672 sizeof (ip6_address_t));
2673 *buflen = sizeof (*ep);
2674 VDBG (1, "VCL<%d>: VPPCOM_ATTR_GET_LCL_ADDR: sid %u, is_ip4 = %u,"
2675 " addr = %U port %d", getpid (),
2676 session_handle, ep->is_ip4, format_ip46_address,
2677 &session->transport.lcl_ip,
2678 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
2679 clib_net_to_host_u16 (ep->port));
2685 case VPPCOM_ATTR_GET_LIBC_EPFD:
2686 rv = session->libc_epfd;
2687 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd %d",
2691 case VPPCOM_ATTR_SET_LIBC_EPFD:
2692 if (PREDICT_TRUE (buffer && buflen &&
2693 (*buflen == sizeof (session->libc_epfd))))
2695 session->libc_epfd = *(int *) buffer;
2696 *buflen = sizeof (session->libc_epfd);
2698 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd %d, "
2699 "buflen %d", getpid (), session->libc_epfd, *buflen);
2705 case VPPCOM_ATTR_GET_PROTOCOL:
2706 if (buffer && buflen && (*buflen >= sizeof (int)))
2708 *(int *) buffer = session->session_type;
2709 *buflen = sizeof (int);
2711 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_PROTOCOL: %d (%s), buflen %d",
2712 getpid (), *(int *) buffer, *(int *) buffer ? "UDP" : "TCP",
2719 case VPPCOM_ATTR_GET_LISTEN:
2720 if (buffer && buflen && (*buflen >= sizeof (int)))
2722 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2723 VCL_SESS_ATTR_LISTEN);
2724 *buflen = sizeof (int);
2726 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_LISTEN: %d, buflen %d",
2727 getpid (), *(int *) buffer, *buflen);
2733 case VPPCOM_ATTR_GET_ERROR:
2734 if (buffer && buflen && (*buflen >= sizeof (int)))
2736 *(int *) buffer = 0;
2737 *buflen = sizeof (int);
2739 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_ERROR: %d, buflen %d, #VPP-TBD#",
2740 getpid (), *(int *) buffer, *buflen);
2746 case VPPCOM_ATTR_GET_TX_FIFO_LEN:
2747 if (buffer && buflen && (*buflen >= sizeof (u32)))
2751 *(size_t *) buffer = (session->sndbuf_size ? session->sndbuf_size :
2752 session->tx_fifo ? session->tx_fifo->nitems :
2753 vcm->cfg.tx_fifo_size);
2754 *buflen = sizeof (u32);
2756 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TX_FIFO_LEN: %u (0x%x), "
2757 "buflen %d, #VPP-TBD#", getpid (),
2758 *(size_t *) buffer, *(size_t *) buffer, *buflen);
2764 case VPPCOM_ATTR_SET_TX_FIFO_LEN:
2765 if (buffer && buflen && (*buflen == sizeof (u32)))
2768 session->sndbuf_size = *(u32 *) buffer;
2769 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TX_FIFO_LEN: %u (0x%x), "
2770 "buflen %d, #VPP-TBD#", getpid (),
2771 session->sndbuf_size, session->sndbuf_size, *buflen);
2777 case VPPCOM_ATTR_GET_RX_FIFO_LEN:
2778 if (buffer && buflen && (*buflen >= sizeof (u32)))
2782 *(size_t *) buffer = (session->rcvbuf_size ? session->rcvbuf_size :
2783 session->rx_fifo ? session->rx_fifo->nitems :
2784 vcm->cfg.rx_fifo_size);
2785 *buflen = sizeof (u32);
2787 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_RX_FIFO_LEN: %u (0x%x), "
2788 "buflen %d, #VPP-TBD#", getpid (),
2789 *(size_t *) buffer, *(size_t *) buffer, *buflen);
2795 case VPPCOM_ATTR_SET_RX_FIFO_LEN:
2796 if (buffer && buflen && (*buflen == sizeof (u32)))
2799 session->rcvbuf_size = *(u32 *) buffer;
2800 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_RX_FIFO_LEN: %u (0x%x), "
2801 "buflen %d, #VPP-TBD#", getpid (),
2802 session->sndbuf_size, session->sndbuf_size, *buflen);
2808 case VPPCOM_ATTR_GET_REUSEADDR:
2809 if (buffer && buflen && (*buflen >= sizeof (int)))
2812 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2813 VCL_SESS_ATTR_REUSEADDR);
2814 *buflen = sizeof (int);
2816 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_REUSEADDR: %d, "
2817 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
2823 case VPPCOM_ATTR_SET_REUSEADDR:
2824 if (buffer && buflen && (*buflen == sizeof (int)) &&
2825 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
2828 if (*(int *) buffer)
2829 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEADDR);
2831 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEADDR);
2833 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_REUSEADDR: %d, buflen %d,"
2834 " #VPP-TBD#", getpid (),
2835 VCL_SESS_ATTR_TEST (session->attr,
2836 VCL_SESS_ATTR_REUSEADDR), *buflen);
2842 case VPPCOM_ATTR_GET_REUSEPORT:
2843 if (buffer && buflen && (*buflen >= sizeof (int)))
2846 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2847 VCL_SESS_ATTR_REUSEPORT);
2848 *buflen = sizeof (int);
2850 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_REUSEPORT: %d, buflen %d,"
2851 " #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
2857 case VPPCOM_ATTR_SET_REUSEPORT:
2858 if (buffer && buflen && (*buflen == sizeof (int)) &&
2859 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
2862 if (*(int *) buffer)
2863 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEPORT);
2865 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEPORT);
2867 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_REUSEPORT: %d, buflen %d,"
2868 " #VPP-TBD#", getpid (),
2869 VCL_SESS_ATTR_TEST (session->attr,
2870 VCL_SESS_ATTR_REUSEPORT), *buflen);
2876 case VPPCOM_ATTR_GET_BROADCAST:
2877 if (buffer && buflen && (*buflen >= sizeof (int)))
2880 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2881 VCL_SESS_ATTR_BROADCAST);
2882 *buflen = sizeof (int);
2884 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_BROADCAST: %d, buflen %d,"
2885 " #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
2891 case VPPCOM_ATTR_SET_BROADCAST:
2892 if (buffer && buflen && (*buflen == sizeof (int)))
2895 if (*(int *) buffer)
2896 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_BROADCAST);
2898 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_BROADCAST);
2900 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_BROADCAST: %d, buflen %d, "
2901 "#VPP-TBD#", getpid (),
2902 VCL_SESS_ATTR_TEST (session->attr,
2903 VCL_SESS_ATTR_BROADCAST), *buflen);
2909 case VPPCOM_ATTR_GET_V6ONLY:
2910 if (buffer && buflen && (*buflen >= sizeof (int)))
2913 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2914 VCL_SESS_ATTR_V6ONLY);
2915 *buflen = sizeof (int);
2917 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_V6ONLY: %d, buflen %d, "
2918 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
2924 case VPPCOM_ATTR_SET_V6ONLY:
2925 if (buffer && buflen && (*buflen == sizeof (int)))
2928 if (*(int *) buffer)
2929 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_V6ONLY);
2931 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_V6ONLY);
2933 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_V6ONLY: %d, buflen %d, "
2934 "#VPP-TBD#", getpid (),
2935 VCL_SESS_ATTR_TEST (session->attr,
2936 VCL_SESS_ATTR_V6ONLY), *buflen);
2942 case VPPCOM_ATTR_GET_KEEPALIVE:
2943 if (buffer && buflen && (*buflen >= sizeof (int)))
2946 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2947 VCL_SESS_ATTR_KEEPALIVE);
2948 *buflen = sizeof (int);
2950 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_KEEPALIVE: %d, buflen %d, "
2951 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
2957 case VPPCOM_ATTR_SET_KEEPALIVE:
2958 if (buffer && buflen && (*buflen == sizeof (int)))
2961 if (*(int *) buffer)
2962 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_KEEPALIVE);
2964 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_KEEPALIVE);
2966 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_KEEPALIVE: %d, buflen %d, "
2967 "#VPP-TBD#", getpid (),
2968 VCL_SESS_ATTR_TEST (session->attr,
2969 VCL_SESS_ATTR_KEEPALIVE), *buflen);
2975 case VPPCOM_ATTR_GET_TCP_NODELAY:
2976 if (buffer && buflen && (*buflen >= sizeof (int)))
2979 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2980 VCL_SESS_ATTR_TCP_NODELAY);
2981 *buflen = sizeof (int);
2983 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_NODELAY: %d, buflen %d, "
2984 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
2990 case VPPCOM_ATTR_SET_TCP_NODELAY:
2991 if (buffer && buflen && (*buflen == sizeof (int)))
2994 if (*(int *) buffer)
2995 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
2997 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
2999 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_NODELAY: %d, buflen %d, "
3000 "#VPP-TBD#", getpid (),
3001 VCL_SESS_ATTR_TEST (session->attr,
3002 VCL_SESS_ATTR_TCP_NODELAY), *buflen);
3008 case VPPCOM_ATTR_GET_TCP_KEEPIDLE:
3009 if (buffer && buflen && (*buflen >= sizeof (int)))
3012 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3013 VCL_SESS_ATTR_TCP_KEEPIDLE);
3014 *buflen = sizeof (int);
3016 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d, buflen %d, "
3017 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
3023 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
3024 if (buffer && buflen && (*buflen == sizeof (int)))
3027 if (*(int *) buffer)
3028 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
3030 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
3032 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d, buflen %d, "
3033 "#VPP-TBD#", getpid (),
3034 VCL_SESS_ATTR_TEST (session->attr,
3035 VCL_SESS_ATTR_TCP_KEEPIDLE), *buflen);
3041 case VPPCOM_ATTR_GET_TCP_KEEPINTVL:
3042 if (buffer && buflen && (*buflen >= sizeof (int)))
3045 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3046 VCL_SESS_ATTR_TCP_KEEPINTVL);
3047 *buflen = sizeof (int);
3049 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_KEEPINTVL: %d, buflen %d, "
3050 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
3056 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
3057 if (buffer && buflen && (*buflen == sizeof (int)))
3060 if (*(int *) buffer)
3061 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
3063 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
3065 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d, buflen %d, "
3066 "#VPP-TBD#", getpid (),
3067 VCL_SESS_ATTR_TEST (session->attr,
3068 VCL_SESS_ATTR_TCP_KEEPINTVL), *buflen);
3074 case VPPCOM_ATTR_GET_TCP_USER_MSS:
3075 if (buffer && buflen && (*buflen >= sizeof (u32)))
3078 *(u32 *) buffer = session->user_mss;
3079 *buflen = sizeof (int);
3081 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_USER_MSS: %d, buflen %d,"
3082 " #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
3088 case VPPCOM_ATTR_SET_TCP_USER_MSS:
3089 if (buffer && buflen && (*buflen == sizeof (u32)))
3092 session->user_mss = *(u32 *) buffer;
3094 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_USER_MSS: %u, buflen %d, "
3095 "#VPP-TBD#", getpid (), session->user_mss, *buflen);
3110 vppcom_session_recvfrom (uint32_t session_handle, void *buffer,
3111 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3113 vcl_worker_t *wrk = vcl_worker_get_current ();
3115 vcl_session_t *session = 0;
3119 session = vcl_session_get_w_handle (wrk, session_handle);
3120 if (PREDICT_FALSE (!session))
3122 VDBG (0, "VCL<%d>: invalid session, sid (%u) has been closed!",
3123 getpid (), session_handle);
3124 return VPPCOM_EBADFD;
3126 ep->is_ip4 = session->transport.is_ip4;
3127 ep->port = session->transport.rmt_port;
3131 rv = vppcom_session_read (session_handle, buffer, buflen);
3132 else if (flags & MSG_PEEK)
3133 rv = vppcom_session_peek (session_handle, buffer, buflen);
3136 clib_warning ("VCL<%d>: Unsupport flags for recvfrom %d",
3138 return VPPCOM_EAFNOSUPPORT;
3143 if (session->transport.is_ip4)
3144 clib_memcpy (ep->ip, &session->transport.rmt_ip.ip4,
3145 sizeof (ip4_address_t));
3147 clib_memcpy (ep->ip, &session->transport.rmt_ip.ip6,
3148 sizeof (ip6_address_t));
3155 vppcom_session_sendto (uint32_t session_handle, void *buffer,
3156 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3159 return VPPCOM_EINVAL;
3164 return VPPCOM_EINVAL;
3169 // TBD check the flags and do the right thing
3170 VDBG (2, "VCL<%d>: handling flags 0x%u (%d) not implemented yet.",
3171 getpid (), flags, flags);
3174 return (vppcom_session_write (session_handle, buffer, buflen));
3178 vppcom_poll (vcl_poll_t * vp, uint32_t n_sids, double wait_for_time)
3180 vcl_worker_t *wrk = vcl_worker_get_current ();
3181 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
3182 u32 i, keep_trying = 1;
3185 VDBG (3, "VCL<%d>: vp %p, nsids %u, wait_for_time %f",
3186 getpid (), vp, n_sids, wait_for_time);
3189 return VPPCOM_EFAULT;
3193 vcl_session_t *session;
3195 for (i = 0; i < n_sids; i++)
3197 ASSERT (vp[i].revents);
3199 session = vcl_session_get (wrk, vp[i].sid);
3206 if (POLLIN & vp[i].events)
3208 rv = vppcom_session_read_ready (session);
3211 *vp[i].revents |= POLLIN;
3218 case VPPCOM_ECONNRESET:
3219 *vp[i].revents = POLLHUP;
3223 *vp[i].revents = POLLERR;
3230 if (POLLOUT & vp[i].events)
3232 rv = vppcom_session_write_ready (session);
3235 *vp[i].revents |= POLLOUT;
3242 case VPPCOM_ECONNRESET:
3243 *vp[i].revents = POLLHUP;
3247 *vp[i].revents = POLLERR;
3254 if (0) // Note "done:" label used by VCL_SESSION_LOCK_AND_GET()
3256 *vp[i].revents = POLLNVAL;
3260 if (wait_for_time != -1)
3261 keep_trying = (clib_time_now (&wrk->clib_time) <= timeout) ? 1 : 0;
3263 while ((num_ev == 0) && keep_trying);
3265 if (VPPCOM_DEBUG > 3)
3267 clib_warning ("VCL<%d>: returning %d", getpid (), num_ev);
3268 for (i = 0; i < n_sids; i++)
3270 clib_warning ("VCL<%d>: vp[%d].sid %d (0x%x), .events 0x%x, "
3271 ".revents 0x%x", getpid (), i, vp[i].sid, vp[i].sid,
3272 vp[i].events, *vp[i].revents);
3279 vppcom_mq_epoll_fd (void)
3281 vcl_worker_t *wrk = vcl_worker_get_current ();
3282 return wrk->mqs_epfd;
3286 vppcom_session_index (uint32_t session_handle)
3288 return session_handle & 0xFFFFFF;
3292 vppcom_worker_register (void)
3294 if (vcl_worker_alloc_and_init ())
3296 return VPPCOM_EEXIST;
3300 * fd.io coding-style-patch-verification: ON
3303 * eval: (c-set-style "gnu")