FD.io VPP  v20.05.1-6-gf53edbc3b
Vector Packet Processing
vpp_echo.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <stdio.h>
17 #include <signal.h>
18 
19 #include <vlibmemory/api.h>
20 #include <svm/fifo_segment.h>
21 
23 
25 
26 static void
28 {
29  /* We need to prealloc to avoid vec resize in threads */
30  echo_session_t *session;
31  int i;
32  for (i = 0; i < em->n_sessions; i++)
33  {
34  pool_get (em->sessions, session);
35  clib_memset (session, 0, sizeof (*session));
36  session->session_index = session - em->sessions;
38  session->session_state = ECHO_SESSION_STATE_INITIAL;
39  }
40 }
41 
42 static void
44 {
45  if (em->rx_results_diff)
46  CHECK_DIFF (ECHO_FAIL_TEST_ASSERT_RX_TOTAL, em->stats.rx_expected,
47  em->stats.rx_total, "Invalid amount of data received");
48  else
49  CHECK_SAME (ECHO_FAIL_TEST_ASSERT_RX_TOTAL, em->stats.rx_expected,
50  em->stats.rx_total, "Invalid amount of data received");
51 
52  if (em->tx_results_diff)
53  CHECK_DIFF (ECHO_FAIL_TEST_ASSERT_TX_TOTAL, em->stats.tx_expected,
54  em->stats.tx_total, "Invalid amount of data sent");
55  else
56  CHECK_SAME (ECHO_FAIL_TEST_ASSERT_TX_TOTAL, em->stats.tx_expected,
57  em->stats.tx_total, "Invalid amount of data sent");
58 
60  CHECK_SAME (ECHO_FAIL_TEST_ASSERT_ALL_SESSIONS_CLOSED,
62  "Some sessions are still open");
64 }
65 
66 always_inline void
68 {
69  int rv;
70  if (!svm_fifo_set_event (s->rx_fifo))
71  return;
72  if ((rv =
73  app_send_io_evt_to_vpp (s->vpp_evt_q, s->rx_fifo->master_session_index,
75  ECHO_FAIL (ECHO_FAIL_SEND_IO_EVT, "app_send_io_evt_to_vpp errored %d",
76  rv);
77  svm_fifo_clear_deq_ntf (s->rx_fifo);
78 }
79 
80 static void
81 stop_signal (int signum)
82 {
83  echo_main_t *em = &echo_main;
84  em->time_to_stop = 1;
85 }
86 
87 int
89 {
90  echo_main_t *em = &echo_main;
92 
93  if (em->use_sock_api)
94  {
95  if (vl_socket_client_connect ((char *) em->socket_name, name,
96  0 /* default rx, tx buffer */ ))
97  {
98  ECHO_FAIL (ECHO_FAIL_SOCKET_CONNECT, "socket connect failed");
99  return -1;
100  }
101 
102  if (vl_socket_client_init_shm (0, 1 /* want_pthread */ ))
103  {
104  ECHO_FAIL (ECHO_FAIL_INIT_SHM_API, "init shm api failed");
105  return -1;
106  }
107  }
108  else
109  {
110  if (vl_client_connect_to_vlib ("/vpe-api", name, 32) < 0)
111  {
112  ECHO_FAIL (ECHO_FAIL_SHMEM_CONNECT, "shmem connect failed");
113  return -1;
114  }
115  }
118  return 0;
119 }
120 
121 static void
123 {
124  u8 *start_evt =
126  u8 *end_evt =
128  u8 start_evt_missing = !(em->timing.events_sent & em->timing.start_event);
129  u8 end_evt_missing = (em->rx_results_diff || em->tx_results_diff) ? 0 :
130  !(em->timing.events_sent & em->timing.end_event);
131  f64 deltat = start_evt_missing || end_evt_missing ? 0 :
132  em->timing.end_time - em->timing.start_time;
133 
134  if (start_evt_missing)
135  ECHO_FAIL (ECHO_FAIL_MISSING_START_EVENT,
136  "Expected event %v to happen, but it did not!", start_evt);
137 
138  if (end_evt_missing)
139  ECHO_FAIL (ECHO_FAIL_MISSING_END_EVENT,
140  "Expected event %v to happen, but it did not!", end_evt);
141 
142  fformat (stdout, "vpp_echo JSON stats:\n{\n");
143  fformat (stdout, " \"role\": \"%s\",\n",
144  em->i_am_master ? "server" : "client");
145  fformat (stdout, " \"time\": \"%.9f\",\n", deltat);
146  fformat (stdout, " \"start_evt\": \"%v\",\n", start_evt);
147  fformat (stdout, " \"start_evt_missing\": \"%s\",\n",
148  start_evt_missing ? "True" : "False");
149  fformat (stdout, " \"end_evt\": \"%v\",\n", end_evt);
150  fformat (stdout, " \"end_evt_missing\": \"%s\",\n",
151  end_evt_missing ? "True" : "False");
152  fformat (stdout, " \"rx_data\": %lld,\n", em->stats.rx_total);
153  fformat (stdout, " \"tx_data\": %lld,\n", em->stats.tx_total);
154  fformat (stdout, " \"rx_bits_per_second\": %.1f,\n",
155  em->stats.rx_total * 8 / deltat);
156  fformat (stdout, " \"tx_bits_per_second\": %.1f,\n",
157  em->stats.tx_total * 8 / deltat);
158  fformat (stdout, " \"closing\": {\n");
159  fformat (stdout, " \"reset\": { \"q\": %d, \"s\": %d },\n",
160  em->stats.reset_count.q, em->stats.reset_count.s);
161  fformat (stdout, " \"recv evt\": { \"q\": %d, \"s\": %d },\n",
162  em->stats.close_count.q, em->stats.close_count.s);
163  fformat (stdout, " \"send evt\": { \"q\": %d, \"s\": %d },\n",
165  fformat (stdout, " \"clean\": { \"q\": %d, \"s\": %d },\n",
166  em->stats.clean_count.q, em->stats.clean_count.s);
167  fformat (stdout, " \"accepted\": { \"q\": %d, \"s\": %d },\n",
169  fformat (stdout, " \"connected\": { \"q\": %d, \"s\": %d }\n",
171  fformat (stdout, " },\n");
172  fformat (stdout, " \"results\": {\n");
173  fformat (stdout, " \"has_failed\": \"%d\",\n", em->has_failed);
174  fformat (stdout, " \"fail_descr\": \"%v\"\n", em->fail_descr);
175  fformat (stdout, " }\n");
176  fformat (stdout, "}\n");
177  fflush (stdout);
178  vec_free (start_evt);
179  vec_free (end_evt);
180 }
181 
182 static void
184 {
185  u8 *start_evt =
187  u8 *end_evt =
189  u8 start_evt_missing = !(em->timing.events_sent & em->timing.start_event);
190  u8 end_evt_missing = (em->rx_results_diff || em->tx_results_diff) ? 0 :
191  !(em->timing.events_sent & em->timing.end_event);
192  f64 deltat = start_evt_missing || end_evt_missing ? 0 :
193  em->timing.end_time - em->timing.start_time;
194 
195  if (start_evt_missing)
196  ECHO_FAIL (ECHO_FAIL_MISSING_START_EVENT,
197  "Expected event %v to happen, but it did not!", start_evt);
198 
199  if (end_evt_missing)
200  ECHO_FAIL (ECHO_FAIL_MISSING_END_EVENT,
201  "Expected event %v to happen, but it did not!", end_evt);
202 
203  fformat (stdout, "Timing %v:%v\n", start_evt, end_evt);
204  if (start_evt_missing)
205  fformat (stdout, "Missing Start Timing Event (%v)!\n", start_evt);
206  if (end_evt_missing)
207  fformat (stdout, "Missing End Timing Event (%v)!\n", end_evt);
208  fformat (stdout, "-------- TX --------\n");
209  fformat (stdout, "%lld bytes (%lld mbytes, %lld gbytes) in %.6f seconds\n",
210  em->stats.tx_total, em->stats.tx_total / (1ULL << 20),
211  em->stats.tx_total / (1ULL << 30), deltat);
212  if (deltat)
213  fformat (stdout, "%.4f Gbit/second\n",
214  (em->stats.tx_total * 8.0) / deltat / 1e9);
215  fformat (stdout, "-------- RX --------\n");
216  fformat (stdout, "%lld bytes (%lld mbytes, %lld gbytes) in %.6f seconds\n",
217  em->stats.rx_total, em->stats.rx_total / (1ULL << 20),
218  em->stats.rx_total / (1ULL << 30), deltat);
219  if (deltat)
220  fformat (stdout, "%.4f Gbit/second\n",
221  (em->stats.rx_total * 8.0) / deltat / 1e9);
222  fformat (stdout, "--------------------\n");
223  fformat (stdout, "Received close on %d streams (and %d Quic conn)\n",
224  em->stats.close_count.s, em->stats.close_count.q);
225  fformat (stdout, "Received reset on %d streams (and %d Quic conn)\n",
226  em->stats.reset_count.s, em->stats.reset_count.q);
227  fformat (stdout, "Sent close on %d streams (and %d Quic conn)\n",
229  fformat (stdout, "Discarded %d streams (and %d Quic conn)\n",
230  em->stats.clean_count.s, em->stats.clean_count.q);
231  fformat (stdout, "--------------------\n");
232  fformat (stdout, "Got accept on %d streams (and %d Quic conn)\n",
234  fformat (stdout, "Got connected on %d streams (and %d Quic conn)\n",
236  if (em->has_failed)
237  fformat (stdout, "\nFailure Return Status: %d\n%v", em->has_failed,
238  em->fail_descr);
239  vec_free (start_evt);
240  vec_free (end_evt);
241 }
242 
243 void
245 {
246 
247  ECHO_LOG (2, "[%lu/%lu] -> %U -> [%lu/%lu]",
250  s->bytes_sent + s->bytes_to_send);
251 
252  if (PREDICT_FALSE
253  ((em->stats.rx_total == em->stats.rx_expected)
254  && (em->stats.tx_total == em->stats.tx_expected)))
256 }
257 
258 static void
260 {
261  /* Free marked sessions */
262  echo_session_t *s;
263  u32 *session_indexes = 0, *session_index;
264 
265  /* *INDENT-OFF* */
266  pool_foreach (s, em->sessions,
267  ({
268  if (s->session_state == ECHO_SESSION_STATE_CLOSED)
269  vec_add1 (session_indexes, s->session_index);}
270  ));
271  /* *INDENT-ON* */
272  vec_foreach (session_index, session_indexes)
273  {
274  /* Free session */
275  s = pool_elt_at_index (em->sessions, *session_index);
278  clib_memset (s, 0xfe, sizeof (*s));
279  pool_put (em->sessions, s);
280  }
281 }
282 
283 static void
285  u32 n_read)
286 {
287  u32 i;
288  u8 expected;
289  for (i = 0; i < n_read; i++)
290  {
291  expected = (s->bytes_received + i) & 0xff;
292  if (rx_buf[i] == expected || em->max_test_msg > 0)
293  continue;
294  ECHO_LOG (1, "Session 0x%lx byte %lld was 0x%x expected 0x%x",
295  s->vpp_session_handle, s->bytes_received + i, rx_buf[i],
296  expected);
297  em->max_test_msg--;
298  if (em->max_test_msg == 0)
299  ECHO_LOG (1, "Too many errors, hiding next ones");
301  ECHO_FAIL (ECHO_FAIL_TEST_BYTES_ERR, "test-bytes errored");
302  }
303 }
304 
305 static int
307 {
308  int n_read;
309  n_read = app_recv ((app_session_t *) s, rx_buf, vec_len (rx_buf));
310  if (n_read <= 0)
311  return 0;
312  if (svm_fifo_needs_deq_ntf (s->rx_fifo, n_read))
314 
315  if (em->test_return_packets)
316  test_recv_bytes (em, s, rx_buf, n_read);
317 
318  s->bytes_received += n_read;
319  s->bytes_to_receive -= n_read;
320  clib_atomic_fetch_add (&em->stats.rx_total, n_read);
321  return n_read;
322 }
323 
324 static int
325 send_data_chunk (echo_session_t * s, u8 * tx_buf, int offset, int len)
326 {
327  int n_sent;
328  int bytes_this_chunk = clib_min (s->bytes_to_send, len - offset);
329  echo_main_t *em = &echo_main;
330 
331  if (!bytes_this_chunk)
332  return 0;
333  n_sent = app_send ((app_session_t *) s, tx_buf + offset,
334  bytes_this_chunk, SVM_Q_WAIT);
335  if (n_sent < 0)
336  return 0;
337  s->bytes_to_send -= n_sent;
338  s->bytes_sent += n_sent;
339  clib_atomic_fetch_add (&em->stats.tx_total, n_sent);
340  return n_sent;
341 }
342 
343 static int
345 {
346  u64 n_sent = 0;
347  while (n_sent < len && !em->time_to_stop)
348  n_sent += send_data_chunk (s, tx_buf, n_sent, len);
349  return n_sent;
350 }
351 
352 static inline void
354 {
355  echo_session_t *ls;
356  /* if parent has died, terminate gracefully */
358  {
359  ECHO_LOG (3, "%U: listener_index == SESSION_INVALID_INDEX",
361  return;
362  }
364  if (ls->session_state < ECHO_SESSION_STATE_CLOSING)
365  {
366  ECHO_LOG (3, "%U: ls->session_state (%d) < "
367  "ECHO_SESSION_STATE_CLOSING (%d)",
368  echo_format_session, ls, ls->session_state,
370  return;
371  }
372 
373  ECHO_LOG (3, "%U died, close child %U", echo_format_session, ls,
376  em->proto_cb_vft->cleanup_cb (s, 1 /* parent_died */ );
377 }
378 
379 /*
380  * Rx/Tx polling thread per connection
381  */
382 static void
384 {
385  int n_read, n_sent = 0;
386 
387  n_read = recv_data_chunk (em, s, rx_buf);
389  n_sent = send_data_chunk (s, em->connect_test_data,
390  s->bytes_sent % em->tx_buf_size,
391  em->tx_buf_size);
392  else if (em->data_source == ECHO_RX_DATA_SOURCE)
393  n_sent = mirror_data_chunk (em, s, rx_buf, n_read);
394  if (!s->bytes_to_send && !s->bytes_to_receive)
395  {
396  /* Session is done, need to close */
397  if (s->session_state == ECHO_SESSION_STATE_AWAIT_DATA)
398  s->session_state = ECHO_SESSION_STATE_CLOSING;
399  else
400  {
401  s->session_state = ECHO_SESSION_STATE_AWAIT_CLOSING;
403  {
407  }
409  {
410  s->session_state = ECHO_SESSION_STATE_CLOSING;
412  }
413  }
414  ECHO_LOG (3, "%U: %U", echo_format_session, s,
415  echo_format_session_state, s->session_state);
416  return;
417  }
418 
419  /* Check for idle clients */
420  if (em->log_lvl > 1)
421  {
422  if (n_sent || n_read)
423  s->idle_cycles = 0;
424  else if (s->idle_cycles++ == LOG_EVERY_N_IDLE_CYCLES)
425  {
426  s->idle_cycles = 0;
427  ECHO_LOG (2, "Idle client TX:%dB RX:%dB", s->bytes_to_send,
428  s->bytes_to_receive);
429  ECHO_LOG (2, "Idle FIFOs TX:%dB RX:%dB",
430  svm_fifo_max_dequeue (s->tx_fifo),
431  svm_fifo_max_dequeue (s->rx_fifo));
432  ECHO_LOG (2, "Session 0x%lx state %U", s->vpp_session_handle,
433  echo_format_session_state, s->session_state);
434  }
435  }
436 }
437 
438 static void *
440 {
441  clib_mem_set_thread_index (); /* First thing to do in client thread */
442  echo_main_t *em = &echo_main;
443  u32 N = em->n_clients;
444  u32 n = (N + em->n_rx_threads - 1) / em->n_rx_threads;
445  u32 idx = (u64) arg;
446  if (n * idx >= N)
447  {
448  ECHO_LOG (2, "Thread %u exiting, no sessions to care for", idx);
449  pthread_exit (0);
450  }
451  u32 thread_n_sessions = clib_min (n, N - n * idx);
452 
453  u32 i = 0;
454  u32 n_closed_sessions = 0;
455  u32 session_index;
456  u8 *rx_buf = 0;
457  echo_session_t *s;
458  vec_validate (rx_buf, em->rx_buf_size);
459 
460  for (i = 0; !em->time_to_stop; i = (i + 1) % thread_n_sessions)
461  {
462  n_closed_sessions = i == 0 ? 0 : n_closed_sessions;
463  session_index = em->data_thread_args[n * idx + i];
464  if (session_index == SESSION_INVALID_INDEX)
465  continue;
466  s = pool_elt_at_index (em->sessions, session_index);
467  switch (s->session_state)
468  {
471  echo_handle_data (em, s, rx_buf);
473  break;
475  ECHO_LOG (3, "%U: %U", echo_format_session, s,
476  echo_format_session_state, s->session_state);
478  break;
480  ECHO_LOG (3, "%U: %U", echo_format_session, s,
481  echo_format_session_state, s->session_state);
483  em->proto_cb_vft->cleanup_cb (s, 0 /* parent_died */ );
484  break;
486  ECHO_LOG (3, "%U: %U", echo_format_session, s,
487  echo_format_session_state, s->session_state);
488  n_closed_sessions++;
489  break;
490  }
491  if (n_closed_sessions == thread_n_sessions)
492  break;
493  }
494  ECHO_LOG (2, "Mission accomplished!");
495  pthread_exit (0);
496 }
497 
498 static void
500 {
501  echo_session_t *ls;
502  echo_main_t *em = &echo_main;
503 
504  ls = echo_get_session_from_handle (em, mp->handle);
505  if (!ls)
506  return;
507  em->proto_cb_vft->cleanup_cb (ls, 0 /* parent_died */ );
508  ls->session_state = ECHO_SESSION_STATE_CLOSED;
509  if (--em->listen_session_cnt == 0)
511 }
512 
513 static void
515 {
516  echo_main_t *em = &echo_main;
517  echo_session_t *listen_session;
518  if (mp->retval)
519  {
520  ECHO_FAIL (ECHO_FAIL_BIND, "bind failed: %U", format_api_error,
521  clib_net_to_host_u32 (mp->retval));
522  return;
523  }
524  ECHO_LOG (1, "listening on %U:%u", format_ip46_address, mp->lcl_ip,
526  clib_net_to_host_u16 (mp->lcl_port));
527 
528  /* Allocate local session and set it up */
529  listen_session = echo_session_new (em);
530  listen_session->session_type = ECHO_SESSION_TYPE_LISTEN;
531  listen_session->vpp_session_handle = mp->handle;
532  echo_session_handle_add_del (em, mp->handle, listen_session->session_index);
533  vec_add1 (em->listen_session_indexes, listen_session->session_index);
534  if (++em->listen_session_cnt == em->n_uris)
535  em->state = STATE_LISTEN;
536  if (em->proto_cb_vft->bound_uri_cb)
537  em->proto_cb_vft->bound_uri_cb (mp, listen_session);
538 }
539 
540 static int
542 {
543  echo_main_t *em = &echo_main;
544  uword *segment_present;
545  ECHO_LOG (3, "Check if segment mapped 0x%lx...", segment_handle);
547  segment_present = hash_get (em->shared_segment_handles, segment_handle);
549  if (segment_present != 0)
550  return 0;
551  ECHO_LOG (2, "Segment not mapped (0x%lx)", segment_handle);
552  return -1;
553 }
554 
555 static void
557 {
558  app_session_evt_t _app_evt, *app_evt = &_app_evt;
560  svm_fifo_t *rx_fifo, *tx_fifo;
561  echo_main_t *em = &echo_main;
562  echo_session_t *session, *ls;
563 
564  if (!(ls = echo_get_session_from_handle (em, mp->listener_handle)))
565  {
566  ECHO_FAIL (ECHO_FAIL_SESSION_ACCEPTED_BAD_LISTENER,
567  "Unknown listener handle 0x%lx", mp->listener_handle);
568  return;
569  }
571  {
572  ECHO_FAIL (ECHO_FAIL_ACCEPTED_WAIT_FOR_SEG_ALLOC,
573  "accepted wait_for_segment_allocation errored");
574  return;
575  }
576 
577  /* Allocate local session and set it up */
578  session = echo_session_new (em);
579  session->vpp_session_handle = mp->handle;
580 
581  rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
582  rx_fifo->client_session_index = session->session_index;
583  tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
584  tx_fifo->client_session_index = session->session_index;
585 
586  session->rx_fifo = rx_fifo;
587  session->tx_fifo = tx_fifo;
588 
589  /* session->transport needed by app_send_dgram */
590  clib_memcpy_fast (&session->transport.rmt_ip, &mp->rmt.ip,
591  sizeof (ip46_address_t));
592  session->transport.is_ip4 = mp->rmt.is_ip4;
593  session->transport.rmt_port = mp->rmt.port;
594  clib_memcpy_fast (&session->transport.lcl_ip, &em->uri_elts.ip,
595  sizeof (ip46_address_t));
596  session->transport.lcl_port = em->uri_elts.port;
597 
598  session->vpp_session_handle = mp->handle;
599  session->start = clib_time_now (&em->clib_time);
600  session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address,
601  svm_msg_q_t *);
602  session->listener_index = ls->session_index;
603 
604  /* Add it to lookup table */
605  ECHO_LOG (2, "Accepted session 0x%lx S[%u] -> 0x%lx S[%u]",
606  mp->handle, session->session_index,
607  mp->listener_handle, session->listener_index);
608  echo_session_handle_add_del (em, mp->handle, session->session_index);
609 
610  app_alloc_ctrl_evt_to_vpp (session->vpp_evt_q, app_evt,
612  rmp = (session_accepted_reply_msg_t *) app_evt->evt->data;
613  rmp->handle = mp->handle;
614  rmp->context = mp->context;
615  app_send_ctrl_evt_to_vpp (session->vpp_evt_q, app_evt);
616  em->proto_cb_vft->accepted_cb (mp, session);
617 }
618 
619 static void
621 {
622  echo_main_t *em = &echo_main;
623  echo_session_t *session;
624  u32 listener_index = htonl (mp->context);
625  svm_fifo_t *rx_fifo, *tx_fifo;
626 
628 
629  if (mp->retval)
630  {
631  if (em->proto_cb_vft->connected_cb)
632  em->
633  proto_cb_vft->connected_cb ((session_connected_bundled_msg_t *) mp,
634  listener_index, 1 /* is_failed */ );
635  return;
636  }
637 
638  session = echo_session_new (em);
640  {
641  ECHO_FAIL (ECHO_FAIL_CONNECTED_WAIT_FOR_SEG_ALLOC,
642  "connected wait_for_segment_allocation errored");
643  return;
644  }
645 
646  rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
647  rx_fifo->client_session_index = session->session_index;
648  tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
649  tx_fifo->client_session_index = session->session_index;
650 
651  session->rx_fifo = rx_fifo;
652  session->tx_fifo = tx_fifo;
653  session->vpp_session_handle = mp->handle;
654  session->start = clib_time_now (&em->clib_time);
655  session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address,
656  svm_msg_q_t *);
657  session->listener_index = listener_index;
658  /* session->transport needed by app_send_dgram */
659  clib_memcpy_fast (&session->transport.lcl_ip, &mp->lcl.ip,
660  sizeof (ip46_address_t));
661  session->transport.is_ip4 = mp->lcl.is_ip4;
662  session->transport.lcl_port = mp->lcl.port;
663  clib_memcpy_fast (&session->transport.rmt_ip, &em->uri_elts.ip,
664  sizeof (ip46_address_t));
665  session->transport.rmt_port = em->uri_elts.port;
666 
667  echo_session_handle_add_del (em, mp->handle, session->session_index);
669  session->session_index, 0 /* is_failed */ );
670 }
671 
672 /*
673  *
674  * End of ECHO callback definitions
675  *
676  */
677 
678 static void
680 {
681  app_session_evt_t _app_evt, *app_evt = &_app_evt;
683  echo_main_t *em = &echo_main;
684  echo_session_t *s;
685  if (!(s = echo_get_session_from_handle (em, mp->handle)))
686  {
687  ECHO_LOG (1, "Invalid vpp_session_handle: 0x%lx", mp->handle);
688  return;
689  }
690  if (s->session_state == ECHO_SESSION_STATE_CLOSED)
691  {
692  ECHO_LOG (2, "%U: already in ECHO_SESSION_STATE_CLOSED",
694  }
695  else
696  {
697  ECHO_LOG (2, "%U: passive close", echo_format_session, s);
698  em->proto_cb_vft->disconnected_cb (mp, s);
699  }
700  app_alloc_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt,
702  rmp = (session_disconnected_reply_msg_t *) app_evt->evt->data;
703  rmp->retval = 0;
704  rmp->handle = mp->handle;
705  rmp->context = mp->context;
706  app_send_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt);
707 }
708 
709 static void
711 {
712  app_session_evt_t _app_evt, *app_evt = &_app_evt;
713  echo_main_t *em = &echo_main;
715  echo_session_t *s = 0;
716  if (!(s = echo_get_session_from_handle (em, mp->handle)))
717  {
718  ECHO_LOG (1, "Invalid vpp_session_handle: 0x%lx", mp->handle);
719  return;
720  }
721  ECHO_LOG (2, "%U: session reset", echo_format_session, s);
722  em->proto_cb_vft->reset_cb (mp, s);
723 
724  app_alloc_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt,
726  rmp = (session_reset_reply_msg_t *) app_evt->evt->data;
727  rmp->retval = 0;
728  rmp->handle = mp->handle;
729  app_send_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt);
730 }
731 
732 static void
734 {
735  fifo_segment_main_t *sm = &echo_main.segment_main;
736  fifo_segment_create_args_t _a, *a = &_a;
737  echo_main_t *em = &echo_main;
738  int *fds = 0, i;
739  char *seg_name = (char *) mp->segment_name;
740  u64 segment_handle = mp->segment_handle;
741 
742  if (mp->fd_flags & SESSION_FD_F_MEMFD_SEGMENT)
743  {
744  vec_validate (fds, 1);
745  if (vl_socket_client_recv_fd_msg (fds, 1, 5))
746  {
747  ECHO_FAIL (ECHO_FAIL_VL_API_RECV_FD_MSG,
748  "vl_socket_client_recv_fd_msg failed");
749  goto failed;
750  }
751 
752  if (echo_ssvm_segment_attach (seg_name, SSVM_SEGMENT_MEMFD, fds[0]))
753  {
754  ECHO_FAIL (ECHO_FAIL_VL_API_SVM_FIFO_SEG_ATTACH,
755  "svm_fifo_segment_attach ('%s') "
756  "failed on SSVM_SEGMENT_MEMFD", seg_name);
757  goto failed;
758  }
759  vec_free (fds);
760  }
761  else
762  {
763  clib_memset (a, 0, sizeof (*a));
764  a->segment_name = seg_name;
765  a->segment_size = mp->segment_size;
766  /* Attach to the segment vpp created */
767  if (fifo_segment_attach (sm, a))
768  {
769  ECHO_FAIL (ECHO_FAIL_VL_API_FIFO_SEG_ATTACH,
770  "fifo_segment_attach ('%s') failed", seg_name);
771  goto failed;
772  }
773  }
774  echo_segment_handle_add_del (em, segment_handle, 1 /* add */ );
775  ECHO_LOG (2, "Mapped segment 0x%lx", segment_handle);
776  return;
777 
778 failed:
779  for (i = 0; i < vec_len (fds); i++)
780  close (fds[i]);
781  vec_free (fds);
782 }
783 
784 static void
786 {
787  echo_main_t *em = &echo_main;
788  echo_segment_handle_add_del (em, mp->segment_handle, 0 /* add */ );
789  ECHO_LOG (2, "Unmaped segment 0x%lx", mp->segment_handle);
790 }
791 
792 static void
794 {
795  ECHO_LOG (1, "Cleanup confirmed for 0x%lx", mp->handle);
796 }
797 
798 static void
799 handle_mq_event (session_event_t * e)
800 {
801  switch (e->event_type)
802  {
804  return session_bound_handler ((session_bound_msg_t *) e->data);
811  e->data);
813  return session_reset_handler ((session_reset_msg_t *) e->data);
816  e->data);
819  break;
822  break;
825  break;
826  case SESSION_IO_EVT_RX:
827  break;
828  default:
829  ECHO_LOG (1, "unhandled event %u", e->event_type);
830  }
831 }
832 
833 static void
835 {
836  echo_rpc_msg_t *rpc;
837  svm_msg_q_msg_t msg;
838  svm_msg_q_t *mq = em->rpc_msq_queue;
839 
840  while (em->state < STATE_DATA_DONE && !em->time_to_stop)
841  {
842  svm_msg_q_lock (mq);
843  if (svm_msg_q_is_empty (mq) && svm_msg_q_timedwait (mq, 1))
844  {
845  svm_msg_q_unlock (mq);
846  continue;
847  }
848  svm_msg_q_sub_w_lock (mq, &msg);
849  rpc = svm_msg_q_msg_data (mq, &msg);
850  svm_msg_q_unlock (mq);
851  ((echo_rpc_t) rpc->fp) (em, &rpc->args);
852  svm_msg_q_free_msg (mq, &msg);
853  }
854 }
855 
856 static inline void
858 {
859  f64 delta, now = clib_time_now (&em->clib_time);
860  echo_stats_t _st, *st = &_st;
861  echo_stats_t *lst = &em->last_stat_sampling;
862  delta = now - em->last_stat_sampling_ts;
863  if (delta < em->periodic_stats_delta)
864  return;
865 
866  clib_memcpy_fast (st, &em->stats, sizeof (*st));
867  if (st->rx_total - lst->rx_total)
869  (st->rx_total - lst->rx_total) / delta);
870  if (st->tx_total - lst->tx_total)
872  (st->tx_total - lst->tx_total) / delta);
873  if (st->connected_count.q - lst->connected_count.q)
874  clib_warning ("conn: %d/s",
875  st->connected_count.q - lst->connected_count.q);
876  if (st->accepted_count.q - lst->accepted_count.q)
877  clib_warning ("accept: %d/s",
878  st->accepted_count.q - lst->accepted_count.q);
879 
880  clib_memcpy_fast (lst, st, sizeof (*st));
881  em->last_stat_sampling_ts = now;
882 }
883 
884 static void *
885 echo_mq_thread_fn (void *arg)
886 {
887  clib_mem_set_thread_index (); /* First thing to do in client thread */
888  svm_msg_q_msg_t *msg_vec = 0;
889  echo_main_t *em = &echo_main;
890  session_event_t *e;
891  svm_msg_q_msg_t *msg;
892  svm_msg_q_t *mq;
893  int i;
894 
895  vec_validate (msg_vec, em->evt_q_size);
896  vec_reset_length (msg_vec);
898  mq = em->app_mq;
899  if (em->state < STATE_ATTACHED || !mq)
900  {
901  ECHO_FAIL (ECHO_FAIL_APP_ATTACH, "Application failed to attach");
902  pthread_exit (0);
903  }
904 
905  while (em->state < STATE_DETACHED && !em->time_to_stop)
906  {
907  if (em->periodic_stats_delta)
909 
910  svm_msg_q_lock (mq);
911  if (svm_msg_q_is_empty (mq) && svm_msg_q_timedwait (mq, 1))
912  {
913  svm_msg_q_unlock (mq);
914  continue;
915  }
916  for (i = 0; i < svm_msg_q_size (mq); i++)
917  {
918  vec_add2 (msg_vec, msg, 1);
919  svm_msg_q_sub_w_lock (mq, msg);
920  }
921  svm_msg_q_unlock (mq);
922 
923  for (i = 0; i < vec_len (msg_vec); i++)
924  {
925  msg = vec_elt_at_index (msg_vec, i);
926  e = svm_msg_q_msg_data (mq, msg);
927  handle_mq_event (e);
928  svm_msg_q_free_msg (mq, msg); /* No lock, single thread dequeuing */
929  }
930  vec_reset_length (msg_vec);
931  }
932  vec_free (msg_vec);
933  pthread_exit (0);
934 }
935 
936 static inline void
937 echo_cycle_ip (echo_main_t * em, ip46_address_t * ip, ip46_address_t * src_ip,
938  u32 i)
939 {
940  u8 *ipu8;
941  u8 l;
942  if (i % em->n_uris == 0)
943  {
944  clib_memcpy_fast (ip, src_ip, sizeof (*ip));
945  return;
946  }
947  l = em->uri_elts.is_ip4 ? 3 : 15;
948  ipu8 = em->uri_elts.is_ip4 ? ip->ip4.as_u8 : ip->ip6.as_u8;
949  while (ipu8[l] == 0xf)
950  ipu8[l--] = 0;
951  if (l)
952  ipu8[l]++;
953 }
954 
955 static void
957 {
958  echo_connect_args_t _a, *a = &_a;
959  u64 i;
960 
963  clib_memset (&a->lcl_ip, 0, sizeof (a->lcl_ip));
964 
966  for (i = 0; i < em->n_connects; i++)
967  {
968  echo_cycle_ip (em, &a->ip, &em->uri_elts.ip, i);
969  if (em->lcl_ip_set)
970  echo_cycle_ip (em, &a->lcl_ip, &em->lcl_ip, i);
971  echo_send_connect (em, a);
972  }
974  ECHO_LOG (2, "App is ready");
975  echo_process_rpcs (em);
976 }
977 
978 static void
980 {
981  echo_session_t *ls;
982  ip46_address_t _ip, *ip = &_ip;
983  u32 *listen_session_index;
984  u32 i;
985 
986  for (i = 0; i < em->n_uris; i++)
987  {
988  echo_cycle_ip (em, ip, &em->uri_elts.ip, i);
989  echo_send_listen (em, ip);
990  }
992  ECHO_LOG (2, "App is ready");
993  echo_process_rpcs (em);
994  /* Cleanup */
995  vec_foreach (listen_session_index, em->listen_session_indexes)
996  {
997  ECHO_LOG (2, "Unbind listen port %d", em->listen_session_cnt);
998  ls = pool_elt_at_index (em->sessions, *listen_session_index);
999  echo_send_unbind (em, ls);
1000  }
1002  {
1003  ECHO_FAIL (ECHO_FAIL_SERVER_DISCONNECT_TIMEOUT,
1004  "Timeout waiting for state disconnected");
1005  return;
1006  }
1007 }
1008 
1009 static void
1011 {
1012  echo_main_t *em = &echo_main;
1013  int i;
1014  fprintf (stderr,
1015  "Usage: vpp_echo [socket-name SOCKET] [client|server] [uri URI] [OPTIONS]\n"
1016  "Generates traffic and assert correct teardown of the hoststack\n"
1017  "\n"
1018  " socket-name PATH Specify the binary socket path to connect to VPP\n"
1019  " use-svm-api Use SVM API to connect to VPP\n"
1020  " test-bytes[:assert] Check data correctness when receiving (assert fails on first error)\n"
1021  " fifo-size N[K|M|G] Use N[K|M|G] fifos\n"
1022  " mq-size N Use mq with N slots for [vpp_echo->vpp] communication\n"
1023  " max-sim-connects N Do not allow more than N mq events inflight\n"
1024  " rx-buf N[K|M|G] Use N[Kb|Mb|GB] RX buffer\n"
1025  " tx-buf N[K|M|G] Use N[Kb|Mb|GB] TX test buffer\n"
1026  " appns NAMESPACE Use the namespace NAMESPACE\n"
1027  " all-scope all-scope option\n"
1028  " local-scope local-scope option\n"
1029  " global-scope global-scope option\n"
1030  " secret SECRET set namespace secret\n"
1031  " chroot prefix PATH Use PATH as memory root path\n"
1032  " sclose=[Y|N|W] When stream is done, send[Y]|nop[N]|wait[W] for close\n"
1033  " nuris N Cycle through N consecutive (src&dst) ips when creating connections\n"
1034  " lcl IP Set the local ip to use as a client (use with nuris to set first src ip)\n"
1035  "\n"
1036  " time START:END Time between evts START & END, events being :\n"
1037  " start - Start of the app\n"
1038  " qconnect - first Connection connect sent\n"
1039  " qconnected - last Connection connected\n"
1040  " sconnect - first Stream connect sent\n"
1041  " sconnected - last Stream got connected\n"
1042  " lastbyte - Last expected byte received\n"
1043  " exit - Exiting of the app\n"
1044  " rx-results-diff Rx results different to pass test\n"
1045  " tx-results-diff Tx results different to pass test\n"
1046  " json Output global stats in json\n"
1047  " stats N Output stats evry N secs\n"
1048  " log=N Set the log level to [0: no output, 1:errors, 2:log]\n"
1049  " crypto [engine] Set the crypto engine [openssl, vpp, picotls, mbedtls]\n"
1050  "\n"
1051  " nclients N Open N clients sending data\n"
1052  " nthreads N Use N busy loop threads for data [in addition to main & msg queue]\n"
1053  " TX=1337[K|M|G]|RX Send 1337 [K|M|G]bytes, use TX=RX to reflect the data\n"
1054  " RX=1337[K|M|G] Expect 1337 [K|M|G]bytes\n" "\n");
1055  for (i = 0; i < vec_len (em->available_proto_cb_vft); i++)
1056  {
1058  if (vft && vft->print_usage_cb)
1059  vft->print_usage_cb ();
1060  }
1061  fprintf (stderr, "\nDefault configuration is :\n"
1062  " server nclients 1 [quic-streams 1] RX=64Kb TX=RX\n"
1063  " client nclients 1 [quic-streams 1] RX=64Kb TX=64Kb\n");
1064  exit (ECHO_FAIL_USAGE);
1065 }
1066 
1067 static int
1069 {
1070  echo_main_t *em = &echo_main;
1071  int i, rv;
1072  for (i = 0; i < vec_len (em->available_proto_cb_vft); i++)
1073  {
1075  if (vft && vft->process_opts_cb)
1076  if ((rv = vft->process_opts_cb (a)))
1077  return rv;
1078  }
1079  return 0;
1080 }
1081 
1082 static void
1084 {
1085  int i;
1086  for (i = 0; i < vec_len (em->available_proto_cb_vft); i++)
1087  {
1089  if (vft && vft->set_defaults_before_opts_cb)
1091  }
1092 }
1093 
1094 void
1095 echo_process_opts (int argc, char **argv)
1096 {
1097  echo_main_t *em = &echo_main;
1098  unformat_input_t _argv, *a = &_argv;
1099  u8 *chroot_prefix;
1100  u8 *uri = 0;
1101  u8 default_f_active;
1102  uword tmp;
1103 
1104  unformat_init_command_line (a, argv);
1106  {
1108  ;
1109  else if (unformat (a, "chroot prefix %s", &chroot_prefix))
1110  vl_set_memory_root_path ((char *) chroot_prefix);
1111  else if (unformat (a, "uri %s", &uri))
1112  em->uri = format (0, "%s%c", uri, 0);
1113  else if (unformat (a, "lcl %U", unformat_ip46_address, &em->lcl_ip))
1114  em->lcl_ip_set = 1;
1115  else if (unformat (a, "nuris %u", &em->n_uris))
1116  em->n_sessions = em->n_clients + em->n_uris;
1117  else if (unformat (a, "server"))
1118  em->i_am_master = 1;
1119  else if (unformat (a, "client"))
1120  em->i_am_master = 0;
1121  else if (unformat (a, "test-bytes:assert"))
1123  else if (unformat (a, "test-bytes"))
1125  else if (unformat (a, "socket-name %s", &em->socket_name))
1126  ;
1127  else if (unformat (a, "use-svm-api"))
1128  em->use_sock_api = 0;
1129  else if (unformat (a, "fifo-size %U", unformat_memory_size, &tmp))
1130  {
1131  if (tmp >= 0x100000000ULL)
1132  {
1133  fprintf (stderr,
1134  "ERROR: fifo-size %ld (0x%lx) too large\n", tmp, tmp);
1136  }
1137  em->fifo_size = tmp;
1138  }
1139  else if (unformat (a, "prealloc-fifos %u", &em->prealloc_fifo_pairs))
1140  ;
1141  else
1142  if (unformat (a, "rx-buf %U", unformat_data_size, &em->rx_buf_size))
1143  ;
1144  else
1145  if (unformat (a, "tx-buf %U", unformat_data_size, &em->tx_buf_size))
1146  ;
1147  else if (unformat (a, "mq-size %d", &em->evt_q_size))
1148  ;
1149  else if (unformat (a, "nclients %d", &em->n_clients))
1150  {
1151  em->n_sessions = em->n_clients + em->n_uris;
1152  em->n_connects = em->n_clients;
1153  }
1154  else if (unformat (a, "nthreads %d", &em->n_rx_threads))
1155  ;
1156  else if (unformat (a, "crypto %U", echo_unformat_crypto_engine, &tmp))
1157  em->crypto_engine = tmp;
1158  else if (unformat (a, "appns %_%v%_", &em->appns_id))
1159  ;
1160  else if (unformat (a, "all-scope"))
1161  em->appns_flags |= (APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE
1162  | APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE);
1163  else if (unformat (a, "local-scope"))
1164  em->appns_flags = APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE;
1165  else if (unformat (a, "global-scope"))
1166  em->appns_flags = APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
1167  else if (unformat (a, "secret %lu", &em->appns_secret))
1168  ;
1169  else if (unformat (a, "TX=RX"))
1171  else if (unformat (a, "TX=%U", unformat_data_size, &em->bytes_to_send))
1172  ;
1173  else if (unformat (a, "RX=%U", unformat_data_size,
1174  &em->bytes_to_receive))
1175  ;
1176  else if (unformat (a, "rx-results-diff"))
1177  em->rx_results_diff = 1;
1178  else if (unformat (a, "tx-results-diff"))
1179  em->tx_results_diff = 1;
1180  else if (unformat (a, "json"))
1181  em->output_json = 1;
1182  else if (unformat (a, "stats %d", &em->periodic_stats_delta))
1183  ;
1184  else if (unformat (a, "wait-for-gdb"))
1185  em->wait_for_gdb = 1;
1186  else if (unformat (a, "log=%d", &em->log_lvl))
1187  ;
1188  else if (unformat (a, "sclose=%U",
1190  ;
1191  else if (unformat (a, "time %U:%U",
1194  ;
1195  else if (unformat (a, "max-sim-connects %d", &em->max_sim_connects))
1196  ;
1197  else
1199  }
1200 
1201  /* setting default for unset values
1202  *
1203  * bytes_to_send / bytes_to_receive & data_source */
1204  if (em->bytes_to_receive == (u64) ~ 0)
1205  em->bytes_to_receive = 64 << 10; /* default */
1206  if (em->bytes_to_send == (u64) ~ 0)
1207  em->bytes_to_send = 64 << 10; /* default */
1208  else if (em->bytes_to_send == 0)
1210  else
1212 
1214  em->data_source =
1216  if (em->data_source == ECHO_RX_DATA_SOURCE)
1217  em->bytes_to_send = em->bytes_to_receive;
1218 
1219  /* disconnect flags */
1220  if (em->i_am_master)
1221  default_f_active =
1223  else
1224  default_f_active =
1227  em->send_stream_disconnects = default_f_active;
1228 
1229  if (em->max_sim_connects == 0)
1230  em->max_sim_connects = em->evt_q_size >> 1;
1231 
1232  if (em->wait_for_gdb)
1233  {
1234  volatile u64 nop = 0;
1235 
1236  clib_warning ("Waiting for gdb...");
1237  while (em->wait_for_gdb)
1238  nop++;
1239  clib_warning ("Resuming execution (%llu)!", nop);
1240  }
1241 }
1242 
1243 void
1245 {
1246  unformat_input_t _input, *input = &_input;
1247  u32 port;
1248  unformat_init_string (input, (char *) em->uri, strlen ((char *) em->uri));
1249  if (unformat
1250  (input, "%U://%U/%d", unformat_transport_proto,
1252  &em->uri_elts.ip.ip4, &port))
1253  em->uri_elts.is_ip4 = 1;
1254  else
1255  if (unformat
1256  (input, "%U://%U/%d", unformat_transport_proto,
1258  &em->uri_elts.ip.ip6, &port))
1259  em->uri_elts.is_ip4 = 0;
1260  else
1261  ECHO_FAIL (ECHO_FAIL_INVALID_URI, "Unable to process uri");
1262  em->uri_elts.port = clib_host_to_net_u16 (port);
1263  unformat_free (input);
1264 }
1265 
1266 static void __clib_constructor
1268 {
1269  /* init memory before proto register themselves */
1270  echo_main_t *em = &echo_main;
1271  clib_mem_init_thread_safe (0, 256 << 20);
1272  clib_memset (em, 0, sizeof (*em));
1273 }
1274 
1275 int
1276 main (int argc, char **argv)
1277 {
1278  echo_main_t *em = &echo_main;
1279  fifo_segment_main_t *sm = &em->segment_main;
1280  char *app_name;
1281  u64 i;
1282  svm_msg_q_cfg_t _cfg, *cfg = &_cfg;
1283  u32 rpc_queue_size = 256 << 10;
1284 
1285  em->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
1287  em->shared_segment_handles = hash_create (0, sizeof (uword));
1289  em->socket_name = format (0, "%s%c", API_SOCKET_FILE, 0);
1290  em->use_sock_api = 1;
1291  em->fifo_size = 64 << 10;
1292  em->prealloc_fifo_pairs = 16;
1293  em->n_clients = 1;
1294  em->n_connects = 1;
1295  em->n_sessions = 2;
1296  em->max_test_msg = 50;
1297  em->time_to_stop = 0;
1298  em->i_am_master = 1;
1299  em->n_rx_threads = 4;
1300  em->evt_q_size = 256;
1301  em->lcl_ip_set = 0;
1302  clib_memset (&em->lcl_ip, 0, sizeof (em->lcl_ip));
1306  em->bytes_to_receive = ~0; /* defaulted when we know if server/client */
1307  em->bytes_to_send = ~0; /* defaulted when we know if server/client */
1308  em->rx_buf_size = 1 << 20;
1309  em->tx_buf_size = 1 << 20;
1311  em->uri = format (0, "%s%c", "tcp://0.0.0.0/1234", 0);
1312  em->n_uris = 1;
1313  em->max_sim_connects = 0;
1314  em->listen_session_cnt = 0;
1317  echo_process_opts (argc, argv);
1318  echo_process_uri (em);
1320  if (!em->proto_cb_vft)
1321  {
1322  ECHO_FAIL (ECHO_FAIL_PROTOCOL_NOT_SUPPORTED,
1323  "Protocol %U is not supported",
1325  goto exit_on_error;
1326  }
1329 
1330  em->stats.rx_expected = em->bytes_to_receive * em->n_clients;
1331  em->stats.tx_expected = em->bytes_to_send * em->n_clients;
1332 
1335  for (i = 0; i < em->n_clients; i++)
1337  clib_time_init (&em->clib_time);
1341  for (i = 0; i < em->tx_buf_size; i++)
1342  em->connect_test_data[i] = i & 0xff;
1343 
1344  /* *INDENT-OFF* */
1345  svm_msg_q_ring_cfg_t rc[1] = {
1346  {rpc_queue_size, sizeof (echo_rpc_msg_t), 0},
1347  };
1348  /* *INDENT-ON* */
1349  cfg->consumer_pid = getpid ();
1350  cfg->n_rings = 1;
1351  cfg->q_nitems = rpc_queue_size;
1352  cfg->ring_cfgs = rc;
1353  em->rpc_msq_queue = svm_msg_q_alloc (cfg);
1354 
1355  signal (SIGINT, stop_signal);
1356  signal (SIGQUIT, stop_signal);
1357  signal (SIGTERM, stop_signal);
1358  echo_api_hookup (em);
1359 
1360  app_name = em->i_am_master ? "echo_server" : "echo_client";
1361  if (connect_to_vpp (app_name))
1362  {
1363  svm_region_exit ();
1364  ECHO_FAIL (ECHO_FAIL_CONNECT_TO_VPP, "Couldn't connect to vpp");
1365  goto exit_on_error;
1366  }
1367 
1368  echo_session_prealloc (em);
1370 
1371  echo_send_attach (em);
1373  {
1374  ECHO_FAIL (ECHO_FAIL_ATTACH_TO_VPP,
1375  "Couldn't attach to vpp, did you run <session enable> ?");
1376  goto exit_on_error;
1377  }
1378 
1379  if (em->uri_elts.transport_proto != TRANSPORT_PROTO_QUIC
1380  && em->uri_elts.transport_proto != TRANSPORT_PROTO_TLS)
1381  em->state = STATE_ATTACHED;
1382  else
1383  {
1384  ECHO_LOG (2, "Adding crypto context %U", echo_format_crypto_engine,
1385  em->crypto_engine);
1388  {
1389  ECHO_FAIL (ECHO_FAIL_APP_ATTACH,
1390  "Couldn't add crypto context to vpp\n");
1391  exit (1);
1392  }
1393  }
1394 
1395  if (pthread_create (&em->mq_thread_handle,
1396  NULL /*attr */ , echo_mq_thread_fn, 0))
1397  {
1398  ECHO_FAIL (ECHO_FAIL_PTHREAD_CREATE, "pthread create errored");
1399  goto exit_on_error;
1400  }
1401 
1402  for (i = 0; i < em->n_rx_threads; i++)
1403  if (pthread_create (&em->data_thread_handles[i],
1404  NULL /*attr */ , echo_data_thread_fn, (void *) i))
1405  {
1406  ECHO_FAIL (ECHO_FAIL_PTHREAD_CREATE,
1407  "pthread create errored (index %d)", i);
1408  goto exit_on_error;
1409  }
1410  if (em->i_am_master)
1411  server_run (em);
1412  else
1413  clients_run (em);
1415  echo_free_sessions (em);
1418  {
1419  ECHO_FAIL (ECHO_FAIL_DEL_CERT_KEY, "Couldn't cleanup cert and key");
1420  goto exit_on_error;
1421  }
1422 
1423  echo_send_detach (em);
1425  {
1426  ECHO_FAIL (ECHO_FAIL_DETACH, "Couldn't detach from vpp");
1427  goto exit_on_error;
1428  }
1429  int *rv;
1430  pthread_join (em->mq_thread_handle, (void **) &rv);
1431  if (rv)
1432  {
1433  ECHO_FAIL (ECHO_FAIL_MQ_PTHREAD, "mq pthread errored %d", rv);
1434  goto exit_on_error;
1435  }
1436  if (em->use_sock_api)
1438  else
1441 exit_on_error:
1442  ECHO_LOG (1, "Test complete !\n");
1443  if (em->output_json)
1445  else
1446  print_global_stats (em);
1447  vec_free (em->fail_descr);
1448  vec_free (em->available_proto_cb_vft);
1449  exit (em->has_failed);
1450 }
1451 
1452 /*
1453  * fd.io coding-style-patch-verification: ON
1454  *
1455  * Local Variables:
1456  * eval: (c-set-style "gnu")
1457  * End:
1458  */
volatile u32 listen_session_cnt
echo_proto_cb_vft_t ** available_proto_cb_vft
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:507
clib_time_t clib_time
static void echo_session_prealloc(echo_main_t *em)
Definition: vpp_echo.c:27
#define CHECK_SAME(fail, expected, result, _fmt, _args...)
echo_stats_t last_stat_sampling
void * svm_msg_q_msg_data(svm_msg_q_t *mq, svm_msg_q_msg_t *msg)
Get data for message in queue.
teardown_stat_t clean_count
#define clib_min(x, y)
Definition: clib.h:319
static_always_inline void clib_spinlock_unlock(clib_spinlock_t *p)
Definition: lock.h:102
int vl_socket_client_init_shm(vl_api_shm_elem_config_t *config, int want_pthread)
static_always_inline void clib_spinlock_lock(clib_spinlock_t *p)
Definition: lock.h:80
u8 * echo_format_timing_event(u8 *s, va_list *args)
int vl_client_connect_to_vlib(const char *svm_name, const char *client_name, int rx_queue_size)
u8 * echo_format_session(u8 *s, va_list *args)
uword unformat_data_size(unformat_input_t *input, va_list *args)
Definition: unformat.c:1081
transport_endpoint_t rmt
a
Definition: bitmap.h:538
struct echo_main_t::@630 uri_elts
#define clib_atomic_add_fetch(a, b)
Definition: atomics.h:30
echo_session_t * sessions
static void * echo_mq_thread_fn(void *arg)
Definition: vpp_echo.c:885
clib_spinlock_t sid_vpp_handles_lock
static int echo_process_each_proto_opts(unformat_input_t *a)
Definition: vpp_echo.c:1068
static void echo_print_periodic_stats(echo_main_t *em)
Definition: vpp_echo.c:857
void echo_send_del_cert_key(echo_main_t *em)
Definition: vpp_echo_bapi.c:90
u8 use_sock_api
Flag that decides if socket, instead of svm, api is used to connect to vpp.
svm_queue_t * vl_input_queue
unsigned long u64
Definition: types.h:89
ip46_address_t ip
int my_client_index
All VLIB-side message handlers use my_client_index to identify the queue / client.
Definition: api_common.h:334
#define clib_memcpy_fast(a, b, c)
Definition: string.h:81
static void echo_session_dequeue_notify(echo_session_t *s)
Definition: vpp_echo.c:67
static void test_recv_bytes(echo_main_t *em, echo_session_t *s, u8 *rx_buf, u32 n_read)
Definition: vpp_echo.c:284
static u8 * format_api_error(u8 *s, va_list *args)
Definition: api_main.c:12
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
ip46_address_t lcl_ip
void echo_send_add_cert_key(echo_main_t *em)
Definition: vpp_echo_bapi.c:69
void echo_process_uri(echo_main_t *em)
Definition: vpp_echo.c:1244
static int echo_segment_is_not_mapped(u64 segment_handle)
Definition: vpp_echo.c:541
static f64 clib_time_now(clib_time_t *c)
Definition: time.h:230
static int mirror_data_chunk(echo_main_t *em, echo_session_t *s, u8 *tx_buf, u64 len)
Definition: vpp_echo.c:344
uword echo_unformat_timing_event(unformat_input_t *input, va_list *args)
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:590
static u8 svm_msg_q_is_empty(svm_msg_q_t *mq)
Check if message queue is empty.
#define vec_add2(V, P, N)
Add N elements to end of vector V, return pointer to new elements in P.
Definition: vec.h:628
static void print_global_json_stats(echo_main_t *em)
Definition: vpp_echo.c:122
echo_main_t echo_main
Definition: vpp_echo.c:24
ip46_address_t lcl_ip
int vl_socket_client_connect(char *socket_path, char *client_name, u32 socket_buffer_size)
#define TIMEOUT
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:424
volatile connection_state_t state
uword echo_unformat_crypto_engine(unformat_input_t *input, va_list *args)
void(* reset_cb)(session_reset_msg_t *mp, echo_session_t *s)
volatile int max_sim_connects
#define pool_get(P, E)
Allocate an object E from a pool P (unspecified alignment).
Definition: pool.h:252
static void echo_free_sessions(echo_main_t *em)
Definition: vpp_echo.c:259
unsigned char u8
Definition: types.h:56
int wait_for_state_change(echo_main_t *em, connection_state_t state, f64 timeout)
static void session_accepted_handler(session_accepted_msg_t *mp)
Definition: vpp_echo.c:556
foreach_app_session_field u64 vpp_session_handle
#define vec_reset_length(v)
Reset vector length to zero NULL-pointer tolerant.
double f64
Definition: types.h:142
static void handle_mq_event(session_event_t *e)
Definition: vpp_echo.c:799
u8 * connect_test_data
void(* connected_cb)(session_connected_bundled_msg_t *mp, u32 session_index, u8 is_failed)
static void echo_check_closed_listener(echo_main_t *em, echo_session_t *s)
Definition: vpp_echo.c:353
static void session_reset_handler(session_reset_msg_t *mp)
Definition: vpp_echo.c:710
u8 * echo_format_session_state(u8 *s, va_list *args)
static void del_segment_handler(session_app_del_segment_msg_t *mp)
Definition: vpp_echo.c:785
static void echo_process_rpcs(echo_main_t *em)
Definition: vpp_echo.c:834
void svm_region_exit(void)
Definition: svm.c:1210
static void app_alloc_ctrl_evt_to_vpp(svm_msg_q_t *mq, app_session_evt_t *app_evt, u8 evt_type)
static int app_send(app_session_t *s, u8 *data, u32 len, u8 noblock)
void echo_send_attach(echo_main_t *em)
Definition: vpp_echo_bapi.c:28
echo_session_t * echo_session_new(echo_main_t *em)
static void clib_mem_set_thread_index(void)
Definition: mem.h:97
#define pool_foreach(VAR, POOL, BODY)
Iterate through pool.
Definition: pool.h:513
unformat_function_t unformat_ip4_address
Definition: format.h:68
svm_msg_q_t * svm_msg_q_alloc(svm_msg_q_cfg_t *cfg)
Allocate message queue.
Definition: message_queue.c:41
volatile u64 bytes_received
void echo_session_handle_add_del(echo_main_t *em, u64 handle, u32 sid)
static u32 svm_fifo_max_dequeue(svm_fifo_t *f)
Fifo max bytes to dequeue.
Definition: svm_fifo.h:431
int main(int argc, char **argv)
Definition: vpp_echo.c:1276
static void clients_run(echo_main_t *em)
Definition: vpp_echo.c:956
void echo_send_unbind(echo_main_t *em, echo_session_t *s)
static int app_send_io_evt_to_vpp(svm_msg_q_t *mq, u32 session_index, u8 evt_type, u8 noblock)
Send fifo io event to vpp worker thread.
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
clib_spinlock_t segment_handles_lock
void echo_send_disconnect_session(echo_main_t *em, void *args)
struct vl_shmem_hdr_ * shmem_hdr
Binary API shared-memory segment header pointer.
Definition: api_common.h:289
#define ECHO_FAIL(fail, _fmt, _args...)
unsigned int u32
Definition: types.h:88
#define SESSION_INVALID_HANDLE
Definition: session_types.h:23
char * segment_name
segment name
Definition: fifo_segment.h:85
void unformat_init_string(unformat_input_t *input, char *string, int string_len)
Definition: unformat.c:1029
void(* echo_rpc_t)(echo_main_t *em, echo_rpc_args_t *arg)
static void clib_spinlock_init(clib_spinlock_t *p)
Definition: lock.h:63
int fifo_segment_attach(fifo_segment_main_t *sm, fifo_segment_create_args_t *a)
Attach as slave to a fifo segment.
Definition: fifo_segment.c:235
void vl_set_memory_root_path(const char *name)
static int app_recv(app_session_t *s, u8 *data, u32 len)
#define hash_get(h, key)
Definition: hash.h:249
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:534
void(* accepted_cb)(session_accepted_msg_t *mp, echo_session_t *session)
struct _unformat_input_t unformat_input_t
void(* cleanup_cb)(echo_session_t *s, u8 parent_died)
u8 * echo_format_bytes_per_sec(u8 *s, va_list *args)
teardown_stat_t accepted_count
#define pool_put(P, E)
Free an object E in pool P.
Definition: pool.h:302
void unformat_init_command_line(unformat_input_t *input, char *argv[])
Definition: unformat.c:1013
void echo_api_hookup(echo_main_t *em)
#define PREDICT_FALSE(x)
Definition: clib.h:118
#define always_inline
Definition: ipsec.h:28
#define SESSION_INVALID_INDEX
Definition: session_types.h:22
static void cleanup_handler(session_cleanup_msg_t *mp)
Definition: vpp_echo.c:793
fifo_segment_main_t segment_main
void clib_time_init(clib_time_t *c)
Definition: time.c:207
svm_msg_q_t * app_mq
void(* bound_uri_cb)(session_bound_msg_t *mp, echo_session_t *session)
data_source_t data_source
u32 periodic_stats_delta
word fformat(FILE *f, char *fmt,...)
Definition: format.c:462
static void server_run(echo_main_t *em)
Definition: vpp_echo.c:979
u8 len
Definition: ip_types.api:92
API main structure, used by both vpp and binary API clients.
Definition: api_common.h:226
format_function_t format_ip46_address
Definition: ip46_address.h:50
#define CHECK_DIFF(fail, expected, result, _fmt, _args...)
static void __clib_constructor vpp_echo_init()
Definition: vpp_echo.c:1267
unformat_function_t unformat_ip6_address
Definition: format.h:89
static void app_send_ctrl_evt_to_vpp(svm_msg_q_t *mq, app_session_evt_t *app_evt)
u32 segment_size
size of the segment
Definition: fifo_segment.h:83
static u8 svm_fifo_set_event(svm_fifo_t *f)
Set fifo event flag.
Definition: svm_fifo.h:699
static void print_global_stats(echo_main_t *em)
Definition: vpp_echo.c:183
int connect_to_vpp(char *name)
Definition: vpp_echo.c:88
clib_error_t * vl_socket_client_recv_fd_msg(int fds[], int n_fds, u32 wait)
static u8 svm_fifo_needs_deq_ntf(svm_fifo_t *f, u32 n_last_deq)
Check if fifo needs dequeue notification.
Definition: svm_fifo.h:790
#define UNFORMAT_END_OF_INPUT
Definition: format.h:145
void vl_socket_client_disconnect(void)
static void svm_msg_q_unlock(svm_msg_q_t *mq)
Unlock message queue.
static int svm_msg_q_timedwait(svm_msg_q_t *mq, double timeout)
Timed wait for message queue event.
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
void fifo_segment_main_init(fifo_segment_main_t *sm, u64 baseva, u32 timeout_in_seconds)
Definition: fifo_segment.c:292
uword * shared_segment_handles
#define clib_warning(format, args...)
Definition: error.h:59
#define ECHO_LOG(lvl, _fmt, _args...)
#define LOG_EVERY_N_IDLE_CYCLES
void echo_notify_event(echo_main_t *em, echo_test_evt_t e)
u32 *volatile data_thread_args
volatile u8 time_to_stop
static void add_segment_handler(session_app_add_segment_msg_t *mp)
Definition: vpp_echo.c:733
#define HIGH_SEGMENT_BASEVA
Definition: svm_common.h:87
blocking call - best used in combination with condvars, for eventfds we don&#39;t yield the cpu ...
Definition: queue.h:42
static void echo_handle_data(echo_main_t *em, echo_session_t *s, u8 *rx_buf)
Definition: vpp_echo.c:383
svm_queue_t * vl_input_queue
Definition: memory_shared.h:84
string name[64]
Definition: ip.api:44
int echo_send_rpc(echo_main_t *em, void *fp, echo_rpc_args_t *args)
#define hash_create(elts, value_bytes)
Definition: hash.h:696
#define uword_to_pointer(u, type)
Definition: types.h:136
static void print_usage_and_exit(void)
Definition: vpp_echo.c:1010
static uword hash_elts(void *v)
Definition: hash.h:118
uword unformat_transport_proto(unformat_input_t *input, va_list *args)
Definition: transport.c:155
static void session_bound_handler(session_bound_msg_t *mp)
Definition: vpp_echo.c:514
void echo_segment_handle_add_del(echo_main_t *em, u64 segment_handle, u8 add)
static void echo_cycle_ip(echo_main_t *em, ip46_address_t *ip, ip46_address_t *src_ip, u32 i)
Definition: vpp_echo.c:937
static void svm_fifo_clear_deq_ntf(svm_fifo_t *f)
Clear the want notification flag and set has notification.
Definition: svm_fifo.h:757
echo_session_t * echo_get_session_from_handle(echo_main_t *em, u64 handle)
static void init_error_string_table(vat_main_t *vam)
Definition: api_main.c:29
teardown_stat_t close_count
void echo_update_count_on_session_close(echo_main_t *em, echo_session_t *s)
Definition: vpp_echo.c:244
static void * echo_data_thread_fn(void *arg)
Definition: vpp_echo.c:439
void(* set_defaults_before_opts_cb)(void)
unformat_function_t unformat_ip46_address
Definition: format.h:63
#define N
Definition: aes_cbc.c:225
#define clib_atomic_fetch_add(a, b)
Definition: atomics.h:23
static void echo_set_each_proto_defaults_before_opts(echo_main_t *em)
Definition: vpp_echo.c:1083
vl_api_address_t src_ip
Definition: udp.api:43
void echo_send_detach(echo_main_t *em)
Definition: vpp_echo_bapi.c:55
int echo_ssvm_segment_attach(char *name, ssvm_segment_type_t type, int fd)
template key/value backing page structure
Definition: bihash_doc.h:44
static void session_unlisten_handler(session_unlisten_reply_msg_t *mp)
Definition: vpp_echo.c:499
#define API_SOCKET_FILE
Definition: socket_api.h:26
vl_api_address_t ip
Definition: l2.api:501
void svm_msg_q_free_msg(svm_msg_q_t *mq, svm_msg_q_msg_t *msg)
Free message buffer.
static int recv_data_chunk(echo_main_t *em, echo_session_t *s, u8 *rx_buf)
Definition: vpp_echo.c:306
volatile u64 bytes_to_receive
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
void echo_process_opts(int argc, char **argv)
Definition: vpp_echo.c:1095
ip46_address_t ip
u64 uword
Definition: types.h:112
static void unformat_free(unformat_input_t *i)
Definition: format.h:163
teardown_stat_t active_count
void(* print_usage_cb)(void)
echo_stats_t stats
void(* set_defaults_after_opts_cb)(void)
u32 * listen_session_indexes
uword * session_index_by_vpp_handles
u16 port
Definition: lb_types.api:72
static void echo_assert_test_suceeded(echo_main_t *em)
Definition: vpp_echo.c:43
unformat_function_t unformat_memory_size
Definition: format.h:296
svm_msg_q_t * rpc_msq_queue
static void session_disconnected_handler(session_disconnected_msg_t *mp)
Definition: vpp_echo.c:679
static api_main_t * vlibapi_get_main(void)
Definition: api_common.h:379
void vl_client_disconnect_from_vlib(void)
volatile u8 wait_for_gdb
static u32 svm_msg_q_size(svm_msg_q_t *mq)
Check length of message queue.
echo_proto_cb_vft_t * proto_cb_vft
#define vec_foreach(var, vec)
Vector iterator.
transport_endpoint_t lcl
void echo_send_listen(echo_main_t *em, ip46_address_t *ip)
teardown_stat_t connected_count
pthread_t * data_thread_handles
int(* process_opts_cb)(unformat_input_t *a)
f64 last_stat_sampling_ts
static int send_data_chunk(echo_session_t *s, u8 *tx_buf, int offset, int len)
Definition: vpp_echo.c:325
void echo_send_connect(echo_main_t *em, void *args)
void * clib_mem_init_thread_safe(void *memory, uword memory_size)
Definition: mem_dlmalloc.c:235
static int svm_msg_q_lock(svm_msg_q_t *mq)
Lock, or block trying, the message queue.
static void session_connected_handler(session_connected_msg_t *mp)
Definition: vpp_echo.c:620
struct _svm_fifo svm_fifo_t
u8 * echo_format_crypto_engine(u8 *s, va_list *args)
struct echo_main_t::@629 timing
teardown_stat_t reset_count
u8 * format_transport_proto(u8 *s, va_list *args)
Definition: transport.c:52
u8 send_stream_disconnects
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:978
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:171
uword echo_unformat_close(unformat_input_t *input, va_list *args)
void(* disconnected_cb)(session_disconnected_msg_t *mp, echo_session_t *s)
echo_rpc_args_t args
void svm_msg_q_sub_w_lock(svm_msg_q_t *mq, svm_msg_q_msg_t *msg)
Consumer dequeue one message from queue with mutex held.
static void stop_signal(int signum)
Definition: vpp_echo.c:81