blob: b6ef815b53d5297ab1aff302c1ae498f49e52dcb [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
Filip Tehlar8ccc6b32022-02-02 17:38:20 +000041/* Add unix socket to epoll.
42 * Used only to get a notification on socket close
43 * We can't use eventfd because we don't get notifications on that fds
44 */
45static int
46vcl_mq_epoll_add_api_sock (vcl_worker_t *wrk)
47{
48 clib_socket_t *cs = &wrk->app_api_sock;
49 struct epoll_event e = { 0 };
50 int rv;
51
52 e.data.u32 = ~0;
53 rv = epoll_ctl (wrk->mqs_epfd, EPOLL_CTL_ADD, cs->fd, &e);
54 if (rv != EEXIST && rv < 0)
55 return -1;
56
57 return 0;
58}
59
Florin Coras99368312018-08-02 10:45:44 -070060int
Florin Coras134a9962018-08-28 11:32:04 -070061vcl_mq_epoll_add_evfd (vcl_worker_t * wrk, svm_msg_q_t * mq)
Florin Coras99368312018-08-02 10:45:44 -070062{
63 struct epoll_event e = { 0 };
64 vcl_mq_evt_conn_t *mqc;
65 u32 mqc_index;
66 int mq_fd;
67
Florin Coras86f12322021-01-22 15:05:14 -080068 mq_fd = svm_msg_q_get_eventfd (mq);
Florin Coras99368312018-08-02 10:45:44 -070069
Florin Coras134a9962018-08-28 11:32:04 -070070 if (wrk->mqs_epfd < 0 || mq_fd == -1)
Florin Coras99368312018-08-02 10:45:44 -070071 return -1;
72
Florin Coras134a9962018-08-28 11:32:04 -070073 mqc = vcl_mq_evt_conn_alloc (wrk);
74 mqc_index = vcl_mq_evt_conn_index (wrk, mqc);
Florin Coras99368312018-08-02 10:45:44 -070075 mqc->mq_fd = mq_fd;
76 mqc->mq = mq;
77
78 e.events = EPOLLIN;
79 e.data.u32 = mqc_index;
Florin Coras134a9962018-08-28 11:32:04 -070080 if (epoll_ctl (wrk->mqs_epfd, EPOLL_CTL_ADD, mq_fd, &e) < 0)
Florin Coras99368312018-08-02 10:45:44 -070081 {
Florin Coras5e062572019-03-14 19:07:51 -070082 VDBG (0, "failed to add mq eventfd to mq epoll fd");
Florin Coras99368312018-08-02 10:45:44 -070083 return -1;
84 }
85
Filip Tehlar8ccc6b32022-02-02 17:38:20 +000086 if (vcl_mq_epoll_add_api_sock (wrk))
87 {
88 VDBG (0, "failed to add mq socket to mq epoll fd");
89 return -1;
90 }
91
Florin Coras99368312018-08-02 10:45:44 -070092 return mqc_index;
93}
94
95int
Florin Coras134a9962018-08-28 11:32:04 -070096vcl_mq_epoll_del_evfd (vcl_worker_t * wrk, u32 mqc_index)
Florin Coras99368312018-08-02 10:45:44 -070097{
98 vcl_mq_evt_conn_t *mqc;
99
Florin Coras134a9962018-08-28 11:32:04 -0700100 if (wrk->mqs_epfd || mqc_index == ~0)
Florin Coras99368312018-08-02 10:45:44 -0700101 return -1;
102
Florin Coras134a9962018-08-28 11:32:04 -0700103 mqc = vcl_mq_evt_conn_get (wrk, mqc_index);
104 if (epoll_ctl (wrk->mqs_epfd, EPOLL_CTL_DEL, mqc->mq_fd, 0) < 0)
Florin Coras99368312018-08-02 10:45:44 -0700105 {
Florin Coras5e062572019-03-14 19:07:51 -0700106 VDBG (0, "failed to del mq eventfd to mq epoll fd");
Florin Coras99368312018-08-02 10:45:44 -0700107 return -1;
108 }
109 return 0;
110}
111
Florin Coras134a9962018-08-28 11:32:04 -0700112static vcl_worker_t *
113vcl_worker_alloc (void)
114{
115 vcl_worker_t *wrk;
116 pool_get (vcm->workers, wrk);
117 memset (wrk, 0, sizeof (*wrk));
118 wrk->wrk_index = wrk - vcm->workers;
Florin Coras01f3f892018-12-02 12:45:53 -0800119 wrk->forked_child = ~0;
Florin Coras134a9962018-08-28 11:32:04 -0700120 return wrk;
121}
122
123static void
124vcl_worker_free (vcl_worker_t * wrk)
125{
126 pool_put (vcm->workers, wrk);
127}
128
Florin Coras935ce752020-09-08 22:43:47 -0700129int
130vcl_api_app_worker_add (void)
131{
132 if (vcm->cfg.vpp_app_socket_api)
133 return vcl_sapi_app_worker_add ();
134
135 return vcl_bapi_app_worker_add ();
136}
137
138void
139vcl_api_app_worker_del (vcl_worker_t * wrk)
140{
141 if (vcm->cfg.vpp_app_socket_api)
142 return vcl_sapi_app_worker_del (wrk);
143
144 vcl_bapi_app_worker_del (wrk);
145}
146
Florin Coras47c40e22018-11-26 17:01:36 -0800147void
Florin Coras01f3f892018-12-02 12:45:53 -0800148vcl_worker_cleanup (vcl_worker_t * wrk, u8 notify_vpp)
Florin Coras134a9962018-08-28 11:32:04 -0700149{
Florin Coras47c40e22018-11-26 17:01:36 -0800150 clib_spinlock_lock (&vcm->workers_lock);
Florin Coras940f78f2018-11-30 12:11:20 -0800151 if (notify_vpp)
Florin Coras935ce752020-09-08 22:43:47 -0700152 vcl_api_app_worker_del (wrk);
Florin Coras64cf4592019-12-12 12:01:24 -0800153
Florin Coras940f78f2018-11-30 12:11:20 -0800154 if (wrk->mqs_epfd > 0)
155 close (wrk->mqs_epfd);
Florin Corascba215d2021-06-16 14:41:01 -0700156 pool_free (wrk->sessions);
157 pool_free (wrk->mq_evt_conns);
Florin Coras134a9962018-08-28 11:32:04 -0700158 hash_free (wrk->session_index_by_vpp_handles);
Florin Coras134a9962018-08-28 11:32:04 -0700159 vec_free (wrk->mq_events);
160 vec_free (wrk->mq_msg_vector);
Florin Corascba215d2021-06-16 14:41:01 -0700161 vec_free (wrk->unhandled_evts_vector);
162 vec_free (wrk->pending_session_wrk_updates);
163 clib_bitmap_free (wrk->rd_bitmap);
164 clib_bitmap_free (wrk->wr_bitmap);
165 clib_bitmap_free (wrk->ex_bitmap);
Florin Coras134a9962018-08-28 11:32:04 -0700166 vcl_worker_free (wrk);
Florin Coras47c40e22018-11-26 17:01:36 -0800167 clib_spinlock_unlock (&vcm->workers_lock);
Florin Coras47c40e22018-11-26 17:01:36 -0800168}
169
170static void
171vcl_worker_cleanup_cb (void *arg)
172{
Florin Coras70e21972021-04-07 00:16:37 -0700173 vcl_worker_t *wrk;
174 u32 wrk_index;
175
176 wrk_index = vcl_get_worker_index ();
177 wrk = vcl_worker_get_if_valid (wrk_index);
178 if (!wrk)
179 return;
180
Florin Coras01f3f892018-12-02 12:45:53 -0800181 vcl_worker_cleanup (wrk, 1 /* notify vpp */ );
182 vcl_set_worker_index (~0);
Florin Coras940f78f2018-11-30 12:11:20 -0800183 VDBG (0, "cleaned up worker %u", wrk_index);
Florin Coras134a9962018-08-28 11:32:04 -0700184}
185
Filip Tehlar8ccc6b32022-02-02 17:38:20 +0000186void
187vcl_worker_detach_sessions (vcl_worker_t *wrk)
188{
189 session_event_t *e;
190 vcl_session_t *s;
191
192 close (wrk->app_api_sock.fd);
193 pool_foreach (s, wrk->sessions)
194 {
195 if (s->session_state == VCL_STATE_LISTEN)
196 {
197 s->session_state = VCL_STATE_LISTEN_NO_MQ;
198 continue;
199 }
200 if (s->flags & VCL_SESSION_F_IS_VEP)
201 continue;
202
203 s->session_state = VCL_STATE_DETACHED;
204 vec_add2 (wrk->unhandled_evts_vector, e, 1);
205 e->event_type = SESSION_CTRL_EVT_DISCONNECTED;
206 e->session_index = s->session_index;
207 e->postponed = 1;
208 }
209
210 vcl_segment_detach_all ();
211}
212
Florin Coras134a9962018-08-28 11:32:04 -0700213vcl_worker_t *
214vcl_worker_alloc_and_init ()
215{
216 vcl_worker_t *wrk;
217
218 /* This was initialized already */
219 if (vcl_get_worker_index () != ~0)
220 return 0;
221
Florin Coras94fef3e2021-09-23 15:08:05 -0700222 /* Grab lock before selecting mem thread index */
223 clib_spinlock_lock (&vcm->workers_lock);
224
Florin Corasd2c9e702019-08-02 10:53:01 -0700225 /* Use separate heap map entry for worker */
226 clib_mem_set_thread_index ();
227
Florin Corasde9f08b2018-09-07 14:32:58 -0700228 if (pool_elts (vcm->workers) == vcm->cfg.max_workers)
229 {
230 VDBG (0, "max-workers %u limit reached", vcm->cfg.max_workers);
Florin Coras94fef3e2021-09-23 15:08:05 -0700231 wrk = 0;
232 goto done;
Florin Corasde9f08b2018-09-07 14:32:58 -0700233 }
234
Florin Coras134a9962018-08-28 11:32:04 -0700235 wrk = vcl_worker_alloc ();
236 vcl_set_worker_index (wrk->wrk_index);
Florin Coras47c40e22018-11-26 17:01:36 -0800237 wrk->thread_id = pthread_self ();
238 wrk->current_pid = getpid ();
Florin Coras134a9962018-08-28 11:32:04 -0700239
240 wrk->mqs_epfd = -1;
241 if (vcm->cfg.use_mq_eventfd)
242 {
hanlina3a48962020-07-13 11:09:15 +0800243 wrk->vcl_needs_real_epoll = 1;
Florin Coras134a9962018-08-28 11:32:04 -0700244 wrk->mqs_epfd = epoll_create (1);
hanlina3a48962020-07-13 11:09:15 +0800245 wrk->vcl_needs_real_epoll = 0;
Florin Coras134a9962018-08-28 11:32:04 -0700246 if (wrk->mqs_epfd < 0)
247 {
248 clib_unix_warning ("epoll_create() returned");
Florin Coras47c40e22018-11-26 17:01:36 -0800249 goto done;
Florin Coras134a9962018-08-28 11:32:04 -0700250 }
251 }
252
Florin Corasfa3884f2021-06-22 20:04:46 -0700253 wrk->ep_lt_current = VCL_INVALID_SESSION_INDEX;
Florin Coras134a9962018-08-28 11:32:04 -0700254 wrk->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
Florin Coras134a9962018-08-28 11:32:04 -0700255 clib_time_init (&wrk->clib_time);
256 vec_validate (wrk->mq_events, 64);
257 vec_validate (wrk->mq_msg_vector, 128);
258 vec_reset_length (wrk->mq_msg_vector);
Florin Coras86f04502018-09-12 16:08:01 -0700259 vec_validate (wrk->unhandled_evts_vector, 128);
260 vec_reset_length (wrk->unhandled_evts_vector);
Florin Coras134a9962018-08-28 11:32:04 -0700261
Florin Coras47c40e22018-11-26 17:01:36 -0800262done:
Florin Coras94fef3e2021-09-23 15:08:05 -0700263 clib_spinlock_unlock (&vcm->workers_lock);
Florin Coras47c40e22018-11-26 17:01:36 -0800264 return wrk;
265}
266
267int
268vcl_worker_register_with_vpp (void)
269{
270 vcl_worker_t *wrk = vcl_worker_get_current ();
271
272 clib_spinlock_lock (&vcm->workers_lock);
Florin Coras134a9962018-08-28 11:32:04 -0700273
Florin Coras935ce752020-09-08 22:43:47 -0700274 if (vcl_api_app_worker_add ())
Florin Coras134a9962018-08-28 11:32:04 -0700275 {
Florin Coras5e062572019-03-14 19:07:51 -0700276 VDBG (0, "failed to add worker to vpp");
Florin Corasb88de902020-09-08 16:47:57 -0700277 clib_spinlock_unlock (&vcm->workers_lock);
Florin Coras47c40e22018-11-26 17:01:36 -0800278 return -1;
Florin Coras134a9962018-08-28 11:32:04 -0700279 }
Florin Coras47c40e22018-11-26 17:01:36 -0800280 if (pthread_key_create (&vcl_worker_stop_key, vcl_worker_cleanup_cb))
Florin Coras5e062572019-03-14 19:07:51 -0700281 VDBG (0, "failed to add pthread cleanup function");
Florin Coras3348a4c2018-09-07 17:09:35 -0700282 if (pthread_setspecific (vcl_worker_stop_key, &wrk->thread_id))
Florin Coras5e062572019-03-14 19:07:51 -0700283 VDBG (0, "failed to setup key value");
Florin Coras134a9962018-08-28 11:32:04 -0700284
Florin Corasde9f08b2018-09-07 14:32:58 -0700285 clib_spinlock_unlock (&vcm->workers_lock);
286
Florin Coras134a9962018-08-28 11:32:04 -0700287 VDBG (0, "added worker %u", wrk->wrk_index);
Florin Coras47c40e22018-11-26 17:01:36 -0800288 return 0;
289}
Florin Coras134a9962018-08-28 11:32:04 -0700290
Florin Coras458089b2019-08-21 16:20:44 -0700291svm_msg_q_t *
292vcl_worker_ctrl_mq (vcl_worker_t * wrk)
293{
294 return wrk->ctrl_mq;
295}
296
Florin Coras0ef8ef22019-01-18 08:37:13 -0800297int
Florin Corasc127d5a2020-10-14 16:35:58 -0700298vcl_session_read_ready (vcl_session_t * s)
Florin Coras0ef8ef22019-01-18 08:37:13 -0800299{
Florin Coras6c3b2182020-10-19 18:36:48 -0700300 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Florin Coras0ef8ef22019-01-18 08:37:13 -0800301 {
302 VDBG (0, "ERROR: session %u: cannot read from an epoll session!",
Florin Corasc127d5a2020-10-14 16:35:58 -0700303 s->session_index);
Florin Coras0ef8ef22019-01-18 08:37:13 -0800304 return VPPCOM_EBADFD;
305 }
306
Florin Coras1bc919e2020-10-18 20:17:49 -0700307 if (vcl_session_is_open (s))
Florin Coras0ef8ef22019-01-18 08:37:13 -0800308 {
Florin Coras1bc919e2020-10-18 20:17:49 -0700309 if (vcl_session_is_ct (s))
310 return svm_fifo_max_dequeue_cons (s->ct_rx_fifo);
Florin Coras0ef8ef22019-01-18 08:37:13 -0800311
Florin Coras1bc919e2020-10-18 20:17:49 -0700312 if (s->is_dgram)
313 {
314 session_dgram_pre_hdr_t ph;
315 u32 max_deq;
316
317 max_deq = svm_fifo_max_dequeue_cons (s->rx_fifo);
318 if (max_deq <= SESSION_CONN_HDR_LEN)
319 return 0;
320 if (svm_fifo_peek (s->rx_fifo, 0, sizeof (ph), (u8 *) & ph) < 0)
321 return 0;
322 if (ph.data_length + SESSION_CONN_HDR_LEN > max_deq)
323 return 0;
324
325 return ph.data_length;
326 }
327
328 return svm_fifo_max_dequeue_cons (s->rx_fifo);
329 }
330 else if (s->session_state == VCL_STATE_LISTEN)
331 {
332 return clib_fifo_elts (s->accept_evts_fifo);
333 }
334 else
335 {
336 return (s->session_state == VCL_STATE_DISCONNECT) ?
Florin Corasc127d5a2020-10-14 16:35:58 -0700337 VPPCOM_ECONNRESET : VPPCOM_ENOTCONN;
Florin Coras0ef8ef22019-01-18 08:37:13 -0800338 }
Florin Coras0ef8ef22019-01-18 08:37:13 -0800339}
340
341int
Florin Corasc127d5a2020-10-14 16:35:58 -0700342vcl_session_write_ready (vcl_session_t * s)
Florin Coras0ef8ef22019-01-18 08:37:13 -0800343{
Florin Coras6c3b2182020-10-19 18:36:48 -0700344 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Florin Coras0ef8ef22019-01-18 08:37:13 -0800345 {
346 VDBG (0, "session %u [0x%llx]: cannot write to an epoll session!",
Florin Corasc127d5a2020-10-14 16:35:58 -0700347 s->session_index, s->vpp_handle);
Florin Coras0ef8ef22019-01-18 08:37:13 -0800348 return VPPCOM_EBADFD;
349 }
350
Florin Coras1bc919e2020-10-18 20:17:49 -0700351 if (vcl_session_is_open (s))
352 {
353 if (vcl_session_is_ct (s))
354 return svm_fifo_max_enqueue_prod (s->ct_tx_fifo);
355
356 if (s->is_dgram)
357 {
358 u32 max_enq = svm_fifo_max_enqueue_prod (s->tx_fifo);
359
360 if (max_enq <= sizeof (session_dgram_hdr_t))
361 return 0;
362 return max_enq - sizeof (session_dgram_hdr_t);
363 }
364
365 return svm_fifo_max_enqueue_prod (s->tx_fifo);
366 }
367 else if (s->session_state == VCL_STATE_LISTEN)
Florin Coras0ef8ef22019-01-18 08:37:13 -0800368 {
Florin Corasc127d5a2020-10-14 16:35:58 -0700369 if (s->tx_fifo)
370 return svm_fifo_max_enqueue_prod (s->tx_fifo);
Florin Coras0ef8ef22019-01-18 08:37:13 -0800371 else
372 return VPPCOM_EBADFD;
373 }
liuyacancde17692021-06-24 20:39:02 +0800374 else if (s->session_state == VCL_STATE_UPDATED)
375 {
376 return 0;
377 }
Florin Coras1bc919e2020-10-18 20:17:49 -0700378 else
Florin Coras0ef8ef22019-01-18 08:37:13 -0800379 {
Florin Coras1bc919e2020-10-18 20:17:49 -0700380 return (s->session_state == VCL_STATE_DISCONNECT) ?
Florin Corasc127d5a2020-10-14 16:35:58 -0700381 VPPCOM_ECONNRESET : VPPCOM_ENOTCONN;
Florin Coras0ef8ef22019-01-18 08:37:13 -0800382 }
Florin Coras0ef8ef22019-01-18 08:37:13 -0800383}
384
Florin Corasc4c4cf52019-08-24 18:17:34 -0700385int
Florin Corasa54b62d2021-04-21 09:05:56 -0700386vcl_session_alloc_ext_cfg (vcl_session_t *s,
Florin Coras87f63892021-05-01 16:01:40 -0700387 transport_endpt_ext_cfg_type_t type, u32 len)
Florin Corasa54b62d2021-04-21 09:05:56 -0700388{
389 if (s->ext_config)
390 return -1;
391
Florin Coras87f63892021-05-01 16:01:40 -0700392 s->ext_config = clib_mem_alloc (len);
393 clib_memset (s->ext_config, 0, len);
394 s->ext_config->len = len;
Florin Corasa54b62d2021-04-21 09:05:56 -0700395 s->ext_config->type = type;
396
397 return 0;
398}
399
400int
Florin Corasc4c4cf52019-08-24 18:17:34 -0700401vcl_segment_attach (u64 segment_handle, char *name, ssvm_segment_type_t type,
402 int fd)
403{
404 fifo_segment_create_args_t _a, *a = &_a;
405 int rv;
406
407 memset (a, 0, sizeof (*a));
Jakub Grajciarb4e5e502020-01-31 09:35:29 +0100408 a->segment_name = name;
Florin Corasc4c4cf52019-08-24 18:17:34 -0700409 a->segment_type = type;
410
411 if (type == SSVM_SEGMENT_MEMFD)
412 a->memfd_fd = fd;
413
Florin Corasda8f2182019-12-22 12:48:05 -0800414 clib_rwlock_writer_lock (&vcm->segment_table_lock);
415
Florin Corasc4c4cf52019-08-24 18:17:34 -0700416 if ((rv = fifo_segment_attach (&vcm->segment_main, a)))
417 {
418 clib_warning ("svm_fifo_segment_attach ('%s') failed", name);
419 return rv;
420 }
Florin Corasda8f2182019-12-22 12:48:05 -0800421 hash_set (vcm->segment_table, segment_handle, a->new_segment_indices[0]);
422
423 clib_rwlock_writer_unlock (&vcm->segment_table_lock);
424
Florin Coras4c6bbd72021-02-23 08:07:57 -0800425 vec_free (a->new_segment_indices);
Florin Corasc4c4cf52019-08-24 18:17:34 -0700426 return 0;
427}
428
Florin Corasda8f2182019-12-22 12:48:05 -0800429u32
430vcl_segment_table_lookup (u64 segment_handle)
431{
432 uword *seg_indexp;
433
434 clib_rwlock_reader_lock (&vcm->segment_table_lock);
435 seg_indexp = hash_get (vcm->segment_table, segment_handle);
436 clib_rwlock_reader_unlock (&vcm->segment_table_lock);
437
438 if (!seg_indexp)
439 return VCL_INVALID_SEGMENT_INDEX;
440 return ((u32) * seg_indexp);
441}
442
Florin Corasc4c4cf52019-08-24 18:17:34 -0700443void
444vcl_segment_detach (u64 segment_handle)
445{
446 fifo_segment_main_t *sm = &vcm->segment_main;
447 fifo_segment_t *segment;
448 u32 segment_index;
449
450 segment_index = vcl_segment_table_lookup (segment_handle);
451 if (segment_index == (u32) ~ 0)
452 return;
Florin Corasda8f2182019-12-22 12:48:05 -0800453
454 clib_rwlock_writer_lock (&vcm->segment_table_lock);
455
Florin Corasc4c4cf52019-08-24 18:17:34 -0700456 segment = fifo_segment_get_segment (sm, segment_index);
457 fifo_segment_delete (sm, segment);
Florin Corasda8f2182019-12-22 12:48:05 -0800458 hash_unset (vcm->segment_table, segment_handle);
459
460 clib_rwlock_writer_unlock (&vcm->segment_table_lock);
461
Florin Corasc4c4cf52019-08-24 18:17:34 -0700462 VDBG (0, "detached segment %u handle %u", segment_index, segment_handle);
463}
464
Filip Tehlar8ccc6b32022-02-02 17:38:20 +0000465void
466vcl_segment_detach_all ()
467{
468 u64 *segs = 0, *seg, key;
469 u32 val;
470
471 clib_rwlock_reader_lock (&vcm->segment_table_lock);
472
473 hash_foreach (key, val, vcm->segment_table, ({ vec_add1 (segs, key); }));
474
475 clib_rwlock_reader_unlock (&vcm->segment_table_lock);
476
477 vec_foreach (seg, segs)
478 vcl_segment_detach (seg[0]);
479
480 vec_free (segs);
481}
482
Florin Corasc547e912020-12-08 17:50:45 -0800483int
484vcl_segment_attach_session (uword segment_handle, uword rxf_offset,
Florin Corasf6e284b2021-07-21 18:17:20 -0700485 uword txf_offset, uword mq_offset, u32 mq_index,
486 u8 is_ct, vcl_session_t *s)
Florin Corasc547e912020-12-08 17:50:45 -0800487{
Florin Corasb4624182020-12-11 13:58:12 -0800488 u32 fs_index, eqs_index;
Florin Corasc547e912020-12-08 17:50:45 -0800489 svm_fifo_t *rxf, *txf;
490 fifo_segment_t *fs;
Florin Corasb4624182020-12-11 13:58:12 -0800491 u64 eqs_handle;
Florin Corasc547e912020-12-08 17:50:45 -0800492
493 fs_index = vcl_segment_table_lookup (segment_handle);
494 if (fs_index == VCL_INVALID_SEGMENT_INDEX)
495 {
496 VDBG (0, "ERROR: segment for session %u is not mounted!",
497 s->session_index);
498 return -1;
499 }
500
Florin Coras14f066e2020-12-10 18:52:40 -0800501 if (!is_ct && mq_offset != (uword) ~0)
Florin Corasb4624182020-12-11 13:58:12 -0800502 {
503 eqs_handle = vcl_vpp_worker_segment_handle (0);
504 eqs_index = vcl_segment_table_lookup (eqs_handle);
505 ASSERT (eqs_index != VCL_INVALID_SEGMENT_INDEX);
506 }
507
Florin Corasc547e912020-12-08 17:50:45 -0800508 clib_rwlock_reader_lock (&vcm->segment_table_lock);
509
510 fs = fifo_segment_get_segment (&vcm->segment_main, fs_index);
Florin Coras14f066e2020-12-10 18:52:40 -0800511 rxf = fifo_segment_alloc_fifo_w_offset (fs, rxf_offset);
512 txf = fifo_segment_alloc_fifo_w_offset (fs, txf_offset);
Florin Corascbb5e822021-02-20 10:42:22 -0800513 rxf->segment_index = fs_index;
514 txf->segment_index = fs_index;
Florin Corasc547e912020-12-08 17:50:45 -0800515
Florin Corasb4624182020-12-11 13:58:12 -0800516 if (!is_ct && mq_offset != (uword) ~0)
517 {
518 fs = fifo_segment_get_segment (&vcm->segment_main, eqs_index);
Florin Corasf6e284b2021-07-21 18:17:20 -0700519 s->vpp_evt_q = fifo_segment_msg_q_attach (fs, mq_offset, mq_index);
Florin Corasb4624182020-12-11 13:58:12 -0800520 }
521
Florin Corasc547e912020-12-08 17:50:45 -0800522 clib_rwlock_reader_unlock (&vcm->segment_table_lock);
523
524 if (!is_ct)
525 {
Florin Coras14f066e2020-12-10 18:52:40 -0800526 rxf->shr->client_session_index = s->session_index;
527 txf->shr->client_session_index = s->session_index;
Florin Corasc547e912020-12-08 17:50:45 -0800528 rxf->client_thread_index = vcl_get_worker_index ();
529 txf->client_thread_index = vcl_get_worker_index ();
530 s->rx_fifo = rxf;
531 s->tx_fifo = txf;
532 }
533 else
534 {
535 s->ct_rx_fifo = rxf;
536 s->ct_tx_fifo = txf;
537 }
538
539 return 0;
540}
Florin Corasc4c4cf52019-08-24 18:17:34 -0700541
Florin Corascbb5e822021-02-20 10:42:22 -0800542void
543vcl_session_detach_fifos (vcl_session_t *s)
544{
545 fifo_segment_t *fs;
546
547 if (!s->rx_fifo)
548 return;
549
550 clib_rwlock_reader_lock (&vcm->segment_table_lock);
551
552 fs = fifo_segment_get_segment_if_valid (&vcm->segment_main,
553 s->rx_fifo->segment_index);
554 if (!fs)
555 goto done;
556
557 fifo_segment_free_client_fifo (fs, s->rx_fifo);
558 fifo_segment_free_client_fifo (fs, s->tx_fifo);
559 if (s->ct_rx_fifo)
560 {
Florin Corascf0e2572021-04-14 17:55:02 -0700561 fs = fifo_segment_get_segment_if_valid (&vcm->segment_main,
562 s->ct_rx_fifo->segment_index);
563 if (!fs)
564 goto done;
565
Florin Corascbb5e822021-02-20 10:42:22 -0800566 fifo_segment_free_client_fifo (fs, s->ct_rx_fifo);
567 fifo_segment_free_client_fifo (fs, s->ct_tx_fifo);
568 }
569
570done:
571 clib_rwlock_reader_unlock (&vcm->segment_table_lock);
572}
573
Florin Corasb4624182020-12-11 13:58:12 -0800574int
575vcl_segment_attach_mq (uword segment_handle, uword mq_offset, u32 mq_index,
576 svm_msg_q_t **mq)
577{
578 fifo_segment_t *fs;
579 u32 fs_index;
580
581 fs_index = vcl_segment_table_lookup (segment_handle);
582 if (fs_index == VCL_INVALID_SEGMENT_INDEX)
583 {
584 VDBG (0, "ERROR: mq segment %lx for is not attached!", segment_handle);
585 return -1;
586 }
587
588 clib_rwlock_reader_lock (&vcm->segment_table_lock);
589
590 fs = fifo_segment_get_segment (&vcm->segment_main, fs_index);
591 *mq = fifo_segment_msg_q_attach (fs, mq_offset, mq_index);
592
593 clib_rwlock_reader_unlock (&vcm->segment_table_lock);
594
595 return 0;
596}
597
Florin Coras80b74252021-01-27 18:08:25 -0800598int
599vcl_segment_discover_mqs (uword segment_handle, int *fds, u32 n_fds)
600{
601 fifo_segment_t *fs;
602 u32 fs_index;
603
604 fs_index = vcl_segment_table_lookup (segment_handle);
605 if (fs_index == VCL_INVALID_SEGMENT_INDEX)
606 {
607 VDBG (0, "ERROR: mq segment %lx for is not attached!", segment_handle);
608 return -1;
609 }
610
611 clib_rwlock_reader_lock (&vcm->segment_table_lock);
612
613 fs = fifo_segment_get_segment (&vcm->segment_main, fs_index);
614 fifo_segment_msg_qs_discover (fs, fds, n_fds);
615
616 clib_rwlock_reader_unlock (&vcm->segment_table_lock);
617
618 return 0;
619}
620
Florin Coras4ac25842021-04-19 17:34:54 -0700621svm_fifo_chunk_t *
622vcl_segment_alloc_chunk (uword segment_handle, u32 slice_index, u32 size,
623 uword *offset)
624{
625 svm_fifo_chunk_t *c;
626 fifo_segment_t *fs;
627 u32 fs_index;
628
629 fs_index = vcl_segment_table_lookup (segment_handle);
630 if (fs_index == VCL_INVALID_SEGMENT_INDEX)
631 {
632 VDBG (0, "ERROR: mq segment %lx for is not attached!", segment_handle);
633 return 0;
634 }
635
636 clib_rwlock_reader_lock (&vcm->segment_table_lock);
637
638 fs = fifo_segment_get_segment (&vcm->segment_main, fs_index);
639 c = fifo_segment_alloc_chunk_w_slice (fs, slice_index, size);
640 *offset = fifo_segment_chunk_offset (fs, c);
641
642 clib_rwlock_reader_unlock (&vcm->segment_table_lock);
643
644 return c;
645}
646
Florin Coras8eb8d502021-06-16 14:46:57 -0700647int
648vcl_session_share_fifos (vcl_session_t *s, svm_fifo_t *rxf, svm_fifo_t *txf)
649{
650 vcl_worker_t *wrk = vcl_worker_get_current ();
651 fifo_segment_t *fs;
652
653 clib_rwlock_reader_lock (&vcm->segment_table_lock);
654
655 fs = fifo_segment_get_segment (&vcm->segment_main, rxf->segment_index);
656 s->rx_fifo = fifo_segment_duplicate_fifo (fs, rxf);
657 s->tx_fifo = fifo_segment_duplicate_fifo (fs, txf);
658
659 clib_rwlock_reader_unlock (&vcm->segment_table_lock);
660
661 svm_fifo_add_subscriber (s->rx_fifo, wrk->vpp_wrk_index);
662 svm_fifo_add_subscriber (s->tx_fifo, wrk->vpp_wrk_index);
663
664 return 0;
665}
666
Florin Coras60687192021-11-29 21:00:47 -0800667const char *
668vcl_session_state_str (vcl_session_state_t state)
669{
670 char *st;
671
672 switch (state)
673 {
674 case VCL_STATE_CLOSED:
675 st = "STATE_CLOSED";
676 break;
677 case VCL_STATE_LISTEN:
678 st = "STATE_LISTEN";
679 break;
680 case VCL_STATE_READY:
681 st = "STATE_READY";
682 break;
683 case VCL_STATE_VPP_CLOSING:
684 st = "STATE_VPP_CLOSING";
685 break;
686 case VCL_STATE_DISCONNECT:
687 st = "STATE_DISCONNECT";
688 break;
689 case VCL_STATE_DETACHED:
690 st = "STATE_DETACHED";
691 break;
692 case VCL_STATE_UPDATED:
693 st = "STATE_UPDATED";
694 break;
695 case VCL_STATE_LISTEN_NO_MQ:
696 st = "STATE_LISTEN_NO_MQ";
697 break;
698 default:
699 st = "UNKNOWN_STATE";
700 break;
701 }
702
703 return st;
704}
705
706u8 *
707vcl_format_ip4_address (u8 *s, va_list *args)
708{
709 u8 *a = va_arg (*args, u8 *);
710 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
711}
712
713u8 *
714vcl_format_ip6_address (u8 *s, va_list *args)
715{
716 ip6_address_t *a = va_arg (*args, ip6_address_t *);
717 u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
718
719 i_max_n_zero = ARRAY_LEN (a->as_u16);
720 max_n_zeros = 0;
721 i_first_zero = i_max_n_zero;
722 n_zeros = 0;
723 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
724 {
725 u32 is_zero = a->as_u16[i] == 0;
726 if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
727 {
728 i_first_zero = i;
729 n_zeros = 0;
730 }
731 n_zeros += is_zero;
732 if ((!is_zero && n_zeros > max_n_zeros) ||
733 (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
734 {
735 i_max_n_zero = i_first_zero;
736 max_n_zeros = n_zeros;
737 i_first_zero = ARRAY_LEN (a->as_u16);
738 n_zeros = 0;
739 }
740 }
741
742 last_double_colon = 0;
743 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
744 {
745 if (i == i_max_n_zero && max_n_zeros > 1)
746 {
747 s = format (s, "::");
748 i += max_n_zeros - 1;
749 last_double_colon = 1;
750 }
751 else
752 {
753 s = format (s, "%s%x", (last_double_colon || i == 0) ? "" : ":",
754 clib_net_to_host_u16 (a->as_u16[i]));
755 last_double_colon = 0;
756 }
757 }
758
759 return s;
760}
761
762/* Format an IP46 address. */
763u8 *
764vcl_format_ip46_address (u8 *s, va_list *args)
765{
766 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
767 ip46_type_t type = va_arg (*args, ip46_type_t);
768 int is_ip4 = 1;
769
770 switch (type)
771 {
772 case IP46_TYPE_ANY:
773 is_ip4 = ip46_address_is_ip4 (ip46);
774 break;
775 case IP46_TYPE_IP4:
776 is_ip4 = 1;
777 break;
778 case IP46_TYPE_IP6:
779 is_ip4 = 0;
780 break;
781 }
782
783 return is_ip4 ? format (s, "%U", vcl_format_ip4_address, &ip46->ip4) :
784 format (s, "%U", vcl_format_ip6_address, &ip46->ip6);
785}
786
Florin Coras99368312018-08-02 10:45:44 -0700787/*
788 * fd.io coding-style-patch-verification: ON
789 *
790 * Local Variables:
791 * eval: (c-set-style "gnu")
792 * End:
793 */