blob: 300b82c4f21117b3691fc0175be44668b8e90614 [file] [log] [blame]
Florin Coras99368312018-08-02 10:45:44 -07001/*
Florin Coras5e062572019-03-14 19:07:51 -07002 * Copyright (c) 2018-2019 Cisco and/or its affiliates.
Florin Coras99368312018-08-02 10:45:44 -07003 * 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
Florin Coras3348a4c2018-09-07 17:09:35 -070018static pthread_key_t vcl_worker_stop_key;
Florin Coras134a9962018-08-28 11:32:04 -070019
20static const char *
21vppcom_app_state_str (app_state_t state)
22{
23 char *st;
24
25 switch (state)
26 {
27 case STATE_APP_START:
28 st = "STATE_APP_START";
29 break;
30
31 case STATE_APP_CONN_VPP:
32 st = "STATE_APP_CONN_VPP";
33 break;
34
35 case STATE_APP_ENABLED:
36 st = "STATE_APP_ENABLED";
37 break;
38
39 case STATE_APP_ATTACHED:
40 st = "STATE_APP_ATTACHED";
41 break;
42
43 default:
44 st = "UNKNOWN_APP_STATE";
45 break;
46 }
47
48 return st;
49}
50
51int
52vcl_wait_for_app_state_change (app_state_t app_state)
53{
54 vcl_worker_t *wrk = vcl_worker_get_current ();
55 f64 timeout = clib_time_now (&wrk->clib_time) + vcm->cfg.app_timeout;
56
57 while (clib_time_now (&wrk->clib_time) < timeout)
58 {
59 if (vcm->app_state == app_state)
60 return VPPCOM_OK;
61 if (vcm->app_state == STATE_APP_FAILED)
62 return VPPCOM_ECONNABORTED;
63 }
Florin Coras5e062572019-03-14 19:07:51 -070064 VDBG (0, "timeout waiting for state %s (%d)",
Florin Coras134a9962018-08-28 11:32:04 -070065 vppcom_app_state_str (app_state), app_state);
66 vcl_evt (VCL_EVT_SESSION_TIMEOUT, vcm, app_state);
67
68 return VPPCOM_ETIMEDOUT;
69}
70
Florin Coras99368312018-08-02 10:45:44 -070071vcl_mq_evt_conn_t *
Florin Coras134a9962018-08-28 11:32:04 -070072vcl_mq_evt_conn_alloc (vcl_worker_t * wrk)
Florin Coras99368312018-08-02 10:45:44 -070073{
74 vcl_mq_evt_conn_t *mqc;
Florin Coras134a9962018-08-28 11:32:04 -070075 pool_get (wrk->mq_evt_conns, mqc);
Florin Coras99368312018-08-02 10:45:44 -070076 memset (mqc, 0, sizeof (*mqc));
77 return mqc;
78}
79
80u32
Florin Coras134a9962018-08-28 11:32:04 -070081vcl_mq_evt_conn_index (vcl_worker_t * wrk, vcl_mq_evt_conn_t * mqc)
Florin Coras99368312018-08-02 10:45:44 -070082{
Florin Coras134a9962018-08-28 11:32:04 -070083 return (mqc - wrk->mq_evt_conns);
Florin Coras99368312018-08-02 10:45:44 -070084}
85
86vcl_mq_evt_conn_t *
Florin Coras134a9962018-08-28 11:32:04 -070087vcl_mq_evt_conn_get (vcl_worker_t * wrk, u32 mq_conn_idx)
Florin Coras99368312018-08-02 10:45:44 -070088{
Florin Coras134a9962018-08-28 11:32:04 -070089 return pool_elt_at_index (wrk->mq_evt_conns, mq_conn_idx);
Florin Coras99368312018-08-02 10:45:44 -070090}
91
92int
Florin Coras134a9962018-08-28 11:32:04 -070093vcl_mq_epoll_add_evfd (vcl_worker_t * wrk, svm_msg_q_t * mq)
Florin Coras99368312018-08-02 10:45:44 -070094{
95 struct epoll_event e = { 0 };
96 vcl_mq_evt_conn_t *mqc;
97 u32 mqc_index;
98 int mq_fd;
99
100 mq_fd = svm_msg_q_get_consumer_eventfd (mq);
101
Florin Coras134a9962018-08-28 11:32:04 -0700102 if (wrk->mqs_epfd < 0 || mq_fd == -1)
Florin Coras99368312018-08-02 10:45:44 -0700103 return -1;
104
Florin Coras134a9962018-08-28 11:32:04 -0700105 mqc = vcl_mq_evt_conn_alloc (wrk);
106 mqc_index = vcl_mq_evt_conn_index (wrk, mqc);
Florin Coras99368312018-08-02 10:45:44 -0700107 mqc->mq_fd = mq_fd;
108 mqc->mq = mq;
109
110 e.events = EPOLLIN;
111 e.data.u32 = mqc_index;
Florin Coras134a9962018-08-28 11:32:04 -0700112 if (epoll_ctl (wrk->mqs_epfd, EPOLL_CTL_ADD, mq_fd, &e) < 0)
Florin Coras99368312018-08-02 10:45:44 -0700113 {
Florin Coras5e062572019-03-14 19:07:51 -0700114 VDBG (0, "failed to add mq eventfd to mq epoll fd");
Florin Coras99368312018-08-02 10:45:44 -0700115 return -1;
116 }
117
118 return mqc_index;
119}
120
121int
Florin Coras134a9962018-08-28 11:32:04 -0700122vcl_mq_epoll_del_evfd (vcl_worker_t * wrk, u32 mqc_index)
Florin Coras99368312018-08-02 10:45:44 -0700123{
124 vcl_mq_evt_conn_t *mqc;
125
Florin Coras134a9962018-08-28 11:32:04 -0700126 if (wrk->mqs_epfd || mqc_index == ~0)
Florin Coras99368312018-08-02 10:45:44 -0700127 return -1;
128
Florin Coras134a9962018-08-28 11:32:04 -0700129 mqc = vcl_mq_evt_conn_get (wrk, mqc_index);
130 if (epoll_ctl (wrk->mqs_epfd, EPOLL_CTL_DEL, mqc->mq_fd, 0) < 0)
Florin Coras99368312018-08-02 10:45:44 -0700131 {
Florin Coras5e062572019-03-14 19:07:51 -0700132 VDBG (0, "failed to del mq eventfd to mq epoll fd");
Florin Coras99368312018-08-02 10:45:44 -0700133 return -1;
134 }
135 return 0;
136}
137
Florin Coras134a9962018-08-28 11:32:04 -0700138static vcl_worker_t *
139vcl_worker_alloc (void)
140{
141 vcl_worker_t *wrk;
142 pool_get (vcm->workers, wrk);
143 memset (wrk, 0, sizeof (*wrk));
144 wrk->wrk_index = wrk - vcm->workers;
Florin Coras01f3f892018-12-02 12:45:53 -0800145 wrk->forked_child = ~0;
Florin Coras134a9962018-08-28 11:32:04 -0700146 return wrk;
147}
148
149static void
150vcl_worker_free (vcl_worker_t * wrk)
151{
152 pool_put (vcm->workers, wrk);
153}
154
Florin Coras47c40e22018-11-26 17:01:36 -0800155void
Florin Coras01f3f892018-12-02 12:45:53 -0800156vcl_worker_cleanup (vcl_worker_t * wrk, u8 notify_vpp)
Florin Coras134a9962018-08-28 11:32:04 -0700157{
Florin Coras47c40e22018-11-26 17:01:36 -0800158 clib_spinlock_lock (&vcm->workers_lock);
Florin Coras940f78f2018-11-30 12:11:20 -0800159 if (notify_vpp)
Florin Coras01f3f892018-12-02 12:45:53 -0800160 {
Florin Coras64cf4592019-12-12 12:01:24 -0800161 /* Notify vpp that the worker is going away */
Florin Coras01f3f892018-12-02 12:45:53 -0800162 if (wrk->wrk_index == vcl_get_worker_index ())
163 vcl_send_app_worker_add_del (0 /* is_add */ );
164 else
165 vcl_send_child_worker_del (wrk);
Florin Coras64cf4592019-12-12 12:01:24 -0800166
167 /* Disconnect the binary api */
168 if (vec_len (vcm->workers) == 1)
169 vppcom_disconnect_from_vpp ();
170 else
171 vl_client_send_disconnect (1 /* vpp should cleanup */ );
Florin Coras01f3f892018-12-02 12:45:53 -0800172 }
Florin Coras64cf4592019-12-12 12:01:24 -0800173
Florin Coras940f78f2018-11-30 12:11:20 -0800174 if (wrk->mqs_epfd > 0)
175 close (wrk->mqs_epfd);
Florin Coras134a9962018-08-28 11:32:04 -0700176 hash_free (wrk->session_index_by_vpp_handles);
Florin Coras134a9962018-08-28 11:32:04 -0700177 vec_free (wrk->mq_events);
178 vec_free (wrk->mq_msg_vector);
Florin Coras134a9962018-08-28 11:32:04 -0700179 vcl_worker_free (wrk);
Florin Coras47c40e22018-11-26 17:01:36 -0800180 clib_spinlock_unlock (&vcm->workers_lock);
Florin Coras47c40e22018-11-26 17:01:36 -0800181}
182
183static void
184vcl_worker_cleanup_cb (void *arg)
185{
Florin Coras01f3f892018-12-02 12:45:53 -0800186 vcl_worker_t *wrk = vcl_worker_get_current ();
187 u32 wrk_index = wrk->wrk_index;
188 vcl_worker_cleanup (wrk, 1 /* notify vpp */ );
189 vcl_set_worker_index (~0);
Florin Coras940f78f2018-11-30 12:11:20 -0800190 VDBG (0, "cleaned up worker %u", wrk_index);
Florin Coras134a9962018-08-28 11:32:04 -0700191}
192
193vcl_worker_t *
194vcl_worker_alloc_and_init ()
195{
196 vcl_worker_t *wrk;
197
198 /* This was initialized already */
199 if (vcl_get_worker_index () != ~0)
200 return 0;
201
Florin Corasd2c9e702019-08-02 10:53:01 -0700202 /* Use separate heap map entry for worker */
203 clib_mem_set_thread_index ();
204
Florin Corasde9f08b2018-09-07 14:32:58 -0700205 if (pool_elts (vcm->workers) == vcm->cfg.max_workers)
206 {
207 VDBG (0, "max-workers %u limit reached", vcm->cfg.max_workers);
208 return 0;
209 }
210
211 clib_spinlock_lock (&vcm->workers_lock);
Florin Coras134a9962018-08-28 11:32:04 -0700212 wrk = vcl_worker_alloc ();
213 vcl_set_worker_index (wrk->wrk_index);
Florin Coras47c40e22018-11-26 17:01:36 -0800214 wrk->thread_id = pthread_self ();
215 wrk->current_pid = getpid ();
Florin Coras134a9962018-08-28 11:32:04 -0700216
217 wrk->mqs_epfd = -1;
218 if (vcm->cfg.use_mq_eventfd)
219 {
220 wrk->mqs_epfd = epoll_create (1);
221 if (wrk->mqs_epfd < 0)
222 {
223 clib_unix_warning ("epoll_create() returned");
Florin Coras47c40e22018-11-26 17:01:36 -0800224 goto done;
Florin Coras134a9962018-08-28 11:32:04 -0700225 }
226 }
227
228 wrk->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
Florin Coras134a9962018-08-28 11:32:04 -0700229 clib_time_init (&wrk->clib_time);
230 vec_validate (wrk->mq_events, 64);
231 vec_validate (wrk->mq_msg_vector, 128);
232 vec_reset_length (wrk->mq_msg_vector);
Florin Coras86f04502018-09-12 16:08:01 -0700233 vec_validate (wrk->unhandled_evts_vector, 128);
234 vec_reset_length (wrk->unhandled_evts_vector);
Florin Coras47c40e22018-11-26 17:01:36 -0800235 clib_spinlock_unlock (&vcm->workers_lock);
Florin Coras134a9962018-08-28 11:32:04 -0700236
Florin Coras47c40e22018-11-26 17:01:36 -0800237done:
238 return wrk;
239}
240
241int
242vcl_worker_register_with_vpp (void)
243{
244 vcl_worker_t *wrk = vcl_worker_get_current ();
245
246 clib_spinlock_lock (&vcm->workers_lock);
Florin Coras134a9962018-08-28 11:32:04 -0700247
Florin Coras134a9962018-08-28 11:32:04 -0700248 vcm->app_state = STATE_APP_ADDING_WORKER;
249 vcl_send_app_worker_add_del (1 /* is_add */ );
250 if (vcl_wait_for_app_state_change (STATE_APP_READY))
251 {
Florin Coras5e062572019-03-14 19:07:51 -0700252 VDBG (0, "failed to add worker to vpp");
Florin Coras47c40e22018-11-26 17:01:36 -0800253 return -1;
Florin Coras134a9962018-08-28 11:32:04 -0700254 }
Florin Coras47c40e22018-11-26 17:01:36 -0800255 if (pthread_key_create (&vcl_worker_stop_key, vcl_worker_cleanup_cb))
Florin Coras5e062572019-03-14 19:07:51 -0700256 VDBG (0, "failed to add pthread cleanup function");
Florin Coras3348a4c2018-09-07 17:09:35 -0700257 if (pthread_setspecific (vcl_worker_stop_key, &wrk->thread_id))
Florin Coras5e062572019-03-14 19:07:51 -0700258 VDBG (0, "failed to setup key value");
Florin Coras134a9962018-08-28 11:32:04 -0700259
Florin Corasde9f08b2018-09-07 14:32:58 -0700260 clib_spinlock_unlock (&vcm->workers_lock);
261
Florin Coras134a9962018-08-28 11:32:04 -0700262 VDBG (0, "added worker %u", wrk->wrk_index);
Florin Coras47c40e22018-11-26 17:01:36 -0800263 return 0;
264}
Florin Coras134a9962018-08-28 11:32:04 -0700265
Florin Coras47c40e22018-11-26 17:01:36 -0800266int
267vcl_worker_set_bapi (void)
268{
269 vcl_worker_t *wrk = vcl_worker_get_current ();
270 int i;
271
272 /* Find the first worker with the same pid */
273 for (i = 0; i < vec_len (vcm->workers); i++)
274 {
275 if (i == wrk->wrk_index)
276 continue;
277 if (vcm->workers[i].current_pid == wrk->current_pid)
278 {
279 wrk->vl_input_queue = vcm->workers[i].vl_input_queue;
280 wrk->my_client_index = vcm->workers[i].my_client_index;
281 return 0;
282 }
283 }
284 return -1;
285}
286
Florin Coras458089b2019-08-21 16:20:44 -0700287svm_msg_q_t *
288vcl_worker_ctrl_mq (vcl_worker_t * wrk)
289{
290 return wrk->ctrl_mq;
291}
292
Florin Corasd85de682018-11-29 17:02:29 -0800293void
Florin Corasf9240dc2019-01-15 08:03:17 -0800294vcl_cleanup_bapi (void)
295{
296 socket_client_main_t *scm = &socket_client_main;
Dave Barach39d69112019-11-27 11:42:13 -0500297 api_main_t *am = vlibapi_get_main ();
Florin Corasf9240dc2019-01-15 08:03:17 -0800298
299 am->my_client_index = ~0;
300 am->my_registration = 0;
301 am->vl_input_queue = 0;
302 am->msg_index_by_name_and_crc = 0;
303 scm->socket_fd = 0;
304
305 vl_client_api_unmap ();
306}
307
Florin Coras0ef8ef22019-01-18 08:37:13 -0800308int
309vcl_session_read_ready (vcl_session_t * session)
310{
Florin Coras0a1e1832020-03-29 18:54:04 +0000311 u32 max_deq;
312
Florin Coras0ef8ef22019-01-18 08:37:13 -0800313 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
314 if (PREDICT_FALSE (session->is_vep))
315 {
316 VDBG (0, "ERROR: session %u: cannot read from an epoll session!",
317 session->session_index);
318 return VPPCOM_EBADFD;
319 }
320
321 if (PREDICT_FALSE (!(session->session_state & (STATE_OPEN | STATE_LISTEN))))
322 {
Florin Coras288eaab2019-02-03 15:26:14 -0800323 vcl_session_state_t state = session->session_state;
Florin Coras0ef8ef22019-01-18 08:37:13 -0800324 int rv;
325
326 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
327
328 VDBG (1, "session %u [0x%llx]: not open! state 0x%x (%s), ret %d (%s)",
329 session->session_index, session->vpp_handle, state,
330 vppcom_session_state_str (state), rv, vppcom_retval_str (rv));
331 return rv;
332 }
333
334 if (session->session_state & STATE_LISTEN)
335 return clib_fifo_elts (session->accept_evts_fifo);
336
Florin Coras653e43f2019-03-04 10:56:23 -0800337 if (vcl_session_is_ct (session))
Sirshak Das28aa5392019-02-05 01:33:33 -0600338 return svm_fifo_max_dequeue_cons (session->ct_rx_fifo);
Florin Coras653e43f2019-03-04 10:56:23 -0800339
Florin Coras0a1e1832020-03-29 18:54:04 +0000340 max_deq = svm_fifo_max_dequeue_cons (session->rx_fifo);
341
342 if (session->is_dgram)
343 {
344 session_dgram_pre_hdr_t ph;
345
346 if (max_deq <= SESSION_CONN_HDR_LEN)
347 return 0;
348 if (svm_fifo_peek (session->rx_fifo, 0, sizeof (ph), (u8 *) & ph) < 0)
349 return 0;
350 if (ph.data_length + SESSION_CONN_HDR_LEN > max_deq)
351 return 0;
352
353 return ph.data_length;
354 }
355
356 return max_deq;
Florin Coras0ef8ef22019-01-18 08:37:13 -0800357}
358
359int
360vcl_session_write_ready (vcl_session_t * session)
361{
362 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
363 if (PREDICT_FALSE (session->is_vep))
364 {
365 VDBG (0, "session %u [0x%llx]: cannot write to an epoll session!",
366 session->session_index, session->vpp_handle);
367 return VPPCOM_EBADFD;
368 }
369
370 if (PREDICT_FALSE (session->session_state & STATE_LISTEN))
371 {
372 if (session->tx_fifo)
Sirshak Das28aa5392019-02-05 01:33:33 -0600373 return svm_fifo_max_enqueue_prod (session->tx_fifo);
Florin Coras0ef8ef22019-01-18 08:37:13 -0800374 else
375 return VPPCOM_EBADFD;
376 }
377
378 if (PREDICT_FALSE (!(session->session_state & STATE_OPEN)))
379 {
Florin Coras288eaab2019-02-03 15:26:14 -0800380 vcl_session_state_t state = session->session_state;
Florin Coras0ef8ef22019-01-18 08:37:13 -0800381 int rv;
382
383 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
384 VDBG (0, "session %u [0x%llx]: not open! state 0x%x (%s), ret %d (%s)",
385 session->session_index, session->vpp_handle, state,
386 vppcom_session_state_str (state), rv, vppcom_retval_str (rv));
387 return rv;
388 }
389
Florin Coras653e43f2019-03-04 10:56:23 -0800390 if (vcl_session_is_ct (session))
Sirshak Das28aa5392019-02-05 01:33:33 -0600391 return svm_fifo_max_enqueue_prod (session->ct_tx_fifo);
Florin Coras653e43f2019-03-04 10:56:23 -0800392
Florin Coras7a2abce2020-04-05 19:25:44 +0000393 if (session->is_dgram)
394 {
395 u32 max_enq = svm_fifo_max_enqueue_prod (session->tx_fifo);
396
397 if (max_enq <= sizeof (session_dgram_hdr_t))
398 return 0;
399 return max_enq - sizeof (session_dgram_hdr_t);
400 }
401
Sirshak Das28aa5392019-02-05 01:33:33 -0600402 return svm_fifo_max_enqueue_prod (session->tx_fifo);
Florin Coras0ef8ef22019-01-18 08:37:13 -0800403}
404
Florin Corasc4c4cf52019-08-24 18:17:34 -0700405int
406vcl_segment_attach (u64 segment_handle, char *name, ssvm_segment_type_t type,
407 int fd)
408{
409 fifo_segment_create_args_t _a, *a = &_a;
410 int rv;
411
412 memset (a, 0, sizeof (*a));
Jakub Grajciarb4e5e502020-01-31 09:35:29 +0100413 a->segment_name = name;
Florin Corasc4c4cf52019-08-24 18:17:34 -0700414 a->segment_type = type;
415
416 if (type == SSVM_SEGMENT_MEMFD)
417 a->memfd_fd = fd;
418
Florin Corasda8f2182019-12-22 12:48:05 -0800419 clib_rwlock_writer_lock (&vcm->segment_table_lock);
420
Florin Corasc4c4cf52019-08-24 18:17:34 -0700421 if ((rv = fifo_segment_attach (&vcm->segment_main, a)))
422 {
423 clib_warning ("svm_fifo_segment_attach ('%s') failed", name);
424 return rv;
425 }
Florin Corasda8f2182019-12-22 12:48:05 -0800426 hash_set (vcm->segment_table, segment_handle, a->new_segment_indices[0]);
427
428 clib_rwlock_writer_unlock (&vcm->segment_table_lock);
429
Florin Corasc4c4cf52019-08-24 18:17:34 -0700430 vec_reset_length (a->new_segment_indices);
431 return 0;
432}
433
Florin Corasda8f2182019-12-22 12:48:05 -0800434u32
435vcl_segment_table_lookup (u64 segment_handle)
436{
437 uword *seg_indexp;
438
439 clib_rwlock_reader_lock (&vcm->segment_table_lock);
440 seg_indexp = hash_get (vcm->segment_table, segment_handle);
441 clib_rwlock_reader_unlock (&vcm->segment_table_lock);
442
443 if (!seg_indexp)
444 return VCL_INVALID_SEGMENT_INDEX;
445 return ((u32) * seg_indexp);
446}
447
Florin Corasc4c4cf52019-08-24 18:17:34 -0700448void
449vcl_segment_detach (u64 segment_handle)
450{
451 fifo_segment_main_t *sm = &vcm->segment_main;
452 fifo_segment_t *segment;
453 u32 segment_index;
454
455 segment_index = vcl_segment_table_lookup (segment_handle);
456 if (segment_index == (u32) ~ 0)
457 return;
Florin Corasda8f2182019-12-22 12:48:05 -0800458
459 clib_rwlock_writer_lock (&vcm->segment_table_lock);
460
Florin Corasc4c4cf52019-08-24 18:17:34 -0700461 segment = fifo_segment_get_segment (sm, segment_index);
462 fifo_segment_delete (sm, segment);
Florin Corasda8f2182019-12-22 12:48:05 -0800463 hash_unset (vcm->segment_table, segment_handle);
464
465 clib_rwlock_writer_unlock (&vcm->segment_table_lock);
466
Florin Corasc4c4cf52019-08-24 18:17:34 -0700467 VDBG (0, "detached segment %u handle %u", segment_index, segment_handle);
468}
469
470
Florin Coras99368312018-08-02 10:45:44 -0700471/*
472 * fd.io coding-style-patch-verification: ON
473 *
474 * Local Variables:
475 * eval: (c-set-style "gnu")
476 * End:
477 */