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