76ae0e7719fc05f907a1f5c4fbb69afec4a122a0
[vpp.git] / src / vcl / vcl_private.c
1 /*
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
18 static pthread_key_t vcl_worker_stop_key;
19
20 static const char *
21 vppcom_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
51 int
52 vcl_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
71 vcl_cut_through_registration_t *
72 vcl_ct_registration_lock_and_alloc (vcl_worker_t * wrk)
73 {
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;
79   return cr;
80 }
81
82 u32
83 vcl_ct_registration_index (vcl_worker_t * wrk,
84                            vcl_cut_through_registration_t * ctr)
85 {
86   return (ctr - wrk->cut_through_registrations);
87 }
88
89 void
90 vcl_ct_registration_lock (vcl_worker_t * wrk)
91 {
92   clib_spinlock_lock (&wrk->ct_registration_lock);
93 }
94
95 void
96 vcl_ct_registration_unlock (vcl_worker_t * wrk)
97 {
98   clib_spinlock_unlock (&wrk->ct_registration_lock);
99 }
100
101 vcl_cut_through_registration_t *
102 vcl_ct_registration_get (vcl_worker_t * wrk, u32 ctr_index)
103 {
104   if (pool_is_free_index (wrk->cut_through_registrations, ctr_index))
105     return 0;
106   return pool_elt_at_index (wrk->cut_through_registrations, ctr_index);
107 }
108
109 vcl_cut_through_registration_t *
110 vcl_ct_registration_lock_and_lookup (vcl_worker_t * wrk, uword mq_addr)
111 {
112   uword *p;
113   clib_spinlock_lock (&wrk->ct_registration_lock);
114   p = hash_get (wrk->ct_registration_by_mq, mq_addr);
115   if (!p)
116     return 0;
117   return vcl_ct_registration_get (wrk, p[0]);
118 }
119
120 void
121 vcl_ct_registration_lookup_add (vcl_worker_t * wrk, uword mq_addr,
122                                 u32 ctr_index)
123 {
124   hash_set (wrk->ct_registration_by_mq, mq_addr, ctr_index);
125 }
126
127 void
128 vcl_ct_registration_lookup_del (vcl_worker_t * wrk, uword mq_addr)
129 {
130   hash_unset (wrk->ct_registration_by_mq, mq_addr);
131 }
132
133 void
134 vcl_ct_registration_del (vcl_worker_t * wrk,
135                          vcl_cut_through_registration_t * ctr)
136 {
137   pool_put (wrk->cut_through_registrations, ctr);
138 }
139
140 vcl_mq_evt_conn_t *
141 vcl_mq_evt_conn_alloc (vcl_worker_t * wrk)
142 {
143   vcl_mq_evt_conn_t *mqc;
144   pool_get (wrk->mq_evt_conns, mqc);
145   memset (mqc, 0, sizeof (*mqc));
146   return mqc;
147 }
148
149 u32
150 vcl_mq_evt_conn_index (vcl_worker_t * wrk, vcl_mq_evt_conn_t * mqc)
151 {
152   return (mqc - wrk->mq_evt_conns);
153 }
154
155 vcl_mq_evt_conn_t *
156 vcl_mq_evt_conn_get (vcl_worker_t * wrk, u32 mq_conn_idx)
157 {
158   return pool_elt_at_index (wrk->mq_evt_conns, mq_conn_idx);
159 }
160
161 int
162 vcl_mq_epoll_add_evfd (vcl_worker_t * wrk, svm_msg_q_t * mq)
163 {
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
171   if (wrk->mqs_epfd < 0 || mq_fd == -1)
172     return -1;
173
174   mqc = vcl_mq_evt_conn_alloc (wrk);
175   mqc_index = vcl_mq_evt_conn_index (wrk, mqc);
176   mqc->mq_fd = mq_fd;
177   mqc->mq = mq;
178
179   e.events = EPOLLIN;
180   e.data.u32 = mqc_index;
181   if (epoll_ctl (wrk->mqs_epfd, EPOLL_CTL_ADD, mq_fd, &e) < 0)
182     {
183       clib_warning ("failed to add mq eventfd to mq epoll fd");
184       return -1;
185     }
186
187   return mqc_index;
188 }
189
190 int
191 vcl_mq_epoll_del_evfd (vcl_worker_t * wrk, u32 mqc_index)
192 {
193   vcl_mq_evt_conn_t *mqc;
194
195   if (wrk->mqs_epfd || mqc_index == ~0)
196     return -1;
197
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)
200     {
201       clib_warning ("failed to del mq eventfd to mq epoll fd");
202       return -1;
203     }
204   return 0;
205 }
206
207 static vcl_worker_t *
208 vcl_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
217 static void
218 vcl_worker_free (vcl_worker_t * wrk)
219 {
220   pool_put (vcm->workers, wrk);
221 }
222
223 void
224 vcl_worker_cleanup (void)
225 {
226   vcl_worker_t *wrk = vcl_worker_get_current ();
227
228   clib_spinlock_lock (&vcm->workers_lock);
229   vcl_send_app_worker_add_del (0 /* is_add */ );
230   close (wrk->mqs_epfd);
231   hash_free (wrk->session_index_by_vpp_handles);
232   hash_free (wrk->ct_registration_by_mq);
233   clib_spinlock_free (&wrk->ct_registration_lock);
234   vec_free (wrk->mq_events);
235   vec_free (wrk->mq_msg_vector);
236   vcl_set_worker_index (~0);
237   vcl_worker_free (wrk);
238   clib_spinlock_unlock (&vcm->workers_lock);
239   VDBG (0, "cleaned up worker %u", wrk->wrk_index);
240 }
241
242 static void
243 vcl_worker_cleanup_cb (void *arg)
244 {
245   vcl_worker_cleanup ();
246 }
247
248 vcl_worker_t *
249 vcl_worker_alloc_and_init ()
250 {
251   vcl_worker_t *wrk;
252
253   /* This was initialized already */
254   if (vcl_get_worker_index () != ~0)
255     return 0;
256
257   if (pool_elts (vcm->workers) == vcm->cfg.max_workers)
258     {
259       VDBG (0, "max-workers %u limit reached", vcm->cfg.max_workers);
260       return 0;
261     }
262
263   clib_spinlock_lock (&vcm->workers_lock);
264   wrk = vcl_worker_alloc ();
265   vcl_set_worker_index (wrk->wrk_index);
266   wrk->thread_id = pthread_self ();
267   wrk->current_pid = getpid ();
268
269   wrk->mqs_epfd = -1;
270   if (vcm->cfg.use_mq_eventfd)
271     {
272       wrk->mqs_epfd = epoll_create (1);
273       if (wrk->mqs_epfd < 0)
274         {
275           clib_unix_warning ("epoll_create() returned");
276           goto done;
277         }
278     }
279
280   wrk->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
281   wrk->ct_registration_by_mq = hash_create (0, sizeof (uword));
282   clib_spinlock_init (&wrk->ct_registration_lock);
283   clib_time_init (&wrk->clib_time);
284   vec_validate (wrk->mq_events, 64);
285   vec_validate (wrk->mq_msg_vector, 128);
286   vec_reset_length (wrk->mq_msg_vector);
287   vec_validate (wrk->unhandled_evts_vector, 128);
288   vec_reset_length (wrk->unhandled_evts_vector);
289   clib_spinlock_unlock (&vcm->workers_lock);
290
291 done:
292   return wrk;
293 }
294
295 int
296 vcl_worker_register_with_vpp (void)
297 {
298   vcl_worker_t *wrk = vcl_worker_get_current ();
299
300   clib_spinlock_lock (&vcm->workers_lock);
301
302   vcm->app_state = STATE_APP_ADDING_WORKER;
303   vcl_send_app_worker_add_del (1 /* is_add */ );
304   if (vcl_wait_for_app_state_change (STATE_APP_READY))
305     {
306       clib_warning ("failed to add worker to vpp");
307       return -1;
308     }
309
310   if (pthread_key_create (&vcl_worker_stop_key, vcl_worker_cleanup_cb))
311     clib_warning ("failed to add pthread cleanup function");
312   if (pthread_setspecific (vcl_worker_stop_key, &wrk->thread_id))
313     clib_warning ("failed to setup key value");
314
315   clib_spinlock_unlock (&vcm->workers_lock);
316
317   VDBG (0, "added worker %u", wrk->wrk_index);
318   return 0;
319 }
320
321 int
322 vcl_worker_set_bapi (void)
323 {
324   vcl_worker_t *wrk = vcl_worker_get_current ();
325   int i;
326
327   /* Find the first worker with the same pid */
328   for (i = 0; i < vec_len (vcm->workers); i++)
329     {
330       if (i == wrk->wrk_index)
331         continue;
332       if (vcm->workers[i].current_pid == wrk->current_pid)
333         {
334           wrk->vl_input_queue = vcm->workers[i].vl_input_queue;
335           wrk->my_client_index = vcm->workers[i].my_client_index;
336           return 0;
337         }
338     }
339   return -1;
340 }
341
342 vcl_shared_session_t *
343 vcl_shared_session_alloc (void)
344 {
345   vcl_shared_session_t *ss;
346   pool_get (vcm->shared_sessions, ss);
347   memset (ss, 0, sizeof (*ss));
348   ss->ss_index = ss - vcm->shared_sessions;
349   return ss;
350 }
351
352 vcl_shared_session_t *
353 vcl_shared_session_get (u32 ss_index)
354 {
355   if (pool_is_free_index (vcm->shared_sessions, ss_index))
356     return 0;
357   return pool_elt_at_index (vcm->shared_sessions, ss_index);
358 }
359
360 void
361 vcl_shared_session_free (vcl_shared_session_t * ss)
362 {
363   pool_put (vcm->shared_sessions, ss);
364 }
365
366 void
367 vcl_worker_share_session (vcl_worker_t * parent, vcl_worker_t * wrk,
368                           vcl_session_t * new_s)
369 {
370   vcl_shared_session_t *ss;
371   vcl_session_t *s;
372
373   s = vcl_session_get (parent, new_s->session_index);
374   if (s->shared_index == ~0)
375     {
376       ss = vcl_shared_session_alloc ();
377       vec_add1 (ss->workers, parent->wrk_index);
378       s->shared_index = ss->ss_index;
379     }
380   else
381     {
382       ss = vcl_shared_session_get (s->shared_index);
383     }
384   new_s->shared_index = ss->ss_index;
385   vec_add1 (ss->workers, wrk->wrk_index);
386 }
387
388 int
389 vcl_worker_unshare_session (vcl_worker_t * wrk, vcl_session_t * s)
390 {
391   vcl_shared_session_t *ss;
392   int i;
393
394   ss = vcl_shared_session_get (s->shared_index);
395   for (i = 0; i < vec_len (ss->workers); i++)
396     {
397       if (ss->workers[i] == wrk->wrk_index)
398         {
399           vec_del1 (ss->workers, i);
400           break;
401         }
402     }
403
404   if (vec_len (ss->workers) == 0)
405     {
406       vcl_shared_session_free (ss);
407       return 1;
408     }
409
410   return 0;
411 }
412
413 void
414 vcl_worker_share_sessions (u32 parent_wrk_index)
415 {
416   vcl_worker_t *parent_wrk, *wrk;
417   vcl_session_t *new_s;
418
419   parent_wrk = vcl_worker_get (parent_wrk_index);
420   if (!parent_wrk->sessions)
421     return;
422
423   wrk = vcl_worker_get_current ();
424   wrk->sessions = pool_dup (parent_wrk->sessions);
425   wrk->session_index_by_vpp_handles =
426     hash_dup (parent_wrk->session_index_by_vpp_handles);
427
428   /* *INDENT-OFF* */
429   pool_foreach (new_s, wrk->sessions, ({
430     vcl_worker_share_session (parent_wrk, wrk, new_s);
431   }));
432   /* *INDENT-ON* */
433 }
434
435 int
436 vcl_session_get_refcnt (vcl_session_t * s)
437 {
438   vcl_shared_session_t *ss;
439   ss = vcl_shared_session_get (s->shared_index);
440   if (ss)
441     return vec_len (ss->workers);
442   return 0;
443 }
444
445 void
446 vcl_segment_table_add (u64 segment_handle, u32 svm_segment_index)
447 {
448   clib_rwlock_writer_lock (&vcm->segment_table_lock);
449   hash_set (vcm->segment_table, segment_handle, svm_segment_index);
450   clib_rwlock_writer_unlock (&vcm->segment_table_lock);
451 }
452
453 u32
454 vcl_segment_table_lookup (u64 segment_handle)
455 {
456   uword *seg_indexp;
457
458   clib_rwlock_reader_lock (&vcm->segment_table_lock);
459   seg_indexp = hash_get (vcm->segment_table, segment_handle);
460   clib_rwlock_reader_unlock (&vcm->segment_table_lock);
461
462   if (!seg_indexp)
463     return VCL_INVALID_SEGMENT_INDEX;
464   return ((u32) * seg_indexp);
465 }
466
467 void
468 vcl_segment_table_del (u64 segment_handle)
469 {
470   clib_rwlock_writer_lock (&vcm->segment_table_lock);
471   hash_unset (vcm->segment_table, segment_handle);
472   clib_rwlock_writer_unlock (&vcm->segment_table_lock);
473 }
474
475 /*
476  * fd.io coding-style-patch-verification: ON
477  *
478  * Local Variables:
479  * eval: (c-set-style "gnu")
480  * End:
481  */