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