Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1 | /* |
Dave Wallace | 33e002b | 2017-09-06 01:20:02 -0400 | [diff] [blame] | 2 | * Copyright (c) 2017 Cisco and/or its affiliates. |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [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 <stdio.h> |
| 17 | #include <stdlib.h> |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 18 | #include <svm/svm_fifo_segment.h> |
Dave Wallace | 5c7cf1c | 2017-10-24 04:12:18 -0400 | [diff] [blame] | 19 | #include <vcl/vppcom.h> |
Keith Burns (alagalah) | 3cf2d64 | 2018-02-23 10:17:01 -0800 | [diff] [blame] | 20 | #include <vcl/vcl_event.h> |
Florin Coras | 0d427d8 | 2018-06-27 03:24:07 -0700 | [diff] [blame] | 21 | #include <vcl/vcl_debug.h> |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 22 | #include <vcl/vcl_private.h> |
Dave Wallace | 7e607a7 | 2018-06-18 18:41:32 -0400 | [diff] [blame] | 23 | |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 24 | static u8 not_ready; |
| 25 | |
| 26 | void |
| 27 | sigsegv_signal (int signum) |
| 28 | { |
| 29 | not_ready = 1; |
| 30 | } |
| 31 | |
| 32 | static void |
| 33 | vcl_wait_for_memory (void *mem) |
| 34 | { |
| 35 | u8 __clib_unused test; |
Florin Coras | 460dce6 | 2018-07-27 05:45:06 -0700 | [diff] [blame] | 36 | if (vcm->mounting_segment) |
| 37 | { |
| 38 | while (vcm->mounting_segment) |
| 39 | ; |
| 40 | return; |
| 41 | } |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 42 | if (1 || vcm->debug) |
| 43 | { |
Florin Coras | 460dce6 | 2018-07-27 05:45:06 -0700 | [diff] [blame] | 44 | usleep (1e5); |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 45 | return; |
| 46 | } |
| 47 | if (signal (SIGSEGV, sigsegv_signal)) |
| 48 | { |
| 49 | perror ("signal()"); |
| 50 | return; |
| 51 | } |
| 52 | not_ready = 0; |
| 53 | |
| 54 | again: |
| 55 | test = *(u8 *) mem; |
| 56 | if (not_ready) |
| 57 | { |
| 58 | not_ready = 0; |
| 59 | usleep (1); |
| 60 | goto again; |
| 61 | } |
| 62 | |
| 63 | signal (SIGSEGV, SIG_DFL); |
| 64 | } |
| 65 | |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 66 | static const char * |
| 67 | vppcom_app_state_str (app_state_t state) |
| 68 | { |
| 69 | char *st; |
| 70 | |
| 71 | switch (state) |
| 72 | { |
| 73 | case STATE_APP_START: |
| 74 | st = "STATE_APP_START"; |
| 75 | break; |
| 76 | |
| 77 | case STATE_APP_CONN_VPP: |
| 78 | st = "STATE_APP_CONN_VPP"; |
| 79 | break; |
| 80 | |
| 81 | case STATE_APP_ENABLED: |
| 82 | st = "STATE_APP_ENABLED"; |
| 83 | break; |
| 84 | |
| 85 | case STATE_APP_ATTACHED: |
| 86 | st = "STATE_APP_ATTACHED"; |
| 87 | break; |
| 88 | |
| 89 | default: |
| 90 | st = "UNKNOWN_APP_STATE"; |
| 91 | break; |
| 92 | } |
| 93 | |
| 94 | return st; |
| 95 | } |
| 96 | |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 97 | const char * |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 98 | vppcom_session_state_str (session_state_t state) |
| 99 | { |
| 100 | char *st; |
| 101 | |
| 102 | switch (state) |
| 103 | { |
| 104 | case STATE_START: |
| 105 | st = "STATE_START"; |
| 106 | break; |
| 107 | |
| 108 | case STATE_CONNECT: |
| 109 | st = "STATE_CONNECT"; |
| 110 | break; |
| 111 | |
| 112 | case STATE_LISTEN: |
| 113 | st = "STATE_LISTEN"; |
| 114 | break; |
| 115 | |
| 116 | case STATE_ACCEPT: |
| 117 | st = "STATE_ACCEPT"; |
| 118 | break; |
| 119 | |
Dave Wallace | 4878cbe | 2017-11-21 03:45:09 -0500 | [diff] [blame] | 120 | case STATE_CLOSE_ON_EMPTY: |
| 121 | st = "STATE_CLOSE_ON_EMPTY"; |
| 122 | break; |
| 123 | |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 124 | case STATE_DISCONNECT: |
| 125 | st = "STATE_DISCONNECT"; |
| 126 | break; |
| 127 | |
| 128 | case STATE_FAILED: |
| 129 | st = "STATE_FAILED"; |
| 130 | break; |
| 131 | |
| 132 | default: |
| 133 | st = "UNKNOWN_STATE"; |
| 134 | break; |
| 135 | } |
| 136 | |
| 137 | return st; |
| 138 | } |
| 139 | |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 140 | u8 * |
| 141 | format_ip4_address (u8 * s, va_list * args) |
| 142 | { |
| 143 | u8 *a = va_arg (*args, u8 *); |
| 144 | return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]); |
| 145 | } |
| 146 | |
| 147 | u8 * |
| 148 | format_ip6_address (u8 * s, va_list * args) |
| 149 | { |
| 150 | ip6_address_t *a = va_arg (*args, ip6_address_t *); |
| 151 | u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon; |
| 152 | |
| 153 | i_max_n_zero = ARRAY_LEN (a->as_u16); |
| 154 | max_n_zeros = 0; |
| 155 | i_first_zero = i_max_n_zero; |
| 156 | n_zeros = 0; |
| 157 | for (i = 0; i < ARRAY_LEN (a->as_u16); i++) |
| 158 | { |
| 159 | u32 is_zero = a->as_u16[i] == 0; |
| 160 | if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16)) |
| 161 | { |
| 162 | i_first_zero = i; |
| 163 | n_zeros = 0; |
| 164 | } |
| 165 | n_zeros += is_zero; |
| 166 | if ((!is_zero && n_zeros > max_n_zeros) |
| 167 | || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros)) |
| 168 | { |
| 169 | i_max_n_zero = i_first_zero; |
| 170 | max_n_zeros = n_zeros; |
| 171 | i_first_zero = ARRAY_LEN (a->as_u16); |
| 172 | n_zeros = 0; |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | last_double_colon = 0; |
| 177 | for (i = 0; i < ARRAY_LEN (a->as_u16); i++) |
| 178 | { |
| 179 | if (i == i_max_n_zero && max_n_zeros > 1) |
| 180 | { |
| 181 | s = format (s, "::"); |
| 182 | i += max_n_zeros - 1; |
| 183 | last_double_colon = 1; |
| 184 | } |
| 185 | else |
| 186 | { |
| 187 | s = format (s, "%s%x", |
| 188 | (last_double_colon || i == 0) ? "" : ":", |
| 189 | clib_net_to_host_u16 (a->as_u16[i])); |
| 190 | last_double_colon = 0; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | return s; |
| 195 | } |
| 196 | |
| 197 | /* Format an IP46 address. */ |
| 198 | u8 * |
| 199 | format_ip46_address (u8 * s, va_list * args) |
| 200 | { |
| 201 | ip46_address_t *ip46 = va_arg (*args, ip46_address_t *); |
| 202 | ip46_type_t type = va_arg (*args, ip46_type_t); |
| 203 | int is_ip4 = 1; |
| 204 | |
| 205 | switch (type) |
| 206 | { |
| 207 | case IP46_TYPE_ANY: |
| 208 | is_ip4 = ip46_address_is_ip4 (ip46); |
| 209 | break; |
| 210 | case IP46_TYPE_IP4: |
| 211 | is_ip4 = 1; |
| 212 | break; |
| 213 | case IP46_TYPE_IP6: |
| 214 | is_ip4 = 0; |
| 215 | break; |
| 216 | } |
| 217 | |
| 218 | return is_ip4 ? |
| 219 | format (s, "%U", format_ip4_address, &ip46->ip4) : |
| 220 | format (s, "%U", format_ip6_address, &ip46->ip6); |
| 221 | } |
| 222 | |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 223 | /* |
| 224 | * VPPCOM Utility Functions |
| 225 | */ |
| 226 | |
| 227 | static inline void |
| 228 | vppcom_session_table_del_listener (u64 listener_handle) |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 229 | { |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 230 | listener_handle |= 1ULL << 63; |
| 231 | hash_unset (vcm->session_index_by_vpp_handles, listener_handle); |
| 232 | } |
| 233 | |
| 234 | static inline int |
| 235 | vppcom_wait_for_app_state_change (app_state_t app_state) |
| 236 | { |
| 237 | f64 timeout = clib_time_now (&vcm->clib_time) + vcm->cfg.app_timeout; |
| 238 | |
| 239 | while (clib_time_now (&vcm->clib_time) < timeout) |
| 240 | { |
| 241 | if (vcm->app_state == app_state) |
| 242 | return VPPCOM_OK; |
| 243 | } |
| 244 | VDBG (0, "VCL<%d>: timeout waiting for state %s (%d)", getpid (), |
| 245 | vppcom_app_state_str (app_state), app_state); |
| 246 | vcl_evt (VCL_EVT_SESSION_TIMEOUT, vcm, app_state); |
| 247 | |
| 248 | return VPPCOM_ETIMEDOUT; |
| 249 | } |
| 250 | |
Florin Coras | c9fbd66 | 2018-08-24 12:59:56 -0700 | [diff] [blame] | 251 | static svm_msg_q_t * |
| 252 | vcl_session_vpp_evt_q (vcl_session_t * s) |
| 253 | { |
| 254 | if (vcl_session_is_ct (s)) |
| 255 | return vcm->vpp_event_queues[0]; |
| 256 | else |
| 257 | return vcm->vpp_event_queues[s->tx_fifo->master_thread_index]; |
| 258 | } |
| 259 | |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 260 | static void |
| 261 | vcl_send_session_accepted_reply (svm_msg_q_t * mq, u32 context, |
| 262 | session_handle_t handle, int retval) |
| 263 | { |
| 264 | app_session_evt_t _app_evt, *app_evt = &_app_evt; |
| 265 | session_accepted_reply_msg_t *rmp; |
| 266 | app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_ACCEPTED_REPLY); |
| 267 | rmp = (session_accepted_reply_msg_t *) app_evt->evt->data; |
| 268 | rmp->handle = handle; |
| 269 | rmp->context = context; |
| 270 | rmp->retval = retval; |
| 271 | app_send_ctrl_evt_to_vpp (mq, app_evt); |
| 272 | } |
| 273 | |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 274 | static void |
| 275 | vcl_send_session_disconnected_reply (svm_msg_q_t * mq, u32 context, |
| 276 | session_handle_t handle, int retval) |
| 277 | { |
| 278 | app_session_evt_t _app_evt, *app_evt = &_app_evt; |
| 279 | session_disconnected_reply_msg_t *rmp; |
| 280 | app_alloc_ctrl_evt_to_vpp (mq, app_evt, |
| 281 | SESSION_CTRL_EVT_DISCONNECTED_REPLY); |
| 282 | rmp = (session_disconnected_reply_msg_t *) app_evt->evt->data; |
| 283 | rmp->handle = handle; |
| 284 | rmp->context = context; |
| 285 | rmp->retval = retval; |
| 286 | app_send_ctrl_evt_to_vpp (mq, app_evt); |
| 287 | } |
| 288 | |
Florin Coras | c9fbd66 | 2018-08-24 12:59:56 -0700 | [diff] [blame] | 289 | static void |
| 290 | vcl_send_session_reset_reply (svm_msg_q_t * mq, u32 context, |
| 291 | session_handle_t handle, int retval) |
| 292 | { |
| 293 | app_session_evt_t _app_evt, *app_evt = &_app_evt; |
| 294 | session_reset_reply_msg_t *rmp; |
| 295 | app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_RESET_REPLY); |
| 296 | rmp = (session_reset_reply_msg_t *) app_evt->evt->data; |
| 297 | rmp->handle = handle; |
| 298 | rmp->context = context; |
| 299 | rmp->retval = retval; |
| 300 | app_send_ctrl_evt_to_vpp (mq, app_evt); |
| 301 | } |
| 302 | |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 303 | static u32 |
| 304 | vcl_session_accepted_handler (session_accepted_msg_t * mp) |
| 305 | { |
| 306 | vcl_session_t *session, *listen_session; |
| 307 | svm_fifo_t *rx_fifo, *tx_fifo; |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 308 | u32 session_index, vpp_wrk_index; |
| 309 | svm_msg_q_t *evt_q; |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 310 | |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 311 | session = vcl_session_alloc (); |
| 312 | session_index = vcl_session_index (session); |
| 313 | |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 314 | listen_session = vppcom_session_table_lookup_listener (mp->listener_handle); |
| 315 | if (!listen_session) |
| 316 | { |
| 317 | svm_msg_q_t *evt_q; |
| 318 | evt_q = uword_to_pointer (mp->vpp_event_queue_address, svm_msg_q_t *); |
| 319 | clib_warning ("VCL<%d>: ERROR: couldn't find listen session: " |
| 320 | "unknown vpp listener handle %llx", |
| 321 | getpid (), mp->listener_handle); |
| 322 | vcl_send_session_accepted_reply (evt_q, mp->context, mp->handle, |
| 323 | VNET_API_ERROR_INVALID_ARGUMENT); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 324 | vcl_session_free (session); |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 325 | return VCL_INVALID_SESSION_INDEX; |
| 326 | } |
| 327 | |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 328 | rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *); |
| 329 | tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *); |
| 330 | |
| 331 | if (mp->server_event_queue_address) |
| 332 | { |
| 333 | session->vpp_evt_q = uword_to_pointer (mp->client_event_queue_address, |
| 334 | svm_msg_q_t *); |
| 335 | session->our_evt_q = uword_to_pointer (mp->server_event_queue_address, |
| 336 | svm_msg_q_t *); |
| 337 | vcl_wait_for_memory (session->vpp_evt_q); |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 338 | rx_fifo->master_session_index = session_index; |
| 339 | tx_fifo->master_session_index = session_index; |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 340 | vec_validate (vcm->vpp_event_queues, 0); |
| 341 | evt_q = uword_to_pointer (mp->vpp_event_queue_address, svm_msg_q_t *); |
| 342 | vcm->vpp_event_queues[0] = evt_q; |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 343 | } |
| 344 | else |
| 345 | { |
| 346 | session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address, |
| 347 | svm_msg_q_t *); |
| 348 | rx_fifo->client_session_index = session_index; |
| 349 | tx_fifo->client_session_index = session_index; |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 350 | |
| 351 | vpp_wrk_index = tx_fifo->master_thread_index; |
| 352 | vec_validate (vcm->vpp_event_queues, vpp_wrk_index); |
| 353 | vcm->vpp_event_queues[vpp_wrk_index] = session->vpp_evt_q; |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | session->vpp_handle = mp->handle; |
| 357 | session->client_context = mp->context; |
| 358 | session->rx_fifo = rx_fifo; |
| 359 | session->tx_fifo = tx_fifo; |
| 360 | |
| 361 | session->session_state = STATE_ACCEPT; |
| 362 | session->transport.rmt_port = mp->port; |
| 363 | session->transport.is_ip4 = mp->is_ip4; |
| 364 | clib_memcpy (&session->transport.rmt_ip, mp->ip, sizeof (ip46_address_t)); |
| 365 | |
| 366 | hash_set (vcm->session_index_by_vpp_handles, mp->handle, session_index); |
| 367 | session->transport.lcl_port = listen_session->transport.lcl_port; |
| 368 | session->transport.lcl_ip = listen_session->transport.lcl_ip; |
Florin Coras | 460dce6 | 2018-07-27 05:45:06 -0700 | [diff] [blame] | 369 | session->session_type = listen_session->session_type; |
| 370 | session->is_dgram = session->session_type == VPPCOM_PROTO_UDP; |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 371 | |
| 372 | VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: client accept request from %s" |
| 373 | " address %U port %d queue %p!", getpid (), mp->handle, session_index, |
| 374 | mp->is_ip4 ? "IPv4" : "IPv6", format_ip46_address, &mp->ip, |
| 375 | mp->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6, |
| 376 | clib_net_to_host_u16 (mp->port), session->vpp_evt_q); |
| 377 | vcl_evt (VCL_EVT_ACCEPT, session, listen_session, session_index); |
| 378 | |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 379 | return session_index; |
| 380 | } |
| 381 | |
| 382 | static u32 |
| 383 | vcl_session_connected_handler (session_connected_msg_t * mp) |
| 384 | { |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 385 | u32 session_index, vpp_wrk_index; |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 386 | svm_fifo_t *rx_fifo, *tx_fifo; |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 387 | vcl_session_t *session = 0; |
| 388 | svm_msg_q_t *evt_q; |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 389 | |
| 390 | session_index = mp->context; |
Florin Coras | 070453d | 2018-08-24 17:04:27 -0700 | [diff] [blame] | 391 | session = vcl_session_get (session_index); |
| 392 | if (!session) |
| 393 | { |
| 394 | clib_warning ("[%s] ERROR: vpp handle 0x%llx, sid %u: " |
| 395 | "Invalid session index (%u)!", |
| 396 | getpid (), mp->handle, session_index); |
| 397 | return VCL_INVALID_SESSION_INDEX; |
| 398 | } |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 399 | if (mp->retval) |
| 400 | { |
Florin Coras | 070453d | 2018-08-24 17:04:27 -0700 | [diff] [blame] | 401 | clib_warning ("VCL<%d>: ERROR: sid %u: connect failed! %U", getpid (), |
| 402 | mp->handle, session_index, format_api_error, |
| 403 | ntohl (mp->retval)); |
| 404 | session->session_state = STATE_FAILED; |
| 405 | session->vpp_handle = mp->handle; |
| 406 | return session_index; |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 407 | } |
| 408 | |
Florin Coras | 460dce6 | 2018-07-27 05:45:06 -0700 | [diff] [blame] | 409 | rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *); |
| 410 | tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *); |
| 411 | vcl_wait_for_memory (rx_fifo); |
| 412 | rx_fifo->client_session_index = session_index; |
| 413 | tx_fifo->client_session_index = session_index; |
| 414 | |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 415 | if (mp->client_event_queue_address) |
| 416 | { |
| 417 | session->vpp_evt_q = uword_to_pointer (mp->server_event_queue_address, |
| 418 | svm_msg_q_t *); |
| 419 | session->our_evt_q = uword_to_pointer (mp->client_event_queue_address, |
| 420 | svm_msg_q_t *); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 421 | |
| 422 | vec_validate (vcm->vpp_event_queues, 0); |
| 423 | evt_q = uword_to_pointer (mp->vpp_event_queue_address, svm_msg_q_t *); |
| 424 | vcm->vpp_event_queues[0] = evt_q; |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 425 | } |
| 426 | else |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 427 | { |
| 428 | session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address, |
| 429 | svm_msg_q_t *); |
| 430 | vpp_wrk_index = tx_fifo->master_thread_index; |
| 431 | vec_validate (vcm->vpp_event_queues, vpp_wrk_index); |
| 432 | vcm->vpp_event_queues[vpp_wrk_index] = session->vpp_evt_q; |
| 433 | } |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 434 | |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 435 | session->rx_fifo = rx_fifo; |
| 436 | session->tx_fifo = tx_fifo; |
| 437 | session->vpp_handle = mp->handle; |
| 438 | session->transport.is_ip4 = mp->is_ip4; |
| 439 | clib_memcpy (&session->transport.lcl_ip, mp->lcl_ip, |
| 440 | sizeof (session->transport.lcl_ip)); |
| 441 | session->transport.lcl_port = mp->lcl_port; |
| 442 | session->session_state = STATE_CONNECT; |
| 443 | |
| 444 | /* Add it to lookup table */ |
| 445 | hash_set (vcm->session_index_by_vpp_handles, mp->handle, session_index); |
| 446 | |
| 447 | VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: connect succeeded! " |
| 448 | "session_rx_fifo %p, refcnt %d, session_tx_fifo %p, refcnt %d", |
| 449 | getpid (), mp->handle, session_index, session->rx_fifo, |
| 450 | session->rx_fifo->refcnt, session->tx_fifo, session->tx_fifo->refcnt); |
Florin Coras | 070453d | 2018-08-24 17:04:27 -0700 | [diff] [blame] | 451 | |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 452 | return session_index; |
| 453 | } |
| 454 | |
Florin Coras | c9fbd66 | 2018-08-24 12:59:56 -0700 | [diff] [blame] | 455 | static u32 |
Florin Coras | 6011699 | 2018-08-27 09:52:18 -0700 | [diff] [blame] | 456 | vcl_session_reset_handler (session_reset_msg_t * reset_msg) |
Florin Coras | c9fbd66 | 2018-08-24 12:59:56 -0700 | [diff] [blame] | 457 | { |
| 458 | vcl_session_t *session; |
| 459 | u32 sid; |
| 460 | |
| 461 | sid = vcl_session_get_index_from_handle (reset_msg->handle); |
| 462 | session = vcl_session_get (sid); |
| 463 | if (!session) |
| 464 | { |
| 465 | VDBG (0, "request to reset unknown handle 0x%llx", reset_msg->handle); |
| 466 | return VCL_INVALID_SESSION_INDEX; |
| 467 | } |
| 468 | session->session_state = STATE_CLOSE_ON_EMPTY; |
| 469 | VDBG (0, "reset handle 0x%llx, sid %u ", reset_msg->handle, sid); |
| 470 | vcl_send_session_reset_reply (vcl_session_vpp_evt_q (session), |
| 471 | vcm->my_client_index, reset_msg->handle, 0); |
| 472 | return sid; |
| 473 | } |
| 474 | |
Florin Coras | 6011699 | 2018-08-27 09:52:18 -0700 | [diff] [blame] | 475 | static u32 |
| 476 | vcl_session_bound_handler (session_bound_msg_t * mp) |
| 477 | { |
| 478 | vcl_session_t *session; |
| 479 | u32 sid = mp->context; |
| 480 | |
| 481 | session = vcl_session_get (sid); |
| 482 | if (mp->retval) |
| 483 | { |
| 484 | VDBG (0, "VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: bind failed: %U", |
| 485 | getpid (), mp->handle, sid, format_api_error, ntohl (mp->retval)); |
| 486 | if (session) |
| 487 | { |
| 488 | session->session_state = STATE_FAILED; |
| 489 | session->vpp_handle = mp->handle; |
| 490 | return sid; |
| 491 | } |
| 492 | else |
| 493 | { |
| 494 | clib_warning ("[%s] ERROR: vpp handle 0x%llx, sid %u: " |
| 495 | "Invalid session index (%u)!", |
| 496 | getpid (), mp->handle, sid); |
| 497 | return VCL_INVALID_SESSION_INDEX; |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | session->vpp_handle = mp->handle; |
| 502 | session->transport.is_ip4 = mp->lcl_is_ip4; |
| 503 | clib_memcpy (&session->transport.lcl_ip, mp->lcl_ip, |
| 504 | sizeof (ip46_address_t)); |
| 505 | session->transport.lcl_port = mp->lcl_port; |
| 506 | vppcom_session_table_add_listener (mp->handle, sid); |
| 507 | session->session_state = STATE_LISTEN; |
| 508 | |
| 509 | if (session->is_dgram) |
| 510 | { |
| 511 | svm_fifo_t *rx_fifo, *tx_fifo; |
| 512 | session->vpp_evt_q = uword_to_pointer (mp->vpp_evt_q, svm_msg_q_t *); |
| 513 | rx_fifo = uword_to_pointer (mp->rx_fifo, svm_fifo_t *); |
| 514 | rx_fifo->client_session_index = sid; |
| 515 | tx_fifo = uword_to_pointer (mp->tx_fifo, svm_fifo_t *); |
| 516 | tx_fifo->client_session_index = sid; |
| 517 | session->rx_fifo = rx_fifo; |
| 518 | session->tx_fifo = tx_fifo; |
| 519 | } |
| 520 | |
| 521 | VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: bind succeeded!", |
| 522 | getpid (), mp->handle, sid); |
| 523 | return sid; |
| 524 | } |
| 525 | |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 526 | int |
| 527 | vcl_handle_mq_ctrl_event (session_event_t * e) |
| 528 | { |
| 529 | session_accepted_msg_t *accepted_msg; |
| 530 | session_disconnected_msg_t *disconnected_msg; |
| 531 | vcl_session_msg_t *vcl_msg; |
| 532 | vcl_session_t *session; |
| 533 | u64 handle; |
| 534 | u32 sid; |
| 535 | |
| 536 | switch (e->event_type) |
| 537 | { |
| 538 | case FIFO_EVENT_APP_RX: |
| 539 | clib_warning ("unhandled rx: sid %u (0x%x)", |
| 540 | e->fifo->client_session_index, |
| 541 | e->fifo->client_session_index); |
| 542 | break; |
| 543 | case SESSION_CTRL_EVT_ACCEPTED: |
| 544 | accepted_msg = (session_accepted_msg_t *) e->data; |
| 545 | handle = accepted_msg->listener_handle; |
| 546 | session = vppcom_session_table_lookup_listener (handle); |
| 547 | if (!session) |
| 548 | { |
| 549 | clib_warning ("VCL<%d>: ERROR: couldn't find listen session:" |
| 550 | "listener handle %llx", getpid (), handle); |
| 551 | break; |
| 552 | } |
| 553 | |
| 554 | clib_fifo_add2 (session->accept_evts_fifo, vcl_msg); |
| 555 | vcl_msg->accepted_msg = *accepted_msg; |
| 556 | break; |
| 557 | case SESSION_CTRL_EVT_CONNECTED: |
| 558 | vcl_session_connected_handler ((session_connected_msg_t *) e->data); |
| 559 | break; |
| 560 | case SESSION_CTRL_EVT_DISCONNECTED: |
| 561 | disconnected_msg = (session_disconnected_msg_t *) e->data; |
| 562 | sid = vcl_session_get_index_from_handle (disconnected_msg->handle); |
| 563 | session = vcl_session_get (sid); |
Florin Coras | c9fbd66 | 2018-08-24 12:59:56 -0700 | [diff] [blame] | 564 | if (!session) |
| 565 | { |
| 566 | VDBG (0, "request to disconnect unknown handle 0x%llx", |
| 567 | disconnected_msg->handle); |
| 568 | break; |
| 569 | } |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 570 | session->session_state = STATE_DISCONNECT; |
Florin Coras | c9fbd66 | 2018-08-24 12:59:56 -0700 | [diff] [blame] | 571 | VDBG (0, "disconnected handle 0xllx, sid %u", disconnected_msg->handle, |
| 572 | sid); |
| 573 | break; |
| 574 | case SESSION_CTRL_EVT_RESET: |
Florin Coras | 6011699 | 2018-08-27 09:52:18 -0700 | [diff] [blame] | 575 | vcl_session_reset_handler ((session_reset_msg_t *) e->data); |
| 576 | break; |
| 577 | case SESSION_CTRL_EVT_BOUND: |
| 578 | vcl_session_bound_handler ((session_bound_msg_t *) e->data); |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 579 | break; |
| 580 | default: |
| 581 | clib_warning ("unhandled %u", e->event_type); |
| 582 | } |
| 583 | return VPPCOM_OK; |
| 584 | } |
| 585 | |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 586 | static inline int |
| 587 | vppcom_wait_for_session_state_change (u32 session_index, |
| 588 | session_state_t state, |
| 589 | f64 wait_for_time) |
| 590 | { |
| 591 | f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time; |
| 592 | vcl_session_t *volatile session; |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 593 | svm_msg_q_msg_t msg; |
| 594 | session_event_t *e; |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 595 | |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 596 | do |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 597 | { |
Florin Coras | 070453d | 2018-08-24 17:04:27 -0700 | [diff] [blame] | 598 | session = vcl_session_get (session_index); |
| 599 | if (PREDICT_FALSE (!session)) |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 600 | { |
Florin Coras | 070453d | 2018-08-24 17:04:27 -0700 | [diff] [blame] | 601 | return VPPCOM_EBADFD; |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 602 | } |
| 603 | if (session->session_state & state) |
| 604 | { |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 605 | return VPPCOM_OK; |
| 606 | } |
| 607 | if (session->session_state & STATE_FAILED) |
| 608 | { |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 609 | return VPPCOM_ECONNREFUSED; |
| 610 | } |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 611 | |
| 612 | if (svm_msg_q_sub (vcm->app_event_queue, &msg, SVM_Q_NOWAIT, 0)) |
| 613 | continue; |
| 614 | e = svm_msg_q_msg_data (vcm->app_event_queue, &msg); |
| 615 | vcl_handle_mq_ctrl_event (e); |
| 616 | svm_msg_q_free_msg (vcm->app_event_queue, &msg); |
Florin Coras | dcf55ce | 2017-11-16 15:32:50 -0800 | [diff] [blame] | 617 | } |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 618 | while (clib_time_now (&vcm->clib_time) < timeout); |
Florin Coras | dcf55ce | 2017-11-16 15:32:50 -0800 | [diff] [blame] | 619 | |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 620 | VDBG (0, "VCL<%d>: timeout waiting for state 0x%x (%s)", getpid (), state, |
| 621 | vppcom_session_state_str (state)); |
| 622 | vcl_evt (VCL_EVT_SESSION_TIMEOUT, session, session_state); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 623 | |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 624 | return VPPCOM_ETIMEDOUT; |
Dave Wallace | 60caa06 | 2017-11-10 17:07:13 -0500 | [diff] [blame] | 625 | } |
| 626 | |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 627 | static int |
| 628 | vppcom_app_session_enable (void) |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 629 | { |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 630 | int rv; |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 631 | |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 632 | if (vcm->app_state != STATE_APP_ENABLED) |
| 633 | { |
| 634 | vppcom_send_session_enable_disable (1 /* is_enabled == TRUE */ ); |
| 635 | rv = vppcom_wait_for_app_state_change (STATE_APP_ENABLED); |
| 636 | if (PREDICT_FALSE (rv)) |
| 637 | { |
| 638 | VDBG (0, "VCL<%d>: application session enable timed out! " |
| 639 | "returning %d (%s)", getpid (), rv, vppcom_retval_str (rv)); |
| 640 | return rv; |
| 641 | } |
| 642 | } |
| 643 | return VPPCOM_OK; |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 644 | } |
| 645 | |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 646 | static int |
| 647 | vppcom_app_attach (void) |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 648 | { |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 649 | int rv; |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 650 | |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 651 | vppcom_app_send_attach (); |
| 652 | rv = vppcom_wait_for_app_state_change (STATE_APP_ATTACHED); |
| 653 | if (PREDICT_FALSE (rv)) |
| 654 | { |
| 655 | VDBG (0, "VCL<%d>: application attach timed out! returning %d (%s)", |
| 656 | getpid (), rv, vppcom_retval_str (rv)); |
| 657 | return rv; |
| 658 | } |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 659 | |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 660 | return VPPCOM_OK; |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 661 | } |
| 662 | |
| 663 | static int |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 664 | vppcom_session_unbind (u32 session_index) |
| 665 | { |
Florin Coras | 7e12d94 | 2018-06-27 14:32:43 -0700 | [diff] [blame] | 666 | vcl_session_t *session = 0; |
Dave Wallace | 4878cbe | 2017-11-21 03:45:09 -0500 | [diff] [blame] | 667 | u64 vpp_handle; |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 668 | |
Florin Coras | 070453d | 2018-08-24 17:04:27 -0700 | [diff] [blame] | 669 | session = vcl_session_get (session_index); |
| 670 | if (!session) |
| 671 | return VPPCOM_EBADFD; |
Dave Wallace | 4878cbe | 2017-11-21 03:45:09 -0500 | [diff] [blame] | 672 | |
| 673 | vpp_handle = session->vpp_handle; |
| 674 | vppcom_session_table_del_listener (vpp_handle); |
| 675 | session->vpp_handle = ~0; |
Florin Coras | 7e12d94 | 2018-06-27 14:32:43 -0700 | [diff] [blame] | 676 | session->session_state = STATE_DISCONNECT; |
Dave Wallace | 4878cbe | 2017-11-21 03:45:09 -0500 | [diff] [blame] | 677 | |
Florin Coras | 0d427d8 | 2018-06-27 03:24:07 -0700 | [diff] [blame] | 678 | VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: sending unbind msg! new state" |
| 679 | " 0x%x (%s)", getpid (), vpp_handle, session_index, STATE_DISCONNECT, |
| 680 | vppcom_session_state_str (STATE_DISCONNECT)); |
| 681 | vcl_evt (VCL_EVT_UNBIND, session); |
Dave Wallace | 4878cbe | 2017-11-21 03:45:09 -0500 | [diff] [blame] | 682 | vppcom_send_unbind_sock (vpp_handle); |
| 683 | |
Florin Coras | 070453d | 2018-08-24 17:04:27 -0700 | [diff] [blame] | 684 | return VPPCOM_OK; |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 685 | } |
| 686 | |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 687 | static int |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 688 | vppcom_session_disconnect (u32 session_index) |
| 689 | { |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 690 | svm_msg_q_t *vpp_evt_q; |
Florin Coras | 7e12d94 | 2018-06-27 14:32:43 -0700 | [diff] [blame] | 691 | vcl_session_t *session; |
Dave Wallace | 4878cbe | 2017-11-21 03:45:09 -0500 | [diff] [blame] | 692 | session_state_t state; |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 693 | u64 vpp_handle; |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 694 | |
Florin Coras | 070453d | 2018-08-24 17:04:27 -0700 | [diff] [blame] | 695 | session = vcl_session_get (session_index); |
Dave Wallace | 4878cbe | 2017-11-21 03:45:09 -0500 | [diff] [blame] | 696 | vpp_handle = session->vpp_handle; |
Florin Coras | 7e12d94 | 2018-06-27 14:32:43 -0700 | [diff] [blame] | 697 | state = session->session_state; |
Dave Wallace | 4878cbe | 2017-11-21 03:45:09 -0500 | [diff] [blame] | 698 | |
Florin Coras | 0d427d8 | 2018-06-27 03:24:07 -0700 | [diff] [blame] | 699 | VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u state 0x%x (%s)", getpid (), |
| 700 | vpp_handle, session_index, state, vppcom_session_state_str (state)); |
Dave Wallace | 66cf6eb | 2017-10-26 02:51:07 -0400 | [diff] [blame] | 701 | |
Keith Burns (alagalah) | 9b79377 | 2018-02-16 08:20:56 -0800 | [diff] [blame] | 702 | if (PREDICT_FALSE (state & STATE_LISTEN)) |
Dave Wallace | 66cf6eb | 2017-10-26 02:51:07 -0400 | [diff] [blame] | 703 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 704 | clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: " |
Dave Wallace | ee45d41 | 2017-11-24 21:44:06 -0500 | [diff] [blame] | 705 | "Cannot disconnect a listen socket!", |
| 706 | getpid (), vpp_handle, session_index); |
Florin Coras | 070453d | 2018-08-24 17:04:27 -0700 | [diff] [blame] | 707 | return VPPCOM_EBADFD; |
Dave Wallace | 4878cbe | 2017-11-21 03:45:09 -0500 | [diff] [blame] | 708 | } |
Dave Wallace | 66cf6eb | 2017-10-26 02:51:07 -0400 | [diff] [blame] | 709 | |
Keith Burns (alagalah) | 9b79377 | 2018-02-16 08:20:56 -0800 | [diff] [blame] | 710 | if (state & STATE_CLOSE_ON_EMPTY) |
Dave Wallace | 4878cbe | 2017-11-21 03:45:09 -0500 | [diff] [blame] | 711 | { |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 712 | vpp_evt_q = vcl_session_vpp_evt_q (session); |
| 713 | vcl_send_session_disconnected_reply (vpp_evt_q, vcm->my_client_index, |
| 714 | vpp_handle, 0); |
Florin Coras | 0d427d8 | 2018-06-27 03:24:07 -0700 | [diff] [blame] | 715 | VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: sending disconnect " |
| 716 | "REPLY...", getpid (), vpp_handle, session_index); |
Dave Wallace | 66cf6eb | 2017-10-26 02:51:07 -0400 | [diff] [blame] | 717 | } |
| 718 | else |
Dave Wallace | 227867f | 2017-11-13 21:21:53 -0500 | [diff] [blame] | 719 | { |
Florin Coras | 0d427d8 | 2018-06-27 03:24:07 -0700 | [diff] [blame] | 720 | VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: sending disconnect...", |
| 721 | getpid (), vpp_handle, session_index); |
Keith Burns (alagalah) | 9b79377 | 2018-02-16 08:20:56 -0800 | [diff] [blame] | 722 | vppcom_send_disconnect_session (vpp_handle, session_index); |
Dave Wallace | 227867f | 2017-11-13 21:21:53 -0500 | [diff] [blame] | 723 | } |
Dave Wallace | 66cf6eb | 2017-10-26 02:51:07 -0400 | [diff] [blame] | 724 | |
Florin Coras | 070453d | 2018-08-24 17:04:27 -0700 | [diff] [blame] | 725 | return VPPCOM_OK; |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 726 | } |
| 727 | |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 728 | /* |
| 729 | * VPPCOM Public API functions |
| 730 | */ |
| 731 | int |
| 732 | vppcom_app_create (char *app_name) |
| 733 | { |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 734 | vppcom_cfg_t *vcl_cfg = &vcm->cfg; |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 735 | int rv; |
| 736 | |
| 737 | if (!vcm->init) |
| 738 | { |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 739 | vcm->init = 1; |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 740 | vppcom_cfg (&vcm->cfg); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 741 | vcl_cfg = &vcm->cfg; |
| 742 | |
| 743 | vcm->mqs_epfd = -1; |
| 744 | if (vcl_cfg->use_mq_eventfd) |
| 745 | vcm->mqs_epfd = epoll_create (1); |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 746 | |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 747 | vcm->main_cpu = os_get_thread_index (); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 748 | vcm->session_index_by_vpp_handles = hash_create (0, sizeof (uword)); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 749 | vcm->ct_registration_by_mq = hash_create (0, sizeof (uword)); |
| 750 | clib_spinlock_init (&vcm->ct_registration_lock); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 751 | |
| 752 | clib_time_init (&vcm->clib_time); |
| 753 | vppcom_init_error_string_table (); |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 754 | svm_fifo_segment_main_init (vcl_cfg->segment_baseva, |
| 755 | 20 /* timeout in secs */ ); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 756 | vec_validate (vcm->mq_events, 64); |
| 757 | vec_validate (vcm->mq_msg_vector, 128); |
| 758 | vec_reset_length (vcm->mq_msg_vector); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 759 | } |
| 760 | |
| 761 | if (vcm->my_client_index == ~0) |
| 762 | { |
Keith Burns (alagalah) | 3cf2d64 | 2018-02-23 10:17:01 -0800 | [diff] [blame] | 763 | /* API hookup and connect to VPP */ |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 764 | vppcom_api_hookup (); |
Florin Coras | 0d427d8 | 2018-06-27 03:24:07 -0700 | [diff] [blame] | 765 | vcl_elog_init (vcm); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 766 | vcm->app_state = STATE_APP_START; |
| 767 | rv = vppcom_connect_to_vpp (app_name); |
| 768 | if (rv) |
| 769 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 770 | clib_warning ("VCL<%d>: ERROR: couldn't connect to VPP!", |
Dave Wallace | 2e005bb | 2017-11-07 01:21:39 -0500 | [diff] [blame] | 771 | getpid ()); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 772 | return rv; |
| 773 | } |
| 774 | |
Florin Coras | 0d427d8 | 2018-06-27 03:24:07 -0700 | [diff] [blame] | 775 | VDBG (0, "VCL<%d>: sending session enable", getpid ()); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 776 | rv = vppcom_app_session_enable (); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 777 | if (rv) |
| 778 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 779 | clib_warning ("VCL<%d>: ERROR: vppcom_app_session_enable() " |
| 780 | "failed!", getpid ()); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 781 | return rv; |
| 782 | } |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 783 | |
Florin Coras | 0d427d8 | 2018-06-27 03:24:07 -0700 | [diff] [blame] | 784 | VDBG (0, "VCL<%d>: sending app attach", getpid ()); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 785 | rv = vppcom_app_attach (); |
| 786 | if (rv) |
| 787 | { |
| 788 | clib_warning ("VCL<%d>: ERROR: vppcom_app_attach() failed!", |
| 789 | getpid ()); |
| 790 | return rv; |
| 791 | } |
| 792 | |
Florin Coras | 0d427d8 | 2018-06-27 03:24:07 -0700 | [diff] [blame] | 793 | VDBG (0, "VCL<%d>: app_name '%s', my_client_index %d (0x%x)", |
| 794 | getpid (), app_name, vcm->my_client_index, vcm->my_client_index); |
Dave Wallace | 66cf6eb | 2017-10-26 02:51:07 -0400 | [diff] [blame] | 795 | } |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 796 | |
| 797 | return VPPCOM_OK; |
| 798 | } |
| 799 | |
| 800 | void |
| 801 | vppcom_app_destroy (void) |
| 802 | { |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 803 | int rv; |
Dave Wallace | de91006 | 2018-03-20 09:22:13 -0400 | [diff] [blame] | 804 | f64 orig_app_timeout; |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 805 | |
| 806 | if (vcm->my_client_index == ~0) |
| 807 | return; |
| 808 | |
Florin Coras | 0d427d8 | 2018-06-27 03:24:07 -0700 | [diff] [blame] | 809 | VDBG (0, "VCL<%d>: detaching from VPP, my_client_index %d (0x%x)", |
| 810 | getpid (), vcm->my_client_index, vcm->my_client_index); |
| 811 | vcl_evt (VCL_EVT_DETACH, vcm); |
Keith Burns (alagalah) | 8aa9aaf | 2018-01-05 12:16:22 -0800 | [diff] [blame] | 812 | |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 813 | vppcom_app_send_detach (); |
Dave Wallace | de91006 | 2018-03-20 09:22:13 -0400 | [diff] [blame] | 814 | orig_app_timeout = vcm->cfg.app_timeout; |
| 815 | vcm->cfg.app_timeout = 2.0; |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 816 | rv = vppcom_wait_for_app_state_change (STATE_APP_ENABLED); |
Dave Wallace | de91006 | 2018-03-20 09:22:13 -0400 | [diff] [blame] | 817 | vcm->cfg.app_timeout = orig_app_timeout; |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 818 | if (PREDICT_FALSE (rv)) |
Florin Coras | 0d427d8 | 2018-06-27 03:24:07 -0700 | [diff] [blame] | 819 | VDBG (0, "VCL<%d>: application detach timed out! returning %d (%s)", |
| 820 | getpid (), rv, vppcom_retval_str (rv)); |
Keith Burns (alagalah) | 8aa9aaf | 2018-01-05 12:16:22 -0800 | [diff] [blame] | 821 | |
Florin Coras | 0d427d8 | 2018-06-27 03:24:07 -0700 | [diff] [blame] | 822 | vcl_elog_stop (vcm); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 823 | vl_client_disconnect_from_vlib (); |
| 824 | vcm->my_client_index = ~0; |
| 825 | vcm->app_state = STATE_APP_START; |
| 826 | } |
| 827 | |
| 828 | int |
Dave Wallace | c04cbf1 | 2018-02-07 18:14:02 -0500 | [diff] [blame] | 829 | vppcom_session_create (u8 proto, u8 is_nonblocking) |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 830 | { |
Florin Coras | 7e12d94 | 2018-06-27 14:32:43 -0700 | [diff] [blame] | 831 | vcl_session_t *session; |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 832 | u32 session_index; |
| 833 | |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 834 | pool_get (vcm->sessions, session); |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 835 | memset (session, 0, sizeof (*session)); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 836 | session_index = session - vcm->sessions; |
| 837 | |
Florin Coras | 7e12d94 | 2018-06-27 14:32:43 -0700 | [diff] [blame] | 838 | session->session_type = proto; |
| 839 | session->session_state = STATE_START; |
Dave Wallace | 4878cbe | 2017-11-21 03:45:09 -0500 | [diff] [blame] | 840 | session->vpp_handle = ~0; |
Florin Coras | 460dce6 | 2018-07-27 05:45:06 -0700 | [diff] [blame] | 841 | session->is_dgram = proto == VPPCOM_PROTO_UDP; |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 842 | |
Keith Burns (alagalah) | 9b79377 | 2018-02-16 08:20:56 -0800 | [diff] [blame] | 843 | if (is_nonblocking) |
| 844 | VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 845 | |
Florin Coras | 7e12d94 | 2018-06-27 14:32:43 -0700 | [diff] [blame] | 846 | vcl_evt (VCL_EVT_CREATE, session, session_type, session->session_state, |
| 847 | is_nonblocking, session_index); |
Keith Burns (alagalah) | 09b2784 | 2018-01-05 14:39:59 -0800 | [diff] [blame] | 848 | |
Florin Coras | 0d427d8 | 2018-06-27 03:24:07 -0700 | [diff] [blame] | 849 | VDBG (0, "VCL<%d>: sid %u", getpid (), session_index); |
Keith Burns (alagalah) | 09b2784 | 2018-01-05 14:39:59 -0800 | [diff] [blame] | 850 | |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 851 | return (int) session_index; |
| 852 | } |
| 853 | |
| 854 | int |
| 855 | vppcom_session_close (uint32_t session_index) |
| 856 | { |
Florin Coras | 7e12d94 | 2018-06-27 14:32:43 -0700 | [diff] [blame] | 857 | vcl_session_t *session = 0; |
Florin Coras | 070453d | 2018-08-24 17:04:27 -0700 | [diff] [blame] | 858 | int rv = VPPCOM_OK; |
Dave Wallace | 66cf6eb | 2017-10-26 02:51:07 -0400 | [diff] [blame] | 859 | u8 is_vep; |
| 860 | u8 is_vep_session; |
| 861 | u32 next_sid; |
| 862 | u32 vep_idx; |
Dave Wallace | ee45d41 | 2017-11-24 21:44:06 -0500 | [diff] [blame] | 863 | u64 vpp_handle; |
Dave Wallace | 4878cbe | 2017-11-21 03:45:09 -0500 | [diff] [blame] | 864 | uword *p; |
Dave Wallace | 66cf6eb | 2017-10-26 02:51:07 -0400 | [diff] [blame] | 865 | session_state_t state; |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 866 | |
Florin Coras | 070453d | 2018-08-24 17:04:27 -0700 | [diff] [blame] | 867 | session = vcl_session_get (session_index); |
| 868 | if (!session) |
| 869 | return VPPCOM_EBADFD; |
| 870 | |
Dave Wallace | 66cf6eb | 2017-10-26 02:51:07 -0400 | [diff] [blame] | 871 | is_vep = session->is_vep; |
| 872 | is_vep_session = session->is_vep_session; |
| 873 | next_sid = session->vep.next_sid; |
| 874 | vep_idx = session->vep.vep_idx; |
Florin Coras | 7e12d94 | 2018-06-27 14:32:43 -0700 | [diff] [blame] | 875 | state = session->session_state; |
Dave Wallace | ee45d41 | 2017-11-24 21:44:06 -0500 | [diff] [blame] | 876 | vpp_handle = session->vpp_handle; |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 877 | |
| 878 | if (VPPCOM_DEBUG > 0) |
Dave Wallace | ee45d41 | 2017-11-24 21:44:06 -0500 | [diff] [blame] | 879 | { |
| 880 | if (is_vep) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 881 | clib_warning ("VCL<%d>: vep_idx %u / sid %u: " |
| 882 | "closing epoll session...", |
Dave Wallace | ee45d41 | 2017-11-24 21:44:06 -0500 | [diff] [blame] | 883 | getpid (), session_index, session_index); |
| 884 | else |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 885 | clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %d: " |
| 886 | "closing session...", |
Dave Wallace | ee45d41 | 2017-11-24 21:44:06 -0500 | [diff] [blame] | 887 | getpid (), vpp_handle, session_index); |
| 888 | } |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 889 | |
Dave Wallace | 66cf6eb | 2017-10-26 02:51:07 -0400 | [diff] [blame] | 890 | if (is_vep) |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 891 | { |
Dave Wallace | 66cf6eb | 2017-10-26 02:51:07 -0400 | [diff] [blame] | 892 | while (next_sid != ~0) |
Dave Wallace | 1948161 | 2017-09-15 18:47:44 -0400 | [diff] [blame] | 893 | { |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 894 | rv = vppcom_epoll_ctl (session_index, EPOLL_CTL_DEL, next_sid, 0); |
Florin Coras | 0d427d8 | 2018-06-27 03:24:07 -0700 | [diff] [blame] | 895 | if (PREDICT_FALSE (rv < 0)) |
| 896 | VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: EPOLL_CTL_DEL " |
| 897 | "vep_idx %u failed! rv %d (%s)", |
| 898 | getpid (), vpp_handle, next_sid, vep_idx, |
| 899 | rv, vppcom_retval_str (rv)); |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 900 | |
Dave Wallace | 66cf6eb | 2017-10-26 02:51:07 -0400 | [diff] [blame] | 901 | next_sid = session->vep.next_sid; |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 902 | } |
| 903 | } |
| 904 | else |
| 905 | { |
Dave Wallace | 66cf6eb | 2017-10-26 02:51:07 -0400 | [diff] [blame] | 906 | if (is_vep_session) |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 907 | { |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 908 | rv = vppcom_epoll_ctl (vep_idx, EPOLL_CTL_DEL, session_index, 0); |
Florin Coras | 0d427d8 | 2018-06-27 03:24:07 -0700 | [diff] [blame] | 909 | if (rv < 0) |
| 910 | VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: EPOLL_CTL_DEL " |
| 911 | "vep_idx %u failed! rv %d (%s)", |
| 912 | getpid (), vpp_handle, session_index, |
| 913 | vep_idx, rv, vppcom_retval_str (rv)); |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 914 | } |
| 915 | |
Keith Burns (alagalah) | 9b79377 | 2018-02-16 08:20:56 -0800 | [diff] [blame] | 916 | if (state & STATE_LISTEN) |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 917 | { |
Keith Burns (alagalah) | 9b79377 | 2018-02-16 08:20:56 -0800 | [diff] [blame] | 918 | rv = vppcom_session_unbind (session_index); |
| 919 | if (PREDICT_FALSE (rv < 0)) |
Florin Coras | 0d427d8 | 2018-06-27 03:24:07 -0700 | [diff] [blame] | 920 | VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: listener unbind " |
| 921 | "failed! rv %d (%s)", |
| 922 | getpid (), vpp_handle, session_index, |
| 923 | rv, vppcom_retval_str (rv)); |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 924 | } |
Florin Coras | 070453d | 2018-08-24 17:04:27 -0700 | [diff] [blame] | 925 | else if (state & STATE_OPEN) |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 926 | { |
Dave Wallace | 4878cbe | 2017-11-21 03:45:09 -0500 | [diff] [blame] | 927 | rv = vppcom_session_disconnect (session_index); |
| 928 | if (PREDICT_FALSE (rv < 0)) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 929 | clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: " |
Dave Wallace | ee45d41 | 2017-11-24 21:44:06 -0500 | [diff] [blame] | 930 | "session disconnect failed! rv %d (%s)", |
| 931 | getpid (), vpp_handle, session_index, |
| 932 | rv, vppcom_retval_str (rv)); |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 933 | } |
Dave Wallace | 1948161 | 2017-09-15 18:47:44 -0400 | [diff] [blame] | 934 | } |
Dave Wallace | 4878cbe | 2017-11-21 03:45:09 -0500 | [diff] [blame] | 935 | |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 936 | if (vcl_session_is_ct (session)) |
| 937 | { |
| 938 | vcl_cut_through_registration_t *ctr; |
| 939 | uword mq_addr; |
| 940 | |
| 941 | mq_addr = pointer_to_uword (session->our_evt_q); |
| 942 | ctr = vcl_ct_registration_lock_and_lookup (mq_addr); |
| 943 | ASSERT (ctr); |
| 944 | if (ctr->epoll_evt_conn_index != ~0) |
| 945 | vcl_mq_epoll_del_evfd (ctr->epoll_evt_conn_index); |
| 946 | VDBG (0, "Removing ct registration %u", |
| 947 | vcl_ct_registration_index (ctr)); |
| 948 | vcl_ct_registration_del (ctr); |
| 949 | vcl_ct_registration_unlock (); |
| 950 | } |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 951 | |
Dave Wallace | ee45d41 | 2017-11-24 21:44:06 -0500 | [diff] [blame] | 952 | if (vpp_handle != ~0) |
Dave Wallace | 4878cbe | 2017-11-21 03:45:09 -0500 | [diff] [blame] | 953 | { |
Dave Wallace | ee45d41 | 2017-11-24 21:44:06 -0500 | [diff] [blame] | 954 | p = hash_get (vcm->session_index_by_vpp_handles, vpp_handle); |
Dave Wallace | 4878cbe | 2017-11-21 03:45:09 -0500 | [diff] [blame] | 955 | if (p) |
Dave Wallace | ee45d41 | 2017-11-24 21:44:06 -0500 | [diff] [blame] | 956 | hash_unset (vcm->session_index_by_vpp_handles, vpp_handle); |
Dave Wallace | 4878cbe | 2017-11-21 03:45:09 -0500 | [diff] [blame] | 957 | } |
Florin Coras | 070453d | 2018-08-24 17:04:27 -0700 | [diff] [blame] | 958 | vcl_session_free (session); |
Dave Wallace | 4878cbe | 2017-11-21 03:45:09 -0500 | [diff] [blame] | 959 | |
| 960 | if (VPPCOM_DEBUG > 0) |
Dave Wallace | ee45d41 | 2017-11-24 21:44:06 -0500 | [diff] [blame] | 961 | { |
| 962 | if (is_vep) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 963 | clib_warning ("VCL<%d>: vep_idx %u / sid %u: epoll session removed.", |
Dave Wallace | ee45d41 | 2017-11-24 21:44:06 -0500 | [diff] [blame] | 964 | getpid (), session_index, session_index); |
| 965 | else |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 966 | clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: session removed.", |
Dave Wallace | ee45d41 | 2017-11-24 21:44:06 -0500 | [diff] [blame] | 967 | getpid (), vpp_handle, session_index); |
| 968 | } |
Keith Burns (alagalah) | 09b2784 | 2018-01-05 14:39:59 -0800 | [diff] [blame] | 969 | |
Florin Coras | 0d427d8 | 2018-06-27 03:24:07 -0700 | [diff] [blame] | 970 | vcl_evt (VCL_EVT_CLOSE, session, rv); |
Keith Burns (alagalah) | 09b2784 | 2018-01-05 14:39:59 -0800 | [diff] [blame] | 971 | |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 972 | return rv; |
| 973 | } |
| 974 | |
| 975 | int |
| 976 | vppcom_session_bind (uint32_t session_index, vppcom_endpt_t * ep) |
| 977 | { |
Florin Coras | 7e12d94 | 2018-06-27 14:32:43 -0700 | [diff] [blame] | 978 | vcl_session_t *session = 0; |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 979 | |
| 980 | if (!ep || !ep->ip) |
| 981 | return VPPCOM_EINVAL; |
| 982 | |
Florin Coras | 070453d | 2018-08-24 17:04:27 -0700 | [diff] [blame] | 983 | session = vcl_session_get (session_index); |
| 984 | if (!session) |
| 985 | return VPPCOM_EBADFD; |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 986 | |
Dave Wallace | 9c4b5b2 | 2017-10-18 21:15:48 -0400 | [diff] [blame] | 987 | if (session->is_vep) |
| 988 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 989 | clib_warning ("VCL<%d>: ERROR: sid %u: cannot " |
| 990 | "bind to an epoll session!", getpid (), session_index); |
Florin Coras | 070453d | 2018-08-24 17:04:27 -0700 | [diff] [blame] | 991 | return VPPCOM_EBADFD; |
Dave Wallace | 9c4b5b2 | 2017-10-18 21:15:48 -0400 | [diff] [blame] | 992 | } |
| 993 | |
Florin Coras | 7e12d94 | 2018-06-27 14:32:43 -0700 | [diff] [blame] | 994 | session->transport.is_ip4 = ep->is_ip4; |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 995 | if (ep->is_ip4) |
| 996 | clib_memcpy (&session->transport.lcl_ip.ip4, ep->ip, |
| 997 | sizeof (ip4_address_t)); |
| 998 | else |
| 999 | clib_memcpy (&session->transport.lcl_ip.ip6, ep->ip, |
| 1000 | sizeof (ip6_address_t)); |
Florin Coras | 7e12d94 | 2018-06-27 14:32:43 -0700 | [diff] [blame] | 1001 | session->transport.lcl_port = ep->port; |
Steven | ac1f96d | 2017-10-24 16:03:58 -0700 | [diff] [blame] | 1002 | |
Florin Coras | 0d427d8 | 2018-06-27 03:24:07 -0700 | [diff] [blame] | 1003 | VDBG (0, "VCL<%d>: sid %u: binding to local %s address %U port %u, " |
| 1004 | "proto %s", getpid (), session_index, |
Florin Coras | 7e12d94 | 2018-06-27 14:32:43 -0700 | [diff] [blame] | 1005 | session->transport.is_ip4 ? "IPv4" : "IPv6", |
| 1006 | format_ip46_address, &session->transport.lcl_ip, |
| 1007 | session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6, |
| 1008 | clib_net_to_host_u16 (session->transport.lcl_port), |
| 1009 | session->session_type ? "UDP" : "TCP"); |
Florin Coras | 0d427d8 | 2018-06-27 03:24:07 -0700 | [diff] [blame] | 1010 | vcl_evt (VCL_EVT_BIND, session); |
Florin Coras | 460dce6 | 2018-07-27 05:45:06 -0700 | [diff] [blame] | 1011 | |
| 1012 | if (session->session_type == VPPCOM_PROTO_UDP) |
| 1013 | vppcom_session_listen (session_index, 10); |
| 1014 | |
Florin Coras | 070453d | 2018-08-24 17:04:27 -0700 | [diff] [blame] | 1015 | return VPPCOM_OK; |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1016 | } |
| 1017 | |
| 1018 | int |
Dave Wallace | 33e002b | 2017-09-06 01:20:02 -0400 | [diff] [blame] | 1019 | vppcom_session_listen (uint32_t listen_session_index, uint32_t q_len) |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1020 | { |
Florin Coras | 7e12d94 | 2018-06-27 14:32:43 -0700 | [diff] [blame] | 1021 | vcl_session_t *listen_session = 0; |
Dave Wallace | ee45d41 | 2017-11-24 21:44:06 -0500 | [diff] [blame] | 1022 | u64 listen_vpp_handle; |
Florin Coras | 070453d | 2018-08-24 17:04:27 -0700 | [diff] [blame] | 1023 | int rv; |
| 1024 | |
| 1025 | listen_session = vcl_session_get (listen_session_index); |
| 1026 | if (!listen_session) |
| 1027 | return VPPCOM_EBADFD; |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1028 | |
Keith Burns (alagalah) | aba98de | 2018-02-22 03:23:40 -0800 | [diff] [blame] | 1029 | if (q_len == 0 || q_len == ~0) |
| 1030 | q_len = vcm->cfg.listen_queue_size; |
| 1031 | |
Dave Wallace | 9c4b5b2 | 2017-10-18 21:15:48 -0400 | [diff] [blame] | 1032 | if (listen_session->is_vep) |
| 1033 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1034 | clib_warning ("VCL<%d>: ERROR: sid %u: cannot listen on an " |
Dave Wallace | 4878cbe | 2017-11-21 03:45:09 -0500 | [diff] [blame] | 1035 | "epoll session!", getpid (), listen_session_index); |
Florin Coras | 070453d | 2018-08-24 17:04:27 -0700 | [diff] [blame] | 1036 | return VPPCOM_EBADFD; |
Dave Wallace | 9c4b5b2 | 2017-10-18 21:15:48 -0400 | [diff] [blame] | 1037 | } |
| 1038 | |
Dave Wallace | ee45d41 | 2017-11-24 21:44:06 -0500 | [diff] [blame] | 1039 | listen_vpp_handle = listen_session->vpp_handle; |
Florin Coras | 7e12d94 | 2018-06-27 14:32:43 -0700 | [diff] [blame] | 1040 | if (listen_session->session_state & STATE_LISTEN) |
Dave Wallace | e695cb4 | 2017-11-02 22:04:42 -0400 | [diff] [blame] | 1041 | { |
Florin Coras | 0d427d8 | 2018-06-27 03:24:07 -0700 | [diff] [blame] | 1042 | VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: already in listen state!", |
| 1043 | getpid (), listen_vpp_handle, listen_session_index); |
Florin Coras | 070453d | 2018-08-24 17:04:27 -0700 | [diff] [blame] | 1044 | return VPPCOM_OK; |
Dave Wallace | e695cb4 | 2017-11-02 22:04:42 -0400 | [diff] [blame] | 1045 | } |
| 1046 | |
Florin Coras | 0d427d8 | 2018-06-27 03:24:07 -0700 | [diff] [blame] | 1047 | VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: sending VPP bind+listen " |
| 1048 | "request...", getpid (), listen_vpp_handle, listen_session_index); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1049 | |
Florin Coras | 070453d | 2018-08-24 17:04:27 -0700 | [diff] [blame] | 1050 | /* |
| 1051 | * Send listen request to vpp and wait for reply |
| 1052 | */ |
Dave Wallace | 4878cbe | 2017-11-21 03:45:09 -0500 | [diff] [blame] | 1053 | vppcom_send_bind_sock (listen_session, listen_session_index); |
Florin Coras | 070453d | 2018-08-24 17:04:27 -0700 | [diff] [blame] | 1054 | rv = |
| 1055 | vppcom_wait_for_session_state_change (listen_session_index, STATE_LISTEN, |
| 1056 | vcm->cfg.session_timeout); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1057 | |
Florin Coras | 070453d | 2018-08-24 17:04:27 -0700 | [diff] [blame] | 1058 | if (PREDICT_FALSE (rv)) |
Dave Wallace | ee45d41 | 2017-11-24 21:44:06 -0500 | [diff] [blame] | 1059 | { |
Florin Coras | 070453d | 2018-08-24 17:04:27 -0700 | [diff] [blame] | 1060 | listen_session = vcl_session_get (listen_session_index); |
Florin Coras | 0d427d8 | 2018-06-27 03:24:07 -0700 | [diff] [blame] | 1061 | VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: bind+listen failed! " |
| 1062 | "returning %d (%s)", getpid (), listen_session->vpp_handle, |
Florin Coras | 070453d | 2018-08-24 17:04:27 -0700 | [diff] [blame] | 1063 | listen_session_index, rv, vppcom_retval_str (rv)); |
| 1064 | return rv; |
Dave Wallace | ee45d41 | 2017-11-24 21:44:06 -0500 | [diff] [blame] | 1065 | } |
| 1066 | |
Florin Coras | 070453d | 2018-08-24 17:04:27 -0700 | [diff] [blame] | 1067 | return VPPCOM_OK; |
Keith Burns (alagalah) | 0d2b0d5 | 2018-03-06 15:55:22 -0800 | [diff] [blame] | 1068 | } |
| 1069 | |
| 1070 | int |
Florin Coras | 7e12d94 | 2018-06-27 14:32:43 -0700 | [diff] [blame] | 1071 | validate_args_session_accept_ (vcl_session_t * listen_session) |
Keith Burns (alagalah) | 3cf2d64 | 2018-02-23 10:17:01 -0800 | [diff] [blame] | 1072 | { |
| 1073 | u32 listen_session_index = listen_session - vcm->sessions; |
| 1074 | |
| 1075 | /* Input validation - expects spinlock on sessions_lockp */ |
| 1076 | if (listen_session->is_vep) |
| 1077 | { |
| 1078 | clib_warning ("VCL<%d>: ERROR: sid %u: cannot accept on an " |
| 1079 | "epoll session!", getpid (), listen_session_index); |
| 1080 | return VPPCOM_EBADFD; |
| 1081 | } |
| 1082 | |
Florin Coras | 7e12d94 | 2018-06-27 14:32:43 -0700 | [diff] [blame] | 1083 | if (listen_session->session_state != STATE_LISTEN) |
Keith Burns (alagalah) | 3cf2d64 | 2018-02-23 10:17:01 -0800 | [diff] [blame] | 1084 | { |
| 1085 | clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: " |
| 1086 | "not in listen state! state 0x%x (%s)", getpid (), |
| 1087 | listen_session->vpp_handle, listen_session_index, |
Florin Coras | 7e12d94 | 2018-06-27 14:32:43 -0700 | [diff] [blame] | 1088 | listen_session->session_state, |
| 1089 | vppcom_session_state_str (listen_session->session_state)); |
Keith Burns (alagalah) | 3cf2d64 | 2018-02-23 10:17:01 -0800 | [diff] [blame] | 1090 | return VPPCOM_EBADFD; |
| 1091 | } |
| 1092 | return VPPCOM_OK; |
| 1093 | } |
| 1094 | |
| 1095 | int |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1096 | vppcom_session_accept (uint32_t listen_session_index, vppcom_endpt_t * ep, |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1097 | uint32_t flags) |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1098 | { |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1099 | session_accepted_msg_t accepted_msg; |
Florin Coras | 7e12d94 | 2018-06-27 14:32:43 -0700 | [diff] [blame] | 1100 | vcl_session_t *listen_session = 0; |
| 1101 | vcl_session_t *client_session = 0; |
Keith Burns (alagalah) | 9b79377 | 2018-02-16 08:20:56 -0800 | [diff] [blame] | 1102 | u32 client_session_index = ~0; |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1103 | svm_msg_q_t *vpp_evt_q; |
| 1104 | vcl_session_msg_t *evt; |
Dave Wallace | ee45d41 | 2017-11-24 21:44:06 -0500 | [diff] [blame] | 1105 | u64 listen_vpp_handle; |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1106 | svm_msg_q_msg_t msg; |
| 1107 | session_event_t *e; |
| 1108 | u8 is_nonblocking; |
| 1109 | int rv; |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1110 | |
Florin Coras | 070453d | 2018-08-24 17:04:27 -0700 | [diff] [blame] | 1111 | listen_session = vcl_session_get (listen_session_index); |
| 1112 | if (!listen_session) |
| 1113 | return VPPCOM_EBADFD; |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1114 | |
Florin Coras | 070453d | 2018-08-24 17:04:27 -0700 | [diff] [blame] | 1115 | if ((rv = validate_args_session_accept_ (listen_session))) |
| 1116 | return rv; |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1117 | |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1118 | if (clib_fifo_elts (listen_session->accept_evts_fifo)) |
Keith Burns (alagalah) | 3cf2d64 | 2018-02-23 10:17:01 -0800 | [diff] [blame] | 1119 | { |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1120 | clib_fifo_sub2 (listen_session->accept_evts_fifo, evt); |
| 1121 | accepted_msg = evt->accepted_msg; |
| 1122 | goto handle; |
| 1123 | } |
| 1124 | |
| 1125 | is_nonblocking = VCL_SESS_ATTR_TEST (listen_session->attr, |
| 1126 | VCL_SESS_ATTR_NONBLOCK); |
| 1127 | if (svm_msg_q_is_empty (vcm->app_event_queue) && is_nonblocking) |
| 1128 | return VPPCOM_EAGAIN; |
| 1129 | |
| 1130 | while (1) |
| 1131 | { |
| 1132 | if (svm_msg_q_sub (vcm->app_event_queue, &msg, SVM_Q_WAIT, 0)) |
| 1133 | return VPPCOM_EAGAIN; |
| 1134 | |
| 1135 | e = svm_msg_q_msg_data (vcm->app_event_queue, &msg); |
| 1136 | if (e->event_type != SESSION_CTRL_EVT_ACCEPTED) |
Keith Burns (alagalah) | 3cf2d64 | 2018-02-23 10:17:01 -0800 | [diff] [blame] | 1137 | { |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1138 | clib_warning ("discarded event: %u", e->event_type); |
| 1139 | svm_msg_q_free_msg (vcm->app_event_queue, &msg); |
| 1140 | continue; |
Keith Burns (alagalah) | 3cf2d64 | 2018-02-23 10:17:01 -0800 | [diff] [blame] | 1141 | } |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1142 | clib_memcpy (&accepted_msg, e->data, sizeof (accepted_msg)); |
| 1143 | svm_msg_q_free_msg (vcm->app_event_queue, &msg); |
| 1144 | break; |
Keith Burns (alagalah) | 3cf2d64 | 2018-02-23 10:17:01 -0800 | [diff] [blame] | 1145 | } |
Keith Burns (alagalah) | 3cf2d64 | 2018-02-23 10:17:01 -0800 | [diff] [blame] | 1146 | |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1147 | handle: |
Keith Burns (alagalah) | 00f44cc | 2018-03-07 09:26:38 -0800 | [diff] [blame] | 1148 | |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1149 | client_session_index = vcl_session_accepted_handler (&accepted_msg); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 1150 | listen_session = vcl_session_get (listen_session_index); |
Florin Coras | 070453d | 2018-08-24 17:04:27 -0700 | [diff] [blame] | 1151 | client_session = vcl_session_get (client_session_index); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1152 | |
Keith Burns (alagalah) | 9b79377 | 2018-02-16 08:20:56 -0800 | [diff] [blame] | 1153 | if (flags & O_NONBLOCK) |
| 1154 | VCL_SESS_ATTR_SET (client_session->attr, VCL_SESS_ATTR_NONBLOCK); |
Keith Burns (alagalah) | 9b79377 | 2018-02-16 08:20:56 -0800 | [diff] [blame] | 1155 | |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1156 | listen_vpp_handle = listen_session->vpp_handle; |
Florin Coras | 0d427d8 | 2018-06-27 03:24:07 -0700 | [diff] [blame] | 1157 | VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: Got a client request! " |
| 1158 | "vpp handle 0x%llx, sid %u, flags %d, is_nonblocking %u", |
| 1159 | getpid (), listen_vpp_handle, listen_session_index, |
| 1160 | client_session->vpp_handle, client_session_index, |
| 1161 | flags, VCL_SESS_ATTR_TEST (client_session->attr, |
| 1162 | VCL_SESS_ATTR_NONBLOCK)); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1163 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1164 | if (ep) |
| 1165 | { |
Florin Coras | 7e12d94 | 2018-06-27 14:32:43 -0700 | [diff] [blame] | 1166 | ep->is_ip4 = client_session->transport.is_ip4; |
| 1167 | ep->port = client_session->transport.rmt_port; |
| 1168 | if (client_session->transport.is_ip4) |
| 1169 | clib_memcpy (ep->ip, &client_session->transport.rmt_ip.ip4, |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1170 | sizeof (ip4_address_t)); |
| 1171 | else |
Florin Coras | 7e12d94 | 2018-06-27 14:32:43 -0700 | [diff] [blame] | 1172 | clib_memcpy (ep->ip, &client_session->transport.rmt_ip.ip6, |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1173 | sizeof (ip6_address_t)); |
| 1174 | } |
Dave Wallace | 60caa06 | 2017-11-10 17:07:13 -0500 | [diff] [blame] | 1175 | |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1176 | if (accepted_msg.server_event_queue_address) |
| 1177 | vpp_evt_q = uword_to_pointer (accepted_msg.vpp_event_queue_address, |
| 1178 | svm_msg_q_t *); |
| 1179 | else |
| 1180 | vpp_evt_q = client_session->vpp_evt_q; |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 1181 | |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1182 | vcl_send_session_accepted_reply (vpp_evt_q, client_session->client_context, |
| 1183 | client_session->vpp_handle, 0); |
Dave Wallace | 60caa06 | 2017-11-10 17:07:13 -0500 | [diff] [blame] | 1184 | |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1185 | VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: accepted vpp handle 0x%llx, " |
| 1186 | "sid %u connection from peer %s address %U port %u to local %s " |
| 1187 | "address %U port %u", getpid (), listen_vpp_handle, |
Florin Coras | 0d427d8 | 2018-06-27 03:24:07 -0700 | [diff] [blame] | 1188 | listen_session_index, client_session->vpp_handle, |
| 1189 | client_session_index, |
Florin Coras | 7e12d94 | 2018-06-27 14:32:43 -0700 | [diff] [blame] | 1190 | client_session->transport.is_ip4 ? "IPv4" : "IPv6", |
| 1191 | format_ip46_address, &client_session->transport.rmt_ip, |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1192 | client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6, |
Florin Coras | 7e12d94 | 2018-06-27 14:32:43 -0700 | [diff] [blame] | 1193 | clib_net_to_host_u16 (client_session->transport.rmt_port), |
| 1194 | client_session->transport.is_ip4 ? "IPv4" : "IPv6", |
| 1195 | format_ip46_address, &client_session->transport.lcl_ip, |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1196 | client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6, |
Florin Coras | 7e12d94 | 2018-06-27 14:32:43 -0700 | [diff] [blame] | 1197 | clib_net_to_host_u16 (client_session->transport.lcl_port)); |
Florin Coras | 0d427d8 | 2018-06-27 03:24:07 -0700 | [diff] [blame] | 1198 | vcl_evt (VCL_EVT_ACCEPT, client_session, listen_session, |
| 1199 | client_session_index); |
Keith Burns (alagalah) | 3cf2d64 | 2018-02-23 10:17:01 -0800 | [diff] [blame] | 1200 | |
Florin Coras | 070453d | 2018-08-24 17:04:27 -0700 | [diff] [blame] | 1201 | return client_session_index; |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1202 | } |
| 1203 | |
| 1204 | int |
| 1205 | vppcom_session_connect (uint32_t session_index, vppcom_endpt_t * server_ep) |
| 1206 | { |
Florin Coras | 7e12d94 | 2018-06-27 14:32:43 -0700 | [diff] [blame] | 1207 | vcl_session_t *session = 0; |
Keith Burns (alagalah) | 9b79377 | 2018-02-16 08:20:56 -0800 | [diff] [blame] | 1208 | u64 vpp_handle = 0; |
Florin Coras | 070453d | 2018-08-24 17:04:27 -0700 | [diff] [blame] | 1209 | int rv; |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1210 | |
Florin Coras | 070453d | 2018-08-24 17:04:27 -0700 | [diff] [blame] | 1211 | session = vcl_session_get (session_index); |
| 1212 | if (!session) |
| 1213 | return VPPCOM_EBADFD; |
Dave Wallace | 4878cbe | 2017-11-21 03:45:09 -0500 | [diff] [blame] | 1214 | |
| 1215 | if (PREDICT_FALSE (session->is_vep)) |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1216 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1217 | clib_warning ("VCL<%d>: ERROR: sid %u: cannot " |
| 1218 | "connect on an epoll session!", getpid (), session_index); |
Florin Coras | 070453d | 2018-08-24 17:04:27 -0700 | [diff] [blame] | 1219 | return VPPCOM_EBADFD; |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1220 | } |
| 1221 | |
Florin Coras | 7e12d94 | 2018-06-27 14:32:43 -0700 | [diff] [blame] | 1222 | if (PREDICT_FALSE (session->session_state & CLIENT_STATE_OPEN)) |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1223 | { |
Florin Coras | 0d427d8 | 2018-06-27 03:24:07 -0700 | [diff] [blame] | 1224 | VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: session already " |
| 1225 | "connected to %s %U port %d proto %s, state 0x%x (%s)", |
| 1226 | getpid (), session->vpp_handle, session_index, |
Florin Coras | 7e12d94 | 2018-06-27 14:32:43 -0700 | [diff] [blame] | 1227 | session->transport.is_ip4 ? "IPv4" : "IPv6", |
Florin Coras | 0d427d8 | 2018-06-27 03:24:07 -0700 | [diff] [blame] | 1228 | format_ip46_address, |
Florin Coras | 7e12d94 | 2018-06-27 14:32:43 -0700 | [diff] [blame] | 1229 | &session->transport.rmt_ip, session->transport.is_ip4 ? |
Florin Coras | 0d427d8 | 2018-06-27 03:24:07 -0700 | [diff] [blame] | 1230 | IP46_TYPE_IP4 : IP46_TYPE_IP6, |
Florin Coras | 7e12d94 | 2018-06-27 14:32:43 -0700 | [diff] [blame] | 1231 | clib_net_to_host_u16 (session->transport.rmt_port), |
| 1232 | session->session_type ? "UDP" : "TCP", session->session_state, |
| 1233 | vppcom_session_state_str (session->session_state)); |
Florin Coras | 070453d | 2018-08-24 17:04:27 -0700 | [diff] [blame] | 1234 | return VPPCOM_OK; |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1235 | } |
| 1236 | |
Florin Coras | 7e12d94 | 2018-06-27 14:32:43 -0700 | [diff] [blame] | 1237 | session->transport.is_ip4 = server_ep->is_ip4; |
| 1238 | if (session->transport.is_ip4) |
| 1239 | clib_memcpy (&session->transport.rmt_ip.ip4, server_ep->ip, |
Dave Wallace | d239f8d | 2018-06-19 13:37:30 -0400 | [diff] [blame] | 1240 | sizeof (ip4_address_t)); |
| 1241 | else |
Florin Coras | 7e12d94 | 2018-06-27 14:32:43 -0700 | [diff] [blame] | 1242 | clib_memcpy (&session->transport.rmt_ip.ip6, server_ep->ip, |
Dave Wallace | d239f8d | 2018-06-19 13:37:30 -0400 | [diff] [blame] | 1243 | sizeof (ip6_address_t)); |
Florin Coras | 7e12d94 | 2018-06-27 14:32:43 -0700 | [diff] [blame] | 1244 | session->transport.rmt_port = server_ep->port; |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1245 | |
Florin Coras | 0d427d8 | 2018-06-27 03:24:07 -0700 | [diff] [blame] | 1246 | VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: connecting to server %s %U " |
| 1247 | "port %d proto %s", |
| 1248 | getpid (), session->vpp_handle, session_index, |
Florin Coras | 7e12d94 | 2018-06-27 14:32:43 -0700 | [diff] [blame] | 1249 | session->transport.is_ip4 ? "IPv4" : "IPv6", |
Florin Coras | 0d427d8 | 2018-06-27 03:24:07 -0700 | [diff] [blame] | 1250 | format_ip46_address, |
Florin Coras | 7e12d94 | 2018-06-27 14:32:43 -0700 | [diff] [blame] | 1251 | &session->transport.rmt_ip, session->transport.is_ip4 ? |
Florin Coras | 0d427d8 | 2018-06-27 03:24:07 -0700 | [diff] [blame] | 1252 | IP46_TYPE_IP4 : IP46_TYPE_IP6, |
Florin Coras | 7e12d94 | 2018-06-27 14:32:43 -0700 | [diff] [blame] | 1253 | clib_net_to_host_u16 (session->transport.rmt_port), |
| 1254 | session->session_type ? "UDP" : "TCP"); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1255 | |
Florin Coras | 070453d | 2018-08-24 17:04:27 -0700 | [diff] [blame] | 1256 | /* |
| 1257 | * Send connect request and wait for reply from vpp |
| 1258 | */ |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1259 | vppcom_send_connect_sock (session, session_index); |
Florin Coras | 070453d | 2018-08-24 17:04:27 -0700 | [diff] [blame] | 1260 | rv = vppcom_wait_for_session_state_change (session_index, STATE_CONNECT, |
| 1261 | vcm->cfg.session_timeout); |
Dave Wallace | 4878cbe | 2017-11-21 03:45:09 -0500 | [diff] [blame] | 1262 | |
Florin Coras | 070453d | 2018-08-24 17:04:27 -0700 | [diff] [blame] | 1263 | session = vcl_session_get (session_index); |
Dave Wallace | ee45d41 | 2017-11-24 21:44:06 -0500 | [diff] [blame] | 1264 | vpp_handle = session->vpp_handle; |
Dave Wallace | 7876d39 | 2017-10-19 03:53:57 -0400 | [diff] [blame] | 1265 | |
Florin Coras | 070453d | 2018-08-24 17:04:27 -0700 | [diff] [blame] | 1266 | if (PREDICT_FALSE (rv)) |
Dave Wallace | ee45d41 | 2017-11-24 21:44:06 -0500 | [diff] [blame] | 1267 | { |
Dave Wallace | ee45d41 | 2017-11-24 21:44:06 -0500 | [diff] [blame] | 1268 | if (VPPCOM_DEBUG > 0) |
Keith Burns (alagalah) | 9b79377 | 2018-02-16 08:20:56 -0800 | [diff] [blame] | 1269 | { |
| 1270 | if (session) |
Florin Coras | 0d427d8 | 2018-06-27 03:24:07 -0700 | [diff] [blame] | 1271 | clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: connect " |
| 1272 | "failed! returning %d (%s)", getpid (), vpp_handle, |
| 1273 | session_index, rv, vppcom_retval_str (rv)); |
Keith Burns (alagalah) | 9b79377 | 2018-02-16 08:20:56 -0800 | [diff] [blame] | 1274 | else |
| 1275 | clib_warning ("VCL<%d>: no session for sid %u: connect failed! " |
| 1276 | "returning %d (%s)", getpid (), |
| 1277 | session_index, rv, vppcom_retval_str (rv)); |
| 1278 | } |
Dave Wallace | ee45d41 | 2017-11-24 21:44:06 -0500 | [diff] [blame] | 1279 | } |
Florin Coras | 0d427d8 | 2018-06-27 03:24:07 -0700 | [diff] [blame] | 1280 | else |
| 1281 | VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: connected!", |
| 1282 | getpid (), vpp_handle, session_index); |
Dave Wallace | ee45d41 | 2017-11-24 21:44:06 -0500 | [diff] [blame] | 1283 | |
Dave Wallace | 4878cbe | 2017-11-21 03:45:09 -0500 | [diff] [blame] | 1284 | return rv; |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1285 | } |
| 1286 | |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1287 | static u8 |
| 1288 | vcl_is_rx_evt_for_session (session_event_t * e, u32 sid, u8 is_ct) |
| 1289 | { |
| 1290 | if (!is_ct) |
| 1291 | return (e->event_type == FIFO_EVENT_APP_RX |
| 1292 | && e->fifo->client_session_index == sid); |
| 1293 | else |
| 1294 | return (e->event_type == SESSION_IO_EVT_CT_TX); |
| 1295 | } |
| 1296 | |
Florin Coras | 460dce6 | 2018-07-27 05:45:06 -0700 | [diff] [blame] | 1297 | static inline u8 |
| 1298 | vcl_session_is_readable (vcl_session_t * s) |
| 1299 | { |
| 1300 | return ((s->session_state & STATE_OPEN) |
| 1301 | || (s->session_state == STATE_LISTEN |
| 1302 | && s->session_type == VPPCOM_PROTO_UDP)); |
| 1303 | } |
| 1304 | |
Steven | 58f464e | 2017-10-25 12:33:12 -0700 | [diff] [blame] | 1305 | static inline int |
| 1306 | vppcom_session_read_internal (uint32_t session_index, void *buf, int n, |
| 1307 | u8 peek) |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1308 | { |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1309 | int n_read = 0, rv, is_nonblocking; |
Florin Coras | 460dce6 | 2018-07-27 05:45:06 -0700 | [diff] [blame] | 1310 | vcl_session_t *s = 0; |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1311 | svm_fifo_t *rx_fifo; |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1312 | svm_msg_q_msg_t msg; |
| 1313 | session_event_t *e; |
| 1314 | svm_msg_q_t *mq; |
| 1315 | u8 is_full; |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1316 | |
Florin Coras | 070453d | 2018-08-24 17:04:27 -0700 | [diff] [blame] | 1317 | if (PREDICT_FALSE (!buf)) |
| 1318 | return VPPCOM_EINVAL; |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1319 | |
Florin Coras | 070453d | 2018-08-24 17:04:27 -0700 | [diff] [blame] | 1320 | s = vcl_session_get (session_index); |
| 1321 | if (PREDICT_FALSE (!s)) |
| 1322 | return VPPCOM_EBADFD; |
Dave Wallace | 4878cbe | 2017-11-21 03:45:09 -0500 | [diff] [blame] | 1323 | |
Florin Coras | 460dce6 | 2018-07-27 05:45:06 -0700 | [diff] [blame] | 1324 | if (PREDICT_FALSE (s->is_vep)) |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1325 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1326 | clib_warning ("VCL<%d>: ERROR: sid %u: cannot " |
| 1327 | "read from an epoll session!", getpid (), session_index); |
Florin Coras | 070453d | 2018-08-24 17:04:27 -0700 | [diff] [blame] | 1328 | return VPPCOM_EBADFD; |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1329 | } |
| 1330 | |
Florin Coras | 460dce6 | 2018-07-27 05:45:06 -0700 | [diff] [blame] | 1331 | is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK); |
| 1332 | rx_fifo = s->rx_fifo; |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1333 | |
Florin Coras | 460dce6 | 2018-07-27 05:45:06 -0700 | [diff] [blame] | 1334 | if (PREDICT_FALSE (!vcl_session_is_readable (s))) |
Dave Wallace | 9c4b5b2 | 2017-10-18 21:15:48 -0400 | [diff] [blame] | 1335 | { |
Florin Coras | 460dce6 | 2018-07-27 05:45:06 -0700 | [diff] [blame] | 1336 | session_state_t state = s->session_state; |
Keith Burns (alagalah) | 56a0d06 | 2018-02-15 07:52:50 -0800 | [diff] [blame] | 1337 | rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN); |
Dave Wallace | 4878cbe | 2017-11-21 03:45:09 -0500 | [diff] [blame] | 1338 | |
Florin Coras | 0d427d8 | 2018-06-27 03:24:07 -0700 | [diff] [blame] | 1339 | VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: %s session is not open! " |
| 1340 | "state 0x%x (%s), returning %d (%s)", |
Florin Coras | 460dce6 | 2018-07-27 05:45:06 -0700 | [diff] [blame] | 1341 | getpid (), s->vpp_handle, session_index, state, |
Florin Coras | 0d427d8 | 2018-06-27 03:24:07 -0700 | [diff] [blame] | 1342 | vppcom_session_state_str (state), rv, vppcom_retval_str (rv)); |
Florin Coras | 070453d | 2018-08-24 17:04:27 -0700 | [diff] [blame] | 1343 | return rv; |
Dave Wallace | 9c4b5b2 | 2017-10-18 21:15:48 -0400 | [diff] [blame] | 1344 | } |
| 1345 | |
Florin Coras | 460dce6 | 2018-07-27 05:45:06 -0700 | [diff] [blame] | 1346 | mq = vcl_session_is_ct (s) ? s->our_evt_q : vcm->app_event_queue; |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 1347 | svm_fifo_unset_event (rx_fifo); |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1348 | is_full = svm_fifo_is_full (rx_fifo); |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 1349 | |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1350 | if (svm_fifo_is_empty (rx_fifo)) |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 1351 | { |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1352 | if (is_nonblocking) |
Dave Wallace | 4878cbe | 2017-11-21 03:45:09 -0500 | [diff] [blame] | 1353 | { |
Florin Coras | 070453d | 2018-08-24 17:04:27 -0700 | [diff] [blame] | 1354 | return VPPCOM_OK; |
Dave Wallace | 4878cbe | 2017-11-21 03:45:09 -0500 | [diff] [blame] | 1355 | } |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1356 | while (1) |
| 1357 | { |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 1358 | svm_msg_q_lock (mq); |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1359 | if (svm_msg_q_is_empty (mq)) |
| 1360 | svm_msg_q_wait (mq); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 1361 | |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1362 | svm_msg_q_sub_w_lock (mq, &msg); |
| 1363 | e = svm_msg_q_msg_data (mq, &msg); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 1364 | svm_msg_q_unlock (mq); |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1365 | if (!vcl_is_rx_evt_for_session (e, session_index, |
Florin Coras | 460dce6 | 2018-07-27 05:45:06 -0700 | [diff] [blame] | 1366 | s->our_evt_q != 0)) |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1367 | { |
| 1368 | vcl_handle_mq_ctrl_event (e); |
| 1369 | svm_msg_q_free_msg (mq, &msg); |
| 1370 | continue; |
| 1371 | } |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 1372 | svm_fifo_unset_event (rx_fifo); |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1373 | svm_msg_q_free_msg (mq, &msg); |
Florin Coras | 60f1fc1 | 2018-08-16 14:57:31 -0700 | [diff] [blame] | 1374 | if (PREDICT_FALSE (s->session_state == STATE_CLOSE_ON_EMPTY)) |
| 1375 | return 0; |
| 1376 | if (svm_fifo_is_empty (rx_fifo)) |
| 1377 | continue; |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1378 | break; |
| 1379 | } |
Dave Wallace | 9c4b5b2 | 2017-10-18 21:15:48 -0400 | [diff] [blame] | 1380 | } |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1381 | |
Florin Coras | 460dce6 | 2018-07-27 05:45:06 -0700 | [diff] [blame] | 1382 | if (s->is_dgram) |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 1383 | n_read = app_recv_dgram_raw (rx_fifo, buf, n, &s->transport, 0, peek); |
Dave Wallace | 4878cbe | 2017-11-21 03:45:09 -0500 | [diff] [blame] | 1384 | else |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 1385 | n_read = app_recv_stream_raw (rx_fifo, buf, n, 0, peek); |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1386 | |
Florin Coras | 460dce6 | 2018-07-27 05:45:06 -0700 | [diff] [blame] | 1387 | if (vcl_session_is_ct (s) && is_full) |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 1388 | { |
| 1389 | /* If the peer is not polling send notification */ |
| 1390 | if (!svm_fifo_has_event (s->rx_fifo)) |
| 1391 | app_send_io_evt_to_vpp (s->vpp_evt_q, s->rx_fifo, |
| 1392 | SESSION_IO_EVT_CT_RX, SVM_Q_WAIT); |
| 1393 | } |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1394 | |
Dave Wallace | 4878cbe | 2017-11-21 03:45:09 -0500 | [diff] [blame] | 1395 | if (VPPCOM_DEBUG > 2) |
| 1396 | { |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1397 | if (n_read > 0) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1398 | clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: read %d bytes " |
Florin Coras | 460dce6 | 2018-07-27 05:45:06 -0700 | [diff] [blame] | 1399 | "from (%p)", getpid (), s->vpp_handle, |
Keith Burns (alagalah) | 9b79377 | 2018-02-16 08:20:56 -0800 | [diff] [blame] | 1400 | session_index, n_read, rx_fifo); |
Dave Wallace | 4878cbe | 2017-11-21 03:45:09 -0500 | [diff] [blame] | 1401 | else |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1402 | clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: nothing read! " |
Florin Coras | 460dce6 | 2018-07-27 05:45:06 -0700 | [diff] [blame] | 1403 | "returning %d (%s)", getpid (), s->vpp_handle, |
Florin Coras | 070453d | 2018-08-24 17:04:27 -0700 | [diff] [blame] | 1404 | session_index, n_read, vppcom_retval_str (n_read)); |
Dave Wallace | 4878cbe | 2017-11-21 03:45:09 -0500 | [diff] [blame] | 1405 | } |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1406 | return n_read; |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1407 | } |
| 1408 | |
Steven | 58f464e | 2017-10-25 12:33:12 -0700 | [diff] [blame] | 1409 | int |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1410 | vppcom_session_read (uint32_t session_index, void *buf, size_t n) |
Steven | 58f464e | 2017-10-25 12:33:12 -0700 | [diff] [blame] | 1411 | { |
| 1412 | return (vppcom_session_read_internal (session_index, buf, n, 0)); |
| 1413 | } |
| 1414 | |
| 1415 | static int |
| 1416 | vppcom_session_peek (uint32_t session_index, void *buf, int n) |
| 1417 | { |
| 1418 | return (vppcom_session_read_internal (session_index, buf, n, 1)); |
| 1419 | } |
| 1420 | |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1421 | static inline int |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1422 | vppcom_session_read_ready (vcl_session_t * session) |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1423 | { |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1424 | /* Assumes caller has acquired spinlock: vcm->sessions_lockp */ |
Dave Wallace | 4878cbe | 2017-11-21 03:45:09 -0500 | [diff] [blame] | 1425 | if (PREDICT_FALSE (session->is_vep)) |
Dave Wallace | 9c4b5b2 | 2017-10-18 21:15:48 -0400 | [diff] [blame] | 1426 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1427 | clib_warning ("VCL<%d>: ERROR: sid %u: cannot read from an " |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1428 | "epoll session!", getpid (), vcl_session_index (session)); |
| 1429 | return VPPCOM_EBADFD; |
| 1430 | } |
| 1431 | |
| 1432 | if (PREDICT_FALSE (!(session->session_state & (STATE_OPEN | STATE_LISTEN)))) |
| 1433 | { |
| 1434 | session_state_t state = session->session_state; |
| 1435 | int rv; |
| 1436 | |
| 1437 | rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN); |
| 1438 | |
| 1439 | VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: session is not open!" |
| 1440 | " state 0x%x (%s), returning %d (%s)", getpid (), |
| 1441 | session->vpp_handle, vcl_session_index (session), state, |
| 1442 | vppcom_session_state_str (state), rv, vppcom_retval_str (rv)); |
| 1443 | return rv; |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1444 | } |
Dave Wallace | 33e002b | 2017-09-06 01:20:02 -0400 | [diff] [blame] | 1445 | |
Florin Coras | 7e12d94 | 2018-06-27 14:32:43 -0700 | [diff] [blame] | 1446 | if (session->session_state & STATE_LISTEN) |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1447 | return clib_fifo_elts (session->accept_evts_fifo); |
| 1448 | |
| 1449 | return svm_fifo_max_dequeue (session->rx_fifo); |
| 1450 | } |
| 1451 | |
| 1452 | static u8 |
| 1453 | vcl_is_tx_evt_for_session (session_event_t * e, u32 sid, u8 is_ct) |
| 1454 | { |
| 1455 | if (!is_ct) |
| 1456 | return (e->event_type == FIFO_EVENT_APP_TX |
| 1457 | && e->fifo->client_session_index == sid); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1458 | else |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1459 | return (e->event_type == SESSION_IO_EVT_CT_RX); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1460 | } |
| 1461 | |
| 1462 | int |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1463 | vppcom_session_write (uint32_t session_index, void *buf, size_t n) |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1464 | { |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1465 | int rv, n_write, is_nonblocking; |
Florin Coras | 460dce6 | 2018-07-27 05:45:06 -0700 | [diff] [blame] | 1466 | vcl_session_t *s = 0; |
Keith Burns (alagalah) | 9b79377 | 2018-02-16 08:20:56 -0800 | [diff] [blame] | 1467 | svm_fifo_t *tx_fifo = 0; |
Florin Coras | 460dce6 | 2018-07-27 05:45:06 -0700 | [diff] [blame] | 1468 | session_evt_type_t et; |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1469 | svm_msg_q_msg_t msg; |
| 1470 | session_event_t *e; |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 1471 | svm_msg_q_t *mq; |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1472 | |
Florin Coras | 070453d | 2018-08-24 17:04:27 -0700 | [diff] [blame] | 1473 | if (PREDICT_FALSE (!buf)) |
| 1474 | return VPPCOM_EINVAL; |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1475 | |
Florin Coras | 070453d | 2018-08-24 17:04:27 -0700 | [diff] [blame] | 1476 | s = vcl_session_get (session_index); |
| 1477 | if (PREDICT_FALSE (!s)) |
| 1478 | return VPPCOM_EBADFD; |
Dave Wallace | 4878cbe | 2017-11-21 03:45:09 -0500 | [diff] [blame] | 1479 | |
Florin Coras | 460dce6 | 2018-07-27 05:45:06 -0700 | [diff] [blame] | 1480 | tx_fifo = s->tx_fifo; |
| 1481 | is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK); |
Keith Burns (alagalah) | 9b79377 | 2018-02-16 08:20:56 -0800 | [diff] [blame] | 1482 | |
Florin Coras | 460dce6 | 2018-07-27 05:45:06 -0700 | [diff] [blame] | 1483 | if (PREDICT_FALSE (s->is_vep)) |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1484 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1485 | clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: " |
Dave Wallace | ee45d41 | 2017-11-24 21:44:06 -0500 | [diff] [blame] | 1486 | "cannot write to an epoll session!", |
Florin Coras | 460dce6 | 2018-07-27 05:45:06 -0700 | [diff] [blame] | 1487 | getpid (), s->vpp_handle, session_index); |
Dave Wallace | 4878cbe | 2017-11-21 03:45:09 -0500 | [diff] [blame] | 1488 | |
Florin Coras | 070453d | 2018-08-24 17:04:27 -0700 | [diff] [blame] | 1489 | return VPPCOM_EBADFD; |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1490 | } |
| 1491 | |
Florin Coras | 460dce6 | 2018-07-27 05:45:06 -0700 | [diff] [blame] | 1492 | if (!(s->session_state & STATE_OPEN)) |
Dave Wallace | 9c4b5b2 | 2017-10-18 21:15:48 -0400 | [diff] [blame] | 1493 | { |
Florin Coras | 460dce6 | 2018-07-27 05:45:06 -0700 | [diff] [blame] | 1494 | session_state_t state = s->session_state; |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1495 | rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN); |
Florin Coras | 0d427d8 | 2018-06-27 03:24:07 -0700 | [diff] [blame] | 1496 | VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: session is not open! " |
| 1497 | "state 0x%x (%s)", |
Florin Coras | 460dce6 | 2018-07-27 05:45:06 -0700 | [diff] [blame] | 1498 | getpid (), s->vpp_handle, session_index, |
Florin Coras | 0d427d8 | 2018-06-27 03:24:07 -0700 | [diff] [blame] | 1499 | state, vppcom_session_state_str (state)); |
Florin Coras | 070453d | 2018-08-24 17:04:27 -0700 | [diff] [blame] | 1500 | return rv; |
Dave Wallace | 9c4b5b2 | 2017-10-18 21:15:48 -0400 | [diff] [blame] | 1501 | } |
| 1502 | |
Florin Coras | 460dce6 | 2018-07-27 05:45:06 -0700 | [diff] [blame] | 1503 | mq = vcl_session_is_ct (s) ? s->our_evt_q : vcm->app_event_queue; |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1504 | if (svm_fifo_is_full (tx_fifo)) |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1505 | { |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1506 | if (is_nonblocking) |
| 1507 | { |
Florin Coras | 070453d | 2018-08-24 17:04:27 -0700 | [diff] [blame] | 1508 | return VPPCOM_EWOULDBLOCK; |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1509 | } |
Florin Coras | 60f1fc1 | 2018-08-16 14:57:31 -0700 | [diff] [blame] | 1510 | while (svm_fifo_is_full (tx_fifo)) |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1511 | { |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 1512 | svm_msg_q_lock (mq); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 1513 | while (svm_msg_q_is_empty (mq) && svm_msg_q_timedwait (mq, 10e-6)) |
| 1514 | ; |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1515 | svm_msg_q_sub_w_lock (mq, &msg); |
| 1516 | e = svm_msg_q_msg_data (mq, &msg); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 1517 | svm_msg_q_unlock (mq); |
| 1518 | |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1519 | if (!vcl_is_tx_evt_for_session (e, session_index, |
Florin Coras | 460dce6 | 2018-07-27 05:45:06 -0700 | [diff] [blame] | 1520 | s->our_evt_q != 0)) |
Florin Coras | 60f1fc1 | 2018-08-16 14:57:31 -0700 | [diff] [blame] | 1521 | vcl_handle_mq_ctrl_event (e); |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1522 | svm_msg_q_free_msg (mq, &msg); |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1523 | } |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1524 | } |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1525 | |
Florin Coras | 460dce6 | 2018-07-27 05:45:06 -0700 | [diff] [blame] | 1526 | ASSERT (FIFO_EVENT_APP_TX + 1 == SESSION_IO_EVT_CT_TX); |
| 1527 | et = FIFO_EVENT_APP_TX + vcl_session_is_ct (s); |
| 1528 | if (s->is_dgram) |
| 1529 | n_write = app_send_dgram_raw (tx_fifo, &s->transport, |
| 1530 | s->vpp_evt_q, buf, n, et, SVM_Q_WAIT); |
| 1531 | else |
| 1532 | n_write = app_send_stream_raw (tx_fifo, s->vpp_evt_q, buf, n, et, |
| 1533 | SVM_Q_WAIT); |
Keith Burns (alagalah) | 410bcca | 2018-03-23 13:42:49 -0700 | [diff] [blame] | 1534 | |
Florin Coras | 460dce6 | 2018-07-27 05:45:06 -0700 | [diff] [blame] | 1535 | ASSERT (n_write > 0); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1536 | |
| 1537 | if (VPPCOM_DEBUG > 2) |
Dave Wallace | 9c4b5b2 | 2017-10-18 21:15:48 -0400 | [diff] [blame] | 1538 | { |
Dave Wallace | 4878cbe | 2017-11-21 03:45:09 -0500 | [diff] [blame] | 1539 | if (n_write <= 0) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1540 | clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: " |
Florin Coras | 460dce6 | 2018-07-27 05:45:06 -0700 | [diff] [blame] | 1541 | "FIFO-FULL (%p)", getpid (), s->vpp_handle, |
Keith Burns (alagalah) | 9b79377 | 2018-02-16 08:20:56 -0800 | [diff] [blame] | 1542 | session_index, tx_fifo); |
Dave Wallace | 9c4b5b2 | 2017-10-18 21:15:48 -0400 | [diff] [blame] | 1543 | else |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1544 | clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: " |
Keith Burns (alagalah) | 9b79377 | 2018-02-16 08:20:56 -0800 | [diff] [blame] | 1545 | "wrote %d bytes tx-fifo: (%p)", getpid (), |
Florin Coras | 460dce6 | 2018-07-27 05:45:06 -0700 | [diff] [blame] | 1546 | s->vpp_handle, session_index, n_write, tx_fifo); |
Dave Wallace | 9c4b5b2 | 2017-10-18 21:15:48 -0400 | [diff] [blame] | 1547 | } |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1548 | return n_write; |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1549 | } |
| 1550 | |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 1551 | static vcl_session_t * |
| 1552 | vcl_ct_session_get_from_fifo (svm_fifo_t * f, u8 type) |
| 1553 | { |
| 1554 | vcl_session_t *s; |
| 1555 | s = vcl_session_get (f->client_session_index); |
| 1556 | if (s) |
| 1557 | { |
| 1558 | /* rx fifo */ |
| 1559 | if (type == 0 && s->rx_fifo == f) |
| 1560 | return s; |
| 1561 | /* tx fifo */ |
| 1562 | if (type == 1 && s->tx_fifo == f) |
| 1563 | return s; |
| 1564 | } |
| 1565 | s = vcl_session_get (f->master_session_index); |
| 1566 | if (s) |
| 1567 | { |
| 1568 | if (type == 0 && s->rx_fifo == f) |
| 1569 | return s; |
| 1570 | if (type == 1 && s->tx_fifo == f) |
| 1571 | return s; |
| 1572 | } |
| 1573 | return 0; |
| 1574 | } |
| 1575 | |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1576 | static inline int |
Florin Coras | 7e12d94 | 2018-06-27 14:32:43 -0700 | [diff] [blame] | 1577 | vppcom_session_write_ready (vcl_session_t * session, u32 session_index) |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1578 | { |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1579 | /* Assumes caller has acquired spinlock: vcm->sessions_lockp */ |
Dave Wallace | 4878cbe | 2017-11-21 03:45:09 -0500 | [diff] [blame] | 1580 | if (PREDICT_FALSE (session->is_vep)) |
Dave Wallace | 9c4b5b2 | 2017-10-18 21:15:48 -0400 | [diff] [blame] | 1581 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1582 | clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: " |
Dave Wallace | ee45d41 | 2017-11-24 21:44:06 -0500 | [diff] [blame] | 1583 | "cannot write to an epoll session!", |
| 1584 | getpid (), session->vpp_handle, session_index); |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1585 | return VPPCOM_EBADFD; |
Dave Wallace | 9c4b5b2 | 2017-10-18 21:15:48 -0400 | [diff] [blame] | 1586 | } |
| 1587 | |
Florin Coras | 7e12d94 | 2018-06-27 14:32:43 -0700 | [diff] [blame] | 1588 | if (PREDICT_FALSE (session->session_state & STATE_LISTEN)) |
Dave Wallace | 33e002b | 2017-09-06 01:20:02 -0400 | [diff] [blame] | 1589 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1590 | clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: " |
Dave Wallace | ee45d41 | 2017-11-24 21:44:06 -0500 | [diff] [blame] | 1591 | "cannot write to a listen session!", |
| 1592 | getpid (), session->vpp_handle, session_index); |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1593 | return VPPCOM_EBADFD; |
Dave Wallace | 4878cbe | 2017-11-21 03:45:09 -0500 | [diff] [blame] | 1594 | } |
| 1595 | |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1596 | if (PREDICT_FALSE (!(session->session_state & STATE_OPEN))) |
Dave Wallace | 4878cbe | 2017-11-21 03:45:09 -0500 | [diff] [blame] | 1597 | { |
Florin Coras | 7e12d94 | 2018-06-27 14:32:43 -0700 | [diff] [blame] | 1598 | session_state_t state = session->session_state; |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1599 | int rv; |
Dave Wallace | 4878cbe | 2017-11-21 03:45:09 -0500 | [diff] [blame] | 1600 | |
Keith Burns (alagalah) | 56a0d06 | 2018-02-15 07:52:50 -0800 | [diff] [blame] | 1601 | rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1602 | clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: " |
Keith Burns (alagalah) | 9b79377 | 2018-02-16 08:20:56 -0800 | [diff] [blame] | 1603 | "session is not open! state 0x%x (%s), " |
Dave Wallace | ee45d41 | 2017-11-24 21:44:06 -0500 | [diff] [blame] | 1604 | "returning %d (%s)", getpid (), session->vpp_handle, |
Keith Burns (alagalah) | 9b79377 | 2018-02-16 08:20:56 -0800 | [diff] [blame] | 1605 | session_index, |
Dave Wallace | 4878cbe | 2017-11-21 03:45:09 -0500 | [diff] [blame] | 1606 | state, vppcom_session_state_str (state), |
| 1607 | rv, vppcom_retval_str (rv)); |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1608 | return rv; |
Dave Wallace | 33e002b | 2017-09-06 01:20:02 -0400 | [diff] [blame] | 1609 | } |
| 1610 | |
Florin Coras | 0d427d8 | 2018-06-27 03:24:07 -0700 | [diff] [blame] | 1611 | VDBG (3, "VCL<%d>: vpp handle 0x%llx, sid %u: peek %s (%p), ready = %d", |
| 1612 | getpid (), session->vpp_handle, session_index, session->tx_fifo, |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1613 | svm_fifo_max_enqueue (session->tx_fifo)); |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 1614 | |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1615 | return svm_fifo_max_enqueue (session->tx_fifo); |
| 1616 | } |
| 1617 | |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 1618 | static inline int |
| 1619 | vcl_mq_dequeue_batch (svm_msg_q_t * mq) |
| 1620 | { |
| 1621 | svm_msg_q_msg_t *msg; |
| 1622 | u32 n_msgs; |
| 1623 | int i; |
| 1624 | |
| 1625 | n_msgs = svm_msg_q_size (mq); |
| 1626 | for (i = 0; i < n_msgs; i++) |
| 1627 | { |
| 1628 | vec_add2 (vcm->mq_msg_vector, msg, 1); |
| 1629 | svm_msg_q_sub_w_lock (mq, msg); |
| 1630 | } |
| 1631 | return n_msgs; |
| 1632 | } |
| 1633 | |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1634 | static int |
| 1635 | vcl_select_handle_mq (svm_msg_q_t * mq, unsigned long n_bits, |
| 1636 | unsigned long *read_map, unsigned long *write_map, |
| 1637 | unsigned long *except_map, double time_to_wait, |
| 1638 | u32 * bits_set) |
| 1639 | { |
| 1640 | session_disconnected_msg_t *disconnected_msg; |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 1641 | session_connected_msg_t *connected_msg; |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1642 | session_accepted_msg_t *accepted_msg; |
| 1643 | vcl_session_msg_t *vcl_msg; |
| 1644 | vcl_session_t *session; |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 1645 | svm_msg_q_msg_t *msg; |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1646 | session_event_t *e; |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 1647 | u32 i, sid; |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1648 | u64 handle; |
| 1649 | |
| 1650 | svm_msg_q_lock (mq); |
| 1651 | if (svm_msg_q_is_empty (mq)) |
Dave Wallace | 4878cbe | 2017-11-21 03:45:09 -0500 | [diff] [blame] | 1652 | { |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1653 | if (*bits_set) |
Dave Wallace | 4878cbe | 2017-11-21 03:45:09 -0500 | [diff] [blame] | 1654 | { |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1655 | svm_msg_q_unlock (mq); |
| 1656 | return 0; |
| 1657 | } |
Dave Wallace | 4878cbe | 2017-11-21 03:45:09 -0500 | [diff] [blame] | 1658 | |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1659 | if (!time_to_wait) |
| 1660 | { |
| 1661 | svm_msg_q_unlock (mq); |
| 1662 | return 0; |
| 1663 | } |
| 1664 | else if (time_to_wait < 0) |
| 1665 | { |
| 1666 | svm_msg_q_wait (mq); |
| 1667 | } |
| 1668 | else |
| 1669 | { |
| 1670 | if (svm_msg_q_timedwait (mq, time_to_wait)) |
| 1671 | { |
| 1672 | svm_msg_q_unlock (mq); |
| 1673 | return 0; |
| 1674 | } |
Dave Wallace | 4878cbe | 2017-11-21 03:45:09 -0500 | [diff] [blame] | 1675 | } |
| 1676 | } |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 1677 | vcl_mq_dequeue_batch (mq); |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1678 | svm_msg_q_unlock (mq); |
| 1679 | |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 1680 | for (i = 0; i < vec_len (vcm->mq_msg_vector); i++) |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1681 | { |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 1682 | msg = vec_elt_at_index (vcm->mq_msg_vector, i); |
| 1683 | e = svm_msg_q_msg_data (mq, msg); |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1684 | switch (e->event_type) |
| 1685 | { |
| 1686 | case FIFO_EVENT_APP_RX: |
| 1687 | sid = e->fifo->client_session_index; |
| 1688 | session = vcl_session_get (sid); |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1689 | if (sid < n_bits && read_map) |
| 1690 | { |
| 1691 | clib_bitmap_set_no_check (read_map, sid, 1); |
| 1692 | *bits_set += 1; |
| 1693 | } |
| 1694 | break; |
| 1695 | case FIFO_EVENT_APP_TX: |
| 1696 | sid = e->fifo->client_session_index; |
| 1697 | session = vcl_session_get (sid); |
Florin Coras | 60f1fc1 | 2018-08-16 14:57:31 -0700 | [diff] [blame] | 1698 | if (!session) |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1699 | break; |
| 1700 | if (sid < n_bits && write_map) |
| 1701 | { |
| 1702 | clib_bitmap_set_no_check (write_map, sid, 1); |
| 1703 | *bits_set += 1; |
| 1704 | } |
| 1705 | break; |
| 1706 | case SESSION_IO_EVT_CT_TX: |
| 1707 | session = vcl_ct_session_get_from_fifo (e->fifo, 0); |
| 1708 | sid = vcl_session_index (session); |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1709 | if (sid < n_bits && read_map) |
| 1710 | { |
| 1711 | clib_bitmap_set_no_check (read_map, sid, 1); |
| 1712 | *bits_set += 1; |
| 1713 | } |
| 1714 | break; |
| 1715 | break; |
| 1716 | case SESSION_IO_EVT_CT_RX: |
| 1717 | session = vcl_ct_session_get_from_fifo (e->fifo, 1); |
| 1718 | sid = vcl_session_index (session); |
Florin Coras | 60f1fc1 | 2018-08-16 14:57:31 -0700 | [diff] [blame] | 1719 | if (!session) |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1720 | break; |
| 1721 | if (sid < n_bits && write_map) |
| 1722 | { |
| 1723 | clib_bitmap_set_no_check (write_map, sid, 1); |
| 1724 | *bits_set += 1; |
| 1725 | } |
| 1726 | break; |
| 1727 | case SESSION_CTRL_EVT_ACCEPTED: |
| 1728 | accepted_msg = (session_accepted_msg_t *) e->data; |
| 1729 | handle = accepted_msg->listener_handle; |
| 1730 | session = vppcom_session_table_lookup_listener (handle); |
| 1731 | if (!session) |
| 1732 | { |
| 1733 | clib_warning ("VCL<%d>: ERROR: couldn't find listen session:" |
| 1734 | "listener handle %llx", getpid (), handle); |
| 1735 | break; |
| 1736 | } |
| 1737 | |
| 1738 | clib_fifo_add2 (session->accept_evts_fifo, vcl_msg); |
| 1739 | vcl_msg->accepted_msg = *accepted_msg; |
| 1740 | sid = session - vcm->sessions; |
| 1741 | if (sid < n_bits && read_map) |
| 1742 | { |
| 1743 | clib_bitmap_set_no_check (read_map, sid, 1); |
| 1744 | *bits_set += 1; |
| 1745 | } |
| 1746 | break; |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 1747 | case SESSION_CTRL_EVT_CONNECTED: |
| 1748 | connected_msg = (session_connected_msg_t *) e->data; |
| 1749 | vcl_session_connected_handler (connected_msg); |
| 1750 | break; |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1751 | case SESSION_CTRL_EVT_DISCONNECTED: |
| 1752 | disconnected_msg = (session_disconnected_msg_t *) e->data; |
| 1753 | sid = vcl_session_get_index_from_handle (disconnected_msg->handle); |
| 1754 | if (sid < n_bits && except_map) |
| 1755 | { |
| 1756 | clib_bitmap_set_no_check (except_map, sid, 1); |
| 1757 | *bits_set += 1; |
| 1758 | } |
| 1759 | break; |
Florin Coras | c9fbd66 | 2018-08-24 12:59:56 -0700 | [diff] [blame] | 1760 | case SESSION_CTRL_EVT_RESET: |
Florin Coras | 6011699 | 2018-08-27 09:52:18 -0700 | [diff] [blame] | 1761 | sid = vcl_session_reset_handler ((session_reset_msg_t *) e->data); |
Florin Coras | c9fbd66 | 2018-08-24 12:59:56 -0700 | [diff] [blame] | 1762 | if (sid < n_bits && except_map) |
| 1763 | { |
| 1764 | clib_bitmap_set_no_check (except_map, sid, 1); |
| 1765 | *bits_set += 1; |
| 1766 | } |
| 1767 | break; |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1768 | default: |
| 1769 | clib_warning ("unhandled: %u", e->event_type); |
| 1770 | break; |
| 1771 | } |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 1772 | svm_msg_q_free_msg (mq, msg); |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1773 | } |
| 1774 | |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 1775 | vec_reset_length (vcm->mq_msg_vector); |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1776 | return *bits_set; |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1777 | } |
| 1778 | |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 1779 | static int |
| 1780 | vppcom_select_condvar (unsigned long n_bits, unsigned long *read_map, |
| 1781 | unsigned long *write_map, unsigned long *except_map, |
| 1782 | double time_to_wait, u32 * bits_set) |
| 1783 | { |
| 1784 | double total_wait = 0, wait_slice; |
| 1785 | vcl_cut_through_registration_t *cr; |
| 1786 | |
| 1787 | time_to_wait = (time_to_wait == -1) ? 10e9 : time_to_wait; |
| 1788 | wait_slice = vcm->cut_through_registrations ? 10e-6 : time_to_wait; |
| 1789 | do |
| 1790 | { |
| 1791 | /* *INDENT-OFF* */ |
| 1792 | pool_foreach (cr, vcm->cut_through_registrations, ({ |
| 1793 | vcl_select_handle_mq (cr->mq, n_bits, read_map, write_map, except_map, |
| 1794 | 0, bits_set); |
| 1795 | })); |
| 1796 | /* *INDENT-ON* */ |
| 1797 | |
| 1798 | vcl_select_handle_mq (vcm->app_event_queue, n_bits, read_map, write_map, |
| 1799 | except_map, time_to_wait, bits_set); |
| 1800 | total_wait += wait_slice; |
| 1801 | if (*bits_set) |
| 1802 | return *bits_set; |
| 1803 | } |
| 1804 | while (total_wait < time_to_wait); |
| 1805 | |
| 1806 | return 0; |
| 1807 | } |
| 1808 | |
| 1809 | static int |
| 1810 | vppcom_select_eventfd (unsigned long n_bits, unsigned long *read_map, |
| 1811 | unsigned long *write_map, unsigned long *except_map, |
| 1812 | double time_to_wait, u32 * bits_set) |
| 1813 | { |
| 1814 | vcl_mq_evt_conn_t *mqc; |
| 1815 | int __clib_unused n_read; |
| 1816 | int n_mq_evts, i; |
| 1817 | u64 buf; |
| 1818 | |
| 1819 | vec_validate (vcm->mq_events, pool_elts (vcm->mq_evt_conns)); |
| 1820 | n_mq_evts = epoll_wait (vcm->mqs_epfd, vcm->mq_events, |
| 1821 | vec_len (vcm->mq_events), time_to_wait); |
| 1822 | for (i = 0; i < n_mq_evts; i++) |
| 1823 | { |
| 1824 | mqc = vcl_mq_evt_conn_get (vcm->mq_events[i].data.u32); |
| 1825 | n_read = read (mqc->mq_fd, &buf, sizeof (buf)); |
| 1826 | vcl_select_handle_mq (mqc->mq, n_bits, read_map, write_map, |
| 1827 | except_map, 0, bits_set); |
| 1828 | } |
| 1829 | |
| 1830 | return (n_mq_evts > 0 ? (int) *bits_set : 0); |
| 1831 | } |
| 1832 | |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1833 | int |
| 1834 | vppcom_select (unsigned long n_bits, unsigned long *read_map, |
| 1835 | unsigned long *write_map, unsigned long *except_map, |
| 1836 | double time_to_wait) |
| 1837 | { |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1838 | u32 sid, minbits = clib_max (n_bits, BITS (uword)), bits_set = 0; |
Florin Coras | 7e12d94 | 2018-06-27 14:32:43 -0700 | [diff] [blame] | 1839 | vcl_session_t *session = 0; |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1840 | int rv; |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1841 | |
| 1842 | ASSERT (sizeof (clib_bitmap_t) == sizeof (long int)); |
| 1843 | |
Dave Wallace | 7876d39 | 2017-10-19 03:53:57 -0400 | [diff] [blame] | 1844 | if (n_bits && read_map) |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1845 | { |
| 1846 | clib_bitmap_validate (vcm->rd_bitmap, minbits); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1847 | clib_memcpy (vcm->rd_bitmap, read_map, |
| 1848 | vec_len (vcm->rd_bitmap) * sizeof (clib_bitmap_t)); |
| 1849 | memset (read_map, 0, vec_len (vcm->rd_bitmap) * sizeof (clib_bitmap_t)); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1850 | } |
Dave Wallace | 7876d39 | 2017-10-19 03:53:57 -0400 | [diff] [blame] | 1851 | if (n_bits && write_map) |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1852 | { |
| 1853 | clib_bitmap_validate (vcm->wr_bitmap, minbits); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1854 | clib_memcpy (vcm->wr_bitmap, write_map, |
| 1855 | vec_len (vcm->wr_bitmap) * sizeof (clib_bitmap_t)); |
| 1856 | memset (write_map, 0, |
| 1857 | vec_len (vcm->wr_bitmap) * sizeof (clib_bitmap_t)); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1858 | } |
Dave Wallace | 7876d39 | 2017-10-19 03:53:57 -0400 | [diff] [blame] | 1859 | if (n_bits && except_map) |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1860 | { |
| 1861 | clib_bitmap_validate (vcm->ex_bitmap, minbits); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1862 | clib_memcpy (vcm->ex_bitmap, except_map, |
| 1863 | vec_len (vcm->ex_bitmap) * sizeof (clib_bitmap_t)); |
| 1864 | memset (except_map, 0, |
| 1865 | vec_len (vcm->ex_bitmap) * sizeof (clib_bitmap_t)); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1866 | } |
| 1867 | |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1868 | if (!n_bits) |
| 1869 | return 0; |
| 1870 | |
| 1871 | if (!write_map) |
| 1872 | goto check_rd; |
| 1873 | |
| 1874 | /* *INDENT-OFF* */ |
| 1875 | clib_bitmap_foreach (sid, vcm->wr_bitmap, ({ |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1876 | if (!(session = vcl_session_get (sid))) |
| 1877 | { |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1878 | VDBG (0, "VCL<%d>: session %d specified in write_map is closed.", |
| 1879 | getpid (), sid); |
| 1880 | return VPPCOM_EBADFD; |
| 1881 | } |
| 1882 | |
| 1883 | rv = svm_fifo_is_full (session->tx_fifo); |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1884 | if (!rv) |
| 1885 | { |
| 1886 | clib_bitmap_set_no_check (write_map, sid, 1); |
| 1887 | bits_set++; |
| 1888 | } |
| 1889 | })); |
| 1890 | |
| 1891 | check_rd: |
| 1892 | if (!read_map) |
| 1893 | goto check_mq; |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 1894 | |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1895 | clib_bitmap_foreach (sid, vcm->rd_bitmap, ({ |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1896 | if (!(session = vcl_session_get (sid))) |
| 1897 | { |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1898 | VDBG (0, "VCL<%d>: session %d specified in write_map is closed.", |
| 1899 | getpid (), sid); |
| 1900 | return VPPCOM_EBADFD; |
| 1901 | } |
| 1902 | |
| 1903 | rv = vppcom_session_read_ready (session); |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1904 | if (rv) |
| 1905 | { |
| 1906 | clib_bitmap_set_no_check (read_map, sid, 1); |
| 1907 | bits_set++; |
| 1908 | } |
| 1909 | })); |
| 1910 | /* *INDENT-ON* */ |
| 1911 | |
| 1912 | check_mq: |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1913 | |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 1914 | if (vcm->cfg.use_mq_eventfd) |
| 1915 | vppcom_select_eventfd (n_bits, read_map, write_map, except_map, |
| 1916 | time_to_wait, &bits_set); |
| 1917 | else |
| 1918 | vppcom_select_condvar (n_bits, read_map, write_map, except_map, |
| 1919 | time_to_wait, &bits_set); |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1920 | |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1921 | return (bits_set); |
| 1922 | } |
| 1923 | |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 1924 | static inline void |
| 1925 | vep_verify_epoll_chain (u32 vep_idx) |
| 1926 | { |
Florin Coras | 7e12d94 | 2018-06-27 14:32:43 -0700 | [diff] [blame] | 1927 | vcl_session_t *session; |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 1928 | vppcom_epoll_t *vep; |
Dave Wallace | 498b3a5 | 2017-11-09 13:00:34 -0500 | [diff] [blame] | 1929 | u32 sid = vep_idx; |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 1930 | |
Dave Wallace | 498b3a5 | 2017-11-09 13:00:34 -0500 | [diff] [blame] | 1931 | if (VPPCOM_DEBUG <= 1) |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 1932 | return; |
| 1933 | |
| 1934 | /* Assumes caller has acquired spinlock: vcm->sessions_lockp */ |
Florin Coras | 070453d | 2018-08-24 17:04:27 -0700 | [diff] [blame] | 1935 | session = vcl_session_get (vep_idx); |
| 1936 | if (PREDICT_FALSE (!session)) |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 1937 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1938 | clib_warning ("VCL<%d>: ERROR: Invalid vep_idx (%u)!", |
| 1939 | getpid (), vep_idx); |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 1940 | goto done; |
| 1941 | } |
| 1942 | if (PREDICT_FALSE (!session->is_vep)) |
| 1943 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1944 | clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!", |
| 1945 | getpid (), vep_idx); |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 1946 | goto done; |
| 1947 | } |
Dave Wallace | 4878cbe | 2017-11-21 03:45:09 -0500 | [diff] [blame] | 1948 | vep = &session->vep; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1949 | clib_warning ("VCL<%d>: vep_idx (%u): Dumping epoll chain\n" |
Dave Wallace | 498b3a5 | 2017-11-09 13:00:34 -0500 | [diff] [blame] | 1950 | "{\n" |
| 1951 | " is_vep = %u\n" |
| 1952 | " is_vep_session = %u\n" |
Dave Wallace | 4878cbe | 2017-11-21 03:45:09 -0500 | [diff] [blame] | 1953 | " next_sid = 0x%x (%u)\n" |
Dave Wallace | 498b3a5 | 2017-11-09 13:00:34 -0500 | [diff] [blame] | 1954 | " wait_cont_idx = 0x%x (%u)\n" |
Dave Wallace | 4878cbe | 2017-11-21 03:45:09 -0500 | [diff] [blame] | 1955 | "}\n", getpid (), vep_idx, |
| 1956 | session->is_vep, session->is_vep_session, |
| 1957 | vep->next_sid, vep->next_sid, |
Dave Wallace | 498b3a5 | 2017-11-09 13:00:34 -0500 | [diff] [blame] | 1958 | session->wait_cont_idx, session->wait_cont_idx); |
Dave Wallace | 4878cbe | 2017-11-21 03:45:09 -0500 | [diff] [blame] | 1959 | |
| 1960 | for (sid = vep->next_sid; sid != ~0; sid = vep->next_sid) |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 1961 | { |
Florin Coras | 070453d | 2018-08-24 17:04:27 -0700 | [diff] [blame] | 1962 | session = vcl_session_get (sid); |
| 1963 | if (PREDICT_FALSE (!session)) |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 1964 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1965 | clib_warning ("VCL<%d>: ERROR: Invalid sid (%u)!", getpid (), sid); |
Dave Wallace | 4878cbe | 2017-11-21 03:45:09 -0500 | [diff] [blame] | 1966 | goto done; |
| 1967 | } |
| 1968 | if (PREDICT_FALSE (session->is_vep)) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1969 | clib_warning ("VCL<%d>: ERROR: sid (%u) is a vep!", |
| 1970 | getpid (), vep_idx); |
Dave Wallace | 4878cbe | 2017-11-21 03:45:09 -0500 | [diff] [blame] | 1971 | else if (PREDICT_FALSE (!session->is_vep_session)) |
| 1972 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1973 | clib_warning ("VCL<%d>: ERROR: session (%u) " |
| 1974 | "is not a vep session!", getpid (), sid); |
Dave Wallace | 4878cbe | 2017-11-21 03:45:09 -0500 | [diff] [blame] | 1975 | goto done; |
| 1976 | } |
| 1977 | vep = &session->vep; |
| 1978 | if (PREDICT_FALSE (vep->vep_idx != vep_idx)) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1979 | clib_warning ("VCL<%d>: ERROR: session (%u) vep_idx (%u) != " |
Dave Wallace | 4878cbe | 2017-11-21 03:45:09 -0500 | [diff] [blame] | 1980 | "vep_idx (%u)!", getpid (), |
| 1981 | sid, session->vep.vep_idx, vep_idx); |
| 1982 | if (session->is_vep_session) |
| 1983 | { |
| 1984 | clib_warning ("vep_idx[%u]: sid 0x%x (%u)\n" |
| 1985 | "{\n" |
| 1986 | " next_sid = 0x%x (%u)\n" |
| 1987 | " prev_sid = 0x%x (%u)\n" |
| 1988 | " vep_idx = 0x%x (%u)\n" |
| 1989 | " ev.events = 0x%x\n" |
| 1990 | " ev.data.u64 = 0x%llx\n" |
| 1991 | " et_mask = 0x%x\n" |
| 1992 | "}\n", |
| 1993 | vep_idx, sid, sid, |
| 1994 | vep->next_sid, vep->next_sid, |
| 1995 | vep->prev_sid, vep->prev_sid, |
| 1996 | vep->vep_idx, vep->vep_idx, |
| 1997 | vep->ev.events, vep->ev.data.u64, vep->et_mask); |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 1998 | } |
| 1999 | } |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 2000 | |
| 2001 | done: |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2002 | clib_warning ("VCL<%d>: vep_idx (%u): Dump complete!\n", |
| 2003 | getpid (), vep_idx); |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 2004 | } |
| 2005 | |
| 2006 | int |
| 2007 | vppcom_epoll_create (void) |
| 2008 | { |
Florin Coras | 7e12d94 | 2018-06-27 14:32:43 -0700 | [diff] [blame] | 2009 | vcl_session_t *vep_session; |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 2010 | u32 vep_idx; |
| 2011 | |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 2012 | pool_get (vcm->sessions, vep_session); |
| 2013 | memset (vep_session, 0, sizeof (*vep_session)); |
| 2014 | vep_idx = vep_session - vcm->sessions; |
| 2015 | |
| 2016 | vep_session->is_vep = 1; |
| 2017 | vep_session->vep.vep_idx = ~0; |
| 2018 | vep_session->vep.next_sid = ~0; |
| 2019 | vep_session->vep.prev_sid = ~0; |
| 2020 | vep_session->wait_cont_idx = ~0; |
Dave Wallace | 4878cbe | 2017-11-21 03:45:09 -0500 | [diff] [blame] | 2021 | vep_session->vpp_handle = ~0; |
Keith Burns (alagalah) | 1275651 | 2018-03-06 05:55:27 -0800 | [diff] [blame] | 2022 | vep_session->poll_reg = 0; |
Keith Burns (alagalah) | 9b79377 | 2018-02-16 08:20:56 -0800 | [diff] [blame] | 2023 | |
Florin Coras | 0d427d8 | 2018-06-27 03:24:07 -0700 | [diff] [blame] | 2024 | vcl_evt (VCL_EVT_EPOLL_CREATE, vep_session, vep_idx); |
Florin Coras | 0d427d8 | 2018-06-27 03:24:07 -0700 | [diff] [blame] | 2025 | VDBG (0, "VCL<%d>: Created vep_idx %u / sid %u!", |
| 2026 | getpid (), vep_idx, vep_idx); |
Keith Burns (alagalah) | 09b2784 | 2018-01-05 14:39:59 -0800 | [diff] [blame] | 2027 | |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 2028 | return (vep_idx); |
| 2029 | } |
| 2030 | |
| 2031 | int |
| 2032 | vppcom_epoll_ctl (uint32_t vep_idx, int op, uint32_t session_index, |
| 2033 | struct epoll_event *event) |
| 2034 | { |
Florin Coras | 7e12d94 | 2018-06-27 14:32:43 -0700 | [diff] [blame] | 2035 | vcl_session_t *vep_session; |
| 2036 | vcl_session_t *session; |
Florin Coras | 070453d | 2018-08-24 17:04:27 -0700 | [diff] [blame] | 2037 | int rv = VPPCOM_OK; |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 2038 | |
| 2039 | if (vep_idx == session_index) |
| 2040 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2041 | clib_warning ("VCL<%d>: ERROR: vep_idx == session_index (%u)!", |
Dave Wallace | 4878cbe | 2017-11-21 03:45:09 -0500 | [diff] [blame] | 2042 | getpid (), vep_idx); |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 2043 | return VPPCOM_EINVAL; |
| 2044 | } |
| 2045 | |
Florin Coras | 070453d | 2018-08-24 17:04:27 -0700 | [diff] [blame] | 2046 | vep_session = vcl_session_get (vep_idx); |
| 2047 | if (PREDICT_FALSE (!vep_session)) |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 2048 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2049 | clib_warning ("VCL<%d>: ERROR: Invalid vep_idx (%u)!", vep_idx); |
Florin Coras | 070453d | 2018-08-24 17:04:27 -0700 | [diff] [blame] | 2050 | return VPPCOM_EBADFD; |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 2051 | } |
| 2052 | if (PREDICT_FALSE (!vep_session->is_vep)) |
| 2053 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2054 | clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!", |
Dave Wallace | 4878cbe | 2017-11-21 03:45:09 -0500 | [diff] [blame] | 2055 | getpid (), vep_idx); |
Florin Coras | 070453d | 2018-08-24 17:04:27 -0700 | [diff] [blame] | 2056 | return VPPCOM_EINVAL; |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 2057 | } |
| 2058 | |
| 2059 | ASSERT (vep_session->vep.vep_idx == ~0); |
| 2060 | ASSERT (vep_session->vep.prev_sid == ~0); |
| 2061 | |
Florin Coras | 070453d | 2018-08-24 17:04:27 -0700 | [diff] [blame] | 2062 | session = vcl_session_get (session_index); |
| 2063 | if (PREDICT_FALSE (!session)) |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 2064 | { |
Florin Coras | 0d427d8 | 2018-06-27 03:24:07 -0700 | [diff] [blame] | 2065 | VDBG (0, "VCL<%d>: ERROR: Invalid session_index (%u)!", |
| 2066 | getpid (), session_index); |
Florin Coras | 070453d | 2018-08-24 17:04:27 -0700 | [diff] [blame] | 2067 | return VPPCOM_EBADFD; |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 2068 | } |
| 2069 | if (PREDICT_FALSE (session->is_vep)) |
| 2070 | { |
Dave Wallace | 4878cbe | 2017-11-21 03:45:09 -0500 | [diff] [blame] | 2071 | clib_warning ("ERROR: session_index (%u) is a vep!", vep_idx); |
Florin Coras | 070453d | 2018-08-24 17:04:27 -0700 | [diff] [blame] | 2072 | return VPPCOM_EINVAL; |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 2073 | } |
| 2074 | |
| 2075 | switch (op) |
| 2076 | { |
| 2077 | case EPOLL_CTL_ADD: |
| 2078 | if (PREDICT_FALSE (!event)) |
| 2079 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2080 | clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_ADD: NULL pointer to " |
Dave Wallace | 2e005bb | 2017-11-07 01:21:39 -0500 | [diff] [blame] | 2081 | "epoll_event structure!", getpid ()); |
Florin Coras | 070453d | 2018-08-24 17:04:27 -0700 | [diff] [blame] | 2082 | return VPPCOM_EINVAL; |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 2083 | } |
| 2084 | if (vep_session->vep.next_sid != ~0) |
| 2085 | { |
Florin Coras | 7e12d94 | 2018-06-27 14:32:43 -0700 | [diff] [blame] | 2086 | vcl_session_t *next_session; |
Florin Coras | 070453d | 2018-08-24 17:04:27 -0700 | [diff] [blame] | 2087 | next_session = vcl_session_get (vep_session->vep.next_sid); |
| 2088 | if (PREDICT_FALSE (!next_session)) |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 2089 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2090 | clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_ADD: Invalid " |
Dave Wallace | 4878cbe | 2017-11-21 03:45:09 -0500 | [diff] [blame] | 2091 | "vep.next_sid (%u) on vep_idx (%u)!", |
| 2092 | getpid (), vep_session->vep.next_sid, vep_idx); |
Florin Coras | 070453d | 2018-08-24 17:04:27 -0700 | [diff] [blame] | 2093 | return VPPCOM_EBADFD; |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 2094 | } |
| 2095 | ASSERT (next_session->vep.prev_sid == vep_idx); |
| 2096 | next_session->vep.prev_sid = session_index; |
| 2097 | } |
| 2098 | session->vep.next_sid = vep_session->vep.next_sid; |
| 2099 | session->vep.prev_sid = vep_idx; |
| 2100 | session->vep.vep_idx = vep_idx; |
| 2101 | session->vep.et_mask = VEP_DEFAULT_ET_MASK; |
| 2102 | session->vep.ev = *event; |
Dave Wallace | 4878cbe | 2017-11-21 03:45:09 -0500 | [diff] [blame] | 2103 | session->is_vep = 0; |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 2104 | session->is_vep_session = 1; |
| 2105 | vep_session->vep.next_sid = session_index; |
Keith Burns (alagalah) | 3cf2d64 | 2018-02-23 10:17:01 -0800 | [diff] [blame] | 2106 | |
Florin Coras | 070453d | 2018-08-24 17:04:27 -0700 | [diff] [blame] | 2107 | VDBG (1, "VCL<%d>: EPOLL_CTL_ADD: vep_idx %u, sid %u, events 0x%x, " |
| 2108 | "data 0x%llx!", getpid (), vep_idx, session_index, event->events, |
| 2109 | event->data.u64); |
Florin Coras | 0d427d8 | 2018-06-27 03:24:07 -0700 | [diff] [blame] | 2110 | vcl_evt (VCL_EVT_EPOLL_CTLADD, session, event->events, event->data.u64); |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 2111 | break; |
| 2112 | |
| 2113 | case EPOLL_CTL_MOD: |
| 2114 | if (PREDICT_FALSE (!event)) |
| 2115 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2116 | clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_MOD: NULL pointer to " |
Dave Wallace | 2e005bb | 2017-11-07 01:21:39 -0500 | [diff] [blame] | 2117 | "epoll_event structure!", getpid ()); |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 2118 | rv = VPPCOM_EINVAL; |
| 2119 | goto done; |
| 2120 | } |
Dave Wallace | 4878cbe | 2017-11-21 03:45:09 -0500 | [diff] [blame] | 2121 | else if (PREDICT_FALSE (!session->is_vep_session)) |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 2122 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2123 | clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_MOD: " |
Dave Wallace | 4878cbe | 2017-11-21 03:45:09 -0500 | [diff] [blame] | 2124 | "not a vep session!", getpid (), session_index); |
| 2125 | rv = VPPCOM_EINVAL; |
| 2126 | goto done; |
| 2127 | } |
| 2128 | else if (PREDICT_FALSE (session->vep.vep_idx != vep_idx)) |
| 2129 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2130 | clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_MOD: " |
Dave Wallace | 4878cbe | 2017-11-21 03:45:09 -0500 | [diff] [blame] | 2131 | "vep_idx (%u) != vep_idx (%u)!", |
| 2132 | getpid (), session_index, |
| 2133 | session->vep.vep_idx, vep_idx); |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 2134 | rv = VPPCOM_EINVAL; |
| 2135 | goto done; |
| 2136 | } |
| 2137 | session->vep.et_mask = VEP_DEFAULT_ET_MASK; |
| 2138 | session->vep.ev = *event; |
Florin Coras | 0d427d8 | 2018-06-27 03:24:07 -0700 | [diff] [blame] | 2139 | VDBG (1, "VCL<%d>: EPOLL_CTL_MOD: vep_idx %u, sid %u, events 0x%x," |
| 2140 | " data 0x%llx!", getpid (), vep_idx, session_index, event->events, |
| 2141 | event->data.u64); |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 2142 | break; |
| 2143 | |
| 2144 | case EPOLL_CTL_DEL: |
Dave Wallace | 4878cbe | 2017-11-21 03:45:09 -0500 | [diff] [blame] | 2145 | if (PREDICT_FALSE (!session->is_vep_session)) |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 2146 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2147 | clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_DEL: " |
Dave Wallace | 4878cbe | 2017-11-21 03:45:09 -0500 | [diff] [blame] | 2148 | "not a vep session!", getpid (), session_index); |
| 2149 | rv = VPPCOM_EINVAL; |
| 2150 | goto done; |
| 2151 | } |
| 2152 | else if (PREDICT_FALSE (session->vep.vep_idx != vep_idx)) |
| 2153 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2154 | clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_DEL: " |
Dave Wallace | 4878cbe | 2017-11-21 03:45:09 -0500 | [diff] [blame] | 2155 | "vep_idx (%u) != vep_idx (%u)!", |
| 2156 | getpid (), session_index, |
| 2157 | session->vep.vep_idx, vep_idx); |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 2158 | rv = VPPCOM_EINVAL; |
| 2159 | goto done; |
| 2160 | } |
| 2161 | |
| 2162 | vep_session->wait_cont_idx = |
| 2163 | (vep_session->wait_cont_idx == session_index) ? |
| 2164 | session->vep.next_sid : vep_session->wait_cont_idx; |
| 2165 | |
| 2166 | if (session->vep.prev_sid == vep_idx) |
| 2167 | vep_session->vep.next_sid = session->vep.next_sid; |
| 2168 | else |
| 2169 | { |
Florin Coras | 7e12d94 | 2018-06-27 14:32:43 -0700 | [diff] [blame] | 2170 | vcl_session_t *prev_session; |
Florin Coras | 070453d | 2018-08-24 17:04:27 -0700 | [diff] [blame] | 2171 | prev_session = vcl_session_get (session->vep.prev_sid); |
| 2172 | if (PREDICT_FALSE (!prev_session)) |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 2173 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2174 | clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_DEL: Invalid " |
Dave Wallace | 4878cbe | 2017-11-21 03:45:09 -0500 | [diff] [blame] | 2175 | "vep.prev_sid (%u) on sid (%u)!", |
| 2176 | getpid (), session->vep.prev_sid, session_index); |
Florin Coras | 070453d | 2018-08-24 17:04:27 -0700 | [diff] [blame] | 2177 | return VPPCOM_EBADFD; |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 2178 | } |
| 2179 | ASSERT (prev_session->vep.next_sid == session_index); |
| 2180 | prev_session->vep.next_sid = session->vep.next_sid; |
| 2181 | } |
| 2182 | if (session->vep.next_sid != ~0) |
| 2183 | { |
Florin Coras | 7e12d94 | 2018-06-27 14:32:43 -0700 | [diff] [blame] | 2184 | vcl_session_t *next_session; |
Florin Coras | 070453d | 2018-08-24 17:04:27 -0700 | [diff] [blame] | 2185 | next_session = vcl_session_get (session->vep.next_sid); |
| 2186 | if (PREDICT_FALSE (!next_session)) |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 2187 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2188 | clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_DEL: Invalid " |
Dave Wallace | 4878cbe | 2017-11-21 03:45:09 -0500 | [diff] [blame] | 2189 | "vep.next_sid (%u) on sid (%u)!", |
| 2190 | getpid (), session->vep.next_sid, session_index); |
Florin Coras | 070453d | 2018-08-24 17:04:27 -0700 | [diff] [blame] | 2191 | return VPPCOM_EBADFD; |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 2192 | } |
| 2193 | ASSERT (next_session->vep.prev_sid == session_index); |
| 2194 | next_session->vep.prev_sid = session->vep.prev_sid; |
| 2195 | } |
| 2196 | |
| 2197 | memset (&session->vep, 0, sizeof (session->vep)); |
| 2198 | session->vep.next_sid = ~0; |
| 2199 | session->vep.prev_sid = ~0; |
| 2200 | session->vep.vep_idx = ~0; |
| 2201 | session->is_vep_session = 0; |
Florin Coras | 0d427d8 | 2018-06-27 03:24:07 -0700 | [diff] [blame] | 2202 | VDBG (1, "VCL<%d>: EPOLL_CTL_DEL: vep_idx %u, sid %u!", |
| 2203 | getpid (), vep_idx, session_index); |
| 2204 | vcl_evt (VCL_EVT_EPOLL_CTLDEL, session, vep_idx); |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 2205 | break; |
| 2206 | |
| 2207 | default: |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2208 | clib_warning ("VCL<%d>: ERROR: Invalid operation (%d)!", getpid (), op); |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 2209 | rv = VPPCOM_EINVAL; |
| 2210 | } |
| 2211 | |
| 2212 | vep_verify_epoll_chain (vep_idx); |
| 2213 | |
| 2214 | done: |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 2215 | return rv; |
| 2216 | } |
| 2217 | |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 2218 | static int |
| 2219 | vcl_epoll_wait_handle_mq (svm_msg_q_t * mq, struct epoll_event *events, |
| 2220 | u32 maxevents, double wait_for_time, u32 * num_ev) |
| 2221 | { |
| 2222 | session_disconnected_msg_t *disconnected_msg; |
| 2223 | session_connected_msg_t *connected_msg; |
| 2224 | session_accepted_msg_t *accepted_msg; |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 2225 | u64 session_evt_data = ~0, handle; |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 2226 | u32 sid = ~0, session_events; |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 2227 | vcl_session_msg_t *vcl_msg; |
| 2228 | vcl_session_t *session; |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 2229 | svm_msg_q_msg_t *msg; |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 2230 | session_event_t *e; |
| 2231 | u8 add_event; |
| 2232 | int i; |
| 2233 | |
| 2234 | svm_msg_q_lock (mq); |
| 2235 | if (svm_msg_q_is_empty (mq)) |
| 2236 | { |
| 2237 | if (!wait_for_time) |
| 2238 | { |
| 2239 | svm_msg_q_unlock (mq); |
| 2240 | return 0; |
| 2241 | } |
| 2242 | else if (wait_for_time < 0) |
| 2243 | { |
| 2244 | svm_msg_q_wait (mq); |
| 2245 | } |
| 2246 | else |
| 2247 | { |
Florin Coras | 60f1fc1 | 2018-08-16 14:57:31 -0700 | [diff] [blame] | 2248 | if (svm_msg_q_timedwait (mq, wait_for_time / 1e3)) |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 2249 | { |
| 2250 | svm_msg_q_unlock (mq); |
| 2251 | return 0; |
| 2252 | } |
| 2253 | } |
| 2254 | } |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 2255 | vcl_mq_dequeue_batch (mq); |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 2256 | svm_msg_q_unlock (mq); |
| 2257 | |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 2258 | for (i = 0; i < vec_len (vcm->mq_msg_vector); i++) |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 2259 | { |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 2260 | msg = vec_elt_at_index (vcm->mq_msg_vector, i); |
| 2261 | e = svm_msg_q_msg_data (mq, msg); |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 2262 | add_event = 0; |
| 2263 | switch (e->event_type) |
| 2264 | { |
| 2265 | case FIFO_EVENT_APP_RX: |
| 2266 | sid = e->fifo->client_session_index; |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 2267 | session = vcl_session_get (sid); |
| 2268 | session_events = session->vep.ev.events; |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 2269 | if (!(EPOLLIN & session->vep.ev.events)) |
| 2270 | break; |
Florin Coras | 60f1fc1 | 2018-08-16 14:57:31 -0700 | [diff] [blame] | 2271 | add_event = 1; |
| 2272 | events[*num_ev].events |= EPOLLIN; |
| 2273 | session_evt_data = session->vep.ev.data.u64; |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 2274 | break; |
| 2275 | case FIFO_EVENT_APP_TX: |
| 2276 | sid = e->fifo->client_session_index; |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 2277 | session = vcl_session_get (sid); |
| 2278 | session_events = session->vep.ev.events; |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 2279 | if (!(EPOLLOUT & session_events)) |
| 2280 | break; |
Florin Coras | 60f1fc1 | 2018-08-16 14:57:31 -0700 | [diff] [blame] | 2281 | add_event = 1; |
| 2282 | events[*num_ev].events |= EPOLLOUT; |
| 2283 | session_evt_data = session->vep.ev.data.u64; |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 2284 | break; |
| 2285 | case SESSION_IO_EVT_CT_TX: |
| 2286 | session = vcl_ct_session_get_from_fifo (e->fifo, 0); |
| 2287 | sid = vcl_session_index (session); |
| 2288 | session_events = session->vep.ev.events; |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 2289 | if (!(EPOLLIN & session->vep.ev.events)) |
| 2290 | break; |
Florin Coras | 60f1fc1 | 2018-08-16 14:57:31 -0700 | [diff] [blame] | 2291 | add_event = 1; |
| 2292 | events[*num_ev].events |= EPOLLIN; |
| 2293 | session_evt_data = session->vep.ev.data.u64; |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 2294 | break; |
| 2295 | case SESSION_IO_EVT_CT_RX: |
| 2296 | session = vcl_ct_session_get_from_fifo (e->fifo, 1); |
| 2297 | sid = vcl_session_index (session); |
| 2298 | session_events = session->vep.ev.events; |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 2299 | if (!(EPOLLOUT & session_events)) |
| 2300 | break; |
Florin Coras | 60f1fc1 | 2018-08-16 14:57:31 -0700 | [diff] [blame] | 2301 | add_event = 1; |
| 2302 | events[*num_ev].events |= EPOLLOUT; |
| 2303 | session_evt_data = session->vep.ev.data.u64; |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 2304 | break; |
| 2305 | case SESSION_CTRL_EVT_ACCEPTED: |
| 2306 | accepted_msg = (session_accepted_msg_t *) e->data; |
| 2307 | handle = accepted_msg->listener_handle; |
| 2308 | session = vppcom_session_table_lookup_listener (handle); |
| 2309 | if (!session) |
| 2310 | { |
| 2311 | clib_warning ("VCL<%d>: ERROR: couldn't find listen session:" |
| 2312 | "listener handle %llx", getpid (), handle); |
| 2313 | break; |
| 2314 | } |
| 2315 | |
| 2316 | clib_fifo_add2 (session->accept_evts_fifo, vcl_msg); |
| 2317 | vcl_msg->accepted_msg = *accepted_msg; |
| 2318 | session_events = session->vep.ev.events; |
| 2319 | if (!(EPOLLIN & session_events)) |
| 2320 | break; |
| 2321 | |
| 2322 | add_event = 1; |
| 2323 | events[*num_ev].events |= EPOLLIN; |
| 2324 | session_evt_data = session->vep.ev.data.u64; |
| 2325 | break; |
| 2326 | case SESSION_CTRL_EVT_CONNECTED: |
| 2327 | connected_msg = (session_connected_msg_t *) e->data; |
| 2328 | vcl_session_connected_handler (connected_msg); |
| 2329 | /* Generate EPOLLOUT because there's no connected event */ |
| 2330 | sid = vcl_session_get_index_from_handle (connected_msg->handle); |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 2331 | session = vcl_session_get (sid); |
| 2332 | session_events = session->vep.ev.events; |
| 2333 | if (EPOLLOUT & session_events) |
| 2334 | { |
| 2335 | add_event = 1; |
| 2336 | events[*num_ev].events |= EPOLLOUT; |
| 2337 | session_evt_data = session->vep.ev.data.u64; |
| 2338 | } |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 2339 | break; |
| 2340 | case SESSION_CTRL_EVT_DISCONNECTED: |
| 2341 | disconnected_msg = (session_disconnected_msg_t *) e->data; |
| 2342 | sid = vcl_session_get_index_from_handle (disconnected_msg->handle); |
Florin Coras | c9fbd66 | 2018-08-24 12:59:56 -0700 | [diff] [blame] | 2343 | if (!(session = vcl_session_get (sid))) |
| 2344 | break; |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 2345 | add_event = 1; |
| 2346 | events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP; |
| 2347 | session_evt_data = session->vep.ev.data.u64; |
| 2348 | session_events = session->vep.ev.events; |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 2349 | break; |
Florin Coras | c9fbd66 | 2018-08-24 12:59:56 -0700 | [diff] [blame] | 2350 | case SESSION_CTRL_EVT_RESET: |
Florin Coras | 6011699 | 2018-08-27 09:52:18 -0700 | [diff] [blame] | 2351 | sid = vcl_session_reset_handler ((session_reset_msg_t *) e->data); |
Florin Coras | c9fbd66 | 2018-08-24 12:59:56 -0700 | [diff] [blame] | 2352 | if (!(session = vcl_session_get (sid))) |
| 2353 | break; |
| 2354 | add_event = 1; |
| 2355 | events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP; |
| 2356 | session_evt_data = session->vep.ev.data.u64; |
| 2357 | session_events = session->vep.ev.events; |
| 2358 | break; |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 2359 | default: |
| 2360 | clib_warning ("unhandled: %u", e->event_type); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 2361 | svm_msg_q_free_msg (mq, msg); |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 2362 | continue; |
| 2363 | } |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 2364 | svm_msg_q_free_msg (mq, msg); |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 2365 | |
| 2366 | if (add_event) |
| 2367 | { |
| 2368 | events[*num_ev].data.u64 = session_evt_data; |
| 2369 | if (EPOLLONESHOT & session_events) |
| 2370 | { |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 2371 | session = vcl_session_get (sid); |
| 2372 | session->vep.ev.events = 0; |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 2373 | } |
| 2374 | *num_ev += 1; |
| 2375 | if (*num_ev == maxevents) |
| 2376 | break; |
| 2377 | } |
| 2378 | } |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 2379 | |
| 2380 | vec_reset_length (vcm->mq_msg_vector); |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 2381 | return *num_ev; |
| 2382 | } |
| 2383 | |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 2384 | static int |
| 2385 | vppcom_epoll_wait_condvar (struct epoll_event *events, int maxevents, |
| 2386 | double wait_for_time) |
| 2387 | { |
| 2388 | vcl_cut_through_registration_t *cr; |
| 2389 | double total_wait = 0, wait_slice; |
| 2390 | u32 num_ev = 0; |
| 2391 | int rv; |
| 2392 | |
| 2393 | wait_for_time = (wait_for_time == -1) ? (double) 10e9 : wait_for_time; |
| 2394 | wait_slice = vcm->cut_through_registrations ? 10e-6 : wait_for_time; |
| 2395 | |
| 2396 | do |
| 2397 | { |
| 2398 | /* *INDENT-OFF* */ |
| 2399 | pool_foreach (cr, vcm->cut_through_registrations, ({ |
| 2400 | vcl_epoll_wait_handle_mq (cr->mq, events, maxevents, 0, &num_ev); |
| 2401 | })); |
| 2402 | /* *INDENT-ON* */ |
| 2403 | |
| 2404 | rv = vcl_epoll_wait_handle_mq (vcm->app_event_queue, events, maxevents, |
| 2405 | num_ev ? 0 : wait_slice, &num_ev); |
| 2406 | if (rv) |
| 2407 | total_wait += wait_slice; |
| 2408 | if (num_ev) |
| 2409 | return num_ev; |
| 2410 | } |
| 2411 | while (total_wait < wait_for_time); |
| 2412 | return (int) num_ev; |
| 2413 | } |
| 2414 | |
| 2415 | static int |
| 2416 | vppcom_epoll_wait_eventfd (struct epoll_event *events, int maxevents, |
| 2417 | double wait_for_time) |
| 2418 | { |
| 2419 | vcl_mq_evt_conn_t *mqc; |
| 2420 | int __clib_unused n_read; |
| 2421 | int n_mq_evts, i; |
| 2422 | u32 n_evts = 0; |
| 2423 | u64 buf; |
| 2424 | |
| 2425 | vec_validate (vcm->mq_events, pool_elts (vcm->mq_evt_conns)); |
| 2426 | n_mq_evts = epoll_wait (vcm->mqs_epfd, vcm->mq_events, |
| 2427 | vec_len (vcm->mq_events), wait_for_time); |
| 2428 | for (i = 0; i < n_mq_evts; i++) |
| 2429 | { |
| 2430 | mqc = vcl_mq_evt_conn_get (vcm->mq_events[i].data.u32); |
| 2431 | n_read = read (mqc->mq_fd, &buf, sizeof (buf)); |
| 2432 | vcl_epoll_wait_handle_mq (mqc->mq, events, maxevents, 0, &n_evts); |
| 2433 | } |
| 2434 | |
| 2435 | return (int) n_evts; |
| 2436 | } |
| 2437 | |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 2438 | int |
| 2439 | vppcom_epoll_wait (uint32_t vep_idx, struct epoll_event *events, |
| 2440 | int maxevents, double wait_for_time) |
| 2441 | { |
Florin Coras | 7e12d94 | 2018-06-27 14:32:43 -0700 | [diff] [blame] | 2442 | vcl_session_t *vep_session; |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 2443 | |
| 2444 | if (PREDICT_FALSE (maxevents <= 0)) |
| 2445 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2446 | clib_warning ("VCL<%d>: ERROR: Invalid maxevents (%d)!", |
Dave Wallace | 4878cbe | 2017-11-21 03:45:09 -0500 | [diff] [blame] | 2447 | getpid (), maxevents); |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 2448 | return VPPCOM_EINVAL; |
| 2449 | } |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 2450 | |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 2451 | vep_session = vcl_session_get (vep_idx); |
| 2452 | if (PREDICT_FALSE (!vep_session->is_vep)) |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 2453 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2454 | clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!", |
Dave Wallace | 4878cbe | 2017-11-21 03:45:09 -0500 | [diff] [blame] | 2455 | getpid (), vep_idx); |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 2456 | return VPPCOM_EINVAL; |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 2457 | } |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 2458 | |
| 2459 | memset (events, 0, sizeof (*events) * maxevents); |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 2460 | |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 2461 | if (vcm->cfg.use_mq_eventfd) |
| 2462 | return vppcom_epoll_wait_eventfd (events, maxevents, wait_for_time); |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 2463 | |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 2464 | return vppcom_epoll_wait_condvar (events, maxevents, wait_for_time); |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 2465 | } |
| 2466 | |
Dave Wallace | 35830af | 2017-10-09 01:43:42 -0400 | [diff] [blame] | 2467 | int |
| 2468 | vppcom_session_attr (uint32_t session_index, uint32_t op, |
| 2469 | void *buffer, uint32_t * buflen) |
| 2470 | { |
Florin Coras | 7e12d94 | 2018-06-27 14:32:43 -0700 | [diff] [blame] | 2471 | vcl_session_t *session; |
Dave Wallace | 35830af | 2017-10-09 01:43:42 -0400 | [diff] [blame] | 2472 | int rv = VPPCOM_OK; |
| 2473 | u32 *flags = buffer; |
Steven | 2199aab | 2017-10-15 20:18:47 -0700 | [diff] [blame] | 2474 | vppcom_endpt_t *ep = buffer; |
Dave Wallace | 35830af | 2017-10-09 01:43:42 -0400 | [diff] [blame] | 2475 | |
Florin Coras | 070453d | 2018-08-24 17:04:27 -0700 | [diff] [blame] | 2476 | session = vcl_session_get (session_index); |
| 2477 | if (!session) |
| 2478 | return VPPCOM_EBADFD; |
Keith Burns (alagalah) | 9b79377 | 2018-02-16 08:20:56 -0800 | [diff] [blame] | 2479 | |
Dave Wallace | 35830af | 2017-10-09 01:43:42 -0400 | [diff] [blame] | 2480 | switch (op) |
| 2481 | { |
| 2482 | case VPPCOM_ATTR_GET_NREAD: |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 2483 | rv = vppcom_session_read_ready (session); |
Florin Coras | 0d427d8 | 2018-06-27 03:24:07 -0700 | [diff] [blame] | 2484 | VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_NREAD: sid %u, nread = %d", |
| 2485 | getpid (), rv); |
Dave Wallace | 35830af | 2017-10-09 01:43:42 -0400 | [diff] [blame] | 2486 | break; |
| 2487 | |
Dave Wallace | 227867f | 2017-11-13 21:21:53 -0500 | [diff] [blame] | 2488 | case VPPCOM_ATTR_GET_NWRITE: |
| 2489 | rv = vppcom_session_write_ready (session, session_index); |
Florin Coras | 0d427d8 | 2018-06-27 03:24:07 -0700 | [diff] [blame] | 2490 | VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_NWRITE: sid %u, nwrite = %d", |
| 2491 | getpid (), session_index, rv); |
Dave Wallace | 35830af | 2017-10-09 01:43:42 -0400 | [diff] [blame] | 2492 | break; |
| 2493 | |
| 2494 | case VPPCOM_ATTR_GET_FLAGS: |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2495 | if (PREDICT_TRUE (buffer && buflen && (*buflen >= sizeof (*flags)))) |
Dave Wallace | 35830af | 2017-10-09 01:43:42 -0400 | [diff] [blame] | 2496 | { |
Keith Burns (alagalah) | 9b79377 | 2018-02-16 08:20:56 -0800 | [diff] [blame] | 2497 | *flags = O_RDWR | (VCL_SESS_ATTR_TEST (session->attr, |
| 2498 | VCL_SESS_ATTR_NONBLOCK)); |
Dave Wallace | 35830af | 2017-10-09 01:43:42 -0400 | [diff] [blame] | 2499 | *buflen = sizeof (*flags); |
Florin Coras | 0d427d8 | 2018-06-27 03:24:07 -0700 | [diff] [blame] | 2500 | VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_FLAGS: sid %u, flags = 0x%08x, " |
| 2501 | "is_nonblocking = %u", getpid (), |
| 2502 | session_index, *flags, |
| 2503 | VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK)); |
Dave Wallace | 35830af | 2017-10-09 01:43:42 -0400 | [diff] [blame] | 2504 | } |
| 2505 | else |
| 2506 | rv = VPPCOM_EINVAL; |
| 2507 | break; |
| 2508 | |
| 2509 | case VPPCOM_ATTR_SET_FLAGS: |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2510 | if (PREDICT_TRUE (buffer && buflen && (*buflen == sizeof (*flags)))) |
Dave Wallace | 35830af | 2017-10-09 01:43:42 -0400 | [diff] [blame] | 2511 | { |
Keith Burns (alagalah) | 9b79377 | 2018-02-16 08:20:56 -0800 | [diff] [blame] | 2512 | if (*flags & O_NONBLOCK) |
| 2513 | VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK); |
| 2514 | else |
| 2515 | VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_NONBLOCK); |
| 2516 | |
Florin Coras | 0d427d8 | 2018-06-27 03:24:07 -0700 | [diff] [blame] | 2517 | VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_FLAGS: sid %u, flags = 0x%08x," |
| 2518 | " is_nonblocking = %u", |
| 2519 | getpid (), session_index, *flags, |
| 2520 | VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK)); |
Dave Wallace | 35830af | 2017-10-09 01:43:42 -0400 | [diff] [blame] | 2521 | } |
| 2522 | else |
| 2523 | rv = VPPCOM_EINVAL; |
| 2524 | break; |
| 2525 | |
| 2526 | case VPPCOM_ATTR_GET_PEER_ADDR: |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2527 | if (PREDICT_TRUE (buffer && buflen && |
| 2528 | (*buflen >= sizeof (*ep)) && ep->ip)) |
Dave Wallace | 35830af | 2017-10-09 01:43:42 -0400 | [diff] [blame] | 2529 | { |
Florin Coras | 7e12d94 | 2018-06-27 14:32:43 -0700 | [diff] [blame] | 2530 | ep->is_ip4 = session->transport.is_ip4; |
| 2531 | ep->port = session->transport.rmt_port; |
| 2532 | if (session->transport.is_ip4) |
| 2533 | clib_memcpy (ep->ip, &session->transport.rmt_ip.ip4, |
Steven | 2199aab | 2017-10-15 20:18:47 -0700 | [diff] [blame] | 2534 | sizeof (ip4_address_t)); |
| 2535 | else |
Florin Coras | 7e12d94 | 2018-06-27 14:32:43 -0700 | [diff] [blame] | 2536 | clib_memcpy (ep->ip, &session->transport.rmt_ip.ip6, |
Steven | 2199aab | 2017-10-15 20:18:47 -0700 | [diff] [blame] | 2537 | sizeof (ip6_address_t)); |
| 2538 | *buflen = sizeof (*ep); |
Florin Coras | 0d427d8 | 2018-06-27 03:24:07 -0700 | [diff] [blame] | 2539 | VDBG (1, "VCL<%d>: VPPCOM_ATTR_GET_PEER_ADDR: sid %u, is_ip4 = %u, " |
| 2540 | "addr = %U, port %u", getpid (), |
| 2541 | session_index, ep->is_ip4, format_ip46_address, |
Florin Coras | 7e12d94 | 2018-06-27 14:32:43 -0700 | [diff] [blame] | 2542 | &session->transport.rmt_ip, |
Florin Coras | 0d427d8 | 2018-06-27 03:24:07 -0700 | [diff] [blame] | 2543 | ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6, |
| 2544 | clib_net_to_host_u16 (ep->port)); |
Dave Wallace | 35830af | 2017-10-09 01:43:42 -0400 | [diff] [blame] | 2545 | } |
| 2546 | else |
| 2547 | rv = VPPCOM_EINVAL; |
| 2548 | break; |
| 2549 | |
| 2550 | case VPPCOM_ATTR_GET_LCL_ADDR: |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2551 | if (PREDICT_TRUE (buffer && buflen && |
| 2552 | (*buflen >= sizeof (*ep)) && ep->ip)) |
Dave Wallace | 35830af | 2017-10-09 01:43:42 -0400 | [diff] [blame] | 2553 | { |
Florin Coras | 7e12d94 | 2018-06-27 14:32:43 -0700 | [diff] [blame] | 2554 | ep->is_ip4 = session->transport.is_ip4; |
| 2555 | ep->port = session->transport.lcl_port; |
| 2556 | if (session->transport.is_ip4) |
| 2557 | clib_memcpy (ep->ip, &session->transport.lcl_ip.ip4, |
Steven | 2199aab | 2017-10-15 20:18:47 -0700 | [diff] [blame] | 2558 | sizeof (ip4_address_t)); |
| 2559 | else |
Florin Coras | 7e12d94 | 2018-06-27 14:32:43 -0700 | [diff] [blame] | 2560 | clib_memcpy (ep->ip, &session->transport.lcl_ip.ip6, |
Steven | 2199aab | 2017-10-15 20:18:47 -0700 | [diff] [blame] | 2561 | sizeof (ip6_address_t)); |
| 2562 | *buflen = sizeof (*ep); |
Florin Coras | 0d427d8 | 2018-06-27 03:24:07 -0700 | [diff] [blame] | 2563 | VDBG (1, "VCL<%d>: VPPCOM_ATTR_GET_LCL_ADDR: sid %u, is_ip4 = %u," |
| 2564 | " addr = %U port %d", getpid (), |
| 2565 | session_index, ep->is_ip4, format_ip46_address, |
Florin Coras | 7e12d94 | 2018-06-27 14:32:43 -0700 | [diff] [blame] | 2566 | &session->transport.lcl_ip, |
Florin Coras | 0d427d8 | 2018-06-27 03:24:07 -0700 | [diff] [blame] | 2567 | ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6, |
| 2568 | clib_net_to_host_u16 (ep->port)); |
Dave Wallace | 35830af | 2017-10-09 01:43:42 -0400 | [diff] [blame] | 2569 | } |
| 2570 | else |
| 2571 | rv = VPPCOM_EINVAL; |
| 2572 | break; |
Steven | b5a1160 | 2017-10-11 09:59:30 -0700 | [diff] [blame] | 2573 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2574 | case VPPCOM_ATTR_GET_LIBC_EPFD: |
| 2575 | rv = session->libc_epfd; |
Florin Coras | 0d427d8 | 2018-06-27 03:24:07 -0700 | [diff] [blame] | 2576 | VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd %d", |
| 2577 | getpid (), rv); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2578 | break; |
| 2579 | |
| 2580 | case VPPCOM_ATTR_SET_LIBC_EPFD: |
| 2581 | if (PREDICT_TRUE (buffer && buflen && |
| 2582 | (*buflen == sizeof (session->libc_epfd)))) |
| 2583 | { |
| 2584 | session->libc_epfd = *(int *) buffer; |
| 2585 | *buflen = sizeof (session->libc_epfd); |
| 2586 | |
Florin Coras | 0d427d8 | 2018-06-27 03:24:07 -0700 | [diff] [blame] | 2587 | VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd %d, " |
| 2588 | "buflen %d", getpid (), session->libc_epfd, *buflen); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2589 | } |
| 2590 | else |
| 2591 | rv = VPPCOM_EINVAL; |
| 2592 | break; |
| 2593 | |
| 2594 | case VPPCOM_ATTR_GET_PROTOCOL: |
| 2595 | if (buffer && buflen && (*buflen >= sizeof (int))) |
| 2596 | { |
Florin Coras | 7e12d94 | 2018-06-27 14:32:43 -0700 | [diff] [blame] | 2597 | *(int *) buffer = session->session_type; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2598 | *buflen = sizeof (int); |
| 2599 | |
Florin Coras | 0d427d8 | 2018-06-27 03:24:07 -0700 | [diff] [blame] | 2600 | VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_PROTOCOL: %d (%s), buflen %d", |
| 2601 | getpid (), *(int *) buffer, *(int *) buffer ? "UDP" : "TCP", |
| 2602 | *buflen); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2603 | } |
| 2604 | else |
| 2605 | rv = VPPCOM_EINVAL; |
| 2606 | break; |
| 2607 | |
| 2608 | case VPPCOM_ATTR_GET_LISTEN: |
| 2609 | if (buffer && buflen && (*buflen >= sizeof (int))) |
| 2610 | { |
| 2611 | *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr, |
| 2612 | VCL_SESS_ATTR_LISTEN); |
| 2613 | *buflen = sizeof (int); |
| 2614 | |
Florin Coras | 0d427d8 | 2018-06-27 03:24:07 -0700 | [diff] [blame] | 2615 | VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_LISTEN: %d, buflen %d", |
| 2616 | getpid (), *(int *) buffer, *buflen); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2617 | } |
| 2618 | else |
| 2619 | rv = VPPCOM_EINVAL; |
| 2620 | break; |
| 2621 | |
| 2622 | case VPPCOM_ATTR_GET_ERROR: |
| 2623 | if (buffer && buflen && (*buflen >= sizeof (int))) |
| 2624 | { |
| 2625 | *(int *) buffer = 0; |
| 2626 | *buflen = sizeof (int); |
| 2627 | |
Florin Coras | 0d427d8 | 2018-06-27 03:24:07 -0700 | [diff] [blame] | 2628 | VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_ERROR: %d, buflen %d, #VPP-TBD#", |
| 2629 | getpid (), *(int *) buffer, *buflen); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2630 | } |
| 2631 | else |
| 2632 | rv = VPPCOM_EINVAL; |
| 2633 | break; |
| 2634 | |
| 2635 | case VPPCOM_ATTR_GET_TX_FIFO_LEN: |
| 2636 | if (buffer && buflen && (*buflen >= sizeof (u32))) |
| 2637 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2638 | |
| 2639 | /* VPP-TBD */ |
| 2640 | *(size_t *) buffer = (session->sndbuf_size ? session->sndbuf_size : |
Keith Burns (alagalah) | 9b79377 | 2018-02-16 08:20:56 -0800 | [diff] [blame] | 2641 | session->tx_fifo ? session->tx_fifo->nitems : |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2642 | vcm->cfg.tx_fifo_size); |
| 2643 | *buflen = sizeof (u32); |
| 2644 | |
Florin Coras | 0d427d8 | 2018-06-27 03:24:07 -0700 | [diff] [blame] | 2645 | VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TX_FIFO_LEN: %u (0x%x), " |
| 2646 | "buflen %d, #VPP-TBD#", getpid (), |
| 2647 | *(size_t *) buffer, *(size_t *) buffer, *buflen); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2648 | } |
| 2649 | else |
| 2650 | rv = VPPCOM_EINVAL; |
| 2651 | break; |
| 2652 | |
| 2653 | case VPPCOM_ATTR_SET_TX_FIFO_LEN: |
| 2654 | if (buffer && buflen && (*buflen == sizeof (u32))) |
| 2655 | { |
| 2656 | /* VPP-TBD */ |
| 2657 | session->sndbuf_size = *(u32 *) buffer; |
Florin Coras | 0d427d8 | 2018-06-27 03:24:07 -0700 | [diff] [blame] | 2658 | VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TX_FIFO_LEN: %u (0x%x), " |
| 2659 | "buflen %d, #VPP-TBD#", getpid (), |
| 2660 | session->sndbuf_size, session->sndbuf_size, *buflen); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2661 | } |
| 2662 | else |
| 2663 | rv = VPPCOM_EINVAL; |
| 2664 | break; |
| 2665 | |
| 2666 | case VPPCOM_ATTR_GET_RX_FIFO_LEN: |
| 2667 | if (buffer && buflen && (*buflen >= sizeof (u32))) |
| 2668 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2669 | |
| 2670 | /* VPP-TBD */ |
| 2671 | *(size_t *) buffer = (session->rcvbuf_size ? session->rcvbuf_size : |
Keith Burns (alagalah) | 9b79377 | 2018-02-16 08:20:56 -0800 | [diff] [blame] | 2672 | session->rx_fifo ? session->rx_fifo->nitems : |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2673 | vcm->cfg.rx_fifo_size); |
| 2674 | *buflen = sizeof (u32); |
| 2675 | |
Florin Coras | 0d427d8 | 2018-06-27 03:24:07 -0700 | [diff] [blame] | 2676 | VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_RX_FIFO_LEN: %u (0x%x), " |
| 2677 | "buflen %d, #VPP-TBD#", getpid (), |
| 2678 | *(size_t *) buffer, *(size_t *) buffer, *buflen); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2679 | } |
| 2680 | else |
| 2681 | rv = VPPCOM_EINVAL; |
| 2682 | break; |
| 2683 | |
| 2684 | case VPPCOM_ATTR_SET_RX_FIFO_LEN: |
| 2685 | if (buffer && buflen && (*buflen == sizeof (u32))) |
| 2686 | { |
| 2687 | /* VPP-TBD */ |
| 2688 | session->rcvbuf_size = *(u32 *) buffer; |
Florin Coras | 0d427d8 | 2018-06-27 03:24:07 -0700 | [diff] [blame] | 2689 | VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_RX_FIFO_LEN: %u (0x%x), " |
| 2690 | "buflen %d, #VPP-TBD#", getpid (), |
| 2691 | session->sndbuf_size, session->sndbuf_size, *buflen); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2692 | } |
| 2693 | else |
| 2694 | rv = VPPCOM_EINVAL; |
| 2695 | break; |
| 2696 | |
| 2697 | case VPPCOM_ATTR_GET_REUSEADDR: |
| 2698 | if (buffer && buflen && (*buflen >= sizeof (int))) |
| 2699 | { |
| 2700 | /* VPP-TBD */ |
| 2701 | *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr, |
| 2702 | VCL_SESS_ATTR_REUSEADDR); |
| 2703 | *buflen = sizeof (int); |
| 2704 | |
Florin Coras | 0d427d8 | 2018-06-27 03:24:07 -0700 | [diff] [blame] | 2705 | VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_REUSEADDR: %d, " |
| 2706 | "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer, *buflen); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2707 | } |
| 2708 | else |
| 2709 | rv = VPPCOM_EINVAL; |
| 2710 | break; |
| 2711 | |
Steven | b5a1160 | 2017-10-11 09:59:30 -0700 | [diff] [blame] | 2712 | case VPPCOM_ATTR_SET_REUSEADDR: |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2713 | if (buffer && buflen && (*buflen == sizeof (int)) && |
| 2714 | !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN)) |
| 2715 | { |
| 2716 | /* VPP-TBD */ |
| 2717 | if (*(int *) buffer) |
| 2718 | VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEADDR); |
| 2719 | else |
| 2720 | VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEADDR); |
| 2721 | |
Florin Coras | 0d427d8 | 2018-06-27 03:24:07 -0700 | [diff] [blame] | 2722 | VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_REUSEADDR: %d, buflen %d," |
| 2723 | " #VPP-TBD#", getpid (), |
| 2724 | VCL_SESS_ATTR_TEST (session->attr, |
| 2725 | VCL_SESS_ATTR_REUSEADDR), *buflen); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2726 | } |
| 2727 | else |
| 2728 | rv = VPPCOM_EINVAL; |
| 2729 | break; |
| 2730 | |
| 2731 | case VPPCOM_ATTR_GET_REUSEPORT: |
| 2732 | if (buffer && buflen && (*buflen >= sizeof (int))) |
| 2733 | { |
| 2734 | /* VPP-TBD */ |
| 2735 | *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr, |
| 2736 | VCL_SESS_ATTR_REUSEPORT); |
| 2737 | *buflen = sizeof (int); |
| 2738 | |
Florin Coras | 0d427d8 | 2018-06-27 03:24:07 -0700 | [diff] [blame] | 2739 | VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_REUSEPORT: %d, buflen %d," |
| 2740 | " #VPP-TBD#", getpid (), *(int *) buffer, *buflen); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2741 | } |
| 2742 | else |
| 2743 | rv = VPPCOM_EINVAL; |
| 2744 | break; |
| 2745 | |
| 2746 | case VPPCOM_ATTR_SET_REUSEPORT: |
| 2747 | if (buffer && buflen && (*buflen == sizeof (int)) && |
| 2748 | !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN)) |
| 2749 | { |
| 2750 | /* VPP-TBD */ |
| 2751 | if (*(int *) buffer) |
| 2752 | VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEPORT); |
| 2753 | else |
| 2754 | VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEPORT); |
| 2755 | |
Florin Coras | 0d427d8 | 2018-06-27 03:24:07 -0700 | [diff] [blame] | 2756 | VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_REUSEPORT: %d, buflen %d," |
| 2757 | " #VPP-TBD#", getpid (), |
| 2758 | VCL_SESS_ATTR_TEST (session->attr, |
| 2759 | VCL_SESS_ATTR_REUSEPORT), *buflen); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2760 | } |
| 2761 | else |
| 2762 | rv = VPPCOM_EINVAL; |
| 2763 | break; |
| 2764 | |
| 2765 | case VPPCOM_ATTR_GET_BROADCAST: |
| 2766 | if (buffer && buflen && (*buflen >= sizeof (int))) |
| 2767 | { |
| 2768 | /* VPP-TBD */ |
| 2769 | *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr, |
| 2770 | VCL_SESS_ATTR_BROADCAST); |
| 2771 | *buflen = sizeof (int); |
| 2772 | |
Florin Coras | 0d427d8 | 2018-06-27 03:24:07 -0700 | [diff] [blame] | 2773 | VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_BROADCAST: %d, buflen %d," |
| 2774 | " #VPP-TBD#", getpid (), *(int *) buffer, *buflen); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2775 | } |
| 2776 | else |
| 2777 | rv = VPPCOM_EINVAL; |
Steven | b5a1160 | 2017-10-11 09:59:30 -0700 | [diff] [blame] | 2778 | break; |
| 2779 | |
| 2780 | case VPPCOM_ATTR_SET_BROADCAST: |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2781 | if (buffer && buflen && (*buflen == sizeof (int))) |
| 2782 | { |
| 2783 | /* VPP-TBD */ |
| 2784 | if (*(int *) buffer) |
| 2785 | VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_BROADCAST); |
| 2786 | else |
| 2787 | VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_BROADCAST); |
| 2788 | |
Florin Coras | 0d427d8 | 2018-06-27 03:24:07 -0700 | [diff] [blame] | 2789 | VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_BROADCAST: %d, buflen %d, " |
| 2790 | "#VPP-TBD#", getpid (), |
| 2791 | VCL_SESS_ATTR_TEST (session->attr, |
| 2792 | VCL_SESS_ATTR_BROADCAST), *buflen); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2793 | } |
| 2794 | else |
| 2795 | rv = VPPCOM_EINVAL; |
| 2796 | break; |
| 2797 | |
| 2798 | case VPPCOM_ATTR_GET_V6ONLY: |
| 2799 | if (buffer && buflen && (*buflen >= sizeof (int))) |
| 2800 | { |
| 2801 | /* VPP-TBD */ |
| 2802 | *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr, |
| 2803 | VCL_SESS_ATTR_V6ONLY); |
| 2804 | *buflen = sizeof (int); |
| 2805 | |
Florin Coras | 0d427d8 | 2018-06-27 03:24:07 -0700 | [diff] [blame] | 2806 | VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_V6ONLY: %d, buflen %d, " |
| 2807 | "#VPP-TBD#", getpid (), *(int *) buffer, *buflen); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2808 | } |
| 2809 | else |
| 2810 | rv = VPPCOM_EINVAL; |
Steven | b5a1160 | 2017-10-11 09:59:30 -0700 | [diff] [blame] | 2811 | break; |
| 2812 | |
| 2813 | case VPPCOM_ATTR_SET_V6ONLY: |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2814 | if (buffer && buflen && (*buflen == sizeof (int))) |
| 2815 | { |
| 2816 | /* VPP-TBD */ |
| 2817 | if (*(int *) buffer) |
| 2818 | VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_V6ONLY); |
| 2819 | else |
| 2820 | VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_V6ONLY); |
| 2821 | |
Florin Coras | 0d427d8 | 2018-06-27 03:24:07 -0700 | [diff] [blame] | 2822 | VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_V6ONLY: %d, buflen %d, " |
| 2823 | "#VPP-TBD#", getpid (), |
| 2824 | VCL_SESS_ATTR_TEST (session->attr, |
| 2825 | VCL_SESS_ATTR_V6ONLY), *buflen); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2826 | } |
| 2827 | else |
| 2828 | rv = VPPCOM_EINVAL; |
| 2829 | break; |
| 2830 | |
| 2831 | case VPPCOM_ATTR_GET_KEEPALIVE: |
| 2832 | if (buffer && buflen && (*buflen >= sizeof (int))) |
| 2833 | { |
| 2834 | /* VPP-TBD */ |
| 2835 | *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr, |
| 2836 | VCL_SESS_ATTR_KEEPALIVE); |
| 2837 | *buflen = sizeof (int); |
| 2838 | |
Florin Coras | 0d427d8 | 2018-06-27 03:24:07 -0700 | [diff] [blame] | 2839 | VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_KEEPALIVE: %d, buflen %d, " |
| 2840 | "#VPP-TBD#", getpid (), *(int *) buffer, *buflen); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2841 | } |
| 2842 | else |
| 2843 | rv = VPPCOM_EINVAL; |
Steven | b5a1160 | 2017-10-11 09:59:30 -0700 | [diff] [blame] | 2844 | break; |
Steven | bccd339 | 2017-10-12 20:42:21 -0700 | [diff] [blame] | 2845 | |
| 2846 | case VPPCOM_ATTR_SET_KEEPALIVE: |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2847 | if (buffer && buflen && (*buflen == sizeof (int))) |
| 2848 | { |
| 2849 | /* VPP-TBD */ |
| 2850 | if (*(int *) buffer) |
| 2851 | VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_KEEPALIVE); |
| 2852 | else |
| 2853 | VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_KEEPALIVE); |
| 2854 | |
Florin Coras | 0d427d8 | 2018-06-27 03:24:07 -0700 | [diff] [blame] | 2855 | VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_KEEPALIVE: %d, buflen %d, " |
| 2856 | "#VPP-TBD#", getpid (), |
| 2857 | VCL_SESS_ATTR_TEST (session->attr, |
| 2858 | VCL_SESS_ATTR_KEEPALIVE), *buflen); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2859 | } |
| 2860 | else |
| 2861 | rv = VPPCOM_EINVAL; |
| 2862 | break; |
| 2863 | |
| 2864 | case VPPCOM_ATTR_GET_TCP_NODELAY: |
| 2865 | if (buffer && buflen && (*buflen >= sizeof (int))) |
| 2866 | { |
| 2867 | /* VPP-TBD */ |
| 2868 | *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr, |
| 2869 | VCL_SESS_ATTR_TCP_NODELAY); |
| 2870 | *buflen = sizeof (int); |
| 2871 | |
Florin Coras | 0d427d8 | 2018-06-27 03:24:07 -0700 | [diff] [blame] | 2872 | VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_NODELAY: %d, buflen %d, " |
| 2873 | "#VPP-TBD#", getpid (), *(int *) buffer, *buflen); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2874 | } |
| 2875 | else |
| 2876 | rv = VPPCOM_EINVAL; |
| 2877 | break; |
| 2878 | |
| 2879 | case VPPCOM_ATTR_SET_TCP_NODELAY: |
| 2880 | if (buffer && buflen && (*buflen == sizeof (int))) |
| 2881 | { |
| 2882 | /* VPP-TBD */ |
| 2883 | if (*(int *) buffer) |
| 2884 | VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_NODELAY); |
| 2885 | else |
| 2886 | VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_NODELAY); |
| 2887 | |
Florin Coras | 0d427d8 | 2018-06-27 03:24:07 -0700 | [diff] [blame] | 2888 | VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_NODELAY: %d, buflen %d, " |
| 2889 | "#VPP-TBD#", getpid (), |
| 2890 | VCL_SESS_ATTR_TEST (session->attr, |
| 2891 | VCL_SESS_ATTR_TCP_NODELAY), *buflen); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2892 | } |
| 2893 | else |
| 2894 | rv = VPPCOM_EINVAL; |
| 2895 | break; |
| 2896 | |
| 2897 | case VPPCOM_ATTR_GET_TCP_KEEPIDLE: |
| 2898 | if (buffer && buflen && (*buflen >= sizeof (int))) |
| 2899 | { |
| 2900 | /* VPP-TBD */ |
| 2901 | *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr, |
| 2902 | VCL_SESS_ATTR_TCP_KEEPIDLE); |
| 2903 | *buflen = sizeof (int); |
| 2904 | |
Florin Coras | 0d427d8 | 2018-06-27 03:24:07 -0700 | [diff] [blame] | 2905 | VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d, buflen %d, " |
| 2906 | "#VPP-TBD#", getpid (), *(int *) buffer, *buflen); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2907 | } |
| 2908 | else |
| 2909 | rv = VPPCOM_EINVAL; |
Steven | bccd339 | 2017-10-12 20:42:21 -0700 | [diff] [blame] | 2910 | break; |
| 2911 | |
| 2912 | case VPPCOM_ATTR_SET_TCP_KEEPIDLE: |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2913 | if (buffer && buflen && (*buflen == sizeof (int))) |
| 2914 | { |
| 2915 | /* VPP-TBD */ |
| 2916 | if (*(int *) buffer) |
| 2917 | VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE); |
| 2918 | else |
| 2919 | VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE); |
| 2920 | |
Florin Coras | 0d427d8 | 2018-06-27 03:24:07 -0700 | [diff] [blame] | 2921 | VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d, buflen %d, " |
| 2922 | "#VPP-TBD#", getpid (), |
| 2923 | VCL_SESS_ATTR_TEST (session->attr, |
| 2924 | VCL_SESS_ATTR_TCP_KEEPIDLE), *buflen); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2925 | } |
| 2926 | else |
| 2927 | rv = VPPCOM_EINVAL; |
| 2928 | break; |
| 2929 | |
| 2930 | case VPPCOM_ATTR_GET_TCP_KEEPINTVL: |
| 2931 | if (buffer && buflen && (*buflen >= sizeof (int))) |
| 2932 | { |
| 2933 | /* VPP-TBD */ |
| 2934 | *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr, |
| 2935 | VCL_SESS_ATTR_TCP_KEEPINTVL); |
| 2936 | *buflen = sizeof (int); |
| 2937 | |
Florin Coras | 0d427d8 | 2018-06-27 03:24:07 -0700 | [diff] [blame] | 2938 | VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_KEEPINTVL: %d, buflen %d, " |
| 2939 | "#VPP-TBD#", getpid (), *(int *) buffer, *buflen); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2940 | } |
| 2941 | else |
| 2942 | rv = VPPCOM_EINVAL; |
Steven | bccd339 | 2017-10-12 20:42:21 -0700 | [diff] [blame] | 2943 | break; |
| 2944 | |
| 2945 | case VPPCOM_ATTR_SET_TCP_KEEPINTVL: |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2946 | if (buffer && buflen && (*buflen == sizeof (int))) |
| 2947 | { |
| 2948 | /* VPP-TBD */ |
| 2949 | if (*(int *) buffer) |
| 2950 | VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL); |
| 2951 | else |
| 2952 | VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL); |
| 2953 | |
Florin Coras | 0d427d8 | 2018-06-27 03:24:07 -0700 | [diff] [blame] | 2954 | VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d, buflen %d, " |
| 2955 | "#VPP-TBD#", getpid (), |
| 2956 | VCL_SESS_ATTR_TEST (session->attr, |
| 2957 | VCL_SESS_ATTR_TCP_KEEPINTVL), *buflen); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2958 | } |
| 2959 | else |
| 2960 | rv = VPPCOM_EINVAL; |
| 2961 | break; |
| 2962 | |
| 2963 | case VPPCOM_ATTR_GET_TCP_USER_MSS: |
| 2964 | if (buffer && buflen && (*buflen >= sizeof (u32))) |
| 2965 | { |
| 2966 | /* VPP-TBD */ |
| 2967 | *(u32 *) buffer = session->user_mss; |
| 2968 | *buflen = sizeof (int); |
| 2969 | |
Florin Coras | 0d427d8 | 2018-06-27 03:24:07 -0700 | [diff] [blame] | 2970 | VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_USER_MSS: %d, buflen %d," |
| 2971 | " #VPP-TBD#", getpid (), *(int *) buffer, *buflen); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2972 | } |
| 2973 | else |
| 2974 | rv = VPPCOM_EINVAL; |
| 2975 | break; |
| 2976 | |
| 2977 | case VPPCOM_ATTR_SET_TCP_USER_MSS: |
| 2978 | if (buffer && buflen && (*buflen == sizeof (u32))) |
| 2979 | { |
| 2980 | /* VPP-TBD */ |
| 2981 | session->user_mss = *(u32 *) buffer; |
| 2982 | |
Florin Coras | 0d427d8 | 2018-06-27 03:24:07 -0700 | [diff] [blame] | 2983 | VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_USER_MSS: %u, buflen %d, " |
| 2984 | "#VPP-TBD#", getpid (), session->user_mss, *buflen); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2985 | } |
| 2986 | else |
| 2987 | rv = VPPCOM_EINVAL; |
Steven | bccd339 | 2017-10-12 20:42:21 -0700 | [diff] [blame] | 2988 | break; |
Dave Wallace | e22aa74 | 2017-10-20 12:30:38 -0400 | [diff] [blame] | 2989 | |
| 2990 | default: |
| 2991 | rv = VPPCOM_EINVAL; |
| 2992 | break; |
Dave Wallace | 35830af | 2017-10-09 01:43:42 -0400 | [diff] [blame] | 2993 | } |
| 2994 | |
Dave Wallace | 35830af | 2017-10-09 01:43:42 -0400 | [diff] [blame] | 2995 | return rv; |
| 2996 | } |
| 2997 | |
Steven | ac1f96d | 2017-10-24 16:03:58 -0700 | [diff] [blame] | 2998 | int |
| 2999 | vppcom_session_recvfrom (uint32_t session_index, void *buffer, |
| 3000 | uint32_t buflen, int flags, vppcom_endpt_t * ep) |
| 3001 | { |
Steven | ac1f96d | 2017-10-24 16:03:58 -0700 | [diff] [blame] | 3002 | int rv = VPPCOM_OK; |
Florin Coras | 7e12d94 | 2018-06-27 14:32:43 -0700 | [diff] [blame] | 3003 | vcl_session_t *session = 0; |
Steven | ac1f96d | 2017-10-24 16:03:58 -0700 | [diff] [blame] | 3004 | |
| 3005 | if (ep) |
| 3006 | { |
Florin Coras | 070453d | 2018-08-24 17:04:27 -0700 | [diff] [blame] | 3007 | session = vcl_session_get (session_index); |
| 3008 | if (PREDICT_FALSE (!session)) |
Steven | ac1f96d | 2017-10-24 16:03:58 -0700 | [diff] [blame] | 3009 | { |
Florin Coras | 0d427d8 | 2018-06-27 03:24:07 -0700 | [diff] [blame] | 3010 | VDBG (0, "VCL<%d>: invalid session, sid (%u) has been closed!", |
| 3011 | getpid (), session_index); |
Florin Coras | 460dce6 | 2018-07-27 05:45:06 -0700 | [diff] [blame] | 3012 | return VPPCOM_EBADFD; |
Steven | ac1f96d | 2017-10-24 16:03:58 -0700 | [diff] [blame] | 3013 | } |
Florin Coras | 7e12d94 | 2018-06-27 14:32:43 -0700 | [diff] [blame] | 3014 | ep->is_ip4 = session->transport.is_ip4; |
| 3015 | ep->port = session->transport.rmt_port; |
Steven | ac1f96d | 2017-10-24 16:03:58 -0700 | [diff] [blame] | 3016 | } |
Steven | 58f464e | 2017-10-25 12:33:12 -0700 | [diff] [blame] | 3017 | |
| 3018 | if (flags == 0) |
Steven | ac1f96d | 2017-10-24 16:03:58 -0700 | [diff] [blame] | 3019 | rv = vppcom_session_read (session_index, buffer, buflen); |
| 3020 | else if (flags & MSG_PEEK) |
Steven | 58f464e | 2017-10-25 12:33:12 -0700 | [diff] [blame] | 3021 | rv = vppcom_session_peek (session_index, buffer, buflen); |
Steven | ac1f96d | 2017-10-24 16:03:58 -0700 | [diff] [blame] | 3022 | else |
| 3023 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 3024 | clib_warning ("VCL<%d>: Unsupport flags for recvfrom %d", |
| 3025 | getpid (), flags); |
Florin Coras | 460dce6 | 2018-07-27 05:45:06 -0700 | [diff] [blame] | 3026 | return VPPCOM_EAFNOSUPPORT; |
Steven | ac1f96d | 2017-10-24 16:03:58 -0700 | [diff] [blame] | 3027 | } |
| 3028 | |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 3029 | if (ep) |
| 3030 | { |
| 3031 | if (session->transport.is_ip4) |
| 3032 | clib_memcpy (ep->ip, &session->transport.rmt_ip.ip4, |
| 3033 | sizeof (ip4_address_t)); |
| 3034 | else |
| 3035 | clib_memcpy (ep->ip, &session->transport.rmt_ip.ip6, |
| 3036 | sizeof (ip6_address_t)); |
| 3037 | } |
Florin Coras | 460dce6 | 2018-07-27 05:45:06 -0700 | [diff] [blame] | 3038 | |
Steven | ac1f96d | 2017-10-24 16:03:58 -0700 | [diff] [blame] | 3039 | return rv; |
| 3040 | } |
| 3041 | |
| 3042 | int |
| 3043 | vppcom_session_sendto (uint32_t session_index, void *buffer, |
| 3044 | uint32_t buflen, int flags, vppcom_endpt_t * ep) |
| 3045 | { |
Dave Wallace | 617dffa | 2017-10-26 14:47:06 -0400 | [diff] [blame] | 3046 | if (!buffer) |
| 3047 | return VPPCOM_EINVAL; |
| 3048 | |
| 3049 | if (ep) |
| 3050 | { |
| 3051 | // TBD |
| 3052 | return VPPCOM_EINVAL; |
| 3053 | } |
| 3054 | |
| 3055 | if (flags) |
| 3056 | { |
| 3057 | // TBD check the flags and do the right thing |
Florin Coras | 0d427d8 | 2018-06-27 03:24:07 -0700 | [diff] [blame] | 3058 | VDBG (2, "VCL<%d>: handling flags 0x%u (%d) not implemented yet.", |
| 3059 | getpid (), flags, flags); |
Dave Wallace | 617dffa | 2017-10-26 14:47:06 -0400 | [diff] [blame] | 3060 | } |
| 3061 | |
| 3062 | return (vppcom_session_write (session_index, buffer, buflen)); |
Steven | ac1f96d | 2017-10-24 16:03:58 -0700 | [diff] [blame] | 3063 | } |
| 3064 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 3065 | int |
| 3066 | vppcom_poll (vcl_poll_t * vp, uint32_t n_sids, double wait_for_time) |
| 3067 | { |
| 3068 | f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time; |
| 3069 | u32 i, keep_trying = 1; |
| 3070 | int rv, num_ev = 0; |
| 3071 | |
Florin Coras | 0d427d8 | 2018-06-27 03:24:07 -0700 | [diff] [blame] | 3072 | VDBG (3, "VCL<%d>: vp %p, nsids %u, wait_for_time %f", |
| 3073 | getpid (), vp, n_sids, wait_for_time); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 3074 | |
| 3075 | if (!vp) |
| 3076 | return VPPCOM_EFAULT; |
| 3077 | |
| 3078 | do |
| 3079 | { |
Florin Coras | 7e12d94 | 2018-06-27 14:32:43 -0700 | [diff] [blame] | 3080 | vcl_session_t *session; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 3081 | |
| 3082 | for (i = 0; i < n_sids; i++) |
| 3083 | { |
| 3084 | ASSERT (vp[i].revents); |
| 3085 | |
Florin Coras | 070453d | 2018-08-24 17:04:27 -0700 | [diff] [blame] | 3086 | session = vcl_session_get (vp[i].sid); |
| 3087 | if (!session) |
| 3088 | continue; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 3089 | |
| 3090 | if (*vp[i].revents) |
| 3091 | *vp[i].revents = 0; |
| 3092 | |
| 3093 | if (POLLIN & vp[i].events) |
| 3094 | { |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 3095 | rv = vppcom_session_read_ready (session); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 3096 | if (rv > 0) |
| 3097 | { |
| 3098 | *vp[i].revents |= POLLIN; |
| 3099 | num_ev++; |
| 3100 | } |
| 3101 | else if (rv < 0) |
| 3102 | { |
| 3103 | switch (rv) |
| 3104 | { |
| 3105 | case VPPCOM_ECONNRESET: |
| 3106 | *vp[i].revents = POLLHUP; |
| 3107 | break; |
| 3108 | |
| 3109 | default: |
| 3110 | *vp[i].revents = POLLERR; |
| 3111 | break; |
| 3112 | } |
| 3113 | num_ev++; |
| 3114 | } |
| 3115 | } |
| 3116 | |
| 3117 | if (POLLOUT & vp[i].events) |
| 3118 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 3119 | rv = vppcom_session_write_ready (session, vp[i].sid); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 3120 | if (rv > 0) |
| 3121 | { |
| 3122 | *vp[i].revents |= POLLOUT; |
| 3123 | num_ev++; |
| 3124 | } |
| 3125 | else if (rv < 0) |
| 3126 | { |
| 3127 | switch (rv) |
| 3128 | { |
| 3129 | case VPPCOM_ECONNRESET: |
| 3130 | *vp[i].revents = POLLHUP; |
| 3131 | break; |
| 3132 | |
| 3133 | default: |
| 3134 | *vp[i].revents = POLLERR; |
| 3135 | break; |
| 3136 | } |
| 3137 | num_ev++; |
| 3138 | } |
| 3139 | } |
| 3140 | |
Dave Wallace | 7e607a7 | 2018-06-18 18:41:32 -0400 | [diff] [blame] | 3141 | if (0) // Note "done:" label used by VCL_SESSION_LOCK_AND_GET() |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 3142 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 3143 | *vp[i].revents = POLLNVAL; |
| 3144 | num_ev++; |
| 3145 | } |
| 3146 | } |
| 3147 | if (wait_for_time != -1) |
| 3148 | keep_trying = (clib_time_now (&vcm->clib_time) <= timeout) ? 1 : 0; |
| 3149 | } |
| 3150 | while ((num_ev == 0) && keep_trying); |
| 3151 | |
| 3152 | if (VPPCOM_DEBUG > 3) |
| 3153 | { |
| 3154 | clib_warning ("VCL<%d>: returning %d", getpid (), num_ev); |
| 3155 | for (i = 0; i < n_sids; i++) |
| 3156 | { |
| 3157 | clib_warning ("VCL<%d>: vp[%d].sid %d (0x%x), .events 0x%x, " |
| 3158 | ".revents 0x%x", getpid (), i, vp[i].sid, vp[i].sid, |
| 3159 | vp[i].events, *vp[i].revents); |
| 3160 | } |
| 3161 | } |
| 3162 | return num_ev; |
| 3163 | } |
| 3164 | |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 3165 | int |
| 3166 | vppcom_mq_epoll_fd (void) |
| 3167 | { |
| 3168 | return vcm->mqs_epfd; |
| 3169 | } |
| 3170 | |
Dave Wallace | e22aa74 | 2017-10-20 12:30:38 -0400 | [diff] [blame] | 3171 | /* |
| 3172 | * fd.io coding-style-patch-verification: ON |
| 3173 | * |
| 3174 | * Local Variables: |
| 3175 | * eval: (c-set-style "gnu") |
| 3176 | * End: |
| 3177 | */ |