blob: 2fdb63f1780265e7e1c0113b4e38556738a4185e [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_redirect_connect_callback (u32 client_index, void *mp)
Dave Barach52851e62017-08-07 09:35:25 -0400150{
151 clib_warning ("called...");
152 return -1;
153}
154
155static int
Florin Coras4399c2e2018-01-25 06:34:42 -0800156proxy_rx_callback (stream_session_t * s)
Dave Barach52851e62017-08-07 09:35:25 -0400157{
158 u32 max_dequeue;
159 int actual_transfer __attribute__ ((unused));
160 svm_fifo_t *tx_fifo, *rx_fifo;
Florin Coras4399c2e2018-01-25 06:34:42 -0800161 proxy_main_t *pm = &proxy_main;
Dave Barach52851e62017-08-07 09:35:25 -0400162 u32 thread_index = vlib_get_thread_index ();
163 vnet_connect_args_t _a, *a = &_a;
164 proxy_session_t *ps;
165 int proxy_index;
166 uword *p;
167 svm_fifo_t *active_open_tx_fifo;
168 session_fifo_event_t evt;
169
170 ASSERT (s->thread_index == thread_index);
171
Florin Coras4399c2e2018-01-25 06:34:42 -0800172 clib_spinlock_lock_if_init (&pm->sessions_lock);
173 p = hash_get (pm->proxy_session_by_server_handle, session_handle (s));
Dave Barach52851e62017-08-07 09:35:25 -0400174
175 if (PREDICT_TRUE (p != 0))
176 {
Florin Coras4399c2e2018-01-25 06:34:42 -0800177 clib_spinlock_unlock_if_init (&pm->sessions_lock);
Dave Barach52851e62017-08-07 09:35:25 -0400178 active_open_tx_fifo = s->server_rx_fifo;
179
180 /*
181 * Send event for active open tx fifo
182 */
183 if (svm_fifo_set_event (active_open_tx_fifo))
184 {
185 evt.fifo = active_open_tx_fifo;
186 evt.event_type = FIFO_EVENT_APP_TX;
Florin Corase86a8ed2018-01-05 03:20:25 -0800187 if (svm_queue_add
Florin Coras4399c2e2018-01-25 06:34:42 -0800188 (pm->active_open_event_queue[thread_index], (u8 *) & evt,
Dave Barach52851e62017-08-07 09:35:25 -0400189 0 /* do wait for mutex */ ))
190 clib_warning ("failed to enqueue tx evt");
191 }
192 }
193 else
194 {
195 rx_fifo = s->server_rx_fifo;
196 tx_fifo = s->server_tx_fifo;
197
198 ASSERT (rx_fifo->master_thread_index == thread_index);
199 ASSERT (tx_fifo->master_thread_index == thread_index);
200
201 max_dequeue = svm_fifo_max_dequeue (s->server_rx_fifo);
202
203 if (PREDICT_FALSE (max_dequeue == 0))
204 return 0;
205
206 actual_transfer = svm_fifo_peek (rx_fifo, 0 /* relative_offset */ ,
Florin Coras4399c2e2018-01-25 06:34:42 -0800207 max_dequeue, pm->rx_buf[thread_index]);
Dave Barach52851e62017-08-07 09:35:25 -0400208
209 /* $$$ your message in this space: parse url, etc. */
210
211 memset (a, 0, sizeof (*a));
212
Florin Coras4399c2e2018-01-25 06:34:42 -0800213 clib_spinlock_lock_if_init (&pm->sessions_lock);
214 pool_get (pm->sessions, ps);
Dave Barach52851e62017-08-07 09:35:25 -0400215 memset (ps, 0, sizeof (*ps));
216 ps->server_rx_fifo = rx_fifo;
217 ps->server_tx_fifo = tx_fifo;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700218 ps->vpp_server_handle = session_handle (s);
Dave Barach52851e62017-08-07 09:35:25 -0400219
Florin Coras4399c2e2018-01-25 06:34:42 -0800220 proxy_index = ps - pm->sessions;
Dave Barach52851e62017-08-07 09:35:25 -0400221
Florin Coras4399c2e2018-01-25 06:34:42 -0800222 hash_set (pm->proxy_session_by_server_handle, ps->vpp_server_handle,
Dave Barach52851e62017-08-07 09:35:25 -0400223 proxy_index);
224
Florin Coras4399c2e2018-01-25 06:34:42 -0800225 clib_spinlock_unlock_if_init (&pm->sessions_lock);
Dave Barach52851e62017-08-07 09:35:25 -0400226
Florin Coras4399c2e2018-01-25 06:34:42 -0800227 a->uri = (char *) pm->client_uri;
Dave Barach52851e62017-08-07 09:35:25 -0400228 a->api_context = proxy_index;
Florin Coras4399c2e2018-01-25 06:34:42 -0800229 a->app_index = pm->active_open_app_index;
Dave Barach52851e62017-08-07 09:35:25 -0400230 a->mp = 0;
231 vnet_connect_uri (a);
232 }
233
234 return 0;
235}
236
Florin Coras4399c2e2018-01-25 06:34:42 -0800237static session_cb_vft_t proxy_session_cb_vft = {
238 .session_accept_callback = proxy_accept_callback,
239 .session_disconnect_callback = proxy_disconnect_callback,
240 .session_connected_callback = proxy_connected_callback,
241 .add_segment_callback = proxy_add_segment_callback,
242 .redirect_connect_callback = proxy_redirect_connect_callback,
243 .builtin_server_rx_callback = proxy_rx_callback,
244 .session_reset_callback = proxy_reset_callback
Dave Barach52851e62017-08-07 09:35:25 -0400245};
246
247static int
248active_open_connected_callback (u32 app_index, u32 opaque,
249 stream_session_t * s, u8 is_fail)
250{
Florin Coras4399c2e2018-01-25 06:34:42 -0800251 proxy_main_t *pm = &proxy_main;
Dave Barach52851e62017-08-07 09:35:25 -0400252 proxy_session_t *ps;
253 u8 thread_index = vlib_get_thread_index ();
254 session_fifo_event_t evt;
255
256 if (is_fail)
257 {
258 clib_warning ("connection %d failed!", opaque);
259 return 0;
260 }
261
262 /*
263 * Setup proxy session handle.
264 */
Florin Coras4399c2e2018-01-25 06:34:42 -0800265 clib_spinlock_lock_if_init (&pm->sessions_lock);
Dave Barach52851e62017-08-07 09:35:25 -0400266
Florin Coras4399c2e2018-01-25 06:34:42 -0800267 ps = pool_elt_at_index (pm->sessions, opaque);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700268 ps->vpp_active_open_handle = session_handle (s);
Dave Barach52851e62017-08-07 09:35:25 -0400269
270 s->server_tx_fifo = ps->server_rx_fifo;
271 s->server_rx_fifo = ps->server_tx_fifo;
272
273 /*
274 * Reset the active-open tx-fifo master indices so the active-open session
275 * will receive data, etc.
276 */
277 s->server_tx_fifo->master_session_index = s->session_index;
278 s->server_tx_fifo->master_thread_index = s->thread_index;
279
280 /*
281 * Account for the active-open session's use of the fifos
282 * so they won't disappear until the last session which uses
283 * them disappears
284 */
285 s->server_tx_fifo->refcnt++;
286 s->server_rx_fifo->refcnt++;
287
Florin Coras4399c2e2018-01-25 06:34:42 -0800288 hash_set (pm->proxy_session_by_active_open_handle,
Dave Barach52851e62017-08-07 09:35:25 -0400289 ps->vpp_active_open_handle, opaque);
290
Florin Coras4399c2e2018-01-25 06:34:42 -0800291 clib_spinlock_unlock_if_init (&pm->sessions_lock);
Dave Barach52851e62017-08-07 09:35:25 -0400292
293 /*
294 * Send event for active open tx fifo
295 */
296 if (svm_fifo_set_event (s->server_tx_fifo))
297 {
298 evt.fifo = s->server_tx_fifo;
299 evt.event_type = FIFO_EVENT_APP_TX;
Florin Corase86a8ed2018-01-05 03:20:25 -0800300 if (svm_queue_add
Florin Coras4399c2e2018-01-25 06:34:42 -0800301 (pm->active_open_event_queue[thread_index], (u8 *) & evt,
Dave Barach52851e62017-08-07 09:35:25 -0400302 0 /* do wait for mutex */ ))
303 clib_warning ("failed to enqueue tx evt");
304 }
305
306 return 0;
307}
308
309static void
310active_open_reset_callback (stream_session_t * s)
311{
312 delete_proxy_session (s, 1 /* is_active_open */ );
313}
314
315static int
316active_open_create_callback (stream_session_t * s)
317{
318 return 0;
319}
320
321static void
322active_open_disconnect_callback (stream_session_t * s)
323{
324 delete_proxy_session (s, 1 /* is_active_open */ );
325}
326
327static int
328active_open_rx_callback (stream_session_t * s)
329{
Florin Coras4399c2e2018-01-25 06:34:42 -0800330 proxy_main_t *pm = &proxy_main;
Dave Barach52851e62017-08-07 09:35:25 -0400331 session_fifo_event_t evt;
332 svm_fifo_t *server_rx_fifo;
333 u32 thread_index = vlib_get_thread_index ();
334
335 server_rx_fifo = s->server_rx_fifo;
336
337 /*
338 * Send event for server tx fifo
339 */
340 if (svm_fifo_set_event (server_rx_fifo))
341 {
342 evt.fifo = server_rx_fifo;
343 evt.event_type = FIFO_EVENT_APP_TX;
Florin Corase86a8ed2018-01-05 03:20:25 -0800344 if (svm_queue_add
Florin Coras4399c2e2018-01-25 06:34:42 -0800345 (pm->server_event_queue[thread_index], (u8 *) & evt,
Dave Barach52851e62017-08-07 09:35:25 -0400346 0 /* do wait for mutex */ ))
347 clib_warning ("failed to enqueue server rx evt");
348 }
349
350 return 0;
351}
352
353/* *INDENT-OFF* */
Florin Coras4399c2e2018-01-25 06:34:42 -0800354static session_cb_vft_t active_open_clients = {
Dave Barach52851e62017-08-07 09:35:25 -0400355 .session_reset_callback = active_open_reset_callback,
356 .session_connected_callback = active_open_connected_callback,
357 .session_accept_callback = active_open_create_callback,
358 .session_disconnect_callback = active_open_disconnect_callback,
359 .builtin_server_rx_callback = active_open_rx_callback
360};
361/* *INDENT-ON* */
362
363
364static void
365create_api_loopbacks (vlib_main_t * vm)
366{
Florin Coras4399c2e2018-01-25 06:34:42 -0800367 proxy_main_t *pm = &proxy_main;
Dave Barach52851e62017-08-07 09:35:25 -0400368 api_main_t *am = &api_main;
369 vl_shmem_hdr_t *shmem_hdr;
370
371 shmem_hdr = am->shmem_hdr;
Florin Coras4399c2e2018-01-25 06:34:42 -0800372 pm->vl_input_queue = shmem_hdr->vl_input_queue;
373 pm->server_client_index =
374 vl_api_memclnt_create_internal ("proxy_server", pm->vl_input_queue);
375 pm->active_open_client_index =
376 vl_api_memclnt_create_internal ("proxy_active_open", pm->vl_input_queue);
Dave Barach52851e62017-08-07 09:35:25 -0400377}
378
379static int
Florin Coras4399c2e2018-01-25 06:34:42 -0800380proxy_server_attach ()
Dave Barach52851e62017-08-07 09:35:25 -0400381{
Florin Coras4399c2e2018-01-25 06:34:42 -0800382 proxy_main_t *pm = &proxy_main;
Florin Corasff6e7692017-12-11 04:59:01 -0800383 u64 options[APP_OPTIONS_N_OPTIONS];
Dave Barach52851e62017-08-07 09:35:25 -0400384 vnet_app_attach_args_t _a, *a = &_a;
Florin Corasff6e7692017-12-11 04:59:01 -0800385 u32 segment_size = 512 << 20;
Dave Barach52851e62017-08-07 09:35:25 -0400386
387 memset (a, 0, sizeof (*a));
388 memset (options, 0, sizeof (options));
389
Florin Coras4399c2e2018-01-25 06:34:42 -0800390 if (pm->private_segment_size)
391 segment_size = pm->private_segment_size;
392 a->api_client_index = pm->server_client_index;
393 a->session_cb_vft = &proxy_session_cb_vft;
Dave Barach52851e62017-08-07 09:35:25 -0400394 a->options = options;
Florin Corasff6e7692017-12-11 04:59:01 -0800395 a->options[APP_OPTIONS_SEGMENT_SIZE] = segment_size;
Florin Coras4399c2e2018-01-25 06:34:42 -0800396 a->options[APP_OPTIONS_RX_FIFO_SIZE] = pm->fifo_size;
397 a->options[APP_OPTIONS_TX_FIFO_SIZE] = pm->fifo_size;
398 a->options[APP_OPTIONS_PRIVATE_SEGMENT_COUNT] = pm->private_segment_count;
Dave Barach52851e62017-08-07 09:35:25 -0400399 a->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] =
Florin Coras4399c2e2018-01-25 06:34:42 -0800400 pm->prealloc_fifos ? pm->prealloc_fifos : 1;
Dave Barach52851e62017-08-07 09:35:25 -0400401
Florin Coras7999e832017-10-31 01:51:04 -0700402 a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
Dave Barach52851e62017-08-07 09:35:25 -0400403
Dave Barach52851e62017-08-07 09:35:25 -0400404 if (vnet_application_attach (a))
405 {
406 clib_warning ("failed to attach server");
407 return -1;
408 }
Florin Coras4399c2e2018-01-25 06:34:42 -0800409 pm->server_app_index = a->app_index;
Dave Barach52851e62017-08-07 09:35:25 -0400410
411 return 0;
412}
413
414static int
415active_open_attach (void)
416{
Florin Coras4399c2e2018-01-25 06:34:42 -0800417 proxy_main_t *pm = &proxy_main;
Dave Barach52851e62017-08-07 09:35:25 -0400418 vnet_app_attach_args_t _a, *a = &_a;
Dave Barach52851e62017-08-07 09:35:25 -0400419 u64 options[16];
420
Dave Barach52851e62017-08-07 09:35:25 -0400421 memset (a, 0, sizeof (*a));
422 memset (options, 0, sizeof (options));
423
Florin Coras4399c2e2018-01-25 06:34:42 -0800424 a->api_client_index = pm->active_open_client_index;
425 a->session_cb_vft = &active_open_clients;
Dave Barach52851e62017-08-07 09:35:25 -0400426
Florin Corasff6e7692017-12-11 04:59:01 -0800427 options[APP_OPTIONS_ACCEPT_COOKIE] = 0x12345678;
428 options[APP_OPTIONS_SEGMENT_SIZE] = 512 << 20;
Florin Coras4399c2e2018-01-25 06:34:42 -0800429 options[APP_OPTIONS_RX_FIFO_SIZE] = pm->fifo_size;
430 options[APP_OPTIONS_TX_FIFO_SIZE] = pm->fifo_size;
431 options[APP_OPTIONS_PRIVATE_SEGMENT_COUNT] = pm->private_segment_count;
Dave Barach52851e62017-08-07 09:35:25 -0400432 options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] =
Florin Coras4399c2e2018-01-25 06:34:42 -0800433 pm->prealloc_fifos ? pm->prealloc_fifos : 1;
Dave Barach52851e62017-08-07 09:35:25 -0400434
Florin Coras7999e832017-10-31 01:51:04 -0700435 options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN
Dave Barach52851e62017-08-07 09:35:25 -0400436 | APP_OPTIONS_FLAGS_IS_PROXY;
437
438 a->options = options;
439
440 if (vnet_application_attach (a))
441 return -1;
442
Florin Coras4399c2e2018-01-25 06:34:42 -0800443 pm->active_open_app_index = a->app_index;
Dave Barach52851e62017-08-07 09:35:25 -0400444
445 return 0;
446}
447
448static int
Florin Coras4399c2e2018-01-25 06:34:42 -0800449proxy_server_listen ()
Dave Barach52851e62017-08-07 09:35:25 -0400450{
Florin Coras4399c2e2018-01-25 06:34:42 -0800451 proxy_main_t *pm = &proxy_main;
Dave Barach52851e62017-08-07 09:35:25 -0400452 vnet_bind_args_t _a, *a = &_a;
453 memset (a, 0, sizeof (*a));
Florin Coras4399c2e2018-01-25 06:34:42 -0800454 a->app_index = pm->server_app_index;
455 a->uri = (char *) pm->server_uri;
Dave Barach52851e62017-08-07 09:35:25 -0400456 return vnet_bind_uri (a);
457}
458
459static int
Florin Coras4399c2e2018-01-25 06:34:42 -0800460proxy_server_create (vlib_main_t * vm)
Dave Barach52851e62017-08-07 09:35:25 -0400461{
Florin Coras4399c2e2018-01-25 06:34:42 -0800462 proxy_main_t *pm = &proxy_main;
Dave Barach52851e62017-08-07 09:35:25 -0400463 vlib_thread_main_t *vtm = vlib_get_thread_main ();
464 u32 num_threads;
465 int i;
466
Florin Coras4399c2e2018-01-25 06:34:42 -0800467 if (pm->server_client_index == (u32) ~ 0)
Dave Barach52851e62017-08-07 09:35:25 -0400468 create_api_loopbacks (vm);
469
470 num_threads = 1 /* main thread */ + vtm->n_threads;
Florin Coras4399c2e2018-01-25 06:34:42 -0800471 vec_validate (proxy_main.server_event_queue, num_threads - 1);
472 vec_validate (proxy_main.active_open_event_queue, num_threads - 1);
473 vec_validate (pm->rx_buf, num_threads - 1);
Dave Barach52851e62017-08-07 09:35:25 -0400474
475 for (i = 0; i < num_threads; i++)
Florin Coras4399c2e2018-01-25 06:34:42 -0800476 vec_validate (pm->rx_buf[i], pm->rcv_buffer_size);
Dave Barach52851e62017-08-07 09:35:25 -0400477
Florin Coras4399c2e2018-01-25 06:34:42 -0800478 if (proxy_server_attach ())
Dave Barach52851e62017-08-07 09:35:25 -0400479 {
480 clib_warning ("failed to attach server app");
481 return -1;
482 }
Florin Coras4399c2e2018-01-25 06:34:42 -0800483 if (proxy_server_listen ())
Dave Barach52851e62017-08-07 09:35:25 -0400484 {
485 clib_warning ("failed to start listening");
486 return -1;
487 }
488 if (active_open_attach ())
489 {
490 clib_warning ("failed to attach active open app");
491 return -1;
492 }
493
494 for (i = 0; i < num_threads; i++)
495 {
Florin Coras4399c2e2018-01-25 06:34:42 -0800496 pm->active_open_event_queue[i] =
Dave Barach52851e62017-08-07 09:35:25 -0400497 session_manager_get_vpp_event_queue (i);
498
Florin Coras4399c2e2018-01-25 06:34:42 -0800499 ASSERT (pm->active_open_event_queue[i]);
Dave Barach52851e62017-08-07 09:35:25 -0400500
Florin Coras4399c2e2018-01-25 06:34:42 -0800501 pm->server_event_queue[i] = session_manager_get_vpp_event_queue (i);
Dave Barach52851e62017-08-07 09:35:25 -0400502 }
503
504 return 0;
505}
506
507static clib_error_t *
508proxy_server_create_command_fn (vlib_main_t * vm, unformat_input_t * input,
509 vlib_cli_command_t * cmd)
510{
Florin Coras4399c2e2018-01-25 06:34:42 -0800511 proxy_main_t *pm = &proxy_main;
512 char *default_server_uri = "tcp://0.0.0.0/23";
513 char *default_client_uri = "tcp://6.0.2.2/23";
Dave Barach52851e62017-08-07 09:35:25 -0400514 int rv;
Dave Barach91f3e742017-09-01 19:12:11 -0400515 u64 tmp;
Dave Barach52851e62017-08-07 09:35:25 -0400516
Florin Coras4399c2e2018-01-25 06:34:42 -0800517 pm->fifo_size = 64 << 10;
518 pm->rcv_buffer_size = 1024;
519 pm->prealloc_fifos = 0;
520 pm->private_segment_count = 0;
521 pm->private_segment_size = 0;
522 pm->server_uri = 0;
Dave Barach52851e62017-08-07 09:35:25 -0400523
524 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
525 {
Florin Coras4399c2e2018-01-25 06:34:42 -0800526 if (unformat (input, "fifo-size %d", &pm->fifo_size))
527 pm->fifo_size <<= 10;
528 else if (unformat (input, "rcv-buf-size %d", &pm->rcv_buffer_size))
Dave Barach52851e62017-08-07 09:35:25 -0400529 ;
Florin Coras4399c2e2018-01-25 06:34:42 -0800530 else if (unformat (input, "prealloc-fifos %d", &pm->prealloc_fifos))
Dave Barach52851e62017-08-07 09:35:25 -0400531 ;
532 else if (unformat (input, "private-segment-count %d",
Florin Coras4399c2e2018-01-25 06:34:42 -0800533 &pm->private_segment_count))
Dave Barach52851e62017-08-07 09:35:25 -0400534 ;
Dave Barach91f3e742017-09-01 19:12:11 -0400535 else if (unformat (input, "private-segment-size %U",
536 unformat_memory_size, &tmp))
537 {
538 if (tmp >= 0x100000000ULL)
539 return clib_error_return
540 (0, "private segment size %lld (%llu) too large", tmp, tmp);
Florin Coras4399c2e2018-01-25 06:34:42 -0800541 pm->private_segment_size = tmp;
Dave Barach91f3e742017-09-01 19:12:11 -0400542 }
Florin Coras4399c2e2018-01-25 06:34:42 -0800543 else if (unformat (input, "server-uri %s", &pm->server_uri))
Florin Corasee7e1f52018-01-11 02:23:35 -0800544 ;
Florin Coras4399c2e2018-01-25 06:34:42 -0800545 else if (unformat (input, "client-uri %s", &pm->client_uri))
Florin Corasee7e1f52018-01-11 02:23:35 -0800546 ;
Dave Barach52851e62017-08-07 09:35:25 -0400547 else
548 return clib_error_return (0, "unknown input `%U'",
549 format_unformat_error, input);
550 }
551
Florin Coras4399c2e2018-01-25 06:34:42 -0800552 if (!pm->server_uri)
553 {
554 clib_warning ("No server-uri provided, Using default: %s",
555 default_server_uri);
556 pm->server_uri = format (0, "%s%c", default_server_uri, 0);
557 }
558 if (!pm->client_uri)
559 {
560 clib_warning ("No client-uri provided, Using default: %s",
561 default_client_uri);
562 pm->client_uri = format (0, "%s%c", default_client_uri, 0);
563 }
Florin Corasee7e1f52018-01-11 02:23:35 -0800564
Florin Coras4399c2e2018-01-25 06:34:42 -0800565 vnet_session_enable_disable (vm, 1 /* turn on session and transport */ );
Dave Barach52851e62017-08-07 09:35:25 -0400566
Florin Coras4399c2e2018-01-25 06:34:42 -0800567 rv = proxy_server_create (vm);
Dave Barach52851e62017-08-07 09:35:25 -0400568 switch (rv)
569 {
570 case 0:
571 break;
572 default:
573 return clib_error_return (0, "server_create returned %d", rv);
574 }
575
576 return 0;
577}
578
579/* *INDENT-OFF* */
Florin Coras4399c2e2018-01-25 06:34:42 -0800580VLIB_CLI_COMMAND (proxy_create_command, static) =
Dave Barach52851e62017-08-07 09:35:25 -0400581{
582 .path = "test proxy server",
Florin Corasee7e1f52018-01-11 02:23:35 -0800583 .short_help = "test proxy server [server-uri <tcp://ip/port>]"
584 "[client-uri <tcp://ip/port>][fifo-size <nn>][rcv-buf-size <nn>]"
585 "[prealloc-fifos <nn>][private-segment-size <mem>]"
586 "[private-segment-count <nn>]",
Dave Barach52851e62017-08-07 09:35:25 -0400587 .function = proxy_server_create_command_fn,
588};
589/* *INDENT-ON* */
590
591clib_error_t *
Florin Coras4399c2e2018-01-25 06:34:42 -0800592proxy_main_init (vlib_main_t * vm)
Dave Barach52851e62017-08-07 09:35:25 -0400593{
Florin Coras4399c2e2018-01-25 06:34:42 -0800594 proxy_main_t *pm = &proxy_main;
595 pm->server_client_index = ~0;
596 pm->active_open_client_index = ~0;
597 pm->proxy_session_by_active_open_handle = hash_create (0, sizeof (uword));
598 pm->proxy_session_by_server_handle = hash_create (0, sizeof (uword));
Dave Barach52851e62017-08-07 09:35:25 -0400599
600 return 0;
601}
602
Florin Coras4399c2e2018-01-25 06:34:42 -0800603VLIB_INIT_FUNCTION (proxy_main_init);
Dave Barach52851e62017-08-07 09:35:25 -0400604
605/*
606* fd.io coding-style-patch-verification: ON
607*
608* Local Variables:
609* eval: (c-set-style "gnu")
610* End:
611*/