blob: 8308e3d9fd25d1d5066b1c2158e03ca9a6c58a20 [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
Florin Coras6792ec02017-03-13 03:49:51 -0700144 max_dequeue = svm_fifo_max_dequeue (s->server_rx_fifo);
145 max_enqueue = svm_fifo_max_enqueue (s->server_tx_fifo);
146
147 if (PREDICT_FALSE (max_dequeue == 0))
Florin Corasd79b41e2017-03-04 05:37:52 -0800148 {
Florin Corasd79b41e2017-03-04 05:37:52 -0800149 return 0;
150 }
151
152 tx_fifo = s->server_tx_fifo;
Florin Coras6792ec02017-03-13 03:49:51 -0700153 rx_fifo = s->server_rx_fifo;
Florin Corasd79b41e2017-03-04 05:37:52 -0800154
155 /* Number of bytes we're going to copy */
Florin Coras6792ec02017-03-13 03:49:51 -0700156 max_transfer = (max_dequeue < max_enqueue) ? max_dequeue : max_enqueue;
Florin Corasd79b41e2017-03-04 05:37:52 -0800157
Florin Coras6792ec02017-03-13 03:49:51 -0700158 /* No space in tx fifo */
159 if (PREDICT_FALSE (max_transfer == 0))
Florin Corasd79b41e2017-03-04 05:37:52 -0800160 {
Florin Coras6792ec02017-03-13 03:49:51 -0700161 /* XXX timeout for session that are stuck */
162
Florin Coras3e350af2017-03-30 02:54:28 -0700163 rx_event:
Florin Coras6792ec02017-03-13 03:49:51 -0700164 /* Program self-tap to retry */
165 if (svm_fifo_set_event (rx_fifo))
166 {
167 evt.fifo = rx_fifo;
168 evt.event_type = FIFO_EVENT_BUILTIN_RX;
169 evt.event_id = 0;
170 unix_shared_memory_queue_add (bsm->vpp_queue[s->thread_index],
171 (u8 *) & evt,
172 0 /* do wait for mutex */ );
173 }
174
Florin Corasd79b41e2017-03-04 05:37:52 -0800175 return 0;
176 }
177
Florin Coras6792ec02017-03-13 03:49:51 -0700178 svm_fifo_unset_event (rx_fifo);
Florin Corasd79b41e2017-03-04 05:37:52 -0800179
Florin Coras6792ec02017-03-13 03:49:51 -0700180 vec_validate (bsm->rx_buf, max_transfer - 1);
181 _vec_len (bsm->rx_buf) = max_transfer;
182
183 actual_transfer = svm_fifo_dequeue_nowait (rx_fifo, 0, max_transfer,
184 bsm->rx_buf);
185 ASSERT (actual_transfer == max_transfer);
186
187// test_bytes (bsm, actual_transfer);
Florin Corasd79b41e2017-03-04 05:37:52 -0800188
189 /*
190 * Echo back
191 */
192
Florin Coras6792ec02017-03-13 03:49:51 -0700193 n_written =
194 svm_fifo_enqueue_nowait (tx_fifo, 0, actual_transfer, bsm->rx_buf);
Florin Coras3e350af2017-03-30 02:54:28 -0700195
196 if (n_written != max_transfer)
197 clib_warning ("short trout!");
Florin Corasd79b41e2017-03-04 05:37:52 -0800198
Florin Coras6792ec02017-03-13 03:49:51 -0700199 if (svm_fifo_set_event (tx_fifo))
200 {
201 /* Fabricate TX event, send to vpp */
202 evt.fifo = tx_fifo;
203 evt.event_type = FIFO_EVENT_SERVER_TX;
204 evt.event_id = serial_number++;
Florin Corasd79b41e2017-03-04 05:37:52 -0800205
Florin Coras6792ec02017-03-13 03:49:51 -0700206 unix_shared_memory_queue_add (bsm->vpp_queue[s->thread_index],
207 (u8 *) & evt, 0 /* do wait for mutex */ );
208 }
Florin Corasd79b41e2017-03-04 05:37:52 -0800209
Florin Coras3e350af2017-03-30 02:54:28 -0700210 if (PREDICT_FALSE (max_enqueue < max_dequeue))
211 goto rx_event;
212
Florin Corase04c2992017-03-01 08:17:34 -0800213 return 0;
214}
215
216static session_cb_vft_t builtin_session_cb_vft = {
217 .session_accept_callback = builtin_session_accept_callback,
218 .session_disconnect_callback = builtin_session_disconnect_callback,
219 .session_connected_callback = builtin_session_connected_callback,
220 .add_segment_callback = builtin_add_segment_callback,
221 .redirect_connect_callback = builtin_redirect_connect_callback,
Florin Corasd79b41e2017-03-04 05:37:52 -0800222 .builtin_server_rx_callback = builtin_server_rx_callback,
223 .session_reset_callback = builtin_session_reset_callback
Florin Corase04c2992017-03-01 08:17:34 -0800224};
225
Florin Coras6cf30ad2017-04-04 23:08:23 -0700226/* Abuse VPP's input queue */
Florin Corase04c2992017-03-01 08:17:34 -0800227static int
Florin Coras6cf30ad2017-04-04 23:08:23 -0700228create_api_loopback (vlib_main_t * vm)
Florin Corase04c2992017-03-01 08:17:34 -0800229{
Florin Coras6cf30ad2017-04-04 23:08:23 -0700230 builtin_server_main_t *bsm = &builtin_server_main;
231 vl_api_memclnt_create_t _m, *mp = &_m;
232 extern void vl_api_memclnt_create_t_handler (vl_api_memclnt_create_t *);
233 api_main_t *am = &api_main;
234 vl_shmem_hdr_t *shmem_hdr;
235 uword *event_data = 0, event_type;
236 int resolved = 0;
Florin Corasd79b41e2017-03-04 05:37:52 -0800237
Florin Coras6cf30ad2017-04-04 23:08:23 -0700238 /*
239 * Create a "loopback" API client connection
240 * Don't do things like this unless you know what you're doing...
241 */
242
243 shmem_hdr = am->shmem_hdr;
244 bsm->vl_input_queue = shmem_hdr->vl_input_queue;
245 memset (mp, 0, sizeof (*mp));
246 mp->_vl_msg_id = VL_API_MEMCLNT_CREATE;
247 mp->context = 0xFEEDFACE;
248 mp->input_queue = (u64) bsm->vl_input_queue;
249 strncpy ((char *) mp->name, "tcp_test_server", sizeof (mp->name) - 1);
250
251 vl_api_memclnt_create_t_handler (mp);
252
253 /* Wait for reply */
254 bsm->node_index = vlib_get_current_process (vm)->node_runtime.node_index;
255 vlib_process_wait_for_event_or_clock (vm, 1.0);
256 event_type = vlib_process_get_events (vm, &event_data);
257 switch (event_type)
258 {
259 case 1:
260 resolved = 1;
261 break;
262 case ~0:
263 /* timed out */
264 break;
265 default:
266 clib_warning ("unknown event_type %d", event_type);
267 }
268 if (!resolved)
269 return -1;
270
271 return 0;
272}
273
274static int
275server_attach ()
276{
277 builtin_server_main_t *bsm = &builtin_server_main;
278 u8 segment_name[128];
279 u64 options[SESSION_OPTIONS_N_OPTIONS];
280 vnet_app_attach_args_t _a, *a = &_a;
Florin Corase04c2992017-03-01 08:17:34 -0800281
282 memset (a, 0, sizeof (*a));
283 memset (options, 0, sizeof (options));
284
Florin Coras6cf30ad2017-04-04 23:08:23 -0700285 a->api_client_index = bsm->my_client_index;
Florin Corase04c2992017-03-01 08:17:34 -0800286 a->session_cb_vft = &builtin_session_cb_vft;
287 a->options = options;
Florin Coras6792ec02017-03-13 03:49:51 -0700288 a->options[SESSION_OPTIONS_SEGMENT_SIZE] = 128 << 20;
Florin Coras3e350af2017-03-30 02:54:28 -0700289 a->options[SESSION_OPTIONS_RX_FIFO_SIZE] = 1 << 16;
290 a->options[SESSION_OPTIONS_TX_FIFO_SIZE] = 1 << 16;
Florin Corase04c2992017-03-01 08:17:34 -0800291 a->segment_name = segment_name;
292 a->segment_name_length = ARRAY_LEN (segment_name);
293
Florin Coras6cf30ad2017-04-04 23:08:23 -0700294 if (vnet_application_attach (a))
295 {
296 clib_warning ("failed to attach server");
297 return -1;
298 }
299 bsm->app_index = a->app_index;
300 return 0;
301}
302
303static int
304server_listen ()
305{
306 builtin_server_main_t *bsm = &builtin_server_main;
307 vnet_bind_args_t _a, *a = &_a;
308 memset (a, 0, sizeof (*a));
309 a->app_index = bsm->app_index;
310 a->uri = "tcp://0.0.0.0/1234";
Florin Corase04c2992017-03-01 08:17:34 -0800311 return vnet_bind_uri (a);
312}
313
Florin Coras6cf30ad2017-04-04 23:08:23 -0700314static int
315server_create (vlib_main_t * vm)
316{
317 builtin_server_main_t *bsm = &builtin_server_main;
318 u32 num_threads;
319 vlib_thread_main_t *vtm = vlib_get_thread_main ();
320
321 if (bsm->my_client_index == (u32) ~ 0)
322 {
323 if (create_api_loopback (vm))
324 return -1;
325 }
326
327 num_threads = 1 /* main thread */ + vtm->n_threads;
328 vec_validate (builtin_server_main.vpp_queue, num_threads - 1);
329
330 if (server_attach ())
331 {
332 clib_warning ("failed to attach server");
333 return -1;
334 }
335 if (server_listen ())
336 {
337 clib_warning ("failed to start listening");
338 return -1;
339 }
340 return 0;
341}
342
343/* Get our api client index */
344static void
345vl_api_memclnt_create_reply_t_handler (vl_api_memclnt_create_reply_t * mp)
346{
347 vlib_main_t *vm = vlib_get_main ();
348 builtin_server_main_t *bsm = &builtin_server_main;
349 bsm->my_client_index = mp->index;
350 vlib_process_signal_event (vm, bsm->node_index, 1 /* evt */ ,
351 0 /* data */ );
352}
353
354#define foreach_tcp_builtin_server_api_msg \
355_(MEMCLNT_CREATE_REPLY, memclnt_create_reply) \
356
357static clib_error_t *
358tcp_builtin_server_api_hookup (vlib_main_t * vm)
359{
360 vl_msg_api_msg_config_t _c, *c = &_c;
361
362 /* Hook up client-side static APIs to our handlers */
363#define _(N,n) do { \
364 c->id = VL_API_##N; \
365 c->name = #n; \
366 c->handler = vl_api_##n##_t_handler; \
367 c->cleanup = vl_noop_handler; \
368 c->endian = vl_api_##n##_t_endian; \
369 c->print = vl_api_##n##_t_print; \
370 c->size = sizeof(vl_api_##n##_t); \
371 c->traced = 1; /* trace, so these msgs print */ \
372 c->replay = 0; /* don't replay client create/delete msgs */ \
373 c->message_bounce = 0; /* don't bounce this message */ \
374 vl_msg_api_config(c);} while (0);
375
376 foreach_tcp_builtin_server_api_msg;
377#undef _
378
379 return 0;
380}
381
Florin Corase04c2992017-03-01 08:17:34 -0800382static clib_error_t *
383server_create_command_fn (vlib_main_t * vm,
384 unformat_input_t * input, vlib_cli_command_t * cmd)
385{
386 int rv;
387#if 0
388 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
389 {
390 if (unformat (input, "whatever %d", &whatever))
391 ;
392 else
393 return clib_error_return (0, "unknown input `%U'",
394 format_unformat_error, input);
395 }
396#endif
397
Florin Coras6cf30ad2017-04-04 23:08:23 -0700398 tcp_builtin_server_api_hookup (vm);
Florin Corasd79b41e2017-03-04 05:37:52 -0800399 vnet_session_enable_disable (vm, 1 /* turn on TCP, etc. */ );
Florin Corase04c2992017-03-01 08:17:34 -0800400 rv = server_create (vm);
401 switch (rv)
402 {
403 case 0:
404 break;
405 default:
406 return clib_error_return (0, "server_create returned %d", rv);
407 }
408 return 0;
409}
410
Florin Corasd79b41e2017-03-04 05:37:52 -0800411/* *INDENT-OFF* */
Florin Corase04c2992017-03-01 08:17:34 -0800412VLIB_CLI_COMMAND (server_create_command, static) =
413{
Florin Coras6cf30ad2017-04-04 23:08:23 -0700414 .path = "test tcp server",
415 .short_help = "test tcp server",
Florin Corasd79b41e2017-03-04 05:37:52 -0800416 .function = server_create_command_fn,
417};
418/* *INDENT-ON* */
Florin Corase04c2992017-03-01 08:17:34 -0800419
Florin Coras6cf30ad2017-04-04 23:08:23 -0700420clib_error_t *
421builtin_tcp_server_main_init (vlib_main_t * vm)
422{
423 builtin_server_main_t *bsm = &builtin_server_main;
424 bsm->my_client_index = ~0;
425 return 0;
426}
427
428VLIB_INIT_FUNCTION (builtin_tcp_server_main_init);
429
Florin Corase04c2992017-03-01 08:17:34 -0800430/*
431* fd.io coding-style-patch-verification: ON
432*
433* Local Variables:
434* eval: (c-set-style "gnu")
435* End:
436*/