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/tcp/builtin_proxy.h>
22 builtin_proxy_main_t builtin_proxy_main;
25 delete_proxy_session (stream_session_t * s, int is_active_open)
27 builtin_proxy_main_t *bpm = &builtin_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 = stream_session_handle (s);
37 clib_spinlock_lock_if_init (&bpm->sessions_lock);
40 active_open_session = s;
42 p = hash_get (bpm->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 (bpm->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 (bpm->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 (bpm->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 (bpm->sessions, ps);
87 clib_spinlock_unlock_if_init (&bpm->sessions_lock);
89 if (active_open_session)
91 a->handle = stream_session_handle (active_open_session);
92 a->app_index = bpm->active_open_app_index;
93 hash_unset (bpm->proxy_session_by_active_open_handle,
94 stream_session_handle (active_open_session));
95 vnet_disconnect_session (a);
100 a->handle = stream_session_handle (server_session);
101 a->app_index = bpm->server_app_index;
102 hash_unset (bpm->proxy_session_by_server_handle,
103 stream_session_handle (server_session));
104 vnet_disconnect_session (a);
109 server_accept_callback (stream_session_t * s)
111 builtin_proxy_main_t *bpm = &builtin_proxy_main;
113 s->session_state = SESSION_STATE_READY;
115 clib_spinlock_lock_if_init (&bpm->sessions_lock);
121 server_disconnect_callback (stream_session_t * s)
123 delete_proxy_session (s, 0 /* is_active_open */ );
127 server_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 server_connected_callback (u32 app_index, u32 api_context,
135 stream_session_t * s, u8 is_fail)
137 clib_warning ("called...");
142 server_add_segment_callback (u32 client_index,
143 const u8 * seg_name, u32 seg_size)
145 clib_warning ("called...");
150 server_redirect_connect_callback (u32 client_index, void *mp)
152 clib_warning ("called...");
157 server_rx_callback (stream_session_t * s)
160 int actual_transfer __attribute__ ((unused));
161 svm_fifo_t *tx_fifo, *rx_fifo;
162 builtin_proxy_main_t *bpm = &builtin_proxy_main;
163 u32 thread_index = vlib_get_thread_index ();
164 vnet_connect_args_t _a, *a = &_a;
168 svm_fifo_t *active_open_tx_fifo;
169 session_fifo_event_t evt;
171 ASSERT (s->thread_index == thread_index);
173 clib_spinlock_lock_if_init (&bpm->sessions_lock);
175 hash_get (bpm->proxy_session_by_server_handle, stream_session_handle (s));
177 if (PREDICT_TRUE (p != 0))
179 clib_spinlock_unlock_if_init (&bpm->sessions_lock);
180 active_open_tx_fifo = s->server_rx_fifo;
183 * Send event for active open tx fifo
185 if (svm_fifo_set_event (active_open_tx_fifo))
187 evt.fifo = active_open_tx_fifo;
188 evt.event_type = FIFO_EVENT_APP_TX;
189 if (unix_shared_memory_queue_add
190 (bpm->active_open_event_queue[thread_index], (u8 *) & evt,
191 0 /* do wait for mutex */ ))
192 clib_warning ("failed to enqueue tx evt");
197 rx_fifo = s->server_rx_fifo;
198 tx_fifo = s->server_tx_fifo;
200 ASSERT (rx_fifo->master_thread_index == thread_index);
201 ASSERT (tx_fifo->master_thread_index == thread_index);
203 max_dequeue = svm_fifo_max_dequeue (s->server_rx_fifo);
205 if (PREDICT_FALSE (max_dequeue == 0))
208 actual_transfer = svm_fifo_peek (rx_fifo, 0 /* relative_offset */ ,
210 bpm->rx_buf[thread_index]);
212 /* $$$ your message in this space: parse url, etc. */
214 memset (a, 0, sizeof (*a));
216 clib_spinlock_lock_if_init (&bpm->sessions_lock);
217 pool_get (bpm->sessions, ps);
218 memset (ps, 0, sizeof (*ps));
219 ps->server_rx_fifo = rx_fifo;
220 ps->server_tx_fifo = tx_fifo;
221 ps->vpp_server_handle = stream_session_handle (s);
223 proxy_index = ps - bpm->sessions;
225 hash_set (bpm->proxy_session_by_server_handle, ps->vpp_server_handle,
228 clib_spinlock_unlock_if_init (&bpm->sessions_lock);
230 a->uri = "tcp://6.0.2.2/23";
231 a->api_context = proxy_index;
232 a->app_index = bpm->active_open_app_index;
234 vnet_connect_uri (a);
240 static session_cb_vft_t builtin_session_cb_vft = {
241 .session_accept_callback = server_accept_callback,
242 .session_disconnect_callback = server_disconnect_callback,
243 .session_connected_callback = server_connected_callback,
244 .add_segment_callback = server_add_segment_callback,
245 .redirect_connect_callback = server_redirect_connect_callback,
246 .builtin_server_rx_callback = server_rx_callback,
247 .session_reset_callback = server_reset_callback
251 active_open_connected_callback (u32 app_index, u32 opaque,
252 stream_session_t * s, u8 is_fail)
254 builtin_proxy_main_t *bpm = &builtin_proxy_main;
256 u8 thread_index = vlib_get_thread_index ();
257 session_fifo_event_t evt;
261 clib_warning ("connection %d failed!", opaque);
266 * Setup proxy session handle.
268 clib_spinlock_lock_if_init (&bpm->sessions_lock);
270 ps = pool_elt_at_index (bpm->sessions, opaque);
271 ps->vpp_active_open_handle = stream_session_handle (s);
273 s->server_tx_fifo = ps->server_rx_fifo;
274 s->server_rx_fifo = ps->server_tx_fifo;
277 * Reset the active-open tx-fifo master indices so the active-open session
278 * will receive data, etc.
280 s->server_tx_fifo->master_session_index = s->session_index;
281 s->server_tx_fifo->master_thread_index = s->thread_index;
284 * Account for the active-open session's use of the fifos
285 * so they won't disappear until the last session which uses
288 s->server_tx_fifo->refcnt++;
289 s->server_rx_fifo->refcnt++;
291 hash_set (bpm->proxy_session_by_active_open_handle,
292 ps->vpp_active_open_handle, opaque);
294 clib_spinlock_unlock_if_init (&bpm->sessions_lock);
297 * Send event for active open tx fifo
299 if (svm_fifo_set_event (s->server_tx_fifo))
301 evt.fifo = s->server_tx_fifo;
302 evt.event_type = FIFO_EVENT_APP_TX;
303 if (unix_shared_memory_queue_add
304 (bpm->active_open_event_queue[thread_index], (u8 *) & evt,
305 0 /* do wait for mutex */ ))
306 clib_warning ("failed to enqueue tx evt");
313 active_open_reset_callback (stream_session_t * s)
315 delete_proxy_session (s, 1 /* is_active_open */ );
319 active_open_create_callback (stream_session_t * s)
325 active_open_disconnect_callback (stream_session_t * s)
327 delete_proxy_session (s, 1 /* is_active_open */ );
331 active_open_rx_callback (stream_session_t * s)
333 builtin_proxy_main_t *bpm = &builtin_proxy_main;
334 session_fifo_event_t evt;
335 svm_fifo_t *server_rx_fifo;
336 u32 thread_index = vlib_get_thread_index ();
338 server_rx_fifo = s->server_rx_fifo;
341 * Send event for server tx fifo
343 if (svm_fifo_set_event (server_rx_fifo))
345 evt.fifo = server_rx_fifo;
346 evt.event_type = FIFO_EVENT_APP_TX;
347 if (unix_shared_memory_queue_add
348 (bpm->server_event_queue[thread_index], (u8 *) & evt,
349 0 /* do wait for mutex */ ))
350 clib_warning ("failed to enqueue server rx evt");
357 static session_cb_vft_t builtin_clients = {
358 .session_reset_callback = active_open_reset_callback,
359 .session_connected_callback = active_open_connected_callback,
360 .session_accept_callback = active_open_create_callback,
361 .session_disconnect_callback = active_open_disconnect_callback,
362 .builtin_server_rx_callback = active_open_rx_callback
368 create_api_loopbacks (vlib_main_t * vm)
370 builtin_proxy_main_t *bpm = &builtin_proxy_main;
371 api_main_t *am = &api_main;
372 vl_shmem_hdr_t *shmem_hdr;
374 shmem_hdr = am->shmem_hdr;
375 bpm->vl_input_queue = shmem_hdr->vl_input_queue;
376 bpm->server_client_index =
377 vl_api_memclnt_create_internal ("proxy_server", bpm->vl_input_queue);
378 bpm->active_open_client_index =
379 vl_api_memclnt_create_internal ("proxy_active_open", bpm->vl_input_queue);
385 builtin_proxy_main_t *bpm = &builtin_proxy_main;
386 u8 segment_name[128];
387 u64 options[SESSION_OPTIONS_N_OPTIONS];
388 vnet_app_attach_args_t _a, *a = &_a;
390 memset (a, 0, sizeof (*a));
391 memset (options, 0, sizeof (options));
393 a->api_client_index = bpm->server_client_index;
394 a->session_cb_vft = &builtin_session_cb_vft;
395 a->options = options;
396 a->options[SESSION_OPTIONS_SEGMENT_SIZE] = 512 << 20;
397 a->options[SESSION_OPTIONS_RX_FIFO_SIZE] = bpm->fifo_size;
398 a->options[SESSION_OPTIONS_TX_FIFO_SIZE] = bpm->fifo_size;
399 a->options[APP_OPTIONS_PRIVATE_SEGMENT_COUNT] = bpm->private_segment_count;
400 a->options[APP_OPTIONS_PRIVATE_SEGMENT_SIZE] = bpm->private_segment_size;
401 a->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] =
402 bpm->prealloc_fifos ? bpm->prealloc_fifos : 1;
404 a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_BUILTIN_APP;
406 a->segment_name = segment_name;
407 a->segment_name_length = ARRAY_LEN (segment_name);
409 if (vnet_application_attach (a))
411 clib_warning ("failed to attach server");
414 bpm->server_app_index = a->app_index;
420 active_open_attach (void)
422 builtin_proxy_main_t *bpm = &builtin_proxy_main;
423 vnet_app_attach_args_t _a, *a = &_a;
424 u8 segment_name[128];
425 u32 segment_name_length;
428 segment_name_length = ARRAY_LEN (segment_name);
430 memset (a, 0, sizeof (*a));
431 memset (options, 0, sizeof (options));
433 a->api_client_index = bpm->active_open_client_index;
434 a->segment_name = segment_name;
435 a->segment_name_length = segment_name_length;
436 a->session_cb_vft = &builtin_clients;
438 options[SESSION_OPTIONS_ACCEPT_COOKIE] = 0x12345678;
439 options[SESSION_OPTIONS_SEGMENT_SIZE] = 512 << 20;
440 options[SESSION_OPTIONS_RX_FIFO_SIZE] = bpm->fifo_size;
441 options[SESSION_OPTIONS_TX_FIFO_SIZE] = bpm->fifo_size;
442 options[APP_OPTIONS_PRIVATE_SEGMENT_COUNT] = bpm->private_segment_count;
443 options[APP_OPTIONS_PRIVATE_SEGMENT_SIZE] = bpm->private_segment_size;
444 options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] =
445 bpm->prealloc_fifos ? bpm->prealloc_fifos : 1;
447 options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_BUILTIN_APP
448 | APP_OPTIONS_FLAGS_IS_PROXY;
450 a->options = options;
452 if (vnet_application_attach (a))
455 bpm->active_open_app_index = a->app_index;
463 builtin_proxy_main_t *bpm = &builtin_proxy_main;
464 vnet_bind_args_t _a, *a = &_a;
465 memset (a, 0, sizeof (*a));
466 a->app_index = bpm->server_app_index;
467 a->uri = "tcp://0.0.0.0/23";
468 return vnet_bind_uri (a);
472 server_create (vlib_main_t * vm)
474 builtin_proxy_main_t *bpm = &builtin_proxy_main;
475 vlib_thread_main_t *vtm = vlib_get_thread_main ();
479 if (bpm->server_client_index == (u32) ~ 0)
480 create_api_loopbacks (vm);
482 num_threads = 1 /* main thread */ + vtm->n_threads;
483 vec_validate (builtin_proxy_main.server_event_queue, num_threads - 1);
484 vec_validate (builtin_proxy_main.active_open_event_queue, num_threads - 1);
485 vec_validate (bpm->rx_buf, num_threads - 1);
487 for (i = 0; i < num_threads; i++)
488 vec_validate (bpm->rx_buf[i], bpm->rcv_buffer_size);
490 if (server_attach ())
492 clib_warning ("failed to attach server app");
495 if (server_listen ())
497 clib_warning ("failed to start listening");
500 if (active_open_attach ())
502 clib_warning ("failed to attach active open app");
506 for (i = 0; i < num_threads; i++)
508 bpm->active_open_event_queue[i] =
509 session_manager_get_vpp_event_queue (i);
511 ASSERT (bpm->active_open_event_queue[i]);
513 bpm->server_event_queue[i] = session_manager_get_vpp_event_queue (i);
519 static clib_error_t *
520 proxy_server_create_command_fn (vlib_main_t * vm, unformat_input_t * input,
521 vlib_cli_command_t * cmd)
523 builtin_proxy_main_t *bpm = &builtin_proxy_main;
527 bpm->fifo_size = 64 << 10;
528 bpm->rcv_buffer_size = 1024;
529 bpm->prealloc_fifos = 0;
530 bpm->private_segment_count = 0;
531 bpm->private_segment_size = 0;
533 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
535 if (unformat (input, "fifo-size %d", &bpm->fifo_size))
536 bpm->fifo_size <<= 10;
537 else if (unformat (input, "rcv-buf-size %d", &bpm->rcv_buffer_size))
539 else if (unformat (input, "prealloc-fifos %d", &bpm->prealloc_fifos))
541 else if (unformat (input, "private-segment-count %d",
542 &bpm->private_segment_count))
544 else if (unformat (input, "private-segment-size %U",
545 unformat_memory_size, &tmp))
547 if (tmp >= 0x100000000ULL)
548 return clib_error_return
549 (0, "private segment size %lld (%llu) too large", tmp, tmp);
550 bpm->private_segment_size = tmp;
553 return clib_error_return (0, "unknown input `%U'",
554 format_unformat_error, input);
557 vnet_session_enable_disable (vm, 1 /* turn on TCP, etc. */ );
559 rv = server_create (vm);
565 return clib_error_return (0, "server_create returned %d", rv);
572 VLIB_CLI_COMMAND (server_create_command, static) =
574 .path = "test proxy server",
575 .short_help = "test proxy server",
576 .function = proxy_server_create_command_fn,
581 builtin_tcp_proxy_main_init (vlib_main_t * vm)
583 builtin_proxy_main_t *bpm = &builtin_proxy_main;
584 bpm->server_client_index = ~0;
585 bpm->active_open_client_index = ~0;
586 bpm->proxy_session_by_active_open_handle = hash_create (0, sizeof (uword));
587 bpm->proxy_session_by_server_handle = hash_create (0, sizeof (uword));
592 VLIB_INIT_FUNCTION (builtin_tcp_proxy_main_init);
595 * fd.io coding-style-patch-verification: ON
598 * eval: (c-set-style "gnu")