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 ev = pool_elt_at_index (evt->vce_events, ev_idx);
76 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;
92 /* TODO - multiple handler support. For now we can replace
93 * and re-instate, which is useful for event recycling */
95 adj_key = evk->as_u64 | (1LL << 63); //evk can be 0, which won't hash
97 clib_spinlock_lock (&evt->handlers_lockp);
99 p = hash_get (evt->handlers_index_by_event_key, adj_key);
102 old_handler = pool_elt_at_index (evt->vce_event_handlers, p[0]);
103 /* If we are just re-registering, ignore and move on
104 * else store the old handler_fn for unregister to re-instate */
105 if (old_handler->handler_fn == cb)
108 clib_spinlock_unlock (&evt->handlers_lockp);
110 /* Signal event thread that a handler exists in case any
111 * recycled events requiring this handler are pending */
112 pthread_mutex_lock (&(evt->generator_lock));
113 pthread_cond_signal (&(evt->generator_cond));
114 pthread_mutex_unlock (&(evt->generator_lock));
119 pool_get (evt->vce_event_handlers, handler);
120 handler_index = (u32) (handler - evt->vce_event_handlers);
122 handler->handler_fn = cb;
123 handler->replaced_handler_idx = (p) ? p[0] : ~0;
125 hash_set (evt->handlers_index_by_event_key, adj_key, handler_index);
127 pthread_cond_init (&(handler->handler_cond), NULL);
128 pthread_mutex_init (&(handler->handler_lock), NULL);
130 clib_spinlock_unlock (&evt->handlers_lockp);
132 /* Signal event thread that a new handler exists in case any
133 * recycled events requiring this handler are pending */
134 pthread_mutex_lock (&(evt->generator_lock));
135 pthread_cond_signal (&(evt->generator_cond));
136 pthread_mutex_unlock (&(evt->generator_lock));
142 vce_unregister_handler (vce_event_thread_t *evt, vce_event_t *ev)
144 vce_event_handler_reg_t *handler;
146 u64 adj_key = ev->evk.as_u64 | (1LL << 63);
147 u8 generate_signal = 0;
149 clib_spinlock_lock (&evt->handlers_lockp);
151 p = hash_get (evt->handlers_index_by_event_key, adj_key);
154 clib_spinlock_unlock (&evt->handlers_lockp);
156 return VNET_API_ERROR_NO_SUCH_ENTRY;
159 handler = pool_elt_at_index (evt->vce_event_handlers, p[0]);
161 /* If this handler replaced another handler, re-instate it */
162 if (handler->replaced_handler_idx != ~0)
164 hash_set (evt->handlers_index_by_event_key, adj_key,
165 handler->replaced_handler_idx);
170 hash_unset (evt->handlers_index_by_event_key, adj_key);
173 pthread_mutex_destroy (&(handler->handler_lock));
174 pthread_cond_destroy (&(handler->handler_cond));
175 pool_put (evt->vce_event_handlers, handler);
177 clib_spinlock_unlock (&evt->handlers_lockp);
181 /* Signal event thread that a new handler exists in case any
182 * recycled events requiring this handler are pending */
183 pthread_mutex_lock (&(evt->generator_lock));
184 pthread_cond_signal (&(evt->generator_cond));
185 pthread_mutex_unlock (&(evt->generator_lock));
192 vce_event_thread_fn (void *arg)
194 vce_event_thread_t *evt = (vce_event_thread_t *) arg;
197 vce_event_handler_reg_t *handler;
201 evt->recycle_event = 1; // Used for recycling events with no handlers
206 pthread_mutex_lock (&(evt->generator_lock));
207 while ( (clib_fifo_elts (evt->event_index_fifo) == 0) ||
210 evt->recycle_event = 0;
211 pthread_cond_wait (&(evt->generator_cond),
212 &(evt->generator_lock));
216 clib_spinlock_lock (&(evt->events_lockp));
218 clib_fifo_sub1 (evt->event_index_fifo, ev_idx);
219 ev = pool_elt_at_index (evt->vce_events, ev_idx);
221 clib_spinlock_unlock (&(evt->events_lockp));
224 adj_key = ev->evk.as_u64 | (1LL << 63);
226 clib_spinlock_lock (&evt->handlers_lockp);
228 p = hash_get (evt->handlers_index_by_event_key, adj_key);
231 /* If an event falls in the woods, and there is no handler to hear it,
232 * does it make any sound?
233 * I don't know either, so lets try recycling the event */
234 clib_fifo_add1 (evt->event_index_fifo, ev_idx);
235 evt->recycle_event = 1;
236 clib_spinlock_unlock (&evt->handlers_lockp);
239 handler = pool_elt_at_index (evt->vce_event_handlers, p[0]);
240 handler->ev_idx = ev_idx;
242 clib_spinlock_unlock (&evt->handlers_lockp);
244 (handler->handler_fn)(handler);
247 pthread_mutex_unlock (&(evt->generator_lock));
254 vce_start_event_thread (vce_event_thread_t *evt, u8 max_events)
256 clib_fifo_validate (evt->event_index_fifo, max_events);
257 evt->handlers_index_by_event_key = hash_create (0, sizeof (uword));
259 pthread_cond_init (&(evt->generator_cond), NULL);
260 pthread_mutex_init (&(evt->generator_lock), NULL);
262 clib_spinlock_init (&(evt->events_lockp));
263 clib_spinlock_init (&(evt->handlers_lockp));
265 return pthread_create (&(evt->thread), NULL /* attr */ ,
266 vce_event_thread_fn, evt);