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;
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)
53 n_msgs = svm_msg_q_size (mq);
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 (session_state_t state)
85 case STATE_VPP_CLOSING:
86 st = "STATE_VPP_CLOSING";
89 case STATE_DISCONNECT:
90 st = "STATE_DISCONNECT";
106 format_ip4_address (u8 * s, va_list * args)
108 u8 *a = va_arg (*args, u8 *);
109 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
113 format_ip6_address (u8 * s, va_list * args)
115 ip6_address_t *a = va_arg (*args, ip6_address_t *);
116 u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
118 i_max_n_zero = ARRAY_LEN (a->as_u16);
120 i_first_zero = i_max_n_zero;
122 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
124 u32 is_zero = a->as_u16[i] == 0;
125 if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
131 if ((!is_zero && n_zeros > max_n_zeros)
132 || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
134 i_max_n_zero = i_first_zero;
135 max_n_zeros = n_zeros;
136 i_first_zero = ARRAY_LEN (a->as_u16);
141 last_double_colon = 0;
142 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
144 if (i == i_max_n_zero && max_n_zeros > 1)
146 s = format (s, "::");
147 i += max_n_zeros - 1;
148 last_double_colon = 1;
152 s = format (s, "%s%x",
153 (last_double_colon || i == 0) ? "" : ":",
154 clib_net_to_host_u16 (a->as_u16[i]));
155 last_double_colon = 0;
162 /* Format an IP46 address. */
164 format_ip46_address (u8 * s, va_list * args)
166 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
167 ip46_type_t type = va_arg (*args, ip46_type_t);
173 is_ip4 = ip46_address_is_ip4 (ip46);
184 format (s, "%U", format_ip4_address, &ip46->ip4) :
185 format (s, "%U", format_ip6_address, &ip46->ip6);
189 * VPPCOM Utility Functions
194 vcl_send_session_accepted_reply (svm_msg_q_t * mq, u32 context,
195 session_handle_t handle, int retval)
197 app_session_evt_t _app_evt, *app_evt = &_app_evt;
198 session_accepted_reply_msg_t *rmp;
199 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_ACCEPTED_REPLY);
200 rmp = (session_accepted_reply_msg_t *) app_evt->evt->data;
201 rmp->handle = handle;
202 rmp->context = context;
203 rmp->retval = retval;
204 app_send_ctrl_evt_to_vpp (mq, app_evt);
208 vcl_send_session_disconnected_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_disconnected_reply_msg_t *rmp;
213 app_alloc_ctrl_evt_to_vpp (mq, app_evt,
214 SESSION_CTRL_EVT_DISCONNECTED_REPLY);
215 rmp = (session_disconnected_reply_msg_t *) app_evt->evt->data;
216 rmp->handle = handle;
217 rmp->context = context;
218 rmp->retval = retval;
219 app_send_ctrl_evt_to_vpp (mq, app_evt);
223 vcl_send_session_reset_reply (svm_msg_q_t * mq, u32 context,
224 session_handle_t handle, int retval)
226 app_session_evt_t _app_evt, *app_evt = &_app_evt;
227 session_reset_reply_msg_t *rmp;
228 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_RESET_REPLY);
229 rmp = (session_reset_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_worker_update (vcl_worker_t * wrk, vcl_session_t * s,
240 app_session_evt_t _app_evt, *app_evt = &_app_evt;
241 session_worker_update_msg_t *mp;
244 mq = vcl_session_vpp_evt_q (wrk, s);
245 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_WORKER_UPDATE);
246 mp = (session_worker_update_msg_t *) app_evt->evt->data;
247 mp->client_index = wrk->my_client_index;
248 mp->handle = s->vpp_handle;
249 mp->req_wrk_index = wrk->vpp_wrk_index;
250 mp->wrk_index = wrk_index;
251 app_send_ctrl_evt_to_vpp (mq, app_evt);
255 vcl_session_accepted_handler (vcl_worker_t * wrk, session_accepted_msg_t * mp)
257 vcl_session_t *session, *listen_session;
258 svm_fifo_t *rx_fifo, *tx_fifo;
262 session = vcl_session_alloc (wrk);
264 listen_session = vcl_session_table_lookup_listener (wrk,
265 mp->listener_handle);
269 evt_q = uword_to_pointer (mp->vpp_event_queue_address, svm_msg_q_t *);
270 clib_warning ("VCL<%d>: ERROR: couldn't find listen session: "
271 "unknown vpp listener handle %llx",
272 getpid (), mp->listener_handle);
273 vcl_send_session_accepted_reply (evt_q, mp->context, mp->handle,
274 VNET_API_ERROR_INVALID_ARGUMENT);
275 vcl_session_free (wrk, session);
276 return VCL_INVALID_SESSION_INDEX;
279 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
280 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
282 if (mp->server_event_queue_address)
284 session->vpp_evt_q = uword_to_pointer (mp->client_event_queue_address,
286 session->our_evt_q = uword_to_pointer (mp->server_event_queue_address,
288 if (vcl_wait_for_segment (mp->segment_handle))
290 clib_warning ("segment for session %u couldn't be mounted!",
291 session->session_index);
292 return VCL_INVALID_SESSION_INDEX;
294 rx_fifo->master_session_index = session->session_index;
295 tx_fifo->master_session_index = session->session_index;
296 rx_fifo->master_thread_index = vcl_get_worker_index ();
297 tx_fifo->master_thread_index = vcl_get_worker_index ();
298 vec_validate (wrk->vpp_event_queues, 0);
299 evt_q = uword_to_pointer (mp->vpp_event_queue_address, svm_msg_q_t *);
300 wrk->vpp_event_queues[0] = evt_q;
304 session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address,
306 rx_fifo->client_session_index = session->session_index;
307 tx_fifo->client_session_index = session->session_index;
308 rx_fifo->client_thread_index = vcl_get_worker_index ();
309 tx_fifo->client_thread_index = vcl_get_worker_index ();
310 vpp_wrk_index = tx_fifo->master_thread_index;
311 vec_validate (wrk->vpp_event_queues, vpp_wrk_index);
312 wrk->vpp_event_queues[vpp_wrk_index] = session->vpp_evt_q;
315 session->vpp_handle = mp->handle;
316 session->vpp_thread_index = rx_fifo->master_thread_index;
317 session->client_context = mp->context;
318 session->rx_fifo = rx_fifo;
319 session->tx_fifo = tx_fifo;
321 session->session_state = STATE_ACCEPT;
322 session->transport.rmt_port = mp->port;
323 session->transport.is_ip4 = mp->is_ip4;
324 clib_memcpy_fast (&session->transport.rmt_ip, mp->ip,
325 sizeof (ip46_address_t));
327 vcl_session_table_add_vpp_handle (wrk, mp->handle, session->session_index);
328 session->transport.lcl_port = listen_session->transport.lcl_port;
329 session->transport.lcl_ip = listen_session->transport.lcl_ip;
330 session->session_type = listen_session->session_type;
331 session->is_dgram = session->session_type == VPPCOM_PROTO_UDP;
333 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: client accept request from %s"
334 " address %U port %d queue %p!", getpid (), mp->handle,
335 session->session_index,
336 mp->is_ip4 ? "IPv4" : "IPv6", format_ip46_address, &mp->ip,
337 mp->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
338 clib_net_to_host_u16 (mp->port), session->vpp_evt_q);
339 vcl_evt (VCL_EVT_ACCEPT, session, listen_session, session_index);
341 return session->session_index;
345 vcl_session_connected_handler (vcl_worker_t * wrk,
346 session_connected_msg_t * mp)
348 u32 session_index, vpp_wrk_index;
349 svm_fifo_t *rx_fifo, *tx_fifo;
350 vcl_session_t *session = 0;
353 session_index = mp->context;
354 session = vcl_session_get (wrk, session_index);
357 clib_warning ("[%s] ERROR: vpp handle 0x%llx, sid %u: "
358 "Invalid session index (%u)!",
359 getpid (), mp->handle, session_index);
360 return VCL_INVALID_SESSION_INDEX;
364 clib_warning ("VCL<%d>: ERROR: sid %u: connect failed! %U", getpid (),
365 session_index, format_api_error, ntohl (mp->retval));
366 session->session_state = STATE_FAILED;
367 session->vpp_handle = mp->handle;
368 return session_index;
371 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
372 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
373 if (vcl_wait_for_segment (mp->segment_handle))
375 clib_warning ("segment for session %u couldn't be mounted!",
376 session->session_index);
377 return VCL_INVALID_SESSION_INDEX;
380 rx_fifo->client_session_index = session_index;
381 tx_fifo->client_session_index = session_index;
382 rx_fifo->client_thread_index = vcl_get_worker_index ();
383 tx_fifo->client_thread_index = vcl_get_worker_index ();
385 if (mp->client_event_queue_address)
387 session->vpp_evt_q = uword_to_pointer (mp->server_event_queue_address,
389 session->our_evt_q = uword_to_pointer (mp->client_event_queue_address,
392 vec_validate (wrk->vpp_event_queues, 0);
393 evt_q = uword_to_pointer (mp->vpp_event_queue_address, svm_msg_q_t *);
394 wrk->vpp_event_queues[0] = evt_q;
398 session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address,
400 vpp_wrk_index = tx_fifo->master_thread_index;
401 vec_validate (wrk->vpp_event_queues, vpp_wrk_index);
402 wrk->vpp_event_queues[vpp_wrk_index] = session->vpp_evt_q;
405 session->rx_fifo = rx_fifo;
406 session->tx_fifo = tx_fifo;
407 session->vpp_handle = mp->handle;
408 session->vpp_thread_index = rx_fifo->master_thread_index;
409 session->transport.is_ip4 = mp->is_ip4;
410 clib_memcpy_fast (&session->transport.lcl_ip, mp->lcl_ip,
411 sizeof (session->transport.lcl_ip));
412 session->transport.lcl_port = mp->lcl_port;
413 session->session_state = STATE_CONNECT;
415 /* Add it to lookup table */
416 vcl_session_table_add_vpp_handle (wrk, mp->handle, session_index);
418 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: connect succeeded! "
419 "session_rx_fifo %p, refcnt %d, session_tx_fifo %p, refcnt %d",
420 getpid (), mp->handle, session_index, session->rx_fifo,
421 session->rx_fifo->refcnt, session->tx_fifo, session->tx_fifo->refcnt);
423 return session_index;
427 vcl_flag_accepted_session (vcl_session_t * session, u64 handle, u32 flags)
429 vcl_session_msg_t *accepted_msg;
432 for (i = 0; i < vec_len (session->accept_evts_fifo); i++)
434 accepted_msg = &session->accept_evts_fifo[i];
435 if (accepted_msg->accepted_msg.handle == handle)
437 accepted_msg->flags |= flags;
445 vcl_session_reset_handler (vcl_worker_t * wrk,
446 session_reset_msg_t * reset_msg)
448 vcl_session_t *session;
451 sid = vcl_session_index_from_vpp_handle (wrk, reset_msg->handle);
452 session = vcl_session_get (wrk, sid);
455 VDBG (0, "request to reset unknown handle 0x%llx", reset_msg->handle);
456 return VCL_INVALID_SESSION_INDEX;
459 /* Caught a reset before actually accepting the session */
460 if (session->session_state == STATE_LISTEN)
463 if (!vcl_flag_accepted_session (session, reset_msg->handle,
464 VCL_ACCEPTED_F_RESET))
465 VDBG (0, "session was not accepted!");
466 return VCL_INVALID_SESSION_INDEX;
469 session->session_state = STATE_DISCONNECT;
470 VDBG (0, "reset session %u [0x%llx]", sid, reset_msg->handle);
475 vcl_session_bound_handler (vcl_worker_t * wrk, session_bound_msg_t * mp)
477 vcl_session_t *session;
478 u32 sid = mp->context;
480 session = vcl_session_get (wrk, sid);
483 VERR ("vpp handle 0x%llx, sid %u: bind failed: %U", mp->handle, sid,
484 format_api_error, mp->retval);
487 session->session_state = STATE_FAILED;
488 session->vpp_handle = mp->handle;
493 clib_warning ("[%s] ERROR: vpp handle 0x%llx, sid %u: "
494 "Invalid session index (%u)!",
495 getpid (), mp->handle, sid);
496 return VCL_INVALID_SESSION_INDEX;
500 session->vpp_handle = mp->handle;
501 session->transport.is_ip4 = mp->lcl_is_ip4;
502 clib_memcpy_fast (&session->transport.lcl_ip, mp->lcl_ip,
503 sizeof (ip46_address_t));
504 session->transport.lcl_port = mp->lcl_port;
505 vcl_session_table_add_listener (wrk, mp->handle, sid);
506 session->session_state = STATE_LISTEN;
508 if (session->is_dgram)
510 svm_fifo_t *rx_fifo, *tx_fifo;
511 session->vpp_evt_q = uword_to_pointer (mp->vpp_evt_q, svm_msg_q_t *);
512 rx_fifo = uword_to_pointer (mp->rx_fifo, svm_fifo_t *);
513 rx_fifo->client_session_index = sid;
514 tx_fifo = uword_to_pointer (mp->tx_fifo, svm_fifo_t *);
515 tx_fifo->client_session_index = sid;
516 session->rx_fifo = rx_fifo;
517 session->tx_fifo = tx_fifo;
520 VDBG (0, "session %u [0x%llx]: listen succeeded!", sid, mp->handle);
524 static vcl_session_t *
525 vcl_session_accepted (vcl_worker_t * wrk, session_accepted_msg_t * msg)
527 vcl_session_msg_t *vcl_msg;
528 vcl_session_t *session;
530 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
531 if (PREDICT_FALSE (session != 0))
532 VWRN ("session overlap handle %lu state %u!", msg->handle,
533 session->session_state);
535 session = vcl_session_table_lookup_listener (wrk, msg->listener_handle);
538 VERR ("couldn't find listen session: listener handle %llx",
539 msg->listener_handle);
543 clib_fifo_add2 (session->accept_evts_fifo, vcl_msg);
544 vcl_msg->accepted_msg = *msg;
545 /* Session handle points to listener until fully accepted by app */
546 vcl_session_table_add_vpp_handle (wrk, msg->handle, session->session_index);
551 static vcl_session_t *
552 vcl_session_disconnected_handler (vcl_worker_t * wrk,
553 session_disconnected_msg_t * msg)
555 vcl_session_t *session;
557 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
560 VDBG (0, "request to disconnect unknown handle 0x%llx", msg->handle);
564 /* Caught a disconnect before actually accepting the session */
565 if (session->session_state == STATE_LISTEN)
567 if (!vcl_flag_accepted_session (session, msg->handle,
568 VCL_ACCEPTED_F_CLOSED))
569 VDBG (0, "session was not accepted!");
573 session->session_state = STATE_VPP_CLOSING;
578 vcl_session_req_worker_update_handler (vcl_worker_t * wrk, void *data)
580 session_req_worker_update_msg_t *msg;
583 msg = (session_req_worker_update_msg_t *) data;
584 s = vcl_session_get_w_vpp_handle (wrk, msg->session_handle);
588 vec_add1 (wrk->pending_session_wrk_updates, s->session_index);
592 vcl_session_worker_update_reply_handler (vcl_worker_t * wrk, void *data)
594 session_worker_update_reply_msg_t *msg;
597 msg = (session_worker_update_reply_msg_t *) data;
598 s = vcl_session_get_w_vpp_handle (wrk, msg->handle);
601 VDBG (0, "unknown handle 0x%llx", msg->handle);
604 if (vcl_wait_for_segment (msg->segment_handle))
606 clib_warning ("segment for session %u couldn't be mounted!",
610 s->rx_fifo = uword_to_pointer (msg->rx_fifo, svm_fifo_t *);
611 s->tx_fifo = uword_to_pointer (msg->tx_fifo, svm_fifo_t *);
613 s->rx_fifo->client_session_index = s->session_index;
614 s->tx_fifo->client_session_index = s->session_index;
615 s->rx_fifo->client_thread_index = wrk->wrk_index;
616 s->tx_fifo->client_thread_index = wrk->wrk_index;
617 s->session_state = STATE_UPDATED;
619 VDBG (0, "session %u[0x%llx] moved to worker %u", s->session_index,
620 s->vpp_handle, wrk->wrk_index);
624 vcl_handle_mq_event (vcl_worker_t * wrk, session_event_t * e)
626 session_disconnected_msg_t *disconnected_msg;
627 vcl_session_t *session;
629 switch (e->event_type)
631 case FIFO_EVENT_APP_RX:
632 case FIFO_EVENT_APP_TX:
633 case SESSION_IO_EVT_CT_RX:
634 case SESSION_IO_EVT_CT_TX:
635 vec_add1 (wrk->unhandled_evts_vector, *e);
637 case SESSION_CTRL_EVT_ACCEPTED:
638 vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
640 case SESSION_CTRL_EVT_CONNECTED:
641 vcl_session_connected_handler (wrk,
642 (session_connected_msg_t *) e->data);
644 case SESSION_CTRL_EVT_DISCONNECTED:
645 disconnected_msg = (session_disconnected_msg_t *) e->data;
646 session = vcl_session_disconnected_handler (wrk, disconnected_msg);
649 session->session_state = STATE_DISCONNECT;
650 VDBG (0, "disconnected session %u [0x%llx]", session->session_index,
651 session->vpp_handle);
653 case SESSION_CTRL_EVT_RESET:
654 vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
656 case SESSION_CTRL_EVT_BOUND:
657 vcl_session_bound_handler (wrk, (session_bound_msg_t *) e->data);
659 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
660 vcl_session_req_worker_update_handler (wrk, e->data);
662 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
663 vcl_session_worker_update_reply_handler (wrk, e->data);
666 clib_warning ("unhandled %u", e->event_type);
672 vppcom_wait_for_session_state_change (u32 session_index,
673 session_state_t state,
676 vcl_worker_t *wrk = vcl_worker_get_current ();
677 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
678 vcl_session_t *volatile session;
684 session = vcl_session_get (wrk, session_index);
685 if (PREDICT_FALSE (!session))
687 return VPPCOM_EBADFD;
689 if (session->session_state & state)
693 if (session->session_state & STATE_FAILED)
695 return VPPCOM_ECONNREFUSED;
698 if (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0))
703 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
704 vcl_handle_mq_event (wrk, e);
705 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
707 while (clib_time_now (&wrk->clib_time) < timeout);
709 VDBG (0, "timeout waiting for state 0x%x (%s)", state,
710 vppcom_session_state_str (state));
711 vcl_evt (VCL_EVT_SESSION_TIMEOUT, session, session_state);
713 return VPPCOM_ETIMEDOUT;
717 vcl_handle_pending_wrk_updates (vcl_worker_t * wrk)
719 session_state_t state;
723 if (PREDICT_TRUE (vec_len (wrk->pending_session_wrk_updates) == 0))
726 vec_foreach (sip, wrk->pending_session_wrk_updates)
728 s = vcl_session_get (wrk, *sip);
729 vcl_send_session_worker_update (wrk, s, wrk->wrk_index);
730 state = s->session_state;
731 vppcom_wait_for_session_state_change (s->session_index, STATE_UPDATED, 5);
732 s->session_state = state;
734 vec_reset_length (wrk->pending_session_wrk_updates);
738 vcl_flush_mq_events (void)
740 vcl_worker_t *wrk = vcl_worker_get_current ();
741 svm_msg_q_msg_t *msg;
746 mq = wrk->app_event_queue;
748 vcl_mq_dequeue_batch (wrk, mq);
749 svm_msg_q_unlock (mq);
751 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
753 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
754 e = svm_msg_q_msg_data (mq, msg);
755 vcl_handle_mq_event (wrk, e);
756 svm_msg_q_free_msg (mq, msg);
758 vec_reset_length (wrk->mq_msg_vector);
759 vcl_handle_pending_wrk_updates (wrk);
763 vppcom_app_session_enable (void)
767 if (vcm->app_state != STATE_APP_ENABLED)
769 vppcom_send_session_enable_disable (1 /* is_enabled == TRUE */ );
770 rv = vcl_wait_for_app_state_change (STATE_APP_ENABLED);
771 if (PREDICT_FALSE (rv))
773 VDBG (0, "VCL<%d>: application session enable timed out! "
774 "returning %d (%s)", getpid (), rv, vppcom_retval_str (rv));
782 vppcom_app_attach (void)
786 vppcom_app_send_attach ();
787 rv = vcl_wait_for_app_state_change (STATE_APP_ATTACHED);
788 if (PREDICT_FALSE (rv))
790 VDBG (0, "VCL<%d>: application attach timed out! returning %d (%s)",
791 getpid (), rv, vppcom_retval_str (rv));
799 vppcom_session_unbind (u32 session_handle)
801 vcl_worker_t *wrk = vcl_worker_get_current ();
802 vcl_session_t *session = 0;
805 session = vcl_session_get_w_handle (wrk, session_handle);
807 return VPPCOM_EBADFD;
809 vpp_handle = session->vpp_handle;
810 vcl_session_table_del_listener (wrk, vpp_handle);
811 session->vpp_handle = ~0;
812 session->session_state = STATE_DISCONNECT;
814 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: sending unbind msg! new state"
815 " 0x%x (%s)", getpid (), vpp_handle, session_handle, STATE_DISCONNECT,
816 vppcom_session_state_str (STATE_DISCONNECT));
817 vcl_evt (VCL_EVT_UNBIND, session);
818 vppcom_send_unbind_sock (vpp_handle);
824 vppcom_session_disconnect (u32 session_handle)
826 vcl_worker_t *wrk = vcl_worker_get_current ();
827 svm_msg_q_t *vpp_evt_q;
828 vcl_session_t *session;
829 session_state_t state;
832 session = vcl_session_get_w_handle (wrk, session_handle);
834 return VPPCOM_EBADFD;
836 vpp_handle = session->vpp_handle;
837 state = session->session_state;
839 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u state 0x%x (%s)", getpid (),
840 vpp_handle, session_handle, state, vppcom_session_state_str (state));
842 if (PREDICT_FALSE (state & STATE_LISTEN))
844 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
845 "Cannot disconnect a listen socket!",
846 getpid (), vpp_handle, session_handle);
847 return VPPCOM_EBADFD;
850 if (state & STATE_VPP_CLOSING)
852 vpp_evt_q = vcl_session_vpp_evt_q (wrk, session);
853 vcl_send_session_disconnected_reply (vpp_evt_q, wrk->my_client_index,
855 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: sending disconnect "
856 "REPLY...", getpid (), vpp_handle, session_handle);
860 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: sending disconnect...",
861 getpid (), vpp_handle, session_handle);
862 vppcom_send_disconnect_session (vpp_handle);
871 * Notify vpp of the disconnect and mark the worker as free. If we're the
872 * last worker, do a full cleanup otherwise, since we're probably a forked
873 * child, avoid syscalls as much as possible. We might've lost privileges.
876 vppcom_app_exit (void)
878 if (!pool_elts (vcm->workers))
880 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
881 vcl_set_worker_index (~0);
883 if (vec_len (vcm->workers) == 1)
884 vl_client_disconnect_from_vlib ();
886 vl_client_send_disconnect (1 /* vpp should cleanup */ );
890 * VPPCOM Public API functions
893 vppcom_app_create (char *app_name)
895 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
900 VDBG (1, "already initialized");
901 return VPPCOM_EEXIST;
905 vppcom_cfg (&vcm->cfg);
908 vcm->main_cpu = pthread_self ();
909 vcm->main_pid = getpid ();
910 vcm->app_name = format (0, "%s", app_name);
911 vppcom_init_error_string_table ();
912 svm_fifo_segment_main_init (&vcm->segment_main, vcl_cfg->segment_baseva,
913 20 /* timeout in secs */ );
914 pool_alloc (vcm->workers, vcl_cfg->max_workers);
915 clib_spinlock_init (&vcm->workers_lock);
916 clib_rwlock_init (&vcm->segment_table_lock);
917 atexit (vppcom_app_exit);
919 /* Allocate default worker */
920 vcl_worker_alloc_and_init ();
922 /* API hookup and connect to VPP */
923 vppcom_api_hookup ();
925 vcm->app_state = STATE_APP_START;
926 rv = vppcom_connect_to_vpp (app_name);
929 VERR ("couldn't connect to VPP!");
932 VDBG (0, "sending session enable");
933 rv = vppcom_app_session_enable ();
936 VERR ("vppcom_app_session_enable() failed!");
940 VDBG (0, "sending app attach");
941 rv = vppcom_app_attach ();
944 VERR ("vppcom_app_attach() failed!");
948 VDBG (0, "app_name '%s', my_client_index %d (0x%x)", app_name,
949 vcm->workers[0].my_client_index, vcm->workers[0].my_client_index);
955 vppcom_app_destroy (void)
958 f64 orig_app_timeout;
960 if (!pool_elts (vcm->workers))
963 vcl_evt (VCL_EVT_DETACH, vcm);
965 if (pool_elts (vcm->workers) == 1)
967 vppcom_app_send_detach ();
968 orig_app_timeout = vcm->cfg.app_timeout;
969 vcm->cfg.app_timeout = 2.0;
970 rv = vcl_wait_for_app_state_change (STATE_APP_ENABLED);
971 vcm->cfg.app_timeout = orig_app_timeout;
972 if (PREDICT_FALSE (rv))
973 VDBG (0, "application detach timed out! returning %d (%s)", rv,
974 vppcom_retval_str (rv));
975 vec_free (vcm->app_name);
976 vcl_worker_cleanup (vcl_worker_get_current (), 0 /* notify vpp */ );
980 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
983 vcl_set_worker_index (~0);
985 vl_client_disconnect_from_vlib ();
989 vppcom_session_create (u8 proto, u8 is_nonblocking)
991 vcl_worker_t *wrk = vcl_worker_get_current ();
992 vcl_session_t *session;
994 session = vcl_session_alloc (wrk);
996 session->session_type = proto;
997 session->session_state = STATE_START;
998 session->vpp_handle = ~0;
999 session->is_dgram = proto == VPPCOM_PROTO_UDP;
1002 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK);
1004 vcl_evt (VCL_EVT_CREATE, session, session_type, session->session_state,
1005 is_nonblocking, session_index);
1007 VDBG (0, "created sid %u", session->session_index);
1009 return vcl_session_handle (session);
1013 vcl_session_cleanup (vcl_worker_t * wrk, vcl_session_t * session,
1014 vcl_session_handle_t sh, u8 do_disconnect)
1016 session_state_t state;
1017 u32 next_sh, vep_sh;
1022 is_vep = session->is_vep;
1023 next_sh = session->vep.next_sh;
1024 vep_sh = session->vep.vep_sh;
1025 state = session->session_state;
1026 vpp_handle = session->vpp_handle;
1028 VDBG (1, "session %u [0x%llx] closing", session->session_index, vpp_handle);
1032 while (next_sh != ~0)
1034 rv = vppcom_epoll_ctl (sh, EPOLL_CTL_DEL, next_sh, 0);
1035 if (PREDICT_FALSE (rv < 0))
1036 VDBG (0, "vpp handle 0x%llx, sid %u: EPOLL_CTL_DEL vep_idx %u"
1037 " failed! rv %d (%s)", vpp_handle, next_sh, vep_sh, rv,
1038 vppcom_retval_str (rv));
1040 next_sh = session->vep.next_sh;
1045 if (session->is_vep_session)
1047 rv = vppcom_epoll_ctl (vep_sh, EPOLL_CTL_DEL, sh, 0);
1049 VDBG (0, "session %u [0x%llx]: EPOLL_CTL_DEL vep_idx %u "
1050 "failed! rv %d (%s)", session->session_index, vpp_handle,
1051 vep_sh, rv, vppcom_retval_str (rv));
1056 VDBG (0, "session %u [0x%llx] disconnect skipped",
1057 session->session_index, vpp_handle);
1061 if (state & STATE_LISTEN)
1063 rv = vppcom_session_unbind (sh);
1064 if (PREDICT_FALSE (rv < 0))
1065 VDBG (0, "session %u [0x%llx]: listener unbind failed! "
1066 "rv %d (%s)", session->session_index, vpp_handle, rv,
1067 vppcom_retval_str (rv));
1069 else if (state & STATE_OPEN)
1071 rv = vppcom_session_disconnect (sh);
1072 if (PREDICT_FALSE (rv < 0))
1073 VDBG (0, "ERROR: session %u [0x%llx]: disconnect failed!"
1074 " rv %d (%s)", session->session_index, vpp_handle,
1075 rv, vppcom_retval_str (rv));
1077 else if (state == STATE_DISCONNECT)
1079 svm_msg_q_t *mq = vcl_session_vpp_evt_q (wrk, session);
1080 vcl_send_session_reset_reply (mq, wrk->my_client_index,
1081 session->vpp_handle, 0);
1085 if (vcl_session_is_ct (session))
1087 vcl_cut_through_registration_t *ctr;
1090 mq_addr = pointer_to_uword (session->our_evt_q);
1091 ctr = vcl_ct_registration_lock_and_lookup (wrk, mq_addr);
1093 if (ctr->epoll_evt_conn_index != ~0)
1094 vcl_mq_epoll_del_evfd (wrk, ctr->epoll_evt_conn_index);
1095 VDBG (0, "Removing ct registration %u",
1096 vcl_ct_registration_index (wrk, ctr));
1097 vcl_ct_registration_del (wrk, ctr);
1098 vcl_ct_registration_lookup_del (wrk, mq_addr);
1099 vcl_ct_registration_unlock (wrk);
1103 vcl_session_table_del_vpp_handle (wrk, vpp_handle);
1104 vcl_session_free (wrk, session);
1106 VDBG (0, "session %u [0x%llx] removed", session->session_index, vpp_handle);
1107 vcl_evt (VCL_EVT_CLOSE, session, rv);
1113 vppcom_session_close (uint32_t session_handle)
1115 vcl_worker_t *wrk = vcl_worker_get_current ();
1116 vcl_session_t *session;
1118 session = vcl_session_get_w_handle (wrk, session_handle);
1120 return VPPCOM_EBADFD;
1121 return vcl_session_cleanup (wrk, session, session_handle,
1122 1 /* do_disconnect */ );
1126 vppcom_session_bind (uint32_t session_handle, vppcom_endpt_t * ep)
1128 vcl_worker_t *wrk = vcl_worker_get_current ();
1129 vcl_session_t *session = 0;
1132 return VPPCOM_EINVAL;
1134 session = vcl_session_get_w_handle (wrk, session_handle);
1136 return VPPCOM_EBADFD;
1138 if (session->is_vep)
1140 clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
1141 "bind to an epoll session!", getpid (), session_handle);
1142 return VPPCOM_EBADFD;
1145 session->transport.is_ip4 = ep->is_ip4;
1147 clib_memcpy_fast (&session->transport.lcl_ip.ip4, ep->ip,
1148 sizeof (ip4_address_t));
1150 clib_memcpy_fast (&session->transport.lcl_ip.ip6, ep->ip,
1151 sizeof (ip6_address_t));
1152 session->transport.lcl_port = ep->port;
1154 VDBG (0, "VCL<%d>: sid %u: binding to local %s address %U port %u, "
1155 "proto %s", getpid (), session_handle,
1156 session->transport.is_ip4 ? "IPv4" : "IPv6",
1157 format_ip46_address, &session->transport.lcl_ip,
1158 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
1159 clib_net_to_host_u16 (session->transport.lcl_port),
1160 vppcom_proto_str (session->session_type));
1161 vcl_evt (VCL_EVT_BIND, session);
1163 if (session->session_type == VPPCOM_PROTO_UDP)
1164 vppcom_session_listen (session_handle, 10);
1170 vppcom_session_listen (uint32_t listen_sh, uint32_t q_len)
1172 vcl_worker_t *wrk = vcl_worker_get_current ();
1173 vcl_session_t *listen_session = 0;
1174 u64 listen_vpp_handle;
1177 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
1178 if (!listen_session || listen_session->is_vep)
1179 return VPPCOM_EBADFD;
1181 if (q_len == 0 || q_len == ~0)
1182 q_len = vcm->cfg.listen_queue_size;
1184 listen_vpp_handle = listen_session->vpp_handle;
1185 if (listen_session->session_state & STATE_LISTEN)
1187 VDBG (0, "session %u [0x%llx]: already in listen state!",
1188 listen_sh, listen_vpp_handle);
1192 VDBG (0, "session %u [0x%llx]: sending vpp listen request...",
1193 listen_sh, listen_vpp_handle);
1196 * Send listen request to vpp and wait for reply
1198 vppcom_send_bind_sock (listen_session);
1199 rv = vppcom_wait_for_session_state_change (listen_session->session_index,
1201 vcm->cfg.session_timeout);
1203 if (PREDICT_FALSE (rv))
1205 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
1206 VDBG (0, "session %u [0x%llx]: listen failed! returning %d (%s)",
1207 listen_sh, listen_session->vpp_handle, rv,
1208 vppcom_retval_str (rv));
1216 vppcom_session_tls_add_cert (uint32_t session_handle, char *cert,
1220 vcl_worker_t *wrk = vcl_worker_get_current ();
1221 vcl_session_t *session = 0;
1223 session = vcl_session_get_w_handle (wrk, session_handle);
1225 return VPPCOM_EBADFD;
1227 if (cert_len == 0 || cert_len == ~0)
1228 return VPPCOM_EBADFD;
1231 * Send listen request to vpp and wait for reply
1233 vppcom_send_application_tls_cert_add (session, cert, cert_len);
1240 vppcom_session_tls_add_key (uint32_t session_handle, char *key,
1244 vcl_worker_t *wrk = vcl_worker_get_current ();
1245 vcl_session_t *session = 0;
1247 session = vcl_session_get_w_handle (wrk, session_handle);
1249 return VPPCOM_EBADFD;
1251 if (key_len == 0 || key_len == ~0)
1252 return VPPCOM_EBADFD;
1255 * Send listen request to vpp and wait for reply
1257 vppcom_send_application_tls_key_add (session, key, key_len);
1265 validate_args_session_accept_ (vcl_worker_t * wrk,
1266 vcl_session_t * listen_session)
1268 /* Input validation - expects spinlock on sessions_lockp */
1269 if (listen_session->is_vep)
1271 clib_warning ("VCL<%d>: ERROR: sid %u: cannot accept on an "
1272 "epoll session!", getpid (),
1273 listen_session->session_index);
1274 return VPPCOM_EBADFD;
1277 if (listen_session->session_state != STATE_LISTEN)
1279 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
1280 "not in listen state! state 0x%x (%s)", getpid (),
1281 listen_session->vpp_handle, listen_session->session_index,
1282 listen_session->session_state,
1283 vppcom_session_state_str (listen_session->session_state));
1284 return VPPCOM_EBADFD;
1290 vppcom_session_accept (uint32_t listen_session_handle, vppcom_endpt_t * ep,
1293 u32 client_session_index = ~0, listen_session_index, accept_flags = 0;
1294 vcl_worker_t *wrk = vcl_worker_get_current ();
1295 session_accepted_msg_t accepted_msg;
1296 vcl_session_t *listen_session = 0;
1297 vcl_session_t *client_session = 0;
1298 svm_msg_q_t *vpp_evt_q;
1299 vcl_session_msg_t *evt;
1300 u64 listen_vpp_handle;
1301 svm_msg_q_msg_t msg;
1306 listen_session = vcl_session_get_w_handle (wrk, listen_session_handle);
1307 if (!listen_session)
1308 return VPPCOM_EBADFD;
1310 listen_session_index = listen_session->session_index;
1311 if ((rv = validate_args_session_accept_ (wrk, listen_session)))
1314 if (clib_fifo_elts (listen_session->accept_evts_fifo))
1316 clib_fifo_sub2 (listen_session->accept_evts_fifo, evt);
1317 accept_flags = evt->flags;
1318 accepted_msg = evt->accepted_msg;
1322 is_nonblocking = VCL_SESS_ATTR_TEST (listen_session->attr,
1323 VCL_SESS_ATTR_NONBLOCK);
1324 if (svm_msg_q_is_empty (wrk->app_event_queue) && is_nonblocking)
1325 return VPPCOM_EAGAIN;
1329 if (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_WAIT, 0))
1330 return VPPCOM_EAGAIN;
1332 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
1333 if (e->event_type != SESSION_CTRL_EVT_ACCEPTED)
1335 clib_warning ("discarded event: %u", e->event_type);
1336 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
1339 clib_memcpy_fast (&accepted_msg, e->data, sizeof (accepted_msg));
1340 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
1346 client_session_index = vcl_session_accepted_handler (wrk, &accepted_msg);
1347 listen_session = vcl_session_get (wrk, listen_session_index);
1348 client_session = vcl_session_get (wrk, client_session_index);
1350 if (flags & O_NONBLOCK)
1351 VCL_SESS_ATTR_SET (client_session->attr, VCL_SESS_ATTR_NONBLOCK);
1353 listen_vpp_handle = listen_session->vpp_handle;
1354 VDBG (1, "vpp handle 0x%llx, sid %u: Got a client request! "
1355 "vpp handle 0x%llx, sid %u, flags %d, is_nonblocking %u",
1356 listen_vpp_handle, listen_session_handle,
1357 client_session->vpp_handle, client_session_index,
1358 flags, VCL_SESS_ATTR_TEST (client_session->attr,
1359 VCL_SESS_ATTR_NONBLOCK));
1363 ep->is_ip4 = client_session->transport.is_ip4;
1364 ep->port = client_session->transport.rmt_port;
1365 if (client_session->transport.is_ip4)
1366 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip4,
1367 sizeof (ip4_address_t));
1369 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip6,
1370 sizeof (ip6_address_t));
1373 if (accepted_msg.server_event_queue_address)
1374 vpp_evt_q = uword_to_pointer (accepted_msg.vpp_event_queue_address,
1377 vpp_evt_q = client_session->vpp_evt_q;
1379 vcl_send_session_accepted_reply (vpp_evt_q, client_session->client_context,
1380 client_session->vpp_handle, 0);
1382 VDBG (0, "listener %u [0x%llx] accepted %u [0x%llx] peer: %U:%u "
1383 "local: %U:%u", listen_session_handle, listen_vpp_handle,
1384 client_session_index, client_session->vpp_handle,
1385 format_ip46_address, &client_session->transport.rmt_ip,
1386 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
1387 clib_net_to_host_u16 (client_session->transport.rmt_port),
1388 format_ip46_address, &client_session->transport.lcl_ip,
1389 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
1390 clib_net_to_host_u16 (client_session->transport.lcl_port));
1391 vcl_evt (VCL_EVT_ACCEPT, client_session, listen_session,
1392 client_session_index);
1395 * Session might have been closed already
1399 if (accept_flags & VCL_ACCEPTED_F_CLOSED)
1400 client_session->session_state = STATE_VPP_CLOSING;
1401 else if (accept_flags & VCL_ACCEPTED_F_RESET)
1402 client_session->session_state = STATE_DISCONNECT;
1404 return vcl_session_handle (client_session);
1408 vppcom_session_connect (uint32_t session_handle, vppcom_endpt_t * server_ep)
1410 vcl_worker_t *wrk = vcl_worker_get_current ();
1411 vcl_session_t *session = 0;
1415 session = vcl_session_get_w_handle (wrk, session_handle);
1417 return VPPCOM_EBADFD;
1418 session_index = session->session_index;
1420 if (PREDICT_FALSE (session->is_vep))
1422 clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
1423 "connect on an epoll session!", getpid (),
1425 return VPPCOM_EBADFD;
1428 if (PREDICT_FALSE (session->session_state & CLIENT_STATE_OPEN))
1430 VDBG (0, "session handle %u [0x%llx]: session already "
1431 "connected to %s %U port %d proto %s, state 0x%x (%s)",
1432 session_handle, session->vpp_handle,
1433 session->transport.is_ip4 ? "IPv4" : "IPv6",
1434 format_ip46_address,
1435 &session->transport.rmt_ip, session->transport.is_ip4 ?
1436 IP46_TYPE_IP4 : IP46_TYPE_IP6,
1437 clib_net_to_host_u16 (session->transport.rmt_port),
1438 vppcom_proto_str (session->session_type), session->session_state,
1439 vppcom_session_state_str (session->session_state));
1443 session->transport.is_ip4 = server_ep->is_ip4;
1444 if (session->transport.is_ip4)
1445 clib_memcpy_fast (&session->transport.rmt_ip.ip4, server_ep->ip,
1446 sizeof (ip4_address_t));
1448 clib_memcpy_fast (&session->transport.rmt_ip.ip6, server_ep->ip,
1449 sizeof (ip6_address_t));
1450 session->transport.rmt_port = server_ep->port;
1452 VDBG (0, "session handle %u [0x%llx]: connecting to server %s %U "
1453 "port %d proto %s", session_handle, session->vpp_handle,
1454 session->transport.is_ip4 ? "IPv4" : "IPv6",
1455 format_ip46_address,
1456 &session->transport.rmt_ip, session->transport.is_ip4 ?
1457 IP46_TYPE_IP4 : IP46_TYPE_IP6,
1458 clib_net_to_host_u16 (session->transport.rmt_port),
1459 vppcom_proto_str (session->session_type));
1462 * Send connect request and wait for reply from vpp
1464 vppcom_send_connect_sock (session);
1465 rv = vppcom_wait_for_session_state_change (session_index, STATE_CONNECT,
1466 vcm->cfg.session_timeout);
1468 session = vcl_session_get (wrk, session_index);
1470 if (PREDICT_FALSE (rv))
1472 if (VPPCOM_DEBUG > 0)
1475 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: connect "
1476 "failed! returning %d (%s)", getpid (),
1477 session->vpp_handle, session_handle, rv,
1478 vppcom_retval_str (rv));
1480 clib_warning ("VCL<%d>: no session for sid %u: connect failed! "
1481 "returning %d (%s)", getpid (),
1482 session_handle, rv, vppcom_retval_str (rv));
1486 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: connected!",
1487 getpid (), session->vpp_handle, session_handle);
1493 vcl_is_rx_evt_for_session (session_event_t * e, u32 sid, u8 is_ct)
1496 return (e->event_type == FIFO_EVENT_APP_RX
1497 && e->fifo->client_session_index == sid);
1499 return (e->event_type == SESSION_IO_EVT_CT_TX);
1503 vcl_session_is_readable (vcl_session_t * s)
1505 return ((s->session_state & STATE_OPEN)
1506 || (s->session_state == STATE_LISTEN
1507 && s->session_type == VPPCOM_PROTO_UDP));
1511 vppcom_session_read_internal (uint32_t session_handle, void *buf, int n,
1514 vcl_worker_t *wrk = vcl_worker_get_current ();
1515 int n_read = 0, rv, is_nonblocking;
1516 vcl_session_t *s = 0;
1517 svm_fifo_t *rx_fifo;
1518 svm_msg_q_msg_t msg;
1523 if (PREDICT_FALSE (!buf))
1524 return VPPCOM_EINVAL;
1526 s = vcl_session_get_w_handle (wrk, session_handle);
1527 if (PREDICT_FALSE (!s || s->is_vep))
1528 return VPPCOM_EBADFD;
1530 if (PREDICT_FALSE (!vcl_session_is_readable (s)))
1532 session_state_t state = s->session_state;
1533 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
1535 VDBG (0, "session handle %u[0x%llx] is not open! state 0x%x (%s),"
1536 " returning %d (%s)", session_handle, s->vpp_handle, state,
1537 vppcom_session_state_str (state), rv, vppcom_retval_str (rv));
1541 is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
1542 is_ct = vcl_session_is_ct (s);
1543 mq = is_ct ? s->our_evt_q : wrk->app_event_queue;
1544 rx_fifo = s->rx_fifo;
1547 if (svm_fifo_is_empty (rx_fifo))
1551 svm_fifo_unset_event (rx_fifo);
1552 return VPPCOM_EWOULDBLOCK;
1554 while (svm_fifo_is_empty (rx_fifo))
1556 svm_fifo_unset_event (rx_fifo);
1557 svm_msg_q_lock (mq);
1558 if (svm_msg_q_is_empty (mq))
1559 svm_msg_q_wait (mq);
1561 svm_msg_q_sub_w_lock (mq, &msg);
1562 e = svm_msg_q_msg_data (mq, &msg);
1563 svm_msg_q_unlock (mq);
1564 if (!vcl_is_rx_evt_for_session (e, s->session_index, is_ct))
1565 vcl_handle_mq_event (wrk, e);
1566 svm_msg_q_free_msg (mq, &msg);
1568 if (PREDICT_FALSE (s->session_state == STATE_DISCONNECT))
1569 return VPPCOM_ECONNRESET;
1574 n_read = app_recv_dgram_raw (rx_fifo, buf, n, &s->transport, 0, peek);
1576 n_read = app_recv_stream_raw (rx_fifo, buf, n, 0, peek);
1578 if (svm_fifo_is_empty (rx_fifo))
1579 svm_fifo_unset_event (rx_fifo);
1581 if (is_ct && svm_fifo_needs_tx_ntf (rx_fifo, n_read))
1583 svm_fifo_clear_tx_ntf (s->rx_fifo);
1584 app_send_io_evt_to_vpp (s->vpp_evt_q, s->rx_fifo, SESSION_IO_EVT_CT_RX,
1588 VDBG (2, "vpp handle 0x%llx, sid %u: read %d bytes from (%p)",
1589 s->vpp_handle, session_handle, n_read, rx_fifo);
1595 vppcom_session_read (uint32_t session_handle, void *buf, size_t n)
1597 return (vppcom_session_read_internal (session_handle, buf, n, 0));
1601 vppcom_session_peek (uint32_t session_handle, void *buf, int n)
1603 return (vppcom_session_read_internal (session_handle, buf, n, 1));
1607 vppcom_session_read_segments (uint32_t session_handle,
1608 vppcom_data_segments_t ds)
1610 vcl_worker_t *wrk = vcl_worker_get_current ();
1611 int n_read = 0, rv, is_nonblocking;
1612 vcl_session_t *s = 0;
1613 svm_fifo_t *rx_fifo;
1614 svm_msg_q_msg_t msg;
1619 s = vcl_session_get_w_handle (wrk, session_handle);
1620 if (PREDICT_FALSE (!s || s->is_vep))
1621 return VPPCOM_EBADFD;
1623 if (PREDICT_FALSE (!vcl_session_is_readable (s)))
1625 session_state_t state = s->session_state;
1626 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
1630 is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
1631 is_ct = vcl_session_is_ct (s);
1632 mq = is_ct ? s->our_evt_q : wrk->app_event_queue;
1633 rx_fifo = s->rx_fifo;
1636 if (svm_fifo_is_empty (rx_fifo))
1640 svm_fifo_unset_event (rx_fifo);
1641 return VPPCOM_EWOULDBLOCK;
1643 while (svm_fifo_is_empty (rx_fifo))
1645 svm_fifo_unset_event (rx_fifo);
1646 svm_msg_q_lock (mq);
1647 if (svm_msg_q_is_empty (mq))
1648 svm_msg_q_wait (mq);
1650 svm_msg_q_sub_w_lock (mq, &msg);
1651 e = svm_msg_q_msg_data (mq, &msg);
1652 svm_msg_q_unlock (mq);
1653 if (!vcl_is_rx_evt_for_session (e, s->session_index, is_ct))
1654 vcl_handle_mq_event (wrk, e);
1655 svm_msg_q_free_msg (mq, &msg);
1657 if (PREDICT_FALSE (s->session_state == STATE_DISCONNECT))
1658 return VPPCOM_ECONNRESET;
1662 n_read = svm_fifo_segments (rx_fifo, (svm_fifo_segment_t *) ds);
1663 svm_fifo_unset_event (rx_fifo);
1665 if (is_ct && n_read + svm_fifo_max_dequeue (rx_fifo) == rx_fifo->nitems)
1667 /* If the peer is not polling send notification */
1668 if (!svm_fifo_has_event (s->rx_fifo))
1669 app_send_io_evt_to_vpp (s->vpp_evt_q, s->rx_fifo,
1670 SESSION_IO_EVT_CT_RX, SVM_Q_WAIT);
1677 vppcom_session_free_segments (uint32_t session_handle,
1678 vppcom_data_segments_t ds)
1680 vcl_worker_t *wrk = vcl_worker_get_current ();
1683 s = vcl_session_get_w_handle (wrk, session_handle);
1684 if (PREDICT_FALSE (!s || s->is_vep))
1687 svm_fifo_segments_free (s->rx_fifo, (svm_fifo_segment_t *) ds);
1691 vppcom_session_read_ready (vcl_session_t * session)
1693 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
1694 if (PREDICT_FALSE (session->is_vep))
1696 clib_warning ("VCL<%d>: ERROR: sid %u: cannot read from an "
1697 "epoll session!", getpid (), session->session_index);
1698 return VPPCOM_EBADFD;
1701 if (PREDICT_FALSE (!(session->session_state & (STATE_OPEN | STATE_LISTEN))))
1703 session_state_t state = session->session_state;
1706 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
1708 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: session is not open!"
1709 " state 0x%x (%s), returning %d (%s)", getpid (),
1710 session->vpp_handle, session->session_index, state,
1711 vppcom_session_state_str (state), rv, vppcom_retval_str (rv));
1715 if (session->session_state & STATE_LISTEN)
1716 return clib_fifo_elts (session->accept_evts_fifo);
1718 return svm_fifo_max_dequeue (session->rx_fifo);
1722 vppcom_data_segment_copy (void *buf, vppcom_data_segments_t ds, u32 max_bytes)
1724 u32 first_copy = clib_min (ds[0].len, max_bytes);
1725 clib_memcpy_fast (buf, ds[0].data, first_copy);
1726 if (first_copy < max_bytes)
1728 clib_memcpy_fast (buf + first_copy, ds[1].data,
1729 clib_min (ds[1].len, max_bytes - first_copy));
1735 vcl_is_tx_evt_for_session (session_event_t * e, u32 sid, u8 is_ct)
1738 return (e->event_type == FIFO_EVENT_APP_TX
1739 && e->fifo->client_session_index == sid);
1741 return (e->event_type == SESSION_IO_EVT_CT_RX);
1745 vppcom_session_write_inline (uint32_t session_handle, void *buf, size_t n,
1748 vcl_worker_t *wrk = vcl_worker_get_current ();
1749 int rv, n_write, is_nonblocking;
1750 vcl_session_t *s = 0;
1751 svm_fifo_t *tx_fifo = 0;
1752 session_evt_type_t et;
1753 svm_msg_q_msg_t msg;
1758 if (PREDICT_FALSE (!buf))
1759 return VPPCOM_EINVAL;
1761 s = vcl_session_get_w_handle (wrk, session_handle);
1762 if (PREDICT_FALSE (!s))
1763 return VPPCOM_EBADFD;
1765 if (PREDICT_FALSE (s->is_vep))
1767 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
1768 "cannot write to an epoll session!",
1769 getpid (), s->vpp_handle, session_handle);
1771 return VPPCOM_EBADFD;
1774 if (PREDICT_FALSE (!(s->session_state & STATE_OPEN)))
1776 session_state_t state = s->session_state;
1777 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
1778 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: session is not open! "
1779 "state 0x%x (%s)", getpid (), s->vpp_handle, session_handle,
1780 state, vppcom_session_state_str (state));
1784 tx_fifo = s->tx_fifo;
1785 is_ct = vcl_session_is_ct (s);
1786 is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
1787 mq = is_ct ? s->our_evt_q : wrk->app_event_queue;
1788 if (svm_fifo_is_full (tx_fifo))
1792 return VPPCOM_EWOULDBLOCK;
1794 while (svm_fifo_is_full (tx_fifo))
1796 svm_fifo_add_want_tx_ntf (tx_fifo, SVM_FIFO_WANT_TX_NOTIF);
1797 svm_msg_q_lock (mq);
1798 if (svm_msg_q_is_empty (mq))
1799 svm_msg_q_wait (mq);
1801 svm_msg_q_sub_w_lock (mq, &msg);
1802 e = svm_msg_q_msg_data (mq, &msg);
1803 svm_msg_q_unlock (mq);
1805 if (!vcl_is_tx_evt_for_session (e, s->session_index, is_ct))
1806 vcl_handle_mq_event (wrk, e);
1807 svm_msg_q_free_msg (mq, &msg);
1809 if (PREDICT_FALSE (!(s->session_state & STATE_OPEN)))
1810 return VPPCOM_ECONNRESET;
1814 ASSERT (FIFO_EVENT_APP_TX + 1 == SESSION_IO_EVT_CT_TX);
1815 et = FIFO_EVENT_APP_TX + vcl_session_is_ct (s);
1816 if (is_flush && !vcl_session_is_ct (s))
1817 et = SESSION_IO_EVT_TX_FLUSH;
1820 n_write = app_send_dgram_raw (tx_fifo, &s->transport,
1821 s->vpp_evt_q, buf, n, et, SVM_Q_WAIT);
1823 n_write = app_send_stream_raw (tx_fifo, s->vpp_evt_q, buf, n, et,
1826 ASSERT (n_write > 0);
1828 VDBG (2, "VCL<%d>: vpp handle 0x%llx, sid %u: wrote %d bytes", getpid (),
1829 s->vpp_handle, session_handle, n_write);
1835 vppcom_session_write (uint32_t session_handle, void *buf, size_t n)
1837 return vppcom_session_write_inline (session_handle, buf, n,
1842 vppcom_session_write_msg (uint32_t session_handle, void *buf, size_t n)
1844 return vppcom_session_write_inline (session_handle, buf, n,
1849 static vcl_session_t *
1850 vcl_ct_session_get_from_fifo (vcl_worker_t * wrk, svm_fifo_t * f, u8 type)
1853 s = vcl_session_get (wrk, f->client_session_index);
1857 if (type == 0 && s->rx_fifo == f)
1860 if (type == 1 && s->tx_fifo == f)
1863 s = vcl_session_get (wrk, f->master_session_index);
1866 if (type == 0 && s->rx_fifo == f)
1868 if (type == 1 && s->tx_fifo == f)
1875 vppcom_session_write_ready (vcl_session_t * session)
1877 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
1878 if (PREDICT_FALSE (session->is_vep))
1880 VDBG (0, "session %u [0x%llx]: cannot write to an epoll session!",
1881 session->session_index, session->vpp_handle);
1882 return VPPCOM_EBADFD;
1885 if (PREDICT_FALSE (session->session_state & STATE_LISTEN))
1887 if (session->tx_fifo)
1888 return svm_fifo_max_enqueue (session->tx_fifo);
1890 return VPPCOM_EBADFD;
1893 if (PREDICT_FALSE (!(session->session_state & STATE_OPEN)))
1895 session_state_t state = session->session_state;
1898 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
1899 VDBG (0, "session %u [0x%llx]: session is not open! state 0x%x (%s), "
1900 "returning %d (%s)", session->session_index, session->vpp_handle,
1901 state, vppcom_session_state_str (state), rv,
1902 vppcom_retval_str (rv));
1906 return svm_fifo_max_enqueue (session->tx_fifo);
1909 #define vcl_fifo_rx_evt_valid_or_break(_fifo) \
1910 if (PREDICT_FALSE (svm_fifo_is_empty (_fifo))) \
1912 svm_fifo_unset_event (_fifo); \
1913 if (svm_fifo_is_empty (_fifo)) \
1918 vcl_select_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
1919 unsigned long n_bits, unsigned long *read_map,
1920 unsigned long *write_map,
1921 unsigned long *except_map, u32 * bits_set)
1923 session_disconnected_msg_t *disconnected_msg;
1924 session_connected_msg_t *connected_msg;
1925 vcl_session_t *session;
1928 switch (e->event_type)
1930 case FIFO_EVENT_APP_RX:
1931 vcl_fifo_rx_evt_valid_or_break (e->fifo);
1932 sid = e->fifo->client_session_index;
1933 session = vcl_session_get (wrk, sid);
1936 if (sid < n_bits && read_map)
1938 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
1942 case FIFO_EVENT_APP_TX:
1943 sid = e->fifo->client_session_index;
1944 session = vcl_session_get (wrk, sid);
1947 if (sid < n_bits && write_map)
1949 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
1953 case SESSION_IO_EVT_CT_TX:
1954 vcl_fifo_rx_evt_valid_or_break (e->fifo);
1955 session = vcl_ct_session_get_from_fifo (wrk, e->fifo, 0);
1958 sid = session->session_index;
1959 if (sid < n_bits && read_map)
1961 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
1965 case SESSION_IO_EVT_CT_RX:
1966 session = vcl_ct_session_get_from_fifo (wrk, e->fifo, 1);
1969 sid = session->session_index;
1970 if (sid < n_bits && write_map)
1972 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
1976 case SESSION_CTRL_EVT_ACCEPTED:
1977 session = vcl_session_accepted (wrk,
1978 (session_accepted_msg_t *) e->data);
1981 sid = session->session_index;
1982 if (sid < n_bits && read_map)
1984 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
1988 case SESSION_CTRL_EVT_CONNECTED:
1989 connected_msg = (session_connected_msg_t *) e->data;
1990 vcl_session_connected_handler (wrk, connected_msg);
1992 case SESSION_CTRL_EVT_DISCONNECTED:
1993 disconnected_msg = (session_disconnected_msg_t *) e->data;
1994 session = vcl_session_disconnected_handler (wrk, disconnected_msg);
1997 sid = session->session_index;
1998 if (sid < n_bits && except_map)
2000 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
2004 case SESSION_CTRL_EVT_RESET:
2005 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
2006 if (sid < n_bits && except_map)
2008 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
2012 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
2013 vcl_session_worker_update_reply_handler (wrk, e->data);
2015 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
2016 vcl_session_req_worker_update_handler (wrk, e->data);
2019 clib_warning ("unhandled: %u", e->event_type);
2025 vcl_select_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
2026 unsigned long n_bits, unsigned long *read_map,
2027 unsigned long *write_map, unsigned long *except_map,
2028 double time_to_wait, u32 * bits_set)
2030 svm_msg_q_msg_t *msg;
2034 svm_msg_q_lock (mq);
2035 if (svm_msg_q_is_empty (mq))
2039 svm_msg_q_unlock (mq);
2045 svm_msg_q_unlock (mq);
2048 else if (time_to_wait < 0)
2050 svm_msg_q_wait (mq);
2054 if (svm_msg_q_timedwait (mq, time_to_wait))
2056 svm_msg_q_unlock (mq);
2061 vcl_mq_dequeue_batch (wrk, mq);
2062 svm_msg_q_unlock (mq);
2064 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
2066 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
2067 e = svm_msg_q_msg_data (mq, msg);
2068 vcl_select_handle_mq_event (wrk, e, n_bits, read_map, write_map,
2069 except_map, bits_set);
2070 svm_msg_q_free_msg (mq, msg);
2072 vec_reset_length (wrk->mq_msg_vector);
2073 vcl_handle_pending_wrk_updates (wrk);
2078 vppcom_select_condvar (vcl_worker_t * wrk, int n_bits,
2079 vcl_si_set * read_map, vcl_si_set * write_map,
2080 vcl_si_set * except_map, double time_to_wait,
2083 double total_wait = 0, wait_slice;
2084 vcl_cut_through_registration_t *cr;
2086 time_to_wait = (time_to_wait == -1) ? 1e6 : time_to_wait;
2087 wait_slice = wrk->cut_through_registrations ? 10e-6 : time_to_wait;
2090 vcl_ct_registration_lock (wrk);
2092 pool_foreach (cr, wrk->cut_through_registrations, ({
2093 vcl_select_handle_mq (wrk, cr->mq, n_bits, read_map, write_map, except_map,
2097 vcl_ct_registration_unlock (wrk);
2099 vcl_select_handle_mq (wrk, wrk->app_event_queue, n_bits, read_map,
2100 write_map, except_map, wait_slice, bits_set);
2101 total_wait += wait_slice;
2105 while (total_wait < time_to_wait);
2111 vppcom_select_eventfd (vcl_worker_t * wrk, int n_bits,
2112 vcl_si_set * read_map, vcl_si_set * write_map,
2113 vcl_si_set * except_map, double time_to_wait,
2116 vcl_mq_evt_conn_t *mqc;
2117 int __clib_unused n_read;
2121 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
2122 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
2123 vec_len (wrk->mq_events), time_to_wait);
2124 for (i = 0; i < n_mq_evts; i++)
2126 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
2127 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
2128 vcl_select_handle_mq (wrk, mqc->mq, n_bits, read_map, write_map,
2129 except_map, 0, bits_set);
2132 return (n_mq_evts > 0 ? (int) *bits_set : 0);
2136 vppcom_select (int n_bits, vcl_si_set * read_map, vcl_si_set * write_map,
2137 vcl_si_set * except_map, double time_to_wait)
2139 u32 sid, minbits = clib_max (n_bits, BITS (uword)), bits_set = 0;
2140 vcl_worker_t *wrk = vcl_worker_get_current ();
2141 vcl_session_t *session = 0;
2144 if (n_bits && read_map)
2146 clib_bitmap_validate (wrk->rd_bitmap, minbits);
2147 clib_memcpy_fast (wrk->rd_bitmap, read_map,
2148 vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
2149 memset (read_map, 0, vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
2151 if (n_bits && write_map)
2153 clib_bitmap_validate (wrk->wr_bitmap, minbits);
2154 clib_memcpy_fast (wrk->wr_bitmap, write_map,
2155 vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
2156 memset (write_map, 0, vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
2158 if (n_bits && except_map)
2160 clib_bitmap_validate (wrk->ex_bitmap, minbits);
2161 clib_memcpy_fast (wrk->ex_bitmap, except_map,
2162 vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
2163 memset (except_map, 0, vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
2173 clib_bitmap_foreach (sid, wrk->wr_bitmap, ({
2174 if (!(session = vcl_session_get (wrk, sid)))
2176 if (except_map && sid < minbits)
2177 clib_bitmap_set_no_check (except_map, sid, 1);
2181 rv = svm_fifo_is_full (session->tx_fifo);
2184 clib_bitmap_set_no_check ((uword*)write_map, sid, 1);
2188 svm_fifo_add_want_tx_ntf (session->tx_fifo, SVM_FIFO_WANT_TX_NOTIF);
2195 clib_bitmap_foreach (sid, wrk->rd_bitmap, ({
2196 if (!(session = vcl_session_get (wrk, sid)))
2198 if (except_map && sid < minbits)
2199 clib_bitmap_set_no_check (except_map, sid, 1);
2203 rv = vppcom_session_read_ready (session);
2206 clib_bitmap_set_no_check ((uword*)read_map, sid, 1);
2214 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
2216 vcl_select_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i], n_bits,
2217 read_map, write_map, except_map, &bits_set);
2219 vec_reset_length (wrk->unhandled_evts_vector);
2221 if (vcm->cfg.use_mq_eventfd)
2222 vppcom_select_eventfd (wrk, n_bits, read_map, write_map, except_map,
2223 time_to_wait, &bits_set);
2225 vppcom_select_condvar (wrk, n_bits, read_map, write_map, except_map,
2226 time_to_wait, &bits_set);
2232 vep_verify_epoll_chain (vcl_worker_t * wrk, u32 vep_idx)
2234 vcl_session_t *session;
2235 vppcom_epoll_t *vep;
2238 if (VPPCOM_DEBUG <= 1)
2241 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
2242 session = vcl_session_get (wrk, vep_idx);
2243 if (PREDICT_FALSE (!session))
2245 clib_warning ("VCL<%d>: ERROR: Invalid vep_idx (%u)!",
2246 getpid (), vep_idx);
2249 if (PREDICT_FALSE (!session->is_vep))
2251 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
2252 getpid (), vep_idx);
2255 vep = &session->vep;
2256 clib_warning ("VCL<%d>: vep_idx (%u): Dumping epoll chain\n"
2259 " is_vep_session = %u\n"
2260 " next_sid = 0x%x (%u)\n"
2261 " wait_cont_idx = 0x%x (%u)\n"
2262 "}\n", getpid (), vep_idx,
2263 session->is_vep, session->is_vep_session,
2264 vep->next_sh, vep->next_sh,
2265 session->wait_cont_idx, session->wait_cont_idx);
2267 for (sid = vep->next_sh; sid != ~0; sid = vep->next_sh)
2269 session = vcl_session_get (wrk, sid);
2270 if (PREDICT_FALSE (!session))
2272 clib_warning ("VCL<%d>: ERROR: Invalid sid (%u)!", getpid (), sid);
2275 if (PREDICT_FALSE (session->is_vep))
2276 clib_warning ("VCL<%d>: ERROR: sid (%u) is a vep!",
2277 getpid (), vep_idx);
2278 else if (PREDICT_FALSE (!session->is_vep_session))
2280 clib_warning ("VCL<%d>: ERROR: session (%u) "
2281 "is not a vep session!", getpid (), sid);
2284 vep = &session->vep;
2285 if (PREDICT_FALSE (vep->vep_sh != vep_idx))
2286 clib_warning ("VCL<%d>: ERROR: session (%u) vep_idx (%u) != "
2287 "vep_idx (%u)!", getpid (),
2288 sid, session->vep.vep_sh, vep_idx);
2289 if (session->is_vep_session)
2291 clib_warning ("vep_idx[%u]: sid 0x%x (%u)\n"
2293 " next_sid = 0x%x (%u)\n"
2294 " prev_sid = 0x%x (%u)\n"
2295 " vep_idx = 0x%x (%u)\n"
2296 " ev.events = 0x%x\n"
2297 " ev.data.u64 = 0x%llx\n"
2301 vep->next_sh, vep->next_sh,
2302 vep->prev_sh, vep->prev_sh,
2303 vep->vep_sh, vep->vep_sh,
2304 vep->ev.events, vep->ev.data.u64, vep->et_mask);
2309 clib_warning ("VCL<%d>: vep_idx (%u): Dump complete!\n",
2310 getpid (), vep_idx);
2314 vppcom_epoll_create (void)
2316 vcl_worker_t *wrk = vcl_worker_get_current ();
2317 vcl_session_t *vep_session;
2319 vep_session = vcl_session_alloc (wrk);
2321 vep_session->is_vep = 1;
2322 vep_session->vep.vep_sh = ~0;
2323 vep_session->vep.next_sh = ~0;
2324 vep_session->vep.prev_sh = ~0;
2325 vep_session->wait_cont_idx = ~0;
2326 vep_session->vpp_handle = ~0;
2328 vcl_evt (VCL_EVT_EPOLL_CREATE, vep_session, vep_session->session_index);
2329 VDBG (0, "Created vep_idx %u", vep_session->session_index);
2331 return vcl_session_handle (vep_session);
2335 vppcom_epoll_ctl (uint32_t vep_handle, int op, uint32_t session_handle,
2336 struct epoll_event *event)
2338 vcl_worker_t *wrk = vcl_worker_get_current ();
2339 vcl_session_t *vep_session;
2340 vcl_session_t *session;
2343 if (vep_handle == session_handle)
2345 VDBG (0, "vep_sh == session handle (%u)!", vep_handle);
2346 return VPPCOM_EINVAL;
2349 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
2350 if (PREDICT_FALSE (!vep_session))
2352 VDBG (0, "Invalid vep_sh (%u)!", vep_handle);
2353 return VPPCOM_EBADFD;
2355 if (PREDICT_FALSE (!vep_session->is_vep))
2357 VDBG (0, "vep_sh (%u) is not a vep!", vep_handle);
2358 return VPPCOM_EINVAL;
2361 ASSERT (vep_session->vep.vep_sh == ~0);
2362 ASSERT (vep_session->vep.prev_sh == ~0);
2364 session = vcl_session_get_w_handle (wrk, session_handle);
2365 if (PREDICT_FALSE (!session))
2367 VDBG (0, "Invalid session_handle (%u)!", session_handle);
2368 return VPPCOM_EBADFD;
2370 if (PREDICT_FALSE (session->is_vep))
2372 VDBG (0, "session_handle (%u) is a vep!", vep_handle);
2373 return VPPCOM_EINVAL;
2379 if (PREDICT_FALSE (!event))
2381 VDBG (0, "EPOLL_CTL_ADD: NULL pointer to epoll_event structure!");
2382 return VPPCOM_EINVAL;
2384 if (vep_session->vep.next_sh != ~0)
2386 vcl_session_t *next_session;
2387 next_session = vcl_session_get_w_handle (wrk,
2388 vep_session->vep.next_sh);
2389 if (PREDICT_FALSE (!next_session))
2391 VDBG (0, "EPOLL_CTL_ADD: Invalid vep.next_sid (%u) on "
2392 "vep_idx (%u)!", vep_session->vep.next_sh, vep_handle);
2393 return VPPCOM_EBADFD;
2395 ASSERT (next_session->vep.prev_sh == vep_handle);
2396 next_session->vep.prev_sh = session_handle;
2398 session->vep.next_sh = vep_session->vep.next_sh;
2399 session->vep.prev_sh = vep_handle;
2400 session->vep.vep_sh = vep_handle;
2401 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
2402 session->vep.ev = *event;
2403 session->is_vep = 0;
2404 session->is_vep_session = 1;
2405 vep_session->vep.next_sh = session_handle;
2407 if (session->tx_fifo)
2408 svm_fifo_add_want_tx_ntf (session->tx_fifo,
2409 SVM_FIFO_WANT_TX_NOTIF_IF_FULL);
2411 VDBG (1, "EPOLL_CTL_ADD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2412 vep_handle, session_handle, event->events, event->data.u64);
2413 vcl_evt (VCL_EVT_EPOLL_CTLADD, session, event->events, event->data.u64);
2417 if (PREDICT_FALSE (!event))
2419 VDBG (0, "EPOLL_CTL_MOD: NULL pointer to epoll_event structure!");
2423 else if (PREDICT_FALSE (!session->is_vep_session))
2425 VDBG (0, "sid %u EPOLL_CTL_MOD: not a vep session!",
2430 else if (PREDICT_FALSE (session->vep.vep_sh != vep_handle))
2432 VDBG (0, "EPOLL_CTL_MOD: sh %u vep_sh (%u) != vep_sh (%u)!",
2433 session_handle, session->vep.vep_sh, vep_handle);
2437 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
2438 session->vep.ev = *event;
2439 VDBG (1, "EPOLL_CTL_MOD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2440 vep_handle, session_handle, event->events, event->data.u64);
2444 if (PREDICT_FALSE (!session->is_vep_session))
2446 VDBG (0, "EPOLL_CTL_DEL: %u not a vep session!", session_handle);
2450 else if (PREDICT_FALSE (session->vep.vep_sh != vep_handle))
2452 VDBG (0, "EPOLL_CTL_DEL: sh %u vep_sh (%u) != vep_sh (%u)!",
2453 session_handle, session->vep.vep_sh, vep_handle);
2458 vep_session->wait_cont_idx =
2459 (vep_session->wait_cont_idx == session_handle) ?
2460 session->vep.next_sh : vep_session->wait_cont_idx;
2462 if (session->vep.prev_sh == vep_handle)
2463 vep_session->vep.next_sh = session->vep.next_sh;
2466 vcl_session_t *prev_session;
2467 prev_session = vcl_session_get_w_handle (wrk, session->vep.prev_sh);
2468 if (PREDICT_FALSE (!prev_session))
2470 VDBG (0, "EPOLL_CTL_DEL: Invalid prev_sid (%u) on sid (%u)!",
2471 session->vep.prev_sh, session_handle);
2472 return VPPCOM_EBADFD;
2474 ASSERT (prev_session->vep.next_sh == session_handle);
2475 prev_session->vep.next_sh = session->vep.next_sh;
2477 if (session->vep.next_sh != ~0)
2479 vcl_session_t *next_session;
2480 next_session = vcl_session_get_w_handle (wrk, session->vep.next_sh);
2481 if (PREDICT_FALSE (!next_session))
2483 VDBG (0, "EPOLL_CTL_DEL: Invalid next_sid (%u) on sid (%u)!",
2484 session->vep.next_sh, session_handle);
2485 return VPPCOM_EBADFD;
2487 ASSERT (next_session->vep.prev_sh == session_handle);
2488 next_session->vep.prev_sh = session->vep.prev_sh;
2491 memset (&session->vep, 0, sizeof (session->vep));
2492 session->vep.next_sh = ~0;
2493 session->vep.prev_sh = ~0;
2494 session->vep.vep_sh = ~0;
2495 session->is_vep_session = 0;
2497 if (session->tx_fifo)
2498 svm_fifo_del_want_tx_ntf (session->tx_fifo, SVM_FIFO_NO_TX_NOTIF);
2500 VDBG (1, "EPOLL_CTL_DEL: vep_idx %u, sid %u!", vep_handle,
2502 vcl_evt (VCL_EVT_EPOLL_CTLDEL, session, vep_sh);
2506 VDBG (0, "Invalid operation (%d)!", op);
2510 vep_verify_epoll_chain (wrk, vep_handle);
2517 vcl_epoll_wait_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
2518 struct epoll_event *events, u32 * num_ev)
2520 session_disconnected_msg_t *disconnected_msg;
2521 session_connected_msg_t *connected_msg;
2522 u32 sid = ~0, session_events;
2523 u64 session_evt_data = ~0;
2524 vcl_session_t *session;
2527 switch (e->event_type)
2529 case FIFO_EVENT_APP_RX:
2530 ASSERT (e->fifo->client_thread_index == vcl_get_worker_index ());
2531 vcl_fifo_rx_evt_valid_or_break (e->fifo);
2532 sid = e->fifo->client_session_index;
2533 if (!(session = vcl_session_get (wrk, sid)))
2535 session_events = session->vep.ev.events;
2536 if (!(EPOLLIN & session->vep.ev.events) || session->has_rx_evt)
2539 events[*num_ev].events |= EPOLLIN;
2540 session_evt_data = session->vep.ev.data.u64;
2541 session->has_rx_evt = 1;
2543 case FIFO_EVENT_APP_TX:
2544 sid = e->fifo->client_session_index;
2545 if (!(session = vcl_session_get (wrk, sid)))
2547 session_events = session->vep.ev.events;
2548 if (!(EPOLLOUT & session_events))
2551 events[*num_ev].events |= EPOLLOUT;
2552 session_evt_data = session->vep.ev.data.u64;
2553 svm_fifo_reset_tx_ntf (session->tx_fifo);
2555 case SESSION_IO_EVT_CT_TX:
2556 vcl_fifo_rx_evt_valid_or_break (e->fifo);
2557 session = vcl_ct_session_get_from_fifo (wrk, e->fifo, 0);
2558 if (PREDICT_FALSE (!session))
2560 sid = session->session_index;
2561 session_events = session->vep.ev.events;
2562 if (!(EPOLLIN & session->vep.ev.events) || session->has_rx_evt)
2565 events[*num_ev].events |= EPOLLIN;
2566 session_evt_data = session->vep.ev.data.u64;
2567 session->has_rx_evt = 1;
2569 case SESSION_IO_EVT_CT_RX:
2570 session = vcl_ct_session_get_from_fifo (wrk, e->fifo, 1);
2571 if (PREDICT_FALSE (!session))
2573 sid = session->session_index;
2574 session_events = session->vep.ev.events;
2575 if (!(EPOLLOUT & session_events))
2578 events[*num_ev].events |= EPOLLOUT;
2579 session_evt_data = session->vep.ev.data.u64;
2580 svm_fifo_reset_tx_ntf (session->tx_fifo);
2582 case SESSION_CTRL_EVT_ACCEPTED:
2583 session = vcl_session_accepted (wrk,
2584 (session_accepted_msg_t *) e->data);
2588 session_events = session->vep.ev.events;
2589 if (!(EPOLLIN & session_events))
2593 events[*num_ev].events |= EPOLLIN;
2594 session_evt_data = session->vep.ev.data.u64;
2596 case SESSION_CTRL_EVT_CONNECTED:
2597 connected_msg = (session_connected_msg_t *) e->data;
2598 vcl_session_connected_handler (wrk, connected_msg);
2599 /* Generate EPOLLOUT because there's no connected event */
2600 sid = vcl_session_index_from_vpp_handle (wrk, connected_msg->handle);
2601 if (!(session = vcl_session_get (wrk, sid)))
2603 session_events = session->vep.ev.events;
2604 if (EPOLLOUT & session_events)
2607 events[*num_ev].events |= EPOLLOUT;
2608 session_evt_data = session->vep.ev.data.u64;
2611 case SESSION_CTRL_EVT_DISCONNECTED:
2612 disconnected_msg = (session_disconnected_msg_t *) e->data;
2613 session = vcl_session_disconnected_handler (wrk, disconnected_msg);
2617 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
2618 session_evt_data = session->vep.ev.data.u64;
2619 session_events = session->vep.ev.events;
2621 case SESSION_CTRL_EVT_RESET:
2622 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
2623 if (!(session = vcl_session_get (wrk, sid)))
2626 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
2627 session_evt_data = session->vep.ev.data.u64;
2628 session_events = session->vep.ev.events;
2630 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
2631 vcl_session_req_worker_update_handler (wrk, e->data);
2633 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
2634 vcl_session_worker_update_reply_handler (wrk, e->data);
2637 VDBG (0, "unhandled: %u", e->event_type);
2643 events[*num_ev].data.u64 = session_evt_data;
2644 if (EPOLLONESHOT & session_events)
2646 session = vcl_session_get (wrk, sid);
2647 session->vep.ev.events = 0;
2654 vcl_epoll_wait_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
2655 struct epoll_event *events, u32 maxevents,
2656 double wait_for_time, u32 * num_ev)
2658 svm_msg_q_msg_t *msg;
2662 if (vec_len (wrk->mq_msg_vector) && svm_msg_q_is_empty (mq))
2663 goto handle_dequeued;
2665 svm_msg_q_lock (mq);
2666 if (svm_msg_q_is_empty (mq))
2670 svm_msg_q_unlock (mq);
2673 else if (wait_for_time < 0)
2675 svm_msg_q_wait (mq);
2679 if (svm_msg_q_timedwait (mq, wait_for_time / 1e3))
2681 svm_msg_q_unlock (mq);
2686 vcl_mq_dequeue_batch (wrk, mq);
2687 svm_msg_q_unlock (mq);
2690 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
2692 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
2693 e = svm_msg_q_msg_data (mq, msg);
2694 if (*num_ev < maxevents)
2695 vcl_epoll_wait_handle_mq_event (wrk, e, events, num_ev);
2697 vec_add1 (wrk->unhandled_evts_vector, *e);
2698 svm_msg_q_free_msg (mq, msg);
2700 vec_reset_length (wrk->mq_msg_vector);
2701 vcl_handle_pending_wrk_updates (wrk);
2706 vppcom_epoll_wait_condvar (vcl_worker_t * wrk, struct epoll_event *events,
2707 int maxevents, u32 n_evts, double wait_for_time)
2709 vcl_cut_through_registration_t *cr;
2710 double total_wait = 0, wait_slice;
2713 wait_for_time = (wait_for_time == -1) ? (double) 10e9 : wait_for_time;
2714 wait_slice = wrk->cut_through_registrations ? 10e-6 : wait_for_time;
2718 vcl_ct_registration_lock (wrk);
2720 pool_foreach (cr, wrk->cut_through_registrations, ({
2721 vcl_epoll_wait_handle_mq (wrk, cr->mq, events, maxevents, 0, &n_evts);
2724 vcl_ct_registration_unlock (wrk);
2726 rv = vcl_epoll_wait_handle_mq (wrk, wrk->app_event_queue, events,
2727 maxevents, n_evts ? 0 : wait_slice,
2730 total_wait += wait_slice;
2734 while (total_wait < wait_for_time);
2739 vppcom_epoll_wait_eventfd (vcl_worker_t * wrk, struct epoll_event *events,
2740 int maxevents, u32 n_evts, double wait_for_time)
2742 vcl_mq_evt_conn_t *mqc;
2743 int __clib_unused n_read;
2747 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
2749 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
2750 vec_len (wrk->mq_events), wait_for_time);
2751 for (i = 0; i < n_mq_evts; i++)
2753 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
2754 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
2755 vcl_epoll_wait_handle_mq (wrk, mqc->mq, events, maxevents, 0, &n_evts);
2757 if (!n_evts && n_mq_evts > 0)
2760 return (int) n_evts;
2764 vppcom_epoll_wait (uint32_t vep_handle, struct epoll_event *events,
2765 int maxevents, double wait_for_time)
2767 vcl_worker_t *wrk = vcl_worker_get_current ();
2768 vcl_session_t *vep_session;
2772 if (PREDICT_FALSE (maxevents <= 0))
2774 clib_warning ("VCL<%d>: ERROR: Invalid maxevents (%d)!",
2775 getpid (), maxevents);
2776 return VPPCOM_EINVAL;
2779 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
2781 return VPPCOM_EBADFD;
2783 if (PREDICT_FALSE (!vep_session->is_vep))
2785 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
2786 getpid (), vep_handle);
2787 return VPPCOM_EINVAL;
2790 memset (events, 0, sizeof (*events) * maxevents);
2792 if (vec_len (wrk->unhandled_evts_vector))
2794 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
2796 vcl_epoll_wait_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i],
2798 if (n_evts == maxevents)
2805 vec_delete (wrk->unhandled_evts_vector, i, 0);
2808 if (vcm->cfg.use_mq_eventfd)
2809 return vppcom_epoll_wait_eventfd (wrk, events, maxevents, n_evts,
2812 return vppcom_epoll_wait_condvar (wrk, events, maxevents, n_evts,
2817 vppcom_session_attr (uint32_t session_handle, uint32_t op,
2818 void *buffer, uint32_t * buflen)
2820 vcl_worker_t *wrk = vcl_worker_get_current ();
2821 vcl_session_t *session;
2823 u32 *flags = buffer, tmp_flags = 0;
2824 vppcom_endpt_t *ep = buffer;
2826 session = vcl_session_get_w_handle (wrk, session_handle);
2828 return VPPCOM_EBADFD;
2832 case VPPCOM_ATTR_GET_NREAD:
2833 rv = vppcom_session_read_ready (session);
2834 VDBG (2, "VPPCOM_ATTR_GET_NREAD: sid %u, nread = %d", rv);
2837 case VPPCOM_ATTR_GET_NWRITE:
2838 rv = vppcom_session_write_ready (session);
2839 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_NWRITE: sid %u, nwrite = %d",
2840 getpid (), session_handle, rv);
2843 case VPPCOM_ATTR_GET_FLAGS:
2844 if (PREDICT_TRUE (buffer && buflen && (*buflen >= sizeof (*flags))))
2846 *flags = O_RDWR | (VCL_SESS_ATTR_TEST (session->attr,
2847 VCL_SESS_ATTR_NONBLOCK));
2848 *buflen = sizeof (*flags);
2849 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_FLAGS: sid %u, flags = 0x%08x, "
2850 "is_nonblocking = %u", getpid (),
2851 session_handle, *flags,
2852 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK));
2858 case VPPCOM_ATTR_SET_FLAGS:
2859 if (PREDICT_TRUE (buffer && buflen && (*buflen == sizeof (*flags))))
2861 if (*flags & O_NONBLOCK)
2862 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK);
2864 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_NONBLOCK);
2866 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_FLAGS: sid %u, flags = 0x%08x,"
2867 " is_nonblocking = %u",
2868 getpid (), session_handle, *flags,
2869 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK));
2875 case VPPCOM_ATTR_GET_PEER_ADDR:
2876 if (PREDICT_TRUE (buffer && buflen &&
2877 (*buflen >= sizeof (*ep)) && ep->ip))
2879 ep->is_ip4 = session->transport.is_ip4;
2880 ep->port = session->transport.rmt_port;
2881 if (session->transport.is_ip4)
2882 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
2883 sizeof (ip4_address_t));
2885 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
2886 sizeof (ip6_address_t));
2887 *buflen = sizeof (*ep);
2888 VDBG (1, "VCL<%d>: VPPCOM_ATTR_GET_PEER_ADDR: sid %u, is_ip4 = %u, "
2889 "addr = %U, port %u", getpid (),
2890 session_handle, ep->is_ip4, format_ip46_address,
2891 &session->transport.rmt_ip,
2892 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
2893 clib_net_to_host_u16 (ep->port));
2899 case VPPCOM_ATTR_GET_LCL_ADDR:
2900 if (PREDICT_TRUE (buffer && buflen &&
2901 (*buflen >= sizeof (*ep)) && ep->ip))
2903 ep->is_ip4 = session->transport.is_ip4;
2904 ep->port = session->transport.lcl_port;
2905 if (session->transport.is_ip4)
2906 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip4,
2907 sizeof (ip4_address_t));
2909 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip6,
2910 sizeof (ip6_address_t));
2911 *buflen = sizeof (*ep);
2912 VDBG (1, "VCL<%d>: VPPCOM_ATTR_GET_LCL_ADDR: sid %u, is_ip4 = %u,"
2913 " addr = %U port %d", getpid (),
2914 session_handle, ep->is_ip4, format_ip46_address,
2915 &session->transport.lcl_ip,
2916 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
2917 clib_net_to_host_u16 (ep->port));
2923 case VPPCOM_ATTR_GET_LIBC_EPFD:
2924 rv = session->libc_epfd;
2925 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd %d",
2929 case VPPCOM_ATTR_SET_LIBC_EPFD:
2930 if (PREDICT_TRUE (buffer && buflen &&
2931 (*buflen == sizeof (session->libc_epfd))))
2933 session->libc_epfd = *(int *) buffer;
2934 *buflen = sizeof (session->libc_epfd);
2936 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd %d, "
2937 "buflen %d", getpid (), session->libc_epfd, *buflen);
2943 case VPPCOM_ATTR_GET_PROTOCOL:
2944 if (buffer && buflen && (*buflen >= sizeof (int)))
2946 *(int *) buffer = session->session_type;
2947 *buflen = sizeof (int);
2949 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_PROTOCOL: %d (%s), buflen %d",
2950 getpid (), *(int *) buffer, *(int *) buffer ? "UDP" : "TCP",
2957 case VPPCOM_ATTR_GET_LISTEN:
2958 if (buffer && buflen && (*buflen >= sizeof (int)))
2960 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2961 VCL_SESS_ATTR_LISTEN);
2962 *buflen = sizeof (int);
2964 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_LISTEN: %d, buflen %d",
2965 getpid (), *(int *) buffer, *buflen);
2971 case VPPCOM_ATTR_GET_ERROR:
2972 if (buffer && buflen && (*buflen >= sizeof (int)))
2974 *(int *) buffer = 0;
2975 *buflen = sizeof (int);
2977 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_ERROR: %d, buflen %d, #VPP-TBD#",
2978 getpid (), *(int *) buffer, *buflen);
2984 case VPPCOM_ATTR_GET_TX_FIFO_LEN:
2985 if (buffer && buflen && (*buflen >= sizeof (u32)))
2989 *(size_t *) buffer = (session->sndbuf_size ? session->sndbuf_size :
2990 session->tx_fifo ? session->tx_fifo->nitems :
2991 vcm->cfg.tx_fifo_size);
2992 *buflen = sizeof (u32);
2994 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TX_FIFO_LEN: %u (0x%x), "
2995 "buflen %d, #VPP-TBD#", getpid (),
2996 *(size_t *) buffer, *(size_t *) buffer, *buflen);
3002 case VPPCOM_ATTR_SET_TX_FIFO_LEN:
3003 if (buffer && buflen && (*buflen == sizeof (u32)))
3006 session->sndbuf_size = *(u32 *) buffer;
3007 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TX_FIFO_LEN: %u (0x%x), "
3008 "buflen %d, #VPP-TBD#", getpid (),
3009 session->sndbuf_size, session->sndbuf_size, *buflen);
3015 case VPPCOM_ATTR_GET_RX_FIFO_LEN:
3016 if (buffer && buflen && (*buflen >= sizeof (u32)))
3020 *(size_t *) buffer = (session->rcvbuf_size ? session->rcvbuf_size :
3021 session->rx_fifo ? session->rx_fifo->nitems :
3022 vcm->cfg.rx_fifo_size);
3023 *buflen = sizeof (u32);
3025 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_RX_FIFO_LEN: %u (0x%x), "
3026 "buflen %d, #VPP-TBD#", getpid (),
3027 *(size_t *) buffer, *(size_t *) buffer, *buflen);
3033 case VPPCOM_ATTR_SET_RX_FIFO_LEN:
3034 if (buffer && buflen && (*buflen == sizeof (u32)))
3037 session->rcvbuf_size = *(u32 *) buffer;
3038 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_RX_FIFO_LEN: %u (0x%x), "
3039 "buflen %d, #VPP-TBD#", getpid (),
3040 session->sndbuf_size, session->sndbuf_size, *buflen);
3046 case VPPCOM_ATTR_GET_REUSEADDR:
3047 if (buffer && buflen && (*buflen >= sizeof (int)))
3050 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3051 VCL_SESS_ATTR_REUSEADDR);
3052 *buflen = sizeof (int);
3054 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_REUSEADDR: %d, "
3055 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
3061 case VPPCOM_ATTR_SET_REUSEADDR:
3062 if (buffer && buflen && (*buflen == sizeof (int)) &&
3063 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
3066 if (*(int *) buffer)
3067 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEADDR);
3069 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEADDR);
3071 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_REUSEADDR: %d, buflen %d,"
3072 " #VPP-TBD#", getpid (),
3073 VCL_SESS_ATTR_TEST (session->attr,
3074 VCL_SESS_ATTR_REUSEADDR), *buflen);
3080 case VPPCOM_ATTR_GET_REUSEPORT:
3081 if (buffer && buflen && (*buflen >= sizeof (int)))
3084 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3085 VCL_SESS_ATTR_REUSEPORT);
3086 *buflen = sizeof (int);
3088 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_REUSEPORT: %d, buflen %d,"
3089 " #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
3095 case VPPCOM_ATTR_SET_REUSEPORT:
3096 if (buffer && buflen && (*buflen == sizeof (int)) &&
3097 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
3100 if (*(int *) buffer)
3101 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEPORT);
3103 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEPORT);
3105 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_REUSEPORT: %d, buflen %d,"
3106 " #VPP-TBD#", getpid (),
3107 VCL_SESS_ATTR_TEST (session->attr,
3108 VCL_SESS_ATTR_REUSEPORT), *buflen);
3114 case VPPCOM_ATTR_GET_BROADCAST:
3115 if (buffer && buflen && (*buflen >= sizeof (int)))
3118 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3119 VCL_SESS_ATTR_BROADCAST);
3120 *buflen = sizeof (int);
3122 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_BROADCAST: %d, buflen %d,"
3123 " #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
3129 case VPPCOM_ATTR_SET_BROADCAST:
3130 if (buffer && buflen && (*buflen == sizeof (int)))
3133 if (*(int *) buffer)
3134 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_BROADCAST);
3136 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_BROADCAST);
3138 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_BROADCAST: %d, buflen %d, "
3139 "#VPP-TBD#", getpid (),
3140 VCL_SESS_ATTR_TEST (session->attr,
3141 VCL_SESS_ATTR_BROADCAST), *buflen);
3147 case VPPCOM_ATTR_GET_V6ONLY:
3148 if (buffer && buflen && (*buflen >= sizeof (int)))
3151 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3152 VCL_SESS_ATTR_V6ONLY);
3153 *buflen = sizeof (int);
3155 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_V6ONLY: %d, buflen %d, "
3156 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
3162 case VPPCOM_ATTR_SET_V6ONLY:
3163 if (buffer && buflen && (*buflen == sizeof (int)))
3166 if (*(int *) buffer)
3167 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_V6ONLY);
3169 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_V6ONLY);
3171 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_V6ONLY: %d, buflen %d, "
3172 "#VPP-TBD#", getpid (),
3173 VCL_SESS_ATTR_TEST (session->attr,
3174 VCL_SESS_ATTR_V6ONLY), *buflen);
3180 case VPPCOM_ATTR_GET_KEEPALIVE:
3181 if (buffer && buflen && (*buflen >= sizeof (int)))
3184 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3185 VCL_SESS_ATTR_KEEPALIVE);
3186 *buflen = sizeof (int);
3188 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_KEEPALIVE: %d, buflen %d, "
3189 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
3195 case VPPCOM_ATTR_SET_KEEPALIVE:
3196 if (buffer && buflen && (*buflen == sizeof (int)))
3199 if (*(int *) buffer)
3200 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_KEEPALIVE);
3202 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_KEEPALIVE);
3204 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_KEEPALIVE: %d, buflen %d, "
3205 "#VPP-TBD#", getpid (),
3206 VCL_SESS_ATTR_TEST (session->attr,
3207 VCL_SESS_ATTR_KEEPALIVE), *buflen);
3213 case VPPCOM_ATTR_GET_TCP_NODELAY:
3214 if (buffer && buflen && (*buflen >= sizeof (int)))
3217 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3218 VCL_SESS_ATTR_TCP_NODELAY);
3219 *buflen = sizeof (int);
3221 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_NODELAY: %d, buflen %d, "
3222 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
3228 case VPPCOM_ATTR_SET_TCP_NODELAY:
3229 if (buffer && buflen && (*buflen == sizeof (int)))
3232 if (*(int *) buffer)
3233 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
3235 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
3237 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_NODELAY: %d, buflen %d, "
3238 "#VPP-TBD#", getpid (),
3239 VCL_SESS_ATTR_TEST (session->attr,
3240 VCL_SESS_ATTR_TCP_NODELAY), *buflen);
3246 case VPPCOM_ATTR_GET_TCP_KEEPIDLE:
3247 if (buffer && buflen && (*buflen >= sizeof (int)))
3250 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3251 VCL_SESS_ATTR_TCP_KEEPIDLE);
3252 *buflen = sizeof (int);
3254 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d, buflen %d, "
3255 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
3261 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
3262 if (buffer && buflen && (*buflen == sizeof (int)))
3265 if (*(int *) buffer)
3266 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
3268 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
3270 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d, buflen %d, "
3271 "#VPP-TBD#", getpid (),
3272 VCL_SESS_ATTR_TEST (session->attr,
3273 VCL_SESS_ATTR_TCP_KEEPIDLE), *buflen);
3279 case VPPCOM_ATTR_GET_TCP_KEEPINTVL:
3280 if (buffer && buflen && (*buflen >= sizeof (int)))
3283 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3284 VCL_SESS_ATTR_TCP_KEEPINTVL);
3285 *buflen = sizeof (int);
3287 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_KEEPINTVL: %d, buflen %d, "
3288 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
3294 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
3295 if (buffer && buflen && (*buflen == sizeof (int)))
3298 if (*(int *) buffer)
3299 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
3301 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
3303 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d, buflen %d, "
3304 "#VPP-TBD#", getpid (),
3305 VCL_SESS_ATTR_TEST (session->attr,
3306 VCL_SESS_ATTR_TCP_KEEPINTVL), *buflen);
3312 case VPPCOM_ATTR_GET_TCP_USER_MSS:
3313 if (buffer && buflen && (*buflen >= sizeof (u32)))
3316 *(u32 *) buffer = session->user_mss;
3317 *buflen = sizeof (int);
3319 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_USER_MSS: %d, buflen %d,"
3320 " #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
3326 case VPPCOM_ATTR_SET_TCP_USER_MSS:
3327 if (buffer && buflen && (*buflen == sizeof (u32)))
3330 session->user_mss = *(u32 *) buffer;
3332 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_USER_MSS: %u, buflen %d, "
3333 "#VPP-TBD#", getpid (), session->user_mss, *buflen);
3339 case VPPCOM_ATTR_SET_SHUT:
3340 if (*flags == SHUT_RD || *flags == SHUT_RDWR)
3341 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_SHUT_RD);
3342 if (*flags == SHUT_WR || *flags == SHUT_RDWR)
3343 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_SHUT_WR);
3346 case VPPCOM_ATTR_GET_SHUT:
3347 if (VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_SHUT_RD))
3349 if (VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_SHUT_WR))
3352 *(int *) buffer = SHUT_RD;
3353 else if (tmp_flags == 2)
3354 *(int *) buffer = SHUT_WR;
3355 else if (tmp_flags == 3)
3356 *(int *) buffer = SHUT_RDWR;
3357 *buflen = sizeof (int);
3368 vppcom_session_recvfrom (uint32_t session_handle, void *buffer,
3369 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3371 vcl_worker_t *wrk = vcl_worker_get_current ();
3373 vcl_session_t *session = 0;
3377 session = vcl_session_get_w_handle (wrk, session_handle);
3378 if (PREDICT_FALSE (!session))
3380 VDBG (0, "VCL<%d>: invalid session, sid (%u) has been closed!",
3381 getpid (), session_handle);
3382 return VPPCOM_EBADFD;
3384 ep->is_ip4 = session->transport.is_ip4;
3385 ep->port = session->transport.rmt_port;
3389 rv = vppcom_session_read (session_handle, buffer, buflen);
3390 else if (flags & MSG_PEEK)
3391 rv = vppcom_session_peek (session_handle, buffer, buflen);
3394 VDBG (0, "Unsupport flags for recvfrom %d", flags);
3395 return VPPCOM_EAFNOSUPPORT;
3400 if (session->transport.is_ip4)
3401 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
3402 sizeof (ip4_address_t));
3404 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
3405 sizeof (ip6_address_t));
3412 vppcom_session_sendto (uint32_t session_handle, void *buffer,
3413 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3416 return VPPCOM_EINVAL;
3421 return VPPCOM_EINVAL;
3426 // TBD check the flags and do the right thing
3427 VDBG (2, "VCL<%d>: handling flags 0x%u (%d) not implemented yet.",
3428 getpid (), flags, flags);
3431 return (vppcom_session_write_inline (session_handle, buffer, buflen, 1));
3435 vppcom_poll (vcl_poll_t * vp, uint32_t n_sids, double wait_for_time)
3437 vcl_worker_t *wrk = vcl_worker_get_current ();
3438 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
3439 u32 i, keep_trying = 1;
3440 svm_msg_q_msg_t msg;
3444 VDBG (3, "VCL<%d>: vp %p, nsids %u, wait_for_time %f",
3445 getpid (), vp, n_sids, wait_for_time);
3448 return VPPCOM_EFAULT;
3452 vcl_session_t *session;
3454 /* Dequeue all events and drop all unhandled io events */
3455 while (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0) == 0)
3457 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
3458 vcl_handle_mq_event (wrk, e);
3459 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
3461 vec_reset_length (wrk->unhandled_evts_vector);
3463 for (i = 0; i < n_sids; i++)
3465 session = vcl_session_get (wrk, vp[i].sh);
3468 vp[i].revents = POLLHUP;
3475 if (POLLIN & vp[i].events)
3477 rv = vppcom_session_read_ready (session);
3480 vp[i].revents |= POLLIN;
3487 case VPPCOM_ECONNRESET:
3488 vp[i].revents = POLLHUP;
3492 vp[i].revents = POLLERR;
3499 if (POLLOUT & vp[i].events)
3501 rv = vppcom_session_write_ready (session);
3504 vp[i].revents |= POLLOUT;
3511 case VPPCOM_ECONNRESET:
3512 vp[i].revents = POLLHUP;
3516 vp[i].revents = POLLERR;
3523 if (0) // Note "done:" label used by VCL_SESSION_LOCK_AND_GET()
3525 vp[i].revents = POLLNVAL;
3529 if (wait_for_time != -1)
3530 keep_trying = (clib_time_now (&wrk->clib_time) <= timeout) ? 1 : 0;
3532 while ((num_ev == 0) && keep_trying);
3534 if (VPPCOM_DEBUG > 3)
3536 clib_warning ("VCL<%d>: returning %d", getpid (), num_ev);
3537 for (i = 0; i < n_sids; i++)
3539 clib_warning ("VCL<%d>: vp[%d].sid %d (0x%x), .events 0x%x, "
3540 ".revents 0x%x", getpid (), i, vp[i].sh, vp[i].sh,
3541 vp[i].events, vp[i].revents);
3548 vppcom_mq_epoll_fd (void)
3550 vcl_worker_t *wrk = vcl_worker_get_current ();
3551 return wrk->mqs_epfd;
3555 vppcom_session_index (vcl_session_handle_t session_handle)
3557 return session_handle & 0xFFFFFF;
3561 vppcom_session_worker (vcl_session_handle_t session_handle)
3563 return session_handle >> 24;
3567 vppcom_worker_register (void)
3569 if (!vcl_worker_alloc_and_init ())
3570 return VPPCOM_EEXIST;
3572 if (vcl_worker_set_bapi ())
3573 return VPPCOM_EEXIST;
3575 if (vcl_worker_register_with_vpp ())
3576 return VPPCOM_EEXIST;
3582 vppcom_worker_index (void)
3584 return vcl_get_worker_index ();
3588 * fd.io coding-style-patch-verification: ON
3591 * eval: (c-set-style "gnu")