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