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_consumer_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 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);
139 vcl_worker_cleanup_cb (void *arg)
141 vcl_worker_t *wrk = vcl_worker_get_current ();
142 u32 wrk_index = wrk->wrk_index;
143 vcl_worker_cleanup (wrk, 1 /* notify vpp */ );
144 vcl_set_worker_index (~0);
145 VDBG (0, "cleaned up worker %u", wrk_index);
149 vcl_worker_alloc_and_init ()
153 /* This was initialized already */
154 if (vcl_get_worker_index () != ~0)
157 /* Use separate heap map entry for worker */
158 clib_mem_set_thread_index ();
160 if (pool_elts (vcm->workers) == vcm->cfg.max_workers)
162 VDBG (0, "max-workers %u limit reached", vcm->cfg.max_workers);
166 clib_spinlock_lock (&vcm->workers_lock);
167 wrk = vcl_worker_alloc ();
168 vcl_set_worker_index (wrk->wrk_index);
169 wrk->thread_id = pthread_self ();
170 wrk->current_pid = getpid ();
173 if (vcm->cfg.use_mq_eventfd)
175 wrk->vcl_needs_real_epoll = 1;
176 wrk->mqs_epfd = epoll_create (1);
177 wrk->vcl_needs_real_epoll = 0;
178 if (wrk->mqs_epfd < 0)
180 clib_unix_warning ("epoll_create() returned");
185 wrk->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
186 clib_time_init (&wrk->clib_time);
187 vec_validate (wrk->mq_events, 64);
188 vec_validate (wrk->mq_msg_vector, 128);
189 vec_reset_length (wrk->mq_msg_vector);
190 vec_validate (wrk->unhandled_evts_vector, 128);
191 vec_reset_length (wrk->unhandled_evts_vector);
192 clib_spinlock_unlock (&vcm->workers_lock);
199 vcl_worker_register_with_vpp (void)
201 vcl_worker_t *wrk = vcl_worker_get_current ();
203 clib_spinlock_lock (&vcm->workers_lock);
205 if (vcl_api_app_worker_add ())
207 VDBG (0, "failed to add worker to vpp");
208 clib_spinlock_unlock (&vcm->workers_lock);
211 if (pthread_key_create (&vcl_worker_stop_key, vcl_worker_cleanup_cb))
212 VDBG (0, "failed to add pthread cleanup function");
213 if (pthread_setspecific (vcl_worker_stop_key, &wrk->thread_id))
214 VDBG (0, "failed to setup key value");
216 clib_spinlock_unlock (&vcm->workers_lock);
218 VDBG (0, "added worker %u", wrk->wrk_index);
223 vcl_worker_ctrl_mq (vcl_worker_t * wrk)
229 vcl_session_read_ready (vcl_session_t * s)
231 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
233 VDBG (0, "ERROR: session %u: cannot read from an epoll session!",
235 return VPPCOM_EBADFD;
238 if (vcl_session_is_open (s))
240 if (vcl_session_is_ct (s))
241 return svm_fifo_max_dequeue_cons (s->ct_rx_fifo);
245 session_dgram_pre_hdr_t ph;
248 max_deq = svm_fifo_max_dequeue_cons (s->rx_fifo);
249 if (max_deq <= SESSION_CONN_HDR_LEN)
251 if (svm_fifo_peek (s->rx_fifo, 0, sizeof (ph), (u8 *) & ph) < 0)
253 if (ph.data_length + SESSION_CONN_HDR_LEN > max_deq)
256 return ph.data_length;
259 return svm_fifo_max_dequeue_cons (s->rx_fifo);
261 else if (s->session_state == VCL_STATE_LISTEN)
263 return clib_fifo_elts (s->accept_evts_fifo);
267 return (s->session_state == VCL_STATE_DISCONNECT) ?
268 VPPCOM_ECONNRESET : VPPCOM_ENOTCONN;
273 vcl_session_write_ready (vcl_session_t * s)
275 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
277 VDBG (0, "session %u [0x%llx]: cannot write to an epoll session!",
278 s->session_index, s->vpp_handle);
279 return VPPCOM_EBADFD;
282 if (vcl_session_is_open (s))
284 if (vcl_session_is_ct (s))
285 return svm_fifo_max_enqueue_prod (s->ct_tx_fifo);
289 u32 max_enq = svm_fifo_max_enqueue_prod (s->tx_fifo);
291 if (max_enq <= sizeof (session_dgram_hdr_t))
293 return max_enq - sizeof (session_dgram_hdr_t);
296 return svm_fifo_max_enqueue_prod (s->tx_fifo);
298 else if (s->session_state == VCL_STATE_LISTEN)
301 return svm_fifo_max_enqueue_prod (s->tx_fifo);
303 return VPPCOM_EBADFD;
307 return (s->session_state == VCL_STATE_DISCONNECT) ?
308 VPPCOM_ECONNRESET : VPPCOM_ENOTCONN;
313 vcl_segment_attach (u64 segment_handle, char *name, ssvm_segment_type_t type,
316 fifo_segment_create_args_t _a, *a = &_a;
319 memset (a, 0, sizeof (*a));
320 a->segment_name = name;
321 a->segment_type = type;
323 if (type == SSVM_SEGMENT_MEMFD)
326 clib_rwlock_writer_lock (&vcm->segment_table_lock);
328 if ((rv = fifo_segment_attach (&vcm->segment_main, a)))
330 clib_warning ("svm_fifo_segment_attach ('%s') failed", name);
333 hash_set (vcm->segment_table, segment_handle, a->new_segment_indices[0]);
335 clib_rwlock_writer_unlock (&vcm->segment_table_lock);
337 vec_reset_length (a->new_segment_indices);
342 vcl_segment_table_lookup (u64 segment_handle)
346 clib_rwlock_reader_lock (&vcm->segment_table_lock);
347 seg_indexp = hash_get (vcm->segment_table, segment_handle);
348 clib_rwlock_reader_unlock (&vcm->segment_table_lock);
351 return VCL_INVALID_SEGMENT_INDEX;
352 return ((u32) * seg_indexp);
356 vcl_segment_detach (u64 segment_handle)
358 fifo_segment_main_t *sm = &vcm->segment_main;
359 fifo_segment_t *segment;
362 segment_index = vcl_segment_table_lookup (segment_handle);
363 if (segment_index == (u32) ~ 0)
366 clib_rwlock_writer_lock (&vcm->segment_table_lock);
368 segment = fifo_segment_get_segment (sm, segment_index);
369 fifo_segment_delete (sm, segment);
370 hash_unset (vcm->segment_table, segment_handle);
372 clib_rwlock_writer_unlock (&vcm->segment_table_lock);
374 VDBG (0, "detached segment %u handle %u", segment_index, segment_handle);
378 vcl_segment_attach_session (uword segment_handle, uword rxf_offset,
379 uword txf_offset, uword mq_offset, u8 is_ct,
382 u32 fs_index, eqs_index;
383 svm_fifo_t *rxf, *txf;
387 fs_index = vcl_segment_table_lookup (segment_handle);
388 if (fs_index == VCL_INVALID_SEGMENT_INDEX)
390 VDBG (0, "ERROR: segment for session %u is not mounted!",
395 if (!is_ct && mq_offset != (uword) ~0)
397 eqs_handle = vcl_vpp_worker_segment_handle (0);
398 eqs_index = vcl_segment_table_lookup (eqs_handle);
399 ASSERT (eqs_index != VCL_INVALID_SEGMENT_INDEX);
402 clib_rwlock_reader_lock (&vcm->segment_table_lock);
404 fs = fifo_segment_get_segment (&vcm->segment_main, fs_index);
405 rxf = fifo_segment_alloc_fifo_w_offset (fs, rxf_offset);
406 txf = fifo_segment_alloc_fifo_w_offset (fs, txf_offset);
408 if (!is_ct && mq_offset != (uword) ~0)
410 fs = fifo_segment_get_segment (&vcm->segment_main, eqs_index);
412 fifo_segment_msg_q_attach (fs, mq_offset, rxf->shr->slice_index);
415 clib_rwlock_reader_unlock (&vcm->segment_table_lock);
419 rxf->shr->client_session_index = s->session_index;
420 txf->shr->client_session_index = s->session_index;
421 rxf->client_thread_index = vcl_get_worker_index ();
422 txf->client_thread_index = vcl_get_worker_index ();
436 vcl_segment_attach_mq (uword segment_handle, uword mq_offset, u32 mq_index,
442 fs_index = vcl_segment_table_lookup (segment_handle);
443 if (fs_index == VCL_INVALID_SEGMENT_INDEX)
445 VDBG (0, "ERROR: mq segment %lx for is not attached!", segment_handle);
449 clib_rwlock_reader_lock (&vcm->segment_table_lock);
451 fs = fifo_segment_get_segment (&vcm->segment_main, fs_index);
452 *mq = fifo_segment_msg_q_attach (fs, mq_offset, mq_index);
454 clib_rwlock_reader_unlock (&vcm->segment_table_lock);
460 * fd.io coding-style-patch-verification: ON
463 * eval: (c-set-style "gnu")