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_worker_cleanup (vcl_worker_t * wrk, u8 notify_vpp)
107 clib_spinlock_lock (&vcm->workers_lock);
109 vcl_bapi_app_worker_del (wrk);
111 if (wrk->mqs_epfd > 0)
112 close (wrk->mqs_epfd);
113 hash_free (wrk->session_index_by_vpp_handles);
114 vec_free (wrk->mq_events);
115 vec_free (wrk->mq_msg_vector);
116 vcl_worker_free (wrk);
117 clib_spinlock_unlock (&vcm->workers_lock);
121 vcl_worker_cleanup_cb (void *arg)
123 vcl_worker_t *wrk = vcl_worker_get_current ();
124 u32 wrk_index = wrk->wrk_index;
125 vcl_worker_cleanup (wrk, 1 /* notify vpp */ );
126 vcl_set_worker_index (~0);
127 VDBG (0, "cleaned up worker %u", wrk_index);
131 vcl_worker_alloc_and_init ()
135 /* This was initialized already */
136 if (vcl_get_worker_index () != ~0)
139 /* Use separate heap map entry for worker */
140 clib_mem_set_thread_index ();
142 if (pool_elts (vcm->workers) == vcm->cfg.max_workers)
144 VDBG (0, "max-workers %u limit reached", vcm->cfg.max_workers);
148 clib_spinlock_lock (&vcm->workers_lock);
149 wrk = vcl_worker_alloc ();
150 vcl_set_worker_index (wrk->wrk_index);
151 wrk->thread_id = pthread_self ();
152 wrk->current_pid = getpid ();
155 if (vcm->cfg.use_mq_eventfd)
157 wrk->vcl_needs_real_epoll = 1;
158 wrk->mqs_epfd = epoll_create (1);
159 wrk->vcl_needs_real_epoll = 0;
160 if (wrk->mqs_epfd < 0)
162 clib_unix_warning ("epoll_create() returned");
167 wrk->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
168 clib_time_init (&wrk->clib_time);
169 vec_validate (wrk->mq_events, 64);
170 vec_validate (wrk->mq_msg_vector, 128);
171 vec_reset_length (wrk->mq_msg_vector);
172 vec_validate (wrk->unhandled_evts_vector, 128);
173 vec_reset_length (wrk->unhandled_evts_vector);
174 clib_spinlock_unlock (&vcm->workers_lock);
181 vcl_worker_register_with_vpp (void)
183 vcl_worker_t *wrk = vcl_worker_get_current ();
185 clib_spinlock_lock (&vcm->workers_lock);
187 if (vcl_bapi_app_worker_add ())
189 VDBG (0, "failed to add worker to vpp");
190 clib_spinlock_unlock (&vcm->workers_lock);
193 if (pthread_key_create (&vcl_worker_stop_key, vcl_worker_cleanup_cb))
194 VDBG (0, "failed to add pthread cleanup function");
195 if (pthread_setspecific (vcl_worker_stop_key, &wrk->thread_id))
196 VDBG (0, "failed to setup key value");
198 clib_spinlock_unlock (&vcm->workers_lock);
200 VDBG (0, "added worker %u", wrk->wrk_index);
205 vcl_worker_ctrl_mq (vcl_worker_t * wrk)
211 vcl_session_read_ready (vcl_session_t * session)
215 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
216 if (PREDICT_FALSE (session->is_vep))
218 VDBG (0, "ERROR: session %u: cannot read from an epoll session!",
219 session->session_index);
220 return VPPCOM_EBADFD;
223 if (PREDICT_FALSE (!(session->session_state & (STATE_OPEN | STATE_LISTEN))))
225 vcl_session_state_t state = session->session_state;
228 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
230 VDBG (1, "session %u [0x%llx]: not open! state 0x%x (%s), ret %d (%s)",
231 session->session_index, session->vpp_handle, state,
232 vppcom_session_state_str (state), rv, vppcom_retval_str (rv));
236 if (session->session_state & STATE_LISTEN)
237 return clib_fifo_elts (session->accept_evts_fifo);
239 if (vcl_session_is_ct (session))
240 return svm_fifo_max_dequeue_cons (session->ct_rx_fifo);
242 max_deq = svm_fifo_max_dequeue_cons (session->rx_fifo);
244 if (session->is_dgram)
246 session_dgram_pre_hdr_t ph;
248 if (max_deq <= SESSION_CONN_HDR_LEN)
250 if (svm_fifo_peek (session->rx_fifo, 0, sizeof (ph), (u8 *) & ph) < 0)
252 if (ph.data_length + SESSION_CONN_HDR_LEN > max_deq)
255 return ph.data_length;
262 vcl_session_write_ready (vcl_session_t * session)
264 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
265 if (PREDICT_FALSE (session->is_vep))
267 VDBG (0, "session %u [0x%llx]: cannot write to an epoll session!",
268 session->session_index, session->vpp_handle);
269 return VPPCOM_EBADFD;
272 if (PREDICT_FALSE (session->session_state & STATE_LISTEN))
274 if (session->tx_fifo)
275 return svm_fifo_max_enqueue_prod (session->tx_fifo);
277 return VPPCOM_EBADFD;
280 if (PREDICT_FALSE (!(session->session_state & STATE_OPEN)))
282 vcl_session_state_t state = session->session_state;
285 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
286 VDBG (0, "session %u [0x%llx]: not open! state 0x%x (%s), ret %d (%s)",
287 session->session_index, session->vpp_handle, state,
288 vppcom_session_state_str (state), rv, vppcom_retval_str (rv));
292 if (vcl_session_is_ct (session))
293 return svm_fifo_max_enqueue_prod (session->ct_tx_fifo);
295 if (session->is_dgram)
297 u32 max_enq = svm_fifo_max_enqueue_prod (session->tx_fifo);
299 if (max_enq <= sizeof (session_dgram_hdr_t))
301 return max_enq - sizeof (session_dgram_hdr_t);
304 return svm_fifo_max_enqueue_prod (session->tx_fifo);
308 vcl_segment_attach (u64 segment_handle, char *name, ssvm_segment_type_t type,
311 fifo_segment_create_args_t _a, *a = &_a;
314 memset (a, 0, sizeof (*a));
315 a->segment_name = name;
316 a->segment_type = type;
318 if (type == SSVM_SEGMENT_MEMFD)
321 clib_rwlock_writer_lock (&vcm->segment_table_lock);
323 if ((rv = fifo_segment_attach (&vcm->segment_main, a)))
325 clib_warning ("svm_fifo_segment_attach ('%s') failed", name);
328 hash_set (vcm->segment_table, segment_handle, a->new_segment_indices[0]);
330 clib_rwlock_writer_unlock (&vcm->segment_table_lock);
332 vec_reset_length (a->new_segment_indices);
337 vcl_segment_table_lookup (u64 segment_handle)
341 clib_rwlock_reader_lock (&vcm->segment_table_lock);
342 seg_indexp = hash_get (vcm->segment_table, segment_handle);
343 clib_rwlock_reader_unlock (&vcm->segment_table_lock);
346 return VCL_INVALID_SEGMENT_INDEX;
347 return ((u32) * seg_indexp);
351 vcl_segment_detach (u64 segment_handle)
353 fifo_segment_main_t *sm = &vcm->segment_main;
354 fifo_segment_t *segment;
357 segment_index = vcl_segment_table_lookup (segment_handle);
358 if (segment_index == (u32) ~ 0)
361 clib_rwlock_writer_lock (&vcm->segment_table_lock);
363 segment = fifo_segment_get_segment (sm, segment_index);
364 fifo_segment_delete (sm, segment);
365 hash_unset (vcm->segment_table, segment_handle);
367 clib_rwlock_writer_unlock (&vcm->segment_table_lock);
369 VDBG (0, "detached segment %u handle %u", segment_index, segment_handle);
374 * fd.io coding-style-patch-verification: ON
377 * eval: (c-set-style "gnu")