2 * Copyright (c) 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.
16 #include <vppinfra/fifo.h>
17 #include <vppinfra/pool.h>
18 #include <vppinfra/hash.h>
19 #include <vnet/api_errno.h>
21 #include <vcl/vppcom.h>
22 #include <vcl/vcl_event.h>
23 #include <vcl/vcl_private.h>
27 * @brief VPP Communications Library (VCL) event handler.
29 * Definitions for generic event handling in VCL.
33 vce_generate_event (vce_event_thread_t * evt, u32 ev_idx)
38 pthread_mutex_lock (&(evt->generator_lock));
40 /* Check there is event data for this event */
43 p = pool_elt_at_index (evt->vce_events, ev_idx);
46 elts = (int) clib_fifo_free_elts (evt->event_index_fifo);
47 if (PREDICT_TRUE (elts))
49 /* Add event to queue */
50 clib_fifo_add1 (evt->event_index_fifo, ev_idx);
51 pthread_cond_signal (&(evt->generator_cond));
55 rv = VNET_API_ERROR_QUEUE_FULL;
59 pthread_mutex_unlock (&(evt->generator_lock));
65 vce_clear_event (vce_event_thread_t * evt, u32 ev_idx)
68 pool_put_index (evt->vce_events, ev_idx);
73 vce_get_event_from_index (vce_event_thread_t * evt, u32 ev_idx)
76 /* Assumes caller has obtained the spinlock (evt->events_lockp) */
78 if (!pool_is_free_index (evt->vce_events, ev_idx))
79 ev = pool_elt_at_index (evt->vce_events, ev_idx);
84 vce_event_handler_reg_t *
85 vce_get_event_handler (vce_event_thread_t * evt, vce_event_key_t * evk)
87 vce_event_handler_reg_t *handler = 0;
91 p = hash_get (evt->handlers_index_by_event_key, evk->as_u64);
93 handler = pool_elt_at_index (evt->vce_event_handlers, p[0]);
94 VCE_HANDLERS_UNLOCK ();
99 vce_event_handler_reg_t *
100 vce_register_handler (vce_event_thread_t * evt, vce_event_key_t * evk,
101 vce_event_callback_t cb, void *cb_args)
103 vce_event_handler_reg_t *handler;
104 vce_event_handler_reg_t *old_handler = 0;
108 /* TODO - multiple handler support. For now we can replace
109 * and re-instate, which is useful for event recycling */
111 VCE_HANDLERS_LOCK ();
113 p = hash_get (evt->handlers_index_by_event_key, evk->as_u64);
116 old_handler = pool_elt_at_index (evt->vce_event_handlers, p[0]);
117 /* If we are just re-registering, ignore and move on
118 * else store the old handler_fn for unregister to re-instate */
119 if (old_handler->handler_fn == cb)
122 VCE_HANDLERS_UNLOCK ();
124 /* Signal event thread that a handler exists in case any
125 * recycled events requiring this handler are pending */
126 pthread_mutex_lock (&(evt->generator_lock));
127 pthread_cond_signal (&(evt->generator_cond));
128 pthread_mutex_unlock (&(evt->generator_lock));
133 pool_get (evt->vce_event_handlers, handler);
134 handler_index = (u32) (handler - evt->vce_event_handlers);
136 handler->handler_fn = cb;
137 handler->replaced_handler_idx = (u32) ((p) ? p[0] : ~0);
138 handler->ev_idx = (u32) ~ 0; //This will be set by the event thread if event happens
139 handler->evk = evk->as_u64;
140 handler->handler_fn_args = cb_args;
142 hash_set (evt->handlers_index_by_event_key, evk->as_u64, handler_index);
144 pthread_cond_init (&(handler->handler_cond), NULL);
145 pthread_mutex_init (&(handler->handler_lock), NULL);
147 VCE_HANDLERS_UNLOCK ();
149 /* Signal event thread that a new handler exists in case any
150 * recycled events requiring this handler are pending */
151 pthread_mutex_lock (&(evt->generator_lock));
152 pthread_cond_signal (&(evt->generator_cond));
153 pthread_mutex_unlock (&(evt->generator_lock));
159 vce_unregister_handler (vce_event_thread_t * evt,
160 vce_event_handler_reg_t * handler)
163 u64 evk = handler->evk;
164 u8 generate_signal = 0;
166 VCE_HANDLERS_LOCK ();
168 p = hash_get (evt->handlers_index_by_event_key, evk);
171 VCE_HANDLERS_UNLOCK ();
172 return VNET_API_ERROR_NO_SUCH_ENTRY;
175 handler = pool_elt_at_index (evt->vce_event_handlers, p[0]);
177 /* If this handler replaced another handler, re-instate it */
178 if (handler->replaced_handler_idx != ~0)
180 hash_set (evt->handlers_index_by_event_key, evk,
181 handler->replaced_handler_idx);
186 hash_unset (evt->handlers_index_by_event_key, evk);
189 pthread_mutex_destroy (&(handler->handler_lock));
190 pthread_cond_destroy (&(handler->handler_cond));
191 pool_put (evt->vce_event_handlers, handler);
193 VCE_HANDLERS_UNLOCK ();
197 /* Signal event thread that a new handler exists in case any
198 * recycled events requiring this handler are pending */
199 pthread_mutex_lock (&(evt->generator_lock));
200 pthread_cond_signal (&(evt->generator_cond));
201 pthread_mutex_unlock (&(evt->generator_lock));
208 vce_event_thread_fn (void *arg)
210 vce_event_thread_t *evt = (vce_event_thread_t *) arg;
213 vce_event_handler_reg_t *handler;
215 u32 recycle_count = 0;
217 pthread_mutex_lock (&(evt->generator_lock));
220 uword fifo_depth = clib_fifo_elts (evt->event_index_fifo);
221 while ((fifo_depth == 0) || (recycle_count == fifo_depth))
224 pthread_cond_wait (&(evt->generator_cond), &(evt->generator_lock));
225 fifo_depth = clib_fifo_elts (evt->event_index_fifo);
230 clib_fifo_sub1 (evt->event_index_fifo, ev_idx);
231 ev = vce_get_event_from_index (evt, ev_idx);
233 if (recycle_count && ev->recycle)
235 clib_fifo_add1 (evt->event_index_fifo, ev_idx);
236 VCE_EVENTS_UNLOCK ();
239 VCE_HANDLERS_LOCK ();
241 p = hash_get (evt->handlers_index_by_event_key, ev->evk.as_u64);
244 /* If an event falls in the woods, and there is no handler to hear it,
245 * does it make any sound?
246 * I don't know either, so lets biff the event */
247 pool_put (evt->vce_events, ev);
248 VCE_EVENTS_UNLOCK ();
249 VCE_HANDLERS_UNLOCK ();
250 pthread_mutex_unlock (&(evt->generator_lock));
254 u32 evt_recycle = ev->recycle;
255 handler = pool_elt_at_index (evt->vce_event_handlers, p[0]);
256 handler->ev_idx = ev_idx;
259 VCE_EVENTS_UNLOCK ();
260 VCE_HANDLERS_UNLOCK ();
261 pthread_mutex_unlock (&(evt->generator_lock));
263 (handler->handler_fn) (handler);
266 ev = vce_get_event_from_index (evt, ev_idx);
267 recycle_count += (!evt_recycle && ev && ev->recycle) ? 1 : 0;
268 VCE_EVENTS_UNLOCK ();
271 pthread_mutex_lock (&(evt->generator_lock));
277 vce_start_event_thread (vce_event_thread_t * evt, u8 max_events)
279 clib_fifo_validate (evt->event_index_fifo, max_events);
280 evt->handlers_index_by_event_key = hash_create (0, sizeof (uword));
282 pthread_cond_init (&(evt->generator_cond), NULL);
283 pthread_mutex_init (&(evt->generator_lock), NULL);
285 clib_spinlock_init (&(evt->events_lockp));
286 clib_spinlock_init (&(evt->handlers_lockp));
288 return pthread_create (&(evt->thread), NULL /* attr */ ,
289 vce_event_thread_fn, evt);
293 vppcom_session_io_thread_fn (void *arg)
295 vppcom_session_io_thread_t *evt = (vppcom_session_io_thread_t *) arg;
296 u32 *session_indexes = 0, *session_index;
299 vcl_session_t *session;
303 vec_reset_length (session_indexes);
304 VCE_IO_SESSIONS_LOCK ();
306 pool_foreach (session_index, evt->active_session_indexes, ({
307 vec_add1 (session_indexes, *session_index);
310 VCE_IO_SESSIONS_UNLOCK ();
313 for (i = 0; i < vec_len (session_indexes); ++i)
315 VCL_SESSION_LOCK_AND_GET (session_indexes[i], &session);
316 bytes = svm_fifo_max_dequeue (session->rx_fifo);
317 VCL_SESSION_UNLOCK ();
321 vppcom_ioevent_t *eio;
327 pool_get (vcm->event_thread.vce_events, ev);
328 ev_idx = (u32) (ev - vcm->event_thread.vce_events);
329 eio = vce_get_event_data (ev, sizeof (*eio));
330 ev->evk.eid = VCL_EVENT_IOEVENT_RX_FIFO;
331 ev->evk.session_index = session_indexes[i];
333 eio->session_index = session_indexes[i];
335 VCL_EVENTS_UNLOCK ();
337 rv = vce_generate_event (&vcm->event_thread, ev_idx);
343 ts.tv_nsec = 1000000; /* 1 millisecond */
344 nanosleep (&ts, NULL);
347 VCL_SESSION_UNLOCK ();
352 vppcom_start_io_event_thread (vppcom_session_io_thread_t * evt,
355 pthread_cond_init (&(evt->vce_io_cond), NULL);
356 pthread_mutex_init (&(evt->vce_io_lock), NULL);
358 clib_spinlock_init (&(evt->io_sessions_lockp));
360 return pthread_create (&(evt->thread), NULL /* attr */ ,
361 vppcom_session_io_thread_fn, evt);
365 vce_registered_ioevent_handler_fn (void *arg)
367 vce_event_handler_reg_t *reg = (vce_event_handler_reg_t *) arg;
368 vppcom_ioevent_t *eio;
370 u32 ioevt_ndx = (u64) (reg->handler_fn_args);
371 vppcom_session_ioevent_t *ioevent, ioevent_;
374 ev = vce_get_event_from_index (&vcm->event_thread, reg->ev_idx);
375 eio = vce_get_event_data (ev, sizeof (*eio));
376 VCL_EVENTS_UNLOCK ();
378 VCL_IO_SESSIONS_LOCK ();
379 ioevent = pool_elt_at_index (vcm->session_io_thread.ioevents, ioevt_ndx);
381 VCL_IO_SESSIONS_UNLOCK ();
382 (ioevent_.user_cb) (eio, ioevent_.user_cb_data);
383 vce_clear_event (&vcm->event_thread, reg->ev_idx);
386 /*TODO - Unregister check in close for this listener */
391 vce_registered_listener_connect_handler_fn (void *arg)
393 vce_event_handler_reg_t *reg = (vce_event_handler_reg_t *) arg;
394 vce_event_connect_request_t *ecr;
398 vcl_session_t *new_session;
401 vppcom_session_listener_t *session_listener =
402 (vppcom_session_listener_t *) reg->handler_fn_args;
405 ev = vce_get_event_from_index (&vcm->event_thread, reg->ev_idx);
406 ecr = vce_get_event_data (ev, sizeof (*ecr));
407 VCL_EVENTS_UNLOCK ();
408 VCL_SESSION_LOCK_AND_GET (ecr->accepted_session_index, &new_session);
410 ep.is_ip4 = new_session->transport.is_ip4;
411 ep.port = new_session->transport.rmt_port;
412 if (new_session->transport.is_ip4)
413 clib_memcpy (&ep.ip, &new_session->transport.rmt_ip.ip4,
414 sizeof (ip4_address_t));
416 clib_memcpy (&ep.ip, &new_session->transport.rmt_ip.ip6,
417 sizeof (ip6_address_t));
419 vppcom_send_accept_session_reply (new_session->vpp_handle,
420 new_session->client_context,
422 VCL_SESSION_UNLOCK ();
424 (session_listener->user_cb) (ecr->accepted_session_index, &ep,
425 session_listener->user_cb_data);
427 if (vcm->session_io_thread.io_sessions_lockp)
429 /* Throw this new accepted session index into the rx poll thread pool */
430 VCL_IO_SESSIONS_LOCK ();
431 u32 *active_session_index;
432 pool_get (vcm->session_io_thread.active_session_indexes,
433 active_session_index);
434 *active_session_index = ecr->accepted_session_index;
435 VCL_IO_SESSIONS_UNLOCK ();
438 /*TODO - Unregister check in close for this listener */
442 ASSERT (0); // If we can't get a lock or accepted session fails, lets blow up.
446 * @brief vce_poll_wait_connect_request_handler_fn
447 * - used by vppcom_epoll_xxxx() for listener sessions
448 * - when a vl_api_accept_session_t_handler() generates an event
449 * this callback is alerted and sets the fields that vppcom_epoll_wait()
452 * @param arg - void* to be cast to vce_event_handler_reg_t*
455 vce_poll_wait_connect_request_handler_fn (void *arg)
457 vce_event_handler_reg_t *reg = (vce_event_handler_reg_t *) arg;
459 /* Retrieve the VCL_EVENT_CONNECT_REQ_ACCEPTED event */
460 ev = vce_get_event_from_index (&vcm->event_thread, reg->ev_idx);
461 vce_event_connect_request_t *ecr = vce_get_event_data (ev, sizeof (*ecr));
463 /* Add the accepted_session_index to the FIFO */
464 VCL_ACCEPT_FIFO_LOCK ();
465 clib_fifo_add1 (vcm->client_session_index_fifo,
466 ecr->accepted_session_index);
467 VCL_ACCEPT_FIFO_UNLOCK ();
469 /* Recycling the event. */
472 clib_fifo_add1 (vcm->event_thread.event_index_fifo, reg->ev_idx);
473 VCL_EVENTS_UNLOCK ();
477 vppcom_session_register_ioevent_cb (uint32_t session_index,
478 vppcom_session_ioevent_cb cb,
479 uint8_t rx, void *ptr)
483 vppcom_session_ioevent_t *ioevent;
485 if (!vcm->session_io_thread.io_sessions_lockp)
486 rv = vppcom_start_io_event_thread (&vcm->session_io_thread, 100); /* DAW_TODO: ??? hard-coded value */
492 /* Register handler for ioevent on session_index */
493 VCL_IO_SESSIONS_LOCK ();
494 pool_get (vcm->session_io_thread.ioevents, ioevent);
495 io_evt_ndx = (void *) (ioevent - vcm->session_io_thread.ioevents);
496 ioevent->user_cb = cb;
497 ioevent->user_cb_data = ptr;
498 VCL_IO_SESSIONS_UNLOCK ();
500 evk.session_index = session_index;
501 evk.eid = rx ? VCL_EVENT_IOEVENT_RX_FIFO : VCL_EVENT_IOEVENT_TX_FIFO;
503 (void) vce_register_handler (&vcm->event_thread, &evk,
504 vce_registered_ioevent_handler_fn,
511 vppcom_session_register_listener (uint32_t session_index,
512 vppcom_session_listener_cb cb,
513 vppcom_session_listener_errcb
514 errcb, uint8_t flags, int q_len, void *ptr)
518 vppcom_session_listener_t *listener_args;
520 if (!vcm->session_io_thread.io_sessions_lockp)
521 rv = vppcom_start_io_event_thread (&vcm->session_io_thread, 100); /* DAW_TODO: ??? hard-coded value */
526 rv = vppcom_session_listen (session_index, q_len);
532 /* Register handler for connect_request event on listen_session_index */
533 listener_args = clib_mem_alloc (sizeof (vppcom_session_listener_t)); // DAW_TODO: Use a pool instead of thrashing the memory allocator!
534 listener_args->user_cb = cb;
535 listener_args->user_cb_data = ptr;
536 listener_args->user_errcb = errcb;
538 evk.session_index = session_index;
539 evk.eid = VCL_EVENT_CONNECT_REQ_ACCEPTED;
540 (void) vce_register_handler (&vcm->event_thread, &evk,
541 vce_registered_listener_connect_handler_fn,
549 * fd.io coding-style-patch-verification: ON
552 * eval: (c-set-style "gnu")