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