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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
23 proxy_main_t proxy_main;
29 session_endpoint_cfg_t sep;
32 } proxy_connect_args_t;
35 proxy_cb_fn (void *data, u32 data_len)
37 proxy_connect_args_t *pa = (proxy_connect_args_t *) data;
38 vnet_connect_args_t a;
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));
45 if (a.sep_ext.ext_cfg)
46 clib_mem_free (a.sep_ext.ext_cfg);
50 proxy_call_main_thread (vnet_connect_args_t * a)
52 if (vlib_get_thread_index () == 0)
55 if (a->sep_ext.ext_cfg)
56 clib_mem_free (a->sep_ext.ext_cfg);
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));
68 static proxy_session_t *
69 proxy_get_active_open (proxy_main_t * pm, session_handle_t handle)
71 proxy_session_t *ps = 0;
74 p = hash_get (pm->proxy_session_by_active_open_handle, handle);
76 ps = pool_elt_at_index (pm->sessions, p[0]);
80 static proxy_session_t *
81 proxy_get_passive_open (proxy_main_t * pm, session_handle_t handle)
83 proxy_session_t *ps = 0;
86 p = hash_get (pm->proxy_session_by_server_handle, handle);
88 ps = pool_elt_at_index (pm->sessions, p[0]);
93 proxy_try_close_session (session_t * s, int is_active_open)
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;
100 handle = session_handle (s);
102 clib_spinlock_lock_if_init (&pm->sessions_lock);
106 ps = proxy_get_active_open (pm, handle);
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;
114 if (!ps->po_disconnected)
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;
125 ps = proxy_get_passive_open (pm, handle);
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;
133 if (!ps->ao_disconnected && !ps->active_open_establishing)
135 /* Proxy session closed before active open */
136 if (ps->vpp_active_open_handle != SESSION_INVALID_HANDLE)
138 a->handle = ps->vpp_active_open_handle;
139 a->app_index = pm->active_open_app_index;
140 vnet_disconnect_session (a);
142 ps->ao_disconnected = 1;
145 clib_spinlock_unlock_if_init (&pm->sessions_lock);
149 proxy_session_free (proxy_session_t * ps)
151 proxy_main_t *pm = &proxy_main;
153 clib_memset (ps, 0xFE, sizeof (*ps));
154 pool_put (pm->sessions, ps);
158 proxy_try_delete_session (session_t * s, u8 is_active_open)
160 proxy_main_t *pm = &proxy_main;
161 proxy_session_t *ps = 0;
162 session_handle_t handle;
164 handle = session_handle (s);
166 clib_spinlock_lock_if_init (&pm->sessions_lock);
170 ps = proxy_get_active_open (pm, handle);
173 ps->vpp_active_open_handle = SESSION_INVALID_HANDLE;
174 hash_unset (pm->proxy_session_by_active_open_handle, handle);
176 if (ps->vpp_server_handle == SESSION_INVALID_HANDLE)
177 proxy_session_free (ps);
181 ps = proxy_get_passive_open (pm, handle);
184 ps->vpp_server_handle = SESSION_INVALID_HANDLE;
185 hash_unset (pm->proxy_session_by_server_handle, handle);
187 if (ps->vpp_active_open_handle == SESSION_INVALID_HANDLE)
189 if (!ps->active_open_establishing)
190 proxy_session_free (ps);
193 clib_spinlock_unlock_if_init (&pm->sessions_lock);
197 common_fifo_tuning_callback (session_t * s, svm_fifo_t * f,
198 session_ft_action_t act, u32 bytes)
200 proxy_main_t *pm = &proxy_main;
202 segment_manager_t *sm = segment_manager_get (f->segment_manager);
203 fifo_segment_t *fs = segment_manager_get_segment (sm, f->segment_index);
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;
211 ASSERT (act < SESSION_FT_ACTION_N_ACTIONS);
213 if (act == SESSION_FT_ACTION_ENQUEUED)
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;
220 update_size = clib_min (update_size, sm->max_fifo_size - fifo_size);
222 svm_fifo_set_size (f, fifo_size + update_size);
226 if (seg_usage > pm->high_watermark || fifo_usage < 20)
228 else if (seg_usage > pm->low_watermark && fifo_usage < 50)
229 update_size = (bytes / 2);
231 ASSERT (fifo_size >= 4096);
232 update_size = clib_min (update_size, fifo_size - 4096);
234 svm_fifo_set_size (f, fifo_size - update_size);
241 proxy_accept_callback (session_t * s)
243 proxy_main_t *pm = &proxy_main;
246 clib_spinlock_lock_if_init (&pm->sessions_lock);
248 pool_get_zero (pm->sessions, ps);
249 ps->vpp_server_handle = session_handle (s);
250 ps->vpp_active_open_handle = SESSION_INVALID_HANDLE;
252 hash_set (pm->proxy_session_by_server_handle, ps->vpp_server_handle,
255 clib_spinlock_unlock_if_init (&pm->sessions_lock);
257 s->session_state = SESSION_STATE_READY;
263 proxy_disconnect_callback (session_t * s)
265 proxy_try_close_session (s, 0 /* is_active_open */ );
269 proxy_reset_callback (session_t * s)
271 proxy_try_close_session (s, 0 /* is_active_open */ );
275 proxy_connected_callback (u32 app_index, u32 api_context,
276 session_t * s, session_error_t err)
278 clib_warning ("called...");
283 proxy_add_segment_callback (u32 client_index, u64 segment_handle)
289 proxy_transport_needs_crypto (transport_proto_t proto)
291 return proto == TRANSPORT_PROTO_TLS;
295 proxy_rx_callback (session_t * s)
297 proxy_main_t *pm = &proxy_main;
298 u32 thread_index = vlib_get_thread_index ();
299 svm_fifo_t *ao_tx_fifo;
302 ASSERT (s->thread_index == thread_index);
304 clib_spinlock_lock_if_init (&pm->sessions_lock);
306 ps = proxy_get_passive_open (pm, session_handle (s));
309 if (PREDICT_TRUE (ps->vpp_active_open_handle != SESSION_INVALID_HANDLE))
311 clib_spinlock_unlock_if_init (&pm->sessions_lock);
313 ao_tx_fifo = s->rx_fifo;
316 * Send event for active open tx fifo
318 if (svm_fifo_set_event (ao_tx_fifo))
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,
325 clib_warning ("failed to enqueue tx evt");
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);
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));
338 rx_fifo = s->rx_fifo;
339 tx_fifo = s->tx_fifo;
341 ASSERT (rx_fifo->master_thread_index == thread_index);
342 ASSERT (tx_fifo->master_thread_index == thread_index);
344 max_dequeue = svm_fifo_max_dequeue_cons (s->rx_fifo);
346 if (PREDICT_FALSE (max_dequeue == 0))
348 clib_spinlock_unlock_if_init (&pm->sessions_lock);
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]);
356 /* $$$ your message in this space: parse url, etc. */
358 clib_memset (a, 0, sizeof (*a));
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;
365 clib_spinlock_unlock_if_init (&pm->sessions_lock);
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;
371 if (proxy_transport_needs_crypto (a->sep.transport_proto))
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;
378 proxy_call_main_thread (a);
385 proxy_force_ack (void *handlep)
387 transport_connection_t *tc;
390 ao_s = session_get_from_handle (pointer_to_uword (handlep));
391 if (session_get_transport_proto (ao_s) != TRANSPORT_PROTO_TCP)
393 tc = session_get_transport (ao_s);
394 tcp_send_ack ((tcp_connection_t *) tc);
398 proxy_tx_callback (session_t * proxy_s)
400 proxy_main_t *pm = &proxy_main;
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)
407 svm_fifo_add_want_deq_ntf (proxy_s->tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
411 clib_spinlock_lock_if_init (&pm->sessions_lock);
413 ps = proxy_get_passive_open (pm, session_handle (proxy_s));
416 if (ps->vpp_active_open_handle == SESSION_INVALID_HANDLE)
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);
426 clib_spinlock_unlock_if_init (&pm->sessions_lock);
432 proxy_cleanup_callback (session_t * s, session_cleanup_ntf_t ntf)
434 if (ntf == SESSION_CLEANUP_TRANSPORT)
437 proxy_try_delete_session (s, 0 /* is_active_open */ );
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
453 active_open_connected_callback (u32 app_index, u32 opaque,
454 session_t * s, session_error_t err)
456 proxy_main_t *pm = &proxy_main;
458 u8 thread_index = vlib_get_thread_index ();
461 * Setup proxy session handle.
463 clib_spinlock_lock_if_init (&pm->sessions_lock);
465 ps = pool_elt_at_index (pm->sessions, opaque);
467 /* Connection failed */
470 vnet_disconnect_args_t _a, *a = &_a;
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;
479 ps->vpp_active_open_handle = session_handle (s);
480 ps->active_open_establishing = 0;
483 /* Passive open session was already closed! */
484 if (ps->po_disconnected)
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);
494 s->tx_fifo = ps->server_rx_fifo;
495 s->rx_fifo = ps->server_tx_fifo;
498 * Reset the active-open tx-fifo master indices so the active-open session
499 * will receive data, etc.
501 s->tx_fifo->shr->master_session_index = s->session_index;
502 s->tx_fifo->master_thread_index = s->thread_index;
505 * Account for the active-open session's use of the fifos
506 * so they won't disappear until the last session which uses
509 s->tx_fifo->refcnt++;
510 s->rx_fifo->refcnt++;
512 hash_set (pm->proxy_session_by_active_open_handle,
513 ps->vpp_active_open_handle, opaque);
515 clib_spinlock_unlock_if_init (&pm->sessions_lock);
518 * Send event for active open tx fifo
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);
528 active_open_reset_callback (session_t * s)
530 proxy_try_close_session (s, 1 /* is_active_open */ );
534 active_open_create_callback (session_t * s)
540 active_open_disconnect_callback (session_t * s)
542 proxy_try_close_session (s, 1 /* is_active_open */ );
546 active_open_rx_callback (session_t * s)
548 svm_fifo_t *proxy_tx_fifo;
550 proxy_tx_fifo = s->rx_fifo;
553 * Send event for server tx fifo
555 if (svm_fifo_set_event (proxy_tx_fifo))
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,
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);
571 active_open_tx_callback (session_t * ao_s)
573 proxy_main_t *pm = &proxy_main;
574 transport_connection_t *tc;
575 session_handle_t handle;
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)
584 svm_fifo_add_want_deq_ntf (ao_s->tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
588 clib_spinlock_lock_if_init (&pm->sessions_lock);
590 handle = session_handle (ao_s);
591 p = hash_get (pm->proxy_session_by_active_open_handle, handle);
595 if (pool_is_free_index (pm->sessions, p[0]))
598 ps = pool_elt_at_index (pm->sessions, p[0]);
599 if (ps->vpp_server_handle == ~0)
602 proxy_s = session_get_from_handle (ps->vpp_server_handle);
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);
609 clib_spinlock_unlock_if_init (&pm->sessions_lock);
615 active_open_cleanup_callback (session_t * s, session_cleanup_ntf_t ntf)
617 if (ntf == SESSION_CLEANUP_TRANSPORT)
620 proxy_try_delete_session (s, 1 /* is_active_open */ );
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
637 proxy_server_attach ()
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;
644 clib_memset (a, 0, sizeof (*a));
645 clib_memset (options, 0, sizeof (options));
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;
664 a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
666 if (vnet_application_attach (a))
668 clib_warning ("failed to attach server");
671 pm->server_app_index = a->app_index;
678 active_open_attach (void)
680 proxy_main_t *pm = &proxy_main;
681 vnet_app_attach_args_t _a, *a = &_a;
682 u64 options[APP_OPTIONS_N_OPTIONS];
684 clib_memset (a, 0, sizeof (*a));
685 clib_memset (options, 0, sizeof (options));
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");
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;
702 options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN
703 | APP_OPTIONS_FLAGS_IS_PROXY;
705 a->options = options;
707 if (vnet_application_attach (a))
710 pm->active_open_app_index = a->app_index;
718 proxy_server_listen ()
720 proxy_main_t *pm = &proxy_main;
721 vnet_listen_args_t _a, *a = &_a;
724 clib_memset (a, 0, sizeof (*a));
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))
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;
735 rv = vnet_listen (a);
736 if (a->sep_ext.ext_cfg)
737 clib_mem_free (a->sep_ext.ext_cfg);
743 proxy_server_add_ckpair (void)
745 vnet_app_add_cert_key_pair_args_t _ck_pair, *ck_pair = &_ck_pair;
746 proxy_main_t *pm = &proxy_main;
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);
755 pm->ckpair_index = ck_pair->index;
759 proxy_server_create (vlib_main_t * vm)
761 proxy_main_t *pm = &proxy_main;
762 vlib_thread_main_t *vtm = vlib_get_thread_main ();
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);
771 for (i = 0; i < num_threads; i++)
772 vec_validate (pm->rx_buf[i], pm->rcv_buffer_size);
774 proxy_server_add_ckpair ();
776 if (proxy_server_attach ())
778 clib_warning ("failed to attach server app");
781 if (proxy_server_listen ())
783 clib_warning ("failed to start listening");
786 if (active_open_attach ())
788 clib_warning ("failed to attach active open app");
792 for (i = 0; i < num_threads; i++)
794 pm->active_open_event_queue[i] = session_main_get_vpp_event_queue (i);
796 ASSERT (pm->active_open_event_queue[i]);
798 pm->server_event_queue[i] = session_main_get_vpp_event_queue (i);
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)
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;
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;
826 if (vlib_num_workers ())
827 clib_spinlock_init (&pm->sessions_lock);
829 if (!unformat_user (input, unformat_line_input, line_input))
832 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
834 if (unformat (line_input, "fifo-size %U", unformat_memory_size,
837 else if (unformat (line_input, "max-fifo-size %U", unformat_memory_size,
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))
846 else if (unformat (line_input, "prealloc-fifos %d", &pm->prealloc_fifos))
848 else if (unformat (line_input, "private-segment-count %d",
849 &pm->private_segment_count))
851 else if (unformat (line_input, "private-segment-size %U",
852 unformat_memory_size, &tmp64))
854 if (tmp64 >= 0x100000000ULL)
856 error = clib_error_return (
857 0, "private segment size %lld (%llu) too large", tmp64, tmp64);
860 pm->private_segment_size = tmp64;
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);
868 error = clib_error_return (0, "unknown input `%U'",
869 format_unformat_error, line_input);
876 clib_warning ("No server-uri provided, Using default: %s",
878 server_uri = format (0, "%s%c", default_server_uri, 0);
882 clib_warning ("No client-uri provided, Using default: %s",
884 client_uri = format (0, "%s%c", default_client_uri, 0);
887 if (parse_uri ((char *) server_uri, &pm->server_sep))
889 error = clib_error_return (0, "Invalid server uri %v", server_uri);
892 if (parse_uri ((char *) client_uri, &pm->client_sep))
894 error = clib_error_return (0, "Invalid client uri %v", client_uri);
898 vnet_session_enable_disable (vm, 1 /* turn on session and transport */ );
900 rv = proxy_server_create (vm);
906 error = clib_error_return (0, "server_create returned %d", rv);
910 unformat_free (line_input);
911 vec_free (client_uri);
912 vec_free (server_uri);
917 VLIB_CLI_COMMAND (proxy_create_command, static) =
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,
930 proxy_main_init (vlib_main_t * vm)
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));
941 VLIB_INIT_FUNCTION (proxy_main_init);
944 * fd.io coding-style-patch-verification: ON
947 * eval: (c-set-style "gnu")