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