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_event.h>
21 #include <vcl/vcl_debug.h>
22 #include <vcl/vcl_private.h>
24 __thread uword __vcl_worker_index = ~0;
29 sigsegv_signal (int signum)
35 vcl_wait_for_memory (void *mem)
37 u8 __clib_unused test;
38 if (vcm->mounting_segment)
40 while (vcm->mounting_segment)
49 if (signal (SIGSEGV, sigsegv_signal))
65 signal (SIGSEGV, SIG_DFL);
69 vppcom_session_state_str (session_state_t state)
91 case STATE_CLOSE_ON_EMPTY:
92 st = "STATE_CLOSE_ON_EMPTY";
95 case STATE_DISCONNECT:
96 st = "STATE_DISCONNECT";
104 st = "UNKNOWN_STATE";
112 format_ip4_address (u8 * s, va_list * args)
114 u8 *a = va_arg (*args, u8 *);
115 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
119 format_ip6_address (u8 * s, va_list * args)
121 ip6_address_t *a = va_arg (*args, ip6_address_t *);
122 u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
124 i_max_n_zero = ARRAY_LEN (a->as_u16);
126 i_first_zero = i_max_n_zero;
128 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
130 u32 is_zero = a->as_u16[i] == 0;
131 if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
137 if ((!is_zero && n_zeros > max_n_zeros)
138 || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
140 i_max_n_zero = i_first_zero;
141 max_n_zeros = n_zeros;
142 i_first_zero = ARRAY_LEN (a->as_u16);
147 last_double_colon = 0;
148 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
150 if (i == i_max_n_zero && max_n_zeros > 1)
152 s = format (s, "::");
153 i += max_n_zeros - 1;
154 last_double_colon = 1;
158 s = format (s, "%s%x",
159 (last_double_colon || i == 0) ? "" : ":",
160 clib_net_to_host_u16 (a->as_u16[i]));
161 last_double_colon = 0;
168 /* Format an IP46 address. */
170 format_ip46_address (u8 * s, va_list * args)
172 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
173 ip46_type_t type = va_arg (*args, ip46_type_t);
179 is_ip4 = ip46_address_is_ip4 (ip46);
190 format (s, "%U", format_ip4_address, &ip46->ip4) :
191 format (s, "%U", format_ip6_address, &ip46->ip6);
195 * VPPCOM Utility Functions
200 vcl_session_vpp_evt_q (vcl_worker_t * wrk, vcl_session_t * s)
202 if (vcl_session_is_ct (s))
203 return wrk->vpp_event_queues[0];
205 return wrk->vpp_event_queues[s->tx_fifo->master_thread_index];
209 vcl_send_session_accepted_reply (svm_msg_q_t * mq, u32 context,
210 session_handle_t handle, int retval)
212 app_session_evt_t _app_evt, *app_evt = &_app_evt;
213 session_accepted_reply_msg_t *rmp;
214 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_ACCEPTED_REPLY);
215 rmp = (session_accepted_reply_msg_t *) app_evt->evt->data;
216 rmp->handle = handle;
217 rmp->context = context;
218 rmp->retval = retval;
219 app_send_ctrl_evt_to_vpp (mq, app_evt);
223 vcl_send_session_disconnected_reply (svm_msg_q_t * mq, u32 context,
224 session_handle_t handle, int retval)
226 app_session_evt_t _app_evt, *app_evt = &_app_evt;
227 session_disconnected_reply_msg_t *rmp;
228 app_alloc_ctrl_evt_to_vpp (mq, app_evt,
229 SESSION_CTRL_EVT_DISCONNECTED_REPLY);
230 rmp = (session_disconnected_reply_msg_t *) app_evt->evt->data;
231 rmp->handle = handle;
232 rmp->context = context;
233 rmp->retval = retval;
234 app_send_ctrl_evt_to_vpp (mq, app_evt);
238 vcl_send_session_reset_reply (svm_msg_q_t * mq, u32 context,
239 session_handle_t handle, int retval)
241 app_session_evt_t _app_evt, *app_evt = &_app_evt;
242 session_reset_reply_msg_t *rmp;
243 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_RESET_REPLY);
244 rmp = (session_reset_reply_msg_t *) app_evt->evt->data;
245 rmp->handle = handle;
246 rmp->context = context;
247 rmp->retval = retval;
248 app_send_ctrl_evt_to_vpp (mq, app_evt);
252 vcl_session_accepted_handler (vcl_worker_t * wrk, session_accepted_msg_t * mp)
254 vcl_session_t *session, *listen_session;
255 svm_fifo_t *rx_fifo, *tx_fifo;
259 session = vcl_session_alloc (wrk);
261 listen_session = vcl_session_table_lookup_listener (wrk,
262 mp->listener_handle);
266 evt_q = uword_to_pointer (mp->vpp_event_queue_address, svm_msg_q_t *);
267 clib_warning ("VCL<%d>: ERROR: couldn't find listen session: "
268 "unknown vpp listener handle %llx",
269 getpid (), mp->listener_handle);
270 vcl_send_session_accepted_reply (evt_q, mp->context, mp->handle,
271 VNET_API_ERROR_INVALID_ARGUMENT);
272 vcl_session_free (wrk, session);
273 return VCL_INVALID_SESSION_INDEX;
276 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
277 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
279 if (mp->server_event_queue_address)
281 session->vpp_evt_q = uword_to_pointer (mp->client_event_queue_address,
283 session->our_evt_q = uword_to_pointer (mp->server_event_queue_address,
285 vcl_wait_for_memory (session->vpp_evt_q);
286 rx_fifo->master_session_index = session->session_index;
287 tx_fifo->master_session_index = session->session_index;
288 rx_fifo->master_thread_index = vcl_get_worker_index ();
289 tx_fifo->master_thread_index = vcl_get_worker_index ();
290 vec_validate (wrk->vpp_event_queues, 0);
291 evt_q = uword_to_pointer (mp->vpp_event_queue_address, svm_msg_q_t *);
292 wrk->vpp_event_queues[0] = evt_q;
296 session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address,
298 rx_fifo->client_session_index = session->session_index;
299 tx_fifo->client_session_index = session->session_index;
300 rx_fifo->client_thread_index = vcl_get_worker_index ();
301 tx_fifo->client_thread_index = vcl_get_worker_index ();
302 vpp_wrk_index = tx_fifo->master_thread_index;
303 vec_validate (wrk->vpp_event_queues, vpp_wrk_index);
304 wrk->vpp_event_queues[vpp_wrk_index] = session->vpp_evt_q;
307 session->vpp_handle = mp->handle;
308 session->client_context = mp->context;
309 session->rx_fifo = rx_fifo;
310 session->tx_fifo = tx_fifo;
312 session->session_state = STATE_ACCEPT;
313 session->transport.rmt_port = mp->port;
314 session->transport.is_ip4 = mp->is_ip4;
315 clib_memcpy (&session->transport.rmt_ip, mp->ip, sizeof (ip46_address_t));
317 vcl_session_table_add_vpp_handle (wrk, mp->handle, session->session_index);
318 session->transport.lcl_port = listen_session->transport.lcl_port;
319 session->transport.lcl_ip = listen_session->transport.lcl_ip;
320 session->session_type = listen_session->session_type;
321 session->is_dgram = session->session_type == VPPCOM_PROTO_UDP;
323 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: client accept request from %s"
324 " address %U port %d queue %p!", getpid (), mp->handle,
325 session->session_index,
326 mp->is_ip4 ? "IPv4" : "IPv6", format_ip46_address, &mp->ip,
327 mp->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
328 clib_net_to_host_u16 (mp->port), session->vpp_evt_q);
329 vcl_evt (VCL_EVT_ACCEPT, session, listen_session, session_index);
331 return session->session_index;
335 vcl_session_connected_handler (vcl_worker_t * wrk,
336 session_connected_msg_t * mp)
338 u32 session_index, vpp_wrk_index;
339 svm_fifo_t *rx_fifo, *tx_fifo;
340 vcl_session_t *session = 0;
343 session_index = mp->context;
344 session = vcl_session_get (wrk, session_index);
347 clib_warning ("[%s] ERROR: vpp handle 0x%llx, sid %u: "
348 "Invalid session index (%u)!",
349 getpid (), mp->handle, session_index);
350 return VCL_INVALID_SESSION_INDEX;
354 clib_warning ("VCL<%d>: ERROR: sid %u: connect failed! %U", getpid (),
355 session_index, format_api_error, ntohl (mp->retval));
356 session->session_state = STATE_FAILED;
357 session->vpp_handle = mp->handle;
358 return session_index;
361 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
362 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
363 vcl_wait_for_memory (rx_fifo);
364 rx_fifo->client_session_index = session_index;
365 tx_fifo->client_session_index = session_index;
366 rx_fifo->client_thread_index = vcl_get_worker_index ();
367 tx_fifo->client_thread_index = vcl_get_worker_index ();
369 if (mp->client_event_queue_address)
371 session->vpp_evt_q = uword_to_pointer (mp->server_event_queue_address,
373 session->our_evt_q = uword_to_pointer (mp->client_event_queue_address,
376 vec_validate (wrk->vpp_event_queues, 0);
377 evt_q = uword_to_pointer (mp->vpp_event_queue_address, svm_msg_q_t *);
378 wrk->vpp_event_queues[0] = evt_q;
382 session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address,
384 vpp_wrk_index = tx_fifo->master_thread_index;
385 vec_validate (wrk->vpp_event_queues, vpp_wrk_index);
386 wrk->vpp_event_queues[vpp_wrk_index] = session->vpp_evt_q;
389 session->rx_fifo = rx_fifo;
390 session->tx_fifo = tx_fifo;
391 session->vpp_handle = mp->handle;
392 session->transport.is_ip4 = mp->is_ip4;
393 clib_memcpy (&session->transport.lcl_ip, mp->lcl_ip,
394 sizeof (session->transport.lcl_ip));
395 session->transport.lcl_port = mp->lcl_port;
396 session->session_state = STATE_CONNECT;
398 /* Add it to lookup table */
399 hash_set (wrk->session_index_by_vpp_handles, mp->handle, session_index);
401 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: connect succeeded! "
402 "session_rx_fifo %p, refcnt %d, session_tx_fifo %p, refcnt %d",
403 getpid (), mp->handle, session_index, session->rx_fifo,
404 session->rx_fifo->refcnt, session->tx_fifo, session->tx_fifo->refcnt);
406 return session_index;
410 vcl_session_reset_handler (vcl_worker_t * wrk,
411 session_reset_msg_t * reset_msg)
413 vcl_session_t *session;
416 sid = vcl_session_index_from_vpp_handle (wrk, reset_msg->handle);
417 session = vcl_session_get (wrk, sid);
420 VDBG (0, "request to reset unknown handle 0x%llx", reset_msg->handle);
421 return VCL_INVALID_SESSION_INDEX;
423 session->session_state = STATE_CLOSE_ON_EMPTY;
424 VDBG (0, "reset handle 0x%llx, sid %u ", reset_msg->handle, sid);
425 vcl_send_session_reset_reply (vcl_session_vpp_evt_q (wrk, session),
426 vcm->my_client_index, reset_msg->handle, 0);
431 vcl_session_bound_handler (vcl_worker_t * wrk, session_bound_msg_t * mp)
433 vcl_session_t *session;
434 u32 sid = mp->context;
436 session = vcl_session_get (wrk, sid);
439 VDBG (0, "VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: bind failed: %U",
440 getpid (), mp->handle, sid, format_api_error, ntohl (mp->retval));
443 session->session_state = STATE_FAILED;
444 session->vpp_handle = mp->handle;
449 clib_warning ("[%s] ERROR: vpp handle 0x%llx, sid %u: "
450 "Invalid session index (%u)!",
451 getpid (), mp->handle, sid);
452 return VCL_INVALID_SESSION_INDEX;
456 session->vpp_handle = mp->handle;
457 session->transport.is_ip4 = mp->lcl_is_ip4;
458 clib_memcpy (&session->transport.lcl_ip, mp->lcl_ip,
459 sizeof (ip46_address_t));
460 session->transport.lcl_port = mp->lcl_port;
461 vcl_session_table_add_listener (wrk, mp->handle, sid);
462 session->session_state = STATE_LISTEN;
464 if (session->is_dgram)
466 svm_fifo_t *rx_fifo, *tx_fifo;
467 session->vpp_evt_q = uword_to_pointer (mp->vpp_evt_q, svm_msg_q_t *);
468 rx_fifo = uword_to_pointer (mp->rx_fifo, svm_fifo_t *);
469 rx_fifo->client_session_index = sid;
470 tx_fifo = uword_to_pointer (mp->tx_fifo, svm_fifo_t *);
471 tx_fifo->client_session_index = sid;
472 session->rx_fifo = rx_fifo;
473 session->tx_fifo = tx_fifo;
476 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: bind succeeded!",
477 getpid (), mp->handle, sid);
482 vcl_handle_mq_event (vcl_worker_t * wrk, session_event_t * e)
484 session_accepted_msg_t *accepted_msg;
485 session_disconnected_msg_t *disconnected_msg;
486 vcl_session_msg_t *vcl_msg;
487 vcl_session_t *session;
491 switch (e->event_type)
493 case FIFO_EVENT_APP_RX:
494 case FIFO_EVENT_APP_TX:
495 case SESSION_IO_EVT_CT_RX:
496 case SESSION_IO_EVT_CT_TX:
497 vec_add1 (wrk->unhandled_evts_vector, *e);
499 case SESSION_CTRL_EVT_ACCEPTED:
500 accepted_msg = (session_accepted_msg_t *) e->data;
501 handle = accepted_msg->listener_handle;
502 session = vcl_session_table_lookup_listener (wrk, handle);
505 clib_warning ("VCL<%d>: ERROR: couldn't find listen session:"
506 "listener handle %llx", getpid (), handle);
510 clib_fifo_add2 (session->accept_evts_fifo, vcl_msg);
511 vcl_msg->accepted_msg = *accepted_msg;
513 case SESSION_CTRL_EVT_CONNECTED:
514 vcl_session_connected_handler (wrk,
515 (session_connected_msg_t *) e->data);
517 case SESSION_CTRL_EVT_DISCONNECTED:
518 disconnected_msg = (session_disconnected_msg_t *) e->data;
519 sid = vcl_session_index_from_vpp_handle (wrk, disconnected_msg->handle);
520 session = vcl_session_get (wrk, sid);
523 VDBG (0, "request to disconnect unknown handle 0x%llx",
524 disconnected_msg->handle);
527 session->session_state = STATE_DISCONNECT;
528 VDBG (0, "disconnected handle 0x%llx, sid %u", disconnected_msg->handle,
531 case SESSION_CTRL_EVT_RESET:
532 vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
534 case SESSION_CTRL_EVT_BOUND:
535 vcl_session_bound_handler (wrk, (session_bound_msg_t *) e->data);
538 clib_warning ("unhandled %u", e->event_type);
544 vppcom_wait_for_session_state_change (u32 session_index,
545 session_state_t state,
548 vcl_worker_t *wrk = vcl_worker_get_current ();
549 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
550 vcl_session_t *volatile session;
556 session = vcl_session_get (wrk, session_index);
557 if (PREDICT_FALSE (!session))
559 return VPPCOM_EBADFD;
561 if (session->session_state & state)
565 if (session->session_state & STATE_FAILED)
567 return VPPCOM_ECONNREFUSED;
570 if (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0))
572 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
573 vcl_handle_mq_event (wrk, e);
574 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
576 while (clib_time_now (&wrk->clib_time) < timeout);
578 VDBG (0, "VCL<%d>: timeout waiting for state 0x%x (%s)", getpid (), state,
579 vppcom_session_state_str (state));
580 vcl_evt (VCL_EVT_SESSION_TIMEOUT, session, session_state);
582 return VPPCOM_ETIMEDOUT;
586 vppcom_app_session_enable (void)
590 if (vcm->app_state != STATE_APP_ENABLED)
592 vppcom_send_session_enable_disable (1 /* is_enabled == TRUE */ );
593 rv = vcl_wait_for_app_state_change (STATE_APP_ENABLED);
594 if (PREDICT_FALSE (rv))
596 VDBG (0, "VCL<%d>: application session enable timed out! "
597 "returning %d (%s)", getpid (), rv, vppcom_retval_str (rv));
605 vppcom_app_attach (void)
609 vppcom_app_send_attach ();
610 rv = vcl_wait_for_app_state_change (STATE_APP_ATTACHED);
611 if (PREDICT_FALSE (rv))
613 VDBG (0, "VCL<%d>: application attach timed out! returning %d (%s)",
614 getpid (), rv, vppcom_retval_str (rv));
622 vppcom_session_unbind (u32 session_handle)
624 vcl_worker_t *wrk = vcl_worker_get_current ();
625 vcl_session_t *session = 0;
628 session = vcl_session_get_w_handle (wrk, session_handle);
630 return VPPCOM_EBADFD;
632 vpp_handle = session->vpp_handle;
633 vcl_session_table_del_listener (wrk, vpp_handle);
634 session->vpp_handle = ~0;
635 session->session_state = STATE_DISCONNECT;
637 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: sending unbind msg! new state"
638 " 0x%x (%s)", getpid (), vpp_handle, session_handle, STATE_DISCONNECT,
639 vppcom_session_state_str (STATE_DISCONNECT));
640 vcl_evt (VCL_EVT_UNBIND, session);
641 vppcom_send_unbind_sock (vpp_handle);
647 vppcom_session_disconnect (u32 session_handle)
649 vcl_worker_t *wrk = vcl_worker_get_current ();
650 svm_msg_q_t *vpp_evt_q;
651 vcl_session_t *session;
652 session_state_t state;
655 session = vcl_session_get_w_handle (wrk, session_handle);
657 return VPPCOM_EBADFD;
659 vpp_handle = session->vpp_handle;
660 state = session->session_state;
662 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u state 0x%x (%s)", getpid (),
663 vpp_handle, session_handle, state, vppcom_session_state_str (state));
665 if (PREDICT_FALSE (state & STATE_LISTEN))
667 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
668 "Cannot disconnect a listen socket!",
669 getpid (), vpp_handle, session_handle);
670 return VPPCOM_EBADFD;
673 if (state & STATE_CLOSE_ON_EMPTY)
675 vpp_evt_q = vcl_session_vpp_evt_q (wrk, session);
676 vcl_send_session_disconnected_reply (vpp_evt_q, vcm->my_client_index,
678 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: sending disconnect "
679 "REPLY...", getpid (), vpp_handle, session_handle);
683 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: sending disconnect...",
684 getpid (), vpp_handle, session_handle);
685 vppcom_send_disconnect_session (vpp_handle);
692 * VPPCOM Public API functions
695 vppcom_app_create (char *app_name)
697 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
703 vppcom_cfg (&vcm->cfg);
706 vcm->main_cpu = pthread_self ();
707 vppcom_init_error_string_table ();
708 svm_fifo_segment_main_init (vcl_cfg->segment_baseva,
709 20 /* timeout in secs */ );
710 pool_init_fixed (vcm->workers, vcl_cfg->max_workers);
711 clib_spinlock_init (&vcm->workers_lock);
712 vcl_worker_alloc_and_init ();
715 if (vcm->my_client_index == ~0)
717 /* API hookup and connect to VPP */
718 vppcom_api_hookup ();
720 vcm->app_state = STATE_APP_START;
721 rv = vppcom_connect_to_vpp (app_name);
724 clib_warning ("VCL<%d>: ERROR: couldn't connect to VPP!",
729 VDBG (0, "VCL<%d>: sending session enable", getpid ());
730 rv = vppcom_app_session_enable ();
733 clib_warning ("VCL<%d>: ERROR: vppcom_app_session_enable() "
734 "failed!", getpid ());
738 VDBG (0, "VCL<%d>: sending app attach", getpid ());
739 rv = vppcom_app_attach ();
742 clib_warning ("VCL<%d>: ERROR: vppcom_app_attach() failed!",
747 VDBG (0, "VCL<%d>: app_name '%s', my_client_index %d (0x%x)",
748 getpid (), app_name, vcm->my_client_index, vcm->my_client_index);
755 vppcom_app_destroy (void)
758 f64 orig_app_timeout;
760 if (vcm->my_client_index == ~0)
763 VDBG (0, "VCL<%d>: detaching from VPP, my_client_index %d (0x%x)",
764 getpid (), vcm->my_client_index, vcm->my_client_index);
765 vcl_evt (VCL_EVT_DETACH, vcm);
767 vppcom_app_send_detach ();
768 orig_app_timeout = vcm->cfg.app_timeout;
769 vcm->cfg.app_timeout = 2.0;
770 rv = vcl_wait_for_app_state_change (STATE_APP_ENABLED);
771 vcm->cfg.app_timeout = orig_app_timeout;
772 if (PREDICT_FALSE (rv))
773 VDBG (0, "VCL<%d>: application detach timed out! returning %d (%s)",
774 getpid (), rv, vppcom_retval_str (rv));
777 vl_client_disconnect_from_vlib ();
778 vcm->my_client_index = ~0;
779 vcm->app_state = STATE_APP_START;
783 vppcom_session_create (u8 proto, u8 is_nonblocking)
785 vcl_worker_t *wrk = vcl_worker_get_current ();
786 vcl_session_t *session;
788 session = vcl_session_alloc (wrk);
790 session->session_type = proto;
791 session->session_state = STATE_START;
792 session->vpp_handle = ~0;
793 session->is_dgram = proto == VPPCOM_PROTO_UDP;
796 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK);
798 vcl_evt (VCL_EVT_CREATE, session, session_type, session->session_state,
799 is_nonblocking, session_index);
801 VDBG (0, "VCL<%d>: sid %u", getpid (), session->session_index);
803 return vcl_session_handle (session);
807 vppcom_session_close (uint32_t session_handle)
809 vcl_worker_t *wrk = vcl_worker_get_current ();
810 vcl_session_t *session = 0;
811 u8 is_vep, is_vep_session;
812 session_state_t state;
817 session = vcl_session_get_w_handle (wrk, session_handle);
819 return VPPCOM_EBADFD;
821 is_vep = session->is_vep;
822 is_vep_session = session->is_vep_session;
823 next_sh = session->vep.next_sh;
824 vep_sh = session->vep.vep_sh;
825 state = session->session_state;
826 vpp_handle = session->vpp_handle;
828 if (VPPCOM_DEBUG > 0)
831 clib_warning ("VCL<%d>: vep_idx %u / sid %u: "
832 "closing epoll session...",
833 getpid (), session_handle, session_handle);
835 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %d: "
836 "closing session...",
837 getpid (), vpp_handle, session_handle);
842 while (next_sh != ~0)
844 rv = vppcom_epoll_ctl (session_handle, EPOLL_CTL_DEL, next_sh, 0);
845 if (PREDICT_FALSE (rv < 0))
846 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: EPOLL_CTL_DEL "
847 "vep_idx %u failed! rv %d (%s)",
848 getpid (), vpp_handle, next_sh, vep_sh,
849 rv, vppcom_retval_str (rv));
851 next_sh = session->vep.next_sh;
858 rv = vppcom_epoll_ctl (vep_sh, EPOLL_CTL_DEL, session_handle, 0);
860 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: EPOLL_CTL_DEL "
861 "vep_idx %u failed! rv %d (%s)",
862 getpid (), vpp_handle, session_handle,
863 vep_sh, rv, vppcom_retval_str (rv));
866 if (state & STATE_LISTEN)
868 rv = vppcom_session_unbind (session_handle);
869 if (PREDICT_FALSE (rv < 0))
870 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: listener unbind "
871 "failed! rv %d (%s)",
872 getpid (), vpp_handle, session_handle,
873 rv, vppcom_retval_str (rv));
875 else if (state & STATE_OPEN)
877 rv = vppcom_session_disconnect (session_handle);
878 if (PREDICT_FALSE (rv < 0))
879 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
880 "session disconnect failed! rv %d (%s)",
881 getpid (), vpp_handle, session_handle,
882 rv, vppcom_retval_str (rv));
886 if (vcl_session_is_ct (session))
888 vcl_cut_through_registration_t *ctr;
891 mq_addr = pointer_to_uword (session->our_evt_q);
892 ctr = vcl_ct_registration_lock_and_lookup (wrk, mq_addr);
894 if (ctr->epoll_evt_conn_index != ~0)
895 vcl_mq_epoll_del_evfd (wrk, ctr->epoll_evt_conn_index);
896 VDBG (0, "Removing ct registration %u",
897 vcl_ct_registration_index (wrk, ctr));
898 vcl_ct_registration_del (wrk, ctr);
899 vcl_ct_registration_lookup_del (wrk, mq_addr);
900 vcl_ct_registration_unlock (wrk);
903 if (vpp_handle != ~0)
905 vcl_session_table_del_vpp_handle (wrk, vpp_handle);
907 vcl_session_free (wrk, session);
909 if (VPPCOM_DEBUG > 0)
912 clib_warning ("VCL<%d>: vep_idx %u / sid %u: epoll session removed.",
913 getpid (), session_handle, session_handle);
915 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: session removed.",
916 getpid (), vpp_handle, session_handle);
919 vcl_evt (VCL_EVT_CLOSE, session, rv);
925 vppcom_session_bind (uint32_t session_handle, vppcom_endpt_t * ep)
927 vcl_worker_t *wrk = vcl_worker_get_current ();
928 vcl_session_t *session = 0;
931 return VPPCOM_EINVAL;
933 session = vcl_session_get_w_handle (wrk, session_handle);
935 return VPPCOM_EBADFD;
939 clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
940 "bind to an epoll session!", getpid (), session_handle);
941 return VPPCOM_EBADFD;
944 session->transport.is_ip4 = ep->is_ip4;
946 clib_memcpy (&session->transport.lcl_ip.ip4, ep->ip,
947 sizeof (ip4_address_t));
949 clib_memcpy (&session->transport.lcl_ip.ip6, ep->ip,
950 sizeof (ip6_address_t));
951 session->transport.lcl_port = ep->port;
953 VDBG (0, "VCL<%d>: sid %u: binding to local %s address %U port %u, "
954 "proto %s", getpid (), session_handle,
955 session->transport.is_ip4 ? "IPv4" : "IPv6",
956 format_ip46_address, &session->transport.lcl_ip,
957 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
958 clib_net_to_host_u16 (session->transport.lcl_port),
959 session->session_type ? "UDP" : "TCP");
960 vcl_evt (VCL_EVT_BIND, session);
962 if (session->session_type == VPPCOM_PROTO_UDP)
963 vppcom_session_listen (session_handle, 10);
969 vppcom_session_listen (uint32_t listen_sh, uint32_t q_len)
971 vcl_worker_t *wrk = vcl_worker_get_current ();
972 vcl_session_t *listen_session = 0;
973 u64 listen_vpp_handle;
976 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
978 return VPPCOM_EBADFD;
980 if (q_len == 0 || q_len == ~0)
981 q_len = vcm->cfg.listen_queue_size;
983 if (listen_session->is_vep)
985 clib_warning ("VCL<%d>: ERROR: sid %u: cannot listen on an "
986 "epoll session!", getpid (), listen_sh);
987 return VPPCOM_EBADFD;
990 listen_vpp_handle = listen_session->vpp_handle;
991 if (listen_session->session_state & STATE_LISTEN)
993 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: already in listen state!",
994 getpid (), listen_vpp_handle, listen_sh);
998 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: sending VPP bind+listen "
999 "request...", getpid (), listen_vpp_handle, listen_sh);
1002 * Send listen request to vpp and wait for reply
1004 vppcom_send_bind_sock (listen_session);
1005 rv = vppcom_wait_for_session_state_change (listen_session->session_index,
1007 vcm->cfg.session_timeout);
1009 if (PREDICT_FALSE (rv))
1011 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
1012 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: bind+listen failed! "
1013 "returning %d (%s)", getpid (), listen_session->vpp_handle,
1014 listen_sh, rv, vppcom_retval_str (rv));
1022 validate_args_session_accept_ (vcl_worker_t * wrk,
1023 vcl_session_t * listen_session)
1025 /* Input validation - expects spinlock on sessions_lockp */
1026 if (listen_session->is_vep)
1028 clib_warning ("VCL<%d>: ERROR: sid %u: cannot accept on an "
1029 "epoll session!", getpid (),
1030 listen_session->session_index);
1031 return VPPCOM_EBADFD;
1034 if (listen_session->session_state != STATE_LISTEN)
1036 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
1037 "not in listen state! state 0x%x (%s)", getpid (),
1038 listen_session->vpp_handle, listen_session->session_index,
1039 listen_session->session_state,
1040 vppcom_session_state_str (listen_session->session_state));
1041 return VPPCOM_EBADFD;
1047 vppcom_session_accept (uint32_t listen_session_handle, vppcom_endpt_t * ep,
1050 u32 client_session_index = ~0, listen_session_index;
1051 vcl_worker_t *wrk = vcl_worker_get_current ();
1052 session_accepted_msg_t accepted_msg;
1053 vcl_session_t *listen_session = 0;
1054 vcl_session_t *client_session = 0;
1055 svm_msg_q_t *vpp_evt_q;
1056 vcl_session_msg_t *evt;
1057 u64 listen_vpp_handle;
1058 svm_msg_q_msg_t msg;
1063 listen_session = vcl_session_get_w_handle (wrk, listen_session_handle);
1064 if (!listen_session)
1065 return VPPCOM_EBADFD;
1067 listen_session_index = listen_session->session_index;
1068 if ((rv = validate_args_session_accept_ (wrk, listen_session)))
1071 if (clib_fifo_elts (listen_session->accept_evts_fifo))
1073 clib_fifo_sub2 (listen_session->accept_evts_fifo, evt);
1074 accepted_msg = evt->accepted_msg;
1078 is_nonblocking = VCL_SESS_ATTR_TEST (listen_session->attr,
1079 VCL_SESS_ATTR_NONBLOCK);
1080 if (svm_msg_q_is_empty (wrk->app_event_queue) && is_nonblocking)
1081 return VPPCOM_EAGAIN;
1085 if (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_WAIT, 0))
1086 return VPPCOM_EAGAIN;
1088 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
1089 if (e->event_type != SESSION_CTRL_EVT_ACCEPTED)
1091 clib_warning ("discarded event: %u", e->event_type);
1092 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
1095 clib_memcpy (&accepted_msg, e->data, sizeof (accepted_msg));
1096 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
1102 client_session_index = vcl_session_accepted_handler (wrk, &accepted_msg);
1103 listen_session = vcl_session_get (wrk, listen_session_index);
1104 client_session = vcl_session_get (wrk, client_session_index);
1106 if (flags & O_NONBLOCK)
1107 VCL_SESS_ATTR_SET (client_session->attr, VCL_SESS_ATTR_NONBLOCK);
1109 listen_vpp_handle = listen_session->vpp_handle;
1110 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: Got a client request! "
1111 "vpp handle 0x%llx, sid %u, flags %d, is_nonblocking %u",
1112 getpid (), listen_vpp_handle, listen_session_handle,
1113 client_session->vpp_handle, client_session_index,
1114 flags, VCL_SESS_ATTR_TEST (client_session->attr,
1115 VCL_SESS_ATTR_NONBLOCK));
1119 ep->is_ip4 = client_session->transport.is_ip4;
1120 ep->port = client_session->transport.rmt_port;
1121 if (client_session->transport.is_ip4)
1122 clib_memcpy (ep->ip, &client_session->transport.rmt_ip.ip4,
1123 sizeof (ip4_address_t));
1125 clib_memcpy (ep->ip, &client_session->transport.rmt_ip.ip6,
1126 sizeof (ip6_address_t));
1129 if (accepted_msg.server_event_queue_address)
1130 vpp_evt_q = uword_to_pointer (accepted_msg.vpp_event_queue_address,
1133 vpp_evt_q = client_session->vpp_evt_q;
1135 vcl_send_session_accepted_reply (vpp_evt_q, client_session->client_context,
1136 client_session->vpp_handle, 0);
1138 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: accepted vpp handle 0x%llx, "
1139 "sid %u connection from peer %s address %U port %u to local %s "
1140 "address %U port %u", getpid (), listen_vpp_handle,
1141 listen_session_handle, client_session->vpp_handle,
1142 client_session_index,
1143 client_session->transport.is_ip4 ? "IPv4" : "IPv6",
1144 format_ip46_address, &client_session->transport.rmt_ip,
1145 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
1146 clib_net_to_host_u16 (client_session->transport.rmt_port),
1147 client_session->transport.is_ip4 ? "IPv4" : "IPv6",
1148 format_ip46_address, &client_session->transport.lcl_ip,
1149 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
1150 clib_net_to_host_u16 (client_session->transport.lcl_port));
1151 vcl_evt (VCL_EVT_ACCEPT, client_session, listen_session,
1152 client_session_index);
1154 return vcl_session_handle (client_session);
1158 vppcom_session_connect (uint32_t session_handle, vppcom_endpt_t * server_ep)
1160 vcl_worker_t *wrk = vcl_worker_get_current ();
1161 vcl_session_t *session = 0;
1165 session = vcl_session_get_w_handle (wrk, session_handle);
1167 return VPPCOM_EBADFD;
1168 session_index = session->session_index;
1170 if (PREDICT_FALSE (session->is_vep))
1172 clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
1173 "connect on an epoll session!", getpid (),
1175 return VPPCOM_EBADFD;
1178 if (PREDICT_FALSE (session->session_state & CLIENT_STATE_OPEN))
1180 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: session already "
1181 "connected to %s %U port %d proto %s, state 0x%x (%s)",
1182 getpid (), session->vpp_handle, session_handle,
1183 session->transport.is_ip4 ? "IPv4" : "IPv6",
1184 format_ip46_address,
1185 &session->transport.rmt_ip, session->transport.is_ip4 ?
1186 IP46_TYPE_IP4 : IP46_TYPE_IP6,
1187 clib_net_to_host_u16 (session->transport.rmt_port),
1188 session->session_type ? "UDP" : "TCP", session->session_state,
1189 vppcom_session_state_str (session->session_state));
1193 session->transport.is_ip4 = server_ep->is_ip4;
1194 if (session->transport.is_ip4)
1195 clib_memcpy (&session->transport.rmt_ip.ip4, server_ep->ip,
1196 sizeof (ip4_address_t));
1198 clib_memcpy (&session->transport.rmt_ip.ip6, server_ep->ip,
1199 sizeof (ip6_address_t));
1200 session->transport.rmt_port = server_ep->port;
1202 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: connecting to server %s %U "
1204 getpid (), session->vpp_handle, session_handle,
1205 session->transport.is_ip4 ? "IPv4" : "IPv6",
1206 format_ip46_address,
1207 &session->transport.rmt_ip, session->transport.is_ip4 ?
1208 IP46_TYPE_IP4 : IP46_TYPE_IP6,
1209 clib_net_to_host_u16 (session->transport.rmt_port),
1210 session->session_type ? "UDP" : "TCP");
1213 * Send connect request and wait for reply from vpp
1215 vppcom_send_connect_sock (session);
1216 rv = vppcom_wait_for_session_state_change (session_index, STATE_CONNECT,
1217 vcm->cfg.session_timeout);
1219 session = vcl_session_get (wrk, session_index);
1221 if (PREDICT_FALSE (rv))
1223 if (VPPCOM_DEBUG > 0)
1226 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: connect "
1227 "failed! returning %d (%s)", getpid (),
1228 session->vpp_handle, session_handle, rv,
1229 vppcom_retval_str (rv));
1231 clib_warning ("VCL<%d>: no session for sid %u: connect failed! "
1232 "returning %d (%s)", getpid (),
1233 session_handle, rv, vppcom_retval_str (rv));
1237 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: connected!",
1238 getpid (), session->vpp_handle, session_handle);
1244 vcl_is_rx_evt_for_session (session_event_t * e, u32 sid, u8 is_ct)
1247 return (e->event_type == FIFO_EVENT_APP_RX
1248 && e->fifo->client_session_index == sid);
1250 return (e->event_type == SESSION_IO_EVT_CT_TX);
1254 vcl_session_is_readable (vcl_session_t * s)
1256 return ((s->session_state & STATE_OPEN)
1257 || (s->session_state == STATE_LISTEN
1258 && s->session_type == VPPCOM_PROTO_UDP));
1262 vppcom_session_read_internal (uint32_t session_handle, void *buf, int n,
1265 vcl_worker_t *wrk = vcl_worker_get_current ();
1266 int n_read = 0, rv, is_nonblocking;
1267 vcl_session_t *s = 0;
1268 svm_fifo_t *rx_fifo;
1269 svm_msg_q_msg_t msg;
1274 if (PREDICT_FALSE (!buf))
1275 return VPPCOM_EINVAL;
1277 s = vcl_session_get_w_handle (wrk, session_handle);
1278 if (PREDICT_FALSE (!s || s->is_vep))
1279 return VPPCOM_EBADFD;
1281 if (PREDICT_FALSE (!vcl_session_is_readable (s)))
1283 session_state_t state = s->session_state;
1284 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
1286 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: %s session is not open! "
1287 "state 0x%x (%s), returning %d (%s)",
1288 getpid (), s->vpp_handle, session_handle, state,
1289 vppcom_session_state_str (state), rv, vppcom_retval_str (rv));
1293 is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
1294 is_ct = vcl_session_is_ct (s);
1295 mq = is_ct ? s->our_evt_q : wrk->app_event_queue;
1296 rx_fifo = s->rx_fifo;
1298 if (svm_fifo_is_empty (rx_fifo))
1302 svm_fifo_unset_event (rx_fifo);
1305 while (svm_fifo_is_empty (rx_fifo))
1307 svm_fifo_unset_event (rx_fifo);
1308 svm_msg_q_lock (mq);
1309 if (svm_msg_q_is_empty (mq))
1310 svm_msg_q_wait (mq);
1312 svm_msg_q_sub_w_lock (mq, &msg);
1313 e = svm_msg_q_msg_data (mq, &msg);
1314 svm_msg_q_unlock (mq);
1315 if (!vcl_is_rx_evt_for_session (e, s->session_index, is_ct))
1317 vcl_handle_mq_event (wrk, e);
1318 svm_msg_q_free_msg (mq, &msg);
1321 svm_msg_q_free_msg (mq, &msg);
1323 if (PREDICT_FALSE (s->session_state == STATE_CLOSE_ON_EMPTY))
1329 n_read = app_recv_dgram_raw (rx_fifo, buf, n, &s->transport, 0, peek);
1331 n_read = app_recv_stream_raw (rx_fifo, buf, n, 0, peek);
1333 if (svm_fifo_is_empty (rx_fifo))
1334 svm_fifo_unset_event (rx_fifo);
1336 if (is_ct && n_read + svm_fifo_max_dequeue (rx_fifo) == rx_fifo->nitems)
1338 /* If the peer is not polling send notification */
1339 if (!svm_fifo_has_event (s->rx_fifo))
1340 app_send_io_evt_to_vpp (s->vpp_evt_q, s->rx_fifo,
1341 SESSION_IO_EVT_CT_RX, SVM_Q_WAIT);
1344 VDBG (2, "VCL<%d>: vpp handle 0x%llx, sid %u: read %d bytes from (%p)",
1345 getpid (), s->vpp_handle, session_handle, n_read, rx_fifo);
1351 vppcom_session_read (uint32_t session_handle, void *buf, size_t n)
1353 return (vppcom_session_read_internal (session_handle, buf, n, 0));
1357 vppcom_session_peek (uint32_t session_handle, void *buf, int n)
1359 return (vppcom_session_read_internal (session_handle, buf, n, 1));
1363 vppcom_session_read_segments (uint32_t session_handle,
1364 vppcom_data_segments_t ds)
1366 vcl_worker_t *wrk = vcl_worker_get_current ();
1367 int n_read = 0, rv, is_nonblocking;
1368 vcl_session_t *s = 0;
1369 svm_fifo_t *rx_fifo;
1370 svm_msg_q_msg_t msg;
1375 s = vcl_session_get_w_handle (wrk, session_handle);
1376 if (PREDICT_FALSE (!s || s->is_vep))
1377 return VPPCOM_EBADFD;
1379 if (PREDICT_FALSE (!vcl_session_is_readable (s)))
1381 session_state_t state = s->session_state;
1382 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
1386 is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
1387 is_ct = vcl_session_is_ct (s);
1388 mq = is_ct ? s->our_evt_q : wrk->app_event_queue;
1389 rx_fifo = s->rx_fifo;
1391 if (svm_fifo_is_empty (rx_fifo))
1395 svm_fifo_unset_event (rx_fifo);
1398 while (svm_fifo_is_empty (rx_fifo))
1400 svm_fifo_unset_event (rx_fifo);
1401 svm_msg_q_lock (mq);
1402 if (svm_msg_q_is_empty (mq))
1403 svm_msg_q_wait (mq);
1405 svm_msg_q_sub_w_lock (mq, &msg);
1406 e = svm_msg_q_msg_data (mq, &msg);
1407 svm_msg_q_unlock (mq);
1408 if (!vcl_is_rx_evt_for_session (e, s->session_index, is_ct))
1410 vcl_handle_mq_event (wrk, e);
1411 svm_msg_q_free_msg (mq, &msg);
1414 svm_msg_q_free_msg (mq, &msg);
1416 if (PREDICT_FALSE (s->session_state == STATE_CLOSE_ON_EMPTY))
1421 n_read = svm_fifo_segments (rx_fifo, (svm_fifo_segment_t *) ds);
1422 svm_fifo_unset_event (rx_fifo);
1424 if (is_ct && n_read + svm_fifo_max_dequeue (rx_fifo) == rx_fifo->nitems)
1426 /* If the peer is not polling send notification */
1427 if (!svm_fifo_has_event (s->rx_fifo))
1428 app_send_io_evt_to_vpp (s->vpp_evt_q, s->rx_fifo,
1429 SESSION_IO_EVT_CT_RX, SVM_Q_WAIT);
1436 vppcom_session_free_segments (uint32_t session_handle,
1437 vppcom_data_segments_t ds)
1439 vcl_worker_t *wrk = vcl_worker_get_current ();
1442 s = vcl_session_get_w_handle (wrk, session_handle);
1443 if (PREDICT_FALSE (!s || s->is_vep))
1446 svm_fifo_segments_free (s->rx_fifo, (svm_fifo_segment_t *) ds);
1450 vppcom_session_read_ready (vcl_session_t * session)
1452 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
1453 if (PREDICT_FALSE (session->is_vep))
1455 clib_warning ("VCL<%d>: ERROR: sid %u: cannot read from an "
1456 "epoll session!", getpid (), session->session_index);
1457 return VPPCOM_EBADFD;
1460 if (PREDICT_FALSE (!(session->session_state & (STATE_OPEN | STATE_LISTEN))))
1462 session_state_t state = session->session_state;
1465 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
1467 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: session is not open!"
1468 " state 0x%x (%s), returning %d (%s)", getpid (),
1469 session->vpp_handle, session->session_index, state,
1470 vppcom_session_state_str (state), rv, vppcom_retval_str (rv));
1474 if (session->session_state & STATE_LISTEN)
1475 return clib_fifo_elts (session->accept_evts_fifo);
1477 return svm_fifo_max_dequeue (session->rx_fifo);
1481 vppcom_data_segment_copy (void *buf, vppcom_data_segments_t ds, u32 max_bytes)
1483 u32 first_copy = clib_min (ds[0].len, max_bytes);
1484 clib_memcpy (buf, ds[0].data, first_copy);
1485 if (first_copy < max_bytes)
1487 clib_memcpy (buf + first_copy, ds[1].data,
1488 clib_min (ds[1].len, max_bytes - first_copy));
1494 vcl_is_tx_evt_for_session (session_event_t * e, u32 sid, u8 is_ct)
1497 return (e->event_type == FIFO_EVENT_APP_TX
1498 && e->fifo->client_session_index == sid);
1500 return (e->event_type == SESSION_IO_EVT_CT_RX);
1504 vppcom_session_write (uint32_t session_handle, void *buf, size_t n)
1506 vcl_worker_t *wrk = vcl_worker_get_current ();
1507 int rv, n_write, is_nonblocking;
1508 vcl_session_t *s = 0;
1509 svm_fifo_t *tx_fifo = 0;
1510 session_evt_type_t et;
1511 svm_msg_q_msg_t msg;
1515 if (PREDICT_FALSE (!buf))
1516 return VPPCOM_EINVAL;
1518 s = vcl_session_get_w_handle (wrk, session_handle);
1519 if (PREDICT_FALSE (!s))
1520 return VPPCOM_EBADFD;
1522 tx_fifo = s->tx_fifo;
1523 is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
1525 if (PREDICT_FALSE (s->is_vep))
1527 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
1528 "cannot write to an epoll session!",
1529 getpid (), s->vpp_handle, session_handle);
1531 return VPPCOM_EBADFD;
1534 if (!(s->session_state & STATE_OPEN))
1536 session_state_t state = s->session_state;
1537 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
1538 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: session is not open! "
1540 getpid (), s->vpp_handle, session_handle,
1541 state, vppcom_session_state_str (state));
1545 mq = vcl_session_is_ct (s) ? s->our_evt_q : wrk->app_event_queue;
1546 if (svm_fifo_is_full (tx_fifo))
1550 return VPPCOM_EWOULDBLOCK;
1552 while (svm_fifo_is_full (tx_fifo))
1554 svm_msg_q_lock (mq);
1555 while (svm_msg_q_is_empty (mq) && svm_msg_q_timedwait (mq, 10e-6))
1557 svm_msg_q_sub_w_lock (mq, &msg);
1558 e = svm_msg_q_msg_data (mq, &msg);
1559 svm_msg_q_unlock (mq);
1561 if (!vcl_is_tx_evt_for_session (e, s->session_index,
1563 vcl_handle_mq_event (wrk, e);
1564 svm_msg_q_free_msg (mq, &msg);
1568 ASSERT (FIFO_EVENT_APP_TX + 1 == SESSION_IO_EVT_CT_TX);
1569 et = FIFO_EVENT_APP_TX + vcl_session_is_ct (s);
1571 n_write = app_send_dgram_raw (tx_fifo, &s->transport,
1572 s->vpp_evt_q, buf, n, et, SVM_Q_WAIT);
1574 n_write = app_send_stream_raw (tx_fifo, s->vpp_evt_q, buf, n, et,
1577 ASSERT (n_write > 0);
1579 if (VPPCOM_DEBUG > 2)
1582 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
1583 "FIFO-FULL (%p)", getpid (), s->vpp_handle,
1584 session_handle, tx_fifo);
1586 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
1587 "wrote %d bytes tx-fifo: (%p)", getpid (),
1588 s->vpp_handle, session_handle, n_write, tx_fifo);
1593 static vcl_session_t *
1594 vcl_ct_session_get_from_fifo (vcl_worker_t * wrk, svm_fifo_t * f, u8 type)
1597 s = vcl_session_get (wrk, f->client_session_index);
1601 if (type == 0 && s->rx_fifo == f)
1604 if (type == 1 && s->tx_fifo == f)
1607 s = vcl_session_get (wrk, f->master_session_index);
1610 if (type == 0 && s->rx_fifo == f)
1612 if (type == 1 && s->tx_fifo == f)
1619 vppcom_session_write_ready (vcl_session_t * session)
1621 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
1622 if (PREDICT_FALSE (session->is_vep))
1624 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
1625 "cannot write to an epoll session!",
1626 getpid (), session->vpp_handle, session->session_index);
1627 return VPPCOM_EBADFD;
1630 if (PREDICT_FALSE (session->session_state & STATE_LISTEN))
1632 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
1633 "cannot write to a listen session!",
1634 getpid (), session->vpp_handle, session->session_index);
1635 return VPPCOM_EBADFD;
1638 if (PREDICT_FALSE (!(session->session_state & STATE_OPEN)))
1640 session_state_t state = session->session_state;
1643 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
1644 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
1645 "session is not open! state 0x%x (%s), "
1646 "returning %d (%s)", getpid (), session->vpp_handle,
1647 session->session_index,
1648 state, vppcom_session_state_str (state),
1649 rv, vppcom_retval_str (rv));
1653 VDBG (3, "VCL<%d>: vpp handle 0x%llx, sid %u: peek %s (%p), ready = %d",
1654 getpid (), session->vpp_handle, session->session_index,
1655 session->tx_fifo, svm_fifo_max_enqueue (session->tx_fifo));
1657 return svm_fifo_max_enqueue (session->tx_fifo);
1661 vcl_mq_dequeue_batch (vcl_worker_t * wrk, svm_msg_q_t * mq)
1663 svm_msg_q_msg_t *msg;
1667 n_msgs = svm_msg_q_size (mq);
1668 for (i = 0; i < n_msgs; i++)
1670 vec_add2 (wrk->mq_msg_vector, msg, 1);
1671 svm_msg_q_sub_w_lock (mq, msg);
1676 #define vcl_fifo_rx_evt_valid_or_break(_fifo) \
1677 if (PREDICT_FALSE (svm_fifo_is_empty (_fifo))) \
1679 svm_fifo_unset_event (_fifo); \
1680 if (svm_fifo_is_empty (_fifo)) \
1685 vcl_select_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
1686 unsigned long n_bits, unsigned long *read_map,
1687 unsigned long *write_map,
1688 unsigned long *except_map, u32 * bits_set)
1690 session_disconnected_msg_t *disconnected_msg;
1691 session_connected_msg_t *connected_msg;
1692 session_accepted_msg_t *accepted_msg;
1693 vcl_session_msg_t *vcl_msg;
1694 vcl_session_t *session;
1698 switch (e->event_type)
1700 case FIFO_EVENT_APP_RX:
1701 vcl_fifo_rx_evt_valid_or_break (e->fifo);
1702 sid = e->fifo->client_session_index;
1703 session = vcl_session_get (wrk, sid);
1706 if (sid < n_bits && read_map)
1708 clib_bitmap_set_no_check (read_map, sid, 1);
1712 case FIFO_EVENT_APP_TX:
1713 sid = e->fifo->client_session_index;
1714 session = vcl_session_get (wrk, sid);
1717 if (sid < n_bits && write_map)
1719 clib_bitmap_set_no_check (write_map, sid, 1);
1723 case SESSION_IO_EVT_CT_TX:
1724 vcl_fifo_rx_evt_valid_or_break (e->fifo);
1725 session = vcl_ct_session_get_from_fifo (wrk, e->fifo, 0);
1728 sid = session->session_index;
1729 if (sid < n_bits && read_map)
1731 clib_bitmap_set_no_check (read_map, sid, 1);
1735 case SESSION_IO_EVT_CT_RX:
1736 session = vcl_ct_session_get_from_fifo (wrk, e->fifo, 1);
1739 sid = session->session_index;
1740 if (sid < n_bits && write_map)
1742 clib_bitmap_set_no_check (write_map, sid, 1);
1746 case SESSION_CTRL_EVT_ACCEPTED:
1747 accepted_msg = (session_accepted_msg_t *) e->data;
1748 handle = accepted_msg->listener_handle;
1749 session = vcl_session_table_lookup_listener (wrk, handle);
1752 clib_warning ("VCL<%d>: ERROR: couldn't find listen session:"
1753 "listener handle %llx", getpid (), handle);
1757 clib_fifo_add2 (session->accept_evts_fifo, vcl_msg);
1758 vcl_msg->accepted_msg = *accepted_msg;
1759 sid = session->session_index;
1760 if (sid < n_bits && read_map)
1762 clib_bitmap_set_no_check (read_map, sid, 1);
1766 case SESSION_CTRL_EVT_CONNECTED:
1767 connected_msg = (session_connected_msg_t *) e->data;
1768 vcl_session_connected_handler (wrk, connected_msg);
1770 case SESSION_CTRL_EVT_DISCONNECTED:
1771 disconnected_msg = (session_disconnected_msg_t *) e->data;
1772 sid = vcl_session_index_from_vpp_handle (wrk, disconnected_msg->handle);
1773 if (sid < n_bits && except_map)
1775 clib_bitmap_set_no_check (except_map, sid, 1);
1779 case SESSION_CTRL_EVT_RESET:
1780 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
1781 if (sid < n_bits && except_map)
1783 clib_bitmap_set_no_check (except_map, sid, 1);
1788 clib_warning ("unhandled: %u", e->event_type);
1794 vcl_select_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
1795 unsigned long n_bits, unsigned long *read_map,
1796 unsigned long *write_map, unsigned long *except_map,
1797 double time_to_wait, u32 * bits_set)
1799 svm_msg_q_msg_t *msg;
1803 svm_msg_q_lock (mq);
1804 if (svm_msg_q_is_empty (mq))
1808 svm_msg_q_unlock (mq);
1814 svm_msg_q_unlock (mq);
1817 else if (time_to_wait < 0)
1819 svm_msg_q_wait (mq);
1823 if (svm_msg_q_timedwait (mq, time_to_wait))
1825 svm_msg_q_unlock (mq);
1830 vcl_mq_dequeue_batch (wrk, mq);
1831 svm_msg_q_unlock (mq);
1833 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
1835 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
1836 e = svm_msg_q_msg_data (mq, msg);
1837 vcl_select_handle_mq_event (wrk, e, n_bits, read_map, write_map,
1838 except_map, bits_set);
1839 svm_msg_q_free_msg (mq, msg);
1841 vec_reset_length (wrk->mq_msg_vector);
1846 vppcom_select_condvar (vcl_worker_t * wrk, unsigned long n_bits,
1847 unsigned long *read_map, unsigned long *write_map,
1848 unsigned long *except_map, double time_to_wait,
1851 double total_wait = 0, wait_slice;
1852 vcl_cut_through_registration_t *cr;
1854 time_to_wait = (time_to_wait == -1) ? 10e9 : time_to_wait;
1855 wait_slice = wrk->cut_through_registrations ? 10e-6 : time_to_wait;
1858 vcl_ct_registration_lock (wrk);
1860 pool_foreach (cr, wrk->cut_through_registrations, ({
1861 vcl_select_handle_mq (wrk, cr->mq, n_bits, read_map, write_map, except_map,
1865 vcl_ct_registration_unlock (wrk);
1867 vcl_select_handle_mq (wrk, wrk->app_event_queue, n_bits, read_map,
1868 write_map, except_map, time_to_wait, bits_set);
1869 total_wait += wait_slice;
1873 while (total_wait < time_to_wait);
1879 vppcom_select_eventfd (vcl_worker_t * wrk, unsigned long n_bits,
1880 unsigned long *read_map, unsigned long *write_map,
1881 unsigned long *except_map, double time_to_wait,
1884 vcl_mq_evt_conn_t *mqc;
1885 int __clib_unused n_read;
1889 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
1890 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
1891 vec_len (wrk->mq_events), time_to_wait);
1892 for (i = 0; i < n_mq_evts; i++)
1894 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
1895 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
1896 vcl_select_handle_mq (wrk, mqc->mq, n_bits, read_map, write_map,
1897 except_map, 0, bits_set);
1900 return (n_mq_evts > 0 ? (int) *bits_set : 0);
1904 vppcom_select (unsigned long n_bits, unsigned long *read_map,
1905 unsigned long *write_map, unsigned long *except_map,
1906 double time_to_wait)
1908 u32 sid, minbits = clib_max (n_bits, BITS (uword)), bits_set = 0;
1909 vcl_worker_t *wrk = vcl_worker_get_current ();
1910 vcl_session_t *session = 0;
1913 ASSERT (sizeof (clib_bitmap_t) == sizeof (long int));
1915 if (n_bits && read_map)
1917 clib_bitmap_validate (wrk->rd_bitmap, minbits);
1918 clib_memcpy (wrk->rd_bitmap, read_map,
1919 vec_len (wrk->rd_bitmap) * sizeof (clib_bitmap_t));
1920 memset (read_map, 0, vec_len (wrk->rd_bitmap) * sizeof (clib_bitmap_t));
1922 if (n_bits && write_map)
1924 clib_bitmap_validate (wrk->wr_bitmap, minbits);
1925 clib_memcpy (wrk->wr_bitmap, write_map,
1926 vec_len (wrk->wr_bitmap) * sizeof (clib_bitmap_t));
1927 memset (write_map, 0,
1928 vec_len (wrk->wr_bitmap) * sizeof (clib_bitmap_t));
1930 if (n_bits && except_map)
1932 clib_bitmap_validate (wrk->ex_bitmap, minbits);
1933 clib_memcpy (wrk->ex_bitmap, except_map,
1934 vec_len (wrk->ex_bitmap) * sizeof (clib_bitmap_t));
1935 memset (except_map, 0,
1936 vec_len (wrk->ex_bitmap) * sizeof (clib_bitmap_t));
1946 clib_bitmap_foreach (sid, wrk->wr_bitmap, ({
1947 if (!(session = vcl_session_get (wrk, sid)))
1949 VDBG (0, "VCL<%d>: session %d specified in write_map is closed.",
1951 return VPPCOM_EBADFD;
1954 rv = svm_fifo_is_full (session->tx_fifo);
1957 clib_bitmap_set_no_check (write_map, sid, 1);
1966 clib_bitmap_foreach (sid, wrk->rd_bitmap, ({
1967 if (!(session = vcl_session_get (wrk, sid)))
1969 VDBG (0, "VCL<%d>: session %d specified in write_map is closed.",
1971 return VPPCOM_EBADFD;
1974 rv = vppcom_session_read_ready (session);
1977 clib_bitmap_set_no_check (read_map, sid, 1);
1985 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
1987 vcl_select_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i], n_bits,
1988 read_map, write_map, except_map, &bits_set);
1990 vec_reset_length (wrk->unhandled_evts_vector);
1992 if (vcm->cfg.use_mq_eventfd)
1993 vppcom_select_eventfd (wrk, n_bits, read_map, write_map, except_map,
1994 time_to_wait, &bits_set);
1996 vppcom_select_condvar (wrk, n_bits, read_map, write_map, except_map,
1997 time_to_wait, &bits_set);
2003 vep_verify_epoll_chain (vcl_worker_t * wrk, u32 vep_idx)
2005 vcl_session_t *session;
2006 vppcom_epoll_t *vep;
2009 if (VPPCOM_DEBUG <= 1)
2012 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
2013 session = vcl_session_get (wrk, vep_idx);
2014 if (PREDICT_FALSE (!session))
2016 clib_warning ("VCL<%d>: ERROR: Invalid vep_idx (%u)!",
2017 getpid (), vep_idx);
2020 if (PREDICT_FALSE (!session->is_vep))
2022 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
2023 getpid (), vep_idx);
2026 vep = &session->vep;
2027 clib_warning ("VCL<%d>: vep_idx (%u): Dumping epoll chain\n"
2030 " is_vep_session = %u\n"
2031 " next_sid = 0x%x (%u)\n"
2032 " wait_cont_idx = 0x%x (%u)\n"
2033 "}\n", getpid (), vep_idx,
2034 session->is_vep, session->is_vep_session,
2035 vep->next_sh, vep->next_sh,
2036 session->wait_cont_idx, session->wait_cont_idx);
2038 for (sid = vep->next_sh; sid != ~0; sid = vep->next_sh)
2040 session = vcl_session_get (wrk, sid);
2041 if (PREDICT_FALSE (!session))
2043 clib_warning ("VCL<%d>: ERROR: Invalid sid (%u)!", getpid (), sid);
2046 if (PREDICT_FALSE (session->is_vep))
2047 clib_warning ("VCL<%d>: ERROR: sid (%u) is a vep!",
2048 getpid (), vep_idx);
2049 else if (PREDICT_FALSE (!session->is_vep_session))
2051 clib_warning ("VCL<%d>: ERROR: session (%u) "
2052 "is not a vep session!", getpid (), sid);
2055 vep = &session->vep;
2056 if (PREDICT_FALSE (vep->vep_sh != vep_idx))
2057 clib_warning ("VCL<%d>: ERROR: session (%u) vep_idx (%u) != "
2058 "vep_idx (%u)!", getpid (),
2059 sid, session->vep.vep_sh, vep_idx);
2060 if (session->is_vep_session)
2062 clib_warning ("vep_idx[%u]: sid 0x%x (%u)\n"
2064 " next_sid = 0x%x (%u)\n"
2065 " prev_sid = 0x%x (%u)\n"
2066 " vep_idx = 0x%x (%u)\n"
2067 " ev.events = 0x%x\n"
2068 " ev.data.u64 = 0x%llx\n"
2072 vep->next_sh, vep->next_sh,
2073 vep->prev_sh, vep->prev_sh,
2074 vep->vep_sh, vep->vep_sh,
2075 vep->ev.events, vep->ev.data.u64, vep->et_mask);
2080 clib_warning ("VCL<%d>: vep_idx (%u): Dump complete!\n",
2081 getpid (), vep_idx);
2085 vppcom_epoll_create (void)
2087 vcl_worker_t *wrk = vcl_worker_get_current ();
2088 vcl_session_t *vep_session;
2090 vep_session = vcl_session_alloc (wrk);
2092 vep_session->is_vep = 1;
2093 vep_session->vep.vep_sh = ~0;
2094 vep_session->vep.next_sh = ~0;
2095 vep_session->vep.prev_sh = ~0;
2096 vep_session->wait_cont_idx = ~0;
2097 vep_session->vpp_handle = ~0;
2099 vcl_evt (VCL_EVT_EPOLL_CREATE, vep_session, vep_sh);
2100 VDBG (0, "VCL<%d>: Created vep_idx %u / sid %u!",
2101 getpid (), vep_session->session_index, vep_session->session_index);
2103 return vcl_session_handle (vep_session);
2107 vppcom_epoll_ctl (uint32_t vep_handle, int op, uint32_t session_handle,
2108 struct epoll_event *event)
2110 vcl_worker_t *wrk = vcl_worker_get_current ();
2111 vcl_session_t *vep_session;
2112 vcl_session_t *session;
2115 if (vep_handle == session_handle)
2117 clib_warning ("VCL<%d>: ERROR: vep_idx == session_index (%u)!",
2118 getpid (), vep_handle);
2119 return VPPCOM_EINVAL;
2122 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
2123 if (PREDICT_FALSE (!vep_session))
2125 clib_warning ("VCL<%d>: ERROR: Invalid vep_idx (%u)!", vep_handle);
2126 return VPPCOM_EBADFD;
2128 if (PREDICT_FALSE (!vep_session->is_vep))
2130 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
2131 getpid (), vep_handle);
2132 return VPPCOM_EINVAL;
2135 ASSERT (vep_session->vep.vep_sh == ~0);
2136 ASSERT (vep_session->vep.prev_sh == ~0);
2138 session = vcl_session_get_w_handle (wrk, session_handle);
2139 if (PREDICT_FALSE (!session))
2141 VDBG (0, "VCL<%d>: ERROR: Invalid session_handle (%u)!",
2142 getpid (), session_handle);
2143 return VPPCOM_EBADFD;
2145 if (PREDICT_FALSE (session->is_vep))
2147 clib_warning ("ERROR: session_handle (%u) is a vep!", vep_handle);
2148 return VPPCOM_EINVAL;
2154 if (PREDICT_FALSE (!event))
2156 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_ADD: NULL pointer to "
2157 "epoll_event structure!", getpid ());
2158 return VPPCOM_EINVAL;
2160 if (vep_session->vep.next_sh != ~0)
2162 vcl_session_t *next_session;
2163 next_session = vcl_session_get_w_handle (wrk,
2164 vep_session->vep.next_sh);
2165 if (PREDICT_FALSE (!next_session))
2167 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_ADD: Invalid "
2168 "vep.next_sid (%u) on vep_idx (%u)!",
2169 getpid (), vep_session->vep.next_sh, vep_handle);
2170 return VPPCOM_EBADFD;
2172 ASSERT (next_session->vep.prev_sh == vep_handle);
2173 next_session->vep.prev_sh = session_handle;
2175 session->vep.next_sh = vep_session->vep.next_sh;
2176 session->vep.prev_sh = vep_handle;
2177 session->vep.vep_sh = vep_handle;
2178 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
2179 session->vep.ev = *event;
2180 session->is_vep = 0;
2181 session->is_vep_session = 1;
2182 vep_session->vep.next_sh = session_handle;
2184 VDBG (1, "VCL<%d>: EPOLL_CTL_ADD: vep_idx %u, sid %u, events 0x%x, "
2185 "data 0x%llx!", getpid (), vep_handle, session_handle,
2186 event->events, event->data.u64);
2187 vcl_evt (VCL_EVT_EPOLL_CTLADD, session, event->events, event->data.u64);
2191 if (PREDICT_FALSE (!event))
2193 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_MOD: NULL pointer to "
2194 "epoll_event structure!", getpid ());
2198 else if (PREDICT_FALSE (!session->is_vep_session))
2200 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_MOD: "
2201 "not a vep session!", getpid (), session_handle);
2205 else if (PREDICT_FALSE (session->vep.vep_sh != vep_handle))
2207 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_MOD: "
2208 "vep_idx (%u) != vep_idx (%u)!",
2209 getpid (), session_handle,
2210 session->vep.vep_sh, vep_handle);
2214 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
2215 session->vep.ev = *event;
2216 VDBG (1, "VCL<%d>: EPOLL_CTL_MOD: vep_idx %u, sid %u, events 0x%x,"
2217 " data 0x%llx!", getpid (), vep_handle, session_handle,
2218 event->events, event->data.u64);
2222 if (PREDICT_FALSE (!session->is_vep_session))
2224 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_DEL: "
2225 "not a vep session!", getpid (), session_handle);
2229 else if (PREDICT_FALSE (session->vep.vep_sh != vep_handle))
2231 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_DEL: "
2232 "vep_idx (%u) != vep_idx (%u)!",
2233 getpid (), session_handle,
2234 session->vep.vep_sh, vep_handle);
2239 vep_session->wait_cont_idx =
2240 (vep_session->wait_cont_idx == session_handle) ?
2241 session->vep.next_sh : vep_session->wait_cont_idx;
2243 if (session->vep.prev_sh == vep_handle)
2244 vep_session->vep.next_sh = session->vep.next_sh;
2247 vcl_session_t *prev_session;
2248 prev_session = vcl_session_get_w_handle (wrk, session->vep.prev_sh);
2249 if (PREDICT_FALSE (!prev_session))
2251 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_DEL: Invalid "
2252 "vep.prev_sid (%u) on sid (%u)!",
2253 getpid (), session->vep.prev_sh, session_handle);
2254 return VPPCOM_EBADFD;
2256 ASSERT (prev_session->vep.next_sh == session_handle);
2257 prev_session->vep.next_sh = session->vep.next_sh;
2259 if (session->vep.next_sh != ~0)
2261 vcl_session_t *next_session;
2262 next_session = vcl_session_get_w_handle (wrk, session->vep.next_sh);
2263 if (PREDICT_FALSE (!next_session))
2265 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_DEL: Invalid "
2266 "vep.next_sid (%u) on sid (%u)!",
2267 getpid (), session->vep.next_sh, session_handle);
2268 return VPPCOM_EBADFD;
2270 ASSERT (next_session->vep.prev_sh == session_handle);
2271 next_session->vep.prev_sh = session->vep.prev_sh;
2274 memset (&session->vep, 0, sizeof (session->vep));
2275 session->vep.next_sh = ~0;
2276 session->vep.prev_sh = ~0;
2277 session->vep.vep_sh = ~0;
2278 session->is_vep_session = 0;
2279 VDBG (1, "VCL<%d>: EPOLL_CTL_DEL: vep_idx %u, sid %u!",
2280 getpid (), vep_handle, session_handle);
2281 vcl_evt (VCL_EVT_EPOLL_CTLDEL, session, vep_sh);
2285 clib_warning ("VCL<%d>: ERROR: Invalid operation (%d)!", getpid (), op);
2289 vep_verify_epoll_chain (wrk, vep_handle);
2296 vcl_epoll_wait_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
2297 struct epoll_event *events, u32 * num_ev)
2299 session_disconnected_msg_t *disconnected_msg;
2300 session_connected_msg_t *connected_msg;
2301 session_accepted_msg_t *accepted_msg;
2302 u64 session_evt_data = ~0, handle;
2303 u32 sid = ~0, session_events;
2304 vcl_session_msg_t *vcl_msg;
2305 vcl_session_t *session;
2308 switch (e->event_type)
2310 case FIFO_EVENT_APP_RX:
2311 ASSERT (e->fifo->client_thread_index == vcl_get_worker_index ());
2312 vcl_fifo_rx_evt_valid_or_break (e->fifo);
2313 sid = e->fifo->client_session_index;
2314 session = vcl_session_get (wrk, sid);
2315 session_events = session->vep.ev.events;
2316 if (!(EPOLLIN & session->vep.ev.events))
2319 events[*num_ev].events |= EPOLLIN;
2320 session_evt_data = session->vep.ev.data.u64;
2322 case FIFO_EVENT_APP_TX:
2323 sid = e->fifo->client_session_index;
2324 session = vcl_session_get (wrk, sid);
2325 session_events = session->vep.ev.events;
2326 if (!(EPOLLOUT & session_events))
2329 events[*num_ev].events |= EPOLLOUT;
2330 session_evt_data = session->vep.ev.data.u64;
2332 case SESSION_IO_EVT_CT_TX:
2333 vcl_fifo_rx_evt_valid_or_break (e->fifo);
2334 session = vcl_ct_session_get_from_fifo (wrk, e->fifo, 0);
2335 sid = session->session_index;
2336 session_events = session->vep.ev.events;
2337 if (!(EPOLLIN & session->vep.ev.events))
2340 events[*num_ev].events |= EPOLLIN;
2341 session_evt_data = session->vep.ev.data.u64;
2343 case SESSION_IO_EVT_CT_RX:
2344 session = vcl_ct_session_get_from_fifo (wrk, e->fifo, 1);
2345 sid = session->session_index;
2346 session_events = session->vep.ev.events;
2347 if (!(EPOLLOUT & session_events))
2350 events[*num_ev].events |= EPOLLOUT;
2351 session_evt_data = session->vep.ev.data.u64;
2353 case SESSION_CTRL_EVT_ACCEPTED:
2354 accepted_msg = (session_accepted_msg_t *) e->data;
2355 handle = accepted_msg->listener_handle;
2356 session = vcl_session_table_lookup_listener (wrk, handle);
2359 clib_warning ("VCL<%d>: ERROR: couldn't find listen session:"
2360 "listener handle %llx", getpid (), handle);
2364 clib_fifo_add2 (session->accept_evts_fifo, vcl_msg);
2365 vcl_msg->accepted_msg = *accepted_msg;
2366 session_events = session->vep.ev.events;
2367 if (!(EPOLLIN & session_events))
2371 events[*num_ev].events |= EPOLLIN;
2372 session_evt_data = session->vep.ev.data.u64;
2374 case SESSION_CTRL_EVT_CONNECTED:
2375 connected_msg = (session_connected_msg_t *) e->data;
2376 vcl_session_connected_handler (wrk, connected_msg);
2377 /* Generate EPOLLOUT because there's no connected event */
2378 sid = vcl_session_index_from_vpp_handle (wrk, connected_msg->handle);
2379 session = vcl_session_get (wrk, sid);
2380 session_events = session->vep.ev.events;
2381 if (EPOLLOUT & session_events)
2384 events[*num_ev].events |= EPOLLOUT;
2385 session_evt_data = session->vep.ev.data.u64;
2388 case SESSION_CTRL_EVT_DISCONNECTED:
2389 disconnected_msg = (session_disconnected_msg_t *) e->data;
2390 sid = vcl_session_index_from_vpp_handle (wrk, disconnected_msg->handle);
2391 if (!(session = vcl_session_get (wrk, sid)))
2394 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
2395 session_evt_data = session->vep.ev.data.u64;
2396 session_events = session->vep.ev.events;
2398 case SESSION_CTRL_EVT_RESET:
2399 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
2400 if (!(session = vcl_session_get (wrk, sid)))
2403 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
2404 session_evt_data = session->vep.ev.data.u64;
2405 session_events = session->vep.ev.events;
2408 VDBG (0, "unhandled: %u", e->event_type);
2414 events[*num_ev].data.u64 = session_evt_data;
2415 if (EPOLLONESHOT & session_events)
2417 session = vcl_session_get (wrk, sid);
2418 session->vep.ev.events = 0;
2425 vcl_epoll_wait_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
2426 struct epoll_event *events, u32 maxevents,
2427 double wait_for_time, u32 * num_ev)
2429 svm_msg_q_msg_t *msg;
2433 svm_msg_q_lock (mq);
2434 if (svm_msg_q_is_empty (mq))
2438 svm_msg_q_unlock (mq);
2441 else if (wait_for_time < 0)
2443 svm_msg_q_wait (mq);
2447 if (svm_msg_q_timedwait (mq, wait_for_time / 1e3))
2449 svm_msg_q_unlock (mq);
2454 vcl_mq_dequeue_batch (wrk, mq);
2455 svm_msg_q_unlock (mq);
2457 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
2459 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
2460 e = svm_msg_q_msg_data (mq, msg);
2461 vcl_epoll_wait_handle_mq_event (wrk, e, events, num_ev);
2462 svm_msg_q_free_msg (mq, msg);
2463 if (*num_ev == maxevents)
2470 vec_delete (wrk->mq_msg_vector, i, 0);
2476 vppcom_epoll_wait_condvar (vcl_worker_t * wrk, struct epoll_event *events,
2477 int maxevents, u32 n_evts, double wait_for_time)
2479 vcl_cut_through_registration_t *cr;
2480 double total_wait = 0, wait_slice;
2483 wait_for_time = (wait_for_time == -1) ? (double) 10e9 : wait_for_time;
2484 wait_slice = wrk->cut_through_registrations ? 10e-6 : wait_for_time;
2488 vcl_ct_registration_lock (wrk);
2490 pool_foreach (cr, wrk->cut_through_registrations, ({
2491 vcl_epoll_wait_handle_mq (wrk, cr->mq, events, maxevents, 0, &n_evts);
2494 vcl_ct_registration_unlock (wrk);
2496 rv = vcl_epoll_wait_handle_mq (wrk, wrk->app_event_queue, events,
2497 maxevents, n_evts ? 0 : wait_slice,
2500 total_wait += wait_slice;
2504 while (total_wait < wait_for_time);
2509 vppcom_epoll_wait_eventfd (vcl_worker_t * wrk, struct epoll_event *events,
2510 int maxevents, u32 n_evts, double wait_for_time)
2512 vcl_mq_evt_conn_t *mqc;
2513 int __clib_unused n_read;
2517 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
2518 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
2519 vec_len (wrk->mq_events), wait_for_time);
2520 for (i = 0; i < n_mq_evts; i++)
2522 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
2523 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
2524 vcl_epoll_wait_handle_mq (wrk, mqc->mq, events, maxevents, 0, &n_evts);
2527 return (int) n_evts;
2531 vppcom_epoll_wait (uint32_t vep_handle, struct epoll_event *events,
2532 int maxevents, double wait_for_time)
2534 vcl_worker_t *wrk = vcl_worker_get_current ();
2535 vcl_session_t *vep_session;
2539 if (PREDICT_FALSE (maxevents <= 0))
2541 clib_warning ("VCL<%d>: ERROR: Invalid maxevents (%d)!",
2542 getpid (), maxevents);
2543 return VPPCOM_EINVAL;
2546 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
2548 return VPPCOM_EBADFD;
2550 if (PREDICT_FALSE (!vep_session->is_vep))
2552 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
2553 getpid (), vep_handle);
2554 return VPPCOM_EINVAL;
2557 memset (events, 0, sizeof (*events) * maxevents);
2559 if (vec_len (wrk->unhandled_evts_vector))
2561 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
2563 vcl_epoll_wait_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i],
2565 if (n_evts == maxevents)
2572 vec_delete (wrk->unhandled_evts_vector, i, 0);
2575 if (vcm->cfg.use_mq_eventfd)
2576 return vppcom_epoll_wait_eventfd (wrk, events, maxevents, n_evts,
2579 return vppcom_epoll_wait_condvar (wrk, events, maxevents, n_evts,
2584 vppcom_session_attr (uint32_t session_handle, uint32_t op,
2585 void *buffer, uint32_t * buflen)
2587 vcl_worker_t *wrk = vcl_worker_get_current ();
2588 vcl_session_t *session;
2590 u32 *flags = buffer;
2591 vppcom_endpt_t *ep = buffer;
2593 session = vcl_session_get_w_handle (wrk, session_handle);
2595 return VPPCOM_EBADFD;
2599 case VPPCOM_ATTR_GET_NREAD:
2600 rv = vppcom_session_read_ready (session);
2601 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_NREAD: sid %u, nread = %d",
2605 case VPPCOM_ATTR_GET_NWRITE:
2606 rv = vppcom_session_write_ready (session);
2607 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_NWRITE: sid %u, nwrite = %d",
2608 getpid (), session_handle, rv);
2611 case VPPCOM_ATTR_GET_FLAGS:
2612 if (PREDICT_TRUE (buffer && buflen && (*buflen >= sizeof (*flags))))
2614 *flags = O_RDWR | (VCL_SESS_ATTR_TEST (session->attr,
2615 VCL_SESS_ATTR_NONBLOCK));
2616 *buflen = sizeof (*flags);
2617 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_FLAGS: sid %u, flags = 0x%08x, "
2618 "is_nonblocking = %u", getpid (),
2619 session_handle, *flags,
2620 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK));
2626 case VPPCOM_ATTR_SET_FLAGS:
2627 if (PREDICT_TRUE (buffer && buflen && (*buflen == sizeof (*flags))))
2629 if (*flags & O_NONBLOCK)
2630 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK);
2632 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_NONBLOCK);
2634 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_FLAGS: sid %u, flags = 0x%08x,"
2635 " is_nonblocking = %u",
2636 getpid (), session_handle, *flags,
2637 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK));
2643 case VPPCOM_ATTR_GET_PEER_ADDR:
2644 if (PREDICT_TRUE (buffer && buflen &&
2645 (*buflen >= sizeof (*ep)) && ep->ip))
2647 ep->is_ip4 = session->transport.is_ip4;
2648 ep->port = session->transport.rmt_port;
2649 if (session->transport.is_ip4)
2650 clib_memcpy (ep->ip, &session->transport.rmt_ip.ip4,
2651 sizeof (ip4_address_t));
2653 clib_memcpy (ep->ip, &session->transport.rmt_ip.ip6,
2654 sizeof (ip6_address_t));
2655 *buflen = sizeof (*ep);
2656 VDBG (1, "VCL<%d>: VPPCOM_ATTR_GET_PEER_ADDR: sid %u, is_ip4 = %u, "
2657 "addr = %U, port %u", getpid (),
2658 session_handle, ep->is_ip4, format_ip46_address,
2659 &session->transport.rmt_ip,
2660 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
2661 clib_net_to_host_u16 (ep->port));
2667 case VPPCOM_ATTR_GET_LCL_ADDR:
2668 if (PREDICT_TRUE (buffer && buflen &&
2669 (*buflen >= sizeof (*ep)) && ep->ip))
2671 ep->is_ip4 = session->transport.is_ip4;
2672 ep->port = session->transport.lcl_port;
2673 if (session->transport.is_ip4)
2674 clib_memcpy (ep->ip, &session->transport.lcl_ip.ip4,
2675 sizeof (ip4_address_t));
2677 clib_memcpy (ep->ip, &session->transport.lcl_ip.ip6,
2678 sizeof (ip6_address_t));
2679 *buflen = sizeof (*ep);
2680 VDBG (1, "VCL<%d>: VPPCOM_ATTR_GET_LCL_ADDR: sid %u, is_ip4 = %u,"
2681 " addr = %U port %d", getpid (),
2682 session_handle, ep->is_ip4, format_ip46_address,
2683 &session->transport.lcl_ip,
2684 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
2685 clib_net_to_host_u16 (ep->port));
2691 case VPPCOM_ATTR_GET_LIBC_EPFD:
2692 rv = session->libc_epfd;
2693 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd %d",
2697 case VPPCOM_ATTR_SET_LIBC_EPFD:
2698 if (PREDICT_TRUE (buffer && buflen &&
2699 (*buflen == sizeof (session->libc_epfd))))
2701 session->libc_epfd = *(int *) buffer;
2702 *buflen = sizeof (session->libc_epfd);
2704 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd %d, "
2705 "buflen %d", getpid (), session->libc_epfd, *buflen);
2711 case VPPCOM_ATTR_GET_PROTOCOL:
2712 if (buffer && buflen && (*buflen >= sizeof (int)))
2714 *(int *) buffer = session->session_type;
2715 *buflen = sizeof (int);
2717 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_PROTOCOL: %d (%s), buflen %d",
2718 getpid (), *(int *) buffer, *(int *) buffer ? "UDP" : "TCP",
2725 case VPPCOM_ATTR_GET_LISTEN:
2726 if (buffer && buflen && (*buflen >= sizeof (int)))
2728 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2729 VCL_SESS_ATTR_LISTEN);
2730 *buflen = sizeof (int);
2732 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_LISTEN: %d, buflen %d",
2733 getpid (), *(int *) buffer, *buflen);
2739 case VPPCOM_ATTR_GET_ERROR:
2740 if (buffer && buflen && (*buflen >= sizeof (int)))
2742 *(int *) buffer = 0;
2743 *buflen = sizeof (int);
2745 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_ERROR: %d, buflen %d, #VPP-TBD#",
2746 getpid (), *(int *) buffer, *buflen);
2752 case VPPCOM_ATTR_GET_TX_FIFO_LEN:
2753 if (buffer && buflen && (*buflen >= sizeof (u32)))
2757 *(size_t *) buffer = (session->sndbuf_size ? session->sndbuf_size :
2758 session->tx_fifo ? session->tx_fifo->nitems :
2759 vcm->cfg.tx_fifo_size);
2760 *buflen = sizeof (u32);
2762 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TX_FIFO_LEN: %u (0x%x), "
2763 "buflen %d, #VPP-TBD#", getpid (),
2764 *(size_t *) buffer, *(size_t *) buffer, *buflen);
2770 case VPPCOM_ATTR_SET_TX_FIFO_LEN:
2771 if (buffer && buflen && (*buflen == sizeof (u32)))
2774 session->sndbuf_size = *(u32 *) buffer;
2775 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TX_FIFO_LEN: %u (0x%x), "
2776 "buflen %d, #VPP-TBD#", getpid (),
2777 session->sndbuf_size, session->sndbuf_size, *buflen);
2783 case VPPCOM_ATTR_GET_RX_FIFO_LEN:
2784 if (buffer && buflen && (*buflen >= sizeof (u32)))
2788 *(size_t *) buffer = (session->rcvbuf_size ? session->rcvbuf_size :
2789 session->rx_fifo ? session->rx_fifo->nitems :
2790 vcm->cfg.rx_fifo_size);
2791 *buflen = sizeof (u32);
2793 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_RX_FIFO_LEN: %u (0x%x), "
2794 "buflen %d, #VPP-TBD#", getpid (),
2795 *(size_t *) buffer, *(size_t *) buffer, *buflen);
2801 case VPPCOM_ATTR_SET_RX_FIFO_LEN:
2802 if (buffer && buflen && (*buflen == sizeof (u32)))
2805 session->rcvbuf_size = *(u32 *) buffer;
2806 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_RX_FIFO_LEN: %u (0x%x), "
2807 "buflen %d, #VPP-TBD#", getpid (),
2808 session->sndbuf_size, session->sndbuf_size, *buflen);
2814 case VPPCOM_ATTR_GET_REUSEADDR:
2815 if (buffer && buflen && (*buflen >= sizeof (int)))
2818 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2819 VCL_SESS_ATTR_REUSEADDR);
2820 *buflen = sizeof (int);
2822 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_REUSEADDR: %d, "
2823 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
2829 case VPPCOM_ATTR_SET_REUSEADDR:
2830 if (buffer && buflen && (*buflen == sizeof (int)) &&
2831 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
2834 if (*(int *) buffer)
2835 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEADDR);
2837 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEADDR);
2839 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_REUSEADDR: %d, buflen %d,"
2840 " #VPP-TBD#", getpid (),
2841 VCL_SESS_ATTR_TEST (session->attr,
2842 VCL_SESS_ATTR_REUSEADDR), *buflen);
2848 case VPPCOM_ATTR_GET_REUSEPORT:
2849 if (buffer && buflen && (*buflen >= sizeof (int)))
2852 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2853 VCL_SESS_ATTR_REUSEPORT);
2854 *buflen = sizeof (int);
2856 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_REUSEPORT: %d, buflen %d,"
2857 " #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
2863 case VPPCOM_ATTR_SET_REUSEPORT:
2864 if (buffer && buflen && (*buflen == sizeof (int)) &&
2865 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
2868 if (*(int *) buffer)
2869 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEPORT);
2871 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEPORT);
2873 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_REUSEPORT: %d, buflen %d,"
2874 " #VPP-TBD#", getpid (),
2875 VCL_SESS_ATTR_TEST (session->attr,
2876 VCL_SESS_ATTR_REUSEPORT), *buflen);
2882 case VPPCOM_ATTR_GET_BROADCAST:
2883 if (buffer && buflen && (*buflen >= sizeof (int)))
2886 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2887 VCL_SESS_ATTR_BROADCAST);
2888 *buflen = sizeof (int);
2890 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_BROADCAST: %d, buflen %d,"
2891 " #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
2897 case VPPCOM_ATTR_SET_BROADCAST:
2898 if (buffer && buflen && (*buflen == sizeof (int)))
2901 if (*(int *) buffer)
2902 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_BROADCAST);
2904 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_BROADCAST);
2906 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_BROADCAST: %d, buflen %d, "
2907 "#VPP-TBD#", getpid (),
2908 VCL_SESS_ATTR_TEST (session->attr,
2909 VCL_SESS_ATTR_BROADCAST), *buflen);
2915 case VPPCOM_ATTR_GET_V6ONLY:
2916 if (buffer && buflen && (*buflen >= sizeof (int)))
2919 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2920 VCL_SESS_ATTR_V6ONLY);
2921 *buflen = sizeof (int);
2923 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_V6ONLY: %d, buflen %d, "
2924 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
2930 case VPPCOM_ATTR_SET_V6ONLY:
2931 if (buffer && buflen && (*buflen == sizeof (int)))
2934 if (*(int *) buffer)
2935 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_V6ONLY);
2937 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_V6ONLY);
2939 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_V6ONLY: %d, buflen %d, "
2940 "#VPP-TBD#", getpid (),
2941 VCL_SESS_ATTR_TEST (session->attr,
2942 VCL_SESS_ATTR_V6ONLY), *buflen);
2948 case VPPCOM_ATTR_GET_KEEPALIVE:
2949 if (buffer && buflen && (*buflen >= sizeof (int)))
2952 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2953 VCL_SESS_ATTR_KEEPALIVE);
2954 *buflen = sizeof (int);
2956 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_KEEPALIVE: %d, buflen %d, "
2957 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
2963 case VPPCOM_ATTR_SET_KEEPALIVE:
2964 if (buffer && buflen && (*buflen == sizeof (int)))
2967 if (*(int *) buffer)
2968 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_KEEPALIVE);
2970 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_KEEPALIVE);
2972 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_KEEPALIVE: %d, buflen %d, "
2973 "#VPP-TBD#", getpid (),
2974 VCL_SESS_ATTR_TEST (session->attr,
2975 VCL_SESS_ATTR_KEEPALIVE), *buflen);
2981 case VPPCOM_ATTR_GET_TCP_NODELAY:
2982 if (buffer && buflen && (*buflen >= sizeof (int)))
2985 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2986 VCL_SESS_ATTR_TCP_NODELAY);
2987 *buflen = sizeof (int);
2989 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_NODELAY: %d, buflen %d, "
2990 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
2996 case VPPCOM_ATTR_SET_TCP_NODELAY:
2997 if (buffer && buflen && (*buflen == sizeof (int)))
3000 if (*(int *) buffer)
3001 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
3003 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
3005 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_NODELAY: %d, buflen %d, "
3006 "#VPP-TBD#", getpid (),
3007 VCL_SESS_ATTR_TEST (session->attr,
3008 VCL_SESS_ATTR_TCP_NODELAY), *buflen);
3014 case VPPCOM_ATTR_GET_TCP_KEEPIDLE:
3015 if (buffer && buflen && (*buflen >= sizeof (int)))
3018 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3019 VCL_SESS_ATTR_TCP_KEEPIDLE);
3020 *buflen = sizeof (int);
3022 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d, buflen %d, "
3023 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
3029 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
3030 if (buffer && buflen && (*buflen == sizeof (int)))
3033 if (*(int *) buffer)
3034 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
3036 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
3038 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d, buflen %d, "
3039 "#VPP-TBD#", getpid (),
3040 VCL_SESS_ATTR_TEST (session->attr,
3041 VCL_SESS_ATTR_TCP_KEEPIDLE), *buflen);
3047 case VPPCOM_ATTR_GET_TCP_KEEPINTVL:
3048 if (buffer && buflen && (*buflen >= sizeof (int)))
3051 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3052 VCL_SESS_ATTR_TCP_KEEPINTVL);
3053 *buflen = sizeof (int);
3055 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_KEEPINTVL: %d, buflen %d, "
3056 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
3062 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
3063 if (buffer && buflen && (*buflen == sizeof (int)))
3066 if (*(int *) buffer)
3067 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
3069 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
3071 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d, buflen %d, "
3072 "#VPP-TBD#", getpid (),
3073 VCL_SESS_ATTR_TEST (session->attr,
3074 VCL_SESS_ATTR_TCP_KEEPINTVL), *buflen);
3080 case VPPCOM_ATTR_GET_TCP_USER_MSS:
3081 if (buffer && buflen && (*buflen >= sizeof (u32)))
3084 *(u32 *) buffer = session->user_mss;
3085 *buflen = sizeof (int);
3087 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_USER_MSS: %d, buflen %d,"
3088 " #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
3094 case VPPCOM_ATTR_SET_TCP_USER_MSS:
3095 if (buffer && buflen && (*buflen == sizeof (u32)))
3098 session->user_mss = *(u32 *) buffer;
3100 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_USER_MSS: %u, buflen %d, "
3101 "#VPP-TBD#", getpid (), session->user_mss, *buflen);
3116 vppcom_session_recvfrom (uint32_t session_handle, void *buffer,
3117 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3119 vcl_worker_t *wrk = vcl_worker_get_current ();
3121 vcl_session_t *session = 0;
3125 session = vcl_session_get_w_handle (wrk, session_handle);
3126 if (PREDICT_FALSE (!session))
3128 VDBG (0, "VCL<%d>: invalid session, sid (%u) has been closed!",
3129 getpid (), session_handle);
3130 return VPPCOM_EBADFD;
3132 ep->is_ip4 = session->transport.is_ip4;
3133 ep->port = session->transport.rmt_port;
3137 rv = vppcom_session_read (session_handle, buffer, buflen);
3138 else if (flags & MSG_PEEK)
3139 rv = vppcom_session_peek (session_handle, buffer, buflen);
3142 clib_warning ("VCL<%d>: Unsupport flags for recvfrom %d",
3144 return VPPCOM_EAFNOSUPPORT;
3149 if (session->transport.is_ip4)
3150 clib_memcpy (ep->ip, &session->transport.rmt_ip.ip4,
3151 sizeof (ip4_address_t));
3153 clib_memcpy (ep->ip, &session->transport.rmt_ip.ip6,
3154 sizeof (ip6_address_t));
3161 vppcom_session_sendto (uint32_t session_handle, void *buffer,
3162 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3165 return VPPCOM_EINVAL;
3170 return VPPCOM_EINVAL;
3175 // TBD check the flags and do the right thing
3176 VDBG (2, "VCL<%d>: handling flags 0x%u (%d) not implemented yet.",
3177 getpid (), flags, flags);
3180 return (vppcom_session_write (session_handle, buffer, buflen));
3184 vppcom_poll (vcl_poll_t * vp, uint32_t n_sids, double wait_for_time)
3186 vcl_worker_t *wrk = vcl_worker_get_current ();
3187 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
3188 u32 i, keep_trying = 1;
3191 VDBG (3, "VCL<%d>: vp %p, nsids %u, wait_for_time %f",
3192 getpid (), vp, n_sids, wait_for_time);
3195 return VPPCOM_EFAULT;
3199 vcl_session_t *session;
3201 for (i = 0; i < n_sids; i++)
3203 ASSERT (vp[i].revents);
3205 session = vcl_session_get (wrk, vp[i].sid);
3212 if (POLLIN & vp[i].events)
3214 rv = vppcom_session_read_ready (session);
3217 *vp[i].revents |= POLLIN;
3224 case VPPCOM_ECONNRESET:
3225 *vp[i].revents = POLLHUP;
3229 *vp[i].revents = POLLERR;
3236 if (POLLOUT & vp[i].events)
3238 rv = vppcom_session_write_ready (session);
3241 *vp[i].revents |= POLLOUT;
3248 case VPPCOM_ECONNRESET:
3249 *vp[i].revents = POLLHUP;
3253 *vp[i].revents = POLLERR;
3260 if (0) // Note "done:" label used by VCL_SESSION_LOCK_AND_GET()
3262 *vp[i].revents = POLLNVAL;
3266 if (wait_for_time != -1)
3267 keep_trying = (clib_time_now (&wrk->clib_time) <= timeout) ? 1 : 0;
3269 while ((num_ev == 0) && keep_trying);
3271 if (VPPCOM_DEBUG > 3)
3273 clib_warning ("VCL<%d>: returning %d", getpid (), num_ev);
3274 for (i = 0; i < n_sids; i++)
3276 clib_warning ("VCL<%d>: vp[%d].sid %d (0x%x), .events 0x%x, "
3277 ".revents 0x%x", getpid (), i, vp[i].sid, vp[i].sid,
3278 vp[i].events, *vp[i].revents);
3285 vppcom_mq_epoll_fd (void)
3287 vcl_worker_t *wrk = vcl_worker_get_current ();
3288 return wrk->mqs_epfd;
3292 vppcom_session_index (uint32_t session_handle)
3294 return session_handle & 0xFFFFFF;
3298 vppcom_worker_register (void)
3300 if (vcl_worker_alloc_and_init ())
3302 return VPPCOM_EEXIST;
3306 * fd.io coding-style-patch-verification: ON
3309 * eval: (c-set-style "gnu")