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