blob: ea93811361571ba428c29054b5351ebf6b379e9f [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
Florin Coras99368312018-08-02 10:45:44 -070020vcl_mq_evt_conn_t *
Florin Coras134a9962018-08-28 11:32:04 -070021vcl_mq_evt_conn_alloc (vcl_worker_t * wrk)
Florin Coras99368312018-08-02 10:45:44 -070022{
23 vcl_mq_evt_conn_t *mqc;
Florin Coras134a9962018-08-28 11:32:04 -070024 pool_get (wrk->mq_evt_conns, mqc);
Florin Coras99368312018-08-02 10:45:44 -070025 memset (mqc, 0, sizeof (*mqc));
26 return mqc;
27}
28
29u32
Florin Coras134a9962018-08-28 11:32:04 -070030vcl_mq_evt_conn_index (vcl_worker_t * wrk, vcl_mq_evt_conn_t * mqc)
Florin Coras99368312018-08-02 10:45:44 -070031{
Florin Coras134a9962018-08-28 11:32:04 -070032 return (mqc - wrk->mq_evt_conns);
Florin Coras99368312018-08-02 10:45:44 -070033}
34
35vcl_mq_evt_conn_t *
Florin Coras134a9962018-08-28 11:32:04 -070036vcl_mq_evt_conn_get (vcl_worker_t * wrk, u32 mq_conn_idx)
Florin Coras99368312018-08-02 10:45:44 -070037{
Florin Coras134a9962018-08-28 11:32:04 -070038 return pool_elt_at_index (wrk->mq_evt_conns, mq_conn_idx);
Florin Coras99368312018-08-02 10:45:44 -070039}
40
41int
Florin Coras134a9962018-08-28 11:32:04 -070042vcl_mq_epoll_add_evfd (vcl_worker_t * wrk, svm_msg_q_t * mq)
Florin Coras99368312018-08-02 10:45:44 -070043{
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_consumer_eventfd (mq);
50
Florin Coras134a9962018-08-28 11:32:04 -070051 if (wrk->mqs_epfd < 0 || mq_fd == -1)
Florin Coras99368312018-08-02 10:45:44 -070052 return -1;
53
Florin Coras134a9962018-08-28 11:32:04 -070054 mqc = vcl_mq_evt_conn_alloc (wrk);
55 mqc_index = vcl_mq_evt_conn_index (wrk, mqc);
Florin Coras99368312018-08-02 10:45:44 -070056 mqc->mq_fd = mq_fd;
57 mqc->mq = mq;
58
59 e.events = EPOLLIN;
60 e.data.u32 = mqc_index;
Florin Coras134a9962018-08-28 11:32:04 -070061 if (epoll_ctl (wrk->mqs_epfd, EPOLL_CTL_ADD, mq_fd, &e) < 0)
Florin Coras99368312018-08-02 10:45:44 -070062 {
Florin Coras5e062572019-03-14 19:07:51 -070063 VDBG (0, "failed to add mq eventfd to mq epoll fd");
Florin Coras99368312018-08-02 10:45:44 -070064 return -1;
65 }
66
67 return mqc_index;
68}
69
70int
Florin Coras134a9962018-08-28 11:32:04 -070071vcl_mq_epoll_del_evfd (vcl_worker_t * wrk, u32 mqc_index)
Florin Coras99368312018-08-02 10:45:44 -070072{
73 vcl_mq_evt_conn_t *mqc;
74
Florin Coras134a9962018-08-28 11:32:04 -070075 if (wrk->mqs_epfd || mqc_index == ~0)
Florin Coras99368312018-08-02 10:45:44 -070076 return -1;
77
Florin Coras134a9962018-08-28 11:32:04 -070078 mqc = vcl_mq_evt_conn_get (wrk, mqc_index);
79 if (epoll_ctl (wrk->mqs_epfd, EPOLL_CTL_DEL, mqc->mq_fd, 0) < 0)
Florin Coras99368312018-08-02 10:45:44 -070080 {
Florin Coras5e062572019-03-14 19:07:51 -070081 VDBG (0, "failed to del mq eventfd to mq epoll fd");
Florin Coras99368312018-08-02 10:45:44 -070082 return -1;
83 }
84 return 0;
85}
86
Florin Coras134a9962018-08-28 11:32:04 -070087static vcl_worker_t *
88vcl_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;
Florin Coras01f3f892018-12-02 12:45:53 -080094 wrk->forked_child = ~0;
Florin Coras134a9962018-08-28 11:32:04 -070095 return wrk;
96}
97
98static void
99vcl_worker_free (vcl_worker_t * wrk)
100{
101 pool_put (vcm->workers, wrk);
102}
103
Florin Coras935ce752020-09-08 22:43:47 -0700104int
105vcl_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
113void
114vcl_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
Florin Coras47c40e22018-11-26 17:01:36 -0800122void
Florin Coras01f3f892018-12-02 12:45:53 -0800123vcl_worker_cleanup (vcl_worker_t * wrk, u8 notify_vpp)
Florin Coras134a9962018-08-28 11:32:04 -0700124{
Florin Coras47c40e22018-11-26 17:01:36 -0800125 clib_spinlock_lock (&vcm->workers_lock);
Florin Coras940f78f2018-11-30 12:11:20 -0800126 if (notify_vpp)
Florin Coras935ce752020-09-08 22:43:47 -0700127 vcl_api_app_worker_del (wrk);
Florin Coras64cf4592019-12-12 12:01:24 -0800128
Florin Coras940f78f2018-11-30 12:11:20 -0800129 if (wrk->mqs_epfd > 0)
130 close (wrk->mqs_epfd);
Florin Coras134a9962018-08-28 11:32:04 -0700131 hash_free (wrk->session_index_by_vpp_handles);
Florin Coras134a9962018-08-28 11:32:04 -0700132 vec_free (wrk->mq_events);
133 vec_free (wrk->mq_msg_vector);
Florin Coras134a9962018-08-28 11:32:04 -0700134 vcl_worker_free (wrk);
Florin Coras47c40e22018-11-26 17:01:36 -0800135 clib_spinlock_unlock (&vcm->workers_lock);
Florin Coras47c40e22018-11-26 17:01:36 -0800136}
137
138static void
139vcl_worker_cleanup_cb (void *arg)
140{
Florin Coras01f3f892018-12-02 12:45:53 -0800141 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);
Florin Coras940f78f2018-11-30 12:11:20 -0800145 VDBG (0, "cleaned up worker %u", wrk_index);
Florin Coras134a9962018-08-28 11:32:04 -0700146}
147
148vcl_worker_t *
149vcl_worker_alloc_and_init ()
150{
151 vcl_worker_t *wrk;
152
153 /* This was initialized already */
154 if (vcl_get_worker_index () != ~0)
155 return 0;
156
Florin Corasd2c9e702019-08-02 10:53:01 -0700157 /* Use separate heap map entry for worker */
158 clib_mem_set_thread_index ();
159
Florin Corasde9f08b2018-09-07 14:32:58 -0700160 if (pool_elts (vcm->workers) == vcm->cfg.max_workers)
161 {
162 VDBG (0, "max-workers %u limit reached", vcm->cfg.max_workers);
163 return 0;
164 }
165
166 clib_spinlock_lock (&vcm->workers_lock);
Florin Coras134a9962018-08-28 11:32:04 -0700167 wrk = vcl_worker_alloc ();
168 vcl_set_worker_index (wrk->wrk_index);
Florin Coras47c40e22018-11-26 17:01:36 -0800169 wrk->thread_id = pthread_self ();
170 wrk->current_pid = getpid ();
Florin Coras134a9962018-08-28 11:32:04 -0700171
172 wrk->mqs_epfd = -1;
173 if (vcm->cfg.use_mq_eventfd)
174 {
hanlina3a48962020-07-13 11:09:15 +0800175 wrk->vcl_needs_real_epoll = 1;
Florin Coras134a9962018-08-28 11:32:04 -0700176 wrk->mqs_epfd = epoll_create (1);
hanlina3a48962020-07-13 11:09:15 +0800177 wrk->vcl_needs_real_epoll = 0;
Florin Coras134a9962018-08-28 11:32:04 -0700178 if (wrk->mqs_epfd < 0)
179 {
180 clib_unix_warning ("epoll_create() returned");
Florin Coras47c40e22018-11-26 17:01:36 -0800181 goto done;
Florin Coras134a9962018-08-28 11:32:04 -0700182 }
183 }
184
185 wrk->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
Florin Coras134a9962018-08-28 11:32:04 -0700186 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);
Florin Coras86f04502018-09-12 16:08:01 -0700190 vec_validate (wrk->unhandled_evts_vector, 128);
191 vec_reset_length (wrk->unhandled_evts_vector);
Florin Coras47c40e22018-11-26 17:01:36 -0800192 clib_spinlock_unlock (&vcm->workers_lock);
Florin Coras134a9962018-08-28 11:32:04 -0700193
Florin Coras47c40e22018-11-26 17:01:36 -0800194done:
195 return wrk;
196}
197
198int
199vcl_worker_register_with_vpp (void)
200{
201 vcl_worker_t *wrk = vcl_worker_get_current ();
202
203 clib_spinlock_lock (&vcm->workers_lock);
Florin Coras134a9962018-08-28 11:32:04 -0700204
Florin Coras935ce752020-09-08 22:43:47 -0700205 if (vcl_api_app_worker_add ())
Florin Coras134a9962018-08-28 11:32:04 -0700206 {
Florin Coras5e062572019-03-14 19:07:51 -0700207 VDBG (0, "failed to add worker to vpp");
Florin Corasb88de902020-09-08 16:47:57 -0700208 clib_spinlock_unlock (&vcm->workers_lock);
Florin Coras47c40e22018-11-26 17:01:36 -0800209 return -1;
Florin Coras134a9962018-08-28 11:32:04 -0700210 }
Florin Coras47c40e22018-11-26 17:01:36 -0800211 if (pthread_key_create (&vcl_worker_stop_key, vcl_worker_cleanup_cb))
Florin Coras5e062572019-03-14 19:07:51 -0700212 VDBG (0, "failed to add pthread cleanup function");
Florin Coras3348a4c2018-09-07 17:09:35 -0700213 if (pthread_setspecific (vcl_worker_stop_key, &wrk->thread_id))
Florin Coras5e062572019-03-14 19:07:51 -0700214 VDBG (0, "failed to setup key value");
Florin Coras134a9962018-08-28 11:32:04 -0700215
Florin Corasde9f08b2018-09-07 14:32:58 -0700216 clib_spinlock_unlock (&vcm->workers_lock);
217
Florin Coras134a9962018-08-28 11:32:04 -0700218 VDBG (0, "added worker %u", wrk->wrk_index);
Florin Coras47c40e22018-11-26 17:01:36 -0800219 return 0;
220}
Florin Coras134a9962018-08-28 11:32:04 -0700221
Florin Coras458089b2019-08-21 16:20:44 -0700222svm_msg_q_t *
223vcl_worker_ctrl_mq (vcl_worker_t * wrk)
224{
225 return wrk->ctrl_mq;
226}
227
Florin Coras0ef8ef22019-01-18 08:37:13 -0800228int
Florin Corasc127d5a2020-10-14 16:35:58 -0700229vcl_session_read_ready (vcl_session_t * s)
Florin Coras0ef8ef22019-01-18 08:37:13 -0800230{
Florin Coras6c3b2182020-10-19 18:36:48 -0700231 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Florin Coras0ef8ef22019-01-18 08:37:13 -0800232 {
233 VDBG (0, "ERROR: session %u: cannot read from an epoll session!",
Florin Corasc127d5a2020-10-14 16:35:58 -0700234 s->session_index);
Florin Coras0ef8ef22019-01-18 08:37:13 -0800235 return VPPCOM_EBADFD;
236 }
237
Florin Coras1bc919e2020-10-18 20:17:49 -0700238 if (vcl_session_is_open (s))
Florin Coras0ef8ef22019-01-18 08:37:13 -0800239 {
Florin Coras1bc919e2020-10-18 20:17:49 -0700240 if (vcl_session_is_ct (s))
241 return svm_fifo_max_dequeue_cons (s->ct_rx_fifo);
Florin Coras0ef8ef22019-01-18 08:37:13 -0800242
Florin Coras1bc919e2020-10-18 20:17:49 -0700243 if (s->is_dgram)
244 {
245 session_dgram_pre_hdr_t ph;
246 u32 max_deq;
247
248 max_deq = svm_fifo_max_dequeue_cons (s->rx_fifo);
249 if (max_deq <= SESSION_CONN_HDR_LEN)
250 return 0;
251 if (svm_fifo_peek (s->rx_fifo, 0, sizeof (ph), (u8 *) & ph) < 0)
252 return 0;
253 if (ph.data_length + SESSION_CONN_HDR_LEN > max_deq)
254 return 0;
255
256 return ph.data_length;
257 }
258
259 return svm_fifo_max_dequeue_cons (s->rx_fifo);
260 }
261 else if (s->session_state == VCL_STATE_LISTEN)
262 {
263 return clib_fifo_elts (s->accept_evts_fifo);
264 }
265 else
266 {
267 return (s->session_state == VCL_STATE_DISCONNECT) ?
Florin Corasc127d5a2020-10-14 16:35:58 -0700268 VPPCOM_ECONNRESET : VPPCOM_ENOTCONN;
Florin Coras0ef8ef22019-01-18 08:37:13 -0800269 }
Florin Coras0ef8ef22019-01-18 08:37:13 -0800270}
271
272int
Florin Corasc127d5a2020-10-14 16:35:58 -0700273vcl_session_write_ready (vcl_session_t * s)
Florin Coras0ef8ef22019-01-18 08:37:13 -0800274{
Florin Coras6c3b2182020-10-19 18:36:48 -0700275 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Florin Coras0ef8ef22019-01-18 08:37:13 -0800276 {
277 VDBG (0, "session %u [0x%llx]: cannot write to an epoll session!",
Florin Corasc127d5a2020-10-14 16:35:58 -0700278 s->session_index, s->vpp_handle);
Florin Coras0ef8ef22019-01-18 08:37:13 -0800279 return VPPCOM_EBADFD;
280 }
281
Florin Coras1bc919e2020-10-18 20:17:49 -0700282 if (vcl_session_is_open (s))
283 {
284 if (vcl_session_is_ct (s))
285 return svm_fifo_max_enqueue_prod (s->ct_tx_fifo);
286
287 if (s->is_dgram)
288 {
289 u32 max_enq = svm_fifo_max_enqueue_prod (s->tx_fifo);
290
291 if (max_enq <= sizeof (session_dgram_hdr_t))
292 return 0;
293 return max_enq - sizeof (session_dgram_hdr_t);
294 }
295
296 return svm_fifo_max_enqueue_prod (s->tx_fifo);
297 }
298 else if (s->session_state == VCL_STATE_LISTEN)
Florin Coras0ef8ef22019-01-18 08:37:13 -0800299 {
Florin Corasc127d5a2020-10-14 16:35:58 -0700300 if (s->tx_fifo)
301 return svm_fifo_max_enqueue_prod (s->tx_fifo);
Florin Coras0ef8ef22019-01-18 08:37:13 -0800302 else
303 return VPPCOM_EBADFD;
304 }
Florin Coras1bc919e2020-10-18 20:17:49 -0700305 else
Florin Coras0ef8ef22019-01-18 08:37:13 -0800306 {
Florin Coras1bc919e2020-10-18 20:17:49 -0700307 return (s->session_state == VCL_STATE_DISCONNECT) ?
Florin Corasc127d5a2020-10-14 16:35:58 -0700308 VPPCOM_ECONNRESET : VPPCOM_ENOTCONN;
Florin Coras0ef8ef22019-01-18 08:37:13 -0800309 }
Florin Coras0ef8ef22019-01-18 08:37:13 -0800310}
311
Florin Corasc4c4cf52019-08-24 18:17:34 -0700312int
313vcl_segment_attach (u64 segment_handle, char *name, ssvm_segment_type_t type,
314 int fd)
315{
316 fifo_segment_create_args_t _a, *a = &_a;
317 int rv;
318
319 memset (a, 0, sizeof (*a));
Jakub Grajciarb4e5e502020-01-31 09:35:29 +0100320 a->segment_name = name;
Florin Corasc4c4cf52019-08-24 18:17:34 -0700321 a->segment_type = type;
322
323 if (type == SSVM_SEGMENT_MEMFD)
324 a->memfd_fd = fd;
325
Florin Corasda8f2182019-12-22 12:48:05 -0800326 clib_rwlock_writer_lock (&vcm->segment_table_lock);
327
Florin Corasc4c4cf52019-08-24 18:17:34 -0700328 if ((rv = fifo_segment_attach (&vcm->segment_main, a)))
329 {
330 clib_warning ("svm_fifo_segment_attach ('%s') failed", name);
331 return rv;
332 }
Florin Corasda8f2182019-12-22 12:48:05 -0800333 hash_set (vcm->segment_table, segment_handle, a->new_segment_indices[0]);
334
335 clib_rwlock_writer_unlock (&vcm->segment_table_lock);
336
Florin Corasc4c4cf52019-08-24 18:17:34 -0700337 vec_reset_length (a->new_segment_indices);
338 return 0;
339}
340
Florin Corasda8f2182019-12-22 12:48:05 -0800341u32
342vcl_segment_table_lookup (u64 segment_handle)
343{
344 uword *seg_indexp;
345
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);
349
350 if (!seg_indexp)
351 return VCL_INVALID_SEGMENT_INDEX;
352 return ((u32) * seg_indexp);
353}
354
Florin Corasc4c4cf52019-08-24 18:17:34 -0700355void
356vcl_segment_detach (u64 segment_handle)
357{
358 fifo_segment_main_t *sm = &vcm->segment_main;
359 fifo_segment_t *segment;
360 u32 segment_index;
361
362 segment_index = vcl_segment_table_lookup (segment_handle);
363 if (segment_index == (u32) ~ 0)
364 return;
Florin Corasda8f2182019-12-22 12:48:05 -0800365
366 clib_rwlock_writer_lock (&vcm->segment_table_lock);
367
Florin Corasc4c4cf52019-08-24 18:17:34 -0700368 segment = fifo_segment_get_segment (sm, segment_index);
369 fifo_segment_delete (sm, segment);
Florin Corasda8f2182019-12-22 12:48:05 -0800370 hash_unset (vcm->segment_table, segment_handle);
371
372 clib_rwlock_writer_unlock (&vcm->segment_table_lock);
373
Florin Corasc4c4cf52019-08-24 18:17:34 -0700374 VDBG (0, "detached segment %u handle %u", segment_index, segment_handle);
375}
376
Florin Corasc547e912020-12-08 17:50:45 -0800377int
378vcl_segment_attach_session (uword segment_handle, uword rxf_offset,
379 uword txf_offset, u8 is_ct, vcl_session_t *s)
380{
381 svm_fifo_shared_t *rxsf, *txsf;
382 svm_fifo_t *rxf, *txf;
383 fifo_segment_t *fs;
384 u32 fs_index;
385
386 fs_index = vcl_segment_table_lookup (segment_handle);
387 if (fs_index == VCL_INVALID_SEGMENT_INDEX)
388 {
389 VDBG (0, "ERROR: segment for session %u is not mounted!",
390 s->session_index);
391 return -1;
392 }
393
394 rxsf = uword_to_pointer (rxf_offset, svm_fifo_shared_t *);
395 txsf = uword_to_pointer (txf_offset, svm_fifo_shared_t *);
396
397 clib_rwlock_reader_lock (&vcm->segment_table_lock);
398
399 fs = fifo_segment_get_segment (&vcm->segment_main, fs_index);
400 rxf = fifo_segment_alloc_fifo_w_shared (fs, rxsf);
401 txf = fifo_segment_alloc_fifo_w_shared (fs, txsf);
402
403 clib_rwlock_reader_unlock (&vcm->segment_table_lock);
404
405 if (!is_ct)
406 {
407 rxsf->client_session_index = s->session_index;
408 txsf->client_session_index = s->session_index;
409 rxf->client_thread_index = vcl_get_worker_index ();
410 txf->client_thread_index = vcl_get_worker_index ();
411 s->rx_fifo = rxf;
412 s->tx_fifo = txf;
413 }
414 else
415 {
416 s->ct_rx_fifo = rxf;
417 s->ct_tx_fifo = txf;
418 }
419
420 return 0;
421}
Florin Corasc4c4cf52019-08-24 18:17:34 -0700422
Florin Coras99368312018-08-02 10:45:44 -0700423/*
424 * fd.io coding-style-patch-verification: ON
425 *
426 * Local Variables:
427 * eval: (c-set-style "gnu")
428 * End:
429 */