blob: 8f8ebf9d2e825434136e49733f70b0e7abca7fe9 [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
Florin Coras86f12322021-01-22 15:05:14 -080049 mq_fd = svm_msg_q_get_eventfd (mq);
Florin Coras99368312018-08-02 10:45:44 -070050
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 Corascba215d2021-06-16 14:41:01 -0700131 pool_free (wrk->sessions);
132 pool_free (wrk->mq_evt_conns);
Florin Coras134a9962018-08-28 11:32:04 -0700133 hash_free (wrk->session_index_by_vpp_handles);
Florin Coras134a9962018-08-28 11:32:04 -0700134 vec_free (wrk->mq_events);
135 vec_free (wrk->mq_msg_vector);
Florin Corascba215d2021-06-16 14:41:01 -0700136 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);
Florin Coras134a9962018-08-28 11:32:04 -0700141 vcl_worker_free (wrk);
Florin Coras47c40e22018-11-26 17:01:36 -0800142 clib_spinlock_unlock (&vcm->workers_lock);
Florin Coras47c40e22018-11-26 17:01:36 -0800143}
144
145static void
146vcl_worker_cleanup_cb (void *arg)
147{
Florin Coras70e21972021-04-07 00:16:37 -0700148 vcl_worker_t *wrk;
149 u32 wrk_index;
150
151 wrk_index = vcl_get_worker_index ();
152 wrk = vcl_worker_get_if_valid (wrk_index);
153 if (!wrk)
154 return;
155
Florin Coras01f3f892018-12-02 12:45:53 -0800156 vcl_worker_cleanup (wrk, 1 /* notify vpp */ );
157 vcl_set_worker_index (~0);
Florin Coras940f78f2018-11-30 12:11:20 -0800158 VDBG (0, "cleaned up worker %u", wrk_index);
Florin Coras134a9962018-08-28 11:32:04 -0700159}
160
161vcl_worker_t *
162vcl_worker_alloc_and_init ()
163{
164 vcl_worker_t *wrk;
165
166 /* This was initialized already */
167 if (vcl_get_worker_index () != ~0)
168 return 0;
169
Florin Corasd2c9e702019-08-02 10:53:01 -0700170 /* Use separate heap map entry for worker */
171 clib_mem_set_thread_index ();
172
Florin Corasde9f08b2018-09-07 14:32:58 -0700173 if (pool_elts (vcm->workers) == vcm->cfg.max_workers)
174 {
175 VDBG (0, "max-workers %u limit reached", vcm->cfg.max_workers);
176 return 0;
177 }
178
179 clib_spinlock_lock (&vcm->workers_lock);
Florin Coras134a9962018-08-28 11:32:04 -0700180 wrk = vcl_worker_alloc ();
181 vcl_set_worker_index (wrk->wrk_index);
Florin Coras47c40e22018-11-26 17:01:36 -0800182 wrk->thread_id = pthread_self ();
183 wrk->current_pid = getpid ();
Florin Coras134a9962018-08-28 11:32:04 -0700184
185 wrk->mqs_epfd = -1;
186 if (vcm->cfg.use_mq_eventfd)
187 {
hanlina3a48962020-07-13 11:09:15 +0800188 wrk->vcl_needs_real_epoll = 1;
Florin Coras134a9962018-08-28 11:32:04 -0700189 wrk->mqs_epfd = epoll_create (1);
hanlina3a48962020-07-13 11:09:15 +0800190 wrk->vcl_needs_real_epoll = 0;
Florin Coras134a9962018-08-28 11:32:04 -0700191 if (wrk->mqs_epfd < 0)
192 {
193 clib_unix_warning ("epoll_create() returned");
Florin Coras47c40e22018-11-26 17:01:36 -0800194 goto done;
Florin Coras134a9962018-08-28 11:32:04 -0700195 }
196 }
197
Florin Corasfa3884f2021-06-22 20:04:46 -0700198 wrk->ep_lt_current = VCL_INVALID_SESSION_INDEX;
Florin Coras134a9962018-08-28 11:32:04 -0700199 wrk->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
Florin Coras134a9962018-08-28 11:32:04 -0700200 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);
Florin Coras86f04502018-09-12 16:08:01 -0700204 vec_validate (wrk->unhandled_evts_vector, 128);
205 vec_reset_length (wrk->unhandled_evts_vector);
Florin Coras47c40e22018-11-26 17:01:36 -0800206 clib_spinlock_unlock (&vcm->workers_lock);
Florin Coras134a9962018-08-28 11:32:04 -0700207
Florin Coras47c40e22018-11-26 17:01:36 -0800208done:
209 return wrk;
210}
211
212int
213vcl_worker_register_with_vpp (void)
214{
215 vcl_worker_t *wrk = vcl_worker_get_current ();
216
217 clib_spinlock_lock (&vcm->workers_lock);
Florin Coras134a9962018-08-28 11:32:04 -0700218
Florin Coras935ce752020-09-08 22:43:47 -0700219 if (vcl_api_app_worker_add ())
Florin Coras134a9962018-08-28 11:32:04 -0700220 {
Florin Coras5e062572019-03-14 19:07:51 -0700221 VDBG (0, "failed to add worker to vpp");
Florin Corasb88de902020-09-08 16:47:57 -0700222 clib_spinlock_unlock (&vcm->workers_lock);
Florin Coras47c40e22018-11-26 17:01:36 -0800223 return -1;
Florin Coras134a9962018-08-28 11:32:04 -0700224 }
Florin Coras47c40e22018-11-26 17:01:36 -0800225 if (pthread_key_create (&vcl_worker_stop_key, vcl_worker_cleanup_cb))
Florin Coras5e062572019-03-14 19:07:51 -0700226 VDBG (0, "failed to add pthread cleanup function");
Florin Coras3348a4c2018-09-07 17:09:35 -0700227 if (pthread_setspecific (vcl_worker_stop_key, &wrk->thread_id))
Florin Coras5e062572019-03-14 19:07:51 -0700228 VDBG (0, "failed to setup key value");
Florin Coras134a9962018-08-28 11:32:04 -0700229
Florin Corasde9f08b2018-09-07 14:32:58 -0700230 clib_spinlock_unlock (&vcm->workers_lock);
231
Florin Coras134a9962018-08-28 11:32:04 -0700232 VDBG (0, "added worker %u", wrk->wrk_index);
Florin Coras47c40e22018-11-26 17:01:36 -0800233 return 0;
234}
Florin Coras134a9962018-08-28 11:32:04 -0700235
Florin Coras458089b2019-08-21 16:20:44 -0700236svm_msg_q_t *
237vcl_worker_ctrl_mq (vcl_worker_t * wrk)
238{
239 return wrk->ctrl_mq;
240}
241
Florin Coras0ef8ef22019-01-18 08:37:13 -0800242int
Florin Corasc127d5a2020-10-14 16:35:58 -0700243vcl_session_read_ready (vcl_session_t * s)
Florin Coras0ef8ef22019-01-18 08:37:13 -0800244{
Florin Coras6c3b2182020-10-19 18:36:48 -0700245 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Florin Coras0ef8ef22019-01-18 08:37:13 -0800246 {
247 VDBG (0, "ERROR: session %u: cannot read from an epoll session!",
Florin Corasc127d5a2020-10-14 16:35:58 -0700248 s->session_index);
Florin Coras0ef8ef22019-01-18 08:37:13 -0800249 return VPPCOM_EBADFD;
250 }
251
Florin Coras1bc919e2020-10-18 20:17:49 -0700252 if (vcl_session_is_open (s))
Florin Coras0ef8ef22019-01-18 08:37:13 -0800253 {
Florin Coras1bc919e2020-10-18 20:17:49 -0700254 if (vcl_session_is_ct (s))
255 return svm_fifo_max_dequeue_cons (s->ct_rx_fifo);
Florin Coras0ef8ef22019-01-18 08:37:13 -0800256
Florin Coras1bc919e2020-10-18 20:17:49 -0700257 if (s->is_dgram)
258 {
259 session_dgram_pre_hdr_t ph;
260 u32 max_deq;
261
262 max_deq = svm_fifo_max_dequeue_cons (s->rx_fifo);
263 if (max_deq <= SESSION_CONN_HDR_LEN)
264 return 0;
265 if (svm_fifo_peek (s->rx_fifo, 0, sizeof (ph), (u8 *) & ph) < 0)
266 return 0;
267 if (ph.data_length + SESSION_CONN_HDR_LEN > max_deq)
268 return 0;
269
270 return ph.data_length;
271 }
272
273 return svm_fifo_max_dequeue_cons (s->rx_fifo);
274 }
275 else if (s->session_state == VCL_STATE_LISTEN)
276 {
277 return clib_fifo_elts (s->accept_evts_fifo);
278 }
279 else
280 {
281 return (s->session_state == VCL_STATE_DISCONNECT) ?
Florin Corasc127d5a2020-10-14 16:35:58 -0700282 VPPCOM_ECONNRESET : VPPCOM_ENOTCONN;
Florin Coras0ef8ef22019-01-18 08:37:13 -0800283 }
Florin Coras0ef8ef22019-01-18 08:37:13 -0800284}
285
286int
Florin Corasc127d5a2020-10-14 16:35:58 -0700287vcl_session_write_ready (vcl_session_t * s)
Florin Coras0ef8ef22019-01-18 08:37:13 -0800288{
Florin Coras6c3b2182020-10-19 18:36:48 -0700289 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Florin Coras0ef8ef22019-01-18 08:37:13 -0800290 {
291 VDBG (0, "session %u [0x%llx]: cannot write to an epoll session!",
Florin Corasc127d5a2020-10-14 16:35:58 -0700292 s->session_index, s->vpp_handle);
Florin Coras0ef8ef22019-01-18 08:37:13 -0800293 return VPPCOM_EBADFD;
294 }
295
Florin Coras1bc919e2020-10-18 20:17:49 -0700296 if (vcl_session_is_open (s))
297 {
298 if (vcl_session_is_ct (s))
299 return svm_fifo_max_enqueue_prod (s->ct_tx_fifo);
300
301 if (s->is_dgram)
302 {
303 u32 max_enq = svm_fifo_max_enqueue_prod (s->tx_fifo);
304
305 if (max_enq <= sizeof (session_dgram_hdr_t))
306 return 0;
307 return max_enq - sizeof (session_dgram_hdr_t);
308 }
309
310 return svm_fifo_max_enqueue_prod (s->tx_fifo);
311 }
312 else if (s->session_state == VCL_STATE_LISTEN)
Florin Coras0ef8ef22019-01-18 08:37:13 -0800313 {
Florin Corasc127d5a2020-10-14 16:35:58 -0700314 if (s->tx_fifo)
315 return svm_fifo_max_enqueue_prod (s->tx_fifo);
Florin Coras0ef8ef22019-01-18 08:37:13 -0800316 else
317 return VPPCOM_EBADFD;
318 }
liuyacancde17692021-06-24 20:39:02 +0800319 else if (s->session_state == VCL_STATE_UPDATED)
320 {
321 return 0;
322 }
Florin Coras1bc919e2020-10-18 20:17:49 -0700323 else
Florin Coras0ef8ef22019-01-18 08:37:13 -0800324 {
Florin Coras1bc919e2020-10-18 20:17:49 -0700325 return (s->session_state == VCL_STATE_DISCONNECT) ?
Florin Corasc127d5a2020-10-14 16:35:58 -0700326 VPPCOM_ECONNRESET : VPPCOM_ENOTCONN;
Florin Coras0ef8ef22019-01-18 08:37:13 -0800327 }
Florin Coras0ef8ef22019-01-18 08:37:13 -0800328}
329
Florin Corasc4c4cf52019-08-24 18:17:34 -0700330int
Florin Corasa54b62d2021-04-21 09:05:56 -0700331vcl_session_alloc_ext_cfg (vcl_session_t *s,
Florin Coras87f63892021-05-01 16:01:40 -0700332 transport_endpt_ext_cfg_type_t type, u32 len)
Florin Corasa54b62d2021-04-21 09:05:56 -0700333{
334 if (s->ext_config)
335 return -1;
336
Florin Coras87f63892021-05-01 16:01:40 -0700337 s->ext_config = clib_mem_alloc (len);
338 clib_memset (s->ext_config, 0, len);
339 s->ext_config->len = len;
Florin Corasa54b62d2021-04-21 09:05:56 -0700340 s->ext_config->type = type;
341
342 return 0;
343}
344
345int
Florin Corasc4c4cf52019-08-24 18:17:34 -0700346vcl_segment_attach (u64 segment_handle, char *name, ssvm_segment_type_t type,
347 int fd)
348{
349 fifo_segment_create_args_t _a, *a = &_a;
350 int rv;
351
352 memset (a, 0, sizeof (*a));
Jakub Grajciarb4e5e502020-01-31 09:35:29 +0100353 a->segment_name = name;
Florin Corasc4c4cf52019-08-24 18:17:34 -0700354 a->segment_type = type;
355
356 if (type == SSVM_SEGMENT_MEMFD)
357 a->memfd_fd = fd;
358
Florin Corasda8f2182019-12-22 12:48:05 -0800359 clib_rwlock_writer_lock (&vcm->segment_table_lock);
360
Florin Corasc4c4cf52019-08-24 18:17:34 -0700361 if ((rv = fifo_segment_attach (&vcm->segment_main, a)))
362 {
363 clib_warning ("svm_fifo_segment_attach ('%s') failed", name);
364 return rv;
365 }
Florin Corasda8f2182019-12-22 12:48:05 -0800366 hash_set (vcm->segment_table, segment_handle, a->new_segment_indices[0]);
367
368 clib_rwlock_writer_unlock (&vcm->segment_table_lock);
369
Florin Coras4c6bbd72021-02-23 08:07:57 -0800370 vec_free (a->new_segment_indices);
Florin Corasc4c4cf52019-08-24 18:17:34 -0700371 return 0;
372}
373
Florin Corasda8f2182019-12-22 12:48:05 -0800374u32
375vcl_segment_table_lookup (u64 segment_handle)
376{
377 uword *seg_indexp;
378
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);
382
383 if (!seg_indexp)
384 return VCL_INVALID_SEGMENT_INDEX;
385 return ((u32) * seg_indexp);
386}
387
Florin Corasc4c4cf52019-08-24 18:17:34 -0700388void
389vcl_segment_detach (u64 segment_handle)
390{
391 fifo_segment_main_t *sm = &vcm->segment_main;
392 fifo_segment_t *segment;
393 u32 segment_index;
394
395 segment_index = vcl_segment_table_lookup (segment_handle);
396 if (segment_index == (u32) ~ 0)
397 return;
Florin Corasda8f2182019-12-22 12:48:05 -0800398
399 clib_rwlock_writer_lock (&vcm->segment_table_lock);
400
Florin Corasc4c4cf52019-08-24 18:17:34 -0700401 segment = fifo_segment_get_segment (sm, segment_index);
402 fifo_segment_delete (sm, segment);
Florin Corasda8f2182019-12-22 12:48:05 -0800403 hash_unset (vcm->segment_table, segment_handle);
404
405 clib_rwlock_writer_unlock (&vcm->segment_table_lock);
406
Florin Corasc4c4cf52019-08-24 18:17:34 -0700407 VDBG (0, "detached segment %u handle %u", segment_index, segment_handle);
408}
409
Florin Corasc547e912020-12-08 17:50:45 -0800410int
411vcl_segment_attach_session (uword segment_handle, uword rxf_offset,
Florin Corasf6e284b2021-07-21 18:17:20 -0700412 uword txf_offset, uword mq_offset, u32 mq_index,
413 u8 is_ct, vcl_session_t *s)
Florin Corasc547e912020-12-08 17:50:45 -0800414{
Florin Corasb4624182020-12-11 13:58:12 -0800415 u32 fs_index, eqs_index;
Florin Corasc547e912020-12-08 17:50:45 -0800416 svm_fifo_t *rxf, *txf;
417 fifo_segment_t *fs;
Florin Corasb4624182020-12-11 13:58:12 -0800418 u64 eqs_handle;
Florin Corasc547e912020-12-08 17:50:45 -0800419
420 fs_index = vcl_segment_table_lookup (segment_handle);
421 if (fs_index == VCL_INVALID_SEGMENT_INDEX)
422 {
423 VDBG (0, "ERROR: segment for session %u is not mounted!",
424 s->session_index);
425 return -1;
426 }
427
Florin Coras14f066e2020-12-10 18:52:40 -0800428 if (!is_ct && mq_offset != (uword) ~0)
Florin Corasb4624182020-12-11 13:58:12 -0800429 {
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);
433 }
434
Florin Corasc547e912020-12-08 17:50:45 -0800435 clib_rwlock_reader_lock (&vcm->segment_table_lock);
436
437 fs = fifo_segment_get_segment (&vcm->segment_main, fs_index);
Florin Coras14f066e2020-12-10 18:52:40 -0800438 rxf = fifo_segment_alloc_fifo_w_offset (fs, rxf_offset);
439 txf = fifo_segment_alloc_fifo_w_offset (fs, txf_offset);
Florin Corascbb5e822021-02-20 10:42:22 -0800440 rxf->segment_index = fs_index;
441 txf->segment_index = fs_index;
Florin Corasc547e912020-12-08 17:50:45 -0800442
Florin Corasb4624182020-12-11 13:58:12 -0800443 if (!is_ct && mq_offset != (uword) ~0)
444 {
445 fs = fifo_segment_get_segment (&vcm->segment_main, eqs_index);
Florin Corasf6e284b2021-07-21 18:17:20 -0700446 s->vpp_evt_q = fifo_segment_msg_q_attach (fs, mq_offset, mq_index);
Florin Corasb4624182020-12-11 13:58:12 -0800447 }
448
Florin Corasc547e912020-12-08 17:50:45 -0800449 clib_rwlock_reader_unlock (&vcm->segment_table_lock);
450
451 if (!is_ct)
452 {
Florin Coras14f066e2020-12-10 18:52:40 -0800453 rxf->shr->client_session_index = s->session_index;
454 txf->shr->client_session_index = s->session_index;
Florin Corasc547e912020-12-08 17:50:45 -0800455 rxf->client_thread_index = vcl_get_worker_index ();
456 txf->client_thread_index = vcl_get_worker_index ();
457 s->rx_fifo = rxf;
458 s->tx_fifo = txf;
459 }
460 else
461 {
462 s->ct_rx_fifo = rxf;
463 s->ct_tx_fifo = txf;
464 }
465
466 return 0;
467}
Florin Corasc4c4cf52019-08-24 18:17:34 -0700468
Florin Corascbb5e822021-02-20 10:42:22 -0800469void
470vcl_session_detach_fifos (vcl_session_t *s)
471{
472 fifo_segment_t *fs;
473
474 if (!s->rx_fifo)
475 return;
476
477 clib_rwlock_reader_lock (&vcm->segment_table_lock);
478
479 fs = fifo_segment_get_segment_if_valid (&vcm->segment_main,
480 s->rx_fifo->segment_index);
481 if (!fs)
482 goto done;
483
484 fifo_segment_free_client_fifo (fs, s->rx_fifo);
485 fifo_segment_free_client_fifo (fs, s->tx_fifo);
486 if (s->ct_rx_fifo)
487 {
Florin Corascf0e2572021-04-14 17:55:02 -0700488 fs = fifo_segment_get_segment_if_valid (&vcm->segment_main,
489 s->ct_rx_fifo->segment_index);
490 if (!fs)
491 goto done;
492
Florin Corascbb5e822021-02-20 10:42:22 -0800493 fifo_segment_free_client_fifo (fs, s->ct_rx_fifo);
494 fifo_segment_free_client_fifo (fs, s->ct_tx_fifo);
495 }
496
497done:
498 clib_rwlock_reader_unlock (&vcm->segment_table_lock);
499}
500
Florin Corasb4624182020-12-11 13:58:12 -0800501int
502vcl_segment_attach_mq (uword segment_handle, uword mq_offset, u32 mq_index,
503 svm_msg_q_t **mq)
504{
505 fifo_segment_t *fs;
506 u32 fs_index;
507
508 fs_index = vcl_segment_table_lookup (segment_handle);
509 if (fs_index == VCL_INVALID_SEGMENT_INDEX)
510 {
511 VDBG (0, "ERROR: mq segment %lx for is not attached!", segment_handle);
512 return -1;
513 }
514
515 clib_rwlock_reader_lock (&vcm->segment_table_lock);
516
517 fs = fifo_segment_get_segment (&vcm->segment_main, fs_index);
518 *mq = fifo_segment_msg_q_attach (fs, mq_offset, mq_index);
519
520 clib_rwlock_reader_unlock (&vcm->segment_table_lock);
521
522 return 0;
523}
524
Florin Coras80b74252021-01-27 18:08:25 -0800525int
526vcl_segment_discover_mqs (uword segment_handle, int *fds, u32 n_fds)
527{
528 fifo_segment_t *fs;
529 u32 fs_index;
530
531 fs_index = vcl_segment_table_lookup (segment_handle);
532 if (fs_index == VCL_INVALID_SEGMENT_INDEX)
533 {
534 VDBG (0, "ERROR: mq segment %lx for is not attached!", segment_handle);
535 return -1;
536 }
537
538 clib_rwlock_reader_lock (&vcm->segment_table_lock);
539
540 fs = fifo_segment_get_segment (&vcm->segment_main, fs_index);
541 fifo_segment_msg_qs_discover (fs, fds, n_fds);
542
543 clib_rwlock_reader_unlock (&vcm->segment_table_lock);
544
545 return 0;
546}
547
Florin Coras4ac25842021-04-19 17:34:54 -0700548svm_fifo_chunk_t *
549vcl_segment_alloc_chunk (uword segment_handle, u32 slice_index, u32 size,
550 uword *offset)
551{
552 svm_fifo_chunk_t *c;
553 fifo_segment_t *fs;
554 u32 fs_index;
555
556 fs_index = vcl_segment_table_lookup (segment_handle);
557 if (fs_index == VCL_INVALID_SEGMENT_INDEX)
558 {
559 VDBG (0, "ERROR: mq segment %lx for is not attached!", segment_handle);
560 return 0;
561 }
562
563 clib_rwlock_reader_lock (&vcm->segment_table_lock);
564
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);
568
569 clib_rwlock_reader_unlock (&vcm->segment_table_lock);
570
571 return c;
572}
573
Florin Coras8eb8d502021-06-16 14:46:57 -0700574int
575vcl_session_share_fifos (vcl_session_t *s, svm_fifo_t *rxf, svm_fifo_t *txf)
576{
577 vcl_worker_t *wrk = vcl_worker_get_current ();
578 fifo_segment_t *fs;
579
580 clib_rwlock_reader_lock (&vcm->segment_table_lock);
581
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);
585
586 clib_rwlock_reader_unlock (&vcm->segment_table_lock);
587
588 svm_fifo_add_subscriber (s->rx_fifo, wrk->vpp_wrk_index);
589 svm_fifo_add_subscriber (s->tx_fifo, wrk->vpp_wrk_index);
590
591 return 0;
592}
593
Florin Coras99368312018-08-02 10:45:44 -0700594/*
595 * fd.io coding-style-patch-verification: ON
596 *
597 * Local Variables:
598 * eval: (c-set-style "gnu")
599 * End:
600 */