blob: 06c095440bcc7b22f2042bfd3bb7b08b55683288 [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
Florin Coras89666382018-11-09 13:40:02 -080037 memset (&a, 0, sizeof (a));
Florin Corascd330c82018-05-09 17:33:00 -070038 a.api_context = pa->api_context;
39 a.app_index = pa->app_index;
40 a.uri = pa->uri;
41 vnet_connect_uri (&a);
42}
43
44static void
45proxy_call_main_thread (vnet_connect_args_t * a)
46{
47 if (vlib_get_thread_index () == 0)
48 {
49 vnet_connect_uri (a);
50 }
51 else
52 {
53 proxy_connect_args_t args;
54 args.api_context = a->api_context;
55 args.app_index = a->app_index;
56 clib_memcpy (args.uri, a->uri, vec_len (a->uri));
57 vl_api_rpc_call_main_thread (proxy_cb_fn, (u8 *) & args, sizeof (args));
58 }
59}
60
Dave Barach52851e62017-08-07 09:35:25 -040061static void
62delete_proxy_session (stream_session_t * s, int is_active_open)
63{
Florin Coras4399c2e2018-01-25 06:34:42 -080064 proxy_main_t *pm = &proxy_main;
Dave Barach52851e62017-08-07 09:35:25 -040065 proxy_session_t *ps = 0;
66 vnet_disconnect_args_t _a, *a = &_a;
67 stream_session_t *active_open_session = 0;
68 stream_session_t *server_session = 0;
69 uword *p;
70 u64 handle;
71
Florin Coras3cbc04b2017-10-02 00:18:51 -070072 handle = session_handle (s);
Dave Barach52851e62017-08-07 09:35:25 -040073
Florin Coras4399c2e2018-01-25 06:34:42 -080074 clib_spinlock_lock_if_init (&pm->sessions_lock);
Dave Barach52851e62017-08-07 09:35:25 -040075 if (is_active_open)
76 {
77 active_open_session = s;
78
Florin Coras4399c2e2018-01-25 06:34:42 -080079 p = hash_get (pm->proxy_session_by_active_open_handle, handle);
Dave Barach52851e62017-08-07 09:35:25 -040080 if (p == 0)
81 {
82 clib_warning ("proxy session for %s handle %lld (%llx) AWOL",
83 is_active_open ? "active open" : "server",
84 handle, handle);
85 }
Florin Corascd330c82018-05-09 17:33:00 -070086 else if (!pool_is_free_index (pm->sessions, p[0]))
Dave Barach52851e62017-08-07 09:35:25 -040087 {
Florin Coras4399c2e2018-01-25 06:34:42 -080088 ps = pool_elt_at_index (pm->sessions, p[0]);
Dave Barach52851e62017-08-07 09:35:25 -040089 if (ps->vpp_server_handle != ~0)
Florin Corascea194d2017-10-02 00:18:51 -070090 server_session = session_get_from_handle (ps->vpp_server_handle);
Dave Barach52851e62017-08-07 09:35:25 -040091 else
92 server_session = 0;
93 }
94 }
95 else
96 {
97 server_session = s;
98
Florin Coras4399c2e2018-01-25 06:34:42 -080099 p = hash_get (pm->proxy_session_by_server_handle, handle);
Dave Barach52851e62017-08-07 09:35:25 -0400100 if (p == 0)
101 {
102 clib_warning ("proxy session for %s handle %lld (%llx) AWOL",
103 is_active_open ? "active open" : "server",
104 handle, handle);
105 }
Florin Corascd330c82018-05-09 17:33:00 -0700106 else if (!pool_is_free_index (pm->sessions, p[0]))
Dave Barach52851e62017-08-07 09:35:25 -0400107 {
Florin Coras4399c2e2018-01-25 06:34:42 -0800108 ps = pool_elt_at_index (pm->sessions, p[0]);
Florin Coras70816f02018-05-14 01:44:01 -0700109 if (ps->vpp_active_open_handle != ~0)
Florin Corascea194d2017-10-02 00:18:51 -0700110 active_open_session = session_get_from_handle
Florin Coras70816f02018-05-14 01:44:01 -0700111 (ps->vpp_active_open_handle);
Dave Barach52851e62017-08-07 09:35:25 -0400112 else
113 active_open_session = 0;
114 }
115 }
116
117 if (ps)
118 {
119 if (CLIB_DEBUG > 0)
Dave Barachb7b92992018-10-17 10:38:51 -0400120 clib_memset (ps, 0xFE, sizeof (*ps));
Florin Coras4399c2e2018-01-25 06:34:42 -0800121 pool_put (pm->sessions, ps);
Dave Barach52851e62017-08-07 09:35:25 -0400122 }
123
Florin Coras4399c2e2018-01-25 06:34:42 -0800124 clib_spinlock_unlock_if_init (&pm->sessions_lock);
Dave Barach52851e62017-08-07 09:35:25 -0400125
126 if (active_open_session)
127 {
Florin Coras3cbc04b2017-10-02 00:18:51 -0700128 a->handle = session_handle (active_open_session);
Florin Coras4399c2e2018-01-25 06:34:42 -0800129 a->app_index = pm->active_open_app_index;
130 hash_unset (pm->proxy_session_by_active_open_handle,
Florin Coras3cbc04b2017-10-02 00:18:51 -0700131 session_handle (active_open_session));
Dave Barach52851e62017-08-07 09:35:25 -0400132 vnet_disconnect_session (a);
133 }
134
135 if (server_session)
136 {
Florin Coras3cbc04b2017-10-02 00:18:51 -0700137 a->handle = session_handle (server_session);
Florin Coras4399c2e2018-01-25 06:34:42 -0800138 a->app_index = pm->server_app_index;
139 hash_unset (pm->proxy_session_by_server_handle,
Florin Coras3cbc04b2017-10-02 00:18:51 -0700140 session_handle (server_session));
Dave Barach52851e62017-08-07 09:35:25 -0400141 vnet_disconnect_session (a);
142 }
143}
144
145static int
Florin Coras4399c2e2018-01-25 06:34:42 -0800146proxy_accept_callback (stream_session_t * s)
Dave Barach52851e62017-08-07 09:35:25 -0400147{
Florin Coras4399c2e2018-01-25 06:34:42 -0800148 proxy_main_t *pm = &proxy_main;
Dave Barach52851e62017-08-07 09:35:25 -0400149
150 s->session_state = SESSION_STATE_READY;
151
Florin Coras4399c2e2018-01-25 06:34:42 -0800152 clib_spinlock_lock_if_init (&pm->sessions_lock);
Dave Barach52851e62017-08-07 09:35:25 -0400153
154 return 0;
155}
156
157static void
Florin Coras4399c2e2018-01-25 06:34:42 -0800158proxy_disconnect_callback (stream_session_t * s)
Dave Barach52851e62017-08-07 09:35:25 -0400159{
160 delete_proxy_session (s, 0 /* is_active_open */ );
161}
162
163static void
Florin Coras4399c2e2018-01-25 06:34:42 -0800164proxy_reset_callback (stream_session_t * s)
Dave Barach52851e62017-08-07 09:35:25 -0400165{
166 clib_warning ("Reset session %U", format_stream_session, s, 2);
167 delete_proxy_session (s, 0 /* is_active_open */ );
168}
169
170static int
Florin Coras4399c2e2018-01-25 06:34:42 -0800171proxy_connected_callback (u32 app_index, u32 api_context,
172 stream_session_t * s, u8 is_fail)
Dave Barach52851e62017-08-07 09:35:25 -0400173{
174 clib_warning ("called...");
175 return -1;
176}
177
178static int
Florin Corasfa76a762018-11-29 12:40:10 -0800179proxy_add_segment_callback (u32 client_index, u64 segment_handle)
Dave Barach52851e62017-08-07 09:35:25 -0400180{
181 clib_warning ("called...");
182 return -1;
183}
184
185static int
Florin Coras4399c2e2018-01-25 06:34:42 -0800186proxy_rx_callback (stream_session_t * s)
Dave Barach52851e62017-08-07 09:35:25 -0400187{
188 u32 max_dequeue;
189 int actual_transfer __attribute__ ((unused));
190 svm_fifo_t *tx_fifo, *rx_fifo;
Florin Coras4399c2e2018-01-25 06:34:42 -0800191 proxy_main_t *pm = &proxy_main;
Dave Barach52851e62017-08-07 09:35:25 -0400192 u32 thread_index = vlib_get_thread_index ();
193 vnet_connect_args_t _a, *a = &_a;
194 proxy_session_t *ps;
195 int proxy_index;
196 uword *p;
197 svm_fifo_t *active_open_tx_fifo;
Dave Barach52851e62017-08-07 09:35:25 -0400198
199 ASSERT (s->thread_index == thread_index);
200
Florin Coras4399c2e2018-01-25 06:34:42 -0800201 clib_spinlock_lock_if_init (&pm->sessions_lock);
202 p = hash_get (pm->proxy_session_by_server_handle, session_handle (s));
Dave Barach52851e62017-08-07 09:35:25 -0400203
204 if (PREDICT_TRUE (p != 0))
205 {
Florin Coras4399c2e2018-01-25 06:34:42 -0800206 clib_spinlock_unlock_if_init (&pm->sessions_lock);
Dave Barach52851e62017-08-07 09:35:25 -0400207 active_open_tx_fifo = s->server_rx_fifo;
208
209 /*
210 * Send event for active open tx fifo
211 */
212 if (svm_fifo_set_event (active_open_tx_fifo))
213 {
Florin Corascd330c82018-05-09 17:33:00 -0700214 u32 ao_thread_index = active_open_tx_fifo->master_thread_index;
Florin Coras3c2fed52018-07-04 04:15:05 -0700215 if (session_send_io_evt_to_thread_custom (active_open_tx_fifo,
216 ao_thread_index,
217 FIFO_EVENT_APP_TX))
Dave Barach52851e62017-08-07 09:35:25 -0400218 clib_warning ("failed to enqueue tx evt");
219 }
220 }
221 else
222 {
223 rx_fifo = s->server_rx_fifo;
224 tx_fifo = s->server_tx_fifo;
225
226 ASSERT (rx_fifo->master_thread_index == thread_index);
227 ASSERT (tx_fifo->master_thread_index == thread_index);
228
229 max_dequeue = svm_fifo_max_dequeue (s->server_rx_fifo);
230
231 if (PREDICT_FALSE (max_dequeue == 0))
232 return 0;
233
234 actual_transfer = svm_fifo_peek (rx_fifo, 0 /* relative_offset */ ,
Florin Coras4399c2e2018-01-25 06:34:42 -0800235 max_dequeue, pm->rx_buf[thread_index]);
Dave Barach52851e62017-08-07 09:35:25 -0400236
237 /* $$$ your message in this space: parse url, etc. */
238
Dave Barachb7b92992018-10-17 10:38:51 -0400239 clib_memset (a, 0, sizeof (*a));
Dave Barach52851e62017-08-07 09:35:25 -0400240
Florin Coras4399c2e2018-01-25 06:34:42 -0800241 clib_spinlock_lock_if_init (&pm->sessions_lock);
242 pool_get (pm->sessions, ps);
Dave Barachb7b92992018-10-17 10:38:51 -0400243 clib_memset (ps, 0, sizeof (*ps));
Dave Barach52851e62017-08-07 09:35:25 -0400244 ps->server_rx_fifo = rx_fifo;
245 ps->server_tx_fifo = tx_fifo;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700246 ps->vpp_server_handle = session_handle (s);
Dave Barach52851e62017-08-07 09:35:25 -0400247
Florin Coras4399c2e2018-01-25 06:34:42 -0800248 proxy_index = ps - pm->sessions;
Dave Barach52851e62017-08-07 09:35:25 -0400249
Florin Coras4399c2e2018-01-25 06:34:42 -0800250 hash_set (pm->proxy_session_by_server_handle, ps->vpp_server_handle,
Dave Barach52851e62017-08-07 09:35:25 -0400251 proxy_index);
252
Florin Coras4399c2e2018-01-25 06:34:42 -0800253 clib_spinlock_unlock_if_init (&pm->sessions_lock);
Dave Barach52851e62017-08-07 09:35:25 -0400254
Florin Coras4399c2e2018-01-25 06:34:42 -0800255 a->uri = (char *) pm->client_uri;
Dave Barach52851e62017-08-07 09:35:25 -0400256 a->api_context = proxy_index;
Florin Coras4399c2e2018-01-25 06:34:42 -0800257 a->app_index = pm->active_open_app_index;
Florin Corascd330c82018-05-09 17:33:00 -0700258 proxy_call_main_thread (a);
Dave Barach52851e62017-08-07 09:35:25 -0400259 }
260
261 return 0;
262}
263
Florin Coras4399c2e2018-01-25 06:34:42 -0800264static session_cb_vft_t proxy_session_cb_vft = {
265 .session_accept_callback = proxy_accept_callback,
266 .session_disconnect_callback = proxy_disconnect_callback,
267 .session_connected_callback = proxy_connected_callback,
268 .add_segment_callback = proxy_add_segment_callback,
Florin Coras371ca502018-02-21 12:07:41 -0800269 .builtin_app_rx_callback = proxy_rx_callback,
Florin Coras4399c2e2018-01-25 06:34:42 -0800270 .session_reset_callback = proxy_reset_callback
Dave Barach52851e62017-08-07 09:35:25 -0400271};
272
273static int
274active_open_connected_callback (u32 app_index, u32 opaque,
275 stream_session_t * s, u8 is_fail)
276{
Florin Coras4399c2e2018-01-25 06:34:42 -0800277 proxy_main_t *pm = &proxy_main;
Dave Barach52851e62017-08-07 09:35:25 -0400278 proxy_session_t *ps;
279 u8 thread_index = vlib_get_thread_index ();
Dave Barach52851e62017-08-07 09:35:25 -0400280
281 if (is_fail)
282 {
283 clib_warning ("connection %d failed!", opaque);
284 return 0;
285 }
286
287 /*
288 * Setup proxy session handle.
289 */
Florin Coras4399c2e2018-01-25 06:34:42 -0800290 clib_spinlock_lock_if_init (&pm->sessions_lock);
Dave Barach52851e62017-08-07 09:35:25 -0400291
Florin Coras4399c2e2018-01-25 06:34:42 -0800292 ps = pool_elt_at_index (pm->sessions, opaque);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700293 ps->vpp_active_open_handle = session_handle (s);
Dave Barach52851e62017-08-07 09:35:25 -0400294
295 s->server_tx_fifo = ps->server_rx_fifo;
296 s->server_rx_fifo = ps->server_tx_fifo;
297
298 /*
299 * Reset the active-open tx-fifo master indices so the active-open session
300 * will receive data, etc.
301 */
302 s->server_tx_fifo->master_session_index = s->session_index;
303 s->server_tx_fifo->master_thread_index = s->thread_index;
304
305 /*
306 * Account for the active-open session's use of the fifos
307 * so they won't disappear until the last session which uses
308 * them disappears
309 */
310 s->server_tx_fifo->refcnt++;
311 s->server_rx_fifo->refcnt++;
312
Florin Coras4399c2e2018-01-25 06:34:42 -0800313 hash_set (pm->proxy_session_by_active_open_handle,
Dave Barach52851e62017-08-07 09:35:25 -0400314 ps->vpp_active_open_handle, opaque);
315
Florin Coras4399c2e2018-01-25 06:34:42 -0800316 clib_spinlock_unlock_if_init (&pm->sessions_lock);
Dave Barach52851e62017-08-07 09:35:25 -0400317
318 /*
319 * Send event for active open tx fifo
320 */
Florin Coras3c2fed52018-07-04 04:15:05 -0700321 ASSERT (s->thread_index == thread_index);
Dave Barach52851e62017-08-07 09:35:25 -0400322 if (svm_fifo_set_event (s->server_tx_fifo))
Florin Coras3c2fed52018-07-04 04:15:05 -0700323 session_send_io_evt_to_thread (s->server_tx_fifo, FIFO_EVENT_APP_TX);
Dave Barach52851e62017-08-07 09:35:25 -0400324
325 return 0;
326}
327
328static void
329active_open_reset_callback (stream_session_t * s)
330{
331 delete_proxy_session (s, 1 /* is_active_open */ );
332}
333
334static int
335active_open_create_callback (stream_session_t * s)
336{
337 return 0;
338}
339
340static void
341active_open_disconnect_callback (stream_session_t * s)
342{
343 delete_proxy_session (s, 1 /* is_active_open */ );
344}
345
346static int
347active_open_rx_callback (stream_session_t * s)
348{
Florin Corascd330c82018-05-09 17:33:00 -0700349 svm_fifo_t *proxy_tx_fifo;
Dave Barach52851e62017-08-07 09:35:25 -0400350
Florin Corascd330c82018-05-09 17:33:00 -0700351 proxy_tx_fifo = s->server_rx_fifo;
Dave Barach52851e62017-08-07 09:35:25 -0400352
353 /*
354 * Send event for server tx fifo
355 */
Florin Corascd330c82018-05-09 17:33:00 -0700356 if (svm_fifo_set_event (proxy_tx_fifo))
Dave Barach52851e62017-08-07 09:35:25 -0400357 {
Florin Coras3c2fed52018-07-04 04:15:05 -0700358 u8 thread_index = proxy_tx_fifo->master_thread_index;
359 return session_send_io_evt_to_thread_custom (proxy_tx_fifo,
360 thread_index,
361 FIFO_EVENT_APP_TX);
Dave Barach52851e62017-08-07 09:35:25 -0400362 }
363
364 return 0;
365}
366
367/* *INDENT-OFF* */
Florin Coras4399c2e2018-01-25 06:34:42 -0800368static session_cb_vft_t active_open_clients = {
Dave Barach52851e62017-08-07 09:35:25 -0400369 .session_reset_callback = active_open_reset_callback,
370 .session_connected_callback = active_open_connected_callback,
371 .session_accept_callback = active_open_create_callback,
372 .session_disconnect_callback = active_open_disconnect_callback,
Florin Coras371ca502018-02-21 12:07:41 -0800373 .builtin_app_rx_callback = active_open_rx_callback
Dave Barach52851e62017-08-07 09:35:25 -0400374};
375/* *INDENT-ON* */
376
377
378static void
379create_api_loopbacks (vlib_main_t * vm)
380{
Florin Coras4399c2e2018-01-25 06:34:42 -0800381 proxy_main_t *pm = &proxy_main;
Dave Barach52851e62017-08-07 09:35:25 -0400382 api_main_t *am = &api_main;
383 vl_shmem_hdr_t *shmem_hdr;
384
385 shmem_hdr = am->shmem_hdr;
Florin Coras4399c2e2018-01-25 06:34:42 -0800386 pm->vl_input_queue = shmem_hdr->vl_input_queue;
387 pm->server_client_index =
388 vl_api_memclnt_create_internal ("proxy_server", pm->vl_input_queue);
389 pm->active_open_client_index =
390 vl_api_memclnt_create_internal ("proxy_active_open", pm->vl_input_queue);
Dave Barach52851e62017-08-07 09:35:25 -0400391}
392
393static int
Florin Coras4399c2e2018-01-25 06:34:42 -0800394proxy_server_attach ()
Dave Barach52851e62017-08-07 09:35:25 -0400395{
Florin Coras4399c2e2018-01-25 06:34:42 -0800396 proxy_main_t *pm = &proxy_main;
Florin Corasff6e7692017-12-11 04:59:01 -0800397 u64 options[APP_OPTIONS_N_OPTIONS];
Dave Barach52851e62017-08-07 09:35:25 -0400398 vnet_app_attach_args_t _a, *a = &_a;
Florin Corasff6e7692017-12-11 04:59:01 -0800399 u32 segment_size = 512 << 20;
Dave Barach52851e62017-08-07 09:35:25 -0400400
Dave Barachb7b92992018-10-17 10:38:51 -0400401 clib_memset (a, 0, sizeof (*a));
402 clib_memset (options, 0, sizeof (options));
Dave Barach52851e62017-08-07 09:35:25 -0400403
Florin Coras4399c2e2018-01-25 06:34:42 -0800404 if (pm->private_segment_size)
405 segment_size = pm->private_segment_size;
406 a->api_client_index = pm->server_client_index;
407 a->session_cb_vft = &proxy_session_cb_vft;
Dave Barach52851e62017-08-07 09:35:25 -0400408 a->options = options;
Florin Corasff6e7692017-12-11 04:59:01 -0800409 a->options[APP_OPTIONS_SEGMENT_SIZE] = segment_size;
Florin Coras4399c2e2018-01-25 06:34:42 -0800410 a->options[APP_OPTIONS_RX_FIFO_SIZE] = pm->fifo_size;
411 a->options[APP_OPTIONS_TX_FIFO_SIZE] = pm->fifo_size;
412 a->options[APP_OPTIONS_PRIVATE_SEGMENT_COUNT] = pm->private_segment_count;
Dave Barach52851e62017-08-07 09:35:25 -0400413 a->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] =
Florin Coras25579b42018-06-06 17:55:02 -0700414 pm->prealloc_fifos ? pm->prealloc_fifos : 0;
Dave Barach52851e62017-08-07 09:35:25 -0400415
Florin Coras7999e832017-10-31 01:51:04 -0700416 a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
Dave Barach52851e62017-08-07 09:35:25 -0400417
Dave Barach52851e62017-08-07 09:35:25 -0400418 if (vnet_application_attach (a))
419 {
420 clib_warning ("failed to attach server");
421 return -1;
422 }
Florin Coras4399c2e2018-01-25 06:34:42 -0800423 pm->server_app_index = a->app_index;
Dave Barach52851e62017-08-07 09:35:25 -0400424
425 return 0;
426}
427
428static int
429active_open_attach (void)
430{
Florin Coras4399c2e2018-01-25 06:34:42 -0800431 proxy_main_t *pm = &proxy_main;
Dave Barach52851e62017-08-07 09:35:25 -0400432 vnet_app_attach_args_t _a, *a = &_a;
Dave Barach52851e62017-08-07 09:35:25 -0400433 u64 options[16];
434
Dave Barachb7b92992018-10-17 10:38:51 -0400435 clib_memset (a, 0, sizeof (*a));
436 clib_memset (options, 0, sizeof (options));
Dave Barach52851e62017-08-07 09:35:25 -0400437
Florin Coras4399c2e2018-01-25 06:34:42 -0800438 a->api_client_index = pm->active_open_client_index;
439 a->session_cb_vft = &active_open_clients;
Dave Barach52851e62017-08-07 09:35:25 -0400440
Florin Corasff6e7692017-12-11 04:59:01 -0800441 options[APP_OPTIONS_ACCEPT_COOKIE] = 0x12345678;
442 options[APP_OPTIONS_SEGMENT_SIZE] = 512 << 20;
Florin Coras4399c2e2018-01-25 06:34:42 -0800443 options[APP_OPTIONS_RX_FIFO_SIZE] = pm->fifo_size;
444 options[APP_OPTIONS_TX_FIFO_SIZE] = pm->fifo_size;
445 options[APP_OPTIONS_PRIVATE_SEGMENT_COUNT] = pm->private_segment_count;
Dave Barach52851e62017-08-07 09:35:25 -0400446 options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] =
Florin Coras25579b42018-06-06 17:55:02 -0700447 pm->prealloc_fifos ? pm->prealloc_fifos : 0;
Dave Barach52851e62017-08-07 09:35:25 -0400448
Florin Coras7999e832017-10-31 01:51:04 -0700449 options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN
Dave Barach52851e62017-08-07 09:35:25 -0400450 | APP_OPTIONS_FLAGS_IS_PROXY;
451
452 a->options = options;
453
454 if (vnet_application_attach (a))
455 return -1;
456
Florin Coras4399c2e2018-01-25 06:34:42 -0800457 pm->active_open_app_index = a->app_index;
Dave Barach52851e62017-08-07 09:35:25 -0400458
459 return 0;
460}
461
462static int
Florin Coras4399c2e2018-01-25 06:34:42 -0800463proxy_server_listen ()
Dave Barach52851e62017-08-07 09:35:25 -0400464{
Florin Coras4399c2e2018-01-25 06:34:42 -0800465 proxy_main_t *pm = &proxy_main;
Dave Barach52851e62017-08-07 09:35:25 -0400466 vnet_bind_args_t _a, *a = &_a;
Dave Barachb7b92992018-10-17 10:38:51 -0400467 clib_memset (a, 0, sizeof (*a));
Florin Coras4399c2e2018-01-25 06:34:42 -0800468 a->app_index = pm->server_app_index;
469 a->uri = (char *) pm->server_uri;
Dave Barach52851e62017-08-07 09:35:25 -0400470 return vnet_bind_uri (a);
471}
472
473static int
Florin Coras4399c2e2018-01-25 06:34:42 -0800474proxy_server_create (vlib_main_t * vm)
Dave Barach52851e62017-08-07 09:35:25 -0400475{
Florin Coras4399c2e2018-01-25 06:34:42 -0800476 proxy_main_t *pm = &proxy_main;
Dave Barach52851e62017-08-07 09:35:25 -0400477 vlib_thread_main_t *vtm = vlib_get_thread_main ();
478 u32 num_threads;
479 int i;
480
Florin Coras4399c2e2018-01-25 06:34:42 -0800481 if (pm->server_client_index == (u32) ~ 0)
Dave Barach52851e62017-08-07 09:35:25 -0400482 create_api_loopbacks (vm);
483
484 num_threads = 1 /* main thread */ + vtm->n_threads;
Florin Coras4399c2e2018-01-25 06:34:42 -0800485 vec_validate (proxy_main.server_event_queue, num_threads - 1);
486 vec_validate (proxy_main.active_open_event_queue, num_threads - 1);
487 vec_validate (pm->rx_buf, num_threads - 1);
Dave Barach52851e62017-08-07 09:35:25 -0400488
489 for (i = 0; i < num_threads; i++)
Florin Coras4399c2e2018-01-25 06:34:42 -0800490 vec_validate (pm->rx_buf[i], pm->rcv_buffer_size);
Dave Barach52851e62017-08-07 09:35:25 -0400491
Florin Coras4399c2e2018-01-25 06:34:42 -0800492 if (proxy_server_attach ())
Dave Barach52851e62017-08-07 09:35:25 -0400493 {
494 clib_warning ("failed to attach server app");
495 return -1;
496 }
Florin Coras4399c2e2018-01-25 06:34:42 -0800497 if (proxy_server_listen ())
Dave Barach52851e62017-08-07 09:35:25 -0400498 {
499 clib_warning ("failed to start listening");
500 return -1;
501 }
502 if (active_open_attach ())
503 {
504 clib_warning ("failed to attach active open app");
505 return -1;
506 }
507
508 for (i = 0; i < num_threads; i++)
509 {
Florin Coras4399c2e2018-01-25 06:34:42 -0800510 pm->active_open_event_queue[i] =
Dave Barach52851e62017-08-07 09:35:25 -0400511 session_manager_get_vpp_event_queue (i);
512
Florin Coras4399c2e2018-01-25 06:34:42 -0800513 ASSERT (pm->active_open_event_queue[i]);
Dave Barach52851e62017-08-07 09:35:25 -0400514
Florin Coras4399c2e2018-01-25 06:34:42 -0800515 pm->server_event_queue[i] = session_manager_get_vpp_event_queue (i);
Dave Barach52851e62017-08-07 09:35:25 -0400516 }
517
518 return 0;
519}
520
521static clib_error_t *
522proxy_server_create_command_fn (vlib_main_t * vm, unformat_input_t * input,
523 vlib_cli_command_t * cmd)
524{
Florin Coras4399c2e2018-01-25 06:34:42 -0800525 proxy_main_t *pm = &proxy_main;
526 char *default_server_uri = "tcp://0.0.0.0/23";
527 char *default_client_uri = "tcp://6.0.2.2/23";
Dave Barach52851e62017-08-07 09:35:25 -0400528 int rv;
Dave Barach91f3e742017-09-01 19:12:11 -0400529 u64 tmp;
Dave Barach52851e62017-08-07 09:35:25 -0400530
Florin Coras4399c2e2018-01-25 06:34:42 -0800531 pm->fifo_size = 64 << 10;
532 pm->rcv_buffer_size = 1024;
533 pm->prealloc_fifos = 0;
534 pm->private_segment_count = 0;
535 pm->private_segment_size = 0;
536 pm->server_uri = 0;
Dave Barach52851e62017-08-07 09:35:25 -0400537
538 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
539 {
Florin Coras4399c2e2018-01-25 06:34:42 -0800540 if (unformat (input, "fifo-size %d", &pm->fifo_size))
541 pm->fifo_size <<= 10;
542 else if (unformat (input, "rcv-buf-size %d", &pm->rcv_buffer_size))
Dave Barach52851e62017-08-07 09:35:25 -0400543 ;
Florin Coras4399c2e2018-01-25 06:34:42 -0800544 else if (unformat (input, "prealloc-fifos %d", &pm->prealloc_fifos))
Dave Barach52851e62017-08-07 09:35:25 -0400545 ;
546 else if (unformat (input, "private-segment-count %d",
Florin Coras4399c2e2018-01-25 06:34:42 -0800547 &pm->private_segment_count))
Dave Barach52851e62017-08-07 09:35:25 -0400548 ;
Dave Barach91f3e742017-09-01 19:12:11 -0400549 else if (unformat (input, "private-segment-size %U",
550 unformat_memory_size, &tmp))
551 {
552 if (tmp >= 0x100000000ULL)
553 return clib_error_return
554 (0, "private segment size %lld (%llu) too large", tmp, tmp);
Florin Coras4399c2e2018-01-25 06:34:42 -0800555 pm->private_segment_size = tmp;
Dave Barach91f3e742017-09-01 19:12:11 -0400556 }
Florin Coras4399c2e2018-01-25 06:34:42 -0800557 else if (unformat (input, "server-uri %s", &pm->server_uri))
Florin Corasee7e1f52018-01-11 02:23:35 -0800558 ;
Florin Coras4399c2e2018-01-25 06:34:42 -0800559 else if (unformat (input, "client-uri %s", &pm->client_uri))
Florin Corascd330c82018-05-09 17:33:00 -0700560 pm->client_uri = format (0, "%s%c", pm->client_uri, 0);
Dave Barach52851e62017-08-07 09:35:25 -0400561 else
562 return clib_error_return (0, "unknown input `%U'",
563 format_unformat_error, input);
564 }
565
Florin Coras4399c2e2018-01-25 06:34:42 -0800566 if (!pm->server_uri)
567 {
568 clib_warning ("No server-uri provided, Using default: %s",
569 default_server_uri);
570 pm->server_uri = format (0, "%s%c", default_server_uri, 0);
571 }
572 if (!pm->client_uri)
573 {
574 clib_warning ("No client-uri provided, Using default: %s",
575 default_client_uri);
576 pm->client_uri = format (0, "%s%c", default_client_uri, 0);
577 }
Florin Corasee7e1f52018-01-11 02:23:35 -0800578
Florin Coras4399c2e2018-01-25 06:34:42 -0800579 vnet_session_enable_disable (vm, 1 /* turn on session and transport */ );
Dave Barach52851e62017-08-07 09:35:25 -0400580
Florin Coras4399c2e2018-01-25 06:34:42 -0800581 rv = proxy_server_create (vm);
Dave Barach52851e62017-08-07 09:35:25 -0400582 switch (rv)
583 {
584 case 0:
585 break;
586 default:
587 return clib_error_return (0, "server_create returned %d", rv);
588 }
589
590 return 0;
591}
592
593/* *INDENT-OFF* */
Florin Coras4399c2e2018-01-25 06:34:42 -0800594VLIB_CLI_COMMAND (proxy_create_command, static) =
Dave Barach52851e62017-08-07 09:35:25 -0400595{
596 .path = "test proxy server",
Florin Corasee7e1f52018-01-11 02:23:35 -0800597 .short_help = "test proxy server [server-uri <tcp://ip/port>]"
598 "[client-uri <tcp://ip/port>][fifo-size <nn>][rcv-buf-size <nn>]"
599 "[prealloc-fifos <nn>][private-segment-size <mem>]"
600 "[private-segment-count <nn>]",
Dave Barach52851e62017-08-07 09:35:25 -0400601 .function = proxy_server_create_command_fn,
602};
603/* *INDENT-ON* */
604
605clib_error_t *
Florin Coras4399c2e2018-01-25 06:34:42 -0800606proxy_main_init (vlib_main_t * vm)
Dave Barach52851e62017-08-07 09:35:25 -0400607{
Florin Coras4399c2e2018-01-25 06:34:42 -0800608 proxy_main_t *pm = &proxy_main;
609 pm->server_client_index = ~0;
610 pm->active_open_client_index = ~0;
611 pm->proxy_session_by_active_open_handle = hash_create (0, sizeof (uword));
612 pm->proxy_session_by_server_handle = hash_create (0, sizeof (uword));
Dave Barach52851e62017-08-07 09:35:25 -0400613
614 return 0;
615}
616
Florin Coras4399c2e2018-01-25 06:34:42 -0800617VLIB_INIT_FUNCTION (proxy_main_init);
Dave Barach52851e62017-08-07 09:35:25 -0400618
619/*
620* fd.io coding-style-patch-verification: ON
621*
622* Local Variables:
623* eval: (c-set-style "gnu")
624* End:
625*/