blob: 673b91d45765eb7ca523c4e0a0a77ff2d97792a5 [file] [log] [blame]
Florin Coras99368312018-08-02 10:45:44 -07001/*
2 * Copyright (c) 2018 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 <vcl/vcl_private.h>
17
Florin Coras3348a4c2018-09-07 17:09:35 -070018static pthread_key_t vcl_worker_stop_key;
Florin Coras134a9962018-08-28 11:32:04 -070019
20static const char *
21vppcom_app_state_str (app_state_t state)
22{
23 char *st;
24
25 switch (state)
26 {
27 case STATE_APP_START:
28 st = "STATE_APP_START";
29 break;
30
31 case STATE_APP_CONN_VPP:
32 st = "STATE_APP_CONN_VPP";
33 break;
34
35 case STATE_APP_ENABLED:
36 st = "STATE_APP_ENABLED";
37 break;
38
39 case STATE_APP_ATTACHED:
40 st = "STATE_APP_ATTACHED";
41 break;
42
43 default:
44 st = "UNKNOWN_APP_STATE";
45 break;
46 }
47
48 return st;
49}
50
51int
52vcl_wait_for_app_state_change (app_state_t app_state)
53{
54 vcl_worker_t *wrk = vcl_worker_get_current ();
55 f64 timeout = clib_time_now (&wrk->clib_time) + vcm->cfg.app_timeout;
56
57 while (clib_time_now (&wrk->clib_time) < timeout)
58 {
59 if (vcm->app_state == app_state)
60 return VPPCOM_OK;
61 if (vcm->app_state == STATE_APP_FAILED)
62 return VPPCOM_ECONNABORTED;
63 }
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);
67
68 return VPPCOM_ETIMEDOUT;
69}
70
Florin Coras99368312018-08-02 10:45:44 -070071vcl_cut_through_registration_t *
Florin Coras134a9962018-08-28 11:32:04 -070072vcl_ct_registration_lock_and_alloc (vcl_worker_t * wrk)
Florin Coras99368312018-08-02 10:45:44 -070073{
74 vcl_cut_through_registration_t *cr;
Florin Coras134a9962018-08-28 11:32:04 -070075 clib_spinlock_lock (&wrk->ct_registration_lock);
76 pool_get (wrk->cut_through_registrations, cr);
Florin Coras99368312018-08-02 10:45:44 -070077 memset (cr, 0, sizeof (*cr));
78 cr->epoll_evt_conn_index = -1;
79 return cr;
80}
81
82u32
Florin Coras134a9962018-08-28 11:32:04 -070083vcl_ct_registration_index (vcl_worker_t * wrk,
84 vcl_cut_through_registration_t * ctr)
Florin Coras99368312018-08-02 10:45:44 -070085{
Florin Coras134a9962018-08-28 11:32:04 -070086 return (ctr - wrk->cut_through_registrations);
Florin Coras99368312018-08-02 10:45:44 -070087}
88
89void
Florin Coras134a9962018-08-28 11:32:04 -070090vcl_ct_registration_lock (vcl_worker_t * wrk)
Florin Coras99368312018-08-02 10:45:44 -070091{
Florin Coras134a9962018-08-28 11:32:04 -070092 clib_spinlock_lock (&wrk->ct_registration_lock);
93}
94
95void
96vcl_ct_registration_unlock (vcl_worker_t * wrk)
97{
98 clib_spinlock_unlock (&wrk->ct_registration_lock);
Florin Coras99368312018-08-02 10:45:44 -070099}
100
101vcl_cut_through_registration_t *
Florin Coras134a9962018-08-28 11:32:04 -0700102vcl_ct_registration_get (vcl_worker_t * wrk, u32 ctr_index)
Florin Coras99368312018-08-02 10:45:44 -0700103{
Florin Coras134a9962018-08-28 11:32:04 -0700104 if (pool_is_free_index (wrk->cut_through_registrations, ctr_index))
Florin Coras99368312018-08-02 10:45:44 -0700105 return 0;
Florin Coras134a9962018-08-28 11:32:04 -0700106 return pool_elt_at_index (wrk->cut_through_registrations, ctr_index);
Florin Coras99368312018-08-02 10:45:44 -0700107}
108
109vcl_cut_through_registration_t *
Florin Coras134a9962018-08-28 11:32:04 -0700110vcl_ct_registration_lock_and_lookup (vcl_worker_t * wrk, uword mq_addr)
Florin Coras99368312018-08-02 10:45:44 -0700111{
112 uword *p;
Florin Coras134a9962018-08-28 11:32:04 -0700113 clib_spinlock_lock (&wrk->ct_registration_lock);
114 p = hash_get (wrk->ct_registration_by_mq, mq_addr);
Florin Coras99368312018-08-02 10:45:44 -0700115 if (!p)
116 return 0;
Florin Coras134a9962018-08-28 11:32:04 -0700117 return vcl_ct_registration_get (wrk, p[0]);
Florin Coras99368312018-08-02 10:45:44 -0700118}
119
120void
Florin Coras134a9962018-08-28 11:32:04 -0700121vcl_ct_registration_lookup_add (vcl_worker_t * wrk, uword mq_addr,
122 u32 ctr_index)
Florin Coras99368312018-08-02 10:45:44 -0700123{
Florin Coras134a9962018-08-28 11:32:04 -0700124 hash_set (wrk->ct_registration_by_mq, mq_addr, ctr_index);
Florin Coras99368312018-08-02 10:45:44 -0700125}
126
127void
Florin Coras134a9962018-08-28 11:32:04 -0700128vcl_ct_registration_lookup_del (vcl_worker_t * wrk, uword mq_addr)
Florin Coras99368312018-08-02 10:45:44 -0700129{
Florin Coras134a9962018-08-28 11:32:04 -0700130 hash_unset (wrk->ct_registration_by_mq, mq_addr);
Florin Coras99368312018-08-02 10:45:44 -0700131}
132
133void
Florin Coras134a9962018-08-28 11:32:04 -0700134vcl_ct_registration_del (vcl_worker_t * wrk,
135 vcl_cut_through_registration_t * ctr)
Florin Coras99368312018-08-02 10:45:44 -0700136{
Florin Coras134a9962018-08-28 11:32:04 -0700137 pool_put (wrk->cut_through_registrations, ctr);
Florin Coras99368312018-08-02 10:45:44 -0700138}
139
140vcl_mq_evt_conn_t *
Florin Coras134a9962018-08-28 11:32:04 -0700141vcl_mq_evt_conn_alloc (vcl_worker_t * wrk)
Florin Coras99368312018-08-02 10:45:44 -0700142{
143 vcl_mq_evt_conn_t *mqc;
Florin Coras134a9962018-08-28 11:32:04 -0700144 pool_get (wrk->mq_evt_conns, mqc);
Florin Coras99368312018-08-02 10:45:44 -0700145 memset (mqc, 0, sizeof (*mqc));
146 return mqc;
147}
148
149u32
Florin Coras134a9962018-08-28 11:32:04 -0700150vcl_mq_evt_conn_index (vcl_worker_t * wrk, vcl_mq_evt_conn_t * mqc)
Florin Coras99368312018-08-02 10:45:44 -0700151{
Florin Coras134a9962018-08-28 11:32:04 -0700152 return (mqc - wrk->mq_evt_conns);
Florin Coras99368312018-08-02 10:45:44 -0700153}
154
155vcl_mq_evt_conn_t *
Florin Coras134a9962018-08-28 11:32:04 -0700156vcl_mq_evt_conn_get (vcl_worker_t * wrk, u32 mq_conn_idx)
Florin Coras99368312018-08-02 10:45:44 -0700157{
Florin Coras134a9962018-08-28 11:32:04 -0700158 return pool_elt_at_index (wrk->mq_evt_conns, mq_conn_idx);
Florin Coras99368312018-08-02 10:45:44 -0700159}
160
161int
Florin Coras134a9962018-08-28 11:32:04 -0700162vcl_mq_epoll_add_evfd (vcl_worker_t * wrk, svm_msg_q_t * mq)
Florin Coras99368312018-08-02 10:45:44 -0700163{
164 struct epoll_event e = { 0 };
165 vcl_mq_evt_conn_t *mqc;
166 u32 mqc_index;
167 int mq_fd;
168
169 mq_fd = svm_msg_q_get_consumer_eventfd (mq);
170
Florin Coras134a9962018-08-28 11:32:04 -0700171 if (wrk->mqs_epfd < 0 || mq_fd == -1)
Florin Coras99368312018-08-02 10:45:44 -0700172 return -1;
173
Florin Coras134a9962018-08-28 11:32:04 -0700174 mqc = vcl_mq_evt_conn_alloc (wrk);
175 mqc_index = vcl_mq_evt_conn_index (wrk, mqc);
Florin Coras99368312018-08-02 10:45:44 -0700176 mqc->mq_fd = mq_fd;
177 mqc->mq = mq;
178
179 e.events = EPOLLIN;
180 e.data.u32 = mqc_index;
Florin Coras134a9962018-08-28 11:32:04 -0700181 if (epoll_ctl (wrk->mqs_epfd, EPOLL_CTL_ADD, mq_fd, &e) < 0)
Florin Coras99368312018-08-02 10:45:44 -0700182 {
183 clib_warning ("failed to add mq eventfd to mq epoll fd");
184 return -1;
185 }
186
187 return mqc_index;
188}
189
190int
Florin Coras134a9962018-08-28 11:32:04 -0700191vcl_mq_epoll_del_evfd (vcl_worker_t * wrk, u32 mqc_index)
Florin Coras99368312018-08-02 10:45:44 -0700192{
193 vcl_mq_evt_conn_t *mqc;
194
Florin Coras134a9962018-08-28 11:32:04 -0700195 if (wrk->mqs_epfd || mqc_index == ~0)
Florin Coras99368312018-08-02 10:45:44 -0700196 return -1;
197
Florin Coras134a9962018-08-28 11:32:04 -0700198 mqc = vcl_mq_evt_conn_get (wrk, mqc_index);
199 if (epoll_ctl (wrk->mqs_epfd, EPOLL_CTL_DEL, mqc->mq_fd, 0) < 0)
Florin Coras99368312018-08-02 10:45:44 -0700200 {
201 clib_warning ("failed to del mq eventfd to mq epoll fd");
202 return -1;
203 }
204 return 0;
205}
206
Florin Coras134a9962018-08-28 11:32:04 -0700207static vcl_worker_t *
208vcl_worker_alloc (void)
209{
210 vcl_worker_t *wrk;
211 pool_get (vcm->workers, wrk);
212 memset (wrk, 0, sizeof (*wrk));
213 wrk->wrk_index = wrk - vcm->workers;
214 return wrk;
215}
216
217static void
218vcl_worker_free (vcl_worker_t * wrk)
219{
220 pool_put (vcm->workers, wrk);
221}
222
Florin Coras47c40e22018-11-26 17:01:36 -0800223void
Florin Coras940f78f2018-11-30 12:11:20 -0800224vcl_worker_cleanup (u8 notify_vpp)
Florin Coras134a9962018-08-28 11:32:04 -0700225{
226 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras3348a4c2018-09-07 17:09:35 -0700227
Florin Coras47c40e22018-11-26 17:01:36 -0800228 clib_spinlock_lock (&vcm->workers_lock);
Florin Coras940f78f2018-11-30 12:11:20 -0800229 if (notify_vpp)
230 vcl_send_app_worker_add_del (0 /* is_add */ );
231 if (wrk->mqs_epfd > 0)
232 close (wrk->mqs_epfd);
Florin Coras134a9962018-08-28 11:32:04 -0700233 hash_free (wrk->session_index_by_vpp_handles);
234 hash_free (wrk->ct_registration_by_mq);
235 clib_spinlock_free (&wrk->ct_registration_lock);
236 vec_free (wrk->mq_events);
237 vec_free (wrk->mq_msg_vector);
238 vcl_set_worker_index (~0);
239 vcl_worker_free (wrk);
Florin Coras47c40e22018-11-26 17:01:36 -0800240 clib_spinlock_unlock (&vcm->workers_lock);
Florin Coras47c40e22018-11-26 17:01:36 -0800241}
242
243static void
244vcl_worker_cleanup_cb (void *arg)
245{
Florin Coras940f78f2018-11-30 12:11:20 -0800246 u32 wrk_index = vcl_get_worker_index ();
247 vcl_worker_cleanup (1 /* notify vpp */ );
248 VDBG (0, "cleaned up worker %u", wrk_index);
Florin Coras134a9962018-08-28 11:32:04 -0700249}
250
251vcl_worker_t *
252vcl_worker_alloc_and_init ()
253{
254 vcl_worker_t *wrk;
255
256 /* This was initialized already */
257 if (vcl_get_worker_index () != ~0)
258 return 0;
259
Florin Corasde9f08b2018-09-07 14:32:58 -0700260 if (pool_elts (vcm->workers) == vcm->cfg.max_workers)
261 {
262 VDBG (0, "max-workers %u limit reached", vcm->cfg.max_workers);
263 return 0;
264 }
265
266 clib_spinlock_lock (&vcm->workers_lock);
Florin Coras134a9962018-08-28 11:32:04 -0700267 wrk = vcl_worker_alloc ();
268 vcl_set_worker_index (wrk->wrk_index);
Florin Coras47c40e22018-11-26 17:01:36 -0800269 wrk->thread_id = pthread_self ();
270 wrk->current_pid = getpid ();
Florin Coras134a9962018-08-28 11:32:04 -0700271
272 wrk->mqs_epfd = -1;
273 if (vcm->cfg.use_mq_eventfd)
274 {
275 wrk->mqs_epfd = epoll_create (1);
276 if (wrk->mqs_epfd < 0)
277 {
278 clib_unix_warning ("epoll_create() returned");
Florin Coras47c40e22018-11-26 17:01:36 -0800279 goto done;
Florin Coras134a9962018-08-28 11:32:04 -0700280 }
281 }
282
283 wrk->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
284 wrk->ct_registration_by_mq = hash_create (0, sizeof (uword));
285 clib_spinlock_init (&wrk->ct_registration_lock);
286 clib_time_init (&wrk->clib_time);
287 vec_validate (wrk->mq_events, 64);
288 vec_validate (wrk->mq_msg_vector, 128);
289 vec_reset_length (wrk->mq_msg_vector);
Florin Coras86f04502018-09-12 16:08:01 -0700290 vec_validate (wrk->unhandled_evts_vector, 128);
291 vec_reset_length (wrk->unhandled_evts_vector);
Florin Coras47c40e22018-11-26 17:01:36 -0800292 clib_spinlock_unlock (&vcm->workers_lock);
Florin Coras134a9962018-08-28 11:32:04 -0700293
Florin Coras47c40e22018-11-26 17:01:36 -0800294done:
295 return wrk;
296}
297
298int
299vcl_worker_register_with_vpp (void)
300{
301 vcl_worker_t *wrk = vcl_worker_get_current ();
302
303 clib_spinlock_lock (&vcm->workers_lock);
Florin Coras134a9962018-08-28 11:32:04 -0700304
Florin Coras134a9962018-08-28 11:32:04 -0700305 vcm->app_state = STATE_APP_ADDING_WORKER;
306 vcl_send_app_worker_add_del (1 /* is_add */ );
307 if (vcl_wait_for_app_state_change (STATE_APP_READY))
308 {
309 clib_warning ("failed to add worker to vpp");
Florin Coras47c40e22018-11-26 17:01:36 -0800310 return -1;
Florin Coras134a9962018-08-28 11:32:04 -0700311 }
312
Florin Coras47c40e22018-11-26 17:01:36 -0800313 if (pthread_key_create (&vcl_worker_stop_key, vcl_worker_cleanup_cb))
Florin Coras134a9962018-08-28 11:32:04 -0700314 clib_warning ("failed to add pthread cleanup function");
Florin Coras3348a4c2018-09-07 17:09:35 -0700315 if (pthread_setspecific (vcl_worker_stop_key, &wrk->thread_id))
316 clib_warning ("failed to setup key value");
Florin Coras134a9962018-08-28 11:32:04 -0700317
Florin Corasde9f08b2018-09-07 14:32:58 -0700318 clib_spinlock_unlock (&vcm->workers_lock);
319
Florin Coras134a9962018-08-28 11:32:04 -0700320 VDBG (0, "added worker %u", wrk->wrk_index);
Florin Coras47c40e22018-11-26 17:01:36 -0800321 return 0;
322}
Florin Coras134a9962018-08-28 11:32:04 -0700323
Florin Coras47c40e22018-11-26 17:01:36 -0800324int
325vcl_worker_set_bapi (void)
326{
327 vcl_worker_t *wrk = vcl_worker_get_current ();
328 int i;
329
330 /* Find the first worker with the same pid */
331 for (i = 0; i < vec_len (vcm->workers); i++)
332 {
333 if (i == wrk->wrk_index)
334 continue;
335 if (vcm->workers[i].current_pid == wrk->current_pid)
336 {
337 wrk->vl_input_queue = vcm->workers[i].vl_input_queue;
338 wrk->my_client_index = vcm->workers[i].my_client_index;
339 return 0;
340 }
341 }
342 return -1;
343}
344
345vcl_shared_session_t *
346vcl_shared_session_alloc (void)
347{
348 vcl_shared_session_t *ss;
349 pool_get (vcm->shared_sessions, ss);
350 memset (ss, 0, sizeof (*ss));
351 ss->ss_index = ss - vcm->shared_sessions;
352 return ss;
353}
354
355vcl_shared_session_t *
356vcl_shared_session_get (u32 ss_index)
357{
358 if (pool_is_free_index (vcm->shared_sessions, ss_index))
359 return 0;
360 return pool_elt_at_index (vcm->shared_sessions, ss_index);
361}
362
363void
364vcl_shared_session_free (vcl_shared_session_t * ss)
365{
366 pool_put (vcm->shared_sessions, ss);
367}
368
369void
370vcl_worker_share_session (vcl_worker_t * parent, vcl_worker_t * wrk,
371 vcl_session_t * new_s)
372{
373 vcl_shared_session_t *ss;
374 vcl_session_t *s;
375
376 s = vcl_session_get (parent, new_s->session_index);
377 if (s->shared_index == ~0)
378 {
379 ss = vcl_shared_session_alloc ();
380 vec_add1 (ss->workers, parent->wrk_index);
381 s->shared_index = ss->ss_index;
382 }
383 else
384 {
385 ss = vcl_shared_session_get (s->shared_index);
386 }
387 new_s->shared_index = ss->ss_index;
388 vec_add1 (ss->workers, wrk->wrk_index);
389}
390
391int
392vcl_worker_unshare_session (vcl_worker_t * wrk, vcl_session_t * s)
393{
394 vcl_shared_session_t *ss;
395 int i;
396
397 ss = vcl_shared_session_get (s->shared_index);
398 for (i = 0; i < vec_len (ss->workers); i++)
399 {
400 if (ss->workers[i] == wrk->wrk_index)
401 {
402 vec_del1 (ss->workers, i);
403 break;
404 }
405 }
406
407 if (vec_len (ss->workers) == 0)
408 {
409 vcl_shared_session_free (ss);
410 return 1;
411 }
412
413 return 0;
414}
415
416void
417vcl_worker_share_sessions (u32 parent_wrk_index)
418{
419 vcl_worker_t *parent_wrk, *wrk;
420 vcl_session_t *new_s;
421
422 parent_wrk = vcl_worker_get (parent_wrk_index);
423 if (!parent_wrk->sessions)
424 return;
425
426 wrk = vcl_worker_get_current ();
427 wrk->sessions = pool_dup (parent_wrk->sessions);
428 wrk->session_index_by_vpp_handles =
429 hash_dup (parent_wrk->session_index_by_vpp_handles);
430
431 /* *INDENT-OFF* */
432 pool_foreach (new_s, wrk->sessions, ({
433 vcl_worker_share_session (parent_wrk, wrk, new_s);
434 }));
435 /* *INDENT-ON* */
436}
437
438int
439vcl_session_get_refcnt (vcl_session_t * s)
440{
441 vcl_shared_session_t *ss;
442 ss = vcl_shared_session_get (s->shared_index);
443 if (ss)
444 return vec_len (ss->workers);
445 return 0;
Florin Coras134a9962018-08-28 11:32:04 -0700446}
447
Florin Corasd85de682018-11-29 17:02:29 -0800448void
449vcl_segment_table_add (u64 segment_handle, u32 svm_segment_index)
450{
451 clib_rwlock_writer_lock (&vcm->segment_table_lock);
452 hash_set (vcm->segment_table, segment_handle, svm_segment_index);
453 clib_rwlock_writer_unlock (&vcm->segment_table_lock);
454}
455
456u32
457vcl_segment_table_lookup (u64 segment_handle)
458{
459 uword *seg_indexp;
460
461 clib_rwlock_reader_lock (&vcm->segment_table_lock);
462 seg_indexp = hash_get (vcm->segment_table, segment_handle);
463 clib_rwlock_reader_unlock (&vcm->segment_table_lock);
464
465 if (!seg_indexp)
466 return VCL_INVALID_SEGMENT_INDEX;
467 return ((u32) * seg_indexp);
468}
469
470void
471vcl_segment_table_del (u64 segment_handle)
472{
473 clib_rwlock_writer_lock (&vcm->segment_table_lock);
474 hash_unset (vcm->segment_table, segment_handle);
475 clib_rwlock_writer_unlock (&vcm->segment_table_lock);
476}
477
Florin Coras99368312018-08-02 10:45:44 -0700478/*
479 * fd.io coding-style-patch-verification: ON
480 *
481 * Local Variables:
482 * eval: (c-set-style "gnu")
483 * End:
484 */