blob: 64fc4a7111aa54fe392deb59625cc9c7b5c4a88f [file] [log] [blame]
Florin Corase04c2992017-03-01 08:17:34 -08001/*
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>
20
Florin Coras6cf30ad2017-04-04 23:08:23 -070021/* define message IDs */
22#include <vpp/api/vpe_msg_enum.h>
23
24/* define message structures */
25#define vl_typedefs
26#include <vpp/api/vpe_all_api_h.h>
27#undef vl_typedefs
28
29/* define generated endian-swappers */
30#define vl_endianfun
31#include <vpp/api/vpe_all_api_h.h>
32#undef vl_endianfun
33
34/* instantiate all the print functions we know about */
35#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
36#define vl_printfun
37#include <vpp/api/vpe_all_api_h.h>
38#undef vl_printfun
39
Florin Corasd79b41e2017-03-04 05:37:52 -080040typedef struct
41{
42 u8 *rx_buf;
43 unix_shared_memory_queue_t **vpp_queue;
Florin Coras6cf30ad2017-04-04 23:08:23 -070044 u64 byte_index;
45
46 /* Sever's event queue */
47 unix_shared_memory_queue_t *vl_input_queue;
48
49 /* API client handle */
50 u32 my_client_index;
51
52 u32 app_index;
53
54 /* process node index for evnt scheduling */
55 u32 node_index;
Florin Corasd79b41e2017-03-04 05:37:52 -080056 vlib_main_t *vlib_main;
57} builtin_server_main_t;
58
59builtin_server_main_t builtin_server_main;
60
Florin Corase04c2992017-03-01 08:17:34 -080061int
62builtin_session_accept_callback (stream_session_t * s)
63{
Florin Corasd79b41e2017-03-04 05:37:52 -080064 builtin_server_main_t *bsm = &builtin_server_main;
Florin Corase04c2992017-03-01 08:17:34 -080065 clib_warning ("called...");
Florin Corasd79b41e2017-03-04 05:37:52 -080066
67 bsm->vpp_queue[s->thread_index] =
68 session_manager_get_vpp_event_queue (s->thread_index);
Florin Corase04c2992017-03-01 08:17:34 -080069 s->session_state = SESSION_STATE_READY;
Florin Coras6792ec02017-03-13 03:49:51 -070070 bsm->byte_index = 0;
Florin Corase04c2992017-03-01 08:17:34 -080071 return 0;
72}
73
74void
75builtin_session_disconnect_callback (stream_session_t * s)
76{
Florin Coras6cf30ad2017-04-04 23:08:23 -070077 builtin_server_main_t *bsm = &builtin_server_main;
78 vnet_disconnect_args_t _a, *a = &_a;
Florin Corase04c2992017-03-01 08:17:34 -080079 clib_warning ("called...");
Florin Corasd79b41e2017-03-04 05:37:52 -080080
Florin Coras6cf30ad2017-04-04 23:08:23 -070081 a->handle = stream_session_handle (s);
82 a->app_index = bsm->app_index;
83 vnet_disconnect_session (a);
Florin Corase04c2992017-03-01 08:17:34 -080084}
85
Florin Corasd79b41e2017-03-04 05:37:52 -080086void
87builtin_session_reset_callback (stream_session_t * s)
88{
89 clib_warning ("called.. ");
90
91 stream_session_cleanup (s);
92}
93
94
Florin Corase04c2992017-03-01 08:17:34 -080095int
Florin Coras6cf30ad2017-04-04 23:08:23 -070096builtin_session_connected_callback (u32 app_index, u32 api_context,
Florin Corase04c2992017-03-01 08:17:34 -080097 stream_session_t * s, u8 is_fail)
98{
99 clib_warning ("called...");
100 return -1;
101}
102
103int
104builtin_add_segment_callback (u32 client_index,
105 const u8 * seg_name, u32 seg_size)
106{
107 clib_warning ("called...");
108 return -1;
109}
110
111int
112builtin_redirect_connect_callback (u32 client_index, void *mp)
113{
114 clib_warning ("called...");
115 return -1;
116}
117
Florin Coras6792ec02017-03-13 03:49:51 -0700118void
119test_bytes (builtin_server_main_t * bsm, int actual_transfer)
Florin Corase04c2992017-03-01 08:17:34 -0800120{
Florin Coras6792ec02017-03-13 03:49:51 -0700121 int i;
122
123 for (i = 0; i < actual_transfer; i++)
124 {
125 if (bsm->rx_buf[i] != ((bsm->byte_index + i) & 0xff))
126 {
Florin Coras6cf30ad2017-04-04 23:08:23 -0700127 clib_warning ("at %lld expected %d got %d", bsm->byte_index + i,
Florin Coras6792ec02017-03-13 03:49:51 -0700128 (bsm->byte_index + i) & 0xff, bsm->rx_buf[i]);
129 }
130 }
131 bsm->byte_index += actual_transfer;
132}
133
134int
135builtin_server_rx_callback (stream_session_t * s)
136{
137 u32 n_written, max_dequeue, max_enqueue, max_transfer;
138 int actual_transfer;
139 svm_fifo_t *tx_fifo, *rx_fifo;
Florin Corasd79b41e2017-03-04 05:37:52 -0800140 builtin_server_main_t *bsm = &builtin_server_main;
141 session_fifo_event_t evt;
142 static int serial_number = 0;
143
Dave Barachacd2a6a2017-05-16 17:41:34 -0400144 tx_fifo = s->server_tx_fifo;
145 rx_fifo = s->server_rx_fifo;
146
Florin Coras6792ec02017-03-13 03:49:51 -0700147 max_dequeue = svm_fifo_max_dequeue (s->server_rx_fifo);
148 max_enqueue = svm_fifo_max_enqueue (s->server_tx_fifo);
149
150 if (PREDICT_FALSE (max_dequeue == 0))
Dave Barachacd2a6a2017-05-16 17:41:34 -0400151 return 0;
Florin Corasd79b41e2017-03-04 05:37:52 -0800152
153 /* Number of bytes we're going to copy */
Florin Coras6792ec02017-03-13 03:49:51 -0700154 max_transfer = (max_dequeue < max_enqueue) ? max_dequeue : max_enqueue;
Florin Corasd79b41e2017-03-04 05:37:52 -0800155
Florin Coras6792ec02017-03-13 03:49:51 -0700156 /* No space in tx fifo */
157 if (PREDICT_FALSE (max_transfer == 0))
Florin Corasd79b41e2017-03-04 05:37:52 -0800158 {
Florin Coras6792ec02017-03-13 03:49:51 -0700159 /* XXX timeout for session that are stuck */
160
Florin Coras3e350af2017-03-30 02:54:28 -0700161 rx_event:
Florin Coras6792ec02017-03-13 03:49:51 -0700162 /* Program self-tap to retry */
163 if (svm_fifo_set_event (rx_fifo))
164 {
165 evt.fifo = rx_fifo;
166 evt.event_type = FIFO_EVENT_BUILTIN_RX;
167 evt.event_id = 0;
168 unix_shared_memory_queue_add (bsm->vpp_queue[s->thread_index],
169 (u8 *) & evt,
170 0 /* do wait for mutex */ );
171 }
172
Florin Corasd79b41e2017-03-04 05:37:52 -0800173 return 0;
174 }
175
Florin Coras6792ec02017-03-13 03:49:51 -0700176 vec_validate (bsm->rx_buf, max_transfer - 1);
177 _vec_len (bsm->rx_buf) = max_transfer;
178
Florin Corasa5464812017-04-19 13:00:05 -0700179 actual_transfer = svm_fifo_dequeue_nowait (rx_fifo, max_transfer,
Florin Coras6792ec02017-03-13 03:49:51 -0700180 bsm->rx_buf);
181 ASSERT (actual_transfer == max_transfer);
182
183// test_bytes (bsm, actual_transfer);
Florin Corasd79b41e2017-03-04 05:37:52 -0800184
185 /*
186 * Echo back
187 */
188
Florin Corasa5464812017-04-19 13:00:05 -0700189 n_written = svm_fifo_enqueue_nowait (tx_fifo, actual_transfer, bsm->rx_buf);
Florin Coras3e350af2017-03-30 02:54:28 -0700190
191 if (n_written != max_transfer)
192 clib_warning ("short trout!");
Florin Corasd79b41e2017-03-04 05:37:52 -0800193
Florin Coras6792ec02017-03-13 03:49:51 -0700194 if (svm_fifo_set_event (tx_fifo))
195 {
196 /* Fabricate TX event, send to vpp */
197 evt.fifo = tx_fifo;
Florin Corasa5464812017-04-19 13:00:05 -0700198 evt.event_type = FIFO_EVENT_APP_TX;
Florin Coras6792ec02017-03-13 03:49:51 -0700199 evt.event_id = serial_number++;
Florin Corasd79b41e2017-03-04 05:37:52 -0800200
Florin Coras6792ec02017-03-13 03:49:51 -0700201 unix_shared_memory_queue_add (bsm->vpp_queue[s->thread_index],
202 (u8 *) & evt, 0 /* do wait for mutex */ );
203 }
Florin Corasd79b41e2017-03-04 05:37:52 -0800204
Florin Coras3e350af2017-03-30 02:54:28 -0700205 if (PREDICT_FALSE (max_enqueue < max_dequeue))
206 goto rx_event;
207
Florin Corase04c2992017-03-01 08:17:34 -0800208 return 0;
209}
210
211static session_cb_vft_t builtin_session_cb_vft = {
212 .session_accept_callback = builtin_session_accept_callback,
213 .session_disconnect_callback = builtin_session_disconnect_callback,
214 .session_connected_callback = builtin_session_connected_callback,
215 .add_segment_callback = builtin_add_segment_callback,
216 .redirect_connect_callback = builtin_redirect_connect_callback,
Florin Corasd79b41e2017-03-04 05:37:52 -0800217 .builtin_server_rx_callback = builtin_server_rx_callback,
218 .session_reset_callback = builtin_session_reset_callback
Florin Corase04c2992017-03-01 08:17:34 -0800219};
220
Florin Coras6cf30ad2017-04-04 23:08:23 -0700221/* Abuse VPP's input queue */
Florin Corase04c2992017-03-01 08:17:34 -0800222static int
Florin Coras6cf30ad2017-04-04 23:08:23 -0700223create_api_loopback (vlib_main_t * vm)
Florin Corase04c2992017-03-01 08:17:34 -0800224{
Florin Coras6cf30ad2017-04-04 23:08:23 -0700225 builtin_server_main_t *bsm = &builtin_server_main;
226 vl_api_memclnt_create_t _m, *mp = &_m;
227 extern void vl_api_memclnt_create_t_handler (vl_api_memclnt_create_t *);
228 api_main_t *am = &api_main;
229 vl_shmem_hdr_t *shmem_hdr;
230 uword *event_data = 0, event_type;
231 int resolved = 0;
Florin Corasd79b41e2017-03-04 05:37:52 -0800232
Florin Coras6cf30ad2017-04-04 23:08:23 -0700233 /*
234 * Create a "loopback" API client connection
235 * Don't do things like this unless you know what you're doing...
236 */
237
238 shmem_hdr = am->shmem_hdr;
239 bsm->vl_input_queue = shmem_hdr->vl_input_queue;
240 memset (mp, 0, sizeof (*mp));
241 mp->_vl_msg_id = VL_API_MEMCLNT_CREATE;
242 mp->context = 0xFEEDFACE;
Damjan Marion7bee80c2017-04-26 15:32:12 +0200243 mp->input_queue = pointer_to_uword (bsm->vl_input_queue);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700244 strncpy ((char *) mp->name, "tcp_test_server", sizeof (mp->name) - 1);
245
246 vl_api_memclnt_create_t_handler (mp);
247
248 /* Wait for reply */
249 bsm->node_index = vlib_get_current_process (vm)->node_runtime.node_index;
250 vlib_process_wait_for_event_or_clock (vm, 1.0);
251 event_type = vlib_process_get_events (vm, &event_data);
252 switch (event_type)
253 {
254 case 1:
255 resolved = 1;
256 break;
257 case ~0:
258 /* timed out */
259 break;
260 default:
261 clib_warning ("unknown event_type %d", event_type);
262 }
263 if (!resolved)
264 return -1;
265
266 return 0;
267}
268
269static int
270server_attach ()
271{
272 builtin_server_main_t *bsm = &builtin_server_main;
273 u8 segment_name[128];
274 u64 options[SESSION_OPTIONS_N_OPTIONS];
275 vnet_app_attach_args_t _a, *a = &_a;
Florin Corase04c2992017-03-01 08:17:34 -0800276
277 memset (a, 0, sizeof (*a));
278 memset (options, 0, sizeof (options));
279
Florin Coras6cf30ad2017-04-04 23:08:23 -0700280 a->api_client_index = bsm->my_client_index;
Florin Corase04c2992017-03-01 08:17:34 -0800281 a->session_cb_vft = &builtin_session_cb_vft;
282 a->options = options;
Florin Coras6792ec02017-03-13 03:49:51 -0700283 a->options[SESSION_OPTIONS_SEGMENT_SIZE] = 128 << 20;
Florin Coras3e350af2017-03-30 02:54:28 -0700284 a->options[SESSION_OPTIONS_RX_FIFO_SIZE] = 1 << 16;
285 a->options[SESSION_OPTIONS_TX_FIFO_SIZE] = 1 << 16;
Florin Corasa5464812017-04-19 13:00:05 -0700286 a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_BUILTIN_APP;
Florin Corase04c2992017-03-01 08:17:34 -0800287 a->segment_name = segment_name;
288 a->segment_name_length = ARRAY_LEN (segment_name);
289
Florin Coras6cf30ad2017-04-04 23:08:23 -0700290 if (vnet_application_attach (a))
291 {
292 clib_warning ("failed to attach server");
293 return -1;
294 }
295 bsm->app_index = a->app_index;
296 return 0;
297}
298
299static int
300server_listen ()
301{
302 builtin_server_main_t *bsm = &builtin_server_main;
303 vnet_bind_args_t _a, *a = &_a;
304 memset (a, 0, sizeof (*a));
305 a->app_index = bsm->app_index;
306 a->uri = "tcp://0.0.0.0/1234";
Florin Corase04c2992017-03-01 08:17:34 -0800307 return vnet_bind_uri (a);
308}
309
Florin Coras6cf30ad2017-04-04 23:08:23 -0700310static int
311server_create (vlib_main_t * vm)
312{
313 builtin_server_main_t *bsm = &builtin_server_main;
314 u32 num_threads;
315 vlib_thread_main_t *vtm = vlib_get_thread_main ();
316
317 if (bsm->my_client_index == (u32) ~ 0)
318 {
319 if (create_api_loopback (vm))
320 return -1;
321 }
322
323 num_threads = 1 /* main thread */ + vtm->n_threads;
324 vec_validate (builtin_server_main.vpp_queue, num_threads - 1);
325
326 if (server_attach ())
327 {
328 clib_warning ("failed to attach server");
329 return -1;
330 }
331 if (server_listen ())
332 {
333 clib_warning ("failed to start listening");
334 return -1;
335 }
336 return 0;
337}
338
339/* Get our api client index */
340static void
341vl_api_memclnt_create_reply_t_handler (vl_api_memclnt_create_reply_t * mp)
342{
343 vlib_main_t *vm = vlib_get_main ();
344 builtin_server_main_t *bsm = &builtin_server_main;
345 bsm->my_client_index = mp->index;
346 vlib_process_signal_event (vm, bsm->node_index, 1 /* evt */ ,
347 0 /* data */ );
348}
349
350#define foreach_tcp_builtin_server_api_msg \
351_(MEMCLNT_CREATE_REPLY, memclnt_create_reply) \
352
353static clib_error_t *
354tcp_builtin_server_api_hookup (vlib_main_t * vm)
355{
356 vl_msg_api_msg_config_t _c, *c = &_c;
357
358 /* Hook up client-side static APIs to our handlers */
359#define _(N,n) do { \
360 c->id = VL_API_##N; \
361 c->name = #n; \
362 c->handler = vl_api_##n##_t_handler; \
363 c->cleanup = vl_noop_handler; \
364 c->endian = vl_api_##n##_t_endian; \
365 c->print = vl_api_##n##_t_print; \
366 c->size = sizeof(vl_api_##n##_t); \
367 c->traced = 1; /* trace, so these msgs print */ \
368 c->replay = 0; /* don't replay client create/delete msgs */ \
369 c->message_bounce = 0; /* don't bounce this message */ \
370 vl_msg_api_config(c);} while (0);
371
372 foreach_tcp_builtin_server_api_msg;
373#undef _
374
375 return 0;
376}
377
Florin Corase04c2992017-03-01 08:17:34 -0800378static clib_error_t *
379server_create_command_fn (vlib_main_t * vm,
380 unformat_input_t * input, vlib_cli_command_t * cmd)
381{
382 int rv;
383#if 0
384 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
385 {
386 if (unformat (input, "whatever %d", &whatever))
387 ;
388 else
389 return clib_error_return (0, "unknown input `%U'",
390 format_unformat_error, input);
391 }
392#endif
393
Florin Coras6cf30ad2017-04-04 23:08:23 -0700394 tcp_builtin_server_api_hookup (vm);
Florin Corasd79b41e2017-03-04 05:37:52 -0800395 vnet_session_enable_disable (vm, 1 /* turn on TCP, etc. */ );
Florin Corase04c2992017-03-01 08:17:34 -0800396 rv = server_create (vm);
397 switch (rv)
398 {
399 case 0:
400 break;
401 default:
402 return clib_error_return (0, "server_create returned %d", rv);
403 }
404 return 0;
405}
406
Florin Corasd79b41e2017-03-04 05:37:52 -0800407/* *INDENT-OFF* */
Florin Corase04c2992017-03-01 08:17:34 -0800408VLIB_CLI_COMMAND (server_create_command, static) =
409{
Florin Coras6cf30ad2017-04-04 23:08:23 -0700410 .path = "test tcp server",
411 .short_help = "test tcp server",
Florin Corasd79b41e2017-03-04 05:37:52 -0800412 .function = server_create_command_fn,
413};
414/* *INDENT-ON* */
Florin Corase04c2992017-03-01 08:17:34 -0800415
Florin Coras6cf30ad2017-04-04 23:08:23 -0700416clib_error_t *
417builtin_tcp_server_main_init (vlib_main_t * vm)
418{
419 builtin_server_main_t *bsm = &builtin_server_main;
420 bsm->my_client_index = ~0;
421 return 0;
422}
423
424VLIB_INIT_FUNCTION (builtin_tcp_server_main_init);
425
Florin Corase04c2992017-03-01 08:17:34 -0800426/*
427* fd.io coding-style-patch-verification: ON
428*
429* Local Variables:
430* eval: (c-set-style "gnu")
431* End:
432*/