blob: 190821780e5a33c304e58bc1af87c3d2400f156b [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
24static void
25delete_proxy_session (stream_session_t * s, int is_active_open)
26{
Florin Coras4399c2e2018-01-25 06:34:42 -080027 proxy_main_t *pm = &proxy_main;
Dave Barach52851e62017-08-07 09:35:25 -040028 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;
32 uword *p;
33 u64 handle;
34
Florin Coras3cbc04b2017-10-02 00:18:51 -070035 handle = session_handle (s);
Dave Barach52851e62017-08-07 09:35:25 -040036
Florin Coras4399c2e2018-01-25 06:34:42 -080037 clib_spinlock_lock_if_init (&pm->sessions_lock);
Dave Barach52851e62017-08-07 09:35:25 -040038 if (is_active_open)
39 {
40 active_open_session = s;
41
Florin Coras4399c2e2018-01-25 06:34:42 -080042 p = hash_get (pm->proxy_session_by_active_open_handle, handle);
Dave Barach52851e62017-08-07 09:35:25 -040043 if (p == 0)
44 {
45 clib_warning ("proxy session for %s handle %lld (%llx) AWOL",
46 is_active_open ? "active open" : "server",
47 handle, handle);
48 }
49 else
50 {
Florin Coras4399c2e2018-01-25 06:34:42 -080051 ps = pool_elt_at_index (pm->sessions, p[0]);
Dave Barach52851e62017-08-07 09:35:25 -040052 if (ps->vpp_server_handle != ~0)
Florin Corascea194d2017-10-02 00:18:51 -070053 server_session = session_get_from_handle (ps->vpp_server_handle);
Dave Barach52851e62017-08-07 09:35:25 -040054 else
55 server_session = 0;
56 }
57 }
58 else
59 {
60 server_session = s;
61
Florin Coras4399c2e2018-01-25 06:34:42 -080062 p = hash_get (pm->proxy_session_by_server_handle, handle);
Dave Barach52851e62017-08-07 09:35:25 -040063 if (p == 0)
64 {
65 clib_warning ("proxy session for %s handle %lld (%llx) AWOL",
66 is_active_open ? "active open" : "server",
67 handle, handle);
68 }
69 else
70 {
Florin Coras4399c2e2018-01-25 06:34:42 -080071 ps = pool_elt_at_index (pm->sessions, p[0]);
Dave Barach52851e62017-08-07 09:35:25 -040072 if (ps->vpp_server_handle != ~0)
Florin Corascea194d2017-10-02 00:18:51 -070073 active_open_session = session_get_from_handle
Dave Barach52851e62017-08-07 09:35:25 -040074 (ps->vpp_server_handle);
75 else
76 active_open_session = 0;
77 }
78 }
79
80 if (ps)
81 {
82 if (CLIB_DEBUG > 0)
83 memset (ps, 0xFE, sizeof (*ps));
Florin Coras4399c2e2018-01-25 06:34:42 -080084 pool_put (pm->sessions, ps);
Dave Barach52851e62017-08-07 09:35:25 -040085 }
86
Florin Coras4399c2e2018-01-25 06:34:42 -080087 clib_spinlock_unlock_if_init (&pm->sessions_lock);
Dave Barach52851e62017-08-07 09:35:25 -040088
89 if (active_open_session)
90 {
Florin Coras3cbc04b2017-10-02 00:18:51 -070091 a->handle = session_handle (active_open_session);
Florin Coras4399c2e2018-01-25 06:34:42 -080092 a->app_index = pm->active_open_app_index;
93 hash_unset (pm->proxy_session_by_active_open_handle,
Florin Coras3cbc04b2017-10-02 00:18:51 -070094 session_handle (active_open_session));
Dave Barach52851e62017-08-07 09:35:25 -040095 vnet_disconnect_session (a);
96 }
97
98 if (server_session)
99 {
Florin Coras3cbc04b2017-10-02 00:18:51 -0700100 a->handle = session_handle (server_session);
Florin Coras4399c2e2018-01-25 06:34:42 -0800101 a->app_index = pm->server_app_index;
102 hash_unset (pm->proxy_session_by_server_handle,
Florin Coras3cbc04b2017-10-02 00:18:51 -0700103 session_handle (server_session));
Dave Barach52851e62017-08-07 09:35:25 -0400104 vnet_disconnect_session (a);
105 }
106}
107
108static int
Florin Coras4399c2e2018-01-25 06:34:42 -0800109proxy_accept_callback (stream_session_t * s)
Dave Barach52851e62017-08-07 09:35:25 -0400110{
Florin Coras4399c2e2018-01-25 06:34:42 -0800111 proxy_main_t *pm = &proxy_main;
Dave Barach52851e62017-08-07 09:35:25 -0400112
113 s->session_state = SESSION_STATE_READY;
114
Florin Coras4399c2e2018-01-25 06:34:42 -0800115 clib_spinlock_lock_if_init (&pm->sessions_lock);
Dave Barach52851e62017-08-07 09:35:25 -0400116
117 return 0;
118}
119
120static void
Florin Coras4399c2e2018-01-25 06:34:42 -0800121proxy_disconnect_callback (stream_session_t * s)
Dave Barach52851e62017-08-07 09:35:25 -0400122{
123 delete_proxy_session (s, 0 /* is_active_open */ );
124}
125
126static void
Florin Coras4399c2e2018-01-25 06:34:42 -0800127proxy_reset_callback (stream_session_t * s)
Dave Barach52851e62017-08-07 09:35:25 -0400128{
129 clib_warning ("Reset session %U", format_stream_session, s, 2);
130 delete_proxy_session (s, 0 /* is_active_open */ );
131}
132
133static int
Florin Coras4399c2e2018-01-25 06:34:42 -0800134proxy_connected_callback (u32 app_index, u32 api_context,
135 stream_session_t * s, u8 is_fail)
Dave Barach52851e62017-08-07 09:35:25 -0400136{
137 clib_warning ("called...");
138 return -1;
139}
140
141static int
Florin Coras4399c2e2018-01-25 06:34:42 -0800142proxy_add_segment_callback (u32 client_index, const ssvm_private_t * sp)
Dave Barach52851e62017-08-07 09:35:25 -0400143{
144 clib_warning ("called...");
145 return -1;
146}
147
148static int
Florin Coras4399c2e2018-01-25 06:34:42 -0800149proxy_rx_callback (stream_session_t * s)
Dave Barach52851e62017-08-07 09:35:25 -0400150{
151 u32 max_dequeue;
152 int actual_transfer __attribute__ ((unused));
153 svm_fifo_t *tx_fifo, *rx_fifo;
Florin Coras4399c2e2018-01-25 06:34:42 -0800154 proxy_main_t *pm = &proxy_main;
Dave Barach52851e62017-08-07 09:35:25 -0400155 u32 thread_index = vlib_get_thread_index ();
156 vnet_connect_args_t _a, *a = &_a;
157 proxy_session_t *ps;
158 int proxy_index;
159 uword *p;
160 svm_fifo_t *active_open_tx_fifo;
161 session_fifo_event_t evt;
162
163 ASSERT (s->thread_index == thread_index);
164
Florin Coras4399c2e2018-01-25 06:34:42 -0800165 clib_spinlock_lock_if_init (&pm->sessions_lock);
166 p = hash_get (pm->proxy_session_by_server_handle, session_handle (s));
Dave Barach52851e62017-08-07 09:35:25 -0400167
168 if (PREDICT_TRUE (p != 0))
169 {
Florin Coras4399c2e2018-01-25 06:34:42 -0800170 clib_spinlock_unlock_if_init (&pm->sessions_lock);
Dave Barach52851e62017-08-07 09:35:25 -0400171 active_open_tx_fifo = s->server_rx_fifo;
172
173 /*
174 * Send event for active open tx fifo
175 */
176 if (svm_fifo_set_event (active_open_tx_fifo))
177 {
178 evt.fifo = active_open_tx_fifo;
179 evt.event_type = FIFO_EVENT_APP_TX;
Florin Corase86a8ed2018-01-05 03:20:25 -0800180 if (svm_queue_add
Florin Coras4399c2e2018-01-25 06:34:42 -0800181 (pm->active_open_event_queue[thread_index], (u8 *) & evt,
Dave Barach52851e62017-08-07 09:35:25 -0400182 0 /* do wait for mutex */ ))
183 clib_warning ("failed to enqueue tx evt");
184 }
185 }
186 else
187 {
188 rx_fifo = s->server_rx_fifo;
189 tx_fifo = s->server_tx_fifo;
190
191 ASSERT (rx_fifo->master_thread_index == thread_index);
192 ASSERT (tx_fifo->master_thread_index == thread_index);
193
194 max_dequeue = svm_fifo_max_dequeue (s->server_rx_fifo);
195
196 if (PREDICT_FALSE (max_dequeue == 0))
197 return 0;
198
199 actual_transfer = svm_fifo_peek (rx_fifo, 0 /* relative_offset */ ,
Florin Coras4399c2e2018-01-25 06:34:42 -0800200 max_dequeue, pm->rx_buf[thread_index]);
Dave Barach52851e62017-08-07 09:35:25 -0400201
202 /* $$$ your message in this space: parse url, etc. */
203
204 memset (a, 0, sizeof (*a));
205
Florin Coras4399c2e2018-01-25 06:34:42 -0800206 clib_spinlock_lock_if_init (&pm->sessions_lock);
207 pool_get (pm->sessions, ps);
Dave Barach52851e62017-08-07 09:35:25 -0400208 memset (ps, 0, sizeof (*ps));
209 ps->server_rx_fifo = rx_fifo;
210 ps->server_tx_fifo = tx_fifo;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700211 ps->vpp_server_handle = session_handle (s);
Dave Barach52851e62017-08-07 09:35:25 -0400212
Florin Coras4399c2e2018-01-25 06:34:42 -0800213 proxy_index = ps - pm->sessions;
Dave Barach52851e62017-08-07 09:35:25 -0400214
Florin Coras4399c2e2018-01-25 06:34:42 -0800215 hash_set (pm->proxy_session_by_server_handle, ps->vpp_server_handle,
Dave Barach52851e62017-08-07 09:35:25 -0400216 proxy_index);
217
Florin Coras4399c2e2018-01-25 06:34:42 -0800218 clib_spinlock_unlock_if_init (&pm->sessions_lock);
Dave Barach52851e62017-08-07 09:35:25 -0400219
Florin Coras4399c2e2018-01-25 06:34:42 -0800220 a->uri = (char *) pm->client_uri;
Dave Barach52851e62017-08-07 09:35:25 -0400221 a->api_context = proxy_index;
Florin Coras4399c2e2018-01-25 06:34:42 -0800222 a->app_index = pm->active_open_app_index;
Dave Barach52851e62017-08-07 09:35:25 -0400223 vnet_connect_uri (a);
224 }
225
226 return 0;
227}
228
Florin Coras4399c2e2018-01-25 06:34:42 -0800229static session_cb_vft_t proxy_session_cb_vft = {
230 .session_accept_callback = proxy_accept_callback,
231 .session_disconnect_callback = proxy_disconnect_callback,
232 .session_connected_callback = proxy_connected_callback,
233 .add_segment_callback = proxy_add_segment_callback,
Florin Coras371ca502018-02-21 12:07:41 -0800234 .builtin_app_rx_callback = proxy_rx_callback,
Florin Coras4399c2e2018-01-25 06:34:42 -0800235 .session_reset_callback = proxy_reset_callback
Dave Barach52851e62017-08-07 09:35:25 -0400236};
237
238static int
239active_open_connected_callback (u32 app_index, u32 opaque,
240 stream_session_t * s, u8 is_fail)
241{
Florin Coras4399c2e2018-01-25 06:34:42 -0800242 proxy_main_t *pm = &proxy_main;
Dave Barach52851e62017-08-07 09:35:25 -0400243 proxy_session_t *ps;
244 u8 thread_index = vlib_get_thread_index ();
245 session_fifo_event_t evt;
246
247 if (is_fail)
248 {
249 clib_warning ("connection %d failed!", opaque);
250 return 0;
251 }
252
253 /*
254 * Setup proxy session handle.
255 */
Florin Coras4399c2e2018-01-25 06:34:42 -0800256 clib_spinlock_lock_if_init (&pm->sessions_lock);
Dave Barach52851e62017-08-07 09:35:25 -0400257
Florin Coras4399c2e2018-01-25 06:34:42 -0800258 ps = pool_elt_at_index (pm->sessions, opaque);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700259 ps->vpp_active_open_handle = session_handle (s);
Dave Barach52851e62017-08-07 09:35:25 -0400260
261 s->server_tx_fifo = ps->server_rx_fifo;
262 s->server_rx_fifo = ps->server_tx_fifo;
263
264 /*
265 * Reset the active-open tx-fifo master indices so the active-open session
266 * will receive data, etc.
267 */
268 s->server_tx_fifo->master_session_index = s->session_index;
269 s->server_tx_fifo->master_thread_index = s->thread_index;
270
271 /*
272 * Account for the active-open session's use of the fifos
273 * so they won't disappear until the last session which uses
274 * them disappears
275 */
276 s->server_tx_fifo->refcnt++;
277 s->server_rx_fifo->refcnt++;
278
Florin Coras4399c2e2018-01-25 06:34:42 -0800279 hash_set (pm->proxy_session_by_active_open_handle,
Dave Barach52851e62017-08-07 09:35:25 -0400280 ps->vpp_active_open_handle, opaque);
281
Florin Coras4399c2e2018-01-25 06:34:42 -0800282 clib_spinlock_unlock_if_init (&pm->sessions_lock);
Dave Barach52851e62017-08-07 09:35:25 -0400283
284 /*
285 * Send event for active open tx fifo
286 */
287 if (svm_fifo_set_event (s->server_tx_fifo))
288 {
289 evt.fifo = s->server_tx_fifo;
290 evt.event_type = FIFO_EVENT_APP_TX;
Florin Corase86a8ed2018-01-05 03:20:25 -0800291 if (svm_queue_add
Florin Coras4399c2e2018-01-25 06:34:42 -0800292 (pm->active_open_event_queue[thread_index], (u8 *) & evt,
Dave Barach52851e62017-08-07 09:35:25 -0400293 0 /* do wait for mutex */ ))
294 clib_warning ("failed to enqueue tx evt");
295 }
296
297 return 0;
298}
299
300static void
301active_open_reset_callback (stream_session_t * s)
302{
303 delete_proxy_session (s, 1 /* is_active_open */ );
304}
305
306static int
307active_open_create_callback (stream_session_t * s)
308{
309 return 0;
310}
311
312static void
313active_open_disconnect_callback (stream_session_t * s)
314{
315 delete_proxy_session (s, 1 /* is_active_open */ );
316}
317
318static int
319active_open_rx_callback (stream_session_t * s)
320{
Florin Coras4399c2e2018-01-25 06:34:42 -0800321 proxy_main_t *pm = &proxy_main;
Dave Barach52851e62017-08-07 09:35:25 -0400322 session_fifo_event_t evt;
323 svm_fifo_t *server_rx_fifo;
324 u32 thread_index = vlib_get_thread_index ();
325
326 server_rx_fifo = s->server_rx_fifo;
327
328 /*
329 * Send event for server tx fifo
330 */
331 if (svm_fifo_set_event (server_rx_fifo))
332 {
333 evt.fifo = server_rx_fifo;
334 evt.event_type = FIFO_EVENT_APP_TX;
Florin Corase86a8ed2018-01-05 03:20:25 -0800335 if (svm_queue_add
Florin Coras4399c2e2018-01-25 06:34:42 -0800336 (pm->server_event_queue[thread_index], (u8 *) & evt,
Dave Barach52851e62017-08-07 09:35:25 -0400337 0 /* do wait for mutex */ ))
338 clib_warning ("failed to enqueue server rx evt");
339 }
340
341 return 0;
342}
343
344/* *INDENT-OFF* */
Florin Coras4399c2e2018-01-25 06:34:42 -0800345static session_cb_vft_t active_open_clients = {
Dave Barach52851e62017-08-07 09:35:25 -0400346 .session_reset_callback = active_open_reset_callback,
347 .session_connected_callback = active_open_connected_callback,
348 .session_accept_callback = active_open_create_callback,
349 .session_disconnect_callback = active_open_disconnect_callback,
Florin Coras371ca502018-02-21 12:07:41 -0800350 .builtin_app_rx_callback = active_open_rx_callback
Dave Barach52851e62017-08-07 09:35:25 -0400351};
352/* *INDENT-ON* */
353
354
355static void
356create_api_loopbacks (vlib_main_t * vm)
357{
Florin Coras4399c2e2018-01-25 06:34:42 -0800358 proxy_main_t *pm = &proxy_main;
Dave Barach52851e62017-08-07 09:35:25 -0400359 api_main_t *am = &api_main;
360 vl_shmem_hdr_t *shmem_hdr;
361
362 shmem_hdr = am->shmem_hdr;
Florin Coras4399c2e2018-01-25 06:34:42 -0800363 pm->vl_input_queue = shmem_hdr->vl_input_queue;
364 pm->server_client_index =
365 vl_api_memclnt_create_internal ("proxy_server", pm->vl_input_queue);
366 pm->active_open_client_index =
367 vl_api_memclnt_create_internal ("proxy_active_open", pm->vl_input_queue);
Dave Barach52851e62017-08-07 09:35:25 -0400368}
369
370static int
Florin Coras4399c2e2018-01-25 06:34:42 -0800371proxy_server_attach ()
Dave Barach52851e62017-08-07 09:35:25 -0400372{
Florin Coras4399c2e2018-01-25 06:34:42 -0800373 proxy_main_t *pm = &proxy_main;
Florin Corasff6e7692017-12-11 04:59:01 -0800374 u64 options[APP_OPTIONS_N_OPTIONS];
Dave Barach52851e62017-08-07 09:35:25 -0400375 vnet_app_attach_args_t _a, *a = &_a;
Florin Corasff6e7692017-12-11 04:59:01 -0800376 u32 segment_size = 512 << 20;
Dave Barach52851e62017-08-07 09:35:25 -0400377
378 memset (a, 0, sizeof (*a));
379 memset (options, 0, sizeof (options));
380
Florin Coras4399c2e2018-01-25 06:34:42 -0800381 if (pm->private_segment_size)
382 segment_size = pm->private_segment_size;
383 a->api_client_index = pm->server_client_index;
384 a->session_cb_vft = &proxy_session_cb_vft;
Dave Barach52851e62017-08-07 09:35:25 -0400385 a->options = options;
Florin Corasff6e7692017-12-11 04:59:01 -0800386 a->options[APP_OPTIONS_SEGMENT_SIZE] = segment_size;
Florin Coras4399c2e2018-01-25 06:34:42 -0800387 a->options[APP_OPTIONS_RX_FIFO_SIZE] = pm->fifo_size;
388 a->options[APP_OPTIONS_TX_FIFO_SIZE] = pm->fifo_size;
389 a->options[APP_OPTIONS_PRIVATE_SEGMENT_COUNT] = pm->private_segment_count;
Dave Barach52851e62017-08-07 09:35:25 -0400390 a->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] =
Florin Coras4399c2e2018-01-25 06:34:42 -0800391 pm->prealloc_fifos ? pm->prealloc_fifos : 1;
Dave Barach52851e62017-08-07 09:35:25 -0400392
Florin Coras7999e832017-10-31 01:51:04 -0700393 a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
Dave Barach52851e62017-08-07 09:35:25 -0400394
Dave Barach52851e62017-08-07 09:35:25 -0400395 if (vnet_application_attach (a))
396 {
397 clib_warning ("failed to attach server");
398 return -1;
399 }
Florin Coras4399c2e2018-01-25 06:34:42 -0800400 pm->server_app_index = a->app_index;
Dave Barach52851e62017-08-07 09:35:25 -0400401
402 return 0;
403}
404
405static int
406active_open_attach (void)
407{
Florin Coras4399c2e2018-01-25 06:34:42 -0800408 proxy_main_t *pm = &proxy_main;
Dave Barach52851e62017-08-07 09:35:25 -0400409 vnet_app_attach_args_t _a, *a = &_a;
Dave Barach52851e62017-08-07 09:35:25 -0400410 u64 options[16];
411
Dave Barach52851e62017-08-07 09:35:25 -0400412 memset (a, 0, sizeof (*a));
413 memset (options, 0, sizeof (options));
414
Florin Coras4399c2e2018-01-25 06:34:42 -0800415 a->api_client_index = pm->active_open_client_index;
416 a->session_cb_vft = &active_open_clients;
Dave Barach52851e62017-08-07 09:35:25 -0400417
Florin Corasff6e7692017-12-11 04:59:01 -0800418 options[APP_OPTIONS_ACCEPT_COOKIE] = 0x12345678;
419 options[APP_OPTIONS_SEGMENT_SIZE] = 512 << 20;
Florin Coras4399c2e2018-01-25 06:34:42 -0800420 options[APP_OPTIONS_RX_FIFO_SIZE] = pm->fifo_size;
421 options[APP_OPTIONS_TX_FIFO_SIZE] = pm->fifo_size;
422 options[APP_OPTIONS_PRIVATE_SEGMENT_COUNT] = pm->private_segment_count;
Dave Barach52851e62017-08-07 09:35:25 -0400423 options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] =
Florin Coras4399c2e2018-01-25 06:34:42 -0800424 pm->prealloc_fifos ? pm->prealloc_fifos : 1;
Dave Barach52851e62017-08-07 09:35:25 -0400425
Florin Coras7999e832017-10-31 01:51:04 -0700426 options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN
Dave Barach52851e62017-08-07 09:35:25 -0400427 | APP_OPTIONS_FLAGS_IS_PROXY;
428
429 a->options = options;
430
431 if (vnet_application_attach (a))
432 return -1;
433
Florin Coras4399c2e2018-01-25 06:34:42 -0800434 pm->active_open_app_index = a->app_index;
Dave Barach52851e62017-08-07 09:35:25 -0400435
436 return 0;
437}
438
439static int
Florin Coras4399c2e2018-01-25 06:34:42 -0800440proxy_server_listen ()
Dave Barach52851e62017-08-07 09:35:25 -0400441{
Florin Coras4399c2e2018-01-25 06:34:42 -0800442 proxy_main_t *pm = &proxy_main;
Dave Barach52851e62017-08-07 09:35:25 -0400443 vnet_bind_args_t _a, *a = &_a;
444 memset (a, 0, sizeof (*a));
Florin Coras4399c2e2018-01-25 06:34:42 -0800445 a->app_index = pm->server_app_index;
446 a->uri = (char *) pm->server_uri;
Dave Barach52851e62017-08-07 09:35:25 -0400447 return vnet_bind_uri (a);
448}
449
450static int
Florin Coras4399c2e2018-01-25 06:34:42 -0800451proxy_server_create (vlib_main_t * vm)
Dave Barach52851e62017-08-07 09:35:25 -0400452{
Florin Coras4399c2e2018-01-25 06:34:42 -0800453 proxy_main_t *pm = &proxy_main;
Dave Barach52851e62017-08-07 09:35:25 -0400454 vlib_thread_main_t *vtm = vlib_get_thread_main ();
455 u32 num_threads;
456 int i;
457
Florin Coras4399c2e2018-01-25 06:34:42 -0800458 if (pm->server_client_index == (u32) ~ 0)
Dave Barach52851e62017-08-07 09:35:25 -0400459 create_api_loopbacks (vm);
460
461 num_threads = 1 /* main thread */ + vtm->n_threads;
Florin Coras4399c2e2018-01-25 06:34:42 -0800462 vec_validate (proxy_main.server_event_queue, num_threads - 1);
463 vec_validate (proxy_main.active_open_event_queue, num_threads - 1);
464 vec_validate (pm->rx_buf, num_threads - 1);
Dave Barach52851e62017-08-07 09:35:25 -0400465
466 for (i = 0; i < num_threads; i++)
Florin Coras4399c2e2018-01-25 06:34:42 -0800467 vec_validate (pm->rx_buf[i], pm->rcv_buffer_size);
Dave Barach52851e62017-08-07 09:35:25 -0400468
Florin Coras4399c2e2018-01-25 06:34:42 -0800469 if (proxy_server_attach ())
Dave Barach52851e62017-08-07 09:35:25 -0400470 {
471 clib_warning ("failed to attach server app");
472 return -1;
473 }
Florin Coras4399c2e2018-01-25 06:34:42 -0800474 if (proxy_server_listen ())
Dave Barach52851e62017-08-07 09:35:25 -0400475 {
476 clib_warning ("failed to start listening");
477 return -1;
478 }
479 if (active_open_attach ())
480 {
481 clib_warning ("failed to attach active open app");
482 return -1;
483 }
484
485 for (i = 0; i < num_threads; i++)
486 {
Florin Coras4399c2e2018-01-25 06:34:42 -0800487 pm->active_open_event_queue[i] =
Dave Barach52851e62017-08-07 09:35:25 -0400488 session_manager_get_vpp_event_queue (i);
489
Florin Coras4399c2e2018-01-25 06:34:42 -0800490 ASSERT (pm->active_open_event_queue[i]);
Dave Barach52851e62017-08-07 09:35:25 -0400491
Florin Coras4399c2e2018-01-25 06:34:42 -0800492 pm->server_event_queue[i] = session_manager_get_vpp_event_queue (i);
Dave Barach52851e62017-08-07 09:35:25 -0400493 }
494
495 return 0;
496}
497
498static clib_error_t *
499proxy_server_create_command_fn (vlib_main_t * vm, unformat_input_t * input,
500 vlib_cli_command_t * cmd)
501{
Florin Coras4399c2e2018-01-25 06:34:42 -0800502 proxy_main_t *pm = &proxy_main;
503 char *default_server_uri = "tcp://0.0.0.0/23";
504 char *default_client_uri = "tcp://6.0.2.2/23";
Dave Barach52851e62017-08-07 09:35:25 -0400505 int rv;
Dave Barach91f3e742017-09-01 19:12:11 -0400506 u64 tmp;
Dave Barach52851e62017-08-07 09:35:25 -0400507
Florin Coras4399c2e2018-01-25 06:34:42 -0800508 pm->fifo_size = 64 << 10;
509 pm->rcv_buffer_size = 1024;
510 pm->prealloc_fifos = 0;
511 pm->private_segment_count = 0;
512 pm->private_segment_size = 0;
513 pm->server_uri = 0;
Dave Barach52851e62017-08-07 09:35:25 -0400514
515 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
516 {
Florin Coras4399c2e2018-01-25 06:34:42 -0800517 if (unformat (input, "fifo-size %d", &pm->fifo_size))
518 pm->fifo_size <<= 10;
519 else if (unformat (input, "rcv-buf-size %d", &pm->rcv_buffer_size))
Dave Barach52851e62017-08-07 09:35:25 -0400520 ;
Florin Coras4399c2e2018-01-25 06:34:42 -0800521 else if (unformat (input, "prealloc-fifos %d", &pm->prealloc_fifos))
Dave Barach52851e62017-08-07 09:35:25 -0400522 ;
523 else if (unformat (input, "private-segment-count %d",
Florin Coras4399c2e2018-01-25 06:34:42 -0800524 &pm->private_segment_count))
Dave Barach52851e62017-08-07 09:35:25 -0400525 ;
Dave Barach91f3e742017-09-01 19:12:11 -0400526 else if (unformat (input, "private-segment-size %U",
527 unformat_memory_size, &tmp))
528 {
529 if (tmp >= 0x100000000ULL)
530 return clib_error_return
531 (0, "private segment size %lld (%llu) too large", tmp, tmp);
Florin Coras4399c2e2018-01-25 06:34:42 -0800532 pm->private_segment_size = tmp;
Dave Barach91f3e742017-09-01 19:12:11 -0400533 }
Florin Coras4399c2e2018-01-25 06:34:42 -0800534 else if (unformat (input, "server-uri %s", &pm->server_uri))
Florin Corasee7e1f52018-01-11 02:23:35 -0800535 ;
Florin Coras4399c2e2018-01-25 06:34:42 -0800536 else if (unformat (input, "client-uri %s", &pm->client_uri))
Florin Corasee7e1f52018-01-11 02:23:35 -0800537 ;
Dave Barach52851e62017-08-07 09:35:25 -0400538 else
539 return clib_error_return (0, "unknown input `%U'",
540 format_unformat_error, input);
541 }
542
Florin Coras4399c2e2018-01-25 06:34:42 -0800543 if (!pm->server_uri)
544 {
545 clib_warning ("No server-uri provided, Using default: %s",
546 default_server_uri);
547 pm->server_uri = format (0, "%s%c", default_server_uri, 0);
548 }
549 if (!pm->client_uri)
550 {
551 clib_warning ("No client-uri provided, Using default: %s",
552 default_client_uri);
553 pm->client_uri = format (0, "%s%c", default_client_uri, 0);
554 }
Florin Corasee7e1f52018-01-11 02:23:35 -0800555
Florin Coras4399c2e2018-01-25 06:34:42 -0800556 vnet_session_enable_disable (vm, 1 /* turn on session and transport */ );
Dave Barach52851e62017-08-07 09:35:25 -0400557
Florin Coras4399c2e2018-01-25 06:34:42 -0800558 rv = proxy_server_create (vm);
Dave Barach52851e62017-08-07 09:35:25 -0400559 switch (rv)
560 {
561 case 0:
562 break;
563 default:
564 return clib_error_return (0, "server_create returned %d", rv);
565 }
566
567 return 0;
568}
569
570/* *INDENT-OFF* */
Florin Coras4399c2e2018-01-25 06:34:42 -0800571VLIB_CLI_COMMAND (proxy_create_command, static) =
Dave Barach52851e62017-08-07 09:35:25 -0400572{
573 .path = "test proxy server",
Florin Corasee7e1f52018-01-11 02:23:35 -0800574 .short_help = "test proxy server [server-uri <tcp://ip/port>]"
575 "[client-uri <tcp://ip/port>][fifo-size <nn>][rcv-buf-size <nn>]"
576 "[prealloc-fifos <nn>][private-segment-size <mem>]"
577 "[private-segment-count <nn>]",
Dave Barach52851e62017-08-07 09:35:25 -0400578 .function = proxy_server_create_command_fn,
579};
580/* *INDENT-ON* */
581
582clib_error_t *
Florin Coras4399c2e2018-01-25 06:34:42 -0800583proxy_main_init (vlib_main_t * vm)
Dave Barach52851e62017-08-07 09:35:25 -0400584{
Florin Coras4399c2e2018-01-25 06:34:42 -0800585 proxy_main_t *pm = &proxy_main;
586 pm->server_client_index = ~0;
587 pm->active_open_client_index = ~0;
588 pm->proxy_session_by_active_open_handle = hash_create (0, sizeof (uword));
589 pm->proxy_session_by_server_handle = hash_create (0, sizeof (uword));
Dave Barach52851e62017-08-07 09:35:25 -0400590
591 return 0;
592}
593
Florin Coras4399c2e2018-01-25 06:34:42 -0800594VLIB_INIT_FUNCTION (proxy_main_init);
Dave Barach52851e62017-08-07 09:35:25 -0400595
596/*
597* fd.io coding-style-patch-verification: ON
598*
599* Local Variables:
600* eval: (c-set-style "gnu")
601* End:
602*/