blob: 843d474fa198d3ddcdfb8aa99b4b10ada0ed727a [file] [log] [blame]
Dave Barach68b0fb02017-02-28 15:15:56 -05001/*
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 Coras04e53442017-07-16 17:12:15 -070021#include <vnet/session/session_debug.h>
22#include <vnet/session/application.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050023#include <vlibmemory/api.h>
24#include <vnet/dpo/load_balance.h>
25#include <vnet/fib/ip4_fib.h>
Florin Corasa0b34a72017-03-07 01:20:52 -080026#include <vnet/tcp/tcp.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050027
28session_manager_main_t session_manager_main;
Florin Coras04e53442017-07-16 17:12:15 -070029extern transport_proto_vft_t *tp_vfts;
Florin Coras3eb50622017-07-13 01:24:57 -040030
Dave Barach68b0fb02017-02-28 15:15:56 -050031int
Florin Coras6cf30ad2017-04-04 23:08:23 -070032stream_session_create_i (segment_manager_t * sm, transport_connection_t * tc,
Dave Barach52851e62017-08-07 09:35:25 -040033 u8 alloc_fifos, stream_session_t ** ret_s)
Dave Barach68b0fb02017-02-28 15:15:56 -050034{
Florin Coras6cf30ad2017-04-04 23:08:23 -070035 session_manager_main_t *smm = &session_manager_main;
Dave Barach68b0fb02017-02-28 15:15:56 -050036 svm_fifo_t *server_rx_fifo = 0, *server_tx_fifo = 0;
37 u32 fifo_segment_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -070038 u32 pool_index;
Dave Barach68b0fb02017-02-28 15:15:56 -050039 stream_session_t *s;
40 u64 value;
41 u32 thread_index = tc->thread_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -070042 int rv;
Dave Barach68b0fb02017-02-28 15:15:56 -050043
Dave Barach2c25a622017-06-26 11:35:07 -040044 ASSERT (thread_index == vlib_get_thread_index ());
45
Dave Barach68b0fb02017-02-28 15:15:56 -050046 /* Create the session */
Dave Barach1015a1e2017-05-08 19:15:03 -040047 pool_get_aligned (smm->sessions[thread_index], s, CLIB_CACHE_LINE_BYTES);
Dave Barach68b0fb02017-02-28 15:15:56 -050048 memset (s, 0, sizeof (*s));
Dave Barach68b0fb02017-02-28 15:15:56 -050049 pool_index = s - smm->sessions[thread_index];
Dave Barach68b0fb02017-02-28 15:15:56 -050050
Dave Barach52851e62017-08-07 09:35:25 -040051 /* 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 Barach68b0fb02017-02-28 15:15:56 -050064
Dave Barach52851e62017-08-07 09:35:25 -040065 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 Barach68b0fb02017-02-28 15:15:56 -050072
73 /* Initialize state machine, such as it is... */
Florin Coras68810622017-07-24 17:40:28 -070074 s->session_type = session_type_from_proto_and_ip (tc->transport_proto,
75 tc->is_ip4);
Dave Barach68b0fb02017-02-28 15:15:56 -050076 s->session_state = SESSION_STATE_CONNECTING;
Dave Barach68b0fb02017-02-28 15:15:56 -050077 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 Coras6534b7a2017-07-18 05:38:03 -040087 value = stream_session_handle (s);
Florin Coras6cf30ad2017-04-04 23:08:23 -070088 stream_session_table_add_for_tc (tc, value);
Dave Barach68b0fb02017-02-28 15:15:56 -050089
90 *ret_s = s;
91
92 return 0;
93}
94
Florin Corasf6d68ed2017-05-07 19:12:02 -070095/** Enqueue buffer chain tail */
96always_inline int
97session_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 Corasb2215d62017-08-01 16:56:58 -0700101 u32 chain_bi = b->next_buffer, len;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700102 vlib_main_t *vm = vlib_get_main ();
Florin Corasb2215d62017-08-01 16:56:58 -0700103 u8 *data;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700104 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 Barach68b0fb02017-02-28 15:15:56 -0500139/*
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 Corasf6d68ed2017-05-07 19:12:02 -0700145 * @param b Buffer to be enqueued
146 * @param offset Offset at which to start enqueueing if out-of-order
Dave Barach68b0fb02017-02-28 15:15:56 -0500147 * @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 Corasf6d68ed2017-05-07 19:12:02 -0700150 * @param is_in_order Flag to indicate if data is in order
Dave Barach68b0fb02017-02-28 15:15:56 -0500151 * @return Number of bytes enqueued or a negative value if enqueueing failed.
152 */
153int
Florin Corasf6d68ed2017-05-07 19:12:02 -0700154stream_session_enqueue_data (transport_connection_t * tc, vlib_buffer_t * b,
155 u32 offset, u8 queue_event, u8 is_in_order)
Dave Barach68b0fb02017-02-28 15:15:56 -0500156{
157 stream_session_t *s;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700158 int enqueued = 0, rv;
Dave Barach68b0fb02017-02-28 15:15:56 -0500159
160 s = stream_session_get (tc->s_index, tc->thread_index);
161
Florin Corasf6d68ed2017-05-07 19:12:02 -0700162 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 Barach68b0fb02017-02-28 15:15:56 -0500186
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 Corasf6d68ed2017-05-07 19:12:02 -0700203 if (is_in_order)
204 return enqueued;
205
206 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500207}
208
209/** Check if we have space in rx fifo to push more bytes */
210u8
211stream_session_no_space (transport_connection_t * tc, u32 thread_index,
212 u16 data_len)
213{
Florin Coras93992a92017-05-24 18:03:56 -0700214 stream_session_t *s = stream_session_get (tc->s_index, thread_index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500215
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
225u32
Florin Coras93992a92017-05-24 18:03:56 -0700226stream_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 Corasb2215d62017-08-01 16:56:58 -0700229 if (!s->server_tx_fifo)
Florin Coras93992a92017-05-24 18:03:56 -0700230 return 0;
231 return svm_fifo_max_dequeue (s->server_tx_fifo);
232}
233
234int
Dave Barach68b0fb02017-02-28 15:15:56 -0500235stream_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 Corasa5464812017-04-19 13:00:05 -0700239 return svm_fifo_peek (s->server_tx_fifo, offset, max_bytes, buffer);
Dave Barach68b0fb02017-02-28 15:15:56 -0500240}
241
242u32
243stream_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 Corasa5464812017-04-19 13:00:05 -0700246 return svm_fifo_dequeue_drop (s->server_tx_fifo, max_bytes);
Dave Barach68b0fb02017-02-28 15:15:56 -0500247}
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 */
257static int
258stream_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 Barach818eb542017-08-02 13:56:13 -0400269 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 Barach68b0fb02017-02-28 15:15:56 -0500276
Florin Corasd79b41e2017-03-04 05:37:52 -0800277 /* Built-in server? Hand event to the callback... */
278 if (app->cb_fns.builtin_server_rx_callback)
Florin Coras6792ec02017-03-13 03:49:51 -0700279 return app->cb_fns.builtin_server_rx_callback (s);
Florin Corasd79b41e2017-03-04 05:37:52 -0800280
Florin Coras6792ec02017-03-13 03:49:51 -0700281 /* 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 Corasa5464812017-04-19 13:00:05 -0700286 evt.event_type = FIFO_EVENT_APP_RX;
Florin Coras6792ec02017-03-13 03:49:51 -0700287 evt.event_id = serial_number++;
Dave Barach68b0fb02017-02-28 15:15:56 -0500288
Florin Coras6792ec02017-03-13 03:49:51 -0700289 /* 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 Barach68b0fb02017-02-28 15:15:56 -0500302
Florin Corase69f4952017-03-07 10:06:24 -0800303 /* *INDENT-OFF* */
Florin Coras6792ec02017-03-13 03:49:51 -0700304 SESSION_EVT_DBG(SESSION_EVT_ENQ, s, ({
Dave Barach68b0fb02017-02-28 15:15:56 -0500305 ed->data[0] = evt.event_id;
Florin Coras6792ec02017-03-13 03:49:51 -0700306 ed->data[1] = svm_fifo_max_dequeue (s->server_rx_fifo);
Florin Corase69f4952017-03-07 10:06:24 -0800307 }));
308 /* *INDENT-ON* */
Dave Barach68b0fb02017-02-28 15:15:56 -0500309
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 */
321int
322session_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 Barach818eb542017-08-02 13:56:13 -0400336 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 Barach68b0fb02017-02-28 15:15:56 -0500339 {
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 Corasc28764f2017-04-26 00:08:42 -0700355/**
356 * Init fifo tail and head pointers
357 *
358 * Useful if transport uses absolute offsets for tracking ooo segments.
359 */
360void
361stream_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 Corasf03a59a2017-06-09 21:07:32 -0700370int
Florin Coras68810622017-07-24 17:40:28 -0700371stream_session_connect_notify (transport_connection_t * tc, u8 is_fail)
Dave Barach68b0fb02017-02-28 15:15:56 -0500372{
Dave Barach68b0fb02017-02-28 15:15:56 -0500373 application_t *app;
374 stream_session_t *new_s = 0;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700375 u64 handle;
Florin Corasab0289a2017-08-14 11:25:25 -0700376 u32 opaque = 0;
Florin Corasf03a59a2017-06-09 21:07:32 -0700377 int error = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500378
Florin Coras04e53442017-07-16 17:12:15 -0700379 handle = stream_session_half_open_lookup_handle (&tc->lcl_ip, &tc->rmt_ip,
380 tc->lcl_port, tc->rmt_port,
Florin Coras68810622017-07-24 17:40:28 -0700381 tc->transport_proto);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700382 if (handle == HALF_OPEN_LOOKUP_INVALID_VALUE)
Dave Barach68b0fb02017-02-28 15:15:56 -0500383 {
384 clib_warning ("This can't be good!");
Florin Corasf03a59a2017-06-09 21:07:32 -0700385 return -1;
Dave Barach68b0fb02017-02-28 15:15:56 -0500386 }
387
Florin Corasab0289a2017-08-14 11:25:25 -0700388 /* Get the app's index from the handle we stored when opening connection
389 * and the opaque (api_context for external apps) from transport session
390 * index*/
Florin Coras6cf30ad2017-04-04 23:08:23 -0700391 app = application_get (handle >> 32);
Florin Corasab0289a2017-08-14 11:25:25 -0700392 opaque = tc->s_index;
Dave Barach68b0fb02017-02-28 15:15:56 -0500393
394 if (!is_fail)
395 {
Florin Coras6cf30ad2017-04-04 23:08:23 -0700396 segment_manager_t *sm;
Dave Barach52851e62017-08-07 09:35:25 -0400397 u8 alloc_fifos;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700398 sm = application_get_connect_segment_manager (app);
Dave Barach52851e62017-08-07 09:35:25 -0400399 alloc_fifos = application_is_proxy (app);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700400 /* Create new session (svm segments are allocated if needed) */
Dave Barach52851e62017-08-07 09:35:25 -0400401 if (stream_session_create_i (sm, tc, alloc_fifos, &new_s))
Florin Corasf03a59a2017-06-09 21:07:32 -0700402 {
403 is_fail = 1;
404 error = -1;
405 }
406 else
407 new_s->app_index = app->index;
Dave Barach68b0fb02017-02-28 15:15:56 -0500408 }
409
Florin Coras68810622017-07-24 17:40:28 -0700410 /* Notify client application */
Florin Corasab0289a2017-08-14 11:25:25 -0700411 if (app->cb_fns.session_connected_callback (app->index, opaque, new_s,
Florin Coras6534b7a2017-07-18 05:38:03 -0400412 is_fail))
413 {
414 clib_warning ("failed to notify app");
415 if (!is_fail)
416 stream_session_disconnect (new_s);
417 }
418 else
419 {
420 if (!is_fail)
421 new_s->session_state = SESSION_STATE_READY;
422 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500423
424 /* Cleanup session lookup */
Florin Coras68810622017-07-24 17:40:28 -0700425 stream_session_half_open_table_del (tc);
Florin Corasf03a59a2017-06-09 21:07:32 -0700426
427 return error;
Dave Barach68b0fb02017-02-28 15:15:56 -0500428}
429
430void
431stream_session_accept_notify (transport_connection_t * tc)
432{
433 application_t *server;
434 stream_session_t *s;
435
436 s = stream_session_get (tc->s_index, tc->thread_index);
437 server = application_get (s->app_index);
438 server->cb_fns.session_accept_callback (s);
439}
440
441/**
442 * Notification from transport that connection is being closed.
443 *
444 * A disconnect is sent to application but state is not removed. Once
445 * disconnect is acknowledged by application, session disconnect is called.
446 * Ultimately this leads to close being called on transport (passive close).
447 */
448void
449stream_session_disconnect_notify (transport_connection_t * tc)
450{
451 application_t *server;
452 stream_session_t *s;
453
454 s = stream_session_get (tc->s_index, tc->thread_index);
455 server = application_get (s->app_index);
456 server->cb_fns.session_disconnect_callback (s);
457}
458
459/**
460 * Cleans up session and associated app if needed.
461 */
462void
463stream_session_delete (stream_session_t * s)
464{
465 session_manager_main_t *smm = vnet_get_session_manager_main ();
Florin Coras6534b7a2017-07-18 05:38:03 -0400466 int rv;
Dave Barach68b0fb02017-02-28 15:15:56 -0500467
Florin Corasd79b41e2017-03-04 05:37:52 -0800468 /* Delete from the main lookup table. */
Florin Coras04e53442017-07-16 17:12:15 -0700469 if ((rv = stream_session_table_del (s)))
Florin Coras6534b7a2017-07-18 05:38:03 -0400470 clib_warning ("hash delete error, rv %d", rv);
Dave Barach68b0fb02017-02-28 15:15:56 -0500471
472 /* Cleanup fifo segments */
Florin Coras6cf30ad2017-04-04 23:08:23 -0700473 segment_manager_dealloc_fifos (s->svm_segment_index, s->server_rx_fifo,
474 s->server_tx_fifo);
Dave Barach68b0fb02017-02-28 15:15:56 -0500475
476 pool_put (smm->sessions[s->thread_index], s);
Florin Coras6534b7a2017-07-18 05:38:03 -0400477 if (CLIB_DEBUG)
478 memset (s, 0xFA, sizeof (*s));
Dave Barach68b0fb02017-02-28 15:15:56 -0500479}
480
481/**
482 * Notification from transport that connection is being deleted
483 *
484 * This should be called only on previously fully established sessions. For
485 * instance failed connects should call stream_session_connect_notify and
486 * indicate that the connect has failed.
487 */
488void
489stream_session_delete_notify (transport_connection_t * tc)
490{
491 stream_session_t *s;
492
Florin Corase04c2992017-03-01 08:17:34 -0800493 /* App might've been removed already */
Dave Barach68b0fb02017-02-28 15:15:56 -0500494 s = stream_session_get_if_valid (tc->s_index, tc->thread_index);
495 if (!s)
496 {
Dave Barach68b0fb02017-02-28 15:15:56 -0500497 return;
498 }
499 stream_session_delete (s);
500}
501
502/**
503 * Notify application that connection has been reset.
504 */
505void
506stream_session_reset_notify (transport_connection_t * tc)
507{
508 stream_session_t *s;
509 application_t *app;
510 s = stream_session_get (tc->s_index, tc->thread_index);
511
512 app = application_get (s->app_index);
513 app->cb_fns.session_reset_callback (s);
514}
515
516/**
517 * Accept a stream session. Optionally ping the server by callback.
518 */
519int
520stream_session_accept (transport_connection_t * tc, u32 listener_index,
521 u8 sst, u8 notify)
522{
Dave Barach68b0fb02017-02-28 15:15:56 -0500523 application_t *server;
524 stream_session_t *s, *listener;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700525 segment_manager_t *sm;
Dave Barach68b0fb02017-02-28 15:15:56 -0500526
527 int rv;
528
529 /* Find the server */
Florin Coras6cf30ad2017-04-04 23:08:23 -0700530 listener = listen_session_get (sst, listener_index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500531 server = application_get (listener->app_index);
532
Florin Coras6cf30ad2017-04-04 23:08:23 -0700533 sm = application_get_listen_segment_manager (server, listener);
Dave Barach52851e62017-08-07 09:35:25 -0400534 if ((rv = stream_session_create_i (sm, tc, 1, &s)))
Dave Barach68b0fb02017-02-28 15:15:56 -0500535 return rv;
536
Florin Coras6cf30ad2017-04-04 23:08:23 -0700537 s->app_index = server->index;
538 s->listener_index = listener_index;
Dave Barach2c25a622017-06-26 11:35:07 -0400539 s->session_state = SESSION_STATE_ACCEPTING;
Dave Barach68b0fb02017-02-28 15:15:56 -0500540
541 /* Shoulder-tap the server */
542 if (notify)
543 {
544 server->cb_fns.session_accept_callback (s);
545 }
546
547 return 0;
548}
549
Florin Coras6cf30ad2017-04-04 23:08:23 -0700550/**
551 * Ask transport to open connection to remote transport endpoint.
552 *
553 * Stores handle for matching request with reply since the call can be
554 * asynchronous. For instance, for TCP the 3-way handshake must complete
555 * before reply comes. Session is only created once connection is established.
556 *
557 * @param app_index Index of the application requesting the connect
558 * @param st Session type requested.
559 * @param tep Remote transport endpoint
560 * @param res Resulting transport connection .
561 */
Florin Corase04c2992017-03-01 08:17:34 -0800562int
Florin Coras6cf30ad2017-04-04 23:08:23 -0700563stream_session_open (u32 app_index, session_type_t st,
Florin Coras04e53442017-07-16 17:12:15 -0700564 transport_endpoint_t * rmt,
Florin Coras6cf30ad2017-04-04 23:08:23 -0700565 transport_connection_t ** res)
Dave Barach68b0fb02017-02-28 15:15:56 -0500566{
567 transport_connection_t *tc;
Florin Corase04c2992017-03-01 08:17:34 -0800568 int rv;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700569 u64 handle;
Dave Barach68b0fb02017-02-28 15:15:56 -0500570
Florin Coras04e53442017-07-16 17:12:15 -0700571 rv = tp_vfts[st].open (rmt);
Florin Corase04c2992017-03-01 08:17:34 -0800572 if (rv < 0)
573 {
574 clib_warning ("Transport failed to open connection.");
575 return VNET_API_ERROR_SESSION_CONNECT_FAIL;
576 }
577
Florin Coras6cf30ad2017-04-04 23:08:23 -0700578 tc = tp_vfts[st].get_half_open ((u32) rv);
Dave Barach68b0fb02017-02-28 15:15:56 -0500579
Florin Coras6cf30ad2017-04-04 23:08:23 -0700580 /* Save app and tc index. The latter is needed to help establish the
581 * connection while the former is needed when the connect notify comes
582 * and we have to notify the external app */
583 handle = (((u64) app_index) << 32) | (u64) tc->c_index;
Dave Barach68b0fb02017-02-28 15:15:56 -0500584
585 /* Add to the half-open lookup table */
Florin Coras68810622017-07-24 17:40:28 -0700586 stream_session_half_open_table_add (tc, handle);
Florin Corase04c2992017-03-01 08:17:34 -0800587
Florin Coras6cf30ad2017-04-04 23:08:23 -0700588 *res = tc;
589
590 return 0;
591}
592
593/**
594 * Ask transport to listen on local transport endpoint.
595 *
596 * @param s Session for which listen will be called. Note that unlike
597 * established sessions, listen sessions are not associated to a
598 * thread.
599 * @param tep Local endpoint to be listened on.
600 */
601int
602stream_session_listen (stream_session_t * s, transport_endpoint_t * tep)
603{
604 transport_connection_t *tc;
605 u32 tci;
606
607 /* Transport bind/listen */
Florin Coras04e53442017-07-16 17:12:15 -0700608 tci = tp_vfts[s->session_type].bind (s->session_index, tep);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700609
610 if (tci == (u32) ~ 0)
611 return -1;
612
613 /* Attach transport to session */
614 s->connection_index = tci;
615 tc = tp_vfts[s->session_type].get_listener (tci);
616
617 /* Weird but handle it ... */
618 if (tc == 0)
619 return -1;
620
621 /* Add to the main lookup table */
622 stream_session_table_add_for_tc (tc, s->session_index);
623
624 return 0;
625}
626
627/**
628 * Ask transport to stop listening on local transport endpoint.
629 *
630 * @param s Session to stop listening on. It must be in state LISTENING.
631 */
632int
633stream_session_stop_listen (stream_session_t * s)
634{
635 transport_connection_t *tc;
636
637 if (s->session_state != SESSION_STATE_LISTENING)
638 {
639 clib_warning ("not a listening session");
640 return -1;
641 }
642
643 tc = tp_vfts[s->session_type].get_listener (s->connection_index);
644 if (!tc)
645 {
646 clib_warning ("no transport");
647 return VNET_API_ERROR_ADDRESS_NOT_IN_USE;
648 }
649
650 stream_session_table_del_for_tc (tc);
651 tp_vfts[s->session_type].unbind (s->connection_index);
Florin Corase04c2992017-03-01 08:17:34 -0800652 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500653}
654
Florin Corasa5464812017-04-19 13:00:05 -0700655void
656session_send_session_evt_to_thread (u64 session_handle,
657 fifo_event_type_t evt_type,
658 u32 thread_index)
659{
660 static u16 serial_number = 0;
661 session_fifo_event_t evt;
662 unix_shared_memory_queue_t *q;
663
664 /* Fabricate event */
665 evt.session_handle = session_handle;
666 evt.event_type = evt_type;
667 evt.event_id = serial_number++;
668
669 q = session_manager_get_vpp_event_queue (thread_index);
670
671 /* Based on request block (or not) for lack of space */
672 if (PREDICT_TRUE (q->cursize < q->maxsize))
Florin Corasf03a59a2017-06-09 21:07:32 -0700673 {
674 if (unix_shared_memory_queue_add (q, (u8 *) & evt,
675 1 /* do wait for mutex */ ))
676 {
677 clib_warning ("failed to enqueue evt");
678 }
679 }
Florin Corasa5464812017-04-19 13:00:05 -0700680 else
681 {
682 clib_warning ("queue full");
683 return;
684 }
685}
686
Dave Barach68b0fb02017-02-28 15:15:56 -0500687/**
688 * Disconnect session and propagate to transport. This should eventually
689 * result in a delete notification that allows us to cleanup session state.
690 * Called for both active/passive disconnects.
Florin Corasa5464812017-04-19 13:00:05 -0700691 *
692 * Should be called from the session's thread.
Dave Barach68b0fb02017-02-28 15:15:56 -0500693 */
694void
695stream_session_disconnect (stream_session_t * s)
696{
Dave Barach68b0fb02017-02-28 15:15:56 -0500697 s->session_state = SESSION_STATE_CLOSED;
Florin Corasd79b41e2017-03-04 05:37:52 -0800698 tp_vfts[s->session_type].close (s->connection_index, s->thread_index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500699}
700
701/**
702 * Cleanup transport and session state.
Florin Corasd79b41e2017-03-04 05:37:52 -0800703 *
704 * Notify transport of the cleanup, wait for a delete notify to actually
705 * remove the session state.
Dave Barach68b0fb02017-02-28 15:15:56 -0500706 */
707void
708stream_session_cleanup (stream_session_t * s)
709{
Florin Corasd79b41e2017-03-04 05:37:52 -0800710 int rv;
711
712 s->session_state = SESSION_STATE_CLOSED;
713
714 /* Delete from the main lookup table to avoid more enqueues */
Florin Coras04e53442017-07-16 17:12:15 -0700715 rv = stream_session_table_del (s);
Florin Corasd79b41e2017-03-04 05:37:52 -0800716 if (rv)
717 clib_warning ("hash delete error, rv %d", rv);
718
Dave Barach68b0fb02017-02-28 15:15:56 -0500719 tp_vfts[s->session_type].cleanup (s->connection_index, s->thread_index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500720}
721
Florin Corasa5464812017-04-19 13:00:05 -0700722/**
723 * Allocate vpp event queue (once) per worker thread
724 */
725void
726session_vpp_event_queue_allocate (session_manager_main_t * smm,
727 u32 thread_index)
728{
729 api_main_t *am = &api_main;
730 void *oldheap;
Dave Barach10d8cc62017-05-30 09:30:07 -0400731 u32 event_queue_length = 2048;
Florin Corasa5464812017-04-19 13:00:05 -0700732
733 if (smm->vpp_event_queues[thread_index] == 0)
734 {
735 /* Allocate event fifo in the /vpe-api shared-memory segment */
736 oldheap = svm_push_data_heap (am->vlib_rp);
737
Dave Barach10d8cc62017-05-30 09:30:07 -0400738 if (smm->configured_event_queue_length)
739 event_queue_length = smm->configured_event_queue_length;
740
Florin Corasa5464812017-04-19 13:00:05 -0700741 smm->vpp_event_queues[thread_index] =
Dave Barach10d8cc62017-05-30 09:30:07 -0400742 unix_shared_memory_queue_init
743 (event_queue_length,
744 sizeof (session_fifo_event_t), 0 /* consumer pid */ ,
745 0 /* (do not) send signal when queue non-empty */ );
Florin Corasa5464812017-04-19 13:00:05 -0700746
747 svm_pop_heap (oldheap);
748 }
749}
750
Dave Barach2c25a622017-06-26 11:35:07 -0400751session_type_t
752session_type_from_proto_and_ip (transport_proto_t proto, u8 is_ip4)
753{
754 if (proto == TRANSPORT_PROTO_TCP)
755 {
756 if (is_ip4)
757 return SESSION_TYPE_IP4_TCP;
758 else
759 return SESSION_TYPE_IP6_TCP;
760 }
761 else
762 {
763 if (is_ip4)
764 return SESSION_TYPE_IP4_UDP;
765 else
766 return SESSION_TYPE_IP6_UDP;
767 }
768
769 return SESSION_N_TYPES;
770}
771
Dave Barach68b0fb02017-02-28 15:15:56 -0500772static clib_error_t *
Florin Corase04c2992017-03-01 08:17:34 -0800773session_manager_main_enable (vlib_main_t * vm)
Dave Barach68b0fb02017-02-28 15:15:56 -0500774{
Dave Barach68b0fb02017-02-28 15:15:56 -0500775 session_manager_main_t *smm = &session_manager_main;
Florin Corase04c2992017-03-01 08:17:34 -0800776 vlib_thread_main_t *vtm = vlib_get_thread_main ();
777 u32 num_threads;
Florin Coras66b11312017-07-31 17:18:03 -0700778 u32 preallocated_sessions_per_worker;
Dave Barach68b0fb02017-02-28 15:15:56 -0500779 int i;
780
Dave Barach68b0fb02017-02-28 15:15:56 -0500781 num_threads = 1 /* main thread */ + vtm->n_threads;
782
783 if (num_threads < 1)
784 return clib_error_return (0, "n_thread_stacks not set");
785
786 /* $$$ config parameters */
787 svm_fifo_segment_init (0x200000000ULL /* first segment base VA */ ,
788 20 /* timeout in seconds */ );
789
790 /* configure per-thread ** vectors */
791 vec_validate (smm->sessions, num_threads - 1);
792 vec_validate (smm->session_indices_to_enqueue_by_thread, num_threads - 1);
793 vec_validate (smm->tx_buffers, num_threads - 1);
Dave Barachacd2a6a2017-05-16 17:41:34 -0400794 vec_validate (smm->pending_event_vector, num_threads - 1);
795 vec_validate (smm->free_event_vector, num_threads - 1);
Dave Barach68b0fb02017-02-28 15:15:56 -0500796 vec_validate (smm->current_enqueue_epoch, num_threads - 1);
797 vec_validate (smm->vpp_event_queues, num_threads - 1);
798
Dave Barachacd2a6a2017-05-16 17:41:34 -0400799 for (i = 0; i < num_threads; i++)
800 {
801 vec_validate (smm->free_event_vector[i], 0);
802 _vec_len (smm->free_event_vector[i]) = 0;
803 vec_validate (smm->pending_event_vector[i], 0);
804 _vec_len (smm->pending_event_vector[i]) = 0;
805 }
806
Florin Coras3e350af2017-03-30 02:54:28 -0700807#if SESSION_DBG
808 vec_validate (smm->last_event_poll_by_thread, num_threads - 1);
809#endif
810
Florin Coras6cf30ad2017-04-04 23:08:23 -0700811 /* Allocate vpp event queues */
812 for (i = 0; i < vec_len (smm->vpp_event_queues); i++)
813 session_vpp_event_queue_allocate (smm, i);
814
Florin Coras66b11312017-07-31 17:18:03 -0700815 /* Preallocate sessions */
816 if (num_threads == 1)
Dave Barach68b0fb02017-02-28 15:15:56 -0500817 {
Florin Coras66b11312017-07-31 17:18:03 -0700818 for (i = 0; i < smm->preallocated_sessions; i++)
819 {
820 stream_session_t *ss __attribute__ ((unused));
821 pool_get_aligned (smm->sessions[0], ss, CLIB_CACHE_LINE_BYTES);
822 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500823
Florin Coras66b11312017-07-31 17:18:03 -0700824 for (i = 0; i < smm->preallocated_sessions; i++)
825 pool_put_index (smm->sessions[0], i);
826 }
827 else
828 {
829 int j;
830 preallocated_sessions_per_worker = smm->preallocated_sessions /
831 (num_threads - 1);
832
833 for (j = 1; j < num_threads; j++)
834 {
835 for (i = 0; i < preallocated_sessions_per_worker; i++)
836 {
837 stream_session_t *ss __attribute__ ((unused));
838 pool_get_aligned (smm->sessions[j], ss, CLIB_CACHE_LINE_BYTES);
839 }
840 for (i = 0; i < preallocated_sessions_per_worker; i++)
841 pool_put_index (smm->sessions[j], i);
842 }
843 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500844
Florin Coras04e53442017-07-16 17:12:15 -0700845 session_lookup_init ();
Dave Barach68b0fb02017-02-28 15:15:56 -0500846
Florin Corase04c2992017-03-01 08:17:34 -0800847 smm->is_enabled = 1;
848
Florin Corasa0b34a72017-03-07 01:20:52 -0800849 /* Enable TCP transport */
850 vnet_tcp_enable_disable (vm, 1);
851
Dave Barach68b0fb02017-02-28 15:15:56 -0500852 return 0;
853}
854
Florin Corasa5464812017-04-19 13:00:05 -0700855void
856session_node_enable_disable (u8 is_en)
857{
858 u8 state = is_en ? VLIB_NODE_STATE_POLLING : VLIB_NODE_STATE_DISABLED;
859 /* *INDENT-OFF* */
860 foreach_vlib_main (({
861 vlib_node_set_state (this_vlib_main, session_queue_node.index,
862 state);
863 }));
864 /* *INDENT-ON* */
865}
866
Florin Corase04c2992017-03-01 08:17:34 -0800867clib_error_t *
868vnet_session_enable_disable (vlib_main_t * vm, u8 is_en)
869{
870 if (is_en)
871 {
872 if (session_manager_main.is_enabled)
873 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500874
Florin Corasa5464812017-04-19 13:00:05 -0700875 session_node_enable_disable (is_en);
Florin Corase04c2992017-03-01 08:17:34 -0800876
877 return session_manager_main_enable (vm);
878 }
879 else
880 {
881 session_manager_main.is_enabled = 0;
Florin Corasa5464812017-04-19 13:00:05 -0700882 session_node_enable_disable (is_en);
Florin Corase04c2992017-03-01 08:17:34 -0800883 }
884
885 return 0;
886}
887
Florin Corase04c2992017-03-01 08:17:34 -0800888clib_error_t *
889session_manager_main_init (vlib_main_t * vm)
890{
891 session_manager_main_t *smm = &session_manager_main;
Florin Corase04c2992017-03-01 08:17:34 -0800892 smm->is_enabled = 0;
Florin Corase04c2992017-03-01 08:17:34 -0800893 return 0;
894}
895
Dave Barach2c25a622017-06-26 11:35:07 -0400896VLIB_INIT_FUNCTION (session_manager_main_init);
897
898static clib_error_t *
899session_config_fn (vlib_main_t * vm, unformat_input_t * input)
Dave Barach10d8cc62017-05-30 09:30:07 -0400900{
901 session_manager_main_t *smm = &session_manager_main;
902 u32 nitems;
Florin Coras66b11312017-07-31 17:18:03 -0700903 uword tmp;
Dave Barach10d8cc62017-05-30 09:30:07 -0400904
905 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
906 {
907 if (unformat (input, "event-queue-length %d", &nitems))
908 {
909 if (nitems >= 2048)
910 smm->configured_event_queue_length = nitems;
911 else
912 clib_warning ("event queue length %d too small, ignored", nitems);
913 }
Florin Coras66b11312017-07-31 17:18:03 -0700914 else if (unformat (input, "preallocated-sessions %d",
915 &smm->preallocated_sessions))
Dave Barach2c25a622017-06-26 11:35:07 -0400916 ;
Florin Coras66b11312017-07-31 17:18:03 -0700917 else if (unformat (input, "v4-session-table-buckets %d",
918 &smm->configured_v4_session_table_buckets))
919 ;
920 else if (unformat (input, "v4-halfopen-table-buckets %d",
921 &smm->configured_v4_halfopen_table_buckets))
922 ;
923 else if (unformat (input, "v6-session-table-buckets %d",
924 &smm->configured_v6_session_table_buckets))
925 ;
926 else if (unformat (input, "v6-halfopen-table-buckets %d",
927 &smm->configured_v6_halfopen_table_buckets))
928 ;
929 else if (unformat (input, "v4-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_v4_session_table_memory = tmp;
936 }
937 else if (unformat (input, "v4-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_v4_halfopen_table_memory = tmp;
944 }
945 else if (unformat (input, "v6-session-table-memory %U",
946 unformat_memory_size, &tmp))
947 {
948 if (tmp >= 0x100000000)
949 return clib_error_return (0, "memory size %llx (%lld) too large",
950 tmp, tmp);
951 smm->configured_v6_session_table_memory = tmp;
952 }
953 else if (unformat (input, "v6-halfopen-table-memory %U",
954 unformat_memory_size, &tmp))
955 {
956 if (tmp >= 0x100000000)
957 return clib_error_return (0, "memory size %llx (%lld) too large",
958 tmp, tmp);
959 smm->configured_v6_halfopen_table_memory = tmp;
960 }
Dave Barach10d8cc62017-05-30 09:30:07 -0400961 else
962 return clib_error_return (0, "unknown input `%U'",
963 format_unformat_error, input);
964 }
965 return 0;
966}
967
968VLIB_CONFIG_FUNCTION (session_config_fn, "session");
969
Dave Barach68b0fb02017-02-28 15:15:56 -0500970/*
971 * fd.io coding-style-patch-verification: ON
972 *
973 * Local Variables:
974 * eval: (c-set-style "gnu")
975 * End:
976 */