2 * Copyright (c) 2017-2019 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 <vcl/vppcom.h>
19 #include <vcl/vcl_debug.h>
20 #include <vcl/vcl_private.h>
21 #include <svm/fifo_segment.h>
23 __thread uword __vcl_worker_index = ~0;
26 vcl_wait_for_segment (u64 segment_handle)
28 vcl_worker_t *wrk = vcl_worker_get_current ();
29 u32 wait_for_seconds = 10, segment_index;
32 if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
35 timeout = clib_time_now (&wrk->clib_time) + wait_for_seconds;
36 while (clib_time_now (&wrk->clib_time) < timeout)
38 segment_index = vcl_segment_table_lookup (segment_handle);
39 if (segment_index != VCL_INVALID_SEGMENT_INDEX)
47 vcl_mq_dequeue_batch (vcl_worker_t * wrk, svm_msg_q_t * mq, u32 n_max_msg)
53 n_msgs = clib_min (svm_msg_q_size (mq), n_max_msg);
54 for (i = 0; i < n_msgs; i++)
56 vec_add2 (wrk->mq_msg_vector, msg, 1);
57 svm_msg_q_sub_w_lock (mq, msg);
63 vppcom_session_state_str (vcl_session_state_t state)
85 case STATE_VPP_CLOSING:
86 st = "STATE_VPP_CLOSING";
89 case STATE_DISCONNECT:
90 st = "STATE_DISCONNECT";
101 case STATE_LISTEN_NO_MQ:
102 st = "STATE_LISTEN_NO_MQ";
106 st = "UNKNOWN_STATE";
114 format_ip4_address (u8 * s, va_list * args)
116 u8 *a = va_arg (*args, u8 *);
117 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
121 format_ip6_address (u8 * s, va_list * args)
123 ip6_address_t *a = va_arg (*args, ip6_address_t *);
124 u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
126 i_max_n_zero = ARRAY_LEN (a->as_u16);
128 i_first_zero = i_max_n_zero;
130 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
132 u32 is_zero = a->as_u16[i] == 0;
133 if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
139 if ((!is_zero && n_zeros > max_n_zeros)
140 || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
142 i_max_n_zero = i_first_zero;
143 max_n_zeros = n_zeros;
144 i_first_zero = ARRAY_LEN (a->as_u16);
149 last_double_colon = 0;
150 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
152 if (i == i_max_n_zero && max_n_zeros > 1)
154 s = format (s, "::");
155 i += max_n_zeros - 1;
156 last_double_colon = 1;
160 s = format (s, "%s%x",
161 (last_double_colon || i == 0) ? "" : ":",
162 clib_net_to_host_u16 (a->as_u16[i]));
163 last_double_colon = 0;
170 /* Format an IP46 address. */
172 format_ip46_address (u8 * s, va_list * args)
174 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
175 ip46_type_t type = va_arg (*args, ip46_type_t);
181 is_ip4 = ip46_address_is_ip4 (ip46);
192 format (s, "%U", format_ip4_address, &ip46->ip4) :
193 format (s, "%U", format_ip6_address, &ip46->ip6);
197 * VPPCOM Utility Functions
202 vcl_send_session_accepted_reply (svm_msg_q_t * mq, u32 context,
203 session_handle_t handle, int retval)
205 app_session_evt_t _app_evt, *app_evt = &_app_evt;
206 session_accepted_reply_msg_t *rmp;
207 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_ACCEPTED_REPLY);
208 rmp = (session_accepted_reply_msg_t *) app_evt->evt->data;
209 rmp->handle = handle;
210 rmp->context = context;
211 rmp->retval = retval;
212 app_send_ctrl_evt_to_vpp (mq, app_evt);
216 vcl_send_session_disconnected_reply (svm_msg_q_t * mq, u32 context,
217 session_handle_t handle, int retval)
219 app_session_evt_t _app_evt, *app_evt = &_app_evt;
220 session_disconnected_reply_msg_t *rmp;
221 app_alloc_ctrl_evt_to_vpp (mq, app_evt,
222 SESSION_CTRL_EVT_DISCONNECTED_REPLY);
223 rmp = (session_disconnected_reply_msg_t *) app_evt->evt->data;
224 rmp->handle = handle;
225 rmp->context = context;
226 rmp->retval = retval;
227 app_send_ctrl_evt_to_vpp (mq, app_evt);
231 vcl_send_session_reset_reply (svm_msg_q_t * mq, u32 context,
232 session_handle_t handle, int retval)
234 app_session_evt_t _app_evt, *app_evt = &_app_evt;
235 session_reset_reply_msg_t *rmp;
236 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_RESET_REPLY);
237 rmp = (session_reset_reply_msg_t *) app_evt->evt->data;
238 rmp->handle = handle;
239 rmp->context = context;
240 rmp->retval = retval;
241 app_send_ctrl_evt_to_vpp (mq, app_evt);
245 vcl_send_session_worker_update (vcl_worker_t * wrk, vcl_session_t * s,
248 app_session_evt_t _app_evt, *app_evt = &_app_evt;
249 session_worker_update_msg_t *mp;
252 mq = vcl_session_vpp_evt_q (wrk, s);
253 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_WORKER_UPDATE);
254 mp = (session_worker_update_msg_t *) app_evt->evt->data;
255 mp->client_index = wrk->my_client_index;
256 mp->handle = s->vpp_handle;
257 mp->req_wrk_index = wrk->vpp_wrk_index;
258 mp->wrk_index = wrk_index;
259 app_send_ctrl_evt_to_vpp (mq, app_evt);
263 vcl_session_accepted_handler (vcl_worker_t * wrk, session_accepted_msg_t * mp,
266 vcl_session_t *session, *listen_session;
267 svm_fifo_t *rx_fifo, *tx_fifo;
271 session = vcl_session_alloc (wrk);
273 listen_session = vcl_session_get (wrk, ls_index);
274 if (listen_session->vpp_handle != mp->listener_handle)
276 VDBG (0, "ERROR: listener handle %lu does not match session %u",
277 mp->listener_handle, ls_index);
281 if (vcl_wait_for_segment (mp->segment_handle))
283 VDBG (0, "ERROR: segment for session %u couldn't be mounted!",
284 session->session_index);
288 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
289 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
290 session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address,
292 rx_fifo->client_session_index = session->session_index;
293 tx_fifo->client_session_index = session->session_index;
294 rx_fifo->client_thread_index = vcl_get_worker_index ();
295 tx_fifo->client_thread_index = vcl_get_worker_index ();
296 vpp_wrk_index = tx_fifo->master_thread_index;
297 vec_validate (wrk->vpp_event_queues, vpp_wrk_index);
298 wrk->vpp_event_queues[vpp_wrk_index] = session->vpp_evt_q;
300 session->vpp_handle = mp->handle;
301 session->vpp_thread_index = rx_fifo->master_thread_index;
302 session->rx_fifo = rx_fifo;
303 session->tx_fifo = tx_fifo;
305 session->session_state = STATE_ACCEPT;
306 session->transport.rmt_port = mp->rmt.port;
307 session->transport.is_ip4 = mp->rmt.is_ip4;
308 clib_memcpy_fast (&session->transport.rmt_ip, &mp->rmt.ip,
309 sizeof (ip46_address_t));
311 vcl_session_table_add_vpp_handle (wrk, mp->handle, session->session_index);
312 session->transport.lcl_port = listen_session->transport.lcl_port;
313 session->transport.lcl_ip = listen_session->transport.lcl_ip;
314 session->session_type = listen_session->session_type;
315 session->is_dgram = vcl_proto_is_dgram (session->session_type);
316 session->listener_index = listen_session->session_index;
317 listen_session->n_accepted_sessions++;
319 VDBG (1, "session %u [0x%llx]: client accept request from %s address %U"
320 " port %d queue %p!", session->session_index, mp->handle,
321 mp->rmt.is_ip4 ? "IPv4" : "IPv6", format_ip46_address, &mp->rmt.ip,
322 mp->rmt.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
323 clib_net_to_host_u16 (mp->rmt.port), session->vpp_evt_q);
324 vcl_evt (VCL_EVT_ACCEPT, session, listen_session, session_index);
326 vcl_send_session_accepted_reply (session->vpp_evt_q, mp->context,
327 session->vpp_handle, 0);
329 return session->session_index;
332 evt_q = uword_to_pointer (mp->vpp_event_queue_address, svm_msg_q_t *);
333 vcl_send_session_accepted_reply (evt_q, mp->context, mp->handle,
334 VNET_API_ERROR_INVALID_ARGUMENT);
335 vcl_session_free (wrk, session);
336 return VCL_INVALID_SESSION_INDEX;
340 vcl_session_connected_handler (vcl_worker_t * wrk,
341 session_connected_msg_t * mp)
343 u32 session_index, vpp_wrk_index;
344 svm_fifo_t *rx_fifo, *tx_fifo;
345 vcl_session_t *session = 0;
347 session_index = mp->context;
348 session = vcl_session_get (wrk, session_index);
351 VDBG (0, "ERROR: vpp handle 0x%llx has no session index (%u)!",
352 mp->handle, session_index);
353 return VCL_INVALID_SESSION_INDEX;
357 VDBG (0, "ERROR: session index %u: connect failed! %U",
358 session_index, format_api_error, ntohl (mp->retval));
359 session->session_state = STATE_FAILED;
360 session->vpp_handle = mp->handle;
361 return session_index;
364 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
365 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
366 if (vcl_wait_for_segment (mp->segment_handle))
368 VDBG (0, "segment for session %u couldn't be mounted!",
369 session->session_index);
370 session->session_state = STATE_FAILED;
371 return VCL_INVALID_SESSION_INDEX;
374 rx_fifo->client_session_index = session_index;
375 tx_fifo->client_session_index = session_index;
376 rx_fifo->client_thread_index = vcl_get_worker_index ();
377 tx_fifo->client_thread_index = vcl_get_worker_index ();
379 session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address,
381 vpp_wrk_index = tx_fifo->master_thread_index;
382 vec_validate (wrk->vpp_event_queues, vpp_wrk_index);
383 wrk->vpp_event_queues[vpp_wrk_index] = session->vpp_evt_q;
387 session->ct_rx_fifo = uword_to_pointer (mp->ct_rx_fifo, svm_fifo_t *);
388 session->ct_tx_fifo = uword_to_pointer (mp->ct_tx_fifo, svm_fifo_t *);
389 if (vcl_wait_for_segment (mp->ct_segment_handle))
391 VDBG (0, "ct segment for session %u couldn't be mounted!",
392 session->session_index);
393 session->session_state = STATE_FAILED;
394 return VCL_INVALID_SESSION_INDEX;
398 session->rx_fifo = rx_fifo;
399 session->tx_fifo = tx_fifo;
400 session->vpp_handle = mp->handle;
401 session->vpp_thread_index = rx_fifo->master_thread_index;
402 session->transport.is_ip4 = mp->lcl.is_ip4;
403 clib_memcpy_fast (&session->transport.lcl_ip, &mp->lcl.ip,
404 sizeof (session->transport.lcl_ip));
405 session->transport.lcl_port = mp->lcl.port;
406 session->session_state = STATE_CONNECT;
408 /* Add it to lookup table */
409 vcl_session_table_add_vpp_handle (wrk, mp->handle, session_index);
411 VDBG (1, "session %u [0x%llx] connected! rx_fifo %p, refcnt %d, tx_fifo %p,"
412 " refcnt %d", session_index, mp->handle, session->rx_fifo,
413 session->rx_fifo->refcnt, session->tx_fifo, session->tx_fifo->refcnt);
415 return session_index;
419 vcl_flag_accepted_session (vcl_session_t * session, u64 handle, u32 flags)
421 vcl_session_msg_t *accepted_msg;
424 for (i = 0; i < vec_len (session->accept_evts_fifo); i++)
426 accepted_msg = &session->accept_evts_fifo[i];
427 if (accepted_msg->accepted_msg.handle == handle)
429 accepted_msg->flags |= flags;
437 vcl_session_reset_handler (vcl_worker_t * wrk,
438 session_reset_msg_t * reset_msg)
440 vcl_session_t *session;
443 sid = vcl_session_index_from_vpp_handle (wrk, reset_msg->handle);
444 session = vcl_session_get (wrk, sid);
447 VDBG (0, "request to reset unknown handle 0x%llx", reset_msg->handle);
448 return VCL_INVALID_SESSION_INDEX;
451 /* Caught a reset before actually accepting the session */
452 if (session->session_state == STATE_LISTEN)
455 if (!vcl_flag_accepted_session (session, reset_msg->handle,
456 VCL_ACCEPTED_F_RESET))
457 VDBG (0, "session was not accepted!");
458 return VCL_INVALID_SESSION_INDEX;
461 session->session_state = STATE_DISCONNECT;
462 VDBG (0, "reset session %u [0x%llx]", sid, reset_msg->handle);
467 vcl_session_bound_handler (vcl_worker_t * wrk, session_bound_msg_t * mp)
469 vcl_session_t *session;
470 u32 sid = mp->context;
472 session = vcl_session_get (wrk, sid);
475 VERR ("session %u [0x%llx]: bind failed: %U", sid, mp->handle,
476 format_api_error, mp->retval);
479 session->session_state = STATE_FAILED;
480 session->vpp_handle = mp->handle;
485 VDBG (0, "ERROR: session %u [0x%llx]: Invalid session index!",
487 return VCL_INVALID_SESSION_INDEX;
491 session->vpp_handle = mp->handle;
492 session->transport.is_ip4 = mp->lcl_is_ip4;
493 clib_memcpy_fast (&session->transport.lcl_ip, mp->lcl_ip,
494 sizeof (ip46_address_t));
495 session->transport.lcl_port = mp->lcl_port;
496 vcl_session_table_add_listener (wrk, mp->handle, sid);
497 session->session_state = STATE_LISTEN;
499 session->vpp_evt_q = uword_to_pointer (mp->vpp_evt_q, svm_msg_q_t *);
500 vec_validate (wrk->vpp_event_queues, 0);
501 wrk->vpp_event_queues[0] = session->vpp_evt_q;
503 if (session->is_dgram)
505 svm_fifo_t *rx_fifo, *tx_fifo;
506 session->vpp_evt_q = uword_to_pointer (mp->vpp_evt_q, svm_msg_q_t *);
507 rx_fifo = uword_to_pointer (mp->rx_fifo, svm_fifo_t *);
508 rx_fifo->client_session_index = sid;
509 tx_fifo = uword_to_pointer (mp->tx_fifo, svm_fifo_t *);
510 tx_fifo->client_session_index = sid;
511 session->rx_fifo = rx_fifo;
512 session->tx_fifo = tx_fifo;
515 VDBG (0, "session %u [0x%llx]: listen succeeded!", sid, mp->handle);
520 vcl_session_unlisten_reply_handler (vcl_worker_t * wrk, void *data)
522 session_unlisten_reply_msg_t *mp = (session_unlisten_reply_msg_t *) data;
525 s = vcl_session_get_w_vpp_handle (wrk, mp->handle);
526 if (!s || s->session_state != STATE_DISCONNECT)
528 VDBG (0, "Unlisten reply with wrong handle %llx", mp->handle);
533 VDBG (0, "ERROR: session %u [0xllx]: unlisten failed: %U",
534 s->session_index, mp->handle, format_api_error, ntohl (mp->retval));
536 if (mp->context != wrk->wrk_index)
537 VDBG (0, "wrong context");
539 vcl_session_table_del_vpp_handle (wrk, mp->handle);
540 vcl_session_free (wrk, s);
543 static vcl_session_t *
544 vcl_session_accepted (vcl_worker_t * wrk, session_accepted_msg_t * msg)
546 vcl_session_msg_t *vcl_msg;
547 vcl_session_t *session;
549 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
550 if (PREDICT_FALSE (session != 0))
551 VWRN ("session overlap handle %lu state %u!", msg->handle,
552 session->session_state);
554 session = vcl_session_table_lookup_listener (wrk, msg->listener_handle);
557 VERR ("couldn't find listen session: listener handle %llx",
558 msg->listener_handle);
562 clib_fifo_add2 (session->accept_evts_fifo, vcl_msg);
563 vcl_msg->accepted_msg = *msg;
564 /* Session handle points to listener until fully accepted by app */
565 vcl_session_table_add_vpp_handle (wrk, msg->handle, session->session_index);
570 static vcl_session_t *
571 vcl_session_disconnected_handler (vcl_worker_t * wrk,
572 session_disconnected_msg_t * msg)
574 vcl_session_t *session;
576 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
579 VDBG (0, "request to disconnect unknown handle 0x%llx", msg->handle);
583 /* Caught a disconnect before actually accepting the session */
584 if (session->session_state == STATE_LISTEN)
586 if (!vcl_flag_accepted_session (session, msg->handle,
587 VCL_ACCEPTED_F_CLOSED))
588 VDBG (0, "session was not accepted!");
592 session->session_state = STATE_VPP_CLOSING;
597 vcl_session_req_worker_update_handler (vcl_worker_t * wrk, void *data)
599 session_req_worker_update_msg_t *msg;
602 msg = (session_req_worker_update_msg_t *) data;
603 s = vcl_session_get_w_vpp_handle (wrk, msg->session_handle);
607 vec_add1 (wrk->pending_session_wrk_updates, s->session_index);
611 vcl_session_worker_update_reply_handler (vcl_worker_t * wrk, void *data)
613 session_worker_update_reply_msg_t *msg;
616 msg = (session_worker_update_reply_msg_t *) data;
617 s = vcl_session_get_w_vpp_handle (wrk, msg->handle);
620 VDBG (0, "unknown handle 0x%llx", msg->handle);
623 if (vcl_wait_for_segment (msg->segment_handle))
625 clib_warning ("segment for session %u couldn't be mounted!",
632 s->rx_fifo = uword_to_pointer (msg->rx_fifo, svm_fifo_t *);
633 s->tx_fifo = uword_to_pointer (msg->tx_fifo, svm_fifo_t *);
634 s->rx_fifo->client_session_index = s->session_index;
635 s->tx_fifo->client_session_index = s->session_index;
636 s->rx_fifo->client_thread_index = wrk->wrk_index;
637 s->tx_fifo->client_thread_index = wrk->wrk_index;
639 s->session_state = STATE_UPDATED;
641 VDBG (0, "session %u[0x%llx] moved to worker %u", s->session_index,
642 s->vpp_handle, wrk->wrk_index);
646 vcl_handle_mq_event (vcl_worker_t * wrk, session_event_t * e)
648 session_disconnected_msg_t *disconnected_msg;
649 vcl_session_t *session;
651 switch (e->event_type)
653 case SESSION_IO_EVT_RX:
654 case SESSION_IO_EVT_TX:
655 session = vcl_session_get (wrk, e->session_index);
656 if (!session || !(session->session_state & STATE_OPEN))
658 vec_add1 (wrk->unhandled_evts_vector, *e);
660 case SESSION_CTRL_EVT_ACCEPTED:
661 vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
663 case SESSION_CTRL_EVT_CONNECTED:
664 vcl_session_connected_handler (wrk,
665 (session_connected_msg_t *) e->data);
667 case SESSION_CTRL_EVT_DISCONNECTED:
668 disconnected_msg = (session_disconnected_msg_t *) e->data;
669 session = vcl_session_disconnected_handler (wrk, disconnected_msg);
672 VDBG (0, "disconnected session %u [0x%llx]", session->session_index,
673 session->vpp_handle);
675 case SESSION_CTRL_EVT_RESET:
676 vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
678 case SESSION_CTRL_EVT_BOUND:
679 vcl_session_bound_handler (wrk, (session_bound_msg_t *) e->data);
681 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
682 vcl_session_unlisten_reply_handler (wrk, e->data);
684 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
685 vcl_session_req_worker_update_handler (wrk, e->data);
687 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
688 vcl_session_worker_update_reply_handler (wrk, e->data);
691 clib_warning ("unhandled %u", e->event_type);
697 vppcom_wait_for_session_state_change (u32 session_index,
698 vcl_session_state_t state,
701 vcl_worker_t *wrk = vcl_worker_get_current ();
702 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
703 vcl_session_t *volatile session;
709 session = vcl_session_get (wrk, session_index);
710 if (PREDICT_FALSE (!session))
712 return VPPCOM_EBADFD;
714 if (session->session_state & state)
718 if (session->session_state & STATE_FAILED)
720 return VPPCOM_ECONNREFUSED;
723 if (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0))
728 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
729 vcl_handle_mq_event (wrk, e);
730 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
732 while (clib_time_now (&wrk->clib_time) < timeout);
734 VDBG (0, "timeout waiting for state 0x%x (%s)", state,
735 vppcom_session_state_str (state));
736 vcl_evt (VCL_EVT_SESSION_TIMEOUT, session, session_state);
738 return VPPCOM_ETIMEDOUT;
742 vcl_handle_pending_wrk_updates (vcl_worker_t * wrk)
744 vcl_session_state_t state;
748 if (PREDICT_TRUE (vec_len (wrk->pending_session_wrk_updates) == 0))
751 vec_foreach (sip, wrk->pending_session_wrk_updates)
753 s = vcl_session_get (wrk, *sip);
754 vcl_send_session_worker_update (wrk, s, wrk->wrk_index);
755 state = s->session_state;
756 vppcom_wait_for_session_state_change (s->session_index, STATE_UPDATED, 5);
757 s->session_state = state;
759 vec_reset_length (wrk->pending_session_wrk_updates);
763 vcl_flush_mq_events (void)
765 vcl_worker_t *wrk = vcl_worker_get_current ();
766 svm_msg_q_msg_t *msg;
771 mq = wrk->app_event_queue;
773 vcl_mq_dequeue_batch (wrk, mq, ~0);
774 svm_msg_q_unlock (mq);
776 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
778 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
779 e = svm_msg_q_msg_data (mq, msg);
780 vcl_handle_mq_event (wrk, e);
781 svm_msg_q_free_msg (mq, msg);
783 vec_reset_length (wrk->mq_msg_vector);
784 vcl_handle_pending_wrk_updates (wrk);
788 vppcom_app_session_enable (void)
792 if (vcm->app_state != STATE_APP_ENABLED)
794 vppcom_send_session_enable_disable (1 /* is_enabled == TRUE */ );
795 rv = vcl_wait_for_app_state_change (STATE_APP_ENABLED);
796 if (PREDICT_FALSE (rv))
798 VDBG (0, "application session enable timed out! returning %d (%s)",
799 rv, vppcom_retval_str (rv));
807 vppcom_app_attach (void)
811 vppcom_app_send_attach ();
812 rv = vcl_wait_for_app_state_change (STATE_APP_ATTACHED);
813 if (PREDICT_FALSE (rv))
815 VDBG (0, "application attach timed out! returning %d (%s)", rv,
816 vppcom_retval_str (rv));
824 vppcom_session_unbind (u32 session_handle)
826 vcl_worker_t *wrk = vcl_worker_get_current ();
827 session_accepted_msg_t *accepted_msg;
828 vcl_session_t *session = 0;
829 vcl_session_msg_t *evt;
832 session = vcl_session_get_w_handle (wrk, session_handle);
834 return VPPCOM_EBADFD;
836 /* Flush pending accept events, if any */
837 while (clib_fifo_elts (session->accept_evts_fifo))
839 clib_fifo_sub2 (session->accept_evts_fifo, evt);
840 accepted_msg = &evt->accepted_msg;
841 vcl_session_table_del_vpp_handle (wrk, accepted_msg->handle);
842 vcl_send_session_accepted_reply (session->vpp_evt_q,
843 accepted_msg->context,
844 session->vpp_handle, -1);
846 clib_fifo_free (session->accept_evts_fifo);
848 vpp_handle = session->vpp_handle;
849 session->vpp_handle = ~0;
850 session->session_state = STATE_DISCONNECT;
852 VDBG (1, "session %u [0x%llx]: sending unbind!", session->session_index,
854 vcl_evt (VCL_EVT_UNBIND, session);
855 vppcom_send_unbind_sock (wrk, vpp_handle);
861 vppcom_session_disconnect (u32 session_handle)
863 vcl_worker_t *wrk = vcl_worker_get_current ();
864 svm_msg_q_t *vpp_evt_q;
865 vcl_session_t *session, *listen_session;
866 vcl_session_state_t state;
869 session = vcl_session_get_w_handle (wrk, session_handle);
871 return VPPCOM_EBADFD;
873 vpp_handle = session->vpp_handle;
874 state = session->session_state;
876 VDBG (1, "session %u [0x%llx] state 0x%x (%s)", session->session_index,
877 vpp_handle, state, vppcom_session_state_str (state));
879 if (PREDICT_FALSE (state & STATE_LISTEN))
881 VDBG (0, "ERROR: Cannot disconnect a listen socket!");
882 return VPPCOM_EBADFD;
885 if (state & STATE_VPP_CLOSING)
887 vpp_evt_q = vcl_session_vpp_evt_q (wrk, session);
888 vcl_send_session_disconnected_reply (vpp_evt_q, wrk->my_client_index,
890 VDBG (1, "session %u [0x%llx]: sending disconnect REPLY...",
891 session->session_index, vpp_handle);
895 VDBG (1, "session %u [0x%llx]: sending disconnect...",
896 session->session_index, vpp_handle);
897 vppcom_send_disconnect_session (vpp_handle);
900 if (session->listener_index != VCL_INVALID_SESSION_INDEX)
902 listen_session = vcl_session_get (wrk, session->listener_index);
903 listen_session->n_accepted_sessions--;
912 * Notify vpp of the disconnect and mark the worker as free. If we're the
913 * last worker, do a full cleanup otherwise, since we're probably a forked
914 * child, avoid syscalls as much as possible. We might've lost privileges.
917 vppcom_app_exit (void)
919 if (!pool_elts (vcm->workers))
921 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
922 vcl_set_worker_index (~0);
924 if (vec_len (vcm->workers) == 1)
925 vppcom_disconnect_from_vpp ();
927 vl_client_send_disconnect (1 /* vpp should cleanup */ );
931 * VPPCOM Public API functions
934 vppcom_app_create (char *app_name)
936 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
941 VDBG (1, "already initialized");
942 return VPPCOM_EEXIST;
946 vppcom_cfg (&vcm->cfg);
949 vcm->main_cpu = pthread_self ();
950 vcm->main_pid = getpid ();
951 vcm->app_name = format (0, "%s", app_name);
952 vppcom_init_error_string_table ();
953 fifo_segment_main_init (&vcm->segment_main, vcl_cfg->segment_baseva,
954 20 /* timeout in secs */ );
955 pool_alloc (vcm->workers, vcl_cfg->max_workers);
956 clib_spinlock_init (&vcm->workers_lock);
957 clib_rwlock_init (&vcm->segment_table_lock);
958 atexit (vppcom_app_exit);
960 /* Allocate default worker */
961 vcl_worker_alloc_and_init ();
963 /* API hookup and connect to VPP */
964 vppcom_api_hookup ();
966 vcm->app_state = STATE_APP_START;
967 rv = vppcom_connect_to_vpp (app_name);
970 VERR ("couldn't connect to VPP!");
973 VDBG (0, "sending session enable");
974 rv = vppcom_app_session_enable ();
977 VERR ("vppcom_app_session_enable() failed!");
981 VDBG (0, "sending app attach");
982 rv = vppcom_app_attach ();
985 VERR ("vppcom_app_attach() failed!");
989 VDBG (0, "app_name '%s', my_client_index %d (0x%x)", app_name,
990 vcm->workers[0].my_client_index, vcm->workers[0].my_client_index);
996 vppcom_app_destroy (void)
999 f64 orig_app_timeout;
1001 if (!pool_elts (vcm->workers))
1004 vcl_evt (VCL_EVT_DETACH, vcm);
1006 if (pool_elts (vcm->workers) == 1)
1008 vppcom_app_send_detach ();
1009 orig_app_timeout = vcm->cfg.app_timeout;
1010 vcm->cfg.app_timeout = 2.0;
1011 rv = vcl_wait_for_app_state_change (STATE_APP_ENABLED);
1012 vcm->cfg.app_timeout = orig_app_timeout;
1013 if (PREDICT_FALSE (rv))
1014 VDBG (0, "application detach timed out! returning %d (%s)", rv,
1015 vppcom_retval_str (rv));
1016 vec_free (vcm->app_name);
1017 vcl_worker_cleanup (vcl_worker_get_current (), 0 /* notify vpp */ );
1021 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
1024 vcl_set_worker_index (~0);
1025 vcl_elog_stop (vcm);
1026 vl_client_disconnect_from_vlib ();
1030 vppcom_session_create (u8 proto, u8 is_nonblocking)
1032 vcl_worker_t *wrk = vcl_worker_get_current ();
1033 vcl_session_t *session;
1035 session = vcl_session_alloc (wrk);
1037 session->session_type = proto;
1038 session->session_state = STATE_START;
1039 session->vpp_handle = ~0;
1040 session->is_dgram = vcl_proto_is_dgram (proto);
1043 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK);
1045 vcl_evt (VCL_EVT_CREATE, session, session_type, session->session_state,
1046 is_nonblocking, session_index);
1048 VDBG (0, "created session %u", session->session_index);
1050 return vcl_session_handle (session);
1054 vcl_session_cleanup (vcl_worker_t * wrk, vcl_session_t * session,
1055 vcl_session_handle_t sh, u8 do_disconnect)
1057 vcl_session_state_t state;
1058 u32 next_sh, vep_sh;
1063 is_vep = session->is_vep;
1064 next_sh = session->vep.next_sh;
1065 vep_sh = session->vep.vep_sh;
1066 state = session->session_state;
1067 vpp_handle = session->vpp_handle;
1069 VDBG (1, "session %u [0x%llx] closing", session->session_index, vpp_handle);
1073 while (next_sh != ~0)
1075 rv = vppcom_epoll_ctl (sh, EPOLL_CTL_DEL, next_sh, 0);
1076 if (PREDICT_FALSE (rv < 0))
1077 VDBG (0, "vpp handle 0x%llx, sh %u: EPOLL_CTL_DEL vep_idx %u"
1078 " failed! rv %d (%s)", vpp_handle, next_sh, vep_sh, rv,
1079 vppcom_retval_str (rv));
1081 next_sh = session->vep.next_sh;
1086 if (session->is_vep_session)
1088 rv = vppcom_epoll_ctl (vep_sh, EPOLL_CTL_DEL, sh, 0);
1090 VDBG (0, "session %u [0x%llx]: EPOLL_CTL_DEL vep_idx %u "
1091 "failed! rv %d (%s)", session->session_index, vpp_handle,
1092 vep_sh, rv, vppcom_retval_str (rv));
1097 VDBG (1, "session %u [0x%llx] disconnect skipped",
1098 session->session_index, vpp_handle);
1102 if (state & STATE_LISTEN)
1104 rv = vppcom_session_unbind (sh);
1105 if (PREDICT_FALSE (rv < 0))
1106 VDBG (0, "session %u [0x%llx]: listener unbind failed! "
1107 "rv %d (%s)", session->session_index, vpp_handle, rv,
1108 vppcom_retval_str (rv));
1111 else if ((state & STATE_OPEN)
1112 || (vcl_session_is_connectable_listener (wrk, session)))
1114 rv = vppcom_session_disconnect (sh);
1115 if (PREDICT_FALSE (rv < 0))
1116 VDBG (0, "ERROR: session %u [0x%llx]: disconnect failed!"
1117 " rv %d (%s)", session->session_index, vpp_handle,
1118 rv, vppcom_retval_str (rv));
1120 else if (state == STATE_DISCONNECT)
1122 svm_msg_q_t *mq = vcl_session_vpp_evt_q (wrk, session);
1123 vcl_send_session_reset_reply (mq, wrk->my_client_index,
1124 session->vpp_handle, 0);
1128 VDBG (0, "session %u [0x%llx] removed", session->session_index, vpp_handle);
1131 vcl_session_table_del_vpp_handle (wrk, vpp_handle);
1132 vcl_session_free (wrk, session);
1133 vcl_evt (VCL_EVT_CLOSE, session, rv);
1139 vppcom_session_close (uint32_t session_handle)
1141 vcl_worker_t *wrk = vcl_worker_get_current ();
1142 vcl_session_t *session;
1144 session = vcl_session_get_w_handle (wrk, session_handle);
1146 return VPPCOM_EBADFD;
1147 return vcl_session_cleanup (wrk, session, session_handle,
1148 1 /* do_disconnect */ );
1152 vppcom_session_bind (uint32_t session_handle, vppcom_endpt_t * ep)
1154 vcl_worker_t *wrk = vcl_worker_get_current ();
1155 vcl_session_t *session = 0;
1158 return VPPCOM_EINVAL;
1160 session = vcl_session_get_w_handle (wrk, session_handle);
1162 return VPPCOM_EBADFD;
1164 if (session->is_vep)
1166 VDBG (0, "ERROR: cannot bind to epoll session %u!",
1167 session->session_index);
1168 return VPPCOM_EBADFD;
1171 session->transport.is_ip4 = ep->is_ip4;
1173 clib_memcpy_fast (&session->transport.lcl_ip.ip4, ep->ip,
1174 sizeof (ip4_address_t));
1176 clib_memcpy_fast (&session->transport.lcl_ip.ip6, ep->ip,
1177 sizeof (ip6_address_t));
1178 session->transport.lcl_port = ep->port;
1180 VDBG (0, "session %u handle %u: binding to local %s address %U port %u, "
1181 "proto %s", session->session_index, session_handle,
1182 session->transport.is_ip4 ? "IPv4" : "IPv6",
1183 format_ip46_address, &session->transport.lcl_ip,
1184 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
1185 clib_net_to_host_u16 (session->transport.lcl_port),
1186 vppcom_proto_str (session->session_type));
1187 vcl_evt (VCL_EVT_BIND, session);
1189 if (session->session_type == VPPCOM_PROTO_UDP)
1190 vppcom_session_listen (session_handle, 10);
1196 vppcom_session_listen (uint32_t listen_sh, uint32_t q_len)
1198 vcl_worker_t *wrk = vcl_worker_get_current ();
1199 vcl_session_t *listen_session = 0;
1200 u64 listen_vpp_handle;
1203 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
1204 if (!listen_session || listen_session->is_vep)
1205 return VPPCOM_EBADFD;
1207 if (q_len == 0 || q_len == ~0)
1208 q_len = vcm->cfg.listen_queue_size;
1210 listen_vpp_handle = listen_session->vpp_handle;
1211 if (listen_session->session_state & STATE_LISTEN)
1213 VDBG (0, "session %u [0x%llx]: already in listen state!",
1214 listen_sh, listen_vpp_handle);
1218 VDBG (0, "session %u: sending vpp listen request...", listen_sh);
1221 * Send listen request to vpp and wait for reply
1223 vppcom_send_bind_sock (listen_session);
1224 rv = vppcom_wait_for_session_state_change (listen_session->session_index,
1226 vcm->cfg.session_timeout);
1228 if (PREDICT_FALSE (rv))
1230 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
1231 VDBG (0, "session %u [0x%llx]: listen failed! returning %d (%s)",
1232 listen_sh, listen_session->vpp_handle, rv,
1233 vppcom_retval_str (rv));
1241 vppcom_session_tls_add_cert (uint32_t session_handle, char *cert,
1245 vcl_worker_t *wrk = vcl_worker_get_current ();
1246 vcl_session_t *session = 0;
1248 session = vcl_session_get_w_handle (wrk, session_handle);
1250 return VPPCOM_EBADFD;
1252 if (cert_len == 0 || cert_len == ~0)
1253 return VPPCOM_EBADFD;
1256 * Send listen request to vpp and wait for reply
1258 vppcom_send_application_tls_cert_add (session, cert, cert_len);
1265 vppcom_session_tls_add_key (uint32_t session_handle, char *key,
1269 vcl_worker_t *wrk = vcl_worker_get_current ();
1270 vcl_session_t *session = 0;
1272 session = vcl_session_get_w_handle (wrk, session_handle);
1274 return VPPCOM_EBADFD;
1276 if (key_len == 0 || key_len == ~0)
1277 return VPPCOM_EBADFD;
1280 * Send listen request to vpp and wait for reply
1282 vppcom_send_application_tls_key_add (session, key, key_len);
1290 validate_args_session_accept_ (vcl_worker_t * wrk, vcl_session_t * ls)
1294 VDBG (0, "ERROR: cannot accept on epoll session %u!",
1296 return VPPCOM_EBADFD;
1299 if ((ls->session_state != STATE_LISTEN)
1300 && (!vcl_session_is_connectable_listener (wrk, ls)))
1303 "ERROR: session [0x%llx]: not in listen state! state 0x%x"
1304 " (%s)", ls->vpp_handle, ls->session_state,
1305 vppcom_session_state_str (ls->session_state));
1306 return VPPCOM_EBADFD;
1312 vppcom_unformat_proto (uint8_t * proto, char *proto_str)
1314 if (!strcmp (proto_str, "TCP"))
1315 *proto = VPPCOM_PROTO_TCP;
1316 else if (!strcmp (proto_str, "tcp"))
1317 *proto = VPPCOM_PROTO_TCP;
1318 else if (!strcmp (proto_str, "UDP"))
1319 *proto = VPPCOM_PROTO_UDP;
1320 else if (!strcmp (proto_str, "udp"))
1321 *proto = VPPCOM_PROTO_UDP;
1322 else if (!strcmp (proto_str, "UDPC"))
1323 *proto = VPPCOM_PROTO_UDPC;
1324 else if (!strcmp (proto_str, "udpc"))
1325 *proto = VPPCOM_PROTO_UDPC;
1326 else if (!strcmp (proto_str, "SCTP"))
1327 *proto = VPPCOM_PROTO_SCTP;
1328 else if (!strcmp (proto_str, "sctp"))
1329 *proto = VPPCOM_PROTO_SCTP;
1330 else if (!strcmp (proto_str, "TLS"))
1331 *proto = VPPCOM_PROTO_TLS;
1332 else if (!strcmp (proto_str, "tls"))
1333 *proto = VPPCOM_PROTO_TLS;
1334 else if (!strcmp (proto_str, "QUIC"))
1335 *proto = VPPCOM_PROTO_QUIC;
1336 else if (!strcmp (proto_str, "quic"))
1337 *proto = VPPCOM_PROTO_QUIC;
1344 vppcom_session_accept (uint32_t listen_session_handle, vppcom_endpt_t * ep,
1347 u32 client_session_index = ~0, listen_session_index, accept_flags = 0;
1348 vcl_worker_t *wrk = vcl_worker_get_current ();
1349 session_accepted_msg_t accepted_msg;
1350 vcl_session_t *listen_session = 0;
1351 vcl_session_t *client_session = 0;
1352 vcl_session_msg_t *evt;
1353 svm_msg_q_msg_t msg;
1358 listen_session = vcl_session_get_w_handle (wrk, listen_session_handle);
1359 if (!listen_session)
1360 return VPPCOM_EBADFD;
1362 listen_session_index = listen_session->session_index;
1363 if ((rv = validate_args_session_accept_ (wrk, listen_session)))
1366 if (clib_fifo_elts (listen_session->accept_evts_fifo))
1368 clib_fifo_sub2 (listen_session->accept_evts_fifo, evt);
1369 accept_flags = evt->flags;
1370 accepted_msg = evt->accepted_msg;
1374 is_nonblocking = VCL_SESS_ATTR_TEST (listen_session->attr,
1375 VCL_SESS_ATTR_NONBLOCK);
1376 if (svm_msg_q_is_empty (wrk->app_event_queue) && is_nonblocking)
1377 return VPPCOM_EAGAIN;
1381 if (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_WAIT, 0))
1382 return VPPCOM_EAGAIN;
1384 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
1385 if (e->event_type != SESSION_CTRL_EVT_ACCEPTED)
1387 VDBG (0, "discarded event: %u", e->event_type);
1388 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
1391 clib_memcpy_fast (&accepted_msg, e->data, sizeof (accepted_msg));
1392 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
1398 client_session_index = vcl_session_accepted_handler (wrk, &accepted_msg,
1399 listen_session_index);
1400 if (client_session_index == VCL_INVALID_SESSION_INDEX)
1401 return VPPCOM_ECONNABORTED;
1403 listen_session = vcl_session_get (wrk, listen_session_index);
1404 client_session = vcl_session_get (wrk, client_session_index);
1406 if (flags & O_NONBLOCK)
1407 VCL_SESS_ATTR_SET (client_session->attr, VCL_SESS_ATTR_NONBLOCK);
1409 VDBG (1, "listener %u [0x%llx]: Got a connect request! session %u [0x%llx],"
1410 " flags %d, is_nonblocking %u", listen_session->session_index,
1411 listen_session->vpp_handle, client_session_index,
1412 client_session->vpp_handle, flags,
1413 VCL_SESS_ATTR_TEST (client_session->attr, VCL_SESS_ATTR_NONBLOCK));
1417 ep->is_ip4 = client_session->transport.is_ip4;
1418 ep->port = client_session->transport.rmt_port;
1419 if (client_session->transport.is_ip4)
1420 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip4,
1421 sizeof (ip4_address_t));
1423 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip6,
1424 sizeof (ip6_address_t));
1427 VDBG (0, "listener %u [0x%llx] accepted %u [0x%llx] peer: %U:%u "
1428 "local: %U:%u", listen_session_handle, listen_session->vpp_handle,
1429 client_session_index, client_session->vpp_handle,
1430 format_ip46_address, &client_session->transport.rmt_ip,
1431 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
1432 clib_net_to_host_u16 (client_session->transport.rmt_port),
1433 format_ip46_address, &client_session->transport.lcl_ip,
1434 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
1435 clib_net_to_host_u16 (client_session->transport.lcl_port));
1436 vcl_evt (VCL_EVT_ACCEPT, client_session, listen_session,
1437 client_session_index);
1440 * Session might have been closed already
1444 if (accept_flags & VCL_ACCEPTED_F_CLOSED)
1445 client_session->session_state = STATE_VPP_CLOSING;
1446 else if (accept_flags & VCL_ACCEPTED_F_RESET)
1447 client_session->session_state = STATE_DISCONNECT;
1449 return vcl_session_handle (client_session);
1453 vppcom_session_connect (uint32_t session_handle, vppcom_endpt_t * server_ep)
1455 vcl_worker_t *wrk = vcl_worker_get_current ();
1456 vcl_session_t *session = 0;
1460 session = vcl_session_get_w_handle (wrk, session_handle);
1462 return VPPCOM_EBADFD;
1463 session_index = session->session_index;
1465 if (PREDICT_FALSE (session->is_vep))
1467 VDBG (0, "ERROR: cannot connect epoll session %u!",
1468 session->session_index);
1469 return VPPCOM_EBADFD;
1472 if (PREDICT_FALSE (session->session_state & CLIENT_STATE_OPEN))
1474 VDBG (0, "session handle %u [0x%llx]: session already "
1475 "connected to %s %U port %d proto %s, state 0x%x (%s)",
1476 session_handle, session->vpp_handle,
1477 session->transport.is_ip4 ? "IPv4" : "IPv6", format_ip46_address,
1478 &session->transport.rmt_ip, session->transport.is_ip4 ?
1479 IP46_TYPE_IP4 : IP46_TYPE_IP6,
1480 clib_net_to_host_u16 (session->transport.rmt_port),
1481 vppcom_proto_str (session->session_type), session->session_state,
1482 vppcom_session_state_str (session->session_state));
1486 session->transport.is_ip4 = server_ep->is_ip4;
1487 if (session->transport.is_ip4)
1488 clib_memcpy_fast (&session->transport.rmt_ip.ip4, server_ep->ip,
1489 sizeof (ip4_address_t));
1491 clib_memcpy_fast (&session->transport.rmt_ip.ip6, server_ep->ip,
1492 sizeof (ip6_address_t));
1493 session->transport.rmt_port = server_ep->port;
1494 session->parent_handle = VCL_INVALID_SESSION_HANDLE;
1496 VDBG (0, "session handle %u: connecting to server %s %U "
1497 "port %d proto %s", session_handle,
1498 session->transport.is_ip4 ? "IPv4" : "IPv6",
1499 format_ip46_address,
1500 &session->transport.rmt_ip, session->transport.is_ip4 ?
1501 IP46_TYPE_IP4 : IP46_TYPE_IP6,
1502 clib_net_to_host_u16 (session->transport.rmt_port),
1503 vppcom_proto_str (session->session_type));
1506 * Send connect request and wait for reply from vpp
1508 vppcom_send_connect_sock (session);
1509 rv = vppcom_wait_for_session_state_change (session_index, STATE_CONNECT,
1510 vcm->cfg.session_timeout);
1512 session = vcl_session_get (wrk, session_index);
1513 VDBG (0, "session %u [0x%llx]: connect %s!", session->session_index,
1514 session->vpp_handle, rv ? "failed" : "succeeded");
1520 vppcom_session_stream_connect (uint32_t session_handle,
1521 uint32_t parent_session_handle)
1523 vcl_worker_t *wrk = vcl_worker_get_current ();
1524 vcl_session_t *session, *parent_session;
1525 u32 session_index, parent_session_index;
1528 session = vcl_session_get_w_handle (wrk, session_handle);
1530 return VPPCOM_EBADFD;
1531 parent_session = vcl_session_get_w_handle (wrk, parent_session_handle);
1532 if (!parent_session)
1533 return VPPCOM_EBADFD;
1535 session_index = session->session_index;
1536 parent_session_index = parent_session->session_index;
1537 if (PREDICT_FALSE (session->is_vep))
1539 VDBG (0, "ERROR: cannot connect epoll session %u!",
1540 session->session_index);
1541 return VPPCOM_EBADFD;
1544 if (PREDICT_FALSE (session->session_state & CLIENT_STATE_OPEN))
1546 VDBG (0, "session handle %u [0x%llx]: session already "
1547 "connected to session %u [0x%llx] proto %s, state 0x%x (%s)",
1548 session_handle, session->vpp_handle,
1549 parent_session_handle, parent_session->vpp_handle,
1550 vppcom_proto_str (session->session_type), session->session_state,
1551 vppcom_session_state_str (session->session_state));
1555 /* Connect to quic session specifics */
1556 session->transport.is_ip4 = parent_session->transport.is_ip4;
1557 session->transport.rmt_ip.ip4.as_u32 = (uint32_t) 1;
1558 session->transport.rmt_port = 0;
1559 session->parent_handle = parent_session->vpp_handle;
1561 VDBG (0, "session handle %u: connecting to session %u [0x%llx]",
1562 session_handle, parent_session_handle, parent_session->vpp_handle);
1565 * Send connect request and wait for reply from vpp
1567 vppcom_send_connect_sock (session);
1568 rv = vppcom_wait_for_session_state_change (session_index, STATE_CONNECT,
1569 vcm->cfg.session_timeout);
1571 session->listener_index = parent_session_index;
1572 parent_session = vcl_session_get_w_handle (wrk, parent_session_handle);
1574 parent_session->n_accepted_sessions++;
1576 session = vcl_session_get (wrk, session_index);
1577 VDBG (0, "session %u [0x%llx]: connect %s!", session->session_index,
1578 session->vpp_handle, rv ? "failed" : "succeeded");
1584 vcl_is_rx_evt_for_session (session_event_t * e, u32 sid, u8 is_ct)
1586 return (e->event_type == SESSION_IO_EVT_RX && e->session_index == sid);
1590 vppcom_session_read_internal (uint32_t session_handle, void *buf, int n,
1593 vcl_worker_t *wrk = vcl_worker_get_current ();
1594 int n_read = 0, is_nonblocking;
1595 vcl_session_t *s = 0;
1596 svm_fifo_t *rx_fifo;
1597 svm_msg_q_msg_t msg;
1602 if (PREDICT_FALSE (!buf))
1603 return VPPCOM_EINVAL;
1605 s = vcl_session_get_w_handle (wrk, session_handle);
1606 if (PREDICT_FALSE (!s || s->is_vep))
1607 return VPPCOM_EBADFD;
1609 if (PREDICT_FALSE (!vcl_session_is_open (s)))
1611 VDBG (0, "session %u[0x%llx] is not open! state 0x%x (%s)",
1612 s->session_index, s->vpp_handle, s->session_state,
1613 vppcom_session_state_str (s->session_state));
1614 return vcl_session_closed_error (s);
1617 is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
1618 is_ct = vcl_session_is_ct (s);
1619 mq = wrk->app_event_queue;
1620 rx_fifo = is_ct ? s->ct_rx_fifo : s->rx_fifo;
1623 if (svm_fifo_is_empty_cons (rx_fifo))
1627 svm_fifo_unset_event (s->rx_fifo);
1628 return VPPCOM_EWOULDBLOCK;
1630 while (svm_fifo_is_empty_cons (rx_fifo))
1632 if (vcl_session_is_closing (s))
1633 return vcl_session_closing_error (s);
1635 svm_fifo_unset_event (s->rx_fifo);
1636 svm_msg_q_lock (mq);
1637 if (svm_msg_q_is_empty (mq))
1638 svm_msg_q_wait (mq);
1640 svm_msg_q_sub_w_lock (mq, &msg);
1641 e = svm_msg_q_msg_data (mq, &msg);
1642 svm_msg_q_unlock (mq);
1643 if (!vcl_is_rx_evt_for_session (e, s->session_index, is_ct))
1644 vcl_handle_mq_event (wrk, e);
1645 svm_msg_q_free_msg (mq, &msg);
1650 n_read = app_recv_dgram_raw (rx_fifo, buf, n, &s->transport, 0, peek);
1652 n_read = app_recv_stream_raw (rx_fifo, buf, n, 0, peek);
1654 if (svm_fifo_is_empty_cons (rx_fifo))
1655 svm_fifo_unset_event (s->rx_fifo);
1657 /* Cut-through sessions might request tx notifications on rx fifos */
1658 if (PREDICT_FALSE (rx_fifo->want_deq_ntf))
1660 app_send_io_evt_to_vpp (s->vpp_evt_q, s->rx_fifo->master_session_index,
1661 SESSION_IO_EVT_RX, SVM_Q_WAIT);
1662 svm_fifo_reset_has_deq_ntf (s->rx_fifo);
1665 VDBG (2, "session %u[0x%llx]: read %d bytes from (%p)", s->session_index,
1666 s->vpp_handle, n_read, rx_fifo);
1672 vppcom_session_read (uint32_t session_handle, void *buf, size_t n)
1674 return (vppcom_session_read_internal (session_handle, buf, n, 0));
1678 vppcom_session_peek (uint32_t session_handle, void *buf, int n)
1680 return (vppcom_session_read_internal (session_handle, buf, n, 1));
1684 vppcom_session_read_segments (uint32_t session_handle,
1685 vppcom_data_segments_t ds)
1687 vcl_worker_t *wrk = vcl_worker_get_current ();
1688 int n_read = 0, is_nonblocking;
1689 vcl_session_t *s = 0;
1690 svm_fifo_t *rx_fifo;
1691 svm_msg_q_msg_t msg;
1696 s = vcl_session_get_w_handle (wrk, session_handle);
1697 if (PREDICT_FALSE (!s || s->is_vep))
1698 return VPPCOM_EBADFD;
1700 if (PREDICT_FALSE (!vcl_session_is_open (s)))
1701 return vcl_session_closed_error (s);
1703 is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
1704 is_ct = vcl_session_is_ct (s);
1705 mq = is_ct ? s->our_evt_q : wrk->app_event_queue;
1706 rx_fifo = s->rx_fifo;
1710 svm_fifo_unset_event (s->rx_fifo);
1712 if (svm_fifo_is_empty_cons (rx_fifo))
1716 svm_fifo_unset_event (rx_fifo);
1717 return VPPCOM_EWOULDBLOCK;
1719 while (svm_fifo_is_empty_cons (rx_fifo))
1721 if (vcl_session_is_closing (s))
1722 return vcl_session_closing_error (s);
1724 svm_fifo_unset_event (rx_fifo);
1725 svm_msg_q_lock (mq);
1726 if (svm_msg_q_is_empty (mq))
1727 svm_msg_q_wait (mq);
1729 svm_msg_q_sub_w_lock (mq, &msg);
1730 e = svm_msg_q_msg_data (mq, &msg);
1731 svm_msg_q_unlock (mq);
1732 if (!vcl_is_rx_evt_for_session (e, s->session_index, is_ct))
1733 vcl_handle_mq_event (wrk, e);
1734 svm_msg_q_free_msg (mq, &msg);
1738 n_read = svm_fifo_segments (rx_fifo, (svm_fifo_seg_t *) ds);
1739 svm_fifo_unset_event (rx_fifo);
1745 vppcom_session_free_segments (uint32_t session_handle,
1746 vppcom_data_segments_t ds)
1748 vcl_worker_t *wrk = vcl_worker_get_current ();
1751 s = vcl_session_get_w_handle (wrk, session_handle);
1752 if (PREDICT_FALSE (!s || s->is_vep))
1755 svm_fifo_segments_free (s->rx_fifo, (svm_fifo_seg_t *) ds);
1759 vppcom_data_segment_copy (void *buf, vppcom_data_segments_t ds, u32 max_bytes)
1761 u32 first_copy = clib_min (ds[0].len, max_bytes);
1762 clib_memcpy_fast (buf, ds[0].data, first_copy);
1763 if (first_copy < max_bytes)
1765 clib_memcpy_fast (buf + first_copy, ds[1].data,
1766 clib_min (ds[1].len, max_bytes - first_copy));
1772 vcl_is_tx_evt_for_session (session_event_t * e, u32 sid, u8 is_ct)
1774 return (e->event_type == SESSION_IO_EVT_TX && e->session_index == sid);
1778 vppcom_session_write_inline (uint32_t session_handle, void *buf, size_t n,
1781 vcl_worker_t *wrk = vcl_worker_get_current ();
1782 int n_write, is_nonblocking;
1783 vcl_session_t *s = 0;
1784 session_evt_type_t et;
1785 svm_msg_q_msg_t msg;
1786 svm_fifo_t *tx_fifo;
1791 if (PREDICT_FALSE (!buf))
1792 return VPPCOM_EINVAL;
1794 s = vcl_session_get_w_handle (wrk, session_handle);
1795 if (PREDICT_FALSE (!s))
1796 return VPPCOM_EBADFD;
1798 if (PREDICT_FALSE (s->is_vep))
1800 VDBG (0, "ERROR: session %u [0x%llx]: cannot write to an epoll"
1801 " session!", s->session_index, s->vpp_handle);
1802 return VPPCOM_EBADFD;
1805 if (PREDICT_FALSE (!vcl_session_is_open (s)))
1807 VDBG (1, "session %u [0x%llx]: is not open! state 0x%x (%s)",
1808 s->session_index, s->vpp_handle, s->session_state,
1809 vppcom_session_state_str (s->session_state));
1810 return vcl_session_closed_error (s);;
1813 is_ct = vcl_session_is_ct (s);
1814 tx_fifo = is_ct ? s->ct_tx_fifo : s->tx_fifo;
1815 is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
1817 mq = wrk->app_event_queue;
1818 if (svm_fifo_is_full_prod (tx_fifo))
1822 return VPPCOM_EWOULDBLOCK;
1824 while (svm_fifo_is_full_prod (tx_fifo))
1826 svm_fifo_add_want_deq_ntf (tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
1827 if (vcl_session_is_closing (s))
1828 return vcl_session_closing_error (s);
1829 svm_msg_q_lock (mq);
1830 if (svm_msg_q_is_empty (mq))
1831 svm_msg_q_wait (mq);
1833 svm_msg_q_sub_w_lock (mq, &msg);
1834 e = svm_msg_q_msg_data (mq, &msg);
1835 svm_msg_q_unlock (mq);
1837 if (!vcl_is_tx_evt_for_session (e, s->session_index, is_ct))
1838 vcl_handle_mq_event (wrk, e);
1839 svm_msg_q_free_msg (mq, &msg);
1843 et = SESSION_IO_EVT_TX;
1844 if (is_flush && !is_ct)
1845 et = SESSION_IO_EVT_TX_FLUSH;
1848 n_write = app_send_dgram_raw (tx_fifo, &s->transport,
1849 s->vpp_evt_q, buf, n, et,
1850 0 /* do_evt */ , SVM_Q_WAIT);
1852 n_write = app_send_stream_raw (tx_fifo, s->vpp_evt_q, buf, n, et,
1853 0 /* do_evt */ , SVM_Q_WAIT);
1855 if (svm_fifo_set_event (s->tx_fifo))
1856 app_send_io_evt_to_vpp (s->vpp_evt_q, s->tx_fifo->master_session_index,
1859 ASSERT (n_write > 0);
1861 VDBG (2, "session %u [0x%llx]: wrote %d bytes", s->session_index,
1862 s->vpp_handle, n_write);
1868 vppcom_session_write (uint32_t session_handle, void *buf, size_t n)
1870 return vppcom_session_write_inline (session_handle, buf, n,
1875 vppcom_session_write_msg (uint32_t session_handle, void *buf, size_t n)
1877 return vppcom_session_write_inline (session_handle, buf, n,
1881 #define vcl_fifo_rx_evt_valid_or_break(_s) \
1882 if (PREDICT_FALSE (svm_fifo_is_empty (_s->rx_fifo))) \
1884 if (!vcl_session_is_ct (_s)) \
1886 svm_fifo_unset_event (_s->rx_fifo); \
1887 if (svm_fifo_is_empty (_s->rx_fifo)) \
1890 else if (svm_fifo_is_empty (_s->ct_rx_fifo)) \
1892 svm_fifo_unset_event (_s->ct_rx_fifo); \
1893 if (svm_fifo_is_empty (_s->ct_rx_fifo)) \
1899 vcl_select_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
1900 unsigned long n_bits, unsigned long *read_map,
1901 unsigned long *write_map,
1902 unsigned long *except_map, u32 * bits_set)
1904 session_disconnected_msg_t *disconnected_msg;
1905 session_connected_msg_t *connected_msg;
1906 vcl_session_t *session;
1909 switch (e->event_type)
1911 case SESSION_IO_EVT_RX:
1912 sid = e->session_index;
1913 session = vcl_session_get (wrk, sid);
1916 vcl_fifo_rx_evt_valid_or_break (session);
1917 if (sid < n_bits && read_map)
1919 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
1923 case SESSION_IO_EVT_TX:
1924 sid = e->session_index;
1925 session = vcl_session_get (wrk, sid);
1928 if (sid < n_bits && write_map)
1930 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
1934 case SESSION_CTRL_EVT_ACCEPTED:
1935 session = vcl_session_accepted (wrk,
1936 (session_accepted_msg_t *) e->data);
1939 sid = session->session_index;
1940 if (sid < n_bits && read_map)
1942 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
1946 case SESSION_CTRL_EVT_CONNECTED:
1947 connected_msg = (session_connected_msg_t *) e->data;
1948 vcl_session_connected_handler (wrk, connected_msg);
1950 case SESSION_CTRL_EVT_DISCONNECTED:
1951 disconnected_msg = (session_disconnected_msg_t *) e->data;
1952 session = vcl_session_disconnected_handler (wrk, disconnected_msg);
1955 sid = session->session_index;
1956 if (sid < n_bits && except_map)
1958 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
1962 case SESSION_CTRL_EVT_RESET:
1963 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
1964 if (sid < n_bits && except_map)
1966 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
1970 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
1971 vcl_session_unlisten_reply_handler (wrk, e->data);
1973 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
1974 vcl_session_worker_update_reply_handler (wrk, e->data);
1976 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
1977 vcl_session_req_worker_update_handler (wrk, e->data);
1980 clib_warning ("unhandled: %u", e->event_type);
1986 vcl_select_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
1987 unsigned long n_bits, unsigned long *read_map,
1988 unsigned long *write_map, unsigned long *except_map,
1989 double time_to_wait, u32 * bits_set)
1991 svm_msg_q_msg_t *msg;
1995 svm_msg_q_lock (mq);
1996 if (svm_msg_q_is_empty (mq))
2000 svm_msg_q_unlock (mq);
2006 svm_msg_q_unlock (mq);
2009 else if (time_to_wait < 0)
2011 svm_msg_q_wait (mq);
2015 if (svm_msg_q_timedwait (mq, time_to_wait))
2017 svm_msg_q_unlock (mq);
2022 vcl_mq_dequeue_batch (wrk, mq, ~0);
2023 svm_msg_q_unlock (mq);
2025 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
2027 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
2028 e = svm_msg_q_msg_data (mq, msg);
2029 vcl_select_handle_mq_event (wrk, e, n_bits, read_map, write_map,
2030 except_map, bits_set);
2031 svm_msg_q_free_msg (mq, msg);
2033 vec_reset_length (wrk->mq_msg_vector);
2034 vcl_handle_pending_wrk_updates (wrk);
2039 vppcom_select_condvar (vcl_worker_t * wrk, int n_bits,
2040 vcl_si_set * read_map, vcl_si_set * write_map,
2041 vcl_si_set * except_map, double time_to_wait,
2044 double wait = 0, start = 0;
2048 wait = time_to_wait;
2049 start = clib_time_now (&wrk->clib_time);
2054 vcl_select_handle_mq (wrk, wrk->app_event_queue, n_bits, read_map,
2055 write_map, except_map, wait, bits_set);
2061 wait = wait - (clib_time_now (&wrk->clib_time) - start);
2069 vppcom_select_eventfd (vcl_worker_t * wrk, int n_bits,
2070 vcl_si_set * read_map, vcl_si_set * write_map,
2071 vcl_si_set * except_map, double time_to_wait,
2074 vcl_mq_evt_conn_t *mqc;
2075 int __clib_unused n_read;
2079 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
2080 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
2081 vec_len (wrk->mq_events), time_to_wait);
2082 for (i = 0; i < n_mq_evts; i++)
2084 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
2085 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
2086 vcl_select_handle_mq (wrk, mqc->mq, n_bits, read_map, write_map,
2087 except_map, 0, bits_set);
2090 return (n_mq_evts > 0 ? (int) *bits_set : 0);
2094 vppcom_select (int n_bits, vcl_si_set * read_map, vcl_si_set * write_map,
2095 vcl_si_set * except_map, double time_to_wait)
2097 u32 sid, minbits = clib_max (n_bits, BITS (uword)), bits_set = 0;
2098 vcl_worker_t *wrk = vcl_worker_get_current ();
2099 vcl_session_t *session = 0;
2102 if (n_bits && read_map)
2104 clib_bitmap_validate (wrk->rd_bitmap, minbits);
2105 clib_memcpy_fast (wrk->rd_bitmap, read_map,
2106 vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
2107 memset (read_map, 0, vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
2109 if (n_bits && write_map)
2111 clib_bitmap_validate (wrk->wr_bitmap, minbits);
2112 clib_memcpy_fast (wrk->wr_bitmap, write_map,
2113 vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
2114 memset (write_map, 0, vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
2116 if (n_bits && except_map)
2118 clib_bitmap_validate (wrk->ex_bitmap, minbits);
2119 clib_memcpy_fast (wrk->ex_bitmap, except_map,
2120 vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
2121 memset (except_map, 0, vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
2131 clib_bitmap_foreach (sid, wrk->wr_bitmap, ({
2132 if (!(session = vcl_session_get (wrk, sid)))
2134 if (except_map && sid < minbits)
2135 clib_bitmap_set_no_check (except_map, sid, 1);
2139 rv = svm_fifo_is_full_prod (session->tx_fifo);
2142 clib_bitmap_set_no_check ((uword*)write_map, sid, 1);
2146 svm_fifo_add_want_deq_ntf (session->tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
2153 clib_bitmap_foreach (sid, wrk->rd_bitmap, ({
2154 if (!(session = vcl_session_get (wrk, sid)))
2156 if (except_map && sid < minbits)
2157 clib_bitmap_set_no_check (except_map, sid, 1);
2161 rv = vcl_session_read_ready (session);
2164 clib_bitmap_set_no_check ((uword*)read_map, sid, 1);
2172 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
2174 vcl_select_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i], n_bits,
2175 read_map, write_map, except_map, &bits_set);
2177 vec_reset_length (wrk->unhandled_evts_vector);
2179 if (vcm->cfg.use_mq_eventfd)
2180 vppcom_select_eventfd (wrk, n_bits, read_map, write_map, except_map,
2181 time_to_wait, &bits_set);
2183 vppcom_select_condvar (wrk, n_bits, read_map, write_map, except_map,
2184 time_to_wait, &bits_set);
2190 vep_verify_epoll_chain (vcl_worker_t * wrk, u32 vep_handle)
2192 vcl_session_t *session;
2193 vppcom_epoll_t *vep;
2194 u32 sh = vep_handle;
2196 if (VPPCOM_DEBUG <= 2)
2199 session = vcl_session_get_w_handle (wrk, vep_handle);
2200 if (PREDICT_FALSE (!session))
2202 VDBG (0, "ERROR: Invalid vep_sh (%u)!", vep_handle);
2205 if (PREDICT_FALSE (!session->is_vep))
2207 VDBG (0, "ERROR: vep_sh (%u) is not a vep!", vep_handle);
2210 vep = &session->vep;
2211 VDBG (0, "vep_sh (%u): Dumping epoll chain\n"
2214 " is_vep_session = %u\n"
2215 " next_sh = 0x%x (%u)\n"
2216 "}\n", vep_handle, session->is_vep, session->is_vep_session,
2217 vep->next_sh, vep->next_sh);
2219 for (sh = vep->next_sh; sh != ~0; sh = vep->next_sh)
2221 session = vcl_session_get_w_handle (wrk, sh);
2222 if (PREDICT_FALSE (!session))
2224 VDBG (0, "ERROR: Invalid sh (%u)!", sh);
2227 if (PREDICT_FALSE (session->is_vep))
2229 VDBG (0, "ERROR: sh (%u) is a vep!", vep_handle);
2231 else if (PREDICT_FALSE (!session->is_vep_session))
2233 VDBG (0, "ERROR: sh (%u) is not a vep session handle!", sh);
2236 vep = &session->vep;
2237 if (PREDICT_FALSE (vep->vep_sh != vep_handle))
2238 VDBG (0, "ERROR: session (%u) vep_sh (%u) != vep_sh (%u)!",
2239 sh, session->vep.vep_sh, vep_handle);
2240 if (session->is_vep_session)
2242 VDBG (0, "vep_sh[%u]: sh 0x%x (%u)\n"
2244 " next_sh = 0x%x (%u)\n"
2245 " prev_sh = 0x%x (%u)\n"
2246 " vep_sh = 0x%x (%u)\n"
2247 " ev.events = 0x%x\n"
2248 " ev.data.u64 = 0x%llx\n"
2251 vep_handle, sh, sh, vep->next_sh, vep->next_sh, vep->prev_sh,
2252 vep->prev_sh, vep->vep_sh, vep->vep_sh, vep->ev.events,
2253 vep->ev.data.u64, vep->et_mask);
2258 VDBG (0, "vep_sh (%u): Dump complete!\n", vep_handle);
2262 vppcom_epoll_create (void)
2264 vcl_worker_t *wrk = vcl_worker_get_current ();
2265 vcl_session_t *vep_session;
2267 vep_session = vcl_session_alloc (wrk);
2269 vep_session->is_vep = 1;
2270 vep_session->vep.vep_sh = ~0;
2271 vep_session->vep.next_sh = ~0;
2272 vep_session->vep.prev_sh = ~0;
2273 vep_session->vpp_handle = ~0;
2275 vcl_evt (VCL_EVT_EPOLL_CREATE, vep_session, vep_session->session_index);
2276 VDBG (0, "Created vep_idx %u", vep_session->session_index);
2278 return vcl_session_handle (vep_session);
2282 vppcom_epoll_ctl (uint32_t vep_handle, int op, uint32_t session_handle,
2283 struct epoll_event *event)
2285 vcl_worker_t *wrk = vcl_worker_get_current ();
2286 vcl_session_t *vep_session;
2287 vcl_session_t *session;
2290 if (vep_handle == session_handle)
2292 VDBG (0, "vep_sh == session handle (%u)!", vep_handle);
2293 return VPPCOM_EINVAL;
2296 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
2297 if (PREDICT_FALSE (!vep_session))
2299 VDBG (0, "Invalid vep_sh (%u)!", vep_handle);
2300 return VPPCOM_EBADFD;
2302 if (PREDICT_FALSE (!vep_session->is_vep))
2304 VDBG (0, "vep_sh (%u) is not a vep!", vep_handle);
2305 return VPPCOM_EINVAL;
2308 ASSERT (vep_session->vep.vep_sh == ~0);
2309 ASSERT (vep_session->vep.prev_sh == ~0);
2311 session = vcl_session_get_w_handle (wrk, session_handle);
2312 if (PREDICT_FALSE (!session))
2314 VDBG (0, "Invalid session_handle (%u)!", session_handle);
2315 return VPPCOM_EBADFD;
2317 if (PREDICT_FALSE (session->is_vep))
2319 VDBG (0, "session_handle (%u) is a vep!", vep_handle);
2320 return VPPCOM_EINVAL;
2326 if (PREDICT_FALSE (!event))
2328 VDBG (0, "EPOLL_CTL_ADD: NULL pointer to epoll_event structure!");
2329 return VPPCOM_EINVAL;
2331 if (vep_session->vep.next_sh != ~0)
2333 vcl_session_t *next_session;
2334 next_session = vcl_session_get_w_handle (wrk,
2335 vep_session->vep.next_sh);
2336 if (PREDICT_FALSE (!next_session))
2338 VDBG (0, "EPOLL_CTL_ADD: Invalid vep.next_sh (%u) on "
2339 "vep_idx (%u)!", vep_session->vep.next_sh, vep_handle);
2340 return VPPCOM_EBADFD;
2342 ASSERT (next_session->vep.prev_sh == vep_handle);
2343 next_session->vep.prev_sh = session_handle;
2345 session->vep.next_sh = vep_session->vep.next_sh;
2346 session->vep.prev_sh = vep_handle;
2347 session->vep.vep_sh = vep_handle;
2348 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
2349 session->vep.ev = *event;
2350 session->is_vep = 0;
2351 session->is_vep_session = 1;
2352 vep_session->vep.next_sh = session_handle;
2354 if (session->tx_fifo)
2355 svm_fifo_add_want_deq_ntf (session->tx_fifo,
2356 SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
2358 VDBG (1, "EPOLL_CTL_ADD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2359 vep_handle, session_handle, event->events, event->data.u64);
2360 vcl_evt (VCL_EVT_EPOLL_CTLADD, session, event->events, event->data.u64);
2364 if (PREDICT_FALSE (!event))
2366 VDBG (0, "EPOLL_CTL_MOD: NULL pointer to epoll_event structure!");
2370 else if (PREDICT_FALSE (!session->is_vep_session))
2372 VDBG (0, "sh %u EPOLL_CTL_MOD: not a vep session!", session_handle);
2376 else if (PREDICT_FALSE (session->vep.vep_sh != vep_handle))
2378 VDBG (0, "EPOLL_CTL_MOD: sh %u vep_sh (%u) != vep_sh (%u)!",
2379 session_handle, session->vep.vep_sh, vep_handle);
2383 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
2384 session->vep.ev = *event;
2385 VDBG (1, "EPOLL_CTL_MOD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2386 vep_handle, session_handle, event->events, event->data.u64);
2390 if (PREDICT_FALSE (!session->is_vep_session))
2392 VDBG (0, "EPOLL_CTL_DEL: %u not a vep session!", session_handle);
2396 else if (PREDICT_FALSE (session->vep.vep_sh != vep_handle))
2398 VDBG (0, "EPOLL_CTL_DEL: sh %u vep_sh (%u) != vep_sh (%u)!",
2399 session_handle, session->vep.vep_sh, vep_handle);
2404 if (session->vep.prev_sh == vep_handle)
2405 vep_session->vep.next_sh = session->vep.next_sh;
2408 vcl_session_t *prev_session;
2409 prev_session = vcl_session_get_w_handle (wrk, session->vep.prev_sh);
2410 if (PREDICT_FALSE (!prev_session))
2412 VDBG (0, "EPOLL_CTL_DEL: Invalid prev_sh (%u) on sh (%u)!",
2413 session->vep.prev_sh, session_handle);
2414 return VPPCOM_EBADFD;
2416 ASSERT (prev_session->vep.next_sh == session_handle);
2417 prev_session->vep.next_sh = session->vep.next_sh;
2419 if (session->vep.next_sh != ~0)
2421 vcl_session_t *next_session;
2422 next_session = vcl_session_get_w_handle (wrk, session->vep.next_sh);
2423 if (PREDICT_FALSE (!next_session))
2425 VDBG (0, "EPOLL_CTL_DEL: Invalid next_sh (%u) on sh (%u)!",
2426 session->vep.next_sh, session_handle);
2427 return VPPCOM_EBADFD;
2429 ASSERT (next_session->vep.prev_sh == session_handle);
2430 next_session->vep.prev_sh = session->vep.prev_sh;
2433 memset (&session->vep, 0, sizeof (session->vep));
2434 session->vep.next_sh = ~0;
2435 session->vep.prev_sh = ~0;
2436 session->vep.vep_sh = ~0;
2437 session->is_vep_session = 0;
2439 if (session->tx_fifo)
2440 svm_fifo_del_want_deq_ntf (session->tx_fifo, SVM_FIFO_NO_DEQ_NOTIF);
2442 VDBG (1, "EPOLL_CTL_DEL: vep_idx %u, sh %u!", vep_handle,
2444 vcl_evt (VCL_EVT_EPOLL_CTLDEL, session, vep_sh);
2448 VDBG (0, "Invalid operation (%d)!", op);
2452 vep_verify_epoll_chain (wrk, vep_handle);
2459 vcl_epoll_wait_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
2460 struct epoll_event *events, u32 * num_ev)
2462 session_disconnected_msg_t *disconnected_msg;
2463 session_connected_msg_t *connected_msg;
2464 u32 sid = ~0, session_events;
2465 u64 session_evt_data = ~0;
2466 vcl_session_t *session;
2469 switch (e->event_type)
2471 case SESSION_IO_EVT_RX:
2472 sid = e->session_index;
2473 if (!(session = vcl_session_get (wrk, sid)))
2475 vcl_fifo_rx_evt_valid_or_break (session);
2476 session_events = session->vep.ev.events;
2477 if (!(EPOLLIN & session->vep.ev.events) || session->has_rx_evt)
2480 events[*num_ev].events |= EPOLLIN;
2481 session_evt_data = session->vep.ev.data.u64;
2482 session->has_rx_evt = 1;
2484 case SESSION_IO_EVT_TX:
2485 sid = e->session_index;
2486 if (!(session = vcl_session_get (wrk, sid)))
2488 session_events = session->vep.ev.events;
2489 if (!(EPOLLOUT & session_events))
2492 events[*num_ev].events |= EPOLLOUT;
2493 session_evt_data = session->vep.ev.data.u64;
2494 svm_fifo_reset_has_deq_ntf (session->tx_fifo);
2496 case SESSION_CTRL_EVT_ACCEPTED:
2497 session = vcl_session_accepted (wrk,
2498 (session_accepted_msg_t *) e->data);
2502 session_events = session->vep.ev.events;
2503 if (!(EPOLLIN & session_events))
2507 events[*num_ev].events |= EPOLLIN;
2508 session_evt_data = session->vep.ev.data.u64;
2510 case SESSION_CTRL_EVT_CONNECTED:
2511 connected_msg = (session_connected_msg_t *) e->data;
2512 vcl_session_connected_handler (wrk, connected_msg);
2513 /* Generate EPOLLOUT because there's no connected event */
2514 sid = vcl_session_index_from_vpp_handle (wrk, connected_msg->handle);
2515 if (!(session = vcl_session_get (wrk, sid)))
2517 session_events = session->vep.ev.events;
2518 if (!(EPOLLOUT & session_events))
2521 events[*num_ev].events |= EPOLLOUT;
2522 session_evt_data = session->vep.ev.data.u64;
2524 case SESSION_CTRL_EVT_DISCONNECTED:
2525 disconnected_msg = (session_disconnected_msg_t *) e->data;
2526 session = vcl_session_disconnected_handler (wrk, disconnected_msg);
2529 session_events = session->vep.ev.events;
2530 if (!((EPOLLHUP | EPOLLRDHUP) & session_events))
2533 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
2534 session_evt_data = session->vep.ev.data.u64;
2536 case SESSION_CTRL_EVT_RESET:
2537 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
2538 if (!(session = vcl_session_get (wrk, sid)))
2540 session_events = session->vep.ev.events;
2541 if (!((EPOLLHUP | EPOLLRDHUP) & session_events))
2544 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
2545 session_evt_data = session->vep.ev.data.u64;
2547 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
2548 vcl_session_unlisten_reply_handler (wrk, e->data);
2550 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
2551 vcl_session_req_worker_update_handler (wrk, e->data);
2553 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
2554 vcl_session_worker_update_reply_handler (wrk, e->data);
2557 VDBG (0, "unhandled: %u", e->event_type);
2563 events[*num_ev].data.u64 = session_evt_data;
2564 if (EPOLLONESHOT & session_events)
2566 session = vcl_session_get (wrk, sid);
2567 session->vep.ev.events = 0;
2574 vcl_epoll_wait_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
2575 struct epoll_event *events, u32 maxevents,
2576 double wait_for_time, u32 * num_ev)
2578 svm_msg_q_msg_t *msg;
2582 if (vec_len (wrk->mq_msg_vector) && svm_msg_q_is_empty (mq))
2583 goto handle_dequeued;
2585 svm_msg_q_lock (mq);
2586 if (svm_msg_q_is_empty (mq))
2590 svm_msg_q_unlock (mq);
2593 else if (wait_for_time < 0)
2595 svm_msg_q_wait (mq);
2599 if (svm_msg_q_timedwait (mq, wait_for_time / 1e3))
2601 svm_msg_q_unlock (mq);
2606 ASSERT (maxevents > *num_ev);
2607 vcl_mq_dequeue_batch (wrk, mq, maxevents - *num_ev);
2608 svm_msg_q_unlock (mq);
2611 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
2613 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
2614 e = svm_msg_q_msg_data (mq, msg);
2615 vcl_epoll_wait_handle_mq_event (wrk, e, events, num_ev);
2616 svm_msg_q_free_msg (mq, msg);
2618 vec_reset_length (wrk->mq_msg_vector);
2619 vcl_handle_pending_wrk_updates (wrk);
2624 vppcom_epoll_wait_condvar (vcl_worker_t * wrk, struct epoll_event *events,
2625 int maxevents, u32 n_evts, double wait_for_time)
2627 double wait = 0, start = 0, now;
2631 wait = wait_for_time;
2632 start = clib_time_now (&wrk->clib_time);
2637 vcl_epoll_wait_handle_mq (wrk, wrk->app_event_queue, events, maxevents,
2644 now = clib_time_now (&wrk->clib_time);
2645 wait -= now - start;
2654 vppcom_epoll_wait_eventfd (vcl_worker_t * wrk, struct epoll_event *events,
2655 int maxevents, u32 n_evts, double wait_for_time)
2657 vcl_mq_evt_conn_t *mqc;
2658 int __clib_unused n_read;
2662 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
2664 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
2665 vec_len (wrk->mq_events), wait_for_time);
2666 for (i = 0; i < n_mq_evts; i++)
2668 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
2669 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
2670 vcl_epoll_wait_handle_mq (wrk, mqc->mq, events, maxevents, 0, &n_evts);
2672 if (!n_evts && n_mq_evts > 0)
2675 return (int) n_evts;
2679 vppcom_epoll_wait (uint32_t vep_handle, struct epoll_event *events,
2680 int maxevents, double wait_for_time)
2682 vcl_worker_t *wrk = vcl_worker_get_current ();
2683 vcl_session_t *vep_session;
2687 if (PREDICT_FALSE (maxevents <= 0))
2689 VDBG (0, "ERROR: Invalid maxevents (%d)!", maxevents);
2690 return VPPCOM_EINVAL;
2693 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
2695 return VPPCOM_EBADFD;
2697 if (PREDICT_FALSE (!vep_session->is_vep))
2699 VDBG (0, "ERROR: vep_idx (%u) is not a vep!", vep_handle);
2700 return VPPCOM_EINVAL;
2703 memset (events, 0, sizeof (*events) * maxevents);
2705 if (vec_len (wrk->unhandled_evts_vector))
2707 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
2709 vcl_epoll_wait_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i],
2711 if (n_evts == maxevents)
2713 vec_delete (wrk->unhandled_evts_vector, i + 1, 0);
2717 vec_reset_length (wrk->unhandled_evts_vector);
2720 if (vcm->cfg.use_mq_eventfd)
2721 return vppcom_epoll_wait_eventfd (wrk, events, maxevents, n_evts,
2724 return vppcom_epoll_wait_condvar (wrk, events, maxevents, n_evts,
2729 vppcom_session_attr (uint32_t session_handle, uint32_t op,
2730 void *buffer, uint32_t * buflen)
2732 vcl_worker_t *wrk = vcl_worker_get_current ();
2733 vcl_session_t *session;
2735 u32 *flags = buffer, tmp_flags = 0;
2736 vppcom_endpt_t *ep = buffer;
2738 session = vcl_session_get_w_handle (wrk, session_handle);
2740 return VPPCOM_EBADFD;
2744 case VPPCOM_ATTR_GET_NREAD:
2745 rv = vcl_session_read_ready (session);
2746 VDBG (2, "VPPCOM_ATTR_GET_NREAD: sh %u, nread = %d", session_handle,
2750 case VPPCOM_ATTR_GET_NWRITE:
2751 rv = vcl_session_write_ready (session);
2752 VDBG (2, "VPPCOM_ATTR_GET_NWRITE: sh %u, nwrite = %d", session_handle,
2756 case VPPCOM_ATTR_GET_FLAGS:
2757 if (PREDICT_TRUE (buffer && buflen && (*buflen >= sizeof (*flags))))
2759 *flags = O_RDWR | (VCL_SESS_ATTR_TEST (session->attr,
2760 VCL_SESS_ATTR_NONBLOCK));
2761 *buflen = sizeof (*flags);
2762 VDBG (2, "VPPCOM_ATTR_GET_FLAGS: sh %u, flags = 0x%08x, "
2763 "is_nonblocking = %u", session_handle, *flags,
2764 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK));
2770 case VPPCOM_ATTR_SET_FLAGS:
2771 if (PREDICT_TRUE (buffer && buflen && (*buflen == sizeof (*flags))))
2773 if (*flags & O_NONBLOCK)
2774 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK);
2776 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_NONBLOCK);
2778 VDBG (2, "VPPCOM_ATTR_SET_FLAGS: sh %u, flags = 0x%08x,"
2779 " is_nonblocking = %u", session_handle, *flags,
2780 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK));
2786 case VPPCOM_ATTR_GET_PEER_ADDR:
2787 if (PREDICT_TRUE (buffer && buflen &&
2788 (*buflen >= sizeof (*ep)) && ep->ip))
2790 ep->is_ip4 = session->transport.is_ip4;
2791 ep->port = session->transport.rmt_port;
2792 if (session->transport.is_ip4)
2793 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
2794 sizeof (ip4_address_t));
2796 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
2797 sizeof (ip6_address_t));
2798 *buflen = sizeof (*ep);
2799 VDBG (1, "VPPCOM_ATTR_GET_PEER_ADDR: sh %u, is_ip4 = %u, "
2800 "addr = %U, port %u", session_handle, ep->is_ip4,
2801 format_ip46_address, &session->transport.rmt_ip,
2802 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
2803 clib_net_to_host_u16 (ep->port));
2809 case VPPCOM_ATTR_GET_LCL_ADDR:
2810 if (PREDICT_TRUE (buffer && buflen &&
2811 (*buflen >= sizeof (*ep)) && ep->ip))
2813 ep->is_ip4 = session->transport.is_ip4;
2814 ep->port = session->transport.lcl_port;
2815 if (session->transport.is_ip4)
2816 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip4,
2817 sizeof (ip4_address_t));
2819 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip6,
2820 sizeof (ip6_address_t));
2821 *buflen = sizeof (*ep);
2822 VDBG (1, "VPPCOM_ATTR_GET_LCL_ADDR: sh %u, is_ip4 = %u, addr = %U"
2823 " port %d", session_handle, ep->is_ip4, format_ip46_address,
2824 &session->transport.lcl_ip,
2825 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
2826 clib_net_to_host_u16 (ep->port));
2832 case VPPCOM_ATTR_GET_LIBC_EPFD:
2833 rv = session->libc_epfd;
2834 VDBG (2, "VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd %d", rv);
2837 case VPPCOM_ATTR_SET_LIBC_EPFD:
2838 if (PREDICT_TRUE (buffer && buflen &&
2839 (*buflen == sizeof (session->libc_epfd))))
2841 session->libc_epfd = *(int *) buffer;
2842 *buflen = sizeof (session->libc_epfd);
2844 VDBG (2, "VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd %d, buflen %d",
2845 session->libc_epfd, *buflen);
2851 case VPPCOM_ATTR_GET_PROTOCOL:
2852 if (buffer && buflen && (*buflen >= sizeof (int)))
2854 *(int *) buffer = session->session_type;
2855 *buflen = sizeof (int);
2857 VDBG (2, "VPPCOM_ATTR_GET_PROTOCOL: %d (%s), buflen %d",
2858 *(int *) buffer, *(int *) buffer ? "UDP" : "TCP", *buflen);
2864 case VPPCOM_ATTR_GET_LISTEN:
2865 if (buffer && buflen && (*buflen >= sizeof (int)))
2867 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2868 VCL_SESS_ATTR_LISTEN);
2869 *buflen = sizeof (int);
2871 VDBG (2, "VPPCOM_ATTR_GET_LISTEN: %d, buflen %d", *(int *) buffer,
2878 case VPPCOM_ATTR_GET_ERROR:
2879 if (buffer && buflen && (*buflen >= sizeof (int)))
2881 *(int *) buffer = 0;
2882 *buflen = sizeof (int);
2884 VDBG (2, "VPPCOM_ATTR_GET_ERROR: %d, buflen %d, #VPP-TBD#",
2885 *(int *) buffer, *buflen);
2891 case VPPCOM_ATTR_GET_TX_FIFO_LEN:
2892 if (buffer && buflen && (*buflen >= sizeof (u32)))
2896 *(size_t *) buffer = (session->sndbuf_size ? session->sndbuf_size :
2897 session->tx_fifo ? session->tx_fifo->nitems :
2898 vcm->cfg.tx_fifo_size);
2899 *buflen = sizeof (u32);
2901 VDBG (2, "VPPCOM_ATTR_GET_TX_FIFO_LEN: %u (0x%x), buflen %d,"
2902 " #VPP-TBD#", *(size_t *) buffer, *(size_t *) buffer,
2909 case VPPCOM_ATTR_SET_TX_FIFO_LEN:
2910 if (buffer && buflen && (*buflen == sizeof (u32)))
2913 session->sndbuf_size = *(u32 *) buffer;
2914 VDBG (2, "VPPCOM_ATTR_SET_TX_FIFO_LEN: %u (0x%x), buflen %d,"
2915 " #VPP-TBD#", session->sndbuf_size, session->sndbuf_size,
2922 case VPPCOM_ATTR_GET_RX_FIFO_LEN:
2923 if (buffer && buflen && (*buflen >= sizeof (u32)))
2927 *(size_t *) buffer = (session->rcvbuf_size ? session->rcvbuf_size :
2928 session->rx_fifo ? session->rx_fifo->nitems :
2929 vcm->cfg.rx_fifo_size);
2930 *buflen = sizeof (u32);
2932 VDBG (2, "VPPCOM_ATTR_GET_RX_FIFO_LEN: %u (0x%x), buflen %d, "
2933 "#VPP-TBD#", *(size_t *) buffer, *(size_t *) buffer, *buflen);
2939 case VPPCOM_ATTR_SET_RX_FIFO_LEN:
2940 if (buffer && buflen && (*buflen == sizeof (u32)))
2943 session->rcvbuf_size = *(u32 *) buffer;
2944 VDBG (2, "VPPCOM_ATTR_SET_RX_FIFO_LEN: %u (0x%x), buflen %d,"
2945 " #VPP-TBD#", session->sndbuf_size, session->sndbuf_size,
2952 case VPPCOM_ATTR_GET_REUSEADDR:
2953 if (buffer && buflen && (*buflen >= sizeof (int)))
2956 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2957 VCL_SESS_ATTR_REUSEADDR);
2958 *buflen = sizeof (int);
2960 VDBG (2, "VPPCOM_ATTR_GET_REUSEADDR: %d, buflen %d, #VPP-TBD#",
2961 *(int *) buffer, *buflen);
2967 case VPPCOM_ATTR_SET_REUSEADDR:
2968 if (buffer && buflen && (*buflen == sizeof (int)) &&
2969 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
2972 if (*(int *) buffer)
2973 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEADDR);
2975 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEADDR);
2977 VDBG (2, "VPPCOM_ATTR_SET_REUSEADDR: %d, buflen %d, #VPP-TBD#",
2978 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_REUSEADDR),
2985 case VPPCOM_ATTR_GET_REUSEPORT:
2986 if (buffer && buflen && (*buflen >= sizeof (int)))
2989 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2990 VCL_SESS_ATTR_REUSEPORT);
2991 *buflen = sizeof (int);
2993 VDBG (2, "VPPCOM_ATTR_GET_REUSEPORT: %d, buflen %d, #VPP-TBD#",
2994 *(int *) buffer, *buflen);
3000 case VPPCOM_ATTR_SET_REUSEPORT:
3001 if (buffer && buflen && (*buflen == sizeof (int)) &&
3002 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
3005 if (*(int *) buffer)
3006 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEPORT);
3008 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEPORT);
3010 VDBG (2, "VPPCOM_ATTR_SET_REUSEPORT: %d, buflen %d, #VPP-TBD#",
3011 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_REUSEPORT),
3018 case VPPCOM_ATTR_GET_BROADCAST:
3019 if (buffer && buflen && (*buflen >= sizeof (int)))
3022 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3023 VCL_SESS_ATTR_BROADCAST);
3024 *buflen = sizeof (int);
3026 VDBG (2, "VPPCOM_ATTR_GET_BROADCAST: %d, buflen %d, #VPP-TBD#",
3027 *(int *) buffer, *buflen);
3033 case VPPCOM_ATTR_SET_BROADCAST:
3034 if (buffer && buflen && (*buflen == sizeof (int)))
3037 if (*(int *) buffer)
3038 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_BROADCAST);
3040 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_BROADCAST);
3042 VDBG (2, "VPPCOM_ATTR_SET_BROADCAST: %d, buflen %d, #VPP-TBD#",
3043 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_BROADCAST),
3050 case VPPCOM_ATTR_GET_V6ONLY:
3051 if (buffer && buflen && (*buflen >= sizeof (int)))
3054 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3055 VCL_SESS_ATTR_V6ONLY);
3056 *buflen = sizeof (int);
3058 VDBG (2, "VPPCOM_ATTR_GET_V6ONLY: %d, buflen %d, #VPP-TBD#",
3059 *(int *) buffer, *buflen);
3065 case VPPCOM_ATTR_SET_V6ONLY:
3066 if (buffer && buflen && (*buflen == sizeof (int)))
3069 if (*(int *) buffer)
3070 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_V6ONLY);
3072 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_V6ONLY);
3074 VDBG (2, "VPPCOM_ATTR_SET_V6ONLY: %d, buflen %d, #VPP-TBD#",
3075 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_V6ONLY),
3082 case VPPCOM_ATTR_GET_KEEPALIVE:
3083 if (buffer && buflen && (*buflen >= sizeof (int)))
3086 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3087 VCL_SESS_ATTR_KEEPALIVE);
3088 *buflen = sizeof (int);
3090 VDBG (2, "VPPCOM_ATTR_GET_KEEPALIVE: %d, buflen %d, #VPP-TBD#",
3091 *(int *) buffer, *buflen);
3097 case VPPCOM_ATTR_SET_KEEPALIVE:
3098 if (buffer && buflen && (*buflen == sizeof (int)))
3101 if (*(int *) buffer)
3102 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_KEEPALIVE);
3104 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_KEEPALIVE);
3106 VDBG (2, "VPPCOM_ATTR_SET_KEEPALIVE: %d, buflen %d, #VPP-TBD#",
3107 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_KEEPALIVE),
3114 case VPPCOM_ATTR_GET_TCP_NODELAY:
3115 if (buffer && buflen && (*buflen >= sizeof (int)))
3118 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3119 VCL_SESS_ATTR_TCP_NODELAY);
3120 *buflen = sizeof (int);
3122 VDBG (2, "VPPCOM_ATTR_GET_TCP_NODELAY: %d, buflen %d, #VPP-TBD#",
3123 *(int *) buffer, *buflen);
3129 case VPPCOM_ATTR_SET_TCP_NODELAY:
3130 if (buffer && buflen && (*buflen == sizeof (int)))
3133 if (*(int *) buffer)
3134 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
3136 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
3138 VDBG (2, "VPPCOM_ATTR_SET_TCP_NODELAY: %d, buflen %d, #VPP-TBD#",
3139 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_TCP_NODELAY),
3146 case VPPCOM_ATTR_GET_TCP_KEEPIDLE:
3147 if (buffer && buflen && (*buflen >= sizeof (int)))
3150 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3151 VCL_SESS_ATTR_TCP_KEEPIDLE);
3152 *buflen = sizeof (int);
3154 VDBG (2, "VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d, buflen %d, #VPP-TBD#",
3155 *(int *) buffer, *buflen);
3161 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
3162 if (buffer && buflen && (*buflen == sizeof (int)))
3165 if (*(int *) buffer)
3166 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
3168 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
3170 VDBG (2, "VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d, buflen %d, #VPP-TBD#",
3171 VCL_SESS_ATTR_TEST (session->attr,
3172 VCL_SESS_ATTR_TCP_KEEPIDLE), *buflen);
3178 case VPPCOM_ATTR_GET_TCP_KEEPINTVL:
3179 if (buffer && buflen && (*buflen >= sizeof (int)))
3182 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3183 VCL_SESS_ATTR_TCP_KEEPINTVL);
3184 *buflen = sizeof (int);
3186 VDBG (2, "VPPCOM_ATTR_GET_TCP_KEEPINTVL: %d, buflen %d, #VPP-TBD#",
3187 *(int *) buffer, *buflen);
3193 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
3194 if (buffer && buflen && (*buflen == sizeof (int)))
3197 if (*(int *) buffer)
3198 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
3200 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
3202 VDBG (2, "VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d, buflen %d, #VPP-TBD#",
3203 VCL_SESS_ATTR_TEST (session->attr,
3204 VCL_SESS_ATTR_TCP_KEEPINTVL), *buflen);
3210 case VPPCOM_ATTR_GET_TCP_USER_MSS:
3211 if (buffer && buflen && (*buflen >= sizeof (u32)))
3214 *(u32 *) buffer = session->user_mss;
3215 *buflen = sizeof (int);
3217 VDBG (2, "VPPCOM_ATTR_GET_TCP_USER_MSS: %d, buflen %d, #VPP-TBD#",
3218 *(int *) buffer, *buflen);
3224 case VPPCOM_ATTR_SET_TCP_USER_MSS:
3225 if (buffer && buflen && (*buflen == sizeof (u32)))
3228 session->user_mss = *(u32 *) buffer;
3230 VDBG (2, "VPPCOM_ATTR_SET_TCP_USER_MSS: %u, buflen %d, #VPP-TBD#",
3231 session->user_mss, *buflen);
3237 case VPPCOM_ATTR_SET_SHUT:
3238 if (*flags == SHUT_RD || *flags == SHUT_RDWR)
3239 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_SHUT_RD);
3240 if (*flags == SHUT_WR || *flags == SHUT_RDWR)
3241 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_SHUT_WR);
3244 case VPPCOM_ATTR_GET_SHUT:
3245 if (VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_SHUT_RD))
3247 if (VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_SHUT_WR))
3250 *(int *) buffer = SHUT_RD;
3251 else if (tmp_flags == 2)
3252 *(int *) buffer = SHUT_WR;
3253 else if (tmp_flags == 3)
3254 *(int *) buffer = SHUT_RDWR;
3255 *buflen = sizeof (int);
3266 vppcom_session_recvfrom (uint32_t session_handle, void *buffer,
3267 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3269 vcl_worker_t *wrk = vcl_worker_get_current ();
3271 vcl_session_t *session = 0;
3275 session = vcl_session_get_w_handle (wrk, session_handle);
3276 if (PREDICT_FALSE (!session))
3278 VDBG (0, "sh 0x%llx is closed!", session_handle);
3279 return VPPCOM_EBADFD;
3281 ep->is_ip4 = session->transport.is_ip4;
3282 ep->port = session->transport.rmt_port;
3286 rv = vppcom_session_read (session_handle, buffer, buflen);
3287 else if (flags & MSG_PEEK)
3288 rv = vppcom_session_peek (session_handle, buffer, buflen);
3291 VDBG (0, "Unsupport flags for recvfrom %d", flags);
3292 return VPPCOM_EAFNOSUPPORT;
3297 if (session->transport.is_ip4)
3298 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
3299 sizeof (ip4_address_t));
3301 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
3302 sizeof (ip6_address_t));
3309 vppcom_session_sendto (uint32_t session_handle, void *buffer,
3310 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3313 return VPPCOM_EINVAL;
3318 return VPPCOM_EINVAL;
3323 // TBD check the flags and do the right thing
3324 VDBG (2, "handling flags 0x%u (%d) not implemented yet.", flags, flags);
3327 return (vppcom_session_write_inline (session_handle, buffer, buflen, 1));
3331 vppcom_poll (vcl_poll_t * vp, uint32_t n_sids, double wait_for_time)
3333 vcl_worker_t *wrk = vcl_worker_get_current ();
3334 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
3335 u32 i, keep_trying = 1;
3336 svm_msg_q_msg_t msg;
3340 VDBG (3, "vp %p, nsids %u, wait_for_time %f", vp, n_sids, wait_for_time);
3343 return VPPCOM_EFAULT;
3347 vcl_session_t *session;
3349 /* Dequeue all events and drop all unhandled io events */
3350 while (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0) == 0)
3352 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
3353 vcl_handle_mq_event (wrk, e);
3354 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
3356 vec_reset_length (wrk->unhandled_evts_vector);
3358 for (i = 0; i < n_sids; i++)
3360 session = vcl_session_get (wrk, vp[i].sh);
3363 vp[i].revents = POLLHUP;
3370 if (POLLIN & vp[i].events)
3372 rv = vcl_session_read_ready (session);
3375 vp[i].revents |= POLLIN;
3382 case VPPCOM_ECONNRESET:
3383 vp[i].revents = POLLHUP;
3387 vp[i].revents = POLLERR;
3394 if (POLLOUT & vp[i].events)
3396 rv = vcl_session_write_ready (session);
3399 vp[i].revents |= POLLOUT;
3406 case VPPCOM_ECONNRESET:
3407 vp[i].revents = POLLHUP;
3411 vp[i].revents = POLLERR;
3418 if (0) // Note "done:" label used by VCL_SESSION_LOCK_AND_GET()
3420 vp[i].revents = POLLNVAL;
3424 if (wait_for_time != -1)
3425 keep_trying = (clib_time_now (&wrk->clib_time) <= timeout) ? 1 : 0;
3427 while ((num_ev == 0) && keep_trying);
3433 vppcom_mq_epoll_fd (void)
3435 vcl_worker_t *wrk = vcl_worker_get_current ();
3436 return wrk->mqs_epfd;
3440 vppcom_session_index (vcl_session_handle_t session_handle)
3442 return session_handle & 0xFFFFFF;
3446 vppcom_session_worker (vcl_session_handle_t session_handle)
3448 return session_handle >> 24;
3452 vppcom_worker_register (void)
3454 if (!vcl_worker_alloc_and_init ())
3455 return VPPCOM_EEXIST;
3457 if (vcl_worker_set_bapi ())
3458 return VPPCOM_EEXIST;
3460 if (vcl_worker_register_with_vpp ())
3461 return VPPCOM_EEXIST;
3467 vppcom_worker_unregister (void)
3469 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
3470 vcl_set_worker_index (~0);
3474 vppcom_worker_index (void)
3476 return vcl_get_worker_index ();
3480 vppcom_worker_mqs_epfd (void)
3482 vcl_worker_t *wrk = vcl_worker_get_current ();
3483 if (!vcm->cfg.use_mq_eventfd)
3485 return wrk->mqs_epfd;
3489 vppcom_session_is_connectable_listener (uint32_t session_handle)
3491 vcl_session_t *session;
3492 vcl_worker_t *wrk = vcl_worker_get_current ();
3493 session = vcl_session_get_w_handle (wrk, session_handle);
3495 return VPPCOM_EBADFD;
3496 return vcl_session_is_connectable_listener (wrk, session);
3500 vppcom_session_listener (uint32_t session_handle)
3502 vcl_worker_t *wrk = vcl_worker_get_current ();
3503 vcl_session_t *listen_session, *session;
3504 session = vcl_session_get_w_handle (wrk, session_handle);
3506 return VPPCOM_EBADFD;
3507 if (session->listener_index == VCL_INVALID_SESSION_INDEX)
3508 return VPPCOM_EBADFD;
3509 listen_session = vcl_session_get_w_handle (wrk, session->listener_index);
3510 if (!listen_session)
3511 return VPPCOM_EBADFD;
3512 return vcl_session_handle (listen_session);
3516 vppcom_session_n_accepted (uint32_t session_handle)
3518 vcl_worker_t *wrk = vcl_worker_get_current ();
3519 vcl_session_t *session = vcl_session_get_w_handle (wrk, session_handle);
3521 return VPPCOM_EBADFD;
3522 return session->n_accepted_sessions;
3526 * fd.io coding-style-patch-verification: ON
3529 * eval: (c-set-style "gnu")