VPP-846: tcp perf / scale / hardening
[vpp.git] / src / vnet / tcp / builtin_server.c
1 /*
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
21 /* 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
40 typedef struct
41 {
42   u8 *rx_buf;
43   unix_shared_memory_queue_t **vpp_queue;
44   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;
56   vlib_main_t *vlib_main;
57 } builtin_server_main_t;
58
59 builtin_server_main_t builtin_server_main;
60
61 int
62 builtin_session_accept_callback (stream_session_t * s)
63 {
64   builtin_server_main_t *bsm = &builtin_server_main;
65   clib_warning ("called...");
66
67   bsm->vpp_queue[s->thread_index] =
68     session_manager_get_vpp_event_queue (s->thread_index);
69   s->session_state = SESSION_STATE_READY;
70   bsm->byte_index = 0;
71   return 0;
72 }
73
74 void
75 builtin_session_disconnect_callback (stream_session_t * s)
76 {
77   builtin_server_main_t *bsm = &builtin_server_main;
78   vnet_disconnect_args_t _a, *a = &_a;
79   clib_warning ("called...");
80
81   a->handle = stream_session_handle (s);
82   a->app_index = bsm->app_index;
83   vnet_disconnect_session (a);
84 }
85
86 void
87 builtin_session_reset_callback (stream_session_t * s)
88 {
89   clib_warning ("called.. ");
90
91   stream_session_cleanup (s);
92 }
93
94
95 int
96 builtin_session_connected_callback (u32 app_index, u32 api_context,
97                                     stream_session_t * s, u8 is_fail)
98 {
99   clib_warning ("called...");
100   return -1;
101 }
102
103 int
104 builtin_add_segment_callback (u32 client_index,
105                               const u8 * seg_name, u32 seg_size)
106 {
107   clib_warning ("called...");
108   return -1;
109 }
110
111 int
112 builtin_redirect_connect_callback (u32 client_index, void *mp)
113 {
114   clib_warning ("called...");
115   return -1;
116 }
117
118 void
119 test_bytes (builtin_server_main_t * bsm, int actual_transfer)
120 {
121   int i;
122
123   for (i = 0; i < actual_transfer; i++)
124     {
125       if (bsm->rx_buf[i] != ((bsm->byte_index + i) & 0xff))
126         {
127           clib_warning ("at %lld expected %d got %d", bsm->byte_index + i,
128                         (bsm->byte_index + i) & 0xff, bsm->rx_buf[i]);
129         }
130     }
131   bsm->byte_index += actual_transfer;
132 }
133
134 int
135 builtin_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;
140   builtin_server_main_t *bsm = &builtin_server_main;
141   session_fifo_event_t evt;
142   static int serial_number = 0;
143
144   tx_fifo = s->server_tx_fifo;
145   rx_fifo = s->server_rx_fifo;
146
147   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))
151     return 0;
152
153   /* Number of bytes we're going to copy */
154   max_transfer = (max_dequeue < max_enqueue) ? max_dequeue : max_enqueue;
155
156   /* No space in tx fifo */
157   if (PREDICT_FALSE (max_transfer == 0))
158     {
159       /* XXX timeout for session that are stuck */
160
161     rx_event:
162       /* 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
173       return 0;
174     }
175
176   vec_validate (bsm->rx_buf, max_transfer - 1);
177   _vec_len (bsm->rx_buf) = max_transfer;
178
179   actual_transfer = svm_fifo_dequeue_nowait (rx_fifo, max_transfer,
180                                              bsm->rx_buf);
181   ASSERT (actual_transfer == max_transfer);
182
183 //  test_bytes (bsm, actual_transfer);
184
185   /*
186    * Echo back
187    */
188
189   n_written = svm_fifo_enqueue_nowait (tx_fifo, actual_transfer, bsm->rx_buf);
190
191   if (n_written != max_transfer)
192     clib_warning ("short trout!");
193
194   if (svm_fifo_set_event (tx_fifo))
195     {
196       /* Fabricate TX event, send to vpp */
197       evt.fifo = tx_fifo;
198       evt.event_type = FIFO_EVENT_APP_TX;
199       evt.event_id = serial_number++;
200
201       unix_shared_memory_queue_add (bsm->vpp_queue[s->thread_index],
202                                     (u8 *) & evt, 0 /* do wait for mutex */ );
203     }
204
205   if (PREDICT_FALSE (max_enqueue < max_dequeue))
206     goto rx_event;
207
208   return 0;
209 }
210
211 static 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,
217   .builtin_server_rx_callback = builtin_server_rx_callback,
218   .session_reset_callback = builtin_session_reset_callback
219 };
220
221 /* Abuse VPP's input queue */
222 static int
223 create_api_loopback (vlib_main_t * vm)
224 {
225   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;
232
233   /*
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;
243   mp->input_queue = pointer_to_uword (bsm->vl_input_queue);
244   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
269 static int
270 server_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;
276
277   memset (a, 0, sizeof (*a));
278   memset (options, 0, sizeof (options));
279
280   a->api_client_index = bsm->my_client_index;
281   a->session_cb_vft = &builtin_session_cb_vft;
282   a->options = options;
283   a->options[SESSION_OPTIONS_SEGMENT_SIZE] = 128 << 20;
284   a->options[SESSION_OPTIONS_RX_FIFO_SIZE] = 1 << 16;
285   a->options[SESSION_OPTIONS_TX_FIFO_SIZE] = 1 << 16;
286   a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_BUILTIN_APP;
287   a->segment_name = segment_name;
288   a->segment_name_length = ARRAY_LEN (segment_name);
289
290   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
299 static int
300 server_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";
307   return vnet_bind_uri (a);
308 }
309
310 static int
311 server_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 */
340 static void
341 vl_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
353 static clib_error_t *
354 tcp_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
378 static clib_error_t *
379 server_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
394   tcp_builtin_server_api_hookup (vm);
395   vnet_session_enable_disable (vm, 1 /* turn on TCP, etc. */ );
396   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
407 /* *INDENT-OFF* */
408 VLIB_CLI_COMMAND (server_create_command, static) =
409 {
410   .path = "test tcp server",
411   .short_help = "test tcp server",
412   .function = server_create_command_fn,
413 };
414 /* *INDENT-ON* */
415
416 clib_error_t *
417 builtin_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
424 VLIB_INIT_FUNCTION (builtin_tcp_server_main_init);
425
426 /*
427 * fd.io coding-style-patch-verification: ON
428 *
429 * Local Variables:
430 * eval: (c-set-style "gnu")
431 * End:
432 */