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