2 * Copyright (c) 2017-2019 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
18 #include <vcl/vppcom.h>
19 #include <vcl/vcl_debug.h>
20 #include <vcl/vcl_private.h>
21 #include <svm/fifo_segment.h>
23 __thread uword __vcl_worker_index = ~0;
26 vcl_wait_for_segment (u64 segment_handle)
28 vcl_worker_t *wrk = vcl_worker_get_current ();
29 u32 wait_for_seconds = 10, segment_index;
32 if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
35 timeout = clib_time_now (&wrk->clib_time) + wait_for_seconds;
36 while (clib_time_now (&wrk->clib_time) < timeout)
38 segment_index = vcl_segment_table_lookup (segment_handle);
39 if (segment_index != VCL_INVALID_SEGMENT_INDEX)
47 vcl_mq_dequeue_batch (vcl_worker_t * wrk, svm_msg_q_t * mq, u32 n_max_msg)
53 n_msgs = clib_min (svm_msg_q_size (mq), n_max_msg);
54 for (i = 0; i < n_msgs; i++)
56 vec_add2 (wrk->mq_msg_vector, msg, 1);
57 svm_msg_q_sub_w_lock (mq, msg);
63 vppcom_session_state_str (vcl_session_state_t state)
85 case STATE_VPP_CLOSING:
86 st = "STATE_VPP_CLOSING";
89 case STATE_DISCONNECT:
90 st = "STATE_DISCONNECT";
101 case STATE_LISTEN_NO_MQ:
102 st = "STATE_LISTEN_NO_MQ";
106 st = "UNKNOWN_STATE";
114 format_ip4_address (u8 * s, va_list * args)
116 u8 *a = va_arg (*args, u8 *);
117 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
121 format_ip6_address (u8 * s, va_list * args)
123 ip6_address_t *a = va_arg (*args, ip6_address_t *);
124 u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
126 i_max_n_zero = ARRAY_LEN (a->as_u16);
128 i_first_zero = i_max_n_zero;
130 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
132 u32 is_zero = a->as_u16[i] == 0;
133 if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
139 if ((!is_zero && n_zeros > max_n_zeros)
140 || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
142 i_max_n_zero = i_first_zero;
143 max_n_zeros = n_zeros;
144 i_first_zero = ARRAY_LEN (a->as_u16);
149 last_double_colon = 0;
150 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
152 if (i == i_max_n_zero && max_n_zeros > 1)
154 s = format (s, "::");
155 i += max_n_zeros - 1;
156 last_double_colon = 1;
160 s = format (s, "%s%x",
161 (last_double_colon || i == 0) ? "" : ":",
162 clib_net_to_host_u16 (a->as_u16[i]));
163 last_double_colon = 0;
170 /* Format an IP46 address. */
172 format_ip46_address (u8 * s, va_list * args)
174 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
175 ip46_type_t type = va_arg (*args, ip46_type_t);
181 is_ip4 = ip46_address_is_ip4 (ip46);
192 format (s, "%U", format_ip4_address, &ip46->ip4) :
193 format (s, "%U", format_ip6_address, &ip46->ip6);
197 * VPPCOM Utility Functions
201 vcl_send_session_listen (vcl_worker_t * wrk, vcl_session_t * s)
203 app_session_evt_t _app_evt, *app_evt = &_app_evt;
204 session_listen_msg_t *mp;
207 mq = vcl_worker_ctrl_mq (wrk);
208 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_LISTEN);
209 mp = (session_listen_msg_t *) app_evt->evt->data;
210 memset (mp, 0, sizeof (*mp));
211 mp->client_index = wrk->my_client_index;
212 mp->context = s->session_index;
213 mp->wrk_index = wrk->vpp_wrk_index;
214 mp->is_ip4 = s->transport.is_ip4;
215 clib_memcpy_fast (&mp->ip, &s->transport.lcl_ip, sizeof (mp->ip));
216 mp->port = s->transport.lcl_port;
217 mp->proto = s->session_type;
218 app_send_ctrl_evt_to_vpp (mq, app_evt);
222 vcl_send_session_connect (vcl_worker_t * wrk, vcl_session_t * s)
224 app_session_evt_t _app_evt, *app_evt = &_app_evt;
225 session_connect_msg_t *mp;
228 mq = vcl_worker_ctrl_mq (wrk);
229 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_CONNECT);
230 mp = (session_connect_msg_t *) app_evt->evt->data;
231 memset (mp, 0, sizeof (*mp));
232 mp->client_index = wrk->my_client_index;
233 mp->context = s->session_index;
234 mp->wrk_index = wrk->vpp_wrk_index;
235 mp->is_ip4 = s->transport.is_ip4;
236 mp->parent_handle = s->parent_handle;
237 clib_memcpy_fast (&mp->ip, &s->transport.rmt_ip, sizeof (mp->ip));
238 mp->port = s->transport.rmt_port;
239 mp->proto = s->session_type;
240 app_send_ctrl_evt_to_vpp (mq, app_evt);
244 vcl_send_session_unlisten (vcl_worker_t * wrk, vcl_session_t * s)
246 app_session_evt_t _app_evt, *app_evt = &_app_evt;
247 session_unlisten_msg_t *mp;
250 mq = vcl_worker_ctrl_mq (wrk);
251 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_UNLISTEN);
252 mp = (session_unlisten_msg_t *) app_evt->evt->data;
253 memset (mp, 0, sizeof (*mp));
254 mp->client_index = wrk->my_client_index;
255 mp->wrk_index = wrk->vpp_wrk_index;
256 mp->handle = s->vpp_handle;
257 mp->context = wrk->wrk_index;
258 app_send_ctrl_evt_to_vpp (mq, app_evt);
262 vcl_send_session_disconnect (vcl_worker_t * wrk, vcl_session_t * s)
264 app_session_evt_t _app_evt, *app_evt = &_app_evt;
265 session_disconnect_msg_t *mp;
268 /* Send to thread that owns the session */
270 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_DISCONNECT);
271 mp = (session_disconnect_msg_t *) app_evt->evt->data;
272 memset (mp, 0, sizeof (*mp));
273 mp->client_index = wrk->my_client_index;
274 mp->handle = s->vpp_handle;
275 app_send_ctrl_evt_to_vpp (mq, app_evt);
279 vcl_send_app_detach (vcl_worker_t * wrk)
281 app_session_evt_t _app_evt, *app_evt = &_app_evt;
282 session_app_detach_msg_t *mp;
285 mq = vcl_worker_ctrl_mq (wrk);
286 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_APP_DETACH);
287 mp = (session_app_detach_msg_t *) app_evt->evt->data;
288 memset (mp, 0, sizeof (*mp));
289 mp->client_index = wrk->my_client_index;
290 app_send_ctrl_evt_to_vpp (mq, app_evt);
294 vcl_send_session_accepted_reply (svm_msg_q_t * mq, u32 context,
295 session_handle_t handle, int retval)
297 app_session_evt_t _app_evt, *app_evt = &_app_evt;
298 session_accepted_reply_msg_t *rmp;
299 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_ACCEPTED_REPLY);
300 rmp = (session_accepted_reply_msg_t *) app_evt->evt->data;
301 rmp->handle = handle;
302 rmp->context = context;
303 rmp->retval = retval;
304 app_send_ctrl_evt_to_vpp (mq, app_evt);
308 vcl_send_session_disconnected_reply (svm_msg_q_t * mq, u32 context,
309 session_handle_t handle, int retval)
311 app_session_evt_t _app_evt, *app_evt = &_app_evt;
312 session_disconnected_reply_msg_t *rmp;
313 app_alloc_ctrl_evt_to_vpp (mq, app_evt,
314 SESSION_CTRL_EVT_DISCONNECTED_REPLY);
315 rmp = (session_disconnected_reply_msg_t *) app_evt->evt->data;
316 rmp->handle = handle;
317 rmp->context = context;
318 rmp->retval = retval;
319 app_send_ctrl_evt_to_vpp (mq, app_evt);
323 vcl_send_session_reset_reply (svm_msg_q_t * mq, u32 context,
324 session_handle_t handle, int retval)
326 app_session_evt_t _app_evt, *app_evt = &_app_evt;
327 session_reset_reply_msg_t *rmp;
328 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_RESET_REPLY);
329 rmp = (session_reset_reply_msg_t *) app_evt->evt->data;
330 rmp->handle = handle;
331 rmp->context = context;
332 rmp->retval = retval;
333 app_send_ctrl_evt_to_vpp (mq, app_evt);
337 vcl_send_session_worker_update (vcl_worker_t * wrk, vcl_session_t * s,
340 app_session_evt_t _app_evt, *app_evt = &_app_evt;
341 session_worker_update_msg_t *mp;
344 mq = vcl_session_vpp_evt_q (wrk, s);
345 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_WORKER_UPDATE);
346 mp = (session_worker_update_msg_t *) app_evt->evt->data;
347 mp->client_index = wrk->my_client_index;
348 mp->handle = s->vpp_handle;
349 mp->req_wrk_index = wrk->vpp_wrk_index;
350 mp->wrk_index = wrk_index;
351 app_send_ctrl_evt_to_vpp (mq, app_evt);
355 vcl_session_accepted_handler (vcl_worker_t * wrk, session_accepted_msg_t * mp,
358 vcl_session_t *session, *listen_session;
359 svm_fifo_t *rx_fifo, *tx_fifo;
363 session = vcl_session_alloc (wrk);
365 listen_session = vcl_session_get (wrk, ls_index);
366 if (listen_session->vpp_handle != mp->listener_handle)
368 VDBG (0, "ERROR: listener handle %lu does not match session %u",
369 mp->listener_handle, ls_index);
373 if (vcl_wait_for_segment (mp->segment_handle))
375 VDBG (0, "ERROR: segment for session %u couldn't be mounted!",
376 session->session_index);
380 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
381 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
382 session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address,
384 rx_fifo->client_session_index = session->session_index;
385 tx_fifo->client_session_index = session->session_index;
386 rx_fifo->client_thread_index = vcl_get_worker_index ();
387 tx_fifo->client_thread_index = vcl_get_worker_index ();
388 vpp_wrk_index = tx_fifo->master_thread_index;
389 vec_validate (wrk->vpp_event_queues, vpp_wrk_index);
390 wrk->vpp_event_queues[vpp_wrk_index] = session->vpp_evt_q;
392 session->vpp_handle = mp->handle;
393 session->vpp_thread_index = rx_fifo->master_thread_index;
394 session->rx_fifo = rx_fifo;
395 session->tx_fifo = tx_fifo;
397 session->session_state = STATE_ACCEPT;
398 session->transport.rmt_port = mp->rmt.port;
399 session->transport.is_ip4 = mp->rmt.is_ip4;
400 clib_memcpy_fast (&session->transport.rmt_ip, &mp->rmt.ip,
401 sizeof (ip46_address_t));
403 vcl_session_table_add_vpp_handle (wrk, mp->handle, session->session_index);
404 session->transport.lcl_port = listen_session->transport.lcl_port;
405 session->transport.lcl_ip = listen_session->transport.lcl_ip;
406 session->session_type = listen_session->session_type;
407 session->is_dgram = vcl_proto_is_dgram (session->session_type);
408 session->listener_index = listen_session->session_index;
409 listen_session->n_accepted_sessions++;
411 VDBG (1, "session %u [0x%llx]: client accept request from %s address %U"
412 " port %d queue %p!", session->session_index, mp->handle,
413 mp->rmt.is_ip4 ? "IPv4" : "IPv6", format_ip46_address, &mp->rmt.ip,
414 mp->rmt.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
415 clib_net_to_host_u16 (mp->rmt.port), session->vpp_evt_q);
416 vcl_evt (VCL_EVT_ACCEPT, session, listen_session, session_index);
418 vcl_send_session_accepted_reply (session->vpp_evt_q, mp->context,
419 session->vpp_handle, 0);
421 return session->session_index;
424 evt_q = uword_to_pointer (mp->vpp_event_queue_address, svm_msg_q_t *);
425 vcl_send_session_accepted_reply (evt_q, mp->context, mp->handle,
426 VNET_API_ERROR_INVALID_ARGUMENT);
427 vcl_session_free (wrk, session);
428 return VCL_INVALID_SESSION_INDEX;
432 vcl_session_connected_handler (vcl_worker_t * wrk,
433 session_connected_msg_t * mp)
435 u32 session_index, vpp_wrk_index;
436 svm_fifo_t *rx_fifo, *tx_fifo;
437 vcl_session_t *session = 0;
439 session_index = mp->context;
440 session = vcl_session_get (wrk, session_index);
443 VDBG (0, "ERROR: vpp handle 0x%llx has no session index (%u)!",
444 mp->handle, session_index);
445 return VCL_INVALID_SESSION_INDEX;
449 VDBG (0, "ERROR: session index %u: connect failed! %U",
450 session_index, format_api_error, ntohl (mp->retval));
451 session->session_state = STATE_FAILED;
452 session->vpp_handle = mp->handle;
453 return session_index;
456 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
457 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
458 if (vcl_wait_for_segment (mp->segment_handle))
460 VDBG (0, "segment for session %u couldn't be mounted!",
461 session->session_index);
462 session->session_state = STATE_FAILED;
463 return VCL_INVALID_SESSION_INDEX;
466 rx_fifo->client_session_index = session_index;
467 tx_fifo->client_session_index = session_index;
468 rx_fifo->client_thread_index = vcl_get_worker_index ();
469 tx_fifo->client_thread_index = vcl_get_worker_index ();
471 session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address,
473 vpp_wrk_index = tx_fifo->master_thread_index;
474 vec_validate (wrk->vpp_event_queues, vpp_wrk_index);
475 wrk->vpp_event_queues[vpp_wrk_index] = session->vpp_evt_q;
479 session->ct_rx_fifo = uword_to_pointer (mp->ct_rx_fifo, svm_fifo_t *);
480 session->ct_tx_fifo = uword_to_pointer (mp->ct_tx_fifo, svm_fifo_t *);
481 if (vcl_wait_for_segment (mp->ct_segment_handle))
483 VDBG (0, "ct segment for session %u couldn't be mounted!",
484 session->session_index);
485 session->session_state = STATE_FAILED;
486 return VCL_INVALID_SESSION_INDEX;
490 session->rx_fifo = rx_fifo;
491 session->tx_fifo = tx_fifo;
492 session->vpp_handle = mp->handle;
493 session->vpp_thread_index = rx_fifo->master_thread_index;
494 session->transport.is_ip4 = mp->lcl.is_ip4;
495 clib_memcpy_fast (&session->transport.lcl_ip, &mp->lcl.ip,
496 sizeof (session->transport.lcl_ip));
497 session->transport.lcl_port = mp->lcl.port;
498 session->session_state = STATE_CONNECT;
500 /* Add it to lookup table */
501 vcl_session_table_add_vpp_handle (wrk, mp->handle, session_index);
503 VDBG (1, "session %u [0x%llx] connected! rx_fifo %p, refcnt %d, tx_fifo %p,"
504 " refcnt %d", session_index, mp->handle, session->rx_fifo,
505 session->rx_fifo->refcnt, session->tx_fifo, session->tx_fifo->refcnt);
507 return session_index;
511 vcl_flag_accepted_session (vcl_session_t * session, u64 handle, u32 flags)
513 vcl_session_msg_t *accepted_msg;
516 for (i = 0; i < vec_len (session->accept_evts_fifo); i++)
518 accepted_msg = &session->accept_evts_fifo[i];
519 if (accepted_msg->accepted_msg.handle == handle)
521 accepted_msg->flags |= flags;
529 vcl_session_reset_handler (vcl_worker_t * wrk,
530 session_reset_msg_t * reset_msg)
532 vcl_session_t *session;
535 sid = vcl_session_index_from_vpp_handle (wrk, reset_msg->handle);
536 session = vcl_session_get (wrk, sid);
539 VDBG (0, "request to reset unknown handle 0x%llx", reset_msg->handle);
540 return VCL_INVALID_SESSION_INDEX;
543 /* Caught a reset before actually accepting the session */
544 if (session->session_state == STATE_LISTEN)
547 if (!vcl_flag_accepted_session (session, reset_msg->handle,
548 VCL_ACCEPTED_F_RESET))
549 VDBG (0, "session was not accepted!");
550 return VCL_INVALID_SESSION_INDEX;
553 session->session_state = STATE_DISCONNECT;
554 VDBG (0, "reset session %u [0x%llx]", sid, reset_msg->handle);
559 vcl_session_bound_handler (vcl_worker_t * wrk, session_bound_msg_t * mp)
561 vcl_session_t *session;
562 u32 sid = mp->context;
564 session = vcl_session_get (wrk, sid);
567 VERR ("session %u [0x%llx]: bind failed: %U", sid, mp->handle,
568 format_api_error, mp->retval);
571 session->session_state = STATE_FAILED;
572 session->vpp_handle = mp->handle;
577 VDBG (0, "ERROR: session %u [0x%llx]: Invalid session index!",
579 return VCL_INVALID_SESSION_INDEX;
583 session->vpp_handle = mp->handle;
584 session->transport.is_ip4 = mp->lcl_is_ip4;
585 clib_memcpy_fast (&session->transport.lcl_ip, mp->lcl_ip,
586 sizeof (ip46_address_t));
587 session->transport.lcl_port = mp->lcl_port;
588 vcl_session_table_add_listener (wrk, mp->handle, sid);
589 session->session_state = STATE_LISTEN;
591 session->vpp_evt_q = uword_to_pointer (mp->vpp_evt_q, svm_msg_q_t *);
592 vec_validate (wrk->vpp_event_queues, 0);
593 wrk->vpp_event_queues[0] = session->vpp_evt_q;
595 if (session->is_dgram)
597 svm_fifo_t *rx_fifo, *tx_fifo;
598 session->vpp_evt_q = uword_to_pointer (mp->vpp_evt_q, svm_msg_q_t *);
599 rx_fifo = uword_to_pointer (mp->rx_fifo, svm_fifo_t *);
600 rx_fifo->client_session_index = sid;
601 tx_fifo = uword_to_pointer (mp->tx_fifo, svm_fifo_t *);
602 tx_fifo->client_session_index = sid;
603 session->rx_fifo = rx_fifo;
604 session->tx_fifo = tx_fifo;
607 VDBG (0, "session %u [0x%llx]: listen succeeded!", sid, mp->handle);
612 vcl_session_unlisten_reply_handler (vcl_worker_t * wrk, void *data)
614 session_unlisten_reply_msg_t *mp = (session_unlisten_reply_msg_t *) data;
617 s = vcl_session_get_w_vpp_handle (wrk, mp->handle);
618 if (!s || s->session_state != STATE_DISCONNECT)
620 VDBG (0, "Unlisten reply with wrong handle %llx", mp->handle);
625 VDBG (0, "ERROR: session %u [0xllx]: unlisten failed: %U",
626 s->session_index, mp->handle, format_api_error, ntohl (mp->retval));
628 if (mp->context != wrk->wrk_index)
629 VDBG (0, "wrong context");
631 vcl_session_table_del_vpp_handle (wrk, mp->handle);
632 vcl_session_free (wrk, s);
635 static vcl_session_t *
636 vcl_session_accepted (vcl_worker_t * wrk, session_accepted_msg_t * msg)
638 vcl_session_msg_t *vcl_msg;
639 vcl_session_t *session;
641 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
642 if (PREDICT_FALSE (session != 0))
643 VWRN ("session overlap handle %lu state %u!", msg->handle,
644 session->session_state);
646 session = vcl_session_table_lookup_listener (wrk, msg->listener_handle);
649 VERR ("couldn't find listen session: listener handle %llx",
650 msg->listener_handle);
654 clib_fifo_add2 (session->accept_evts_fifo, vcl_msg);
655 vcl_msg->accepted_msg = *msg;
656 /* Session handle points to listener until fully accepted by app */
657 vcl_session_table_add_vpp_handle (wrk, msg->handle, session->session_index);
662 static vcl_session_t *
663 vcl_session_disconnected_handler (vcl_worker_t * wrk,
664 session_disconnected_msg_t * msg)
666 vcl_session_t *session;
668 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
671 VDBG (0, "request to disconnect unknown handle 0x%llx", msg->handle);
675 /* Caught a disconnect before actually accepting the session */
676 if (session->session_state == STATE_LISTEN)
678 if (!vcl_flag_accepted_session (session, msg->handle,
679 VCL_ACCEPTED_F_CLOSED))
680 VDBG (0, "session was not accepted!");
684 session->session_state = STATE_VPP_CLOSING;
689 vcl_session_req_worker_update_handler (vcl_worker_t * wrk, void *data)
691 session_req_worker_update_msg_t *msg;
694 msg = (session_req_worker_update_msg_t *) data;
695 s = vcl_session_get_w_vpp_handle (wrk, msg->session_handle);
699 vec_add1 (wrk->pending_session_wrk_updates, s->session_index);
703 vcl_session_worker_update_reply_handler (vcl_worker_t * wrk, void *data)
705 session_worker_update_reply_msg_t *msg;
708 msg = (session_worker_update_reply_msg_t *) data;
709 s = vcl_session_get_w_vpp_handle (wrk, msg->handle);
712 VDBG (0, "unknown handle 0x%llx", msg->handle);
715 if (vcl_wait_for_segment (msg->segment_handle))
717 clib_warning ("segment for session %u couldn't be mounted!",
724 s->rx_fifo = uword_to_pointer (msg->rx_fifo, svm_fifo_t *);
725 s->tx_fifo = uword_to_pointer (msg->tx_fifo, svm_fifo_t *);
726 s->rx_fifo->client_session_index = s->session_index;
727 s->tx_fifo->client_session_index = s->session_index;
728 s->rx_fifo->client_thread_index = wrk->wrk_index;
729 s->tx_fifo->client_thread_index = wrk->wrk_index;
731 s->session_state = STATE_UPDATED;
733 VDBG (0, "session %u[0x%llx] moved to worker %u", s->session_index,
734 s->vpp_handle, wrk->wrk_index);
738 vcl_handle_mq_event (vcl_worker_t * wrk, session_event_t * e)
740 session_disconnected_msg_t *disconnected_msg;
741 vcl_session_t *session;
743 switch (e->event_type)
745 case SESSION_IO_EVT_RX:
746 case SESSION_IO_EVT_TX:
747 session = vcl_session_get (wrk, e->session_index);
748 if (!session || !(session->session_state & STATE_OPEN))
750 vec_add1 (wrk->unhandled_evts_vector, *e);
752 case SESSION_CTRL_EVT_ACCEPTED:
753 vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
755 case SESSION_CTRL_EVT_CONNECTED:
756 vcl_session_connected_handler (wrk,
757 (session_connected_msg_t *) e->data);
759 case SESSION_CTRL_EVT_DISCONNECTED:
760 disconnected_msg = (session_disconnected_msg_t *) e->data;
761 session = vcl_session_disconnected_handler (wrk, disconnected_msg);
764 VDBG (0, "disconnected session %u [0x%llx]", session->session_index,
765 session->vpp_handle);
767 case SESSION_CTRL_EVT_RESET:
768 vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
770 case SESSION_CTRL_EVT_BOUND:
771 vcl_session_bound_handler (wrk, (session_bound_msg_t *) e->data);
773 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
774 vcl_session_unlisten_reply_handler (wrk, e->data);
776 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
777 vcl_session_req_worker_update_handler (wrk, e->data);
779 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
780 vcl_session_worker_update_reply_handler (wrk, e->data);
783 clib_warning ("unhandled %u", e->event_type);
789 vppcom_wait_for_session_state_change (u32 session_index,
790 vcl_session_state_t state,
793 vcl_worker_t *wrk = vcl_worker_get_current ();
794 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
795 vcl_session_t *volatile session;
801 session = vcl_session_get (wrk, session_index);
802 if (PREDICT_FALSE (!session))
804 return VPPCOM_EBADFD;
806 if (session->session_state & state)
810 if (session->session_state & STATE_FAILED)
812 return VPPCOM_ECONNREFUSED;
815 if (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0))
820 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
821 vcl_handle_mq_event (wrk, e);
822 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
824 while (clib_time_now (&wrk->clib_time) < timeout);
826 VDBG (0, "timeout waiting for state 0x%x (%s)", state,
827 vppcom_session_state_str (state));
828 vcl_evt (VCL_EVT_SESSION_TIMEOUT, session, session_state);
830 return VPPCOM_ETIMEDOUT;
834 vcl_handle_pending_wrk_updates (vcl_worker_t * wrk)
836 vcl_session_state_t state;
840 if (PREDICT_TRUE (vec_len (wrk->pending_session_wrk_updates) == 0))
843 vec_foreach (sip, wrk->pending_session_wrk_updates)
845 s = vcl_session_get (wrk, *sip);
846 vcl_send_session_worker_update (wrk, s, wrk->wrk_index);
847 state = s->session_state;
848 vppcom_wait_for_session_state_change (s->session_index, STATE_UPDATED, 5);
849 s->session_state = state;
851 vec_reset_length (wrk->pending_session_wrk_updates);
855 vcl_flush_mq_events (void)
857 vcl_worker_t *wrk = vcl_worker_get_current ();
858 svm_msg_q_msg_t *msg;
863 mq = wrk->app_event_queue;
865 vcl_mq_dequeue_batch (wrk, mq, ~0);
866 svm_msg_q_unlock (mq);
868 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
870 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
871 e = svm_msg_q_msg_data (mq, msg);
872 vcl_handle_mq_event (wrk, e);
873 svm_msg_q_free_msg (mq, msg);
875 vec_reset_length (wrk->mq_msg_vector);
876 vcl_handle_pending_wrk_updates (wrk);
880 vppcom_app_session_enable (void)
884 if (vcm->app_state != STATE_APP_ENABLED)
886 vppcom_send_session_enable_disable (1 /* is_enabled == TRUE */ );
887 rv = vcl_wait_for_app_state_change (STATE_APP_ENABLED);
888 if (PREDICT_FALSE (rv))
890 VDBG (0, "application session enable timed out! returning %d (%s)",
891 rv, vppcom_retval_str (rv));
899 vppcom_app_attach (void)
903 vppcom_app_send_attach ();
904 rv = vcl_wait_for_app_state_change (STATE_APP_ATTACHED);
905 if (PREDICT_FALSE (rv))
907 VDBG (0, "application attach timed out! returning %d (%s)", rv,
908 vppcom_retval_str (rv));
916 vppcom_session_unbind (u32 session_handle)
918 vcl_worker_t *wrk = vcl_worker_get_current ();
919 session_accepted_msg_t *accepted_msg;
920 vcl_session_t *session = 0;
921 vcl_session_msg_t *evt;
923 session = vcl_session_get_w_handle (wrk, session_handle);
925 return VPPCOM_EBADFD;
927 /* Flush pending accept events, if any */
928 while (clib_fifo_elts (session->accept_evts_fifo))
930 clib_fifo_sub2 (session->accept_evts_fifo, evt);
931 accepted_msg = &evt->accepted_msg;
932 vcl_session_table_del_vpp_handle (wrk, accepted_msg->handle);
933 vcl_send_session_accepted_reply (session->vpp_evt_q,
934 accepted_msg->context,
935 session->vpp_handle, -1);
937 clib_fifo_free (session->accept_evts_fifo);
939 vcl_send_session_unlisten (wrk, session);
941 VDBG (1, "session %u [0x%llx]: sending unbind!", session->session_index,
942 session->vpp_handle);
943 vcl_evt (VCL_EVT_UNBIND, session);
945 session->vpp_handle = ~0;
946 session->session_state = STATE_DISCONNECT;
952 vppcom_session_disconnect (u32 session_handle)
954 vcl_worker_t *wrk = vcl_worker_get_current ();
955 svm_msg_q_t *vpp_evt_q;
956 vcl_session_t *session, *listen_session;
957 vcl_session_state_t state;
960 session = vcl_session_get_w_handle (wrk, session_handle);
962 return VPPCOM_EBADFD;
964 vpp_handle = session->vpp_handle;
965 state = session->session_state;
967 VDBG (1, "session %u [0x%llx] state 0x%x (%s)", session->session_index,
968 vpp_handle, state, vppcom_session_state_str (state));
970 if (PREDICT_FALSE (state & STATE_LISTEN))
972 VDBG (0, "ERROR: Cannot disconnect a listen socket!");
973 return VPPCOM_EBADFD;
976 if (state & STATE_VPP_CLOSING)
978 vpp_evt_q = vcl_session_vpp_evt_q (wrk, session);
979 vcl_send_session_disconnected_reply (vpp_evt_q, wrk->my_client_index,
981 VDBG (1, "session %u [0x%llx]: sending disconnect REPLY...",
982 session->session_index, vpp_handle);
986 VDBG (1, "session %u [0x%llx]: sending disconnect...",
987 session->session_index, vpp_handle);
988 vcl_send_session_disconnect (wrk, session);
991 if (session->listener_index != VCL_INVALID_SESSION_INDEX)
993 listen_session = vcl_session_get (wrk, session->listener_index);
994 listen_session->n_accepted_sessions--;
1003 * Notify vpp of the disconnect and mark the worker as free. If we're the
1004 * last worker, do a full cleanup otherwise, since we're probably a forked
1005 * child, avoid syscalls as much as possible. We might've lost privileges.
1008 vppcom_app_exit (void)
1010 if (!pool_elts (vcm->workers))
1012 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
1013 vcl_set_worker_index (~0);
1014 vcl_elog_stop (vcm);
1015 if (vec_len (vcm->workers) == 1)
1016 vppcom_disconnect_from_vpp ();
1018 vl_client_send_disconnect (1 /* vpp should cleanup */ );
1022 * VPPCOM Public API functions
1025 vppcom_app_create (char *app_name)
1027 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
1032 VDBG (1, "already initialized");
1033 return VPPCOM_EEXIST;
1037 vppcom_cfg (&vcm->cfg);
1038 vcl_cfg = &vcm->cfg;
1040 vcm->main_cpu = pthread_self ();
1041 vcm->main_pid = getpid ();
1042 vcm->app_name = format (0, "%s", app_name);
1043 vppcom_init_error_string_table ();
1044 fifo_segment_main_init (&vcm->segment_main, vcl_cfg->segment_baseva,
1045 20 /* timeout in secs */ );
1046 pool_alloc (vcm->workers, vcl_cfg->max_workers);
1047 clib_spinlock_init (&vcm->workers_lock);
1048 clib_rwlock_init (&vcm->segment_table_lock);
1049 atexit (vppcom_app_exit);
1051 /* Allocate default worker */
1052 vcl_worker_alloc_and_init ();
1054 /* API hookup and connect to VPP */
1055 vppcom_api_hookup ();
1056 vcl_elog_init (vcm);
1057 vcm->app_state = STATE_APP_START;
1058 rv = vppcom_connect_to_vpp (app_name);
1061 VERR ("couldn't connect to VPP!");
1064 VDBG (0, "sending session enable");
1065 rv = vppcom_app_session_enable ();
1068 VERR ("vppcom_app_session_enable() failed!");
1072 VDBG (0, "sending app attach");
1073 rv = vppcom_app_attach ();
1076 VERR ("vppcom_app_attach() failed!");
1080 VDBG (0, "app_name '%s', my_client_index %d (0x%x)", app_name,
1081 vcm->workers[0].my_client_index, vcm->workers[0].my_client_index);
1087 vppcom_app_destroy (void)
1090 f64 orig_app_timeout;
1092 if (!pool_elts (vcm->workers))
1095 vcl_evt (VCL_EVT_DETACH, vcm);
1097 if (pool_elts (vcm->workers) == 1)
1099 vcl_send_app_detach (vcl_worker_get_current ());
1100 orig_app_timeout = vcm->cfg.app_timeout;
1101 vcm->cfg.app_timeout = 2.0;
1102 rv = vcl_wait_for_app_state_change (STATE_APP_ENABLED);
1103 vcm->cfg.app_timeout = orig_app_timeout;
1104 if (PREDICT_FALSE (rv))
1105 VDBG (0, "application detach timed out! returning %d (%s)", rv,
1106 vppcom_retval_str (rv));
1107 vec_free (vcm->app_name);
1108 vcl_worker_cleanup (vcl_worker_get_current (), 0 /* notify vpp */ );
1112 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
1115 vcl_set_worker_index (~0);
1116 vcl_elog_stop (vcm);
1117 vl_client_disconnect_from_vlib ();
1121 vppcom_session_create (u8 proto, u8 is_nonblocking)
1123 vcl_worker_t *wrk = vcl_worker_get_current ();
1124 vcl_session_t *session;
1126 session = vcl_session_alloc (wrk);
1128 session->session_type = proto;
1129 session->session_state = STATE_START;
1130 session->vpp_handle = ~0;
1131 session->is_dgram = vcl_proto_is_dgram (proto);
1134 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK);
1136 vcl_evt (VCL_EVT_CREATE, session, session_type, session->session_state,
1137 is_nonblocking, session_index);
1139 VDBG (0, "created session %u", session->session_index);
1141 return vcl_session_handle (session);
1145 vcl_session_cleanup (vcl_worker_t * wrk, vcl_session_t * session,
1146 vcl_session_handle_t sh, u8 do_disconnect)
1148 vcl_session_state_t state;
1149 u32 next_sh, vep_sh;
1154 is_vep = session->is_vep;
1155 next_sh = session->vep.next_sh;
1156 vep_sh = session->vep.vep_sh;
1157 state = session->session_state;
1158 vpp_handle = session->vpp_handle;
1160 VDBG (1, "session %u [0x%llx] closing", session->session_index, vpp_handle);
1164 while (next_sh != ~0)
1166 rv = vppcom_epoll_ctl (sh, EPOLL_CTL_DEL, next_sh, 0);
1167 if (PREDICT_FALSE (rv < 0))
1168 VDBG (0, "vpp handle 0x%llx, sh %u: EPOLL_CTL_DEL vep_idx %u"
1169 " failed! rv %d (%s)", vpp_handle, next_sh, vep_sh, rv,
1170 vppcom_retval_str (rv));
1172 next_sh = session->vep.next_sh;
1177 if (session->is_vep_session)
1179 rv = vppcom_epoll_ctl (vep_sh, EPOLL_CTL_DEL, sh, 0);
1181 VDBG (0, "session %u [0x%llx]: EPOLL_CTL_DEL vep_idx %u "
1182 "failed! rv %d (%s)", session->session_index, vpp_handle,
1183 vep_sh, rv, vppcom_retval_str (rv));
1188 VDBG (1, "session %u [0x%llx] disconnect skipped",
1189 session->session_index, vpp_handle);
1193 if (state & STATE_LISTEN)
1195 rv = vppcom_session_unbind (sh);
1196 if (PREDICT_FALSE (rv < 0))
1197 VDBG (0, "session %u [0x%llx]: listener unbind failed! "
1198 "rv %d (%s)", session->session_index, vpp_handle, rv,
1199 vppcom_retval_str (rv));
1202 else if ((state & STATE_OPEN)
1203 || (vcl_session_is_connectable_listener (wrk, session)))
1205 rv = vppcom_session_disconnect (sh);
1206 if (PREDICT_FALSE (rv < 0))
1207 VDBG (0, "ERROR: session %u [0x%llx]: disconnect failed!"
1208 " rv %d (%s)", session->session_index, vpp_handle,
1209 rv, vppcom_retval_str (rv));
1211 else if (state == STATE_DISCONNECT)
1213 svm_msg_q_t *mq = vcl_session_vpp_evt_q (wrk, session);
1214 vcl_send_session_reset_reply (mq, wrk->my_client_index,
1215 session->vpp_handle, 0);
1219 VDBG (0, "session %u [0x%llx] removed", session->session_index, vpp_handle);
1222 vcl_session_table_del_vpp_handle (wrk, vpp_handle);
1223 vcl_session_free (wrk, session);
1224 vcl_evt (VCL_EVT_CLOSE, session, rv);
1230 vppcom_session_close (uint32_t session_handle)
1232 vcl_worker_t *wrk = vcl_worker_get_current ();
1233 vcl_session_t *session;
1235 session = vcl_session_get_w_handle (wrk, session_handle);
1237 return VPPCOM_EBADFD;
1238 return vcl_session_cleanup (wrk, session, session_handle,
1239 1 /* do_disconnect */ );
1243 vppcom_session_bind (uint32_t session_handle, vppcom_endpt_t * ep)
1245 vcl_worker_t *wrk = vcl_worker_get_current ();
1246 vcl_session_t *session = 0;
1249 return VPPCOM_EINVAL;
1251 session = vcl_session_get_w_handle (wrk, session_handle);
1253 return VPPCOM_EBADFD;
1255 if (session->is_vep)
1257 VDBG (0, "ERROR: cannot bind to epoll session %u!",
1258 session->session_index);
1259 return VPPCOM_EBADFD;
1262 session->transport.is_ip4 = ep->is_ip4;
1264 clib_memcpy_fast (&session->transport.lcl_ip.ip4, ep->ip,
1265 sizeof (ip4_address_t));
1267 clib_memcpy_fast (&session->transport.lcl_ip.ip6, ep->ip,
1268 sizeof (ip6_address_t));
1269 session->transport.lcl_port = ep->port;
1271 VDBG (0, "session %u handle %u: binding to local %s address %U port %u, "
1272 "proto %s", session->session_index, session_handle,
1273 session->transport.is_ip4 ? "IPv4" : "IPv6",
1274 format_ip46_address, &session->transport.lcl_ip,
1275 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
1276 clib_net_to_host_u16 (session->transport.lcl_port),
1277 vppcom_proto_str (session->session_type));
1278 vcl_evt (VCL_EVT_BIND, session);
1280 if (session->session_type == VPPCOM_PROTO_UDP)
1281 vppcom_session_listen (session_handle, 10);
1287 vppcom_session_listen (uint32_t listen_sh, uint32_t q_len)
1289 vcl_worker_t *wrk = vcl_worker_get_current ();
1290 vcl_session_t *listen_session = 0;
1291 u64 listen_vpp_handle;
1294 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
1295 if (!listen_session || listen_session->is_vep)
1296 return VPPCOM_EBADFD;
1298 if (q_len == 0 || q_len == ~0)
1299 q_len = vcm->cfg.listen_queue_size;
1301 listen_vpp_handle = listen_session->vpp_handle;
1302 if (listen_session->session_state & STATE_LISTEN)
1304 VDBG (0, "session %u [0x%llx]: already in listen state!",
1305 listen_sh, listen_vpp_handle);
1309 VDBG (0, "session %u: sending vpp listen request...", listen_sh);
1312 * Send listen request to vpp and wait for reply
1314 vcl_send_session_listen (wrk, listen_session);
1315 rv = vppcom_wait_for_session_state_change (listen_session->session_index,
1317 vcm->cfg.session_timeout);
1319 if (PREDICT_FALSE (rv))
1321 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
1322 VDBG (0, "session %u [0x%llx]: listen failed! returning %d (%s)",
1323 listen_sh, listen_session->vpp_handle, rv,
1324 vppcom_retval_str (rv));
1332 vppcom_session_tls_add_cert (uint32_t session_handle, char *cert,
1336 vcl_worker_t *wrk = vcl_worker_get_current ();
1337 vcl_session_t *session = 0;
1339 session = vcl_session_get_w_handle (wrk, session_handle);
1341 return VPPCOM_EBADFD;
1343 if (cert_len == 0 || cert_len == ~0)
1344 return VPPCOM_EBADFD;
1347 * Send listen request to vpp and wait for reply
1349 vppcom_send_application_tls_cert_add (session, cert, cert_len);
1350 vcm->app_state = STATE_APP_ADDING_TLS_DATA;
1351 vcl_wait_for_app_state_change (STATE_APP_READY);
1357 vppcom_session_tls_add_key (uint32_t session_handle, char *key,
1361 vcl_worker_t *wrk = vcl_worker_get_current ();
1362 vcl_session_t *session = 0;
1364 session = vcl_session_get_w_handle (wrk, session_handle);
1366 return VPPCOM_EBADFD;
1368 if (key_len == 0 || key_len == ~0)
1369 return VPPCOM_EBADFD;
1371 vppcom_send_application_tls_key_add (session, key, key_len);
1372 vcm->app_state = STATE_APP_ADDING_TLS_DATA;
1373 vcl_wait_for_app_state_change (STATE_APP_READY);
1378 validate_args_session_accept_ (vcl_worker_t * wrk, vcl_session_t * ls)
1382 VDBG (0, "ERROR: cannot accept on epoll session %u!",
1384 return VPPCOM_EBADFD;
1387 if ((ls->session_state != STATE_LISTEN)
1388 && (!vcl_session_is_connectable_listener (wrk, ls)))
1391 "ERROR: session [0x%llx]: not in listen state! state 0x%x"
1392 " (%s)", ls->vpp_handle, ls->session_state,
1393 vppcom_session_state_str (ls->session_state));
1394 return VPPCOM_EBADFD;
1400 vppcom_unformat_proto (uint8_t * proto, char *proto_str)
1402 if (!strcmp (proto_str, "TCP"))
1403 *proto = VPPCOM_PROTO_TCP;
1404 else if (!strcmp (proto_str, "tcp"))
1405 *proto = VPPCOM_PROTO_TCP;
1406 else if (!strcmp (proto_str, "UDP"))
1407 *proto = VPPCOM_PROTO_UDP;
1408 else if (!strcmp (proto_str, "udp"))
1409 *proto = VPPCOM_PROTO_UDP;
1410 else if (!strcmp (proto_str, "UDPC"))
1411 *proto = VPPCOM_PROTO_UDPC;
1412 else if (!strcmp (proto_str, "udpc"))
1413 *proto = VPPCOM_PROTO_UDPC;
1414 else if (!strcmp (proto_str, "SCTP"))
1415 *proto = VPPCOM_PROTO_SCTP;
1416 else if (!strcmp (proto_str, "sctp"))
1417 *proto = VPPCOM_PROTO_SCTP;
1418 else if (!strcmp (proto_str, "TLS"))
1419 *proto = VPPCOM_PROTO_TLS;
1420 else if (!strcmp (proto_str, "tls"))
1421 *proto = VPPCOM_PROTO_TLS;
1422 else if (!strcmp (proto_str, "QUIC"))
1423 *proto = VPPCOM_PROTO_QUIC;
1424 else if (!strcmp (proto_str, "quic"))
1425 *proto = VPPCOM_PROTO_QUIC;
1432 vppcom_session_accept (uint32_t listen_session_handle, vppcom_endpt_t * ep,
1435 u32 client_session_index = ~0, listen_session_index, accept_flags = 0;
1436 vcl_worker_t *wrk = vcl_worker_get_current ();
1437 session_accepted_msg_t accepted_msg;
1438 vcl_session_t *listen_session = 0;
1439 vcl_session_t *client_session = 0;
1440 vcl_session_msg_t *evt;
1441 svm_msg_q_msg_t msg;
1446 listen_session = vcl_session_get_w_handle (wrk, listen_session_handle);
1447 if (!listen_session)
1448 return VPPCOM_EBADFD;
1450 listen_session_index = listen_session->session_index;
1451 if ((rv = validate_args_session_accept_ (wrk, listen_session)))
1454 if (clib_fifo_elts (listen_session->accept_evts_fifo))
1456 clib_fifo_sub2 (listen_session->accept_evts_fifo, evt);
1457 accept_flags = evt->flags;
1458 accepted_msg = evt->accepted_msg;
1462 is_nonblocking = VCL_SESS_ATTR_TEST (listen_session->attr,
1463 VCL_SESS_ATTR_NONBLOCK);
1464 if (svm_msg_q_is_empty (wrk->app_event_queue) && is_nonblocking)
1465 return VPPCOM_EAGAIN;
1469 if (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_WAIT, 0))
1470 return VPPCOM_EAGAIN;
1472 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
1473 if (e->event_type != SESSION_CTRL_EVT_ACCEPTED)
1475 VDBG (0, "discarded event: %u", e->event_type);
1476 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
1479 clib_memcpy_fast (&accepted_msg, e->data, sizeof (accepted_msg));
1480 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
1486 client_session_index = vcl_session_accepted_handler (wrk, &accepted_msg,
1487 listen_session_index);
1488 if (client_session_index == VCL_INVALID_SESSION_INDEX)
1489 return VPPCOM_ECONNABORTED;
1491 listen_session = vcl_session_get (wrk, listen_session_index);
1492 client_session = vcl_session_get (wrk, client_session_index);
1494 if (flags & O_NONBLOCK)
1495 VCL_SESS_ATTR_SET (client_session->attr, VCL_SESS_ATTR_NONBLOCK);
1497 VDBG (1, "listener %u [0x%llx]: Got a connect request! session %u [0x%llx],"
1498 " flags %d, is_nonblocking %u", listen_session->session_index,
1499 listen_session->vpp_handle, client_session_index,
1500 client_session->vpp_handle, flags,
1501 VCL_SESS_ATTR_TEST (client_session->attr, VCL_SESS_ATTR_NONBLOCK));
1505 ep->is_ip4 = client_session->transport.is_ip4;
1506 ep->port = client_session->transport.rmt_port;
1507 if (client_session->transport.is_ip4)
1508 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip4,
1509 sizeof (ip4_address_t));
1511 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip6,
1512 sizeof (ip6_address_t));
1515 VDBG (0, "listener %u [0x%llx] accepted %u [0x%llx] peer: %U:%u "
1516 "local: %U:%u", listen_session_handle, listen_session->vpp_handle,
1517 client_session_index, client_session->vpp_handle,
1518 format_ip46_address, &client_session->transport.rmt_ip,
1519 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
1520 clib_net_to_host_u16 (client_session->transport.rmt_port),
1521 format_ip46_address, &client_session->transport.lcl_ip,
1522 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
1523 clib_net_to_host_u16 (client_session->transport.lcl_port));
1524 vcl_evt (VCL_EVT_ACCEPT, client_session, listen_session,
1525 client_session_index);
1528 * Session might have been closed already
1532 if (accept_flags & VCL_ACCEPTED_F_CLOSED)
1533 client_session->session_state = STATE_VPP_CLOSING;
1534 else if (accept_flags & VCL_ACCEPTED_F_RESET)
1535 client_session->session_state = STATE_DISCONNECT;
1537 return vcl_session_handle (client_session);
1541 vppcom_session_connect (uint32_t session_handle, vppcom_endpt_t * server_ep)
1543 vcl_worker_t *wrk = vcl_worker_get_current ();
1544 vcl_session_t *session = 0;
1548 session = vcl_session_get_w_handle (wrk, session_handle);
1550 return VPPCOM_EBADFD;
1551 session_index = session->session_index;
1553 if (PREDICT_FALSE (session->is_vep))
1555 VDBG (0, "ERROR: cannot connect epoll session %u!",
1556 session->session_index);
1557 return VPPCOM_EBADFD;
1560 if (PREDICT_FALSE (session->session_state & CLIENT_STATE_OPEN))
1562 VDBG (0, "session handle %u [0x%llx]: session already "
1563 "connected to %s %U port %d proto %s, state 0x%x (%s)",
1564 session_handle, session->vpp_handle,
1565 session->transport.is_ip4 ? "IPv4" : "IPv6", format_ip46_address,
1566 &session->transport.rmt_ip, session->transport.is_ip4 ?
1567 IP46_TYPE_IP4 : IP46_TYPE_IP6,
1568 clib_net_to_host_u16 (session->transport.rmt_port),
1569 vppcom_proto_str (session->session_type), session->session_state,
1570 vppcom_session_state_str (session->session_state));
1574 session->transport.is_ip4 = server_ep->is_ip4;
1575 if (session->transport.is_ip4)
1576 clib_memcpy_fast (&session->transport.rmt_ip.ip4, server_ep->ip,
1577 sizeof (ip4_address_t));
1579 clib_memcpy_fast (&session->transport.rmt_ip.ip6, server_ep->ip,
1580 sizeof (ip6_address_t));
1581 session->transport.rmt_port = server_ep->port;
1582 session->parent_handle = VCL_INVALID_SESSION_HANDLE;
1584 VDBG (0, "session handle %u: connecting to server %s %U "
1585 "port %d proto %s", session_handle,
1586 session->transport.is_ip4 ? "IPv4" : "IPv6",
1587 format_ip46_address,
1588 &session->transport.rmt_ip, session->transport.is_ip4 ?
1589 IP46_TYPE_IP4 : IP46_TYPE_IP6,
1590 clib_net_to_host_u16 (session->transport.rmt_port),
1591 vppcom_proto_str (session->session_type));
1594 * Send connect request and wait for reply from vpp
1596 vcl_send_session_connect (wrk, session);
1597 rv = vppcom_wait_for_session_state_change (session_index, STATE_CONNECT,
1598 vcm->cfg.session_timeout);
1600 session = vcl_session_get (wrk, session_index);
1601 VDBG (0, "session %u [0x%llx]: connect %s!", session->session_index,
1602 session->vpp_handle, rv ? "failed" : "succeeded");
1608 vppcom_session_stream_connect (uint32_t session_handle,
1609 uint32_t parent_session_handle)
1611 vcl_worker_t *wrk = vcl_worker_get_current ();
1612 vcl_session_t *session, *parent_session;
1613 u32 session_index, parent_session_index;
1616 session = vcl_session_get_w_handle (wrk, session_handle);
1618 return VPPCOM_EBADFD;
1619 parent_session = vcl_session_get_w_handle (wrk, parent_session_handle);
1620 if (!parent_session)
1621 return VPPCOM_EBADFD;
1623 session_index = session->session_index;
1624 parent_session_index = parent_session->session_index;
1625 if (PREDICT_FALSE (session->is_vep))
1627 VDBG (0, "ERROR: cannot connect epoll session %u!",
1628 session->session_index);
1629 return VPPCOM_EBADFD;
1632 if (PREDICT_FALSE (session->session_state & CLIENT_STATE_OPEN))
1634 VDBG (0, "session handle %u [0x%llx]: session already "
1635 "connected to session %u [0x%llx] proto %s, state 0x%x (%s)",
1636 session_handle, session->vpp_handle,
1637 parent_session_handle, parent_session->vpp_handle,
1638 vppcom_proto_str (session->session_type), session->session_state,
1639 vppcom_session_state_str (session->session_state));
1643 /* Connect to quic session specifics */
1644 session->transport.is_ip4 = parent_session->transport.is_ip4;
1645 session->transport.rmt_ip.ip4.as_u32 = (uint32_t) 1;
1646 session->transport.rmt_port = 0;
1647 session->parent_handle = parent_session->vpp_handle;
1649 VDBG (0, "session handle %u: connecting to session %u [0x%llx]",
1650 session_handle, parent_session_handle, parent_session->vpp_handle);
1653 * Send connect request and wait for reply from vpp
1655 vcl_send_session_connect (wrk, session);
1656 rv = vppcom_wait_for_session_state_change (session_index, STATE_CONNECT,
1657 vcm->cfg.session_timeout);
1659 session->listener_index = parent_session_index;
1660 parent_session = vcl_session_get_w_handle (wrk, parent_session_handle);
1662 parent_session->n_accepted_sessions++;
1664 session = vcl_session_get (wrk, session_index);
1665 VDBG (0, "session %u [0x%llx]: connect %s!", session->session_index,
1666 session->vpp_handle, rv ? "failed" : "succeeded");
1672 vcl_is_rx_evt_for_session (session_event_t * e, u32 sid, u8 is_ct)
1674 return (e->event_type == SESSION_IO_EVT_RX && e->session_index == sid);
1678 vppcom_session_read_internal (uint32_t session_handle, void *buf, int n,
1681 vcl_worker_t *wrk = vcl_worker_get_current ();
1682 int n_read = 0, is_nonblocking;
1683 vcl_session_t *s = 0;
1684 svm_fifo_t *rx_fifo;
1685 svm_msg_q_msg_t msg;
1690 if (PREDICT_FALSE (!buf))
1691 return VPPCOM_EINVAL;
1693 s = vcl_session_get_w_handle (wrk, session_handle);
1694 if (PREDICT_FALSE (!s || s->is_vep))
1695 return VPPCOM_EBADFD;
1697 if (PREDICT_FALSE (!vcl_session_is_open (s)))
1699 VDBG (0, "session %u[0x%llx] is not open! state 0x%x (%s)",
1700 s->session_index, s->vpp_handle, s->session_state,
1701 vppcom_session_state_str (s->session_state));
1702 return vcl_session_closed_error (s);
1705 is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
1706 is_ct = vcl_session_is_ct (s);
1707 mq = wrk->app_event_queue;
1708 rx_fifo = is_ct ? s->ct_rx_fifo : s->rx_fifo;
1711 if (svm_fifo_is_empty_cons (rx_fifo))
1715 svm_fifo_unset_event (s->rx_fifo);
1716 return VPPCOM_EWOULDBLOCK;
1718 while (svm_fifo_is_empty_cons (rx_fifo))
1720 if (vcl_session_is_closing (s))
1721 return vcl_session_closing_error (s);
1723 svm_fifo_unset_event (s->rx_fifo);
1724 svm_msg_q_lock (mq);
1725 if (svm_msg_q_is_empty (mq))
1726 svm_msg_q_wait (mq);
1728 svm_msg_q_sub_w_lock (mq, &msg);
1729 e = svm_msg_q_msg_data (mq, &msg);
1730 svm_msg_q_unlock (mq);
1731 if (!vcl_is_rx_evt_for_session (e, s->session_index, is_ct))
1732 vcl_handle_mq_event (wrk, e);
1733 svm_msg_q_free_msg (mq, &msg);
1738 n_read = app_recv_dgram_raw (rx_fifo, buf, n, &s->transport, 0, peek);
1740 n_read = app_recv_stream_raw (rx_fifo, buf, n, 0, peek);
1742 if (svm_fifo_is_empty_cons (rx_fifo))
1743 svm_fifo_unset_event (s->rx_fifo);
1745 /* Cut-through sessions might request tx notifications on rx fifos */
1746 if (PREDICT_FALSE (rx_fifo->want_deq_ntf))
1748 app_send_io_evt_to_vpp (s->vpp_evt_q, s->rx_fifo->master_session_index,
1749 SESSION_IO_EVT_RX, SVM_Q_WAIT);
1750 svm_fifo_reset_has_deq_ntf (s->rx_fifo);
1753 VDBG (2, "session %u[0x%llx]: read %d bytes from (%p)", s->session_index,
1754 s->vpp_handle, n_read, rx_fifo);
1760 vppcom_session_read (uint32_t session_handle, void *buf, size_t n)
1762 return (vppcom_session_read_internal (session_handle, buf, n, 0));
1766 vppcom_session_peek (uint32_t session_handle, void *buf, int n)
1768 return (vppcom_session_read_internal (session_handle, buf, n, 1));
1772 vppcom_session_read_segments (uint32_t session_handle,
1773 vppcom_data_segments_t ds)
1775 vcl_worker_t *wrk = vcl_worker_get_current ();
1776 int n_read = 0, is_nonblocking;
1777 vcl_session_t *s = 0;
1778 svm_fifo_t *rx_fifo;
1779 svm_msg_q_msg_t msg;
1784 s = vcl_session_get_w_handle (wrk, session_handle);
1785 if (PREDICT_FALSE (!s || s->is_vep))
1786 return VPPCOM_EBADFD;
1788 if (PREDICT_FALSE (!vcl_session_is_open (s)))
1789 return vcl_session_closed_error (s);
1791 is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
1792 is_ct = vcl_session_is_ct (s);
1793 mq = is_ct ? s->our_evt_q : wrk->app_event_queue;
1794 rx_fifo = s->rx_fifo;
1798 svm_fifo_unset_event (s->rx_fifo);
1800 if (svm_fifo_is_empty_cons (rx_fifo))
1804 svm_fifo_unset_event (rx_fifo);
1805 return VPPCOM_EWOULDBLOCK;
1807 while (svm_fifo_is_empty_cons (rx_fifo))
1809 if (vcl_session_is_closing (s))
1810 return vcl_session_closing_error (s);
1812 svm_fifo_unset_event (rx_fifo);
1813 svm_msg_q_lock (mq);
1814 if (svm_msg_q_is_empty (mq))
1815 svm_msg_q_wait (mq);
1817 svm_msg_q_sub_w_lock (mq, &msg);
1818 e = svm_msg_q_msg_data (mq, &msg);
1819 svm_msg_q_unlock (mq);
1820 if (!vcl_is_rx_evt_for_session (e, s->session_index, is_ct))
1821 vcl_handle_mq_event (wrk, e);
1822 svm_msg_q_free_msg (mq, &msg);
1826 n_read = svm_fifo_segments (rx_fifo, (svm_fifo_seg_t *) ds);
1827 svm_fifo_unset_event (rx_fifo);
1833 vppcom_session_free_segments (uint32_t session_handle,
1834 vppcom_data_segments_t ds)
1836 vcl_worker_t *wrk = vcl_worker_get_current ();
1839 s = vcl_session_get_w_handle (wrk, session_handle);
1840 if (PREDICT_FALSE (!s || s->is_vep))
1843 svm_fifo_segments_free (s->rx_fifo, (svm_fifo_seg_t *) ds);
1847 vppcom_data_segment_copy (void *buf, vppcom_data_segments_t ds, u32 max_bytes)
1849 u32 first_copy = clib_min (ds[0].len, max_bytes);
1850 clib_memcpy_fast (buf, ds[0].data, first_copy);
1851 if (first_copy < max_bytes)
1853 clib_memcpy_fast (buf + first_copy, ds[1].data,
1854 clib_min (ds[1].len, max_bytes - first_copy));
1860 vcl_is_tx_evt_for_session (session_event_t * e, u32 sid, u8 is_ct)
1862 return (e->event_type == SESSION_IO_EVT_TX && e->session_index == sid);
1866 vppcom_session_write_inline (uint32_t session_handle, void *buf, size_t n,
1869 vcl_worker_t *wrk = vcl_worker_get_current ();
1870 int n_write, is_nonblocking;
1871 vcl_session_t *s = 0;
1872 session_evt_type_t et;
1873 svm_msg_q_msg_t msg;
1874 svm_fifo_t *tx_fifo;
1879 if (PREDICT_FALSE (!buf))
1880 return VPPCOM_EINVAL;
1882 s = vcl_session_get_w_handle (wrk, session_handle);
1883 if (PREDICT_FALSE (!s))
1884 return VPPCOM_EBADFD;
1886 if (PREDICT_FALSE (s->is_vep))
1888 VDBG (0, "ERROR: session %u [0x%llx]: cannot write to an epoll"
1889 " session!", s->session_index, s->vpp_handle);
1890 return VPPCOM_EBADFD;
1893 if (PREDICT_FALSE (!vcl_session_is_open (s)))
1895 VDBG (1, "session %u [0x%llx]: is not open! state 0x%x (%s)",
1896 s->session_index, s->vpp_handle, s->session_state,
1897 vppcom_session_state_str (s->session_state));
1898 return vcl_session_closed_error (s);;
1901 is_ct = vcl_session_is_ct (s);
1902 tx_fifo = is_ct ? s->ct_tx_fifo : s->tx_fifo;
1903 is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
1905 mq = wrk->app_event_queue;
1906 if (svm_fifo_is_full_prod (tx_fifo))
1910 return VPPCOM_EWOULDBLOCK;
1912 while (svm_fifo_is_full_prod (tx_fifo))
1914 svm_fifo_add_want_deq_ntf (tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
1915 if (vcl_session_is_closing (s))
1916 return vcl_session_closing_error (s);
1917 svm_msg_q_lock (mq);
1918 if (svm_msg_q_is_empty (mq))
1919 svm_msg_q_wait (mq);
1921 svm_msg_q_sub_w_lock (mq, &msg);
1922 e = svm_msg_q_msg_data (mq, &msg);
1923 svm_msg_q_unlock (mq);
1925 if (!vcl_is_tx_evt_for_session (e, s->session_index, is_ct))
1926 vcl_handle_mq_event (wrk, e);
1927 svm_msg_q_free_msg (mq, &msg);
1931 et = SESSION_IO_EVT_TX;
1932 if (is_flush && !is_ct)
1933 et = SESSION_IO_EVT_TX_FLUSH;
1936 n_write = app_send_dgram_raw (tx_fifo, &s->transport,
1937 s->vpp_evt_q, buf, n, et,
1938 0 /* do_evt */ , SVM_Q_WAIT);
1940 n_write = app_send_stream_raw (tx_fifo, s->vpp_evt_q, buf, n, et,
1941 0 /* do_evt */ , SVM_Q_WAIT);
1943 if (svm_fifo_set_event (s->tx_fifo))
1944 app_send_io_evt_to_vpp (s->vpp_evt_q, s->tx_fifo->master_session_index,
1947 ASSERT (n_write > 0);
1949 VDBG (2, "session %u [0x%llx]: wrote %d bytes", s->session_index,
1950 s->vpp_handle, n_write);
1956 vppcom_session_write (uint32_t session_handle, void *buf, size_t n)
1958 return vppcom_session_write_inline (session_handle, buf, n,
1963 vppcom_session_write_msg (uint32_t session_handle, void *buf, size_t n)
1965 return vppcom_session_write_inline (session_handle, buf, n,
1969 #define vcl_fifo_rx_evt_valid_or_break(_s) \
1970 if (PREDICT_FALSE (svm_fifo_is_empty (_s->rx_fifo))) \
1972 if (!vcl_session_is_ct (_s)) \
1974 svm_fifo_unset_event (_s->rx_fifo); \
1975 if (svm_fifo_is_empty (_s->rx_fifo)) \
1978 else if (svm_fifo_is_empty (_s->ct_rx_fifo)) \
1980 svm_fifo_unset_event (_s->ct_rx_fifo); \
1981 if (svm_fifo_is_empty (_s->ct_rx_fifo)) \
1987 vcl_select_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
1988 unsigned long n_bits, unsigned long *read_map,
1989 unsigned long *write_map,
1990 unsigned long *except_map, u32 * bits_set)
1992 session_disconnected_msg_t *disconnected_msg;
1993 session_connected_msg_t *connected_msg;
1994 vcl_session_t *session;
1997 switch (e->event_type)
1999 case SESSION_IO_EVT_RX:
2000 sid = e->session_index;
2001 session = vcl_session_get (wrk, sid);
2004 vcl_fifo_rx_evt_valid_or_break (session);
2005 if (sid < n_bits && read_map)
2007 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
2011 case SESSION_IO_EVT_TX:
2012 sid = e->session_index;
2013 session = vcl_session_get (wrk, sid);
2016 if (sid < n_bits && write_map)
2018 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
2022 case SESSION_CTRL_EVT_ACCEPTED:
2023 session = vcl_session_accepted (wrk,
2024 (session_accepted_msg_t *) e->data);
2027 sid = session->session_index;
2028 if (sid < n_bits && read_map)
2030 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
2034 case SESSION_CTRL_EVT_CONNECTED:
2035 connected_msg = (session_connected_msg_t *) e->data;
2036 vcl_session_connected_handler (wrk, connected_msg);
2038 case SESSION_CTRL_EVT_DISCONNECTED:
2039 disconnected_msg = (session_disconnected_msg_t *) e->data;
2040 session = vcl_session_disconnected_handler (wrk, disconnected_msg);
2043 sid = session->session_index;
2044 if (sid < n_bits && except_map)
2046 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
2050 case SESSION_CTRL_EVT_RESET:
2051 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
2052 if (sid < n_bits && except_map)
2054 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
2058 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
2059 vcl_session_unlisten_reply_handler (wrk, e->data);
2061 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
2062 vcl_session_worker_update_reply_handler (wrk, e->data);
2064 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
2065 vcl_session_req_worker_update_handler (wrk, e->data);
2068 clib_warning ("unhandled: %u", e->event_type);
2074 vcl_select_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
2075 unsigned long n_bits, unsigned long *read_map,
2076 unsigned long *write_map, unsigned long *except_map,
2077 double time_to_wait, u32 * bits_set)
2079 svm_msg_q_msg_t *msg;
2083 svm_msg_q_lock (mq);
2084 if (svm_msg_q_is_empty (mq))
2088 svm_msg_q_unlock (mq);
2094 svm_msg_q_unlock (mq);
2097 else if (time_to_wait < 0)
2099 svm_msg_q_wait (mq);
2103 if (svm_msg_q_timedwait (mq, time_to_wait))
2105 svm_msg_q_unlock (mq);
2110 vcl_mq_dequeue_batch (wrk, mq, ~0);
2111 svm_msg_q_unlock (mq);
2113 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
2115 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
2116 e = svm_msg_q_msg_data (mq, msg);
2117 vcl_select_handle_mq_event (wrk, e, n_bits, read_map, write_map,
2118 except_map, bits_set);
2119 svm_msg_q_free_msg (mq, msg);
2121 vec_reset_length (wrk->mq_msg_vector);
2122 vcl_handle_pending_wrk_updates (wrk);
2127 vppcom_select_condvar (vcl_worker_t * wrk, int n_bits,
2128 vcl_si_set * read_map, vcl_si_set * write_map,
2129 vcl_si_set * except_map, double time_to_wait,
2132 double wait = 0, start = 0;
2136 wait = time_to_wait;
2137 start = clib_time_now (&wrk->clib_time);
2142 vcl_select_handle_mq (wrk, wrk->app_event_queue, n_bits, read_map,
2143 write_map, except_map, wait, bits_set);
2149 wait = wait - (clib_time_now (&wrk->clib_time) - start);
2157 vppcom_select_eventfd (vcl_worker_t * wrk, int n_bits,
2158 vcl_si_set * read_map, vcl_si_set * write_map,
2159 vcl_si_set * except_map, double time_to_wait,
2162 vcl_mq_evt_conn_t *mqc;
2163 int __clib_unused n_read;
2167 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
2168 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
2169 vec_len (wrk->mq_events), time_to_wait);
2170 for (i = 0; i < n_mq_evts; i++)
2172 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
2173 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
2174 vcl_select_handle_mq (wrk, mqc->mq, n_bits, read_map, write_map,
2175 except_map, 0, bits_set);
2178 return (n_mq_evts > 0 ? (int) *bits_set : 0);
2182 vppcom_select (int n_bits, vcl_si_set * read_map, vcl_si_set * write_map,
2183 vcl_si_set * except_map, double time_to_wait)
2185 u32 sid, minbits = clib_max (n_bits, BITS (uword)), bits_set = 0;
2186 vcl_worker_t *wrk = vcl_worker_get_current ();
2187 vcl_session_t *session = 0;
2190 if (n_bits && read_map)
2192 clib_bitmap_validate (wrk->rd_bitmap, minbits);
2193 clib_memcpy_fast (wrk->rd_bitmap, read_map,
2194 vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
2195 memset (read_map, 0, vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
2197 if (n_bits && write_map)
2199 clib_bitmap_validate (wrk->wr_bitmap, minbits);
2200 clib_memcpy_fast (wrk->wr_bitmap, write_map,
2201 vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
2202 memset (write_map, 0, vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
2204 if (n_bits && except_map)
2206 clib_bitmap_validate (wrk->ex_bitmap, minbits);
2207 clib_memcpy_fast (wrk->ex_bitmap, except_map,
2208 vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
2209 memset (except_map, 0, vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
2219 clib_bitmap_foreach (sid, wrk->wr_bitmap, ({
2220 if (!(session = vcl_session_get (wrk, sid)))
2222 if (except_map && sid < minbits)
2223 clib_bitmap_set_no_check (except_map, sid, 1);
2227 rv = svm_fifo_is_full_prod (session->tx_fifo);
2230 clib_bitmap_set_no_check ((uword*)write_map, sid, 1);
2234 svm_fifo_add_want_deq_ntf (session->tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
2241 clib_bitmap_foreach (sid, wrk->rd_bitmap, ({
2242 if (!(session = vcl_session_get (wrk, sid)))
2244 if (except_map && sid < minbits)
2245 clib_bitmap_set_no_check (except_map, sid, 1);
2249 rv = vcl_session_read_ready (session);
2252 clib_bitmap_set_no_check ((uword*)read_map, sid, 1);
2260 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
2262 vcl_select_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i], n_bits,
2263 read_map, write_map, except_map, &bits_set);
2265 vec_reset_length (wrk->unhandled_evts_vector);
2267 if (vcm->cfg.use_mq_eventfd)
2268 vppcom_select_eventfd (wrk, n_bits, read_map, write_map, except_map,
2269 time_to_wait, &bits_set);
2271 vppcom_select_condvar (wrk, n_bits, read_map, write_map, except_map,
2272 time_to_wait, &bits_set);
2278 vep_verify_epoll_chain (vcl_worker_t * wrk, u32 vep_handle)
2280 vcl_session_t *session;
2281 vppcom_epoll_t *vep;
2282 u32 sh = vep_handle;
2284 if (VPPCOM_DEBUG <= 2)
2287 session = vcl_session_get_w_handle (wrk, vep_handle);
2288 if (PREDICT_FALSE (!session))
2290 VDBG (0, "ERROR: Invalid vep_sh (%u)!", vep_handle);
2293 if (PREDICT_FALSE (!session->is_vep))
2295 VDBG (0, "ERROR: vep_sh (%u) is not a vep!", vep_handle);
2298 vep = &session->vep;
2299 VDBG (0, "vep_sh (%u): Dumping epoll chain\n"
2302 " is_vep_session = %u\n"
2303 " next_sh = 0x%x (%u)\n"
2304 "}\n", vep_handle, session->is_vep, session->is_vep_session,
2305 vep->next_sh, vep->next_sh);
2307 for (sh = vep->next_sh; sh != ~0; sh = vep->next_sh)
2309 session = vcl_session_get_w_handle (wrk, sh);
2310 if (PREDICT_FALSE (!session))
2312 VDBG (0, "ERROR: Invalid sh (%u)!", sh);
2315 if (PREDICT_FALSE (session->is_vep))
2317 VDBG (0, "ERROR: sh (%u) is a vep!", vep_handle);
2319 else if (PREDICT_FALSE (!session->is_vep_session))
2321 VDBG (0, "ERROR: sh (%u) is not a vep session handle!", sh);
2324 vep = &session->vep;
2325 if (PREDICT_FALSE (vep->vep_sh != vep_handle))
2326 VDBG (0, "ERROR: session (%u) vep_sh (%u) != vep_sh (%u)!",
2327 sh, session->vep.vep_sh, vep_handle);
2328 if (session->is_vep_session)
2330 VDBG (0, "vep_sh[%u]: sh 0x%x (%u)\n"
2332 " next_sh = 0x%x (%u)\n"
2333 " prev_sh = 0x%x (%u)\n"
2334 " vep_sh = 0x%x (%u)\n"
2335 " ev.events = 0x%x\n"
2336 " ev.data.u64 = 0x%llx\n"
2339 vep_handle, sh, sh, vep->next_sh, vep->next_sh, vep->prev_sh,
2340 vep->prev_sh, vep->vep_sh, vep->vep_sh, vep->ev.events,
2341 vep->ev.data.u64, vep->et_mask);
2346 VDBG (0, "vep_sh (%u): Dump complete!\n", vep_handle);
2350 vppcom_epoll_create (void)
2352 vcl_worker_t *wrk = vcl_worker_get_current ();
2353 vcl_session_t *vep_session;
2355 vep_session = vcl_session_alloc (wrk);
2357 vep_session->is_vep = 1;
2358 vep_session->vep.vep_sh = ~0;
2359 vep_session->vep.next_sh = ~0;
2360 vep_session->vep.prev_sh = ~0;
2361 vep_session->vpp_handle = ~0;
2363 vcl_evt (VCL_EVT_EPOLL_CREATE, vep_session, vep_session->session_index);
2364 VDBG (0, "Created vep_idx %u", vep_session->session_index);
2366 return vcl_session_handle (vep_session);
2370 vppcom_epoll_ctl (uint32_t vep_handle, int op, uint32_t session_handle,
2371 struct epoll_event *event)
2373 vcl_worker_t *wrk = vcl_worker_get_current ();
2374 vcl_session_t *vep_session;
2375 vcl_session_t *session;
2378 if (vep_handle == session_handle)
2380 VDBG (0, "vep_sh == session handle (%u)!", vep_handle);
2381 return VPPCOM_EINVAL;
2384 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
2385 if (PREDICT_FALSE (!vep_session))
2387 VDBG (0, "Invalid vep_sh (%u)!", vep_handle);
2388 return VPPCOM_EBADFD;
2390 if (PREDICT_FALSE (!vep_session->is_vep))
2392 VDBG (0, "vep_sh (%u) is not a vep!", vep_handle);
2393 return VPPCOM_EINVAL;
2396 ASSERT (vep_session->vep.vep_sh == ~0);
2397 ASSERT (vep_session->vep.prev_sh == ~0);
2399 session = vcl_session_get_w_handle (wrk, session_handle);
2400 if (PREDICT_FALSE (!session))
2402 VDBG (0, "Invalid session_handle (%u)!", session_handle);
2403 return VPPCOM_EBADFD;
2405 if (PREDICT_FALSE (session->is_vep))
2407 VDBG (0, "session_handle (%u) is a vep!", vep_handle);
2408 return VPPCOM_EINVAL;
2414 if (PREDICT_FALSE (!event))
2416 VDBG (0, "EPOLL_CTL_ADD: NULL pointer to epoll_event structure!");
2417 return VPPCOM_EINVAL;
2419 if (vep_session->vep.next_sh != ~0)
2421 vcl_session_t *next_session;
2422 next_session = vcl_session_get_w_handle (wrk,
2423 vep_session->vep.next_sh);
2424 if (PREDICT_FALSE (!next_session))
2426 VDBG (0, "EPOLL_CTL_ADD: Invalid vep.next_sh (%u) on "
2427 "vep_idx (%u)!", vep_session->vep.next_sh, vep_handle);
2428 return VPPCOM_EBADFD;
2430 ASSERT (next_session->vep.prev_sh == vep_handle);
2431 next_session->vep.prev_sh = session_handle;
2433 session->vep.next_sh = vep_session->vep.next_sh;
2434 session->vep.prev_sh = vep_handle;
2435 session->vep.vep_sh = vep_handle;
2436 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
2437 session->vep.ev = *event;
2438 session->is_vep = 0;
2439 session->is_vep_session = 1;
2440 vep_session->vep.next_sh = session_handle;
2442 if (session->tx_fifo)
2443 svm_fifo_add_want_deq_ntf (session->tx_fifo,
2444 SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
2446 VDBG (1, "EPOLL_CTL_ADD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2447 vep_handle, session_handle, event->events, event->data.u64);
2448 vcl_evt (VCL_EVT_EPOLL_CTLADD, session, event->events, event->data.u64);
2452 if (PREDICT_FALSE (!event))
2454 VDBG (0, "EPOLL_CTL_MOD: NULL pointer to epoll_event structure!");
2458 else if (PREDICT_FALSE (!session->is_vep_session))
2460 VDBG (0, "sh %u EPOLL_CTL_MOD: not a vep session!", session_handle);
2464 else if (PREDICT_FALSE (session->vep.vep_sh != vep_handle))
2466 VDBG (0, "EPOLL_CTL_MOD: sh %u vep_sh (%u) != vep_sh (%u)!",
2467 session_handle, session->vep.vep_sh, vep_handle);
2471 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
2472 session->vep.ev = *event;
2473 VDBG (1, "EPOLL_CTL_MOD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2474 vep_handle, session_handle, event->events, event->data.u64);
2478 if (PREDICT_FALSE (!session->is_vep_session))
2480 VDBG (0, "EPOLL_CTL_DEL: %u not a vep session!", session_handle);
2484 else if (PREDICT_FALSE (session->vep.vep_sh != vep_handle))
2486 VDBG (0, "EPOLL_CTL_DEL: sh %u vep_sh (%u) != vep_sh (%u)!",
2487 session_handle, session->vep.vep_sh, vep_handle);
2492 if (session->vep.prev_sh == vep_handle)
2493 vep_session->vep.next_sh = session->vep.next_sh;
2496 vcl_session_t *prev_session;
2497 prev_session = vcl_session_get_w_handle (wrk, session->vep.prev_sh);
2498 if (PREDICT_FALSE (!prev_session))
2500 VDBG (0, "EPOLL_CTL_DEL: Invalid prev_sh (%u) on sh (%u)!",
2501 session->vep.prev_sh, session_handle);
2502 return VPPCOM_EBADFD;
2504 ASSERT (prev_session->vep.next_sh == session_handle);
2505 prev_session->vep.next_sh = session->vep.next_sh;
2507 if (session->vep.next_sh != ~0)
2509 vcl_session_t *next_session;
2510 next_session = vcl_session_get_w_handle (wrk, session->vep.next_sh);
2511 if (PREDICT_FALSE (!next_session))
2513 VDBG (0, "EPOLL_CTL_DEL: Invalid next_sh (%u) on sh (%u)!",
2514 session->vep.next_sh, session_handle);
2515 return VPPCOM_EBADFD;
2517 ASSERT (next_session->vep.prev_sh == session_handle);
2518 next_session->vep.prev_sh = session->vep.prev_sh;
2521 memset (&session->vep, 0, sizeof (session->vep));
2522 session->vep.next_sh = ~0;
2523 session->vep.prev_sh = ~0;
2524 session->vep.vep_sh = ~0;
2525 session->is_vep_session = 0;
2527 if (session->tx_fifo)
2528 svm_fifo_del_want_deq_ntf (session->tx_fifo, SVM_FIFO_NO_DEQ_NOTIF);
2530 VDBG (1, "EPOLL_CTL_DEL: vep_idx %u, sh %u!", vep_handle,
2532 vcl_evt (VCL_EVT_EPOLL_CTLDEL, session, vep_sh);
2536 VDBG (0, "Invalid operation (%d)!", op);
2540 vep_verify_epoll_chain (wrk, vep_handle);
2547 vcl_epoll_wait_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
2548 struct epoll_event *events, u32 * num_ev)
2550 session_disconnected_msg_t *disconnected_msg;
2551 session_connected_msg_t *connected_msg;
2552 u32 sid = ~0, session_events;
2553 u64 session_evt_data = ~0;
2554 vcl_session_t *session;
2557 switch (e->event_type)
2559 case SESSION_IO_EVT_RX:
2560 sid = e->session_index;
2561 if (!(session = vcl_session_get (wrk, sid)))
2563 vcl_fifo_rx_evt_valid_or_break (session);
2564 session_events = session->vep.ev.events;
2565 if (!(EPOLLIN & session->vep.ev.events) || session->has_rx_evt)
2568 events[*num_ev].events |= EPOLLIN;
2569 session_evt_data = session->vep.ev.data.u64;
2570 session->has_rx_evt = 1;
2572 case SESSION_IO_EVT_TX:
2573 sid = e->session_index;
2574 if (!(session = vcl_session_get (wrk, sid)))
2576 session_events = session->vep.ev.events;
2577 if (!(EPOLLOUT & session_events))
2580 events[*num_ev].events |= EPOLLOUT;
2581 session_evt_data = session->vep.ev.data.u64;
2582 svm_fifo_reset_has_deq_ntf (session->tx_fifo);
2584 case SESSION_CTRL_EVT_ACCEPTED:
2585 session = vcl_session_accepted (wrk,
2586 (session_accepted_msg_t *) e->data);
2590 session_events = session->vep.ev.events;
2591 if (!(EPOLLIN & session_events))
2595 events[*num_ev].events |= EPOLLIN;
2596 session_evt_data = session->vep.ev.data.u64;
2598 case SESSION_CTRL_EVT_CONNECTED:
2599 connected_msg = (session_connected_msg_t *) e->data;
2600 vcl_session_connected_handler (wrk, connected_msg);
2601 /* Generate EPOLLOUT because there's no connected event */
2602 sid = vcl_session_index_from_vpp_handle (wrk, connected_msg->handle);
2603 if (!(session = vcl_session_get (wrk, sid)))
2605 session_events = session->vep.ev.events;
2606 if (!(EPOLLOUT & session_events))
2609 events[*num_ev].events |= EPOLLOUT;
2610 session_evt_data = session->vep.ev.data.u64;
2612 case SESSION_CTRL_EVT_DISCONNECTED:
2613 disconnected_msg = (session_disconnected_msg_t *) e->data;
2614 session = vcl_session_disconnected_handler (wrk, disconnected_msg);
2617 session_events = session->vep.ev.events;
2618 if (!((EPOLLHUP | EPOLLRDHUP) & session_events))
2621 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
2622 session_evt_data = session->vep.ev.data.u64;
2624 case SESSION_CTRL_EVT_RESET:
2625 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
2626 if (!(session = vcl_session_get (wrk, sid)))
2628 session_events = session->vep.ev.events;
2629 if (!((EPOLLHUP | EPOLLRDHUP) & session_events))
2632 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
2633 session_evt_data = session->vep.ev.data.u64;
2635 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
2636 vcl_session_unlisten_reply_handler (wrk, e->data);
2638 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
2639 vcl_session_req_worker_update_handler (wrk, e->data);
2641 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
2642 vcl_session_worker_update_reply_handler (wrk, e->data);
2645 VDBG (0, "unhandled: %u", e->event_type);
2651 events[*num_ev].data.u64 = session_evt_data;
2652 if (EPOLLONESHOT & session_events)
2654 session = vcl_session_get (wrk, sid);
2655 session->vep.ev.events = 0;
2662 vcl_epoll_wait_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
2663 struct epoll_event *events, u32 maxevents,
2664 double wait_for_time, u32 * num_ev)
2666 svm_msg_q_msg_t *msg;
2670 if (vec_len (wrk->mq_msg_vector) && svm_msg_q_is_empty (mq))
2671 goto handle_dequeued;
2673 svm_msg_q_lock (mq);
2674 if (svm_msg_q_is_empty (mq))
2678 svm_msg_q_unlock (mq);
2681 else if (wait_for_time < 0)
2683 svm_msg_q_wait (mq);
2687 if (svm_msg_q_timedwait (mq, wait_for_time / 1e3))
2689 svm_msg_q_unlock (mq);
2694 ASSERT (maxevents > *num_ev);
2695 vcl_mq_dequeue_batch (wrk, mq, maxevents - *num_ev);
2696 svm_msg_q_unlock (mq);
2699 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
2701 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
2702 e = svm_msg_q_msg_data (mq, msg);
2703 vcl_epoll_wait_handle_mq_event (wrk, e, events, num_ev);
2704 svm_msg_q_free_msg (mq, msg);
2706 vec_reset_length (wrk->mq_msg_vector);
2707 vcl_handle_pending_wrk_updates (wrk);
2712 vppcom_epoll_wait_condvar (vcl_worker_t * wrk, struct epoll_event *events,
2713 int maxevents, u32 n_evts, double wait_for_time)
2715 double wait = 0, start = 0, now;
2719 wait = wait_for_time;
2720 start = clib_time_now (&wrk->clib_time);
2725 vcl_epoll_wait_handle_mq (wrk, wrk->app_event_queue, events, maxevents,
2732 now = clib_time_now (&wrk->clib_time);
2733 wait -= now - start;
2742 vppcom_epoll_wait_eventfd (vcl_worker_t * wrk, struct epoll_event *events,
2743 int maxevents, u32 n_evts, double wait_for_time)
2745 vcl_mq_evt_conn_t *mqc;
2746 int __clib_unused n_read;
2750 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
2752 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
2753 vec_len (wrk->mq_events), wait_for_time);
2754 for (i = 0; i < n_mq_evts; i++)
2756 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
2757 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
2758 vcl_epoll_wait_handle_mq (wrk, mqc->mq, events, maxevents, 0, &n_evts);
2760 if (!n_evts && n_mq_evts > 0)
2763 return (int) n_evts;
2767 vppcom_epoll_wait (uint32_t vep_handle, struct epoll_event *events,
2768 int maxevents, double wait_for_time)
2770 vcl_worker_t *wrk = vcl_worker_get_current ();
2771 vcl_session_t *vep_session;
2775 if (PREDICT_FALSE (maxevents <= 0))
2777 VDBG (0, "ERROR: Invalid maxevents (%d)!", maxevents);
2778 return VPPCOM_EINVAL;
2781 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
2783 return VPPCOM_EBADFD;
2785 if (PREDICT_FALSE (!vep_session->is_vep))
2787 VDBG (0, "ERROR: vep_idx (%u) is not a vep!", vep_handle);
2788 return VPPCOM_EINVAL;
2791 memset (events, 0, sizeof (*events) * maxevents);
2793 if (vec_len (wrk->unhandled_evts_vector))
2795 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
2797 vcl_epoll_wait_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i],
2799 if (n_evts == maxevents)
2801 vec_delete (wrk->unhandled_evts_vector, i + 1, 0);
2805 vec_reset_length (wrk->unhandled_evts_vector);
2808 if (vcm->cfg.use_mq_eventfd)
2809 return vppcom_epoll_wait_eventfd (wrk, events, maxevents, n_evts,
2812 return vppcom_epoll_wait_condvar (wrk, events, maxevents, n_evts,
2817 vppcom_session_attr (uint32_t session_handle, uint32_t op,
2818 void *buffer, uint32_t * buflen)
2820 vcl_worker_t *wrk = vcl_worker_get_current ();
2821 vcl_session_t *session;
2823 u32 *flags = buffer, tmp_flags = 0;
2824 vppcom_endpt_t *ep = buffer;
2826 session = vcl_session_get_w_handle (wrk, session_handle);
2828 return VPPCOM_EBADFD;
2832 case VPPCOM_ATTR_GET_NREAD:
2833 rv = vcl_session_read_ready (session);
2834 VDBG (2, "VPPCOM_ATTR_GET_NREAD: sh %u, nread = %d", session_handle,
2838 case VPPCOM_ATTR_GET_NWRITE:
2839 rv = vcl_session_write_ready (session);
2840 VDBG (2, "VPPCOM_ATTR_GET_NWRITE: sh %u, nwrite = %d", session_handle,
2844 case VPPCOM_ATTR_GET_FLAGS:
2845 if (PREDICT_TRUE (buffer && buflen && (*buflen >= sizeof (*flags))))
2847 *flags = O_RDWR | (VCL_SESS_ATTR_TEST (session->attr,
2848 VCL_SESS_ATTR_NONBLOCK));
2849 *buflen = sizeof (*flags);
2850 VDBG (2, "VPPCOM_ATTR_GET_FLAGS: sh %u, flags = 0x%08x, "
2851 "is_nonblocking = %u", session_handle, *flags,
2852 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK));
2858 case VPPCOM_ATTR_SET_FLAGS:
2859 if (PREDICT_TRUE (buffer && buflen && (*buflen == sizeof (*flags))))
2861 if (*flags & O_NONBLOCK)
2862 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK);
2864 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_NONBLOCK);
2866 VDBG (2, "VPPCOM_ATTR_SET_FLAGS: sh %u, flags = 0x%08x,"
2867 " is_nonblocking = %u", session_handle, *flags,
2868 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK));
2874 case VPPCOM_ATTR_GET_PEER_ADDR:
2875 if (PREDICT_TRUE (buffer && buflen &&
2876 (*buflen >= sizeof (*ep)) && ep->ip))
2878 ep->is_ip4 = session->transport.is_ip4;
2879 ep->port = session->transport.rmt_port;
2880 if (session->transport.is_ip4)
2881 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
2882 sizeof (ip4_address_t));
2884 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
2885 sizeof (ip6_address_t));
2886 *buflen = sizeof (*ep);
2887 VDBG (1, "VPPCOM_ATTR_GET_PEER_ADDR: sh %u, is_ip4 = %u, "
2888 "addr = %U, port %u", session_handle, ep->is_ip4,
2889 format_ip46_address, &session->transport.rmt_ip,
2890 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
2891 clib_net_to_host_u16 (ep->port));
2897 case VPPCOM_ATTR_GET_LCL_ADDR:
2898 if (PREDICT_TRUE (buffer && buflen &&
2899 (*buflen >= sizeof (*ep)) && ep->ip))
2901 ep->is_ip4 = session->transport.is_ip4;
2902 ep->port = session->transport.lcl_port;
2903 if (session->transport.is_ip4)
2904 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip4,
2905 sizeof (ip4_address_t));
2907 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip6,
2908 sizeof (ip6_address_t));
2909 *buflen = sizeof (*ep);
2910 VDBG (1, "VPPCOM_ATTR_GET_LCL_ADDR: sh %u, is_ip4 = %u, addr = %U"
2911 " port %d", session_handle, ep->is_ip4, format_ip46_address,
2912 &session->transport.lcl_ip,
2913 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
2914 clib_net_to_host_u16 (ep->port));
2920 case VPPCOM_ATTR_GET_LIBC_EPFD:
2921 rv = session->libc_epfd;
2922 VDBG (2, "VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd %d", rv);
2925 case VPPCOM_ATTR_SET_LIBC_EPFD:
2926 if (PREDICT_TRUE (buffer && buflen &&
2927 (*buflen == sizeof (session->libc_epfd))))
2929 session->libc_epfd = *(int *) buffer;
2930 *buflen = sizeof (session->libc_epfd);
2932 VDBG (2, "VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd %d, buflen %d",
2933 session->libc_epfd, *buflen);
2939 case VPPCOM_ATTR_GET_PROTOCOL:
2940 if (buffer && buflen && (*buflen >= sizeof (int)))
2942 *(int *) buffer = session->session_type;
2943 *buflen = sizeof (int);
2945 VDBG (2, "VPPCOM_ATTR_GET_PROTOCOL: %d (%s), buflen %d",
2946 *(int *) buffer, *(int *) buffer ? "UDP" : "TCP", *buflen);
2952 case VPPCOM_ATTR_GET_LISTEN:
2953 if (buffer && buflen && (*buflen >= sizeof (int)))
2955 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2956 VCL_SESS_ATTR_LISTEN);
2957 *buflen = sizeof (int);
2959 VDBG (2, "VPPCOM_ATTR_GET_LISTEN: %d, buflen %d", *(int *) buffer,
2966 case VPPCOM_ATTR_GET_ERROR:
2967 if (buffer && buflen && (*buflen >= sizeof (int)))
2969 *(int *) buffer = 0;
2970 *buflen = sizeof (int);
2972 VDBG (2, "VPPCOM_ATTR_GET_ERROR: %d, buflen %d, #VPP-TBD#",
2973 *(int *) buffer, *buflen);
2979 case VPPCOM_ATTR_GET_TX_FIFO_LEN:
2980 if (buffer && buflen && (*buflen >= sizeof (u32)))
2984 *(size_t *) buffer = (session->sndbuf_size ? session->sndbuf_size :
2985 session->tx_fifo ? session->tx_fifo->nitems :
2986 vcm->cfg.tx_fifo_size);
2987 *buflen = sizeof (u32);
2989 VDBG (2, "VPPCOM_ATTR_GET_TX_FIFO_LEN: %u (0x%x), buflen %d,"
2990 " #VPP-TBD#", *(size_t *) buffer, *(size_t *) buffer,
2997 case VPPCOM_ATTR_SET_TX_FIFO_LEN:
2998 if (buffer && buflen && (*buflen == sizeof (u32)))
3001 session->sndbuf_size = *(u32 *) buffer;
3002 VDBG (2, "VPPCOM_ATTR_SET_TX_FIFO_LEN: %u (0x%x), buflen %d,"
3003 " #VPP-TBD#", session->sndbuf_size, session->sndbuf_size,
3010 case VPPCOM_ATTR_GET_RX_FIFO_LEN:
3011 if (buffer && buflen && (*buflen >= sizeof (u32)))
3015 *(size_t *) buffer = (session->rcvbuf_size ? session->rcvbuf_size :
3016 session->rx_fifo ? session->rx_fifo->nitems :
3017 vcm->cfg.rx_fifo_size);
3018 *buflen = sizeof (u32);
3020 VDBG (2, "VPPCOM_ATTR_GET_RX_FIFO_LEN: %u (0x%x), buflen %d, "
3021 "#VPP-TBD#", *(size_t *) buffer, *(size_t *) buffer, *buflen);
3027 case VPPCOM_ATTR_SET_RX_FIFO_LEN:
3028 if (buffer && buflen && (*buflen == sizeof (u32)))
3031 session->rcvbuf_size = *(u32 *) buffer;
3032 VDBG (2, "VPPCOM_ATTR_SET_RX_FIFO_LEN: %u (0x%x), buflen %d,"
3033 " #VPP-TBD#", session->sndbuf_size, session->sndbuf_size,
3040 case VPPCOM_ATTR_GET_REUSEADDR:
3041 if (buffer && buflen && (*buflen >= sizeof (int)))
3044 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3045 VCL_SESS_ATTR_REUSEADDR);
3046 *buflen = sizeof (int);
3048 VDBG (2, "VPPCOM_ATTR_GET_REUSEADDR: %d, buflen %d, #VPP-TBD#",
3049 *(int *) buffer, *buflen);
3055 case VPPCOM_ATTR_SET_REUSEADDR:
3056 if (buffer && buflen && (*buflen == sizeof (int)) &&
3057 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
3060 if (*(int *) buffer)
3061 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEADDR);
3063 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEADDR);
3065 VDBG (2, "VPPCOM_ATTR_SET_REUSEADDR: %d, buflen %d, #VPP-TBD#",
3066 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_REUSEADDR),
3073 case VPPCOM_ATTR_GET_REUSEPORT:
3074 if (buffer && buflen && (*buflen >= sizeof (int)))
3077 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3078 VCL_SESS_ATTR_REUSEPORT);
3079 *buflen = sizeof (int);
3081 VDBG (2, "VPPCOM_ATTR_GET_REUSEPORT: %d, buflen %d, #VPP-TBD#",
3082 *(int *) buffer, *buflen);
3088 case VPPCOM_ATTR_SET_REUSEPORT:
3089 if (buffer && buflen && (*buflen == sizeof (int)) &&
3090 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
3093 if (*(int *) buffer)
3094 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEPORT);
3096 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEPORT);
3098 VDBG (2, "VPPCOM_ATTR_SET_REUSEPORT: %d, buflen %d, #VPP-TBD#",
3099 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_REUSEPORT),
3106 case VPPCOM_ATTR_GET_BROADCAST:
3107 if (buffer && buflen && (*buflen >= sizeof (int)))
3110 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3111 VCL_SESS_ATTR_BROADCAST);
3112 *buflen = sizeof (int);
3114 VDBG (2, "VPPCOM_ATTR_GET_BROADCAST: %d, buflen %d, #VPP-TBD#",
3115 *(int *) buffer, *buflen);
3121 case VPPCOM_ATTR_SET_BROADCAST:
3122 if (buffer && buflen && (*buflen == sizeof (int)))
3125 if (*(int *) buffer)
3126 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_BROADCAST);
3128 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_BROADCAST);
3130 VDBG (2, "VPPCOM_ATTR_SET_BROADCAST: %d, buflen %d, #VPP-TBD#",
3131 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_BROADCAST),
3138 case VPPCOM_ATTR_GET_V6ONLY:
3139 if (buffer && buflen && (*buflen >= sizeof (int)))
3142 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3143 VCL_SESS_ATTR_V6ONLY);
3144 *buflen = sizeof (int);
3146 VDBG (2, "VPPCOM_ATTR_GET_V6ONLY: %d, buflen %d, #VPP-TBD#",
3147 *(int *) buffer, *buflen);
3153 case VPPCOM_ATTR_SET_V6ONLY:
3154 if (buffer && buflen && (*buflen == sizeof (int)))
3157 if (*(int *) buffer)
3158 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_V6ONLY);
3160 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_V6ONLY);
3162 VDBG (2, "VPPCOM_ATTR_SET_V6ONLY: %d, buflen %d, #VPP-TBD#",
3163 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_V6ONLY),
3170 case VPPCOM_ATTR_GET_KEEPALIVE:
3171 if (buffer && buflen && (*buflen >= sizeof (int)))
3174 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3175 VCL_SESS_ATTR_KEEPALIVE);
3176 *buflen = sizeof (int);
3178 VDBG (2, "VPPCOM_ATTR_GET_KEEPALIVE: %d, buflen %d, #VPP-TBD#",
3179 *(int *) buffer, *buflen);
3185 case VPPCOM_ATTR_SET_KEEPALIVE:
3186 if (buffer && buflen && (*buflen == sizeof (int)))
3189 if (*(int *) buffer)
3190 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_KEEPALIVE);
3192 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_KEEPALIVE);
3194 VDBG (2, "VPPCOM_ATTR_SET_KEEPALIVE: %d, buflen %d, #VPP-TBD#",
3195 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_KEEPALIVE),
3202 case VPPCOM_ATTR_GET_TCP_NODELAY:
3203 if (buffer && buflen && (*buflen >= sizeof (int)))
3206 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3207 VCL_SESS_ATTR_TCP_NODELAY);
3208 *buflen = sizeof (int);
3210 VDBG (2, "VPPCOM_ATTR_GET_TCP_NODELAY: %d, buflen %d, #VPP-TBD#",
3211 *(int *) buffer, *buflen);
3217 case VPPCOM_ATTR_SET_TCP_NODELAY:
3218 if (buffer && buflen && (*buflen == sizeof (int)))
3221 if (*(int *) buffer)
3222 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
3224 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
3226 VDBG (2, "VPPCOM_ATTR_SET_TCP_NODELAY: %d, buflen %d, #VPP-TBD#",
3227 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_TCP_NODELAY),
3234 case VPPCOM_ATTR_GET_TCP_KEEPIDLE:
3235 if (buffer && buflen && (*buflen >= sizeof (int)))
3238 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3239 VCL_SESS_ATTR_TCP_KEEPIDLE);
3240 *buflen = sizeof (int);
3242 VDBG (2, "VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d, buflen %d, #VPP-TBD#",
3243 *(int *) buffer, *buflen);
3249 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
3250 if (buffer && buflen && (*buflen == sizeof (int)))
3253 if (*(int *) buffer)
3254 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
3256 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
3258 VDBG (2, "VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d, buflen %d, #VPP-TBD#",
3259 VCL_SESS_ATTR_TEST (session->attr,
3260 VCL_SESS_ATTR_TCP_KEEPIDLE), *buflen);
3266 case VPPCOM_ATTR_GET_TCP_KEEPINTVL:
3267 if (buffer && buflen && (*buflen >= sizeof (int)))
3270 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3271 VCL_SESS_ATTR_TCP_KEEPINTVL);
3272 *buflen = sizeof (int);
3274 VDBG (2, "VPPCOM_ATTR_GET_TCP_KEEPINTVL: %d, buflen %d, #VPP-TBD#",
3275 *(int *) buffer, *buflen);
3281 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
3282 if (buffer && buflen && (*buflen == sizeof (int)))
3285 if (*(int *) buffer)
3286 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
3288 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
3290 VDBG (2, "VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d, buflen %d, #VPP-TBD#",
3291 VCL_SESS_ATTR_TEST (session->attr,
3292 VCL_SESS_ATTR_TCP_KEEPINTVL), *buflen);
3298 case VPPCOM_ATTR_GET_TCP_USER_MSS:
3299 if (buffer && buflen && (*buflen >= sizeof (u32)))
3302 *(u32 *) buffer = session->user_mss;
3303 *buflen = sizeof (int);
3305 VDBG (2, "VPPCOM_ATTR_GET_TCP_USER_MSS: %d, buflen %d, #VPP-TBD#",
3306 *(int *) buffer, *buflen);
3312 case VPPCOM_ATTR_SET_TCP_USER_MSS:
3313 if (buffer && buflen && (*buflen == sizeof (u32)))
3316 session->user_mss = *(u32 *) buffer;
3318 VDBG (2, "VPPCOM_ATTR_SET_TCP_USER_MSS: %u, buflen %d, #VPP-TBD#",
3319 session->user_mss, *buflen);
3325 case VPPCOM_ATTR_SET_SHUT:
3326 if (*flags == SHUT_RD || *flags == SHUT_RDWR)
3327 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_SHUT_RD);
3328 if (*flags == SHUT_WR || *flags == SHUT_RDWR)
3329 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_SHUT_WR);
3332 case VPPCOM_ATTR_GET_SHUT:
3333 if (VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_SHUT_RD))
3335 if (VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_SHUT_WR))
3338 *(int *) buffer = SHUT_RD;
3339 else if (tmp_flags == 2)
3340 *(int *) buffer = SHUT_WR;
3341 else if (tmp_flags == 3)
3342 *(int *) buffer = SHUT_RDWR;
3343 *buflen = sizeof (int);
3354 vppcom_session_recvfrom (uint32_t session_handle, void *buffer,
3355 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3357 vcl_worker_t *wrk = vcl_worker_get_current ();
3359 vcl_session_t *session = 0;
3363 session = vcl_session_get_w_handle (wrk, session_handle);
3364 if (PREDICT_FALSE (!session))
3366 VDBG (0, "sh 0x%llx is closed!", session_handle);
3367 return VPPCOM_EBADFD;
3369 ep->is_ip4 = session->transport.is_ip4;
3370 ep->port = session->transport.rmt_port;
3374 rv = vppcom_session_read (session_handle, buffer, buflen);
3375 else if (flags & MSG_PEEK)
3376 rv = vppcom_session_peek (session_handle, buffer, buflen);
3379 VDBG (0, "Unsupport flags for recvfrom %d", flags);
3380 return VPPCOM_EAFNOSUPPORT;
3385 if (session->transport.is_ip4)
3386 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
3387 sizeof (ip4_address_t));
3389 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
3390 sizeof (ip6_address_t));
3397 vppcom_session_sendto (uint32_t session_handle, void *buffer,
3398 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3401 return VPPCOM_EINVAL;
3406 return VPPCOM_EINVAL;
3411 // TBD check the flags and do the right thing
3412 VDBG (2, "handling flags 0x%u (%d) not implemented yet.", flags, flags);
3415 return (vppcom_session_write_inline (session_handle, buffer, buflen, 1));
3419 vppcom_poll (vcl_poll_t * vp, uint32_t n_sids, double wait_for_time)
3421 vcl_worker_t *wrk = vcl_worker_get_current ();
3422 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
3423 u32 i, keep_trying = 1;
3424 svm_msg_q_msg_t msg;
3428 VDBG (3, "vp %p, nsids %u, wait_for_time %f", vp, n_sids, wait_for_time);
3431 return VPPCOM_EFAULT;
3435 vcl_session_t *session;
3437 /* Dequeue all events and drop all unhandled io events */
3438 while (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0) == 0)
3440 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
3441 vcl_handle_mq_event (wrk, e);
3442 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
3444 vec_reset_length (wrk->unhandled_evts_vector);
3446 for (i = 0; i < n_sids; i++)
3448 session = vcl_session_get (wrk, vp[i].sh);
3451 vp[i].revents = POLLHUP;
3458 if (POLLIN & vp[i].events)
3460 rv = vcl_session_read_ready (session);
3463 vp[i].revents |= POLLIN;
3470 case VPPCOM_ECONNRESET:
3471 vp[i].revents = POLLHUP;
3475 vp[i].revents = POLLERR;
3482 if (POLLOUT & vp[i].events)
3484 rv = vcl_session_write_ready (session);
3487 vp[i].revents |= POLLOUT;
3494 case VPPCOM_ECONNRESET:
3495 vp[i].revents = POLLHUP;
3499 vp[i].revents = POLLERR;
3506 if (0) // Note "done:" label used by VCL_SESSION_LOCK_AND_GET()
3508 vp[i].revents = POLLNVAL;
3512 if (wait_for_time != -1)
3513 keep_trying = (clib_time_now (&wrk->clib_time) <= timeout) ? 1 : 0;
3515 while ((num_ev == 0) && keep_trying);
3521 vppcom_mq_epoll_fd (void)
3523 vcl_worker_t *wrk = vcl_worker_get_current ();
3524 return wrk->mqs_epfd;
3528 vppcom_session_index (vcl_session_handle_t session_handle)
3530 return session_handle & 0xFFFFFF;
3534 vppcom_session_worker (vcl_session_handle_t session_handle)
3536 return session_handle >> 24;
3540 vppcom_worker_register (void)
3542 if (!vcl_worker_alloc_and_init ())
3543 return VPPCOM_EEXIST;
3545 if (vcl_worker_set_bapi ())
3546 return VPPCOM_EEXIST;
3548 if (vcl_worker_register_with_vpp ())
3549 return VPPCOM_EEXIST;
3555 vppcom_worker_unregister (void)
3557 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
3558 vcl_set_worker_index (~0);
3562 vppcom_worker_index (void)
3564 return vcl_get_worker_index ();
3568 vppcom_worker_mqs_epfd (void)
3570 vcl_worker_t *wrk = vcl_worker_get_current ();
3571 if (!vcm->cfg.use_mq_eventfd)
3573 return wrk->mqs_epfd;
3577 vppcom_session_is_connectable_listener (uint32_t session_handle)
3579 vcl_session_t *session;
3580 vcl_worker_t *wrk = vcl_worker_get_current ();
3581 session = vcl_session_get_w_handle (wrk, session_handle);
3583 return VPPCOM_EBADFD;
3584 return vcl_session_is_connectable_listener (wrk, session);
3588 vppcom_session_listener (uint32_t session_handle)
3590 vcl_worker_t *wrk = vcl_worker_get_current ();
3591 vcl_session_t *listen_session, *session;
3592 session = vcl_session_get_w_handle (wrk, session_handle);
3594 return VPPCOM_EBADFD;
3595 if (session->listener_index == VCL_INVALID_SESSION_INDEX)
3596 return VPPCOM_EBADFD;
3597 listen_session = vcl_session_get_w_handle (wrk, session->listener_index);
3598 if (!listen_session)
3599 return VPPCOM_EBADFD;
3600 return vcl_session_handle (listen_session);
3604 vppcom_session_n_accepted (uint32_t session_handle)
3606 vcl_worker_t *wrk = vcl_worker_get_current ();
3607 vcl_session_t *session = vcl_session_get_w_handle (wrk, session_handle);
3609 return VPPCOM_EBADFD;
3610 return session->n_accepted_sessions;
3614 * fd.io coding-style-patch-verification: ON
3617 * eval: (c-set-style "gnu")