blob: f7896a5b0ae0c1ec8155fc49899abbfb951d9b54 [file] [log] [blame]
Dave Barach52851e62017-08-07 09:35:25 -04001/*
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:
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>
Florin Coras4399c2e2018-01-25 06:34:42 -080020#include <vnet/session-apps/proxy.h>
Dave Barach52851e62017-08-07 09:35:25 -040021
Florin Coras4399c2e2018-01-25 06:34:42 -080022proxy_main_t proxy_main;
Dave Barach52851e62017-08-07 09:35:25 -040023
Florin Corascd330c82018-05-09 17:33:00 -070024typedef struct
25{
26 char uri[128];
27 u32 app_index;
28 u32 api_context;
29} proxy_connect_args_t;
30
31static void
32proxy_cb_fn (void *data, u32 data_len)
33{
34 proxy_connect_args_t *pa = (proxy_connect_args_t *) data;
35 vnet_connect_args_t a;
36
37 a.api_context = pa->api_context;
38 a.app_index = pa->app_index;
39 a.uri = pa->uri;
40 vnet_connect_uri (&a);
41}
42
43static void
44proxy_call_main_thread (vnet_connect_args_t * a)
45{
46 if (vlib_get_thread_index () == 0)
47 {
48 vnet_connect_uri (a);
49 }
50 else
51 {
52 proxy_connect_args_t args;
53 args.api_context = a->api_context;
54 args.app_index = a->app_index;
55 clib_memcpy (args.uri, a->uri, vec_len (a->uri));
56 vl_api_rpc_call_main_thread (proxy_cb_fn, (u8 *) & args, sizeof (args));
57 }
58}
59
Dave Barach52851e62017-08-07 09:35:25 -040060static void
61delete_proxy_session (stream_session_t * s, int is_active_open)
62{
Florin Coras4399c2e2018-01-25 06:34:42 -080063 proxy_main_t *pm = &proxy_main;
Dave Barach52851e62017-08-07 09:35:25 -040064 proxy_session_t *ps = 0;
65 vnet_disconnect_args_t _a, *a = &_a;
66 stream_session_t *active_open_session = 0;
67 stream_session_t *server_session = 0;
68 uword *p;
69 u64 handle;
70
Florin Coras3cbc04b2017-10-02 00:18:51 -070071 handle = session_handle (s);
Dave Barach52851e62017-08-07 09:35:25 -040072
Florin Coras4399c2e2018-01-25 06:34:42 -080073 clib_spinlock_lock_if_init (&pm->sessions_lock);
Dave Barach52851e62017-08-07 09:35:25 -040074 if (is_active_open)
75 {
76 active_open_session = s;
77
Florin Coras4399c2e2018-01-25 06:34:42 -080078 p = hash_get (pm->proxy_session_by_active_open_handle, handle);
Dave Barach52851e62017-08-07 09:35:25 -040079 if (p == 0)
80 {
81 clib_warning ("proxy session for %s handle %lld (%llx) AWOL",
82 is_active_open ? "active open" : "server",
83 handle, handle);
84 }
Florin Corascd330c82018-05-09 17:33:00 -070085 else if (!pool_is_free_index (pm->sessions, p[0]))
Dave Barach52851e62017-08-07 09:35:25 -040086 {
Florin Coras4399c2e2018-01-25 06:34:42 -080087 ps = pool_elt_at_index (pm->sessions, p[0]);
Dave Barach52851e62017-08-07 09:35:25 -040088 if (ps->vpp_server_handle != ~0)
Florin Corascea194d2017-10-02 00:18:51 -070089 server_session = session_get_from_handle (ps->vpp_server_handle);
Dave Barach52851e62017-08-07 09:35:25 -040090 else
91 server_session = 0;
92 }
93 }
94 else
95 {
96 server_session = s;
97
Florin Coras4399c2e2018-01-25 06:34:42 -080098 p = hash_get (pm->proxy_session_by_server_handle, handle);
Dave Barach52851e62017-08-07 09:35:25 -040099 if (p == 0)
100 {
101 clib_warning ("proxy session for %s handle %lld (%llx) AWOL",
102 is_active_open ? "active open" : "server",
103 handle, handle);
104 }
Florin Corascd330c82018-05-09 17:33:00 -0700105 else if (!pool_is_free_index (pm->sessions, p[0]))
Dave Barach52851e62017-08-07 09:35:25 -0400106 {
Florin Coras4399c2e2018-01-25 06:34:42 -0800107 ps = pool_elt_at_index (pm->sessions, p[0]);
Florin Coras70816f02018-05-14 01:44:01 -0700108 if (ps->vpp_active_open_handle != ~0)
Florin Corascea194d2017-10-02 00:18:51 -0700109 active_open_session = session_get_from_handle
Florin Coras70816f02018-05-14 01:44:01 -0700110 (ps->vpp_active_open_handle);
Dave Barach52851e62017-08-07 09:35:25 -0400111 else
112 active_open_session = 0;
113 }
114 }
115
116 if (ps)
117 {
118 if (CLIB_DEBUG > 0)
Dave Barachb7b92992018-10-17 10:38:51 -0400119 clib_memset (ps, 0xFE, sizeof (*ps));
Florin Coras4399c2e2018-01-25 06:34:42 -0800120 pool_put (pm->sessions, ps);
Dave Barach52851e62017-08-07 09:35:25 -0400121 }
122
Florin Coras4399c2e2018-01-25 06:34:42 -0800123 clib_spinlock_unlock_if_init (&pm->sessions_lock);
Dave Barach52851e62017-08-07 09:35:25 -0400124
125 if (active_open_session)
126 {
Florin Coras3cbc04b2017-10-02 00:18:51 -0700127 a->handle = session_handle (active_open_session);
Florin Coras4399c2e2018-01-25 06:34:42 -0800128 a->app_index = pm->active_open_app_index;
129 hash_unset (pm->proxy_session_by_active_open_handle,
Florin Coras3cbc04b2017-10-02 00:18:51 -0700130 session_handle (active_open_session));
Dave Barach52851e62017-08-07 09:35:25 -0400131 vnet_disconnect_session (a);
132 }
133
134 if (server_session)
135 {
Florin Coras3cbc04b2017-10-02 00:18:51 -0700136 a->handle = session_handle (server_session);
Florin Coras4399c2e2018-01-25 06:34:42 -0800137 a->app_index = pm->server_app_index;
138 hash_unset (pm->proxy_session_by_server_handle,
Florin Coras3cbc04b2017-10-02 00:18:51 -0700139 session_handle (server_session));
Dave Barach52851e62017-08-07 09:35:25 -0400140 vnet_disconnect_session (a);
141 }
142}
143
144static int
Florin Coras4399c2e2018-01-25 06:34:42 -0800145proxy_accept_callback (stream_session_t * s)
Dave Barach52851e62017-08-07 09:35:25 -0400146{
Florin Coras4399c2e2018-01-25 06:34:42 -0800147 proxy_main_t *pm = &proxy_main;
Dave Barach52851e62017-08-07 09:35:25 -0400148
149 s->session_state = SESSION_STATE_READY;
150
Florin Coras4399c2e2018-01-25 06:34:42 -0800151 clib_spinlock_lock_if_init (&pm->sessions_lock);
Dave Barach52851e62017-08-07 09:35:25 -0400152
153 return 0;
154}
155
156static void
Florin Coras4399c2e2018-01-25 06:34:42 -0800157proxy_disconnect_callback (stream_session_t * s)
Dave Barach52851e62017-08-07 09:35:25 -0400158{
159 delete_proxy_session (s, 0 /* is_active_open */ );
160}
161
162static void
Florin Coras4399c2e2018-01-25 06:34:42 -0800163proxy_reset_callback (stream_session_t * s)
Dave Barach52851e62017-08-07 09:35:25 -0400164{
165 clib_warning ("Reset session %U", format_stream_session, s, 2);
166 delete_proxy_session (s, 0 /* is_active_open */ );
167}
168
169static int
Florin Coras4399c2e2018-01-25 06:34:42 -0800170proxy_connected_callback (u32 app_index, u32 api_context,
171 stream_session_t * s, u8 is_fail)
Dave Barach52851e62017-08-07 09:35:25 -0400172{
173 clib_warning ("called...");
174 return -1;
175}
176
177static int
Florin Coras4399c2e2018-01-25 06:34:42 -0800178proxy_add_segment_callback (u32 client_index, const ssvm_private_t * sp)
Dave Barach52851e62017-08-07 09:35:25 -0400179{
180 clib_warning ("called...");
181 return -1;
182}
183
184static int
Florin Coras4399c2e2018-01-25 06:34:42 -0800185proxy_rx_callback (stream_session_t * s)
Dave Barach52851e62017-08-07 09:35:25 -0400186{
187 u32 max_dequeue;
188 int actual_transfer __attribute__ ((unused));
189 svm_fifo_t *tx_fifo, *rx_fifo;
Florin Coras4399c2e2018-01-25 06:34:42 -0800190 proxy_main_t *pm = &proxy_main;
Dave Barach52851e62017-08-07 09:35:25 -0400191 u32 thread_index = vlib_get_thread_index ();
192 vnet_connect_args_t _a, *a = &_a;
193 proxy_session_t *ps;
194 int proxy_index;
195 uword *p;
196 svm_fifo_t *active_open_tx_fifo;
Dave Barach52851e62017-08-07 09:35:25 -0400197
198 ASSERT (s->thread_index == thread_index);
199
Florin Coras4399c2e2018-01-25 06:34:42 -0800200 clib_spinlock_lock_if_init (&pm->sessions_lock);
201 p = hash_get (pm->proxy_session_by_server_handle, session_handle (s));
Dave Barach52851e62017-08-07 09:35:25 -0400202
203 if (PREDICT_TRUE (p != 0))
204 {
Florin Coras4399c2e2018-01-25 06:34:42 -0800205 clib_spinlock_unlock_if_init (&pm->sessions_lock);
Dave Barach52851e62017-08-07 09:35:25 -0400206 active_open_tx_fifo = s->server_rx_fifo;
207
208 /*
209 * Send event for active open tx fifo
210 */
211 if (svm_fifo_set_event (active_open_tx_fifo))
212 {
Florin Corascd330c82018-05-09 17:33:00 -0700213 u32 ao_thread_index = active_open_tx_fifo->master_thread_index;
Florin Coras3c2fed52018-07-04 04:15:05 -0700214 if (session_send_io_evt_to_thread_custom (active_open_tx_fifo,
215 ao_thread_index,
216 FIFO_EVENT_APP_TX))
Dave Barach52851e62017-08-07 09:35:25 -0400217 clib_warning ("failed to enqueue tx evt");
218 }
219 }
220 else
221 {
222 rx_fifo = s->server_rx_fifo;
223 tx_fifo = s->server_tx_fifo;
224
225 ASSERT (rx_fifo->master_thread_index == thread_index);
226 ASSERT (tx_fifo->master_thread_index == thread_index);
227
228 max_dequeue = svm_fifo_max_dequeue (s->server_rx_fifo);
229
230 if (PREDICT_FALSE (max_dequeue == 0))
231 return 0;
232
233 actual_transfer = svm_fifo_peek (rx_fifo, 0 /* relative_offset */ ,
Florin Coras4399c2e2018-01-25 06:34:42 -0800234 max_dequeue, pm->rx_buf[thread_index]);
Dave Barach52851e62017-08-07 09:35:25 -0400235
236 /* $$$ your message in this space: parse url, etc. */
237
Dave Barachb7b92992018-10-17 10:38:51 -0400238 clib_memset (a, 0, sizeof (*a));
Dave Barach52851e62017-08-07 09:35:25 -0400239
Florin Coras4399c2e2018-01-25 06:34:42 -0800240 clib_spinlock_lock_if_init (&pm->sessions_lock);
241 pool_get (pm->sessions, ps);
Dave Barachb7b92992018-10-17 10:38:51 -0400242 clib_memset (ps, 0, sizeof (*ps));
Dave Barach52851e62017-08-07 09:35:25 -0400243 ps->server_rx_fifo = rx_fifo;
244 ps->server_tx_fifo = tx_fifo;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700245 ps->vpp_server_handle = session_handle (s);
Dave Barach52851e62017-08-07 09:35:25 -0400246
Florin Coras4399c2e2018-01-25 06:34:42 -0800247 proxy_index = ps - pm->sessions;
Dave Barach52851e62017-08-07 09:35:25 -0400248
Florin Coras4399c2e2018-01-25 06:34:42 -0800249 hash_set (pm->proxy_session_by_server_handle, ps->vpp_server_handle,
Dave Barach52851e62017-08-07 09:35:25 -0400250 proxy_index);
251
Florin Coras4399c2e2018-01-25 06:34:42 -0800252 clib_spinlock_unlock_if_init (&pm->sessions_lock);
Dave Barach52851e62017-08-07 09:35:25 -0400253
Florin Coras4399c2e2018-01-25 06:34:42 -0800254 a->uri = (char *) pm->client_uri;
Dave Barach52851e62017-08-07 09:35:25 -0400255 a->api_context = proxy_index;
Florin Coras4399c2e2018-01-25 06:34:42 -0800256 a->app_index = pm->active_open_app_index;
Florin Corascd330c82018-05-09 17:33:00 -0700257 proxy_call_main_thread (a);
Dave Barach52851e62017-08-07 09:35:25 -0400258 }
259
260 return 0;
261}
262
Florin Coras4399c2e2018-01-25 06:34:42 -0800263static session_cb_vft_t proxy_session_cb_vft = {
264 .session_accept_callback = proxy_accept_callback,
265 .session_disconnect_callback = proxy_disconnect_callback,
266 .session_connected_callback = proxy_connected_callback,
267 .add_segment_callback = proxy_add_segment_callback,
Florin Coras371ca502018-02-21 12:07:41 -0800268 .builtin_app_rx_callback = proxy_rx_callback,
Florin Coras4399c2e2018-01-25 06:34:42 -0800269 .session_reset_callback = proxy_reset_callback
Dave Barach52851e62017-08-07 09:35:25 -0400270};
271
272static int
273active_open_connected_callback (u32 app_index, u32 opaque,
274 stream_session_t * s, u8 is_fail)
275{
Florin Coras4399c2e2018-01-25 06:34:42 -0800276 proxy_main_t *pm = &proxy_main;
Dave Barach52851e62017-08-07 09:35:25 -0400277 proxy_session_t *ps;
278 u8 thread_index = vlib_get_thread_index ();
Dave Barach52851e62017-08-07 09:35:25 -0400279
280 if (is_fail)
281 {
282 clib_warning ("connection %d failed!", opaque);
283 return 0;
284 }
285
286 /*
287 * Setup proxy session handle.
288 */
Florin Coras4399c2e2018-01-25 06:34:42 -0800289 clib_spinlock_lock_if_init (&pm->sessions_lock);
Dave Barach52851e62017-08-07 09:35:25 -0400290
Florin Coras4399c2e2018-01-25 06:34:42 -0800291 ps = pool_elt_at_index (pm->sessions, opaque);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700292 ps->vpp_active_open_handle = session_handle (s);
Dave Barach52851e62017-08-07 09:35:25 -0400293
294 s->server_tx_fifo = ps->server_rx_fifo;
295 s->server_rx_fifo = ps->server_tx_fifo;
296
297 /*
298 * Reset the active-open tx-fifo master indices so the active-open session
299 * will receive data, etc.
300 */
301 s->server_tx_fifo->master_session_index = s->session_index;
302 s->server_tx_fifo->master_thread_index = s->thread_index;
303
304 /*
305 * Account for the active-open session's use of the fifos
306 * so they won't disappear until the last session which uses
307 * them disappears
308 */
309 s->server_tx_fifo->refcnt++;
310 s->server_rx_fifo->refcnt++;
311
Florin Coras4399c2e2018-01-25 06:34:42 -0800312 hash_set (pm->proxy_session_by_active_open_handle,
Dave Barach52851e62017-08-07 09:35:25 -0400313 ps->vpp_active_open_handle, opaque);
314
Florin Coras4399c2e2018-01-25 06:34:42 -0800315 clib_spinlock_unlock_if_init (&pm->sessions_lock);
Dave Barach52851e62017-08-07 09:35:25 -0400316
317 /*
318 * Send event for active open tx fifo
319 */
Florin Coras3c2fed52018-07-04 04:15:05 -0700320 ASSERT (s->thread_index == thread_index);
Dave Barach52851e62017-08-07 09:35:25 -0400321 if (svm_fifo_set_event (s->server_tx_fifo))
Florin Coras3c2fed52018-07-04 04:15:05 -0700322 session_send_io_evt_to_thread (s->server_tx_fifo, FIFO_EVENT_APP_TX);
Dave Barach52851e62017-08-07 09:35:25 -0400323
324 return 0;
325}
326
327static void
328active_open_reset_callback (stream_session_t * s)
329{
330 delete_proxy_session (s, 1 /* is_active_open */ );
331}
332
333static int
334active_open_create_callback (stream_session_t * s)
335{
336 return 0;
337}
338
339static void
340active_open_disconnect_callback (stream_session_t * s)
341{
342 delete_proxy_session (s, 1 /* is_active_open */ );
343}
344
345static int
346active_open_rx_callback (stream_session_t * s)
347{
Florin Corascd330c82018-05-09 17:33:00 -0700348 svm_fifo_t *proxy_tx_fifo;
Dave Barach52851e62017-08-07 09:35:25 -0400349
Florin Corascd330c82018-05-09 17:33:00 -0700350 proxy_tx_fifo = s->server_rx_fifo;
Dave Barach52851e62017-08-07 09:35:25 -0400351
352 /*
353 * Send event for server tx fifo
354 */
Florin Corascd330c82018-05-09 17:33:00 -0700355 if (svm_fifo_set_event (proxy_tx_fifo))
Dave Barach52851e62017-08-07 09:35:25 -0400356 {
Florin Coras3c2fed52018-07-04 04:15:05 -0700357 u8 thread_index = proxy_tx_fifo->master_thread_index;
358 return session_send_io_evt_to_thread_custom (proxy_tx_fifo,
359 thread_index,
360 FIFO_EVENT_APP_TX);
Dave Barach52851e62017-08-07 09:35:25 -0400361 }
362
363 return 0;
364}
365
366/* *INDENT-OFF* */
Florin Coras4399c2e2018-01-25 06:34:42 -0800367static session_cb_vft_t active_open_clients = {
Dave Barach52851e62017-08-07 09:35:25 -0400368 .session_reset_callback = active_open_reset_callback,
369 .session_connected_callback = active_open_connected_callback,
370 .session_accept_callback = active_open_create_callback,
371 .session_disconnect_callback = active_open_disconnect_callback,
Florin Coras371ca502018-02-21 12:07:41 -0800372 .builtin_app_rx_callback = active_open_rx_callback
Dave Barach52851e62017-08-07 09:35:25 -0400373};
374/* *INDENT-ON* */
375
376
377static void
378create_api_loopbacks (vlib_main_t * vm)
379{
Florin Coras4399c2e2018-01-25 06:34:42 -0800380 proxy_main_t *pm = &proxy_main;
Dave Barach52851e62017-08-07 09:35:25 -0400381 api_main_t *am = &api_main;
382 vl_shmem_hdr_t *shmem_hdr;
383
384 shmem_hdr = am->shmem_hdr;
Florin Coras4399c2e2018-01-25 06:34:42 -0800385 pm->vl_input_queue = shmem_hdr->vl_input_queue;
386 pm->server_client_index =
387 vl_api_memclnt_create_internal ("proxy_server", pm->vl_input_queue);
388 pm->active_open_client_index =
389 vl_api_memclnt_create_internal ("proxy_active_open", pm->vl_input_queue);
Dave Barach52851e62017-08-07 09:35:25 -0400390}
391
392static int
Florin Coras4399c2e2018-01-25 06:34:42 -0800393proxy_server_attach ()
Dave Barach52851e62017-08-07 09:35:25 -0400394{
Florin Coras4399c2e2018-01-25 06:34:42 -0800395 proxy_main_t *pm = &proxy_main;
Florin Corasff6e7692017-12-11 04:59:01 -0800396 u64 options[APP_OPTIONS_N_OPTIONS];
Dave Barach52851e62017-08-07 09:35:25 -0400397 vnet_app_attach_args_t _a, *a = &_a;
Florin Corasff6e7692017-12-11 04:59:01 -0800398 u32 segment_size = 512 << 20;
Dave Barach52851e62017-08-07 09:35:25 -0400399
Dave Barachb7b92992018-10-17 10:38:51 -0400400 clib_memset (a, 0, sizeof (*a));
401 clib_memset (options, 0, sizeof (options));
Dave Barach52851e62017-08-07 09:35:25 -0400402
Florin Coras4399c2e2018-01-25 06:34:42 -0800403 if (pm->private_segment_size)
404 segment_size = pm->private_segment_size;
405 a->api_client_index = pm->server_client_index;
406 a->session_cb_vft = &proxy_session_cb_vft;
Dave Barach52851e62017-08-07 09:35:25 -0400407 a->options = options;
Florin Corasff6e7692017-12-11 04:59:01 -0800408 a->options[APP_OPTIONS_SEGMENT_SIZE] = segment_size;
Florin Coras4399c2e2018-01-25 06:34:42 -0800409 a->options[APP_OPTIONS_RX_FIFO_SIZE] = pm->fifo_size;
410 a->options[APP_OPTIONS_TX_FIFO_SIZE] = pm->fifo_size;
411 a->options[APP_OPTIONS_PRIVATE_SEGMENT_COUNT] = pm->private_segment_count;
Dave Barach52851e62017-08-07 09:35:25 -0400412 a->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] =
Florin Coras25579b42018-06-06 17:55:02 -0700413 pm->prealloc_fifos ? pm->prealloc_fifos : 0;
Dave Barach52851e62017-08-07 09:35:25 -0400414
Florin Coras7999e832017-10-31 01:51:04 -0700415 a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
Dave Barach52851e62017-08-07 09:35:25 -0400416
Dave Barach52851e62017-08-07 09:35:25 -0400417 if (vnet_application_attach (a))
418 {
419 clib_warning ("failed to attach server");
420 return -1;
421 }
Florin Coras4399c2e2018-01-25 06:34:42 -0800422 pm->server_app_index = a->app_index;
Dave Barach52851e62017-08-07 09:35:25 -0400423
424 return 0;
425}
426
427static int
428active_open_attach (void)
429{
Florin Coras4399c2e2018-01-25 06:34:42 -0800430 proxy_main_t *pm = &proxy_main;
Dave Barach52851e62017-08-07 09:35:25 -0400431 vnet_app_attach_args_t _a, *a = &_a;
Dave Barach52851e62017-08-07 09:35:25 -0400432 u64 options[16];
433
Dave Barachb7b92992018-10-17 10:38:51 -0400434 clib_memset (a, 0, sizeof (*a));
435 clib_memset (options, 0, sizeof (options));
Dave Barach52851e62017-08-07 09:35:25 -0400436
Florin Coras4399c2e2018-01-25 06:34:42 -0800437 a->api_client_index = pm->active_open_client_index;
438 a->session_cb_vft = &active_open_clients;
Dave Barach52851e62017-08-07 09:35:25 -0400439
Florin Corasff6e7692017-12-11 04:59:01 -0800440 options[APP_OPTIONS_ACCEPT_COOKIE] = 0x12345678;
441 options[APP_OPTIONS_SEGMENT_SIZE] = 512 << 20;
Florin Coras4399c2e2018-01-25 06:34:42 -0800442 options[APP_OPTIONS_RX_FIFO_SIZE] = pm->fifo_size;
443 options[APP_OPTIONS_TX_FIFO_SIZE] = pm->fifo_size;
444 options[APP_OPTIONS_PRIVATE_SEGMENT_COUNT] = pm->private_segment_count;
Dave Barach52851e62017-08-07 09:35:25 -0400445 options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] =
Florin Coras25579b42018-06-06 17:55:02 -0700446 pm->prealloc_fifos ? pm->prealloc_fifos : 0;
Dave Barach52851e62017-08-07 09:35:25 -0400447
Florin Coras7999e832017-10-31 01:51:04 -0700448 options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN
Dave Barach52851e62017-08-07 09:35:25 -0400449 | APP_OPTIONS_FLAGS_IS_PROXY;
450
451 a->options = options;
452
453 if (vnet_application_attach (a))
454 return -1;
455
Florin Coras4399c2e2018-01-25 06:34:42 -0800456 pm->active_open_app_index = a->app_index;
Dave Barach52851e62017-08-07 09:35:25 -0400457
458 return 0;
459}
460
461static int
Florin Coras4399c2e2018-01-25 06:34:42 -0800462proxy_server_listen ()
Dave Barach52851e62017-08-07 09:35:25 -0400463{
Florin Coras4399c2e2018-01-25 06:34:42 -0800464 proxy_main_t *pm = &proxy_main;
Dave Barach52851e62017-08-07 09:35:25 -0400465 vnet_bind_args_t _a, *a = &_a;
Dave Barachb7b92992018-10-17 10:38:51 -0400466 clib_memset (a, 0, sizeof (*a));
Florin Coras4399c2e2018-01-25 06:34:42 -0800467 a->app_index = pm->server_app_index;
468 a->uri = (char *) pm->server_uri;
Dave Barach52851e62017-08-07 09:35:25 -0400469 return vnet_bind_uri (a);
470}
471
472static int
Florin Coras4399c2e2018-01-25 06:34:42 -0800473proxy_server_create (vlib_main_t * vm)
Dave Barach52851e62017-08-07 09:35:25 -0400474{
Florin Coras4399c2e2018-01-25 06:34:42 -0800475 proxy_main_t *pm = &proxy_main;
Dave Barach52851e62017-08-07 09:35:25 -0400476 vlib_thread_main_t *vtm = vlib_get_thread_main ();
477 u32 num_threads;
478 int i;
479
Florin Coras4399c2e2018-01-25 06:34:42 -0800480 if (pm->server_client_index == (u32) ~ 0)
Dave Barach52851e62017-08-07 09:35:25 -0400481 create_api_loopbacks (vm);
482
483 num_threads = 1 /* main thread */ + vtm->n_threads;
Florin Coras4399c2e2018-01-25 06:34:42 -0800484 vec_validate (proxy_main.server_event_queue, num_threads - 1);
485 vec_validate (proxy_main.active_open_event_queue, num_threads - 1);
486 vec_validate (pm->rx_buf, num_threads - 1);
Dave Barach52851e62017-08-07 09:35:25 -0400487
488 for (i = 0; i < num_threads; i++)
Florin Coras4399c2e2018-01-25 06:34:42 -0800489 vec_validate (pm->rx_buf[i], pm->rcv_buffer_size);
Dave Barach52851e62017-08-07 09:35:25 -0400490
Florin Coras4399c2e2018-01-25 06:34:42 -0800491 if (proxy_server_attach ())
Dave Barach52851e62017-08-07 09:35:25 -0400492 {
493 clib_warning ("failed to attach server app");
494 return -1;
495 }
Florin Coras4399c2e2018-01-25 06:34:42 -0800496 if (proxy_server_listen ())
Dave Barach52851e62017-08-07 09:35:25 -0400497 {
498 clib_warning ("failed to start listening");
499 return -1;
500 }
501 if (active_open_attach ())
502 {
503 clib_warning ("failed to attach active open app");
504 return -1;
505 }
506
507 for (i = 0; i < num_threads; i++)
508 {
Florin Coras4399c2e2018-01-25 06:34:42 -0800509 pm->active_open_event_queue[i] =
Dave Barach52851e62017-08-07 09:35:25 -0400510 session_manager_get_vpp_event_queue (i);
511
Florin Coras4399c2e2018-01-25 06:34:42 -0800512 ASSERT (pm->active_open_event_queue[i]);
Dave Barach52851e62017-08-07 09:35:25 -0400513
Florin Coras4399c2e2018-01-25 06:34:42 -0800514 pm->server_event_queue[i] = session_manager_get_vpp_event_queue (i);
Dave Barach52851e62017-08-07 09:35:25 -0400515 }
516
517 return 0;
518}
519
520static clib_error_t *
521proxy_server_create_command_fn (vlib_main_t * vm, unformat_input_t * input,
522 vlib_cli_command_t * cmd)
523{
Florin Coras4399c2e2018-01-25 06:34:42 -0800524 proxy_main_t *pm = &proxy_main;
525 char *default_server_uri = "tcp://0.0.0.0/23";
526 char *default_client_uri = "tcp://6.0.2.2/23";
Dave Barach52851e62017-08-07 09:35:25 -0400527 int rv;
Dave Barach91f3e742017-09-01 19:12:11 -0400528 u64 tmp;
Dave Barach52851e62017-08-07 09:35:25 -0400529
Florin Coras4399c2e2018-01-25 06:34:42 -0800530 pm->fifo_size = 64 << 10;
531 pm->rcv_buffer_size = 1024;
532 pm->prealloc_fifos = 0;
533 pm->private_segment_count = 0;
534 pm->private_segment_size = 0;
535 pm->server_uri = 0;
Dave Barach52851e62017-08-07 09:35:25 -0400536
537 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
538 {
Florin Coras4399c2e2018-01-25 06:34:42 -0800539 if (unformat (input, "fifo-size %d", &pm->fifo_size))
540 pm->fifo_size <<= 10;
541 else if (unformat (input, "rcv-buf-size %d", &pm->rcv_buffer_size))
Dave Barach52851e62017-08-07 09:35:25 -0400542 ;
Florin Coras4399c2e2018-01-25 06:34:42 -0800543 else if (unformat (input, "prealloc-fifos %d", &pm->prealloc_fifos))
Dave Barach52851e62017-08-07 09:35:25 -0400544 ;
545 else if (unformat (input, "private-segment-count %d",
Florin Coras4399c2e2018-01-25 06:34:42 -0800546 &pm->private_segment_count))
Dave Barach52851e62017-08-07 09:35:25 -0400547 ;
Dave Barach91f3e742017-09-01 19:12:11 -0400548 else if (unformat (input, "private-segment-size %U",
549 unformat_memory_size, &tmp))
550 {
551 if (tmp >= 0x100000000ULL)
552 return clib_error_return
553 (0, "private segment size %lld (%llu) too large", tmp, tmp);
Florin Coras4399c2e2018-01-25 06:34:42 -0800554 pm->private_segment_size = tmp;
Dave Barach91f3e742017-09-01 19:12:11 -0400555 }
Florin Coras4399c2e2018-01-25 06:34:42 -0800556 else if (unformat (input, "server-uri %s", &pm->server_uri))
Florin Corasee7e1f52018-01-11 02:23:35 -0800557 ;
Florin Coras4399c2e2018-01-25 06:34:42 -0800558 else if (unformat (input, "client-uri %s", &pm->client_uri))
Florin Corascd330c82018-05-09 17:33:00 -0700559 pm->client_uri = format (0, "%s%c", pm->client_uri, 0);
Dave Barach52851e62017-08-07 09:35:25 -0400560 else
561 return clib_error_return (0, "unknown input `%U'",
562 format_unformat_error, input);
563 }
564
Florin Coras4399c2e2018-01-25 06:34:42 -0800565 if (!pm->server_uri)
566 {
567 clib_warning ("No server-uri provided, Using default: %s",
568 default_server_uri);
569 pm->server_uri = format (0, "%s%c", default_server_uri, 0);
570 }
571 if (!pm->client_uri)
572 {
573 clib_warning ("No client-uri provided, Using default: %s",
574 default_client_uri);
575 pm->client_uri = format (0, "%s%c", default_client_uri, 0);
576 }
Florin Corasee7e1f52018-01-11 02:23:35 -0800577
Florin Coras4399c2e2018-01-25 06:34:42 -0800578 vnet_session_enable_disable (vm, 1 /* turn on session and transport */ );
Dave Barach52851e62017-08-07 09:35:25 -0400579
Florin Coras4399c2e2018-01-25 06:34:42 -0800580 rv = proxy_server_create (vm);
Dave Barach52851e62017-08-07 09:35:25 -0400581 switch (rv)
582 {
583 case 0:
584 break;
585 default:
586 return clib_error_return (0, "server_create returned %d", rv);
587 }
588
589 return 0;
590}
591
592/* *INDENT-OFF* */
Florin Coras4399c2e2018-01-25 06:34:42 -0800593VLIB_CLI_COMMAND (proxy_create_command, static) =
Dave Barach52851e62017-08-07 09:35:25 -0400594{
595 .path = "test proxy server",
Florin Corasee7e1f52018-01-11 02:23:35 -0800596 .short_help = "test proxy server [server-uri <tcp://ip/port>]"
597 "[client-uri <tcp://ip/port>][fifo-size <nn>][rcv-buf-size <nn>]"
598 "[prealloc-fifos <nn>][private-segment-size <mem>]"
599 "[private-segment-count <nn>]",
Dave Barach52851e62017-08-07 09:35:25 -0400600 .function = proxy_server_create_command_fn,
601};
602/* *INDENT-ON* */
603
604clib_error_t *
Florin Coras4399c2e2018-01-25 06:34:42 -0800605proxy_main_init (vlib_main_t * vm)
Dave Barach52851e62017-08-07 09:35:25 -0400606{
Florin Coras4399c2e2018-01-25 06:34:42 -0800607 proxy_main_t *pm = &proxy_main;
608 pm->server_client_index = ~0;
609 pm->active_open_client_index = ~0;
610 pm->proxy_session_by_active_open_handle = hash_create (0, sizeof (uword));
611 pm->proxy_session_by_server_handle = hash_create (0, sizeof (uword));
Dave Barach52851e62017-08-07 09:35:25 -0400612
613 return 0;
614}
615
Florin Coras4399c2e2018-01-25 06:34:42 -0800616VLIB_INIT_FUNCTION (proxy_main_init);
Dave Barach52851e62017-08-07 09:35:25 -0400617
618/*
619* fd.io coding-style-patch-verification: ON
620*
621* Local Variables:
622* eval: (c-set-style "gnu")
623* End:
624*/