+static vcl_worker_t *
+vcl_worker_alloc (void)
+{
+ vcl_worker_t *wrk;
+ pool_get (vcm->workers, wrk);
+ memset (wrk, 0, sizeof (*wrk));
+ wrk->wrk_index = wrk - vcm->workers;
+ return wrk;
+}
+
+static void
+vcl_worker_free (vcl_worker_t * wrk)
+{
+ pool_put (vcm->workers, wrk);
+}
+
+static void
+vcl_worker_cleanup (void *arg)
+{
+ vcl_worker_t *wrk = vcl_worker_get_current ();
+ VDBG (0, "cleaning up worker %u", wrk->wrk_index);
+ vcl_send_app_worker_add_del (0 /* is_add */ );
+ close (wrk->mqs_epfd);
+ hash_free (wrk->session_index_by_vpp_handles);
+ hash_free (wrk->ct_registration_by_mq);
+ clib_spinlock_free (&wrk->ct_registration_lock);
+ vec_free (wrk->mq_events);
+ vec_free (wrk->mq_msg_vector);
+ vcl_set_worker_index (~0);
+ vcl_worker_free (wrk);
+}
+
+vcl_worker_t *
+vcl_worker_alloc_and_init ()
+{
+ vcl_worker_t *wrk;
+
+ /* This was initialized already */
+ if (vcl_get_worker_index () != ~0)
+ return 0;
+
+ if (pool_elts (vcm->workers) == vcm->cfg.max_workers)
+ {
+ VDBG (0, "max-workers %u limit reached", vcm->cfg.max_workers);
+ return 0;
+ }
+
+ clib_spinlock_lock (&vcm->workers_lock);
+ wrk = vcl_worker_alloc ();
+ vcl_set_worker_index (wrk->wrk_index);
+
+ wrk->mqs_epfd = -1;
+ if (vcm->cfg.use_mq_eventfd)
+ {
+ wrk->mqs_epfd = epoll_create (1);
+ if (wrk->mqs_epfd < 0)
+ {
+ clib_unix_warning ("epoll_create() returned");
+ return 0;
+ }
+ }
+
+ wrk->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
+ wrk->ct_registration_by_mq = hash_create (0, sizeof (uword));
+ clib_spinlock_init (&wrk->ct_registration_lock);
+ clib_time_init (&wrk->clib_time);
+ vec_validate (wrk->mq_events, 64);
+ vec_validate (wrk->mq_msg_vector, 128);
+ vec_reset_length (wrk->mq_msg_vector);
+
+ if (wrk->wrk_index == 0)
+ {
+ clib_spinlock_unlock (&vcm->workers_lock);
+ return wrk;
+ }
+
+ vcm->app_state = STATE_APP_ADDING_WORKER;
+ vcl_send_app_worker_add_del (1 /* is_add */ );
+ if (vcl_wait_for_app_state_change (STATE_APP_READY))
+ {
+ clib_warning ("failed to add worker to vpp");
+ return 0;
+ }
+
+ if (pthread_key_create (&vcl_worker_stop_key, vcl_worker_cleanup))
+ clib_warning ("failed to add pthread cleanup function");
+
+ clib_spinlock_unlock (&vcm->workers_lock);
+
+ VDBG (0, "added worker %u", wrk->wrk_index);
+
+ return wrk;
+}
+