vcl: basic support for epoll level-triggered evts
[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   hash_free (wrk->session_index_by_vpp_handles);
132   vec_free (wrk->mq_events);
133   vec_free (wrk->mq_msg_vector);
134   vec_free (wrk->ep_level_evts);
135   vec_free (wrk->ep_level_evts_fl);
136   vcl_worker_free (wrk);
137   clib_spinlock_unlock (&vcm->workers_lock);
138 }
139
140 static void
141 vcl_worker_cleanup_cb (void *arg)
142 {
143   vcl_worker_t *wrk;
144   u32 wrk_index;
145
146   wrk_index = vcl_get_worker_index ();
147   wrk = vcl_worker_get_if_valid (wrk_index);
148   if (!wrk)
149     return;
150
151   vcl_worker_cleanup (wrk, 1 /* notify vpp */ );
152   vcl_set_worker_index (~0);
153   VDBG (0, "cleaned up worker %u", wrk_index);
154 }
155
156 vcl_worker_t *
157 vcl_worker_alloc_and_init ()
158 {
159   vcl_worker_t *wrk;
160
161   /* This was initialized already */
162   if (vcl_get_worker_index () != ~0)
163     return 0;
164
165   /* Use separate heap map entry for worker */
166   clib_mem_set_thread_index ();
167
168   if (pool_elts (vcm->workers) == vcm->cfg.max_workers)
169     {
170       VDBG (0, "max-workers %u limit reached", vcm->cfg.max_workers);
171       return 0;
172     }
173
174   clib_spinlock_lock (&vcm->workers_lock);
175   wrk = vcl_worker_alloc ();
176   vcl_set_worker_index (wrk->wrk_index);
177   wrk->thread_id = pthread_self ();
178   wrk->current_pid = getpid ();
179
180   wrk->mqs_epfd = -1;
181   if (vcm->cfg.use_mq_eventfd)
182     {
183       wrk->vcl_needs_real_epoll = 1;
184       wrk->mqs_epfd = epoll_create (1);
185       wrk->vcl_needs_real_epoll = 0;
186       if (wrk->mqs_epfd < 0)
187         {
188           clib_unix_warning ("epoll_create() returned");
189           goto done;
190         }
191     }
192
193   wrk->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
194   clib_time_init (&wrk->clib_time);
195   vec_validate (wrk->mq_events, 64);
196   vec_validate (wrk->mq_msg_vector, 128);
197   vec_reset_length (wrk->mq_msg_vector);
198   vec_validate (wrk->unhandled_evts_vector, 128);
199   vec_reset_length (wrk->unhandled_evts_vector);
200   clib_spinlock_unlock (&vcm->workers_lock);
201
202 done:
203   return wrk;
204 }
205
206 int
207 vcl_worker_register_with_vpp (void)
208 {
209   vcl_worker_t *wrk = vcl_worker_get_current ();
210
211   clib_spinlock_lock (&vcm->workers_lock);
212
213   if (vcl_api_app_worker_add ())
214     {
215       VDBG (0, "failed to add worker to vpp");
216       clib_spinlock_unlock (&vcm->workers_lock);
217       return -1;
218     }
219   if (pthread_key_create (&vcl_worker_stop_key, vcl_worker_cleanup_cb))
220     VDBG (0, "failed to add pthread cleanup function");
221   if (pthread_setspecific (vcl_worker_stop_key, &wrk->thread_id))
222     VDBG (0, "failed to setup key value");
223
224   clib_spinlock_unlock (&vcm->workers_lock);
225
226   VDBG (0, "added worker %u", wrk->wrk_index);
227   return 0;
228 }
229
230 svm_msg_q_t *
231 vcl_worker_ctrl_mq (vcl_worker_t * wrk)
232 {
233   return wrk->ctrl_mq;
234 }
235
236 int
237 vcl_session_read_ready (vcl_session_t * s)
238 {
239   if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
240     {
241       VDBG (0, "ERROR: session %u: cannot read from an epoll session!",
242             s->session_index);
243       return VPPCOM_EBADFD;
244     }
245
246   if (vcl_session_is_open (s))
247     {
248       if (vcl_session_is_ct (s))
249         return svm_fifo_max_dequeue_cons (s->ct_rx_fifo);
250
251       if (s->is_dgram)
252         {
253           session_dgram_pre_hdr_t ph;
254           u32 max_deq;
255
256           max_deq = svm_fifo_max_dequeue_cons (s->rx_fifo);
257           if (max_deq <= SESSION_CONN_HDR_LEN)
258             return 0;
259           if (svm_fifo_peek (s->rx_fifo, 0, sizeof (ph), (u8 *) & ph) < 0)
260             return 0;
261           if (ph.data_length + SESSION_CONN_HDR_LEN > max_deq)
262             return 0;
263
264           return ph.data_length;
265         }
266
267       return svm_fifo_max_dequeue_cons (s->rx_fifo);
268     }
269   else if (s->session_state == VCL_STATE_LISTEN)
270     {
271       return clib_fifo_elts (s->accept_evts_fifo);
272     }
273   else
274     {
275       return (s->session_state == VCL_STATE_DISCONNECT) ?
276         VPPCOM_ECONNRESET : VPPCOM_ENOTCONN;
277     }
278 }
279
280 int
281 vcl_session_write_ready (vcl_session_t * s)
282 {
283   if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
284     {
285       VDBG (0, "session %u [0x%llx]: cannot write to an epoll session!",
286             s->session_index, s->vpp_handle);
287       return VPPCOM_EBADFD;
288     }
289
290   if (vcl_session_is_open (s))
291     {
292       if (vcl_session_is_ct (s))
293         return svm_fifo_max_enqueue_prod (s->ct_tx_fifo);
294
295       if (s->is_dgram)
296         {
297           u32 max_enq = svm_fifo_max_enqueue_prod (s->tx_fifo);
298
299           if (max_enq <= sizeof (session_dgram_hdr_t))
300             return 0;
301           return max_enq - sizeof (session_dgram_hdr_t);
302         }
303
304       return svm_fifo_max_enqueue_prod (s->tx_fifo);
305     }
306   else if (s->session_state == VCL_STATE_LISTEN)
307     {
308       if (s->tx_fifo)
309         return svm_fifo_max_enqueue_prod (s->tx_fifo);
310       else
311         return VPPCOM_EBADFD;
312     }
313   else
314     {
315       return (s->session_state == VCL_STATE_DISCONNECT) ?
316         VPPCOM_ECONNRESET : VPPCOM_ENOTCONN;
317     }
318 }
319
320 int
321 vcl_session_alloc_ext_cfg (vcl_session_t *s,
322                            transport_endpt_ext_cfg_type_t type, u32 len)
323 {
324   if (s->ext_config)
325     return -1;
326
327   s->ext_config = clib_mem_alloc (len);
328   clib_memset (s->ext_config, 0, len);
329   s->ext_config->len = len;
330   s->ext_config->type = type;
331
332   return 0;
333 }
334
335 int
336 vcl_segment_attach (u64 segment_handle, char *name, ssvm_segment_type_t type,
337                     int fd)
338 {
339   fifo_segment_create_args_t _a, *a = &_a;
340   int rv;
341
342   memset (a, 0, sizeof (*a));
343   a->segment_name = name;
344   a->segment_type = type;
345
346   if (type == SSVM_SEGMENT_MEMFD)
347     a->memfd_fd = fd;
348
349   clib_rwlock_writer_lock (&vcm->segment_table_lock);
350
351   if ((rv = fifo_segment_attach (&vcm->segment_main, a)))
352     {
353       clib_warning ("svm_fifo_segment_attach ('%s') failed", name);
354       return rv;
355     }
356   hash_set (vcm->segment_table, segment_handle, a->new_segment_indices[0]);
357
358   clib_rwlock_writer_unlock (&vcm->segment_table_lock);
359
360   vec_free (a->new_segment_indices);
361   return 0;
362 }
363
364 u32
365 vcl_segment_table_lookup (u64 segment_handle)
366 {
367   uword *seg_indexp;
368
369   clib_rwlock_reader_lock (&vcm->segment_table_lock);
370   seg_indexp = hash_get (vcm->segment_table, segment_handle);
371   clib_rwlock_reader_unlock (&vcm->segment_table_lock);
372
373   if (!seg_indexp)
374     return VCL_INVALID_SEGMENT_INDEX;
375   return ((u32) * seg_indexp);
376 }
377
378 void
379 vcl_segment_detach (u64 segment_handle)
380 {
381   fifo_segment_main_t *sm = &vcm->segment_main;
382   fifo_segment_t *segment;
383   u32 segment_index;
384
385   segment_index = vcl_segment_table_lookup (segment_handle);
386   if (segment_index == (u32) ~ 0)
387     return;
388
389   clib_rwlock_writer_lock (&vcm->segment_table_lock);
390
391   segment = fifo_segment_get_segment (sm, segment_index);
392   fifo_segment_delete (sm, segment);
393   hash_unset (vcm->segment_table, segment_handle);
394
395   clib_rwlock_writer_unlock (&vcm->segment_table_lock);
396
397   VDBG (0, "detached segment %u handle %u", segment_index, segment_handle);
398 }
399
400 int
401 vcl_segment_attach_session (uword segment_handle, uword rxf_offset,
402                             uword txf_offset, uword mq_offset, u8 is_ct,
403                             vcl_session_t *s)
404 {
405   u32 fs_index, eqs_index;
406   svm_fifo_t *rxf, *txf;
407   fifo_segment_t *fs;
408   u64 eqs_handle;
409
410   fs_index = vcl_segment_table_lookup (segment_handle);
411   if (fs_index == VCL_INVALID_SEGMENT_INDEX)
412     {
413       VDBG (0, "ERROR: segment for session %u is not mounted!",
414             s->session_index);
415       return -1;
416     }
417
418   if (!is_ct && mq_offset != (uword) ~0)
419     {
420       eqs_handle = vcl_vpp_worker_segment_handle (0);
421       eqs_index = vcl_segment_table_lookup (eqs_handle);
422       ASSERT (eqs_index != VCL_INVALID_SEGMENT_INDEX);
423     }
424
425   clib_rwlock_reader_lock (&vcm->segment_table_lock);
426
427   fs = fifo_segment_get_segment (&vcm->segment_main, fs_index);
428   rxf = fifo_segment_alloc_fifo_w_offset (fs, rxf_offset);
429   txf = fifo_segment_alloc_fifo_w_offset (fs, txf_offset);
430   rxf->segment_index = fs_index;
431   txf->segment_index = fs_index;
432
433   if (!is_ct && mq_offset != (uword) ~0)
434     {
435       fs = fifo_segment_get_segment (&vcm->segment_main, eqs_index);
436       s->vpp_evt_q =
437         fifo_segment_msg_q_attach (fs, mq_offset, rxf->shr->slice_index);
438     }
439
440   clib_rwlock_reader_unlock (&vcm->segment_table_lock);
441
442   if (!is_ct)
443     {
444       rxf->shr->client_session_index = s->session_index;
445       txf->shr->client_session_index = s->session_index;
446       rxf->client_thread_index = vcl_get_worker_index ();
447       txf->client_thread_index = vcl_get_worker_index ();
448       s->rx_fifo = rxf;
449       s->tx_fifo = txf;
450     }
451   else
452     {
453       s->ct_rx_fifo = rxf;
454       s->ct_tx_fifo = txf;
455     }
456
457   return 0;
458 }
459
460 void
461 vcl_session_detach_fifos (vcl_session_t *s)
462 {
463   fifo_segment_t *fs;
464
465   if (!s->rx_fifo)
466     return;
467
468   clib_rwlock_reader_lock (&vcm->segment_table_lock);
469
470   fs = fifo_segment_get_segment_if_valid (&vcm->segment_main,
471                                           s->rx_fifo->segment_index);
472   if (!fs)
473     goto done;
474
475   fifo_segment_free_client_fifo (fs, s->rx_fifo);
476   fifo_segment_free_client_fifo (fs, s->tx_fifo);
477   if (s->ct_rx_fifo)
478     {
479       fs = fifo_segment_get_segment_if_valid (&vcm->segment_main,
480                                               s->ct_rx_fifo->segment_index);
481       if (!fs)
482         goto done;
483
484       fifo_segment_free_client_fifo (fs, s->ct_rx_fifo);
485       fifo_segment_free_client_fifo (fs, s->ct_tx_fifo);
486     }
487
488 done:
489   clib_rwlock_reader_unlock (&vcm->segment_table_lock);
490 }
491
492 int
493 vcl_segment_attach_mq (uword segment_handle, uword mq_offset, u32 mq_index,
494                        svm_msg_q_t **mq)
495 {
496   fifo_segment_t *fs;
497   u32 fs_index;
498
499   fs_index = vcl_segment_table_lookup (segment_handle);
500   if (fs_index == VCL_INVALID_SEGMENT_INDEX)
501     {
502       VDBG (0, "ERROR: mq segment %lx for is not attached!", segment_handle);
503       return -1;
504     }
505
506   clib_rwlock_reader_lock (&vcm->segment_table_lock);
507
508   fs = fifo_segment_get_segment (&vcm->segment_main, fs_index);
509   *mq = fifo_segment_msg_q_attach (fs, mq_offset, mq_index);
510
511   clib_rwlock_reader_unlock (&vcm->segment_table_lock);
512
513   return 0;
514 }
515
516 int
517 vcl_segment_discover_mqs (uword segment_handle, int *fds, u32 n_fds)
518 {
519   fifo_segment_t *fs;
520   u32 fs_index;
521
522   fs_index = vcl_segment_table_lookup (segment_handle);
523   if (fs_index == VCL_INVALID_SEGMENT_INDEX)
524     {
525       VDBG (0, "ERROR: mq segment %lx for is not attached!", segment_handle);
526       return -1;
527     }
528
529   clib_rwlock_reader_lock (&vcm->segment_table_lock);
530
531   fs = fifo_segment_get_segment (&vcm->segment_main, fs_index);
532   fifo_segment_msg_qs_discover (fs, fds, n_fds);
533
534   clib_rwlock_reader_unlock (&vcm->segment_table_lock);
535
536   return 0;
537 }
538
539 svm_fifo_chunk_t *
540 vcl_segment_alloc_chunk (uword segment_handle, u32 slice_index, u32 size,
541                          uword *offset)
542 {
543   svm_fifo_chunk_t *c;
544   fifo_segment_t *fs;
545   u32 fs_index;
546
547   fs_index = vcl_segment_table_lookup (segment_handle);
548   if (fs_index == VCL_INVALID_SEGMENT_INDEX)
549     {
550       VDBG (0, "ERROR: mq segment %lx for is not attached!", segment_handle);
551       return 0;
552     }
553
554   clib_rwlock_reader_lock (&vcm->segment_table_lock);
555
556   fs = fifo_segment_get_segment (&vcm->segment_main, fs_index);
557   c = fifo_segment_alloc_chunk_w_slice (fs, slice_index, size);
558   *offset = fifo_segment_chunk_offset (fs, c);
559
560   clib_rwlock_reader_unlock (&vcm->segment_table_lock);
561
562   return c;
563 }
564
565 /*
566  * fd.io coding-style-patch-verification: ON
567  *
568  * Local Variables:
569  * eval: (c-set-style "gnu")
570  * End:
571  */