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