2 * Copyright (c) 2015-2017 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
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 <vnet/session-apps/proxy.h>
22 proxy_main_t proxy_main;
25 delete_proxy_session (stream_session_t * s, int is_active_open)
27 proxy_main_t *pm = &proxy_main;
28 proxy_session_t *ps = 0;
29 vnet_disconnect_args_t _a, *a = &_a;
30 stream_session_t *active_open_session = 0;
31 stream_session_t *server_session = 0;
35 handle = session_handle (s);
37 clib_spinlock_lock_if_init (&pm->sessions_lock);
40 active_open_session = s;
42 p = hash_get (pm->proxy_session_by_active_open_handle, handle);
45 clib_warning ("proxy session for %s handle %lld (%llx) AWOL",
46 is_active_open ? "active open" : "server",
51 ps = pool_elt_at_index (pm->sessions, p[0]);
52 if (ps->vpp_server_handle != ~0)
53 server_session = session_get_from_handle (ps->vpp_server_handle);
62 p = hash_get (pm->proxy_session_by_server_handle, handle);
65 clib_warning ("proxy session for %s handle %lld (%llx) AWOL",
66 is_active_open ? "active open" : "server",
71 ps = pool_elt_at_index (pm->sessions, p[0]);
72 if (ps->vpp_server_handle != ~0)
73 active_open_session = session_get_from_handle
74 (ps->vpp_server_handle);
76 active_open_session = 0;
83 memset (ps, 0xFE, sizeof (*ps));
84 pool_put (pm->sessions, ps);
87 clib_spinlock_unlock_if_init (&pm->sessions_lock);
89 if (active_open_session)
91 a->handle = session_handle (active_open_session);
92 a->app_index = pm->active_open_app_index;
93 hash_unset (pm->proxy_session_by_active_open_handle,
94 session_handle (active_open_session));
95 vnet_disconnect_session (a);
100 a->handle = session_handle (server_session);
101 a->app_index = pm->server_app_index;
102 hash_unset (pm->proxy_session_by_server_handle,
103 session_handle (server_session));
104 vnet_disconnect_session (a);
109 proxy_accept_callback (stream_session_t * s)
111 proxy_main_t *pm = &proxy_main;
113 s->session_state = SESSION_STATE_READY;
115 clib_spinlock_lock_if_init (&pm->sessions_lock);
121 proxy_disconnect_callback (stream_session_t * s)
123 delete_proxy_session (s, 0 /* is_active_open */ );
127 proxy_reset_callback (stream_session_t * s)
129 clib_warning ("Reset session %U", format_stream_session, s, 2);
130 delete_proxy_session (s, 0 /* is_active_open */ );
134 proxy_connected_callback (u32 app_index, u32 api_context,
135 stream_session_t * s, u8 is_fail)
137 clib_warning ("called...");
142 proxy_add_segment_callback (u32 client_index, const ssvm_private_t * sp)
144 clib_warning ("called...");
149 proxy_redirect_connect_callback (u32 client_index, void *mp)
151 clib_warning ("called...");
156 proxy_rx_callback (stream_session_t * s)
159 int actual_transfer __attribute__ ((unused));
160 svm_fifo_t *tx_fifo, *rx_fifo;
161 proxy_main_t *pm = &proxy_main;
162 u32 thread_index = vlib_get_thread_index ();
163 vnet_connect_args_t _a, *a = &_a;
167 svm_fifo_t *active_open_tx_fifo;
168 session_fifo_event_t evt;
170 ASSERT (s->thread_index == thread_index);
172 clib_spinlock_lock_if_init (&pm->sessions_lock);
173 p = hash_get (pm->proxy_session_by_server_handle, session_handle (s));
175 if (PREDICT_TRUE (p != 0))
177 clib_spinlock_unlock_if_init (&pm->sessions_lock);
178 active_open_tx_fifo = s->server_rx_fifo;
181 * Send event for active open tx fifo
183 if (svm_fifo_set_event (active_open_tx_fifo))
185 evt.fifo = active_open_tx_fifo;
186 evt.event_type = FIFO_EVENT_APP_TX;
188 (pm->active_open_event_queue[thread_index], (u8 *) & evt,
189 0 /* do wait for mutex */ ))
190 clib_warning ("failed to enqueue tx evt");
195 rx_fifo = s->server_rx_fifo;
196 tx_fifo = s->server_tx_fifo;
198 ASSERT (rx_fifo->master_thread_index == thread_index);
199 ASSERT (tx_fifo->master_thread_index == thread_index);
201 max_dequeue = svm_fifo_max_dequeue (s->server_rx_fifo);
203 if (PREDICT_FALSE (max_dequeue == 0))
206 actual_transfer = svm_fifo_peek (rx_fifo, 0 /* relative_offset */ ,
207 max_dequeue, pm->rx_buf[thread_index]);
209 /* $$$ your message in this space: parse url, etc. */
211 memset (a, 0, sizeof (*a));
213 clib_spinlock_lock_if_init (&pm->sessions_lock);
214 pool_get (pm->sessions, ps);
215 memset (ps, 0, sizeof (*ps));
216 ps->server_rx_fifo = rx_fifo;
217 ps->server_tx_fifo = tx_fifo;
218 ps->vpp_server_handle = session_handle (s);
220 proxy_index = ps - pm->sessions;
222 hash_set (pm->proxy_session_by_server_handle, ps->vpp_server_handle,
225 clib_spinlock_unlock_if_init (&pm->sessions_lock);
227 a->uri = (char *) pm->client_uri;
228 a->api_context = proxy_index;
229 a->app_index = pm->active_open_app_index;
231 vnet_connect_uri (a);
237 static session_cb_vft_t proxy_session_cb_vft = {
238 .session_accept_callback = proxy_accept_callback,
239 .session_disconnect_callback = proxy_disconnect_callback,
240 .session_connected_callback = proxy_connected_callback,
241 .add_segment_callback = proxy_add_segment_callback,
242 .redirect_connect_callback = proxy_redirect_connect_callback,
243 .builtin_server_rx_callback = proxy_rx_callback,
244 .session_reset_callback = proxy_reset_callback
248 active_open_connected_callback (u32 app_index, u32 opaque,
249 stream_session_t * s, u8 is_fail)
251 proxy_main_t *pm = &proxy_main;
253 u8 thread_index = vlib_get_thread_index ();
254 session_fifo_event_t evt;
258 clib_warning ("connection %d failed!", opaque);
263 * Setup proxy session handle.
265 clib_spinlock_lock_if_init (&pm->sessions_lock);
267 ps = pool_elt_at_index (pm->sessions, opaque);
268 ps->vpp_active_open_handle = session_handle (s);
270 s->server_tx_fifo = ps->server_rx_fifo;
271 s->server_rx_fifo = ps->server_tx_fifo;
274 * Reset the active-open tx-fifo master indices so the active-open session
275 * will receive data, etc.
277 s->server_tx_fifo->master_session_index = s->session_index;
278 s->server_tx_fifo->master_thread_index = s->thread_index;
281 * Account for the active-open session's use of the fifos
282 * so they won't disappear until the last session which uses
285 s->server_tx_fifo->refcnt++;
286 s->server_rx_fifo->refcnt++;
288 hash_set (pm->proxy_session_by_active_open_handle,
289 ps->vpp_active_open_handle, opaque);
291 clib_spinlock_unlock_if_init (&pm->sessions_lock);
294 * Send event for active open tx fifo
296 if (svm_fifo_set_event (s->server_tx_fifo))
298 evt.fifo = s->server_tx_fifo;
299 evt.event_type = FIFO_EVENT_APP_TX;
301 (pm->active_open_event_queue[thread_index], (u8 *) & evt,
302 0 /* do wait for mutex */ ))
303 clib_warning ("failed to enqueue tx evt");
310 active_open_reset_callback (stream_session_t * s)
312 delete_proxy_session (s, 1 /* is_active_open */ );
316 active_open_create_callback (stream_session_t * s)
322 active_open_disconnect_callback (stream_session_t * s)
324 delete_proxy_session (s, 1 /* is_active_open */ );
328 active_open_rx_callback (stream_session_t * s)
330 proxy_main_t *pm = &proxy_main;
331 session_fifo_event_t evt;
332 svm_fifo_t *server_rx_fifo;
333 u32 thread_index = vlib_get_thread_index ();
335 server_rx_fifo = s->server_rx_fifo;
338 * Send event for server tx fifo
340 if (svm_fifo_set_event (server_rx_fifo))
342 evt.fifo = server_rx_fifo;
343 evt.event_type = FIFO_EVENT_APP_TX;
345 (pm->server_event_queue[thread_index], (u8 *) & evt,
346 0 /* do wait for mutex */ ))
347 clib_warning ("failed to enqueue server rx evt");
354 static session_cb_vft_t active_open_clients = {
355 .session_reset_callback = active_open_reset_callback,
356 .session_connected_callback = active_open_connected_callback,
357 .session_accept_callback = active_open_create_callback,
358 .session_disconnect_callback = active_open_disconnect_callback,
359 .builtin_server_rx_callback = active_open_rx_callback
365 create_api_loopbacks (vlib_main_t * vm)
367 proxy_main_t *pm = &proxy_main;
368 api_main_t *am = &api_main;
369 vl_shmem_hdr_t *shmem_hdr;
371 shmem_hdr = am->shmem_hdr;
372 pm->vl_input_queue = shmem_hdr->vl_input_queue;
373 pm->server_client_index =
374 vl_api_memclnt_create_internal ("proxy_server", pm->vl_input_queue);
375 pm->active_open_client_index =
376 vl_api_memclnt_create_internal ("proxy_active_open", pm->vl_input_queue);
380 proxy_server_attach ()
382 proxy_main_t *pm = &proxy_main;
383 u64 options[APP_OPTIONS_N_OPTIONS];
384 vnet_app_attach_args_t _a, *a = &_a;
385 u32 segment_size = 512 << 20;
387 memset (a, 0, sizeof (*a));
388 memset (options, 0, sizeof (options));
390 if (pm->private_segment_size)
391 segment_size = pm->private_segment_size;
392 a->api_client_index = pm->server_client_index;
393 a->session_cb_vft = &proxy_session_cb_vft;
394 a->options = options;
395 a->options[APP_OPTIONS_SEGMENT_SIZE] = segment_size;
396 a->options[APP_OPTIONS_RX_FIFO_SIZE] = pm->fifo_size;
397 a->options[APP_OPTIONS_TX_FIFO_SIZE] = pm->fifo_size;
398 a->options[APP_OPTIONS_PRIVATE_SEGMENT_COUNT] = pm->private_segment_count;
399 a->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] =
400 pm->prealloc_fifos ? pm->prealloc_fifos : 1;
402 a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
404 if (vnet_application_attach (a))
406 clib_warning ("failed to attach server");
409 pm->server_app_index = a->app_index;
415 active_open_attach (void)
417 proxy_main_t *pm = &proxy_main;
418 vnet_app_attach_args_t _a, *a = &_a;
421 memset (a, 0, sizeof (*a));
422 memset (options, 0, sizeof (options));
424 a->api_client_index = pm->active_open_client_index;
425 a->session_cb_vft = &active_open_clients;
427 options[APP_OPTIONS_ACCEPT_COOKIE] = 0x12345678;
428 options[APP_OPTIONS_SEGMENT_SIZE] = 512 << 20;
429 options[APP_OPTIONS_RX_FIFO_SIZE] = pm->fifo_size;
430 options[APP_OPTIONS_TX_FIFO_SIZE] = pm->fifo_size;
431 options[APP_OPTIONS_PRIVATE_SEGMENT_COUNT] = pm->private_segment_count;
432 options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] =
433 pm->prealloc_fifos ? pm->prealloc_fifos : 1;
435 options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN
436 | APP_OPTIONS_FLAGS_IS_PROXY;
438 a->options = options;
440 if (vnet_application_attach (a))
443 pm->active_open_app_index = a->app_index;
449 proxy_server_listen ()
451 proxy_main_t *pm = &proxy_main;
452 vnet_bind_args_t _a, *a = &_a;
453 memset (a, 0, sizeof (*a));
454 a->app_index = pm->server_app_index;
455 a->uri = (char *) pm->server_uri;
456 return vnet_bind_uri (a);
460 proxy_server_create (vlib_main_t * vm)
462 proxy_main_t *pm = &proxy_main;
463 vlib_thread_main_t *vtm = vlib_get_thread_main ();
467 if (pm->server_client_index == (u32) ~ 0)
468 create_api_loopbacks (vm);
470 num_threads = 1 /* main thread */ + vtm->n_threads;
471 vec_validate (proxy_main.server_event_queue, num_threads - 1);
472 vec_validate (proxy_main.active_open_event_queue, num_threads - 1);
473 vec_validate (pm->rx_buf, num_threads - 1);
475 for (i = 0; i < num_threads; i++)
476 vec_validate (pm->rx_buf[i], pm->rcv_buffer_size);
478 if (proxy_server_attach ())
480 clib_warning ("failed to attach server app");
483 if (proxy_server_listen ())
485 clib_warning ("failed to start listening");
488 if (active_open_attach ())
490 clib_warning ("failed to attach active open app");
494 for (i = 0; i < num_threads; i++)
496 pm->active_open_event_queue[i] =
497 session_manager_get_vpp_event_queue (i);
499 ASSERT (pm->active_open_event_queue[i]);
501 pm->server_event_queue[i] = session_manager_get_vpp_event_queue (i);
507 static clib_error_t *
508 proxy_server_create_command_fn (vlib_main_t * vm, unformat_input_t * input,
509 vlib_cli_command_t * cmd)
511 proxy_main_t *pm = &proxy_main;
512 char *default_server_uri = "tcp://0.0.0.0/23";
513 char *default_client_uri = "tcp://6.0.2.2/23";
517 pm->fifo_size = 64 << 10;
518 pm->rcv_buffer_size = 1024;
519 pm->prealloc_fifos = 0;
520 pm->private_segment_count = 0;
521 pm->private_segment_size = 0;
524 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
526 if (unformat (input, "fifo-size %d", &pm->fifo_size))
527 pm->fifo_size <<= 10;
528 else if (unformat (input, "rcv-buf-size %d", &pm->rcv_buffer_size))
530 else if (unformat (input, "prealloc-fifos %d", &pm->prealloc_fifos))
532 else if (unformat (input, "private-segment-count %d",
533 &pm->private_segment_count))
535 else if (unformat (input, "private-segment-size %U",
536 unformat_memory_size, &tmp))
538 if (tmp >= 0x100000000ULL)
539 return clib_error_return
540 (0, "private segment size %lld (%llu) too large", tmp, tmp);
541 pm->private_segment_size = tmp;
543 else if (unformat (input, "server-uri %s", &pm->server_uri))
545 else if (unformat (input, "client-uri %s", &pm->client_uri))
548 return clib_error_return (0, "unknown input `%U'",
549 format_unformat_error, input);
554 clib_warning ("No server-uri provided, Using default: %s",
556 pm->server_uri = format (0, "%s%c", default_server_uri, 0);
560 clib_warning ("No client-uri provided, Using default: %s",
562 pm->client_uri = format (0, "%s%c", default_client_uri, 0);
565 vnet_session_enable_disable (vm, 1 /* turn on session and transport */ );
567 rv = proxy_server_create (vm);
573 return clib_error_return (0, "server_create returned %d", rv);
580 VLIB_CLI_COMMAND (proxy_create_command, static) =
582 .path = "test proxy server",
583 .short_help = "test proxy server [server-uri <tcp://ip/port>]"
584 "[client-uri <tcp://ip/port>][fifo-size <nn>][rcv-buf-size <nn>]"
585 "[prealloc-fifos <nn>][private-segment-size <mem>]"
586 "[private-segment-count <nn>]",
587 .function = proxy_server_create_command_fn,
592 proxy_main_init (vlib_main_t * vm)
594 proxy_main_t *pm = &proxy_main;
595 pm->server_client_index = ~0;
596 pm->active_open_client_index = ~0;
597 pm->proxy_session_by_active_open_handle = hash_create (0, sizeof (uword));
598 pm->proxy_session_by_server_handle = hash_create (0, sizeof (uword));
603 VLIB_INIT_FUNCTION (proxy_main_init);
606 * fd.io coding-style-patch-verification: ON
609 * eval: (c-set-style "gnu")