Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 1 | /* |
Florin Coras | 5e06257 | 2019-03-14 19:07:51 -0700 | [diff] [blame] | 2 | * Copyright (c) 2018-2019 Cisco and/or its affiliates. |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 3 | * 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 | #include <vlibmemory/api.h> |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 18 | |
Filip Tehlar | 0046e97 | 2021-06-26 22:12:08 +0000 | [diff] [blame] | 19 | #include <vnet/format_fns.h> |
| 20 | #include <vnet/session/session.api_enum.h> |
| 21 | #include <vnet/session/session.api_types.h> |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 22 | |
Filip Tehlar | 0046e97 | 2021-06-26 22:12:08 +0000 | [diff] [blame] | 23 | #define REPLY_MSG_ID_BASE msg_id_base |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 24 | |
Filip Tehlar | 0046e97 | 2021-06-26 22:12:08 +0000 | [diff] [blame] | 25 | static u16 msg_id_base; |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 26 | |
Florin Coras | 00e01d3 | 2019-10-21 16:07:46 -0700 | [diff] [blame] | 27 | static u8 * |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 28 | format_api_error (u8 * s, va_list * args) |
| 29 | { |
| 30 | i32 error = va_arg (*args, u32); |
| 31 | uword *p; |
| 32 | |
| 33 | p = hash_get (vcm->error_string_by_error_number, -error); |
| 34 | |
| 35 | if (p) |
| 36 | s = format (s, "%s (%d)", p[0], error); |
| 37 | else |
| 38 | s = format (s, "%d", error); |
| 39 | return s; |
| 40 | } |
| 41 | |
| 42 | static void |
| 43 | vl_api_session_enable_disable_reply_t_handler |
| 44 | (vl_api_session_enable_disable_reply_t * mp) |
| 45 | { |
Florin Coras | 3b6c84c | 2021-01-05 20:45:44 -0800 | [diff] [blame] | 46 | vcl_worker_t *wrk = vcl_worker_get (0); |
| 47 | |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 48 | if (mp->retval) |
| 49 | { |
| 50 | clib_warning ("VCL<%d>: session_enable_disable failed: %U", getpid (), |
| 51 | format_api_error, ntohl (mp->retval)); |
| 52 | } |
| 53 | else |
Florin Coras | 3b6c84c | 2021-01-05 20:45:44 -0800 | [diff] [blame] | 54 | wrk->bapi_app_state = STATE_APP_ENABLED; |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Florin Coras | d85de68 | 2018-11-29 17:02:29 -0800 | [diff] [blame] | 57 | static void |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 58 | vl_api_app_attach_reply_t_handler (vl_api_app_attach_reply_t * mp) |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 59 | { |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 60 | vcl_worker_t *wrk = vcl_worker_get (0); |
Florin Coras | d85de68 | 2018-11-29 17:02:29 -0800 | [diff] [blame] | 61 | u64 segment_handle; |
Jakub Grajciar | b4e5e50 | 2020-01-31 09:35:29 +0100 | [diff] [blame] | 62 | int *fds = 0, i, rv; |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 63 | u32 n_fds = 0; |
Jakub Grajciar | b4e5e50 | 2020-01-31 09:35:29 +0100 | [diff] [blame] | 64 | char *segment_name = 0; |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 65 | |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 66 | if (mp->retval) |
| 67 | { |
Florin Coras | a5efca3 | 2019-03-20 08:33:10 -0700 | [diff] [blame] | 68 | VERR ("attach failed: %U", format_api_error, ntohl (mp->retval)); |
| 69 | goto failed; |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Florin Coras | 3b6c84c | 2021-01-05 20:45:44 -0800 | [diff] [blame] | 72 | vcl_set_worker_index (0); |
| 73 | |
Florin Coras | d85de68 | 2018-11-29 17:02:29 -0800 | [diff] [blame] | 74 | segment_handle = clib_net_to_host_u64 (mp->segment_handle); |
| 75 | if (segment_handle == VCL_INVALID_SEGMENT_HANDLE) |
| 76 | { |
Florin Coras | a5efca3 | 2019-03-20 08:33:10 -0700 | [diff] [blame] | 77 | VERR ("invalid segment handle"); |
| 78 | goto failed; |
Florin Coras | d85de68 | 2018-11-29 17:02:29 -0800 | [diff] [blame] | 79 | } |
| 80 | |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 81 | if (mp->n_fds) |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 82 | { |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 83 | vec_validate (fds, mp->n_fds); |
Florin Coras | d4c7092 | 2019-11-28 14:21:21 -0800 | [diff] [blame] | 84 | if (vl_socket_client_recv_fd_msg2 (&wrk->bapi_sock_ctx, fds, mp->n_fds, |
| 85 | 5)) |
Florin Coras | 617574d | 2019-07-01 07:32:31 -0700 | [diff] [blame] | 86 | goto failed; |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 87 | |
| 88 | if (mp->fd_flags & SESSION_FD_F_VPP_MQ_SEGMENT) |
Florin Coras | d85de68 | 2018-11-29 17:02:29 -0800 | [diff] [blame] | 89 | if (vcl_segment_attach (vcl_vpp_worker_segment_handle (0), |
| 90 | "vpp-mq-seg", SSVM_SEGMENT_MEMFD, |
| 91 | fds[n_fds++])) |
Florin Coras | a5efca3 | 2019-03-20 08:33:10 -0700 | [diff] [blame] | 92 | goto failed; |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 93 | |
| 94 | if (mp->fd_flags & SESSION_FD_F_MEMFD_SEGMENT) |
Jakub Grajciar | b4e5e50 | 2020-01-31 09:35:29 +0100 | [diff] [blame] | 95 | { |
| 96 | segment_name = vl_api_from_api_to_new_c_string (&mp->segment_name); |
| 97 | rv = |
| 98 | vcl_segment_attach (segment_handle, segment_name, |
| 99 | SSVM_SEGMENT_MEMFD, fds[n_fds++]); |
| 100 | vec_free (segment_name); |
| 101 | if (rv != 0) |
| 102 | goto failed; |
| 103 | } |
| 104 | |
Florin Coras | b462418 | 2020-12-11 13:58:12 -0800 | [diff] [blame] | 105 | vcl_segment_attach_mq (segment_handle, mp->app_mq, 0, |
| 106 | &wrk->app_event_queue); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 107 | |
| 108 | if (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD) |
| 109 | { |
Florin Coras | 86f1232 | 2021-01-22 15:05:14 -0800 | [diff] [blame] | 110 | svm_msg_q_set_eventfd (wrk->app_event_queue, fds[n_fds]); |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 111 | vcl_mq_epoll_add_evfd (wrk, wrk->app_event_queue); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 112 | n_fds++; |
| 113 | } |
| 114 | |
Florin Coras | 80b7425 | 2021-01-27 18:08:25 -0800 | [diff] [blame] | 115 | vcl_segment_discover_mqs (vcl_vpp_worker_segment_handle (0), fds + n_fds, |
| 116 | mp->n_fds - n_fds); |
| 117 | vcl_segment_attach_mq (vcl_vpp_worker_segment_handle (0), |
| 118 | mp->vpp_ctrl_mq, mp->vpp_ctrl_mq_thread, |
| 119 | &wrk->ctrl_mq); |
| 120 | vcm->ctrl_mq = wrk->ctrl_mq; |
| 121 | |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 122 | vec_free (fds); |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 123 | } |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 124 | else |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 125 | { |
Jakub Grajciar | b4e5e50 | 2020-01-31 09:35:29 +0100 | [diff] [blame] | 126 | segment_name = vl_api_from_api_to_new_c_string (&mp->segment_name); |
| 127 | rv = |
| 128 | vcl_segment_attach (segment_handle, segment_name, SSVM_SEGMENT_SHM, |
| 129 | -1); |
| 130 | vec_free (segment_name); |
| 131 | if (rv != 0) |
Florin Coras | a5efca3 | 2019-03-20 08:33:10 -0700 | [diff] [blame] | 132 | goto failed; |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Florin Coras | c1f5a43 | 2018-11-20 11:31:26 -0800 | [diff] [blame] | 135 | vcm->app_index = clib_net_to_host_u32 (mp->app_index); |
Florin Coras | 3b6c84c | 2021-01-05 20:45:44 -0800 | [diff] [blame] | 136 | wrk->bapi_app_state = STATE_APP_ATTACHED; |
Florin Coras | a5efca3 | 2019-03-20 08:33:10 -0700 | [diff] [blame] | 137 | return; |
| 138 | |
| 139 | failed: |
Florin Coras | 3b6c84c | 2021-01-05 20:45:44 -0800 | [diff] [blame] | 140 | wrk->bapi_app_state = STATE_APP_FAILED; |
Florin Coras | 617574d | 2019-07-01 07:32:31 -0700 | [diff] [blame] | 141 | for (i = clib_max (n_fds - 1, 0); i < vec_len (fds); i++) |
| 142 | close (fds[i]); |
| 143 | vec_free (fds); |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | static void |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 147 | vl_api_app_worker_add_del_reply_t_handler (vl_api_app_worker_add_del_reply_t * |
| 148 | mp) |
| 149 | { |
Jakub Grajciar | b4e5e50 | 2020-01-31 09:35:29 +0100 | [diff] [blame] | 150 | int n_fds = 0, *fds = 0, i, rv; |
Florin Coras | d85de68 | 2018-11-29 17:02:29 -0800 | [diff] [blame] | 151 | u64 segment_handle; |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 152 | vcl_worker_t *wrk; |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 153 | u32 wrk_index; |
Jakub Grajciar | b4e5e50 | 2020-01-31 09:35:29 +0100 | [diff] [blame] | 154 | char *segment_name = 0; |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 155 | |
Florin Coras | 3348a4c | 2018-09-07 17:09:35 -0700 | [diff] [blame] | 156 | if (!mp->is_add) |
| 157 | return; |
| 158 | |
Florin Coras | dc2e251 | 2018-12-03 17:47:26 -0800 | [diff] [blame] | 159 | wrk_index = mp->context; |
Florin Coras | 01f3f89 | 2018-12-02 12:45:53 -0800 | [diff] [blame] | 160 | wrk = vcl_worker_get_if_valid (wrk_index); |
| 161 | if (!wrk) |
| 162 | return; |
| 163 | |
Florin Coras | 3b6c84c | 2021-01-05 20:45:44 -0800 | [diff] [blame] | 164 | if (mp->retval) |
| 165 | { |
| 166 | clib_warning ("VCL<%d>: add/del worker failed: %U", getpid (), |
| 167 | format_api_error, ntohl (mp->retval)); |
| 168 | goto failed; |
| 169 | } |
| 170 | |
| 171 | vcl_set_worker_index (wrk_index); |
Florin Coras | dc2e251 | 2018-12-03 17:47:26 -0800 | [diff] [blame] | 172 | wrk->vpp_wrk_index = clib_net_to_host_u32 (mp->wrk_index); |
Florin Coras | 22ba330 | 2019-08-29 22:45:04 -0700 | [diff] [blame] | 173 | wrk->ctrl_mq = vcm->ctrl_mq; |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 174 | |
Florin Coras | d85de68 | 2018-11-29 17:02:29 -0800 | [diff] [blame] | 175 | segment_handle = clib_net_to_host_u64 (mp->segment_handle); |
| 176 | if (segment_handle == VCL_INVALID_SEGMENT_HANDLE) |
| 177 | { |
| 178 | clib_warning ("invalid segment handle"); |
Florin Coras | 01f3f89 | 2018-12-02 12:45:53 -0800 | [diff] [blame] | 179 | goto failed; |
Florin Coras | d85de68 | 2018-11-29 17:02:29 -0800 | [diff] [blame] | 180 | } |
| 181 | |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 182 | if (mp->n_fds) |
| 183 | { |
| 184 | vec_validate (fds, mp->n_fds); |
Florin Coras | d4c7092 | 2019-11-28 14:21:21 -0800 | [diff] [blame] | 185 | if (vl_socket_client_recv_fd_msg2 (&wrk->bapi_sock_ctx, fds, mp->n_fds, |
| 186 | 5)) |
Florin Coras | 617574d | 2019-07-01 07:32:31 -0700 | [diff] [blame] | 187 | goto failed; |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 188 | |
| 189 | if (mp->fd_flags & SESSION_FD_F_VPP_MQ_SEGMENT) |
Florin Coras | 01f3f89 | 2018-12-02 12:45:53 -0800 | [diff] [blame] | 190 | if (vcl_segment_attach (vcl_vpp_worker_segment_handle (wrk_index), |
| 191 | "vpp-worker-seg", SSVM_SEGMENT_MEMFD, |
| 192 | fds[n_fds++])) |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 193 | goto failed; |
| 194 | |
| 195 | if (mp->fd_flags & SESSION_FD_F_MEMFD_SEGMENT) |
Jakub Grajciar | b4e5e50 | 2020-01-31 09:35:29 +0100 | [diff] [blame] | 196 | { |
| 197 | segment_name = vl_api_from_api_to_new_c_string (&mp->segment_name); |
| 198 | rv = |
| 199 | vcl_segment_attach (segment_handle, segment_name, |
| 200 | SSVM_SEGMENT_MEMFD, fds[n_fds++]); |
| 201 | vec_free (segment_name); |
| 202 | if (rv != 0) |
| 203 | goto failed; |
| 204 | } |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 205 | |
Florin Coras | b462418 | 2020-12-11 13:58:12 -0800 | [diff] [blame] | 206 | vcl_segment_attach_mq (segment_handle, mp->app_event_queue_address, 0, |
| 207 | &wrk->app_event_queue); |
| 208 | |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 209 | if (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD) |
| 210 | { |
Florin Coras | 86f1232 | 2021-01-22 15:05:14 -0800 | [diff] [blame] | 211 | svm_msg_q_set_eventfd (wrk->app_event_queue, fds[n_fds]); |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 212 | vcl_mq_epoll_add_evfd (wrk, wrk->app_event_queue); |
| 213 | n_fds++; |
| 214 | } |
| 215 | |
| 216 | vec_free (fds); |
| 217 | } |
| 218 | else |
| 219 | { |
Jakub Grajciar | b4e5e50 | 2020-01-31 09:35:29 +0100 | [diff] [blame] | 220 | segment_name = vl_api_from_api_to_new_c_string (&mp->segment_name); |
| 221 | rv = |
| 222 | vcl_segment_attach (segment_handle, segment_name, SSVM_SEGMENT_SHM, |
| 223 | -1); |
| 224 | vec_free (segment_name); |
| 225 | if (rv != 0) |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 226 | goto failed; |
| 227 | } |
Florin Coras | 3b6c84c | 2021-01-05 20:45:44 -0800 | [diff] [blame] | 228 | wrk->bapi_app_state = STATE_APP_READY; |
Florin Coras | 053a0e4 | 2018-11-13 15:52:38 -0800 | [diff] [blame] | 229 | VDBG (0, "worker %u vpp-worker %u added", wrk_index, wrk->vpp_wrk_index); |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 230 | return; |
| 231 | |
| 232 | failed: |
Florin Coras | 3b6c84c | 2021-01-05 20:45:44 -0800 | [diff] [blame] | 233 | wrk->bapi_app_state = STATE_APP_FAILED; |
Florin Coras | 617574d | 2019-07-01 07:32:31 -0700 | [diff] [blame] | 234 | for (i = clib_max (n_fds - 1, 0); i < vec_len (fds); i++) |
| 235 | close (fds[i]); |
| 236 | vec_free (fds); |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | static void |
Florin Coras | a5a9efd | 2021-01-05 17:03:29 -0800 | [diff] [blame] | 240 | vl_api_app_add_cert_key_pair_reply_t_handler ( |
| 241 | vl_api_app_add_cert_key_pair_reply_t *mp) |
Ping Yu | 34a3a08 | 2018-11-30 19:16:17 -0500 | [diff] [blame] | 242 | { |
Florin Coras | 3b6c84c | 2021-01-05 20:45:44 -0800 | [diff] [blame] | 243 | vcl_worker_t *wrk = vcl_worker_get_current (); |
| 244 | |
Ping Yu | 34a3a08 | 2018-11-30 19:16:17 -0500 | [diff] [blame] | 245 | if (mp->retval) |
Florin Coras | 3b6c84c | 2021-01-05 20:45:44 -0800 | [diff] [blame] | 246 | { |
Florin Coras | a5a9efd | 2021-01-05 17:03:29 -0800 | [diff] [blame] | 247 | VDBG (0, "Adding cert and key failed: %U", format_api_error, |
| 248 | ntohl (mp->retval)); |
Florin Coras | 3b6c84c | 2021-01-05 20:45:44 -0800 | [diff] [blame] | 249 | return; |
| 250 | } |
Florin Coras | a5a9efd | 2021-01-05 17:03:29 -0800 | [diff] [blame] | 251 | wrk->bapi_return = clib_net_to_host_u32 (mp->index); |
Florin Coras | 3b6c84c | 2021-01-05 20:45:44 -0800 | [diff] [blame] | 252 | wrk->bapi_app_state = STATE_APP_READY; |
Ping Yu | 34a3a08 | 2018-11-30 19:16:17 -0500 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | static void |
Florin Coras | a5a9efd | 2021-01-05 17:03:29 -0800 | [diff] [blame] | 256 | vl_api_app_del_cert_key_pair_reply_t_handler ( |
| 257 | vl_api_app_del_cert_key_pair_reply_t *mp) |
Ping Yu | 34a3a08 | 2018-11-30 19:16:17 -0500 | [diff] [blame] | 258 | { |
| 259 | if (mp->retval) |
Florin Coras | 3b6c84c | 2021-01-05 20:45:44 -0800 | [diff] [blame] | 260 | { |
Florin Coras | a5a9efd | 2021-01-05 17:03:29 -0800 | [diff] [blame] | 261 | VDBG (0, "Deleting cert and key failed: %U", format_api_error, |
| 262 | ntohl (mp->retval)); |
Florin Coras | 3b6c84c | 2021-01-05 20:45:44 -0800 | [diff] [blame] | 263 | return; |
| 264 | } |
Ping Yu | 34a3a08 | 2018-11-30 19:16:17 -0500 | [diff] [blame] | 265 | } |
| 266 | |
Florin Coras | a5a9efd | 2021-01-05 17:03:29 -0800 | [diff] [blame] | 267 | #define foreach_sock_msg \ |
| 268 | _ (SESSION_ENABLE_DISABLE_REPLY, session_enable_disable_reply) \ |
| 269 | _ (APP_ATTACH_REPLY, app_attach_reply) \ |
| 270 | _ (APP_ADD_CERT_KEY_PAIR_REPLY, app_add_cert_key_pair_reply) \ |
| 271 | _ (APP_DEL_CERT_KEY_PAIR_REPLY, app_del_cert_key_pair_reply) \ |
| 272 | _ (APP_WORKER_ADD_DEL_REPLY, app_worker_add_del_reply) |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 273 | |
Filip Tehlar | 0046e97 | 2021-06-26 22:12:08 +0000 | [diff] [blame] | 274 | #define vl_endianfun /* define message structures */ |
| 275 | #include <vnet/session/session.api.h> |
| 276 | #undef vl_endianfun |
| 277 | |
Klement Sekera | 9b7e8ac | 2021-11-22 21:26:20 +0100 | [diff] [blame] | 278 | #define vl_calcsizefun |
| 279 | #include <vnet/session/session.api.h> |
| 280 | #undef vl_calcsizefun |
| 281 | |
Filip Tehlar | 0046e97 | 2021-06-26 22:12:08 +0000 | [diff] [blame] | 282 | /* instantiate all the print functions we know about */ |
| 283 | #define vl_printfun |
| 284 | #include <vnet/session/session.api.h> |
| 285 | #undef vl_printfun |
| 286 | |
| 287 | #define vl_api_version(n, v) static u32 api_version = v; |
| 288 | #include <vnet/session/session.api.h> |
| 289 | #undef vl_api_version |
| 290 | |
Florin Coras | b88de90 | 2020-09-08 16:47:57 -0700 | [diff] [blame] | 291 | static void |
| 292 | vcl_bapi_hookup (void) |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 293 | { |
Filip Tehlar | 0046e97 | 2021-06-26 22:12:08 +0000 | [diff] [blame] | 294 | u8 *msg_base_lookup_name = format (0, "session_%08x%c", api_version, 0); |
| 295 | |
| 296 | REPLY_MSG_ID_BASE = |
| 297 | vl_client_get_first_plugin_msg_id ((char *) msg_base_lookup_name); |
| 298 | |
| 299 | vec_free (msg_base_lookup_name); |
| 300 | |
| 301 | if (REPLY_MSG_ID_BASE == (u16) ~0) |
| 302 | return; |
| 303 | |
| 304 | #define _(N, n) \ |
Damjan Marion | a2eb507 | 2022-05-20 20:06:01 +0200 | [diff] [blame] | 305 | vl_msg_api_config (&(vl_msg_api_msg_config_t){ \ |
| 306 | .id = REPLY_MSG_ID_BASE + VL_API_##N, \ |
| 307 | .name = #n, \ |
| 308 | .handler = vl_api_##n##_t_handler, \ |
| 309 | .endian = vl_api_##n##_t_endian, \ |
| 310 | .format_fn = vl_api_##n##_t_format, \ |
| 311 | .size = sizeof (vl_api_##n##_t), \ |
| 312 | .traced = 1, \ |
| 313 | .tojson = vl_api_##n##_t_tojson, \ |
| 314 | .fromjson = vl_api_##n##_t_fromjson, \ |
| 315 | .calc_size = vl_api_##n##_t_calc_size, \ |
| 316 | }); |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 317 | foreach_sock_msg; |
| 318 | #undef _ |
| 319 | } |
| 320 | |
| 321 | /* |
| 322 | * VPP-API message functions |
| 323 | */ |
Florin Coras | b88de90 | 2020-09-08 16:47:57 -0700 | [diff] [blame] | 324 | static void |
| 325 | vcl_bapi_send_session_enable_disable (u8 is_enable) |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 326 | { |
Florin Coras | 47c40e2 | 2018-11-26 17:01:36 -0800 | [diff] [blame] | 327 | vcl_worker_t *wrk = vcl_worker_get_current (); |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 328 | vl_api_session_enable_disable_t *bmp; |
| 329 | bmp = vl_msg_api_alloc (sizeof (*bmp)); |
| 330 | memset (bmp, 0, sizeof (*bmp)); |
| 331 | |
Filip Tehlar | 0046e97 | 2021-06-26 22:12:08 +0000 | [diff] [blame] | 332 | bmp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_SESSION_ENABLE_DISABLE); |
Florin Coras | cc7c88e | 2020-09-15 15:56:51 -0700 | [diff] [blame] | 333 | bmp->client_index = wrk->api_client_handle; |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 334 | bmp->context = htonl (0xfeedface); |
| 335 | bmp->is_enable = is_enable; |
Florin Coras | 47c40e2 | 2018-11-26 17:01:36 -0800 | [diff] [blame] | 336 | vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & bmp); |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | void |
Florin Coras | b88de90 | 2020-09-08 16:47:57 -0700 | [diff] [blame] | 340 | vcl_bapi_send_attach (void) |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 341 | { |
Florin Coras | 47c40e2 | 2018-11-26 17:01:36 -0800 | [diff] [blame] | 342 | vcl_worker_t *wrk = vcl_worker_get_current (); |
Florin Coras | d747c3c | 2019-10-20 19:55:56 -0700 | [diff] [blame] | 343 | u8 tls_engine = CRYPTO_ENGINE_OPENSSL; |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 344 | vl_api_app_attach_t *bmp; |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 345 | u8 nsid_len = vec_len (vcm->cfg.namespace_id); |
| 346 | u8 app_is_proxy = (vcm->cfg.app_proxy_transport_tcp || |
| 347 | vcm->cfg.app_proxy_transport_udp); |
| 348 | |
Florin Coras | d747c3c | 2019-10-20 19:55:56 -0700 | [diff] [blame] | 349 | tls_engine = vcm->cfg.tls_engine ? vcm->cfg.tls_engine : tls_engine; |
| 350 | |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 351 | bmp = vl_msg_api_alloc (sizeof (*bmp)); |
| 352 | memset (bmp, 0, sizeof (*bmp)); |
| 353 | |
Filip Tehlar | 0046e97 | 2021-06-26 22:12:08 +0000 | [diff] [blame] | 354 | bmp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_APP_ATTACH); |
Florin Coras | cc7c88e | 2020-09-15 15:56:51 -0700 | [diff] [blame] | 355 | bmp->client_index = wrk->api_client_handle; |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 356 | bmp->context = htonl (0xfeedface); |
| 357 | bmp->options[APP_OPTIONS_FLAGS] = |
| 358 | APP_OPTIONS_FLAGS_ACCEPT_REDIRECT | APP_OPTIONS_FLAGS_ADD_SEGMENT | |
| 359 | (vcm->cfg.app_scope_local ? APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE : 0) | |
| 360 | (vcm->cfg.app_scope_global ? APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE : 0) | |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 361 | (app_is_proxy ? APP_OPTIONS_FLAGS_IS_PROXY : 0) | |
Junfeng Wang | c795b88 | 2022-08-12 16:24:46 +0800 | [diff] [blame] | 362 | (vcm->cfg.use_mq_eventfd ? APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD : 0) | |
qinyang | af9b715 | 2023-06-27 01:11:53 -0700 | [diff] [blame] | 363 | (vcm->cfg.huge_page ? APP_OPTIONS_FLAGS_USE_HUGE_PAGE : 0) | |
| 364 | (vcm->cfg.app_original_dst ? APP_OPTIONS_FLAGS_GET_ORIGINAL_DST : 0); |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 365 | bmp->options[APP_OPTIONS_PROXY_TRANSPORT] = |
| 366 | (u64) ((vcm->cfg.app_proxy_transport_tcp ? 1 << TRANSPORT_PROTO_TCP : 0) | |
| 367 | (vcm->cfg.app_proxy_transport_udp ? 1 << TRANSPORT_PROTO_UDP : 0)); |
| 368 | bmp->options[APP_OPTIONS_SEGMENT_SIZE] = vcm->cfg.segment_size; |
| 369 | bmp->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = vcm->cfg.add_segment_size; |
| 370 | bmp->options[APP_OPTIONS_RX_FIFO_SIZE] = vcm->cfg.rx_fifo_size; |
| 371 | bmp->options[APP_OPTIONS_TX_FIFO_SIZE] = vcm->cfg.tx_fifo_size; |
| 372 | bmp->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = |
| 373 | vcm->cfg.preallocated_fifo_pairs; |
| 374 | bmp->options[APP_OPTIONS_EVT_QUEUE_SIZE] = vcm->cfg.event_queue_size; |
Florin Coras | d747c3c | 2019-10-20 19:55:56 -0700 | [diff] [blame] | 375 | bmp->options[APP_OPTIONS_TLS_ENGINE] = tls_engine; |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 376 | if (nsid_len) |
| 377 | { |
Jakub Grajciar | b4e5e50 | 2020-01-31 09:35:29 +0100 | [diff] [blame] | 378 | vl_api_vec_to_api_string (vcm->cfg.namespace_id, &bmp->namespace_id); |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 379 | bmp->options[APP_OPTIONS_NAMESPACE_SECRET] = vcm->cfg.namespace_secret; |
| 380 | } |
Florin Coras | 47c40e2 | 2018-11-26 17:01:36 -0800 | [diff] [blame] | 381 | vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & bmp); |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | void |
Florin Coras | b88de90 | 2020-09-08 16:47:57 -0700 | [diff] [blame] | 385 | vcl_bapi_send_detach (void) |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 386 | { |
Florin Coras | 47c40e2 | 2018-11-26 17:01:36 -0800 | [diff] [blame] | 387 | vcl_worker_t *wrk = vcl_worker_get_current (); |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 388 | vl_api_application_detach_t *bmp; |
| 389 | bmp = vl_msg_api_alloc (sizeof (*bmp)); |
| 390 | memset (bmp, 0, sizeof (*bmp)); |
| 391 | |
Filip Tehlar | 0046e97 | 2021-06-26 22:12:08 +0000 | [diff] [blame] | 392 | bmp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_APPLICATION_DETACH); |
Florin Coras | cc7c88e | 2020-09-15 15:56:51 -0700 | [diff] [blame] | 393 | bmp->client_index = wrk->api_client_handle; |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 394 | bmp->context = htonl (0xfeedface); |
Florin Coras | 47c40e2 | 2018-11-26 17:01:36 -0800 | [diff] [blame] | 395 | vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & bmp); |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 396 | } |
| 397 | |
Florin Coras | b88de90 | 2020-09-08 16:47:57 -0700 | [diff] [blame] | 398 | static void |
| 399 | vcl_bapi_send_app_worker_add_del (u8 is_add) |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 400 | { |
| 401 | vcl_worker_t *wrk = vcl_worker_get_current (); |
| 402 | vl_api_app_worker_add_del_t *mp; |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 403 | |
| 404 | mp = vl_msg_api_alloc (sizeof (*mp)); |
| 405 | memset (mp, 0, sizeof (*mp)); |
| 406 | |
Filip Tehlar | 0046e97 | 2021-06-26 22:12:08 +0000 | [diff] [blame] | 407 | mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_APP_WORKER_ADD_DEL); |
Florin Coras | cc7c88e | 2020-09-15 15:56:51 -0700 | [diff] [blame] | 408 | mp->client_index = wrk->api_client_handle; |
Florin Coras | c1f5a43 | 2018-11-20 11:31:26 -0800 | [diff] [blame] | 409 | mp->app_index = clib_host_to_net_u32 (vcm->app_index); |
Florin Coras | 01f3f89 | 2018-12-02 12:45:53 -0800 | [diff] [blame] | 410 | mp->context = wrk->wrk_index; |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 411 | mp->is_add = is_add; |
| 412 | if (!is_add) |
Florin Coras | 01f3f89 | 2018-12-02 12:45:53 -0800 | [diff] [blame] | 413 | mp->wrk_index = clib_host_to_net_u32 (wrk->vpp_wrk_index); |
| 414 | |
| 415 | vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & mp); |
| 416 | } |
| 417 | |
Florin Coras | b88de90 | 2020-09-08 16:47:57 -0700 | [diff] [blame] | 418 | static void |
| 419 | vcl_bapi_send_child_worker_del (vcl_worker_t * child_wrk) |
Florin Coras | 01f3f89 | 2018-12-02 12:45:53 -0800 | [diff] [blame] | 420 | { |
| 421 | vcl_worker_t *wrk = vcl_worker_get_current (); |
| 422 | vl_api_app_worker_add_del_t *mp; |
| 423 | |
| 424 | mp = vl_msg_api_alloc (sizeof (*mp)); |
| 425 | memset (mp, 0, sizeof (*mp)); |
| 426 | |
Filip Tehlar | 0046e97 | 2021-06-26 22:12:08 +0000 | [diff] [blame] | 427 | mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_APP_WORKER_ADD_DEL); |
Florin Coras | cc7c88e | 2020-09-15 15:56:51 -0700 | [diff] [blame] | 428 | mp->client_index = wrk->api_client_handle; |
Florin Coras | 01f3f89 | 2018-12-02 12:45:53 -0800 | [diff] [blame] | 429 | mp->app_index = clib_host_to_net_u32 (vcm->app_index); |
| 430 | mp->context = wrk->wrk_index; |
| 431 | mp->is_add = 0; |
| 432 | mp->wrk_index = clib_host_to_net_u32 (child_wrk->vpp_wrk_index); |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 433 | |
Florin Coras | 47c40e2 | 2018-11-26 17:01:36 -0800 | [diff] [blame] | 434 | vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & mp); |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 435 | } |
| 436 | |
Florin Coras | a5a9efd | 2021-01-05 17:03:29 -0800 | [diff] [blame] | 437 | static void |
| 438 | vcl_bapi_send_app_add_cert_key_pair (vppcom_cert_key_pair_t *ckpair) |
Ping Yu | 34a3a08 | 2018-11-30 19:16:17 -0500 | [diff] [blame] | 439 | { |
| 440 | vcl_worker_t *wrk = vcl_worker_get_current (); |
Sivaprasad Tummala | 39d25e5 | 2021-07-27 17:38:00 +0530 | [diff] [blame] | 441 | u32 cert_len = ckpair->cert_len; |
| 442 | u32 key_len = ckpair->key_len; |
Florin Coras | a5a9efd | 2021-01-05 17:03:29 -0800 | [diff] [blame] | 443 | vl_api_app_add_cert_key_pair_t *bmp; |
Ping Yu | 34a3a08 | 2018-11-30 19:16:17 -0500 | [diff] [blame] | 444 | |
Florin Coras | a5a9efd | 2021-01-05 17:03:29 -0800 | [diff] [blame] | 445 | bmp = vl_msg_api_alloc (sizeof (*bmp) + cert_len + key_len); |
| 446 | clib_memset (bmp, 0, sizeof (*bmp) + cert_len + key_len); |
| 447 | |
Filip Tehlar | 0046e97 | 2021-06-26 22:12:08 +0000 | [diff] [blame] | 448 | bmp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_APP_ADD_CERT_KEY_PAIR); |
Florin Coras | a5a9efd | 2021-01-05 17:03:29 -0800 | [diff] [blame] | 449 | bmp->client_index = wrk->api_client_handle; |
| 450 | bmp->context = wrk->wrk_index; |
| 451 | bmp->cert_len = clib_host_to_net_u16 (cert_len); |
| 452 | bmp->certkey_len = clib_host_to_net_u16 (key_len + cert_len); |
Sivaprasad Tummala | 39d25e5 | 2021-07-27 17:38:00 +0530 | [diff] [blame] | 453 | clib_memcpy_fast (bmp->certkey, ckpair->cert, cert_len); |
| 454 | clib_memcpy_fast (bmp->certkey + cert_len, ckpair->key, key_len); |
Florin Coras | a5a9efd | 2021-01-05 17:03:29 -0800 | [diff] [blame] | 455 | |
| 456 | vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) &bmp); |
Ping Yu | 34a3a08 | 2018-11-30 19:16:17 -0500 | [diff] [blame] | 457 | } |
| 458 | |
Florin Coras | a5a9efd | 2021-01-05 17:03:29 -0800 | [diff] [blame] | 459 | static void |
| 460 | vcl_bapi_send_app_del_cert_key_pair (u32 ckpair_index) |
Ping Yu | 34a3a08 | 2018-11-30 19:16:17 -0500 | [diff] [blame] | 461 | { |
| 462 | vcl_worker_t *wrk = vcl_worker_get_current (); |
Florin Coras | a5a9efd | 2021-01-05 17:03:29 -0800 | [diff] [blame] | 463 | vl_api_app_del_cert_key_pair_t *bmp; |
| 464 | bmp = vl_msg_api_alloc (sizeof (*bmp)); |
| 465 | clib_memset (bmp, 0, sizeof (*bmp)); |
Ping Yu | 34a3a08 | 2018-11-30 19:16:17 -0500 | [diff] [blame] | 466 | |
Filip Tehlar | 0046e97 | 2021-06-26 22:12:08 +0000 | [diff] [blame] | 467 | bmp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_APP_DEL_CERT_KEY_PAIR); |
Florin Coras | a5a9efd | 2021-01-05 17:03:29 -0800 | [diff] [blame] | 468 | bmp->client_index = wrk->api_client_handle; |
| 469 | bmp->context = wrk->wrk_index; |
| 470 | bmp->index = clib_host_to_net_u32 (ckpair_index); |
| 471 | vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) &bmp); |
Ping Yu | 34a3a08 | 2018-11-30 19:16:17 -0500 | [diff] [blame] | 472 | } |
| 473 | |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 474 | u32 |
Florin Coras | b88de90 | 2020-09-08 16:47:57 -0700 | [diff] [blame] | 475 | vcl_bapi_max_nsid_len (void) |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 476 | { |
Florin Coras | 888d9f0 | 2020-04-02 23:00:13 +0000 | [diff] [blame] | 477 | vl_api_app_attach_t *mp; |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 478 | return (sizeof (mp->namespace_id) - 1); |
| 479 | } |
| 480 | |
Florin Coras | b88de90 | 2020-09-08 16:47:57 -0700 | [diff] [blame] | 481 | static void |
| 482 | vcl_bapi_init_error_string_table (void) |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 483 | { |
| 484 | vcm->error_string_by_error_number = hash_create (0, sizeof (uword)); |
| 485 | |
| 486 | #define _(n, v, s) hash_set (vcm->error_string_by_error_number, -v, s); |
| 487 | foreach_vnet_api_error; |
| 488 | #undef _ |
| 489 | |
| 490 | hash_set (vcm->error_string_by_error_number, 99, "Misc"); |
| 491 | } |
| 492 | |
Florin Coras | b88de90 | 2020-09-08 16:47:57 -0700 | [diff] [blame] | 493 | static void |
| 494 | vcl_bapi_cleanup (void) |
| 495 | { |
| 496 | socket_client_main_t *scm = &socket_client_main; |
| 497 | api_main_t *am = vlibapi_get_main (); |
| 498 | |
| 499 | am->my_client_index = ~0; |
| 500 | am->my_registration = 0; |
| 501 | am->vl_input_queue = 0; |
| 502 | am->msg_index_by_name_and_crc = 0; |
| 503 | scm->socket_fd = 0; |
| 504 | |
| 505 | vl_client_api_unmap (); |
| 506 | } |
| 507 | |
| 508 | static int |
| 509 | vcl_bapi_connect_to_vpp (void) |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 510 | { |
Florin Coras | 47c40e2 | 2018-11-26 17:01:36 -0800 | [diff] [blame] | 511 | vcl_worker_t *wrk = vcl_worker_get_current (); |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 512 | vppcom_cfg_t *vcl_cfg = &vcm->cfg; |
Florin Coras | b88de90 | 2020-09-08 16:47:57 -0700 | [diff] [blame] | 513 | int rv = VPPCOM_OK; |
Florin Coras | d4c7092 | 2019-11-28 14:21:21 -0800 | [diff] [blame] | 514 | api_main_t *am; |
Florin Coras | b88de90 | 2020-09-08 16:47:57 -0700 | [diff] [blame] | 515 | u8 *wrk_name; |
| 516 | |
jiangxiaoming | b438c76 | 2020-10-13 14:23:29 +0800 | [diff] [blame] | 517 | wrk_name = format (0, "%v-wrk-%u%c", vcm->app_name, wrk->wrk_index, 0); |
Florin Coras | b88de90 | 2020-09-08 16:47:57 -0700 | [diff] [blame] | 518 | |
| 519 | /* Make sure api is cleaned up in case this is a connect from a |
| 520 | * forked worker */ |
| 521 | vcl_bapi_cleanup (); |
Florin Coras | d4c7092 | 2019-11-28 14:21:21 -0800 | [diff] [blame] | 522 | |
| 523 | vlibapi_set_main (&wrk->bapi_api_ctx); |
Florin Coras | fda21c9 | 2021-04-07 17:03:01 -0700 | [diff] [blame] | 524 | vlibapi_set_memory_client_main (&wrk->bapi_mem_ctx); |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 525 | |
Florin Coras | 165f3ae | 2020-11-08 18:04:33 -0800 | [diff] [blame] | 526 | if (!vcl_cfg->vpp_bapi_socket_name) |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 527 | { |
Florin Coras | 165f3ae | 2020-11-08 18:04:33 -0800 | [diff] [blame] | 528 | rv = VPPCOM_EINVAL; |
| 529 | goto error; |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 530 | } |
Florin Coras | 165f3ae | 2020-11-08 18:04:33 -0800 | [diff] [blame] | 531 | |
| 532 | if (vl_socket_client_connect2 (&wrk->bapi_sock_ctx, |
| 533 | (char *) vcl_cfg->vpp_bapi_socket_name, |
| 534 | (char *) wrk_name, |
| 535 | 0 /* default rx/tx buffer */ )) |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 536 | { |
Florin Coras | 165f3ae | 2020-11-08 18:04:33 -0800 | [diff] [blame] | 537 | VERR ("app (%s) socket connect failed!", wrk_name); |
| 538 | rv = VPPCOM_ECONNREFUSED; |
| 539 | goto error; |
| 540 | } |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 541 | |
Florin Coras | 165f3ae | 2020-11-08 18:04:33 -0800 | [diff] [blame] | 542 | if (vl_socket_client_init_shm2 (&wrk->bapi_sock_ctx, 0, |
| 543 | 1 /* want_pthread */ )) |
| 544 | { |
| 545 | VERR ("app (%s) init shm failed!", wrk_name); |
| 546 | rv = VPPCOM_ECONNREFUSED; |
| 547 | goto error; |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 548 | } |
Filip Tehlar | 0046e97 | 2021-06-26 22:12:08 +0000 | [diff] [blame] | 549 | vcl_bapi_hookup (); |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 550 | |
Florin Coras | d4c7092 | 2019-11-28 14:21:21 -0800 | [diff] [blame] | 551 | am = vlibapi_get_main (); |
Florin Coras | 47c40e2 | 2018-11-26 17:01:36 -0800 | [diff] [blame] | 552 | wrk->vl_input_queue = am->shmem_hdr->vl_input_queue; |
Florin Coras | cc7c88e | 2020-09-15 15:56:51 -0700 | [diff] [blame] | 553 | wrk->api_client_handle = (u32) am->my_client_index; |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 554 | |
Florin Coras | b88de90 | 2020-09-08 16:47:57 -0700 | [diff] [blame] | 555 | VDBG (0, "app (%s) is connected to VPP!", wrk_name); |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 556 | vcl_evt (VCL_EVT_INIT, vcm); |
Florin Coras | b88de90 | 2020-09-08 16:47:57 -0700 | [diff] [blame] | 557 | |
| 558 | error: |
| 559 | vec_free (wrk_name); |
| 560 | return rv; |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 561 | } |
| 562 | |
Florin Coras | 470b2a6 | 2019-08-03 18:53:48 -0700 | [diff] [blame] | 563 | void |
Florin Coras | b88de90 | 2020-09-08 16:47:57 -0700 | [diff] [blame] | 564 | vcl_bapi_disconnect_from_vpp (void) |
Florin Coras | 470b2a6 | 2019-08-03 18:53:48 -0700 | [diff] [blame] | 565 | { |
Florin Coras | 64cf459 | 2019-12-12 12:01:24 -0800 | [diff] [blame] | 566 | vcl_worker_t *wrk = vcl_worker_get_current (); |
Florin Coras | 470b2a6 | 2019-08-03 18:53:48 -0700 | [diff] [blame] | 567 | vppcom_cfg_t *vcl_cfg = &vcm->cfg; |
| 568 | |
Florin Coras | b88de90 | 2020-09-08 16:47:57 -0700 | [diff] [blame] | 569 | if (vcl_cfg->vpp_bapi_socket_name) |
Florin Coras | 64cf459 | 2019-12-12 12:01:24 -0800 | [diff] [blame] | 570 | vl_socket_client_disconnect2 (&wrk->bapi_sock_ctx); |
Florin Coras | 470b2a6 | 2019-08-03 18:53:48 -0700 | [diff] [blame] | 571 | else |
| 572 | vl_client_disconnect_from_vlib (); |
| 573 | } |
| 574 | |
Florin Coras | b88de90 | 2020-09-08 16:47:57 -0700 | [diff] [blame] | 575 | static const char * |
| 576 | vcl_bapi_app_state_str (vcl_bapi_app_state_t state) |
| 577 | { |
| 578 | char *st; |
| 579 | |
| 580 | switch (state) |
| 581 | { |
| 582 | case STATE_APP_START: |
| 583 | st = "STATE_APP_START"; |
| 584 | break; |
| 585 | |
| 586 | case STATE_APP_CONN_VPP: |
| 587 | st = "STATE_APP_CONN_VPP"; |
| 588 | break; |
| 589 | |
| 590 | case STATE_APP_ENABLED: |
| 591 | st = "STATE_APP_ENABLED"; |
| 592 | break; |
| 593 | |
| 594 | case STATE_APP_ATTACHED: |
| 595 | st = "STATE_APP_ATTACHED"; |
| 596 | break; |
| 597 | |
| 598 | default: |
| 599 | st = "UNKNOWN_APP_STATE"; |
| 600 | break; |
| 601 | } |
| 602 | |
| 603 | return st; |
| 604 | } |
| 605 | |
| 606 | static int |
Florin Coras | 3b6c84c | 2021-01-05 20:45:44 -0800 | [diff] [blame] | 607 | vcl_bapi_wait_for_wrk_state_change (vcl_bapi_app_state_t app_state) |
Florin Coras | b88de90 | 2020-09-08 16:47:57 -0700 | [diff] [blame] | 608 | { |
| 609 | vcl_worker_t *wrk = vcl_worker_get_current (); |
| 610 | f64 timeout = clib_time_now (&wrk->clib_time) + vcm->cfg.app_timeout; |
| 611 | |
| 612 | while (clib_time_now (&wrk->clib_time) < timeout) |
| 613 | { |
Florin Coras | 3b6c84c | 2021-01-05 20:45:44 -0800 | [diff] [blame] | 614 | if (wrk->bapi_app_state == app_state) |
Florin Coras | b88de90 | 2020-09-08 16:47:57 -0700 | [diff] [blame] | 615 | return VPPCOM_OK; |
Florin Coras | 3b6c84c | 2021-01-05 20:45:44 -0800 | [diff] [blame] | 616 | if (wrk->bapi_app_state == STATE_APP_FAILED) |
Florin Coras | b88de90 | 2020-09-08 16:47:57 -0700 | [diff] [blame] | 617 | return VPPCOM_ECONNABORTED; |
| 618 | } |
wanghanlin | 85207e6 | 2021-07-06 15:57:55 +0800 | [diff] [blame] | 619 | VDBG (0, "timeout waiting for state %s, current state %d", |
| 620 | vcl_bapi_app_state_str (app_state), wrk->bapi_app_state); |
Florin Coras | b88de90 | 2020-09-08 16:47:57 -0700 | [diff] [blame] | 621 | vcl_evt (VCL_EVT_SESSION_TIMEOUT, vcm, bapi_app_state); |
| 622 | |
| 623 | return VPPCOM_ETIMEDOUT; |
| 624 | } |
| 625 | |
| 626 | static int |
| 627 | vcl_bapi_session_enable (void) |
| 628 | { |
Florin Coras | 3b6c84c | 2021-01-05 20:45:44 -0800 | [diff] [blame] | 629 | vcl_worker_t *wrk = vcl_worker_get_current (); |
Florin Coras | b88de90 | 2020-09-08 16:47:57 -0700 | [diff] [blame] | 630 | int rv; |
| 631 | |
Florin Coras | 3b6c84c | 2021-01-05 20:45:44 -0800 | [diff] [blame] | 632 | if (wrk->bapi_app_state != STATE_APP_ENABLED) |
Florin Coras | b88de90 | 2020-09-08 16:47:57 -0700 | [diff] [blame] | 633 | { |
| 634 | vcl_bapi_send_session_enable_disable (1 /* is_enabled == TRUE */ ); |
Florin Coras | 3b6c84c | 2021-01-05 20:45:44 -0800 | [diff] [blame] | 635 | rv = vcl_bapi_wait_for_wrk_state_change (STATE_APP_ENABLED); |
Florin Coras | b88de90 | 2020-09-08 16:47:57 -0700 | [diff] [blame] | 636 | if (PREDICT_FALSE (rv)) |
| 637 | { |
| 638 | VDBG (0, "application session enable timed out! returning %d (%s)", |
| 639 | rv, vppcom_retval_str (rv)); |
| 640 | return rv; |
| 641 | } |
| 642 | } |
| 643 | return VPPCOM_OK; |
| 644 | } |
| 645 | |
| 646 | static int |
| 647 | vcl_bapi_init (void) |
| 648 | { |
Florin Coras | 3b6c84c | 2021-01-05 20:45:44 -0800 | [diff] [blame] | 649 | vcl_worker_t *wrk = vcl_worker_get_current (); |
Florin Coras | b88de90 | 2020-09-08 16:47:57 -0700 | [diff] [blame] | 650 | int rv; |
| 651 | |
Florin Coras | 3b6c84c | 2021-01-05 20:45:44 -0800 | [diff] [blame] | 652 | wrk->bapi_app_state = STATE_APP_START; |
Florin Coras | b88de90 | 2020-09-08 16:47:57 -0700 | [diff] [blame] | 653 | vcl_bapi_init_error_string_table (); |
| 654 | rv = vcl_bapi_connect_to_vpp (); |
| 655 | if (rv) |
| 656 | { |
| 657 | VERR ("couldn't connect to VPP!"); |
| 658 | return rv; |
| 659 | } |
| 660 | VDBG (0, "sending session enable"); |
| 661 | rv = vcl_bapi_session_enable (); |
| 662 | if (rv) |
| 663 | { |
| 664 | VERR ("vppcom_app_session_enable() failed!"); |
| 665 | return rv; |
| 666 | } |
| 667 | |
| 668 | return 0; |
| 669 | } |
| 670 | |
| 671 | int |
| 672 | vcl_bapi_attach (void) |
| 673 | { |
| 674 | int rv; |
| 675 | |
| 676 | /* API hookup and connect to VPP */ |
| 677 | if ((rv = vcl_bapi_init ())) |
| 678 | return rv; |
| 679 | |
| 680 | vcl_bapi_send_attach (); |
Florin Coras | 3b6c84c | 2021-01-05 20:45:44 -0800 | [diff] [blame] | 681 | rv = vcl_bapi_wait_for_wrk_state_change (STATE_APP_ATTACHED); |
Florin Coras | b88de90 | 2020-09-08 16:47:57 -0700 | [diff] [blame] | 682 | if (PREDICT_FALSE (rv)) |
| 683 | { |
| 684 | VDBG (0, "application attach timed out! returning %d (%s)", rv, |
| 685 | vppcom_retval_str (rv)); |
| 686 | return rv; |
| 687 | } |
| 688 | |
| 689 | return 0; |
| 690 | } |
| 691 | |
| 692 | int |
| 693 | vcl_bapi_app_worker_add (void) |
| 694 | { |
Florin Coras | 3b6c84c | 2021-01-05 20:45:44 -0800 | [diff] [blame] | 695 | vcl_worker_t *wrk = vcl_worker_get_current (); |
| 696 | |
Florin Coras | b88de90 | 2020-09-08 16:47:57 -0700 | [diff] [blame] | 697 | if (vcl_bapi_connect_to_vpp ()) |
| 698 | return -1; |
| 699 | |
Florin Coras | 3b6c84c | 2021-01-05 20:45:44 -0800 | [diff] [blame] | 700 | wrk->bapi_app_state = STATE_APP_ADDING_WORKER; |
Florin Coras | b88de90 | 2020-09-08 16:47:57 -0700 | [diff] [blame] | 701 | vcl_bapi_send_app_worker_add_del (1 /* is_add */ ); |
Florin Coras | 3b6c84c | 2021-01-05 20:45:44 -0800 | [diff] [blame] | 702 | if (vcl_bapi_wait_for_wrk_state_change (STATE_APP_READY)) |
Florin Coras | b88de90 | 2020-09-08 16:47:57 -0700 | [diff] [blame] | 703 | return -1; |
| 704 | return 0; |
| 705 | } |
| 706 | |
| 707 | void |
| 708 | vcl_bapi_app_worker_del (vcl_worker_t * wrk) |
| 709 | { |
| 710 | /* Notify vpp that the worker is going away */ |
| 711 | if (wrk->wrk_index == vcl_get_worker_index ()) |
| 712 | vcl_bapi_send_app_worker_add_del (0 /* is_add */ ); |
| 713 | else |
| 714 | vcl_bapi_send_child_worker_del (wrk); |
| 715 | |
| 716 | /* Disconnect the binary api */ |
| 717 | if (vec_len (vcm->workers) == 1) |
| 718 | vcl_bapi_disconnect_from_vpp (); |
| 719 | else |
| 720 | vl_client_send_disconnect (1 /* vpp should cleanup */ ); |
| 721 | } |
| 722 | |
| 723 | int |
Florin Coras | 935ce75 | 2020-09-08 22:43:47 -0700 | [diff] [blame] | 724 | vcl_bapi_recv_fds (vcl_worker_t * wrk, int *fds, int n_fds) |
| 725 | { |
| 726 | clib_error_t *err; |
| 727 | |
| 728 | if ((err = vl_socket_client_recv_fd_msg2 (&wrk->bapi_sock_ctx, fds, n_fds, |
| 729 | 5))) |
| 730 | { |
| 731 | clib_error_report (err); |
| 732 | return -1; |
| 733 | } |
| 734 | |
| 735 | return 0; |
| 736 | } |
| 737 | |
| 738 | int |
Florin Coras | a5a9efd | 2021-01-05 17:03:29 -0800 | [diff] [blame] | 739 | vcl_bapi_add_cert_key_pair (vppcom_cert_key_pair_t *ckpair) |
Florin Coras | b88de90 | 2020-09-08 16:47:57 -0700 | [diff] [blame] | 740 | { |
Florin Coras | b88de90 | 2020-09-08 16:47:57 -0700 | [diff] [blame] | 741 | vcl_worker_t *wrk = vcl_worker_get_current (); |
Florin Coras | b88de90 | 2020-09-08 16:47:57 -0700 | [diff] [blame] | 742 | |
Florin Coras | a5a9efd | 2021-01-05 17:03:29 -0800 | [diff] [blame] | 743 | if (ckpair->key_len == 0 || ckpair->key_len == ~0) |
| 744 | return VPPCOM_EINVAL; |
Florin Coras | b88de90 | 2020-09-08 16:47:57 -0700 | [diff] [blame] | 745 | |
Florin Coras | a5a9efd | 2021-01-05 17:03:29 -0800 | [diff] [blame] | 746 | vcl_bapi_send_app_add_cert_key_pair (ckpair); |
Florin Coras | 3b6c84c | 2021-01-05 20:45:44 -0800 | [diff] [blame] | 747 | wrk->bapi_app_state = STATE_APP_ADDING_TLS_DATA; |
| 748 | vcl_bapi_wait_for_wrk_state_change (STATE_APP_READY); |
Florin Coras | a5a9efd | 2021-01-05 17:03:29 -0800 | [diff] [blame] | 749 | if (wrk->bapi_app_state == STATE_APP_READY) |
| 750 | return wrk->bapi_return; |
| 751 | return VPPCOM_EFAULT; |
Florin Coras | b88de90 | 2020-09-08 16:47:57 -0700 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | int |
Florin Coras | a5a9efd | 2021-01-05 17:03:29 -0800 | [diff] [blame] | 755 | vcl_bapi_del_cert_key_pair (u32 ckpair_index) |
Florin Coras | b88de90 | 2020-09-08 16:47:57 -0700 | [diff] [blame] | 756 | { |
Florin Coras | a5a9efd | 2021-01-05 17:03:29 -0800 | [diff] [blame] | 757 | /* Don't wait for reply */ |
| 758 | vcl_bapi_send_app_del_cert_key_pair (ckpair_index); |
| 759 | return 0; |
Florin Coras | b88de90 | 2020-09-08 16:47:57 -0700 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | int |
| 763 | vcl_bapi_worker_set (void) |
| 764 | { |
| 765 | vcl_worker_t *wrk = vcl_worker_get_current (); |
| 766 | int i; |
| 767 | |
| 768 | /* Find the first worker with the same pid */ |
| 769 | for (i = 0; i < vec_len (vcm->workers); i++) |
| 770 | { |
| 771 | if (i == wrk->wrk_index) |
| 772 | continue; |
| 773 | if (vcm->workers[i].current_pid == wrk->current_pid) |
| 774 | { |
| 775 | wrk->vl_input_queue = vcm->workers[i].vl_input_queue; |
Florin Coras | cc7c88e | 2020-09-15 15:56:51 -0700 | [diff] [blame] | 776 | wrk->api_client_handle = vcm->workers[i].api_client_handle; |
Florin Coras | b88de90 | 2020-09-08 16:47:57 -0700 | [diff] [blame] | 777 | return 0; |
| 778 | } |
| 779 | } |
| 780 | return -1; |
| 781 | } |
| 782 | |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 783 | /* |
| 784 | * fd.io coding-style-patch-verification: ON |
| 785 | * |
| 786 | * Local Variables: |
| 787 | * eval: (c-set-style "gnu") |
| 788 | * End: |
| 789 | */ |