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/vcl_event.h>
24 * @brief VPP Communications Library (VCL) event handler.
26 * Definitions for generic event handling in VCL.
30 vce_generate_event (vce_event_thread_t *evt, u32 ev_idx)
35 pthread_mutex_lock (&(evt->generator_lock));
37 /* Check there is event data for this event */
39 clib_spinlock_lock (&(evt->events_lockp));
40 p = pool_elt_at_index (evt->vce_events, ev_idx);
43 elts = (int) clib_fifo_free_elts (evt->event_index_fifo);
44 if (PREDICT_TRUE (elts))
46 /* Add event to queue */
47 clib_fifo_add1 (evt->event_index_fifo, ev_idx);
48 pthread_cond_signal (&(evt->generator_cond));
52 rv = VNET_API_ERROR_QUEUE_FULL;
55 clib_spinlock_unlock (&(evt->events_lockp));
56 pthread_mutex_unlock (&(evt->generator_lock));
62 vce_clear_event (vce_event_thread_t *evt, vce_event_t *ev)
64 clib_spinlock_lock (&(evt->events_lockp));
65 pool_put (evt->vce_events, ev);
66 clib_spinlock_unlock (&(evt->events_lockp));
70 vce_get_event_from_index(vce_event_thread_t *evt, u32 ev_idx)
74 clib_spinlock_lock (&(evt->events_lockp));
75 if ( ! pool_is_free_index (evt->vce_events, ev_idx))
76 ev = pool_elt_at_index (evt->vce_events, ev_idx);
77 clib_spinlock_unlock (&(evt->events_lockp));
82 vce_event_handler_reg_t *
83 vce_register_handler (vce_event_thread_t *evt, vce_event_key_t *evk,
84 vce_event_callback_t cb)
86 vce_event_handler_reg_t *handler;
87 vce_event_handler_reg_t *old_handler = 0;
91 /* TODO - multiple handler support. For now we can replace
92 * and re-instate, which is useful for event recycling */
94 clib_spinlock_lock (&evt->handlers_lockp);
96 p = hash_get (evt->handlers_index_by_event_key, evk->as_u64);
99 old_handler = pool_elt_at_index (evt->vce_event_handlers, p[0]);
100 /* If we are just re-registering, ignore and move on
101 * else store the old handler_fn for unregister to re-instate */
102 if (old_handler->handler_fn == cb)
105 clib_spinlock_unlock (&evt->handlers_lockp);
107 /* Signal event thread that a handler exists in case any
108 * recycled events requiring this handler are pending */
109 pthread_mutex_lock (&(evt->generator_lock));
110 pthread_cond_signal (&(evt->generator_cond));
111 pthread_mutex_unlock (&(evt->generator_lock));
116 pool_get (evt->vce_event_handlers, handler);
117 handler_index = (u32) (handler - evt->vce_event_handlers);
119 handler->handler_fn = cb;
120 handler->replaced_handler_idx = (p) ? p[0] : ~0;
121 handler->ev_idx = ~0; //This will be set by the event thread if event happens
122 handler->evk = evk->as_u64;
124 hash_set (evt->handlers_index_by_event_key, evk->as_u64, handler_index);
126 pthread_cond_init (&(handler->handler_cond), NULL);
127 pthread_mutex_init (&(handler->handler_lock), NULL);
129 clib_spinlock_unlock (&evt->handlers_lockp);
131 /* Signal event thread that a new handler exists in case any
132 * recycled events requiring this handler are pending */
133 pthread_mutex_lock (&(evt->generator_lock));
134 pthread_cond_signal (&(evt->generator_cond));
135 pthread_mutex_unlock (&(evt->generator_lock));
141 vce_unregister_handler (vce_event_thread_t *evt,
142 vce_event_handler_reg_t *handler)
145 u64 evk = handler->evk;
146 u8 generate_signal = 0;
148 clib_spinlock_lock (&evt->handlers_lockp);
150 p = hash_get (evt->handlers_index_by_event_key, evk);
153 clib_spinlock_unlock (&evt->handlers_lockp);
154 return VNET_API_ERROR_NO_SUCH_ENTRY;
157 handler = pool_elt_at_index (evt->vce_event_handlers, p[0]);
159 /* If this handler replaced another handler, re-instate it */
160 if (handler->replaced_handler_idx != ~0)
162 hash_set (evt->handlers_index_by_event_key, evk,
163 handler->replaced_handler_idx);
168 hash_unset (evt->handlers_index_by_event_key, evk);
171 pthread_mutex_destroy (&(handler->handler_lock));
172 pthread_cond_destroy (&(handler->handler_cond));
173 pool_put (evt->vce_event_handlers, handler);
175 clib_spinlock_unlock (&evt->handlers_lockp);
179 /* Signal event thread that a new handler exists in case any
180 * recycled events requiring this handler are pending */
181 pthread_mutex_lock (&(evt->generator_lock));
182 pthread_cond_signal (&(evt->generator_cond));
183 pthread_mutex_unlock (&(evt->generator_lock));
190 vce_event_thread_fn (void *arg)
192 vce_event_thread_t *evt = (vce_event_thread_t *) arg;
195 vce_event_handler_reg_t *handler;
198 evt->recycle_event = 1; // Used for recycling events with no handlers
203 pthread_mutex_lock (&(evt->generator_lock));
204 while ( (clib_fifo_elts (evt->event_index_fifo) == 0) ||
207 evt->recycle_event = 0;
208 pthread_cond_wait (&(evt->generator_cond),
209 &(evt->generator_lock));
213 clib_spinlock_lock (&(evt->events_lockp));
215 clib_fifo_sub1 (evt->event_index_fifo, ev_idx);
216 ev = pool_elt_at_index (evt->vce_events, ev_idx);
218 clib_spinlock_unlock (&(evt->events_lockp));
222 clib_spinlock_lock (&evt->handlers_lockp);
224 p = hash_get (evt->handlers_index_by_event_key, ev->evk.as_u64);
227 /* If an event falls in the woods, and there is no handler to hear it,
228 * does it make any sound?
229 * I don't know either, so lets try recycling the event */
230 clib_fifo_add1 (evt->event_index_fifo, ev_idx);
231 evt->recycle_event = 1;
232 clib_spinlock_unlock (&evt->handlers_lockp);
235 handler = pool_elt_at_index (evt->vce_event_handlers, p[0]);
236 handler->ev_idx = ev_idx;
238 clib_spinlock_unlock (&evt->handlers_lockp);
240 (handler->handler_fn)(handler);
243 pthread_mutex_unlock (&(evt->generator_lock));
250 vce_start_event_thread (vce_event_thread_t *evt, u8 max_events)
252 clib_fifo_validate (evt->event_index_fifo, max_events);
253 evt->handlers_index_by_event_key = hash_create (0, sizeof (uword));
255 pthread_cond_init (&(evt->generator_cond), NULL);
256 pthread_mutex_init (&(evt->generator_lock), NULL);
258 clib_spinlock_init (&(evt->events_lockp));
259 clib_spinlock_init (&(evt->handlers_lockp));
261 return pthread_create (&(evt->thread), NULL /* attr */ ,
262 vce_event_thread_fn, evt);