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 | |
| 36 | static u8 * |
| 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 | |
| 64 | static void |
| 65 | vl_api_application_attach_reply_t_handler (vl_api_application_attach_reply_t * |
| 66 | mp) |
| 67 | { |
| 68 | static svm_fifo_segment_create_args_t _a; |
| 69 | svm_fifo_segment_create_args_t *a = &_a; |
| 70 | int rv; |
| 71 | |
| 72 | memset (a, 0, sizeof (*a)); |
| 73 | if (mp->retval) |
| 74 | { |
| 75 | clib_warning ("VCL<%d>: attach failed: %U", getpid (), |
| 76 | format_api_error, ntohl (mp->retval)); |
| 77 | return; |
| 78 | } |
| 79 | |
| 80 | if (mp->segment_name_length == 0) |
| 81 | { |
| 82 | clib_warning ("VCL<%d>: segment_name_length zero", getpid ()); |
| 83 | return; |
| 84 | } |
| 85 | |
| 86 | a->segment_name = (char *) mp->segment_name; |
| 87 | a->segment_size = mp->segment_size; |
| 88 | |
| 89 | ASSERT (mp->app_event_queue_address); |
| 90 | |
| 91 | /* Attach to the segment vpp created */ |
| 92 | rv = svm_fifo_segment_attach (a); |
| 93 | vec_reset_length (a->new_segment_indices); |
| 94 | if (PREDICT_FALSE (rv)) |
| 95 | { |
| 96 | clib_warning ("VCL<%d>: svm_fifo_segment_attach ('%s') failed", |
| 97 | getpid (), mp->segment_name); |
| 98 | return; |
| 99 | } |
| 100 | |
| 101 | vcm->app_event_queue = |
| 102 | uword_to_pointer (mp->app_event_queue_address, svm_queue_t *); |
| 103 | |
| 104 | vcm->app_state = STATE_APP_ATTACHED; |
| 105 | } |
| 106 | |
| 107 | static void |
| 108 | vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t * |
| 109 | mp) |
| 110 | { |
| 111 | if (mp->retval) |
| 112 | clib_warning ("VCL<%d>: detach failed: %U", getpid (), format_api_error, |
| 113 | ntohl (mp->retval)); |
| 114 | |
| 115 | vcm->app_state = STATE_APP_ENABLED; |
| 116 | } |
| 117 | |
| 118 | static void |
| 119 | vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t * |
| 120 | mp) |
| 121 | { |
| 122 | if (mp->retval) |
| 123 | clib_warning ("VCL<%d>: vpp handle 0x%llx: disconnect session failed: %U", |
| 124 | getpid (), mp->handle, format_api_error, |
| 125 | ntohl (mp->retval)); |
| 126 | } |
| 127 | |
| 128 | static void |
| 129 | vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp) |
| 130 | { |
| 131 | static svm_fifo_segment_create_args_t _a; |
| 132 | svm_fifo_segment_create_args_t *a = &_a; |
| 133 | int rv; |
| 134 | |
| 135 | memset (a, 0, sizeof (*a)); |
| 136 | a->segment_name = (char *) mp->segment_name; |
| 137 | a->segment_size = mp->segment_size; |
| 138 | /* Attach to the segment vpp created */ |
| 139 | rv = svm_fifo_segment_attach (a); |
| 140 | vec_reset_length (a->new_segment_indices); |
| 141 | if (PREDICT_FALSE (rv)) |
| 142 | { |
| 143 | clib_warning ("VCL<%d>: svm_fifo_segment_attach ('%s') failed", |
| 144 | getpid (), mp->segment_name); |
| 145 | return; |
| 146 | } |
| 147 | |
| 148 | VDBG (1, "VCL<%d>: mapped new segment '%s' size %d", getpid (), |
| 149 | mp->segment_name, mp->segment_size); |
| 150 | } |
| 151 | |
| 152 | static void |
| 153 | vl_api_unmap_segment_t_handler (vl_api_unmap_segment_t * mp) |
| 154 | { |
| 155 | |
| 156 | /* |
| 157 | * XXX Need segment_name to session_id hash, |
| 158 | * XXX - have sessionID by handle hash currently |
| 159 | */ |
| 160 | |
| 161 | VDBG (1, "Unmapped segment '%s'", mp->segment_name); |
| 162 | } |
| 163 | |
| 164 | static void |
| 165 | vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp) |
| 166 | { |
| 167 | uword *p; |
| 168 | |
| 169 | p = hash_get (vcm->session_index_by_vpp_handles, mp->handle); |
| 170 | if (p) |
| 171 | { |
| 172 | int rv; |
| 173 | vcl_session_t *session = 0; |
| 174 | u32 session_index = p[0]; |
| 175 | |
| 176 | VCL_SESSION_LOCK_AND_GET (session_index, &session); |
| 177 | session->session_state = STATE_CLOSE_ON_EMPTY; |
| 178 | |
| 179 | VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: setting state to 0x%x " |
| 180 | "(%s)", getpid (), mp->handle, session_index, |
| 181 | session->session_state, |
| 182 | vppcom_session_state_str (session->session_state)); |
| 183 | VCL_SESSION_UNLOCK (); |
| 184 | return; |
| 185 | |
| 186 | done: |
| 187 | VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: session lookup failed!", |
| 188 | getpid (), mp->handle, session_index); |
| 189 | } |
| 190 | else |
| 191 | clib_warning ("VCL<%d>: vpp handle 0x%llx: session lookup by " |
| 192 | "handle failed!", getpid (), mp->handle); |
| 193 | } |
| 194 | |
| 195 | static void |
| 196 | vl_api_reset_session_t_handler (vl_api_reset_session_t * mp) |
| 197 | { |
| 198 | vcl_session_t *session = 0; |
| 199 | vl_api_reset_session_reply_t *rmp; |
| 200 | uword *p; |
| 201 | int rv = 0; |
| 202 | |
| 203 | p = hash_get (vcm->session_index_by_vpp_handles, mp->handle); |
| 204 | if (p) |
| 205 | { |
| 206 | int rval; |
| 207 | VCL_SESSION_LOCK (); |
| 208 | rval = vppcom_session_at_index (p[0], &session); |
| 209 | if (PREDICT_FALSE (rval)) |
| 210 | { |
| 211 | rv = VNET_API_ERROR_INVALID_VALUE_2; |
| 212 | clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: " |
| 213 | "session lookup failed! returning %d %U", |
| 214 | getpid (), mp->handle, p[0], |
| 215 | rv, format_api_error, rv); |
| 216 | } |
| 217 | else |
| 218 | { |
| 219 | /* TBD: should this disconnect immediately and |
| 220 | * flush the fifos? |
| 221 | */ |
| 222 | session->session_state = STATE_CLOSE_ON_EMPTY; |
| 223 | |
| 224 | VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: state set to %d " |
| 225 | "(%s)!", getpid (), mp->handle, p[0], session->session_state, |
| 226 | vppcom_session_state_str (session->session_state)); |
| 227 | } |
| 228 | VCL_SESSION_UNLOCK (); |
| 229 | } |
| 230 | else |
| 231 | { |
| 232 | rv = VNET_API_ERROR_INVALID_VALUE; |
| 233 | clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx: session lookup " |
| 234 | "failed! returning %d %U", |
| 235 | getpid (), mp->handle, rv, format_api_error, rv); |
| 236 | } |
| 237 | |
| 238 | rmp = vl_msg_api_alloc (sizeof (*rmp)); |
| 239 | memset (rmp, 0, sizeof (*rmp)); |
| 240 | rmp->_vl_msg_id = ntohs (VL_API_RESET_SESSION_REPLY); |
| 241 | rmp->retval = htonl (rv); |
| 242 | rmp->handle = mp->handle; |
| 243 | vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp); |
| 244 | } |
| 245 | |
| 246 | static void |
| 247 | vl_api_connect_session_reply_t_handler (vl_api_connect_session_reply_t * mp) |
| 248 | { |
| 249 | vcl_session_t *session = 0; |
| 250 | u32 session_index; |
| 251 | svm_fifo_t *rx_fifo, *tx_fifo; |
| 252 | int rv = VPPCOM_OK; |
| 253 | |
| 254 | session_index = mp->context; |
| 255 | VCL_SESSION_LOCK_AND_GET (session_index, &session); |
| 256 | done: |
| 257 | if (mp->retval) |
| 258 | { |
| 259 | clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: " |
| 260 | "connect failed! %U", |
| 261 | getpid (), mp->handle, session_index, |
| 262 | format_api_error, ntohl (mp->retval)); |
| 263 | if (session) |
| 264 | { |
| 265 | session->session_state = STATE_FAILED; |
| 266 | session->vpp_handle = mp->handle; |
| 267 | } |
| 268 | else |
| 269 | { |
| 270 | clib_warning ("[%s] ERROR: vpp handle 0x%llx, sid %u: " |
| 271 | "Invalid session index (%u)!", |
| 272 | getpid (), mp->handle, session_index); |
| 273 | } |
| 274 | goto done_unlock; |
| 275 | } |
| 276 | |
| 277 | if (rv) |
| 278 | goto done_unlock; |
| 279 | |
| 280 | /* |
| 281 | * Setup session |
| 282 | */ |
| 283 | if (vcm->session_io_thread.io_sessions_lockp) |
| 284 | { |
| 285 | // Add this connection to the active io sessions list |
| 286 | VCL_IO_SESSIONS_LOCK (); |
| 287 | u32 *active_session_index; |
| 288 | pool_get (vcm->session_io_thread.active_session_indexes, |
| 289 | active_session_index); |
| 290 | *active_session_index = session_index; |
| 291 | VCL_IO_SESSIONS_UNLOCK (); |
| 292 | } |
| 293 | session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address, |
| 294 | svm_queue_t *); |
| 295 | |
| 296 | rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *); |
| 297 | rx_fifo->client_session_index = session_index; |
| 298 | tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *); |
| 299 | tx_fifo->client_session_index = session_index; |
| 300 | |
| 301 | session->rx_fifo = rx_fifo; |
| 302 | session->tx_fifo = tx_fifo; |
| 303 | session->vpp_handle = mp->handle; |
| 304 | session->transport.is_ip4 = mp->is_ip4; |
| 305 | clib_memcpy (&session->transport.lcl_ip, mp->lcl_ip, |
| 306 | sizeof (session->transport.rmt_ip)); |
| 307 | session->transport.lcl_port = mp->lcl_port; |
| 308 | session->session_state = STATE_CONNECT; |
| 309 | |
| 310 | /* Add it to lookup table */ |
| 311 | hash_set (vcm->session_index_by_vpp_handles, mp->handle, session_index); |
| 312 | |
| 313 | VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: connect succeeded! " |
| 314 | "session_rx_fifo %p, refcnt %d, session_tx_fifo %p, refcnt %d", |
| 315 | getpid (), mp->handle, session_index, session->rx_fifo, |
| 316 | session->rx_fifo->refcnt, session->tx_fifo, session->tx_fifo->refcnt); |
| 317 | done_unlock: |
| 318 | VCL_SESSION_UNLOCK (); |
| 319 | } |
| 320 | |
| 321 | static void |
| 322 | vl_api_bind_sock_reply_t_handler (vl_api_bind_sock_reply_t * mp) |
| 323 | { |
| 324 | vcl_session_t *session = 0; |
| 325 | u32 session_index = mp->context; |
| 326 | int rv; |
| 327 | |
| 328 | VCL_SESSION_LOCK_AND_GET (session_index, &session); |
| 329 | done: |
| 330 | if (mp->retval) |
| 331 | { |
| 332 | clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, " |
| 333 | "sid %u: bind failed: %U", |
| 334 | getpid (), mp->handle, session_index, |
| 335 | format_api_error, ntohl (mp->retval)); |
| 336 | rv = vppcom_session_at_index (session_index, &session); |
| 337 | if (rv == VPPCOM_OK) |
| 338 | { |
| 339 | session->session_state = STATE_FAILED; |
| 340 | session->vpp_handle = mp->handle; |
| 341 | } |
| 342 | else |
| 343 | { |
| 344 | clib_warning ("[%s] ERROR: vpp handle 0x%llx, sid %u: " |
| 345 | "Invalid session index (%u)!", |
| 346 | getpid (), mp->handle, session_index); |
| 347 | } |
| 348 | goto done_unlock; |
| 349 | } |
| 350 | |
| 351 | session->vpp_handle = mp->handle; |
| 352 | session->transport.is_ip4 = mp->lcl_is_ip4; |
| 353 | session->transport.lcl_ip = to_ip46 (mp->lcl_is_ip4 ? IP46_TYPE_IP4 : |
| 354 | IP46_TYPE_IP6, mp->lcl_ip); |
| 355 | session->transport.lcl_port = mp->lcl_port; |
| 356 | vppcom_session_table_add_listener (mp->handle, session_index); |
| 357 | session->session_state = STATE_LISTEN; |
| 358 | |
| 359 | VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: bind succeeded!", |
| 360 | getpid (), mp->handle, mp->context); |
| 361 | done_unlock: |
| 362 | VCL_SESSION_UNLOCK (); |
| 363 | } |
| 364 | |
| 365 | static void |
| 366 | vl_api_unbind_sock_reply_t_handler (vl_api_unbind_sock_reply_t * mp) |
| 367 | { |
| 368 | if (mp->retval) |
| 369 | clib_warning ("VCL<%d>: ERROR: sid %u: unbind failed: %U", |
| 370 | getpid (), mp->context, format_api_error, |
| 371 | ntohl (mp->retval)); |
| 372 | |
| 373 | else |
| 374 | VDBG (1, "VCL<%d>: sid %u: unbind succeeded!", getpid (), mp->context); |
| 375 | } |
| 376 | |
| 377 | static void |
| 378 | vl_api_accept_session_t_handler (vl_api_accept_session_t * mp) |
| 379 | { |
| 380 | svm_fifo_t *rx_fifo, *tx_fifo; |
| 381 | vcl_session_t *session, *listen_session; |
| 382 | u32 session_index; |
| 383 | vce_event_connect_request_t *ecr; |
| 384 | vce_event_t *ev; |
| 385 | int rv; |
| 386 | u32 ev_idx; |
| 387 | uword elts = 0; |
| 388 | |
| 389 | VCL_SESSION_LOCK (); |
| 390 | |
| 391 | VCL_ACCEPT_FIFO_LOCK (); |
| 392 | elts = clib_fifo_free_elts (vcm->client_session_index_fifo); |
| 393 | VCL_ACCEPT_FIFO_UNLOCK (); |
| 394 | |
| 395 | if (!elts) |
| 396 | { |
| 397 | clib_warning ("VCL<%d>: client session queue is full!", getpid ()); |
| 398 | vppcom_send_accept_session_reply (mp->handle, mp->context, |
| 399 | VNET_API_ERROR_QUEUE_FULL); |
| 400 | VCL_SESSION_UNLOCK (); |
| 401 | return; |
| 402 | } |
| 403 | |
| 404 | listen_session = vppcom_session_table_lookup_listener (mp->listener_handle); |
| 405 | if (!listen_session) |
| 406 | { |
| 407 | clib_warning ("VCL<%d>: ERROR: couldn't find listen session: " |
| 408 | "unknown vpp listener handle %llx", |
| 409 | getpid (), mp->listener_handle); |
| 410 | vppcom_send_accept_session_reply (mp->handle, mp->context, |
| 411 | VNET_API_ERROR_INVALID_ARGUMENT); |
| 412 | VCL_SESSION_UNLOCK (); |
| 413 | return; |
| 414 | } |
| 415 | |
| 416 | /* TODO check listener depth and update */ |
| 417 | /* TODO on "child" fd close, update listener depth */ |
| 418 | |
| 419 | /* Allocate local session and set it up */ |
| 420 | pool_get (vcm->sessions, session); |
| 421 | memset (session, 0, sizeof (*session)); |
| 422 | session_index = (u32) (session - vcm->sessions); |
| 423 | |
| 424 | rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *); |
| 425 | rx_fifo->client_session_index = session_index; |
| 426 | tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *); |
| 427 | tx_fifo->client_session_index = session_index; |
| 428 | |
| 429 | session->vpp_handle = mp->handle; |
| 430 | session->client_context = mp->context; |
| 431 | session->rx_fifo = rx_fifo; |
| 432 | session->tx_fifo = tx_fifo; |
| 433 | session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address, |
| 434 | svm_queue_t *); |
| 435 | session->session_state = STATE_ACCEPT; |
| 436 | session->transport.rmt_port = mp->port; |
| 437 | session->transport.is_ip4 = mp->is_ip4; |
| 438 | session->transport.rmt_ip = to_ip46 (mp->is_ip4 ? IP46_TYPE_IP4 : |
| 439 | IP46_TYPE_IP6, mp->ip); |
| 440 | |
| 441 | /* Add it to lookup table */ |
| 442 | hash_set (vcm->session_index_by_vpp_handles, mp->handle, session_index); |
| 443 | session->transport.lcl_port = listen_session->transport.lcl_port; |
| 444 | session->transport.lcl_ip = listen_session->transport.lcl_ip; |
| 445 | |
| 446 | /* Create an event for handlers */ |
| 447 | |
| 448 | VCL_EVENTS_LOCK (); |
| 449 | |
| 450 | pool_get (vcm->event_thread.vce_events, ev); |
| 451 | ev_idx = (u32) (ev - vcm->event_thread.vce_events); |
| 452 | ecr = vce_get_event_data (ev, sizeof (*ecr)); |
| 453 | ev->evk.eid = VCL_EVENT_CONNECT_REQ_ACCEPTED; |
| 454 | listen_session = vppcom_session_table_lookup_listener (mp->listener_handle); |
| 455 | ev->evk.session_index = (u32) (listen_session - vcm->sessions); |
| 456 | ecr->accepted_session_index = session_index; |
| 457 | |
| 458 | VCL_EVENTS_UNLOCK (); |
| 459 | |
| 460 | rv = vce_generate_event (&vcm->event_thread, ev_idx); |
| 461 | ASSERT (rv == 0); |
| 462 | |
| 463 | VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: client accept request from %s" |
| 464 | " address %U port %d queue %p!", getpid (), mp->handle, session_index, |
| 465 | mp->is_ip4 ? "IPv4" : "IPv6", format_ip46_address, &mp->ip, |
| 466 | mp->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6, |
| 467 | clib_net_to_host_u16 (mp->port), session->vpp_evt_q); |
| 468 | |
| 469 | vcl_evt (VCL_EVT_ACCEPT, session, listen_session, session_index); |
| 470 | VCL_SESSION_UNLOCK (); |
| 471 | } |
| 472 | |
| 473 | #define foreach_sock_msg \ |
| 474 | _(SESSION_ENABLE_DISABLE_REPLY, session_enable_disable_reply) \ |
| 475 | _(BIND_SOCK_REPLY, bind_sock_reply) \ |
| 476 | _(UNBIND_SOCK_REPLY, unbind_sock_reply) \ |
| 477 | _(ACCEPT_SESSION, accept_session) \ |
| 478 | _(CONNECT_SESSION_REPLY, connect_session_reply) \ |
| 479 | _(DISCONNECT_SESSION, disconnect_session) \ |
| 480 | _(DISCONNECT_SESSION_REPLY, disconnect_session_reply) \ |
| 481 | _(RESET_SESSION, reset_session) \ |
| 482 | _(APPLICATION_ATTACH_REPLY, application_attach_reply) \ |
| 483 | _(APPLICATION_DETACH_REPLY, application_detach_reply) \ |
| 484 | _(MAP_ANOTHER_SEGMENT, map_another_segment) \ |
| 485 | _(UNMAP_SEGMENT, unmap_segment) |
| 486 | |
| 487 | void |
| 488 | vppcom_api_hookup (void) |
| 489 | { |
| 490 | #define _(N, n) \ |
| 491 | vl_msg_api_set_handlers(VL_API_##N, #n, \ |
| 492 | vl_api_##n##_t_handler, \ |
| 493 | vl_noop_handler, \ |
| 494 | vl_api_##n##_t_endian, \ |
| 495 | vl_api_##n##_t_print, \ |
| 496 | sizeof(vl_api_##n##_t), 1); |
| 497 | foreach_sock_msg; |
| 498 | #undef _ |
| 499 | } |
| 500 | |
| 501 | /* |
| 502 | * VPP-API message functions |
| 503 | */ |
| 504 | void |
| 505 | vppcom_send_session_enable_disable (u8 is_enable) |
| 506 | { |
| 507 | vl_api_session_enable_disable_t *bmp; |
| 508 | bmp = vl_msg_api_alloc (sizeof (*bmp)); |
| 509 | memset (bmp, 0, sizeof (*bmp)); |
| 510 | |
| 511 | bmp->_vl_msg_id = ntohs (VL_API_SESSION_ENABLE_DISABLE); |
| 512 | bmp->client_index = vcm->my_client_index; |
| 513 | bmp->context = htonl (0xfeedface); |
| 514 | bmp->is_enable = is_enable; |
| 515 | vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp); |
| 516 | } |
| 517 | |
| 518 | void |
| 519 | vppcom_app_send_attach (void) |
| 520 | { |
| 521 | vl_api_application_attach_t *bmp; |
| 522 | u8 nsid_len = vec_len (vcm->cfg.namespace_id); |
| 523 | u8 app_is_proxy = (vcm->cfg.app_proxy_transport_tcp || |
| 524 | vcm->cfg.app_proxy_transport_udp); |
| 525 | |
| 526 | bmp = vl_msg_api_alloc (sizeof (*bmp)); |
| 527 | memset (bmp, 0, sizeof (*bmp)); |
| 528 | |
| 529 | bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_ATTACH); |
| 530 | bmp->client_index = vcm->my_client_index; |
| 531 | bmp->context = htonl (0xfeedface); |
| 532 | bmp->options[APP_OPTIONS_FLAGS] = |
| 533 | APP_OPTIONS_FLAGS_ACCEPT_REDIRECT | APP_OPTIONS_FLAGS_ADD_SEGMENT | |
| 534 | (vcm->cfg.app_scope_local ? APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE : 0) | |
| 535 | (vcm->cfg.app_scope_global ? APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE : 0) | |
| 536 | (app_is_proxy ? APP_OPTIONS_FLAGS_IS_PROXY : 0); |
| 537 | bmp->options[APP_OPTIONS_PROXY_TRANSPORT] = |
| 538 | (u64) ((vcm->cfg.app_proxy_transport_tcp ? 1 << TRANSPORT_PROTO_TCP : 0) | |
| 539 | (vcm->cfg.app_proxy_transport_udp ? 1 << TRANSPORT_PROTO_UDP : 0)); |
| 540 | bmp->options[APP_OPTIONS_SEGMENT_SIZE] = vcm->cfg.segment_size; |
| 541 | bmp->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = vcm->cfg.add_segment_size; |
| 542 | bmp->options[APP_OPTIONS_RX_FIFO_SIZE] = vcm->cfg.rx_fifo_size; |
| 543 | bmp->options[APP_OPTIONS_TX_FIFO_SIZE] = vcm->cfg.tx_fifo_size; |
| 544 | bmp->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = |
| 545 | vcm->cfg.preallocated_fifo_pairs; |
| 546 | bmp->options[APP_OPTIONS_EVT_QUEUE_SIZE] = vcm->cfg.event_queue_size; |
| 547 | if (nsid_len) |
| 548 | { |
| 549 | bmp->namespace_id_len = nsid_len; |
| 550 | clib_memcpy (bmp->namespace_id, vcm->cfg.namespace_id, nsid_len); |
| 551 | bmp->options[APP_OPTIONS_NAMESPACE_SECRET] = vcm->cfg.namespace_secret; |
| 552 | } |
| 553 | vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp); |
| 554 | } |
| 555 | |
| 556 | void |
| 557 | vppcom_app_send_detach (void) |
| 558 | { |
| 559 | vl_api_application_detach_t *bmp; |
| 560 | bmp = vl_msg_api_alloc (sizeof (*bmp)); |
| 561 | memset (bmp, 0, sizeof (*bmp)); |
| 562 | |
| 563 | bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH); |
| 564 | bmp->client_index = vcm->my_client_index; |
| 565 | bmp->context = htonl (0xfeedface); |
| 566 | vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp); |
| 567 | } |
| 568 | |
| 569 | void |
| 570 | vppcom_send_connect_sock (vcl_session_t * session, u32 session_index) |
| 571 | { |
| 572 | vl_api_connect_sock_t *cmp; |
| 573 | |
| 574 | /* Assumes caller as acquired the spinlock: vcm->sessions_lockp */ |
| 575 | cmp = vl_msg_api_alloc (sizeof (*cmp)); |
| 576 | memset (cmp, 0, sizeof (*cmp)); |
| 577 | cmp->_vl_msg_id = ntohs (VL_API_CONNECT_SOCK); |
| 578 | cmp->client_index = vcm->my_client_index; |
| 579 | cmp->context = session_index; |
| 580 | |
| 581 | cmp->is_ip4 = session->transport.is_ip4; |
| 582 | clib_memcpy (cmp->ip, &session->transport.rmt_ip, sizeof (cmp->ip)); |
| 583 | cmp->port = session->transport.rmt_port; |
| 584 | cmp->proto = session->session_type; |
| 585 | clib_memcpy (cmp->options, session->options, sizeof (cmp->options)); |
| 586 | vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & cmp); |
| 587 | } |
| 588 | |
| 589 | void |
| 590 | vppcom_send_disconnect_session_reply (u64 vpp_handle, u32 session_index, |
| 591 | int rv) |
| 592 | { |
| 593 | vl_api_disconnect_session_reply_t *rmp; |
| 594 | |
| 595 | VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: sending disconnect msg", |
| 596 | getpid (), vpp_handle, session_index); |
| 597 | |
| 598 | rmp = vl_msg_api_alloc (sizeof (*rmp)); |
| 599 | memset (rmp, 0, sizeof (*rmp)); |
| 600 | |
| 601 | rmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION_REPLY); |
| 602 | rmp->retval = htonl (rv); |
| 603 | rmp->handle = vpp_handle; |
| 604 | vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp); |
| 605 | } |
| 606 | |
| 607 | void |
| 608 | vppcom_send_disconnect_session (u64 vpp_handle, u32 session_index) |
| 609 | { |
| 610 | vl_api_disconnect_session_t *dmp; |
| 611 | |
| 612 | VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: sending disconnect msg", |
| 613 | getpid (), vpp_handle, session_index); |
| 614 | |
| 615 | dmp = vl_msg_api_alloc (sizeof (*dmp)); |
| 616 | memset (dmp, 0, sizeof (*dmp)); |
| 617 | dmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION); |
| 618 | dmp->client_index = vcm->my_client_index; |
| 619 | dmp->handle = vpp_handle; |
| 620 | vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & dmp); |
| 621 | } |
| 622 | |
| 623 | /* VPP combines bind and listen as one operation. VCL manages the separation |
| 624 | * of bind and listen locally via vppcom_session_bind() and |
| 625 | * vppcom_session_listen() */ |
| 626 | void |
| 627 | vppcom_send_bind_sock (vcl_session_t * session, u32 session_index) |
| 628 | { |
| 629 | vl_api_bind_sock_t *bmp; |
| 630 | |
| 631 | /* Assumes caller has acquired spinlock: vcm->sessions_lockp */ |
| 632 | bmp = vl_msg_api_alloc (sizeof (*bmp)); |
| 633 | memset (bmp, 0, sizeof (*bmp)); |
| 634 | |
| 635 | bmp->_vl_msg_id = ntohs (VL_API_BIND_SOCK); |
| 636 | bmp->client_index = vcm->my_client_index; |
| 637 | bmp->context = session_index; |
| 638 | bmp->is_ip4 = session->transport.is_ip4; |
| 639 | clib_memcpy (bmp->ip, &session->transport.lcl_ip, sizeof (bmp->ip)); |
| 640 | bmp->port = session->transport.lcl_port; |
| 641 | bmp->proto = session->session_type; |
| 642 | clib_memcpy (bmp->options, session->options, sizeof (bmp->options)); |
| 643 | vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp); |
| 644 | } |
| 645 | |
| 646 | void |
| 647 | vppcom_send_unbind_sock (u64 vpp_handle) |
| 648 | { |
| 649 | vl_api_unbind_sock_t *ump; |
| 650 | |
| 651 | ump = vl_msg_api_alloc (sizeof (*ump)); |
| 652 | memset (ump, 0, sizeof (*ump)); |
| 653 | |
| 654 | ump->_vl_msg_id = ntohs (VL_API_UNBIND_SOCK); |
| 655 | ump->client_index = vcm->my_client_index; |
| 656 | ump->handle = vpp_handle; |
| 657 | vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & ump); |
| 658 | } |
| 659 | |
| 660 | void |
| 661 | vppcom_send_accept_session_reply (u64 handle, u32 context, int retval) |
| 662 | { |
| 663 | vl_api_accept_session_reply_t *rmp; |
| 664 | |
| 665 | rmp = vl_msg_api_alloc (sizeof (*rmp)); |
| 666 | memset (rmp, 0, sizeof (*rmp)); |
| 667 | rmp->_vl_msg_id = ntohs (VL_API_ACCEPT_SESSION_REPLY); |
| 668 | rmp->retval = htonl (retval); |
| 669 | rmp->context = context; |
| 670 | rmp->handle = handle; |
| 671 | vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp); |
| 672 | } |
| 673 | |
| 674 | u32 |
| 675 | vcl_max_nsid_len (void) |
| 676 | { |
| 677 | vl_api_application_attach_t *mp; |
| 678 | return (sizeof (mp->namespace_id) - 1); |
| 679 | } |
| 680 | |
| 681 | void |
| 682 | vppcom_init_error_string_table (void) |
| 683 | { |
| 684 | vcm->error_string_by_error_number = hash_create (0, sizeof (uword)); |
| 685 | |
| 686 | #define _(n, v, s) hash_set (vcm->error_string_by_error_number, -v, s); |
| 687 | foreach_vnet_api_error; |
| 688 | #undef _ |
| 689 | |
| 690 | hash_set (vcm->error_string_by_error_number, 99, "Misc"); |
| 691 | } |
| 692 | |
| 693 | int |
| 694 | vppcom_connect_to_vpp (char *app_name) |
| 695 | { |
| 696 | api_main_t *am = &api_main; |
| 697 | vppcom_cfg_t *vcl_cfg = &vcm->cfg; |
| 698 | int rv = VPPCOM_OK; |
| 699 | |
| 700 | if (!vcl_cfg->vpp_api_filename) |
| 701 | vcl_cfg->vpp_api_filename = format (0, "/vpe-api%c", 0); |
| 702 | |
| 703 | VDBG (0, "VCL<%d>: app (%s) connecting to VPP api (%s)...", |
| 704 | getpid (), app_name, vcl_cfg->vpp_api_filename); |
| 705 | |
| 706 | if (vl_client_connect_to_vlib ((char *) vcl_cfg->vpp_api_filename, app_name, |
| 707 | vcm->cfg.vpp_api_q_length) < 0) |
| 708 | { |
| 709 | clib_warning ("VCL<%d>: app (%s) connect failed!", getpid (), app_name); |
| 710 | rv = VPPCOM_ECONNREFUSED; |
| 711 | } |
| 712 | else |
| 713 | { |
| 714 | vcm->vl_input_queue = am->shmem_hdr->vl_input_queue; |
| 715 | vcm->my_client_index = (u32) am->my_client_index; |
| 716 | vcm->app_state = STATE_APP_CONN_VPP; |
| 717 | |
| 718 | VDBG (0, "VCL<%d>: app (%s) is connected to VPP!", getpid (), app_name); |
| 719 | } |
| 720 | |
| 721 | vcl_evt (VCL_EVT_INIT, vcm); |
| 722 | return rv; |
| 723 | } |
| 724 | |
| 725 | /* |
| 726 | * fd.io coding-style-patch-verification: ON |
| 727 | * |
| 728 | * Local Variables: |
| 729 | * eval: (c-set-style "gnu") |
| 730 | * End: |
| 731 | */ |