session: move builtin apps to their own folder
[vpp.git] / src / vnet / session-apps / proxy.c
1 /*
2 * Copyright (c) 2015-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 #include <vnet/vnet.h>
17 #include <vlibmemory/api.h>
18 #include <vnet/session/application.h>
19 #include <vnet/session/application_interface.h>
20 #include <vnet/session-apps/proxy.h>
21
22 proxy_main_t proxy_main;
23
24 static void
25 delete_proxy_session (stream_session_t * s, int is_active_open)
26 {
27   proxy_main_t *pm = &proxy_main;
28   proxy_session_t *ps = 0;
29   vnet_disconnect_args_t _a, *a = &_a;
30   stream_session_t *active_open_session = 0;
31   stream_session_t *server_session = 0;
32   uword *p;
33   u64 handle;
34
35   handle = session_handle (s);
36
37   clib_spinlock_lock_if_init (&pm->sessions_lock);
38   if (is_active_open)
39     {
40       active_open_session = s;
41
42       p = hash_get (pm->proxy_session_by_active_open_handle, handle);
43       if (p == 0)
44         {
45           clib_warning ("proxy session for %s handle %lld (%llx) AWOL",
46                         is_active_open ? "active open" : "server",
47                         handle, handle);
48         }
49       else
50         {
51           ps = pool_elt_at_index (pm->sessions, p[0]);
52           if (ps->vpp_server_handle != ~0)
53             server_session = session_get_from_handle (ps->vpp_server_handle);
54           else
55             server_session = 0;
56         }
57     }
58   else
59     {
60       server_session = s;
61
62       p = hash_get (pm->proxy_session_by_server_handle, handle);
63       if (p == 0)
64         {
65           clib_warning ("proxy session for %s handle %lld (%llx) AWOL",
66                         is_active_open ? "active open" : "server",
67                         handle, handle);
68         }
69       else
70         {
71           ps = pool_elt_at_index (pm->sessions, p[0]);
72           if (ps->vpp_server_handle != ~0)
73             active_open_session = session_get_from_handle
74               (ps->vpp_server_handle);
75           else
76             active_open_session = 0;
77         }
78     }
79
80   if (ps)
81     {
82       if (CLIB_DEBUG > 0)
83         memset (ps, 0xFE, sizeof (*ps));
84       pool_put (pm->sessions, ps);
85     }
86
87   clib_spinlock_unlock_if_init (&pm->sessions_lock);
88
89   if (active_open_session)
90     {
91       a->handle = session_handle (active_open_session);
92       a->app_index = pm->active_open_app_index;
93       hash_unset (pm->proxy_session_by_active_open_handle,
94                   session_handle (active_open_session));
95       vnet_disconnect_session (a);
96     }
97
98   if (server_session)
99     {
100       a->handle = session_handle (server_session);
101       a->app_index = pm->server_app_index;
102       hash_unset (pm->proxy_session_by_server_handle,
103                   session_handle (server_session));
104       vnet_disconnect_session (a);
105     }
106 }
107
108 static int
109 proxy_accept_callback (stream_session_t * s)
110 {
111   proxy_main_t *pm = &proxy_main;
112
113   s->session_state = SESSION_STATE_READY;
114
115   clib_spinlock_lock_if_init (&pm->sessions_lock);
116
117   return 0;
118 }
119
120 static void
121 proxy_disconnect_callback (stream_session_t * s)
122 {
123   delete_proxy_session (s, 0 /* is_active_open */ );
124 }
125
126 static void
127 proxy_reset_callback (stream_session_t * s)
128 {
129   clib_warning ("Reset session %U", format_stream_session, s, 2);
130   delete_proxy_session (s, 0 /* is_active_open */ );
131 }
132
133 static int
134 proxy_connected_callback (u32 app_index, u32 api_context,
135                           stream_session_t * s, u8 is_fail)
136 {
137   clib_warning ("called...");
138   return -1;
139 }
140
141 static int
142 proxy_add_segment_callback (u32 client_index, const ssvm_private_t * sp)
143 {
144   clib_warning ("called...");
145   return -1;
146 }
147
148 static int
149 proxy_redirect_connect_callback (u32 client_index, void *mp)
150 {
151   clib_warning ("called...");
152   return -1;
153 }
154
155 static int
156 proxy_rx_callback (stream_session_t * s)
157 {
158   u32 max_dequeue;
159   int actual_transfer __attribute__ ((unused));
160   svm_fifo_t *tx_fifo, *rx_fifo;
161   proxy_main_t *pm = &proxy_main;
162   u32 thread_index = vlib_get_thread_index ();
163   vnet_connect_args_t _a, *a = &_a;
164   proxy_session_t *ps;
165   int proxy_index;
166   uword *p;
167   svm_fifo_t *active_open_tx_fifo;
168   session_fifo_event_t evt;
169
170   ASSERT (s->thread_index == thread_index);
171
172   clib_spinlock_lock_if_init (&pm->sessions_lock);
173   p = hash_get (pm->proxy_session_by_server_handle, session_handle (s));
174
175   if (PREDICT_TRUE (p != 0))
176     {
177       clib_spinlock_unlock_if_init (&pm->sessions_lock);
178       active_open_tx_fifo = s->server_rx_fifo;
179
180       /*
181        * Send event for active open tx fifo
182        */
183       if (svm_fifo_set_event (active_open_tx_fifo))
184         {
185           evt.fifo = active_open_tx_fifo;
186           evt.event_type = FIFO_EVENT_APP_TX;
187           if (svm_queue_add
188               (pm->active_open_event_queue[thread_index], (u8 *) & evt,
189                0 /* do wait for mutex */ ))
190             clib_warning ("failed to enqueue tx evt");
191         }
192     }
193   else
194     {
195       rx_fifo = s->server_rx_fifo;
196       tx_fifo = s->server_tx_fifo;
197
198       ASSERT (rx_fifo->master_thread_index == thread_index);
199       ASSERT (tx_fifo->master_thread_index == thread_index);
200
201       max_dequeue = svm_fifo_max_dequeue (s->server_rx_fifo);
202
203       if (PREDICT_FALSE (max_dequeue == 0))
204         return 0;
205
206       actual_transfer = svm_fifo_peek (rx_fifo, 0 /* relative_offset */ ,
207                                        max_dequeue, pm->rx_buf[thread_index]);
208
209       /* $$$ your message in this space: parse url, etc. */
210
211       memset (a, 0, sizeof (*a));
212
213       clib_spinlock_lock_if_init (&pm->sessions_lock);
214       pool_get (pm->sessions, ps);
215       memset (ps, 0, sizeof (*ps));
216       ps->server_rx_fifo = rx_fifo;
217       ps->server_tx_fifo = tx_fifo;
218       ps->vpp_server_handle = session_handle (s);
219
220       proxy_index = ps - pm->sessions;
221
222       hash_set (pm->proxy_session_by_server_handle, ps->vpp_server_handle,
223                 proxy_index);
224
225       clib_spinlock_unlock_if_init (&pm->sessions_lock);
226
227       a->uri = (char *) pm->client_uri;
228       a->api_context = proxy_index;
229       a->app_index = pm->active_open_app_index;
230       a->mp = 0;
231       vnet_connect_uri (a);
232     }
233
234   return 0;
235 }
236
237 static session_cb_vft_t proxy_session_cb_vft = {
238   .session_accept_callback = proxy_accept_callback,
239   .session_disconnect_callback = proxy_disconnect_callback,
240   .session_connected_callback = proxy_connected_callback,
241   .add_segment_callback = proxy_add_segment_callback,
242   .redirect_connect_callback = proxy_redirect_connect_callback,
243   .builtin_server_rx_callback = proxy_rx_callback,
244   .session_reset_callback = proxy_reset_callback
245 };
246
247 static int
248 active_open_connected_callback (u32 app_index, u32 opaque,
249                                 stream_session_t * s, u8 is_fail)
250 {
251   proxy_main_t *pm = &proxy_main;
252   proxy_session_t *ps;
253   u8 thread_index = vlib_get_thread_index ();
254   session_fifo_event_t evt;
255
256   if (is_fail)
257     {
258       clib_warning ("connection %d failed!", opaque);
259       return 0;
260     }
261
262   /*
263    * Setup proxy session handle.
264    */
265   clib_spinlock_lock_if_init (&pm->sessions_lock);
266
267   ps = pool_elt_at_index (pm->sessions, opaque);
268   ps->vpp_active_open_handle = session_handle (s);
269
270   s->server_tx_fifo = ps->server_rx_fifo;
271   s->server_rx_fifo = ps->server_tx_fifo;
272
273   /*
274    * Reset the active-open tx-fifo master indices so the active-open session
275    * will receive data, etc.
276    */
277   s->server_tx_fifo->master_session_index = s->session_index;
278   s->server_tx_fifo->master_thread_index = s->thread_index;
279
280   /*
281    * Account for the active-open session's use of the fifos
282    * so they won't disappear until the last session which uses
283    * them disappears
284    */
285   s->server_tx_fifo->refcnt++;
286   s->server_rx_fifo->refcnt++;
287
288   hash_set (pm->proxy_session_by_active_open_handle,
289             ps->vpp_active_open_handle, opaque);
290
291   clib_spinlock_unlock_if_init (&pm->sessions_lock);
292
293   /*
294    * Send event for active open tx fifo
295    */
296   if (svm_fifo_set_event (s->server_tx_fifo))
297     {
298       evt.fifo = s->server_tx_fifo;
299       evt.event_type = FIFO_EVENT_APP_TX;
300       if (svm_queue_add
301           (pm->active_open_event_queue[thread_index], (u8 *) & evt,
302            0 /* do wait for mutex */ ))
303         clib_warning ("failed to enqueue tx evt");
304     }
305
306   return 0;
307 }
308
309 static void
310 active_open_reset_callback (stream_session_t * s)
311 {
312   delete_proxy_session (s, 1 /* is_active_open */ );
313 }
314
315 static int
316 active_open_create_callback (stream_session_t * s)
317 {
318   return 0;
319 }
320
321 static void
322 active_open_disconnect_callback (stream_session_t * s)
323 {
324   delete_proxy_session (s, 1 /* is_active_open */ );
325 }
326
327 static int
328 active_open_rx_callback (stream_session_t * s)
329 {
330   proxy_main_t *pm = &proxy_main;
331   session_fifo_event_t evt;
332   svm_fifo_t *server_rx_fifo;
333   u32 thread_index = vlib_get_thread_index ();
334
335   server_rx_fifo = s->server_rx_fifo;
336
337   /*
338    * Send event for server tx fifo
339    */
340   if (svm_fifo_set_event (server_rx_fifo))
341     {
342       evt.fifo = server_rx_fifo;
343       evt.event_type = FIFO_EVENT_APP_TX;
344       if (svm_queue_add
345           (pm->server_event_queue[thread_index], (u8 *) & evt,
346            0 /* do wait for mutex */ ))
347         clib_warning ("failed to enqueue server rx evt");
348     }
349
350   return 0;
351 }
352
353 /* *INDENT-OFF* */
354 static session_cb_vft_t active_open_clients = {
355   .session_reset_callback = active_open_reset_callback,
356   .session_connected_callback = active_open_connected_callback,
357   .session_accept_callback = active_open_create_callback,
358   .session_disconnect_callback = active_open_disconnect_callback,
359   .builtin_server_rx_callback = active_open_rx_callback
360 };
361 /* *INDENT-ON* */
362
363
364 static void
365 create_api_loopbacks (vlib_main_t * vm)
366 {
367   proxy_main_t *pm = &proxy_main;
368   api_main_t *am = &api_main;
369   vl_shmem_hdr_t *shmem_hdr;
370
371   shmem_hdr = am->shmem_hdr;
372   pm->vl_input_queue = shmem_hdr->vl_input_queue;
373   pm->server_client_index =
374     vl_api_memclnt_create_internal ("proxy_server", pm->vl_input_queue);
375   pm->active_open_client_index =
376     vl_api_memclnt_create_internal ("proxy_active_open", pm->vl_input_queue);
377 }
378
379 static int
380 proxy_server_attach ()
381 {
382   proxy_main_t *pm = &proxy_main;
383   u64 options[APP_OPTIONS_N_OPTIONS];
384   vnet_app_attach_args_t _a, *a = &_a;
385   u32 segment_size = 512 << 20;
386
387   memset (a, 0, sizeof (*a));
388   memset (options, 0, sizeof (options));
389
390   if (pm->private_segment_size)
391     segment_size = pm->private_segment_size;
392   a->api_client_index = pm->server_client_index;
393   a->session_cb_vft = &proxy_session_cb_vft;
394   a->options = options;
395   a->options[APP_OPTIONS_SEGMENT_SIZE] = segment_size;
396   a->options[APP_OPTIONS_RX_FIFO_SIZE] = pm->fifo_size;
397   a->options[APP_OPTIONS_TX_FIFO_SIZE] = pm->fifo_size;
398   a->options[APP_OPTIONS_PRIVATE_SEGMENT_COUNT] = pm->private_segment_count;
399   a->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] =
400     pm->prealloc_fifos ? pm->prealloc_fifos : 1;
401
402   a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
403
404   if (vnet_application_attach (a))
405     {
406       clib_warning ("failed to attach server");
407       return -1;
408     }
409   pm->server_app_index = a->app_index;
410
411   return 0;
412 }
413
414 static int
415 active_open_attach (void)
416 {
417   proxy_main_t *pm = &proxy_main;
418   vnet_app_attach_args_t _a, *a = &_a;
419   u64 options[16];
420
421   memset (a, 0, sizeof (*a));
422   memset (options, 0, sizeof (options));
423
424   a->api_client_index = pm->active_open_client_index;
425   a->session_cb_vft = &active_open_clients;
426
427   options[APP_OPTIONS_ACCEPT_COOKIE] = 0x12345678;
428   options[APP_OPTIONS_SEGMENT_SIZE] = 512 << 20;
429   options[APP_OPTIONS_RX_FIFO_SIZE] = pm->fifo_size;
430   options[APP_OPTIONS_TX_FIFO_SIZE] = pm->fifo_size;
431   options[APP_OPTIONS_PRIVATE_SEGMENT_COUNT] = pm->private_segment_count;
432   options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] =
433     pm->prealloc_fifos ? pm->prealloc_fifos : 1;
434
435   options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN
436     | APP_OPTIONS_FLAGS_IS_PROXY;
437
438   a->options = options;
439
440   if (vnet_application_attach (a))
441     return -1;
442
443   pm->active_open_app_index = a->app_index;
444
445   return 0;
446 }
447
448 static int
449 proxy_server_listen ()
450 {
451   proxy_main_t *pm = &proxy_main;
452   vnet_bind_args_t _a, *a = &_a;
453   memset (a, 0, sizeof (*a));
454   a->app_index = pm->server_app_index;
455   a->uri = (char *) pm->server_uri;
456   return vnet_bind_uri (a);
457 }
458
459 static int
460 proxy_server_create (vlib_main_t * vm)
461 {
462   proxy_main_t *pm = &proxy_main;
463   vlib_thread_main_t *vtm = vlib_get_thread_main ();
464   u32 num_threads;
465   int i;
466
467   if (pm->server_client_index == (u32) ~ 0)
468     create_api_loopbacks (vm);
469
470   num_threads = 1 /* main thread */  + vtm->n_threads;
471   vec_validate (proxy_main.server_event_queue, num_threads - 1);
472   vec_validate (proxy_main.active_open_event_queue, num_threads - 1);
473   vec_validate (pm->rx_buf, num_threads - 1);
474
475   for (i = 0; i < num_threads; i++)
476     vec_validate (pm->rx_buf[i], pm->rcv_buffer_size);
477
478   if (proxy_server_attach ())
479     {
480       clib_warning ("failed to attach server app");
481       return -1;
482     }
483   if (proxy_server_listen ())
484     {
485       clib_warning ("failed to start listening");
486       return -1;
487     }
488   if (active_open_attach ())
489     {
490       clib_warning ("failed to attach active open app");
491       return -1;
492     }
493
494   for (i = 0; i < num_threads; i++)
495     {
496       pm->active_open_event_queue[i] =
497         session_manager_get_vpp_event_queue (i);
498
499       ASSERT (pm->active_open_event_queue[i]);
500
501       pm->server_event_queue[i] = session_manager_get_vpp_event_queue (i);
502     }
503
504   return 0;
505 }
506
507 static clib_error_t *
508 proxy_server_create_command_fn (vlib_main_t * vm, unformat_input_t * input,
509                                 vlib_cli_command_t * cmd)
510 {
511   proxy_main_t *pm = &proxy_main;
512   char *default_server_uri = "tcp://0.0.0.0/23";
513   char *default_client_uri = "tcp://6.0.2.2/23";
514   int rv;
515   u64 tmp;
516
517   pm->fifo_size = 64 << 10;
518   pm->rcv_buffer_size = 1024;
519   pm->prealloc_fifos = 0;
520   pm->private_segment_count = 0;
521   pm->private_segment_size = 0;
522   pm->server_uri = 0;
523
524   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
525     {
526       if (unformat (input, "fifo-size %d", &pm->fifo_size))
527         pm->fifo_size <<= 10;
528       else if (unformat (input, "rcv-buf-size %d", &pm->rcv_buffer_size))
529         ;
530       else if (unformat (input, "prealloc-fifos %d", &pm->prealloc_fifos))
531         ;
532       else if (unformat (input, "private-segment-count %d",
533                          &pm->private_segment_count))
534         ;
535       else if (unformat (input, "private-segment-size %U",
536                          unformat_memory_size, &tmp))
537         {
538           if (tmp >= 0x100000000ULL)
539             return clib_error_return
540               (0, "private segment size %lld (%llu) too large", tmp, tmp);
541           pm->private_segment_size = tmp;
542         }
543       else if (unformat (input, "server-uri %s", &pm->server_uri))
544         ;
545       else if (unformat (input, "client-uri %s", &pm->client_uri))
546         ;
547       else
548         return clib_error_return (0, "unknown input `%U'",
549                                   format_unformat_error, input);
550     }
551
552   if (!pm->server_uri)
553     {
554       clib_warning ("No server-uri provided, Using default: %s",
555                     default_server_uri);
556       pm->server_uri = format (0, "%s%c", default_server_uri, 0);
557     }
558   if (!pm->client_uri)
559     {
560       clib_warning ("No client-uri provided, Using default: %s",
561                     default_client_uri);
562       pm->client_uri = format (0, "%s%c", default_client_uri, 0);
563     }
564
565   vnet_session_enable_disable (vm, 1 /* turn on session and transport */ );
566
567   rv = proxy_server_create (vm);
568   switch (rv)
569     {
570     case 0:
571       break;
572     default:
573       return clib_error_return (0, "server_create returned %d", rv);
574     }
575
576   return 0;
577 }
578
579 /* *INDENT-OFF* */
580 VLIB_CLI_COMMAND (proxy_create_command, static) =
581 {
582   .path = "test proxy server",
583   .short_help = "test proxy server [server-uri <tcp://ip/port>]"
584       "[client-uri <tcp://ip/port>][fifo-size <nn>][rcv-buf-size <nn>]"
585       "[prealloc-fifos <nn>][private-segment-size <mem>]"
586       "[private-segment-count <nn>]",
587   .function = proxy_server_create_command_fn,
588 };
589 /* *INDENT-ON* */
590
591 clib_error_t *
592 proxy_main_init (vlib_main_t * vm)
593 {
594   proxy_main_t *pm = &proxy_main;
595   pm->server_client_index = ~0;
596   pm->active_open_client_index = ~0;
597   pm->proxy_session_by_active_open_handle = hash_create (0, sizeof (uword));
598   pm->proxy_session_by_server_handle = hash_create (0, sizeof (uword));
599
600   return 0;
601 }
602
603 VLIB_INIT_FUNCTION (proxy_main_init);
604
605 /*
606 * fd.io coding-style-patch-verification: ON
607 *
608 * Local Variables:
609 * eval: (c-set-style "gnu")
610 * End:
611 */