FD.io VPP  v21.01.1
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  svm_fifo_t *f = s->data.tx_fifo;
79  u32 max_enqueue = svm_fifo_max_enqueue_prod (f);
80 
81  if (max_enqueue < sizeof (session_dgram_hdr_t))
82  return;
83 
84  max_enqueue -= sizeof (session_dgram_hdr_t);
85 
86  if (ecm->no_copy)
87  {
89  app_session_transport_t *at = &s->data.transport;
90 
91  rv = clib_min (max_enqueue, bytes_this_chunk);
92 
93  hdr.data_length = rv;
94  hdr.data_offset = 0;
95  clib_memcpy_fast (&hdr.rmt_ip, &at->rmt_ip,
96  sizeof (ip46_address_t));
97  hdr.is_ip4 = at->is_ip4;
98  hdr.rmt_port = at->rmt_port;
99  clib_memcpy_fast (&hdr.lcl_ip, &at->lcl_ip,
100  sizeof (ip46_address_t));
101  hdr.lcl_port = at->lcl_port;
102  svm_fifo_enqueue (f, sizeof (hdr), (u8 *) & hdr);
103  svm_fifo_enqueue_nocopy (f, rv);
104  session_send_io_evt_to_thread_custom (&f->master_session_index,
105  s->thread_index,
107  }
108  else
109  {
110  bytes_this_chunk = clib_min (bytes_this_chunk, max_enqueue);
111  rv = app_send_dgram (&s->data, test_data + test_buf_offset,
112  bytes_this_chunk, 0);
113  }
114  }
115 
116  /* If we managed to enqueue data... */
117  if (rv > 0)
118  {
119  /* Account for it... */
120  s->bytes_to_send -= rv;
121  s->bytes_sent += rv;
122 
123  if (ECHO_CLIENT_DBG)
124  {
125  /* *INDENT-OFF* */
126  ELOG_TYPE_DECLARE (e) =
127  {
128  .format = "tx-enq: xfer %d bytes, sent %u remain %u",
129  .format_args = "i4i4i4",
130  };
131  /* *INDENT-ON* */
132  struct
133  {
134  u32 data[3];
135  } *ed;
137  ed->data[0] = rv;
138  ed->data[1] = s->bytes_sent;
139  ed->data[2] = s->bytes_to_send;
140  }
141  }
142 }
143 
144 static void
146 {
147  svm_fifo_t *rx_fifo = s->data.rx_fifo;
148  u32 thread_index = vlib_get_thread_index ();
149  int n_read, i;
150 
151  if (ecm->test_bytes)
152  {
153  if (!ecm->is_dgram)
154  n_read = app_recv_stream (&s->data, ecm->rx_buf[thread_index],
155  vec_len (ecm->rx_buf[thread_index]));
156  else
157  n_read = app_recv_dgram (&s->data, ecm->rx_buf[thread_index],
158  vec_len (ecm->rx_buf[thread_index]));
159  }
160  else
161  {
162  n_read = svm_fifo_max_dequeue_cons (rx_fifo);
163  svm_fifo_dequeue_drop (rx_fifo, n_read);
164  }
165 
166  if (n_read > 0)
167  {
168  if (ECHO_CLIENT_DBG)
169  {
170  /* *INDENT-OFF* */
171  ELOG_TYPE_DECLARE (e) =
172  {
173  .format = "rx-deq: %d bytes",
174  .format_args = "i4",
175  };
176  /* *INDENT-ON* */
177  struct
178  {
179  u32 data[1];
180  } *ed;
182  ed->data[0] = n_read;
183  }
184 
185  if (ecm->test_bytes)
186  {
187  for (i = 0; i < n_read; i++)
188  {
189  if (ecm->rx_buf[thread_index][i]
190  != ((s->bytes_received + i) & 0xff))
191  {
192  clib_warning ("read %d error at byte %lld, 0x%x not 0x%x",
193  n_read, s->bytes_received + i,
194  ecm->rx_buf[thread_index][i],
195  ((s->bytes_received + i) & 0xff));
196  ecm->test_failed = 1;
197  }
198  }
199  }
200  ASSERT (n_read <= s->bytes_to_receive);
201  s->bytes_to_receive -= n_read;
202  s->bytes_received += n_read;
203  }
204 }
205 
206 static uword
209 {
211  int my_thread_index = vlib_get_thread_index ();
212  eclient_session_t *sp;
213  int i;
214  int delete_session;
215  u32 *connection_indices;
216  u32 *connections_this_batch;
217  u32 nconnections_this_batch;
218 
219  connection_indices = ecm->connection_index_by_thread[my_thread_index];
220  connections_this_batch =
221  ecm->connections_this_batch_by_thread[my_thread_index];
222 
223  if ((ecm->run_test != ECHO_CLIENTS_RUNNING) ||
224  ((vec_len (connection_indices) == 0)
225  && vec_len (connections_this_batch) == 0))
226  return 0;
227 
228  /* Grab another pile of connections */
229  if (PREDICT_FALSE (vec_len (connections_this_batch) == 0))
230  {
231  nconnections_this_batch =
232  clib_min (ecm->connections_per_batch, vec_len (connection_indices));
233 
234  ASSERT (nconnections_this_batch > 0);
235  vec_validate (connections_this_batch, nconnections_this_batch - 1);
236  clib_memcpy_fast (connections_this_batch,
237  connection_indices + vec_len (connection_indices)
238  - nconnections_this_batch,
239  nconnections_this_batch * sizeof (u32));
240  _vec_len (connection_indices) -= nconnections_this_batch;
241  }
242 
244  && ecm->prev_conns == vec_len (connections_this_batch)))
245  {
246  ecm->repeats++;
247  ecm->prev_conns = vec_len (connections_this_batch);
248  if (ecm->repeats == 500000)
249  {
250  clib_warning ("stuck clients");
251  }
252  }
253  else
254  {
255  ecm->prev_conns = vec_len (connections_this_batch);
256  ecm->repeats = 0;
257  }
258 
259  for (i = 0; i < vec_len (connections_this_batch); i++)
260  {
261  delete_session = 1;
262 
263  sp = pool_elt_at_index (ecm->sessions, connections_this_batch[i]);
264 
265  if (sp->bytes_to_send > 0)
266  {
267  send_data_chunk (ecm, sp);
268  delete_session = 0;
269  }
270  if (sp->bytes_to_receive > 0)
271  {
272  delete_session = 0;
273  }
274  if (PREDICT_FALSE (delete_session == 1))
275  {
276  session_t *s;
277 
281 
282  if (s)
283  {
284  vnet_disconnect_args_t _a, *a = &_a;
285  a->handle = session_handle (s);
286  a->app_index = ecm->app_index;
288 
289  vec_delete (connections_this_batch, 1, i);
290  i--;
292  }
293  else
294  {
295  clib_warning ("session AWOL?");
296  vec_delete (connections_this_batch, 1, i);
297  }
298 
299  /* Kick the debug CLI process */
300  if (ecm->ready_connections == 0)
301  {
302  signal_evt_to_cli (2);
303  }
304  }
305  }
306 
307  ecm->connection_index_by_thread[my_thread_index] = connection_indices;
308  ecm->connections_this_batch_by_thread[my_thread_index] =
309  connections_this_batch;
310  return 0;
311 }
312 
313 /* *INDENT-OFF* */
315 {
316  .function = echo_client_node_fn,
317  .name = "echo-clients",
318  .type = VLIB_NODE_TYPE_INPUT,
319  .state = VLIB_NODE_STATE_DISABLED,
320 };
321 /* *INDENT-ON* */
322 
323 static int
325 {
328  u32 num_threads;
329  int i;
330 
331  num_threads = 1 /* main thread */ + vtm->n_threads;
332 
333  /* Init test data. Big buffer */
334  vec_validate (ecm->connect_test_data, 4 * 1024 * 1024 - 1);
335  for (i = 0; i < vec_len (ecm->connect_test_data); i++)
336  ecm->connect_test_data[i] = i & 0xff;
337 
338  vec_validate (ecm->rx_buf, num_threads - 1);
339  for (i = 0; i < num_threads; i++)
340  vec_validate (ecm->rx_buf[i], vec_len (ecm->connect_test_data) - 1);
341 
342  ecm->is_init = 1;
343 
348 
349  return 0;
350 }
351 
352 static int
354  session_t * s,
355  session_error_t err)
356 {
358  vnet_connect_args_t *a = 0;
359  int rv;
360  u8 thread_index = vlib_get_thread_index ();
362  u32 stream_n;
363  session_handle_t handle;
364 
365  DBG ("QUIC Connection handle %d", session_handle (s));
366 
367  vec_validate (a, 1);
368  a->uri = (char *) ecm->connect_uri;
369  if (parse_uri (a->uri, &sep))
370  return -1;
371  sep.parent_handle = handle = session_handle (s);
372 
373  for (stream_n = 0; stream_n < ecm->quic_streams; stream_n++)
374  {
375  clib_memset (a, 0, sizeof (*a));
376  a->app_index = ecm->app_index;
377  a->api_context = -1 - api_context;
378  clib_memcpy (&a->sep_ext, &sep, sizeof (sep));
379 
380  DBG ("QUIC opening stream %d", stream_n);
381  if ((rv = vnet_connect (a)))
382  {
383  clib_error ("Stream session %d opening failed: %d", stream_n, rv);
384  return -1;
385  }
386  DBG ("QUIC stream %d connected", stream_n);
387  }
388  /*
389  * 's' is no longer valid, its underlying pool could have been moved in
390  * vnet_connect()
391  */
392  vec_add1 (ecm->quic_session_index_by_thread[thread_index], handle);
393  vec_free (a);
394  return 0;
395 }
396 
397 static int
399  session_t * s,
400  session_error_t err)
401 {
403  eclient_session_t *session;
404  u32 session_index;
405  u8 thread_index;
406 
408  return -1;
409 
410  if (err)
411  {
412  clib_warning ("connection %d failed!", api_context);
414  signal_evt_to_cli (-1);
415  return 0;
416  }
417 
420  api_context, s,
421  err);
422  DBG ("STREAM Connection callback %d", api_context);
423 
424  thread_index = s->thread_index;
425  ASSERT (thread_index == vlib_get_thread_index ()
427 
428  if (!ecm->vpp_event_queue[thread_index])
429  ecm->vpp_event_queue[thread_index] =
430  session_main_get_vpp_event_queue (thread_index);
431 
432  /*
433  * Setup session
434  */
436  pool_get (ecm->sessions, session);
438 
439  clib_memset (session, 0, sizeof (*session));
440  session_index = session - ecm->sessions;
441  session->bytes_to_send = ecm->bytes_to_send;
442  session->bytes_to_receive = ecm->no_return ? 0ULL : ecm->bytes_to_send;
443  session->data.rx_fifo = s->rx_fifo;
444  session->data.rx_fifo->client_session_index = session_index;
445  session->data.tx_fifo = s->tx_fifo;
446  session->data.tx_fifo->client_session_index = session_index;
447  session->data.vpp_evt_q = ecm->vpp_event_queue[thread_index];
448  session->vpp_session_handle = session_handle (s);
449 
450  if (ecm->is_dgram)
451  {
453  tc = session_get_transport (s);
454  clib_memcpy_fast (&session->data.transport, tc,
455  sizeof (session->data.transport));
456  session->data.is_dgram = 1;
457  }
458 
459  vec_add1 (ecm->connection_index_by_thread[thread_index], session_index);
461  if (ecm->ready_connections == ecm->expected_connections)
462  {
464  /* Signal the CLI process that the action is starting... */
465  signal_evt_to_cli (1);
466  }
467 
468  return 0;
469 }
470 
471 static int
473  session_t * s, session_error_t err)
474 {
476  eclient_session_t *session;
477  u32 session_index;
478  u8 thread_index;
479 
481  return -1;
482 
483  if (err)
484  {
485  clib_warning ("connection %d failed!", api_context);
487  signal_evt_to_cli (-1);
488  return 0;
489  }
490 
491  thread_index = s->thread_index;
492  ASSERT (thread_index == vlib_get_thread_index ()
494 
495  if (!ecm->vpp_event_queue[thread_index])
496  ecm->vpp_event_queue[thread_index] =
497  session_main_get_vpp_event_queue (thread_index);
498 
499  /*
500  * Setup session
501  */
503  pool_get (ecm->sessions, session);
505 
506  clib_memset (session, 0, sizeof (*session));
507  session_index = session - ecm->sessions;
508  session->bytes_to_send = ecm->bytes_to_send;
509  session->bytes_to_receive = ecm->no_return ? 0ULL : ecm->bytes_to_send;
510  session->data.rx_fifo = s->rx_fifo;
511  session->data.rx_fifo->client_session_index = session_index;
512  session->data.tx_fifo = s->tx_fifo;
513  session->data.tx_fifo->client_session_index = session_index;
514  session->data.vpp_evt_q = ecm->vpp_event_queue[thread_index];
515  session->vpp_session_handle = session_handle (s);
516 
517  if (ecm->is_dgram)
518  {
520  tc = session_get_transport (s);
521  clib_memcpy_fast (&session->data.transport, tc,
522  sizeof (session->data.transport));
523  session->data.is_dgram = 1;
524  }
525 
526  vec_add1 (ecm->connection_index_by_thread[thread_index], session_index);
528  if (ecm->ready_connections == ecm->expected_connections)
529  {
531  /* Signal the CLI process that the action is starting... */
532  signal_evt_to_cli (1);
533  }
534 
535  return 0;
536 }
537 
538 static void
540 {
542  vnet_disconnect_args_t _a = { 0 }, *a = &_a;
543 
544  if (s->session_state == SESSION_STATE_READY)
545  clib_warning ("Reset active connection %U", format_session, s, 2);
546 
547  a->handle = session_handle (s);
548  a->app_index = ecm->app_index;
550  return;
551 }
552 
553 static int
555 {
556  return 0;
557 }
558 
559 static void
561 {
563  vnet_disconnect_args_t _a = { 0 }, *a = &_a;
564  a->handle = session_handle (s);
565  a->app_index = ecm->app_index;
567  return;
568 }
569 
570 void
572 {
574  vnet_disconnect_args_t _a = { 0 }, *a = &_a;
575  a->handle = session_handle (s);
576  a->app_index = ecm->app_index;
578 }
579 
580 static int
582 {
584  eclient_session_t *sp;
585 
587  {
589  return -1;
590  }
591 
592  sp = pool_elt_at_index (ecm->sessions, s->rx_fifo->client_session_index);
593  receive_data_chunk (ecm, sp);
594 
596  {
597  if (svm_fifo_set_event (s->rx_fifo))
599  }
600  return 0;
601 }
602 
603 int
604 echo_client_add_segment_callback (u32 client_index, u64 segment_handle)
605 {
606  /* New heaps may be added */
607  return 0;
608 }
609 
610 /* *INDENT-OFF* */
611 static session_cb_vft_t echo_clients = {
613  .session_connected_callback = echo_clients_session_connected_callback,
614  .session_accept_callback = echo_clients_session_create_callback,
615  .session_disconnect_callback = echo_clients_session_disconnect_callback,
616  .builtin_app_rx_callback = echo_clients_rx_callback,
617  .add_segment_callback = echo_client_add_segment_callback
618 };
619 /* *INDENT-ON* */
620 
621 static clib_error_t *
622 echo_clients_attach (u8 * appns_id, u64 appns_flags, u64 appns_secret)
623 {
624  vnet_app_add_tls_cert_args_t _a_cert, *a_cert = &_a_cert;
625  vnet_app_add_tls_key_args_t _a_key, *a_key = &_a_key;
626  u32 prealloc_fifos, segment_size = 256 << 20;
628  vnet_app_attach_args_t _a, *a = &_a;
629  u64 options[18];
630  int rv;
631 
632  clib_memset (a, 0, sizeof (*a));
633  clib_memset (options, 0, sizeof (options));
634 
635  a->api_client_index = ~0;
636  a->name = format (0, "echo_client");
637  if (ecm->transport_proto == TRANSPORT_PROTO_QUIC)
638  echo_clients.session_connected_callback =
640  a->session_cb_vft = &echo_clients;
641 
642  prealloc_fifos = ecm->prealloc_fifos ? ecm->expected_connections : 1;
643 
644  if (ecm->private_segment_size)
645  segment_size = ecm->private_segment_size;
646 
647  options[APP_OPTIONS_ACCEPT_COOKIE] = 0x12345678;
648  options[APP_OPTIONS_SEGMENT_SIZE] = segment_size;
649  options[APP_OPTIONS_ADD_SEGMENT_SIZE] = segment_size;
650  options[APP_OPTIONS_RX_FIFO_SIZE] = ecm->fifo_size;
651  options[APP_OPTIONS_TX_FIFO_SIZE] = ecm->fifo_size;
653  options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = prealloc_fifos;
654  options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
655  options[APP_OPTIONS_TLS_ENGINE] = ecm->tls_engine;
656  options[APP_OPTIONS_PCT_FIRST_ALLOC] = 100;
657  if (appns_id)
658  {
659  options[APP_OPTIONS_FLAGS] |= appns_flags;
660  options[APP_OPTIONS_NAMESPACE_SECRET] = appns_secret;
661  }
662  a->options = options;
663  a->namespace_id = appns_id;
664 
665  if ((rv = vnet_application_attach (a)))
666  return clib_error_return (0, "attach returned %d", rv);
667 
668  ecm->app_index = a->app_index;
669  vec_free (a->name);
670 
671  clib_memset (a_cert, 0, sizeof (*a_cert));
672  a_cert->app_index = a->app_index;
673  vec_validate (a_cert->cert, test_srv_crt_rsa_len);
675  vnet_app_add_tls_cert (a_cert);
676 
677  clib_memset (a_key, 0, sizeof (*a_key));
678  a_key->app_index = a->app_index;
679  vec_validate (a_key->key, test_srv_key_rsa_len);
681  vnet_app_add_tls_key (a_key);
682  return 0;
683 }
684 
685 static int
687 {
689  vnet_app_detach_args_t _da, *da = &_da;
690  int rv;
691 
692  da->app_index = ecm->app_index;
693  da->api_client_index = ~0;
694  rv = vnet_application_detach (da);
695  ecm->test_client_attached = 0;
696  ecm->app_index = ~0;
697  return rv;
698 }
699 
700 static void *
702 {
703  return 0;
704 }
705 
706 /** Start a transmit thread */
707 int
709 {
710  if (ecm->client_thread_handle == 0)
711  {
712  int rv = pthread_create (&ecm->client_thread_handle,
713  NULL /*attr */ ,
715  if (rv)
716  {
717  ecm->client_thread_handle = 0;
718  return -1;
719  }
720  }
721  return 0;
722 }
723 
724 clib_error_t *
726 {
728  vnet_connect_args_t _a, *a = &_a;
729  int i, rv;
730 
731  clib_memset (a, 0, sizeof (*a));
732 
733  for (i = 0; i < n_clients; i++)
734  {
735  a->uri = (char *) ecm->connect_uri;
736  a->api_context = i;
737  a->app_index = ecm->app_index;
738 
740  if ((rv = vnet_connect_uri (a)))
741  {
743  return clib_error_return (0, "connect returned: %d", rv);
744  }
746 
747  /* Crude pacing for call setups */
748  if ((i % 16) == 0)
749  vlib_process_suspend (vm, 100e-6);
750  ASSERT (i + 1 >= ecm->ready_connections);
751  while (i + 1 - ecm->ready_connections > 128)
752  vlib_process_suspend (vm, 1e-3);
753  }
754  return 0;
755 }
756 
757 #define ec_cli_output(_fmt, _args...) \
758  if (!ecm->no_output) \
759  vlib_cli_output(vm, _fmt, ##_args)
760 
761 static clib_error_t *
763  unformat_input_t * input, vlib_cli_command_t * cmd)
764 {
766  vlib_thread_main_t *thread_main = vlib_get_thread_main ();
767  u64 tmp, total_bytes, appns_flags = 0, appns_secret = 0;
768  f64 test_timeout = 20.0, syn_timeout = 20.0, delta;
769  char *default_uri = "tcp://6.0.1.1/1234";
770  uword *event_data = 0, event_type;
771  f64 time_before_connects;
772  u32 n_clients = 1;
773  int preallocate_sessions = 0;
774  char *transfer_type;
775  clib_error_t *error = 0;
776  u8 *appns_id = 0;
777  int i;
779  int rv;
780 
781  ecm->quic_streams = 1;
782  ecm->bytes_to_send = 8192;
783  ecm->no_return = 0;
784  ecm->fifo_size = 64 << 10;
785  ecm->connections_per_batch = 1000;
786  ecm->private_segment_count = 0;
787  ecm->private_segment_size = 0;
788  ecm->no_output = 0;
789  ecm->test_bytes = 0;
790  ecm->test_failed = 0;
791  ecm->vlib_main = vm;
793  ecm->no_copy = 0;
795 
796  if (thread_main->n_vlib_mains > 1)
798  vec_free (ecm->connect_uri);
799 
801  {
802  if (unformat (input, "uri %s", &ecm->connect_uri))
803  ;
804  else if (unformat (input, "nclients %d", &n_clients))
805  ;
806  else if (unformat (input, "quic-streams %d", &ecm->quic_streams))
807  ;
808  else if (unformat (input, "mbytes %lld", &tmp))
809  ecm->bytes_to_send = tmp << 20;
810  else if (unformat (input, "gbytes %lld", &tmp))
811  ecm->bytes_to_send = tmp << 30;
812  else if (unformat (input, "bytes %lld", &ecm->bytes_to_send))
813  ;
814  else if (unformat (input, "test-timeout %f", &test_timeout))
815  ;
816  else if (unformat (input, "syn-timeout %f", &syn_timeout))
817  ;
818  else if (unformat (input, "no-return"))
819  ecm->no_return = 1;
820  else if (unformat (input, "fifo-size %d", &ecm->fifo_size))
821  ecm->fifo_size <<= 10;
822  else if (unformat (input, "private-segment-count %d",
823  &ecm->private_segment_count))
824  ;
825  else if (unformat (input, "private-segment-size %U",
826  unformat_memory_size, &tmp))
827  {
828  if (tmp >= 0x100000000ULL)
829  return clib_error_return
830  (0, "private segment size %lld (%llu) too large", tmp, tmp);
831  ecm->private_segment_size = tmp;
832  }
833  else if (unformat (input, "preallocate-fifos"))
834  ecm->prealloc_fifos = 1;
835  else if (unformat (input, "preallocate-sessions"))
836  preallocate_sessions = 1;
837  else
838  if (unformat (input, "client-batch %d", &ecm->connections_per_batch))
839  ;
840  else if (unformat (input, "appns %_%v%_", &appns_id))
841  ;
842  else if (unformat (input, "all-scope"))
843  appns_flags |= (APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE
844  | APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE);
845  else if (unformat (input, "local-scope"))
846  appns_flags = APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE;
847  else if (unformat (input, "global-scope"))
848  appns_flags = APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
849  else if (unformat (input, "secret %lu", &appns_secret))
850  ;
851  else if (unformat (input, "no-output"))
852  ecm->no_output = 1;
853  else if (unformat (input, "test-bytes"))
854  ecm->test_bytes = 1;
855  else if (unformat (input, "tls-engine %d", &ecm->tls_engine))
856  ;
857  else
858  return clib_error_return (0, "failed: unknown input `%U'",
859  format_unformat_error, input);
860  }
861 
862  /* Store cli process node index for signalling */
863  ecm->cli_node_index =
865 
866  if (ecm->is_init == 0)
867  {
868  if (echo_clients_init (vm))
869  return clib_error_return (0, "failed init");
870  }
871 
872 
873  ecm->ready_connections = 0;
874  ecm->expected_connections = n_clients * ecm->quic_streams;
875  ecm->rx_total = 0;
876  ecm->tx_total = 0;
877 
878  if (!ecm->connect_uri)
879  {
880  clib_warning ("No uri provided. Using default: %s", default_uri);
881  ecm->connect_uri = format (0, "%s%c", default_uri, 0);
882  }
883 
884  if ((rv = parse_uri ((char *) ecm->connect_uri, &sep)))
885  return clib_error_return (0, "Uri parse error: %d", rv);
886  ecm->transport_proto = sep.transport_proto;
887  ecm->is_dgram = (sep.transport_proto == TRANSPORT_PROTO_UDP);
888 
889 #if ECHO_CLIENT_PTHREAD
891 #endif
892 
894  vnet_session_enable_disable (vm, 1 /* turn on session and transports */ );
896 
897  if (ecm->test_client_attached == 0)
898  {
899  if ((error = echo_clients_attach (appns_id, appns_flags, appns_secret)))
900  {
901  vec_free (appns_id);
902  clib_error_report (error);
903  return error;
904  }
905  vec_free (appns_id);
906  }
907  ecm->test_client_attached = 1;
908 
909  /* Turn on the builtin client input nodes */
910  for (i = 0; i < thread_main->n_vlib_mains; i++)
912  VLIB_NODE_STATE_POLLING);
913 
914  if (preallocate_sessions)
915  pool_init_fixed (ecm->sessions, 1.1 * n_clients);
916 
917  /* Fire off connect requests */
918  time_before_connects = vlib_time_now (vm);
919  if ((error = echo_clients_connect (vm, n_clients)))
920  {
921  goto cleanup;
922  }
923 
924  /* Park until the sessions come up, or ten seconds elapse... */
925  vlib_process_wait_for_event_or_clock (vm, syn_timeout);
926  event_type = vlib_process_get_events (vm, &event_data);
927  switch (event_type)
928  {
929  case ~0:
930  ec_cli_output ("Timeout with only %d sessions active...",
931  ecm->ready_connections);
932  error = clib_error_return (0, "failed: syn timeout with %d sessions",
933  ecm->ready_connections);
934  goto cleanup;
935 
936  case 1:
937  delta = vlib_time_now (vm) - time_before_connects;
938  if (delta != 0.0)
939  ec_cli_output ("%d three-way handshakes in %.2f seconds %.2f/s",
940  n_clients, delta, ((f64) n_clients) / delta);
941 
943  ec_cli_output ("Test started at %.6f", ecm->test_start_time);
944  break;
945 
946  default:
947  ec_cli_output ("unexpected event(1): %d", event_type);
948  error = clib_error_return (0, "failed: unexpected event(1): %d",
949  event_type);
950  goto cleanup;
951  }
952 
953  /* Now wait for the sessions to finish... */
954  vlib_process_wait_for_event_or_clock (vm, test_timeout);
955  event_type = vlib_process_get_events (vm, &event_data);
956  switch (event_type)
957  {
958  case ~0:
959  ec_cli_output ("Timeout with %d sessions still active...",
960  ecm->ready_connections);
961  error = clib_error_return (0, "failed: timeout with %d sessions",
962  ecm->ready_connections);
963  goto cleanup;
964 
965  case 2:
966  ecm->test_end_time = vlib_time_now (vm);
967  ec_cli_output ("Test finished at %.6f", ecm->test_end_time);
968  break;
969 
970  default:
971  ec_cli_output ("unexpected event(2): %d", event_type);
972  error = clib_error_return (0, "failed: unexpected event(2): %d",
973  event_type);
974  goto cleanup;
975  }
976 
977  delta = ecm->test_end_time - ecm->test_start_time;
978  if (delta != 0.0)
979  {
980  total_bytes = (ecm->no_return ? ecm->tx_total : ecm->rx_total);
981  transfer_type = ecm->no_return ? "half-duplex" : "full-duplex";
982  ec_cli_output ("%lld bytes (%lld mbytes, %lld gbytes) in %.2f seconds",
983  total_bytes, total_bytes / (1ULL << 20),
984  total_bytes / (1ULL << 30), delta);
985  ec_cli_output ("%.2f bytes/second %s", ((f64) total_bytes) / (delta),
986  transfer_type);
987  ec_cli_output ("%.4f gbit/second %s",
988  (((f64) total_bytes * 8.0) / delta / 1e9),
989  transfer_type);
990  }
991  else
992  {
993  ec_cli_output ("zero delta-t?");
994  error = clib_error_return (0, "failed: zero delta-t");
995  goto cleanup;
996  }
997 
998  if (ecm->test_bytes && ecm->test_failed)
999  error = clib_error_return (0, "failed: test bytes");
1000 
1001 cleanup:
1004  for (i = 0; i < vec_len (ecm->connection_index_by_thread); i++)
1005  {
1009  }
1010 
1011  pool_free (ecm->sessions);
1012 
1013  /* Detach the application, so we can use different fifo sizes next time */
1014  if (ecm->test_client_attached)
1015  {
1016  if (echo_clients_detach ())
1017  {
1018  error = clib_error_return (0, "failed: app detach");
1019  ec_cli_output ("WARNING: app detach failed...");
1020  }
1021  }
1022  if (error)
1023  ec_cli_output ("test failed");
1024  vec_free (ecm->connect_uri);
1025  return error;
1026 }
1027 
1028 /* *INDENT-OFF* */
1029 VLIB_CLI_COMMAND (echo_clients_command, static) =
1030 {
1031  .path = "test echo clients",
1032  .short_help = "test echo clients [nclients %d][[m|g]bytes <bytes>]"
1033  "[test-timeout <time>][syn-timeout <time>][no-return][fifo-size <size>]"
1034  "[private-segment-count <count>][private-segment-size <bytes>[m|g]]"
1035  "[preallocate-fifos][preallocate-sessions][client-batch <batch-size>]"
1036  "[uri <tcp://ip/port>][test-bytes][no-output]",
1037  .function = echo_clients_command_fn,
1038  .is_mp_safe = 1,
1039 };
1040 /* *INDENT-ON* */
1041 
1042 clib_error_t *
1044 {
1046  ecm->is_init = 0;
1047  return 0;
1048 }
1049 
1051 
1052 /*
1053  * fd.io coding-style-patch-verification: ON
1054  *
1055  * Local Variables:
1056  * eval: (c-set-style "gnu")
1057  * End:
1058  */
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:509
int echo_client_add_segment_callback(u32 client_index, u64 segment_handle)
Definition: echo_client.c:604
vlib_main_t vlib_global_main
Definition: main.c:2041
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:725
#define clib_min(x, y)
Definition: clib.h:328
static int echo_clients_session_create_callback(session_t *s)
Definition: echo_client.c:554
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:751
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:550
vlib_node_runtime_t node_runtime
Definition: node.h:551
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:544
svm_fifo_t * tx_fifo
struct _vnet_connect_args vnet_connect_args_t
static session_cb_vft_t echo_clients
Definition: echo_client.c:611
#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:645
u32 tls_engine
TLS engine mbedtls/openssl.
Definition: echo_client.h:65
static int echo_clients_detach()
Definition: echo_client.c:686
#define clib_memcpy_fast(a, b, c)
Definition: string.h:81
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
transport_connection_t * session_get_transport(session_t *s)
Definition: session.c:1631
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:334
static uword echo_client_node_fn(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: echo_client.c:207
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:129
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:592
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:935
static void receive_data_chunk(echo_client_main_t *ecm, eclient_session_t *s)
Definition: echo_client.c:145
volatile int run_test
Signal start of test.
Definition: echo_client.h:86
vlib_main_t * vm
Definition: in2out_ed.c:1580
u32 quic_streams
QUIC streams per connection.
Definition: echo_client.h:68
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:251
vlib_main_t ** vlib_mains
Definition: buffer.c:332
void echo_clients_session_disconnect(session_t *s)
Definition: echo_client.c:571
clib_error_t * vnet_app_add_tls_cert(vnet_app_add_tls_cert_args_t *a)
Definition: application.c:1324
unsigned char u8
Definition: types.h:56
u8 data[128]
Definition: ipsec_types.api:90
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.
static int quic_echo_clients_session_connected_callback(u32 app_index, u32 api_context, session_t *s, session_error_t err)
Definition: echo_client.c:398
double f64
Definition: types.h:142
#define vlib_worker_thread_barrier_sync(X)
Definition: threads.h:205
static session_handle_t session_handle(session_t *s)
#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:482
clib_error_t * echo_clients_main_init(vlib_main_t *vm)
Definition: echo_client.c:1043
#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:579
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:708
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:430
description fragment has unexpected format
Definition: map.api:433
#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:619
u32 app_index
app index after attach
Definition: echo_client.h:52
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:79
#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:560
static void clib_spinlock_init(clib_spinlock_t *p)
Definition: lock.h:65
struct _session_endpoint_cfg session_endpoint_cfg_t
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:1334
Definition: cJSON.c:84
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:762
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:546
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:1015
struct _unformat_input_t unformat_input_t
static void cleanup(void)
Definition: client.c:101
int svm_fifo_enqueue(svm_fifo_t *f, u32 len, const u8 *src)
Enqueue data to fifo.
Definition: svm_fifo.c:840
#define ELOG_DATA(em, f)
Definition: elog.h:484
#define PREDICT_FALSE(x)
Definition: clib.h:121
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:488
static const char test_srv_crt_rsa[]
Definition: tls_test.h:23
clib_error_t * vnet_session_enable_disable(vlib_main_t *vm, u8 is_en)
Definition: session.c:1793
int vnet_application_attach(vnet_app_attach_args_t *a)
Attach application to vpp.
Definition: application.c:816
static session_t * session_get_from_handle_if_valid(session_handle_t handle)
Definition: session.h:336
static void echo_clients_session_reset_callback(session_t *s)
Definition: echo_client.c:539
#define pool_free(p)
Free a pool.
Definition: pool.h:440
static u8 svm_fifo_set_event(svm_fifo_t *f)
Set fifo event flag.
Definition: svm_fifo.h:727
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:581
int(* session_connected_callback)(u32 app_wrk_index, u32 opaque, session_t *s, session_error_t code)
Connection request callback.
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:170
#define UNFORMAT_END_OF_INPUT
Definition: format.h:144
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:219
static clib_error_t * echo_clients_attach(u8 *appns_id, u64 appns_flags, u64 appns_secret)
Definition: echo_client.c:622
static vlib_process_t * vlib_get_current_process(vlib_main_t *vm)
Definition: node_funcs.h:444
vlib_node_registration_t echo_clients_node
(constructor) VLIB_REGISTER_NODE (echo_clients_node)
Definition: echo_client.c:314
sll srl srl sll sra u16x4 i
Definition: vector_sse42.h:317
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:380
#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:224
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)
static void send_data_chunk(echo_client_main_t *ecm, eclient_session_t *s)
Definition: echo_client.c:49
#define pool_init_fixed(pool, max_elts)
initialize a fixed-size, preallocated pool
Definition: pool.h:85
vlib_main_t vlib_node_runtime_t * node
Definition: in2out_ed.c:1580
static const char test_srv_key_rsa[]
Definition: tls_test.h:49
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:158
#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:854
int vnet_application_detach(vnet_app_detach_args_t *a)
Detach application from vpp.
Definition: application.c:886
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:174
#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:1018
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)
static int echo_clients_session_connected_callback(u32 app_index, u32 api_context, session_t *s, session_error_t err)
Definition: echo_client.c:472
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
vlib_main_t vlib_node_runtime_t vlib_frame_t * frame
Definition: in2out_ed.c:1581
#define ec_cli_output(_fmt, _args...)
Definition: echo_client.c:757
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:701
connectionless service
int vnet_disconnect_session(vnet_disconnect_args_t *a)
Definition: application.c:1086
#define SESSION_ENDPOINT_CFG_NULL
Definition: session_types.h:79
int session_send_io_evt_to_thread_custom(void *data, u32 thread_index, session_evt_type_t evt_type)
Definition: session.c:86
unformat_function_t unformat_memory_size
Definition: format.h:295
static void signal_evt_to_cli_i(int *code)
Definition: echo_client.c:31
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:1561
static vlib_thread_main_t * vlib_get_thread_main()
Definition: global_funcs.h:32
enum session_error_ session_error_t
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:1151
static transport_service_type_t session_transport_service_type(session_t *s)
struct _svm_fifo svm_fifo_t
static_always_inline void clib_spinlock_lock_if_init(clib_spinlock_t *p)
Definition: lock.h:106
struct session_dgram_header_ session_dgram_hdr_t
static int echo_clients_init(vlib_main_t *vm)
Definition: echo_client.c:324
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:170
u16 rmt_port
remote port (network order)
static int quic_echo_clients_qsession_connected_callback(u32 app_index, u32 api_context, session_t *s, session_error_t err)
Definition: echo_client.c:353