26cbfe7772785f14abaf2e446779378ecb215715
[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 typedef struct
22 {
23   /*
24    * Server app parameters
25    */
26   unix_shared_memory_queue_t **vpp_queue;
27   unix_shared_memory_queue_t *vl_input_queue;   /**< Sever's event queue */
28
29   u32 app_index;                /**< Server app index */
30   u32 my_client_index;          /**< API client handle */
31   u32 node_index;               /**< process node index for evnt scheduling */
32
33   /*
34    * Config params
35    */
36   u8 no_echo;                   /**< Don't echo traffic */
37   u32 fifo_size;                        /**< Fifo size */
38   u32 rcv_buffer_size;          /**< Rcv buffer size */
39   u32 prealloc_fifos;           /**< Preallocate fifos */
40   u32 private_segment_count;    /**< Number of private segments  */
41   u32 private_segment_size;     /**< Size of private segments  */
42   char *server_uri;             /**< Server URI */
43
44   /*
45    * Test state
46    */
47   u8 **rx_buf;                  /**< Per-thread RX buffer */
48   u64 byte_index;
49   u32 **rx_retries;
50
51   vlib_main_t *vlib_main;
52 } builtin_server_main_t;
53
54 builtin_server_main_t builtin_server_main;
55
56 int
57 builtin_session_accept_callback (stream_session_t * s)
58 {
59   builtin_server_main_t *bsm = &builtin_server_main;
60
61   bsm->vpp_queue[s->thread_index] =
62     session_manager_get_vpp_event_queue (s->thread_index);
63   s->session_state = SESSION_STATE_READY;
64   bsm->byte_index = 0;
65   vec_validate (bsm->rx_retries[s->thread_index], s->session_index);
66   bsm->rx_retries[s->thread_index][s->session_index] = 0;
67   return 0;
68 }
69
70 void
71 builtin_session_disconnect_callback (stream_session_t * s)
72 {
73   builtin_server_main_t *bsm = &builtin_server_main;
74   vnet_disconnect_args_t _a, *a = &_a;
75
76   a->handle = session_handle (s);
77   a->app_index = bsm->app_index;
78   vnet_disconnect_session (a);
79 }
80
81 void
82 builtin_session_reset_callback (stream_session_t * s)
83 {
84   clib_warning ("Reset session %U", format_stream_session, s, 2);
85   stream_session_cleanup (s);
86 }
87
88
89 int
90 builtin_session_connected_callback (u32 app_index, u32 api_context,
91                                     stream_session_t * s, u8 is_fail)
92 {
93   clib_warning ("called...");
94   return -1;
95 }
96
97 int
98 builtin_add_segment_callback (u32 client_index,
99                               const u8 * seg_name, u32 seg_size)
100 {
101   clib_warning ("called...");
102   return -1;
103 }
104
105 int
106 builtin_redirect_connect_callback (u32 client_index, void *mp)
107 {
108   clib_warning ("called...");
109   return -1;
110 }
111
112 void
113 test_bytes (builtin_server_main_t * bsm, int actual_transfer)
114 {
115   int i;
116   u32 my_thread_id = vlib_get_thread_index ();
117
118   for (i = 0; i < actual_transfer; i++)
119     {
120       if (bsm->rx_buf[my_thread_id][i] != ((bsm->byte_index + i) & 0xff))
121         {
122           clib_warning ("at %lld expected %d got %d", bsm->byte_index + i,
123                         (bsm->byte_index + i) & 0xff,
124                         bsm->rx_buf[my_thread_id][i]);
125         }
126     }
127   bsm->byte_index += actual_transfer;
128 }
129
130 /*
131  * If no-echo, just read the data and be done with it
132  */
133 int
134 builtin_server_rx_callback_no_echo (stream_session_t * s)
135 {
136   builtin_server_main_t *bsm = &builtin_server_main;
137   u32 my_thread_id = vlib_get_thread_index ();
138   int actual_transfer;
139   svm_fifo_t *rx_fifo;
140
141   rx_fifo = s->server_rx_fifo;
142
143   do
144     {
145       actual_transfer =
146         svm_fifo_dequeue_nowait (rx_fifo, bsm->rcv_buffer_size,
147                                  bsm->rx_buf[my_thread_id]);
148     }
149   while (actual_transfer > 0);
150   return 0;
151 }
152
153 int
154 builtin_server_rx_callback (stream_session_t * s)
155 {
156   u32 n_written, max_dequeue, max_enqueue, max_transfer;
157   int actual_transfer;
158   svm_fifo_t *tx_fifo, *rx_fifo;
159   builtin_server_main_t *bsm = &builtin_server_main;
160   session_fifo_event_t evt;
161   u32 thread_index = vlib_get_thread_index ();
162
163   ASSERT (s->thread_index == thread_index);
164
165   rx_fifo = s->server_rx_fifo;
166   tx_fifo = s->server_tx_fifo;
167
168   ASSERT (rx_fifo->master_thread_index == thread_index);
169   ASSERT (tx_fifo->master_thread_index == thread_index);
170
171   max_dequeue = svm_fifo_max_dequeue (s->server_rx_fifo);
172   max_enqueue = svm_fifo_max_enqueue (s->server_tx_fifo);
173
174   if (PREDICT_FALSE (max_dequeue == 0))
175     return 0;
176
177   /* Number of bytes we're going to copy */
178   max_transfer = (max_dequeue < max_enqueue) ? max_dequeue : max_enqueue;
179
180   /* No space in tx fifo */
181   if (PREDICT_FALSE (max_transfer == 0))
182     {
183       /* XXX timeout for session that are stuck */
184
185     rx_event:
186       /* Program self-tap to retry */
187       if (svm_fifo_set_event (rx_fifo))
188         {
189           unix_shared_memory_queue_t *q;
190           evt.fifo = rx_fifo;
191           evt.event_type = FIFO_EVENT_BUILTIN_RX;
192
193           q = bsm->vpp_queue[thread_index];
194           if (PREDICT_FALSE (q->cursize == q->maxsize))
195             clib_warning ("out of event queue space");
196           else if (unix_shared_memory_queue_add (q, (u8 *) & evt, 0))
197             clib_warning ("failed to enqueue self-tap");
198
199           if (bsm->rx_retries[thread_index][s->session_index] == 500000)
200             {
201               clib_warning ("session stuck: %U", format_stream_session, s, 2);
202             }
203           if (bsm->rx_retries[thread_index][s->session_index] < 500001)
204             bsm->rx_retries[thread_index][s->session_index]++;
205         }
206
207       return 0;
208     }
209
210   _vec_len (bsm->rx_buf[thread_index]) = max_transfer;
211
212   actual_transfer = svm_fifo_dequeue_nowait (rx_fifo, max_transfer,
213                                              bsm->rx_buf[thread_index]);
214   ASSERT (actual_transfer == max_transfer);
215
216 //  test_bytes (bsm, actual_transfer);
217
218   /*
219    * Echo back
220    */
221
222   n_written = svm_fifo_enqueue_nowait (tx_fifo, actual_transfer,
223                                        bsm->rx_buf[thread_index]);
224
225   if (n_written != max_transfer)
226     clib_warning ("short trout!");
227
228   if (svm_fifo_set_event (tx_fifo))
229     {
230       /* Fabricate TX event, send to vpp */
231       evt.fifo = tx_fifo;
232       evt.event_type = FIFO_EVENT_APP_TX;
233
234       if (unix_shared_memory_queue_add (bsm->vpp_queue[s->thread_index],
235                                         (u8 *) & evt,
236                                         0 /* do wait for mutex */ ))
237         clib_warning ("failed to enqueue tx evt");
238     }
239
240   if (PREDICT_FALSE (n_written < max_dequeue))
241     goto rx_event;
242
243   return 0;
244 }
245
246 static session_cb_vft_t builtin_session_cb_vft = {
247   .session_accept_callback = builtin_session_accept_callback,
248   .session_disconnect_callback = builtin_session_disconnect_callback,
249   .session_connected_callback = builtin_session_connected_callback,
250   .add_segment_callback = builtin_add_segment_callback,
251   .redirect_connect_callback = builtin_redirect_connect_callback,
252   .builtin_server_rx_callback = builtin_server_rx_callback,
253   .session_reset_callback = builtin_session_reset_callback
254 };
255
256 /* Abuse VPP's input queue */
257 static int
258 create_api_loopback (vlib_main_t * vm)
259 {
260   builtin_server_main_t *bsm = &builtin_server_main;
261   api_main_t *am = &api_main;
262   vl_shmem_hdr_t *shmem_hdr;
263
264   shmem_hdr = am->shmem_hdr;
265   bsm->vl_input_queue = shmem_hdr->vl_input_queue;
266   bsm->my_client_index =
267     vl_api_memclnt_create_internal ("tcp_test_server", bsm->vl_input_queue);
268   return 0;
269 }
270
271 static int
272 server_attach (u8 * appns_id, u64 appns_flags, u64 appns_secret)
273 {
274   builtin_server_main_t *bsm = &builtin_server_main;
275   u8 segment_name[128];
276   u64 options[APP_OPTIONS_N_OPTIONS];
277   vnet_app_attach_args_t _a, *a = &_a;
278   u32 segment_size = 512 << 20;
279
280   memset (a, 0, sizeof (*a));
281   memset (options, 0, sizeof (options));
282
283   if (bsm->no_echo)
284     builtin_session_cb_vft.builtin_server_rx_callback =
285       builtin_server_rx_callback_no_echo;
286   else
287     builtin_session_cb_vft.builtin_server_rx_callback =
288       builtin_server_rx_callback;
289
290   if (bsm->private_segment_size)
291     segment_size = bsm->private_segment_size;
292
293   a->api_client_index = bsm->my_client_index;
294   a->session_cb_vft = &builtin_session_cb_vft;
295   a->options = options;
296   a->options[APP_OPTIONS_SEGMENT_SIZE] = segment_size;
297   a->options[APP_OPTIONS_RX_FIFO_SIZE] = bsm->fifo_size;
298   a->options[APP_OPTIONS_TX_FIFO_SIZE] = bsm->fifo_size;
299   a->options[APP_OPTIONS_PRIVATE_SEGMENT_COUNT] = bsm->private_segment_count;
300   a->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] =
301     bsm->prealloc_fifos ? bsm->prealloc_fifos : 1;
302
303   a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
304   if (appns_id)
305     {
306       a->namespace_id = appns_id;
307       a->options[APP_OPTIONS_FLAGS] |= appns_flags;
308       a->options[APP_OPTIONS_NAMESPACE_SECRET] = appns_secret;
309     }
310   a->segment_name = segment_name;
311   a->segment_name_length = ARRAY_LEN (segment_name);
312
313   if (vnet_application_attach (a))
314     {
315       clib_warning ("failed to attach server");
316       return -1;
317     }
318   bsm->app_index = a->app_index;
319   return 0;
320 }
321
322 static int
323 server_listen ()
324 {
325   builtin_server_main_t *bsm = &builtin_server_main;
326   vnet_bind_args_t _a, *a = &_a;
327   memset (a, 0, sizeof (*a));
328   a->app_index = bsm->app_index;
329   a->uri = bsm->server_uri;
330   return vnet_bind_uri (a);
331 }
332
333 static int
334 server_create (vlib_main_t * vm, u8 * appns_id, u64 appns_flags,
335                u64 appns_secret)
336 {
337   builtin_server_main_t *bsm = &builtin_server_main;
338   vlib_thread_main_t *vtm = vlib_get_thread_main ();
339   u32 num_threads;
340   int i;
341
342   if (bsm->my_client_index == (u32) ~ 0)
343     {
344       if (create_api_loopback (vm))
345         {
346           clib_warning ("failed to create api loopback");
347           return -1;
348         }
349     }
350
351   num_threads = 1 /* main thread */  + vtm->n_threads;
352   vec_validate (builtin_server_main.vpp_queue, num_threads - 1);
353   vec_validate (bsm->rx_buf, num_threads - 1);
354   vec_validate (bsm->rx_retries, num_threads - 1);
355
356   for (i = 0; i < num_threads; i++)
357     vec_validate (bsm->rx_buf[i], bsm->rcv_buffer_size);
358
359   if (server_attach (appns_id, appns_flags, appns_secret))
360     {
361       clib_warning ("failed to attach server");
362       return -1;
363     }
364   if (server_listen ())
365     {
366       clib_warning ("failed to start listening");
367       return -1;
368     }
369   return 0;
370 }
371
372 static clib_error_t *
373 server_create_command_fn (vlib_main_t * vm, unformat_input_t * input,
374                           vlib_cli_command_t * cmd)
375 {
376   builtin_server_main_t *bsm = &builtin_server_main;
377   u8 server_uri_set = 0, *appns_id = 0;
378   u64 tmp, appns_flags = 0, appns_secret = 0;
379   int rv;
380
381   bsm->no_echo = 0;
382   bsm->fifo_size = 64 << 10;
383   bsm->rcv_buffer_size = 128 << 10;
384   bsm->prealloc_fifos = 0;
385   bsm->private_segment_count = 0;
386   bsm->private_segment_size = 0;
387   vec_free (bsm->server_uri);
388
389   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
390     {
391       if (unformat (input, "no-echo"))
392         bsm->no_echo = 1;
393       else if (unformat (input, "fifo-size %d", &bsm->fifo_size))
394         bsm->fifo_size <<= 10;
395       else if (unformat (input, "rcv-buf-size %d", &bsm->rcv_buffer_size))
396         ;
397       else if (unformat (input, "prealloc-fifos %d", &bsm->prealloc_fifos))
398         ;
399       else if (unformat (input, "private-segment-count %d",
400                          &bsm->private_segment_count))
401         ;
402       else if (unformat (input, "private-segment-size %U",
403                          unformat_memory_size, &tmp))
404         {
405           if (tmp >= 0x100000000ULL)
406             return clib_error_return
407               (0, "private segment size %lld (%llu) too large", tmp, tmp);
408           bsm->private_segment_size = tmp;
409         }
410       else if (unformat (input, "uri %s", &bsm->server_uri))
411         server_uri_set = 1;
412       else if (unformat (input, "appns %_%v%_", &appns_id))
413         ;
414       else if (unformat (input, "all-scope"))
415         appns_flags |= (APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE
416                         | APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE);
417       else if (unformat (input, "local-scope"))
418         appns_flags |= APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE;
419       else if (unformat (input, "global-scope"))
420         appns_flags |= APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
421       else if (unformat (input, "secret %lu", &appns_secret))
422         ;
423       else
424         return clib_error_return (0, "unknown input `%U'",
425                                   format_unformat_error, input);
426     }
427
428   vnet_session_enable_disable (vm, 1 /* turn on TCP, etc. */ );
429
430   if (!server_uri_set)
431     bsm->server_uri = (char *) format (0, "tcp://0.0.0.0/1234%c", 0);
432
433   rv = server_create (vm, appns_id, appns_flags, appns_secret);
434   vec_free (appns_id);
435   switch (rv)
436     {
437     case 0:
438       break;
439     default:
440       return clib_error_return (0, "server_create returned %d", rv);
441     }
442
443   return 0;
444 }
445
446 /* *INDENT-OFF* */
447 VLIB_CLI_COMMAND (server_create_command, static) =
448 {
449   .path = "test tcp server",
450   .short_help = "test tcp server [no echo][fifo-size <mbytes>] "
451       "[rcv-buf-size <bytes>][prealloc-fifos <count>]"
452       "[private-segment-count <count>][private-segment-size <bytes[m|g]>]"
453       "[uri <tcp://ip/port>]",
454   .function = server_create_command_fn,
455 };
456 /* *INDENT-ON* */
457
458 clib_error_t *
459 builtin_tcp_server_main_init (vlib_main_t * vm)
460 {
461   builtin_server_main_t *bsm = &builtin_server_main;
462   bsm->my_client_index = ~0;
463   return 0;
464 }
465
466 VLIB_INIT_FUNCTION (builtin_tcp_server_main_init);
467
468 /*
469 * fd.io coding-style-patch-verification: ON
470 *
471 * Local Variables:
472 * eval: (c-set-style "gnu")
473 * End:
474 */