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