Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 Cisco and/or its affiliates. |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 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 | * @file |
| 17 | * @brief Session and session manager |
| 18 | */ |
| 19 | |
| 20 | #include <vnet/session/session.h> |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 21 | #include <vnet/session/session_debug.h> |
| 22 | #include <vnet/session/application.h> |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 23 | #include <vlibmemory/api.h> |
| 24 | #include <vnet/dpo/load_balance.h> |
| 25 | #include <vnet/fib/ip4_fib.h> |
Florin Coras | a0b34a7 | 2017-03-07 01:20:52 -0800 | [diff] [blame] | 26 | #include <vnet/tcp/tcp.h> |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 27 | |
| 28 | session_manager_main_t session_manager_main; |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 29 | extern transport_proto_vft_t *tp_vfts; |
Florin Coras | 3eb5062 | 2017-07-13 01:24:57 -0400 | [diff] [blame] | 30 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 31 | int |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 32 | stream_session_create_i (segment_manager_t * sm, transport_connection_t * tc, |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 33 | u8 alloc_fifos, stream_session_t ** ret_s) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 34 | { |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 35 | session_manager_main_t *smm = &session_manager_main; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 36 | svm_fifo_t *server_rx_fifo = 0, *server_tx_fifo = 0; |
| 37 | u32 fifo_segment_index; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 38 | u32 pool_index; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 39 | stream_session_t *s; |
| 40 | u64 value; |
| 41 | u32 thread_index = tc->thread_index; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 42 | int rv; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 43 | |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 44 | ASSERT (thread_index == vlib_get_thread_index ()); |
| 45 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 46 | /* Create the session */ |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 47 | pool_get_aligned (smm->sessions[thread_index], s, CLIB_CACHE_LINE_BYTES); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 48 | memset (s, 0, sizeof (*s)); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 49 | pool_index = s - smm->sessions[thread_index]; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 50 | |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 51 | /* Allocate fifos */ |
| 52 | if (alloc_fifos) |
| 53 | { |
| 54 | if ((rv = segment_manager_alloc_session_fifos (sm, &server_rx_fifo, |
| 55 | &server_tx_fifo, |
| 56 | &fifo_segment_index))) |
| 57 | { |
| 58 | pool_put (smm->sessions[thread_index], s); |
| 59 | return rv; |
| 60 | } |
| 61 | /* Initialize backpointers */ |
| 62 | server_rx_fifo->master_session_index = pool_index; |
| 63 | server_rx_fifo->master_thread_index = thread_index; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 64 | |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 65 | server_tx_fifo->master_session_index = pool_index; |
| 66 | server_tx_fifo->master_thread_index = thread_index; |
| 67 | |
| 68 | s->server_rx_fifo = server_rx_fifo; |
| 69 | s->server_tx_fifo = server_tx_fifo; |
| 70 | s->svm_segment_index = fifo_segment_index; |
| 71 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 72 | |
| 73 | /* Initialize state machine, such as it is... */ |
Florin Coras | 6881062 | 2017-07-24 17:40:28 -0700 | [diff] [blame] | 74 | s->session_type = session_type_from_proto_and_ip (tc->transport_proto, |
| 75 | tc->is_ip4); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 76 | s->session_state = SESSION_STATE_CONNECTING; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 77 | s->thread_index = thread_index; |
| 78 | s->session_index = pool_index; |
| 79 | |
| 80 | /* Attach transport to session */ |
| 81 | s->connection_index = tc->c_index; |
| 82 | |
| 83 | /* Attach session to transport */ |
| 84 | tc->s_index = s->session_index; |
| 85 | |
| 86 | /* Add to the main lookup table */ |
Florin Coras | 6534b7a | 2017-07-18 05:38:03 -0400 | [diff] [blame] | 87 | value = stream_session_handle (s); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 88 | stream_session_table_add_for_tc (tc, value); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 89 | |
| 90 | *ret_s = s; |
| 91 | |
| 92 | return 0; |
| 93 | } |
| 94 | |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 95 | /** Enqueue buffer chain tail */ |
| 96 | always_inline int |
| 97 | session_enqueue_chain_tail (stream_session_t * s, vlib_buffer_t * b, |
| 98 | u32 offset, u8 is_in_order) |
| 99 | { |
| 100 | vlib_buffer_t *chain_b; |
Florin Coras | b2215d6 | 2017-08-01 16:56:58 -0700 | [diff] [blame] | 101 | u32 chain_bi = b->next_buffer, len; |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 102 | vlib_main_t *vm = vlib_get_main (); |
Florin Coras | b2215d6 | 2017-08-01 16:56:58 -0700 | [diff] [blame] | 103 | u8 *data; |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 104 | u16 written = 0; |
| 105 | int rv = 0; |
| 106 | |
| 107 | do |
| 108 | { |
| 109 | chain_b = vlib_get_buffer (vm, chain_bi); |
| 110 | data = vlib_buffer_get_current (chain_b); |
| 111 | len = chain_b->current_length; |
| 112 | if (is_in_order) |
| 113 | { |
| 114 | rv = svm_fifo_enqueue_nowait (s->server_rx_fifo, len, data); |
| 115 | if (rv < len) |
| 116 | { |
| 117 | return (rv > 0) ? (written + rv) : written; |
| 118 | } |
| 119 | written += rv; |
| 120 | } |
| 121 | else |
| 122 | { |
| 123 | rv = svm_fifo_enqueue_with_offset (s->server_rx_fifo, offset, len, |
| 124 | data); |
| 125 | if (rv) |
| 126 | return -1; |
| 127 | offset += len; |
| 128 | } |
| 129 | } |
| 130 | while ((chain_bi = (chain_b->flags & VLIB_BUFFER_NEXT_PRESENT) |
| 131 | ? chain_b->next_buffer : 0)); |
| 132 | |
| 133 | if (is_in_order) |
| 134 | return written; |
| 135 | |
| 136 | return 0; |
| 137 | } |
| 138 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 139 | /* |
| 140 | * Enqueue data for delivery to session peer. Does not notify peer of enqueue |
| 141 | * event but on request can queue notification events for later delivery by |
| 142 | * calling stream_server_flush_enqueue_events(). |
| 143 | * |
| 144 | * @param tc Transport connection which is to be enqueued data |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 145 | * @param b Buffer to be enqueued |
| 146 | * @param offset Offset at which to start enqueueing if out-of-order |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 147 | * @param queue_event Flag to indicate if peer is to be notified or if event |
| 148 | * is to be queued. The former is useful when more data is |
| 149 | * enqueued and only one event is to be generated. |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 150 | * @param is_in_order Flag to indicate if data is in order |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 151 | * @return Number of bytes enqueued or a negative value if enqueueing failed. |
| 152 | */ |
| 153 | int |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 154 | stream_session_enqueue_data (transport_connection_t * tc, vlib_buffer_t * b, |
| 155 | u32 offset, u8 queue_event, u8 is_in_order) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 156 | { |
| 157 | stream_session_t *s; |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 158 | int enqueued = 0, rv; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 159 | |
| 160 | s = stream_session_get (tc->s_index, tc->thread_index); |
| 161 | |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 162 | if (is_in_order) |
| 163 | { |
| 164 | enqueued = |
| 165 | svm_fifo_enqueue_nowait (s->server_rx_fifo, b->current_length, |
| 166 | vlib_buffer_get_current (b)); |
| 167 | if (PREDICT_FALSE |
| 168 | ((b->flags & VLIB_BUFFER_NEXT_PRESENT) && enqueued > 0)) |
| 169 | { |
| 170 | rv = session_enqueue_chain_tail (s, b, 0, 1); |
| 171 | if (rv <= 0) |
| 172 | return enqueued; |
| 173 | enqueued += rv; |
| 174 | } |
| 175 | } |
| 176 | else |
| 177 | { |
| 178 | rv = svm_fifo_enqueue_with_offset (s->server_rx_fifo, offset, |
| 179 | b->current_length, |
| 180 | vlib_buffer_get_current (b)); |
| 181 | if (PREDICT_FALSE ((b->flags & VLIB_BUFFER_NEXT_PRESENT) && !rv)) |
| 182 | rv = session_enqueue_chain_tail (s, b, offset + b->current_length, 0); |
| 183 | if (rv) |
| 184 | return -1; |
| 185 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 186 | |
| 187 | if (queue_event) |
| 188 | { |
| 189 | /* Queue RX event on this fifo. Eventually these will need to be flushed |
| 190 | * by calling stream_server_flush_enqueue_events () */ |
| 191 | session_manager_main_t *smm = vnet_get_session_manager_main (); |
| 192 | u32 thread_index = s->thread_index; |
| 193 | u32 my_enqueue_epoch = smm->current_enqueue_epoch[thread_index]; |
| 194 | |
| 195 | if (s->enqueue_epoch != my_enqueue_epoch) |
| 196 | { |
| 197 | s->enqueue_epoch = my_enqueue_epoch; |
| 198 | vec_add1 (smm->session_indices_to_enqueue_by_thread[thread_index], |
| 199 | s - smm->sessions[thread_index]); |
| 200 | } |
| 201 | } |
| 202 | |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 203 | if (is_in_order) |
| 204 | return enqueued; |
| 205 | |
| 206 | return 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | /** Check if we have space in rx fifo to push more bytes */ |
| 210 | u8 |
| 211 | stream_session_no_space (transport_connection_t * tc, u32 thread_index, |
| 212 | u16 data_len) |
| 213 | { |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 214 | stream_session_t *s = stream_session_get (tc->s_index, thread_index); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 215 | |
| 216 | if (PREDICT_FALSE (s->session_state != SESSION_STATE_READY)) |
| 217 | return 1; |
| 218 | |
| 219 | if (data_len > svm_fifo_max_enqueue (s->server_rx_fifo)) |
| 220 | return 1; |
| 221 | |
| 222 | return 0; |
| 223 | } |
| 224 | |
| 225 | u32 |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 226 | stream_session_tx_fifo_max_dequeue (transport_connection_t * tc) |
| 227 | { |
| 228 | stream_session_t *s = stream_session_get (tc->s_index, tc->thread_index); |
Florin Coras | b2215d6 | 2017-08-01 16:56:58 -0700 | [diff] [blame] | 229 | if (!s->server_tx_fifo) |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 230 | return 0; |
| 231 | return svm_fifo_max_dequeue (s->server_tx_fifo); |
| 232 | } |
| 233 | |
| 234 | int |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 235 | stream_session_peek_bytes (transport_connection_t * tc, u8 * buffer, |
| 236 | u32 offset, u32 max_bytes) |
| 237 | { |
| 238 | stream_session_t *s = stream_session_get (tc->s_index, tc->thread_index); |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 239 | return svm_fifo_peek (s->server_tx_fifo, offset, max_bytes, buffer); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | u32 |
| 243 | stream_session_dequeue_drop (transport_connection_t * tc, u32 max_bytes) |
| 244 | { |
| 245 | stream_session_t *s = stream_session_get (tc->s_index, tc->thread_index); |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 246 | return svm_fifo_dequeue_drop (s->server_tx_fifo, max_bytes); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Notify session peer that new data has been enqueued. |
| 251 | * |
| 252 | * @param s Stream session for which the event is to be generated. |
| 253 | * @param block Flag to indicate if call should block if event queue is full. |
| 254 | * |
| 255 | * @return 0 on succes or negative number if failed to send notification. |
| 256 | */ |
| 257 | static int |
| 258 | stream_session_enqueue_notify (stream_session_t * s, u8 block) |
| 259 | { |
| 260 | application_t *app; |
| 261 | session_fifo_event_t evt; |
| 262 | unix_shared_memory_queue_t *q; |
| 263 | static u32 serial_number; |
| 264 | |
| 265 | if (PREDICT_FALSE (s->session_state == SESSION_STATE_CLOSED)) |
| 266 | return 0; |
| 267 | |
| 268 | /* Get session's server */ |
Dave Barach | 818eb54 | 2017-08-02 13:56:13 -0400 | [diff] [blame] | 269 | app = application_get_if_valid (s->app_index); |
| 270 | |
| 271 | if (PREDICT_FALSE (app == 0)) |
| 272 | { |
| 273 | clib_warning ("invalid s->app_index = %d", s->app_index); |
| 274 | return 0; |
| 275 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 276 | |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 277 | /* Built-in server? Hand event to the callback... */ |
| 278 | if (app->cb_fns.builtin_server_rx_callback) |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 279 | return app->cb_fns.builtin_server_rx_callback (s); |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 280 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 281 | /* If no event, send one */ |
| 282 | if (svm_fifo_set_event (s->server_rx_fifo)) |
| 283 | { |
| 284 | /* Fabricate event */ |
| 285 | evt.fifo = s->server_rx_fifo; |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 286 | evt.event_type = FIFO_EVENT_APP_RX; |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 287 | evt.event_id = serial_number++; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 288 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 289 | /* Add event to server's event queue */ |
| 290 | q = app->event_queue; |
| 291 | |
| 292 | /* Based on request block (or not) for lack of space */ |
| 293 | if (block || PREDICT_TRUE (q->cursize < q->maxsize)) |
| 294 | unix_shared_memory_queue_add (app->event_queue, (u8 *) & evt, |
| 295 | 0 /* do wait for mutex */ ); |
| 296 | else |
| 297 | { |
| 298 | clib_warning ("fifo full"); |
| 299 | return -1; |
| 300 | } |
| 301 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 302 | |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 303 | /* *INDENT-OFF* */ |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 304 | SESSION_EVT_DBG(SESSION_EVT_ENQ, s, ({ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 305 | ed->data[0] = evt.event_id; |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 306 | ed->data[1] = svm_fifo_max_dequeue (s->server_rx_fifo); |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 307 | })); |
| 308 | /* *INDENT-ON* */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 309 | |
| 310 | return 0; |
| 311 | } |
| 312 | |
| 313 | /** |
| 314 | * Flushes queue of sessions that are to be notified of new data |
| 315 | * enqueued events. |
| 316 | * |
| 317 | * @param thread_index Thread index for which the flush is to be performed. |
| 318 | * @return 0 on success or a positive number indicating the number of |
| 319 | * failures due to API queue being full. |
| 320 | */ |
| 321 | int |
| 322 | session_manager_flush_enqueue_events (u32 thread_index) |
| 323 | { |
| 324 | session_manager_main_t *smm = &session_manager_main; |
| 325 | u32 *session_indices_to_enqueue; |
| 326 | int i, errors = 0; |
| 327 | |
| 328 | session_indices_to_enqueue = |
| 329 | smm->session_indices_to_enqueue_by_thread[thread_index]; |
| 330 | |
| 331 | for (i = 0; i < vec_len (session_indices_to_enqueue); i++) |
| 332 | { |
| 333 | stream_session_t *s0; |
| 334 | |
| 335 | /* Get session */ |
Dave Barach | 818eb54 | 2017-08-02 13:56:13 -0400 | [diff] [blame] | 336 | s0 = stream_session_get_if_valid (session_indices_to_enqueue[i], |
| 337 | thread_index); |
| 338 | if (s0 == 0 || stream_session_enqueue_notify (s0, 0 /* don't block */ )) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 339 | { |
| 340 | errors++; |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | vec_reset_length (session_indices_to_enqueue); |
| 345 | |
| 346 | smm->session_indices_to_enqueue_by_thread[thread_index] = |
| 347 | session_indices_to_enqueue; |
| 348 | |
| 349 | /* Increment enqueue epoch for next round */ |
| 350 | smm->current_enqueue_epoch[thread_index]++; |
| 351 | |
| 352 | return errors; |
| 353 | } |
| 354 | |
Florin Coras | c28764f | 2017-04-26 00:08:42 -0700 | [diff] [blame] | 355 | /** |
| 356 | * Init fifo tail and head pointers |
| 357 | * |
| 358 | * Useful if transport uses absolute offsets for tracking ooo segments. |
| 359 | */ |
| 360 | void |
| 361 | stream_session_init_fifos_pointers (transport_connection_t * tc, |
| 362 | u32 rx_pointer, u32 tx_pointer) |
| 363 | { |
| 364 | stream_session_t *s; |
| 365 | s = stream_session_get (tc->s_index, tc->thread_index); |
| 366 | svm_fifo_init_pointers (s->server_rx_fifo, rx_pointer); |
| 367 | svm_fifo_init_pointers (s->server_tx_fifo, tx_pointer); |
| 368 | } |
| 369 | |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 370 | int |
Florin Coras | 6881062 | 2017-07-24 17:40:28 -0700 | [diff] [blame] | 371 | stream_session_connect_notify (transport_connection_t * tc, u8 is_fail) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 372 | { |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 373 | application_t *app; |
| 374 | stream_session_t *new_s = 0; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 375 | u64 handle; |
Florin Coras | ab0289a | 2017-08-14 11:25:25 -0700 | [diff] [blame] | 376 | u32 opaque = 0; |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 377 | int error = 0; |
root | c9d1c5b | 2017-08-15 12:58:31 -0400 | [diff] [blame] | 378 | u8 st; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 379 | |
root | c9d1c5b | 2017-08-15 12:58:31 -0400 | [diff] [blame] | 380 | st = session_type_from_proto_and_ip (tc->transport_proto, tc->is_ip4); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 381 | handle = stream_session_half_open_lookup_handle (&tc->lcl_ip, &tc->rmt_ip, |
| 382 | tc->lcl_port, tc->rmt_port, |
root | c9d1c5b | 2017-08-15 12:58:31 -0400 | [diff] [blame] | 383 | st); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 384 | if (handle == HALF_OPEN_LOOKUP_INVALID_VALUE) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 385 | { |
| 386 | clib_warning ("This can't be good!"); |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 387 | return -1; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 388 | } |
| 389 | |
Florin Coras | ab0289a | 2017-08-14 11:25:25 -0700 | [diff] [blame] | 390 | /* Get the app's index from the handle we stored when opening connection |
| 391 | * and the opaque (api_context for external apps) from transport session |
| 392 | * index*/ |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 393 | app = application_get (handle >> 32); |
Florin Coras | ab0289a | 2017-08-14 11:25:25 -0700 | [diff] [blame] | 394 | opaque = tc->s_index; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 395 | |
| 396 | if (!is_fail) |
| 397 | { |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 398 | segment_manager_t *sm; |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 399 | u8 alloc_fifos; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 400 | sm = application_get_connect_segment_manager (app); |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 401 | alloc_fifos = application_is_proxy (app); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 402 | /* Create new session (svm segments are allocated if needed) */ |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 403 | if (stream_session_create_i (sm, tc, alloc_fifos, &new_s)) |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 404 | { |
| 405 | is_fail = 1; |
| 406 | error = -1; |
| 407 | } |
| 408 | else |
| 409 | new_s->app_index = app->index; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 410 | } |
| 411 | |
Florin Coras | 6881062 | 2017-07-24 17:40:28 -0700 | [diff] [blame] | 412 | /* Notify client application */ |
Florin Coras | ab0289a | 2017-08-14 11:25:25 -0700 | [diff] [blame] | 413 | if (app->cb_fns.session_connected_callback (app->index, opaque, new_s, |
Florin Coras | 6534b7a | 2017-07-18 05:38:03 -0400 | [diff] [blame] | 414 | is_fail)) |
| 415 | { |
| 416 | clib_warning ("failed to notify app"); |
| 417 | if (!is_fail) |
| 418 | stream_session_disconnect (new_s); |
| 419 | } |
| 420 | else |
| 421 | { |
| 422 | if (!is_fail) |
| 423 | new_s->session_state = SESSION_STATE_READY; |
| 424 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 425 | |
| 426 | /* Cleanup session lookup */ |
Florin Coras | 6881062 | 2017-07-24 17:40:28 -0700 | [diff] [blame] | 427 | stream_session_half_open_table_del (tc); |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 428 | |
| 429 | return error; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | void |
| 433 | stream_session_accept_notify (transport_connection_t * tc) |
| 434 | { |
| 435 | application_t *server; |
| 436 | stream_session_t *s; |
| 437 | |
| 438 | s = stream_session_get (tc->s_index, tc->thread_index); |
| 439 | server = application_get (s->app_index); |
| 440 | server->cb_fns.session_accept_callback (s); |
| 441 | } |
| 442 | |
| 443 | /** |
| 444 | * Notification from transport that connection is being closed. |
| 445 | * |
| 446 | * A disconnect is sent to application but state is not removed. Once |
| 447 | * disconnect is acknowledged by application, session disconnect is called. |
| 448 | * Ultimately this leads to close being called on transport (passive close). |
| 449 | */ |
| 450 | void |
| 451 | stream_session_disconnect_notify (transport_connection_t * tc) |
| 452 | { |
| 453 | application_t *server; |
| 454 | stream_session_t *s; |
| 455 | |
| 456 | s = stream_session_get (tc->s_index, tc->thread_index); |
| 457 | server = application_get (s->app_index); |
| 458 | server->cb_fns.session_disconnect_callback (s); |
| 459 | } |
| 460 | |
| 461 | /** |
| 462 | * Cleans up session and associated app if needed. |
| 463 | */ |
| 464 | void |
| 465 | stream_session_delete (stream_session_t * s) |
| 466 | { |
| 467 | session_manager_main_t *smm = vnet_get_session_manager_main (); |
Florin Coras | 6534b7a | 2017-07-18 05:38:03 -0400 | [diff] [blame] | 468 | int rv; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 469 | |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 470 | /* Delete from the main lookup table. */ |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 471 | if ((rv = stream_session_table_del (s))) |
Florin Coras | 6534b7a | 2017-07-18 05:38:03 -0400 | [diff] [blame] | 472 | clib_warning ("hash delete error, rv %d", rv); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 473 | |
| 474 | /* Cleanup fifo segments */ |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 475 | segment_manager_dealloc_fifos (s->svm_segment_index, s->server_rx_fifo, |
| 476 | s->server_tx_fifo); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 477 | |
| 478 | pool_put (smm->sessions[s->thread_index], s); |
Florin Coras | 6534b7a | 2017-07-18 05:38:03 -0400 | [diff] [blame] | 479 | if (CLIB_DEBUG) |
| 480 | memset (s, 0xFA, sizeof (*s)); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | /** |
| 484 | * Notification from transport that connection is being deleted |
| 485 | * |
| 486 | * This should be called only on previously fully established sessions. For |
| 487 | * instance failed connects should call stream_session_connect_notify and |
| 488 | * indicate that the connect has failed. |
| 489 | */ |
| 490 | void |
| 491 | stream_session_delete_notify (transport_connection_t * tc) |
| 492 | { |
| 493 | stream_session_t *s; |
| 494 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 495 | /* App might've been removed already */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 496 | s = stream_session_get_if_valid (tc->s_index, tc->thread_index); |
| 497 | if (!s) |
| 498 | { |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 499 | return; |
| 500 | } |
| 501 | stream_session_delete (s); |
| 502 | } |
| 503 | |
| 504 | /** |
| 505 | * Notify application that connection has been reset. |
| 506 | */ |
| 507 | void |
| 508 | stream_session_reset_notify (transport_connection_t * tc) |
| 509 | { |
| 510 | stream_session_t *s; |
| 511 | application_t *app; |
| 512 | s = stream_session_get (tc->s_index, tc->thread_index); |
| 513 | |
| 514 | app = application_get (s->app_index); |
| 515 | app->cb_fns.session_reset_callback (s); |
| 516 | } |
| 517 | |
| 518 | /** |
| 519 | * Accept a stream session. Optionally ping the server by callback. |
| 520 | */ |
| 521 | int |
| 522 | stream_session_accept (transport_connection_t * tc, u32 listener_index, |
| 523 | u8 sst, u8 notify) |
| 524 | { |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 525 | application_t *server; |
| 526 | stream_session_t *s, *listener; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 527 | segment_manager_t *sm; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 528 | |
| 529 | int rv; |
| 530 | |
| 531 | /* Find the server */ |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 532 | listener = listen_session_get (sst, listener_index); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 533 | server = application_get (listener->app_index); |
| 534 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 535 | sm = application_get_listen_segment_manager (server, listener); |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 536 | if ((rv = stream_session_create_i (sm, tc, 1, &s))) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 537 | return rv; |
| 538 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 539 | s->app_index = server->index; |
| 540 | s->listener_index = listener_index; |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 541 | s->session_state = SESSION_STATE_ACCEPTING; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 542 | |
| 543 | /* Shoulder-tap the server */ |
| 544 | if (notify) |
| 545 | { |
| 546 | server->cb_fns.session_accept_callback (s); |
| 547 | } |
| 548 | |
| 549 | return 0; |
| 550 | } |
| 551 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 552 | /** |
| 553 | * Ask transport to open connection to remote transport endpoint. |
| 554 | * |
| 555 | * Stores handle for matching request with reply since the call can be |
| 556 | * asynchronous. For instance, for TCP the 3-way handshake must complete |
| 557 | * before reply comes. Session is only created once connection is established. |
| 558 | * |
| 559 | * @param app_index Index of the application requesting the connect |
| 560 | * @param st Session type requested. |
| 561 | * @param tep Remote transport endpoint |
| 562 | * @param res Resulting transport connection . |
| 563 | */ |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 564 | int |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 565 | stream_session_open (u32 app_index, session_type_t st, |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 566 | transport_endpoint_t * rmt, |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 567 | transport_connection_t ** res) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 568 | { |
| 569 | transport_connection_t *tc; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 570 | int rv; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 571 | u64 handle; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 572 | |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 573 | rv = tp_vfts[st].open (rmt); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 574 | if (rv < 0) |
| 575 | { |
| 576 | clib_warning ("Transport failed to open connection."); |
| 577 | return VNET_API_ERROR_SESSION_CONNECT_FAIL; |
| 578 | } |
| 579 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 580 | tc = tp_vfts[st].get_half_open ((u32) rv); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 581 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 582 | /* Save app and tc index. The latter is needed to help establish the |
| 583 | * connection while the former is needed when the connect notify comes |
| 584 | * and we have to notify the external app */ |
| 585 | handle = (((u64) app_index) << 32) | (u64) tc->c_index; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 586 | |
| 587 | /* Add to the half-open lookup table */ |
Florin Coras | 6881062 | 2017-07-24 17:40:28 -0700 | [diff] [blame] | 588 | stream_session_half_open_table_add (tc, handle); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 589 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 590 | *res = tc; |
| 591 | |
| 592 | return 0; |
| 593 | } |
| 594 | |
| 595 | /** |
| 596 | * Ask transport to listen on local transport endpoint. |
| 597 | * |
| 598 | * @param s Session for which listen will be called. Note that unlike |
| 599 | * established sessions, listen sessions are not associated to a |
| 600 | * thread. |
| 601 | * @param tep Local endpoint to be listened on. |
| 602 | */ |
| 603 | int |
| 604 | stream_session_listen (stream_session_t * s, transport_endpoint_t * tep) |
| 605 | { |
| 606 | transport_connection_t *tc; |
| 607 | u32 tci; |
| 608 | |
| 609 | /* Transport bind/listen */ |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 610 | tci = tp_vfts[s->session_type].bind (s->session_index, tep); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 611 | |
| 612 | if (tci == (u32) ~ 0) |
| 613 | return -1; |
| 614 | |
| 615 | /* Attach transport to session */ |
| 616 | s->connection_index = tci; |
| 617 | tc = tp_vfts[s->session_type].get_listener (tci); |
| 618 | |
| 619 | /* Weird but handle it ... */ |
| 620 | if (tc == 0) |
| 621 | return -1; |
| 622 | |
| 623 | /* Add to the main lookup table */ |
| 624 | stream_session_table_add_for_tc (tc, s->session_index); |
| 625 | |
| 626 | return 0; |
| 627 | } |
| 628 | |
| 629 | /** |
| 630 | * Ask transport to stop listening on local transport endpoint. |
| 631 | * |
| 632 | * @param s Session to stop listening on. It must be in state LISTENING. |
| 633 | */ |
| 634 | int |
| 635 | stream_session_stop_listen (stream_session_t * s) |
| 636 | { |
| 637 | transport_connection_t *tc; |
| 638 | |
| 639 | if (s->session_state != SESSION_STATE_LISTENING) |
| 640 | { |
| 641 | clib_warning ("not a listening session"); |
| 642 | return -1; |
| 643 | } |
| 644 | |
| 645 | tc = tp_vfts[s->session_type].get_listener (s->connection_index); |
| 646 | if (!tc) |
| 647 | { |
| 648 | clib_warning ("no transport"); |
| 649 | return VNET_API_ERROR_ADDRESS_NOT_IN_USE; |
| 650 | } |
| 651 | |
| 652 | stream_session_table_del_for_tc (tc); |
| 653 | tp_vfts[s->session_type].unbind (s->connection_index); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 654 | return 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 655 | } |
| 656 | |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 657 | void |
| 658 | session_send_session_evt_to_thread (u64 session_handle, |
| 659 | fifo_event_type_t evt_type, |
| 660 | u32 thread_index) |
| 661 | { |
| 662 | static u16 serial_number = 0; |
| 663 | session_fifo_event_t evt; |
| 664 | unix_shared_memory_queue_t *q; |
| 665 | |
| 666 | /* Fabricate event */ |
| 667 | evt.session_handle = session_handle; |
| 668 | evt.event_type = evt_type; |
| 669 | evt.event_id = serial_number++; |
| 670 | |
| 671 | q = session_manager_get_vpp_event_queue (thread_index); |
| 672 | |
| 673 | /* Based on request block (or not) for lack of space */ |
| 674 | if (PREDICT_TRUE (q->cursize < q->maxsize)) |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 675 | { |
| 676 | if (unix_shared_memory_queue_add (q, (u8 *) & evt, |
| 677 | 1 /* do wait for mutex */ )) |
| 678 | { |
| 679 | clib_warning ("failed to enqueue evt"); |
| 680 | } |
| 681 | } |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 682 | else |
| 683 | { |
| 684 | clib_warning ("queue full"); |
| 685 | return; |
| 686 | } |
| 687 | } |
| 688 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 689 | /** |
| 690 | * Disconnect session and propagate to transport. This should eventually |
| 691 | * result in a delete notification that allows us to cleanup session state. |
| 692 | * Called for both active/passive disconnects. |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 693 | * |
| 694 | * Should be called from the session's thread. |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 695 | */ |
| 696 | void |
| 697 | stream_session_disconnect (stream_session_t * s) |
| 698 | { |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 699 | s->session_state = SESSION_STATE_CLOSED; |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 700 | tp_vfts[s->session_type].close (s->connection_index, s->thread_index); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 701 | } |
| 702 | |
| 703 | /** |
| 704 | * Cleanup transport and session state. |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 705 | * |
| 706 | * Notify transport of the cleanup, wait for a delete notify to actually |
| 707 | * remove the session state. |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 708 | */ |
| 709 | void |
| 710 | stream_session_cleanup (stream_session_t * s) |
| 711 | { |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 712 | int rv; |
| 713 | |
| 714 | s->session_state = SESSION_STATE_CLOSED; |
| 715 | |
| 716 | /* Delete from the main lookup table to avoid more enqueues */ |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 717 | rv = stream_session_table_del (s); |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 718 | if (rv) |
| 719 | clib_warning ("hash delete error, rv %d", rv); |
| 720 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 721 | tp_vfts[s->session_type].cleanup (s->connection_index, s->thread_index); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 722 | } |
| 723 | |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 724 | /** |
| 725 | * Allocate vpp event queue (once) per worker thread |
| 726 | */ |
| 727 | void |
| 728 | session_vpp_event_queue_allocate (session_manager_main_t * smm, |
| 729 | u32 thread_index) |
| 730 | { |
| 731 | api_main_t *am = &api_main; |
| 732 | void *oldheap; |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 733 | u32 event_queue_length = 2048; |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 734 | |
| 735 | if (smm->vpp_event_queues[thread_index] == 0) |
| 736 | { |
| 737 | /* Allocate event fifo in the /vpe-api shared-memory segment */ |
| 738 | oldheap = svm_push_data_heap (am->vlib_rp); |
| 739 | |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 740 | if (smm->configured_event_queue_length) |
| 741 | event_queue_length = smm->configured_event_queue_length; |
| 742 | |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 743 | smm->vpp_event_queues[thread_index] = |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 744 | unix_shared_memory_queue_init |
| 745 | (event_queue_length, |
| 746 | sizeof (session_fifo_event_t), 0 /* consumer pid */ , |
| 747 | 0 /* (do not) send signal when queue non-empty */ ); |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 748 | |
| 749 | svm_pop_heap (oldheap); |
| 750 | } |
| 751 | } |
| 752 | |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 753 | session_type_t |
| 754 | session_type_from_proto_and_ip (transport_proto_t proto, u8 is_ip4) |
| 755 | { |
| 756 | if (proto == TRANSPORT_PROTO_TCP) |
| 757 | { |
| 758 | if (is_ip4) |
| 759 | return SESSION_TYPE_IP4_TCP; |
| 760 | else |
| 761 | return SESSION_TYPE_IP6_TCP; |
| 762 | } |
| 763 | else |
| 764 | { |
| 765 | if (is_ip4) |
| 766 | return SESSION_TYPE_IP4_UDP; |
| 767 | else |
| 768 | return SESSION_TYPE_IP6_UDP; |
| 769 | } |
| 770 | |
| 771 | return SESSION_N_TYPES; |
| 772 | } |
| 773 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 774 | static clib_error_t * |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 775 | session_manager_main_enable (vlib_main_t * vm) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 776 | { |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 777 | session_manager_main_t *smm = &session_manager_main; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 778 | vlib_thread_main_t *vtm = vlib_get_thread_main (); |
| 779 | u32 num_threads; |
Florin Coras | 66b1131 | 2017-07-31 17:18:03 -0700 | [diff] [blame] | 780 | u32 preallocated_sessions_per_worker; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 781 | int i; |
| 782 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 783 | num_threads = 1 /* main thread */ + vtm->n_threads; |
| 784 | |
| 785 | if (num_threads < 1) |
| 786 | return clib_error_return (0, "n_thread_stacks not set"); |
| 787 | |
| 788 | /* $$$ config parameters */ |
| 789 | svm_fifo_segment_init (0x200000000ULL /* first segment base VA */ , |
| 790 | 20 /* timeout in seconds */ ); |
| 791 | |
| 792 | /* configure per-thread ** vectors */ |
| 793 | vec_validate (smm->sessions, num_threads - 1); |
| 794 | vec_validate (smm->session_indices_to_enqueue_by_thread, num_threads - 1); |
| 795 | vec_validate (smm->tx_buffers, num_threads - 1); |
Dave Barach | acd2a6a | 2017-05-16 17:41:34 -0400 | [diff] [blame] | 796 | vec_validate (smm->pending_event_vector, num_threads - 1); |
| 797 | vec_validate (smm->free_event_vector, num_threads - 1); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 798 | vec_validate (smm->current_enqueue_epoch, num_threads - 1); |
| 799 | vec_validate (smm->vpp_event_queues, num_threads - 1); |
| 800 | |
Dave Barach | acd2a6a | 2017-05-16 17:41:34 -0400 | [diff] [blame] | 801 | for (i = 0; i < num_threads; i++) |
| 802 | { |
| 803 | vec_validate (smm->free_event_vector[i], 0); |
| 804 | _vec_len (smm->free_event_vector[i]) = 0; |
| 805 | vec_validate (smm->pending_event_vector[i], 0); |
| 806 | _vec_len (smm->pending_event_vector[i]) = 0; |
| 807 | } |
| 808 | |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 809 | #if SESSION_DBG |
| 810 | vec_validate (smm->last_event_poll_by_thread, num_threads - 1); |
| 811 | #endif |
| 812 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 813 | /* Allocate vpp event queues */ |
| 814 | for (i = 0; i < vec_len (smm->vpp_event_queues); i++) |
| 815 | session_vpp_event_queue_allocate (smm, i); |
| 816 | |
Florin Coras | 66b1131 | 2017-07-31 17:18:03 -0700 | [diff] [blame] | 817 | /* Preallocate sessions */ |
| 818 | if (num_threads == 1) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 819 | { |
Florin Coras | 66b1131 | 2017-07-31 17:18:03 -0700 | [diff] [blame] | 820 | for (i = 0; i < smm->preallocated_sessions; i++) |
| 821 | { |
| 822 | stream_session_t *ss __attribute__ ((unused)); |
| 823 | pool_get_aligned (smm->sessions[0], ss, CLIB_CACHE_LINE_BYTES); |
| 824 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 825 | |
Florin Coras | 66b1131 | 2017-07-31 17:18:03 -0700 | [diff] [blame] | 826 | for (i = 0; i < smm->preallocated_sessions; i++) |
| 827 | pool_put_index (smm->sessions[0], i); |
| 828 | } |
| 829 | else |
| 830 | { |
| 831 | int j; |
| 832 | preallocated_sessions_per_worker = smm->preallocated_sessions / |
| 833 | (num_threads - 1); |
| 834 | |
| 835 | for (j = 1; j < num_threads; j++) |
| 836 | { |
| 837 | for (i = 0; i < preallocated_sessions_per_worker; i++) |
| 838 | { |
| 839 | stream_session_t *ss __attribute__ ((unused)); |
| 840 | pool_get_aligned (smm->sessions[j], ss, CLIB_CACHE_LINE_BYTES); |
| 841 | } |
| 842 | for (i = 0; i < preallocated_sessions_per_worker; i++) |
| 843 | pool_put_index (smm->sessions[j], i); |
| 844 | } |
| 845 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 846 | |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 847 | session_lookup_init (); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 848 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 849 | smm->is_enabled = 1; |
| 850 | |
Florin Coras | a0b34a7 | 2017-03-07 01:20:52 -0800 | [diff] [blame] | 851 | /* Enable TCP transport */ |
| 852 | vnet_tcp_enable_disable (vm, 1); |
| 853 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 854 | return 0; |
| 855 | } |
| 856 | |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 857 | void |
| 858 | session_node_enable_disable (u8 is_en) |
| 859 | { |
| 860 | u8 state = is_en ? VLIB_NODE_STATE_POLLING : VLIB_NODE_STATE_DISABLED; |
| 861 | /* *INDENT-OFF* */ |
| 862 | foreach_vlib_main (({ |
| 863 | vlib_node_set_state (this_vlib_main, session_queue_node.index, |
| 864 | state); |
| 865 | })); |
| 866 | /* *INDENT-ON* */ |
| 867 | } |
| 868 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 869 | clib_error_t * |
| 870 | vnet_session_enable_disable (vlib_main_t * vm, u8 is_en) |
| 871 | { |
| 872 | if (is_en) |
| 873 | { |
| 874 | if (session_manager_main.is_enabled) |
| 875 | return 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 876 | |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 877 | session_node_enable_disable (is_en); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 878 | |
| 879 | return session_manager_main_enable (vm); |
| 880 | } |
| 881 | else |
| 882 | { |
| 883 | session_manager_main.is_enabled = 0; |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 884 | session_node_enable_disable (is_en); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 885 | } |
| 886 | |
| 887 | return 0; |
| 888 | } |
| 889 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 890 | clib_error_t * |
| 891 | session_manager_main_init (vlib_main_t * vm) |
| 892 | { |
| 893 | session_manager_main_t *smm = &session_manager_main; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 894 | smm->is_enabled = 0; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 895 | return 0; |
| 896 | } |
| 897 | |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 898 | VLIB_INIT_FUNCTION (session_manager_main_init); |
| 899 | |
| 900 | static clib_error_t * |
| 901 | session_config_fn (vlib_main_t * vm, unformat_input_t * input) |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 902 | { |
| 903 | session_manager_main_t *smm = &session_manager_main; |
| 904 | u32 nitems; |
Florin Coras | 66b1131 | 2017-07-31 17:18:03 -0700 | [diff] [blame] | 905 | uword tmp; |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 906 | |
| 907 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 908 | { |
| 909 | if (unformat (input, "event-queue-length %d", &nitems)) |
| 910 | { |
| 911 | if (nitems >= 2048) |
| 912 | smm->configured_event_queue_length = nitems; |
| 913 | else |
| 914 | clib_warning ("event queue length %d too small, ignored", nitems); |
| 915 | } |
Florin Coras | 66b1131 | 2017-07-31 17:18:03 -0700 | [diff] [blame] | 916 | else if (unformat (input, "preallocated-sessions %d", |
| 917 | &smm->preallocated_sessions)) |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 918 | ; |
Florin Coras | 66b1131 | 2017-07-31 17:18:03 -0700 | [diff] [blame] | 919 | else if (unformat (input, "v4-session-table-buckets %d", |
| 920 | &smm->configured_v4_session_table_buckets)) |
| 921 | ; |
| 922 | else if (unformat (input, "v4-halfopen-table-buckets %d", |
| 923 | &smm->configured_v4_halfopen_table_buckets)) |
| 924 | ; |
| 925 | else if (unformat (input, "v6-session-table-buckets %d", |
| 926 | &smm->configured_v6_session_table_buckets)) |
| 927 | ; |
| 928 | else if (unformat (input, "v6-halfopen-table-buckets %d", |
| 929 | &smm->configured_v6_halfopen_table_buckets)) |
| 930 | ; |
| 931 | else if (unformat (input, "v4-session-table-memory %U", |
| 932 | unformat_memory_size, &tmp)) |
| 933 | { |
| 934 | if (tmp >= 0x100000000) |
| 935 | return clib_error_return (0, "memory size %llx (%lld) too large", |
| 936 | tmp, tmp); |
| 937 | smm->configured_v4_session_table_memory = tmp; |
| 938 | } |
| 939 | else if (unformat (input, "v4-halfopen-table-memory %U", |
| 940 | unformat_memory_size, &tmp)) |
| 941 | { |
| 942 | if (tmp >= 0x100000000) |
| 943 | return clib_error_return (0, "memory size %llx (%lld) too large", |
| 944 | tmp, tmp); |
| 945 | smm->configured_v4_halfopen_table_memory = tmp; |
| 946 | } |
| 947 | else if (unformat (input, "v6-session-table-memory %U", |
| 948 | unformat_memory_size, &tmp)) |
| 949 | { |
| 950 | if (tmp >= 0x100000000) |
| 951 | return clib_error_return (0, "memory size %llx (%lld) too large", |
| 952 | tmp, tmp); |
| 953 | smm->configured_v6_session_table_memory = tmp; |
| 954 | } |
| 955 | else if (unformat (input, "v6-halfopen-table-memory %U", |
| 956 | unformat_memory_size, &tmp)) |
| 957 | { |
| 958 | if (tmp >= 0x100000000) |
| 959 | return clib_error_return (0, "memory size %llx (%lld) too large", |
| 960 | tmp, tmp); |
| 961 | smm->configured_v6_halfopen_table_memory = tmp; |
| 962 | } |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 963 | else |
| 964 | return clib_error_return (0, "unknown input `%U'", |
| 965 | format_unformat_error, input); |
| 966 | } |
| 967 | return 0; |
| 968 | } |
| 969 | |
| 970 | VLIB_CONFIG_FUNCTION (session_config_fn, "session"); |
| 971 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 972 | /* |
| 973 | * fd.io coding-style-patch-verification: ON |
| 974 | * |
| 975 | * Local Variables: |
| 976 | * eval: (c-set-style "gnu") |
| 977 | * End: |
| 978 | */ |