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