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);
41 /* 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
46 vcl_mq_epoll_add_api_sock (vcl_worker_t *wrk)
48 clib_socket_t *cs = &wrk->app_api_sock;
49 struct epoll_event e = { 0 };
53 rv = epoll_ctl (wrk->mqs_epfd, EPOLL_CTL_ADD, cs->fd, &e);
54 if (rv != EEXIST && rv < 0)
61 vcl_mq_epoll_add_evfd (vcl_worker_t * wrk, svm_msg_q_t * mq)
63 struct epoll_event e = { 0 };
64 vcl_mq_evt_conn_t *mqc;
68 mq_fd = svm_msg_q_get_eventfd (mq);
70 if (wrk->mqs_epfd < 0 || mq_fd == -1)
73 mqc = vcl_mq_evt_conn_alloc (wrk);
74 mqc_index = vcl_mq_evt_conn_index (wrk, mqc);
79 e.data.u32 = mqc_index;
80 if (epoll_ctl (wrk->mqs_epfd, EPOLL_CTL_ADD, mq_fd, &e) < 0)
82 VDBG (0, "failed to add mq eventfd to mq epoll fd");
86 if (vcl_mq_epoll_add_api_sock (wrk))
88 VDBG (0, "failed to add mq socket to mq epoll fd");
96 vcl_mq_epoll_del_evfd (vcl_worker_t * wrk, u32 mqc_index)
98 vcl_mq_evt_conn_t *mqc;
100 if (wrk->mqs_epfd || mqc_index == ~0)
103 mqc = vcl_mq_evt_conn_get (wrk, mqc_index);
104 if (epoll_ctl (wrk->mqs_epfd, EPOLL_CTL_DEL, mqc->mq_fd, 0) < 0)
106 VDBG (0, "failed to del mq eventfd to mq epoll fd");
112 static vcl_worker_t *
113 vcl_worker_alloc (void)
116 pool_get (vcm->workers, wrk);
117 memset (wrk, 0, sizeof (*wrk));
118 wrk->wrk_index = wrk - vcm->workers;
119 wrk->forked_child = ~0;
124 vcl_worker_free (vcl_worker_t * wrk)
126 pool_put (vcm->workers, wrk);
130 vcl_api_app_worker_add (void)
132 if (vcm->cfg.vpp_app_socket_api)
133 return vcl_sapi_app_worker_add ();
135 return vcl_bapi_app_worker_add ();
139 vcl_api_app_worker_del (vcl_worker_t * wrk)
141 if (wrk->api_client_handle == ~0)
144 if (vcm->cfg.vpp_app_socket_api)
145 return vcl_sapi_app_worker_del (wrk);
147 vcl_bapi_app_worker_del (wrk);
151 vcl_worker_cleanup (vcl_worker_t * wrk, u8 notify_vpp)
153 clib_spinlock_lock (&vcm->workers_lock);
155 vcl_api_app_worker_del (wrk);
157 if (wrk->mqs_epfd > 0)
158 close (wrk->mqs_epfd);
159 pool_free (wrk->sessions);
160 pool_free (wrk->mq_evt_conns);
161 hash_free (wrk->session_index_by_vpp_handles);
162 vec_free (wrk->mq_events);
163 vec_free (wrk->mq_msg_vector);
164 vec_free (wrk->unhandled_evts_vector);
165 vec_free (wrk->pending_session_wrk_updates);
166 clib_bitmap_free (wrk->rd_bitmap);
167 clib_bitmap_free (wrk->wr_bitmap);
168 clib_bitmap_free (wrk->ex_bitmap);
169 vcl_worker_free (wrk);
170 clib_spinlock_unlock (&vcm->workers_lock);
174 vcl_worker_cleanup_cb (void *arg)
179 wrk_index = vcl_get_worker_index ();
180 wrk = vcl_worker_get_if_valid (wrk_index);
184 vcl_worker_cleanup (wrk, 1 /* notify vpp */ );
185 vcl_set_worker_index (~0);
186 VDBG (0, "cleaned up worker %u", wrk_index);
190 vcl_worker_detach_sessions (vcl_worker_t *wrk)
194 uword *seg_indices_map = 0;
195 u32 seg_index, val, *seg_indices = 0;
197 close (wrk->app_api_sock.fd);
198 pool_foreach (s, wrk->sessions)
200 if (s->session_state == VCL_STATE_LISTEN)
202 s->session_state = VCL_STATE_LISTEN_NO_MQ;
205 if ((s->flags & VCL_SESSION_F_IS_VEP) ||
206 s->session_state == VCL_STATE_LISTEN_NO_MQ ||
207 s->session_state == VCL_STATE_CLOSED)
210 hash_set (seg_indices_map, s->tx_fifo->segment_index, 1);
212 s->session_state = VCL_STATE_DETACHED;
213 vec_add2 (wrk->unhandled_evts_vector, e, 1);
214 e->event_type = SESSION_CTRL_EVT_DISCONNECTED;
215 e->session_index = s->session_index;
219 hash_foreach (seg_index, val, seg_indices_map,
220 ({ vec_add1 (seg_indices, seg_index); }));
222 vcl_segment_detach_segments (seg_indices);
224 /* Detach worker's mqs segment */
225 vcl_segment_detach (vcl_vpp_worker_segment_handle (wrk->wrk_index));
227 vec_free (seg_indices);
228 hash_free (seg_indices_map);
232 vcl_worker_alloc_and_init ()
236 /* This was initialized already */
237 if (vcl_get_worker_index () != ~0)
240 /* Grab lock before selecting mem thread index */
241 clib_spinlock_lock (&vcm->workers_lock);
243 /* Use separate heap map entry for worker */
244 clib_mem_set_thread_index ();
246 if (pool_elts (vcm->workers) == vcm->cfg.max_workers)
248 VDBG (0, "max-workers %u limit reached", vcm->cfg.max_workers);
253 wrk = vcl_worker_alloc ();
254 vcl_set_worker_index (wrk->wrk_index);
255 wrk->api_client_handle = ~0;
256 wrk->thread_id = pthread_self ();
257 wrk->current_pid = getpid ();
260 if (vcm->cfg.use_mq_eventfd)
262 wrk->vcl_needs_real_epoll = 1;
263 wrk->mqs_epfd = epoll_create (1);
264 wrk->vcl_needs_real_epoll = 0;
265 if (wrk->mqs_epfd < 0)
267 clib_unix_warning ("epoll_create() returned");
272 wrk->ep_lt_current = VCL_INVALID_SESSION_INDEX;
273 wrk->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
274 clib_time_init (&wrk->clib_time);
275 vec_validate (wrk->mq_events, 64);
276 vec_validate (wrk->mq_msg_vector, 128);
277 vec_reset_length (wrk->mq_msg_vector);
278 vec_validate (wrk->unhandled_evts_vector, 128);
279 vec_reset_length (wrk->unhandled_evts_vector);
282 clib_spinlock_unlock (&vcm->workers_lock);
287 vcl_worker_register_with_vpp (void)
289 vcl_worker_t *wrk = vcl_worker_get_current ();
291 clib_spinlock_lock (&vcm->workers_lock);
293 if (vcl_api_app_worker_add ())
295 VDBG (0, "failed to add worker to vpp");
296 clib_spinlock_unlock (&vcm->workers_lock);
299 if (pthread_key_create (&vcl_worker_stop_key, vcl_worker_cleanup_cb))
300 VDBG (0, "failed to add pthread cleanup function");
301 if (pthread_setspecific (vcl_worker_stop_key, &wrk->thread_id))
302 VDBG (0, "failed to setup key value");
304 clib_spinlock_unlock (&vcm->workers_lock);
306 VDBG (0, "added worker %u", wrk->wrk_index);
311 vcl_worker_ctrl_mq (vcl_worker_t * wrk)
317 vcl_session_read_ready (vcl_session_t * s)
319 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
321 VDBG (0, "ERROR: session %u: cannot read from an epoll session!",
323 return VPPCOM_EBADFD;
326 if (vcl_session_is_open (s))
328 if (vcl_session_is_ct (s))
329 return svm_fifo_max_dequeue_cons (s->ct_rx_fifo);
333 session_dgram_pre_hdr_t ph;
336 max_deq = svm_fifo_max_dequeue_cons (s->rx_fifo);
337 if (max_deq <= SESSION_CONN_HDR_LEN)
339 if (svm_fifo_peek (s->rx_fifo, 0, sizeof (ph), (u8 *) & ph) < 0)
341 if (ph.data_length + SESSION_CONN_HDR_LEN > max_deq)
344 /* Allow zero legth datagrams */
345 return ph.data_length ? ph.data_length : 1;
348 return svm_fifo_max_dequeue_cons (s->rx_fifo);
350 else if (s->session_state == VCL_STATE_LISTEN)
352 return clib_fifo_elts (s->accept_evts_fifo);
356 return (s->session_state == VCL_STATE_DISCONNECT) ?
357 VPPCOM_ECONNRESET : VPPCOM_ENOTCONN;
362 vcl_session_write_ready (vcl_session_t * s)
364 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
366 VDBG (0, "session %u [0x%llx]: cannot write to an epoll session!",
367 s->session_index, s->vpp_handle);
368 return VPPCOM_EBADFD;
371 if (vcl_session_is_open (s))
373 if (vcl_session_is_ct (s))
374 return svm_fifo_max_enqueue_prod (s->ct_tx_fifo);
378 u32 max_enq = svm_fifo_max_enqueue_prod (s->tx_fifo);
380 if (max_enq <= sizeof (session_dgram_hdr_t))
382 return max_enq - sizeof (session_dgram_hdr_t);
385 return svm_fifo_max_enqueue_prod (s->tx_fifo);
387 else if (s->session_state == VCL_STATE_LISTEN)
390 return svm_fifo_max_enqueue_prod (s->tx_fifo);
392 return VPPCOM_EBADFD;
394 else if (s->session_state == VCL_STATE_UPDATED)
400 return (s->session_state == VCL_STATE_DISCONNECT) ?
401 VPPCOM_ECONNRESET : VPPCOM_ENOTCONN;
406 vcl_session_alloc_ext_cfg (vcl_session_t *s,
407 transport_endpt_ext_cfg_type_t type, u32 len)
412 s->ext_config = clib_mem_alloc (len);
413 clib_memset (s->ext_config, 0, len);
414 s->ext_config->len = len;
415 s->ext_config->type = type;
421 vcl_segment_attach (u64 segment_handle, char *name, ssvm_segment_type_t type,
424 fifo_segment_create_args_t _a, *a = &_a;
427 memset (a, 0, sizeof (*a));
428 a->segment_name = name;
429 a->segment_type = type;
431 if (type == SSVM_SEGMENT_MEMFD)
434 clib_rwlock_writer_lock (&vcm->segment_table_lock);
436 if ((rv = fifo_segment_attach (&vcm->segment_main, a)))
438 clib_warning ("svm_fifo_segment_attach ('%s') failed", name);
439 clib_rwlock_writer_unlock (&vcm->segment_table_lock);
442 hash_set (vcm->segment_table, segment_handle, a->new_segment_indices[0]);
444 clib_rwlock_writer_unlock (&vcm->segment_table_lock);
446 vec_free (a->new_segment_indices);
451 vcl_segment_table_lookup (u64 segment_handle)
455 clib_rwlock_reader_lock (&vcm->segment_table_lock);
456 seg_indexp = hash_get (vcm->segment_table, segment_handle);
457 clib_rwlock_reader_unlock (&vcm->segment_table_lock);
460 return VCL_INVALID_SEGMENT_INDEX;
461 return ((u32) * seg_indexp);
465 vcl_segment_detach (u64 segment_handle)
467 fifo_segment_main_t *sm = &vcm->segment_main;
468 fifo_segment_t *segment;
471 segment_index = vcl_segment_table_lookup (segment_handle);
472 if (segment_index == (u32) ~ 0)
475 clib_rwlock_writer_lock (&vcm->segment_table_lock);
477 segment = fifo_segment_get_segment (sm, segment_index);
478 fifo_segment_delete (sm, segment);
479 hash_unset (vcm->segment_table, segment_handle);
481 clib_rwlock_writer_unlock (&vcm->segment_table_lock);
483 VDBG (0, "detached segment %u handle %lx", segment_index, segment_handle);
487 vcl_segment_detach_segments (u32 *seg_indices)
489 u64 *seg_handles = 0, *seg_handle, key;
493 clib_rwlock_reader_lock (&vcm->segment_table_lock);
495 vec_foreach (seg_index, seg_indices)
497 /* clang-format off */
498 hash_foreach (key, val, vcm->segment_table, ({
499 if (val == *seg_index)
501 vec_add1 (seg_handles, key);
505 /* clang-format on */
508 clib_rwlock_reader_unlock (&vcm->segment_table_lock);
510 vec_foreach (seg_handle, seg_handles)
511 vcl_segment_detach (seg_handle[0]);
513 vec_free (seg_handles);
517 vcl_segment_attach_session (uword segment_handle, uword rxf_offset,
518 uword txf_offset, uword mq_offset, u32 mq_index,
519 u8 is_ct, vcl_session_t *s)
521 u32 fs_index, eqs_index;
522 svm_fifo_t *rxf, *txf;
526 fs_index = vcl_segment_table_lookup (segment_handle);
527 if (fs_index == VCL_INVALID_SEGMENT_INDEX)
529 VDBG (0, "ERROR: segment for session %u is not mounted!",
534 if (!is_ct && mq_offset != (uword) ~0)
536 eqs_handle = vcl_vpp_worker_segment_handle (0);
537 eqs_index = vcl_segment_table_lookup (eqs_handle);
538 ASSERT (eqs_index != VCL_INVALID_SEGMENT_INDEX);
541 clib_rwlock_reader_lock (&vcm->segment_table_lock);
543 fs = fifo_segment_get_segment (&vcm->segment_main, fs_index);
544 rxf = fifo_segment_alloc_fifo_w_offset (fs, rxf_offset);
545 txf = fifo_segment_alloc_fifo_w_offset (fs, txf_offset);
546 rxf->segment_index = fs_index;
547 txf->segment_index = fs_index;
549 if (!is_ct && mq_offset != (uword) ~0)
551 fs = fifo_segment_get_segment (&vcm->segment_main, eqs_index);
552 s->vpp_evt_q = fifo_segment_msg_q_attach (fs, mq_offset, mq_index);
555 clib_rwlock_reader_unlock (&vcm->segment_table_lock);
559 rxf->shr->client_session_index = s->session_index;
560 txf->shr->client_session_index = s->session_index;
561 rxf->client_thread_index = vcl_get_worker_index ();
562 txf->client_thread_index = vcl_get_worker_index ();
576 vcl_session_detach_fifos (vcl_session_t *s)
583 clib_rwlock_reader_lock (&vcm->segment_table_lock);
585 fs = fifo_segment_get_segment_if_valid (&vcm->segment_main,
586 s->rx_fifo->segment_index);
590 fifo_segment_free_client_fifo (fs, s->rx_fifo);
591 fifo_segment_free_client_fifo (fs, s->tx_fifo);
594 fs = fifo_segment_get_segment_if_valid (&vcm->segment_main,
595 s->ct_rx_fifo->segment_index);
599 fifo_segment_free_client_fifo (fs, s->ct_rx_fifo);
600 fifo_segment_free_client_fifo (fs, s->ct_tx_fifo);
604 clib_rwlock_reader_unlock (&vcm->segment_table_lock);
608 vcl_segment_attach_mq (uword segment_handle, uword mq_offset, u32 mq_index,
614 fs_index = vcl_segment_table_lookup (segment_handle);
615 if (fs_index == VCL_INVALID_SEGMENT_INDEX)
617 VDBG (0, "ERROR: mq segment %lx for is not attached!", segment_handle);
621 clib_rwlock_reader_lock (&vcm->segment_table_lock);
623 fs = fifo_segment_get_segment (&vcm->segment_main, fs_index);
624 *mq = fifo_segment_msg_q_attach (fs, mq_offset, mq_index);
626 clib_rwlock_reader_unlock (&vcm->segment_table_lock);
632 vcl_segment_discover_mqs (uword segment_handle, int *fds, u32 n_fds)
637 fs_index = vcl_segment_table_lookup (segment_handle);
638 if (fs_index == VCL_INVALID_SEGMENT_INDEX)
640 VDBG (0, "ERROR: mq segment %lx for is not attached!", segment_handle);
644 clib_rwlock_reader_lock (&vcm->segment_table_lock);
646 fs = fifo_segment_get_segment (&vcm->segment_main, fs_index);
647 fifo_segment_msg_qs_discover (fs, fds, n_fds);
649 clib_rwlock_reader_unlock (&vcm->segment_table_lock);
655 vcl_segment_alloc_chunk (uword segment_handle, u32 slice_index, u32 size,
662 fs_index = vcl_segment_table_lookup (segment_handle);
663 if (fs_index == VCL_INVALID_SEGMENT_INDEX)
665 VDBG (0, "ERROR: mq segment %lx for is not attached!", segment_handle);
669 clib_rwlock_reader_lock (&vcm->segment_table_lock);
671 fs = fifo_segment_get_segment (&vcm->segment_main, fs_index);
672 c = fifo_segment_alloc_chunk_w_slice (fs, slice_index, size);
673 *offset = fifo_segment_chunk_offset (fs, c);
675 clib_rwlock_reader_unlock (&vcm->segment_table_lock);
681 vcl_session_share_fifos (vcl_session_t *s, svm_fifo_t *rxf, svm_fifo_t *txf)
683 vcl_worker_t *wrk = vcl_worker_get_current ();
686 clib_rwlock_reader_lock (&vcm->segment_table_lock);
688 fs = fifo_segment_get_segment (&vcm->segment_main, rxf->segment_index);
689 s->rx_fifo = fifo_segment_duplicate_fifo (fs, rxf);
690 s->tx_fifo = fifo_segment_duplicate_fifo (fs, txf);
692 clib_rwlock_reader_unlock (&vcm->segment_table_lock);
694 svm_fifo_add_subscriber (s->rx_fifo, wrk->vpp_wrk_index);
695 svm_fifo_add_subscriber (s->tx_fifo, wrk->vpp_wrk_index);
701 vcl_session_state_str (vcl_session_state_t state)
707 case VCL_STATE_CLOSED:
710 case VCL_STATE_LISTEN:
713 case VCL_STATE_READY:
716 case VCL_STATE_VPP_CLOSING:
717 st = "STATE_VPP_CLOSING";
719 case VCL_STATE_DISCONNECT:
720 st = "STATE_DISCONNECT";
722 case VCL_STATE_DETACHED:
723 st = "STATE_DETACHED";
725 case VCL_STATE_UPDATED:
726 st = "STATE_UPDATED";
728 case VCL_STATE_LISTEN_NO_MQ:
729 st = "STATE_LISTEN_NO_MQ";
732 st = "UNKNOWN_STATE";
740 vcl_format_ip4_address (u8 *s, va_list *args)
742 u8 *a = va_arg (*args, u8 *);
743 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
747 vcl_format_ip6_address (u8 *s, va_list *args)
749 ip6_address_t *a = va_arg (*args, ip6_address_t *);
750 u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
752 i_max_n_zero = ARRAY_LEN (a->as_u16);
754 i_first_zero = i_max_n_zero;
756 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
758 u32 is_zero = a->as_u16[i] == 0;
759 if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
765 if ((!is_zero && n_zeros > max_n_zeros) ||
766 (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
768 i_max_n_zero = i_first_zero;
769 max_n_zeros = n_zeros;
770 i_first_zero = ARRAY_LEN (a->as_u16);
775 last_double_colon = 0;
776 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
778 if (i == i_max_n_zero && max_n_zeros > 1)
780 s = format (s, "::");
781 i += max_n_zeros - 1;
782 last_double_colon = 1;
786 s = format (s, "%s%x", (last_double_colon || i == 0) ? "" : ":",
787 clib_net_to_host_u16 (a->as_u16[i]));
788 last_double_colon = 0;
795 /* Format an IP46 address. */
797 vcl_format_ip46_address (u8 *s, va_list *args)
799 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
800 ip46_type_t type = va_arg (*args, ip46_type_t);
806 is_ip4 = ip46_address_is_ip4 (ip46);
816 return is_ip4 ? format (s, "%U", vcl_format_ip4_address, &ip46->ip4) :
817 format (s, "%U", vcl_format_ip6_address, &ip46->ip6);
821 * fd.io coding-style-patch-verification: ON
824 * eval: (c-set-style "gnu")