blob: 88b38f15a61351bd21122835b947931926c0af2f [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 Corascea194d2017-10-02 00:18:51 -070088 session_lookup_add_connection (tc, value);
Dave Barach68b0fb02017-02-28 15:15:56 -050089
90 *ret_s = s;
91
92 return 0;
93}
94
Florin Coras1f152cd2017-08-18 19:28:03 -070095/**
96 * Discards bytes from buffer chain
97 *
98 * It discards n_bytes_to_drop starting at first buffer after chain_b
99 */
100always_inline void
101session_enqueue_discard_chain_bytes (vlib_main_t * vm, vlib_buffer_t * b,
102 vlib_buffer_t ** chain_b,
103 u32 n_bytes_to_drop)
104{
105 vlib_buffer_t *next = *chain_b;
106 u32 to_drop = n_bytes_to_drop;
107 ASSERT (b->flags & VLIB_BUFFER_NEXT_PRESENT);
108 while (to_drop && (next->flags & VLIB_BUFFER_NEXT_PRESENT))
109 {
110 next = vlib_get_buffer (vm, next->next_buffer);
111 if (next->current_length > to_drop)
112 {
113 vlib_buffer_advance (next, to_drop);
114 to_drop = 0;
115 }
116 else
117 {
118 to_drop -= next->current_length;
119 next->current_length = 0;
120 }
121 }
122 *chain_b = next;
123
124 if (to_drop == 0)
125 b->total_length_not_including_first_buffer -= n_bytes_to_drop;
126}
127
128/**
129 * Enqueue buffer chain tail
130 */
Florin Corasf6d68ed2017-05-07 19:12:02 -0700131always_inline int
132session_enqueue_chain_tail (stream_session_t * s, vlib_buffer_t * b,
133 u32 offset, u8 is_in_order)
134{
135 vlib_buffer_t *chain_b;
Florin Coras1f152cd2017-08-18 19:28:03 -0700136 u32 chain_bi, len, diff;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700137 vlib_main_t *vm = vlib_get_main ();
Florin Corasb2215d62017-08-01 16:56:58 -0700138 u8 *data;
Florin Coras1f152cd2017-08-18 19:28:03 -0700139 u32 written = 0;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700140 int rv = 0;
141
Florin Coras1f152cd2017-08-18 19:28:03 -0700142 if (is_in_order && offset)
143 {
144 diff = offset - b->current_length;
145 if (diff > b->total_length_not_including_first_buffer)
146 return 0;
147 chain_b = b;
148 session_enqueue_discard_chain_bytes (vm, b, &chain_b, diff);
149 chain_bi = vlib_get_buffer_index (vm, chain_b);
150 }
151 else
152 chain_bi = b->next_buffer;
153
Florin Corasf6d68ed2017-05-07 19:12:02 -0700154 do
155 {
156 chain_b = vlib_get_buffer (vm, chain_bi);
157 data = vlib_buffer_get_current (chain_b);
158 len = chain_b->current_length;
Florin Coras1f152cd2017-08-18 19:28:03 -0700159 if (!len)
160 continue;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700161 if (is_in_order)
162 {
163 rv = svm_fifo_enqueue_nowait (s->server_rx_fifo, len, data);
Florin Coras1f152cd2017-08-18 19:28:03 -0700164 if (rv == len)
165 {
166 written += rv;
167 }
168 else if (rv < len)
Florin Corasf6d68ed2017-05-07 19:12:02 -0700169 {
170 return (rv > 0) ? (written + rv) : written;
171 }
Florin Coras1f152cd2017-08-18 19:28:03 -0700172 else if (rv > len)
173 {
174 written += rv;
175
176 /* written more than what was left in chain */
177 if (written > b->total_length_not_including_first_buffer)
178 return written;
179
180 /* drop the bytes that have already been delivered */
181 session_enqueue_discard_chain_bytes (vm, b, &chain_b, rv - len);
182 }
Florin Corasf6d68ed2017-05-07 19:12:02 -0700183 }
184 else
185 {
186 rv = svm_fifo_enqueue_with_offset (s->server_rx_fifo, offset, len,
187 data);
188 if (rv)
Florin Coras1f152cd2017-08-18 19:28:03 -0700189 {
190 clib_warning ("failed to enqueue multi-buffer seg");
191 return -1;
192 }
Florin Corasf6d68ed2017-05-07 19:12:02 -0700193 offset += len;
194 }
195 }
196 while ((chain_bi = (chain_b->flags & VLIB_BUFFER_NEXT_PRESENT)
197 ? chain_b->next_buffer : 0));
198
199 if (is_in_order)
200 return written;
201
202 return 0;
203}
204
Dave Barach68b0fb02017-02-28 15:15:56 -0500205/*
206 * Enqueue data for delivery to session peer. Does not notify peer of enqueue
207 * event but on request can queue notification events for later delivery by
208 * calling stream_server_flush_enqueue_events().
209 *
210 * @param tc Transport connection which is to be enqueued data
Florin Corasf6d68ed2017-05-07 19:12:02 -0700211 * @param b Buffer to be enqueued
212 * @param offset Offset at which to start enqueueing if out-of-order
Dave Barach68b0fb02017-02-28 15:15:56 -0500213 * @param queue_event Flag to indicate if peer is to be notified or if event
214 * is to be queued. The former is useful when more data is
215 * enqueued and only one event is to be generated.
Florin Corasf6d68ed2017-05-07 19:12:02 -0700216 * @param is_in_order Flag to indicate if data is in order
Dave Barach68b0fb02017-02-28 15:15:56 -0500217 * @return Number of bytes enqueued or a negative value if enqueueing failed.
218 */
219int
Florin Corasf6d68ed2017-05-07 19:12:02 -0700220stream_session_enqueue_data (transport_connection_t * tc, vlib_buffer_t * b,
221 u32 offset, u8 queue_event, u8 is_in_order)
Dave Barach68b0fb02017-02-28 15:15:56 -0500222{
223 stream_session_t *s;
Florin Coras1f152cd2017-08-18 19:28:03 -0700224 int enqueued = 0, rv, in_order_off;
Dave Barach68b0fb02017-02-28 15:15:56 -0500225
Florin Corascea194d2017-10-02 00:18:51 -0700226 s = session_get (tc->s_index, tc->thread_index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500227
Florin Corasf6d68ed2017-05-07 19:12:02 -0700228 if (is_in_order)
229 {
Florin Coras1f152cd2017-08-18 19:28:03 -0700230 enqueued = svm_fifo_enqueue_nowait (s->server_rx_fifo,
231 b->current_length,
232 vlib_buffer_get_current (b));
233 if (PREDICT_FALSE ((b->flags & VLIB_BUFFER_NEXT_PRESENT)
234 && enqueued >= 0))
Florin Corasf6d68ed2017-05-07 19:12:02 -0700235 {
Florin Coras1f152cd2017-08-18 19:28:03 -0700236 in_order_off = enqueued > b->current_length ? enqueued : 0;
237 rv = session_enqueue_chain_tail (s, b, in_order_off, 1);
238 if (rv > 0)
239 enqueued += rv;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700240 }
241 }
242 else
243 {
244 rv = svm_fifo_enqueue_with_offset (s->server_rx_fifo, offset,
245 b->current_length,
246 vlib_buffer_get_current (b));
247 if (PREDICT_FALSE ((b->flags & VLIB_BUFFER_NEXT_PRESENT) && !rv))
Florin Coras1f152cd2017-08-18 19:28:03 -0700248 session_enqueue_chain_tail (s, b, offset + b->current_length, 0);
249 /* if something was enqueued, report even this as success for ooo
250 * segment handling */
251 return rv;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700252 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500253
254 if (queue_event)
255 {
256 /* Queue RX event on this fifo. Eventually these will need to be flushed
257 * by calling stream_server_flush_enqueue_events () */
258 session_manager_main_t *smm = vnet_get_session_manager_main ();
259 u32 thread_index = s->thread_index;
260 u32 my_enqueue_epoch = smm->current_enqueue_epoch[thread_index];
261
262 if (s->enqueue_epoch != my_enqueue_epoch)
263 {
264 s->enqueue_epoch = my_enqueue_epoch;
265 vec_add1 (smm->session_indices_to_enqueue_by_thread[thread_index],
266 s - smm->sessions[thread_index]);
267 }
268 }
269
Chris Lukeb2bcad62017-09-18 08:51:22 -0400270 return enqueued;
Dave Barach68b0fb02017-02-28 15:15:56 -0500271}
272
273/** Check if we have space in rx fifo to push more bytes */
274u8
275stream_session_no_space (transport_connection_t * tc, u32 thread_index,
276 u16 data_len)
277{
Florin Corascea194d2017-10-02 00:18:51 -0700278 stream_session_t *s = session_get (tc->s_index, thread_index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500279
280 if (PREDICT_FALSE (s->session_state != SESSION_STATE_READY))
281 return 1;
282
283 if (data_len > svm_fifo_max_enqueue (s->server_rx_fifo))
284 return 1;
285
286 return 0;
287}
288
289u32
Florin Coras93992a92017-05-24 18:03:56 -0700290stream_session_tx_fifo_max_dequeue (transport_connection_t * tc)
291{
Florin Corascea194d2017-10-02 00:18:51 -0700292 stream_session_t *s = session_get (tc->s_index, tc->thread_index);
Florin Corasb2215d62017-08-01 16:56:58 -0700293 if (!s->server_tx_fifo)
Florin Coras93992a92017-05-24 18:03:56 -0700294 return 0;
295 return svm_fifo_max_dequeue (s->server_tx_fifo);
296}
297
298int
Dave Barach68b0fb02017-02-28 15:15:56 -0500299stream_session_peek_bytes (transport_connection_t * tc, u8 * buffer,
300 u32 offset, u32 max_bytes)
301{
Florin Corascea194d2017-10-02 00:18:51 -0700302 stream_session_t *s = session_get (tc->s_index, tc->thread_index);
Florin Corasa5464812017-04-19 13:00:05 -0700303 return svm_fifo_peek (s->server_tx_fifo, offset, max_bytes, buffer);
Dave Barach68b0fb02017-02-28 15:15:56 -0500304}
305
306u32
307stream_session_dequeue_drop (transport_connection_t * tc, u32 max_bytes)
308{
Florin Corascea194d2017-10-02 00:18:51 -0700309 stream_session_t *s = session_get (tc->s_index, tc->thread_index);
Florin Corasa5464812017-04-19 13:00:05 -0700310 return svm_fifo_dequeue_drop (s->server_tx_fifo, max_bytes);
Dave Barach68b0fb02017-02-28 15:15:56 -0500311}
312
313/**
314 * Notify session peer that new data has been enqueued.
315 *
316 * @param s Stream session for which the event is to be generated.
317 * @param block Flag to indicate if call should block if event queue is full.
318 *
319 * @return 0 on succes or negative number if failed to send notification.
320 */
321static int
322stream_session_enqueue_notify (stream_session_t * s, u8 block)
323{
324 application_t *app;
325 session_fifo_event_t evt;
326 unix_shared_memory_queue_t *q;
327 static u32 serial_number;
328
329 if (PREDICT_FALSE (s->session_state == SESSION_STATE_CLOSED))
Florin Coras50958952017-08-29 14:50:13 -0700330 {
331 /* Session is closed so app will never clean up. Flush rx fifo */
332 u32 to_dequeue = svm_fifo_max_dequeue (s->server_rx_fifo);
333 if (to_dequeue)
334 svm_fifo_dequeue_drop (s->server_rx_fifo, to_dequeue);
335 return 0;
336 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500337
338 /* Get session's server */
Dave Barach818eb542017-08-02 13:56:13 -0400339 app = application_get_if_valid (s->app_index);
340
341 if (PREDICT_FALSE (app == 0))
342 {
343 clib_warning ("invalid s->app_index = %d", s->app_index);
344 return 0;
345 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500346
Florin Corasd79b41e2017-03-04 05:37:52 -0800347 /* Built-in server? Hand event to the callback... */
348 if (app->cb_fns.builtin_server_rx_callback)
Florin Coras6792ec02017-03-13 03:49:51 -0700349 return app->cb_fns.builtin_server_rx_callback (s);
Florin Corasd79b41e2017-03-04 05:37:52 -0800350
Florin Coras6792ec02017-03-13 03:49:51 -0700351 /* If no event, send one */
352 if (svm_fifo_set_event (s->server_rx_fifo))
353 {
354 /* Fabricate event */
355 evt.fifo = s->server_rx_fifo;
Florin Corasa5464812017-04-19 13:00:05 -0700356 evt.event_type = FIFO_EVENT_APP_RX;
Florin Coras6792ec02017-03-13 03:49:51 -0700357 evt.event_id = serial_number++;
Dave Barach68b0fb02017-02-28 15:15:56 -0500358
Florin Coras6792ec02017-03-13 03:49:51 -0700359 /* Add event to server's event queue */
360 q = app->event_queue;
361
362 /* Based on request block (or not) for lack of space */
363 if (block || PREDICT_TRUE (q->cursize < q->maxsize))
364 unix_shared_memory_queue_add (app->event_queue, (u8 *) & evt,
365 0 /* do wait for mutex */ );
366 else
367 {
368 clib_warning ("fifo full");
369 return -1;
370 }
371 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500372
Florin Corase69f4952017-03-07 10:06:24 -0800373 /* *INDENT-OFF* */
Florin Coras6792ec02017-03-13 03:49:51 -0700374 SESSION_EVT_DBG(SESSION_EVT_ENQ, s, ({
Dave Barach68b0fb02017-02-28 15:15:56 -0500375 ed->data[0] = evt.event_id;
Florin Coras6792ec02017-03-13 03:49:51 -0700376 ed->data[1] = svm_fifo_max_dequeue (s->server_rx_fifo);
Florin Corase69f4952017-03-07 10:06:24 -0800377 }));
378 /* *INDENT-ON* */
Dave Barach68b0fb02017-02-28 15:15:56 -0500379
380 return 0;
381}
382
383/**
384 * Flushes queue of sessions that are to be notified of new data
385 * enqueued events.
386 *
387 * @param thread_index Thread index for which the flush is to be performed.
388 * @return 0 on success or a positive number indicating the number of
389 * failures due to API queue being full.
390 */
391int
392session_manager_flush_enqueue_events (u32 thread_index)
393{
394 session_manager_main_t *smm = &session_manager_main;
395 u32 *session_indices_to_enqueue;
396 int i, errors = 0;
397
398 session_indices_to_enqueue =
399 smm->session_indices_to_enqueue_by_thread[thread_index];
400
401 for (i = 0; i < vec_len (session_indices_to_enqueue); i++)
402 {
403 stream_session_t *s0;
404
405 /* Get session */
Dave Barach818eb542017-08-02 13:56:13 -0400406 s0 = stream_session_get_if_valid (session_indices_to_enqueue[i],
407 thread_index);
408 if (s0 == 0 || stream_session_enqueue_notify (s0, 0 /* don't block */ ))
Dave Barach68b0fb02017-02-28 15:15:56 -0500409 {
410 errors++;
411 }
412 }
413
414 vec_reset_length (session_indices_to_enqueue);
415
416 smm->session_indices_to_enqueue_by_thread[thread_index] =
417 session_indices_to_enqueue;
418
419 /* Increment enqueue epoch for next round */
420 smm->current_enqueue_epoch[thread_index]++;
421
422 return errors;
423}
424
Florin Corasc28764f2017-04-26 00:08:42 -0700425/**
426 * Init fifo tail and head pointers
427 *
428 * Useful if transport uses absolute offsets for tracking ooo segments.
429 */
430void
431stream_session_init_fifos_pointers (transport_connection_t * tc,
432 u32 rx_pointer, u32 tx_pointer)
433{
434 stream_session_t *s;
Florin Corascea194d2017-10-02 00:18:51 -0700435 s = session_get (tc->s_index, tc->thread_index);
Florin Corasc28764f2017-04-26 00:08:42 -0700436 svm_fifo_init_pointers (s->server_rx_fifo, rx_pointer);
437 svm_fifo_init_pointers (s->server_tx_fifo, tx_pointer);
438}
439
Florin Corasf03a59a2017-06-09 21:07:32 -0700440int
Florin Coras68810622017-07-24 17:40:28 -0700441stream_session_connect_notify (transport_connection_t * tc, u8 is_fail)
Dave Barach68b0fb02017-02-28 15:15:56 -0500442{
Dave Barach68b0fb02017-02-28 15:15:56 -0500443 application_t *app;
444 stream_session_t *new_s = 0;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700445 u64 handle;
Florin Corasab0289a2017-08-14 11:25:25 -0700446 u32 opaque = 0;
Florin Corasf03a59a2017-06-09 21:07:32 -0700447 int error = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500448
Florin Corascea194d2017-10-02 00:18:51 -0700449 handle = session_lookup_half_open_handle (tc);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700450 if (handle == HALF_OPEN_LOOKUP_INVALID_VALUE)
Dave Barach68b0fb02017-02-28 15:15:56 -0500451 {
Florin Corascea194d2017-10-02 00:18:51 -0700452 SESSION_DBG ("half-open was removed!");
Florin Corasf03a59a2017-06-09 21:07:32 -0700453 return -1;
Dave Barach68b0fb02017-02-28 15:15:56 -0500454 }
455
Florin Coras4eeeaaf2017-09-05 14:03:37 -0400456 /* Cleanup half-open table */
Florin Corascea194d2017-10-02 00:18:51 -0700457 session_lookup_del_half_open (tc);
Florin Coras4eeeaaf2017-09-05 14:03:37 -0400458
Florin Corasab0289a2017-08-14 11:25:25 -0700459 /* Get the app's index from the handle we stored when opening connection
460 * and the opaque (api_context for external apps) from transport session
Florin Coras4eeeaaf2017-09-05 14:03:37 -0400461 * index */
Florin Corasc87c91d2017-08-16 19:55:49 -0700462 app = application_get_if_valid (handle >> 32);
463 if (!app)
464 return -1;
465
Florin Corasab0289a2017-08-14 11:25:25 -0700466 opaque = tc->s_index;
Dave Barach68b0fb02017-02-28 15:15:56 -0500467
468 if (!is_fail)
469 {
Florin Coras6cf30ad2017-04-04 23:08:23 -0700470 segment_manager_t *sm;
Dave Barach52851e62017-08-07 09:35:25 -0400471 u8 alloc_fifos;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700472 sm = application_get_connect_segment_manager (app);
Dave Barach52851e62017-08-07 09:35:25 -0400473 alloc_fifos = application_is_proxy (app);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700474 /* Create new session (svm segments are allocated if needed) */
Dave Barach52851e62017-08-07 09:35:25 -0400475 if (stream_session_create_i (sm, tc, alloc_fifos, &new_s))
Florin Corasf03a59a2017-06-09 21:07:32 -0700476 {
477 is_fail = 1;
478 error = -1;
479 }
480 else
481 new_s->app_index = app->index;
Dave Barach68b0fb02017-02-28 15:15:56 -0500482 }
483
Florin Coras68810622017-07-24 17:40:28 -0700484 /* Notify client application */
Florin Corasab0289a2017-08-14 11:25:25 -0700485 if (app->cb_fns.session_connected_callback (app->index, opaque, new_s,
Florin Coras6534b7a2017-07-18 05:38:03 -0400486 is_fail))
487 {
Florin Corascea194d2017-10-02 00:18:51 -0700488 SESSION_DBG ("failed to notify app");
Florin Coras6534b7a2017-07-18 05:38:03 -0400489 if (!is_fail)
490 stream_session_disconnect (new_s);
491 }
492 else
493 {
494 if (!is_fail)
495 new_s->session_state = SESSION_STATE_READY;
496 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500497
Florin Corasf03a59a2017-06-09 21:07:32 -0700498 return error;
Dave Barach68b0fb02017-02-28 15:15:56 -0500499}
500
501void
502stream_session_accept_notify (transport_connection_t * tc)
503{
504 application_t *server;
505 stream_session_t *s;
506
Florin Corascea194d2017-10-02 00:18:51 -0700507 s = session_get (tc->s_index, tc->thread_index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500508 server = application_get (s->app_index);
509 server->cb_fns.session_accept_callback (s);
510}
511
512/**
513 * Notification from transport that connection is being closed.
514 *
515 * A disconnect is sent to application but state is not removed. Once
516 * disconnect is acknowledged by application, session disconnect is called.
517 * Ultimately this leads to close being called on transport (passive close).
518 */
519void
520stream_session_disconnect_notify (transport_connection_t * tc)
521{
522 application_t *server;
523 stream_session_t *s;
524
Florin Corascea194d2017-10-02 00:18:51 -0700525 s = session_get (tc->s_index, tc->thread_index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500526 server = application_get (s->app_index);
527 server->cb_fns.session_disconnect_callback (s);
528}
529
530/**
Florin Coras4eeeaaf2017-09-05 14:03:37 -0400531 * Cleans up session and lookup table.
Dave Barach68b0fb02017-02-28 15:15:56 -0500532 */
533void
534stream_session_delete (stream_session_t * s)
535{
536 session_manager_main_t *smm = vnet_get_session_manager_main ();
Florin Coras6534b7a2017-07-18 05:38:03 -0400537 int rv;
Dave Barach68b0fb02017-02-28 15:15:56 -0500538
Florin Corasd79b41e2017-03-04 05:37:52 -0800539 /* Delete from the main lookup table. */
Florin Corascea194d2017-10-02 00:18:51 -0700540 if ((rv = session_lookup_del_session (s)))
Florin Coras6534b7a2017-07-18 05:38:03 -0400541 clib_warning ("hash delete error, rv %d", rv);
Dave Barach68b0fb02017-02-28 15:15:56 -0500542
543 /* Cleanup fifo segments */
Florin Coras6cf30ad2017-04-04 23:08:23 -0700544 segment_manager_dealloc_fifos (s->svm_segment_index, s->server_rx_fifo,
545 s->server_tx_fifo);
Dave Barach68b0fb02017-02-28 15:15:56 -0500546
547 pool_put (smm->sessions[s->thread_index], s);
Florin Coras6534b7a2017-07-18 05:38:03 -0400548 if (CLIB_DEBUG)
549 memset (s, 0xFA, sizeof (*s));
Dave Barach68b0fb02017-02-28 15:15:56 -0500550}
551
552/**
553 * Notification from transport that connection is being deleted
554 *
Florin Coras4eeeaaf2017-09-05 14:03:37 -0400555 * This removes the session if it is still valid. It should be called only on
556 * previously fully established sessions. For instance failed connects should
557 * call stream_session_connect_notify and indicate that the connect has
558 * failed.
Dave Barach68b0fb02017-02-28 15:15:56 -0500559 */
560void
561stream_session_delete_notify (transport_connection_t * tc)
562{
563 stream_session_t *s;
564
Florin Corase04c2992017-03-01 08:17:34 -0800565 /* App might've been removed already */
Dave Barach68b0fb02017-02-28 15:15:56 -0500566 s = stream_session_get_if_valid (tc->s_index, tc->thread_index);
567 if (!s)
Florin Corasc87c91d2017-08-16 19:55:49 -0700568 return;
Dave Barach68b0fb02017-02-28 15:15:56 -0500569 stream_session_delete (s);
570}
571
572/**
573 * Notify application that connection has been reset.
574 */
575void
576stream_session_reset_notify (transport_connection_t * tc)
577{
578 stream_session_t *s;
579 application_t *app;
Florin Corascea194d2017-10-02 00:18:51 -0700580 s = session_get (tc->s_index, tc->thread_index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500581
582 app = application_get (s->app_index);
583 app->cb_fns.session_reset_callback (s);
584}
585
586/**
587 * Accept a stream session. Optionally ping the server by callback.
588 */
589int
590stream_session_accept (transport_connection_t * tc, u32 listener_index,
Florin Corascea194d2017-10-02 00:18:51 -0700591 u8 notify)
Dave Barach68b0fb02017-02-28 15:15:56 -0500592{
Dave Barach68b0fb02017-02-28 15:15:56 -0500593 application_t *server;
594 stream_session_t *s, *listener;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700595 segment_manager_t *sm;
Florin Corascea194d2017-10-02 00:18:51 -0700596 session_type_t sst;
Dave Barach68b0fb02017-02-28 15:15:56 -0500597 int rv;
598
Florin Corascea194d2017-10-02 00:18:51 -0700599 sst = session_type_from_proto_and_ip (tc->transport_proto, tc->is_ip4);
600
Dave Barach68b0fb02017-02-28 15:15:56 -0500601 /* Find the server */
Florin Coras6cf30ad2017-04-04 23:08:23 -0700602 listener = listen_session_get (sst, listener_index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500603 server = application_get (listener->app_index);
604
Florin Coras6cf30ad2017-04-04 23:08:23 -0700605 sm = application_get_listen_segment_manager (server, listener);
Dave Barach52851e62017-08-07 09:35:25 -0400606 if ((rv = stream_session_create_i (sm, tc, 1, &s)))
Dave Barach68b0fb02017-02-28 15:15:56 -0500607 return rv;
608
Florin Coras6cf30ad2017-04-04 23:08:23 -0700609 s->app_index = server->index;
610 s->listener_index = listener_index;
Dave Barach2c25a622017-06-26 11:35:07 -0400611 s->session_state = SESSION_STATE_ACCEPTING;
Dave Barach68b0fb02017-02-28 15:15:56 -0500612
613 /* Shoulder-tap the server */
614 if (notify)
615 {
616 server->cb_fns.session_accept_callback (s);
617 }
618
619 return 0;
620}
621
Florin Coras6cf30ad2017-04-04 23:08:23 -0700622/**
623 * Ask transport to open connection to remote transport endpoint.
624 *
625 * Stores handle for matching request with reply since the call can be
626 * asynchronous. For instance, for TCP the 3-way handshake must complete
627 * before reply comes. Session is only created once connection is established.
628 *
629 * @param app_index Index of the application requesting the connect
630 * @param st Session type requested.
631 * @param tep Remote transport endpoint
632 * @param res Resulting transport connection .
633 */
Florin Corase04c2992017-03-01 08:17:34 -0800634int
Florin Corascea194d2017-10-02 00:18:51 -0700635stream_session_open (u32 app_index, session_endpoint_t * rmt,
Florin Coras6cf30ad2017-04-04 23:08:23 -0700636 transport_connection_t ** res)
Dave Barach68b0fb02017-02-28 15:15:56 -0500637{
638 transport_connection_t *tc;
Florin Corascea194d2017-10-02 00:18:51 -0700639 session_type_t sst;
Florin Corase04c2992017-03-01 08:17:34 -0800640 int rv;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700641 u64 handle;
Dave Barach68b0fb02017-02-28 15:15:56 -0500642
Florin Corascea194d2017-10-02 00:18:51 -0700643 sst = session_type_from_proto_and_ip (rmt->transport_proto, rmt->is_ip4);
644 rv = tp_vfts[sst].open (session_endpoint_to_transport (rmt));
Florin Corase04c2992017-03-01 08:17:34 -0800645 if (rv < 0)
646 {
647 clib_warning ("Transport failed to open connection.");
Florin Corascea194d2017-10-02 00:18:51 -0700648 return VNET_API_ERROR_SESSION_CONNECT;
Florin Corase04c2992017-03-01 08:17:34 -0800649 }
650
Florin Corascea194d2017-10-02 00:18:51 -0700651 tc = tp_vfts[sst].get_half_open ((u32) rv);
Dave Barach68b0fb02017-02-28 15:15:56 -0500652
Florin Coras6cf30ad2017-04-04 23:08:23 -0700653 /* Save app and tc index. The latter is needed to help establish the
654 * connection while the former is needed when the connect notify comes
655 * and we have to notify the external app */
656 handle = (((u64) app_index) << 32) | (u64) tc->c_index;
Dave Barach68b0fb02017-02-28 15:15:56 -0500657
658 /* Add to the half-open lookup table */
Florin Corascea194d2017-10-02 00:18:51 -0700659 session_lookup_add_half_open (tc, handle);
Florin Corase04c2992017-03-01 08:17:34 -0800660
Florin Coras6cf30ad2017-04-04 23:08:23 -0700661 *res = tc;
662
663 return 0;
664}
665
666/**
667 * Ask transport to listen on local transport endpoint.
668 *
669 * @param s Session for which listen will be called. Note that unlike
670 * established sessions, listen sessions are not associated to a
671 * thread.
672 * @param tep Local endpoint to be listened on.
673 */
674int
Florin Corascea194d2017-10-02 00:18:51 -0700675stream_session_listen (stream_session_t * s, session_endpoint_t * tep)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700676{
677 transport_connection_t *tc;
678 u32 tci;
679
680 /* Transport bind/listen */
Florin Corascea194d2017-10-02 00:18:51 -0700681 tci = tp_vfts[s->session_type].bind (s->session_index,
682 session_endpoint_to_transport (tep));
Florin Coras6cf30ad2017-04-04 23:08:23 -0700683
684 if (tci == (u32) ~ 0)
685 return -1;
686
687 /* Attach transport to session */
688 s->connection_index = tci;
689 tc = tp_vfts[s->session_type].get_listener (tci);
690
691 /* Weird but handle it ... */
692 if (tc == 0)
693 return -1;
694
695 /* Add to the main lookup table */
Florin Corascea194d2017-10-02 00:18:51 -0700696 session_lookup_add_connection (tc, s->session_index);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700697
698 return 0;
699}
700
701/**
702 * Ask transport to stop listening on local transport endpoint.
703 *
704 * @param s Session to stop listening on. It must be in state LISTENING.
705 */
706int
707stream_session_stop_listen (stream_session_t * s)
708{
709 transport_connection_t *tc;
710
711 if (s->session_state != SESSION_STATE_LISTENING)
712 {
713 clib_warning ("not a listening session");
714 return -1;
715 }
716
717 tc = tp_vfts[s->session_type].get_listener (s->connection_index);
718 if (!tc)
719 {
720 clib_warning ("no transport");
721 return VNET_API_ERROR_ADDRESS_NOT_IN_USE;
722 }
723
Florin Corascea194d2017-10-02 00:18:51 -0700724 session_lookup_del_connection (tc);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700725 tp_vfts[s->session_type].unbind (s->connection_index);
Florin Corase04c2992017-03-01 08:17:34 -0800726 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500727}
728
Florin Corasa5464812017-04-19 13:00:05 -0700729void
730session_send_session_evt_to_thread (u64 session_handle,
731 fifo_event_type_t evt_type,
732 u32 thread_index)
733{
734 static u16 serial_number = 0;
Florin Coras9d063042017-09-14 03:08:00 -0400735 u32 tries = 0;
Florin Corasa5464812017-04-19 13:00:05 -0700736 session_fifo_event_t evt;
737 unix_shared_memory_queue_t *q;
738
739 /* Fabricate event */
740 evt.session_handle = session_handle;
741 evt.event_type = evt_type;
742 evt.event_id = serial_number++;
743
744 q = session_manager_get_vpp_event_queue (thread_index);
Florin Coras9d063042017-09-14 03:08:00 -0400745 while (unix_shared_memory_queue_add (q, (u8 *) & evt, 1))
Florin Corasf03a59a2017-06-09 21:07:32 -0700746 {
Florin Coras9d063042017-09-14 03:08:00 -0400747 if (tries++ == 3)
Florin Corasf03a59a2017-06-09 21:07:32 -0700748 {
Florin Coras9d063042017-09-14 03:08:00 -0400749 TCP_DBG ("failed to enqueue evt");
750 break;
Florin Corasf03a59a2017-06-09 21:07:32 -0700751 }
752 }
Florin Corasa5464812017-04-19 13:00:05 -0700753}
754
Dave Barach68b0fb02017-02-28 15:15:56 -0500755/**
756 * Disconnect session and propagate to transport. This should eventually
757 * result in a delete notification that allows us to cleanup session state.
758 * Called for both active/passive disconnects.
Florin Corasa5464812017-04-19 13:00:05 -0700759 *
760 * Should be called from the session's thread.
Dave Barach68b0fb02017-02-28 15:15:56 -0500761 */
762void
763stream_session_disconnect (stream_session_t * s)
764{
Dave Barach68b0fb02017-02-28 15:15:56 -0500765 s->session_state = SESSION_STATE_CLOSED;
Florin Corasd79b41e2017-03-04 05:37:52 -0800766 tp_vfts[s->session_type].close (s->connection_index, s->thread_index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500767}
768
769/**
770 * Cleanup transport and session state.
Florin Corasd79b41e2017-03-04 05:37:52 -0800771 *
772 * Notify transport of the cleanup, wait for a delete notify to actually
773 * remove the session state.
Dave Barach68b0fb02017-02-28 15:15:56 -0500774 */
775void
776stream_session_cleanup (stream_session_t * s)
777{
Florin Corasd79b41e2017-03-04 05:37:52 -0800778 int rv;
779
780 s->session_state = SESSION_STATE_CLOSED;
781
782 /* Delete from the main lookup table to avoid more enqueues */
Florin Corascea194d2017-10-02 00:18:51 -0700783 rv = session_lookup_del_session (s);
Florin Corasd79b41e2017-03-04 05:37:52 -0800784 if (rv)
785 clib_warning ("hash delete error, rv %d", rv);
786
Dave Barach68b0fb02017-02-28 15:15:56 -0500787 tp_vfts[s->session_type].cleanup (s->connection_index, s->thread_index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500788}
789
Florin Corasa5464812017-04-19 13:00:05 -0700790/**
791 * Allocate vpp event queue (once) per worker thread
792 */
793void
794session_vpp_event_queue_allocate (session_manager_main_t * smm,
795 u32 thread_index)
796{
797 api_main_t *am = &api_main;
798 void *oldheap;
Dave Barach10d8cc62017-05-30 09:30:07 -0400799 u32 event_queue_length = 2048;
Florin Corasa5464812017-04-19 13:00:05 -0700800
801 if (smm->vpp_event_queues[thread_index] == 0)
802 {
803 /* Allocate event fifo in the /vpe-api shared-memory segment */
804 oldheap = svm_push_data_heap (am->vlib_rp);
805
Dave Barach10d8cc62017-05-30 09:30:07 -0400806 if (smm->configured_event_queue_length)
807 event_queue_length = smm->configured_event_queue_length;
808
Florin Corasa5464812017-04-19 13:00:05 -0700809 smm->vpp_event_queues[thread_index] =
Dave Barach10d8cc62017-05-30 09:30:07 -0400810 unix_shared_memory_queue_init
811 (event_queue_length,
812 sizeof (session_fifo_event_t), 0 /* consumer pid */ ,
813 0 /* (do not) send signal when queue non-empty */ );
Florin Corasa5464812017-04-19 13:00:05 -0700814
815 svm_pop_heap (oldheap);
816 }
817}
818
Dave Barach2c25a622017-06-26 11:35:07 -0400819session_type_t
820session_type_from_proto_and_ip (transport_proto_t proto, u8 is_ip4)
821{
822 if (proto == TRANSPORT_PROTO_TCP)
823 {
824 if (is_ip4)
825 return SESSION_TYPE_IP4_TCP;
826 else
827 return SESSION_TYPE_IP6_TCP;
828 }
829 else
830 {
831 if (is_ip4)
832 return SESSION_TYPE_IP4_UDP;
833 else
834 return SESSION_TYPE_IP6_UDP;
835 }
836
837 return SESSION_N_TYPES;
838}
839
Florin Corascea194d2017-10-02 00:18:51 -0700840int
841listen_session_get_local_session_endpoint (stream_session_t * listener,
842 session_endpoint_t * sep)
843{
844 transport_connection_t *tc;
845 tc =
846 tp_vfts[listener->session_type].get_listener (listener->connection_index);
847 if (!tc)
848 {
849 clib_warning ("no transport");
850 return -1;
851 }
852
853 /* N.B. The ip should not be copied because this is the local endpoint */
854 sep->port = tc->lcl_port;
855 sep->transport_proto = tc->transport_proto;
856 sep->is_ip4 = tc->is_ip4;
857 return 0;
858}
859
Dave Barach68b0fb02017-02-28 15:15:56 -0500860static clib_error_t *
Florin Corase04c2992017-03-01 08:17:34 -0800861session_manager_main_enable (vlib_main_t * vm)
Dave Barach68b0fb02017-02-28 15:15:56 -0500862{
Dave Barach68b0fb02017-02-28 15:15:56 -0500863 session_manager_main_t *smm = &session_manager_main;
Florin Corase04c2992017-03-01 08:17:34 -0800864 vlib_thread_main_t *vtm = vlib_get_thread_main ();
865 u32 num_threads;
Florin Coras66b11312017-07-31 17:18:03 -0700866 u32 preallocated_sessions_per_worker;
Dave Barach68b0fb02017-02-28 15:15:56 -0500867 int i;
868
Dave Barach68b0fb02017-02-28 15:15:56 -0500869 num_threads = 1 /* main thread */ + vtm->n_threads;
870
871 if (num_threads < 1)
872 return clib_error_return (0, "n_thread_stacks not set");
873
874 /* $$$ config parameters */
875 svm_fifo_segment_init (0x200000000ULL /* first segment base VA */ ,
876 20 /* timeout in seconds */ );
877
878 /* configure per-thread ** vectors */
879 vec_validate (smm->sessions, num_threads - 1);
880 vec_validate (smm->session_indices_to_enqueue_by_thread, num_threads - 1);
881 vec_validate (smm->tx_buffers, num_threads - 1);
Dave Barachacd2a6a2017-05-16 17:41:34 -0400882 vec_validate (smm->pending_event_vector, num_threads - 1);
883 vec_validate (smm->free_event_vector, num_threads - 1);
Dave Barach68b0fb02017-02-28 15:15:56 -0500884 vec_validate (smm->current_enqueue_epoch, num_threads - 1);
885 vec_validate (smm->vpp_event_queues, num_threads - 1);
886
Dave Barachacd2a6a2017-05-16 17:41:34 -0400887 for (i = 0; i < num_threads; i++)
888 {
889 vec_validate (smm->free_event_vector[i], 0);
890 _vec_len (smm->free_event_vector[i]) = 0;
891 vec_validate (smm->pending_event_vector[i], 0);
892 _vec_len (smm->pending_event_vector[i]) = 0;
893 }
894
Florin Coras3e350af2017-03-30 02:54:28 -0700895#if SESSION_DBG
896 vec_validate (smm->last_event_poll_by_thread, num_threads - 1);
897#endif
898
Florin Coras6cf30ad2017-04-04 23:08:23 -0700899 /* Allocate vpp event queues */
900 for (i = 0; i < vec_len (smm->vpp_event_queues); i++)
901 session_vpp_event_queue_allocate (smm, i);
902
Florin Coras66b11312017-07-31 17:18:03 -0700903 /* Preallocate sessions */
Dave Barachb7f1faa2017-08-29 11:43:37 -0400904 if (smm->preallocated_sessions)
Dave Barach68b0fb02017-02-28 15:15:56 -0500905 {
Dave Barachb7f1faa2017-08-29 11:43:37 -0400906 if (num_threads == 1)
Florin Coras66b11312017-07-31 17:18:03 -0700907 {
Dave Barachb7f1faa2017-08-29 11:43:37 -0400908 pool_init_fixed (smm->sessions[0], smm->preallocated_sessions);
Florin Coras66b11312017-07-31 17:18:03 -0700909 }
Dave Barachb7f1faa2017-08-29 11:43:37 -0400910 else
Florin Coras66b11312017-07-31 17:18:03 -0700911 {
Dave Barachb7f1faa2017-08-29 11:43:37 -0400912 int j;
913 preallocated_sessions_per_worker =
914 (1.1 * (f64) smm->preallocated_sessions /
915 (f64) (num_threads - 1));
916
917 for (j = 1; j < num_threads; j++)
Florin Coras66b11312017-07-31 17:18:03 -0700918 {
Dave Barachb7f1faa2017-08-29 11:43:37 -0400919 pool_init_fixed (smm->sessions[j],
920 preallocated_sessions_per_worker);
Florin Coras66b11312017-07-31 17:18:03 -0700921 }
Florin Coras66b11312017-07-31 17:18:03 -0700922 }
923 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500924
Florin Coras04e53442017-07-16 17:12:15 -0700925 session_lookup_init ();
Florin Corascea194d2017-10-02 00:18:51 -0700926 app_namespaces_init ();
Dave Barach68b0fb02017-02-28 15:15:56 -0500927
Florin Corase04c2992017-03-01 08:17:34 -0800928 smm->is_enabled = 1;
929
Florin Corasa0b34a72017-03-07 01:20:52 -0800930 /* Enable TCP transport */
931 vnet_tcp_enable_disable (vm, 1);
932
Dave Barach68b0fb02017-02-28 15:15:56 -0500933 return 0;
934}
935
Florin Corasa5464812017-04-19 13:00:05 -0700936void
937session_node_enable_disable (u8 is_en)
938{
939 u8 state = is_en ? VLIB_NODE_STATE_POLLING : VLIB_NODE_STATE_DISABLED;
940 /* *INDENT-OFF* */
941 foreach_vlib_main (({
942 vlib_node_set_state (this_vlib_main, session_queue_node.index,
943 state);
944 }));
945 /* *INDENT-ON* */
946}
947
Florin Corase04c2992017-03-01 08:17:34 -0800948clib_error_t *
949vnet_session_enable_disable (vlib_main_t * vm, u8 is_en)
950{
Florin Corascea194d2017-10-02 00:18:51 -0700951 clib_error_t *error = 0;
Florin Corase04c2992017-03-01 08:17:34 -0800952 if (is_en)
953 {
954 if (session_manager_main.is_enabled)
955 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500956
Florin Corasa5464812017-04-19 13:00:05 -0700957 session_node_enable_disable (is_en);
Florin Corascea194d2017-10-02 00:18:51 -0700958 error = session_manager_main_enable (vm);
Florin Corase04c2992017-03-01 08:17:34 -0800959 }
960 else
961 {
962 session_manager_main.is_enabled = 0;
Florin Corasa5464812017-04-19 13:00:05 -0700963 session_node_enable_disable (is_en);
Florin Corase04c2992017-03-01 08:17:34 -0800964 }
965
Florin Corascea194d2017-10-02 00:18:51 -0700966 return error;
Florin Corase04c2992017-03-01 08:17:34 -0800967}
968
Florin Corase04c2992017-03-01 08:17:34 -0800969clib_error_t *
970session_manager_main_init (vlib_main_t * vm)
971{
972 session_manager_main_t *smm = &session_manager_main;
Florin Corase04c2992017-03-01 08:17:34 -0800973 smm->is_enabled = 0;
Florin Corase04c2992017-03-01 08:17:34 -0800974 return 0;
975}
976
Dave Barach2c25a622017-06-26 11:35:07 -0400977VLIB_INIT_FUNCTION (session_manager_main_init);
978
979static clib_error_t *
980session_config_fn (vlib_main_t * vm, unformat_input_t * input)
Dave Barach10d8cc62017-05-30 09:30:07 -0400981{
982 session_manager_main_t *smm = &session_manager_main;
983 u32 nitems;
Florin Coras66b11312017-07-31 17:18:03 -0700984 uword tmp;
Dave Barach10d8cc62017-05-30 09:30:07 -0400985
986 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
987 {
988 if (unformat (input, "event-queue-length %d", &nitems))
989 {
990 if (nitems >= 2048)
991 smm->configured_event_queue_length = nitems;
992 else
993 clib_warning ("event queue length %d too small, ignored", nitems);
994 }
Florin Coras66b11312017-07-31 17:18:03 -0700995 else if (unformat (input, "preallocated-sessions %d",
996 &smm->preallocated_sessions))
Dave Barach2c25a622017-06-26 11:35:07 -0400997 ;
Florin Coras66b11312017-07-31 17:18:03 -0700998 else if (unformat (input, "v4-session-table-buckets %d",
999 &smm->configured_v4_session_table_buckets))
1000 ;
1001 else if (unformat (input, "v4-halfopen-table-buckets %d",
1002 &smm->configured_v4_halfopen_table_buckets))
1003 ;
1004 else if (unformat (input, "v6-session-table-buckets %d",
1005 &smm->configured_v6_session_table_buckets))
1006 ;
1007 else if (unformat (input, "v6-halfopen-table-buckets %d",
1008 &smm->configured_v6_halfopen_table_buckets))
1009 ;
1010 else if (unformat (input, "v4-session-table-memory %U",
1011 unformat_memory_size, &tmp))
1012 {
1013 if (tmp >= 0x100000000)
1014 return clib_error_return (0, "memory size %llx (%lld) too large",
1015 tmp, tmp);
1016 smm->configured_v4_session_table_memory = tmp;
1017 }
1018 else if (unformat (input, "v4-halfopen-table-memory %U",
1019 unformat_memory_size, &tmp))
1020 {
1021 if (tmp >= 0x100000000)
1022 return clib_error_return (0, "memory size %llx (%lld) too large",
1023 tmp, tmp);
1024 smm->configured_v4_halfopen_table_memory = tmp;
1025 }
1026 else if (unformat (input, "v6-session-table-memory %U",
1027 unformat_memory_size, &tmp))
1028 {
1029 if (tmp >= 0x100000000)
1030 return clib_error_return (0, "memory size %llx (%lld) too large",
1031 tmp, tmp);
1032 smm->configured_v6_session_table_memory = tmp;
1033 }
1034 else if (unformat (input, "v6-halfopen-table-memory %U",
1035 unformat_memory_size, &tmp))
1036 {
1037 if (tmp >= 0x100000000)
1038 return clib_error_return (0, "memory size %llx (%lld) too large",
1039 tmp, tmp);
1040 smm->configured_v6_halfopen_table_memory = tmp;
1041 }
Dave Barach10d8cc62017-05-30 09:30:07 -04001042 else
1043 return clib_error_return (0, "unknown input `%U'",
1044 format_unformat_error, input);
1045 }
1046 return 0;
1047}
1048
1049VLIB_CONFIG_FUNCTION (session_config_fn, "session");
1050
Dave Barach68b0fb02017-02-28 15:15:56 -05001051/*
1052 * fd.io coding-style-patch-verification: ON
1053 *
1054 * Local Variables:
1055 * eval: (c-set-style "gnu")
1056 * End:
1057 */