db741f3eda1f16ce93cc9254cfd92bf87ebabe32
[vpp.git] / src / plugins / hs_apps / proxy.c
1 /*
2 * Copyright (c) 2017-2019 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 <hs_apps/proxy.h>
21 #include <vnet/tcp/tcp.h>
22
23 proxy_main_t proxy_main;
24
25 #define TCP_MSS 1460
26
27 typedef struct
28 {
29   session_endpoint_cfg_t sep;
30   u32 app_index;
31   u32 api_context;
32 } proxy_connect_args_t;
33
34 static void
35 proxy_cb_fn (void *data, u32 data_len)
36 {
37   proxy_connect_args_t *pa = (proxy_connect_args_t *) data;
38   vnet_connect_args_t a;
39
40   clib_memset (&a, 0, sizeof (a));
41   a.api_context = pa->api_context;
42   a.app_index = pa->app_index;
43   clib_memcpy (&a.sep_ext, &pa->sep, sizeof (pa->sep));
44   vnet_connect (&a);
45   if (a.sep_ext.ext_cfg)
46     clib_mem_free (a.sep_ext.ext_cfg);
47 }
48
49 static void
50 proxy_call_main_thread (vnet_connect_args_t * a)
51 {
52   if (vlib_get_thread_index () == 0)
53     {
54       vnet_connect (a);
55       if (a->sep_ext.ext_cfg)
56         clib_mem_free (a->sep_ext.ext_cfg);
57     }
58   else
59     {
60       proxy_connect_args_t args;
61       args.api_context = a->api_context;
62       args.app_index = a->app_index;
63       clib_memcpy (&args.sep, &a->sep_ext, sizeof (a->sep_ext));
64       vl_api_rpc_call_main_thread (proxy_cb_fn, (u8 *) & args, sizeof (args));
65     }
66 }
67
68 static proxy_session_t *
69 proxy_session_alloc (void)
70 {
71   proxy_main_t *pm = &proxy_main;
72   proxy_session_t *ps;
73
74   pool_get_zero (pm->sessions, ps);
75   ps->ps_index = ps - pm->sessions;
76
77   return ps;
78 }
79
80 static inline proxy_session_t *
81 proxy_session_get (u32 ps_index)
82 {
83   proxy_main_t *pm = &proxy_main;
84
85   return pool_elt_at_index (pm->sessions, ps_index);
86 }
87
88 static inline proxy_session_t *
89 proxy_session_get_if_valid (u32 ps_index)
90 {
91   proxy_main_t *pm = &proxy_main;
92
93   if (pool_is_free_index (pm->sessions, ps_index))
94     return 0;
95   return pool_elt_at_index (pm->sessions, ps_index);
96 }
97
98 static void
99 proxy_session_free (proxy_session_t *ps)
100 {
101   proxy_main_t *pm = &proxy_main;
102
103   if (CLIB_DEBUG > 0)
104     clib_memset (ps, 0xFE, sizeof (*ps));
105   pool_put (pm->sessions, ps);
106 }
107
108 static void
109 proxy_try_close_session (session_t * s, int is_active_open)
110 {
111   proxy_main_t *pm = &proxy_main;
112   proxy_session_t *ps = 0;
113   vnet_disconnect_args_t _a, *a = &_a;
114
115   clib_spinlock_lock_if_init (&pm->sessions_lock);
116
117   ps = proxy_session_get (s->opaque);
118
119   if (is_active_open)
120     {
121       a->handle = ps->vpp_active_open_handle;
122       a->app_index = pm->active_open_app_index;
123       vnet_disconnect_session (a);
124       ps->ao_disconnected = 1;
125
126       if (!ps->po_disconnected)
127         {
128           ASSERT (ps->vpp_server_handle != SESSION_INVALID_HANDLE);
129           a->handle = ps->vpp_server_handle;
130           a->app_index = pm->server_app_index;
131           vnet_disconnect_session (a);
132           ps->po_disconnected = 1;
133         }
134     }
135   else
136     {
137       a->handle = ps->vpp_server_handle;
138       a->app_index = pm->server_app_index;
139       vnet_disconnect_session (a);
140       ps->po_disconnected = 1;
141
142       if (!ps->ao_disconnected && !ps->active_open_establishing)
143         {
144           /* Proxy session closed before active open */
145           if (ps->vpp_active_open_handle != SESSION_INVALID_HANDLE)
146             {
147               a->handle = ps->vpp_active_open_handle;
148               a->app_index = pm->active_open_app_index;
149               vnet_disconnect_session (a);
150             }
151           ps->ao_disconnected = 1;
152         }
153     }
154   clib_spinlock_unlock_if_init (&pm->sessions_lock);
155 }
156
157 static void
158 proxy_try_delete_session (session_t * s, u8 is_active_open)
159 {
160   proxy_main_t *pm = &proxy_main;
161   proxy_session_t *ps = 0;
162
163   clib_spinlock_lock_if_init (&pm->sessions_lock);
164
165   ps = proxy_session_get (s->opaque);
166
167   if (is_active_open)
168     {
169       ps->vpp_active_open_handle = SESSION_INVALID_HANDLE;
170
171       if (ps->vpp_server_handle == SESSION_INVALID_HANDLE)
172         proxy_session_free (ps);
173     }
174   else
175     {
176       ps->vpp_server_handle = SESSION_INVALID_HANDLE;
177
178       if (ps->vpp_active_open_handle == SESSION_INVALID_HANDLE)
179         {
180           if (!ps->active_open_establishing)
181             proxy_session_free (ps);
182         }
183     }
184   clib_spinlock_unlock_if_init (&pm->sessions_lock);
185 }
186
187 static int
188 common_fifo_tuning_callback (session_t * s, svm_fifo_t * f,
189                              session_ft_action_t act, u32 bytes)
190 {
191   proxy_main_t *pm = &proxy_main;
192
193   segment_manager_t *sm = segment_manager_get (f->segment_manager);
194   fifo_segment_t *fs = segment_manager_get_segment (sm, f->segment_index);
195
196   u8 seg_usage = fifo_segment_get_mem_usage (fs);
197   u32 fifo_in_use = svm_fifo_max_dequeue_prod (f);
198   u32 fifo_size = svm_fifo_size (f);
199   u8 fifo_usage = fifo_in_use * 100 / fifo_size;
200   u8 update_size = 0;
201
202   ASSERT (act < SESSION_FT_ACTION_N_ACTIONS);
203
204   if (act == SESSION_FT_ACTION_ENQUEUED)
205     {
206       if (seg_usage < pm->low_watermark && fifo_usage > 50)
207         update_size = fifo_in_use;
208       else if (seg_usage < pm->high_watermark && fifo_usage > 80)
209         update_size = fifo_in_use;
210
211       update_size = clib_min (update_size, sm->max_fifo_size - fifo_size);
212       if (update_size)
213         svm_fifo_set_size (f, fifo_size + update_size);
214     }
215   else                          /* dequeued */
216     {
217       if (seg_usage > pm->high_watermark || fifo_usage < 20)
218         update_size = bytes;
219       else if (seg_usage > pm->low_watermark && fifo_usage < 50)
220         update_size = (bytes / 2);
221
222       ASSERT (fifo_size >= 4096);
223       update_size = clib_min (update_size, fifo_size - 4096);
224       if (update_size)
225         svm_fifo_set_size (f, fifo_size - update_size);
226     }
227
228   return 0;
229 }
230
231 static int
232 proxy_accept_callback (session_t * s)
233 {
234   proxy_main_t *pm = &proxy_main;
235   proxy_session_t *ps;
236
237   clib_spinlock_lock_if_init (&pm->sessions_lock);
238
239   ps = proxy_session_alloc ();
240   ps->vpp_server_handle = session_handle (s);
241   ps->vpp_active_open_handle = SESSION_INVALID_HANDLE;
242
243   s->opaque = ps->ps_index;
244
245   clib_spinlock_unlock_if_init (&pm->sessions_lock);
246
247   s->session_state = SESSION_STATE_READY;
248
249   return 0;
250 }
251
252 static void
253 proxy_disconnect_callback (session_t * s)
254 {
255   proxy_try_close_session (s, 0 /* is_active_open */ );
256 }
257
258 static void
259 proxy_reset_callback (session_t * s)
260 {
261   proxy_try_close_session (s, 0 /* is_active_open */ );
262 }
263
264 static int
265 proxy_connected_callback (u32 app_index, u32 api_context,
266                           session_t * s, session_error_t err)
267 {
268   clib_warning ("called...");
269   return -1;
270 }
271
272 static int
273 proxy_add_segment_callback (u32 client_index, u64 segment_handle)
274 {
275   return 0;
276 }
277
278 static int
279 proxy_transport_needs_crypto (transport_proto_t proto)
280 {
281   return proto == TRANSPORT_PROTO_TLS;
282 }
283
284 static int
285 proxy_rx_callback (session_t * s)
286 {
287   proxy_main_t *pm = &proxy_main;
288   u32 thread_index = vlib_get_thread_index ();
289   svm_fifo_t *ao_tx_fifo;
290   proxy_session_t *ps;
291
292   ASSERT (s->thread_index == thread_index);
293
294   clib_spinlock_lock_if_init (&pm->sessions_lock);
295
296   ps = proxy_session_get (s->opaque);
297
298   if (PREDICT_TRUE (ps->vpp_active_open_handle != SESSION_INVALID_HANDLE))
299     {
300       clib_spinlock_unlock_if_init (&pm->sessions_lock);
301
302       ao_tx_fifo = s->rx_fifo;
303
304       /*
305        * Send event for active open tx fifo
306        */
307       if (svm_fifo_set_event (ao_tx_fifo))
308         {
309           u32 ao_thread_index = ao_tx_fifo->master_thread_index;
310           u32 ao_session_index = ao_tx_fifo->shr->master_session_index;
311           if (session_send_io_evt_to_thread_custom (&ao_session_index,
312                                                     ao_thread_index,
313                                                     SESSION_IO_EVT_TX))
314             clib_warning ("failed to enqueue tx evt");
315         }
316
317       if (svm_fifo_max_enqueue (ao_tx_fifo) <= TCP_MSS)
318         svm_fifo_add_want_deq_ntf (ao_tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
319     }
320   else
321     {
322       vnet_connect_args_t _a, *a = &_a;
323       svm_fifo_t *tx_fifo, *rx_fifo;
324       u32 max_dequeue, ps_index;
325       int actual_transfer __attribute__ ((unused));
326
327       rx_fifo = s->rx_fifo;
328       tx_fifo = s->tx_fifo;
329
330       ASSERT (rx_fifo->master_thread_index == thread_index);
331       ASSERT (tx_fifo->master_thread_index == thread_index);
332
333       max_dequeue = svm_fifo_max_dequeue_cons (s->rx_fifo);
334
335       if (PREDICT_FALSE (max_dequeue == 0))
336         {
337           clib_spinlock_unlock_if_init (&pm->sessions_lock);
338           return 0;
339         }
340
341       max_dequeue = clib_min (pm->rcv_buffer_size, max_dequeue);
342       actual_transfer = svm_fifo_peek (rx_fifo, 0 /* relative_offset */ ,
343                                        max_dequeue, pm->rx_buf[thread_index]);
344
345       /* $$$ your message in this space: parse url, etc. */
346
347       clib_memset (a, 0, sizeof (*a));
348
349       ps->server_rx_fifo = rx_fifo;
350       ps->server_tx_fifo = tx_fifo;
351       ps->active_open_establishing = 1;
352       ps_index = ps->ps_index;
353
354       clib_spinlock_unlock_if_init (&pm->sessions_lock);
355
356       clib_memcpy (&a->sep_ext, &pm->client_sep, sizeof (pm->client_sep));
357       a->api_context = ps_index;
358       a->app_index = pm->active_open_app_index;
359
360       if (proxy_transport_needs_crypto (a->sep.transport_proto))
361         {
362           session_endpoint_alloc_ext_cfg (&a->sep_ext,
363                                           TRANSPORT_ENDPT_EXT_CFG_CRYPTO);
364           a->sep_ext.ext_cfg->crypto.ckpair_index = pm->ckpair_index;
365         }
366
367       proxy_call_main_thread (a);
368     }
369
370   return 0;
371 }
372
373 static void
374 proxy_force_ack (void *handlep)
375 {
376   transport_connection_t *tc;
377   session_t *ao_s;
378
379   ao_s = session_get_from_handle (pointer_to_uword (handlep));
380   if (session_get_transport_proto (ao_s) != TRANSPORT_PROTO_TCP)
381     return;
382   tc = session_get_transport (ao_s);
383   tcp_send_ack ((tcp_connection_t *) tc);
384 }
385
386 static int
387 proxy_tx_callback (session_t * proxy_s)
388 {
389   proxy_main_t *pm = &proxy_main;
390   proxy_session_t *ps;
391   u32 min_free;
392
393   min_free = clib_min (svm_fifo_size (proxy_s->tx_fifo) >> 3, 128 << 10);
394   if (svm_fifo_max_enqueue (proxy_s->tx_fifo) < min_free)
395     {
396       svm_fifo_add_want_deq_ntf (proxy_s->tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
397       return 0;
398     }
399
400   clib_spinlock_lock_if_init (&pm->sessions_lock);
401
402   ps = proxy_session_get (proxy_s->opaque);
403
404   if (ps->vpp_active_open_handle == SESSION_INVALID_HANDLE)
405     goto unlock;
406
407   /* Force ack on active open side to update rcv wnd. Make sure it's done on
408    * the right thread */
409   void *arg = uword_to_pointer (ps->vpp_active_open_handle, void *);
410   session_send_rpc_evt_to_thread (ps->server_rx_fifo->master_thread_index,
411                                   proxy_force_ack, arg);
412
413 unlock:
414   clib_spinlock_unlock_if_init (&pm->sessions_lock);
415
416   return 0;
417 }
418
419 static void
420 proxy_cleanup_callback (session_t * s, session_cleanup_ntf_t ntf)
421 {
422   if (ntf == SESSION_CLEANUP_TRANSPORT)
423     return;
424
425   proxy_try_delete_session (s, 0 /* is_active_open */ );
426 }
427
428 static session_cb_vft_t proxy_session_cb_vft = {
429   .session_accept_callback = proxy_accept_callback,
430   .session_disconnect_callback = proxy_disconnect_callback,
431   .session_connected_callback = proxy_connected_callback,
432   .add_segment_callback = proxy_add_segment_callback,
433   .builtin_app_rx_callback = proxy_rx_callback,
434   .builtin_app_tx_callback = proxy_tx_callback,
435   .session_reset_callback = proxy_reset_callback,
436   .session_cleanup_callback = proxy_cleanup_callback,
437   .fifo_tuning_callback = common_fifo_tuning_callback
438 };
439
440 static int
441 active_open_connected_callback (u32 app_index, u32 opaque,
442                                 session_t * s, session_error_t err)
443 {
444   proxy_main_t *pm = &proxy_main;
445   proxy_session_t *ps;
446   u8 thread_index = vlib_get_thread_index ();
447
448   /*
449    * Setup proxy session handle.
450    */
451   clib_spinlock_lock_if_init (&pm->sessions_lock);
452
453   ps = proxy_session_get (opaque);
454
455   /* Connection failed */
456   if (err)
457     {
458       vnet_disconnect_args_t _a, *a = &_a;
459
460       a->handle = ps->vpp_server_handle;
461       a->app_index = pm->server_app_index;
462       vnet_disconnect_session (a);
463       ps->po_disconnected = 1;
464     }
465   else
466     {
467       ps->vpp_active_open_handle = session_handle (s);
468       ps->active_open_establishing = 0;
469     }
470
471   /* Passive open session was already closed! */
472   if (ps->po_disconnected)
473     {
474       /* Setup everything for the cleanup notification */
475       ps->ao_disconnected = 1;
476       clib_spinlock_unlock_if_init (&pm->sessions_lock);
477       return -1;
478     }
479
480   s->tx_fifo = ps->server_rx_fifo;
481   s->rx_fifo = ps->server_tx_fifo;
482
483   /*
484    * Reset the active-open tx-fifo master indices so the active-open session
485    * will receive data, etc.
486    */
487   s->tx_fifo->shr->master_session_index = s->session_index;
488   s->tx_fifo->master_thread_index = s->thread_index;
489
490   /*
491    * Account for the active-open session's use of the fifos
492    * so they won't disappear until the last session which uses
493    * them disappears
494    */
495   s->tx_fifo->refcnt++;
496   s->rx_fifo->refcnt++;
497
498   s->opaque = opaque;
499
500   clib_spinlock_unlock_if_init (&pm->sessions_lock);
501
502   /*
503    * Send event for active open tx fifo
504    */
505   ASSERT (s->thread_index == thread_index);
506   if (svm_fifo_set_event (s->tx_fifo))
507     session_send_io_evt_to_thread (s->tx_fifo, SESSION_IO_EVT_TX);
508
509   return 0;
510 }
511
512 static void
513 active_open_reset_callback (session_t * s)
514 {
515   proxy_try_close_session (s, 1 /* is_active_open */ );
516 }
517
518 static int
519 active_open_create_callback (session_t * s)
520 {
521   return 0;
522 }
523
524 static void
525 active_open_disconnect_callback (session_t * s)
526 {
527   proxy_try_close_session (s, 1 /* is_active_open */ );
528 }
529
530 static int
531 active_open_rx_callback (session_t * s)
532 {
533   svm_fifo_t *proxy_tx_fifo;
534
535   proxy_tx_fifo = s->rx_fifo;
536
537   /*
538    * Send event for server tx fifo
539    */
540   if (svm_fifo_set_event (proxy_tx_fifo))
541     {
542       u8 thread_index = proxy_tx_fifo->master_thread_index;
543       u32 session_index = proxy_tx_fifo->shr->master_session_index;
544       return session_send_io_evt_to_thread_custom (&session_index,
545                                                    thread_index,
546                                                    SESSION_IO_EVT_TX);
547     }
548
549   if (svm_fifo_max_enqueue (proxy_tx_fifo) <= TCP_MSS)
550     svm_fifo_add_want_deq_ntf (proxy_tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
551
552   return 0;
553 }
554
555 static int
556 active_open_tx_callback (session_t * ao_s)
557 {
558   proxy_main_t *pm = &proxy_main;
559   transport_connection_t *tc;
560   proxy_session_t *ps;
561   session_t *proxy_s;
562   u32 min_free;
563
564   min_free = clib_min (svm_fifo_size (ao_s->tx_fifo) >> 3, 128 << 10);
565   if (svm_fifo_max_enqueue (ao_s->tx_fifo) < min_free)
566     {
567       svm_fifo_add_want_deq_ntf (ao_s->tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
568       return 0;
569     }
570
571   clib_spinlock_lock_if_init (&pm->sessions_lock);
572
573   ps = proxy_session_get_if_valid (ao_s->opaque);
574   if (!ps)
575     goto unlock;
576
577   if (ps->vpp_server_handle == ~0)
578     goto unlock;
579
580   proxy_s = session_get_from_handle (ps->vpp_server_handle);
581
582   /* Force ack on proxy side to update rcv wnd */
583   tc = session_get_transport (proxy_s);
584   tcp_send_ack ((tcp_connection_t *) tc);
585
586 unlock:
587   clib_spinlock_unlock_if_init (&pm->sessions_lock);
588
589   return 0;
590 }
591
592 static void
593 active_open_cleanup_callback (session_t * s, session_cleanup_ntf_t ntf)
594 {
595   if (ntf == SESSION_CLEANUP_TRANSPORT)
596     return;
597
598   proxy_try_delete_session (s, 1 /* is_active_open */ );
599 }
600
601 /* *INDENT-OFF* */
602 static session_cb_vft_t active_open_clients = {
603   .session_reset_callback = active_open_reset_callback,
604   .session_connected_callback = active_open_connected_callback,
605   .session_accept_callback = active_open_create_callback,
606   .session_disconnect_callback = active_open_disconnect_callback,
607   .session_cleanup_callback = active_open_cleanup_callback,
608   .builtin_app_rx_callback = active_open_rx_callback,
609   .builtin_app_tx_callback = active_open_tx_callback,
610   .fifo_tuning_callback = common_fifo_tuning_callback
611 };
612 /* *INDENT-ON* */
613
614 static int
615 proxy_server_attach ()
616 {
617   proxy_main_t *pm = &proxy_main;
618   u64 options[APP_OPTIONS_N_OPTIONS];
619   vnet_app_attach_args_t _a, *a = &_a;
620   u32 segment_size = 512 << 20;
621
622   clib_memset (a, 0, sizeof (*a));
623   clib_memset (options, 0, sizeof (options));
624
625   if (pm->private_segment_size)
626     segment_size = pm->private_segment_size;
627   a->name = format (0, "proxy-server");
628   a->api_client_index = pm->server_client_index;
629   a->session_cb_vft = &proxy_session_cb_vft;
630   a->options = options;
631   a->options[APP_OPTIONS_SEGMENT_SIZE] = segment_size;
632   a->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = segment_size;
633   a->options[APP_OPTIONS_RX_FIFO_SIZE] = pm->fifo_size;
634   a->options[APP_OPTIONS_TX_FIFO_SIZE] = pm->fifo_size;
635   a->options[APP_OPTIONS_MAX_FIFO_SIZE] = pm->max_fifo_size;
636   a->options[APP_OPTIONS_HIGH_WATERMARK] = (u64) pm->high_watermark;
637   a->options[APP_OPTIONS_LOW_WATERMARK] = (u64) pm->low_watermark;
638   a->options[APP_OPTIONS_PRIVATE_SEGMENT_COUNT] = pm->private_segment_count;
639   a->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] =
640     pm->prealloc_fifos ? pm->prealloc_fifos : 0;
641
642   a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
643
644   if (vnet_application_attach (a))
645     {
646       clib_warning ("failed to attach server");
647       return -1;
648     }
649   pm->server_app_index = a->app_index;
650
651   vec_free (a->name);
652   return 0;
653 }
654
655 static int
656 active_open_attach (void)
657 {
658   proxy_main_t *pm = &proxy_main;
659   vnet_app_attach_args_t _a, *a = &_a;
660   u64 options[APP_OPTIONS_N_OPTIONS];
661
662   clib_memset (a, 0, sizeof (*a));
663   clib_memset (options, 0, sizeof (options));
664
665   a->api_client_index = pm->active_open_client_index;
666   a->session_cb_vft = &active_open_clients;
667   a->name = format (0, "proxy-active-open");
668
669   options[APP_OPTIONS_ACCEPT_COOKIE] = 0x12345678;
670   options[APP_OPTIONS_SEGMENT_SIZE] = 512 << 20;
671   options[APP_OPTIONS_RX_FIFO_SIZE] = pm->fifo_size;
672   options[APP_OPTIONS_TX_FIFO_SIZE] = pm->fifo_size;
673   options[APP_OPTIONS_MAX_FIFO_SIZE] = pm->max_fifo_size;
674   options[APP_OPTIONS_HIGH_WATERMARK] = (u64) pm->high_watermark;
675   options[APP_OPTIONS_LOW_WATERMARK] = (u64) pm->low_watermark;
676   options[APP_OPTIONS_PRIVATE_SEGMENT_COUNT] = pm->private_segment_count;
677   options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] =
678     pm->prealloc_fifos ? pm->prealloc_fifos : 0;
679
680   options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN
681     | APP_OPTIONS_FLAGS_IS_PROXY;
682
683   a->options = options;
684
685   if (vnet_application_attach (a))
686     return -1;
687
688   pm->active_open_app_index = a->app_index;
689
690   vec_free (a->name);
691
692   return 0;
693 }
694
695 static int
696 proxy_server_listen ()
697 {
698   proxy_main_t *pm = &proxy_main;
699   vnet_listen_args_t _a, *a = &_a;
700   int rv;
701
702   clib_memset (a, 0, sizeof (*a));
703
704   a->app_index = pm->server_app_index;
705   clib_memcpy (&a->sep_ext, &pm->server_sep, sizeof (pm->server_sep));
706   if (proxy_transport_needs_crypto (a->sep.transport_proto))
707     {
708       session_endpoint_alloc_ext_cfg (&a->sep_ext,
709                                       TRANSPORT_ENDPT_EXT_CFG_CRYPTO);
710       a->sep_ext.ext_cfg->crypto.ckpair_index = pm->ckpair_index;
711     }
712
713   rv = vnet_listen (a);
714   if (a->sep_ext.ext_cfg)
715     clib_mem_free (a->sep_ext.ext_cfg);
716
717   return rv;
718 }
719
720 static void
721 proxy_server_add_ckpair (void)
722 {
723   vnet_app_add_cert_key_pair_args_t _ck_pair, *ck_pair = &_ck_pair;
724   proxy_main_t *pm = &proxy_main;
725
726   clib_memset (ck_pair, 0, sizeof (*ck_pair));
727   ck_pair->cert = (u8 *) test_srv_crt_rsa;
728   ck_pair->key = (u8 *) test_srv_key_rsa;
729   ck_pair->cert_len = test_srv_crt_rsa_len;
730   ck_pair->key_len = test_srv_key_rsa_len;
731   vnet_app_add_cert_key_pair (ck_pair);
732
733   pm->ckpair_index = ck_pair->index;
734 }
735
736 static int
737 proxy_server_create (vlib_main_t * vm)
738 {
739   vlib_thread_main_t *vtm = vlib_get_thread_main ();
740   proxy_main_t *pm = &proxy_main;
741   u32 num_threads;
742   int i;
743
744   num_threads = 1 /* main thread */  + vtm->n_threads;
745   vec_validate (pm->rx_buf, num_threads - 1);
746
747   for (i = 0; i < num_threads; i++)
748     vec_validate (pm->rx_buf[i], pm->rcv_buffer_size);
749
750   proxy_server_add_ckpair ();
751
752   if (proxy_server_attach ())
753     {
754       clib_warning ("failed to attach server app");
755       return -1;
756     }
757   if (proxy_server_listen ())
758     {
759       clib_warning ("failed to start listening");
760       return -1;
761     }
762   if (active_open_attach ())
763     {
764       clib_warning ("failed to attach active open app");
765       return -1;
766     }
767
768   return 0;
769 }
770
771 static clib_error_t *
772 proxy_server_create_command_fn (vlib_main_t * vm, unformat_input_t * input,
773                                 vlib_cli_command_t * cmd)
774 {
775   unformat_input_t _line_input, *line_input = &_line_input;
776   char *default_server_uri = "tcp://0.0.0.0/23";
777   char *default_client_uri = "tcp://6.0.2.2/23";
778   u8 *server_uri = 0, *client_uri = 0;
779   proxy_main_t *pm = &proxy_main;
780   clib_error_t *error = 0;
781   int rv, tmp32;
782   u64 tmp64;
783
784   pm->fifo_size = 64 << 10;
785   pm->max_fifo_size = 128 << 20;
786   pm->high_watermark = 80;
787   pm->low_watermark = 50;
788   pm->rcv_buffer_size = 1024;
789   pm->prealloc_fifos = 0;
790   pm->private_segment_count = 0;
791   pm->private_segment_size = 0;
792
793   if (vlib_num_workers ())
794     clib_spinlock_init (&pm->sessions_lock);
795
796   if (!unformat_user (input, unformat_line_input, line_input))
797     return 0;
798
799   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
800     {
801       if (unformat (line_input, "fifo-size %U", unformat_memory_size,
802                     &pm->fifo_size))
803         ;
804       else if (unformat (line_input, "max-fifo-size %U", unformat_memory_size,
805                          &pm->max_fifo_size))
806         ;
807       else if (unformat (line_input, "high-watermark %d", &tmp32))
808         pm->high_watermark = (u8) tmp32;
809       else if (unformat (line_input, "low-watermark %d", &tmp32))
810         pm->low_watermark = (u8) tmp32;
811       else if (unformat (line_input, "rcv-buf-size %d", &pm->rcv_buffer_size))
812         ;
813       else if (unformat (line_input, "prealloc-fifos %d", &pm->prealloc_fifos))
814         ;
815       else if (unformat (line_input, "private-segment-count %d",
816                          &pm->private_segment_count))
817         ;
818       else if (unformat (line_input, "private-segment-size %U",
819                          unformat_memory_size, &tmp64))
820         {
821           if (tmp64 >= 0x100000000ULL)
822             {
823               error = clib_error_return (
824                 0, "private segment size %lld (%llu) too large", tmp64, tmp64);
825               goto done;
826             }
827           pm->private_segment_size = tmp64;
828         }
829       else if (unformat (line_input, "server-uri %s", &server_uri))
830         vec_add1 (server_uri, 0);
831       else if (unformat (line_input, "client-uri %s", &client_uri))
832         vec_add1 (client_uri, 0);
833       else
834         {
835           error = clib_error_return (0, "unknown input `%U'",
836                                      format_unformat_error, line_input);
837           goto done;
838         }
839     }
840
841   if (!server_uri)
842     {
843       clib_warning ("No server-uri provided, Using default: %s",
844                     default_server_uri);
845       server_uri = format (0, "%s%c", default_server_uri, 0);
846     }
847   if (!client_uri)
848     {
849       clib_warning ("No client-uri provided, Using default: %s",
850                     default_client_uri);
851       client_uri = format (0, "%s%c", default_client_uri, 0);
852     }
853
854   if (parse_uri ((char *) server_uri, &pm->server_sep))
855     {
856       error = clib_error_return (0, "Invalid server uri %v", server_uri);
857       goto done;
858     }
859   if (parse_uri ((char *) client_uri, &pm->client_sep))
860     {
861       error = clib_error_return (0, "Invalid client uri %v", client_uri);
862       goto done;
863     }
864
865   vnet_session_enable_disable (vm, 1 /* turn on session and transport */ );
866
867   rv = proxy_server_create (vm);
868   switch (rv)
869     {
870     case 0:
871       break;
872     default:
873       error = clib_error_return (0, "server_create returned %d", rv);
874     }
875
876 done:
877   unformat_free (line_input);
878   vec_free (client_uri);
879   vec_free (server_uri);
880   return error;
881 }
882
883 VLIB_CLI_COMMAND (proxy_create_command, static) =
884 {
885   .path = "test proxy server",
886   .short_help = "test proxy server [server-uri <tcp://ip/port>]"
887       "[client-uri <tcp://ip/port>][fifo-size <nn>[k|m]]"
888       "[max-fifo-size <nn>[k|m]][high-watermark <nn>]"
889       "[low-watermark <nn>][rcv-buf-size <nn>][prealloc-fifos <nn>]"
890       "[private-segment-size <mem>][private-segment-count <nn>]",
891   .function = proxy_server_create_command_fn,
892 };
893
894 clib_error_t *
895 proxy_main_init (vlib_main_t * vm)
896 {
897   proxy_main_t *pm = &proxy_main;
898   pm->server_client_index = ~0;
899   pm->active_open_client_index = ~0;
900
901   return 0;
902 }
903
904 VLIB_INIT_FUNCTION (proxy_main_init);
905
906 /*
907 * fd.io coding-style-patch-verification: ON
908 *
909 * Local Variables:
910 * eval: (c-set-style "gnu")
911 * End:
912 */