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 session->vpp_evt_q = uword_to_pointer (mp->vpp_evt_q, svm_msg_q_t *);
509 vec_validate (wrk->vpp_event_queues, 0);
510 wrk->vpp_event_queues[0] = session->vpp_evt_q;
512 if (session->is_dgram)
514 svm_fifo_t *rx_fifo, *tx_fifo;
515 session->vpp_evt_q = uword_to_pointer (mp->vpp_evt_q, svm_msg_q_t *);
516 rx_fifo = uword_to_pointer (mp->rx_fifo, svm_fifo_t *);
517 rx_fifo->client_session_index = sid;
518 tx_fifo = uword_to_pointer (mp->tx_fifo, svm_fifo_t *);
519 tx_fifo->client_session_index = sid;
520 session->rx_fifo = rx_fifo;
521 session->tx_fifo = tx_fifo;
524 VDBG (0, "session %u [0x%llx]: listen succeeded!", sid, mp->handle);
528 static vcl_session_t *
529 vcl_session_accepted (vcl_worker_t * wrk, session_accepted_msg_t * msg)
531 vcl_session_msg_t *vcl_msg;
532 vcl_session_t *session;
534 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
535 if (PREDICT_FALSE (session != 0))
536 VWRN ("session overlap handle %lu state %u!", msg->handle,
537 session->session_state);
539 session = vcl_session_table_lookup_listener (wrk, msg->listener_handle);
542 VERR ("couldn't find listen session: listener handle %llx",
543 msg->listener_handle);
547 clib_fifo_add2 (session->accept_evts_fifo, vcl_msg);
548 vcl_msg->accepted_msg = *msg;
549 /* Session handle points to listener until fully accepted by app */
550 vcl_session_table_add_vpp_handle (wrk, msg->handle, session->session_index);
555 static vcl_session_t *
556 vcl_session_disconnected_handler (vcl_worker_t * wrk,
557 session_disconnected_msg_t * msg)
559 vcl_session_t *session;
561 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
564 VDBG (0, "request to disconnect unknown handle 0x%llx", msg->handle);
568 /* Caught a disconnect before actually accepting the session */
569 if (session->session_state == STATE_LISTEN)
571 if (!vcl_flag_accepted_session (session, msg->handle,
572 VCL_ACCEPTED_F_CLOSED))
573 VDBG (0, "session was not accepted!");
577 session->session_state = STATE_VPP_CLOSING;
582 vcl_session_req_worker_update_handler (vcl_worker_t * wrk, void *data)
584 session_req_worker_update_msg_t *msg;
587 msg = (session_req_worker_update_msg_t *) data;
588 s = vcl_session_get_w_vpp_handle (wrk, msg->session_handle);
592 vec_add1 (wrk->pending_session_wrk_updates, s->session_index);
596 vcl_session_worker_update_reply_handler (vcl_worker_t * wrk, void *data)
598 session_worker_update_reply_msg_t *msg;
601 msg = (session_worker_update_reply_msg_t *) data;
602 s = vcl_session_get_w_vpp_handle (wrk, msg->handle);
605 VDBG (0, "unknown handle 0x%llx", msg->handle);
608 if (vcl_wait_for_segment (msg->segment_handle))
610 clib_warning ("segment for session %u couldn't be mounted!",
617 s->rx_fifo = uword_to_pointer (msg->rx_fifo, svm_fifo_t *);
618 s->tx_fifo = uword_to_pointer (msg->tx_fifo, svm_fifo_t *);
619 s->rx_fifo->client_session_index = s->session_index;
620 s->tx_fifo->client_session_index = s->session_index;
621 s->rx_fifo->client_thread_index = wrk->wrk_index;
622 s->tx_fifo->client_thread_index = wrk->wrk_index;
624 s->session_state = STATE_UPDATED;
626 VDBG (0, "session %u[0x%llx] moved to worker %u", s->session_index,
627 s->vpp_handle, wrk->wrk_index);
631 vcl_handle_mq_event (vcl_worker_t * wrk, session_event_t * e)
633 session_disconnected_msg_t *disconnected_msg;
634 vcl_session_t *session;
636 switch (e->event_type)
638 case FIFO_EVENT_APP_RX:
639 case FIFO_EVENT_APP_TX:
640 case SESSION_IO_EVT_CT_RX:
641 case SESSION_IO_EVT_CT_TX:
642 vec_add1 (wrk->unhandled_evts_vector, *e);
644 case SESSION_CTRL_EVT_ACCEPTED:
645 vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
647 case SESSION_CTRL_EVT_CONNECTED:
648 vcl_session_connected_handler (wrk,
649 (session_connected_msg_t *) e->data);
651 case SESSION_CTRL_EVT_DISCONNECTED:
652 disconnected_msg = (session_disconnected_msg_t *) e->data;
653 session = vcl_session_disconnected_handler (wrk, disconnected_msg);
656 session->session_state = STATE_DISCONNECT;
657 VDBG (0, "disconnected session %u [0x%llx]", session->session_index,
658 session->vpp_handle);
660 case SESSION_CTRL_EVT_RESET:
661 vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
663 case SESSION_CTRL_EVT_BOUND:
664 vcl_session_bound_handler (wrk, (session_bound_msg_t *) e->data);
666 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
667 vcl_session_req_worker_update_handler (wrk, e->data);
669 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
670 vcl_session_worker_update_reply_handler (wrk, e->data);
673 clib_warning ("unhandled %u", e->event_type);
679 vppcom_wait_for_session_state_change (u32 session_index,
680 session_state_t state,
683 vcl_worker_t *wrk = vcl_worker_get_current ();
684 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
685 vcl_session_t *volatile session;
691 session = vcl_session_get (wrk, session_index);
692 if (PREDICT_FALSE (!session))
694 return VPPCOM_EBADFD;
696 if (session->session_state & state)
700 if (session->session_state & STATE_FAILED)
702 return VPPCOM_ECONNREFUSED;
705 if (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0))
710 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
711 vcl_handle_mq_event (wrk, e);
712 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
714 while (clib_time_now (&wrk->clib_time) < timeout);
716 VDBG (0, "timeout waiting for state 0x%x (%s)", state,
717 vppcom_session_state_str (state));
718 vcl_evt (VCL_EVT_SESSION_TIMEOUT, session, session_state);
720 return VPPCOM_ETIMEDOUT;
724 vcl_handle_pending_wrk_updates (vcl_worker_t * wrk)
726 session_state_t state;
730 if (PREDICT_TRUE (vec_len (wrk->pending_session_wrk_updates) == 0))
733 vec_foreach (sip, wrk->pending_session_wrk_updates)
735 s = vcl_session_get (wrk, *sip);
736 vcl_send_session_worker_update (wrk, s, wrk->wrk_index);
737 state = s->session_state;
738 vppcom_wait_for_session_state_change (s->session_index, STATE_UPDATED, 5);
739 s->session_state = state;
741 vec_reset_length (wrk->pending_session_wrk_updates);
745 vcl_flush_mq_events (void)
747 vcl_worker_t *wrk = vcl_worker_get_current ();
748 svm_msg_q_msg_t *msg;
753 mq = wrk->app_event_queue;
755 vcl_mq_dequeue_batch (wrk, mq);
756 svm_msg_q_unlock (mq);
758 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
760 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
761 e = svm_msg_q_msg_data (mq, msg);
762 vcl_handle_mq_event (wrk, e);
763 svm_msg_q_free_msg (mq, msg);
765 vec_reset_length (wrk->mq_msg_vector);
766 vcl_handle_pending_wrk_updates (wrk);
770 vppcom_app_session_enable (void)
774 if (vcm->app_state != STATE_APP_ENABLED)
776 vppcom_send_session_enable_disable (1 /* is_enabled == TRUE */ );
777 rv = vcl_wait_for_app_state_change (STATE_APP_ENABLED);
778 if (PREDICT_FALSE (rv))
780 VDBG (0, "VCL<%d>: application session enable timed out! "
781 "returning %d (%s)", getpid (), rv, vppcom_retval_str (rv));
789 vppcom_app_attach (void)
793 vppcom_app_send_attach ();
794 rv = vcl_wait_for_app_state_change (STATE_APP_ATTACHED);
795 if (PREDICT_FALSE (rv))
797 VDBG (0, "VCL<%d>: application attach timed out! returning %d (%s)",
798 getpid (), rv, vppcom_retval_str (rv));
806 vppcom_session_unbind (u32 session_handle)
808 vcl_worker_t *wrk = vcl_worker_get_current ();
809 vcl_session_t *session = 0;
812 session = vcl_session_get_w_handle (wrk, session_handle);
814 return VPPCOM_EBADFD;
816 vpp_handle = session->vpp_handle;
817 vcl_session_table_del_listener (wrk, vpp_handle);
818 session->vpp_handle = ~0;
819 session->session_state = STATE_DISCONNECT;
821 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: sending unbind msg! new state"
822 " 0x%x (%s)", getpid (), vpp_handle, session_handle, STATE_DISCONNECT,
823 vppcom_session_state_str (STATE_DISCONNECT));
824 vcl_evt (VCL_EVT_UNBIND, session);
825 vppcom_send_unbind_sock (vpp_handle);
831 vppcom_session_disconnect (u32 session_handle)
833 vcl_worker_t *wrk = vcl_worker_get_current ();
834 svm_msg_q_t *vpp_evt_q;
835 vcl_session_t *session;
836 session_state_t state;
839 session = vcl_session_get_w_handle (wrk, session_handle);
841 return VPPCOM_EBADFD;
843 vpp_handle = session->vpp_handle;
844 state = session->session_state;
846 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u state 0x%x (%s)", getpid (),
847 vpp_handle, session_handle, state, vppcom_session_state_str (state));
849 if (PREDICT_FALSE (state & STATE_LISTEN))
851 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
852 "Cannot disconnect a listen socket!",
853 getpid (), vpp_handle, session_handle);
854 return VPPCOM_EBADFD;
857 if (state & STATE_VPP_CLOSING)
859 vpp_evt_q = vcl_session_vpp_evt_q (wrk, session);
860 vcl_send_session_disconnected_reply (vpp_evt_q, wrk->my_client_index,
862 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: sending disconnect "
863 "REPLY...", getpid (), vpp_handle, session_handle);
867 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: sending disconnect...",
868 getpid (), vpp_handle, session_handle);
869 vppcom_send_disconnect_session (vpp_handle);
878 * Notify vpp of the disconnect and mark the worker as free. If we're the
879 * last worker, do a full cleanup otherwise, since we're probably a forked
880 * child, avoid syscalls as much as possible. We might've lost privileges.
883 vppcom_app_exit (void)
885 if (!pool_elts (vcm->workers))
887 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
888 vcl_set_worker_index (~0);
890 if (vec_len (vcm->workers) == 1)
891 vl_client_disconnect_from_vlib ();
893 vl_client_send_disconnect (1 /* vpp should cleanup */ );
897 * VPPCOM Public API functions
900 vppcom_app_create (char *app_name)
902 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
907 VDBG (1, "already initialized");
908 return VPPCOM_EEXIST;
912 vppcom_cfg (&vcm->cfg);
915 vcm->main_cpu = pthread_self ();
916 vcm->main_pid = getpid ();
917 vcm->app_name = format (0, "%s", app_name);
918 vppcom_init_error_string_table ();
919 svm_fifo_segment_main_init (&vcm->segment_main, vcl_cfg->segment_baseva,
920 20 /* timeout in secs */ );
921 pool_alloc (vcm->workers, vcl_cfg->max_workers);
922 clib_spinlock_init (&vcm->workers_lock);
923 clib_rwlock_init (&vcm->segment_table_lock);
924 atexit (vppcom_app_exit);
926 /* Allocate default worker */
927 vcl_worker_alloc_and_init ();
929 /* API hookup and connect to VPP */
930 vppcom_api_hookup ();
932 vcm->app_state = STATE_APP_START;
933 rv = vppcom_connect_to_vpp (app_name);
936 VERR ("couldn't connect to VPP!");
939 VDBG (0, "sending session enable");
940 rv = vppcom_app_session_enable ();
943 VERR ("vppcom_app_session_enable() failed!");
947 VDBG (0, "sending app attach");
948 rv = vppcom_app_attach ();
951 VERR ("vppcom_app_attach() failed!");
955 VDBG (0, "app_name '%s', my_client_index %d (0x%x)", app_name,
956 vcm->workers[0].my_client_index, vcm->workers[0].my_client_index);
962 vppcom_app_destroy (void)
965 f64 orig_app_timeout;
967 if (!pool_elts (vcm->workers))
970 vcl_evt (VCL_EVT_DETACH, vcm);
972 if (pool_elts (vcm->workers) == 1)
974 vppcom_app_send_detach ();
975 orig_app_timeout = vcm->cfg.app_timeout;
976 vcm->cfg.app_timeout = 2.0;
977 rv = vcl_wait_for_app_state_change (STATE_APP_ENABLED);
978 vcm->cfg.app_timeout = orig_app_timeout;
979 if (PREDICT_FALSE (rv))
980 VDBG (0, "application detach timed out! returning %d (%s)", rv,
981 vppcom_retval_str (rv));
982 vec_free (vcm->app_name);
983 vcl_worker_cleanup (vcl_worker_get_current (), 0 /* notify vpp */ );
987 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
990 vcl_set_worker_index (~0);
992 vl_client_disconnect_from_vlib ();
996 vppcom_session_create (u8 proto, u8 is_nonblocking)
998 vcl_worker_t *wrk = vcl_worker_get_current ();
999 vcl_session_t *session;
1001 session = vcl_session_alloc (wrk);
1003 session->session_type = proto;
1004 session->session_state = STATE_START;
1005 session->vpp_handle = ~0;
1006 session->is_dgram = proto == VPPCOM_PROTO_UDP;
1009 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK);
1011 vcl_evt (VCL_EVT_CREATE, session, session_type, session->session_state,
1012 is_nonblocking, session_index);
1014 VDBG (0, "created sid %u", session->session_index);
1016 return vcl_session_handle (session);
1020 vcl_session_cleanup (vcl_worker_t * wrk, vcl_session_t * session,
1021 vcl_session_handle_t sh, u8 do_disconnect)
1023 session_state_t state;
1024 u32 next_sh, vep_sh;
1029 is_vep = session->is_vep;
1030 next_sh = session->vep.next_sh;
1031 vep_sh = session->vep.vep_sh;
1032 state = session->session_state;
1033 vpp_handle = session->vpp_handle;
1035 VDBG (1, "session %u [0x%llx] closing", session->session_index, vpp_handle);
1039 while (next_sh != ~0)
1041 rv = vppcom_epoll_ctl (sh, EPOLL_CTL_DEL, next_sh, 0);
1042 if (PREDICT_FALSE (rv < 0))
1043 VDBG (0, "vpp handle 0x%llx, sid %u: EPOLL_CTL_DEL vep_idx %u"
1044 " failed! rv %d (%s)", vpp_handle, next_sh, vep_sh, rv,
1045 vppcom_retval_str (rv));
1047 next_sh = session->vep.next_sh;
1052 if (session->is_vep_session)
1054 rv = vppcom_epoll_ctl (vep_sh, EPOLL_CTL_DEL, sh, 0);
1056 VDBG (0, "session %u [0x%llx]: EPOLL_CTL_DEL vep_idx %u "
1057 "failed! rv %d (%s)", session->session_index, vpp_handle,
1058 vep_sh, rv, vppcom_retval_str (rv));
1063 VDBG (1, "session %u [0x%llx] disconnect skipped",
1064 session->session_index, vpp_handle);
1068 if (state & STATE_LISTEN)
1070 rv = vppcom_session_unbind (sh);
1071 if (PREDICT_FALSE (rv < 0))
1072 VDBG (0, "session %u [0x%llx]: listener unbind failed! "
1073 "rv %d (%s)", session->session_index, vpp_handle, rv,
1074 vppcom_retval_str (rv));
1076 else if (state & STATE_OPEN)
1078 rv = vppcom_session_disconnect (sh);
1079 if (PREDICT_FALSE (rv < 0))
1080 VDBG (0, "ERROR: session %u [0x%llx]: disconnect failed!"
1081 " rv %d (%s)", session->session_index, vpp_handle,
1082 rv, vppcom_retval_str (rv));
1084 else if (state == STATE_DISCONNECT)
1086 svm_msg_q_t *mq = vcl_session_vpp_evt_q (wrk, session);
1087 vcl_send_session_reset_reply (mq, wrk->my_client_index,
1088 session->vpp_handle, 0);
1092 if (vcl_session_is_ct (session))
1094 vcl_cut_through_registration_t *ctr;
1097 mq_addr = pointer_to_uword (session->our_evt_q);
1098 ctr = vcl_ct_registration_lock_and_lookup (wrk, mq_addr);
1100 if (ctr->epoll_evt_conn_index != ~0)
1101 vcl_mq_epoll_del_evfd (wrk, ctr->epoll_evt_conn_index);
1102 VDBG (0, "Removing ct registration %u",
1103 vcl_ct_registration_index (wrk, ctr));
1104 vcl_ct_registration_del (wrk, ctr);
1105 vcl_ct_registration_lookup_del (wrk, mq_addr);
1106 vcl_ct_registration_unlock (wrk);
1109 VDBG (0, "session %u [0x%llx] removed", session->session_index, vpp_handle);
1112 vcl_session_table_del_vpp_handle (wrk, vpp_handle);
1113 vcl_session_free (wrk, session);
1114 vcl_evt (VCL_EVT_CLOSE, session, rv);
1120 vppcom_session_close (uint32_t session_handle)
1122 vcl_worker_t *wrk = vcl_worker_get_current ();
1123 vcl_session_t *session;
1125 session = vcl_session_get_w_handle (wrk, session_handle);
1127 return VPPCOM_EBADFD;
1128 return vcl_session_cleanup (wrk, session, session_handle,
1129 1 /* do_disconnect */ );
1133 vppcom_session_bind (uint32_t session_handle, vppcom_endpt_t * ep)
1135 vcl_worker_t *wrk = vcl_worker_get_current ();
1136 vcl_session_t *session = 0;
1139 return VPPCOM_EINVAL;
1141 session = vcl_session_get_w_handle (wrk, session_handle);
1143 return VPPCOM_EBADFD;
1145 if (session->is_vep)
1147 clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
1148 "bind to an epoll session!", getpid (), session_handle);
1149 return VPPCOM_EBADFD;
1152 session->transport.is_ip4 = ep->is_ip4;
1154 clib_memcpy_fast (&session->transport.lcl_ip.ip4, ep->ip,
1155 sizeof (ip4_address_t));
1157 clib_memcpy_fast (&session->transport.lcl_ip.ip6, ep->ip,
1158 sizeof (ip6_address_t));
1159 session->transport.lcl_port = ep->port;
1161 VDBG (0, "VCL<%d>: sid %u: binding to local %s address %U port %u, "
1162 "proto %s", getpid (), session_handle,
1163 session->transport.is_ip4 ? "IPv4" : "IPv6",
1164 format_ip46_address, &session->transport.lcl_ip,
1165 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
1166 clib_net_to_host_u16 (session->transport.lcl_port),
1167 vppcom_proto_str (session->session_type));
1168 vcl_evt (VCL_EVT_BIND, session);
1170 if (session->session_type == VPPCOM_PROTO_UDP)
1171 vppcom_session_listen (session_handle, 10);
1177 vppcom_session_listen (uint32_t listen_sh, uint32_t q_len)
1179 vcl_worker_t *wrk = vcl_worker_get_current ();
1180 vcl_session_t *listen_session = 0;
1181 u64 listen_vpp_handle;
1184 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
1185 if (!listen_session || listen_session->is_vep)
1186 return VPPCOM_EBADFD;
1188 if (q_len == 0 || q_len == ~0)
1189 q_len = vcm->cfg.listen_queue_size;
1191 listen_vpp_handle = listen_session->vpp_handle;
1192 if (listen_session->session_state & STATE_LISTEN)
1194 VDBG (0, "session %u [0x%llx]: already in listen state!",
1195 listen_sh, listen_vpp_handle);
1199 VDBG (0, "session %u [0x%llx]: sending vpp listen request...",
1200 listen_sh, listen_vpp_handle);
1203 * Send listen request to vpp and wait for reply
1205 vppcom_send_bind_sock (listen_session);
1206 rv = vppcom_wait_for_session_state_change (listen_session->session_index,
1208 vcm->cfg.session_timeout);
1210 if (PREDICT_FALSE (rv))
1212 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
1213 VDBG (0, "session %u [0x%llx]: listen failed! returning %d (%s)",
1214 listen_sh, listen_session->vpp_handle, rv,
1215 vppcom_retval_str (rv));
1223 vppcom_session_tls_add_cert (uint32_t session_handle, char *cert,
1227 vcl_worker_t *wrk = vcl_worker_get_current ();
1228 vcl_session_t *session = 0;
1230 session = vcl_session_get_w_handle (wrk, session_handle);
1232 return VPPCOM_EBADFD;
1234 if (cert_len == 0 || cert_len == ~0)
1235 return VPPCOM_EBADFD;
1238 * Send listen request to vpp and wait for reply
1240 vppcom_send_application_tls_cert_add (session, cert, cert_len);
1247 vppcom_session_tls_add_key (uint32_t session_handle, char *key,
1251 vcl_worker_t *wrk = vcl_worker_get_current ();
1252 vcl_session_t *session = 0;
1254 session = vcl_session_get_w_handle (wrk, session_handle);
1256 return VPPCOM_EBADFD;
1258 if (key_len == 0 || key_len == ~0)
1259 return VPPCOM_EBADFD;
1262 * Send listen request to vpp and wait for reply
1264 vppcom_send_application_tls_key_add (session, key, key_len);
1272 validate_args_session_accept_ (vcl_worker_t * wrk,
1273 vcl_session_t * listen_session)
1275 /* Input validation - expects spinlock on sessions_lockp */
1276 if (listen_session->is_vep)
1278 clib_warning ("VCL<%d>: ERROR: sid %u: cannot accept on an "
1279 "epoll session!", getpid (),
1280 listen_session->session_index);
1281 return VPPCOM_EBADFD;
1284 if (listen_session->session_state != STATE_LISTEN)
1286 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
1287 "not in listen state! state 0x%x (%s)", getpid (),
1288 listen_session->vpp_handle, listen_session->session_index,
1289 listen_session->session_state,
1290 vppcom_session_state_str (listen_session->session_state));
1291 return VPPCOM_EBADFD;
1297 vppcom_session_accept (uint32_t listen_session_handle, vppcom_endpt_t * ep,
1300 u32 client_session_index = ~0, listen_session_index, accept_flags = 0;
1301 vcl_worker_t *wrk = vcl_worker_get_current ();
1302 session_accepted_msg_t accepted_msg;
1303 vcl_session_t *listen_session = 0;
1304 vcl_session_t *client_session = 0;
1305 svm_msg_q_t *vpp_evt_q;
1306 vcl_session_msg_t *evt;
1307 u64 listen_vpp_handle;
1308 svm_msg_q_msg_t msg;
1313 listen_session = vcl_session_get_w_handle (wrk, listen_session_handle);
1314 if (!listen_session)
1315 return VPPCOM_EBADFD;
1317 listen_session_index = listen_session->session_index;
1318 if ((rv = validate_args_session_accept_ (wrk, listen_session)))
1321 if (clib_fifo_elts (listen_session->accept_evts_fifo))
1323 clib_fifo_sub2 (listen_session->accept_evts_fifo, evt);
1324 accept_flags = evt->flags;
1325 accepted_msg = evt->accepted_msg;
1329 is_nonblocking = VCL_SESS_ATTR_TEST (listen_session->attr,
1330 VCL_SESS_ATTR_NONBLOCK);
1331 if (svm_msg_q_is_empty (wrk->app_event_queue) && is_nonblocking)
1332 return VPPCOM_EAGAIN;
1336 if (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_WAIT, 0))
1337 return VPPCOM_EAGAIN;
1339 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
1340 if (e->event_type != SESSION_CTRL_EVT_ACCEPTED)
1342 clib_warning ("discarded event: %u", e->event_type);
1343 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
1346 clib_memcpy_fast (&accepted_msg, e->data, sizeof (accepted_msg));
1347 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
1353 client_session_index = vcl_session_accepted_handler (wrk, &accepted_msg);
1354 listen_session = vcl_session_get (wrk, listen_session_index);
1355 client_session = vcl_session_get (wrk, client_session_index);
1357 if (flags & O_NONBLOCK)
1358 VCL_SESS_ATTR_SET (client_session->attr, VCL_SESS_ATTR_NONBLOCK);
1360 listen_vpp_handle = listen_session->vpp_handle;
1361 VDBG (1, "vpp handle 0x%llx, sid %u: Got a client request! "
1362 "vpp handle 0x%llx, sid %u, flags %d, is_nonblocking %u",
1363 listen_vpp_handle, listen_session_handle,
1364 client_session->vpp_handle, client_session_index,
1365 flags, VCL_SESS_ATTR_TEST (client_session->attr,
1366 VCL_SESS_ATTR_NONBLOCK));
1370 ep->is_ip4 = client_session->transport.is_ip4;
1371 ep->port = client_session->transport.rmt_port;
1372 if (client_session->transport.is_ip4)
1373 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip4,
1374 sizeof (ip4_address_t));
1376 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip6,
1377 sizeof (ip6_address_t));
1380 if (accepted_msg.server_event_queue_address)
1381 vpp_evt_q = uword_to_pointer (accepted_msg.vpp_event_queue_address,
1384 vpp_evt_q = client_session->vpp_evt_q;
1386 vcl_send_session_accepted_reply (vpp_evt_q, client_session->client_context,
1387 client_session->vpp_handle, 0);
1389 VDBG (0, "listener %u [0x%llx] accepted %u [0x%llx] peer: %U:%u "
1390 "local: %U:%u", listen_session_handle, listen_vpp_handle,
1391 client_session_index, client_session->vpp_handle,
1392 format_ip46_address, &client_session->transport.rmt_ip,
1393 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
1394 clib_net_to_host_u16 (client_session->transport.rmt_port),
1395 format_ip46_address, &client_session->transport.lcl_ip,
1396 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
1397 clib_net_to_host_u16 (client_session->transport.lcl_port));
1398 vcl_evt (VCL_EVT_ACCEPT, client_session, listen_session,
1399 client_session_index);
1402 * Session might have been closed already
1406 if (accept_flags & VCL_ACCEPTED_F_CLOSED)
1407 client_session->session_state = STATE_VPP_CLOSING;
1408 else if (accept_flags & VCL_ACCEPTED_F_RESET)
1409 client_session->session_state = STATE_DISCONNECT;
1411 return vcl_session_handle (client_session);
1415 vppcom_session_connect (uint32_t session_handle, vppcom_endpt_t * server_ep)
1417 vcl_worker_t *wrk = vcl_worker_get_current ();
1418 vcl_session_t *session = 0;
1422 session = vcl_session_get_w_handle (wrk, session_handle);
1424 return VPPCOM_EBADFD;
1425 session_index = session->session_index;
1427 if (PREDICT_FALSE (session->is_vep))
1429 clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
1430 "connect on an epoll session!", getpid (),
1432 return VPPCOM_EBADFD;
1435 if (PREDICT_FALSE (session->session_state & CLIENT_STATE_OPEN))
1437 VDBG (0, "session handle %u [0x%llx]: session already "
1438 "connected to %s %U port %d proto %s, state 0x%x (%s)",
1439 session_handle, session->vpp_handle,
1440 session->transport.is_ip4 ? "IPv4" : "IPv6",
1441 format_ip46_address,
1442 &session->transport.rmt_ip, session->transport.is_ip4 ?
1443 IP46_TYPE_IP4 : IP46_TYPE_IP6,
1444 clib_net_to_host_u16 (session->transport.rmt_port),
1445 vppcom_proto_str (session->session_type), session->session_state,
1446 vppcom_session_state_str (session->session_state));
1450 session->transport.is_ip4 = server_ep->is_ip4;
1451 if (session->transport.is_ip4)
1452 clib_memcpy_fast (&session->transport.rmt_ip.ip4, server_ep->ip,
1453 sizeof (ip4_address_t));
1455 clib_memcpy_fast (&session->transport.rmt_ip.ip6, server_ep->ip,
1456 sizeof (ip6_address_t));
1457 session->transport.rmt_port = server_ep->port;
1459 VDBG (0, "session handle %u [0x%llx]: connecting to server %s %U "
1460 "port %d proto %s", session_handle, session->vpp_handle,
1461 session->transport.is_ip4 ? "IPv4" : "IPv6",
1462 format_ip46_address,
1463 &session->transport.rmt_ip, session->transport.is_ip4 ?
1464 IP46_TYPE_IP4 : IP46_TYPE_IP6,
1465 clib_net_to_host_u16 (session->transport.rmt_port),
1466 vppcom_proto_str (session->session_type));
1469 * Send connect request and wait for reply from vpp
1471 vppcom_send_connect_sock (session);
1472 rv = vppcom_wait_for_session_state_change (session_index, STATE_CONNECT,
1473 vcm->cfg.session_timeout);
1475 session = vcl_session_get (wrk, session_index);
1477 if (PREDICT_FALSE (rv))
1479 if (VPPCOM_DEBUG > 0)
1482 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: connect "
1483 "failed! returning %d (%s)", getpid (),
1484 session->vpp_handle, session_handle, rv,
1485 vppcom_retval_str (rv));
1487 clib_warning ("VCL<%d>: no session for sid %u: connect failed! "
1488 "returning %d (%s)", getpid (),
1489 session_handle, rv, vppcom_retval_str (rv));
1493 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: connected!",
1494 getpid (), session->vpp_handle, session_handle);
1500 vcl_is_rx_evt_for_session (session_event_t * e, u32 sid, u8 is_ct)
1503 return (e->event_type == FIFO_EVENT_APP_RX
1504 && e->fifo->client_session_index == sid);
1506 return (e->event_type == SESSION_IO_EVT_CT_TX);
1510 vcl_session_is_readable (vcl_session_t * s)
1512 return ((s->session_state & STATE_OPEN)
1513 || (s->session_state == STATE_LISTEN
1514 && s->session_type == VPPCOM_PROTO_UDP));
1518 vppcom_session_read_internal (uint32_t session_handle, void *buf, int n,
1521 vcl_worker_t *wrk = vcl_worker_get_current ();
1522 int n_read = 0, rv, is_nonblocking;
1523 vcl_session_t *s = 0;
1524 svm_fifo_t *rx_fifo;
1525 svm_msg_q_msg_t msg;
1530 if (PREDICT_FALSE (!buf))
1531 return VPPCOM_EINVAL;
1533 s = vcl_session_get_w_handle (wrk, session_handle);
1534 if (PREDICT_FALSE (!s || s->is_vep))
1535 return VPPCOM_EBADFD;
1537 if (PREDICT_FALSE (!vcl_session_is_readable (s)))
1539 session_state_t state = s->session_state;
1540 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
1542 VDBG (0, "session handle %u[0x%llx] is not open! state 0x%x (%s),"
1543 " returning %d (%s)", session_handle, s->vpp_handle, state,
1544 vppcom_session_state_str (state), rv, vppcom_retval_str (rv));
1548 is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
1549 is_ct = vcl_session_is_ct (s);
1550 mq = is_ct ? s->our_evt_q : wrk->app_event_queue;
1551 rx_fifo = s->rx_fifo;
1554 if (svm_fifo_is_empty (rx_fifo))
1558 svm_fifo_unset_event (rx_fifo);
1559 return VPPCOM_EWOULDBLOCK;
1561 while (svm_fifo_is_empty (rx_fifo))
1563 svm_fifo_unset_event (rx_fifo);
1564 svm_msg_q_lock (mq);
1565 if (svm_msg_q_is_empty (mq))
1566 svm_msg_q_wait (mq);
1568 svm_msg_q_sub_w_lock (mq, &msg);
1569 e = svm_msg_q_msg_data (mq, &msg);
1570 svm_msg_q_unlock (mq);
1571 if (!vcl_is_rx_evt_for_session (e, s->session_index, is_ct))
1572 vcl_handle_mq_event (wrk, e);
1573 svm_msg_q_free_msg (mq, &msg);
1575 if (PREDICT_FALSE (s->session_state == STATE_DISCONNECT))
1576 return VPPCOM_ECONNRESET;
1581 n_read = app_recv_dgram_raw (rx_fifo, buf, n, &s->transport, 0, peek);
1583 n_read = app_recv_stream_raw (rx_fifo, buf, n, 0, peek);
1585 if (svm_fifo_is_empty (rx_fifo))
1586 svm_fifo_unset_event (rx_fifo);
1588 if (is_ct && svm_fifo_needs_tx_ntf (rx_fifo, n_read))
1590 svm_fifo_clear_tx_ntf (s->rx_fifo);
1591 app_send_io_evt_to_vpp (s->vpp_evt_q, s->rx_fifo, SESSION_IO_EVT_CT_RX,
1595 VDBG (2, "vpp handle 0x%llx, sid %u: read %d bytes from (%p)",
1596 s->vpp_handle, session_handle, n_read, rx_fifo);
1602 vppcom_session_read (uint32_t session_handle, void *buf, size_t n)
1604 return (vppcom_session_read_internal (session_handle, buf, n, 0));
1608 vppcom_session_peek (uint32_t session_handle, void *buf, int n)
1610 return (vppcom_session_read_internal (session_handle, buf, n, 1));
1614 vppcom_session_read_segments (uint32_t session_handle,
1615 vppcom_data_segments_t ds)
1617 vcl_worker_t *wrk = vcl_worker_get_current ();
1618 int n_read = 0, rv, is_nonblocking;
1619 vcl_session_t *s = 0;
1620 svm_fifo_t *rx_fifo;
1621 svm_msg_q_msg_t msg;
1626 s = vcl_session_get_w_handle (wrk, session_handle);
1627 if (PREDICT_FALSE (!s || s->is_vep))
1628 return VPPCOM_EBADFD;
1630 if (PREDICT_FALSE (!vcl_session_is_readable (s)))
1632 session_state_t state = s->session_state;
1633 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
1637 is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
1638 is_ct = vcl_session_is_ct (s);
1639 mq = is_ct ? s->our_evt_q : wrk->app_event_queue;
1640 rx_fifo = s->rx_fifo;
1643 if (svm_fifo_is_empty (rx_fifo))
1647 svm_fifo_unset_event (rx_fifo);
1648 return VPPCOM_EWOULDBLOCK;
1650 while (svm_fifo_is_empty (rx_fifo))
1652 svm_fifo_unset_event (rx_fifo);
1653 svm_msg_q_lock (mq);
1654 if (svm_msg_q_is_empty (mq))
1655 svm_msg_q_wait (mq);
1657 svm_msg_q_sub_w_lock (mq, &msg);
1658 e = svm_msg_q_msg_data (mq, &msg);
1659 svm_msg_q_unlock (mq);
1660 if (!vcl_is_rx_evt_for_session (e, s->session_index, is_ct))
1661 vcl_handle_mq_event (wrk, e);
1662 svm_msg_q_free_msg (mq, &msg);
1664 if (PREDICT_FALSE (s->session_state == STATE_DISCONNECT))
1665 return VPPCOM_ECONNRESET;
1669 n_read = svm_fifo_segments (rx_fifo, (svm_fifo_segment_t *) ds);
1670 svm_fifo_unset_event (rx_fifo);
1672 if (is_ct && n_read + svm_fifo_max_dequeue (rx_fifo) == rx_fifo->nitems)
1674 /* If the peer is not polling send notification */
1675 if (!svm_fifo_has_event (s->rx_fifo))
1676 app_send_io_evt_to_vpp (s->vpp_evt_q, s->rx_fifo,
1677 SESSION_IO_EVT_CT_RX, SVM_Q_WAIT);
1684 vppcom_session_free_segments (uint32_t session_handle,
1685 vppcom_data_segments_t ds)
1687 vcl_worker_t *wrk = vcl_worker_get_current ();
1690 s = vcl_session_get_w_handle (wrk, session_handle);
1691 if (PREDICT_FALSE (!s || s->is_vep))
1694 svm_fifo_segments_free (s->rx_fifo, (svm_fifo_segment_t *) ds);
1698 vppcom_data_segment_copy (void *buf, vppcom_data_segments_t ds, u32 max_bytes)
1700 u32 first_copy = clib_min (ds[0].len, max_bytes);
1701 clib_memcpy_fast (buf, ds[0].data, first_copy);
1702 if (first_copy < max_bytes)
1704 clib_memcpy_fast (buf + first_copy, ds[1].data,
1705 clib_min (ds[1].len, max_bytes - first_copy));
1711 vcl_is_tx_evt_for_session (session_event_t * e, u32 sid, u8 is_ct)
1714 return (e->event_type == FIFO_EVENT_APP_TX
1715 && e->fifo->client_session_index == sid);
1717 return (e->event_type == SESSION_IO_EVT_CT_RX);
1721 vppcom_session_write_inline (uint32_t session_handle, void *buf, size_t n,
1724 vcl_worker_t *wrk = vcl_worker_get_current ();
1725 int rv, n_write, is_nonblocking;
1726 vcl_session_t *s = 0;
1727 svm_fifo_t *tx_fifo = 0;
1728 session_evt_type_t et;
1729 svm_msg_q_msg_t msg;
1734 if (PREDICT_FALSE (!buf))
1735 return VPPCOM_EINVAL;
1737 s = vcl_session_get_w_handle (wrk, session_handle);
1738 if (PREDICT_FALSE (!s))
1739 return VPPCOM_EBADFD;
1741 if (PREDICT_FALSE (s->is_vep))
1743 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
1744 "cannot write to an epoll session!",
1745 getpid (), s->vpp_handle, session_handle);
1747 return VPPCOM_EBADFD;
1750 if (PREDICT_FALSE (!(s->session_state & STATE_OPEN)))
1752 session_state_t state = s->session_state;
1753 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
1754 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: session is not open! "
1755 "state 0x%x (%s)", getpid (), s->vpp_handle, session_handle,
1756 state, vppcom_session_state_str (state));
1760 tx_fifo = s->tx_fifo;
1761 is_ct = vcl_session_is_ct (s);
1762 is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
1763 mq = is_ct ? s->our_evt_q : wrk->app_event_queue;
1764 if (svm_fifo_is_full (tx_fifo))
1768 return VPPCOM_EWOULDBLOCK;
1770 while (svm_fifo_is_full (tx_fifo))
1772 svm_fifo_add_want_tx_ntf (tx_fifo, SVM_FIFO_WANT_TX_NOTIF);
1773 svm_msg_q_lock (mq);
1774 if (svm_msg_q_is_empty (mq))
1775 svm_msg_q_wait (mq);
1777 svm_msg_q_sub_w_lock (mq, &msg);
1778 e = svm_msg_q_msg_data (mq, &msg);
1779 svm_msg_q_unlock (mq);
1781 if (!vcl_is_tx_evt_for_session (e, s->session_index, is_ct))
1782 vcl_handle_mq_event (wrk, e);
1783 svm_msg_q_free_msg (mq, &msg);
1785 if (PREDICT_FALSE (!(s->session_state & STATE_OPEN)))
1786 return VPPCOM_ECONNRESET;
1790 ASSERT (FIFO_EVENT_APP_TX + 1 == SESSION_IO_EVT_CT_TX);
1791 et = FIFO_EVENT_APP_TX + vcl_session_is_ct (s);
1792 if (is_flush && !vcl_session_is_ct (s))
1793 et = SESSION_IO_EVT_TX_FLUSH;
1796 n_write = app_send_dgram_raw (tx_fifo, &s->transport,
1797 s->vpp_evt_q, buf, n, et, SVM_Q_WAIT);
1799 n_write = app_send_stream_raw (tx_fifo, s->vpp_evt_q, buf, n, et,
1802 ASSERT (n_write > 0);
1804 VDBG (2, "VCL<%d>: vpp handle 0x%llx, sid %u: wrote %d bytes", getpid (),
1805 s->vpp_handle, session_handle, n_write);
1811 vppcom_session_write (uint32_t session_handle, void *buf, size_t n)
1813 return vppcom_session_write_inline (session_handle, buf, n,
1818 vppcom_session_write_msg (uint32_t session_handle, void *buf, size_t n)
1820 return vppcom_session_write_inline (session_handle, buf, n,
1825 static vcl_session_t *
1826 vcl_ct_session_get_from_fifo (vcl_worker_t * wrk, svm_fifo_t * f, u8 type)
1829 s = vcl_session_get (wrk, f->client_session_index);
1833 if (type == 0 && s->rx_fifo == f)
1836 if (type == 1 && s->tx_fifo == f)
1839 s = vcl_session_get (wrk, f->master_session_index);
1842 if (type == 0 && s->rx_fifo == f)
1844 if (type == 1 && s->tx_fifo == f)
1850 #define vcl_fifo_rx_evt_valid_or_break(_fifo) \
1851 if (PREDICT_FALSE (svm_fifo_is_empty (_fifo))) \
1853 svm_fifo_unset_event (_fifo); \
1854 if (svm_fifo_is_empty (_fifo)) \
1859 vcl_select_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
1860 unsigned long n_bits, unsigned long *read_map,
1861 unsigned long *write_map,
1862 unsigned long *except_map, u32 * bits_set)
1864 session_disconnected_msg_t *disconnected_msg;
1865 session_connected_msg_t *connected_msg;
1866 vcl_session_t *session;
1869 switch (e->event_type)
1871 case FIFO_EVENT_APP_RX:
1872 vcl_fifo_rx_evt_valid_or_break (e->fifo);
1873 sid = e->fifo->client_session_index;
1874 session = vcl_session_get (wrk, sid);
1877 if (sid < n_bits && read_map)
1879 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
1883 case FIFO_EVENT_APP_TX:
1884 sid = e->fifo->client_session_index;
1885 session = vcl_session_get (wrk, sid);
1888 if (sid < n_bits && write_map)
1890 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
1894 case SESSION_IO_EVT_CT_TX:
1895 vcl_fifo_rx_evt_valid_or_break (e->fifo);
1896 session = vcl_ct_session_get_from_fifo (wrk, e->fifo, 0);
1899 sid = session->session_index;
1900 if (sid < n_bits && read_map)
1902 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
1906 case SESSION_IO_EVT_CT_RX:
1907 session = vcl_ct_session_get_from_fifo (wrk, e->fifo, 1);
1910 sid = session->session_index;
1911 if (sid < n_bits && write_map)
1913 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
1917 case SESSION_CTRL_EVT_ACCEPTED:
1918 session = vcl_session_accepted (wrk,
1919 (session_accepted_msg_t *) e->data);
1922 sid = session->session_index;
1923 if (sid < n_bits && read_map)
1925 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
1929 case SESSION_CTRL_EVT_CONNECTED:
1930 connected_msg = (session_connected_msg_t *) e->data;
1931 vcl_session_connected_handler (wrk, connected_msg);
1933 case SESSION_CTRL_EVT_DISCONNECTED:
1934 disconnected_msg = (session_disconnected_msg_t *) e->data;
1935 session = vcl_session_disconnected_handler (wrk, disconnected_msg);
1938 sid = session->session_index;
1939 if (sid < n_bits && except_map)
1941 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
1945 case SESSION_CTRL_EVT_RESET:
1946 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
1947 if (sid < n_bits && except_map)
1949 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
1953 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
1954 vcl_session_worker_update_reply_handler (wrk, e->data);
1956 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
1957 vcl_session_req_worker_update_handler (wrk, e->data);
1960 clib_warning ("unhandled: %u", e->event_type);
1966 vcl_select_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
1967 unsigned long n_bits, unsigned long *read_map,
1968 unsigned long *write_map, unsigned long *except_map,
1969 double time_to_wait, u32 * bits_set)
1971 svm_msg_q_msg_t *msg;
1975 svm_msg_q_lock (mq);
1976 if (svm_msg_q_is_empty (mq))
1980 svm_msg_q_unlock (mq);
1986 svm_msg_q_unlock (mq);
1989 else if (time_to_wait < 0)
1991 svm_msg_q_wait (mq);
1995 if (svm_msg_q_timedwait (mq, time_to_wait))
1997 svm_msg_q_unlock (mq);
2002 vcl_mq_dequeue_batch (wrk, mq);
2003 svm_msg_q_unlock (mq);
2005 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
2007 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
2008 e = svm_msg_q_msg_data (mq, msg);
2009 vcl_select_handle_mq_event (wrk, e, n_bits, read_map, write_map,
2010 except_map, bits_set);
2011 svm_msg_q_free_msg (mq, msg);
2013 vec_reset_length (wrk->mq_msg_vector);
2014 vcl_handle_pending_wrk_updates (wrk);
2019 vppcom_select_condvar (vcl_worker_t * wrk, int n_bits,
2020 vcl_si_set * read_map, vcl_si_set * write_map,
2021 vcl_si_set * except_map, double time_to_wait,
2024 double total_wait = 0, wait_slice;
2025 vcl_cut_through_registration_t *cr;
2027 time_to_wait = (time_to_wait == -1) ? 1e6 : time_to_wait;
2028 wait_slice = wrk->cut_through_registrations ? 10e-6 : time_to_wait;
2031 vcl_ct_registration_lock (wrk);
2033 pool_foreach (cr, wrk->cut_through_registrations, ({
2034 vcl_select_handle_mq (wrk, cr->mq, n_bits, read_map, write_map, except_map,
2038 vcl_ct_registration_unlock (wrk);
2040 vcl_select_handle_mq (wrk, wrk->app_event_queue, n_bits, read_map,
2041 write_map, except_map, wait_slice, bits_set);
2042 total_wait += wait_slice;
2046 while (total_wait < time_to_wait);
2052 vppcom_select_eventfd (vcl_worker_t * wrk, int n_bits,
2053 vcl_si_set * read_map, vcl_si_set * write_map,
2054 vcl_si_set * except_map, double time_to_wait,
2057 vcl_mq_evt_conn_t *mqc;
2058 int __clib_unused n_read;
2062 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
2063 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
2064 vec_len (wrk->mq_events), time_to_wait);
2065 for (i = 0; i < n_mq_evts; i++)
2067 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
2068 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
2069 vcl_select_handle_mq (wrk, mqc->mq, n_bits, read_map, write_map,
2070 except_map, 0, bits_set);
2073 return (n_mq_evts > 0 ? (int) *bits_set : 0);
2077 vppcom_select (int n_bits, vcl_si_set * read_map, vcl_si_set * write_map,
2078 vcl_si_set * except_map, double time_to_wait)
2080 u32 sid, minbits = clib_max (n_bits, BITS (uword)), bits_set = 0;
2081 vcl_worker_t *wrk = vcl_worker_get_current ();
2082 vcl_session_t *session = 0;
2085 if (n_bits && read_map)
2087 clib_bitmap_validate (wrk->rd_bitmap, minbits);
2088 clib_memcpy_fast (wrk->rd_bitmap, read_map,
2089 vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
2090 memset (read_map, 0, vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
2092 if (n_bits && write_map)
2094 clib_bitmap_validate (wrk->wr_bitmap, minbits);
2095 clib_memcpy_fast (wrk->wr_bitmap, write_map,
2096 vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
2097 memset (write_map, 0, vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
2099 if (n_bits && except_map)
2101 clib_bitmap_validate (wrk->ex_bitmap, minbits);
2102 clib_memcpy_fast (wrk->ex_bitmap, except_map,
2103 vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
2104 memset (except_map, 0, vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
2114 clib_bitmap_foreach (sid, wrk->wr_bitmap, ({
2115 if (!(session = vcl_session_get (wrk, sid)))
2117 if (except_map && sid < minbits)
2118 clib_bitmap_set_no_check (except_map, sid, 1);
2122 rv = svm_fifo_is_full (session->tx_fifo);
2125 clib_bitmap_set_no_check ((uword*)write_map, sid, 1);
2129 svm_fifo_add_want_tx_ntf (session->tx_fifo, SVM_FIFO_WANT_TX_NOTIF);
2136 clib_bitmap_foreach (sid, wrk->rd_bitmap, ({
2137 if (!(session = vcl_session_get (wrk, sid)))
2139 if (except_map && sid < minbits)
2140 clib_bitmap_set_no_check (except_map, sid, 1);
2144 rv = vcl_session_read_ready (session);
2147 clib_bitmap_set_no_check ((uword*)read_map, sid, 1);
2155 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
2157 vcl_select_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i], n_bits,
2158 read_map, write_map, except_map, &bits_set);
2160 vec_reset_length (wrk->unhandled_evts_vector);
2162 if (vcm->cfg.use_mq_eventfd)
2163 vppcom_select_eventfd (wrk, n_bits, read_map, write_map, except_map,
2164 time_to_wait, &bits_set);
2166 vppcom_select_condvar (wrk, n_bits, read_map, write_map, except_map,
2167 time_to_wait, &bits_set);
2173 vep_verify_epoll_chain (vcl_worker_t * wrk, u32 vep_idx)
2175 vcl_session_t *session;
2176 vppcom_epoll_t *vep;
2179 if (VPPCOM_DEBUG <= 1)
2182 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
2183 session = vcl_session_get (wrk, vep_idx);
2184 if (PREDICT_FALSE (!session))
2186 clib_warning ("VCL<%d>: ERROR: Invalid vep_idx (%u)!",
2187 getpid (), vep_idx);
2190 if (PREDICT_FALSE (!session->is_vep))
2192 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
2193 getpid (), vep_idx);
2196 vep = &session->vep;
2197 clib_warning ("VCL<%d>: vep_idx (%u): Dumping epoll chain\n"
2200 " is_vep_session = %u\n"
2201 " next_sid = 0x%x (%u)\n"
2202 " wait_cont_idx = 0x%x (%u)\n"
2203 "}\n", getpid (), vep_idx,
2204 session->is_vep, session->is_vep_session,
2205 vep->next_sh, vep->next_sh,
2206 session->wait_cont_idx, session->wait_cont_idx);
2208 for (sid = vep->next_sh; sid != ~0; sid = vep->next_sh)
2210 session = vcl_session_get (wrk, sid);
2211 if (PREDICT_FALSE (!session))
2213 clib_warning ("VCL<%d>: ERROR: Invalid sid (%u)!", getpid (), sid);
2216 if (PREDICT_FALSE (session->is_vep))
2217 clib_warning ("VCL<%d>: ERROR: sid (%u) is a vep!",
2218 getpid (), vep_idx);
2219 else if (PREDICT_FALSE (!session->is_vep_session))
2221 clib_warning ("VCL<%d>: ERROR: session (%u) "
2222 "is not a vep session!", getpid (), sid);
2225 vep = &session->vep;
2226 if (PREDICT_FALSE (vep->vep_sh != vep_idx))
2227 clib_warning ("VCL<%d>: ERROR: session (%u) vep_idx (%u) != "
2228 "vep_idx (%u)!", getpid (),
2229 sid, session->vep.vep_sh, vep_idx);
2230 if (session->is_vep_session)
2232 clib_warning ("vep_idx[%u]: sid 0x%x (%u)\n"
2234 " next_sid = 0x%x (%u)\n"
2235 " prev_sid = 0x%x (%u)\n"
2236 " vep_idx = 0x%x (%u)\n"
2237 " ev.events = 0x%x\n"
2238 " ev.data.u64 = 0x%llx\n"
2242 vep->next_sh, vep->next_sh,
2243 vep->prev_sh, vep->prev_sh,
2244 vep->vep_sh, vep->vep_sh,
2245 vep->ev.events, vep->ev.data.u64, vep->et_mask);
2250 clib_warning ("VCL<%d>: vep_idx (%u): Dump complete!\n",
2251 getpid (), vep_idx);
2255 vppcom_epoll_create (void)
2257 vcl_worker_t *wrk = vcl_worker_get_current ();
2258 vcl_session_t *vep_session;
2260 vep_session = vcl_session_alloc (wrk);
2262 vep_session->is_vep = 1;
2263 vep_session->vep.vep_sh = ~0;
2264 vep_session->vep.next_sh = ~0;
2265 vep_session->vep.prev_sh = ~0;
2266 vep_session->wait_cont_idx = ~0;
2267 vep_session->vpp_handle = ~0;
2269 vcl_evt (VCL_EVT_EPOLL_CREATE, vep_session, vep_session->session_index);
2270 VDBG (0, "Created vep_idx %u", vep_session->session_index);
2272 return vcl_session_handle (vep_session);
2276 vppcom_epoll_ctl (uint32_t vep_handle, int op, uint32_t session_handle,
2277 struct epoll_event *event)
2279 vcl_worker_t *wrk = vcl_worker_get_current ();
2280 vcl_session_t *vep_session;
2281 vcl_session_t *session;
2284 if (vep_handle == session_handle)
2286 VDBG (0, "vep_sh == session handle (%u)!", vep_handle);
2287 return VPPCOM_EINVAL;
2290 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
2291 if (PREDICT_FALSE (!vep_session))
2293 VDBG (0, "Invalid vep_sh (%u)!", vep_handle);
2294 return VPPCOM_EBADFD;
2296 if (PREDICT_FALSE (!vep_session->is_vep))
2298 VDBG (0, "vep_sh (%u) is not a vep!", vep_handle);
2299 return VPPCOM_EINVAL;
2302 ASSERT (vep_session->vep.vep_sh == ~0);
2303 ASSERT (vep_session->vep.prev_sh == ~0);
2305 session = vcl_session_get_w_handle (wrk, session_handle);
2306 if (PREDICT_FALSE (!session))
2308 VDBG (0, "Invalid session_handle (%u)!", session_handle);
2309 return VPPCOM_EBADFD;
2311 if (PREDICT_FALSE (session->is_vep))
2313 VDBG (0, "session_handle (%u) is a vep!", vep_handle);
2314 return VPPCOM_EINVAL;
2320 if (PREDICT_FALSE (!event))
2322 VDBG (0, "EPOLL_CTL_ADD: NULL pointer to epoll_event structure!");
2323 return VPPCOM_EINVAL;
2325 if (vep_session->vep.next_sh != ~0)
2327 vcl_session_t *next_session;
2328 next_session = vcl_session_get_w_handle (wrk,
2329 vep_session->vep.next_sh);
2330 if (PREDICT_FALSE (!next_session))
2332 VDBG (0, "EPOLL_CTL_ADD: Invalid vep.next_sid (%u) on "
2333 "vep_idx (%u)!", vep_session->vep.next_sh, vep_handle);
2334 return VPPCOM_EBADFD;
2336 ASSERT (next_session->vep.prev_sh == vep_handle);
2337 next_session->vep.prev_sh = session_handle;
2339 session->vep.next_sh = vep_session->vep.next_sh;
2340 session->vep.prev_sh = vep_handle;
2341 session->vep.vep_sh = vep_handle;
2342 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
2343 session->vep.ev = *event;
2344 session->is_vep = 0;
2345 session->is_vep_session = 1;
2346 vep_session->vep.next_sh = session_handle;
2348 if (session->tx_fifo)
2349 svm_fifo_add_want_tx_ntf (session->tx_fifo,
2350 SVM_FIFO_WANT_TX_NOTIF_IF_FULL);
2352 VDBG (1, "EPOLL_CTL_ADD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2353 vep_handle, session_handle, event->events, event->data.u64);
2354 vcl_evt (VCL_EVT_EPOLL_CTLADD, session, event->events, event->data.u64);
2358 if (PREDICT_FALSE (!event))
2360 VDBG (0, "EPOLL_CTL_MOD: NULL pointer to epoll_event structure!");
2364 else if (PREDICT_FALSE (!session->is_vep_session))
2366 VDBG (0, "sid %u EPOLL_CTL_MOD: not a vep session!",
2371 else if (PREDICT_FALSE (session->vep.vep_sh != vep_handle))
2373 VDBG (0, "EPOLL_CTL_MOD: sh %u vep_sh (%u) != vep_sh (%u)!",
2374 session_handle, session->vep.vep_sh, vep_handle);
2378 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
2379 session->vep.ev = *event;
2380 VDBG (1, "EPOLL_CTL_MOD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2381 vep_handle, session_handle, event->events, event->data.u64);
2385 if (PREDICT_FALSE (!session->is_vep_session))
2387 VDBG (0, "EPOLL_CTL_DEL: %u not a vep session!", session_handle);
2391 else if (PREDICT_FALSE (session->vep.vep_sh != vep_handle))
2393 VDBG (0, "EPOLL_CTL_DEL: sh %u vep_sh (%u) != vep_sh (%u)!",
2394 session_handle, session->vep.vep_sh, vep_handle);
2399 vep_session->wait_cont_idx =
2400 (vep_session->wait_cont_idx == session_handle) ?
2401 session->vep.next_sh : vep_session->wait_cont_idx;
2403 if (session->vep.prev_sh == vep_handle)
2404 vep_session->vep.next_sh = session->vep.next_sh;
2407 vcl_session_t *prev_session;
2408 prev_session = vcl_session_get_w_handle (wrk, session->vep.prev_sh);
2409 if (PREDICT_FALSE (!prev_session))
2411 VDBG (0, "EPOLL_CTL_DEL: Invalid prev_sid (%u) on sid (%u)!",
2412 session->vep.prev_sh, session_handle);
2413 return VPPCOM_EBADFD;
2415 ASSERT (prev_session->vep.next_sh == session_handle);
2416 prev_session->vep.next_sh = session->vep.next_sh;
2418 if (session->vep.next_sh != ~0)
2420 vcl_session_t *next_session;
2421 next_session = vcl_session_get_w_handle (wrk, session->vep.next_sh);
2422 if (PREDICT_FALSE (!next_session))
2424 VDBG (0, "EPOLL_CTL_DEL: Invalid next_sid (%u) on sid (%u)!",
2425 session->vep.next_sh, session_handle);
2426 return VPPCOM_EBADFD;
2428 ASSERT (next_session->vep.prev_sh == session_handle);
2429 next_session->vep.prev_sh = session->vep.prev_sh;
2432 memset (&session->vep, 0, sizeof (session->vep));
2433 session->vep.next_sh = ~0;
2434 session->vep.prev_sh = ~0;
2435 session->vep.vep_sh = ~0;
2436 session->is_vep_session = 0;
2438 if (session->tx_fifo)
2439 svm_fifo_del_want_tx_ntf (session->tx_fifo, SVM_FIFO_NO_TX_NOTIF);
2441 VDBG (1, "EPOLL_CTL_DEL: vep_idx %u, sid %u!", vep_handle,
2443 vcl_evt (VCL_EVT_EPOLL_CTLDEL, session, vep_sh);
2447 VDBG (0, "Invalid operation (%d)!", op);
2451 vep_verify_epoll_chain (wrk, vep_handle);
2458 vcl_epoll_wait_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
2459 struct epoll_event *events, u32 * num_ev)
2461 session_disconnected_msg_t *disconnected_msg;
2462 session_connected_msg_t *connected_msg;
2463 u32 sid = ~0, session_events;
2464 u64 session_evt_data = ~0;
2465 vcl_session_t *session;
2468 switch (e->event_type)
2470 case FIFO_EVENT_APP_RX:
2471 ASSERT (e->fifo->client_thread_index == vcl_get_worker_index ());
2472 vcl_fifo_rx_evt_valid_or_break (e->fifo);
2473 sid = e->fifo->client_session_index;
2474 if (!(session = vcl_session_get (wrk, sid)))
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 FIFO_EVENT_APP_TX:
2485 sid = e->fifo->client_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_tx_ntf (session->tx_fifo);
2496 case SESSION_IO_EVT_CT_TX:
2497 vcl_fifo_rx_evt_valid_or_break (e->fifo);
2498 session = vcl_ct_session_get_from_fifo (wrk, e->fifo, 0);
2499 if (PREDICT_FALSE (!session))
2501 sid = session->session_index;
2502 session_events = session->vep.ev.events;
2503 if (!(EPOLLIN & session->vep.ev.events) || session->has_rx_evt)
2506 events[*num_ev].events |= EPOLLIN;
2507 session_evt_data = session->vep.ev.data.u64;
2508 session->has_rx_evt = 1;
2510 case SESSION_IO_EVT_CT_RX:
2511 session = vcl_ct_session_get_from_fifo (wrk, e->fifo, 1);
2512 if (PREDICT_FALSE (!session))
2514 sid = session->session_index;
2515 session_events = session->vep.ev.events;
2516 if (!(EPOLLOUT & session_events))
2519 events[*num_ev].events |= EPOLLOUT;
2520 session_evt_data = session->vep.ev.data.u64;
2521 svm_fifo_reset_tx_ntf (session->tx_fifo);
2523 case SESSION_CTRL_EVT_ACCEPTED:
2524 session = vcl_session_accepted (wrk,
2525 (session_accepted_msg_t *) e->data);
2529 session_events = session->vep.ev.events;
2530 if (!(EPOLLIN & session_events))
2534 events[*num_ev].events |= EPOLLIN;
2535 session_evt_data = session->vep.ev.data.u64;
2537 case SESSION_CTRL_EVT_CONNECTED:
2538 connected_msg = (session_connected_msg_t *) e->data;
2539 vcl_session_connected_handler (wrk, connected_msg);
2540 /* Generate EPOLLOUT because there's no connected event */
2541 sid = vcl_session_index_from_vpp_handle (wrk, connected_msg->handle);
2542 if (!(session = vcl_session_get (wrk, sid)))
2544 session_events = session->vep.ev.events;
2545 if (!(EPOLLOUT & session_events))
2548 events[*num_ev].events |= EPOLLOUT;
2549 session_evt_data = session->vep.ev.data.u64;
2551 case SESSION_CTRL_EVT_DISCONNECTED:
2552 disconnected_msg = (session_disconnected_msg_t *) e->data;
2553 session = vcl_session_disconnected_handler (wrk, disconnected_msg);
2556 session_events = session->vep.ev.events;
2557 if (!((EPOLLHUP | EPOLLRDHUP) & session_events))
2560 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
2561 session_evt_data = session->vep.ev.data.u64;
2563 case SESSION_CTRL_EVT_RESET:
2564 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
2565 if (!(session = vcl_session_get (wrk, sid)))
2567 session_events = session->vep.ev.events;
2568 if (!((EPOLLHUP | EPOLLRDHUP) & session_events))
2571 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
2572 session_evt_data = session->vep.ev.data.u64;
2574 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
2575 vcl_session_req_worker_update_handler (wrk, e->data);
2577 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
2578 vcl_session_worker_update_reply_handler (wrk, e->data);
2581 VDBG (0, "unhandled: %u", e->event_type);
2587 events[*num_ev].data.u64 = session_evt_data;
2588 if (EPOLLONESHOT & session_events)
2590 session = vcl_session_get (wrk, sid);
2591 session->vep.ev.events = 0;
2598 vcl_epoll_wait_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
2599 struct epoll_event *events, u32 maxevents,
2600 double wait_for_time, u32 * num_ev)
2602 svm_msg_q_msg_t *msg;
2606 if (vec_len (wrk->mq_msg_vector) && svm_msg_q_is_empty (mq))
2607 goto handle_dequeued;
2609 svm_msg_q_lock (mq);
2610 if (svm_msg_q_is_empty (mq))
2614 svm_msg_q_unlock (mq);
2617 else if (wait_for_time < 0)
2619 svm_msg_q_wait (mq);
2623 if (svm_msg_q_timedwait (mq, wait_for_time / 1e3))
2625 svm_msg_q_unlock (mq);
2630 vcl_mq_dequeue_batch (wrk, mq);
2631 svm_msg_q_unlock (mq);
2634 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
2636 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
2637 e = svm_msg_q_msg_data (mq, msg);
2638 if (*num_ev < maxevents)
2639 vcl_epoll_wait_handle_mq_event (wrk, e, events, num_ev);
2641 vec_add1 (wrk->unhandled_evts_vector, *e);
2642 svm_msg_q_free_msg (mq, msg);
2644 vec_reset_length (wrk->mq_msg_vector);
2645 vcl_handle_pending_wrk_updates (wrk);
2650 vppcom_epoll_wait_condvar (vcl_worker_t * wrk, struct epoll_event *events,
2651 int maxevents, u32 n_evts, double wait_for_time)
2653 vcl_cut_through_registration_t *cr;
2654 double total_wait = 0, wait_slice;
2657 wait_for_time = (wait_for_time == -1) ? (double) 1e6 : wait_for_time;
2658 wait_slice = wrk->cut_through_registrations ? 10e-6 : wait_for_time;
2662 vcl_ct_registration_lock (wrk);
2664 pool_foreach (cr, wrk->cut_through_registrations, ({
2665 vcl_epoll_wait_handle_mq (wrk, cr->mq, events, maxevents, 0, &n_evts);
2668 vcl_ct_registration_unlock (wrk);
2670 rv = vcl_epoll_wait_handle_mq (wrk, wrk->app_event_queue, events,
2671 maxevents, n_evts ? 0 : wait_slice,
2674 total_wait += wait_slice;
2678 while (total_wait < wait_for_time);
2683 vppcom_epoll_wait_eventfd (vcl_worker_t * wrk, struct epoll_event *events,
2684 int maxevents, u32 n_evts, double wait_for_time)
2686 vcl_mq_evt_conn_t *mqc;
2687 int __clib_unused n_read;
2691 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
2693 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
2694 vec_len (wrk->mq_events), wait_for_time);
2695 for (i = 0; i < n_mq_evts; i++)
2697 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
2698 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
2699 vcl_epoll_wait_handle_mq (wrk, mqc->mq, events, maxevents, 0, &n_evts);
2701 if (!n_evts && n_mq_evts > 0)
2704 return (int) n_evts;
2708 vppcom_epoll_wait (uint32_t vep_handle, struct epoll_event *events,
2709 int maxevents, double wait_for_time)
2711 vcl_worker_t *wrk = vcl_worker_get_current ();
2712 vcl_session_t *vep_session;
2716 if (PREDICT_FALSE (maxevents <= 0))
2718 clib_warning ("VCL<%d>: ERROR: Invalid maxevents (%d)!",
2719 getpid (), maxevents);
2720 return VPPCOM_EINVAL;
2723 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
2725 return VPPCOM_EBADFD;
2727 if (PREDICT_FALSE (!vep_session->is_vep))
2729 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
2730 getpid (), vep_handle);
2731 return VPPCOM_EINVAL;
2734 memset (events, 0, sizeof (*events) * maxevents);
2736 if (vec_len (wrk->unhandled_evts_vector))
2738 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
2740 vcl_epoll_wait_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i],
2742 if (n_evts == maxevents)
2749 vec_delete (wrk->unhandled_evts_vector, i, 0);
2752 if (vcm->cfg.use_mq_eventfd)
2753 return vppcom_epoll_wait_eventfd (wrk, events, maxevents, n_evts,
2756 return vppcom_epoll_wait_condvar (wrk, events, maxevents, n_evts,
2761 vppcom_session_attr (uint32_t session_handle, uint32_t op,
2762 void *buffer, uint32_t * buflen)
2764 vcl_worker_t *wrk = vcl_worker_get_current ();
2765 vcl_session_t *session;
2767 u32 *flags = buffer, tmp_flags = 0;
2768 vppcom_endpt_t *ep = buffer;
2770 session = vcl_session_get_w_handle (wrk, session_handle);
2772 return VPPCOM_EBADFD;
2776 case VPPCOM_ATTR_GET_NREAD:
2777 rv = vcl_session_read_ready (session);
2778 VDBG (2, "VPPCOM_ATTR_GET_NREAD: sid %u, nread = %d", rv);
2781 case VPPCOM_ATTR_GET_NWRITE:
2782 rv = vcl_session_write_ready (session);
2783 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_NWRITE: sid %u, nwrite = %d",
2784 getpid (), session_handle, rv);
2787 case VPPCOM_ATTR_GET_FLAGS:
2788 if (PREDICT_TRUE (buffer && buflen && (*buflen >= sizeof (*flags))))
2790 *flags = O_RDWR | (VCL_SESS_ATTR_TEST (session->attr,
2791 VCL_SESS_ATTR_NONBLOCK));
2792 *buflen = sizeof (*flags);
2793 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_FLAGS: sid %u, flags = 0x%08x, "
2794 "is_nonblocking = %u", getpid (),
2795 session_handle, *flags,
2796 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK));
2802 case VPPCOM_ATTR_SET_FLAGS:
2803 if (PREDICT_TRUE (buffer && buflen && (*buflen == sizeof (*flags))))
2805 if (*flags & O_NONBLOCK)
2806 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK);
2808 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_NONBLOCK);
2810 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_FLAGS: sid %u, flags = 0x%08x,"
2811 " is_nonblocking = %u",
2812 getpid (), session_handle, *flags,
2813 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK));
2819 case VPPCOM_ATTR_GET_PEER_ADDR:
2820 if (PREDICT_TRUE (buffer && buflen &&
2821 (*buflen >= sizeof (*ep)) && ep->ip))
2823 ep->is_ip4 = session->transport.is_ip4;
2824 ep->port = session->transport.rmt_port;
2825 if (session->transport.is_ip4)
2826 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
2827 sizeof (ip4_address_t));
2829 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
2830 sizeof (ip6_address_t));
2831 *buflen = sizeof (*ep);
2832 VDBG (1, "VCL<%d>: VPPCOM_ATTR_GET_PEER_ADDR: sid %u, is_ip4 = %u, "
2833 "addr = %U, port %u", getpid (),
2834 session_handle, ep->is_ip4, format_ip46_address,
2835 &session->transport.rmt_ip,
2836 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
2837 clib_net_to_host_u16 (ep->port));
2843 case VPPCOM_ATTR_GET_LCL_ADDR:
2844 if (PREDICT_TRUE (buffer && buflen &&
2845 (*buflen >= sizeof (*ep)) && ep->ip))
2847 ep->is_ip4 = session->transport.is_ip4;
2848 ep->port = session->transport.lcl_port;
2849 if (session->transport.is_ip4)
2850 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip4,
2851 sizeof (ip4_address_t));
2853 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip6,
2854 sizeof (ip6_address_t));
2855 *buflen = sizeof (*ep);
2856 VDBG (1, "VCL<%d>: VPPCOM_ATTR_GET_LCL_ADDR: sid %u, is_ip4 = %u,"
2857 " addr = %U port %d", getpid (),
2858 session_handle, ep->is_ip4, format_ip46_address,
2859 &session->transport.lcl_ip,
2860 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
2861 clib_net_to_host_u16 (ep->port));
2867 case VPPCOM_ATTR_GET_LIBC_EPFD:
2868 rv = session->libc_epfd;
2869 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd %d",
2873 case VPPCOM_ATTR_SET_LIBC_EPFD:
2874 if (PREDICT_TRUE (buffer && buflen &&
2875 (*buflen == sizeof (session->libc_epfd))))
2877 session->libc_epfd = *(int *) buffer;
2878 *buflen = sizeof (session->libc_epfd);
2880 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd %d, "
2881 "buflen %d", getpid (), session->libc_epfd, *buflen);
2887 case VPPCOM_ATTR_GET_PROTOCOL:
2888 if (buffer && buflen && (*buflen >= sizeof (int)))
2890 *(int *) buffer = session->session_type;
2891 *buflen = sizeof (int);
2893 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_PROTOCOL: %d (%s), buflen %d",
2894 getpid (), *(int *) buffer, *(int *) buffer ? "UDP" : "TCP",
2901 case VPPCOM_ATTR_GET_LISTEN:
2902 if (buffer && buflen && (*buflen >= sizeof (int)))
2904 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2905 VCL_SESS_ATTR_LISTEN);
2906 *buflen = sizeof (int);
2908 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_LISTEN: %d, buflen %d",
2909 getpid (), *(int *) buffer, *buflen);
2915 case VPPCOM_ATTR_GET_ERROR:
2916 if (buffer && buflen && (*buflen >= sizeof (int)))
2918 *(int *) buffer = 0;
2919 *buflen = sizeof (int);
2921 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_ERROR: %d, buflen %d, #VPP-TBD#",
2922 getpid (), *(int *) buffer, *buflen);
2928 case VPPCOM_ATTR_GET_TX_FIFO_LEN:
2929 if (buffer && buflen && (*buflen >= sizeof (u32)))
2933 *(size_t *) buffer = (session->sndbuf_size ? session->sndbuf_size :
2934 session->tx_fifo ? session->tx_fifo->nitems :
2935 vcm->cfg.tx_fifo_size);
2936 *buflen = sizeof (u32);
2938 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TX_FIFO_LEN: %u (0x%x), "
2939 "buflen %d, #VPP-TBD#", getpid (),
2940 *(size_t *) buffer, *(size_t *) buffer, *buflen);
2946 case VPPCOM_ATTR_SET_TX_FIFO_LEN:
2947 if (buffer && buflen && (*buflen == sizeof (u32)))
2950 session->sndbuf_size = *(u32 *) buffer;
2951 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TX_FIFO_LEN: %u (0x%x), "
2952 "buflen %d, #VPP-TBD#", getpid (),
2953 session->sndbuf_size, session->sndbuf_size, *buflen);
2959 case VPPCOM_ATTR_GET_RX_FIFO_LEN:
2960 if (buffer && buflen && (*buflen >= sizeof (u32)))
2964 *(size_t *) buffer = (session->rcvbuf_size ? session->rcvbuf_size :
2965 session->rx_fifo ? session->rx_fifo->nitems :
2966 vcm->cfg.rx_fifo_size);
2967 *buflen = sizeof (u32);
2969 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_RX_FIFO_LEN: %u (0x%x), "
2970 "buflen %d, #VPP-TBD#", getpid (),
2971 *(size_t *) buffer, *(size_t *) buffer, *buflen);
2977 case VPPCOM_ATTR_SET_RX_FIFO_LEN:
2978 if (buffer && buflen && (*buflen == sizeof (u32)))
2981 session->rcvbuf_size = *(u32 *) buffer;
2982 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_RX_FIFO_LEN: %u (0x%x), "
2983 "buflen %d, #VPP-TBD#", getpid (),
2984 session->sndbuf_size, session->sndbuf_size, *buflen);
2990 case VPPCOM_ATTR_GET_REUSEADDR:
2991 if (buffer && buflen && (*buflen >= sizeof (int)))
2994 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2995 VCL_SESS_ATTR_REUSEADDR);
2996 *buflen = sizeof (int);
2998 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_REUSEADDR: %d, "
2999 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
3005 case VPPCOM_ATTR_SET_REUSEADDR:
3006 if (buffer && buflen && (*buflen == sizeof (int)) &&
3007 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
3010 if (*(int *) buffer)
3011 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEADDR);
3013 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEADDR);
3015 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_REUSEADDR: %d, buflen %d,"
3016 " #VPP-TBD#", getpid (),
3017 VCL_SESS_ATTR_TEST (session->attr,
3018 VCL_SESS_ATTR_REUSEADDR), *buflen);
3024 case VPPCOM_ATTR_GET_REUSEPORT:
3025 if (buffer && buflen && (*buflen >= sizeof (int)))
3028 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3029 VCL_SESS_ATTR_REUSEPORT);
3030 *buflen = sizeof (int);
3032 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_REUSEPORT: %d, buflen %d,"
3033 " #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
3039 case VPPCOM_ATTR_SET_REUSEPORT:
3040 if (buffer && buflen && (*buflen == sizeof (int)) &&
3041 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
3044 if (*(int *) buffer)
3045 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEPORT);
3047 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEPORT);
3049 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_REUSEPORT: %d, buflen %d,"
3050 " #VPP-TBD#", getpid (),
3051 VCL_SESS_ATTR_TEST (session->attr,
3052 VCL_SESS_ATTR_REUSEPORT), *buflen);
3058 case VPPCOM_ATTR_GET_BROADCAST:
3059 if (buffer && buflen && (*buflen >= sizeof (int)))
3062 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3063 VCL_SESS_ATTR_BROADCAST);
3064 *buflen = sizeof (int);
3066 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_BROADCAST: %d, buflen %d,"
3067 " #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
3073 case VPPCOM_ATTR_SET_BROADCAST:
3074 if (buffer && buflen && (*buflen == sizeof (int)))
3077 if (*(int *) buffer)
3078 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_BROADCAST);
3080 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_BROADCAST);
3082 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_BROADCAST: %d, buflen %d, "
3083 "#VPP-TBD#", getpid (),
3084 VCL_SESS_ATTR_TEST (session->attr,
3085 VCL_SESS_ATTR_BROADCAST), *buflen);
3091 case VPPCOM_ATTR_GET_V6ONLY:
3092 if (buffer && buflen && (*buflen >= sizeof (int)))
3095 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3096 VCL_SESS_ATTR_V6ONLY);
3097 *buflen = sizeof (int);
3099 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_V6ONLY: %d, buflen %d, "
3100 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
3106 case VPPCOM_ATTR_SET_V6ONLY:
3107 if (buffer && buflen && (*buflen == sizeof (int)))
3110 if (*(int *) buffer)
3111 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_V6ONLY);
3113 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_V6ONLY);
3115 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_V6ONLY: %d, buflen %d, "
3116 "#VPP-TBD#", getpid (),
3117 VCL_SESS_ATTR_TEST (session->attr,
3118 VCL_SESS_ATTR_V6ONLY), *buflen);
3124 case VPPCOM_ATTR_GET_KEEPALIVE:
3125 if (buffer && buflen && (*buflen >= sizeof (int)))
3128 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3129 VCL_SESS_ATTR_KEEPALIVE);
3130 *buflen = sizeof (int);
3132 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_KEEPALIVE: %d, buflen %d, "
3133 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
3139 case VPPCOM_ATTR_SET_KEEPALIVE:
3140 if (buffer && buflen && (*buflen == sizeof (int)))
3143 if (*(int *) buffer)
3144 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_KEEPALIVE);
3146 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_KEEPALIVE);
3148 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_KEEPALIVE: %d, buflen %d, "
3149 "#VPP-TBD#", getpid (),
3150 VCL_SESS_ATTR_TEST (session->attr,
3151 VCL_SESS_ATTR_KEEPALIVE), *buflen);
3157 case VPPCOM_ATTR_GET_TCP_NODELAY:
3158 if (buffer && buflen && (*buflen >= sizeof (int)))
3161 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3162 VCL_SESS_ATTR_TCP_NODELAY);
3163 *buflen = sizeof (int);
3165 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_NODELAY: %d, buflen %d, "
3166 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
3172 case VPPCOM_ATTR_SET_TCP_NODELAY:
3173 if (buffer && buflen && (*buflen == sizeof (int)))
3176 if (*(int *) buffer)
3177 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
3179 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
3181 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_NODELAY: %d, buflen %d, "
3182 "#VPP-TBD#", getpid (),
3183 VCL_SESS_ATTR_TEST (session->attr,
3184 VCL_SESS_ATTR_TCP_NODELAY), *buflen);
3190 case VPPCOM_ATTR_GET_TCP_KEEPIDLE:
3191 if (buffer && buflen && (*buflen >= sizeof (int)))
3194 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3195 VCL_SESS_ATTR_TCP_KEEPIDLE);
3196 *buflen = sizeof (int);
3198 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d, buflen %d, "
3199 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
3205 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
3206 if (buffer && buflen && (*buflen == sizeof (int)))
3209 if (*(int *) buffer)
3210 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
3212 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
3214 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d, buflen %d, "
3215 "#VPP-TBD#", getpid (),
3216 VCL_SESS_ATTR_TEST (session->attr,
3217 VCL_SESS_ATTR_TCP_KEEPIDLE), *buflen);
3223 case VPPCOM_ATTR_GET_TCP_KEEPINTVL:
3224 if (buffer && buflen && (*buflen >= sizeof (int)))
3227 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3228 VCL_SESS_ATTR_TCP_KEEPINTVL);
3229 *buflen = sizeof (int);
3231 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_KEEPINTVL: %d, buflen %d, "
3232 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
3238 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
3239 if (buffer && buflen && (*buflen == sizeof (int)))
3242 if (*(int *) buffer)
3243 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
3245 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
3247 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d, buflen %d, "
3248 "#VPP-TBD#", getpid (),
3249 VCL_SESS_ATTR_TEST (session->attr,
3250 VCL_SESS_ATTR_TCP_KEEPINTVL), *buflen);
3256 case VPPCOM_ATTR_GET_TCP_USER_MSS:
3257 if (buffer && buflen && (*buflen >= sizeof (u32)))
3260 *(u32 *) buffer = session->user_mss;
3261 *buflen = sizeof (int);
3263 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_USER_MSS: %d, buflen %d,"
3264 " #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
3270 case VPPCOM_ATTR_SET_TCP_USER_MSS:
3271 if (buffer && buflen && (*buflen == sizeof (u32)))
3274 session->user_mss = *(u32 *) buffer;
3276 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_USER_MSS: %u, buflen %d, "
3277 "#VPP-TBD#", getpid (), session->user_mss, *buflen);
3283 case VPPCOM_ATTR_SET_SHUT:
3284 if (*flags == SHUT_RD || *flags == SHUT_RDWR)
3285 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_SHUT_RD);
3286 if (*flags == SHUT_WR || *flags == SHUT_RDWR)
3287 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_SHUT_WR);
3290 case VPPCOM_ATTR_GET_SHUT:
3291 if (VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_SHUT_RD))
3293 if (VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_SHUT_WR))
3296 *(int *) buffer = SHUT_RD;
3297 else if (tmp_flags == 2)
3298 *(int *) buffer = SHUT_WR;
3299 else if (tmp_flags == 3)
3300 *(int *) buffer = SHUT_RDWR;
3301 *buflen = sizeof (int);
3312 vppcom_session_recvfrom (uint32_t session_handle, void *buffer,
3313 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3315 vcl_worker_t *wrk = vcl_worker_get_current ();
3317 vcl_session_t *session = 0;
3321 session = vcl_session_get_w_handle (wrk, session_handle);
3322 if (PREDICT_FALSE (!session))
3324 VDBG (0, "VCL<%d>: invalid session, sid (%u) has been closed!",
3325 getpid (), session_handle);
3326 return VPPCOM_EBADFD;
3328 ep->is_ip4 = session->transport.is_ip4;
3329 ep->port = session->transport.rmt_port;
3333 rv = vppcom_session_read (session_handle, buffer, buflen);
3334 else if (flags & MSG_PEEK)
3335 rv = vppcom_session_peek (session_handle, buffer, buflen);
3338 VDBG (0, "Unsupport flags for recvfrom %d", flags);
3339 return VPPCOM_EAFNOSUPPORT;
3344 if (session->transport.is_ip4)
3345 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
3346 sizeof (ip4_address_t));
3348 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
3349 sizeof (ip6_address_t));
3356 vppcom_session_sendto (uint32_t session_handle, void *buffer,
3357 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3360 return VPPCOM_EINVAL;
3365 return VPPCOM_EINVAL;
3370 // TBD check the flags and do the right thing
3371 VDBG (2, "VCL<%d>: handling flags 0x%u (%d) not implemented yet.",
3372 getpid (), flags, flags);
3375 return (vppcom_session_write_inline (session_handle, buffer, buflen, 1));
3379 vppcom_poll (vcl_poll_t * vp, uint32_t n_sids, double wait_for_time)
3381 vcl_worker_t *wrk = vcl_worker_get_current ();
3382 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
3383 u32 i, keep_trying = 1;
3384 svm_msg_q_msg_t msg;
3388 VDBG (3, "VCL<%d>: vp %p, nsids %u, wait_for_time %f",
3389 getpid (), vp, n_sids, wait_for_time);
3392 return VPPCOM_EFAULT;
3396 vcl_session_t *session;
3398 /* Dequeue all events and drop all unhandled io events */
3399 while (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0) == 0)
3401 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
3402 vcl_handle_mq_event (wrk, e);
3403 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
3405 vec_reset_length (wrk->unhandled_evts_vector);
3407 for (i = 0; i < n_sids; i++)
3409 session = vcl_session_get (wrk, vp[i].sh);
3412 vp[i].revents = POLLHUP;
3419 if (POLLIN & vp[i].events)
3421 rv = vcl_session_read_ready (session);
3424 vp[i].revents |= POLLIN;
3431 case VPPCOM_ECONNRESET:
3432 vp[i].revents = POLLHUP;
3436 vp[i].revents = POLLERR;
3443 if (POLLOUT & vp[i].events)
3445 rv = vcl_session_write_ready (session);
3448 vp[i].revents |= POLLOUT;
3455 case VPPCOM_ECONNRESET:
3456 vp[i].revents = POLLHUP;
3460 vp[i].revents = POLLERR;
3467 if (0) // Note "done:" label used by VCL_SESSION_LOCK_AND_GET()
3469 vp[i].revents = POLLNVAL;
3473 if (wait_for_time != -1)
3474 keep_trying = (clib_time_now (&wrk->clib_time) <= timeout) ? 1 : 0;
3476 while ((num_ev == 0) && keep_trying);
3478 if (VPPCOM_DEBUG > 3)
3480 clib_warning ("VCL<%d>: returning %d", getpid (), num_ev);
3481 for (i = 0; i < n_sids; i++)
3483 clib_warning ("VCL<%d>: vp[%d].sid %d (0x%x), .events 0x%x, "
3484 ".revents 0x%x", getpid (), i, vp[i].sh, vp[i].sh,
3485 vp[i].events, vp[i].revents);
3492 vppcom_mq_epoll_fd (void)
3494 vcl_worker_t *wrk = vcl_worker_get_current ();
3495 return wrk->mqs_epfd;
3499 vppcom_session_index (vcl_session_handle_t session_handle)
3501 return session_handle & 0xFFFFFF;
3505 vppcom_session_worker (vcl_session_handle_t session_handle)
3507 return session_handle >> 24;
3511 vppcom_worker_register (void)
3513 if (!vcl_worker_alloc_and_init ())
3514 return VPPCOM_EEXIST;
3516 if (vcl_worker_set_bapi ())
3517 return VPPCOM_EEXIST;
3519 if (vcl_worker_register_with_vpp ())
3520 return VPPCOM_EEXIST;
3526 vppcom_worker_index (void)
3528 return vcl_get_worker_index ();
3532 vppcom_worker_mqs_epfd (void)
3534 vcl_worker_t *wrk = vcl_worker_get_current ();
3535 if (!vcm->cfg.use_mq_eventfd)
3537 return wrk->mqs_epfd;
3541 * fd.io coding-style-patch-verification: ON
3544 * eval: (c-set-style "gnu")