2 * Copyright (c) 2018-2019 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <vcl/vcl_private.h>
18 static pthread_key_t vcl_worker_stop_key;
21 vcl_mq_evt_conn_alloc (vcl_worker_t * wrk)
23 vcl_mq_evt_conn_t *mqc;
24 pool_get (wrk->mq_evt_conns, mqc);
25 memset (mqc, 0, sizeof (*mqc));
30 vcl_mq_evt_conn_index (vcl_worker_t * wrk, vcl_mq_evt_conn_t * mqc)
32 return (mqc - wrk->mq_evt_conns);
36 vcl_mq_evt_conn_get (vcl_worker_t * wrk, u32 mq_conn_idx)
38 return pool_elt_at_index (wrk->mq_evt_conns, mq_conn_idx);
42 vcl_mq_epoll_add_evfd (vcl_worker_t * wrk, svm_msg_q_t * mq)
44 struct epoll_event e = { 0 };
45 vcl_mq_evt_conn_t *mqc;
49 mq_fd = svm_msg_q_get_eventfd (mq);
51 if (wrk->mqs_epfd < 0 || mq_fd == -1)
54 mqc = vcl_mq_evt_conn_alloc (wrk);
55 mqc_index = vcl_mq_evt_conn_index (wrk, mqc);
60 e.data.u32 = mqc_index;
61 if (epoll_ctl (wrk->mqs_epfd, EPOLL_CTL_ADD, mq_fd, &e) < 0)
63 VDBG (0, "failed to add mq eventfd to mq epoll fd");
71 vcl_mq_epoll_del_evfd (vcl_worker_t * wrk, u32 mqc_index)
73 vcl_mq_evt_conn_t *mqc;
75 if (wrk->mqs_epfd || mqc_index == ~0)
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)
81 VDBG (0, "failed to del mq eventfd to mq epoll fd");
88 vcl_worker_alloc (void)
91 pool_get (vcm->workers, wrk);
92 memset (wrk, 0, sizeof (*wrk));
93 wrk->wrk_index = wrk - vcm->workers;
94 wrk->forked_child = ~0;
99 vcl_worker_free (vcl_worker_t * wrk)
101 pool_put (vcm->workers, wrk);
105 vcl_api_app_worker_add (void)
107 if (vcm->cfg.vpp_app_socket_api)
108 return vcl_sapi_app_worker_add ();
110 return vcl_bapi_app_worker_add ();
114 vcl_api_app_worker_del (vcl_worker_t * wrk)
116 if (vcm->cfg.vpp_app_socket_api)
117 return vcl_sapi_app_worker_del (wrk);
119 vcl_bapi_app_worker_del (wrk);
123 vcl_worker_cleanup (vcl_worker_t * wrk, u8 notify_vpp)
125 clib_spinlock_lock (&vcm->workers_lock);
127 vcl_api_app_worker_del (wrk);
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->unhandled_evts_vector);
137 vec_free (wrk->pending_session_wrk_updates);
138 clib_bitmap_free (wrk->rd_bitmap);
139 clib_bitmap_free (wrk->wr_bitmap);
140 clib_bitmap_free (wrk->ex_bitmap);
141 vcl_worker_free (wrk);
142 clib_spinlock_unlock (&vcm->workers_lock);
146 vcl_worker_cleanup_cb (void *arg)
151 wrk_index = vcl_get_worker_index ();
152 wrk = vcl_worker_get_if_valid (wrk_index);
156 vcl_worker_cleanup (wrk, 1 /* notify vpp */ );
157 vcl_set_worker_index (~0);
158 VDBG (0, "cleaned up worker %u", wrk_index);
162 vcl_worker_alloc_and_init ()
166 /* This was initialized already */
167 if (vcl_get_worker_index () != ~0)
170 /* Use separate heap map entry for worker */
171 clib_mem_set_thread_index ();
173 if (pool_elts (vcm->workers) == vcm->cfg.max_workers)
175 VDBG (0, "max-workers %u limit reached", vcm->cfg.max_workers);
179 clib_spinlock_lock (&vcm->workers_lock);
180 wrk = vcl_worker_alloc ();
181 vcl_set_worker_index (wrk->wrk_index);
182 wrk->thread_id = pthread_self ();
183 wrk->current_pid = getpid ();
186 if (vcm->cfg.use_mq_eventfd)
188 wrk->vcl_needs_real_epoll = 1;
189 wrk->mqs_epfd = epoll_create (1);
190 wrk->vcl_needs_real_epoll = 0;
191 if (wrk->mqs_epfd < 0)
193 clib_unix_warning ("epoll_create() returned");
198 wrk->ep_lt_current = VCL_INVALID_SESSION_INDEX;
199 wrk->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
200 clib_time_init (&wrk->clib_time);
201 vec_validate (wrk->mq_events, 64);
202 vec_validate (wrk->mq_msg_vector, 128);
203 vec_reset_length (wrk->mq_msg_vector);
204 vec_validate (wrk->unhandled_evts_vector, 128);
205 vec_reset_length (wrk->unhandled_evts_vector);
206 clib_spinlock_unlock (&vcm->workers_lock);
213 vcl_worker_register_with_vpp (void)
215 vcl_worker_t *wrk = vcl_worker_get_current ();
217 clib_spinlock_lock (&vcm->workers_lock);
219 if (vcl_api_app_worker_add ())
221 VDBG (0, "failed to add worker to vpp");
222 clib_spinlock_unlock (&vcm->workers_lock);
225 if (pthread_key_create (&vcl_worker_stop_key, vcl_worker_cleanup_cb))
226 VDBG (0, "failed to add pthread cleanup function");
227 if (pthread_setspecific (vcl_worker_stop_key, &wrk->thread_id))
228 VDBG (0, "failed to setup key value");
230 clib_spinlock_unlock (&vcm->workers_lock);
232 VDBG (0, "added worker %u", wrk->wrk_index);
237 vcl_worker_ctrl_mq (vcl_worker_t * wrk)
243 vcl_session_read_ready (vcl_session_t * s)
245 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
247 VDBG (0, "ERROR: session %u: cannot read from an epoll session!",
249 return VPPCOM_EBADFD;
252 if (vcl_session_is_open (s))
254 if (vcl_session_is_ct (s))
255 return svm_fifo_max_dequeue_cons (s->ct_rx_fifo);
259 session_dgram_pre_hdr_t ph;
262 max_deq = svm_fifo_max_dequeue_cons (s->rx_fifo);
263 if (max_deq <= SESSION_CONN_HDR_LEN)
265 if (svm_fifo_peek (s->rx_fifo, 0, sizeof (ph), (u8 *) & ph) < 0)
267 if (ph.data_length + SESSION_CONN_HDR_LEN > max_deq)
270 return ph.data_length;
273 return svm_fifo_max_dequeue_cons (s->rx_fifo);
275 else if (s->session_state == VCL_STATE_LISTEN)
277 return clib_fifo_elts (s->accept_evts_fifo);
281 return (s->session_state == VCL_STATE_DISCONNECT) ?
282 VPPCOM_ECONNRESET : VPPCOM_ENOTCONN;
287 vcl_session_write_ready (vcl_session_t * s)
289 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
291 VDBG (0, "session %u [0x%llx]: cannot write to an epoll session!",
292 s->session_index, s->vpp_handle);
293 return VPPCOM_EBADFD;
296 if (vcl_session_is_open (s))
298 if (vcl_session_is_ct (s))
299 return svm_fifo_max_enqueue_prod (s->ct_tx_fifo);
303 u32 max_enq = svm_fifo_max_enqueue_prod (s->tx_fifo);
305 if (max_enq <= sizeof (session_dgram_hdr_t))
307 return max_enq - sizeof (session_dgram_hdr_t);
310 return svm_fifo_max_enqueue_prod (s->tx_fifo);
312 else if (s->session_state == VCL_STATE_LISTEN)
315 return svm_fifo_max_enqueue_prod (s->tx_fifo);
317 return VPPCOM_EBADFD;
319 else if (s->session_state == VCL_STATE_UPDATED)
325 return (s->session_state == VCL_STATE_DISCONNECT) ?
326 VPPCOM_ECONNRESET : VPPCOM_ENOTCONN;
331 vcl_session_alloc_ext_cfg (vcl_session_t *s,
332 transport_endpt_ext_cfg_type_t type, u32 len)
337 s->ext_config = clib_mem_alloc (len);
338 clib_memset (s->ext_config, 0, len);
339 s->ext_config->len = len;
340 s->ext_config->type = type;
346 vcl_segment_attach (u64 segment_handle, char *name, ssvm_segment_type_t type,
349 fifo_segment_create_args_t _a, *a = &_a;
352 memset (a, 0, sizeof (*a));
353 a->segment_name = name;
354 a->segment_type = type;
356 if (type == SSVM_SEGMENT_MEMFD)
359 clib_rwlock_writer_lock (&vcm->segment_table_lock);
361 if ((rv = fifo_segment_attach (&vcm->segment_main, a)))
363 clib_warning ("svm_fifo_segment_attach ('%s') failed", name);
366 hash_set (vcm->segment_table, segment_handle, a->new_segment_indices[0]);
368 clib_rwlock_writer_unlock (&vcm->segment_table_lock);
370 vec_free (a->new_segment_indices);
375 vcl_segment_table_lookup (u64 segment_handle)
379 clib_rwlock_reader_lock (&vcm->segment_table_lock);
380 seg_indexp = hash_get (vcm->segment_table, segment_handle);
381 clib_rwlock_reader_unlock (&vcm->segment_table_lock);
384 return VCL_INVALID_SEGMENT_INDEX;
385 return ((u32) * seg_indexp);
389 vcl_segment_detach (u64 segment_handle)
391 fifo_segment_main_t *sm = &vcm->segment_main;
392 fifo_segment_t *segment;
395 segment_index = vcl_segment_table_lookup (segment_handle);
396 if (segment_index == (u32) ~ 0)
399 clib_rwlock_writer_lock (&vcm->segment_table_lock);
401 segment = fifo_segment_get_segment (sm, segment_index);
402 fifo_segment_delete (sm, segment);
403 hash_unset (vcm->segment_table, segment_handle);
405 clib_rwlock_writer_unlock (&vcm->segment_table_lock);
407 VDBG (0, "detached segment %u handle %u", segment_index, segment_handle);
411 vcl_segment_attach_session (uword segment_handle, uword rxf_offset,
412 uword txf_offset, uword mq_offset, u32 mq_index,
413 u8 is_ct, vcl_session_t *s)
415 u32 fs_index, eqs_index;
416 svm_fifo_t *rxf, *txf;
420 fs_index = vcl_segment_table_lookup (segment_handle);
421 if (fs_index == VCL_INVALID_SEGMENT_INDEX)
423 VDBG (0, "ERROR: segment for session %u is not mounted!",
428 if (!is_ct && mq_offset != (uword) ~0)
430 eqs_handle = vcl_vpp_worker_segment_handle (0);
431 eqs_index = vcl_segment_table_lookup (eqs_handle);
432 ASSERT (eqs_index != VCL_INVALID_SEGMENT_INDEX);
435 clib_rwlock_reader_lock (&vcm->segment_table_lock);
437 fs = fifo_segment_get_segment (&vcm->segment_main, fs_index);
438 rxf = fifo_segment_alloc_fifo_w_offset (fs, rxf_offset);
439 txf = fifo_segment_alloc_fifo_w_offset (fs, txf_offset);
440 rxf->segment_index = fs_index;
441 txf->segment_index = fs_index;
443 if (!is_ct && mq_offset != (uword) ~0)
445 fs = fifo_segment_get_segment (&vcm->segment_main, eqs_index);
446 s->vpp_evt_q = fifo_segment_msg_q_attach (fs, mq_offset, mq_index);
449 clib_rwlock_reader_unlock (&vcm->segment_table_lock);
453 rxf->shr->client_session_index = s->session_index;
454 txf->shr->client_session_index = s->session_index;
455 rxf->client_thread_index = vcl_get_worker_index ();
456 txf->client_thread_index = vcl_get_worker_index ();
470 vcl_session_detach_fifos (vcl_session_t *s)
477 clib_rwlock_reader_lock (&vcm->segment_table_lock);
479 fs = fifo_segment_get_segment_if_valid (&vcm->segment_main,
480 s->rx_fifo->segment_index);
484 fifo_segment_free_client_fifo (fs, s->rx_fifo);
485 fifo_segment_free_client_fifo (fs, s->tx_fifo);
488 fs = fifo_segment_get_segment_if_valid (&vcm->segment_main,
489 s->ct_rx_fifo->segment_index);
493 fifo_segment_free_client_fifo (fs, s->ct_rx_fifo);
494 fifo_segment_free_client_fifo (fs, s->ct_tx_fifo);
498 clib_rwlock_reader_unlock (&vcm->segment_table_lock);
502 vcl_segment_attach_mq (uword segment_handle, uword mq_offset, u32 mq_index,
508 fs_index = vcl_segment_table_lookup (segment_handle);
509 if (fs_index == VCL_INVALID_SEGMENT_INDEX)
511 VDBG (0, "ERROR: mq segment %lx for is not attached!", segment_handle);
515 clib_rwlock_reader_lock (&vcm->segment_table_lock);
517 fs = fifo_segment_get_segment (&vcm->segment_main, fs_index);
518 *mq = fifo_segment_msg_q_attach (fs, mq_offset, mq_index);
520 clib_rwlock_reader_unlock (&vcm->segment_table_lock);
526 vcl_segment_discover_mqs (uword segment_handle, int *fds, u32 n_fds)
531 fs_index = vcl_segment_table_lookup (segment_handle);
532 if (fs_index == VCL_INVALID_SEGMENT_INDEX)
534 VDBG (0, "ERROR: mq segment %lx for is not attached!", segment_handle);
538 clib_rwlock_reader_lock (&vcm->segment_table_lock);
540 fs = fifo_segment_get_segment (&vcm->segment_main, fs_index);
541 fifo_segment_msg_qs_discover (fs, fds, n_fds);
543 clib_rwlock_reader_unlock (&vcm->segment_table_lock);
549 vcl_segment_alloc_chunk (uword segment_handle, u32 slice_index, u32 size,
556 fs_index = vcl_segment_table_lookup (segment_handle);
557 if (fs_index == VCL_INVALID_SEGMENT_INDEX)
559 VDBG (0, "ERROR: mq segment %lx for is not attached!", segment_handle);
563 clib_rwlock_reader_lock (&vcm->segment_table_lock);
565 fs = fifo_segment_get_segment (&vcm->segment_main, fs_index);
566 c = fifo_segment_alloc_chunk_w_slice (fs, slice_index, size);
567 *offset = fifo_segment_chunk_offset (fs, c);
569 clib_rwlock_reader_unlock (&vcm->segment_table_lock);
575 vcl_session_share_fifos (vcl_session_t *s, svm_fifo_t *rxf, svm_fifo_t *txf)
577 vcl_worker_t *wrk = vcl_worker_get_current ();
580 clib_rwlock_reader_lock (&vcm->segment_table_lock);
582 fs = fifo_segment_get_segment (&vcm->segment_main, rxf->segment_index);
583 s->rx_fifo = fifo_segment_duplicate_fifo (fs, rxf);
584 s->tx_fifo = fifo_segment_duplicate_fifo (fs, txf);
586 clib_rwlock_reader_unlock (&vcm->segment_table_lock);
588 svm_fifo_add_subscriber (s->rx_fifo, wrk->vpp_wrk_index);
589 svm_fifo_add_subscriber (s->tx_fifo, wrk->vpp_wrk_index);
595 * fd.io coding-style-patch-verification: ON
598 * eval: (c-set-style "gnu")