b706a93e56a327fb0f0677e2a1527630bccb5a8e
[vpp.git] / src / vcl / vcl_event.c
1 /*
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
29 int
30 vce_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
61 void
62 vce_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
69 vce_event_t *
70 vce_get_event_from_index(vce_event_thread_t *evt, u32 ev_idx)
71 {
72   vce_event_t *ev;
73
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));
77
78   return ev;
79
80 }
81
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)
85 {
86   vce_event_handler_reg_t *handler;
87   vce_event_handler_reg_t *old_handler = 0;
88   uword *p;
89   u32 handler_index;
90   u64 adj_key;
91
92   /* TODO - multiple handler support. For now we can replace
93    * and re-instate, which is useful for event recycling */
94
95   adj_key = evk->as_u64 | (1LL << 63); //evk can be 0, which won't hash
96
97   clib_spinlock_lock (&evt->handlers_lockp);
98
99   p = hash_get (evt->handlers_index_by_event_key, adj_key);
100   if (p)
101     {
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)
106         {
107
108           clib_spinlock_unlock (&evt->handlers_lockp);
109
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));
115           return old_handler;
116         }
117     }
118
119   pool_get (evt->vce_event_handlers, handler);
120   handler_index = (u32) (handler - evt->vce_event_handlers);
121
122   handler->handler_fn = cb;
123   handler->replaced_handler_idx = (p) ? p[0] : ~0;
124
125   hash_set (evt->handlers_index_by_event_key, adj_key, handler_index);
126
127   pthread_cond_init (&(handler->handler_cond), NULL);
128   pthread_mutex_init (&(handler->handler_lock), NULL);
129
130   clib_spinlock_unlock (&evt->handlers_lockp);
131
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));
137
138   return handler;
139 }
140
141 int
142 vce_unregister_handler (vce_event_thread_t *evt, vce_event_t *ev)
143 {
144   vce_event_handler_reg_t *handler;
145   uword *p;
146   u64 adj_key = ev->evk.as_u64 | (1LL << 63);
147   u8 generate_signal = 0;
148
149   clib_spinlock_lock (&evt->handlers_lockp);
150
151   p = hash_get (evt->handlers_index_by_event_key, adj_key);
152   if (!p)
153     {
154       clib_spinlock_unlock (&evt->handlers_lockp);
155
156       return VNET_API_ERROR_NO_SUCH_ENTRY;
157     }
158
159   handler = pool_elt_at_index (evt->vce_event_handlers, p[0]);
160
161   /* If this handler replaced another handler, re-instate it */
162   if (handler->replaced_handler_idx != ~0)
163     {
164       hash_set (evt->handlers_index_by_event_key, adj_key,
165                 handler->replaced_handler_idx);
166       generate_signal = 1;
167     }
168   else
169     {
170       hash_unset (evt->handlers_index_by_event_key, adj_key);
171     }
172
173   pthread_mutex_destroy (&(handler->handler_lock));
174   pthread_cond_destroy (&(handler->handler_cond));
175   pool_put (evt->vce_event_handlers, handler);
176
177   clib_spinlock_unlock (&evt->handlers_lockp);
178
179   if (generate_signal)
180     {
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));
186     }
187
188   return 0;
189 }
190
191 void *
192 vce_event_thread_fn (void *arg)
193 {
194   vce_event_thread_t *evt = (vce_event_thread_t *) arg;
195   vce_event_t *ev;
196   u32 ev_idx;
197   vce_event_handler_reg_t *handler;
198   uword *p;
199   u64 adj_key;
200
201   evt->recycle_event = 1; // Used for recycling events with no handlers
202
203
204   do
205     {
206       pthread_mutex_lock (&(evt->generator_lock));
207       while ( (clib_fifo_elts (evt->event_index_fifo) == 0) ||
208               evt->recycle_event)
209         {
210           evt->recycle_event = 0;
211           pthread_cond_wait (&(evt->generator_cond),
212                              &(evt->generator_lock));
213         }
214
215       /* Remove event */
216       clib_spinlock_lock (&(evt->events_lockp));
217
218       clib_fifo_sub1 (evt->event_index_fifo, ev_idx);
219       ev = pool_elt_at_index (evt->vce_events, ev_idx);
220
221       clib_spinlock_unlock (&(evt->events_lockp));
222
223       ASSERT(ev);
224       adj_key = ev->evk.as_u64 | (1LL << 63);
225
226       clib_spinlock_lock (&evt->handlers_lockp);
227
228       p = hash_get (evt->handlers_index_by_event_key, adj_key);
229       if (!p)
230         {
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);
237           goto unlock;
238         }
239       handler = pool_elt_at_index (evt->vce_event_handlers, p[0]);
240       handler->ev_idx = ev_idx;
241
242       clib_spinlock_unlock (&evt->handlers_lockp);
243
244       (handler->handler_fn)(handler);
245
246     unlock:
247       pthread_mutex_unlock (&(evt->generator_lock));
248     }
249   while (1);
250   return NULL;
251 }
252
253 int
254 vce_start_event_thread (vce_event_thread_t *evt, u8 max_events)
255 {
256   clib_fifo_validate (evt->event_index_fifo, max_events);
257   evt->handlers_index_by_event_key = hash_create (0, sizeof (uword));
258
259   pthread_cond_init (&(evt->generator_cond), NULL);
260   pthread_mutex_init (&(evt->generator_lock), NULL);
261
262   clib_spinlock_init (&(evt->events_lockp));
263   clib_spinlock_init (&(evt->handlers_lockp));
264
265   return pthread_create (&(evt->thread), NULL /* attr */ ,
266                          vce_event_thread_fn, evt);
267 }