blob: 6542c160f6324639fdde80f1a18b619893c9d355 [file] [log] [blame]
Dave Barach52851e62017-08-07 09:35:25 -04001/*
Florin Corasc5df8c72019-04-08 07:42:30 -07002* Copyright (c) 2017-2019 Cisco and/or its affiliates.
Dave Barach52851e62017-08-07 09:35:25 -04003* 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
Florin Coras288eaab2019-02-03 15:26:14 -080062delete_proxy_session (session_t * s, int is_active_open)
Dave Barach52851e62017-08-07 09:35:25 -040063{
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;
Florin Coras288eaab2019-02-03 15:26:14 -080067 session_t *active_open_session = 0;
68 session_t *server_session = 0;
Dave Barach52851e62017-08-07 09:35:25 -040069 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 Coras288eaab2019-02-03 15:26:14 -0800146proxy_accept_callback (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 Coras288eaab2019-02-03 15:26:14 -0800158proxy_disconnect_callback (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 Coras288eaab2019-02-03 15:26:14 -0800164proxy_reset_callback (session_t * s)
Dave Barach52851e62017-08-07 09:35:25 -0400165{
Florin Coras31c99552019-03-01 13:00:58 -0800166 clib_warning ("Reset session %U", format_session, s, 2);
Dave Barach52851e62017-08-07 09:35:25 -0400167 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,
Florin Coras288eaab2019-02-03 15:26:14 -0800172 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 Coras288eaab2019-02-03 15:26:14 -0800186proxy_rx_callback (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);
Florin Coras288eaab2019-02-03 15:26:14 -0800207 active_open_tx_fifo = s->rx_fifo;
Dave Barach52851e62017-08-07 09:35:25 -0400208
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 Corasc0737e92019-03-04 14:19:39 -0800215 u32 ao_session_index = active_open_tx_fifo->master_session_index;
216 if (session_send_io_evt_to_thread_custom (&ao_session_index,
Florin Coras3c2fed52018-07-04 04:15:05 -0700217 ao_thread_index,
Florin Corasf6c43132019-03-01 12:41:21 -0800218 SESSION_IO_EVT_TX))
Dave Barach52851e62017-08-07 09:35:25 -0400219 clib_warning ("failed to enqueue tx evt");
220 }
221 }
222 else
223 {
Florin Coras288eaab2019-02-03 15:26:14 -0800224 rx_fifo = s->rx_fifo;
225 tx_fifo = s->tx_fifo;
Dave Barach52851e62017-08-07 09:35:25 -0400226
227 ASSERT (rx_fifo->master_thread_index == thread_index);
228 ASSERT (tx_fifo->master_thread_index == thread_index);
229
Sirshak Das28aa5392019-02-05 01:33:33 -0600230 max_dequeue = svm_fifo_max_dequeue_cons (s->rx_fifo);
Dave Barach52851e62017-08-07 09:35:25 -0400231
232 if (PREDICT_FALSE (max_dequeue == 0))
233 return 0;
234
235 actual_transfer = svm_fifo_peek (rx_fifo, 0 /* relative_offset */ ,
Florin Coras4399c2e2018-01-25 06:34:42 -0800236 max_dequeue, pm->rx_buf[thread_index]);
Dave Barach52851e62017-08-07 09:35:25 -0400237
238 /* $$$ your message in this space: parse url, etc. */
239
Dave Barachb7b92992018-10-17 10:38:51 -0400240 clib_memset (a, 0, sizeof (*a));
Dave Barach52851e62017-08-07 09:35:25 -0400241
Florin Coras4399c2e2018-01-25 06:34:42 -0800242 clib_spinlock_lock_if_init (&pm->sessions_lock);
243 pool_get (pm->sessions, ps);
Dave Barachb7b92992018-10-17 10:38:51 -0400244 clib_memset (ps, 0, sizeof (*ps));
Dave Barach52851e62017-08-07 09:35:25 -0400245 ps->server_rx_fifo = rx_fifo;
246 ps->server_tx_fifo = tx_fifo;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700247 ps->vpp_server_handle = session_handle (s);
Dave Barach52851e62017-08-07 09:35:25 -0400248
Florin Coras4399c2e2018-01-25 06:34:42 -0800249 proxy_index = ps - pm->sessions;
Dave Barach52851e62017-08-07 09:35:25 -0400250
Florin Coras4399c2e2018-01-25 06:34:42 -0800251 hash_set (pm->proxy_session_by_server_handle, ps->vpp_server_handle,
Dave Barach52851e62017-08-07 09:35:25 -0400252 proxy_index);
253
Florin Coras4399c2e2018-01-25 06:34:42 -0800254 clib_spinlock_unlock_if_init (&pm->sessions_lock);
Dave Barach52851e62017-08-07 09:35:25 -0400255
Florin Coras4399c2e2018-01-25 06:34:42 -0800256 a->uri = (char *) pm->client_uri;
Dave Barach52851e62017-08-07 09:35:25 -0400257 a->api_context = proxy_index;
Florin Coras4399c2e2018-01-25 06:34:42 -0800258 a->app_index = pm->active_open_app_index;
Florin Corascd330c82018-05-09 17:33:00 -0700259 proxy_call_main_thread (a);
Dave Barach52851e62017-08-07 09:35:25 -0400260 }
261
262 return 0;
263}
264
Florin Coras4399c2e2018-01-25 06:34:42 -0800265static session_cb_vft_t proxy_session_cb_vft = {
266 .session_accept_callback = proxy_accept_callback,
267 .session_disconnect_callback = proxy_disconnect_callback,
268 .session_connected_callback = proxy_connected_callback,
269 .add_segment_callback = proxy_add_segment_callback,
Florin Coras371ca502018-02-21 12:07:41 -0800270 .builtin_app_rx_callback = proxy_rx_callback,
Florin Coras4399c2e2018-01-25 06:34:42 -0800271 .session_reset_callback = proxy_reset_callback
Dave Barach52851e62017-08-07 09:35:25 -0400272};
273
274static int
275active_open_connected_callback (u32 app_index, u32 opaque,
Florin Coras288eaab2019-02-03 15:26:14 -0800276 session_t * s, u8 is_fail)
Dave Barach52851e62017-08-07 09:35:25 -0400277{
Florin Coras4399c2e2018-01-25 06:34:42 -0800278 proxy_main_t *pm = &proxy_main;
Dave Barach52851e62017-08-07 09:35:25 -0400279 proxy_session_t *ps;
280 u8 thread_index = vlib_get_thread_index ();
Dave Barach52851e62017-08-07 09:35:25 -0400281
282 if (is_fail)
283 {
284 clib_warning ("connection %d failed!", opaque);
285 return 0;
286 }
287
288 /*
289 * Setup proxy session handle.
290 */
Florin Coras4399c2e2018-01-25 06:34:42 -0800291 clib_spinlock_lock_if_init (&pm->sessions_lock);
Dave Barach52851e62017-08-07 09:35:25 -0400292
Florin Coras4399c2e2018-01-25 06:34:42 -0800293 ps = pool_elt_at_index (pm->sessions, opaque);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700294 ps->vpp_active_open_handle = session_handle (s);
Dave Barach52851e62017-08-07 09:35:25 -0400295
Florin Coras288eaab2019-02-03 15:26:14 -0800296 s->tx_fifo = ps->server_rx_fifo;
297 s->rx_fifo = ps->server_tx_fifo;
Dave Barach52851e62017-08-07 09:35:25 -0400298
299 /*
300 * Reset the active-open tx-fifo master indices so the active-open session
301 * will receive data, etc.
302 */
Florin Coras288eaab2019-02-03 15:26:14 -0800303 s->tx_fifo->master_session_index = s->session_index;
304 s->tx_fifo->master_thread_index = s->thread_index;
Dave Barach52851e62017-08-07 09:35:25 -0400305
306 /*
307 * Account for the active-open session's use of the fifos
308 * so they won't disappear until the last session which uses
309 * them disappears
310 */
Florin Coras288eaab2019-02-03 15:26:14 -0800311 s->tx_fifo->refcnt++;
312 s->rx_fifo->refcnt++;
Dave Barach52851e62017-08-07 09:35:25 -0400313
Florin Coras4399c2e2018-01-25 06:34:42 -0800314 hash_set (pm->proxy_session_by_active_open_handle,
Dave Barach52851e62017-08-07 09:35:25 -0400315 ps->vpp_active_open_handle, opaque);
316
Florin Coras4399c2e2018-01-25 06:34:42 -0800317 clib_spinlock_unlock_if_init (&pm->sessions_lock);
Dave Barach52851e62017-08-07 09:35:25 -0400318
319 /*
320 * Send event for active open tx fifo
321 */
Florin Coras3c2fed52018-07-04 04:15:05 -0700322 ASSERT (s->thread_index == thread_index);
Florin Coras288eaab2019-02-03 15:26:14 -0800323 if (svm_fifo_set_event (s->tx_fifo))
Florin Corasf6c43132019-03-01 12:41:21 -0800324 session_send_io_evt_to_thread (s->tx_fifo, SESSION_IO_EVT_TX);
Dave Barach52851e62017-08-07 09:35:25 -0400325
326 return 0;
327}
328
329static void
Florin Coras288eaab2019-02-03 15:26:14 -0800330active_open_reset_callback (session_t * s)
Dave Barach52851e62017-08-07 09:35:25 -0400331{
332 delete_proxy_session (s, 1 /* is_active_open */ );
333}
334
335static int
Florin Coras288eaab2019-02-03 15:26:14 -0800336active_open_create_callback (session_t * s)
Dave Barach52851e62017-08-07 09:35:25 -0400337{
338 return 0;
339}
340
341static void
Florin Coras288eaab2019-02-03 15:26:14 -0800342active_open_disconnect_callback (session_t * s)
Dave Barach52851e62017-08-07 09:35:25 -0400343{
344 delete_proxy_session (s, 1 /* is_active_open */ );
345}
346
347static int
Florin Coras288eaab2019-02-03 15:26:14 -0800348active_open_rx_callback (session_t * s)
Dave Barach52851e62017-08-07 09:35:25 -0400349{
Florin Corascd330c82018-05-09 17:33:00 -0700350 svm_fifo_t *proxy_tx_fifo;
Dave Barach52851e62017-08-07 09:35:25 -0400351
Florin Coras288eaab2019-02-03 15:26:14 -0800352 proxy_tx_fifo = s->rx_fifo;
Dave Barach52851e62017-08-07 09:35:25 -0400353
354 /*
355 * Send event for server tx fifo
356 */
Florin Corascd330c82018-05-09 17:33:00 -0700357 if (svm_fifo_set_event (proxy_tx_fifo))
Dave Barach52851e62017-08-07 09:35:25 -0400358 {
Florin Coras3c2fed52018-07-04 04:15:05 -0700359 u8 thread_index = proxy_tx_fifo->master_thread_index;
Florin Corasc0737e92019-03-04 14:19:39 -0800360 u32 session_index = proxy_tx_fifo->master_session_index;
361 return session_send_io_evt_to_thread_custom (&session_index,
Florin Coras3c2fed52018-07-04 04:15:05 -0700362 thread_index,
Florin Corasf6c43132019-03-01 12:41:21 -0800363 SESSION_IO_EVT_TX);
Dave Barach52851e62017-08-07 09:35:25 -0400364 }
365
366 return 0;
367}
368
369/* *INDENT-OFF* */
Florin Coras4399c2e2018-01-25 06:34:42 -0800370static session_cb_vft_t active_open_clients = {
Dave Barach52851e62017-08-07 09:35:25 -0400371 .session_reset_callback = active_open_reset_callback,
372 .session_connected_callback = active_open_connected_callback,
373 .session_accept_callback = active_open_create_callback,
374 .session_disconnect_callback = active_open_disconnect_callback,
Florin Coras371ca502018-02-21 12:07:41 -0800375 .builtin_app_rx_callback = active_open_rx_callback
Dave Barach52851e62017-08-07 09:35:25 -0400376};
377/* *INDENT-ON* */
378
379
380static void
381create_api_loopbacks (vlib_main_t * vm)
382{
Florin Coras4399c2e2018-01-25 06:34:42 -0800383 proxy_main_t *pm = &proxy_main;
Dave Barach52851e62017-08-07 09:35:25 -0400384 api_main_t *am = &api_main;
385 vl_shmem_hdr_t *shmem_hdr;
386
387 shmem_hdr = am->shmem_hdr;
Florin Coras4399c2e2018-01-25 06:34:42 -0800388 pm->vl_input_queue = shmem_hdr->vl_input_queue;
389 pm->server_client_index =
390 vl_api_memclnt_create_internal ("proxy_server", pm->vl_input_queue);
391 pm->active_open_client_index =
392 vl_api_memclnt_create_internal ("proxy_active_open", pm->vl_input_queue);
Dave Barach52851e62017-08-07 09:35:25 -0400393}
394
395static int
Florin Coras4399c2e2018-01-25 06:34:42 -0800396proxy_server_attach ()
Dave Barach52851e62017-08-07 09:35:25 -0400397{
Florin Coras4399c2e2018-01-25 06:34:42 -0800398 proxy_main_t *pm = &proxy_main;
Florin Corasff6e7692017-12-11 04:59:01 -0800399 u64 options[APP_OPTIONS_N_OPTIONS];
Dave Barach52851e62017-08-07 09:35:25 -0400400 vnet_app_attach_args_t _a, *a = &_a;
Florin Corasff6e7692017-12-11 04:59:01 -0800401 u32 segment_size = 512 << 20;
Dave Barach52851e62017-08-07 09:35:25 -0400402
Dave Barachb7b92992018-10-17 10:38:51 -0400403 clib_memset (a, 0, sizeof (*a));
404 clib_memset (options, 0, sizeof (options));
Dave Barach52851e62017-08-07 09:35:25 -0400405
Florin Coras4399c2e2018-01-25 06:34:42 -0800406 if (pm->private_segment_size)
407 segment_size = pm->private_segment_size;
408 a->api_client_index = pm->server_client_index;
409 a->session_cb_vft = &proxy_session_cb_vft;
Dave Barach52851e62017-08-07 09:35:25 -0400410 a->options = options;
Florin Corasff6e7692017-12-11 04:59:01 -0800411 a->options[APP_OPTIONS_SEGMENT_SIZE] = segment_size;
Florin Coras4399c2e2018-01-25 06:34:42 -0800412 a->options[APP_OPTIONS_RX_FIFO_SIZE] = pm->fifo_size;
413 a->options[APP_OPTIONS_TX_FIFO_SIZE] = pm->fifo_size;
414 a->options[APP_OPTIONS_PRIVATE_SEGMENT_COUNT] = pm->private_segment_count;
Dave Barach52851e62017-08-07 09:35:25 -0400415 a->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] =
Florin Coras25579b42018-06-06 17:55:02 -0700416 pm->prealloc_fifos ? pm->prealloc_fifos : 0;
Dave Barach52851e62017-08-07 09:35:25 -0400417
Florin Coras7999e832017-10-31 01:51:04 -0700418 a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
Dave Barach52851e62017-08-07 09:35:25 -0400419
Dave Barach52851e62017-08-07 09:35:25 -0400420 if (vnet_application_attach (a))
421 {
422 clib_warning ("failed to attach server");
423 return -1;
424 }
Florin Coras4399c2e2018-01-25 06:34:42 -0800425 pm->server_app_index = a->app_index;
Dave Barach52851e62017-08-07 09:35:25 -0400426
427 return 0;
428}
429
430static int
431active_open_attach (void)
432{
Florin Coras4399c2e2018-01-25 06:34:42 -0800433 proxy_main_t *pm = &proxy_main;
Dave Barach52851e62017-08-07 09:35:25 -0400434 vnet_app_attach_args_t _a, *a = &_a;
Dave Barach52851e62017-08-07 09:35:25 -0400435 u64 options[16];
436
Dave Barachb7b92992018-10-17 10:38:51 -0400437 clib_memset (a, 0, sizeof (*a));
438 clib_memset (options, 0, sizeof (options));
Dave Barach52851e62017-08-07 09:35:25 -0400439
Florin Coras4399c2e2018-01-25 06:34:42 -0800440 a->api_client_index = pm->active_open_client_index;
441 a->session_cb_vft = &active_open_clients;
Dave Barach52851e62017-08-07 09:35:25 -0400442
Florin Corasff6e7692017-12-11 04:59:01 -0800443 options[APP_OPTIONS_ACCEPT_COOKIE] = 0x12345678;
444 options[APP_OPTIONS_SEGMENT_SIZE] = 512 << 20;
Florin Coras4399c2e2018-01-25 06:34:42 -0800445 options[APP_OPTIONS_RX_FIFO_SIZE] = pm->fifo_size;
446 options[APP_OPTIONS_TX_FIFO_SIZE] = pm->fifo_size;
447 options[APP_OPTIONS_PRIVATE_SEGMENT_COUNT] = pm->private_segment_count;
Dave Barach52851e62017-08-07 09:35:25 -0400448 options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] =
Florin Coras25579b42018-06-06 17:55:02 -0700449 pm->prealloc_fifos ? pm->prealloc_fifos : 0;
Dave Barach52851e62017-08-07 09:35:25 -0400450
Florin Coras7999e832017-10-31 01:51:04 -0700451 options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN
Dave Barach52851e62017-08-07 09:35:25 -0400452 | APP_OPTIONS_FLAGS_IS_PROXY;
453
454 a->options = options;
455
456 if (vnet_application_attach (a))
457 return -1;
458
Florin Coras4399c2e2018-01-25 06:34:42 -0800459 pm->active_open_app_index = a->app_index;
Dave Barach52851e62017-08-07 09:35:25 -0400460
461 return 0;
462}
463
464static int
Florin Coras4399c2e2018-01-25 06:34:42 -0800465proxy_server_listen ()
Dave Barach52851e62017-08-07 09:35:25 -0400466{
Florin Coras4399c2e2018-01-25 06:34:42 -0800467 proxy_main_t *pm = &proxy_main;
Florin Corasc9940fc2019-02-05 20:55:11 -0800468 vnet_listen_args_t _a, *a = &_a;
Dave Barachb7b92992018-10-17 10:38:51 -0400469 clib_memset (a, 0, sizeof (*a));
Florin Coras4399c2e2018-01-25 06:34:42 -0800470 a->app_index = pm->server_app_index;
471 a->uri = (char *) pm->server_uri;
Dave Barach52851e62017-08-07 09:35:25 -0400472 return vnet_bind_uri (a);
473}
474
475static int
Florin Coras4399c2e2018-01-25 06:34:42 -0800476proxy_server_create (vlib_main_t * vm)
Dave Barach52851e62017-08-07 09:35:25 -0400477{
Florin Coras4399c2e2018-01-25 06:34:42 -0800478 proxy_main_t *pm = &proxy_main;
Dave Barach52851e62017-08-07 09:35:25 -0400479 vlib_thread_main_t *vtm = vlib_get_thread_main ();
480 u32 num_threads;
481 int i;
482
Florin Coras4399c2e2018-01-25 06:34:42 -0800483 if (pm->server_client_index == (u32) ~ 0)
Dave Barach52851e62017-08-07 09:35:25 -0400484 create_api_loopbacks (vm);
485
486 num_threads = 1 /* main thread */ + vtm->n_threads;
Florin Coras4399c2e2018-01-25 06:34:42 -0800487 vec_validate (proxy_main.server_event_queue, num_threads - 1);
488 vec_validate (proxy_main.active_open_event_queue, num_threads - 1);
489 vec_validate (pm->rx_buf, num_threads - 1);
Dave Barach52851e62017-08-07 09:35:25 -0400490
491 for (i = 0; i < num_threads; i++)
Florin Coras4399c2e2018-01-25 06:34:42 -0800492 vec_validate (pm->rx_buf[i], pm->rcv_buffer_size);
Dave Barach52851e62017-08-07 09:35:25 -0400493
Florin Coras4399c2e2018-01-25 06:34:42 -0800494 if (proxy_server_attach ())
Dave Barach52851e62017-08-07 09:35:25 -0400495 {
496 clib_warning ("failed to attach server app");
497 return -1;
498 }
Florin Coras4399c2e2018-01-25 06:34:42 -0800499 if (proxy_server_listen ())
Dave Barach52851e62017-08-07 09:35:25 -0400500 {
501 clib_warning ("failed to start listening");
502 return -1;
503 }
504 if (active_open_attach ())
505 {
506 clib_warning ("failed to attach active open app");
507 return -1;
508 }
509
510 for (i = 0; i < num_threads; i++)
511 {
Florin Coras31c99552019-03-01 13:00:58 -0800512 pm->active_open_event_queue[i] = session_main_get_vpp_event_queue (i);
Dave Barach52851e62017-08-07 09:35:25 -0400513
Florin Coras4399c2e2018-01-25 06:34:42 -0800514 ASSERT (pm->active_open_event_queue[i]);
Dave Barach52851e62017-08-07 09:35:25 -0400515
Florin Coras31c99552019-03-01 13:00:58 -0800516 pm->server_event_queue[i] = session_main_get_vpp_event_queue (i);
Dave Barach52851e62017-08-07 09:35:25 -0400517 }
518
519 return 0;
520}
521
522static clib_error_t *
523proxy_server_create_command_fn (vlib_main_t * vm, unformat_input_t * input,
524 vlib_cli_command_t * cmd)
525{
Florin Coras4399c2e2018-01-25 06:34:42 -0800526 proxy_main_t *pm = &proxy_main;
527 char *default_server_uri = "tcp://0.0.0.0/23";
528 char *default_client_uri = "tcp://6.0.2.2/23";
Dave Barach52851e62017-08-07 09:35:25 -0400529 int rv;
Dave Barach91f3e742017-09-01 19:12:11 -0400530 u64 tmp;
Dave Barach52851e62017-08-07 09:35:25 -0400531
Florin Coras4399c2e2018-01-25 06:34:42 -0800532 pm->fifo_size = 64 << 10;
533 pm->rcv_buffer_size = 1024;
534 pm->prealloc_fifos = 0;
535 pm->private_segment_count = 0;
536 pm->private_segment_size = 0;
537 pm->server_uri = 0;
Dave Barach52851e62017-08-07 09:35:25 -0400538
539 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
540 {
Florin Coras4399c2e2018-01-25 06:34:42 -0800541 if (unformat (input, "fifo-size %d", &pm->fifo_size))
542 pm->fifo_size <<= 10;
543 else if (unformat (input, "rcv-buf-size %d", &pm->rcv_buffer_size))
Dave Barach52851e62017-08-07 09:35:25 -0400544 ;
Florin Coras4399c2e2018-01-25 06:34:42 -0800545 else if (unformat (input, "prealloc-fifos %d", &pm->prealloc_fifos))
Dave Barach52851e62017-08-07 09:35:25 -0400546 ;
547 else if (unformat (input, "private-segment-count %d",
Florin Coras4399c2e2018-01-25 06:34:42 -0800548 &pm->private_segment_count))
Dave Barach52851e62017-08-07 09:35:25 -0400549 ;
Dave Barach91f3e742017-09-01 19:12:11 -0400550 else if (unformat (input, "private-segment-size %U",
551 unformat_memory_size, &tmp))
552 {
553 if (tmp >= 0x100000000ULL)
554 return clib_error_return
555 (0, "private segment size %lld (%llu) too large", tmp, tmp);
Florin Coras4399c2e2018-01-25 06:34:42 -0800556 pm->private_segment_size = tmp;
Dave Barach91f3e742017-09-01 19:12:11 -0400557 }
Florin Coras4399c2e2018-01-25 06:34:42 -0800558 else if (unformat (input, "server-uri %s", &pm->server_uri))
Florin Corasee7e1f52018-01-11 02:23:35 -0800559 ;
Florin Coras4399c2e2018-01-25 06:34:42 -0800560 else if (unformat (input, "client-uri %s", &pm->client_uri))
Florin Corascd330c82018-05-09 17:33:00 -0700561 pm->client_uri = format (0, "%s%c", pm->client_uri, 0);
Dave Barach52851e62017-08-07 09:35:25 -0400562 else
563 return clib_error_return (0, "unknown input `%U'",
564 format_unformat_error, input);
565 }
566
Florin Coras4399c2e2018-01-25 06:34:42 -0800567 if (!pm->server_uri)
568 {
569 clib_warning ("No server-uri provided, Using default: %s",
570 default_server_uri);
571 pm->server_uri = format (0, "%s%c", default_server_uri, 0);
572 }
573 if (!pm->client_uri)
574 {
575 clib_warning ("No client-uri provided, Using default: %s",
576 default_client_uri);
577 pm->client_uri = format (0, "%s%c", default_client_uri, 0);
578 }
Florin Corasee7e1f52018-01-11 02:23:35 -0800579
Florin Coras4399c2e2018-01-25 06:34:42 -0800580 vnet_session_enable_disable (vm, 1 /* turn on session and transport */ );
Dave Barach52851e62017-08-07 09:35:25 -0400581
Florin Coras4399c2e2018-01-25 06:34:42 -0800582 rv = proxy_server_create (vm);
Dave Barach52851e62017-08-07 09:35:25 -0400583 switch (rv)
584 {
585 case 0:
586 break;
587 default:
588 return clib_error_return (0, "server_create returned %d", rv);
589 }
590
591 return 0;
592}
593
594/* *INDENT-OFF* */
Florin Coras4399c2e2018-01-25 06:34:42 -0800595VLIB_CLI_COMMAND (proxy_create_command, static) =
Dave Barach52851e62017-08-07 09:35:25 -0400596{
597 .path = "test proxy server",
Florin Corasee7e1f52018-01-11 02:23:35 -0800598 .short_help = "test proxy server [server-uri <tcp://ip/port>]"
599 "[client-uri <tcp://ip/port>][fifo-size <nn>][rcv-buf-size <nn>]"
600 "[prealloc-fifos <nn>][private-segment-size <mem>]"
601 "[private-segment-count <nn>]",
Dave Barach52851e62017-08-07 09:35:25 -0400602 .function = proxy_server_create_command_fn,
603};
604/* *INDENT-ON* */
605
606clib_error_t *
Florin Coras4399c2e2018-01-25 06:34:42 -0800607proxy_main_init (vlib_main_t * vm)
Dave Barach52851e62017-08-07 09:35:25 -0400608{
Florin Coras4399c2e2018-01-25 06:34:42 -0800609 proxy_main_t *pm = &proxy_main;
610 pm->server_client_index = ~0;
611 pm->active_open_client_index = ~0;
612 pm->proxy_session_by_active_open_handle = hash_create (0, sizeof (uword));
613 pm->proxy_session_by_server_handle = hash_create (0, sizeof (uword));
Dave Barach52851e62017-08-07 09:35:25 -0400614
615 return 0;
616}
617
Florin Coras4399c2e2018-01-25 06:34:42 -0800618VLIB_INIT_FUNCTION (proxy_main_init);
Dave Barach52851e62017-08-07 09:35:25 -0400619
620/*
621* fd.io coding-style-patch-verification: ON
622*
623* Local Variables:
624* eval: (c-set-style "gnu")
625* End:
626*/