vcl: fix fifo sharing
[vpp.git] / src / vcl / vcl_private.c
1 /*
2  * Copyright (c) 2018-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 <vcl/vcl_private.h>
17
18 static pthread_key_t vcl_worker_stop_key;
19
20 vcl_mq_evt_conn_t *
21 vcl_mq_evt_conn_alloc (vcl_worker_t * wrk)
22 {
23   vcl_mq_evt_conn_t *mqc;
24   pool_get (wrk->mq_evt_conns, mqc);
25   memset (mqc, 0, sizeof (*mqc));
26   return mqc;
27 }
28
29 u32
30 vcl_mq_evt_conn_index (vcl_worker_t * wrk, vcl_mq_evt_conn_t * mqc)
31 {
32   return (mqc - wrk->mq_evt_conns);
33 }
34
35 vcl_mq_evt_conn_t *
36 vcl_mq_evt_conn_get (vcl_worker_t * wrk, u32 mq_conn_idx)
37 {
38   return pool_elt_at_index (wrk->mq_evt_conns, mq_conn_idx);
39 }
40
41 int
42 vcl_mq_epoll_add_evfd (vcl_worker_t * wrk, svm_msg_q_t * mq)
43 {
44   struct epoll_event e = { 0 };
45   vcl_mq_evt_conn_t *mqc;
46   u32 mqc_index;
47   int mq_fd;
48
49   mq_fd = svm_msg_q_get_eventfd (mq);
50
51   if (wrk->mqs_epfd < 0 || mq_fd == -1)
52     return -1;
53
54   mqc = vcl_mq_evt_conn_alloc (wrk);
55   mqc_index = vcl_mq_evt_conn_index (wrk, mqc);
56   mqc->mq_fd = mq_fd;
57   mqc->mq = mq;
58
59   e.events = EPOLLIN;
60   e.data.u32 = mqc_index;
61   if (epoll_ctl (wrk->mqs_epfd, EPOLL_CTL_ADD, mq_fd, &e) < 0)
62     {
63       VDBG (0, "failed to add mq eventfd to mq epoll fd");
64       return -1;
65     }
66
67   return mqc_index;
68 }
69
70 int
71 vcl_mq_epoll_del_evfd (vcl_worker_t * wrk, u32 mqc_index)
72 {
73   vcl_mq_evt_conn_t *mqc;
74
75   if (wrk->mqs_epfd || mqc_index == ~0)
76     return -1;
77
78   mqc = vcl_mq_evt_conn_get (wrk, mqc_index);
79   if (epoll_ctl (wrk->mqs_epfd, EPOLL_CTL_DEL, mqc->mq_fd, 0) < 0)
80     {
81       VDBG (0, "failed to del mq eventfd to mq epoll fd");
82       return -1;
83     }
84   return 0;
85 }
86
87 static vcl_worker_t *
88 vcl_worker_alloc (void)
89 {
90   vcl_worker_t *wrk;
91   pool_get (vcm->workers, wrk);
92   memset (wrk, 0, sizeof (*wrk));
93   wrk->wrk_index = wrk - vcm->workers;
94   wrk->forked_child = ~0;
95   return wrk;
96 }
97
98 static void
99 vcl_worker_free (vcl_worker_t * wrk)
100 {
101   pool_put (vcm->workers, wrk);
102 }
103
104 int
105 vcl_api_app_worker_add (void)
106 {
107   if (vcm->cfg.vpp_app_socket_api)
108     return vcl_sapi_app_worker_add ();
109
110   return vcl_bapi_app_worker_add ();
111 }
112
113 void
114 vcl_api_app_worker_del (vcl_worker_t * wrk)
115 {
116   if (vcm->cfg.vpp_app_socket_api)
117     return vcl_sapi_app_worker_del (wrk);
118
119   vcl_bapi_app_worker_del (wrk);
120 }
121
122 void
123 vcl_worker_cleanup (vcl_worker_t * wrk, u8 notify_vpp)
124 {
125   clib_spinlock_lock (&vcm->workers_lock);
126   if (notify_vpp)
127     vcl_api_app_worker_del (wrk);
128
129   if (wrk->mqs_epfd > 0)
130     close (wrk->mqs_epfd);
131   pool_free (wrk->sessions);
132   pool_free (wrk->mq_evt_conns);
133   hash_free (wrk->session_index_by_vpp_handles);
134   vec_free (wrk->mq_events);
135   vec_free (wrk->mq_msg_vector);
136   vec_free (wrk->ep_level_evts);
137   vec_free (wrk->ep_level_evts_fl);
138   vec_free (wrk->unhandled_evts_vector);
139   vec_free (wrk->pending_session_wrk_updates);
140   clib_bitmap_free (wrk->rd_bitmap);
141   clib_bitmap_free (wrk->wr_bitmap);
142   clib_bitmap_free (wrk->ex_bitmap);
143   vcl_worker_free (wrk);
144   clib_spinlock_unlock (&vcm->workers_lock);
145 }
146
147 static void
148 vcl_worker_cleanup_cb (void *arg)
149 {
150   vcl_worker_t *wrk;
151   u32 wrk_index;
152
153   wrk_index = vcl_get_worker_index ();
154   wrk = vcl_worker_get_if_valid (wrk_index);
155   if (!wrk)
156     return;
157
158   vcl_worker_cleanup (wrk, 1 /* notify vpp */ );
159   vcl_set_worker_index (~0);
160   VDBG (0, "cleaned up worker %u", wrk_index);
161 }
162
163 vcl_worker_t *
164 vcl_worker_alloc_and_init ()
165 {
166   vcl_worker_t *wrk;
167
168   /* This was initialized already */
169   if (vcl_get_worker_index () != ~0)
170     return 0;
171
172   /* Use separate heap map entry for worker */
173   clib_mem_set_thread_index ();
174
175   if (pool_elts (vcm->workers) == vcm->cfg.max_workers)
176     {
177       VDBG (0, "max-workers %u limit reached", vcm->cfg.max_workers);
178       return 0;
179     }
180
181   clib_spinlock_lock (&vcm->workers_lock);
182   wrk = vcl_worker_alloc ();
183   vcl_set_worker_index (wrk->wrk_index);
184   wrk->thread_id = pthread_self ();
185   wrk->current_pid = getpid ();
186
187   wrk->mqs_epfd = -1;
188   if (vcm->cfg.use_mq_eventfd)
189     {
190       wrk->vcl_needs_real_epoll = 1;
191       wrk->mqs_epfd = epoll_create (1);
192       wrk->vcl_needs_real_epoll = 0;
193       if (wrk->mqs_epfd < 0)
194         {
195           clib_unix_warning ("epoll_create() returned");
196           goto done;
197         }
198     }
199
200   wrk->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
201   clib_time_init (&wrk->clib_time);
202   vec_validate (wrk->mq_events, 64);
203   vec_validate (wrk->mq_msg_vector, 128);
204   vec_reset_length (wrk->mq_msg_vector);
205   vec_validate (wrk->unhandled_evts_vector, 128);
206   vec_reset_length (wrk->unhandled_evts_vector);
207   clib_spinlock_unlock (&vcm->workers_lock);
208
209 done:
210   return wrk;
211 }
212
213 int
214 vcl_worker_register_with_vpp (void)
215 {
216   vcl_worker_t *wrk = vcl_worker_get_current ();
217
218   clib_spinlock_lock (&vcm->workers_lock);
219
220   if (vcl_api_app_worker_add ())
221     {
222       VDBG (0, "failed to add worker to vpp");
223       clib_spinlock_unlock (&vcm->workers_lock);
224       return -1;
225     }
226   if (pthread_key_create (&vcl_worker_stop_key, vcl_worker_cleanup_cb))
227     VDBG (0, "failed to add pthread cleanup function");
228   if (pthread_setspecific (vcl_worker_stop_key, &wrk->thread_id))
229     VDBG (0, "failed to setup key value");
230
231   clib_spinlock_unlock (&vcm->workers_lock);
232
233   VDBG (0, "added worker %u", wrk->wrk_index);
234   return 0;
235 }
236
237 svm_msg_q_t *
238 vcl_worker_ctrl_mq (vcl_worker_t * wrk)
239 {
240   return wrk->ctrl_mq;
241 }
242
243 int
244 vcl_session_read_ready (vcl_session_t * s)
245 {
246   if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
247     {
248       VDBG (0, "ERROR: session %u: cannot read from an epoll session!",
249             s->session_index);
250       return VPPCOM_EBADFD;
251     }
252
253   if (vcl_session_is_open (s))
254     {
255       if (vcl_session_is_ct (s))
256         return svm_fifo_max_dequeue_cons (s->ct_rx_fifo);
257
258       if (s->is_dgram)
259         {
260           session_dgram_pre_hdr_t ph;
261           u32 max_deq;
262
263           max_deq = svm_fifo_max_dequeue_cons (s->rx_fifo);
264           if (max_deq <= SESSION_CONN_HDR_LEN)
265             return 0;
266           if (svm_fifo_peek (s->rx_fifo, 0, sizeof (ph), (u8 *) & ph) < 0)
267             return 0;
268           if (ph.data_length + SESSION_CONN_HDR_LEN > max_deq)
269             return 0;
270
271           return ph.data_length;
272         }
273
274       return svm_fifo_max_dequeue_cons (s->rx_fifo);
275     }
276   else if (s->session_state == VCL_STATE_LISTEN)
277     {
278       return clib_fifo_elts (s->accept_evts_fifo);
279     }
280   else
281     {
282       return (s->session_state == VCL_STATE_DISCONNECT) ?
283         VPPCOM_ECONNRESET : VPPCOM_ENOTCONN;
284     }
285 }
286
287 int
288 vcl_session_write_ready (vcl_session_t * s)
289 {
290   if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
291     {
292       VDBG (0, "session %u [0x%llx]: cannot write to an epoll session!",
293             s->session_index, s->vpp_handle);
294       return VPPCOM_EBADFD;
295     }
296
297   if (vcl_session_is_open (s))
298     {
299       if (vcl_session_is_ct (s))
300         return svm_fifo_max_enqueue_prod (s->ct_tx_fifo);
301
302       if (s->is_dgram)
303         {
304           u32 max_enq = svm_fifo_max_enqueue_prod (s->tx_fifo);
305
306           if (max_enq <= sizeof (session_dgram_hdr_t))
307             return 0;
308           return max_enq - sizeof (session_dgram_hdr_t);
309         }
310
311       return svm_fifo_max_enqueue_prod (s->tx_fifo);
312     }
313   else if (s->session_state == VCL_STATE_LISTEN)
314     {
315       if (s->tx_fifo)
316         return svm_fifo_max_enqueue_prod (s->tx_fifo);
317       else
318         return VPPCOM_EBADFD;
319     }
320   else
321     {
322       return (s->session_state == VCL_STATE_DISCONNECT) ?
323         VPPCOM_ECONNRESET : VPPCOM_ENOTCONN;
324     }
325 }
326
327 int
328 vcl_session_alloc_ext_cfg (vcl_session_t *s,
329                            transport_endpt_ext_cfg_type_t type, u32 len)
330 {
331   if (s->ext_config)
332     return -1;
333
334   s->ext_config = clib_mem_alloc (len);
335   clib_memset (s->ext_config, 0, len);
336   s->ext_config->len = len;
337   s->ext_config->type = type;
338
339   return 0;
340 }
341
342 int
343 vcl_segment_attach (u64 segment_handle, char *name, ssvm_segment_type_t type,
344                     int fd)
345 {
346   fifo_segment_create_args_t _a, *a = &_a;
347   int rv;
348
349   memset (a, 0, sizeof (*a));
350   a->segment_name = name;
351   a->segment_type = type;
352
353   if (type == SSVM_SEGMENT_MEMFD)
354     a->memfd_fd = fd;
355
356   clib_rwlock_writer_lock (&vcm->segment_table_lock);
357
358   if ((rv = fifo_segment_attach (&vcm->segment_main, a)))
359     {
360       clib_warning ("svm_fifo_segment_attach ('%s') failed", name);
361       return rv;
362     }
363   hash_set (vcm->segment_table, segment_handle, a->new_segment_indices[0]);
364
365   clib_rwlock_writer_unlock (&vcm->segment_table_lock);
366
367   vec_free (a->new_segment_indices);
368   return 0;
369 }
370
371 u32
372 vcl_segment_table_lookup (u64 segment_handle)
373 {
374   uword *seg_indexp;
375
376   clib_rwlock_reader_lock (&vcm->segment_table_lock);
377   seg_indexp = hash_get (vcm->segment_table, segment_handle);
378   clib_rwlock_reader_unlock (&vcm->segment_table_lock);
379
380   if (!seg_indexp)
381     return VCL_INVALID_SEGMENT_INDEX;
382   return ((u32) * seg_indexp);
383 }
384
385 void
386 vcl_segment_detach (u64 segment_handle)
387 {
388   fifo_segment_main_t *sm = &vcm->segment_main;
389   fifo_segment_t *segment;
390   u32 segment_index;
391
392   segment_index = vcl_segment_table_lookup (segment_handle);
393   if (segment_index == (u32) ~ 0)
394     return;
395
396   clib_rwlock_writer_lock (&vcm->segment_table_lock);
397
398   segment = fifo_segment_get_segment (sm, segment_index);
399   fifo_segment_delete (sm, segment);
400   hash_unset (vcm->segment_table, segment_handle);
401
402   clib_rwlock_writer_unlock (&vcm->segment_table_lock);
403
404   VDBG (0, "detached segment %u handle %u", segment_index, segment_handle);
405 }
406
407 int
408 vcl_segment_attach_session (uword segment_handle, uword rxf_offset,
409                             uword txf_offset, uword mq_offset, u8 is_ct,
410                             vcl_session_t *s)
411 {
412   u32 fs_index, eqs_index;
413   svm_fifo_t *rxf, *txf;
414   fifo_segment_t *fs;
415   u64 eqs_handle;
416
417   fs_index = vcl_segment_table_lookup (segment_handle);
418   if (fs_index == VCL_INVALID_SEGMENT_INDEX)
419     {
420       VDBG (0, "ERROR: segment for session %u is not mounted!",
421             s->session_index);
422       return -1;
423     }
424
425   if (!is_ct && mq_offset != (uword) ~0)
426     {
427       eqs_handle = vcl_vpp_worker_segment_handle (0);
428       eqs_index = vcl_segment_table_lookup (eqs_handle);
429       ASSERT (eqs_index != VCL_INVALID_SEGMENT_INDEX);
430     }
431
432   clib_rwlock_reader_lock (&vcm->segment_table_lock);
433
434   fs = fifo_segment_get_segment (&vcm->segment_main, fs_index);
435   rxf = fifo_segment_alloc_fifo_w_offset (fs, rxf_offset);
436   txf = fifo_segment_alloc_fifo_w_offset (fs, txf_offset);
437   rxf->segment_index = fs_index;
438   txf->segment_index = fs_index;
439
440   if (!is_ct && mq_offset != (uword) ~0)
441     {
442       fs = fifo_segment_get_segment (&vcm->segment_main, eqs_index);
443       s->vpp_evt_q =
444         fifo_segment_msg_q_attach (fs, mq_offset, rxf->shr->slice_index);
445     }
446
447   clib_rwlock_reader_unlock (&vcm->segment_table_lock);
448
449   if (!is_ct)
450     {
451       rxf->shr->client_session_index = s->session_index;
452       txf->shr->client_session_index = s->session_index;
453       rxf->client_thread_index = vcl_get_worker_index ();
454       txf->client_thread_index = vcl_get_worker_index ();
455       s->rx_fifo = rxf;
456       s->tx_fifo = txf;
457     }
458   else
459     {
460       s->ct_rx_fifo = rxf;
461       s->ct_tx_fifo = txf;
462     }
463
464   return 0;
465 }
466
467 void
468 vcl_session_detach_fifos (vcl_session_t *s)
469 {
470   fifo_segment_t *fs;
471
472   if (!s->rx_fifo)
473     return;
474
475   clib_rwlock_reader_lock (&vcm->segment_table_lock);
476
477   fs = fifo_segment_get_segment_if_valid (&vcm->segment_main,
478                                           s->rx_fifo->segment_index);
479   if (!fs)
480     goto done;
481
482   fifo_segment_free_client_fifo (fs, s->rx_fifo);
483   fifo_segment_free_client_fifo (fs, s->tx_fifo);
484   if (s->ct_rx_fifo)
485     {
486       fs = fifo_segment_get_segment_if_valid (&vcm->segment_main,
487                                               s->ct_rx_fifo->segment_index);
488       if (!fs)
489         goto done;
490
491       fifo_segment_free_client_fifo (fs, s->ct_rx_fifo);
492       fifo_segment_free_client_fifo (fs, s->ct_tx_fifo);
493     }
494
495 done:
496   clib_rwlock_reader_unlock (&vcm->segment_table_lock);
497 }
498
499 int
500 vcl_segment_attach_mq (uword segment_handle, uword mq_offset, u32 mq_index,
501                        svm_msg_q_t **mq)
502 {
503   fifo_segment_t *fs;
504   u32 fs_index;
505
506   fs_index = vcl_segment_table_lookup (segment_handle);
507   if (fs_index == VCL_INVALID_SEGMENT_INDEX)
508     {
509       VDBG (0, "ERROR: mq segment %lx for is not attached!", segment_handle);
510       return -1;
511     }
512
513   clib_rwlock_reader_lock (&vcm->segment_table_lock);
514
515   fs = fifo_segment_get_segment (&vcm->segment_main, fs_index);
516   *mq = fifo_segment_msg_q_attach (fs, mq_offset, mq_index);
517
518   clib_rwlock_reader_unlock (&vcm->segment_table_lock);
519
520   return 0;
521 }
522
523 int
524 vcl_segment_discover_mqs (uword segment_handle, int *fds, u32 n_fds)
525 {
526   fifo_segment_t *fs;
527   u32 fs_index;
528
529   fs_index = vcl_segment_table_lookup (segment_handle);
530   if (fs_index == VCL_INVALID_SEGMENT_INDEX)
531     {
532       VDBG (0, "ERROR: mq segment %lx for is not attached!", segment_handle);
533       return -1;
534     }
535
536   clib_rwlock_reader_lock (&vcm->segment_table_lock);
537
538   fs = fifo_segment_get_segment (&vcm->segment_main, fs_index);
539   fifo_segment_msg_qs_discover (fs, fds, n_fds);
540
541   clib_rwlock_reader_unlock (&vcm->segment_table_lock);
542
543   return 0;
544 }
545
546 svm_fifo_chunk_t *
547 vcl_segment_alloc_chunk (uword segment_handle, u32 slice_index, u32 size,
548                          uword *offset)
549 {
550   svm_fifo_chunk_t *c;
551   fifo_segment_t *fs;
552   u32 fs_index;
553
554   fs_index = vcl_segment_table_lookup (segment_handle);
555   if (fs_index == VCL_INVALID_SEGMENT_INDEX)
556     {
557       VDBG (0, "ERROR: mq segment %lx for is not attached!", segment_handle);
558       return 0;
559     }
560
561   clib_rwlock_reader_lock (&vcm->segment_table_lock);
562
563   fs = fifo_segment_get_segment (&vcm->segment_main, fs_index);
564   c = fifo_segment_alloc_chunk_w_slice (fs, slice_index, size);
565   *offset = fifo_segment_chunk_offset (fs, c);
566
567   clib_rwlock_reader_unlock (&vcm->segment_table_lock);
568
569   return c;
570 }
571
572 int
573 vcl_session_share_fifos (vcl_session_t *s, svm_fifo_t *rxf, svm_fifo_t *txf)
574 {
575   vcl_worker_t *wrk = vcl_worker_get_current ();
576   fifo_segment_t *fs;
577
578   clib_rwlock_reader_lock (&vcm->segment_table_lock);
579
580   fs = fifo_segment_get_segment (&vcm->segment_main, rxf->segment_index);
581   s->rx_fifo = fifo_segment_duplicate_fifo (fs, rxf);
582   s->tx_fifo = fifo_segment_duplicate_fifo (fs, txf);
583
584   clib_rwlock_reader_unlock (&vcm->segment_table_lock);
585
586   svm_fifo_add_subscriber (s->rx_fifo, wrk->vpp_wrk_index);
587   svm_fifo_add_subscriber (s->tx_fifo, wrk->vpp_wrk_index);
588
589   return 0;
590 }
591
592 /*
593  * fd.io coding-style-patch-verification: ON
594  *
595  * Local Variables:
596  * eval: (c-set-style "gnu")
597  * End:
598  */