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_get_event_handler (vce_event_thread_t *evt, vce_event_key_t *evk)
85 vce_event_handler_reg_t *handler = 0;
88 clib_spinlock_lock (&evt->handlers_lockp);
89 p = hash_get (evt->handlers_index_by_event_key, evk->as_u64);
91 handler = pool_elt_at_index (evt->vce_event_handlers, p[0]);
92 clib_spinlock_unlock (&evt->handlers_lockp);
97 vce_event_handler_reg_t *
98 vce_register_handler (vce_event_thread_t *evt, vce_event_key_t *evk,
99 vce_event_callback_t cb)
101 vce_event_handler_reg_t *handler;
102 vce_event_handler_reg_t *old_handler = 0;
106 /* TODO - multiple handler support. For now we can replace
107 * and re-instate, which is useful for event recycling */
109 clib_spinlock_lock (&evt->handlers_lockp);
111 p = hash_get (evt->handlers_index_by_event_key, evk->as_u64);
114 old_handler = pool_elt_at_index (evt->vce_event_handlers, p[0]);
115 /* If we are just re-registering, ignore and move on
116 * else store the old handler_fn for unregister to re-instate */
117 if (old_handler->handler_fn == cb)
120 clib_spinlock_unlock (&evt->handlers_lockp);
122 /* Signal event thread that a handler exists in case any
123 * recycled events requiring this handler are pending */
124 pthread_mutex_lock (&(evt->generator_lock));
125 pthread_cond_signal (&(evt->generator_cond));
126 pthread_mutex_unlock (&(evt->generator_lock));
131 pool_get (evt->vce_event_handlers, handler);
132 handler_index = (u32) (handler - evt->vce_event_handlers);
134 handler->handler_fn = cb;
135 handler->replaced_handler_idx = (p) ? p[0] : ~0;
136 handler->ev_idx = ~0; //This will be set by the event thread if event happens
137 handler->evk = evk->as_u64;
139 hash_set (evt->handlers_index_by_event_key, evk->as_u64, handler_index);
141 pthread_cond_init (&(handler->handler_cond), NULL);
142 pthread_mutex_init (&(handler->handler_lock), NULL);
144 clib_spinlock_unlock (&evt->handlers_lockp);
146 /* Signal event thread that a new handler exists in case any
147 * recycled events requiring this handler are pending */
148 pthread_mutex_lock (&(evt->generator_lock));
149 pthread_cond_signal (&(evt->generator_cond));
150 pthread_mutex_unlock (&(evt->generator_lock));
156 vce_unregister_handler (vce_event_thread_t *evt,
157 vce_event_handler_reg_t *handler)
160 u64 evk = handler->evk;
161 u8 generate_signal = 0;
163 clib_spinlock_lock (&evt->handlers_lockp);
165 p = hash_get (evt->handlers_index_by_event_key, evk);
168 clib_spinlock_unlock (&evt->handlers_lockp);
169 return VNET_API_ERROR_NO_SUCH_ENTRY;
172 handler = pool_elt_at_index (evt->vce_event_handlers, p[0]);
174 /* If this handler replaced another handler, re-instate it */
175 if (handler->replaced_handler_idx != ~0)
177 hash_set (evt->handlers_index_by_event_key, evk,
178 handler->replaced_handler_idx);
183 hash_unset (evt->handlers_index_by_event_key, evk);
186 pthread_mutex_destroy (&(handler->handler_lock));
187 pthread_cond_destroy (&(handler->handler_cond));
188 pool_put (evt->vce_event_handlers, handler);
190 clib_spinlock_unlock (&evt->handlers_lockp);
194 /* Signal event thread that a new handler exists in case any
195 * recycled events requiring this handler are pending */
196 pthread_mutex_lock (&(evt->generator_lock));
197 pthread_cond_signal (&(evt->generator_cond));
198 pthread_mutex_unlock (&(evt->generator_lock));
205 vce_event_thread_fn (void *arg)
207 vce_event_thread_t *evt = (vce_event_thread_t *) arg;
210 vce_event_handler_reg_t *handler;
213 evt->recycle_event = 1; // Used for recycling events with no handlers
218 pthread_mutex_lock (&(evt->generator_lock));
219 while ( (clib_fifo_elts (evt->event_index_fifo) == 0) ||
222 evt->recycle_event = 0;
223 pthread_cond_wait (&(evt->generator_cond),
224 &(evt->generator_lock));
228 clib_spinlock_lock (&(evt->events_lockp));
230 clib_fifo_sub1 (evt->event_index_fifo, ev_idx);
231 ev = pool_elt_at_index (evt->vce_events, ev_idx);
233 clib_spinlock_unlock (&(evt->events_lockp));
237 clib_spinlock_lock (&evt->handlers_lockp);
239 p = hash_get (evt->handlers_index_by_event_key, ev->evk.as_u64);
242 /* If an event falls in the woods, and there is no handler to hear it,
243 * does it make any sound?
244 * I don't know either, so lets try recycling the event */
245 clib_fifo_add1 (evt->event_index_fifo, ev_idx);
246 evt->recycle_event = 1;
247 clib_spinlock_unlock (&evt->handlers_lockp);
250 handler = pool_elt_at_index (evt->vce_event_handlers, p[0]);
251 handler->ev_idx = ev_idx;
253 clib_spinlock_unlock (&evt->handlers_lockp);
255 (handler->handler_fn)(handler);
258 pthread_mutex_unlock (&(evt->generator_lock));
265 vce_start_event_thread (vce_event_thread_t *evt, u8 max_events)
267 clib_fifo_validate (evt->event_index_fifo, max_events);
268 evt->handlers_index_by_event_key = hash_create (0, sizeof (uword));
270 pthread_cond_init (&(evt->generator_cond), NULL);
271 pthread_mutex_init (&(evt->generator_lock), NULL);
273 clib_spinlock_init (&(evt->events_lockp));
274 clib_spinlock_init (&(evt->handlers_lockp));
276 return pthread_create (&(evt->thread), NULL /* attr */ ,
277 vce_event_thread_fn, evt);