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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <vnet/vnet.h>
17 #include <vlibmemory/api.h>
18 #include <vnet/session/application.h>
19 #include <vnet/session/application_interface.h>
21 /* define message IDs */
22 #include <vpp/api/vpe_msg_enum.h>
24 /* define message structures */
26 #include <vpp/api/vpe_all_api_h.h>
29 /* define generated endian-swappers */
31 #include <vpp/api/vpe_all_api_h.h>
34 /* instantiate all the print functions we know about */
35 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
37 #include <vpp/api/vpe_all_api_h.h>
42 /* Per-thread RX buffer */
44 unix_shared_memory_queue_t **vpp_queue;
47 /* Sever's event queue */
48 unix_shared_memory_queue_t *vl_input_queue;
50 /* API client handle */
55 /* process node index for evnt scheduling */
57 vlib_main_t *vlib_main;
58 } builtin_server_main_t;
60 builtin_server_main_t builtin_server_main;
63 builtin_session_accept_callback (stream_session_t * s)
65 builtin_server_main_t *bsm = &builtin_server_main;
67 bsm->vpp_queue[s->thread_index] =
68 session_manager_get_vpp_event_queue (s->thread_index);
69 s->session_state = SESSION_STATE_READY;
75 builtin_session_disconnect_callback (stream_session_t * s)
77 builtin_server_main_t *bsm = &builtin_server_main;
78 vnet_disconnect_args_t _a, *a = &_a;
80 a->handle = stream_session_handle (s);
81 a->app_index = bsm->app_index;
82 vnet_disconnect_session (a);
86 builtin_session_reset_callback (stream_session_t * s)
88 clib_warning ("called.. ");
90 stream_session_cleanup (s);
95 builtin_session_connected_callback (u32 app_index, u32 api_context,
96 stream_session_t * s, u8 is_fail)
98 clib_warning ("called...");
103 builtin_add_segment_callback (u32 client_index,
104 const u8 * seg_name, u32 seg_size)
106 clib_warning ("called...");
111 builtin_redirect_connect_callback (u32 client_index, void *mp)
113 clib_warning ("called...");
118 test_bytes (builtin_server_main_t * bsm, int actual_transfer)
121 u32 my_thread_id = vlib_get_thread_index ();
123 for (i = 0; i < actual_transfer; i++)
125 if (bsm->rx_buf[my_thread_id][i] != ((bsm->byte_index + i) & 0xff))
127 clib_warning ("at %lld expected %d got %d", bsm->byte_index + i,
128 (bsm->byte_index + i) & 0xff,
129 bsm->rx_buf[my_thread_id][i]);
132 bsm->byte_index += actual_transfer;
136 builtin_server_rx_callback (stream_session_t * s)
138 u32 n_written, max_dequeue, max_enqueue, max_transfer;
140 svm_fifo_t *tx_fifo, *rx_fifo;
141 builtin_server_main_t *bsm = &builtin_server_main;
142 session_fifo_event_t evt;
143 static int serial_number = 0;
144 u32 my_thread_id = vlib_get_thread_index ();
146 tx_fifo = s->server_tx_fifo;
147 rx_fifo = s->server_rx_fifo;
149 max_dequeue = svm_fifo_max_dequeue (s->server_rx_fifo);
150 max_enqueue = svm_fifo_max_enqueue (s->server_tx_fifo);
152 if (PREDICT_FALSE (max_dequeue == 0))
155 /* Number of bytes we're going to copy */
156 max_transfer = (max_dequeue < max_enqueue) ? max_dequeue : max_enqueue;
158 /* No space in tx fifo */
159 if (PREDICT_FALSE (max_transfer == 0))
161 /* XXX timeout for session that are stuck */
164 /* Program self-tap to retry */
165 if (svm_fifo_set_event (rx_fifo))
168 evt.event_type = FIFO_EVENT_BUILTIN_RX;
170 unix_shared_memory_queue_add (bsm->vpp_queue[s->thread_index],
172 0 /* do wait for mutex */ );
178 vec_validate (bsm->rx_buf, my_thread_id);
179 vec_validate (bsm->rx_buf[my_thread_id], max_transfer - 1);
180 _vec_len (bsm->rx_buf[my_thread_id]) = max_transfer;
182 actual_transfer = svm_fifo_dequeue_nowait (rx_fifo, max_transfer,
183 bsm->rx_buf[my_thread_id]);
184 ASSERT (actual_transfer == max_transfer);
186 // test_bytes (bsm, actual_transfer);
192 n_written = svm_fifo_enqueue_nowait (tx_fifo, actual_transfer,
193 bsm->rx_buf[my_thread_id]);
195 if (n_written != max_transfer)
196 clib_warning ("short trout!");
198 if (svm_fifo_set_event (tx_fifo))
200 /* Fabricate TX event, send to vpp */
202 evt.event_type = FIFO_EVENT_APP_TX;
203 evt.event_id = serial_number++;
205 unix_shared_memory_queue_add (bsm->vpp_queue[s->thread_index],
206 (u8 *) & evt, 0 /* do wait for mutex */ );
209 if (PREDICT_FALSE (max_enqueue < max_dequeue))
215 static session_cb_vft_t builtin_session_cb_vft = {
216 .session_accept_callback = builtin_session_accept_callback,
217 .session_disconnect_callback = builtin_session_disconnect_callback,
218 .session_connected_callback = builtin_session_connected_callback,
219 .add_segment_callback = builtin_add_segment_callback,
220 .redirect_connect_callback = builtin_redirect_connect_callback,
221 .builtin_server_rx_callback = builtin_server_rx_callback,
222 .session_reset_callback = builtin_session_reset_callback
225 /* Abuse VPP's input queue */
227 create_api_loopback (vlib_main_t * vm)
229 builtin_server_main_t *bsm = &builtin_server_main;
230 vl_api_memclnt_create_t _m, *mp = &_m;
231 extern void vl_api_memclnt_create_t_handler (vl_api_memclnt_create_t *);
232 api_main_t *am = &api_main;
233 vl_shmem_hdr_t *shmem_hdr;
234 uword *event_data = 0, event_type;
238 * Create a "loopback" API client connection
239 * Don't do things like this unless you know what you're doing...
242 shmem_hdr = am->shmem_hdr;
243 bsm->vl_input_queue = shmem_hdr->vl_input_queue;
244 memset (mp, 0, sizeof (*mp));
245 mp->_vl_msg_id = VL_API_MEMCLNT_CREATE;
246 mp->context = 0xFEEDFACE;
247 mp->input_queue = pointer_to_uword (bsm->vl_input_queue);
248 strncpy ((char *) mp->name, "tcp_test_server", sizeof (mp->name) - 1);
250 vl_api_memclnt_create_t_handler (mp);
253 bsm->node_index = vlib_get_current_process (vm)->node_runtime.node_index;
254 vlib_process_wait_for_event_or_clock (vm, 1.0);
255 event_type = vlib_process_get_events (vm, &event_data);
265 clib_warning ("unknown event_type %d", event_type);
276 builtin_server_main_t *bsm = &builtin_server_main;
277 u8 segment_name[128];
278 u64 options[SESSION_OPTIONS_N_OPTIONS];
279 vnet_app_attach_args_t _a, *a = &_a;
281 memset (a, 0, sizeof (*a));
282 memset (options, 0, sizeof (options));
284 a->api_client_index = bsm->my_client_index;
285 a->session_cb_vft = &builtin_session_cb_vft;
286 a->options = options;
287 a->options[SESSION_OPTIONS_SEGMENT_SIZE] = 512 << 20;
288 a->options[SESSION_OPTIONS_RX_FIFO_SIZE] = 64 << 10;
289 a->options[SESSION_OPTIONS_TX_FIFO_SIZE] = 64 << 10;
290 a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_BUILTIN_APP;
291 a->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = 8192;
292 a->segment_name = segment_name;
293 a->segment_name_length = ARRAY_LEN (segment_name);
295 if (vnet_application_attach (a))
297 clib_warning ("failed to attach server");
300 bsm->app_index = a->app_index;
307 builtin_server_main_t *bsm = &builtin_server_main;
308 vnet_bind_args_t _a, *a = &_a;
309 memset (a, 0, sizeof (*a));
310 a->app_index = bsm->app_index;
311 a->uri = "tcp://0.0.0.0/1234";
312 return vnet_bind_uri (a);
316 server_create (vlib_main_t * vm)
318 builtin_server_main_t *bsm = &builtin_server_main;
320 vlib_thread_main_t *vtm = vlib_get_thread_main ();
322 if (bsm->my_client_index == (u32) ~ 0)
324 if (create_api_loopback (vm))
328 num_threads = 1 /* main thread */ + vtm->n_threads;
329 vec_validate (builtin_server_main.vpp_queue, num_threads - 1);
331 if (server_attach ())
333 clib_warning ("failed to attach server");
336 if (server_listen ())
338 clib_warning ("failed to start listening");
344 /* Get our api client index */
346 vl_api_memclnt_create_reply_t_handler (vl_api_memclnt_create_reply_t * mp)
348 vlib_main_t *vm = vlib_get_main ();
349 builtin_server_main_t *bsm = &builtin_server_main;
350 bsm->my_client_index = mp->index;
351 vlib_process_signal_event (vm, bsm->node_index, 1 /* evt */ ,
355 #define foreach_tcp_builtin_server_api_msg \
356 _(MEMCLNT_CREATE_REPLY, memclnt_create_reply) \
358 static clib_error_t *
359 tcp_builtin_server_api_hookup (vlib_main_t * vm)
361 vl_msg_api_msg_config_t _c, *c = &_c;
363 /* Hook up client-side static APIs to our handlers */
364 #define _(N,n) do { \
365 c->id = VL_API_##N; \
367 c->handler = vl_api_##n##_t_handler; \
368 c->cleanup = vl_noop_handler; \
369 c->endian = vl_api_##n##_t_endian; \
370 c->print = vl_api_##n##_t_print; \
371 c->size = sizeof(vl_api_##n##_t); \
372 c->traced = 1; /* trace, so these msgs print */ \
373 c->replay = 0; /* don't replay client create/delete msgs */ \
374 c->message_bounce = 0; /* don't bounce this message */ \
375 vl_msg_api_config(c);} while (0);
377 foreach_tcp_builtin_server_api_msg;
383 static clib_error_t *
384 server_create_command_fn (vlib_main_t * vm,
385 unformat_input_t * input, vlib_cli_command_t * cmd)
389 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
391 if (unformat (input, "whatever %d", &whatever))
394 return clib_error_return (0, "unknown input `%U'",
395 format_unformat_error, input);
399 tcp_builtin_server_api_hookup (vm);
400 vnet_session_enable_disable (vm, 1 /* turn on TCP, etc. */ );
401 rv = server_create (vm);
407 return clib_error_return (0, "server_create returned %d", rv);
413 VLIB_CLI_COMMAND (server_create_command, static) =
415 .path = "test tcp server",
416 .short_help = "test tcp server",
417 .function = server_create_command_fn,
422 builtin_tcp_server_main_init (vlib_main_t * vm)
424 builtin_server_main_t *bsm = &builtin_server_main;
425 bsm->my_client_index = ~0;
429 VLIB_INIT_FUNCTION (builtin_tcp_server_main_init);
432 * fd.io coding-style-patch-verification: ON
435 * eval: (c-set-style "gnu")