2 * Copyright (c) 2018 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 <vcl/vcl_private.h>
18 static pthread_key_t vcl_worker_stop_key;
21 vppcom_app_state_str (app_state_t state)
28 st = "STATE_APP_START";
31 case STATE_APP_CONN_VPP:
32 st = "STATE_APP_CONN_VPP";
35 case STATE_APP_ENABLED:
36 st = "STATE_APP_ENABLED";
39 case STATE_APP_ATTACHED:
40 st = "STATE_APP_ATTACHED";
44 st = "UNKNOWN_APP_STATE";
52 vcl_wait_for_app_state_change (app_state_t app_state)
54 vcl_worker_t *wrk = vcl_worker_get_current ();
55 f64 timeout = clib_time_now (&wrk->clib_time) + vcm->cfg.app_timeout;
57 while (clib_time_now (&wrk->clib_time) < timeout)
59 if (vcm->app_state == app_state)
61 if (vcm->app_state == STATE_APP_FAILED)
62 return VPPCOM_ECONNABORTED;
64 VDBG (0, "VCL<%d>: timeout waiting for state %s (%d)", getpid (),
65 vppcom_app_state_str (app_state), app_state);
66 vcl_evt (VCL_EVT_SESSION_TIMEOUT, vcm, app_state);
68 return VPPCOM_ETIMEDOUT;
71 vcl_cut_through_registration_t *
72 vcl_ct_registration_lock_and_alloc (vcl_worker_t * wrk)
74 vcl_cut_through_registration_t *cr;
75 clib_spinlock_lock (&wrk->ct_registration_lock);
76 pool_get (wrk->cut_through_registrations, cr);
77 memset (cr, 0, sizeof (*cr));
78 cr->epoll_evt_conn_index = -1;
83 vcl_ct_registration_index (vcl_worker_t * wrk,
84 vcl_cut_through_registration_t * ctr)
86 return (ctr - wrk->cut_through_registrations);
90 vcl_ct_registration_lock (vcl_worker_t * wrk)
92 clib_spinlock_lock (&wrk->ct_registration_lock);
96 vcl_ct_registration_unlock (vcl_worker_t * wrk)
98 clib_spinlock_unlock (&wrk->ct_registration_lock);
101 vcl_cut_through_registration_t *
102 vcl_ct_registration_get (vcl_worker_t * wrk, u32 ctr_index)
104 if (pool_is_free_index (wrk->cut_through_registrations, ctr_index))
106 return pool_elt_at_index (wrk->cut_through_registrations, ctr_index);
109 vcl_cut_through_registration_t *
110 vcl_ct_registration_lock_and_lookup (vcl_worker_t * wrk, uword mq_addr)
113 clib_spinlock_lock (&wrk->ct_registration_lock);
114 p = hash_get (wrk->ct_registration_by_mq, mq_addr);
117 return vcl_ct_registration_get (wrk, p[0]);
121 vcl_ct_registration_lookup_add (vcl_worker_t * wrk, uword mq_addr,
124 hash_set (wrk->ct_registration_by_mq, mq_addr, ctr_index);
128 vcl_ct_registration_lookup_del (vcl_worker_t * wrk, uword mq_addr)
130 hash_unset (wrk->ct_registration_by_mq, mq_addr);
134 vcl_ct_registration_del (vcl_worker_t * wrk,
135 vcl_cut_through_registration_t * ctr)
137 pool_put (wrk->cut_through_registrations, ctr);
141 vcl_mq_evt_conn_alloc (vcl_worker_t * wrk)
143 vcl_mq_evt_conn_t *mqc;
144 pool_get (wrk->mq_evt_conns, mqc);
145 memset (mqc, 0, sizeof (*mqc));
150 vcl_mq_evt_conn_index (vcl_worker_t * wrk, vcl_mq_evt_conn_t * mqc)
152 return (mqc - wrk->mq_evt_conns);
156 vcl_mq_evt_conn_get (vcl_worker_t * wrk, u32 mq_conn_idx)
158 return pool_elt_at_index (wrk->mq_evt_conns, mq_conn_idx);
162 vcl_mq_epoll_add_evfd (vcl_worker_t * wrk, svm_msg_q_t * mq)
164 struct epoll_event e = { 0 };
165 vcl_mq_evt_conn_t *mqc;
169 mq_fd = svm_msg_q_get_consumer_eventfd (mq);
171 if (wrk->mqs_epfd < 0 || mq_fd == -1)
174 mqc = vcl_mq_evt_conn_alloc (wrk);
175 mqc_index = vcl_mq_evt_conn_index (wrk, mqc);
180 e.data.u32 = mqc_index;
181 if (epoll_ctl (wrk->mqs_epfd, EPOLL_CTL_ADD, mq_fd, &e) < 0)
183 clib_warning ("failed to add mq eventfd to mq epoll fd");
191 vcl_mq_epoll_del_evfd (vcl_worker_t * wrk, u32 mqc_index)
193 vcl_mq_evt_conn_t *mqc;
195 if (wrk->mqs_epfd || mqc_index == ~0)
198 mqc = vcl_mq_evt_conn_get (wrk, mqc_index);
199 if (epoll_ctl (wrk->mqs_epfd, EPOLL_CTL_DEL, mqc->mq_fd, 0) < 0)
201 clib_warning ("failed to del mq eventfd to mq epoll fd");
207 static vcl_worker_t *
208 vcl_worker_alloc (void)
211 pool_get (vcm->workers, wrk);
212 memset (wrk, 0, sizeof (*wrk));
213 wrk->wrk_index = wrk - vcm->workers;
214 wrk->forked_child = ~0;
219 vcl_worker_free (vcl_worker_t * wrk)
221 pool_put (vcm->workers, wrk);
225 vcl_worker_cleanup (vcl_worker_t * wrk, u8 notify_vpp)
227 clib_spinlock_lock (&vcm->workers_lock);
230 if (wrk->wrk_index == vcl_get_worker_index ())
231 vcl_send_app_worker_add_del (0 /* is_add */ );
233 vcl_send_child_worker_del (wrk);
235 if (wrk->mqs_epfd > 0)
236 close (wrk->mqs_epfd);
237 hash_free (wrk->session_index_by_vpp_handles);
238 hash_free (wrk->ct_registration_by_mq);
239 clib_spinlock_free (&wrk->ct_registration_lock);
240 vec_free (wrk->mq_events);
241 vec_free (wrk->mq_msg_vector);
242 vcl_worker_free (wrk);
243 clib_spinlock_unlock (&vcm->workers_lock);
247 vcl_worker_cleanup_cb (void *arg)
249 vcl_worker_t *wrk = vcl_worker_get_current ();
250 u32 wrk_index = wrk->wrk_index;
251 vcl_worker_cleanup (wrk, 1 /* notify vpp */ );
252 vcl_set_worker_index (~0);
253 VDBG (0, "cleaned up worker %u", wrk_index);
257 vcl_worker_alloc_and_init ()
261 /* This was initialized already */
262 if (vcl_get_worker_index () != ~0)
265 if (pool_elts (vcm->workers) == vcm->cfg.max_workers)
267 VDBG (0, "max-workers %u limit reached", vcm->cfg.max_workers);
271 clib_spinlock_lock (&vcm->workers_lock);
272 wrk = vcl_worker_alloc ();
273 vcl_set_worker_index (wrk->wrk_index);
274 wrk->thread_id = pthread_self ();
275 wrk->current_pid = getpid ();
278 if (vcm->cfg.use_mq_eventfd)
280 wrk->mqs_epfd = epoll_create (1);
281 if (wrk->mqs_epfd < 0)
283 clib_unix_warning ("epoll_create() returned");
288 wrk->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
289 wrk->ct_registration_by_mq = hash_create (0, sizeof (uword));
290 clib_spinlock_init (&wrk->ct_registration_lock);
291 clib_time_init (&wrk->clib_time);
292 vec_validate (wrk->mq_events, 64);
293 vec_validate (wrk->mq_msg_vector, 128);
294 vec_reset_length (wrk->mq_msg_vector);
295 vec_validate (wrk->unhandled_evts_vector, 128);
296 vec_reset_length (wrk->unhandled_evts_vector);
297 clib_spinlock_unlock (&vcm->workers_lock);
304 vcl_worker_register_with_vpp (void)
306 vcl_worker_t *wrk = vcl_worker_get_current ();
308 clib_spinlock_lock (&vcm->workers_lock);
310 vcm->app_state = STATE_APP_ADDING_WORKER;
311 vcl_send_app_worker_add_del (1 /* is_add */ );
312 if (vcl_wait_for_app_state_change (STATE_APP_READY))
314 clib_warning ("failed to add worker to vpp");
317 if (pthread_key_create (&vcl_worker_stop_key, vcl_worker_cleanup_cb))
318 clib_warning ("failed to add pthread cleanup function");
319 if (pthread_setspecific (vcl_worker_stop_key, &wrk->thread_id))
320 clib_warning ("failed to setup key value");
322 clib_spinlock_unlock (&vcm->workers_lock);
324 VDBG (0, "added worker %u", wrk->wrk_index);
329 vcl_worker_set_bapi (void)
331 vcl_worker_t *wrk = vcl_worker_get_current ();
334 /* Find the first worker with the same pid */
335 for (i = 0; i < vec_len (vcm->workers); i++)
337 if (i == wrk->wrk_index)
339 if (vcm->workers[i].current_pid == wrk->current_pid)
341 wrk->vl_input_queue = vcm->workers[i].vl_input_queue;
342 wrk->my_client_index = vcm->workers[i].my_client_index;
349 vcl_shared_session_t *
350 vcl_shared_session_alloc (void)
352 vcl_shared_session_t *ss;
353 pool_get (vcm->shared_sessions, ss);
354 memset (ss, 0, sizeof (*ss));
355 ss->ss_index = ss - vcm->shared_sessions;
359 vcl_shared_session_t *
360 vcl_shared_session_get (u32 ss_index)
362 if (pool_is_free_index (vcm->shared_sessions, ss_index))
364 return pool_elt_at_index (vcm->shared_sessions, ss_index);
368 vcl_shared_session_free (vcl_shared_session_t * ss)
370 pool_put (vcm->shared_sessions, ss);
374 vcl_worker_share_session (vcl_worker_t * parent, vcl_worker_t * wrk,
375 vcl_session_t * new_s)
377 vcl_shared_session_t *ss;
380 s = vcl_session_get (parent, new_s->session_index);
381 if (s->shared_index == ~0)
383 ss = vcl_shared_session_alloc ();
384 vec_add1 (ss->workers, parent->wrk_index);
385 s->shared_index = ss->ss_index;
389 ss = vcl_shared_session_get (s->shared_index);
391 new_s->shared_index = ss->ss_index;
392 vec_add1 (ss->workers, wrk->wrk_index);
396 vcl_worker_unshare_session (vcl_worker_t * wrk, vcl_session_t * s)
398 vcl_shared_session_t *ss;
401 ss = vcl_shared_session_get (s->shared_index);
402 for (i = 0; i < vec_len (ss->workers); i++)
404 if (ss->workers[i] == wrk->wrk_index)
406 vec_del1 (ss->workers, i);
411 if (vec_len (ss->workers) == 0)
413 vcl_shared_session_free (ss);
421 vcl_worker_share_sessions (vcl_worker_t * parent_wrk)
423 vcl_session_t *new_s;
426 if (!parent_wrk->sessions)
429 wrk = vcl_worker_get_current ();
430 wrk->sessions = pool_dup (parent_wrk->sessions);
431 wrk->session_index_by_vpp_handles =
432 hash_dup (parent_wrk->session_index_by_vpp_handles);
435 pool_foreach (new_s, wrk->sessions, ({
436 vcl_worker_share_session (parent_wrk, wrk, new_s);
442 vcl_session_get_refcnt (vcl_session_t * s)
444 vcl_shared_session_t *ss;
445 ss = vcl_shared_session_get (s->shared_index);
447 return vec_len (ss->workers);
452 vcl_segment_table_add (u64 segment_handle, u32 svm_segment_index)
454 clib_rwlock_writer_lock (&vcm->segment_table_lock);
455 hash_set (vcm->segment_table, segment_handle, svm_segment_index);
456 clib_rwlock_writer_unlock (&vcm->segment_table_lock);
460 vcl_segment_table_lookup (u64 segment_handle)
464 clib_rwlock_reader_lock (&vcm->segment_table_lock);
465 seg_indexp = hash_get (vcm->segment_table, segment_handle);
466 clib_rwlock_reader_unlock (&vcm->segment_table_lock);
469 return VCL_INVALID_SEGMENT_INDEX;
470 return ((u32) * seg_indexp);
474 vcl_segment_table_del (u64 segment_handle)
476 clib_rwlock_writer_lock (&vcm->segment_table_lock);
477 hash_unset (vcm->segment_table, segment_handle);
478 clib_rwlock_writer_unlock (&vcm->segment_table_lock);
482 * fd.io coding-style-patch-verification: ON
485 * eval: (c-set-style "gnu")