FD.io VPP  v20.01-48-g3e0dafb74
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, " \"closing\": {\n");
155  fformat (stdout, " \"reset\": { \"q\": %d, \"s\": %d },\n",
156  em->stats.reset_count.q, em->stats.reset_count.s);
157  fformat (stdout, " \"recv evt\": { \"q\": %d, \"s\": %d },\n",
158  em->stats.close_count.q, em->stats.close_count.s);
159  fformat (stdout, " \"send evt\": { \"q\": %d, \"s\": %d },\n",
161  fformat (stdout, " \"clean\": { \"q\": %d, \"s\": %d }\n",
162  em->stats.clean_count.q, em->stats.clean_count.s);
163  fformat (stdout, " \"accepted\": { \"q\": %d, \"s\": %d }\n",
165  fformat (stdout, " \"connected\": { \"q\": %d, \"s\": %d }\n",
167  fformat (stdout, " }\n");
168  fformat (stdout, " \"results\": {\n");
169  fformat (stdout, " \"has_failed\": \"%d\"\n", em->has_failed);
170  fformat (stdout, " \"fail_descr\": \"%v\"\n", em->fail_descr);
171  fformat (stdout, " }\n");
172  fformat (stdout, "}\n");
173  fflush (stdout);
174  vec_free (start_evt);
175  vec_free (end_evt);
176 }
177 
178 static void
180 {
181  u8 *start_evt =
183  u8 *end_evt =
185  u8 start_evt_missing = !(em->timing.events_sent & em->timing.start_event);
186  u8 end_evt_missing = (em->rx_results_diff || em->tx_results_diff) ? 0 :
187  !(em->timing.events_sent & em->timing.end_event);
188  f64 deltat = start_evt_missing || end_evt_missing ? 0 :
189  em->timing.end_time - em->timing.start_time;
190 
191  if (start_evt_missing)
192  ECHO_FAIL (ECHO_FAIL_MISSING_START_EVENT,
193  "Expected event %v to happen, but it did not!", start_evt);
194 
195  if (end_evt_missing)
196  ECHO_FAIL (ECHO_FAIL_MISSING_END_EVENT,
197  "Expected event %v to happen, but it did not!", end_evt);
198 
199  fformat (stdout, "Timing %v:%v\n", start_evt, end_evt);
200  if (start_evt_missing)
201  fformat (stdout, "Missing Start Timing Event (%v)!\n", start_evt);
202  if (end_evt_missing)
203  fformat (stdout, "Missing End Timing Event (%v)!\n", end_evt);
204  fformat (stdout, "-------- TX --------\n");
205  fformat (stdout, "%lld bytes (%lld mbytes, %lld gbytes) in %.6f seconds\n",
206  em->stats.tx_total, em->stats.tx_total / (1ULL << 20),
207  em->stats.tx_total / (1ULL << 30), deltat);
208  if (deltat)
209  fformat (stdout, "%.4f Gbit/second\n",
210  (em->stats.tx_total * 8.0) / deltat / 1e9);
211  fformat (stdout, "-------- RX --------\n");
212  fformat (stdout, "%lld bytes (%lld mbytes, %lld gbytes) in %.6f seconds\n",
213  em->stats.rx_total, em->stats.rx_total / (1ULL << 20),
214  em->stats.rx_total / (1ULL << 30), deltat);
215  if (deltat)
216  fformat (stdout, "%.4f Gbit/second\n",
217  (em->stats.rx_total * 8.0) / deltat / 1e9);
218  fformat (stdout, "--------------------\n");
219  fformat (stdout, "Received close on %d streams (and %d Quic conn)\n",
220  em->stats.close_count.s, em->stats.close_count.q);
221  fformat (stdout, "Received reset on %d streams (and %d Quic conn)\n",
222  em->stats.reset_count.s, em->stats.reset_count.q);
223  fformat (stdout, "Sent close on %d streams (and %d Quic conn)\n",
225  fformat (stdout, "Discarded %d streams (and %d Quic conn)\n",
226  em->stats.clean_count.s, em->stats.clean_count.q);
227  fformat (stdout, "--------------------\n");
228  fformat (stdout, "Got accept on %d streams (and %d Quic conn)\n",
230  fformat (stdout, "Got connected on %d streams (and %d Quic conn)\n",
232  if (em->has_failed)
233  fformat (stdout, "\nFailure Return Status: %d\n%v", em->has_failed,
234  em->fail_descr);
235  vec_free (start_evt);
236  vec_free (end_evt);
237 }
238 
239 void
241 {
242 
243  ECHO_LOG (2, "[%lu/%lu] -> %U -> [%lu/%lu]",
246  s->bytes_sent + s->bytes_to_send);
247 
248  if (PREDICT_FALSE
249  ((em->stats.rx_total == em->stats.rx_expected)
250  && (em->stats.tx_total == em->stats.tx_expected)))
252 }
253 
254 static void
256 {
257  /* Free marked sessions */
258  echo_session_t *s;
259  u32 *session_indexes = 0, *session_index;
260 
261  /* *INDENT-OFF* */
262  pool_foreach (s, em->sessions,
263  ({
264  if (s->session_state == ECHO_SESSION_STATE_CLOSED)
265  vec_add1 (session_indexes, s->session_index);}
266  ));
267  /* *INDENT-ON* */
268  vec_foreach (session_index, session_indexes)
269  {
270  /* Free session */
271  s = pool_elt_at_index (em->sessions, *session_index);
274  clib_memset (s, 0xfe, sizeof (*s));
275  pool_put (em->sessions, s);
276  }
277 }
278 
279 static void
281  u32 n_read)
282 {
283  u32 i;
284  u8 expected;
285  for (i = 0; i < n_read; i++)
286  {
287  expected = (s->bytes_received + i) & 0xff;
288  if (rx_buf[i] == expected || em->max_test_msg > 0)
289  continue;
290  ECHO_LOG (1, "Session 0x%lx byte %lld was 0x%x expected 0x%x",
291  s->vpp_session_handle, s->bytes_received + i, rx_buf[i],
292  expected);
293  em->max_test_msg--;
294  if (em->max_test_msg == 0)
295  ECHO_LOG (1, "Too many errors, hiding next ones");
297  ECHO_FAIL (ECHO_FAIL_TEST_BYTES_ERR, "test-bytes errored");
298  }
299 }
300 
301 static int
303 {
304  int n_read;
305  n_read = app_recv ((app_session_t *) s, rx_buf, vec_len (rx_buf));
306  if (n_read <= 0)
307  return 0;
308  if (svm_fifo_needs_deq_ntf (s->rx_fifo, n_read))
310 
311  if (em->test_return_packets)
312  test_recv_bytes (em, s, rx_buf, n_read);
313 
314  s->bytes_received += n_read;
315  s->bytes_to_receive -= n_read;
316  clib_atomic_fetch_add (&em->stats.rx_total, n_read);
317  return n_read;
318 }
319 
320 static int
321 send_data_chunk (echo_session_t * s, u8 * tx_buf, int offset, int len)
322 {
323  int n_sent;
324  int bytes_this_chunk = clib_min (s->bytes_to_send, len - offset);
325  echo_main_t *em = &echo_main;
326 
327  if (!bytes_this_chunk)
328  return 0;
329  n_sent = app_send ((app_session_t *) s, tx_buf + offset,
330  bytes_this_chunk, SVM_Q_WAIT);
331  if (n_sent < 0)
332  return 0;
333  s->bytes_to_send -= n_sent;
334  s->bytes_sent += n_sent;
335  clib_atomic_fetch_add (&em->stats.tx_total, n_sent);
336  return n_sent;
337 }
338 
339 static int
341 {
342  u64 n_sent = 0;
343  while (n_sent < len && !em->time_to_stop)
344  n_sent += send_data_chunk (s, tx_buf, n_sent, len);
345  return n_sent;
346 }
347 
348 static inline void
350 {
351  echo_session_t *ls;
352  /* if parent has died, terminate gracefully */
354  {
355  ECHO_LOG (3, "%U: listener_index == SESSION_INVALID_INDEX",
357  return;
358  }
360  if (ls->session_state < ECHO_SESSION_STATE_CLOSING)
361  {
362  ECHO_LOG (3, "%U: ls->session_state (%d) < "
363  "ECHO_SESSION_STATE_CLOSING (%d)",
364  echo_format_session, ls, ls->session_state,
366  return;
367  }
368 
369  ECHO_LOG (3, "%U died, close child %U", echo_format_session, ls,
372  em->proto_cb_vft->cleanup_cb (s, 1 /* parent_died */ );
373 }
374 
375 /*
376  * Rx/Tx polling thread per connection
377  */
378 static void
380 {
381  int n_read, n_sent = 0;
382 
383  n_read = recv_data_chunk (em, s, rx_buf);
385  n_sent = send_data_chunk (s, em->connect_test_data,
386  s->bytes_sent % em->tx_buf_size,
387  em->tx_buf_size);
388  else if (em->data_source == ECHO_RX_DATA_SOURCE)
389  n_sent = mirror_data_chunk (em, s, rx_buf, n_read);
390  if (!s->bytes_to_send && !s->bytes_to_receive)
391  {
392  /* Session is done, need to close */
393  if (s->session_state == ECHO_SESSION_STATE_AWAIT_DATA)
394  s->session_state = ECHO_SESSION_STATE_CLOSING;
395  else
396  {
397  s->session_state = ECHO_SESSION_STATE_AWAIT_CLOSING;
399  {
403  }
405  {
406  s->session_state = ECHO_SESSION_STATE_CLOSING;
408  }
409  }
410  ECHO_LOG (3, "%U: %U", echo_format_session, s,
411  echo_format_session_state, s->session_state);
412  return;
413  }
414 
415  /* Check for idle clients */
416  if (em->log_lvl > 1)
417  {
418  if (n_sent || n_read)
419  s->idle_cycles = 0;
420  else if (s->idle_cycles++ == LOG_EVERY_N_IDLE_CYCLES)
421  {
422  s->idle_cycles = 0;
423  ECHO_LOG (2, "Idle client TX:%dB RX:%dB", s->bytes_to_send,
424  s->bytes_to_receive);
425  ECHO_LOG (2, "Idle FIFOs TX:%dB RX:%dB",
426  svm_fifo_max_dequeue (s->tx_fifo),
427  svm_fifo_max_dequeue (s->rx_fifo));
428  ECHO_LOG (2, "Session 0x%lx state %U", s->vpp_session_handle,
429  echo_format_session_state, s->session_state);
430  }
431  }
432 }
433 
434 static void *
436 {
437  clib_mem_set_thread_index (); /* First thing to do in client thread */
438  echo_main_t *em = &echo_main;
439  u32 N = em->n_clients;
440  u32 n = (N + em->n_rx_threads - 1) / em->n_rx_threads;
441  u32 idx = (u64) arg;
442  if (n * idx >= N)
443  {
444  ECHO_LOG (2, "Thread %u exiting, no sessions to care for", idx);
445  pthread_exit (0);
446  }
447  u32 thread_n_sessions = clib_min (n, N - n * idx);
448 
449  u32 i = 0;
450  u32 n_closed_sessions = 0;
451  u32 session_index;
452  u8 *rx_buf = 0;
453  echo_session_t *s;
454  vec_validate (rx_buf, em->rx_buf_size);
455 
456  for (i = 0; !em->time_to_stop; i = (i + 1) % thread_n_sessions)
457  {
458  n_closed_sessions = i == 0 ? 0 : n_closed_sessions;
459  session_index = em->data_thread_args[n * idx + i];
460  if (session_index == SESSION_INVALID_INDEX)
461  continue;
462  s = pool_elt_at_index (em->sessions, session_index);
463  switch (s->session_state)
464  {
467  echo_handle_data (em, s, rx_buf);
469  break;
471  ECHO_LOG (3, "%U: %U", echo_format_session, s,
472  echo_format_session_state, s->session_state);
474  break;
476  ECHO_LOG (3, "%U: %U", echo_format_session, s,
477  echo_format_session_state, s->session_state);
479  em->proto_cb_vft->cleanup_cb (s, 0 /* parent_died */ );
480  break;
482  ECHO_LOG (3, "%U: %U", echo_format_session, s,
483  echo_format_session_state, s->session_state);
484  n_closed_sessions++;
485  break;
486  }
487  if (n_closed_sessions == thread_n_sessions)
488  break;
489  }
490  ECHO_LOG (2, "Mission accomplished!");
491  pthread_exit (0);
492 }
493 
494 static void
496 {
497  echo_session_t *ls;
498  echo_main_t *em = &echo_main;
499 
500  ls = echo_get_session_from_handle (em, mp->handle);
501  if (!ls)
502  return;
503  em->proto_cb_vft->cleanup_cb (ls, 0 /* parent_died */ );
504  ls->session_state = ECHO_SESSION_STATE_CLOSED;
505  if (--em->listen_session_cnt == 0)
507 }
508 
509 static void
511 {
512  echo_main_t *em = &echo_main;
513  echo_session_t *listen_session;
514  if (mp->retval)
515  {
516  ECHO_FAIL (ECHO_FAIL_BIND, "bind failed: %U", format_api_error,
517  clib_net_to_host_u32 (mp->retval));
518  return;
519  }
520  ECHO_LOG (1, "listening on %U:%u", format_ip46_address, mp->lcl_ip,
522  clib_net_to_host_u16 (mp->lcl_port));
523 
524  /* Allocate local session and set it up */
525  listen_session = echo_session_new (em);
526  listen_session->session_type = ECHO_SESSION_TYPE_LISTEN;
527  listen_session->vpp_session_handle = mp->handle;
528  echo_session_handle_add_del (em, mp->handle, listen_session->session_index);
529  vec_add1 (em->listen_session_indexes, listen_session->session_index);
530  if (++em->listen_session_cnt == em->n_uris)
531  em->state = STATE_LISTEN;
532  if (em->proto_cb_vft->bound_uri_cb)
533  em->proto_cb_vft->bound_uri_cb (mp, listen_session);
534 }
535 
536 static int
538 {
539  echo_main_t *em = &echo_main;
540  uword *segment_present;
541  ECHO_LOG (3, "Check if segment mapped 0x%lx...", segment_handle);
543  segment_present = hash_get (em->shared_segment_handles, segment_handle);
545  if (segment_present != 0)
546  return 0;
547  ECHO_LOG (2, "Segment not mapped (0x%lx)", segment_handle);
548  return -1;
549 }
550 
551 static void
553 {
554  app_session_evt_t _app_evt, *app_evt = &_app_evt;
556  svm_fifo_t *rx_fifo, *tx_fifo;
557  echo_main_t *em = &echo_main;
558  echo_session_t *session, *ls;
559 
560  if (!(ls = echo_get_session_from_handle (em, mp->listener_handle)))
561  {
562  ECHO_FAIL (ECHO_FAIL_SESSION_ACCEPTED_BAD_LISTENER,
563  "Unknown listener handle 0x%lx", mp->listener_handle);
564  return;
565  }
567  {
568  ECHO_FAIL (ECHO_FAIL_ACCEPTED_WAIT_FOR_SEG_ALLOC,
569  "accepted wait_for_segment_allocation errored");
570  return;
571  }
572 
573  /* Allocate local session and set it up */
574  session = echo_session_new (em);
575  session->vpp_session_handle = mp->handle;
576 
577  rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
578  rx_fifo->client_session_index = session->session_index;
579  tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
580  tx_fifo->client_session_index = session->session_index;
581 
582  session->rx_fifo = rx_fifo;
583  session->tx_fifo = tx_fifo;
584 
585  /* session->transport needed by app_send_dgram */
586  clib_memcpy_fast (&session->transport.rmt_ip, &mp->rmt.ip,
587  sizeof (ip46_address_t));
588  session->transport.is_ip4 = mp->rmt.is_ip4;
589  session->transport.rmt_port = mp->rmt.port;
590  clib_memcpy_fast (&session->transport.lcl_ip, &em->uri_elts.ip,
591  sizeof (ip46_address_t));
592  session->transport.lcl_port = em->uri_elts.port;
593 
594  session->vpp_session_handle = mp->handle;
595  session->start = clib_time_now (&em->clib_time);
596  session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address,
597  svm_msg_q_t *);
598  session->listener_index = ls->session_index;
599 
600  /* Add it to lookup table */
601  ECHO_LOG (2, "Accepted session 0x%lx S[%u] -> 0x%lx S[%u]",
602  mp->handle, session->session_index,
603  mp->listener_handle, session->listener_index);
604  echo_session_handle_add_del (em, mp->handle, session->session_index);
605 
606  app_alloc_ctrl_evt_to_vpp (session->vpp_evt_q, app_evt,
608  rmp = (session_accepted_reply_msg_t *) app_evt->evt->data;
609  rmp->handle = mp->handle;
610  rmp->context = mp->context;
611  app_send_ctrl_evt_to_vpp (session->vpp_evt_q, app_evt);
612  em->proto_cb_vft->accepted_cb (mp, session);
613 }
614 
615 static void
617 {
618  echo_main_t *em = &echo_main;
619  echo_session_t *session;
620  u32 listener_index = htonl (mp->context);
621  svm_fifo_t *rx_fifo, *tx_fifo;
622 
624 
625  if (mp->retval)
626  {
627  if (em->proto_cb_vft->connected_cb)
628  em->
629  proto_cb_vft->connected_cb ((session_connected_bundled_msg_t *) mp,
630  listener_index, 1 /* is_failed */ );
631  return;
632  }
633 
634  session = echo_session_new (em);
636  {
637  ECHO_FAIL (ECHO_FAIL_CONNECTED_WAIT_FOR_SEG_ALLOC,
638  "connected wait_for_segment_allocation errored");
639  return;
640  }
641 
642  rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
643  rx_fifo->client_session_index = session->session_index;
644  tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
645  tx_fifo->client_session_index = session->session_index;
646 
647  session->rx_fifo = rx_fifo;
648  session->tx_fifo = tx_fifo;
649  session->vpp_session_handle = mp->handle;
650  session->start = clib_time_now (&em->clib_time);
651  session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address,
652  svm_msg_q_t *);
653  session->listener_index = listener_index;
654  /* session->transport needed by app_send_dgram */
655  clib_memcpy_fast (&session->transport.lcl_ip, &mp->lcl.ip,
656  sizeof (ip46_address_t));
657  session->transport.is_ip4 = mp->lcl.is_ip4;
658  session->transport.lcl_port = mp->lcl.port;
659  clib_memcpy_fast (&session->transport.rmt_ip, &em->uri_elts.ip,
660  sizeof (ip46_address_t));
661  session->transport.rmt_port = em->uri_elts.port;
662 
663  echo_session_handle_add_del (em, mp->handle, session->session_index);
665  session->session_index, 0 /* is_failed */ );
666 }
667 
668 /*
669  *
670  * End of ECHO callback definitions
671  *
672  */
673 
674 static void
676 {
677  app_session_evt_t _app_evt, *app_evt = &_app_evt;
679  echo_main_t *em = &echo_main;
680  echo_session_t *s;
681  if (!(s = echo_get_session_from_handle (em, mp->handle)))
682  {
683  ECHO_LOG (1, "Invalid vpp_session_handle: 0x%lx", mp->handle);
684  return;
685  }
686  if (s->session_state == ECHO_SESSION_STATE_CLOSED)
687  {
688  ECHO_LOG (2, "%U: already in ECHO_SESSION_STATE_CLOSED",
690  }
691  else
692  {
693  ECHO_LOG (2, "%U: passive close", echo_format_session, s);
694  em->proto_cb_vft->disconnected_cb (mp, s);
695  }
696  app_alloc_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt,
698  rmp = (session_disconnected_reply_msg_t *) app_evt->evt->data;
699  rmp->retval = 0;
700  rmp->handle = mp->handle;
701  rmp->context = mp->context;
702  app_send_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt);
703 }
704 
705 static void
707 {
708  app_session_evt_t _app_evt, *app_evt = &_app_evt;
709  echo_main_t *em = &echo_main;
711  echo_session_t *s = 0;
712  if (!(s = echo_get_session_from_handle (em, mp->handle)))
713  {
714  ECHO_LOG (1, "Invalid vpp_session_handle: 0x%lx", mp->handle);
715  return;
716  }
717  ECHO_LOG (2, "%U: session reset", echo_format_session, s);
718  em->proto_cb_vft->reset_cb (mp, s);
719 
720  app_alloc_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt,
722  rmp = (session_reset_reply_msg_t *) app_evt->evt->data;
723  rmp->retval = 0;
724  rmp->handle = mp->handle;
725  app_send_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt);
726 }
727 
728 static void
730 {
731  fifo_segment_main_t *sm = &echo_main.segment_main;
732  fifo_segment_create_args_t _a, *a = &_a;
733  echo_main_t *em = &echo_main;
734  int *fds = 0, i;
735  char *seg_name = (char *) mp->segment_name;
736  u64 segment_handle = mp->segment_handle;
737 
738  if (mp->fd_flags & SESSION_FD_F_MEMFD_SEGMENT)
739  {
740  vec_validate (fds, 1);
741  if (vl_socket_client_recv_fd_msg (fds, 1, 5))
742  {
743  ECHO_FAIL (ECHO_FAIL_VL_API_RECV_FD_MSG,
744  "vl_socket_client_recv_fd_msg failed");
745  goto failed;
746  }
747 
748  if (echo_ssvm_segment_attach (seg_name, SSVM_SEGMENT_MEMFD, fds[0]))
749  {
750  ECHO_FAIL (ECHO_FAIL_VL_API_SVM_FIFO_SEG_ATTACH,
751  "svm_fifo_segment_attach ('%s') "
752  "failed on SSVM_SEGMENT_MEMFD", seg_name);
753  goto failed;
754  }
755  vec_free (fds);
756  }
757  else
758  {
759  clib_memset (a, 0, sizeof (*a));
760  a->segment_name = seg_name;
761  a->segment_size = mp->segment_size;
762  /* Attach to the segment vpp created */
763  if (fifo_segment_attach (sm, a))
764  {
765  ECHO_FAIL (ECHO_FAIL_VL_API_FIFO_SEG_ATTACH,
766  "fifo_segment_attach ('%s') failed", seg_name);
767  goto failed;
768  }
769  }
770  echo_segment_handle_add_del (em, segment_handle, 1 /* add */ );
771  ECHO_LOG (2, "Mapped segment 0x%lx", segment_handle);
772  return;
773 
774 failed:
775  for (i = 0; i < vec_len (fds); i++)
776  close (fds[i]);
777  vec_free (fds);
778 }
779 
780 static void
782 {
783  echo_main_t *em = &echo_main;
784  echo_segment_handle_add_del (em, mp->segment_handle, 0 /* add */ );
785  ECHO_LOG (2, "Unmaped segment 0x%lx", mp->segment_handle);
786 }
787 
788 static void
789 handle_mq_event (session_event_t * e)
790 {
791  switch (e->event_type)
792  {
794  return session_bound_handler ((session_bound_msg_t *) e->data);
801  e->data);
803  return session_reset_handler ((session_reset_msg_t *) e->data);
806  e->data);
809  break;
812  break;
813  case SESSION_IO_EVT_RX:
814  break;
815  default:
816  ECHO_LOG (1, "unhandled event %u", e->event_type);
817  }
818 }
819 
820 static void
822 {
823  echo_rpc_msg_t *rpc;
824  svm_msg_q_msg_t msg;
825  svm_msg_q_t *mq = em->rpc_msq_queue;
826 
827  while (em->state < STATE_DATA_DONE && !em->time_to_stop)
828  {
829  svm_msg_q_lock (mq);
830  if (svm_msg_q_is_empty (mq) && svm_msg_q_timedwait (mq, 1))
831  {
832  svm_msg_q_unlock (mq);
833  continue;
834  }
835  svm_msg_q_sub_w_lock (mq, &msg);
836  rpc = svm_msg_q_msg_data (mq, &msg);
837  svm_msg_q_unlock (mq);
838  ((echo_rpc_t) rpc->fp) (em, &rpc->args);
839  svm_msg_q_free_msg (mq, &msg);
840  }
841 }
842 
843 static inline void
845 {
846  f64 delta, now = clib_time_now (&em->clib_time);
847  echo_stats_t _st, *st = &_st;
848  echo_stats_t *lst = &em->last_stat_sampling;
849  delta = now - em->last_stat_sampling_ts;
850  if (delta < em->periodic_stats_delta)
851  return;
852 
853  clib_memcpy_fast (st, &em->stats, sizeof (*st));
854  if (st->rx_total - lst->rx_total)
856  (st->rx_total - lst->rx_total) / delta);
857  if (st->tx_total - lst->tx_total)
859  (st->tx_total - lst->tx_total) / delta);
860  if (st->connected_count.q - lst->connected_count.q)
861  clib_warning ("conn: %d/s",
862  st->connected_count.q - lst->connected_count.q);
863  if (st->accepted_count.q - lst->accepted_count.q)
864  clib_warning ("accept: %d/s",
865  st->accepted_count.q - lst->accepted_count.q);
866 
867  clib_memcpy_fast (lst, st, sizeof (*st));
868  em->last_stat_sampling_ts = now;
869 }
870 
871 static void *
872 echo_mq_thread_fn (void *arg)
873 {
874  clib_mem_set_thread_index (); /* First thing to do in client thread */
875  svm_msg_q_msg_t *msg_vec = 0;
876  echo_main_t *em = &echo_main;
877  session_event_t *e;
878  svm_msg_q_msg_t *msg;
879  svm_msg_q_t *mq;
880  int i;
881 
882  vec_validate (msg_vec, em->evt_q_size);
883  vec_reset_length (msg_vec);
885  mq = em->app_mq;
886  if (em->state < STATE_ATTACHED || !mq)
887  {
888  ECHO_FAIL (ECHO_FAIL_APP_ATTACH, "Application failed to attach");
889  pthread_exit (0);
890  }
891 
892  while (em->state < STATE_DETACHED && !em->time_to_stop)
893  {
894  if (em->periodic_stats_delta)
896 
897  svm_msg_q_lock (mq);
898  if (svm_msg_q_is_empty (mq) && svm_msg_q_timedwait (mq, 1))
899  {
900  svm_msg_q_unlock (mq);
901  continue;
902  }
903  for (i = 0; i < svm_msg_q_size (mq); i++)
904  {
905  vec_add2 (msg_vec, msg, 1);
906  svm_msg_q_sub_w_lock (mq, msg);
907  }
908  svm_msg_q_unlock (mq);
909 
910  for (i = 0; i < vec_len (msg_vec); i++)
911  {
912  msg = vec_elt_at_index (msg_vec, i);
913  e = svm_msg_q_msg_data (mq, msg);
914  handle_mq_event (e);
915  svm_msg_q_free_msg (mq, msg); /* No lock, single thread dequeuing */
916  }
917  vec_reset_length (msg_vec);
918  }
919  vec_free (msg_vec);
920  pthread_exit (0);
921 }
922 
923 static inline void
924 echo_cycle_ip (echo_main_t * em, ip46_address_t * ip, ip46_address_t * src_ip,
925  u32 i)
926 {
927  u8 *ipu8;
928  u8 l;
929  if (i % em->n_uris == 0)
930  {
931  clib_memcpy_fast (ip, src_ip, sizeof (*ip));
932  return;
933  }
934  l = em->uri_elts.is_ip4 ? 3 : 15;
935  ipu8 = em->uri_elts.is_ip4 ? ip->ip4.as_u8 : ip->ip6.as_u8;
936  while (ipu8[l] == 0xf)
937  ipu8[l--] = 0;
938  if (l)
939  ipu8[l]++;
940 }
941 
942 static void
944 {
945  echo_connect_args_t _a, *a = &_a;
946  u64 i;
947 
950  clib_memset (&a->lcl_ip, 0, sizeof (a->lcl_ip));
951 
953  for (i = 0; i < em->n_connects; i++)
954  {
955  echo_cycle_ip (em, &a->ip, &em->uri_elts.ip, i);
956  if (em->lcl_ip_set)
957  echo_cycle_ip (em, &a->lcl_ip, &em->lcl_ip, i);
958  echo_send_connect (em, a);
959  }
961  ECHO_LOG (2, "App is ready");
962  echo_process_rpcs (em);
963 }
964 
965 static void
967 {
968  echo_session_t *ls;
969  ip46_address_t _ip, *ip = &_ip;
970  u32 *listen_session_index;
971  u32 i;
972 
973  for (i = 0; i < em->n_uris; i++)
974  {
975  echo_cycle_ip (em, ip, &em->uri_elts.ip, i);
976  echo_send_listen (em, ip);
977  }
979  ECHO_LOG (2, "App is ready");
980  echo_process_rpcs (em);
981  /* Cleanup */
982  vec_foreach (listen_session_index, em->listen_session_indexes)
983  {
984  ECHO_LOG (2, "Unbind listen port %d", em->listen_session_cnt);
985  ls = pool_elt_at_index (em->sessions, *listen_session_index);
986  echo_send_unbind (em, ls);
987  }
989  {
990  ECHO_FAIL (ECHO_FAIL_SERVER_DISCONNECT_TIMEOUT,
991  "Timeout waiting for state disconnected");
992  return;
993  }
994 }
995 
996 static void
998 {
999  echo_main_t *em = &echo_main;
1000  int i;
1001  fprintf (stderr,
1002  "Usage: vpp_echo [socket-name SOCKET] [client|server] [uri URI] [OPTIONS]\n"
1003  "Generates traffic and assert correct teardown of the hoststack\n"
1004  "\n"
1005  " socket-name PATH Specify the binary socket path to connect to VPP\n"
1006  " use-svm-api Use SVM API to connect to VPP\n"
1007  " test-bytes[:assert] Check data correctness when receiving (assert fails on first error)\n"
1008  " fifo-size N[K|M|G] Use N[K|M|G] fifos\n"
1009  " mq-size N Use mq with N slots for [vpp_echo->vpp] communication\n"
1010  " max-sim-connects N Do not allow more than N mq events inflight\n"
1011  " rx-buf N[K|M|G] Use N[Kb|Mb|GB] RX buffer\n"
1012  " tx-buf N[K|M|G] Use N[Kb|Mb|GB] TX test buffer\n"
1013  " appns NAMESPACE Use the namespace NAMESPACE\n"
1014  " all-scope all-scope option\n"
1015  " local-scope local-scope option\n"
1016  " global-scope global-scope option\n"
1017  " secret SECRET set namespace secret\n"
1018  " chroot prefix PATH Use PATH as memory root path\n"
1019  " sclose=[Y|N|W] When stream is done, send[Y]|nop[N]|wait[W] for close\n"
1020  " nuris N Cycle through N consecutive (src&dst) ips when creating connections\n"
1021  " lcl IP Set the local ip to use as a client (use with nuris to set first src ip)\n"
1022  "\n"
1023  " time START:END Time between evts START & END, events being :\n"
1024  " start - Start of the app\n"
1025  " qconnect - first Connection connect sent\n"
1026  " qconnected - last Connection connected\n"
1027  " sconnect - first Stream connect sent\n"
1028  " sconnected - last Stream got connected\n"
1029  " lastbyte - Last expected byte received\n"
1030  " exit - Exiting of the app\n"
1031  " rx-results-diff Rx results different to pass test\n"
1032  " tx-results-diff Tx results different to pass test\n"
1033  " json Output global stats in json\n"
1034  " stats N Output stats evry N secs\n"
1035  " log=N Set the log level to [0: no output, 1:errors, 2:log]\n"
1036  " crypto [engine] Set the crypto engine [openssl, vpp, picotls, mbedtls]\n"
1037  "\n"
1038  " nclients N Open N clients sending data\n"
1039  " nthreads N Use N busy loop threads for data [in addition to main & msg queue]\n"
1040  " TX=1337[K|M|G]|RX Send 1337 [K|M|G]bytes, use TX=RX to reflect the data\n"
1041  " RX=1337[K|M|G] Expect 1337 [K|M|G]bytes\n" "\n");
1042  for (i = 0; i < TRANSPORT_N_PROTO; i++)
1043  {
1045  if (vft && vft->print_usage_cb)
1046  vft->print_usage_cb ();
1047  }
1048  fprintf (stderr, "\nDefault configuration is :\n"
1049  " server nclients 1 [quic-streams 1] RX=64Kb TX=RX\n"
1050  " client nclients 1 [quic-streams 1] RX=64Kb TX=64Kb\n");
1051  exit (ECHO_FAIL_USAGE);
1052 }
1053 
1054 static int
1056 {
1057  echo_main_t *em = &echo_main;
1058  int i, rv;
1059  for (i = 0; i < TRANSPORT_N_PROTO; i++)
1060  {
1062  if (vft && vft->process_opts_cb)
1063  if ((rv = vft->process_opts_cb (a)))
1064  return rv;
1065  }
1066  return 0;
1067 }
1068 
1069 static void
1071 {
1072  int i;
1073  for (i = 0; i < TRANSPORT_N_PROTO; i++)
1074  {
1076  if (vft && vft->set_defaults_before_opts_cb)
1078  }
1079 }
1080 
1081 void
1082 echo_process_opts (int argc, char **argv)
1083 {
1084  echo_main_t *em = &echo_main;
1085  unformat_input_t _argv, *a = &_argv;
1086  u8 *chroot_prefix;
1087  u8 *uri = 0;
1088  u8 default_f_active;
1089  uword tmp;
1090 
1091  unformat_init_command_line (a, argv);
1093  {
1095  ;
1096  else if (unformat (a, "chroot prefix %s", &chroot_prefix))
1097  vl_set_memory_root_path ((char *) chroot_prefix);
1098  else if (unformat (a, "uri %s", &uri))
1099  em->uri = format (0, "%s%c", uri, 0);
1100  else if (unformat (a, "lcl %U", unformat_ip46_address, &em->lcl_ip))
1101  em->lcl_ip_set = 1;
1102  else if (unformat (a, "nuris %u", &em->n_uris))
1103  em->n_sessions = em->n_clients + em->n_uris;
1104  else if (unformat (a, "server"))
1105  em->i_am_master = 1;
1106  else if (unformat (a, "client"))
1107  em->i_am_master = 0;
1108  else if (unformat (a, "test-bytes:assert"))
1110  else if (unformat (a, "test-bytes"))
1112  else if (unformat (a, "socket-name %s", &em->socket_name))
1113  ;
1114  else if (unformat (a, "use-svm-api"))
1115  em->use_sock_api = 0;
1116  else if (unformat (a, "fifo-size %U", unformat_memory_size, &tmp))
1117  {
1118  if (tmp >= 0x100000000ULL)
1119  {
1120  fprintf (stderr,
1121  "ERROR: fifo-size %ld (0x%lx) too large\n", tmp, tmp);
1123  }
1124  em->fifo_size = tmp;
1125  }
1126  else if (unformat (a, "prealloc-fifos %u", &em->prealloc_fifo_pairs))
1127  ;
1128  else
1129  if (unformat (a, "rx-buf %U", unformat_data_size, &em->rx_buf_size))
1130  ;
1131  else
1132  if (unformat (a, "tx-buf %U", unformat_data_size, &em->tx_buf_size))
1133  ;
1134  else if (unformat (a, "mq-size %d", &em->evt_q_size))
1135  ;
1136  else if (unformat (a, "nclients %d", &em->n_clients))
1137  {
1138  em->n_sessions = em->n_clients + em->n_uris;
1139  em->n_connects = em->n_clients;
1140  }
1141  else if (unformat (a, "nthreads %d", &em->n_rx_threads))
1142  ;
1143  else if (unformat (a, "crypto %U", echo_unformat_crypto_engine, &tmp))
1144  em->crypto_engine = tmp;
1145  else if (unformat (a, "appns %_%v%_", &em->appns_id))
1146  ;
1147  else if (unformat (a, "all-scope"))
1148  em->appns_flags |= (APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE
1149  | APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE);
1150  else if (unformat (a, "local-scope"))
1151  em->appns_flags = APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE;
1152  else if (unformat (a, "global-scope"))
1153  em->appns_flags = APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
1154  else if (unformat (a, "secret %lu", &em->appns_secret))
1155  ;
1156  else if (unformat (a, "TX=RX"))
1158  else if (unformat (a, "TX=%U", unformat_data_size, &em->bytes_to_send))
1159  ;
1160  else if (unformat (a, "RX=%U", unformat_data_size,
1161  &em->bytes_to_receive))
1162  ;
1163  else if (unformat (a, "rx-results-diff"))
1164  em->rx_results_diff = 1;
1165  else if (unformat (a, "tx-results-diff"))
1166  em->tx_results_diff = 1;
1167  else if (unformat (a, "json"))
1168  em->output_json = 1;
1169  else if (unformat (a, "stats %d", &em->periodic_stats_delta))
1170  ;
1171  else if (unformat (a, "wait-for-gdb"))
1172  em->wait_for_gdb = 1;
1173  else if (unformat (a, "log=%d", &em->log_lvl))
1174  ;
1175  else if (unformat (a, "sclose=%U",
1177  ;
1178  else if (unformat (a, "time %U:%U",
1181  ;
1182  else if (unformat (a, "max-sim-connects %d", &em->max_sim_connects))
1183  ;
1184  else
1186  }
1187 
1188  /* setting default for unset values
1189  *
1190  * bytes_to_send / bytes_to_receive & data_source */
1191  if (em->bytes_to_receive == (u64) ~ 0)
1192  em->bytes_to_receive = 64 << 10; /* default */
1193  if (em->bytes_to_send == (u64) ~ 0)
1194  em->bytes_to_send = 64 << 10; /* default */
1195  else if (em->bytes_to_send == 0)
1197  else
1199 
1201  em->data_source =
1203  if (em->data_source == ECHO_RX_DATA_SOURCE)
1204  em->bytes_to_send = em->bytes_to_receive;
1205 
1206  /* disconnect flags */
1207  if (em->i_am_master)
1208  default_f_active =
1210  else
1211  default_f_active =
1214  em->send_stream_disconnects = default_f_active;
1215 
1216  if (em->max_sim_connects == 0)
1217  em->max_sim_connects = em->evt_q_size >> 1;
1218 
1219  if (em->wait_for_gdb)
1220  {
1221  volatile u64 nop = 0;
1222 
1223  clib_warning ("Waiting for gdb...");
1224  while (em->wait_for_gdb)
1225  nop++;
1226  clib_warning ("Resuming execution (%llu)!", nop);
1227  }
1228 }
1229 
1230 void
1232 {
1233  unformat_input_t _input, *input = &_input;
1234  u32 port;
1235  unformat_init_string (input, (char *) em->uri, strlen ((char *) em->uri));
1236  if (unformat
1237  (input, "%U://%U/%d", unformat_transport_proto,
1239  &em->uri_elts.ip.ip4, &port))
1240  em->uri_elts.is_ip4 = 1;
1241  else
1242  if (unformat
1243  (input, "%U://%U/%d", unformat_transport_proto,
1245  &em->uri_elts.ip.ip6, &port))
1246  em->uri_elts.is_ip4 = 0;
1247  else
1248  ECHO_FAIL (ECHO_FAIL_INVALID_URI, "Unable to process uri");
1249  em->uri_elts.port = clib_host_to_net_u16 (port);
1250  unformat_free (input);
1251 }
1252 
1253 static void __clib_constructor
1255 {
1256  /* init memory before proto register themselves */
1257  echo_main_t *em = &echo_main;
1258  clib_mem_init_thread_safe (0, 256 << 20);
1259  clib_memset (em, 0, sizeof (*em));
1260 }
1261 
1262 int
1263 main (int argc, char **argv)
1264 {
1265  echo_main_t *em = &echo_main;
1266  fifo_segment_main_t *sm = &em->segment_main;
1267  char *app_name;
1268  u64 i;
1269  svm_msg_q_cfg_t _cfg, *cfg = &_cfg;
1270  u32 rpc_queue_size = 256 << 10;
1271 
1272  em->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
1274  em->shared_segment_handles = hash_create (0, sizeof (uword));
1276  em->socket_name = format (0, "%s%c", API_SOCKET_FILE, 0);
1277  em->use_sock_api = 1;
1278  em->fifo_size = 64 << 10;
1279  em->prealloc_fifo_pairs = 16;
1280  em->n_clients = 1;
1281  em->n_connects = 1;
1282  em->n_sessions = 2;
1283  em->max_test_msg = 50;
1284  em->time_to_stop = 0;
1285  em->i_am_master = 1;
1286  em->n_rx_threads = 4;
1287  em->evt_q_size = 256;
1288  em->lcl_ip_set = 0;
1289  clib_memset (&em->lcl_ip, 0, sizeof (em->lcl_ip));
1293  em->bytes_to_receive = ~0; /* defaulted when we know if server/client */
1294  em->bytes_to_send = ~0; /* defaulted when we know if server/client */
1295  em->rx_buf_size = 1 << 20;
1296  em->tx_buf_size = 1 << 20;
1298  em->uri = format (0, "%s%c", "tcp://0.0.0.0/1234", 0);
1299  em->n_uris = 1;
1300  em->max_sim_connects = 0;
1301  em->listen_session_cnt = 0;
1304  echo_process_opts (argc, argv);
1305  echo_process_uri (em);
1307  if (!em->proto_cb_vft)
1308  {
1309  ECHO_FAIL (ECHO_FAIL_PROTOCOL_NOT_SUPPORTED,
1310  "Protocol %U is not supported",
1312  goto exit_on_error;
1313  }
1316 
1317  em->stats.rx_expected = em->bytes_to_receive * em->n_clients;
1318  em->stats.tx_expected = em->bytes_to_send * em->n_clients;
1319 
1322  for (i = 0; i < em->n_clients; i++)
1324  clib_time_init (&em->clib_time);
1328  for (i = 0; i < em->tx_buf_size; i++)
1329  em->connect_test_data[i] = i & 0xff;
1330 
1331  /* *INDENT-OFF* */
1332  svm_msg_q_ring_cfg_t rc[1] = {
1333  {rpc_queue_size, sizeof (echo_rpc_msg_t), 0},
1334  };
1335  /* *INDENT-ON* */
1336  cfg->consumer_pid = getpid ();
1337  cfg->n_rings = 1;
1338  cfg->q_nitems = rpc_queue_size;
1339  cfg->ring_cfgs = rc;
1340  em->rpc_msq_queue = svm_msg_q_alloc (cfg);
1341 
1342  signal (SIGINT, stop_signal);
1343  signal (SIGQUIT, stop_signal);
1344  signal (SIGTERM, stop_signal);
1345  echo_api_hookup (em);
1346 
1347  app_name = em->i_am_master ? "echo_server" : "echo_client";
1348  if (connect_to_vpp (app_name))
1349  {
1350  svm_region_exit ();
1351  ECHO_FAIL (ECHO_FAIL_CONNECT_TO_VPP, "Couldn't connect to vpp");
1352  goto exit_on_error;
1353  }
1354 
1355  echo_session_prealloc (em);
1357 
1358  echo_send_attach (em);
1360  {
1361  ECHO_FAIL (ECHO_FAIL_ATTACH_TO_VPP,
1362  "Couldn't attach to vpp, did you run <session enable> ?");
1363  goto exit_on_error;
1364  }
1365 
1366  if (em->uri_elts.transport_proto != TRANSPORT_PROTO_QUIC
1367  && em->uri_elts.transport_proto != TRANSPORT_PROTO_TLS)
1368  em->state = STATE_ATTACHED;
1369  else
1370  {
1371  ECHO_LOG (2, "Adding crypto context %U", echo_format_crypto_engine,
1372  em->crypto_engine);
1375  {
1376  ECHO_FAIL (ECHO_FAIL_APP_ATTACH,
1377  "Couldn't add crypto context to vpp\n");
1378  exit (1);
1379  }
1380  }
1381 
1382  if (pthread_create (&em->mq_thread_handle,
1383  NULL /*attr */ , echo_mq_thread_fn, 0))
1384  {
1385  ECHO_FAIL (ECHO_FAIL_PTHREAD_CREATE, "pthread create errored");
1386  goto exit_on_error;
1387  }
1388 
1389  for (i = 0; i < em->n_rx_threads; i++)
1390  if (pthread_create (&em->data_thread_handles[i],
1391  NULL /*attr */ , echo_data_thread_fn, (void *) i))
1392  {
1393  ECHO_FAIL (ECHO_FAIL_PTHREAD_CREATE,
1394  "pthread create errored (index %d)", i);
1395  goto exit_on_error;
1396  }
1397  if (em->i_am_master)
1398  server_run (em);
1399  else
1400  clients_run (em);
1402  echo_free_sessions (em);
1405  {
1406  ECHO_FAIL (ECHO_FAIL_DEL_CERT_KEY, "Couldn't cleanup cert and key");
1407  goto exit_on_error;
1408  }
1409 
1410  echo_send_detach (em);
1412  {
1413  ECHO_FAIL (ECHO_FAIL_DETACH, "Couldn't detach from vpp");
1414  goto exit_on_error;
1415  }
1416  int *rv;
1417  pthread_join (em->mq_thread_handle, (void **) &rv);
1418  if (rv)
1419  {
1420  ECHO_FAIL (ECHO_FAIL_MQ_PTHREAD, "mq pthread errored %d", rv);
1421  goto exit_on_error;
1422  }
1423  if (em->use_sock_api)
1425  else
1428 exit_on_error:
1429  ECHO_LOG (1, "Test complete !\n");
1430  if (em->output_json)
1432  else
1433  print_global_stats (em);
1434  vec_free (em->fail_descr);
1435  exit (em->has_failed);
1436 }
1437 
1438 /*
1439  * fd.io coding-style-patch-verification: ON
1440  *
1441  * Local Variables:
1442  * eval: (c-set-style "gnu")
1443  * End:
1444  */
volatile u32 listen_session_cnt
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:440
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:295
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
#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:872
clib_spinlock_t sid_vpp_handles_lock
static int echo_process_each_proto_opts(unformat_input_t *a)
Definition: vpp_echo.c:1055
static void echo_print_periodic_stats(echo_main_t *em)
Definition: vpp_echo.c:844
void echo_send_del_cert_key(echo_main_t *em)
Definition: vpp_echo_bapi.c:92
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:333
#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
#define NULL
Definition: clib.h:58
static void test_recv_bytes(echo_main_t *em, echo_session_t *s, u8 *rx_buf, u32 n_read)
Definition: vpp_echo.c:280
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:71
void echo_process_uri(echo_main_t *em)
Definition: vpp_echo.c:1231
static int echo_segment_is_not_mapped(u64 segment_handle)
Definition: vpp_echo.c:537
static f64 clib_time_now(clib_time_t *c)
Definition: time.h:224
static int mirror_data_chunk(echo_main_t *em, echo_session_t *s, u8 *tx_buf, u64 len)
Definition: vpp_echo.c:340
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:523
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:561
int i
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:237
static void echo_free_sessions(echo_main_t *em)
Definition: vpp_echo.c:255
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:552
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
struct echo_main_t::@572 timing
static void handle_mq_event(session_event_t *e)
Definition: vpp_echo.c:789
struct _svm_fifo svm_fifo_t
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:349
static void session_reset_handler(session_reset_msg_t *mp)
Definition: vpp_echo.c:706
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:781
static void echo_process_rpcs(echo_main_t *em)
Definition: vpp_echo.c:821
void svm_region_exit(void)
Definition: svm.c:1228
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:64
#define pool_foreach(VAR, POOL, BODY)
Iterate through pool.
Definition: pool.h:498
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:528
int main(int argc, char **argv)
Definition: vpp_echo.c:1263
static void clients_run(echo_main_t *em)
Definition: vpp_echo.c:943
void echo_send_unbind(echo_main_t *em, echo_session_t *s)
struct echo_main_t::@573 uri_elts
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:288
#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:80
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:179
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:519
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:287
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:111
#define always_inline
Definition: ipsec.h:28
#define SESSION_INVALID_INDEX
Definition: session_types.h:22
fifo_segment_main_t segment_main
void clib_time_init(clib_time_t *c)
Definition: time.c:178
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:966
u8 len
Definition: ip_types.api:91
API main structure, used by both vpp and binary API clients.
Definition: api_common.h:225
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:1254
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:78
static u8 svm_fifo_set_event(svm_fifo_t *f)
Set fifo event flag.
Definition: svm_fifo.h:749
static void print_global_stats(echo_main_t *em)
Definition: vpp_echo.c:179
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:840
#define UNFORMAT_END_OF_INPUT
Definition: format.h:145
void vl_socket_client_disconnect(void)
echo_proto_cb_vft_t * available_proto_cb_vft[TRANSPORT_N_PROTO]
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.
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:342
void fifo_segment_main_init(fifo_segment_main_t *sm, u64 baseva, u32 timeout_in_seconds)
Definition: fifo_segment.c:236
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:729
#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:379
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:997
static uword hash_elts(void *v)
Definition: hash.h:118
uword unformat_transport_proto(unformat_input_t *input, va_list *args)
Definition: transport.c:156
static void session_bound_handler(session_bound_msg_t *mp)
Definition: vpp_echo.c:510
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:924
static void svm_fifo_clear_deq_ntf(svm_fifo_t *f)
Clear the want notification flag and set has notification.
Definition: svm_fifo.h:807
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:240
static void * echo_data_thread_fn(void *arg)
Definition: vpp_echo.c:435
void(* set_defaults_before_opts_cb)(void)
unformat_function_t unformat_ip46_address
Definition: format.h:63
#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:1070
vl_api_address_t src_ip
Definition: udp.api:43
void echo_send_detach(echo_main_t *em)
Definition: vpp_echo_bapi.c:57
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:495
#define API_SOCKET_FILE
Definition: socket_api.h:26
vl_api_address_t ip
Definition: l2.api:490
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:302
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:1082
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:675
static api_main_t * vlibapi_get_main(void)
Definition: api_common.h:378
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:321
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:229
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:616
u8 * echo_format_crypto_engine(u8 *s, va_list *args)
teardown_stat_t reset_count
u8 * format_transport_proto(u8 *s, va_list *args)
Definition: transport.c:46
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