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