Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 Cisco and/or its affiliates. |
| 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 |
| 65 | ssvm_segment_attach (char *name, ssvm_segment_type_t type, int fd) |
| 66 | { |
| 67 | svm_fifo_segment_create_args_t _a, *a = &_a; |
| 68 | int rv; |
| 69 | |
| 70 | memset (a, 0, sizeof (*a)); |
| 71 | a->segment_name = (char *) name; |
| 72 | a->segment_type = type; |
| 73 | |
| 74 | if (type == SSVM_SEGMENT_MEMFD) |
| 75 | a->memfd_fd = fd; |
| 76 | |
| 77 | if ((rv = svm_fifo_segment_attach (a))) |
| 78 | { |
| 79 | clib_warning ("svm_fifo_segment_attach ('%s') failed", name); |
| 80 | return rv; |
| 81 | } |
| 82 | vec_reset_length (a->new_segment_indices); |
| 83 | return 0; |
| 84 | } |
| 85 | |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 86 | static void |
| 87 | vl_api_application_attach_reply_t_handler (vl_api_application_attach_reply_t * |
| 88 | mp) |
| 89 | { |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 90 | vcl_worker_t *wrk = vcl_worker_get (0); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 91 | u32 n_fds = 0; |
| 92 | int *fds = 0; |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 93 | |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 94 | if (mp->retval) |
| 95 | { |
| 96 | clib_warning ("VCL<%d>: attach failed: %U", getpid (), |
| 97 | format_api_error, ntohl (mp->retval)); |
| 98 | return; |
| 99 | } |
| 100 | |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 101 | wrk->app_event_queue = uword_to_pointer (mp->app_event_queue_address, |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 102 | svm_msg_q_t *); |
| 103 | if (mp->n_fds) |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 104 | { |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 105 | vec_validate (fds, mp->n_fds); |
| 106 | vl_socket_client_recv_fd_msg (fds, mp->n_fds, 5); |
| 107 | |
| 108 | if (mp->fd_flags & SESSION_FD_F_VPP_MQ_SEGMENT) |
| 109 | if (ssvm_segment_attach ("vpp-mq-seg", SSVM_SEGMENT_MEMFD, |
| 110 | fds[n_fds++])) |
| 111 | return; |
| 112 | |
| 113 | if (mp->fd_flags & SESSION_FD_F_MEMFD_SEGMENT) |
| 114 | if (ssvm_segment_attach ((char *) mp->segment_name, |
| 115 | SSVM_SEGMENT_MEMFD, fds[n_fds++])) |
| 116 | return; |
| 117 | |
| 118 | if (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD) |
| 119 | { |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 120 | svm_msg_q_set_consumer_eventfd (wrk->app_event_queue, fds[n_fds]); |
| 121 | vcl_mq_epoll_add_evfd (wrk, wrk->app_event_queue); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 122 | n_fds++; |
| 123 | } |
| 124 | |
| 125 | vec_free (fds); |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 126 | } |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 127 | else |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 128 | { |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 129 | if (ssvm_segment_attach ((char *) mp->segment_name, SSVM_SEGMENT_SHM, |
| 130 | -1)) |
| 131 | return; |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 134 | vcm->app_state = STATE_APP_ATTACHED; |
| 135 | } |
| 136 | |
| 137 | static void |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 138 | vl_api_app_worker_add_del_reply_t_handler (vl_api_app_worker_add_del_reply_t * |
| 139 | mp) |
| 140 | { |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 141 | int n_fds = 0, *fds = 0; |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 142 | vcl_worker_t *wrk; |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 143 | u32 wrk_index; |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 144 | |
| 145 | if (mp->retval) |
| 146 | { |
| 147 | clib_warning ("VCL<%d>: add/del worker failed: %U", getpid (), |
| 148 | format_api_error, ntohl (mp->retval)); |
| 149 | goto failed; |
| 150 | } |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 151 | wrk_index = clib_net_to_host_u32 (mp->wrk_index); |
| 152 | if (mp->context != wrk_index) |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 153 | { |
| 154 | clib_warning ("VCL<%d>: wrk numbering doesn't match ours: %u, vpp: %u", |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 155 | getpid (), mp->context, wrk_index); |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 156 | goto failed; |
| 157 | } |
Florin Coras | 3348a4c | 2018-09-07 17:09:35 -0700 | [diff] [blame] | 158 | if (!mp->is_add) |
| 159 | return; |
| 160 | |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 161 | wrk = vcl_worker_get (wrk_index); |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 162 | wrk->app_event_queue = uword_to_pointer (mp->app_event_queue_address, |
| 163 | svm_msg_q_t *); |
| 164 | |
| 165 | if (mp->n_fds) |
| 166 | { |
| 167 | vec_validate (fds, mp->n_fds); |
| 168 | vl_socket_client_recv_fd_msg (fds, mp->n_fds, 5); |
| 169 | |
| 170 | if (mp->fd_flags & SESSION_FD_F_VPP_MQ_SEGMENT) |
| 171 | if (ssvm_segment_attach ("vpp-worker-seg", SSVM_SEGMENT_MEMFD, |
| 172 | fds[n_fds++])) |
| 173 | goto failed; |
| 174 | |
| 175 | if (mp->fd_flags & SESSION_FD_F_MEMFD_SEGMENT) |
| 176 | if (ssvm_segment_attach ((char *) mp->segment_name, |
| 177 | SSVM_SEGMENT_MEMFD, fds[n_fds++])) |
| 178 | goto failed; |
| 179 | |
| 180 | if (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD) |
| 181 | { |
| 182 | svm_msg_q_set_consumer_eventfd (wrk->app_event_queue, fds[n_fds]); |
| 183 | vcl_mq_epoll_add_evfd (wrk, wrk->app_event_queue); |
| 184 | n_fds++; |
| 185 | } |
| 186 | |
| 187 | vec_free (fds); |
| 188 | } |
| 189 | else |
| 190 | { |
| 191 | if (ssvm_segment_attach ((char *) mp->segment_name, SSVM_SEGMENT_SHM, |
| 192 | -1)) |
| 193 | goto failed; |
| 194 | } |
| 195 | vcm->app_state = STATE_APP_READY; |
| 196 | return; |
| 197 | |
| 198 | failed: |
| 199 | vcm->app_state = STATE_APP_FAILED; |
| 200 | } |
| 201 | |
| 202 | static void |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 203 | vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t * |
| 204 | mp) |
| 205 | { |
| 206 | if (mp->retval) |
| 207 | clib_warning ("VCL<%d>: detach failed: %U", getpid (), format_api_error, |
| 208 | ntohl (mp->retval)); |
| 209 | |
| 210 | vcm->app_state = STATE_APP_ENABLED; |
| 211 | } |
| 212 | |
| 213 | static void |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 214 | vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp) |
| 215 | { |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 216 | ssvm_segment_type_t seg_type = SSVM_SEGMENT_SHM; |
| 217 | int fd = -1; |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 218 | |
Florin Coras | 460dce6 | 2018-07-27 05:45:06 -0700 | [diff] [blame] | 219 | vcm->mounting_segment = 1; |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 220 | |
| 221 | if (mp->fd_flags) |
| 222 | { |
| 223 | vl_socket_client_recv_fd_msg (&fd, 1, 5); |
| 224 | seg_type = SSVM_SEGMENT_MEMFD; |
| 225 | } |
| 226 | |
| 227 | if (PREDICT_FALSE (ssvm_segment_attach ((char *) mp->segment_name, |
| 228 | seg_type, fd))) |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 229 | { |
| 230 | clib_warning ("VCL<%d>: svm_fifo_segment_attach ('%s') failed", |
| 231 | getpid (), mp->segment_name); |
| 232 | return; |
| 233 | } |
| 234 | |
| 235 | VDBG (1, "VCL<%d>: mapped new segment '%s' size %d", getpid (), |
| 236 | mp->segment_name, mp->segment_size); |
Florin Coras | 460dce6 | 2018-07-27 05:45:06 -0700 | [diff] [blame] | 237 | vcm->mounting_segment = 0; |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | static void |
| 241 | vl_api_unmap_segment_t_handler (vl_api_unmap_segment_t * mp) |
| 242 | { |
| 243 | |
| 244 | /* |
| 245 | * XXX Need segment_name to session_id hash, |
| 246 | * XXX - have sessionID by handle hash currently |
| 247 | */ |
| 248 | |
| 249 | VDBG (1, "Unmapped segment '%s'", mp->segment_name); |
| 250 | } |
| 251 | |
| 252 | static void |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 253 | vl_api_app_cut_through_registration_add_t_handler |
| 254 | (vl_api_app_cut_through_registration_add_t * mp) |
| 255 | { |
| 256 | vcl_cut_through_registration_t *ctr; |
| 257 | u32 mqc_index = ~0; |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 258 | vcl_worker_t *wrk; |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 259 | int *fds = 0; |
| 260 | |
| 261 | if (mp->n_fds) |
| 262 | { |
| 263 | ASSERT (mp->n_fds == 2); |
| 264 | vec_validate (fds, mp->n_fds); |
| 265 | vl_socket_client_recv_fd_msg (fds, mp->n_fds, 5); |
| 266 | } |
| 267 | |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 268 | wrk = vcl_worker_get (mp->wrk_index); |
| 269 | ctr = vcl_ct_registration_lock_and_alloc (wrk); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 270 | ctr->mq = uword_to_pointer (mp->evt_q_address, svm_msg_q_t *); |
| 271 | ctr->peer_mq = uword_to_pointer (mp->peer_evt_q_address, svm_msg_q_t *); |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 272 | VDBG (0, "Adding ct registration %u", vcl_ct_registration_index (wrk, ctr)); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 273 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 274 | if (mp->n_fds && (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD)) |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 275 | { |
| 276 | svm_msg_q_set_consumer_eventfd (ctr->mq, fds[0]); |
| 277 | svm_msg_q_set_producer_eventfd (ctr->peer_mq, fds[1]); |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 278 | mqc_index = vcl_mq_epoll_add_evfd (wrk, ctr->mq); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 279 | ctr->epoll_evt_conn_index = mqc_index; |
| 280 | vec_free (fds); |
| 281 | } |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 282 | vcl_ct_registration_lookup_add (wrk, mp->evt_q_address, |
| 283 | vcl_ct_registration_index (wrk, ctr)); |
| 284 | vcl_ct_registration_unlock (wrk); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | static void |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 288 | vl_api_bind_sock_reply_t_handler (vl_api_bind_sock_reply_t * mp) |
| 289 | { |
Florin Coras | 6011699 | 2018-08-27 09:52:18 -0700 | [diff] [blame] | 290 | /* Expecting a similar message on mq. So ignore this */ |
| 291 | VDBG (1, "VCL<%d>: bapi msg vpp handle 0x%llx, sid %u: bind retval: %u!", |
| 292 | getpid (), mp->handle, mp->context, mp->retval); |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | static void |
| 296 | vl_api_unbind_sock_reply_t_handler (vl_api_unbind_sock_reply_t * mp) |
| 297 | { |
| 298 | if (mp->retval) |
| 299 | clib_warning ("VCL<%d>: ERROR: sid %u: unbind failed: %U", |
| 300 | getpid (), mp->context, format_api_error, |
| 301 | ntohl (mp->retval)); |
| 302 | |
| 303 | else |
| 304 | VDBG (1, "VCL<%d>: sid %u: unbind succeeded!", getpid (), mp->context); |
| 305 | } |
| 306 | |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 307 | static void |
| 308 | vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t * |
| 309 | mp) |
| 310 | { |
| 311 | if (mp->retval) |
| 312 | clib_warning ("VCL<%d>: ERROR: sid %u: disconnect failed: %U", |
| 313 | getpid (), mp->context, format_api_error, |
| 314 | ntohl (mp->retval)); |
| 315 | } |
| 316 | |
| 317 | static void |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 318 | vl_api_connect_session_reply_t_handler (vl_api_connect_sock_reply_t * mp) |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 319 | { |
| 320 | if (mp->retval) |
| 321 | clib_warning ("VCL<%d>: ERROR: sid %u: connect failed: %U", |
| 322 | getpid (), mp->context, format_api_error, |
| 323 | ntohl (mp->retval)); |
| 324 | } |
| 325 | |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 326 | #define foreach_sock_msg \ |
| 327 | _(SESSION_ENABLE_DISABLE_REPLY, session_enable_disable_reply) \ |
| 328 | _(BIND_SOCK_REPLY, bind_sock_reply) \ |
| 329 | _(UNBIND_SOCK_REPLY, unbind_sock_reply) \ |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 330 | _(CONNECT_SESSION_REPLY, connect_session_reply) \ |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 331 | _(DISCONNECT_SESSION_REPLY, disconnect_session_reply) \ |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 332 | _(APPLICATION_ATTACH_REPLY, application_attach_reply) \ |
| 333 | _(APPLICATION_DETACH_REPLY, application_detach_reply) \ |
| 334 | _(MAP_ANOTHER_SEGMENT, map_another_segment) \ |
| 335 | _(UNMAP_SEGMENT, unmap_segment) \ |
| 336 | _(APP_CUT_THROUGH_REGISTRATION_ADD, app_cut_through_registration_add) \ |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 337 | _(APP_WORKER_ADD_DEL_REPLY, app_worker_add_del_reply) \ |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 338 | |
| 339 | void |
| 340 | vppcom_api_hookup (void) |
| 341 | { |
Florin Coras | 6011699 | 2018-08-27 09:52:18 -0700 | [diff] [blame] | 342 | #define _(N, n) \ |
| 343 | vl_msg_api_set_handlers(VL_API_##N, #n, \ |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 344 | vl_api_##n##_t_handler, \ |
| 345 | vl_noop_handler, \ |
| 346 | vl_api_##n##_t_endian, \ |
| 347 | vl_api_##n##_t_print, \ |
| 348 | sizeof(vl_api_##n##_t), 1); |
| 349 | foreach_sock_msg; |
| 350 | #undef _ |
| 351 | } |
| 352 | |
| 353 | /* |
| 354 | * VPP-API message functions |
| 355 | */ |
| 356 | void |
| 357 | vppcom_send_session_enable_disable (u8 is_enable) |
| 358 | { |
| 359 | vl_api_session_enable_disable_t *bmp; |
| 360 | bmp = vl_msg_api_alloc (sizeof (*bmp)); |
| 361 | memset (bmp, 0, sizeof (*bmp)); |
| 362 | |
| 363 | bmp->_vl_msg_id = ntohs (VL_API_SESSION_ENABLE_DISABLE); |
| 364 | bmp->client_index = vcm->my_client_index; |
| 365 | bmp->context = htonl (0xfeedface); |
| 366 | bmp->is_enable = is_enable; |
| 367 | vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp); |
| 368 | } |
| 369 | |
| 370 | void |
| 371 | vppcom_app_send_attach (void) |
| 372 | { |
| 373 | vl_api_application_attach_t *bmp; |
| 374 | u8 nsid_len = vec_len (vcm->cfg.namespace_id); |
| 375 | u8 app_is_proxy = (vcm->cfg.app_proxy_transport_tcp || |
| 376 | vcm->cfg.app_proxy_transport_udp); |
| 377 | |
| 378 | bmp = vl_msg_api_alloc (sizeof (*bmp)); |
| 379 | memset (bmp, 0, sizeof (*bmp)); |
| 380 | |
| 381 | bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_ATTACH); |
| 382 | bmp->client_index = vcm->my_client_index; |
| 383 | bmp->context = htonl (0xfeedface); |
| 384 | bmp->options[APP_OPTIONS_FLAGS] = |
| 385 | APP_OPTIONS_FLAGS_ACCEPT_REDIRECT | APP_OPTIONS_FLAGS_ADD_SEGMENT | |
| 386 | (vcm->cfg.app_scope_local ? APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE : 0) | |
| 387 | (vcm->cfg.app_scope_global ? APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE : 0) | |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 388 | (app_is_proxy ? APP_OPTIONS_FLAGS_IS_PROXY : 0) | |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 389 | APP_OPTIONS_FLAGS_USE_MQ_FOR_CTRL_MSGS | |
| 390 | (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] | 391 | bmp->options[APP_OPTIONS_PROXY_TRANSPORT] = |
| 392 | (u64) ((vcm->cfg.app_proxy_transport_tcp ? 1 << TRANSPORT_PROTO_TCP : 0) | |
| 393 | (vcm->cfg.app_proxy_transport_udp ? 1 << TRANSPORT_PROTO_UDP : 0)); |
| 394 | bmp->options[APP_OPTIONS_SEGMENT_SIZE] = vcm->cfg.segment_size; |
| 395 | bmp->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = vcm->cfg.add_segment_size; |
| 396 | bmp->options[APP_OPTIONS_RX_FIFO_SIZE] = vcm->cfg.rx_fifo_size; |
| 397 | bmp->options[APP_OPTIONS_TX_FIFO_SIZE] = vcm->cfg.tx_fifo_size; |
| 398 | bmp->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = |
| 399 | vcm->cfg.preallocated_fifo_pairs; |
| 400 | bmp->options[APP_OPTIONS_EVT_QUEUE_SIZE] = vcm->cfg.event_queue_size; |
| 401 | if (nsid_len) |
| 402 | { |
| 403 | bmp->namespace_id_len = nsid_len; |
| 404 | clib_memcpy (bmp->namespace_id, vcm->cfg.namespace_id, nsid_len); |
| 405 | bmp->options[APP_OPTIONS_NAMESPACE_SECRET] = vcm->cfg.namespace_secret; |
| 406 | } |
| 407 | vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp); |
| 408 | } |
| 409 | |
| 410 | void |
| 411 | vppcom_app_send_detach (void) |
| 412 | { |
| 413 | vl_api_application_detach_t *bmp; |
| 414 | bmp = vl_msg_api_alloc (sizeof (*bmp)); |
| 415 | memset (bmp, 0, sizeof (*bmp)); |
| 416 | |
| 417 | bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH); |
| 418 | bmp->client_index = vcm->my_client_index; |
| 419 | bmp->context = htonl (0xfeedface); |
| 420 | vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp); |
| 421 | } |
| 422 | |
| 423 | void |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 424 | vcl_send_app_worker_add_del (u8 is_add) |
| 425 | { |
| 426 | vcl_worker_t *wrk = vcl_worker_get_current (); |
| 427 | vl_api_app_worker_add_del_t *mp; |
| 428 | u32 wrk_index = wrk->wrk_index; |
| 429 | |
| 430 | mp = vl_msg_api_alloc (sizeof (*mp)); |
| 431 | memset (mp, 0, sizeof (*mp)); |
| 432 | |
| 433 | mp->_vl_msg_id = ntohs (VL_API_APP_WORKER_ADD_DEL); |
| 434 | mp->client_index = vcm->my_client_index; |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 435 | mp->app_api_index = clib_host_to_net_u32 (vcm->my_client_index); |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 436 | mp->context = wrk_index; |
| 437 | mp->is_add = is_add; |
| 438 | if (!is_add) |
| 439 | mp->wrk_index = clib_host_to_net_u32 (wrk_index); |
| 440 | |
| 441 | vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & mp); |
| 442 | } |
| 443 | |
| 444 | void |
| 445 | vppcom_send_connect_sock (vcl_session_t * session) |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 446 | { |
| 447 | vl_api_connect_sock_t *cmp; |
| 448 | |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 449 | cmp = vl_msg_api_alloc (sizeof (*cmp)); |
| 450 | memset (cmp, 0, sizeof (*cmp)); |
| 451 | cmp->_vl_msg_id = ntohs (VL_API_CONNECT_SOCK); |
| 452 | cmp->client_index = vcm->my_client_index; |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 453 | cmp->context = session->session_index; |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 454 | cmp->wrk_index = vcl_get_worker_index (); |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 455 | cmp->is_ip4 = session->transport.is_ip4; |
| 456 | clib_memcpy (cmp->ip, &session->transport.rmt_ip, sizeof (cmp->ip)); |
| 457 | cmp->port = session->transport.rmt_port; |
| 458 | cmp->proto = session->session_type; |
| 459 | clib_memcpy (cmp->options, session->options, sizeof (cmp->options)); |
| 460 | vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & cmp); |
| 461 | } |
| 462 | |
| 463 | void |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 464 | vppcom_send_disconnect_session (u64 vpp_handle) |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 465 | { |
| 466 | vl_api_disconnect_session_t *dmp; |
| 467 | |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 468 | dmp = vl_msg_api_alloc (sizeof (*dmp)); |
| 469 | memset (dmp, 0, sizeof (*dmp)); |
| 470 | dmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION); |
| 471 | dmp->client_index = vcm->my_client_index; |
| 472 | dmp->handle = vpp_handle; |
| 473 | vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & dmp); |
| 474 | } |
| 475 | |
| 476 | /* VPP combines bind and listen as one operation. VCL manages the separation |
| 477 | * of bind and listen locally via vppcom_session_bind() and |
| 478 | * vppcom_session_listen() */ |
| 479 | void |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 480 | vppcom_send_bind_sock (vcl_session_t * session) |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 481 | { |
| 482 | vl_api_bind_sock_t *bmp; |
| 483 | |
| 484 | /* Assumes caller has acquired spinlock: vcm->sessions_lockp */ |
| 485 | bmp = vl_msg_api_alloc (sizeof (*bmp)); |
| 486 | memset (bmp, 0, sizeof (*bmp)); |
| 487 | |
| 488 | bmp->_vl_msg_id = ntohs (VL_API_BIND_SOCK); |
| 489 | bmp->client_index = vcm->my_client_index; |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 490 | bmp->context = session->session_index; |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 491 | bmp->wrk_index = vcl_get_worker_index (); |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 492 | bmp->is_ip4 = session->transport.is_ip4; |
| 493 | clib_memcpy (bmp->ip, &session->transport.lcl_ip, sizeof (bmp->ip)); |
| 494 | bmp->port = session->transport.lcl_port; |
| 495 | bmp->proto = session->session_type; |
| 496 | clib_memcpy (bmp->options, session->options, sizeof (bmp->options)); |
| 497 | vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp); |
| 498 | } |
| 499 | |
| 500 | void |
| 501 | vppcom_send_unbind_sock (u64 vpp_handle) |
| 502 | { |
| 503 | vl_api_unbind_sock_t *ump; |
| 504 | |
| 505 | ump = vl_msg_api_alloc (sizeof (*ump)); |
| 506 | memset (ump, 0, sizeof (*ump)); |
| 507 | |
| 508 | ump->_vl_msg_id = ntohs (VL_API_UNBIND_SOCK); |
| 509 | ump->client_index = vcm->my_client_index; |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 510 | ump->wrk_index = vcl_get_worker_index (); |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 511 | ump->handle = vpp_handle; |
| 512 | vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & ump); |
| 513 | } |
| 514 | |
| 515 | void |
| 516 | vppcom_send_accept_session_reply (u64 handle, u32 context, int retval) |
| 517 | { |
| 518 | vl_api_accept_session_reply_t *rmp; |
| 519 | |
| 520 | rmp = vl_msg_api_alloc (sizeof (*rmp)); |
| 521 | memset (rmp, 0, sizeof (*rmp)); |
| 522 | rmp->_vl_msg_id = ntohs (VL_API_ACCEPT_SESSION_REPLY); |
| 523 | rmp->retval = htonl (retval); |
| 524 | rmp->context = context; |
| 525 | rmp->handle = handle; |
| 526 | vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp); |
| 527 | } |
| 528 | |
| 529 | u32 |
| 530 | vcl_max_nsid_len (void) |
| 531 | { |
| 532 | vl_api_application_attach_t *mp; |
| 533 | return (sizeof (mp->namespace_id) - 1); |
| 534 | } |
| 535 | |
| 536 | void |
| 537 | vppcom_init_error_string_table (void) |
| 538 | { |
| 539 | vcm->error_string_by_error_number = hash_create (0, sizeof (uword)); |
| 540 | |
| 541 | #define _(n, v, s) hash_set (vcm->error_string_by_error_number, -v, s); |
| 542 | foreach_vnet_api_error; |
| 543 | #undef _ |
| 544 | |
| 545 | hash_set (vcm->error_string_by_error_number, 99, "Misc"); |
| 546 | } |
| 547 | |
| 548 | int |
| 549 | vppcom_connect_to_vpp (char *app_name) |
| 550 | { |
| 551 | api_main_t *am = &api_main; |
| 552 | vppcom_cfg_t *vcl_cfg = &vcm->cfg; |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 553 | |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 554 | if (vcl_cfg->vpp_api_socket_name) |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 555 | { |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 556 | if (vl_socket_client_connect ((char *) vcl_cfg->vpp_api_socket_name, |
| 557 | app_name, 0 /* default rx/tx buffer */ )) |
| 558 | { |
| 559 | clib_warning ("VCL<%d>: app (%s) socket connect failed!", |
| 560 | getpid (), app_name); |
| 561 | return VPPCOM_ECONNREFUSED; |
| 562 | } |
| 563 | |
| 564 | if (vl_socket_client_init_shm (0)) |
| 565 | { |
| 566 | clib_warning ("VCL<%d>: app (%s) init shm failed!", |
| 567 | getpid (), app_name); |
| 568 | return VPPCOM_ECONNREFUSED; |
| 569 | } |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 570 | } |
| 571 | else |
| 572 | { |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 573 | if (!vcl_cfg->vpp_api_filename) |
| 574 | vcl_cfg->vpp_api_filename = format (0, "/vpe-api%c", 0); |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 575 | |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 576 | VDBG (0, "VCL<%d>: app (%s) connecting to VPP api (%s)...", getpid (), |
| 577 | app_name, vcl_cfg->vpp_api_filename); |
| 578 | |
| 579 | if (vl_client_connect_to_vlib ((char *) vcl_cfg->vpp_api_filename, |
| 580 | app_name, vcm->cfg.vpp_api_q_length) < 0) |
| 581 | { |
| 582 | clib_warning ("VCL<%d>: app (%s) connect failed!", getpid (), |
| 583 | app_name); |
| 584 | return VPPCOM_ECONNREFUSED; |
| 585 | } |
| 586 | |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 587 | } |
| 588 | |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 589 | vcm->vl_input_queue = am->shmem_hdr->vl_input_queue; |
| 590 | vcm->my_client_index = (u32) am->my_client_index; |
| 591 | vcm->app_state = STATE_APP_CONN_VPP; |
| 592 | |
| 593 | VDBG (0, "VCL<%d>: app (%s) is connected to VPP!", getpid (), app_name); |
| 594 | |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 595 | vcl_evt (VCL_EVT_INIT, vcm); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 596 | return VPPCOM_OK; |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | /* |
| 600 | * fd.io coding-style-patch-verification: ON |
| 601 | * |
| 602 | * Local Variables: |
| 603 | * eval: (c-set-style "gnu") |
| 604 | * End: |
| 605 | */ |