blob: 65d28707607ae8ce67c81a2f29d935895b319fdd [file] [log] [blame]
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001/*
2 * Copyright (c) 2019 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
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.
14 */
15
16#include <vppinfra/fifo.h>
17#include <vppinfra/pool.h>
18#include <vppinfra/hash.h>
19#include <vnet/api_errno.h>
20
21#include <vcl/vcl_event.h>
22/**
23 * @file
24 * @brief VPP Communications Library (VCL) event handler.
25 *
26 * Definitions for generic event handling in VCL.
27 */
28
29int
30vce_generate_event (vce_event_thread_t *evt, u32 ev_idx)
31{
32 int elts, rv = 0;
33 vce_event_t *p;
34
35 pthread_mutex_lock (&(evt->generator_lock));
36
37 /* Check there is event data for this event */
38
39 clib_spinlock_lock (&(evt->events_lockp));
40 p = pool_elt_at_index (evt->vce_events, ev_idx);
41 ASSERT(p);
42
43 elts = (int) clib_fifo_free_elts (evt->event_index_fifo);
44 if (PREDICT_TRUE (elts))
45 {
46 /* Add event to queue */
47 clib_fifo_add1 (evt->event_index_fifo, ev_idx);
48 pthread_cond_signal (&(evt->generator_cond));
49 }
50 else
51 {
52 rv = VNET_API_ERROR_QUEUE_FULL;
53 }
54
55 clib_spinlock_unlock (&(evt->events_lockp));
56 pthread_mutex_unlock (&(evt->generator_lock));
57
58 return rv;
59}
60
61void
62vce_clear_event (vce_event_thread_t *evt, vce_event_t *ev)
63{
64 clib_spinlock_lock (&(evt->events_lockp));
65 pool_put (evt->vce_events, ev);
66 clib_spinlock_unlock (&(evt->events_lockp));
67}
68
69vce_event_t *
70vce_get_event_from_index(vce_event_thread_t *evt, u32 ev_idx)
71{
Keith Burns (alagalah)00f44cc2018-03-07 09:26:38 -080072 vce_event_t *ev = 0;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -080073
74 clib_spinlock_lock (&(evt->events_lockp));
Keith Burns (alagalah)00f44cc2018-03-07 09:26:38 -080075 if ( ! pool_is_free_index (evt->vce_events, ev_idx))
76 ev = pool_elt_at_index (evt->vce_events, ev_idx);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -080077 clib_spinlock_unlock (&(evt->events_lockp));
78
79 return ev;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -080080}
81
82vce_event_handler_reg_t *
83vce_register_handler (vce_event_thread_t *evt, vce_event_key_t *evk,
84 vce_event_callback_t cb)
85{
86 vce_event_handler_reg_t *handler;
87 vce_event_handler_reg_t *old_handler = 0;
88 uword *p;
89 u32 handler_index;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -080090
91 /* TODO - multiple handler support. For now we can replace
92 * and re-instate, which is useful for event recycling */
93
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -080094 clib_spinlock_lock (&evt->handlers_lockp);
95
Keith Burns (alagalah)00f44cc2018-03-07 09:26:38 -080096 p = hash_get (evt->handlers_index_by_event_key, evk->as_u64);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -080097 if (p)
98 {
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)
103 {
104
105 clib_spinlock_unlock (&evt->handlers_lockp);
106
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));
112 return old_handler;
113 }
114 }
115
116 pool_get (evt->vce_event_handlers, handler);
117 handler_index = (u32) (handler - evt->vce_event_handlers);
118
119 handler->handler_fn = cb;
120 handler->replaced_handler_idx = (p) ? p[0] : ~0;
Keith Burns (alagalah)00f44cc2018-03-07 09:26:38 -0800121 handler->ev_idx = ~0; //This will be set by the event thread if event happens
122 handler->evk = evk->as_u64;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -0800123
Keith Burns (alagalah)00f44cc2018-03-07 09:26:38 -0800124 hash_set (evt->handlers_index_by_event_key, evk->as_u64, handler_index);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -0800125
126 pthread_cond_init (&(handler->handler_cond), NULL);
127 pthread_mutex_init (&(handler->handler_lock), NULL);
128
129 clib_spinlock_unlock (&evt->handlers_lockp);
130
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));
136
137 return handler;
138}
139
140int
Keith Burns (alagalah)00f44cc2018-03-07 09:26:38 -0800141vce_unregister_handler (vce_event_thread_t *evt,
142 vce_event_handler_reg_t *handler)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -0800143{
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -0800144 uword *p;
Keith Burns (alagalah)00f44cc2018-03-07 09:26:38 -0800145 u64 evk = handler->evk;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -0800146 u8 generate_signal = 0;
147
148 clib_spinlock_lock (&evt->handlers_lockp);
149
Keith Burns (alagalah)00f44cc2018-03-07 09:26:38 -0800150 p = hash_get (evt->handlers_index_by_event_key, evk);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -0800151 if (!p)
152 {
153 clib_spinlock_unlock (&evt->handlers_lockp);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -0800154 return VNET_API_ERROR_NO_SUCH_ENTRY;
155 }
156
157 handler = pool_elt_at_index (evt->vce_event_handlers, p[0]);
158
159 /* If this handler replaced another handler, re-instate it */
160 if (handler->replaced_handler_idx != ~0)
161 {
Keith Burns (alagalah)00f44cc2018-03-07 09:26:38 -0800162 hash_set (evt->handlers_index_by_event_key, evk,
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -0800163 handler->replaced_handler_idx);
164 generate_signal = 1;
165 }
166 else
167 {
Keith Burns (alagalah)00f44cc2018-03-07 09:26:38 -0800168 hash_unset (evt->handlers_index_by_event_key, evk);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -0800169 }
170
171 pthread_mutex_destroy (&(handler->handler_lock));
172 pthread_cond_destroy (&(handler->handler_cond));
173 pool_put (evt->vce_event_handlers, handler);
174
175 clib_spinlock_unlock (&evt->handlers_lockp);
176
177 if (generate_signal)
178 {
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));
184 }
185
186 return 0;
187}
188
189void *
190vce_event_thread_fn (void *arg)
191{
192 vce_event_thread_t *evt = (vce_event_thread_t *) arg;
193 vce_event_t *ev;
194 u32 ev_idx;
195 vce_event_handler_reg_t *handler;
196 uword *p;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -0800197
198 evt->recycle_event = 1; // Used for recycling events with no handlers
199
200
201 do
202 {
203 pthread_mutex_lock (&(evt->generator_lock));
204 while ( (clib_fifo_elts (evt->event_index_fifo) == 0) ||
205 evt->recycle_event)
206 {
207 evt->recycle_event = 0;
208 pthread_cond_wait (&(evt->generator_cond),
209 &(evt->generator_lock));
210 }
211
212 /* Remove event */
213 clib_spinlock_lock (&(evt->events_lockp));
214
215 clib_fifo_sub1 (evt->event_index_fifo, ev_idx);
216 ev = pool_elt_at_index (evt->vce_events, ev_idx);
217
218 clib_spinlock_unlock (&(evt->events_lockp));
219
220 ASSERT(ev);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -0800221
222 clib_spinlock_lock (&evt->handlers_lockp);
223
Keith Burns (alagalah)00f44cc2018-03-07 09:26:38 -0800224 p = hash_get (evt->handlers_index_by_event_key, ev->evk.as_u64);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -0800225 if (!p)
226 {
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);
233 goto unlock;
234 }
235 handler = pool_elt_at_index (evt->vce_event_handlers, p[0]);
236 handler->ev_idx = ev_idx;
237
238 clib_spinlock_unlock (&evt->handlers_lockp);
239
240 (handler->handler_fn)(handler);
241
242 unlock:
243 pthread_mutex_unlock (&(evt->generator_lock));
244 }
245 while (1);
246 return NULL;
247}
248
249int
250vce_start_event_thread (vce_event_thread_t *evt, u8 max_events)
251{
252 clib_fifo_validate (evt->event_index_fifo, max_events);
253 evt->handlers_index_by_event_key = hash_create (0, sizeof (uword));
254
255 pthread_cond_init (&(evt->generator_cond), NULL);
256 pthread_mutex_init (&(evt->generator_lock), NULL);
257
258 clib_spinlock_init (&(evt->events_lockp));
259 clib_spinlock_init (&(evt->handlers_lockp));
260
261 return pthread_create (&(evt->thread), NULL /* attr */ ,
262 vce_event_thread_fn, evt);
263}