FD.io VPP  v20.01-48-g3e0dafb74
Vector Packet Processing
echo_client.c
Go to the documentation of this file.
1 /*
2  * echo_client.c - vpp built-in echo client code
3  *
4  * Copyright (c) 2017-2019 by Cisco and/or its affiliates.
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #include <vnet/vnet.h>
19 #include <vlibapi/api.h>
20 #include <vlibmemory/api.h>
21 #include <hs_apps/echo_client.h>
22 
24 
25 #define ECHO_CLIENT_DBG (0)
26 #define DBG(_fmt, _args...) \
27  if (ECHO_CLIENT_DBG) \
28  clib_warning (_fmt, ##_args)
29 
30 static void
32 {
34  ASSERT (vlib_get_thread_index () == 0);
35  vlib_process_signal_event (ecm->vlib_main, ecm->cli_node_index, *code, 0);
36 }
37 
38 static void
40 {
41  if (vlib_get_thread_index () != 0)
43  sizeof (code));
44  else
45  signal_evt_to_cli_i (&code);
46 }
47 
48 static void
50 {
51  u8 *test_data = ecm->connect_test_data;
52  int test_buf_len, test_buf_offset, rv;
53  u32 bytes_this_chunk;
54 
55  test_buf_len = vec_len (test_data);
56  ASSERT (test_buf_len > 0);
57  test_buf_offset = s->bytes_sent % test_buf_len;
58  bytes_this_chunk = clib_min (test_buf_len - test_buf_offset,
59  s->bytes_to_send);
60 
61  if (!ecm->is_dgram)
62  {
63  if (ecm->no_copy)
64  {
65  svm_fifo_t *f = s->data.tx_fifo;
66  rv = clib_min (svm_fifo_max_enqueue_prod (f), bytes_this_chunk);
68  session_send_io_evt_to_thread_custom (&f->master_session_index,
69  s->thread_index,
71  }
72  else
73  rv = app_send_stream (&s->data, test_data + test_buf_offset,
74  bytes_this_chunk, 0);
75  }
76  else
77  {
78  if (ecm->no_copy)
79  {
81  svm_fifo_t *f = s->data.tx_fifo;
82  app_session_transport_t *at = &s->data.transport;
83  u32 max_enqueue = svm_fifo_max_enqueue_prod (f);
84 
85  if (max_enqueue <= sizeof (session_dgram_hdr_t))
86  return;
87 
88  max_enqueue -= sizeof (session_dgram_hdr_t);
89  rv = clib_min (max_enqueue, bytes_this_chunk);
90 
91  hdr.data_length = rv;
92  hdr.data_offset = 0;
93  clib_memcpy_fast (&hdr.rmt_ip, &at->rmt_ip,
94  sizeof (ip46_address_t));
95  hdr.is_ip4 = at->is_ip4;
96  hdr.rmt_port = at->rmt_port;
97  clib_memcpy_fast (&hdr.lcl_ip, &at->lcl_ip,
98  sizeof (ip46_address_t));
99  hdr.lcl_port = at->lcl_port;
100  svm_fifo_enqueue (f, sizeof (hdr), (u8 *) & hdr);
101  svm_fifo_enqueue_nocopy (f, rv);
102  session_send_io_evt_to_thread_custom (&f->master_session_index,
103  s->thread_index,
105  }
106  else
107  rv = app_send_dgram (&s->data, test_data + test_buf_offset,
108  bytes_this_chunk, 0);
109  }
110 
111  /* If we managed to enqueue data... */
112  if (rv > 0)
113  {
114  /* Account for it... */
115  s->bytes_to_send -= rv;
116  s->bytes_sent += rv;
117 
118  if (ECHO_CLIENT_DBG)
119  {
120  /* *INDENT-OFF* */
121  ELOG_TYPE_DECLARE (e) =
122  {
123  .format = "tx-enq: xfer %d bytes, sent %u remain %u",
124  .format_args = "i4i4i4",
125  };
126  /* *INDENT-ON* */
127  struct
128  {
129  u32 data[3];
130  } *ed;
132  ed->data[0] = rv;
133  ed->data[1] = s->bytes_sent;
134  ed->data[2] = s->bytes_to_send;
135  }
136  }
137 }
138 
139 static void
141 {
142  svm_fifo_t *rx_fifo = s->data.rx_fifo;
143  u32 thread_index = vlib_get_thread_index ();
144  int n_read, i;
145 
146  if (ecm->test_bytes)
147  {
148  if (!ecm->is_dgram)
149  n_read = app_recv_stream (&s->data, ecm->rx_buf[thread_index],
150  vec_len (ecm->rx_buf[thread_index]));
151  else
152  n_read = app_recv_dgram (&s->data, ecm->rx_buf[thread_index],
153  vec_len (ecm->rx_buf[thread_index]));
154  }
155  else
156  {
157  n_read = svm_fifo_max_dequeue_cons (rx_fifo);
158  svm_fifo_dequeue_drop (rx_fifo, n_read);
159  }
160 
161  if (n_read > 0)
162  {
163  if (ECHO_CLIENT_DBG)
164  {
165  /* *INDENT-OFF* */
166  ELOG_TYPE_DECLARE (e) =
167  {
168  .format = "rx-deq: %d bytes",
169  .format_args = "i4",
170  };
171  /* *INDENT-ON* */
172  struct
173  {
174  u32 data[1];
175  } *ed;
177  ed->data[0] = n_read;
178  }
179 
180  if (ecm->test_bytes)
181  {
182  for (i = 0; i < n_read; i++)
183  {
184  if (ecm->rx_buf[thread_index][i]
185  != ((s->bytes_received + i) & 0xff))
186  {
187  clib_warning ("read %d error at byte %lld, 0x%x not 0x%x",
188  n_read, s->bytes_received + i,
189  ecm->rx_buf[thread_index][i],
190  ((s->bytes_received + i) & 0xff));
191  ecm->test_failed = 1;
192  }
193  }
194  }
195  ASSERT (n_read <= s->bytes_to_receive);
196  s->bytes_to_receive -= n_read;
197  s->bytes_received += n_read;
198  }
199 }
200 
201 static uword
204 {
206  int my_thread_index = vlib_get_thread_index ();
207  eclient_session_t *sp;
208  int i;
209  int delete_session;
210  u32 *connection_indices;
211  u32 *connections_this_batch;
212  u32 nconnections_this_batch;
213 
214  connection_indices = ecm->connection_index_by_thread[my_thread_index];
215  connections_this_batch =
216  ecm->connections_this_batch_by_thread[my_thread_index];
217 
218  if ((ecm->run_test != ECHO_CLIENTS_RUNNING) ||
219  ((vec_len (connection_indices) == 0)
220  && vec_len (connections_this_batch) == 0))
221  return 0;
222 
223  /* Grab another pile of connections */
224  if (PREDICT_FALSE (vec_len (connections_this_batch) == 0))
225  {
226  nconnections_this_batch =
227  clib_min (ecm->connections_per_batch, vec_len (connection_indices));
228 
229  ASSERT (nconnections_this_batch > 0);
230  vec_validate (connections_this_batch, nconnections_this_batch - 1);
231  clib_memcpy_fast (connections_this_batch,
232  connection_indices + vec_len (connection_indices)
233  - nconnections_this_batch,
234  nconnections_this_batch * sizeof (u32));
235  _vec_len (connection_indices) -= nconnections_this_batch;
236  }
237 
239  && ecm->prev_conns == vec_len (connections_this_batch)))
240  {
241  ecm->repeats++;
242  ecm->prev_conns = vec_len (connections_this_batch);
243  if (ecm->repeats == 500000)
244  {
245  clib_warning ("stuck clients");
246  }
247  }
248  else
249  {
250  ecm->prev_conns = vec_len (connections_this_batch);
251  ecm->repeats = 0;
252  }
253 
254  for (i = 0; i < vec_len (connections_this_batch); i++)
255  {
256  delete_session = 1;
257 
258  sp = pool_elt_at_index (ecm->sessions, connections_this_batch[i]);
259 
260  if (sp->bytes_to_send > 0)
261  {
262  send_data_chunk (ecm, sp);
263  delete_session = 0;
264  }
265  if (sp->bytes_to_receive > 0)
266  {
267  delete_session = 0;
268  }
269  if (PREDICT_FALSE (delete_session == 1))
270  {
271  session_t *s;
272 
276 
277  if (s)
278  {
279  vnet_disconnect_args_t _a, *a = &_a;
280  a->handle = session_handle (s);
281  a->app_index = ecm->app_index;
283 
284  vec_delete (connections_this_batch, 1, i);
285  i--;
287  }
288  else
289  {
290  clib_warning ("session AWOL?");
291  vec_delete (connections_this_batch, 1, i);
292  }
293 
294  /* Kick the debug CLI process */
295  if (ecm->ready_connections == 0)
296  {
297  signal_evt_to_cli (2);
298  }
299  }
300  }
301 
302  ecm->connection_index_by_thread[my_thread_index] = connection_indices;
303  ecm->connections_this_batch_by_thread[my_thread_index] =
304  connections_this_batch;
305  return 0;
306 }
307 
308 /* *INDENT-OFF* */
310 {
311  .function = echo_client_node_fn,
312  .name = "echo-clients",
313  .type = VLIB_NODE_TYPE_INPUT,
314  .state = VLIB_NODE_STATE_DISABLED,
315 };
316 /* *INDENT-ON* */
317 
318 static int
320 {
321  api_main_t *am = vlibapi_get_main ();
322  vl_shmem_hdr_t *shmem_hdr;
323 
324  shmem_hdr = am->shmem_hdr;
325  ecm->vl_input_queue = shmem_hdr->vl_input_queue;
326  ecm->my_client_index = vl_api_memclnt_create_internal ("echo_client",
327  ecm->vl_input_queue);
328  return 0;
329 }
330 
331 static int
333 {
336  u32 num_threads;
337  int i;
338 
339  if (create_api_loopback (ecm))
340  return -1;
341 
342  num_threads = 1 /* main thread */ + vtm->n_threads;
343 
344  /* Init test data. Big buffer */
345  vec_validate (ecm->connect_test_data, 4 * 1024 * 1024 - 1);
346  for (i = 0; i < vec_len (ecm->connect_test_data); i++)
347  ecm->connect_test_data[i] = i & 0xff;
348 
349  vec_validate (ecm->rx_buf, num_threads - 1);
350  for (i = 0; i < num_threads; i++)
351  vec_validate (ecm->rx_buf[i], vec_len (ecm->connect_test_data) - 1);
352 
353  ecm->is_init = 1;
354 
359 
360  return 0;
361 }
362 
363 static int
365  session_t * s, u8 is_fail)
366 {
368  vnet_connect_args_t *a = 0;
369  int rv;
370  u8 thread_index = vlib_get_thread_index ();
372  u32 stream_n;
373  session_handle_t handle;
374 
375  DBG ("QUIC Connection handle %d", session_handle (s));
376 
377  vec_validate (a, 1);
378  a->uri = (char *) ecm->connect_uri;
379  if (parse_uri (a->uri, &sep))
380  return -1;
381  sep.parent_handle = handle = session_handle (s);
382 
383  for (stream_n = 0; stream_n < ecm->quic_streams; stream_n++)
384  {
385  clib_memset (a, 0, sizeof (*a));
386  a->app_index = ecm->app_index;
387  a->api_context = -1 - api_context;
388  clib_memcpy (&a->sep_ext, &sep, sizeof (sep));
389 
390  DBG ("QUIC opening stream %d", stream_n);
391  if ((rv = vnet_connect (a)))
392  {
393  clib_error ("Stream session %d opening failed: %d", stream_n, rv);
394  return -1;
395  }
396  DBG ("QUIC stream %d connected", stream_n);
397  }
398  /*
399  * 's' is no longer valid, its underlying pool could have been moved in
400  * vnet_connect()
401  */
402  vec_add1 (ecm->quic_session_index_by_thread[thread_index], handle);
403  vec_free (a);
404  return 0;
405 }
406 
407 static int
409  session_t * s, u8 is_fail)
410 {
412  eclient_session_t *session;
413  u32 session_index;
414  u8 thread_index;
415 
417  return -1;
418 
419  if (is_fail)
420  {
421  clib_warning ("connection %d failed!", api_context);
423  signal_evt_to_cli (-1);
424  return 0;
425  }
426 
429  api_context, s,
430  is_fail);
431  DBG ("STREAM Connection callback %d", api_context);
432 
433  thread_index = s->thread_index;
434  ASSERT (thread_index == vlib_get_thread_index ()
436 
437  if (!ecm->vpp_event_queue[thread_index])
438  ecm->vpp_event_queue[thread_index] =
439  session_main_get_vpp_event_queue (thread_index);
440 
441  /*
442  * Setup session
443  */
445  pool_get (ecm->sessions, session);
447 
448  clib_memset (session, 0, sizeof (*session));
449  session_index = session - ecm->sessions;
450  session->bytes_to_send = ecm->bytes_to_send;
451  session->bytes_to_receive = ecm->no_return ? 0ULL : ecm->bytes_to_send;
452  session->data.rx_fifo = s->rx_fifo;
453  session->data.rx_fifo->client_session_index = session_index;
454  session->data.tx_fifo = s->tx_fifo;
455  session->data.tx_fifo->client_session_index = session_index;
456  session->data.vpp_evt_q = ecm->vpp_event_queue[thread_index];
457  session->vpp_session_handle = session_handle (s);
458 
459  if (ecm->is_dgram)
460  {
462  tc = session_get_transport (s);
463  clib_memcpy_fast (&session->data.transport, tc,
464  sizeof (session->data.transport));
465  session->data.is_dgram = 1;
466  }
467 
468  vec_add1 (ecm->connection_index_by_thread[thread_index], session_index);
470  if (ecm->ready_connections == ecm->expected_connections)
471  {
473  /* Signal the CLI process that the action is starting... */
474  signal_evt_to_cli (1);
475  }
476 
477  return 0;
478 }
479 
480 static int
482  session_t * s, u8 is_fail)
483 {
485  eclient_session_t *session;
486  u32 session_index;
487  u8 thread_index;
488 
490  return -1;
491 
492  if (is_fail)
493  {
494  clib_warning ("connection %d failed!", api_context);
496  signal_evt_to_cli (-1);
497  return 0;
498  }
499 
500  thread_index = s->thread_index;
501  ASSERT (thread_index == vlib_get_thread_index ()
503 
504  if (!ecm->vpp_event_queue[thread_index])
505  ecm->vpp_event_queue[thread_index] =
506  session_main_get_vpp_event_queue (thread_index);
507 
508  /*
509  * Setup session
510  */
512  pool_get (ecm->sessions, session);
514 
515  clib_memset (session, 0, sizeof (*session));
516  session_index = session - ecm->sessions;
517  session->bytes_to_send = ecm->bytes_to_send;
518  session->bytes_to_receive = ecm->no_return ? 0ULL : ecm->bytes_to_send;
519  session->data.rx_fifo = s->rx_fifo;
520  session->data.rx_fifo->client_session_index = session_index;
521  session->data.tx_fifo = s->tx_fifo;
522  session->data.tx_fifo->client_session_index = session_index;
523  session->data.vpp_evt_q = ecm->vpp_event_queue[thread_index];
524  session->vpp_session_handle = session_handle (s);
525 
526  if (ecm->is_dgram)
527  {
529  tc = session_get_transport (s);
530  clib_memcpy_fast (&session->data.transport, tc,
531  sizeof (session->data.transport));
532  session->data.is_dgram = 1;
533  }
534 
535  vec_add1 (ecm->connection_index_by_thread[thread_index], session_index);
537  if (ecm->ready_connections == ecm->expected_connections)
538  {
540  /* Signal the CLI process that the action is starting... */
541  signal_evt_to_cli (1);
542  }
543 
544  return 0;
545 }
546 
547 static void
549 {
551  vnet_disconnect_args_t _a = { 0 }, *a = &_a;
552 
553  if (s->session_state == SESSION_STATE_READY)
554  clib_warning ("Reset active connection %U", format_session, s, 2);
555 
556  a->handle = session_handle (s);
557  a->app_index = ecm->app_index;
559  return;
560 }
561 
562 static int
564 {
565  return 0;
566 }
567 
568 static void
570 {
572  vnet_disconnect_args_t _a = { 0 }, *a = &_a;
573  a->handle = session_handle (s);
574  a->app_index = ecm->app_index;
576  return;
577 }
578 
579 void
581 {
583  vnet_disconnect_args_t _a = { 0 }, *a = &_a;
584  a->handle = session_handle (s);
585  a->app_index = ecm->app_index;
587 }
588 
589 static int
591 {
593  eclient_session_t *sp;
594 
596  {
598  return -1;
599  }
600 
601  sp = pool_elt_at_index (ecm->sessions, s->rx_fifo->client_session_index);
602  receive_data_chunk (ecm, sp);
603 
605  {
606  if (svm_fifo_set_event (s->rx_fifo))
608  }
609  return 0;
610 }
611 
612 int
613 echo_client_add_segment_callback (u32 client_index, u64 segment_handle)
614 {
615  /* New heaps may be added */
616  return 0;
617 }
618 
619 /* *INDENT-OFF* */
620 static session_cb_vft_t echo_clients = {
622  .session_connected_callback = echo_clients_session_connected_callback,
623  .session_accept_callback = echo_clients_session_create_callback,
624  .session_disconnect_callback = echo_clients_session_disconnect_callback,
625  .builtin_app_rx_callback = echo_clients_rx_callback,
626  .add_segment_callback = echo_client_add_segment_callback
627 };
628 /* *INDENT-ON* */
629 
630 static clib_error_t *
631 echo_clients_attach (u8 * appns_id, u64 appns_flags, u64 appns_secret)
632 {
633  vnet_app_add_tls_cert_args_t _a_cert, *a_cert = &_a_cert;
634  vnet_app_add_tls_key_args_t _a_key, *a_key = &_a_key;
635  u32 prealloc_fifos, segment_size = 256 << 20;
637  vnet_app_attach_args_t _a, *a = &_a;
638  u64 options[16];
639  int rv;
640 
641  clib_memset (a, 0, sizeof (*a));
642  clib_memset (options, 0, sizeof (options));
643 
644  a->api_client_index = ecm->my_client_index;
645  if (ecm->transport_proto == TRANSPORT_PROTO_QUIC)
646  echo_clients.session_connected_callback =
648  a->session_cb_vft = &echo_clients;
649 
650  prealloc_fifos = ecm->prealloc_fifos ? ecm->expected_connections : 1;
651 
652  if (ecm->private_segment_size)
653  segment_size = ecm->private_segment_size;
654 
655  options[APP_OPTIONS_ACCEPT_COOKIE] = 0x12345678;
656  options[APP_OPTIONS_SEGMENT_SIZE] = segment_size;
657  options[APP_OPTIONS_ADD_SEGMENT_SIZE] = segment_size;
658  options[APP_OPTIONS_RX_FIFO_SIZE] = ecm->fifo_size;
659  options[APP_OPTIONS_TX_FIFO_SIZE] = ecm->fifo_size;
661  options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = prealloc_fifos;
662  options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
663  options[APP_OPTIONS_TLS_ENGINE] = ecm->tls_engine;
664  if (appns_id)
665  {
666  options[APP_OPTIONS_FLAGS] |= appns_flags;
667  options[APP_OPTIONS_NAMESPACE_SECRET] = appns_secret;
668  }
669  a->options = options;
670  a->namespace_id = appns_id;
671 
672  if ((rv = vnet_application_attach (a)))
673  return clib_error_return (0, "attach returned %d", rv);
674 
675  ecm->app_index = a->app_index;
676 
677  clib_memset (a_cert, 0, sizeof (*a_cert));
678  a_cert->app_index = a->app_index;
679  vec_validate (a_cert->cert, test_srv_crt_rsa_len);
681  vnet_app_add_tls_cert (a_cert);
682 
683  clib_memset (a_key, 0, sizeof (*a_key));
684  a_key->app_index = a->app_index;
685  vec_validate (a_key->key, test_srv_key_rsa_len);
687  vnet_app_add_tls_key (a_key);
688  return 0;
689 }
690 
691 static int
693 {
695  vnet_app_detach_args_t _da, *da = &_da;
696  int rv;
697 
698  da->app_index = ecm->app_index;
699  da->api_client_index = ~0;
700  rv = vnet_application_detach (da);
701  ecm->test_client_attached = 0;
702  ecm->app_index = ~0;
703  return rv;
704 }
705 
706 static void *
708 {
709  return 0;
710 }
711 
712 /** Start a transmit thread */
713 int
715 {
716  if (ecm->client_thread_handle == 0)
717  {
718  int rv = pthread_create (&ecm->client_thread_handle,
719  NULL /*attr */ ,
721  if (rv)
722  {
723  ecm->client_thread_handle = 0;
724  return -1;
725  }
726  }
727  return 0;
728 }
729 
730 clib_error_t *
732 {
734  vnet_connect_args_t _a, *a = &_a;
735  int i, rv;
736 
737  clib_memset (a, 0, sizeof (*a));
738 
739  for (i = 0; i < n_clients; i++)
740  {
741  a->uri = (char *) ecm->connect_uri;
742  a->api_context = i;
743  a->app_index = ecm->app_index;
744 
746  if ((rv = vnet_connect_uri (a)))
747  {
749  return clib_error_return (0, "connect returned: %d", rv);
750  }
752 
753  /* Crude pacing for call setups */
754  if ((i % 16) == 0)
755  vlib_process_suspend (vm, 100e-6);
756  ASSERT (i + 1 >= ecm->ready_connections);
757  while (i + 1 - ecm->ready_connections > 128)
758  vlib_process_suspend (vm, 1e-3);
759  }
760  return 0;
761 }
762 
763 #define ec_cli_output(_fmt, _args...) \
764  if (!ecm->no_output) \
765  vlib_cli_output(vm, _fmt, ##_args)
766 
767 static clib_error_t *
769  unformat_input_t * input, vlib_cli_command_t * cmd)
770 {
772  vlib_thread_main_t *thread_main = vlib_get_thread_main ();
773  u64 tmp, total_bytes, appns_flags = 0, appns_secret = 0;
774  f64 test_timeout = 20.0, syn_timeout = 20.0, delta;
775  char *default_uri = "tcp://6.0.1.1/1234";
776  uword *event_data = 0, event_type;
777  f64 time_before_connects;
778  u32 n_clients = 1;
779  int preallocate_sessions = 0;
780  char *transfer_type;
781  clib_error_t *error = 0;
782  u8 *appns_id = 0;
783  int i;
785  int rv;
786 
787  ecm->quic_streams = 1;
788  ecm->bytes_to_send = 8192;
789  ecm->no_return = 0;
790  ecm->fifo_size = 64 << 10;
791  ecm->connections_per_batch = 1000;
792  ecm->private_segment_count = 0;
793  ecm->private_segment_size = 0;
794  ecm->no_output = 0;
795  ecm->test_bytes = 0;
796  ecm->test_failed = 0;
797  ecm->vlib_main = vm;
799  ecm->no_copy = 0;
801 
802  if (thread_main->n_vlib_mains > 1)
804  vec_free (ecm->connect_uri);
805 
807  {
808  if (unformat (input, "uri %s", &ecm->connect_uri))
809  ;
810  else if (unformat (input, "nclients %d", &n_clients))
811  ;
812  else if (unformat (input, "quic-streams %d", &ecm->quic_streams))
813  ;
814  else if (unformat (input, "mbytes %lld", &tmp))
815  ecm->bytes_to_send = tmp << 20;
816  else if (unformat (input, "gbytes %lld", &tmp))
817  ecm->bytes_to_send = tmp << 30;
818  else if (unformat (input, "bytes %lld", &ecm->bytes_to_send))
819  ;
820  else if (unformat (input, "test-timeout %f", &test_timeout))
821  ;
822  else if (unformat (input, "syn-timeout %f", &syn_timeout))
823  ;
824  else if (unformat (input, "no-return"))
825  ecm->no_return = 1;
826  else if (unformat (input, "fifo-size %d", &ecm->fifo_size))
827  ecm->fifo_size <<= 10;
828  else if (unformat (input, "private-segment-count %d",
829  &ecm->private_segment_count))
830  ;
831  else if (unformat (input, "private-segment-size %U",
832  unformat_memory_size, &tmp))
833  {
834  if (tmp >= 0x100000000ULL)
835  return clib_error_return
836  (0, "private segment size %lld (%llu) too large", tmp, tmp);
837  ecm->private_segment_size = tmp;
838  }
839  else if (unformat (input, "preallocate-fifos"))
840  ecm->prealloc_fifos = 1;
841  else if (unformat (input, "preallocate-sessions"))
842  preallocate_sessions = 1;
843  else
844  if (unformat (input, "client-batch %d", &ecm->connections_per_batch))
845  ;
846  else if (unformat (input, "appns %_%v%_", &appns_id))
847  ;
848  else if (unformat (input, "all-scope"))
849  appns_flags |= (APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE
850  | APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE);
851  else if (unformat (input, "local-scope"))
852  appns_flags = APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE;
853  else if (unformat (input, "global-scope"))
854  appns_flags = APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
855  else if (unformat (input, "secret %lu", &appns_secret))
856  ;
857  else if (unformat (input, "no-output"))
858  ecm->no_output = 1;
859  else if (unformat (input, "test-bytes"))
860  ecm->test_bytes = 1;
861  else if (unformat (input, "tls-engine %d", &ecm->tls_engine))
862  ;
863  else
864  return clib_error_return (0, "failed: unknown input `%U'",
865  format_unformat_error, input);
866  }
867 
868  /* Store cli process node index for signalling */
869  ecm->cli_node_index =
871 
872  if (ecm->is_init == 0)
873  {
874  if (echo_clients_init (vm))
875  return clib_error_return (0, "failed init");
876  }
877 
878 
879  ecm->ready_connections = 0;
880  ecm->expected_connections = n_clients * ecm->quic_streams;
881  ecm->rx_total = 0;
882  ecm->tx_total = 0;
883 
884  if (!ecm->connect_uri)
885  {
886  clib_warning ("No uri provided. Using default: %s", default_uri);
887  ecm->connect_uri = format (0, "%s%c", default_uri, 0);
888  }
889 
890  if ((rv = parse_uri ((char *) ecm->connect_uri, &sep)))
891  return clib_error_return (0, "Uri parse error: %d", rv);
892  ecm->transport_proto = sep.transport_proto;
893  ecm->is_dgram = (sep.transport_proto == TRANSPORT_PROTO_UDP
894  || sep.transport_proto == TRANSPORT_PROTO_UDPC);
895 
896 #if ECHO_CLIENT_PTHREAD
898 #endif
899 
901  vnet_session_enable_disable (vm, 1 /* turn on session and transports */ );
903 
904  if (ecm->test_client_attached == 0)
905  {
906  if ((error = echo_clients_attach (appns_id, appns_flags, appns_secret)))
907  {
908  vec_free (appns_id);
909  clib_error_report (error);
910  return error;
911  }
912  vec_free (appns_id);
913  }
914  ecm->test_client_attached = 1;
915 
916  /* Turn on the builtin client input nodes */
917  for (i = 0; i < thread_main->n_vlib_mains; i++)
919  VLIB_NODE_STATE_POLLING);
920 
921  if (preallocate_sessions)
922  pool_init_fixed (ecm->sessions, 1.1 * n_clients);
923 
924  /* Fire off connect requests */
925  time_before_connects = vlib_time_now (vm);
926  if ((error = echo_clients_connect (vm, n_clients)))
927  {
928  goto cleanup;
929  }
930 
931  /* Park until the sessions come up, or ten seconds elapse... */
932  vlib_process_wait_for_event_or_clock (vm, syn_timeout);
933  event_type = vlib_process_get_events (vm, &event_data);
934  switch (event_type)
935  {
936  case ~0:
937  ec_cli_output ("Timeout with only %d sessions active...",
938  ecm->ready_connections);
939  error = clib_error_return (0, "failed: syn timeout with %d sessions",
940  ecm->ready_connections);
941  goto cleanup;
942 
943  case 1:
944  delta = vlib_time_now (vm) - time_before_connects;
945  if (delta != 0.0)
946  ec_cli_output ("%d three-way handshakes in %.2f seconds %.2f/s",
947  n_clients, delta, ((f64) n_clients) / delta);
948 
950  ec_cli_output ("Test started at %.6f", ecm->test_start_time);
951  break;
952 
953  default:
954  ec_cli_output ("unexpected event(1): %d", event_type);
955  error = clib_error_return (0, "failed: unexpected event(1): %d",
956  event_type);
957  goto cleanup;
958  }
959 
960  /* Now wait for the sessions to finish... */
961  vlib_process_wait_for_event_or_clock (vm, test_timeout);
962  event_type = vlib_process_get_events (vm, &event_data);
963  switch (event_type)
964  {
965  case ~0:
966  ec_cli_output ("Timeout with %d sessions still active...",
967  ecm->ready_connections);
968  error = clib_error_return (0, "failed: timeout with %d sessions",
969  ecm->ready_connections);
970  goto cleanup;
971 
972  case 2:
973  ecm->test_end_time = vlib_time_now (vm);
974  ec_cli_output ("Test finished at %.6f", ecm->test_end_time);
975  break;
976 
977  default:
978  ec_cli_output ("unexpected event(2): %d", event_type);
979  error = clib_error_return (0, "failed: unexpected event(2): %d",
980  event_type);
981  goto cleanup;
982  }
983 
984  delta = ecm->test_end_time - ecm->test_start_time;
985  if (delta != 0.0)
986  {
987  total_bytes = (ecm->no_return ? ecm->tx_total : ecm->rx_total);
988  transfer_type = ecm->no_return ? "half-duplex" : "full-duplex";
989  ec_cli_output ("%lld bytes (%lld mbytes, %lld gbytes) in %.2f seconds",
990  total_bytes, total_bytes / (1ULL << 20),
991  total_bytes / (1ULL << 30), delta);
992  ec_cli_output ("%.2f bytes/second %s", ((f64) total_bytes) / (delta),
993  transfer_type);
994  ec_cli_output ("%.4f gbit/second %s",
995  (((f64) total_bytes * 8.0) / delta / 1e9),
996  transfer_type);
997  }
998  else
999  {
1000  ec_cli_output ("zero delta-t?");
1001  error = clib_error_return (0, "failed: zero delta-t");
1002  goto cleanup;
1003  }
1004 
1005  if (ecm->test_bytes && ecm->test_failed)
1006  error = clib_error_return (0, "failed: test bytes");
1007 
1008 cleanup:
1011  for (i = 0; i < vec_len (ecm->connection_index_by_thread); i++)
1012  {
1016  }
1017 
1018  pool_free (ecm->sessions);
1019 
1020  /* Detach the application, so we can use different fifo sizes next time */
1021  if (ecm->test_client_attached)
1022  {
1023  if (echo_clients_detach ())
1024  {
1025  error = clib_error_return (0, "failed: app detach");
1026  ec_cli_output ("WARNING: app detach failed...");
1027  }
1028  }
1029  if (error)
1030  ec_cli_output ("test failed");
1031  vec_free (ecm->connect_uri);
1032  return error;
1033 }
1034 
1035 /* *INDENT-OFF* */
1036 VLIB_CLI_COMMAND (echo_clients_command, static) =
1037 {
1038  .path = "test echo clients",
1039  .short_help = "test echo clients [nclients %d][[m|g]bytes <bytes>]"
1040  "[test-timeout <time>][syn-timeout <time>][no-return][fifo-size <size>]"
1041  "[private-segment-count <count>][private-segment-size <bytes>[m|g]]"
1042  "[preallocate-fifos][preallocate-sessions][client-batch <batch-size>]"
1043  "[uri <tcp://ip/port>][test-bytes][no-output]",
1044  .function = echo_clients_command_fn,
1045  .is_mp_safe = 1,
1046 };
1047 /* *INDENT-ON* */
1048 
1049 clib_error_t *
1051 {
1053  ecm->is_init = 0;
1054  return 0;
1055 }
1056 
1058 
1059 /*
1060  * fd.io coding-style-patch-verification: ON
1061  *
1062  * Local Variables:
1063  * eval: (c-set-style "gnu")
1064  * End:
1065  */
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:440
int echo_client_add_segment_callback(u32 client_index, u64 segment_handle)
Definition: echo_client.c:613
vlib_main_t vlib_global_main
Definition: main.c:1943
vlib_main_t * vlib_main
Definition: echo_client.h:107
clib_error_t * echo_clients_connect(vlib_main_t *vm, u32 n_clients)
Definition: echo_client.c:731
#define clib_min(x, y)
Definition: clib.h:295
static int echo_clients_session_create_callback(session_t *s)
Definition: echo_client.c:563
u32 vl_api_memclnt_create_internal(char *name, svm_queue_t *q)
Definition: memory_api.c:120
static f64 vlib_process_wait_for_event_or_clock(vlib_main_t *vm, f64 dt)
Suspend a cooperative multi-tasking thread Waits for an event, or for the indicated number of seconds...
Definition: node_funcs.h:673
u8 is_ip4
set if uses ip4 networking
static u32 svm_fifo_max_enqueue_prod(svm_fifo_t *f)
Maximum number of bytes that can be enqueued into fifo.
Definition: svm_fifo.h:619
vlib_node_runtime_t node_runtime
Definition: node.h:554
u8 * format_session(u8 *s, va_list *args)
Format stream session as per the following format.
Definition: session_cli.c:101
a
Definition: bitmap.h:538
svm_fifo_t * tx_fifo
struct _vnet_connect_args vnet_connect_args_t
static session_cb_vft_t echo_clients
Definition: echo_client.c:620
#define clib_error(format, args...)
Definition: error.h:62
unsigned long u64
Definition: types.h:89
static svm_msg_q_t * session_main_get_vpp_event_queue(u32 thread_index)
Definition: session.h:607
u32 tls_engine
TLS engine mbedtls/openssl.
Definition: echo_client.h:65
static int echo_clients_detach()
Definition: echo_client.c:692
#define clib_memcpy_fast(a, b, c)
Definition: string.h:81
#define NULL
Definition: clib.h:58
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
transport_connection_t * session_get_transport(session_t *s)
Definition: session.c:1461
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:279
static uword echo_client_node_fn(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: echo_client.c:202
svm_fifo_t * rx_fifo
Pointers to rx/tx buffers.
u32 expected_connections
Number of clients/connections.
Definition: echo_client.h:61
static_always_inline void clib_spinlock_unlock_if_init(clib_spinlock_t *p)
Definition: lock.h:110
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:523
eclient_session_t * sessions
Session pool, shared.
Definition: echo_client.h:73
static int app_send_stream(app_session_t *s, u8 *data, u32 len, u8 noblock)
u64 bytes_to_send
Bytes to send.
Definition: echo_client.h:58
void svm_fifo_enqueue_nocopy(svm_fifo_t *f, u32 len)
Advance tail.
Definition: svm_fifo.c:964
int i
static void receive_data_chunk(echo_client_main_t *ecm, eclient_session_t *s)
Definition: echo_client.c:140
volatile int run_test
Signal start of test.
Definition: echo_client.h:86
u32 quic_streams
QUIC streams per connection.
Definition: echo_client.h:68
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:424
void(* session_reset_callback)(session_t *s)
Notify app that session was reset.
struct _vnet_application_add_tls_cert_args_t vnet_app_add_tls_cert_args_t
#define pool_get(P, E)
Allocate an object E from a pool P (unspecified alignment).
Definition: pool.h:237
vlib_main_t ** vlib_mains
Definition: buffer.c:332
void echo_clients_session_disconnect(session_t *s)
Definition: echo_client.c:580
clib_error_t * vnet_app_add_tls_cert(vnet_app_add_tls_cert_args_t *a)
Definition: application.c:1312
unsigned char u8
Definition: types.h:56
app_session_t data
Definition: echo_client.h:33
u8 * connect_test_data
Pre-computed test data.
Definition: echo_client.h:76
#define vec_reset_length(v)
Reset vector length to zero NULL-pointer tolerant.
double f64
Definition: types.h:142
#define vlib_worker_thread_barrier_sync(X)
Definition: threads.h:204
static session_handle_t session_handle(session_t *s)
struct _svm_fifo svm_fifo_t
#define clib_memcpy(d, s, n)
Definition: string.h:180
static uword vlib_process_suspend(vlib_main_t *vm, f64 dt)
Suspend a vlib cooperative multi-tasking thread for a period of time.
Definition: node_funcs.h:422
clib_error_t * echo_clients_main_init(vlib_main_t *vm)
Definition: echo_client.c:1050
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:173
static uword vlib_process_get_events(vlib_main_t *vm, uword **data_vector)
Return the first event type which has occurred and a vector of per-event data of that type...
Definition: node_funcs.h:516
struct _vnet_disconnect_args_t vnet_disconnect_args_t
int echo_clients_start_tx_pthread(echo_client_main_t *ecm)
Start a transmit thread.
Definition: echo_client.c:714
static const u32 test_srv_key_rsa_len
Definition: tls_test.h:77
static u32 svm_fifo_max_dequeue_cons(svm_fifo_t *f)
Fifo max bytes to dequeue optimized for consumer.
Definition: svm_fifo.h:499
#define DBG(_fmt, _args...)
Definition: echo_client.c:26
#define clib_error_return(e, args...)
Definition: error.h:99
void vl_api_rpc_call_main_thread(void *fp, u8 *data, u32 data_length)
Definition: vlib_api.c:607
u32 app_index
app index after attach
Definition: echo_client.h:52
static int quic_echo_clients_qsession_connected_callback(u32 app_index, u32 api_context, session_t *s, u8 is_fail)
Definition: echo_client.c:364
svm_queue_t * vl_input_queue
vpe input queue
Definition: echo_client.h:47
struct vl_shmem_hdr_ * shmem_hdr
Binary API shared-memory segment header pointer.
Definition: api_common.h:288
ip46_address_t lcl_ip
svm_msg_q_t ** vpp_event_queue
Definition: echo_client.h:48
unsigned int u32
Definition: types.h:88
int session_send_io_evt_to_thread(svm_fifo_t *f, session_evt_type_t evt_type)
Definition: session.c:80
#define SESSION_INVALID_HANDLE
Definition: session_types.h:23
struct _vnet_app_attach_args_t vnet_app_attach_args_t
static void echo_clients_session_disconnect_callback(session_t *s)
Definition: echo_client.c:569
static void clib_spinlock_init(clib_spinlock_t *p)
Definition: lock.h:63
struct _session_endpoint_cfg session_endpoint_cfg_t
static int create_api_loopback(echo_client_main_t *ecm)
Definition: echo_client.c:319
static void signal_evt_to_cli(int code)
Definition: echo_client.c:39
clib_error_t * vnet_app_add_tls_key(vnet_app_add_tls_key_args_t *a)
Definition: application.c:1322
static clib_error_t * echo_clients_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: echo_client.c:768
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:519
volatile u32 ready_connections
Definition: echo_client.h:82
static void vlib_process_signal_event(vlib_main_t *vm, uword node_index, uword type_opaque, uword data)
Definition: node_funcs.h:934
int(* session_connected_callback)(u32 app_wrk_index, u32 opaque, session_t *s, u8 code)
Connection request callback.
struct _unformat_input_t unformat_input_t
static void cleanup(void)
Definition: client.c:131
int svm_fifo_enqueue(svm_fifo_t *f, u32 len, const u8 *src)
Enqueue data to fifo.
Definition: svm_fifo.c:888
#define ELOG_DATA(em, f)
Definition: elog.h:484
#define PREDICT_FALSE(x)
Definition: clib.h:111
ip46_address_t rmt_ip
remote ip
u32 ** connection_index_by_thread
Definition: echo_client.h:78
u32 private_segment_count
Number of private fifo segs.
Definition: echo_client.h:63
u32 ** connections_this_batch_by_thread
active connection batch
Definition: echo_client.h:79
u32 node_index
Node index.
Definition: node.h:496
static const char test_srv_crt_rsa[]
Definition: tls_test.h:23
vlib_main_t * vm
Definition: in2out_ed.c:1810
clib_error_t * vnet_session_enable_disable(vlib_main_t *vm, u8 is_en)
Definition: session.c:1606
API main structure, used by both vpp and binary API clients.
Definition: api_common.h:225
int vnet_application_attach(vnet_app_attach_args_t *a)
Attach application to vpp.
Definition: application.c:815
static session_t * session_get_from_handle_if_valid(session_handle_t handle)
Definition: session.h:324
static void echo_clients_session_reset_callback(session_t *s)
Definition: echo_client.c:548
#define pool_free(p)
Free a pool.
Definition: pool.h:412
static u8 svm_fifo_set_event(svm_fifo_t *f)
Set fifo event flag.
Definition: svm_fifo.h:749
u32 no_copy
Don&#39;t memcpy data to tx fifo.
Definition: echo_client.h:67
u32 connections_per_batch
Connections to rx/tx at once.
Definition: echo_client.h:62
static int echo_clients_rx_callback(session_t *s)
Definition: echo_client.c:590
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:169
#define UNFORMAT_END_OF_INPUT
Definition: format.h:145
session_handle_t listener_handle
Parent listener session index if the result of an accept.
static_always_inline uword vlib_get_thread_index(void)
Definition: threads.h:218
static clib_error_t * echo_clients_attach(u8 *appns_id, u64 appns_flags, u64 appns_secret)
Definition: echo_client.c:631
static vlib_process_t * vlib_get_current_process(vlib_main_t *vm)
Definition: node_funcs.h:391
vlib_node_registration_t echo_clients_node
(constructor) VLIB_REGISTER_NODE (echo_clients_node)
Definition: echo_client.c:309
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:342
static int echo_clients_session_connected_callback(u32 app_index, u32 api_context, session_t *s, u8 is_fail)
Definition: echo_client.c:481
#define clib_warning(format, args...)
Definition: error.h:59
echo_client_main_t echo_client_main
Definition: echo_client.c:23
elog_main_t elog_main
Definition: main.h:193
struct _transport_connection transport_connection_t
#define ELOG_TYPE_DECLARE(f)
Definition: elog.h:442
u32 private_segment_size
size of private fifo segs
Definition: echo_client.h:64
static int app_recv_stream(app_session_t *s, u8 *buf, u32 len)
svm_queue_t * vl_input_queue
Definition: memory_shared.h:84
static void send_data_chunk(echo_client_main_t *ecm, eclient_session_t *s)
Definition: echo_client.c:49
vlib_main_t vlib_node_runtime_t * node
Definition: in2out_ed.c:1810
#define pool_init_fixed(pool, max_elts)
initialize a fixed-size, preallocated pool
Definition: pool.h:86
static const char test_srv_key_rsa[]
Definition: tls_test.h:49
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:152
#define ASSERT(truth)
u32 cli_node_index
cli process node index
Definition: echo_client.h:50
#define vec_delete(V, N, M)
Delete N elements starting at element M.
Definition: vec.h:785
u8 data[128]
Definition: ipsec_types.api:87
u32 my_client_index
loopback API client handle
Definition: echo_client.h:51
int vnet_application_detach(vnet_app_detach_args_t *a)
Detach application from vpp.
Definition: application.c:875
static int app_send_dgram(app_session_t *s, u8 *data, u32 len, u8 noblock)
struct _vnet_application_add_tls_key_args_t vnet_app_add_tls_key_args_t
#define clib_error_report(e)
Definition: error.h:113
static void vlib_node_set_state(vlib_main_t *vm, u32 node_index, vlib_node_state_t new_state)
Set node dispatch state.
Definition: node_funcs.h:148
#define ECHO_CLIENT_DBG
Definition: echo_client.c:25
ip46_address_t rmt_ip
u16 lcl_port
local port (network order)
struct _vnet_app_detach_args_t vnet_app_detach_args_t
u8 ** rx_buf
intermediate rx buffers
Definition: echo_client.h:75
int vnet_connect(vnet_connect_args_t *a)
Definition: application.c:1007
u8 thread_index
Index of the thread that allocated the session.
#define clib_atomic_fetch_add(a, b)
Definition: atomics.h:23
ip46_address_t lcl_ip
local ip
u8 prealloc_fifos
Request fifo preallocation.
Definition: echo_client.h:101
u32 ** quic_session_index_by_thread
Definition: echo_client.h:77
volatile u64 tx_total
Definition: echo_client.h:85
int parse_uri(char *uri, session_endpoint_cfg_t *sep)
volatile u64 rx_total
Definition: echo_client.h:84
u64 session_handle_t
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
volatile u8 session_state
State in session layer state machine.
u8 * connect_uri
URI for slave&#39;s connect.
Definition: echo_client.h:57
#define ec_cli_output(_fmt, _args...)
Definition: echo_client.c:763
u64 uword
Definition: types.h:112
clib_spinlock_t sessions_lock
Definition: echo_client.h:74
static void * echo_client_thread_fn(void *arg)
Definition: echo_client.c:707
connectionless service
int vnet_disconnect_session(vnet_disconnect_args_t *a)
Definition: application.c:1076
static int quic_echo_clients_session_connected_callback(u32 app_index, u32 api_context, session_t *s, u8 is_fail)
Definition: echo_client.c:408
#define SESSION_ENDPOINT_CFG_NULL
Definition: session_types.h:78
int session_send_io_evt_to_thread_custom(void *data, u32 thread_index, session_evt_type_t evt_type)
Definition: session.c:87
unformat_function_t unformat_memory_size
Definition: format.h:296
static void signal_evt_to_cli_i(int *code)
Definition: echo_client.c:31
static api_main_t * vlibapi_get_main(void)
Definition: api_common.h:378
static struct option options[]
Definition: main.c:52
u8 * format_unformat_error(u8 *s, va_list *va)
Definition: unformat.c:91
void vlib_worker_thread_barrier_release(vlib_main_t *vm)
Definition: threads.c:1496
static vlib_thread_main_t * vlib_get_thread_main()
Definition: global_funcs.h:32
vlib_main_t vlib_node_runtime_t vlib_frame_t * frame
Definition: in2out_ed.c:1811
int vnet_connect_uri(vnet_connect_args_t *a)
pthread_t client_thread_handle
Definition: echo_client.h:80
int svm_fifo_dequeue_drop(svm_fifo_t *f, u32 len)
Dequeue and drop bytes from fifo.
Definition: svm_fifo.c:1029
static transport_service_type_t session_transport_service_type(session_t *s)
static_always_inline void clib_spinlock_lock_if_init(clib_spinlock_t *p)
Definition: lock.h:95
struct session_dgram_header_ session_dgram_hdr_t
static int echo_clients_init(vlib_main_t *vm)
Definition: echo_client.c:332
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:978
static const u32 test_srv_crt_rsa_len
Definition: tls_test.h:47
static int app_recv_dgram(app_session_t *s, u8 *buf, u32 len)
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:171
u16 rmt_port
remote port (network order)