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 (vcl_session_state_t state)
85 case STATE_VPP_CLOSING:
86 st = "STATE_VPP_CLOSING";
89 case STATE_DISCONNECT:
90 st = "STATE_DISCONNECT";
101 case STATE_LISTEN_NO_MQ:
102 st = "STATE_LISTEN_NO_MQ";
106 st = "UNKNOWN_STATE";
114 format_ip4_address (u8 * s, va_list * args)
116 u8 *a = va_arg (*args, u8 *);
117 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
121 format_ip6_address (u8 * s, va_list * args)
123 ip6_address_t *a = va_arg (*args, ip6_address_t *);
124 u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
126 i_max_n_zero = ARRAY_LEN (a->as_u16);
128 i_first_zero = i_max_n_zero;
130 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
132 u32 is_zero = a->as_u16[i] == 0;
133 if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
139 if ((!is_zero && n_zeros > max_n_zeros)
140 || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
142 i_max_n_zero = i_first_zero;
143 max_n_zeros = n_zeros;
144 i_first_zero = ARRAY_LEN (a->as_u16);
149 last_double_colon = 0;
150 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
152 if (i == i_max_n_zero && max_n_zeros > 1)
154 s = format (s, "::");
155 i += max_n_zeros - 1;
156 last_double_colon = 1;
160 s = format (s, "%s%x",
161 (last_double_colon || i == 0) ? "" : ":",
162 clib_net_to_host_u16 (a->as_u16[i]));
163 last_double_colon = 0;
170 /* Format an IP46 address. */
172 format_ip46_address (u8 * s, va_list * args)
174 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
175 ip46_type_t type = va_arg (*args, ip46_type_t);
181 is_ip4 = ip46_address_is_ip4 (ip46);
192 format (s, "%U", format_ip4_address, &ip46->ip4) :
193 format (s, "%U", format_ip6_address, &ip46->ip6);
197 * VPPCOM Utility Functions
202 vcl_send_session_accepted_reply (svm_msg_q_t * mq, u32 context,
203 session_handle_t handle, int retval)
205 app_session_evt_t _app_evt, *app_evt = &_app_evt;
206 session_accepted_reply_msg_t *rmp;
207 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_ACCEPTED_REPLY);
208 rmp = (session_accepted_reply_msg_t *) app_evt->evt->data;
209 rmp->handle = handle;
210 rmp->context = context;
211 rmp->retval = retval;
212 app_send_ctrl_evt_to_vpp (mq, app_evt);
216 vcl_send_session_disconnected_reply (svm_msg_q_t * mq, u32 context,
217 session_handle_t handle, int retval)
219 app_session_evt_t _app_evt, *app_evt = &_app_evt;
220 session_disconnected_reply_msg_t *rmp;
221 app_alloc_ctrl_evt_to_vpp (mq, app_evt,
222 SESSION_CTRL_EVT_DISCONNECTED_REPLY);
223 rmp = (session_disconnected_reply_msg_t *) app_evt->evt->data;
224 rmp->handle = handle;
225 rmp->context = context;
226 rmp->retval = retval;
227 app_send_ctrl_evt_to_vpp (mq, app_evt);
231 vcl_send_session_reset_reply (svm_msg_q_t * mq, u32 context,
232 session_handle_t handle, int retval)
234 app_session_evt_t _app_evt, *app_evt = &_app_evt;
235 session_reset_reply_msg_t *rmp;
236 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_RESET_REPLY);
237 rmp = (session_reset_reply_msg_t *) app_evt->evt->data;
238 rmp->handle = handle;
239 rmp->context = context;
240 rmp->retval = retval;
241 app_send_ctrl_evt_to_vpp (mq, app_evt);
245 vcl_send_session_worker_update (vcl_worker_t * wrk, vcl_session_t * s,
248 app_session_evt_t _app_evt, *app_evt = &_app_evt;
249 session_worker_update_msg_t *mp;
252 mq = vcl_session_vpp_evt_q (wrk, s);
253 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_WORKER_UPDATE);
254 mp = (session_worker_update_msg_t *) app_evt->evt->data;
255 mp->client_index = wrk->my_client_index;
256 mp->handle = s->vpp_handle;
257 mp->req_wrk_index = wrk->vpp_wrk_index;
258 mp->wrk_index = wrk_index;
259 app_send_ctrl_evt_to_vpp (mq, app_evt);
263 vcl_session_accepted_handler (vcl_worker_t * wrk, session_accepted_msg_t * mp)
265 vcl_session_t *session, *listen_session;
266 svm_fifo_t *rx_fifo, *tx_fifo;
270 session = vcl_session_alloc (wrk);
272 listen_session = vcl_session_table_lookup_listener (wrk,
273 mp->listener_handle);
277 evt_q = uword_to_pointer (mp->vpp_event_queue_address, svm_msg_q_t *);
278 clib_warning ("VCL<%d>: ERROR: couldn't find listen session: "
279 "unknown vpp listener handle %llx",
280 getpid (), mp->listener_handle);
281 vcl_send_session_accepted_reply (evt_q, mp->context, mp->handle,
282 VNET_API_ERROR_INVALID_ARGUMENT);
283 vcl_session_free (wrk, session);
284 return VCL_INVALID_SESSION_INDEX;
287 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
288 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
290 if (mp->server_event_queue_address)
292 session->vpp_evt_q = uword_to_pointer (mp->client_event_queue_address,
294 session->our_evt_q = uword_to_pointer (mp->server_event_queue_address,
296 if (vcl_wait_for_segment (mp->segment_handle))
298 clib_warning ("segment for session %u couldn't be mounted!",
299 session->session_index);
300 return VCL_INVALID_SESSION_INDEX;
302 rx_fifo->master_session_index = session->session_index;
303 tx_fifo->master_session_index = session->session_index;
304 rx_fifo->master_thread_index = vcl_get_worker_index ();
305 tx_fifo->master_thread_index = vcl_get_worker_index ();
306 vec_validate (wrk->vpp_event_queues, 0);
307 evt_q = uword_to_pointer (mp->vpp_event_queue_address, svm_msg_q_t *);
308 wrk->vpp_event_queues[0] = evt_q;
312 session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address,
314 rx_fifo->client_session_index = session->session_index;
315 tx_fifo->client_session_index = session->session_index;
316 rx_fifo->client_thread_index = vcl_get_worker_index ();
317 tx_fifo->client_thread_index = vcl_get_worker_index ();
318 vpp_wrk_index = tx_fifo->master_thread_index;
319 vec_validate (wrk->vpp_event_queues, vpp_wrk_index);
320 wrk->vpp_event_queues[vpp_wrk_index] = session->vpp_evt_q;
323 session->vpp_handle = mp->handle;
324 session->vpp_thread_index = rx_fifo->master_thread_index;
325 session->client_context = mp->context;
326 session->rx_fifo = rx_fifo;
327 session->tx_fifo = tx_fifo;
329 session->session_state = STATE_ACCEPT;
330 session->transport.rmt_port = mp->port;
331 session->transport.is_ip4 = mp->is_ip4;
332 clib_memcpy_fast (&session->transport.rmt_ip, mp->ip,
333 sizeof (ip46_address_t));
335 vcl_session_table_add_vpp_handle (wrk, mp->handle, session->session_index);
336 session->transport.lcl_port = listen_session->transport.lcl_port;
337 session->transport.lcl_ip = listen_session->transport.lcl_ip;
338 session->session_type = listen_session->session_type;
339 session->is_dgram = session->session_type == VPPCOM_PROTO_UDP;
341 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: client accept request from %s"
342 " address %U port %d queue %p!", getpid (), mp->handle,
343 session->session_index,
344 mp->is_ip4 ? "IPv4" : "IPv6", format_ip46_address, &mp->ip,
345 mp->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
346 clib_net_to_host_u16 (mp->port), session->vpp_evt_q);
347 vcl_evt (VCL_EVT_ACCEPT, session, listen_session, session_index);
349 return session->session_index;
353 vcl_session_connected_handler (vcl_worker_t * wrk,
354 session_connected_msg_t * mp)
356 u32 session_index, vpp_wrk_index;
357 svm_fifo_t *rx_fifo, *tx_fifo;
358 vcl_session_t *session = 0;
361 session_index = mp->context;
362 session = vcl_session_get (wrk, session_index);
365 clib_warning ("[%s] ERROR: vpp handle 0x%llx, sid %u: "
366 "Invalid session index (%u)!",
367 getpid (), mp->handle, session_index);
368 return VCL_INVALID_SESSION_INDEX;
372 clib_warning ("VCL<%d>: ERROR: sid %u: connect failed! %U", getpid (),
373 session_index, format_api_error, ntohl (mp->retval));
374 session->session_state = STATE_FAILED;
375 session->vpp_handle = mp->handle;
376 return session_index;
379 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
380 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
381 if (vcl_wait_for_segment (mp->segment_handle))
383 clib_warning ("segment for session %u couldn't be mounted!",
384 session->session_index);
385 return VCL_INVALID_SESSION_INDEX;
388 rx_fifo->client_session_index = session_index;
389 tx_fifo->client_session_index = session_index;
390 rx_fifo->client_thread_index = vcl_get_worker_index ();
391 tx_fifo->client_thread_index = vcl_get_worker_index ();
393 if (mp->client_event_queue_address)
395 session->vpp_evt_q = uword_to_pointer (mp->server_event_queue_address,
397 session->our_evt_q = uword_to_pointer (mp->client_event_queue_address,
400 vec_validate (wrk->vpp_event_queues, 0);
401 evt_q = uword_to_pointer (mp->vpp_event_queue_address, svm_msg_q_t *);
402 wrk->vpp_event_queues[0] = evt_q;
406 session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address,
408 vpp_wrk_index = tx_fifo->master_thread_index;
409 vec_validate (wrk->vpp_event_queues, vpp_wrk_index);
410 wrk->vpp_event_queues[vpp_wrk_index] = session->vpp_evt_q;
413 session->rx_fifo = rx_fifo;
414 session->tx_fifo = tx_fifo;
415 session->vpp_handle = mp->handle;
416 session->vpp_thread_index = rx_fifo->master_thread_index;
417 session->transport.is_ip4 = mp->is_ip4;
418 clib_memcpy_fast (&session->transport.lcl_ip, mp->lcl_ip,
419 sizeof (session->transport.lcl_ip));
420 session->transport.lcl_port = mp->lcl_port;
421 session->session_state = STATE_CONNECT;
423 /* Add it to lookup table */
424 vcl_session_table_add_vpp_handle (wrk, mp->handle, session_index);
426 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: connect succeeded! "
427 "session_rx_fifo %p, refcnt %d, session_tx_fifo %p, refcnt %d",
428 getpid (), mp->handle, session_index, session->rx_fifo,
429 session->rx_fifo->refcnt, session->tx_fifo, session->tx_fifo->refcnt);
431 return session_index;
435 vcl_flag_accepted_session (vcl_session_t * session, u64 handle, u32 flags)
437 vcl_session_msg_t *accepted_msg;
440 for (i = 0; i < vec_len (session->accept_evts_fifo); i++)
442 accepted_msg = &session->accept_evts_fifo[i];
443 if (accepted_msg->accepted_msg.handle == handle)
445 accepted_msg->flags |= flags;
453 vcl_session_reset_handler (vcl_worker_t * wrk,
454 session_reset_msg_t * reset_msg)
456 vcl_session_t *session;
459 sid = vcl_session_index_from_vpp_handle (wrk, reset_msg->handle);
460 session = vcl_session_get (wrk, sid);
463 VDBG (0, "request to reset unknown handle 0x%llx", reset_msg->handle);
464 return VCL_INVALID_SESSION_INDEX;
467 /* Caught a reset before actually accepting the session */
468 if (session->session_state == STATE_LISTEN)
471 if (!vcl_flag_accepted_session (session, reset_msg->handle,
472 VCL_ACCEPTED_F_RESET))
473 VDBG (0, "session was not accepted!");
474 return VCL_INVALID_SESSION_INDEX;
477 session->session_state = STATE_DISCONNECT;
478 VDBG (0, "reset session %u [0x%llx]", sid, reset_msg->handle);
483 vcl_session_bound_handler (vcl_worker_t * wrk, session_bound_msg_t * mp)
485 vcl_session_t *session;
486 u32 sid = mp->context;
488 session = vcl_session_get (wrk, sid);
491 VERR ("vpp handle 0x%llx, sid %u: bind failed: %U", mp->handle, sid,
492 format_api_error, mp->retval);
495 session->session_state = STATE_FAILED;
496 session->vpp_handle = mp->handle;
501 clib_warning ("[%s] ERROR: vpp handle 0x%llx, sid %u: "
502 "Invalid session index (%u)!",
503 getpid (), mp->handle, sid);
504 return VCL_INVALID_SESSION_INDEX;
508 session->vpp_handle = mp->handle;
509 session->transport.is_ip4 = mp->lcl_is_ip4;
510 clib_memcpy_fast (&session->transport.lcl_ip, mp->lcl_ip,
511 sizeof (ip46_address_t));
512 session->transport.lcl_port = mp->lcl_port;
513 vcl_session_table_add_listener (wrk, mp->handle, sid);
514 session->session_state = STATE_LISTEN;
516 session->vpp_evt_q = uword_to_pointer (mp->vpp_evt_q, svm_msg_q_t *);
517 vec_validate (wrk->vpp_event_queues, 0);
518 wrk->vpp_event_queues[0] = session->vpp_evt_q;
520 if (session->is_dgram)
522 svm_fifo_t *rx_fifo, *tx_fifo;
523 session->vpp_evt_q = uword_to_pointer (mp->vpp_evt_q, svm_msg_q_t *);
524 rx_fifo = uword_to_pointer (mp->rx_fifo, svm_fifo_t *);
525 rx_fifo->client_session_index = sid;
526 tx_fifo = uword_to_pointer (mp->tx_fifo, svm_fifo_t *);
527 tx_fifo->client_session_index = sid;
528 session->rx_fifo = rx_fifo;
529 session->tx_fifo = tx_fifo;
532 VDBG (0, "session %u [0x%llx]: listen succeeded!", sid, mp->handle);
536 static vcl_session_t *
537 vcl_session_accepted (vcl_worker_t * wrk, session_accepted_msg_t * msg)
539 vcl_session_msg_t *vcl_msg;
540 vcl_session_t *session;
542 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
543 if (PREDICT_FALSE (session != 0))
544 VWRN ("session overlap handle %lu state %u!", msg->handle,
545 session->session_state);
547 session = vcl_session_table_lookup_listener (wrk, msg->listener_handle);
550 VERR ("couldn't find listen session: listener handle %llx",
551 msg->listener_handle);
555 clib_fifo_add2 (session->accept_evts_fifo, vcl_msg);
556 vcl_msg->accepted_msg = *msg;
557 /* Session handle points to listener until fully accepted by app */
558 vcl_session_table_add_vpp_handle (wrk, msg->handle, session->session_index);
563 static vcl_session_t *
564 vcl_session_disconnected_handler (vcl_worker_t * wrk,
565 session_disconnected_msg_t * msg)
567 vcl_session_t *session;
569 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
572 VDBG (0, "request to disconnect unknown handle 0x%llx", msg->handle);
576 /* Caught a disconnect before actually accepting the session */
577 if (session->session_state == STATE_LISTEN)
579 if (!vcl_flag_accepted_session (session, msg->handle,
580 VCL_ACCEPTED_F_CLOSED))
581 VDBG (0, "session was not accepted!");
585 session->session_state = STATE_VPP_CLOSING;
590 vcl_session_req_worker_update_handler (vcl_worker_t * wrk, void *data)
592 session_req_worker_update_msg_t *msg;
595 msg = (session_req_worker_update_msg_t *) data;
596 s = vcl_session_get_w_vpp_handle (wrk, msg->session_handle);
600 vec_add1 (wrk->pending_session_wrk_updates, s->session_index);
604 vcl_session_worker_update_reply_handler (vcl_worker_t * wrk, void *data)
606 session_worker_update_reply_msg_t *msg;
609 msg = (session_worker_update_reply_msg_t *) data;
610 s = vcl_session_get_w_vpp_handle (wrk, msg->handle);
613 VDBG (0, "unknown handle 0x%llx", msg->handle);
616 if (vcl_wait_for_segment (msg->segment_handle))
618 clib_warning ("segment for session %u couldn't be mounted!",
625 s->rx_fifo = uword_to_pointer (msg->rx_fifo, svm_fifo_t *);
626 s->tx_fifo = uword_to_pointer (msg->tx_fifo, svm_fifo_t *);
627 s->rx_fifo->client_session_index = s->session_index;
628 s->tx_fifo->client_session_index = s->session_index;
629 s->rx_fifo->client_thread_index = wrk->wrk_index;
630 s->tx_fifo->client_thread_index = wrk->wrk_index;
632 s->session_state = STATE_UPDATED;
634 VDBG (0, "session %u[0x%llx] moved to worker %u", s->session_index,
635 s->vpp_handle, wrk->wrk_index);
639 vcl_handle_mq_event (vcl_worker_t * wrk, session_event_t * e)
641 session_disconnected_msg_t *disconnected_msg;
642 vcl_session_t *session;
644 switch (e->event_type)
646 case FIFO_EVENT_APP_RX:
647 case FIFO_EVENT_APP_TX:
648 case SESSION_IO_EVT_CT_RX:
649 case SESSION_IO_EVT_CT_TX:
650 vec_add1 (wrk->unhandled_evts_vector, *e);
652 case SESSION_CTRL_EVT_ACCEPTED:
653 vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
655 case SESSION_CTRL_EVT_CONNECTED:
656 vcl_session_connected_handler (wrk,
657 (session_connected_msg_t *) e->data);
659 case SESSION_CTRL_EVT_DISCONNECTED:
660 disconnected_msg = (session_disconnected_msg_t *) e->data;
661 session = vcl_session_disconnected_handler (wrk, disconnected_msg);
664 VDBG (0, "disconnected session %u [0x%llx]", session->session_index,
665 session->vpp_handle);
667 case SESSION_CTRL_EVT_RESET:
668 vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
670 case SESSION_CTRL_EVT_BOUND:
671 vcl_session_bound_handler (wrk, (session_bound_msg_t *) e->data);
673 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
674 vcl_session_req_worker_update_handler (wrk, e->data);
676 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
677 vcl_session_worker_update_reply_handler (wrk, e->data);
680 clib_warning ("unhandled %u", e->event_type);
686 vppcom_wait_for_session_state_change (u32 session_index,
687 vcl_session_state_t state,
690 vcl_worker_t *wrk = vcl_worker_get_current ();
691 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
692 vcl_session_t *volatile session;
698 session = vcl_session_get (wrk, session_index);
699 if (PREDICT_FALSE (!session))
701 return VPPCOM_EBADFD;
703 if (session->session_state & state)
707 if (session->session_state & STATE_FAILED)
709 return VPPCOM_ECONNREFUSED;
712 if (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0))
717 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
718 vcl_handle_mq_event (wrk, e);
719 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
721 while (clib_time_now (&wrk->clib_time) < timeout);
723 VDBG (0, "timeout waiting for state 0x%x (%s)", state,
724 vppcom_session_state_str (state));
725 vcl_evt (VCL_EVT_SESSION_TIMEOUT, session, session_state);
727 return VPPCOM_ETIMEDOUT;
731 vcl_handle_pending_wrk_updates (vcl_worker_t * wrk)
733 vcl_session_state_t state;
737 if (PREDICT_TRUE (vec_len (wrk->pending_session_wrk_updates) == 0))
740 vec_foreach (sip, wrk->pending_session_wrk_updates)
742 s = vcl_session_get (wrk, *sip);
743 vcl_send_session_worker_update (wrk, s, wrk->wrk_index);
744 state = s->session_state;
745 vppcom_wait_for_session_state_change (s->session_index, STATE_UPDATED, 5);
746 s->session_state = state;
748 vec_reset_length (wrk->pending_session_wrk_updates);
752 vcl_flush_mq_events (void)
754 vcl_worker_t *wrk = vcl_worker_get_current ();
755 svm_msg_q_msg_t *msg;
760 mq = wrk->app_event_queue;
762 vcl_mq_dequeue_batch (wrk, mq);
763 svm_msg_q_unlock (mq);
765 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
767 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
768 e = svm_msg_q_msg_data (mq, msg);
769 vcl_handle_mq_event (wrk, e);
770 svm_msg_q_free_msg (mq, msg);
772 vec_reset_length (wrk->mq_msg_vector);
773 vcl_handle_pending_wrk_updates (wrk);
777 vppcom_app_session_enable (void)
781 if (vcm->app_state != STATE_APP_ENABLED)
783 vppcom_send_session_enable_disable (1 /* is_enabled == TRUE */ );
784 rv = vcl_wait_for_app_state_change (STATE_APP_ENABLED);
785 if (PREDICT_FALSE (rv))
787 VDBG (0, "VCL<%d>: application session enable timed out! "
788 "returning %d (%s)", getpid (), rv, vppcom_retval_str (rv));
796 vppcom_app_attach (void)
800 vppcom_app_send_attach ();
801 rv = vcl_wait_for_app_state_change (STATE_APP_ATTACHED);
802 if (PREDICT_FALSE (rv))
804 VDBG (0, "VCL<%d>: application attach timed out! returning %d (%s)",
805 getpid (), rv, vppcom_retval_str (rv));
813 vppcom_session_unbind (u32 session_handle)
815 vcl_worker_t *wrk = vcl_worker_get_current ();
816 vcl_session_t *session = 0;
819 session = vcl_session_get_w_handle (wrk, session_handle);
821 return VPPCOM_EBADFD;
823 vpp_handle = session->vpp_handle;
824 vcl_session_table_del_listener (wrk, vpp_handle);
825 session->vpp_handle = ~0;
826 session->session_state = STATE_DISCONNECT;
828 VDBG (1, "vpp handle 0x%llx, sid %u: sending unbind msg! new state"
829 " 0x%x (%s)", vpp_handle, session_handle, STATE_DISCONNECT,
830 vppcom_session_state_str (STATE_DISCONNECT));
831 vcl_evt (VCL_EVT_UNBIND, session);
832 vppcom_send_unbind_sock (wrk, vpp_handle);
838 vppcom_session_disconnect (u32 session_handle)
840 vcl_worker_t *wrk = vcl_worker_get_current ();
841 svm_msg_q_t *vpp_evt_q;
842 vcl_session_t *session;
843 vcl_session_state_t state;
846 session = vcl_session_get_w_handle (wrk, session_handle);
848 return VPPCOM_EBADFD;
850 vpp_handle = session->vpp_handle;
851 state = session->session_state;
853 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u state 0x%x (%s)", getpid (),
854 vpp_handle, session_handle, state, vppcom_session_state_str (state));
856 if (PREDICT_FALSE (state & STATE_LISTEN))
858 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
859 "Cannot disconnect a listen socket!",
860 getpid (), vpp_handle, session_handle);
861 return VPPCOM_EBADFD;
864 if (state & STATE_VPP_CLOSING)
866 vpp_evt_q = vcl_session_vpp_evt_q (wrk, session);
867 vcl_send_session_disconnected_reply (vpp_evt_q, wrk->my_client_index,
869 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: sending disconnect "
870 "REPLY...", getpid (), vpp_handle, session_handle);
874 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: sending disconnect...",
875 getpid (), vpp_handle, session_handle);
876 vppcom_send_disconnect_session (vpp_handle);
885 * Notify vpp of the disconnect and mark the worker as free. If we're the
886 * last worker, do a full cleanup otherwise, since we're probably a forked
887 * child, avoid syscalls as much as possible. We might've lost privileges.
890 vppcom_app_exit (void)
892 if (!pool_elts (vcm->workers))
894 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
895 vcl_set_worker_index (~0);
897 if (vec_len (vcm->workers) == 1)
898 vl_client_disconnect_from_vlib ();
900 vl_client_send_disconnect (1 /* vpp should cleanup */ );
904 * VPPCOM Public API functions
907 vppcom_app_create (char *app_name)
909 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
914 VDBG (1, "already initialized");
915 return VPPCOM_EEXIST;
919 vppcom_cfg (&vcm->cfg);
922 vcm->main_cpu = pthread_self ();
923 vcm->main_pid = getpid ();
924 vcm->app_name = format (0, "%s", app_name);
925 vppcom_init_error_string_table ();
926 svm_fifo_segment_main_init (&vcm->segment_main, vcl_cfg->segment_baseva,
927 20 /* timeout in secs */ );
928 pool_alloc (vcm->workers, vcl_cfg->max_workers);
929 clib_spinlock_init (&vcm->workers_lock);
930 clib_rwlock_init (&vcm->segment_table_lock);
931 atexit (vppcom_app_exit);
933 /* Allocate default worker */
934 vcl_worker_alloc_and_init ();
936 /* API hookup and connect to VPP */
937 vppcom_api_hookup ();
939 vcm->app_state = STATE_APP_START;
940 rv = vppcom_connect_to_vpp (app_name);
943 VERR ("couldn't connect to VPP!");
946 VDBG (0, "sending session enable");
947 rv = vppcom_app_session_enable ();
950 VERR ("vppcom_app_session_enable() failed!");
954 VDBG (0, "sending app attach");
955 rv = vppcom_app_attach ();
958 VERR ("vppcom_app_attach() failed!");
962 VDBG (0, "app_name '%s', my_client_index %d (0x%x)", app_name,
963 vcm->workers[0].my_client_index, vcm->workers[0].my_client_index);
969 vppcom_app_destroy (void)
972 f64 orig_app_timeout;
974 if (!pool_elts (vcm->workers))
977 vcl_evt (VCL_EVT_DETACH, vcm);
979 if (pool_elts (vcm->workers) == 1)
981 vppcom_app_send_detach ();
982 orig_app_timeout = vcm->cfg.app_timeout;
983 vcm->cfg.app_timeout = 2.0;
984 rv = vcl_wait_for_app_state_change (STATE_APP_ENABLED);
985 vcm->cfg.app_timeout = orig_app_timeout;
986 if (PREDICT_FALSE (rv))
987 VDBG (0, "application detach timed out! returning %d (%s)", rv,
988 vppcom_retval_str (rv));
989 vec_free (vcm->app_name);
990 vcl_worker_cleanup (vcl_worker_get_current (), 0 /* notify vpp */ );
994 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
997 vcl_set_worker_index (~0);
999 vl_client_disconnect_from_vlib ();
1003 vppcom_session_create (u8 proto, u8 is_nonblocking)
1005 vcl_worker_t *wrk = vcl_worker_get_current ();
1006 vcl_session_t *session;
1008 session = vcl_session_alloc (wrk);
1010 session->session_type = proto;
1011 session->session_state = STATE_START;
1012 session->vpp_handle = ~0;
1013 session->is_dgram = proto == VPPCOM_PROTO_UDP;
1016 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK);
1018 vcl_evt (VCL_EVT_CREATE, session, session_type, session->session_state,
1019 is_nonblocking, session_index);
1021 VDBG (0, "created sid %u", session->session_index);
1023 return vcl_session_handle (session);
1027 vcl_session_cleanup (vcl_worker_t * wrk, vcl_session_t * session,
1028 vcl_session_handle_t sh, u8 do_disconnect)
1030 vcl_session_state_t state;
1031 u32 next_sh, vep_sh;
1036 is_vep = session->is_vep;
1037 next_sh = session->vep.next_sh;
1038 vep_sh = session->vep.vep_sh;
1039 state = session->session_state;
1040 vpp_handle = session->vpp_handle;
1042 VDBG (1, "session %u [0x%llx] closing", session->session_index, vpp_handle);
1046 while (next_sh != ~0)
1048 rv = vppcom_epoll_ctl (sh, EPOLL_CTL_DEL, next_sh, 0);
1049 if (PREDICT_FALSE (rv < 0))
1050 VDBG (0, "vpp handle 0x%llx, sid %u: EPOLL_CTL_DEL vep_idx %u"
1051 " failed! rv %d (%s)", vpp_handle, next_sh, vep_sh, rv,
1052 vppcom_retval_str (rv));
1054 next_sh = session->vep.next_sh;
1059 if (session->is_vep_session)
1061 rv = vppcom_epoll_ctl (vep_sh, EPOLL_CTL_DEL, sh, 0);
1063 VDBG (0, "session %u [0x%llx]: EPOLL_CTL_DEL vep_idx %u "
1064 "failed! rv %d (%s)", session->session_index, vpp_handle,
1065 vep_sh, rv, vppcom_retval_str (rv));
1070 VDBG (1, "session %u [0x%llx] disconnect skipped",
1071 session->session_index, vpp_handle);
1075 if (state & STATE_LISTEN)
1077 rv = vppcom_session_unbind (sh);
1078 if (PREDICT_FALSE (rv < 0))
1079 VDBG (0, "session %u [0x%llx]: listener unbind failed! "
1080 "rv %d (%s)", session->session_index, vpp_handle, rv,
1081 vppcom_retval_str (rv));
1083 else if (state & STATE_OPEN)
1085 rv = vppcom_session_disconnect (sh);
1086 if (PREDICT_FALSE (rv < 0))
1087 VDBG (0, "ERROR: session %u [0x%llx]: disconnect failed!"
1088 " rv %d (%s)", session->session_index, vpp_handle,
1089 rv, vppcom_retval_str (rv));
1091 else if (state == STATE_DISCONNECT)
1093 svm_msg_q_t *mq = vcl_session_vpp_evt_q (wrk, session);
1094 vcl_send_session_reset_reply (mq, wrk->my_client_index,
1095 session->vpp_handle, 0);
1099 if (vcl_session_is_ct (session))
1101 vcl_cut_through_registration_t *ctr;
1104 mq_addr = pointer_to_uword (session->our_evt_q);
1105 ctr = vcl_ct_registration_lock_and_lookup (wrk, mq_addr);
1107 if (ctr->epoll_evt_conn_index != ~0)
1108 vcl_mq_epoll_del_evfd (wrk, ctr->epoll_evt_conn_index);
1109 VDBG (0, "Removing ct registration %u",
1110 vcl_ct_registration_index (wrk, ctr));
1111 vcl_ct_registration_del (wrk, ctr);
1112 vcl_ct_registration_lookup_del (wrk, mq_addr);
1113 vcl_ct_registration_unlock (wrk);
1116 VDBG (0, "session %u [0x%llx] removed", session->session_index, vpp_handle);
1119 vcl_session_table_del_vpp_handle (wrk, vpp_handle);
1120 vcl_session_free (wrk, session);
1121 vcl_evt (VCL_EVT_CLOSE, session, rv);
1127 vppcom_session_close (uint32_t session_handle)
1129 vcl_worker_t *wrk = vcl_worker_get_current ();
1130 vcl_session_t *session;
1132 session = vcl_session_get_w_handle (wrk, session_handle);
1134 return VPPCOM_EBADFD;
1135 return vcl_session_cleanup (wrk, session, session_handle,
1136 1 /* do_disconnect */ );
1140 vppcom_session_bind (uint32_t session_handle, vppcom_endpt_t * ep)
1142 vcl_worker_t *wrk = vcl_worker_get_current ();
1143 vcl_session_t *session = 0;
1146 return VPPCOM_EINVAL;
1148 session = vcl_session_get_w_handle (wrk, session_handle);
1150 return VPPCOM_EBADFD;
1152 if (session->is_vep)
1154 clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
1155 "bind to an epoll session!", getpid (), session_handle);
1156 return VPPCOM_EBADFD;
1159 session->transport.is_ip4 = ep->is_ip4;
1161 clib_memcpy_fast (&session->transport.lcl_ip.ip4, ep->ip,
1162 sizeof (ip4_address_t));
1164 clib_memcpy_fast (&session->transport.lcl_ip.ip6, ep->ip,
1165 sizeof (ip6_address_t));
1166 session->transport.lcl_port = ep->port;
1168 VDBG (0, "VCL<%d>: sid %u: binding to local %s address %U port %u, "
1169 "proto %s", getpid (), session_handle,
1170 session->transport.is_ip4 ? "IPv4" : "IPv6",
1171 format_ip46_address, &session->transport.lcl_ip,
1172 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
1173 clib_net_to_host_u16 (session->transport.lcl_port),
1174 vppcom_proto_str (session->session_type));
1175 vcl_evt (VCL_EVT_BIND, session);
1177 if (session->session_type == VPPCOM_PROTO_UDP)
1178 vppcom_session_listen (session_handle, 10);
1184 vppcom_session_listen (uint32_t listen_sh, uint32_t q_len)
1186 vcl_worker_t *wrk = vcl_worker_get_current ();
1187 vcl_session_t *listen_session = 0;
1188 u64 listen_vpp_handle;
1191 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
1192 if (!listen_session || listen_session->is_vep)
1193 return VPPCOM_EBADFD;
1195 if (q_len == 0 || q_len == ~0)
1196 q_len = vcm->cfg.listen_queue_size;
1198 listen_vpp_handle = listen_session->vpp_handle;
1199 if (listen_session->session_state & STATE_LISTEN)
1201 VDBG (0, "session %u [0x%llx]: already in listen state!",
1202 listen_sh, listen_vpp_handle);
1206 VDBG (0, "session %u [0x%llx]: sending vpp listen request...",
1207 listen_sh, listen_vpp_handle);
1210 * Send listen request to vpp and wait for reply
1212 vppcom_send_bind_sock (listen_session);
1213 rv = vppcom_wait_for_session_state_change (listen_session->session_index,
1215 vcm->cfg.session_timeout);
1217 if (PREDICT_FALSE (rv))
1219 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
1220 VDBG (0, "session %u [0x%llx]: listen failed! returning %d (%s)",
1221 listen_sh, listen_session->vpp_handle, rv,
1222 vppcom_retval_str (rv));
1230 vppcom_session_tls_add_cert (uint32_t session_handle, char *cert,
1234 vcl_worker_t *wrk = vcl_worker_get_current ();
1235 vcl_session_t *session = 0;
1237 session = vcl_session_get_w_handle (wrk, session_handle);
1239 return VPPCOM_EBADFD;
1241 if (cert_len == 0 || cert_len == ~0)
1242 return VPPCOM_EBADFD;
1245 * Send listen request to vpp and wait for reply
1247 vppcom_send_application_tls_cert_add (session, cert, cert_len);
1254 vppcom_session_tls_add_key (uint32_t session_handle, char *key,
1258 vcl_worker_t *wrk = vcl_worker_get_current ();
1259 vcl_session_t *session = 0;
1261 session = vcl_session_get_w_handle (wrk, session_handle);
1263 return VPPCOM_EBADFD;
1265 if (key_len == 0 || key_len == ~0)
1266 return VPPCOM_EBADFD;
1269 * Send listen request to vpp and wait for reply
1271 vppcom_send_application_tls_key_add (session, key, key_len);
1279 validate_args_session_accept_ (vcl_worker_t * wrk,
1280 vcl_session_t * listen_session)
1282 /* Input validation - expects spinlock on sessions_lockp */
1283 if (listen_session->is_vep)
1285 clib_warning ("VCL<%d>: ERROR: sid %u: cannot accept on an "
1286 "epoll session!", getpid (),
1287 listen_session->session_index);
1288 return VPPCOM_EBADFD;
1291 if (listen_session->session_state != STATE_LISTEN)
1293 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
1294 "not in listen state! state 0x%x (%s)", getpid (),
1295 listen_session->vpp_handle, listen_session->session_index,
1296 listen_session->session_state,
1297 vppcom_session_state_str (listen_session->session_state));
1298 return VPPCOM_EBADFD;
1304 vppcom_session_accept (uint32_t listen_session_handle, vppcom_endpt_t * ep,
1307 u32 client_session_index = ~0, listen_session_index, accept_flags = 0;
1308 vcl_worker_t *wrk = vcl_worker_get_current ();
1309 session_accepted_msg_t accepted_msg;
1310 vcl_session_t *listen_session = 0;
1311 vcl_session_t *client_session = 0;
1312 svm_msg_q_t *vpp_evt_q;
1313 vcl_session_msg_t *evt;
1314 u64 listen_vpp_handle;
1315 svm_msg_q_msg_t msg;
1320 listen_session = vcl_session_get_w_handle (wrk, listen_session_handle);
1321 if (!listen_session)
1322 return VPPCOM_EBADFD;
1324 listen_session_index = listen_session->session_index;
1325 if ((rv = validate_args_session_accept_ (wrk, listen_session)))
1328 if (clib_fifo_elts (listen_session->accept_evts_fifo))
1330 clib_fifo_sub2 (listen_session->accept_evts_fifo, evt);
1331 accept_flags = evt->flags;
1332 accepted_msg = evt->accepted_msg;
1336 is_nonblocking = VCL_SESS_ATTR_TEST (listen_session->attr,
1337 VCL_SESS_ATTR_NONBLOCK);
1338 if (svm_msg_q_is_empty (wrk->app_event_queue) && is_nonblocking)
1339 return VPPCOM_EAGAIN;
1343 if (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_WAIT, 0))
1344 return VPPCOM_EAGAIN;
1346 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
1347 if (e->event_type != SESSION_CTRL_EVT_ACCEPTED)
1349 clib_warning ("discarded event: %u", e->event_type);
1350 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
1353 clib_memcpy_fast (&accepted_msg, e->data, sizeof (accepted_msg));
1354 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
1360 client_session_index = vcl_session_accepted_handler (wrk, &accepted_msg);
1361 listen_session = vcl_session_get (wrk, listen_session_index);
1362 client_session = vcl_session_get (wrk, client_session_index);
1364 if (flags & O_NONBLOCK)
1365 VCL_SESS_ATTR_SET (client_session->attr, VCL_SESS_ATTR_NONBLOCK);
1367 listen_vpp_handle = listen_session->vpp_handle;
1368 VDBG (1, "vpp handle 0x%llx, sid %u: Got a client request! "
1369 "vpp handle 0x%llx, sid %u, flags %d, is_nonblocking %u",
1370 listen_vpp_handle, listen_session_handle,
1371 client_session->vpp_handle, client_session_index,
1372 flags, VCL_SESS_ATTR_TEST (client_session->attr,
1373 VCL_SESS_ATTR_NONBLOCK));
1377 ep->is_ip4 = client_session->transport.is_ip4;
1378 ep->port = client_session->transport.rmt_port;
1379 if (client_session->transport.is_ip4)
1380 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip4,
1381 sizeof (ip4_address_t));
1383 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip6,
1384 sizeof (ip6_address_t));
1387 if (accepted_msg.server_event_queue_address)
1388 vpp_evt_q = uword_to_pointer (accepted_msg.vpp_event_queue_address,
1391 vpp_evt_q = client_session->vpp_evt_q;
1393 vcl_send_session_accepted_reply (vpp_evt_q, client_session->client_context,
1394 client_session->vpp_handle, 0);
1396 VDBG (0, "listener %u [0x%llx] accepted %u [0x%llx] peer: %U:%u "
1397 "local: %U:%u", listen_session_handle, listen_vpp_handle,
1398 client_session_index, client_session->vpp_handle,
1399 format_ip46_address, &client_session->transport.rmt_ip,
1400 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
1401 clib_net_to_host_u16 (client_session->transport.rmt_port),
1402 format_ip46_address, &client_session->transport.lcl_ip,
1403 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
1404 clib_net_to_host_u16 (client_session->transport.lcl_port));
1405 vcl_evt (VCL_EVT_ACCEPT, client_session, listen_session,
1406 client_session_index);
1409 * Session might have been closed already
1413 if (accept_flags & VCL_ACCEPTED_F_CLOSED)
1414 client_session->session_state = STATE_VPP_CLOSING;
1415 else if (accept_flags & VCL_ACCEPTED_F_RESET)
1416 client_session->session_state = STATE_DISCONNECT;
1418 return vcl_session_handle (client_session);
1422 vppcom_session_connect (uint32_t session_handle, vppcom_endpt_t * server_ep)
1424 vcl_worker_t *wrk = vcl_worker_get_current ();
1425 vcl_session_t *session = 0;
1429 session = vcl_session_get_w_handle (wrk, session_handle);
1431 return VPPCOM_EBADFD;
1432 session_index = session->session_index;
1434 if (PREDICT_FALSE (session->is_vep))
1436 clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
1437 "connect on an epoll session!", getpid (),
1439 return VPPCOM_EBADFD;
1442 if (PREDICT_FALSE (session->session_state & CLIENT_STATE_OPEN))
1444 VDBG (0, "session handle %u [0x%llx]: session already "
1445 "connected to %s %U port %d proto %s, state 0x%x (%s)",
1446 session_handle, session->vpp_handle,
1447 session->transport.is_ip4 ? "IPv4" : "IPv6",
1448 format_ip46_address,
1449 &session->transport.rmt_ip, session->transport.is_ip4 ?
1450 IP46_TYPE_IP4 : IP46_TYPE_IP6,
1451 clib_net_to_host_u16 (session->transport.rmt_port),
1452 vppcom_proto_str (session->session_type), session->session_state,
1453 vppcom_session_state_str (session->session_state));
1457 session->transport.is_ip4 = server_ep->is_ip4;
1458 if (session->transport.is_ip4)
1459 clib_memcpy_fast (&session->transport.rmt_ip.ip4, server_ep->ip,
1460 sizeof (ip4_address_t));
1462 clib_memcpy_fast (&session->transport.rmt_ip.ip6, server_ep->ip,
1463 sizeof (ip6_address_t));
1464 session->transport.rmt_port = server_ep->port;
1466 VDBG (0, "session handle %u [0x%llx]: connecting to server %s %U "
1467 "port %d proto %s", session_handle, session->vpp_handle,
1468 session->transport.is_ip4 ? "IPv4" : "IPv6",
1469 format_ip46_address,
1470 &session->transport.rmt_ip, session->transport.is_ip4 ?
1471 IP46_TYPE_IP4 : IP46_TYPE_IP6,
1472 clib_net_to_host_u16 (session->transport.rmt_port),
1473 vppcom_proto_str (session->session_type));
1476 * Send connect request and wait for reply from vpp
1478 vppcom_send_connect_sock (session);
1479 rv = vppcom_wait_for_session_state_change (session_index, STATE_CONNECT,
1480 vcm->cfg.session_timeout);
1482 session = vcl_session_get (wrk, session_index);
1484 if (PREDICT_FALSE (rv))
1486 if (VPPCOM_DEBUG > 0)
1489 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: connect "
1490 "failed! returning %d (%s)", getpid (),
1491 session->vpp_handle, session_handle, rv,
1492 vppcom_retval_str (rv));
1494 clib_warning ("VCL<%d>: no session for sid %u: connect failed! "
1495 "returning %d (%s)", getpid (),
1496 session_handle, rv, vppcom_retval_str (rv));
1500 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: connected!",
1501 getpid (), session->vpp_handle, session_handle);
1507 vcl_is_rx_evt_for_session (session_event_t * e, u32 sid, u8 is_ct)
1510 return (e->event_type == FIFO_EVENT_APP_RX
1511 && e->fifo->client_session_index == sid);
1513 return (e->event_type == SESSION_IO_EVT_CT_TX);
1517 vppcom_session_read_internal (uint32_t session_handle, void *buf, int n,
1520 vcl_worker_t *wrk = vcl_worker_get_current ();
1521 int n_read = 0, is_nonblocking;
1522 vcl_session_t *s = 0;
1523 svm_fifo_t *rx_fifo;
1524 svm_msg_q_msg_t msg;
1529 if (PREDICT_FALSE (!buf))
1530 return VPPCOM_EINVAL;
1532 s = vcl_session_get_w_handle (wrk, session_handle);
1533 if (PREDICT_FALSE (!s || s->is_vep))
1534 return VPPCOM_EBADFD;
1536 if (PREDICT_FALSE (!vcl_session_is_open (s)))
1538 VDBG (0, "session handle %u[0x%llx] is not open! state 0x%x (%s)",
1539 s->session_index, s->vpp_handle, s->session_state,
1540 vppcom_session_state_str (s->session_state));
1541 return vcl_session_closed_error (s);
1544 is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
1545 is_ct = vcl_session_is_ct (s);
1546 mq = is_ct ? s->our_evt_q : wrk->app_event_queue;
1547 rx_fifo = s->rx_fifo;
1550 if (svm_fifo_is_empty (rx_fifo))
1554 svm_fifo_unset_event (rx_fifo);
1555 return VPPCOM_EWOULDBLOCK;
1557 while (svm_fifo_is_empty (rx_fifo))
1559 if (vcl_session_is_closing (s))
1560 return vcl_session_closing_error (s);
1562 svm_fifo_unset_event (rx_fifo);
1563 svm_msg_q_lock (mq);
1564 if (svm_msg_q_is_empty (mq))
1565 svm_msg_q_wait (mq);
1567 svm_msg_q_sub_w_lock (mq, &msg);
1568 e = svm_msg_q_msg_data (mq, &msg);
1569 svm_msg_q_unlock (mq);
1570 if (!vcl_is_rx_evt_for_session (e, s->session_index, is_ct))
1571 vcl_handle_mq_event (wrk, e);
1572 svm_msg_q_free_msg (mq, &msg);
1577 n_read = app_recv_dgram_raw (rx_fifo, buf, n, &s->transport, 0, peek);
1579 n_read = app_recv_stream_raw (rx_fifo, buf, n, 0, peek);
1581 if (svm_fifo_is_empty (rx_fifo))
1582 svm_fifo_unset_event (rx_fifo);
1584 if (is_ct && svm_fifo_needs_tx_ntf (rx_fifo, n_read))
1586 svm_fifo_clear_tx_ntf (s->rx_fifo);
1587 app_send_io_evt_to_vpp (s->vpp_evt_q, s->rx_fifo, SESSION_IO_EVT_CT_RX,
1591 VDBG (2, "vpp handle 0x%llx, sid %u: read %d bytes from (%p)",
1592 s->vpp_handle, session_handle, n_read, rx_fifo);
1598 vppcom_session_read (uint32_t session_handle, void *buf, size_t n)
1600 return (vppcom_session_read_internal (session_handle, buf, n, 0));
1604 vppcom_session_peek (uint32_t session_handle, void *buf, int n)
1606 return (vppcom_session_read_internal (session_handle, buf, n, 1));
1610 vppcom_session_read_segments (uint32_t session_handle,
1611 vppcom_data_segments_t ds)
1613 vcl_worker_t *wrk = vcl_worker_get_current ();
1614 int n_read = 0, is_nonblocking;
1615 vcl_session_t *s = 0;
1616 svm_fifo_t *rx_fifo;
1617 svm_msg_q_msg_t msg;
1622 s = vcl_session_get_w_handle (wrk, session_handle);
1623 if (PREDICT_FALSE (!s || s->is_vep))
1624 return VPPCOM_EBADFD;
1626 if (PREDICT_FALSE (!vcl_session_is_open (s)))
1627 return vcl_session_closed_error (s);
1629 is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
1630 is_ct = vcl_session_is_ct (s);
1631 mq = is_ct ? s->our_evt_q : wrk->app_event_queue;
1632 rx_fifo = s->rx_fifo;
1635 if (svm_fifo_is_empty (rx_fifo))
1639 svm_fifo_unset_event (rx_fifo);
1640 return VPPCOM_EWOULDBLOCK;
1642 while (svm_fifo_is_empty (rx_fifo))
1644 if (vcl_session_is_closing (s))
1645 return vcl_session_closing_error (s);
1647 svm_fifo_unset_event (rx_fifo);
1648 svm_msg_q_lock (mq);
1649 if (svm_msg_q_is_empty (mq))
1650 svm_msg_q_wait (mq);
1652 svm_msg_q_sub_w_lock (mq, &msg);
1653 e = svm_msg_q_msg_data (mq, &msg);
1654 svm_msg_q_unlock (mq);
1655 if (!vcl_is_rx_evt_for_session (e, s->session_index, is_ct))
1656 vcl_handle_mq_event (wrk, e);
1657 svm_msg_q_free_msg (mq, &msg);
1661 n_read = svm_fifo_segments (rx_fifo, (svm_fifo_segment_t *) ds);
1662 svm_fifo_unset_event (rx_fifo);
1664 if (is_ct && n_read + svm_fifo_max_dequeue (rx_fifo) == rx_fifo->nitems)
1666 /* If the peer is not polling send notification */
1667 if (!svm_fifo_has_event (s->rx_fifo))
1668 app_send_io_evt_to_vpp (s->vpp_evt_q, s->rx_fifo,
1669 SESSION_IO_EVT_CT_RX, SVM_Q_WAIT);
1676 vppcom_session_free_segments (uint32_t session_handle,
1677 vppcom_data_segments_t ds)
1679 vcl_worker_t *wrk = vcl_worker_get_current ();
1682 s = vcl_session_get_w_handle (wrk, session_handle);
1683 if (PREDICT_FALSE (!s || s->is_vep))
1686 svm_fifo_segments_free (s->rx_fifo, (svm_fifo_segment_t *) ds);
1690 vppcom_data_segment_copy (void *buf, vppcom_data_segments_t ds, u32 max_bytes)
1692 u32 first_copy = clib_min (ds[0].len, max_bytes);
1693 clib_memcpy_fast (buf, ds[0].data, first_copy);
1694 if (first_copy < max_bytes)
1696 clib_memcpy_fast (buf + first_copy, ds[1].data,
1697 clib_min (ds[1].len, max_bytes - first_copy));
1703 vcl_is_tx_evt_for_session (session_event_t * e, u32 sid, u8 is_ct)
1706 return (e->event_type == FIFO_EVENT_APP_TX
1707 && e->fifo->client_session_index == sid);
1709 return (e->event_type == SESSION_IO_EVT_CT_RX);
1713 vppcom_session_write_inline (uint32_t session_handle, void *buf, size_t n,
1716 vcl_worker_t *wrk = vcl_worker_get_current ();
1717 int n_write, is_nonblocking;
1718 vcl_session_t *s = 0;
1719 svm_fifo_t *tx_fifo = 0;
1720 session_evt_type_t et;
1721 svm_msg_q_msg_t msg;
1726 if (PREDICT_FALSE (!buf))
1727 return VPPCOM_EINVAL;
1729 s = vcl_session_get_w_handle (wrk, session_handle);
1730 if (PREDICT_FALSE (!s))
1731 return VPPCOM_EBADFD;
1733 if (PREDICT_FALSE (s->is_vep))
1735 VDBG (0, "ERROR: session %u [0x%llx]: cannot write to an epoll"
1736 " session!", s->session_index, s->vpp_handle);
1737 return VPPCOM_EBADFD;
1740 if (PREDICT_FALSE (!vcl_session_is_open (s)))
1742 VDBG (1, "session %u [0x%llx]: is not open! state 0x%x (%s)",
1743 s->session_index, s->vpp_handle, s->session_state,
1744 vppcom_session_state_str (s->session_state));
1745 return vcl_session_closed_error (s);;
1748 tx_fifo = s->tx_fifo;
1749 is_ct = vcl_session_is_ct (s);
1750 is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
1751 mq = is_ct ? s->our_evt_q : wrk->app_event_queue;
1752 if (svm_fifo_is_full (tx_fifo))
1756 return VPPCOM_EWOULDBLOCK;
1758 while (svm_fifo_is_full (tx_fifo))
1760 svm_fifo_add_want_tx_ntf (tx_fifo, SVM_FIFO_WANT_TX_NOTIF);
1761 if (vcl_session_is_closing (s))
1762 return vcl_session_closing_error (s);
1763 svm_msg_q_lock (mq);
1764 if (svm_msg_q_is_empty (mq))
1765 svm_msg_q_wait (mq);
1767 svm_msg_q_sub_w_lock (mq, &msg);
1768 e = svm_msg_q_msg_data (mq, &msg);
1769 svm_msg_q_unlock (mq);
1771 if (!vcl_is_tx_evt_for_session (e, s->session_index, is_ct))
1772 vcl_handle_mq_event (wrk, e);
1773 svm_msg_q_free_msg (mq, &msg);
1777 ASSERT (FIFO_EVENT_APP_TX + 1 == SESSION_IO_EVT_CT_TX);
1778 et = FIFO_EVENT_APP_TX + vcl_session_is_ct (s);
1779 if (is_flush && !vcl_session_is_ct (s))
1780 et = SESSION_IO_EVT_TX_FLUSH;
1783 n_write = app_send_dgram_raw (tx_fifo, &s->transport,
1784 s->vpp_evt_q, buf, n, et, SVM_Q_WAIT);
1786 n_write = app_send_stream_raw (tx_fifo, s->vpp_evt_q, buf, n, et,
1789 ASSERT (n_write > 0);
1791 VDBG (2, "session %u [0x%llx]: wrote %d bytes", s->session_index,
1792 s->vpp_handle, n_write);
1798 vppcom_session_write (uint32_t session_handle, void *buf, size_t n)
1800 return vppcom_session_write_inline (session_handle, buf, n,
1805 vppcom_session_write_msg (uint32_t session_handle, void *buf, size_t n)
1807 return vppcom_session_write_inline (session_handle, buf, n,
1812 static vcl_session_t *
1813 vcl_ct_session_get_from_fifo (vcl_worker_t * wrk, svm_fifo_t * f, u8 type)
1816 s = vcl_session_get (wrk, f->client_session_index);
1820 if (type == 0 && s->rx_fifo == f)
1823 if (type == 1 && s->tx_fifo == f)
1826 s = vcl_session_get (wrk, f->master_session_index);
1829 if (type == 0 && s->rx_fifo == f)
1831 if (type == 1 && s->tx_fifo == f)
1837 #define vcl_fifo_rx_evt_valid_or_break(_fifo) \
1838 if (PREDICT_FALSE (svm_fifo_is_empty (_fifo))) \
1840 svm_fifo_unset_event (_fifo); \
1841 if (svm_fifo_is_empty (_fifo)) \
1846 vcl_select_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
1847 unsigned long n_bits, unsigned long *read_map,
1848 unsigned long *write_map,
1849 unsigned long *except_map, u32 * bits_set)
1851 session_disconnected_msg_t *disconnected_msg;
1852 session_connected_msg_t *connected_msg;
1853 vcl_session_t *session;
1856 switch (e->event_type)
1858 case FIFO_EVENT_APP_RX:
1859 vcl_fifo_rx_evt_valid_or_break (e->fifo);
1860 sid = e->fifo->client_session_index;
1861 session = vcl_session_get (wrk, sid);
1864 if (sid < n_bits && read_map)
1866 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
1870 case FIFO_EVENT_APP_TX:
1871 sid = e->fifo->client_session_index;
1872 session = vcl_session_get (wrk, sid);
1875 if (sid < n_bits && write_map)
1877 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
1881 case SESSION_IO_EVT_CT_TX:
1882 vcl_fifo_rx_evt_valid_or_break (e->fifo);
1883 session = vcl_ct_session_get_from_fifo (wrk, e->fifo, 0);
1886 sid = session->session_index;
1887 if (sid < n_bits && read_map)
1889 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
1893 case SESSION_IO_EVT_CT_RX:
1894 session = vcl_ct_session_get_from_fifo (wrk, e->fifo, 1);
1897 sid = session->session_index;
1898 if (sid < n_bits && write_map)
1900 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
1904 case SESSION_CTRL_EVT_ACCEPTED:
1905 session = vcl_session_accepted (wrk,
1906 (session_accepted_msg_t *) e->data);
1909 sid = session->session_index;
1910 if (sid < n_bits && read_map)
1912 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
1916 case SESSION_CTRL_EVT_CONNECTED:
1917 connected_msg = (session_connected_msg_t *) e->data;
1918 vcl_session_connected_handler (wrk, connected_msg);
1920 case SESSION_CTRL_EVT_DISCONNECTED:
1921 disconnected_msg = (session_disconnected_msg_t *) e->data;
1922 session = vcl_session_disconnected_handler (wrk, disconnected_msg);
1925 sid = session->session_index;
1926 if (sid < n_bits && except_map)
1928 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
1932 case SESSION_CTRL_EVT_RESET:
1933 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
1934 if (sid < n_bits && except_map)
1936 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
1940 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
1941 vcl_session_worker_update_reply_handler (wrk, e->data);
1943 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
1944 vcl_session_req_worker_update_handler (wrk, e->data);
1947 clib_warning ("unhandled: %u", e->event_type);
1953 vcl_select_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
1954 unsigned long n_bits, unsigned long *read_map,
1955 unsigned long *write_map, unsigned long *except_map,
1956 double time_to_wait, u32 * bits_set)
1958 svm_msg_q_msg_t *msg;
1962 svm_msg_q_lock (mq);
1963 if (svm_msg_q_is_empty (mq))
1967 svm_msg_q_unlock (mq);
1973 svm_msg_q_unlock (mq);
1976 else if (time_to_wait < 0)
1978 svm_msg_q_wait (mq);
1982 if (svm_msg_q_timedwait (mq, time_to_wait))
1984 svm_msg_q_unlock (mq);
1989 vcl_mq_dequeue_batch (wrk, mq);
1990 svm_msg_q_unlock (mq);
1992 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
1994 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
1995 e = svm_msg_q_msg_data (mq, msg);
1996 vcl_select_handle_mq_event (wrk, e, n_bits, read_map, write_map,
1997 except_map, bits_set);
1998 svm_msg_q_free_msg (mq, msg);
2000 vec_reset_length (wrk->mq_msg_vector);
2001 vcl_handle_pending_wrk_updates (wrk);
2006 vppcom_select_condvar (vcl_worker_t * wrk, int n_bits,
2007 vcl_si_set * read_map, vcl_si_set * write_map,
2008 vcl_si_set * except_map, double time_to_wait,
2011 double total_wait = 0, wait_slice;
2012 vcl_cut_through_registration_t *cr;
2014 time_to_wait = (time_to_wait == -1) ? 1e6 : time_to_wait;
2015 wait_slice = wrk->cut_through_registrations ? 10e-6 : time_to_wait;
2018 vcl_ct_registration_lock (wrk);
2020 pool_foreach (cr, wrk->cut_through_registrations, ({
2021 vcl_select_handle_mq (wrk, cr->mq, n_bits, read_map, write_map, except_map,
2025 vcl_ct_registration_unlock (wrk);
2027 vcl_select_handle_mq (wrk, wrk->app_event_queue, n_bits, read_map,
2028 write_map, except_map, wait_slice, bits_set);
2029 total_wait += wait_slice;
2033 while (total_wait < time_to_wait);
2039 vppcom_select_eventfd (vcl_worker_t * wrk, int n_bits,
2040 vcl_si_set * read_map, vcl_si_set * write_map,
2041 vcl_si_set * except_map, double time_to_wait,
2044 vcl_mq_evt_conn_t *mqc;
2045 int __clib_unused n_read;
2049 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
2050 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
2051 vec_len (wrk->mq_events), time_to_wait);
2052 for (i = 0; i < n_mq_evts; i++)
2054 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
2055 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
2056 vcl_select_handle_mq (wrk, mqc->mq, n_bits, read_map, write_map,
2057 except_map, 0, bits_set);
2060 return (n_mq_evts > 0 ? (int) *bits_set : 0);
2064 vppcom_select (int n_bits, vcl_si_set * read_map, vcl_si_set * write_map,
2065 vcl_si_set * except_map, double time_to_wait)
2067 u32 sid, minbits = clib_max (n_bits, BITS (uword)), bits_set = 0;
2068 vcl_worker_t *wrk = vcl_worker_get_current ();
2069 vcl_session_t *session = 0;
2072 if (n_bits && read_map)
2074 clib_bitmap_validate (wrk->rd_bitmap, minbits);
2075 clib_memcpy_fast (wrk->rd_bitmap, read_map,
2076 vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
2077 memset (read_map, 0, vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
2079 if (n_bits && write_map)
2081 clib_bitmap_validate (wrk->wr_bitmap, minbits);
2082 clib_memcpy_fast (wrk->wr_bitmap, write_map,
2083 vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
2084 memset (write_map, 0, vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
2086 if (n_bits && except_map)
2088 clib_bitmap_validate (wrk->ex_bitmap, minbits);
2089 clib_memcpy_fast (wrk->ex_bitmap, except_map,
2090 vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
2091 memset (except_map, 0, vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
2101 clib_bitmap_foreach (sid, wrk->wr_bitmap, ({
2102 if (!(session = vcl_session_get (wrk, sid)))
2104 if (except_map && sid < minbits)
2105 clib_bitmap_set_no_check (except_map, sid, 1);
2109 rv = svm_fifo_is_full (session->tx_fifo);
2112 clib_bitmap_set_no_check ((uword*)write_map, sid, 1);
2116 svm_fifo_add_want_tx_ntf (session->tx_fifo, SVM_FIFO_WANT_TX_NOTIF);
2123 clib_bitmap_foreach (sid, wrk->rd_bitmap, ({
2124 if (!(session = vcl_session_get (wrk, sid)))
2126 if (except_map && sid < minbits)
2127 clib_bitmap_set_no_check (except_map, sid, 1);
2131 rv = vcl_session_read_ready (session);
2134 clib_bitmap_set_no_check ((uword*)read_map, sid, 1);
2142 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
2144 vcl_select_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i], n_bits,
2145 read_map, write_map, except_map, &bits_set);
2147 vec_reset_length (wrk->unhandled_evts_vector);
2149 if (vcm->cfg.use_mq_eventfd)
2150 vppcom_select_eventfd (wrk, n_bits, read_map, write_map, except_map,
2151 time_to_wait, &bits_set);
2153 vppcom_select_condvar (wrk, n_bits, read_map, write_map, except_map,
2154 time_to_wait, &bits_set);
2160 vep_verify_epoll_chain (vcl_worker_t * wrk, u32 vep_idx)
2162 vcl_session_t *session;
2163 vppcom_epoll_t *vep;
2166 if (VPPCOM_DEBUG <= 1)
2169 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
2170 session = vcl_session_get (wrk, vep_idx);
2171 if (PREDICT_FALSE (!session))
2173 clib_warning ("VCL<%d>: ERROR: Invalid vep_idx (%u)!",
2174 getpid (), vep_idx);
2177 if (PREDICT_FALSE (!session->is_vep))
2179 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
2180 getpid (), vep_idx);
2183 vep = &session->vep;
2184 clib_warning ("VCL<%d>: vep_idx (%u): Dumping epoll chain\n"
2187 " is_vep_session = %u\n"
2188 " next_sid = 0x%x (%u)\n"
2189 " wait_cont_idx = 0x%x (%u)\n"
2190 "}\n", getpid (), vep_idx,
2191 session->is_vep, session->is_vep_session,
2192 vep->next_sh, vep->next_sh,
2193 session->wait_cont_idx, session->wait_cont_idx);
2195 for (sid = vep->next_sh; sid != ~0; sid = vep->next_sh)
2197 session = vcl_session_get (wrk, sid);
2198 if (PREDICT_FALSE (!session))
2200 clib_warning ("VCL<%d>: ERROR: Invalid sid (%u)!", getpid (), sid);
2203 if (PREDICT_FALSE (session->is_vep))
2204 clib_warning ("VCL<%d>: ERROR: sid (%u) is a vep!",
2205 getpid (), vep_idx);
2206 else if (PREDICT_FALSE (!session->is_vep_session))
2208 clib_warning ("VCL<%d>: ERROR: session (%u) "
2209 "is not a vep session!", getpid (), sid);
2212 vep = &session->vep;
2213 if (PREDICT_FALSE (vep->vep_sh != vep_idx))
2214 clib_warning ("VCL<%d>: ERROR: session (%u) vep_idx (%u) != "
2215 "vep_idx (%u)!", getpid (),
2216 sid, session->vep.vep_sh, vep_idx);
2217 if (session->is_vep_session)
2219 clib_warning ("vep_idx[%u]: sid 0x%x (%u)\n"
2221 " next_sid = 0x%x (%u)\n"
2222 " prev_sid = 0x%x (%u)\n"
2223 " vep_idx = 0x%x (%u)\n"
2224 " ev.events = 0x%x\n"
2225 " ev.data.u64 = 0x%llx\n"
2229 vep->next_sh, vep->next_sh,
2230 vep->prev_sh, vep->prev_sh,
2231 vep->vep_sh, vep->vep_sh,
2232 vep->ev.events, vep->ev.data.u64, vep->et_mask);
2237 clib_warning ("VCL<%d>: vep_idx (%u): Dump complete!\n",
2238 getpid (), vep_idx);
2242 vppcom_epoll_create (void)
2244 vcl_worker_t *wrk = vcl_worker_get_current ();
2245 vcl_session_t *vep_session;
2247 vep_session = vcl_session_alloc (wrk);
2249 vep_session->is_vep = 1;
2250 vep_session->vep.vep_sh = ~0;
2251 vep_session->vep.next_sh = ~0;
2252 vep_session->vep.prev_sh = ~0;
2253 vep_session->wait_cont_idx = ~0;
2254 vep_session->vpp_handle = ~0;
2256 vcl_evt (VCL_EVT_EPOLL_CREATE, vep_session, vep_session->session_index);
2257 VDBG (0, "Created vep_idx %u", vep_session->session_index);
2259 return vcl_session_handle (vep_session);
2263 vppcom_epoll_ctl (uint32_t vep_handle, int op, uint32_t session_handle,
2264 struct epoll_event *event)
2266 vcl_worker_t *wrk = vcl_worker_get_current ();
2267 vcl_session_t *vep_session;
2268 vcl_session_t *session;
2271 if (vep_handle == session_handle)
2273 VDBG (0, "vep_sh == session handle (%u)!", vep_handle);
2274 return VPPCOM_EINVAL;
2277 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
2278 if (PREDICT_FALSE (!vep_session))
2280 VDBG (0, "Invalid vep_sh (%u)!", vep_handle);
2281 return VPPCOM_EBADFD;
2283 if (PREDICT_FALSE (!vep_session->is_vep))
2285 VDBG (0, "vep_sh (%u) is not a vep!", vep_handle);
2286 return VPPCOM_EINVAL;
2289 ASSERT (vep_session->vep.vep_sh == ~0);
2290 ASSERT (vep_session->vep.prev_sh == ~0);
2292 session = vcl_session_get_w_handle (wrk, session_handle);
2293 if (PREDICT_FALSE (!session))
2295 VDBG (0, "Invalid session_handle (%u)!", session_handle);
2296 return VPPCOM_EBADFD;
2298 if (PREDICT_FALSE (session->is_vep))
2300 VDBG (0, "session_handle (%u) is a vep!", vep_handle);
2301 return VPPCOM_EINVAL;
2307 if (PREDICT_FALSE (!event))
2309 VDBG (0, "EPOLL_CTL_ADD: NULL pointer to epoll_event structure!");
2310 return VPPCOM_EINVAL;
2312 if (vep_session->vep.next_sh != ~0)
2314 vcl_session_t *next_session;
2315 next_session = vcl_session_get_w_handle (wrk,
2316 vep_session->vep.next_sh);
2317 if (PREDICT_FALSE (!next_session))
2319 VDBG (0, "EPOLL_CTL_ADD: Invalid vep.next_sid (%u) on "
2320 "vep_idx (%u)!", vep_session->vep.next_sh, vep_handle);
2321 return VPPCOM_EBADFD;
2323 ASSERT (next_session->vep.prev_sh == vep_handle);
2324 next_session->vep.prev_sh = session_handle;
2326 session->vep.next_sh = vep_session->vep.next_sh;
2327 session->vep.prev_sh = vep_handle;
2328 session->vep.vep_sh = vep_handle;
2329 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
2330 session->vep.ev = *event;
2331 session->is_vep = 0;
2332 session->is_vep_session = 1;
2333 vep_session->vep.next_sh = session_handle;
2335 if (session->tx_fifo)
2336 svm_fifo_add_want_tx_ntf (session->tx_fifo,
2337 SVM_FIFO_WANT_TX_NOTIF_IF_FULL);
2339 VDBG (1, "EPOLL_CTL_ADD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2340 vep_handle, session_handle, event->events, event->data.u64);
2341 vcl_evt (VCL_EVT_EPOLL_CTLADD, session, event->events, event->data.u64);
2345 if (PREDICT_FALSE (!event))
2347 VDBG (0, "EPOLL_CTL_MOD: NULL pointer to epoll_event structure!");
2351 else if (PREDICT_FALSE (!session->is_vep_session))
2353 VDBG (0, "sid %u EPOLL_CTL_MOD: not a vep session!",
2358 else if (PREDICT_FALSE (session->vep.vep_sh != vep_handle))
2360 VDBG (0, "EPOLL_CTL_MOD: sh %u vep_sh (%u) != vep_sh (%u)!",
2361 session_handle, session->vep.vep_sh, vep_handle);
2365 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
2366 session->vep.ev = *event;
2367 VDBG (1, "EPOLL_CTL_MOD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2368 vep_handle, session_handle, event->events, event->data.u64);
2372 if (PREDICT_FALSE (!session->is_vep_session))
2374 VDBG (0, "EPOLL_CTL_DEL: %u not a vep session!", session_handle);
2378 else if (PREDICT_FALSE (session->vep.vep_sh != vep_handle))
2380 VDBG (0, "EPOLL_CTL_DEL: sh %u vep_sh (%u) != vep_sh (%u)!",
2381 session_handle, session->vep.vep_sh, vep_handle);
2386 vep_session->wait_cont_idx =
2387 (vep_session->wait_cont_idx == session_handle) ?
2388 session->vep.next_sh : vep_session->wait_cont_idx;
2390 if (session->vep.prev_sh == vep_handle)
2391 vep_session->vep.next_sh = session->vep.next_sh;
2394 vcl_session_t *prev_session;
2395 prev_session = vcl_session_get_w_handle (wrk, session->vep.prev_sh);
2396 if (PREDICT_FALSE (!prev_session))
2398 VDBG (0, "EPOLL_CTL_DEL: Invalid prev_sid (%u) on sid (%u)!",
2399 session->vep.prev_sh, session_handle);
2400 return VPPCOM_EBADFD;
2402 ASSERT (prev_session->vep.next_sh == session_handle);
2403 prev_session->vep.next_sh = session->vep.next_sh;
2405 if (session->vep.next_sh != ~0)
2407 vcl_session_t *next_session;
2408 next_session = vcl_session_get_w_handle (wrk, session->vep.next_sh);
2409 if (PREDICT_FALSE (!next_session))
2411 VDBG (0, "EPOLL_CTL_DEL: Invalid next_sid (%u) on sid (%u)!",
2412 session->vep.next_sh, session_handle);
2413 return VPPCOM_EBADFD;
2415 ASSERT (next_session->vep.prev_sh == session_handle);
2416 next_session->vep.prev_sh = session->vep.prev_sh;
2419 memset (&session->vep, 0, sizeof (session->vep));
2420 session->vep.next_sh = ~0;
2421 session->vep.prev_sh = ~0;
2422 session->vep.vep_sh = ~0;
2423 session->is_vep_session = 0;
2425 if (session->tx_fifo)
2426 svm_fifo_del_want_tx_ntf (session->tx_fifo, SVM_FIFO_NO_TX_NOTIF);
2428 VDBG (1, "EPOLL_CTL_DEL: vep_idx %u, sid %u!", vep_handle,
2430 vcl_evt (VCL_EVT_EPOLL_CTLDEL, session, vep_sh);
2434 VDBG (0, "Invalid operation (%d)!", op);
2438 vep_verify_epoll_chain (wrk, vep_handle);
2445 vcl_epoll_wait_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
2446 struct epoll_event *events, u32 * num_ev)
2448 session_disconnected_msg_t *disconnected_msg;
2449 session_connected_msg_t *connected_msg;
2450 u32 sid = ~0, session_events;
2451 u64 session_evt_data = ~0;
2452 vcl_session_t *session;
2455 switch (e->event_type)
2457 case FIFO_EVENT_APP_RX:
2458 ASSERT (e->fifo->client_thread_index == vcl_get_worker_index ());
2459 vcl_fifo_rx_evt_valid_or_break (e->fifo);
2460 sid = e->fifo->client_session_index;
2461 if (!(session = vcl_session_get (wrk, sid)))
2463 session_events = session->vep.ev.events;
2464 if (!(EPOLLIN & session->vep.ev.events) || session->has_rx_evt)
2467 events[*num_ev].events |= EPOLLIN;
2468 session_evt_data = session->vep.ev.data.u64;
2469 session->has_rx_evt = 1;
2471 case FIFO_EVENT_APP_TX:
2472 sid = e->fifo->client_session_index;
2473 if (!(session = vcl_session_get (wrk, sid)))
2475 session_events = session->vep.ev.events;
2476 if (!(EPOLLOUT & session_events))
2479 events[*num_ev].events |= EPOLLOUT;
2480 session_evt_data = session->vep.ev.data.u64;
2481 svm_fifo_reset_tx_ntf (session->tx_fifo);
2483 case SESSION_IO_EVT_CT_TX:
2484 vcl_fifo_rx_evt_valid_or_break (e->fifo);
2485 session = vcl_ct_session_get_from_fifo (wrk, e->fifo, 0);
2486 if (PREDICT_FALSE (!session))
2488 sid = session->session_index;
2489 session_events = session->vep.ev.events;
2490 if (!(EPOLLIN & session->vep.ev.events) || session->has_rx_evt)
2493 events[*num_ev].events |= EPOLLIN;
2494 session_evt_data = session->vep.ev.data.u64;
2495 session->has_rx_evt = 1;
2497 case SESSION_IO_EVT_CT_RX:
2498 session = vcl_ct_session_get_from_fifo (wrk, e->fifo, 1);
2499 if (PREDICT_FALSE (!session))
2501 sid = session->session_index;
2502 session_events = session->vep.ev.events;
2503 if (!(EPOLLOUT & session_events))
2506 events[*num_ev].events |= EPOLLOUT;
2507 session_evt_data = session->vep.ev.data.u64;
2508 svm_fifo_reset_tx_ntf (session->tx_fifo);
2510 case SESSION_CTRL_EVT_ACCEPTED:
2511 session = vcl_session_accepted (wrk,
2512 (session_accepted_msg_t *) e->data);
2516 session_events = session->vep.ev.events;
2517 if (!(EPOLLIN & session_events))
2521 events[*num_ev].events |= EPOLLIN;
2522 session_evt_data = session->vep.ev.data.u64;
2524 case SESSION_CTRL_EVT_CONNECTED:
2525 connected_msg = (session_connected_msg_t *) e->data;
2526 vcl_session_connected_handler (wrk, connected_msg);
2527 /* Generate EPOLLOUT because there's no connected event */
2528 sid = vcl_session_index_from_vpp_handle (wrk, connected_msg->handle);
2529 if (!(session = vcl_session_get (wrk, sid)))
2531 session_events = session->vep.ev.events;
2532 if (!(EPOLLOUT & session_events))
2535 events[*num_ev].events |= EPOLLOUT;
2536 session_evt_data = session->vep.ev.data.u64;
2538 case SESSION_CTRL_EVT_DISCONNECTED:
2539 disconnected_msg = (session_disconnected_msg_t *) e->data;
2540 session = vcl_session_disconnected_handler (wrk, disconnected_msg);
2543 session_events = session->vep.ev.events;
2544 if (!((EPOLLHUP | EPOLLRDHUP) & session_events))
2547 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
2548 session_evt_data = session->vep.ev.data.u64;
2550 case SESSION_CTRL_EVT_RESET:
2551 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
2552 if (!(session = vcl_session_get (wrk, sid)))
2554 session_events = session->vep.ev.events;
2555 if (!((EPOLLHUP | EPOLLRDHUP) & session_events))
2558 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
2559 session_evt_data = session->vep.ev.data.u64;
2561 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
2562 vcl_session_req_worker_update_handler (wrk, e->data);
2564 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
2565 vcl_session_worker_update_reply_handler (wrk, e->data);
2568 VDBG (0, "unhandled: %u", e->event_type);
2574 events[*num_ev].data.u64 = session_evt_data;
2575 if (EPOLLONESHOT & session_events)
2577 session = vcl_session_get (wrk, sid);
2578 session->vep.ev.events = 0;
2585 vcl_epoll_wait_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
2586 struct epoll_event *events, u32 maxevents,
2587 double wait_for_time, u32 * num_ev)
2589 svm_msg_q_msg_t *msg;
2593 if (vec_len (wrk->mq_msg_vector) && svm_msg_q_is_empty (mq))
2594 goto handle_dequeued;
2596 svm_msg_q_lock (mq);
2597 if (svm_msg_q_is_empty (mq))
2601 svm_msg_q_unlock (mq);
2604 else if (wait_for_time < 0)
2606 svm_msg_q_wait (mq);
2610 if (svm_msg_q_timedwait (mq, wait_for_time / 1e3))
2612 svm_msg_q_unlock (mq);
2617 vcl_mq_dequeue_batch (wrk, mq);
2618 svm_msg_q_unlock (mq);
2621 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
2623 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
2624 e = svm_msg_q_msg_data (mq, msg);
2625 if (*num_ev < maxevents)
2626 vcl_epoll_wait_handle_mq_event (wrk, e, events, num_ev);
2628 vec_add1 (wrk->unhandled_evts_vector, *e);
2629 svm_msg_q_free_msg (mq, msg);
2631 vec_reset_length (wrk->mq_msg_vector);
2632 vcl_handle_pending_wrk_updates (wrk);
2637 vppcom_epoll_wait_condvar (vcl_worker_t * wrk, struct epoll_event *events,
2638 int maxevents, u32 n_evts, double wait_for_time)
2640 vcl_cut_through_registration_t *cr;
2641 double total_wait = 0, wait_slice;
2644 wait_for_time = (wait_for_time == -1) ? (double) 1e6 : wait_for_time;
2645 wait_slice = wrk->cut_through_registrations ? 10e-6 : wait_for_time;
2649 vcl_ct_registration_lock (wrk);
2651 pool_foreach (cr, wrk->cut_through_registrations, ({
2652 vcl_epoll_wait_handle_mq (wrk, cr->mq, events, maxevents, 0, &n_evts);
2655 vcl_ct_registration_unlock (wrk);
2657 rv = vcl_epoll_wait_handle_mq (wrk, wrk->app_event_queue, events,
2658 maxevents, n_evts ? 0 : wait_slice,
2661 total_wait += wait_slice;
2665 while (total_wait < wait_for_time);
2670 vppcom_epoll_wait_eventfd (vcl_worker_t * wrk, struct epoll_event *events,
2671 int maxevents, u32 n_evts, double wait_for_time)
2673 vcl_mq_evt_conn_t *mqc;
2674 int __clib_unused n_read;
2678 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
2680 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
2681 vec_len (wrk->mq_events), wait_for_time);
2682 for (i = 0; i < n_mq_evts; i++)
2684 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
2685 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
2686 vcl_epoll_wait_handle_mq (wrk, mqc->mq, events, maxevents, 0, &n_evts);
2688 if (!n_evts && n_mq_evts > 0)
2691 return (int) n_evts;
2695 vppcom_epoll_wait (uint32_t vep_handle, struct epoll_event *events,
2696 int maxevents, double wait_for_time)
2698 vcl_worker_t *wrk = vcl_worker_get_current ();
2699 vcl_session_t *vep_session;
2703 if (PREDICT_FALSE (maxevents <= 0))
2705 clib_warning ("VCL<%d>: ERROR: Invalid maxevents (%d)!",
2706 getpid (), maxevents);
2707 return VPPCOM_EINVAL;
2710 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
2712 return VPPCOM_EBADFD;
2714 if (PREDICT_FALSE (!vep_session->is_vep))
2716 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
2717 getpid (), vep_handle);
2718 return VPPCOM_EINVAL;
2721 memset (events, 0, sizeof (*events) * maxevents);
2723 if (vec_len (wrk->unhandled_evts_vector))
2725 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
2727 vcl_epoll_wait_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i],
2729 if (n_evts == maxevents)
2736 vec_delete (wrk->unhandled_evts_vector, i, 0);
2739 if (vcm->cfg.use_mq_eventfd)
2740 return vppcom_epoll_wait_eventfd (wrk, events, maxevents, n_evts,
2743 return vppcom_epoll_wait_condvar (wrk, events, maxevents, n_evts,
2748 vppcom_session_attr (uint32_t session_handle, uint32_t op,
2749 void *buffer, uint32_t * buflen)
2751 vcl_worker_t *wrk = vcl_worker_get_current ();
2752 vcl_session_t *session;
2754 u32 *flags = buffer, tmp_flags = 0;
2755 vppcom_endpt_t *ep = buffer;
2757 session = vcl_session_get_w_handle (wrk, session_handle);
2759 return VPPCOM_EBADFD;
2763 case VPPCOM_ATTR_GET_NREAD:
2764 rv = vcl_session_read_ready (session);
2765 VDBG (2, "VPPCOM_ATTR_GET_NREAD: sid %u, nread = %d", rv);
2768 case VPPCOM_ATTR_GET_NWRITE:
2769 rv = vcl_session_write_ready (session);
2770 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_NWRITE: sid %u, nwrite = %d",
2771 getpid (), session_handle, rv);
2774 case VPPCOM_ATTR_GET_FLAGS:
2775 if (PREDICT_TRUE (buffer && buflen && (*buflen >= sizeof (*flags))))
2777 *flags = O_RDWR | (VCL_SESS_ATTR_TEST (session->attr,
2778 VCL_SESS_ATTR_NONBLOCK));
2779 *buflen = sizeof (*flags);
2780 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_FLAGS: sid %u, flags = 0x%08x, "
2781 "is_nonblocking = %u", getpid (),
2782 session_handle, *flags,
2783 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK));
2789 case VPPCOM_ATTR_SET_FLAGS:
2790 if (PREDICT_TRUE (buffer && buflen && (*buflen == sizeof (*flags))))
2792 if (*flags & O_NONBLOCK)
2793 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK);
2795 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_NONBLOCK);
2797 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_FLAGS: sid %u, flags = 0x%08x,"
2798 " is_nonblocking = %u",
2799 getpid (), session_handle, *flags,
2800 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK));
2806 case VPPCOM_ATTR_GET_PEER_ADDR:
2807 if (PREDICT_TRUE (buffer && buflen &&
2808 (*buflen >= sizeof (*ep)) && ep->ip))
2810 ep->is_ip4 = session->transport.is_ip4;
2811 ep->port = session->transport.rmt_port;
2812 if (session->transport.is_ip4)
2813 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
2814 sizeof (ip4_address_t));
2816 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
2817 sizeof (ip6_address_t));
2818 *buflen = sizeof (*ep);
2819 VDBG (1, "VCL<%d>: VPPCOM_ATTR_GET_PEER_ADDR: sid %u, is_ip4 = %u, "
2820 "addr = %U, port %u", getpid (),
2821 session_handle, ep->is_ip4, format_ip46_address,
2822 &session->transport.rmt_ip,
2823 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
2824 clib_net_to_host_u16 (ep->port));
2830 case VPPCOM_ATTR_GET_LCL_ADDR:
2831 if (PREDICT_TRUE (buffer && buflen &&
2832 (*buflen >= sizeof (*ep)) && ep->ip))
2834 ep->is_ip4 = session->transport.is_ip4;
2835 ep->port = session->transport.lcl_port;
2836 if (session->transport.is_ip4)
2837 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip4,
2838 sizeof (ip4_address_t));
2840 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip6,
2841 sizeof (ip6_address_t));
2842 *buflen = sizeof (*ep);
2843 VDBG (1, "VCL<%d>: VPPCOM_ATTR_GET_LCL_ADDR: sid %u, is_ip4 = %u,"
2844 " addr = %U port %d", getpid (),
2845 session_handle, ep->is_ip4, format_ip46_address,
2846 &session->transport.lcl_ip,
2847 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
2848 clib_net_to_host_u16 (ep->port));
2854 case VPPCOM_ATTR_GET_LIBC_EPFD:
2855 rv = session->libc_epfd;
2856 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd %d",
2860 case VPPCOM_ATTR_SET_LIBC_EPFD:
2861 if (PREDICT_TRUE (buffer && buflen &&
2862 (*buflen == sizeof (session->libc_epfd))))
2864 session->libc_epfd = *(int *) buffer;
2865 *buflen = sizeof (session->libc_epfd);
2867 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd %d, "
2868 "buflen %d", getpid (), session->libc_epfd, *buflen);
2874 case VPPCOM_ATTR_GET_PROTOCOL:
2875 if (buffer && buflen && (*buflen >= sizeof (int)))
2877 *(int *) buffer = session->session_type;
2878 *buflen = sizeof (int);
2880 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_PROTOCOL: %d (%s), buflen %d",
2881 getpid (), *(int *) buffer, *(int *) buffer ? "UDP" : "TCP",
2888 case VPPCOM_ATTR_GET_LISTEN:
2889 if (buffer && buflen && (*buflen >= sizeof (int)))
2891 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2892 VCL_SESS_ATTR_LISTEN);
2893 *buflen = sizeof (int);
2895 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_LISTEN: %d, buflen %d",
2896 getpid (), *(int *) buffer, *buflen);
2902 case VPPCOM_ATTR_GET_ERROR:
2903 if (buffer && buflen && (*buflen >= sizeof (int)))
2905 *(int *) buffer = 0;
2906 *buflen = sizeof (int);
2908 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_ERROR: %d, buflen %d, #VPP-TBD#",
2909 getpid (), *(int *) buffer, *buflen);
2915 case VPPCOM_ATTR_GET_TX_FIFO_LEN:
2916 if (buffer && buflen && (*buflen >= sizeof (u32)))
2920 *(size_t *) buffer = (session->sndbuf_size ? session->sndbuf_size :
2921 session->tx_fifo ? session->tx_fifo->nitems :
2922 vcm->cfg.tx_fifo_size);
2923 *buflen = sizeof (u32);
2925 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TX_FIFO_LEN: %u (0x%x), "
2926 "buflen %d, #VPP-TBD#", getpid (),
2927 *(size_t *) buffer, *(size_t *) buffer, *buflen);
2933 case VPPCOM_ATTR_SET_TX_FIFO_LEN:
2934 if (buffer && buflen && (*buflen == sizeof (u32)))
2937 session->sndbuf_size = *(u32 *) buffer;
2938 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TX_FIFO_LEN: %u (0x%x), "
2939 "buflen %d, #VPP-TBD#", getpid (),
2940 session->sndbuf_size, session->sndbuf_size, *buflen);
2946 case VPPCOM_ATTR_GET_RX_FIFO_LEN:
2947 if (buffer && buflen && (*buflen >= sizeof (u32)))
2951 *(size_t *) buffer = (session->rcvbuf_size ? session->rcvbuf_size :
2952 session->rx_fifo ? session->rx_fifo->nitems :
2953 vcm->cfg.rx_fifo_size);
2954 *buflen = sizeof (u32);
2956 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_RX_FIFO_LEN: %u (0x%x), "
2957 "buflen %d, #VPP-TBD#", getpid (),
2958 *(size_t *) buffer, *(size_t *) buffer, *buflen);
2964 case VPPCOM_ATTR_SET_RX_FIFO_LEN:
2965 if (buffer && buflen && (*buflen == sizeof (u32)))
2968 session->rcvbuf_size = *(u32 *) buffer;
2969 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_RX_FIFO_LEN: %u (0x%x), "
2970 "buflen %d, #VPP-TBD#", getpid (),
2971 session->sndbuf_size, session->sndbuf_size, *buflen);
2977 case VPPCOM_ATTR_GET_REUSEADDR:
2978 if (buffer && buflen && (*buflen >= sizeof (int)))
2981 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2982 VCL_SESS_ATTR_REUSEADDR);
2983 *buflen = sizeof (int);
2985 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_REUSEADDR: %d, "
2986 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
2992 case VPPCOM_ATTR_SET_REUSEADDR:
2993 if (buffer && buflen && (*buflen == sizeof (int)) &&
2994 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
2997 if (*(int *) buffer)
2998 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEADDR);
3000 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEADDR);
3002 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_REUSEADDR: %d, buflen %d,"
3003 " #VPP-TBD#", getpid (),
3004 VCL_SESS_ATTR_TEST (session->attr,
3005 VCL_SESS_ATTR_REUSEADDR), *buflen);
3011 case VPPCOM_ATTR_GET_REUSEPORT:
3012 if (buffer && buflen && (*buflen >= sizeof (int)))
3015 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3016 VCL_SESS_ATTR_REUSEPORT);
3017 *buflen = sizeof (int);
3019 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_REUSEPORT: %d, buflen %d,"
3020 " #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
3026 case VPPCOM_ATTR_SET_REUSEPORT:
3027 if (buffer && buflen && (*buflen == sizeof (int)) &&
3028 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
3031 if (*(int *) buffer)
3032 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEPORT);
3034 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEPORT);
3036 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_REUSEPORT: %d, buflen %d,"
3037 " #VPP-TBD#", getpid (),
3038 VCL_SESS_ATTR_TEST (session->attr,
3039 VCL_SESS_ATTR_REUSEPORT), *buflen);
3045 case VPPCOM_ATTR_GET_BROADCAST:
3046 if (buffer && buflen && (*buflen >= sizeof (int)))
3049 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3050 VCL_SESS_ATTR_BROADCAST);
3051 *buflen = sizeof (int);
3053 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_BROADCAST: %d, buflen %d,"
3054 " #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
3060 case VPPCOM_ATTR_SET_BROADCAST:
3061 if (buffer && buflen && (*buflen == sizeof (int)))
3064 if (*(int *) buffer)
3065 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_BROADCAST);
3067 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_BROADCAST);
3069 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_BROADCAST: %d, buflen %d, "
3070 "#VPP-TBD#", getpid (),
3071 VCL_SESS_ATTR_TEST (session->attr,
3072 VCL_SESS_ATTR_BROADCAST), *buflen);
3078 case VPPCOM_ATTR_GET_V6ONLY:
3079 if (buffer && buflen && (*buflen >= sizeof (int)))
3082 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3083 VCL_SESS_ATTR_V6ONLY);
3084 *buflen = sizeof (int);
3086 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_V6ONLY: %d, buflen %d, "
3087 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
3093 case VPPCOM_ATTR_SET_V6ONLY:
3094 if (buffer && buflen && (*buflen == sizeof (int)))
3097 if (*(int *) buffer)
3098 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_V6ONLY);
3100 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_V6ONLY);
3102 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_V6ONLY: %d, buflen %d, "
3103 "#VPP-TBD#", getpid (),
3104 VCL_SESS_ATTR_TEST (session->attr,
3105 VCL_SESS_ATTR_V6ONLY), *buflen);
3111 case VPPCOM_ATTR_GET_KEEPALIVE:
3112 if (buffer && buflen && (*buflen >= sizeof (int)))
3115 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3116 VCL_SESS_ATTR_KEEPALIVE);
3117 *buflen = sizeof (int);
3119 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_KEEPALIVE: %d, buflen %d, "
3120 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
3126 case VPPCOM_ATTR_SET_KEEPALIVE:
3127 if (buffer && buflen && (*buflen == sizeof (int)))
3130 if (*(int *) buffer)
3131 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_KEEPALIVE);
3133 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_KEEPALIVE);
3135 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_KEEPALIVE: %d, buflen %d, "
3136 "#VPP-TBD#", getpid (),
3137 VCL_SESS_ATTR_TEST (session->attr,
3138 VCL_SESS_ATTR_KEEPALIVE), *buflen);
3144 case VPPCOM_ATTR_GET_TCP_NODELAY:
3145 if (buffer && buflen && (*buflen >= sizeof (int)))
3148 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3149 VCL_SESS_ATTR_TCP_NODELAY);
3150 *buflen = sizeof (int);
3152 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_NODELAY: %d, buflen %d, "
3153 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
3159 case VPPCOM_ATTR_SET_TCP_NODELAY:
3160 if (buffer && buflen && (*buflen == sizeof (int)))
3163 if (*(int *) buffer)
3164 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
3166 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
3168 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_NODELAY: %d, buflen %d, "
3169 "#VPP-TBD#", getpid (),
3170 VCL_SESS_ATTR_TEST (session->attr,
3171 VCL_SESS_ATTR_TCP_NODELAY), *buflen);
3177 case VPPCOM_ATTR_GET_TCP_KEEPIDLE:
3178 if (buffer && buflen && (*buflen >= sizeof (int)))
3181 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3182 VCL_SESS_ATTR_TCP_KEEPIDLE);
3183 *buflen = sizeof (int);
3185 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d, buflen %d, "
3186 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
3192 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
3193 if (buffer && buflen && (*buflen == sizeof (int)))
3196 if (*(int *) buffer)
3197 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
3199 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
3201 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d, buflen %d, "
3202 "#VPP-TBD#", getpid (),
3203 VCL_SESS_ATTR_TEST (session->attr,
3204 VCL_SESS_ATTR_TCP_KEEPIDLE), *buflen);
3210 case VPPCOM_ATTR_GET_TCP_KEEPINTVL:
3211 if (buffer && buflen && (*buflen >= sizeof (int)))
3214 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3215 VCL_SESS_ATTR_TCP_KEEPINTVL);
3216 *buflen = sizeof (int);
3218 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_KEEPINTVL: %d, buflen %d, "
3219 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
3225 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
3226 if (buffer && buflen && (*buflen == sizeof (int)))
3229 if (*(int *) buffer)
3230 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
3232 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
3234 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d, buflen %d, "
3235 "#VPP-TBD#", getpid (),
3236 VCL_SESS_ATTR_TEST (session->attr,
3237 VCL_SESS_ATTR_TCP_KEEPINTVL), *buflen);
3243 case VPPCOM_ATTR_GET_TCP_USER_MSS:
3244 if (buffer && buflen && (*buflen >= sizeof (u32)))
3247 *(u32 *) buffer = session->user_mss;
3248 *buflen = sizeof (int);
3250 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_USER_MSS: %d, buflen %d,"
3251 " #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
3257 case VPPCOM_ATTR_SET_TCP_USER_MSS:
3258 if (buffer && buflen && (*buflen == sizeof (u32)))
3261 session->user_mss = *(u32 *) buffer;
3263 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_USER_MSS: %u, buflen %d, "
3264 "#VPP-TBD#", getpid (), session->user_mss, *buflen);
3270 case VPPCOM_ATTR_SET_SHUT:
3271 if (*flags == SHUT_RD || *flags == SHUT_RDWR)
3272 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_SHUT_RD);
3273 if (*flags == SHUT_WR || *flags == SHUT_RDWR)
3274 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_SHUT_WR);
3277 case VPPCOM_ATTR_GET_SHUT:
3278 if (VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_SHUT_RD))
3280 if (VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_SHUT_WR))
3283 *(int *) buffer = SHUT_RD;
3284 else if (tmp_flags == 2)
3285 *(int *) buffer = SHUT_WR;
3286 else if (tmp_flags == 3)
3287 *(int *) buffer = SHUT_RDWR;
3288 *buflen = sizeof (int);
3299 vppcom_session_recvfrom (uint32_t session_handle, void *buffer,
3300 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3302 vcl_worker_t *wrk = vcl_worker_get_current ();
3304 vcl_session_t *session = 0;
3308 session = vcl_session_get_w_handle (wrk, session_handle);
3309 if (PREDICT_FALSE (!session))
3311 VDBG (0, "VCL<%d>: invalid session, sid (%u) has been closed!",
3312 getpid (), session_handle);
3313 return VPPCOM_EBADFD;
3315 ep->is_ip4 = session->transport.is_ip4;
3316 ep->port = session->transport.rmt_port;
3320 rv = vppcom_session_read (session_handle, buffer, buflen);
3321 else if (flags & MSG_PEEK)
3322 rv = vppcom_session_peek (session_handle, buffer, buflen);
3325 VDBG (0, "Unsupport flags for recvfrom %d", flags);
3326 return VPPCOM_EAFNOSUPPORT;
3331 if (session->transport.is_ip4)
3332 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
3333 sizeof (ip4_address_t));
3335 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
3336 sizeof (ip6_address_t));
3343 vppcom_session_sendto (uint32_t session_handle, void *buffer,
3344 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3347 return VPPCOM_EINVAL;
3352 return VPPCOM_EINVAL;
3357 // TBD check the flags and do the right thing
3358 VDBG (2, "VCL<%d>: handling flags 0x%u (%d) not implemented yet.",
3359 getpid (), flags, flags);
3362 return (vppcom_session_write_inline (session_handle, buffer, buflen, 1));
3366 vppcom_poll (vcl_poll_t * vp, uint32_t n_sids, double wait_for_time)
3368 vcl_worker_t *wrk = vcl_worker_get_current ();
3369 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
3370 u32 i, keep_trying = 1;
3371 svm_msg_q_msg_t msg;
3375 VDBG (3, "VCL<%d>: vp %p, nsids %u, wait_for_time %f",
3376 getpid (), vp, n_sids, wait_for_time);
3379 return VPPCOM_EFAULT;
3383 vcl_session_t *session;
3385 /* Dequeue all events and drop all unhandled io events */
3386 while (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0) == 0)
3388 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
3389 vcl_handle_mq_event (wrk, e);
3390 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
3392 vec_reset_length (wrk->unhandled_evts_vector);
3394 for (i = 0; i < n_sids; i++)
3396 session = vcl_session_get (wrk, vp[i].sh);
3399 vp[i].revents = POLLHUP;
3406 if (POLLIN & vp[i].events)
3408 rv = vcl_session_read_ready (session);
3411 vp[i].revents |= POLLIN;
3418 case VPPCOM_ECONNRESET:
3419 vp[i].revents = POLLHUP;
3423 vp[i].revents = POLLERR;
3430 if (POLLOUT & vp[i].events)
3432 rv = vcl_session_write_ready (session);
3435 vp[i].revents |= POLLOUT;
3442 case VPPCOM_ECONNRESET:
3443 vp[i].revents = POLLHUP;
3447 vp[i].revents = POLLERR;
3454 if (0) // Note "done:" label used by VCL_SESSION_LOCK_AND_GET()
3456 vp[i].revents = POLLNVAL;
3460 if (wait_for_time != -1)
3461 keep_trying = (clib_time_now (&wrk->clib_time) <= timeout) ? 1 : 0;
3463 while ((num_ev == 0) && keep_trying);
3465 if (VPPCOM_DEBUG > 3)
3467 clib_warning ("VCL<%d>: returning %d", getpid (), num_ev);
3468 for (i = 0; i < n_sids; i++)
3470 clib_warning ("VCL<%d>: vp[%d].sid %d (0x%x), .events 0x%x, "
3471 ".revents 0x%x", getpid (), i, vp[i].sh, vp[i].sh,
3472 vp[i].events, vp[i].revents);
3479 vppcom_mq_epoll_fd (void)
3481 vcl_worker_t *wrk = vcl_worker_get_current ();
3482 return wrk->mqs_epfd;
3486 vppcom_session_index (vcl_session_handle_t session_handle)
3488 return session_handle & 0xFFFFFF;
3492 vppcom_session_worker (vcl_session_handle_t session_handle)
3494 return session_handle >> 24;
3498 vppcom_worker_register (void)
3500 if (!vcl_worker_alloc_and_init ())
3501 return VPPCOM_EEXIST;
3503 if (vcl_worker_set_bapi ())
3504 return VPPCOM_EEXIST;
3506 if (vcl_worker_register_with_vpp ())
3507 return VPPCOM_EEXIST;
3513 vppcom_worker_index (void)
3515 return vcl_get_worker_index ();
3519 vppcom_worker_mqs_epfd (void)
3521 vcl_worker_t *wrk = vcl_worker_get_current ();
3522 if (!vcm->cfg.use_mq_eventfd)
3524 return wrk->mqs_epfd;
3528 * fd.io coding-style-patch-verification: ON
3531 * eval: (c-set-style "gnu")