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