Keith Burns (alagalah) | 3cf2d64 | 2018-02-23 10:17:01 -0800 | [diff] [blame] | 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 |
Keith Burns (alagalah) | 410bcca | 2018-03-23 13:42:49 -0700 | [diff] [blame] | 62 | vce_clear_event (vce_event_thread_t *evt, u32 ev_idx) |
Keith Burns (alagalah) | 3cf2d64 | 2018-02-23 10:17:01 -0800 | [diff] [blame] | 63 | { |
| 64 | clib_spinlock_lock (&(evt->events_lockp)); |
Keith Burns (alagalah) | 410bcca | 2018-03-23 13:42:49 -0700 | [diff] [blame] | 65 | pool_put_index (evt->vce_events, ev_idx); |
Keith Burns (alagalah) | 3cf2d64 | 2018-02-23 10:17:01 -0800 | [diff] [blame] | 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 | { |
Keith Burns (alagalah) | 00f44cc | 2018-03-07 09:26:38 -0800 | [diff] [blame] | 72 | vce_event_t *ev = 0; |
Keith Burns (alagalah) | 410bcca | 2018-03-23 13:42:49 -0700 | [diff] [blame] | 73 | /* Assumes caller has obtained the spinlock (evt->events_lockp) */ |
Keith Burns (alagalah) | 3cf2d64 | 2018-02-23 10:17:01 -0800 | [diff] [blame] | 74 | |
Keith Burns (alagalah) | 00f44cc | 2018-03-07 09:26:38 -0800 | [diff] [blame] | 75 | if ( ! pool_is_free_index (evt->vce_events, ev_idx)) |
| 76 | ev = pool_elt_at_index (evt->vce_events, ev_idx); |
Keith Burns (alagalah) | 3cf2d64 | 2018-02-23 10:17:01 -0800 | [diff] [blame] | 77 | |
| 78 | return ev; |
Keith Burns (alagalah) | 3cf2d64 | 2018-02-23 10:17:01 -0800 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | vce_event_handler_reg_t * |
Keith Burns (alagalah) | 7cf80e0 | 2018-03-08 16:46:25 -0800 | [diff] [blame] | 82 | vce_get_event_handler (vce_event_thread_t *evt, vce_event_key_t *evk) |
| 83 | { |
| 84 | vce_event_handler_reg_t *handler = 0; |
| 85 | uword *p; |
| 86 | |
| 87 | clib_spinlock_lock (&evt->handlers_lockp); |
| 88 | p = hash_get (evt->handlers_index_by_event_key, evk->as_u64); |
| 89 | if (p) |
| 90 | handler = pool_elt_at_index (evt->vce_event_handlers, p[0]); |
| 91 | clib_spinlock_unlock (&evt->handlers_lockp); |
| 92 | |
| 93 | return handler; |
| 94 | } |
| 95 | |
| 96 | vce_event_handler_reg_t * |
Keith Burns (alagalah) | 3cf2d64 | 2018-02-23 10:17:01 -0800 | [diff] [blame] | 97 | vce_register_handler (vce_event_thread_t *evt, vce_event_key_t *evk, |
Keith Burns (alagalah) | 410bcca | 2018-03-23 13:42:49 -0700 | [diff] [blame] | 98 | vce_event_callback_t cb, void *cb_args) |
Keith Burns (alagalah) | 3cf2d64 | 2018-02-23 10:17:01 -0800 | [diff] [blame] | 99 | { |
| 100 | vce_event_handler_reg_t *handler; |
| 101 | vce_event_handler_reg_t *old_handler = 0; |
| 102 | uword *p; |
| 103 | u32 handler_index; |
Keith Burns (alagalah) | 3cf2d64 | 2018-02-23 10:17:01 -0800 | [diff] [blame] | 104 | |
| 105 | /* TODO - multiple handler support. For now we can replace |
| 106 | * and re-instate, which is useful for event recycling */ |
| 107 | |
Keith Burns (alagalah) | 3cf2d64 | 2018-02-23 10:17:01 -0800 | [diff] [blame] | 108 | clib_spinlock_lock (&evt->handlers_lockp); |
| 109 | |
Keith Burns (alagalah) | 00f44cc | 2018-03-07 09:26:38 -0800 | [diff] [blame] | 110 | p = hash_get (evt->handlers_index_by_event_key, evk->as_u64); |
Keith Burns (alagalah) | 3cf2d64 | 2018-02-23 10:17:01 -0800 | [diff] [blame] | 111 | if (p) |
| 112 | { |
| 113 | old_handler = pool_elt_at_index (evt->vce_event_handlers, p[0]); |
| 114 | /* If we are just re-registering, ignore and move on |
| 115 | * else store the old handler_fn for unregister to re-instate */ |
| 116 | if (old_handler->handler_fn == cb) |
Keith Burns (alagalah) | 410bcca | 2018-03-23 13:42:49 -0700 | [diff] [blame] | 117 | { |
Keith Burns (alagalah) | 3cf2d64 | 2018-02-23 10:17:01 -0800 | [diff] [blame] | 118 | |
Keith Burns (alagalah) | 410bcca | 2018-03-23 13:42:49 -0700 | [diff] [blame] | 119 | clib_spinlock_unlock (&evt->handlers_lockp); |
Keith Burns (alagalah) | 3cf2d64 | 2018-02-23 10:17:01 -0800 | [diff] [blame] | 120 | |
Keith Burns (alagalah) | 410bcca | 2018-03-23 13:42:49 -0700 | [diff] [blame] | 121 | /* Signal event thread that a handler exists in case any |
| 122 | * recycled events requiring this handler are pending */ |
| 123 | pthread_mutex_lock (&(evt->generator_lock)); |
| 124 | pthread_cond_signal (&(evt->generator_cond)); |
| 125 | pthread_mutex_unlock (&(evt->generator_lock)); |
| 126 | return old_handler; |
| 127 | } |
Keith Burns (alagalah) | 3cf2d64 | 2018-02-23 10:17:01 -0800 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | pool_get (evt->vce_event_handlers, handler); |
| 131 | handler_index = (u32) (handler - evt->vce_event_handlers); |
| 132 | |
| 133 | handler->handler_fn = cb; |
Keith Burns (alagalah) | 410bcca | 2018-03-23 13:42:49 -0700 | [diff] [blame] | 134 | handler->replaced_handler_idx = (u32) ((p) ? p[0] : ~0); |
| 135 | handler->ev_idx = (u32) ~0; //This will be set by the event thread if event happens |
Keith Burns (alagalah) | 00f44cc | 2018-03-07 09:26:38 -0800 | [diff] [blame] | 136 | handler->evk = evk->as_u64; |
Keith Burns (alagalah) | 0d2b0d5 | 2018-03-06 15:55:22 -0800 | [diff] [blame] | 137 | handler->handler_fn_args = cb_args; |
Keith Burns (alagalah) | 3cf2d64 | 2018-02-23 10:17:01 -0800 | [diff] [blame] | 138 | |
Keith Burns (alagalah) | 00f44cc | 2018-03-07 09:26:38 -0800 | [diff] [blame] | 139 | hash_set (evt->handlers_index_by_event_key, evk->as_u64, handler_index); |
Keith Burns (alagalah) | 3cf2d64 | 2018-02-23 10:17:01 -0800 | [diff] [blame] | 140 | |
| 141 | pthread_cond_init (&(handler->handler_cond), NULL); |
| 142 | pthread_mutex_init (&(handler->handler_lock), NULL); |
| 143 | |
| 144 | clib_spinlock_unlock (&evt->handlers_lockp); |
| 145 | |
| 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)); |
| 151 | |
| 152 | return handler; |
| 153 | } |
| 154 | |
| 155 | int |
Keith Burns (alagalah) | 00f44cc | 2018-03-07 09:26:38 -0800 | [diff] [blame] | 156 | vce_unregister_handler (vce_event_thread_t *evt, |
| 157 | vce_event_handler_reg_t *handler) |
Keith Burns (alagalah) | 3cf2d64 | 2018-02-23 10:17:01 -0800 | [diff] [blame] | 158 | { |
Keith Burns (alagalah) | 3cf2d64 | 2018-02-23 10:17:01 -0800 | [diff] [blame] | 159 | uword *p; |
Keith Burns (alagalah) | 00f44cc | 2018-03-07 09:26:38 -0800 | [diff] [blame] | 160 | u64 evk = handler->evk; |
Keith Burns (alagalah) | 3cf2d64 | 2018-02-23 10:17:01 -0800 | [diff] [blame] | 161 | u8 generate_signal = 0; |
| 162 | |
| 163 | clib_spinlock_lock (&evt->handlers_lockp); |
| 164 | |
Keith Burns (alagalah) | 00f44cc | 2018-03-07 09:26:38 -0800 | [diff] [blame] | 165 | p = hash_get (evt->handlers_index_by_event_key, evk); |
Keith Burns (alagalah) | 3cf2d64 | 2018-02-23 10:17:01 -0800 | [diff] [blame] | 166 | if (!p) |
| 167 | { |
| 168 | clib_spinlock_unlock (&evt->handlers_lockp); |
Keith Burns (alagalah) | 3cf2d64 | 2018-02-23 10:17:01 -0800 | [diff] [blame] | 169 | return VNET_API_ERROR_NO_SUCH_ENTRY; |
| 170 | } |
| 171 | |
| 172 | handler = pool_elt_at_index (evt->vce_event_handlers, p[0]); |
| 173 | |
| 174 | /* If this handler replaced another handler, re-instate it */ |
| 175 | if (handler->replaced_handler_idx != ~0) |
| 176 | { |
Keith Burns (alagalah) | 00f44cc | 2018-03-07 09:26:38 -0800 | [diff] [blame] | 177 | hash_set (evt->handlers_index_by_event_key, evk, |
Keith Burns (alagalah) | 3cf2d64 | 2018-02-23 10:17:01 -0800 | [diff] [blame] | 178 | handler->replaced_handler_idx); |
| 179 | generate_signal = 1; |
| 180 | } |
| 181 | else |
| 182 | { |
Keith Burns (alagalah) | 00f44cc | 2018-03-07 09:26:38 -0800 | [diff] [blame] | 183 | hash_unset (evt->handlers_index_by_event_key, evk); |
Keith Burns (alagalah) | 3cf2d64 | 2018-02-23 10:17:01 -0800 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | pthread_mutex_destroy (&(handler->handler_lock)); |
| 187 | pthread_cond_destroy (&(handler->handler_cond)); |
| 188 | pool_put (evt->vce_event_handlers, handler); |
| 189 | |
| 190 | clib_spinlock_unlock (&evt->handlers_lockp); |
| 191 | |
| 192 | if (generate_signal) |
| 193 | { |
| 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)); |
| 199 | } |
| 200 | |
| 201 | return 0; |
| 202 | } |
| 203 | |
| 204 | void * |
| 205 | vce_event_thread_fn (void *arg) |
| 206 | { |
| 207 | vce_event_thread_t *evt = (vce_event_thread_t *) arg; |
| 208 | vce_event_t *ev; |
| 209 | u32 ev_idx; |
| 210 | vce_event_handler_reg_t *handler; |
| 211 | uword *p; |
Keith Burns (alagalah) | 410bcca | 2018-03-23 13:42:49 -0700 | [diff] [blame] | 212 | u32 recycle_count = 0; |
Keith Burns (alagalah) | 3cf2d64 | 2018-02-23 10:17:01 -0800 | [diff] [blame] | 213 | |
Dave Wallace | 49c13c7 | 2018-03-13 19:42:02 -0400 | [diff] [blame] | 214 | pthread_mutex_lock (&(evt->generator_lock)); |
Keith Burns (alagalah) | 410bcca | 2018-03-23 13:42:49 -0700 | [diff] [blame] | 215 | while (1) |
Keith Burns (alagalah) | 3cf2d64 | 2018-02-23 10:17:01 -0800 | [diff] [blame] | 216 | { |
Keith Burns (alagalah) | 410bcca | 2018-03-23 13:42:49 -0700 | [diff] [blame] | 217 | uword fifo_depth = clib_fifo_elts (evt->event_index_fifo); |
| 218 | while ((fifo_depth == 0) || (recycle_count == fifo_depth)) |
| 219 | { |
| 220 | recycle_count = 0; |
| 221 | pthread_cond_wait (&(evt->generator_cond), &(evt->generator_lock)); |
| 222 | fifo_depth = clib_fifo_elts (evt->event_index_fifo); |
| 223 | } |
Keith Burns (alagalah) | 3cf2d64 | 2018-02-23 10:17:01 -0800 | [diff] [blame] | 224 | |
| 225 | /* Remove event */ |
| 226 | clib_spinlock_lock (&(evt->events_lockp)); |
Keith Burns (alagalah) | 3cf2d64 | 2018-02-23 10:17:01 -0800 | [diff] [blame] | 227 | clib_fifo_sub1 (evt->event_index_fifo, ev_idx); |
Keith Burns (alagalah) | 410bcca | 2018-03-23 13:42:49 -0700 | [diff] [blame] | 228 | ev = vce_get_event_from_index (evt, ev_idx); |
Keith Burns (alagalah) | 3cf2d64 | 2018-02-23 10:17:01 -0800 | [diff] [blame] | 229 | ASSERT(ev); |
Keith Burns (alagalah) | 410bcca | 2018-03-23 13:42:49 -0700 | [diff] [blame] | 230 | if (recycle_count && ev->recycle) |
| 231 | { |
| 232 | clib_fifo_add1 (evt->event_index_fifo, ev_idx); |
| 233 | clib_spinlock_unlock (&(evt->events_lockp)); |
| 234 | continue; |
| 235 | } |
Keith Burns (alagalah) | 3cf2d64 | 2018-02-23 10:17:01 -0800 | [diff] [blame] | 236 | clib_spinlock_lock (&evt->handlers_lockp); |
| 237 | |
Keith Burns (alagalah) | 00f44cc | 2018-03-07 09:26:38 -0800 | [diff] [blame] | 238 | p = hash_get (evt->handlers_index_by_event_key, ev->evk.as_u64); |
Keith Burns (alagalah) | 3cf2d64 | 2018-02-23 10:17:01 -0800 | [diff] [blame] | 239 | if (!p) |
Keith Burns (alagalah) | 410bcca | 2018-03-23 13:42:49 -0700 | [diff] [blame] | 240 | { |
| 241 | /* If an event falls in the woods, and there is no handler to hear it, |
| 242 | * does it make any sound? |
| 243 | * I don't know either, so lets biff the event */ |
| 244 | pool_put(evt->vce_events, ev); |
Dave Wallace | 49c13c7 | 2018-03-13 19:42:02 -0400 | [diff] [blame] | 245 | clib_spinlock_unlock (&(evt->events_lockp)); |
Keith Burns (alagalah) | 410bcca | 2018-03-23 13:42:49 -0700 | [diff] [blame] | 246 | clib_spinlock_unlock (&evt->handlers_lockp); |
Dave Wallace | 49c13c7 | 2018-03-13 19:42:02 -0400 | [diff] [blame] | 247 | pthread_mutex_unlock (&(evt->generator_lock)); |
Keith Burns (alagalah) | 410bcca | 2018-03-23 13:42:49 -0700 | [diff] [blame] | 248 | } |
Dave Wallace | 49c13c7 | 2018-03-13 19:42:02 -0400 | [diff] [blame] | 249 | else |
| 250 | { |
Keith Burns (alagalah) | 410bcca | 2018-03-23 13:42:49 -0700 | [diff] [blame] | 251 | u32 evt_recycle = ev->recycle; |
Dave Wallace | 49c13c7 | 2018-03-13 19:42:02 -0400 | [diff] [blame] | 252 | handler = pool_elt_at_index (evt->vce_event_handlers, p[0]); |
| 253 | handler->ev_idx = ev_idx; |
Keith Burns (alagalah) | 410bcca | 2018-03-23 13:42:49 -0700 | [diff] [blame] | 254 | ev->recycle = 0; |
Keith Burns (alagalah) | 3cf2d64 | 2018-02-23 10:17:01 -0800 | [diff] [blame] | 255 | |
Dave Wallace | 49c13c7 | 2018-03-13 19:42:02 -0400 | [diff] [blame] | 256 | clib_spinlock_unlock (&(evt->events_lockp)); |
| 257 | clib_spinlock_unlock (&evt->handlers_lockp); |
| 258 | pthread_mutex_unlock (&(evt->generator_lock)); |
Keith Burns (alagalah) | 3cf2d64 | 2018-02-23 10:17:01 -0800 | [diff] [blame] | 259 | |
Dave Wallace | 49c13c7 | 2018-03-13 19:42:02 -0400 | [diff] [blame] | 260 | (handler->handler_fn)(handler); |
Keith Burns (alagalah) | 410bcca | 2018-03-23 13:42:49 -0700 | [diff] [blame] | 261 | |
| 262 | clib_spinlock_lock (&(evt->events_lockp)); |
| 263 | ev = vce_get_event_from_index (evt, ev_idx); |
| 264 | recycle_count += (!evt_recycle && ev && ev->recycle) ? 1 : 0; |
| 265 | clib_spinlock_unlock(&(evt->events_lockp)); |
Dave Wallace | 49c13c7 | 2018-03-13 19:42:02 -0400 | [diff] [blame] | 266 | } |
Keith Burns (alagalah) | 3cf2d64 | 2018-02-23 10:17:01 -0800 | [diff] [blame] | 267 | |
Dave Wallace | 49c13c7 | 2018-03-13 19:42:02 -0400 | [diff] [blame] | 268 | pthread_mutex_lock (&(evt->generator_lock)); |
Keith Burns (alagalah) | 3cf2d64 | 2018-02-23 10:17:01 -0800 | [diff] [blame] | 269 | } |
Keith Burns (alagalah) | 3cf2d64 | 2018-02-23 10:17:01 -0800 | [diff] [blame] | 270 | return NULL; |
| 271 | } |
| 272 | |
| 273 | int |
| 274 | vce_start_event_thread (vce_event_thread_t *evt, u8 max_events) |
| 275 | { |
| 276 | clib_fifo_validate (evt->event_index_fifo, max_events); |
| 277 | evt->handlers_index_by_event_key = hash_create (0, sizeof (uword)); |
| 278 | |
| 279 | pthread_cond_init (&(evt->generator_cond), NULL); |
| 280 | pthread_mutex_init (&(evt->generator_lock), NULL); |
| 281 | |
| 282 | clib_spinlock_init (&(evt->events_lockp)); |
| 283 | clib_spinlock_init (&(evt->handlers_lockp)); |
| 284 | |
| 285 | return pthread_create (&(evt->thread), NULL /* attr */ , |
Keith Burns (alagalah) | 410bcca | 2018-03-23 13:42:49 -0700 | [diff] [blame] | 286 | vce_event_thread_fn, evt); |
Keith Burns (alagalah) | 0d2b0d5 | 2018-03-06 15:55:22 -0800 | [diff] [blame] | 287 | } |
Keith Burns (alagalah) | 410bcca | 2018-03-23 13:42:49 -0700 | [diff] [blame] | 288 | |