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 | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 36 | 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 | { |
| 55 | if (mp->retval) |
| 56 | { |
| 57 | clib_warning ("VCL<%d>: session_enable_disable failed: %U", getpid (), |
| 58 | format_api_error, ntohl (mp->retval)); |
| 59 | } |
| 60 | else |
| 61 | vcm->app_state = STATE_APP_ENABLED; |
| 62 | } |
| 63 | |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 64 | static int |
Florin Coras | d85de68 | 2018-11-29 17:02:29 -0800 | [diff] [blame] | 65 | vcl_segment_attach (u64 segment_handle, char *name, ssvm_segment_type_t type, |
| 66 | int fd) |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 67 | { |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 68 | fifo_segment_create_args_t _a, *a = &_a; |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 69 | int rv; |
| 70 | |
| 71 | memset (a, 0, sizeof (*a)); |
| 72 | a->segment_name = (char *) name; |
| 73 | a->segment_type = type; |
| 74 | |
| 75 | if (type == SSVM_SEGMENT_MEMFD) |
| 76 | a->memfd_fd = fd; |
| 77 | |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 78 | if ((rv = fifo_segment_attach (&vcm->segment_main, a))) |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 79 | { |
| 80 | clib_warning ("svm_fifo_segment_attach ('%s') failed", name); |
| 81 | return rv; |
| 82 | } |
Florin Coras | d85de68 | 2018-11-29 17:02:29 -0800 | [diff] [blame] | 83 | vcl_segment_table_add (segment_handle, a->new_segment_indices[0]); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 84 | vec_reset_length (a->new_segment_indices); |
| 85 | return 0; |
| 86 | } |
| 87 | |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 88 | static void |
Florin Coras | d85de68 | 2018-11-29 17:02:29 -0800 | [diff] [blame] | 89 | vcl_segment_detach (u64 segment_handle) |
| 90 | { |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 91 | fifo_segment_main_t *sm = &vcm->segment_main; |
| 92 | fifo_segment_t *segment; |
Florin Coras | d85de68 | 2018-11-29 17:02:29 -0800 | [diff] [blame] | 93 | u32 segment_index; |
| 94 | |
| 95 | segment_index = vcl_segment_table_lookup (segment_handle); |
| 96 | if (segment_index == (u32) ~ 0) |
| 97 | return; |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 98 | segment = fifo_segment_get_segment (sm, segment_index); |
| 99 | fifo_segment_delete (sm, segment); |
Florin Coras | d85de68 | 2018-11-29 17:02:29 -0800 | [diff] [blame] | 100 | vcl_segment_table_del (segment_handle); |
Florin Coras | 4850e3e | 2018-12-12 14:34:38 -0800 | [diff] [blame] | 101 | VDBG (0, "detached segment %u handle %u", segment_index, segment_handle); |
Florin Coras | d85de68 | 2018-11-29 17:02:29 -0800 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | static u64 |
| 105 | vcl_vpp_worker_segment_handle (u32 wrk_index) |
| 106 | { |
| 107 | return (VCL_INVALID_SEGMENT_HANDLE - wrk_index - 1); |
| 108 | } |
| 109 | |
| 110 | static void |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 111 | 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] | 112 | { |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 113 | vcl_worker_t *wrk = vcl_worker_get (0); |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 114 | svm_msg_q_t *ctrl_mq; |
Florin Coras | d85de68 | 2018-11-29 17:02:29 -0800 | [diff] [blame] | 115 | u64 segment_handle; |
Florin Coras | 617574d | 2019-07-01 07:32:31 -0700 | [diff] [blame] | 116 | int *fds = 0, i; |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 117 | u32 n_fds = 0; |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 118 | |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 119 | if (mp->retval) |
| 120 | { |
Florin Coras | a5efca3 | 2019-03-20 08:33:10 -0700 | [diff] [blame] | 121 | VERR ("attach failed: %U", format_api_error, ntohl (mp->retval)); |
| 122 | goto failed; |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 125 | wrk->app_event_queue = uword_to_pointer (mp->app_mq, svm_msg_q_t *); |
| 126 | ctrl_mq = uword_to_pointer (mp->vpp_ctrl_mq, svm_msg_q_t *); |
| 127 | vec_validate (wrk->vpp_event_queues, mp->vpp_ctrl_mq_thread); |
| 128 | wrk->vpp_event_queues[mp->vpp_ctrl_mq_thread] = ctrl_mq; |
Florin Coras | 22ba330 | 2019-08-29 22:45:04 -0700 | [diff] [blame] | 129 | vcm->ctrl_mq = wrk->ctrl_mq = ctrl_mq; |
Florin Coras | d85de68 | 2018-11-29 17:02:29 -0800 | [diff] [blame] | 130 | segment_handle = clib_net_to_host_u64 (mp->segment_handle); |
| 131 | if (segment_handle == VCL_INVALID_SEGMENT_HANDLE) |
| 132 | { |
Florin Coras | a5efca3 | 2019-03-20 08:33:10 -0700 | [diff] [blame] | 133 | VERR ("invalid segment handle"); |
| 134 | goto failed; |
Florin Coras | d85de68 | 2018-11-29 17:02:29 -0800 | [diff] [blame] | 135 | } |
| 136 | |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 137 | if (mp->n_fds) |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 138 | { |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 139 | vec_validate (fds, mp->n_fds); |
Florin Coras | d4c7092 | 2019-11-28 14:21:21 -0800 | [diff] [blame^] | 140 | if (vl_socket_client_recv_fd_msg2 (&wrk->bapi_sock_ctx, fds, mp->n_fds, |
| 141 | 5)) |
Florin Coras | 617574d | 2019-07-01 07:32:31 -0700 | [diff] [blame] | 142 | goto failed; |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 143 | |
| 144 | if (mp->fd_flags & SESSION_FD_F_VPP_MQ_SEGMENT) |
Florin Coras | d85de68 | 2018-11-29 17:02:29 -0800 | [diff] [blame] | 145 | if (vcl_segment_attach (vcl_vpp_worker_segment_handle (0), |
| 146 | "vpp-mq-seg", SSVM_SEGMENT_MEMFD, |
| 147 | fds[n_fds++])) |
Florin Coras | a5efca3 | 2019-03-20 08:33:10 -0700 | [diff] [blame] | 148 | goto failed; |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 149 | |
| 150 | if (mp->fd_flags & SESSION_FD_F_MEMFD_SEGMENT) |
Florin Coras | d85de68 | 2018-11-29 17:02:29 -0800 | [diff] [blame] | 151 | if (vcl_segment_attach (segment_handle, (char *) mp->segment_name, |
| 152 | SSVM_SEGMENT_MEMFD, fds[n_fds++])) |
Florin Coras | a5efca3 | 2019-03-20 08:33:10 -0700 | [diff] [blame] | 153 | goto failed; |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 154 | |
| 155 | if (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD) |
| 156 | { |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 157 | svm_msg_q_set_consumer_eventfd (wrk->app_event_queue, fds[n_fds]); |
| 158 | vcl_mq_epoll_add_evfd (wrk, wrk->app_event_queue); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 159 | n_fds++; |
| 160 | } |
| 161 | |
| 162 | vec_free (fds); |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 163 | } |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 164 | else |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 165 | { |
Florin Coras | d85de68 | 2018-11-29 17:02:29 -0800 | [diff] [blame] | 166 | if (vcl_segment_attach (segment_handle, (char *) mp->segment_name, |
| 167 | SSVM_SEGMENT_SHM, -1)) |
Florin Coras | a5efca3 | 2019-03-20 08:33:10 -0700 | [diff] [blame] | 168 | goto failed; |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Florin Coras | c1f5a43 | 2018-11-20 11:31:26 -0800 | [diff] [blame] | 171 | vcm->app_index = clib_net_to_host_u32 (mp->app_index); |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 172 | vcm->app_state = STATE_APP_ATTACHED; |
Florin Coras | a5efca3 | 2019-03-20 08:33:10 -0700 | [diff] [blame] | 173 | return; |
| 174 | |
| 175 | failed: |
| 176 | vcm->app_state = STATE_APP_FAILED; |
Florin Coras | 617574d | 2019-07-01 07:32:31 -0700 | [diff] [blame] | 177 | for (i = clib_max (n_fds - 1, 0); i < vec_len (fds); i++) |
| 178 | close (fds[i]); |
| 179 | vec_free (fds); |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | static void |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 183 | vl_api_app_worker_add_del_reply_t_handler (vl_api_app_worker_add_del_reply_t * |
| 184 | mp) |
| 185 | { |
Florin Coras | 617574d | 2019-07-01 07:32:31 -0700 | [diff] [blame] | 186 | int n_fds = 0, *fds = 0, i; |
Florin Coras | d85de68 | 2018-11-29 17:02:29 -0800 | [diff] [blame] | 187 | u64 segment_handle; |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 188 | vcl_worker_t *wrk; |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 189 | u32 wrk_index; |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 190 | |
| 191 | if (mp->retval) |
| 192 | { |
| 193 | clib_warning ("VCL<%d>: add/del worker failed: %U", getpid (), |
| 194 | format_api_error, ntohl (mp->retval)); |
| 195 | goto failed; |
| 196 | } |
Florin Coras | 053a0e4 | 2018-11-13 15:52:38 -0800 | [diff] [blame] | 197 | |
Florin Coras | 3348a4c | 2018-09-07 17:09:35 -0700 | [diff] [blame] | 198 | if (!mp->is_add) |
| 199 | return; |
| 200 | |
Florin Coras | dc2e251 | 2018-12-03 17:47:26 -0800 | [diff] [blame] | 201 | wrk_index = mp->context; |
Florin Coras | 01f3f89 | 2018-12-02 12:45:53 -0800 | [diff] [blame] | 202 | wrk = vcl_worker_get_if_valid (wrk_index); |
| 203 | if (!wrk) |
| 204 | return; |
| 205 | |
Florin Coras | dc2e251 | 2018-12-03 17:47:26 -0800 | [diff] [blame] | 206 | wrk->vpp_wrk_index = clib_net_to_host_u32 (mp->wrk_index); |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 207 | wrk->app_event_queue = uword_to_pointer (mp->app_event_queue_address, |
| 208 | svm_msg_q_t *); |
Florin Coras | 22ba330 | 2019-08-29 22:45:04 -0700 | [diff] [blame] | 209 | wrk->ctrl_mq = vcm->ctrl_mq; |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 210 | |
Florin Coras | d85de68 | 2018-11-29 17:02:29 -0800 | [diff] [blame] | 211 | segment_handle = clib_net_to_host_u64 (mp->segment_handle); |
| 212 | if (segment_handle == VCL_INVALID_SEGMENT_HANDLE) |
| 213 | { |
| 214 | clib_warning ("invalid segment handle"); |
Florin Coras | 01f3f89 | 2018-12-02 12:45:53 -0800 | [diff] [blame] | 215 | goto failed; |
Florin Coras | d85de68 | 2018-11-29 17:02:29 -0800 | [diff] [blame] | 216 | } |
| 217 | |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 218 | if (mp->n_fds) |
| 219 | { |
| 220 | vec_validate (fds, mp->n_fds); |
Florin Coras | d4c7092 | 2019-11-28 14:21:21 -0800 | [diff] [blame^] | 221 | if (vl_socket_client_recv_fd_msg2 (&wrk->bapi_sock_ctx, fds, mp->n_fds, |
| 222 | 5)) |
Florin Coras | 617574d | 2019-07-01 07:32:31 -0700 | [diff] [blame] | 223 | goto failed; |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 224 | |
| 225 | if (mp->fd_flags & SESSION_FD_F_VPP_MQ_SEGMENT) |
Florin Coras | 01f3f89 | 2018-12-02 12:45:53 -0800 | [diff] [blame] | 226 | if (vcl_segment_attach (vcl_vpp_worker_segment_handle (wrk_index), |
| 227 | "vpp-worker-seg", SSVM_SEGMENT_MEMFD, |
| 228 | fds[n_fds++])) |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 229 | goto failed; |
| 230 | |
| 231 | if (mp->fd_flags & SESSION_FD_F_MEMFD_SEGMENT) |
Florin Coras | d85de68 | 2018-11-29 17:02:29 -0800 | [diff] [blame] | 232 | if (vcl_segment_attach (segment_handle, (char *) mp->segment_name, |
| 233 | SSVM_SEGMENT_MEMFD, fds[n_fds++])) |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 234 | goto failed; |
| 235 | |
| 236 | if (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD) |
| 237 | { |
| 238 | svm_msg_q_set_consumer_eventfd (wrk->app_event_queue, fds[n_fds]); |
| 239 | vcl_mq_epoll_add_evfd (wrk, wrk->app_event_queue); |
| 240 | n_fds++; |
| 241 | } |
| 242 | |
| 243 | vec_free (fds); |
| 244 | } |
| 245 | else |
| 246 | { |
Florin Coras | d85de68 | 2018-11-29 17:02:29 -0800 | [diff] [blame] | 247 | if (vcl_segment_attach (segment_handle, (char *) mp->segment_name, |
| 248 | SSVM_SEGMENT_SHM, -1)) |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 249 | goto failed; |
| 250 | } |
| 251 | vcm->app_state = STATE_APP_READY; |
Florin Coras | 053a0e4 | 2018-11-13 15:52:38 -0800 | [diff] [blame] | 252 | 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] | 253 | return; |
| 254 | |
| 255 | failed: |
| 256 | vcm->app_state = STATE_APP_FAILED; |
Florin Coras | 617574d | 2019-07-01 07:32:31 -0700 | [diff] [blame] | 257 | for (i = clib_max (n_fds - 1, 0); i < vec_len (fds); i++) |
| 258 | close (fds[i]); |
| 259 | vec_free (fds); |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | static void |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 263 | vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp) |
| 264 | { |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 265 | ssvm_segment_type_t seg_type = SSVM_SEGMENT_SHM; |
Florin Coras | d85de68 | 2018-11-29 17:02:29 -0800 | [diff] [blame] | 266 | u64 segment_handle; |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 267 | int fd = -1; |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 268 | |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 269 | if (mp->fd_flags) |
| 270 | { |
| 271 | vl_socket_client_recv_fd_msg (&fd, 1, 5); |
| 272 | seg_type = SSVM_SEGMENT_MEMFD; |
| 273 | } |
| 274 | |
Florin Coras | d85de68 | 2018-11-29 17:02:29 -0800 | [diff] [blame] | 275 | segment_handle = clib_net_to_host_u64 (mp->segment_handle); |
| 276 | if (segment_handle == VCL_INVALID_SEGMENT_HANDLE) |
| 277 | { |
| 278 | clib_warning ("invalid segment handle"); |
| 279 | return; |
| 280 | } |
| 281 | |
| 282 | if (vcl_segment_attach (segment_handle, (char *) mp->segment_name, |
| 283 | seg_type, fd)) |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 284 | { |
| 285 | clib_warning ("VCL<%d>: svm_fifo_segment_attach ('%s') failed", |
| 286 | getpid (), mp->segment_name); |
| 287 | return; |
| 288 | } |
| 289 | |
| 290 | VDBG (1, "VCL<%d>: mapped new segment '%s' size %d", getpid (), |
| 291 | mp->segment_name, mp->segment_size); |
| 292 | } |
| 293 | |
| 294 | static void |
| 295 | vl_api_unmap_segment_t_handler (vl_api_unmap_segment_t * mp) |
| 296 | { |
Florin Coras | d85de68 | 2018-11-29 17:02:29 -0800 | [diff] [blame] | 297 | u64 segment_handle = clib_net_to_host_u64 (mp->segment_handle); |
| 298 | vcl_segment_detach (segment_handle); |
| 299 | VDBG (1, "Unmapped segment: %d", segment_handle); |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | static void |
Ping Yu | 34a3a08 | 2018-11-30 19:16:17 -0500 | [diff] [blame] | 303 | vl_api_application_tls_cert_add_reply_t_handler |
| 304 | (vl_api_application_tls_cert_add_reply_t * mp) |
| 305 | { |
| 306 | if (mp->retval) |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 307 | VDBG (0, "add cert failed: %U", format_api_error, ntohl (mp->retval)); |
| 308 | vcm->app_state = STATE_APP_READY; |
Ping Yu | 34a3a08 | 2018-11-30 19:16:17 -0500 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | static void |
| 312 | vl_api_application_tls_key_add_reply_t_handler |
| 313 | (vl_api_application_tls_key_add_reply_t * mp) |
| 314 | { |
| 315 | if (mp->retval) |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 316 | VDBG (0, "add key failed: %U", format_api_error, ntohl (mp->retval)); |
| 317 | vcm->app_state = STATE_APP_READY; |
Ping Yu | 34a3a08 | 2018-11-30 19:16:17 -0500 | [diff] [blame] | 318 | } |
| 319 | |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 320 | #define foreach_sock_msg \ |
| 321 | _(SESSION_ENABLE_DISABLE_REPLY, session_enable_disable_reply) \ |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 322 | _(APP_ATTACH_REPLY, app_attach_reply) \ |
Ping Yu | 34a3a08 | 2018-11-30 19:16:17 -0500 | [diff] [blame] | 323 | _(APPLICATION_TLS_CERT_ADD_REPLY, application_tls_cert_add_reply) \ |
| 324 | _(APPLICATION_TLS_KEY_ADD_REPLY, application_tls_key_add_reply) \ |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 325 | _(MAP_ANOTHER_SEGMENT, map_another_segment) \ |
| 326 | _(UNMAP_SEGMENT, unmap_segment) \ |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 327 | _(APP_WORKER_ADD_DEL_REPLY, app_worker_add_del_reply) \ |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 328 | |
| 329 | void |
| 330 | vppcom_api_hookup (void) |
| 331 | { |
Florin Coras | 6011699 | 2018-08-27 09:52:18 -0700 | [diff] [blame] | 332 | #define _(N, n) \ |
| 333 | vl_msg_api_set_handlers(VL_API_##N, #n, \ |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 334 | vl_api_##n##_t_handler, \ |
| 335 | vl_noop_handler, \ |
| 336 | vl_api_##n##_t_endian, \ |
| 337 | vl_api_##n##_t_print, \ |
| 338 | sizeof(vl_api_##n##_t), 1); |
| 339 | foreach_sock_msg; |
| 340 | #undef _ |
| 341 | } |
| 342 | |
| 343 | /* |
| 344 | * VPP-API message functions |
| 345 | */ |
| 346 | void |
| 347 | vppcom_send_session_enable_disable (u8 is_enable) |
| 348 | { |
Florin Coras | 47c40e2 | 2018-11-26 17:01:36 -0800 | [diff] [blame] | 349 | vcl_worker_t *wrk = vcl_worker_get_current (); |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 350 | vl_api_session_enable_disable_t *bmp; |
| 351 | bmp = vl_msg_api_alloc (sizeof (*bmp)); |
| 352 | memset (bmp, 0, sizeof (*bmp)); |
| 353 | |
| 354 | bmp->_vl_msg_id = ntohs (VL_API_SESSION_ENABLE_DISABLE); |
Florin Coras | 47c40e2 | 2018-11-26 17:01:36 -0800 | [diff] [blame] | 355 | bmp->client_index = wrk->my_client_index; |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 356 | bmp->context = htonl (0xfeedface); |
| 357 | bmp->is_enable = is_enable; |
Florin Coras | 47c40e2 | 2018-11-26 17:01:36 -0800 | [diff] [blame] | 358 | vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & bmp); |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | void |
| 362 | vppcom_app_send_attach (void) |
| 363 | { |
Florin Coras | 47c40e2 | 2018-11-26 17:01:36 -0800 | [diff] [blame] | 364 | vcl_worker_t *wrk = vcl_worker_get_current (); |
Florin Coras | d747c3c | 2019-10-20 19:55:56 -0700 | [diff] [blame] | 365 | u8 tls_engine = CRYPTO_ENGINE_OPENSSL; |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 366 | vl_api_app_attach_t *bmp; |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 367 | u8 nsid_len = vec_len (vcm->cfg.namespace_id); |
| 368 | u8 app_is_proxy = (vcm->cfg.app_proxy_transport_tcp || |
| 369 | vcm->cfg.app_proxy_transport_udp); |
| 370 | |
Florin Coras | d747c3c | 2019-10-20 19:55:56 -0700 | [diff] [blame] | 371 | tls_engine = vcm->cfg.tls_engine ? vcm->cfg.tls_engine : tls_engine; |
| 372 | |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 373 | bmp = vl_msg_api_alloc (sizeof (*bmp)); |
| 374 | memset (bmp, 0, sizeof (*bmp)); |
| 375 | |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 376 | bmp->_vl_msg_id = ntohs (VL_API_APP_ATTACH); |
Florin Coras | 47c40e2 | 2018-11-26 17:01:36 -0800 | [diff] [blame] | 377 | bmp->client_index = wrk->my_client_index; |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 378 | bmp->context = htonl (0xfeedface); |
| 379 | bmp->options[APP_OPTIONS_FLAGS] = |
| 380 | APP_OPTIONS_FLAGS_ACCEPT_REDIRECT | APP_OPTIONS_FLAGS_ADD_SEGMENT | |
| 381 | (vcm->cfg.app_scope_local ? APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE : 0) | |
| 382 | (vcm->cfg.app_scope_global ? APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE : 0) | |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 383 | (app_is_proxy ? APP_OPTIONS_FLAGS_IS_PROXY : 0) | |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 384 | (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] | 385 | bmp->options[APP_OPTIONS_PROXY_TRANSPORT] = |
| 386 | (u64) ((vcm->cfg.app_proxy_transport_tcp ? 1 << TRANSPORT_PROTO_TCP : 0) | |
| 387 | (vcm->cfg.app_proxy_transport_udp ? 1 << TRANSPORT_PROTO_UDP : 0)); |
| 388 | bmp->options[APP_OPTIONS_SEGMENT_SIZE] = vcm->cfg.segment_size; |
| 389 | bmp->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = vcm->cfg.add_segment_size; |
| 390 | bmp->options[APP_OPTIONS_RX_FIFO_SIZE] = vcm->cfg.rx_fifo_size; |
| 391 | bmp->options[APP_OPTIONS_TX_FIFO_SIZE] = vcm->cfg.tx_fifo_size; |
| 392 | bmp->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = |
| 393 | vcm->cfg.preallocated_fifo_pairs; |
| 394 | bmp->options[APP_OPTIONS_EVT_QUEUE_SIZE] = vcm->cfg.event_queue_size; |
Florin Coras | d747c3c | 2019-10-20 19:55:56 -0700 | [diff] [blame] | 395 | bmp->options[APP_OPTIONS_TLS_ENGINE] = tls_engine; |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 396 | if (nsid_len) |
| 397 | { |
| 398 | bmp->namespace_id_len = nsid_len; |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 399 | clib_memcpy_fast (bmp->namespace_id, vcm->cfg.namespace_id, nsid_len); |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 400 | bmp->options[APP_OPTIONS_NAMESPACE_SECRET] = vcm->cfg.namespace_secret; |
| 401 | } |
Florin Coras | 47c40e2 | 2018-11-26 17:01:36 -0800 | [diff] [blame] | 402 | vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & bmp); |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | void |
| 406 | vppcom_app_send_detach (void) |
| 407 | { |
Florin Coras | 47c40e2 | 2018-11-26 17:01:36 -0800 | [diff] [blame] | 408 | vcl_worker_t *wrk = vcl_worker_get_current (); |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 409 | vl_api_application_detach_t *bmp; |
| 410 | bmp = vl_msg_api_alloc (sizeof (*bmp)); |
| 411 | memset (bmp, 0, sizeof (*bmp)); |
| 412 | |
| 413 | bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH); |
Florin Coras | 47c40e2 | 2018-11-26 17:01:36 -0800 | [diff] [blame] | 414 | bmp->client_index = wrk->my_client_index; |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 415 | bmp->context = htonl (0xfeedface); |
Florin Coras | 47c40e2 | 2018-11-26 17:01:36 -0800 | [diff] [blame] | 416 | vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & bmp); |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | void |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 420 | vcl_send_app_worker_add_del (u8 is_add) |
| 421 | { |
| 422 | vcl_worker_t *wrk = vcl_worker_get_current (); |
| 423 | vl_api_app_worker_add_del_t *mp; |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 424 | |
| 425 | mp = vl_msg_api_alloc (sizeof (*mp)); |
| 426 | memset (mp, 0, sizeof (*mp)); |
| 427 | |
| 428 | mp->_vl_msg_id = ntohs (VL_API_APP_WORKER_ADD_DEL); |
Florin Coras | 47c40e2 | 2018-11-26 17:01:36 -0800 | [diff] [blame] | 429 | mp->client_index = wrk->my_client_index; |
Florin Coras | c1f5a43 | 2018-11-20 11:31:26 -0800 | [diff] [blame] | 430 | mp->app_index = clib_host_to_net_u32 (vcm->app_index); |
Florin Coras | 01f3f89 | 2018-12-02 12:45:53 -0800 | [diff] [blame] | 431 | mp->context = wrk->wrk_index; |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 432 | mp->is_add = is_add; |
| 433 | if (!is_add) |
Florin Coras | 01f3f89 | 2018-12-02 12:45:53 -0800 | [diff] [blame] | 434 | mp->wrk_index = clib_host_to_net_u32 (wrk->vpp_wrk_index); |
| 435 | |
| 436 | vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & mp); |
| 437 | } |
| 438 | |
| 439 | void |
| 440 | vcl_send_child_worker_del (vcl_worker_t * child_wrk) |
| 441 | { |
| 442 | vcl_worker_t *wrk = vcl_worker_get_current (); |
| 443 | vl_api_app_worker_add_del_t *mp; |
| 444 | |
| 445 | mp = vl_msg_api_alloc (sizeof (*mp)); |
| 446 | memset (mp, 0, sizeof (*mp)); |
| 447 | |
| 448 | mp->_vl_msg_id = ntohs (VL_API_APP_WORKER_ADD_DEL); |
| 449 | mp->client_index = wrk->my_client_index; |
| 450 | mp->app_index = clib_host_to_net_u32 (vcm->app_index); |
| 451 | mp->context = wrk->wrk_index; |
| 452 | mp->is_add = 0; |
| 453 | 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] | 454 | |
Florin Coras | 47c40e2 | 2018-11-26 17:01:36 -0800 | [diff] [blame] | 455 | vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & mp); |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | void |
Ping Yu | 34a3a08 | 2018-11-30 19:16:17 -0500 | [diff] [blame] | 459 | vppcom_send_application_tls_cert_add (vcl_session_t * session, char *cert, |
| 460 | u32 cert_len) |
| 461 | { |
| 462 | vcl_worker_t *wrk = vcl_worker_get_current (); |
| 463 | vl_api_application_tls_cert_add_t *cert_mp; |
| 464 | |
| 465 | cert_mp = vl_msg_api_alloc (sizeof (*cert_mp) + cert_len); |
| 466 | clib_memset (cert_mp, 0, sizeof (*cert_mp)); |
| 467 | cert_mp->_vl_msg_id = ntohs (VL_API_APPLICATION_TLS_CERT_ADD); |
| 468 | cert_mp->client_index = wrk->my_client_index; |
| 469 | cert_mp->context = session->session_index; |
| 470 | cert_mp->cert_len = clib_host_to_net_u16 (cert_len); |
| 471 | clib_memcpy_fast (cert_mp->cert, cert, cert_len); |
| 472 | vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & cert_mp); |
Ping Yu | 34a3a08 | 2018-11-30 19:16:17 -0500 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | void |
| 476 | vppcom_send_application_tls_key_add (vcl_session_t * session, char *key, |
| 477 | u32 key_len) |
| 478 | { |
| 479 | vcl_worker_t *wrk = vcl_worker_get_current (); |
| 480 | vl_api_application_tls_key_add_t *key_mp; |
| 481 | |
| 482 | key_mp = vl_msg_api_alloc (sizeof (*key_mp) + key_len); |
| 483 | clib_memset (key_mp, 0, sizeof (*key_mp)); |
| 484 | key_mp->_vl_msg_id = ntohs (VL_API_APPLICATION_TLS_KEY_ADD); |
| 485 | key_mp->client_index = wrk->my_client_index; |
| 486 | key_mp->context = session->session_index; |
| 487 | key_mp->key_len = clib_host_to_net_u16 (key_len); |
| 488 | clib_memcpy_fast (key_mp->key, key, key_len); |
| 489 | vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & key_mp); |
Ping Yu | 34a3a08 | 2018-11-30 19:16:17 -0500 | [diff] [blame] | 490 | } |
| 491 | |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 492 | u32 |
| 493 | vcl_max_nsid_len (void) |
| 494 | { |
| 495 | vl_api_application_attach_t *mp; |
| 496 | return (sizeof (mp->namespace_id) - 1); |
| 497 | } |
| 498 | |
| 499 | void |
| 500 | vppcom_init_error_string_table (void) |
| 501 | { |
| 502 | vcm->error_string_by_error_number = hash_create (0, sizeof (uword)); |
| 503 | |
| 504 | #define _(n, v, s) hash_set (vcm->error_string_by_error_number, -v, s); |
| 505 | foreach_vnet_api_error; |
| 506 | #undef _ |
| 507 | |
| 508 | hash_set (vcm->error_string_by_error_number, 99, "Misc"); |
| 509 | } |
| 510 | |
| 511 | int |
| 512 | vppcom_connect_to_vpp (char *app_name) |
| 513 | { |
Florin Coras | 47c40e2 | 2018-11-26 17:01:36 -0800 | [diff] [blame] | 514 | vcl_worker_t *wrk = vcl_worker_get_current (); |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 515 | vppcom_cfg_t *vcl_cfg = &vcm->cfg; |
Florin Coras | d4c7092 | 2019-11-28 14:21:21 -0800 | [diff] [blame^] | 516 | api_main_t *am; |
| 517 | |
| 518 | vlibapi_set_main (&wrk->bapi_api_ctx); |
| 519 | vlibapi_set_memory_client_main (&wrk->bapi_shm_ctx); |
| 520 | vppcom_api_hookup (); |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 521 | |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 522 | if (vcl_cfg->vpp_api_socket_name) |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 523 | { |
Florin Coras | d4c7092 | 2019-11-28 14:21:21 -0800 | [diff] [blame^] | 524 | if (vl_socket_client_connect2 (&wrk->bapi_sock_ctx, |
| 525 | (char *) vcl_cfg->vpp_api_socket_name, |
| 526 | app_name, 0 /* default rx/tx buffer */ )) |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 527 | { |
Florin Coras | 053a0e4 | 2018-11-13 15:52:38 -0800 | [diff] [blame] | 528 | VERR ("app (%s) socket connect failed!", app_name); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 529 | return VPPCOM_ECONNREFUSED; |
| 530 | } |
| 531 | |
Florin Coras | d4c7092 | 2019-11-28 14:21:21 -0800 | [diff] [blame^] | 532 | if (vl_socket_client_init_shm2 (&wrk->bapi_sock_ctx, 0, |
| 533 | 1 /* want_pthread */ )) |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 534 | { |
Florin Coras | 053a0e4 | 2018-11-13 15:52:38 -0800 | [diff] [blame] | 535 | VERR ("app (%s) init shm failed!", app_name); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 536 | return VPPCOM_ECONNREFUSED; |
| 537 | } |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 538 | } |
| 539 | else |
| 540 | { |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 541 | if (!vcl_cfg->vpp_api_filename) |
| 542 | vcl_cfg->vpp_api_filename = format (0, "/vpe-api%c", 0); |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 543 | |
Florin Coras | d4c7092 | 2019-11-28 14:21:21 -0800 | [diff] [blame^] | 544 | vl_set_memory_root_path ((char *) vcl_cfg->vpp_api_chroot); |
| 545 | |
Florin Coras | 053a0e4 | 2018-11-13 15:52:38 -0800 | [diff] [blame] | 546 | VDBG (0, "app (%s) connecting to VPP api (%s)...", |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 547 | app_name, vcl_cfg->vpp_api_filename); |
| 548 | |
| 549 | if (vl_client_connect_to_vlib ((char *) vcl_cfg->vpp_api_filename, |
| 550 | app_name, vcm->cfg.vpp_api_q_length) < 0) |
| 551 | { |
Florin Coras | 053a0e4 | 2018-11-13 15:52:38 -0800 | [diff] [blame] | 552 | VERR ("app (%s) connect failed!", app_name); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 553 | return VPPCOM_ECONNREFUSED; |
| 554 | } |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 555 | } |
| 556 | |
Florin Coras | d4c7092 | 2019-11-28 14:21:21 -0800 | [diff] [blame^] | 557 | am = vlibapi_get_main (); |
Florin Coras | 47c40e2 | 2018-11-26 17:01:36 -0800 | [diff] [blame] | 558 | wrk->vl_input_queue = am->shmem_hdr->vl_input_queue; |
| 559 | wrk->my_client_index = (u32) am->my_client_index; |
| 560 | wrk->wrk_state = STATE_APP_CONN_VPP; |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 561 | |
Florin Coras | 053a0e4 | 2018-11-13 15:52:38 -0800 | [diff] [blame] | 562 | VDBG (0, "app (%s) is connected to VPP!", app_name); |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 563 | vcl_evt (VCL_EVT_INIT, vcm); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 564 | return VPPCOM_OK; |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 565 | } |
| 566 | |
Florin Coras | 470b2a6 | 2019-08-03 18:53:48 -0700 | [diff] [blame] | 567 | void |
| 568 | vppcom_disconnect_from_vpp (void) |
| 569 | { |
| 570 | vppcom_cfg_t *vcl_cfg = &vcm->cfg; |
| 571 | |
| 572 | if (vcl_cfg->vpp_api_socket_name) |
| 573 | vl_socket_client_disconnect (); |
| 574 | else |
| 575 | vl_client_disconnect_from_vlib (); |
| 576 | } |
| 577 | |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 578 | /* |
| 579 | * fd.io coding-style-patch-verification: ON |
| 580 | * |
| 581 | * Local Variables: |
| 582 | * eval: (c-set-style "gnu") |
| 583 | * End: |
| 584 | */ |