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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <vlibmemory/api.h>
20 #include <svm/fifo_segment.h>
22 #include <hs_apps/sapi/vpp_echo_common.h>
24 echo_main_t echo_main;
27 echo_session_prealloc (echo_main_t * em)
29 /* We need to prealloc to avoid vec resize in threads */
30 echo_session_t *session;
32 for (i = 0; i < em->n_sessions; i++)
34 pool_get (em->sessions, session);
35 clib_memset (session, 0, sizeof (*session));
36 session->session_index = session - em->sessions;
37 session->listener_index = SESSION_INVALID_INDEX;
38 session->session_state = ECHO_SESSION_STATE_INITIAL;
43 echo_assert_test_suceeded (echo_main_t * em)
45 CHECK (em->n_clients * em->bytes_to_receive,
46 em->stats.rx_total, "Not enough data received");
47 CHECK (em->n_clients * em->bytes_to_send,
48 em->stats.tx_total, "Not enough data sent");
49 clib_spinlock_lock (&em->sid_vpp_handles_lock);
50 CHECK (0, hash_elts (em->session_index_by_vpp_handles),
51 "Some sessions are still open");
52 clib_spinlock_unlock (&em->sid_vpp_handles_lock);
56 echo_session_dequeue_notify (echo_session_t * s)
59 if (!svm_fifo_set_event (s->rx_fifo))
62 app_send_io_evt_to_vpp (s->vpp_evt_q, s->rx_fifo->master_session_index,
63 SESSION_IO_EVT_RX, SVM_Q_WAIT)))
64 ECHO_FAIL ("app_send_io_evt_to_vpp errored %d", rv);
65 svm_fifo_clear_deq_ntf (s->rx_fifo);
69 stop_signal (int signum)
71 echo_main_t *em = &echo_main;
76 connect_to_vpp (char *name)
78 echo_main_t *em = &echo_main;
79 api_main_t *am = &api_main;
83 if (vl_socket_client_connect ((char *) em->socket_name, name,
84 0 /* default rx, tx buffer */ ))
86 ECHO_FAIL ("socket connect failed");
90 if (vl_socket_client_init_shm (0, 1 /* want_pthread */ ))
92 ECHO_FAIL ("init shm api failed");
98 if (vl_client_connect_to_vlib ("/vpe-api", name, 32) < 0)
100 ECHO_FAIL ("shmem connect failed");
104 em->vl_input_queue = am->shmem_hdr->vl_input_queue;
105 em->my_client_index = am->my_client_index;
110 echo_event_didnt_happen (u8 e)
112 echo_main_t *em = &echo_main;
113 u8 *s = format (0, "%U", echo_format_timing_event, e);
114 ECHO_LOG (0, "Expected event %s to happend, which did not", s);
119 print_global_json_stats (echo_main_t * em)
121 if (!(em->timing.events_sent & em->timing.start_event))
122 return echo_event_didnt_happen (em->timing.start_event);
123 if (!(em->timing.events_sent & em->timing.end_event))
124 return echo_event_didnt_happen (em->timing.end_event);
125 f64 deltat = em->timing.end_time - em->timing.start_time;
127 format (0, "%U", echo_format_timing_event, em->timing.start_event);
129 format (0, "%U", echo_format_timing_event, em->timing.end_event);
130 fformat (stdout, "{\n");
131 fformat (stdout, " \"role\": \"%s\",\n",
132 em->i_am_master ? "server" : "client");
133 fformat (stdout, " \"time\": \"%.9f\",\n", deltat);
134 fformat (stdout, " \"start_evt\": \"%s\",\n", start_evt);
135 fformat (stdout, " \"end_evt\": \"%s\",\n", end_evt);
136 fformat (stdout, " \"rx_data\": %lld,\n", em->stats.rx_total);
137 fformat (stdout, " \"tx_rx\": %lld,\n", em->stats.tx_total);
138 fformat (stdout, " \"closing\": {\n");
139 fformat (stdout, " \"reset\": { \"q\": %d, \"s\": %d },\n",
140 em->stats.reset_count.q, em->stats.reset_count.s);
141 fformat (stdout, " \"close\": { \"q\": %d, \"s\": %d },\n",
142 em->stats.close_count.q, em->stats.close_count.s);
143 fformat (stdout, " \"active\": { \"q\": %d, \"s\": %d },\n",
144 em->stats.active_count.q, em->stats.active_count.s);
145 fformat (stdout, " \"clean\": { \"q\": %d, \"s\": %d }\n",
146 em->stats.clean_count.q, em->stats.clean_count.s);
147 fformat (stdout, " }\n");
148 fformat (stdout, "}\n");
152 print_global_stats (echo_main_t * em)
155 if (!(em->timing.events_sent & em->timing.start_event))
156 return echo_event_didnt_happen (em->timing.start_event);
157 if (!(em->timing.events_sent & em->timing.end_event))
158 return echo_event_didnt_happen (em->timing.end_event);
159 f64 deltat = em->timing.end_time - em->timing.start_time;
160 s = format (0, "%U:%U",
161 echo_format_timing_event, em->timing.start_event,
162 echo_format_timing_event, em->timing.end_event);
163 fformat (stdout, "Timing %s\n", s);
164 fformat (stdout, "-------- TX --------\n");
165 fformat (stdout, "%lld bytes (%lld mbytes, %lld gbytes) in %.6f seconds\n",
166 em->stats.tx_total, em->stats.tx_total / (1ULL << 20),
167 em->stats.tx_total / (1ULL << 30), deltat);
168 fformat (stdout, "%.4f Gbit/second\n",
169 (em->stats.tx_total * 8.0) / deltat / 1e9);
170 fformat (stdout, "-------- RX --------\n");
171 fformat (stdout, "%lld bytes (%lld mbytes, %lld gbytes) in %.6f seconds\n",
172 em->stats.rx_total, em->stats.rx_total / (1ULL << 20),
173 em->stats.rx_total / (1ULL << 30), deltat);
174 fformat (stdout, "%.4f Gbit/second\n",
175 (em->stats.rx_total * 8.0) / deltat / 1e9);
176 fformat (stdout, "--------------------\n");
177 fformat (stdout, "Received close on %d streams (and %d Quic conn)\n",
178 em->stats.close_count.s, em->stats.close_count.q);
179 fformat (stdout, "Received reset on %d streams (and %d Quic conn)\n",
180 em->stats.reset_count.s, em->stats.reset_count.q);
181 fformat (stdout, "Sent close on %d streams (and %d Quic conn)\n",
182 em->stats.active_count.s, em->stats.active_count.q);
183 fformat (stdout, "Discarded %d streams (and %d Quic conn)\n",
184 em->stats.clean_count.s, em->stats.clean_count.q);
188 echo_update_count_on_session_close (echo_main_t * em, echo_session_t * s)
191 ECHO_LOG (1, "[%lu/%lu] -> S(%x) -> [%lu/%lu]",
192 s->bytes_received, s->bytes_received + s->bytes_to_receive,
193 s->session_index, s->bytes_sent,
194 s->bytes_sent + s->bytes_to_send);
195 clib_atomic_fetch_add (&em->stats.tx_total, s->bytes_sent);
196 clib_atomic_fetch_add (&em->stats.rx_total, s->bytes_received);
198 if (PREDICT_FALSE (em->stats.rx_total ==
199 em->n_clients * em->bytes_to_receive))
200 echo_notify_event (em, ECHO_EVT_LAST_BYTE);
204 echo_free_sessions (echo_main_t * em)
206 /* Free marked sessions */
208 u32 *session_indexes = 0, *session_index;
211 pool_foreach (s, em->sessions,
213 if (s->session_state == ECHO_SESSION_STATE_CLOSED)
214 vec_add1 (session_indexes, s->session_index);}
217 vec_foreach (session_index, session_indexes)
220 s = pool_elt_at_index (em->sessions, *session_index);
221 echo_session_handle_add_del (em, s->vpp_session_handle,
222 SESSION_INVALID_INDEX);
223 pool_put (em->sessions, s);
224 clib_memset (s, 0xfe, sizeof (*s));
229 test_recv_bytes (echo_main_t * em, echo_session_t * s, u8 * rx_buf,
234 for (i = 0; i < n_read; i++)
236 expected = (s->bytes_received + i) & 0xff;
237 if (rx_buf[i] == expected || em->max_test_msg > 0)
239 ECHO_LOG (0, "Session 0x%lx byte %lld was 0x%x expected 0x%x",
240 s->vpp_session_handle, s->bytes_received + i, rx_buf[i],
243 if (em->max_test_msg == 0)
244 ECHO_LOG (0, "Too many errors, hiding next ones");
245 if (em->test_return_packets == RETURN_PACKETS_ASSERT)
246 ECHO_FAIL ("test-bytes errored");
251 recv_data_chunk (echo_main_t * em, echo_session_t * s, u8 * rx_buf)
254 n_read = app_recv ((app_session_t *) s, rx_buf, vec_len (rx_buf));
257 if (svm_fifo_needs_deq_ntf (s->rx_fifo, n_read))
258 echo_session_dequeue_notify (s);
260 if (em->test_return_packets)
261 test_recv_bytes (em, s, rx_buf, n_read);
263 s->bytes_received += n_read;
264 s->bytes_to_receive -= n_read;
269 send_data_chunk (echo_session_t * s, u8 * tx_buf, int offset, int len)
272 int bytes_this_chunk = clib_min (s->bytes_to_send, len - offset);
273 if (!bytes_this_chunk)
275 n_sent = app_send ((app_session_t *) s, tx_buf + offset,
276 bytes_this_chunk, SVM_Q_WAIT);
279 s->bytes_to_send -= n_sent;
280 s->bytes_sent += n_sent;
285 mirror_data_chunk (echo_main_t * em, echo_session_t * s, u8 * tx_buf, u64 len)
288 while (n_sent < len && !em->time_to_stop)
289 n_sent += send_data_chunk (s, tx_buf, n_sent, len);
294 echo_check_closed_listener (echo_main_t * em, echo_session_t * s)
297 /* if parent has died, terminate gracefully */
298 if (s->listener_index == SESSION_INVALID_INDEX)
300 ls = pool_elt_at_index (em->sessions, s->listener_index);
301 if (ls->session_state < ECHO_SESSION_STATE_CLOSING)
303 ECHO_LOG (2, "Session 0%lx died, close child 0x%lx", ls->vpp_session_handle,
304 s->vpp_session_handle);
305 echo_update_count_on_session_close (em, s);
306 em->proto_cb_vft->cleanup_cb (s, 1 /* parent_died */ );
310 * Rx/Tx polling thread per connection
313 echo_handle_data (echo_main_t * em, echo_session_t * s, u8 * rx_buf)
315 int n_read, n_sent = 0;
317 n_read = recv_data_chunk (em, s, rx_buf);
318 if (em->data_source == ECHO_TEST_DATA_SOURCE)
319 n_sent = send_data_chunk (s, em->connect_test_data,
320 s->bytes_sent % em->tx_buf_size,
322 else if (em->data_source == ECHO_RX_DATA_SOURCE)
323 n_sent = mirror_data_chunk (em, s, rx_buf, n_read);
324 if (!s->bytes_to_send && !s->bytes_to_receive)
326 /* Session is done, need to close */
327 if (s->session_state == ECHO_SESSION_STATE_AWAIT_DATA)
328 s->session_state = ECHO_SESSION_STATE_CLOSING;
331 s->session_state = ECHO_SESSION_STATE_AWAIT_CLOSING;
332 if (em->send_stream_disconnects == ECHO_CLOSE_F_ACTIVE)
334 echo_send_rpc (em, echo_send_disconnect_session,
335 (void *) s->vpp_session_handle, 0);
336 clib_atomic_fetch_add (&em->stats.active_count.s, 1);
338 else if (em->send_stream_disconnects == ECHO_CLOSE_F_NONE)
340 s->session_state = ECHO_SESSION_STATE_CLOSING;
341 clib_atomic_fetch_add (&em->stats.clean_count.s, 1);
347 /* Check for idle clients */
350 if (n_sent || n_read)
352 else if (s->idle_cycles++ == 1e7)
355 ECHO_LOG (1, "Idle client TX:%dB RX:%dB", s->bytes_to_send,
356 s->bytes_to_receive);
357 ECHO_LOG (1, "Idle FIFOs TX:%dB RX:%dB",
358 svm_fifo_max_dequeue (s->tx_fifo),
359 svm_fifo_max_dequeue (s->rx_fifo));
360 ECHO_LOG (1, "Session 0x%lx state %u", s->vpp_session_handle,
367 echo_data_thread_fn (void *arg)
369 clib_mem_set_thread_index ();
370 echo_main_t *em = &echo_main;
371 u32 N = em->n_clients;
372 u32 n = (N + em->n_rx_threads - 1) / em->n_rx_threads;
376 ECHO_LOG (1, "Thread %u exiting, no sessions to care for", idx);
379 u32 thread_n_sessions = clib_min (n, N - n * idx);
382 u32 n_closed_sessions = 0;
386 vec_validate (rx_buf, em->rx_buf_size);
388 for (i = 0; !em->time_to_stop; i = (i + 1) % thread_n_sessions)
390 n_closed_sessions = i == 0 ? 0 : n_closed_sessions;
391 session_index = em->data_thread_args[n * idx + i];
392 if (session_index == SESSION_INVALID_INDEX)
394 s = pool_elt_at_index (em->sessions, session_index);
395 switch (s->session_state)
397 case ECHO_SESSION_STATE_READY:
398 case ECHO_SESSION_STATE_AWAIT_DATA:
399 echo_handle_data (em, s, rx_buf);
400 echo_check_closed_listener (em, s);
402 case ECHO_SESSION_STATE_AWAIT_CLOSING:
403 echo_check_closed_listener (em, s);
405 case ECHO_SESSION_STATE_CLOSING:
406 echo_update_count_on_session_close (em, s);
407 em->proto_cb_vft->cleanup_cb (s, 0 /* parent_died */ );
409 case ECHO_SESSION_STATE_CLOSED:
413 if (n_closed_sessions == thread_n_sessions)
420 session_bound_handler (session_bound_msg_t * mp)
422 echo_main_t *em = &echo_main;
423 echo_session_t *listen_session;
426 ECHO_FAIL ("bind failed: %U", format_api_error,
427 clib_net_to_host_u32 (mp->retval));
430 ECHO_LOG (0, "listening on %U:%u", format_ip46_address, mp->lcl_ip,
431 mp->lcl_is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
432 clib_net_to_host_u16 (mp->lcl_port));
434 /* Allocate local session and set it up */
435 listen_session = echo_session_new (em);
436 listen_session->session_type = ECHO_SESSION_TYPE_LISTEN;
437 listen_session->vpp_session_handle = mp->handle;
438 echo_session_handle_add_del (em, mp->handle, listen_session->session_index);
439 em->state = STATE_LISTEN;
440 em->listen_session_index = listen_session->session_index;
441 if (em->proto_cb_vft->bound_uri_cb)
442 em->proto_cb_vft->bound_uri_cb (mp, listen_session);
446 session_accepted_handler (session_accepted_msg_t * mp)
448 app_session_evt_t _app_evt, *app_evt = &_app_evt;
449 session_accepted_reply_msg_t *rmp;
450 svm_fifo_t *rx_fifo, *tx_fifo;
451 echo_main_t *em = &echo_main;
452 echo_session_t *session, *ls;
453 /* Allocate local session and set it up */
454 session = echo_session_new (em);
456 if (wait_for_segment_allocation (mp->segment_handle))
458 ECHO_FAIL ("wait_for_segment_allocation errored");
462 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
463 rx_fifo->client_session_index = session->session_index;
464 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
465 tx_fifo->client_session_index = session->session_index;
467 session->rx_fifo = rx_fifo;
468 session->tx_fifo = tx_fifo;
470 /* session->transport needed by app_send_dgram */
471 clib_memcpy_fast (&session->transport.rmt_ip, &mp->rmt.ip,
472 sizeof (ip46_address_t));
473 session->transport.is_ip4 = mp->rmt.is_ip4;
474 session->transport.rmt_port = mp->rmt.port;
475 clib_memcpy_fast (&session->transport.lcl_ip, &em->uri_elts.ip,
476 sizeof (ip46_address_t));
477 session->transport.lcl_port = em->uri_elts.port;
479 session->vpp_session_handle = mp->handle;
480 session->start = clib_time_now (&em->clib_time);
481 session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address,
483 if (!(ls = echo_get_session_from_handle (em, mp->listener_handle)))
485 session->listener_index = ls->session_index;
487 /* Add it to lookup table */
488 ECHO_LOG (1, "Accepted session 0x%lx -> 0x%lx", mp->handle,
489 mp->listener_handle);
490 echo_session_handle_add_del (em, mp->handle, session->session_index);
492 app_alloc_ctrl_evt_to_vpp (session->vpp_evt_q, app_evt,
493 SESSION_CTRL_EVT_ACCEPTED_REPLY);
494 rmp = (session_accepted_reply_msg_t *) app_evt->evt->data;
495 rmp->handle = mp->handle;
496 rmp->context = mp->context;
497 app_send_ctrl_evt_to_vpp (session->vpp_evt_q, app_evt);
498 em->proto_cb_vft->accepted_cb (mp, session);
502 session_connected_handler (session_connected_msg_t * mp)
504 echo_main_t *em = &echo_main;
505 echo_session_t *session;
506 u32 listener_index = htonl (mp->context);
507 svm_fifo_t *rx_fifo, *tx_fifo;
511 ECHO_FAIL ("connection failed with code: %U", format_api_error,
512 clib_net_to_host_u32 (mp->retval));
516 session = echo_session_new (em);
517 if (wait_for_segment_allocation (mp->segment_handle))
519 ECHO_FAIL ("wait_for_segment_allocation errored");
523 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
524 rx_fifo->client_session_index = session->session_index;
525 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
526 tx_fifo->client_session_index = session->session_index;
528 session->rx_fifo = rx_fifo;
529 session->tx_fifo = tx_fifo;
530 session->vpp_session_handle = mp->handle;
531 session->start = clib_time_now (&em->clib_time);
532 session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address,
534 session->listener_index = listener_index;
535 /* session->transport needed by app_send_dgram */
536 clib_memcpy_fast (&session->transport.lcl_ip, &mp->lcl.ip,
537 sizeof (ip46_address_t));
538 session->transport.is_ip4 = mp->lcl.is_ip4;
539 session->transport.lcl_port = mp->lcl.port;
540 clib_memcpy_fast (&session->transport.rmt_ip, &em->uri_elts.ip,
541 sizeof (ip46_address_t));
542 session->transport.rmt_port = em->uri_elts.port;
544 echo_session_handle_add_del (em, mp->handle, session->session_index);
545 em->proto_cb_vft->connected_cb ((session_connected_bundled_msg_t *) mp,
546 session->session_index, 0 /* is_failed */ );
551 * End of ECHO callback definitions
556 session_disconnected_handler (session_disconnected_msg_t * mp)
558 app_session_evt_t _app_evt, *app_evt = &_app_evt;
559 session_disconnected_reply_msg_t *rmp;
560 echo_main_t *em = &echo_main;
562 ECHO_LOG (1, "passive close session 0x%lx", mp->handle);
563 if (!(s = echo_get_session_from_handle (em, mp->handle)))
565 em->proto_cb_vft->disconnected_cb (mp, s);
567 app_alloc_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt,
568 SESSION_CTRL_EVT_DISCONNECTED_REPLY);
569 rmp = (session_disconnected_reply_msg_t *) app_evt->evt->data;
571 rmp->handle = mp->handle;
572 rmp->context = mp->context;
573 app_send_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt);
577 session_reset_handler (session_reset_msg_t * mp)
579 app_session_evt_t _app_evt, *app_evt = &_app_evt;
580 echo_main_t *em = &echo_main;
581 session_reset_reply_msg_t *rmp;
582 echo_session_t *s = 0;
583 ECHO_LOG (1, "Reset session 0x%lx", mp->handle);
584 if (!(s = echo_get_session_from_handle (em, mp->handle)))
586 em->proto_cb_vft->reset_cb (mp, s);
588 app_alloc_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt,
589 SESSION_CTRL_EVT_RESET_REPLY);
590 rmp = (session_reset_reply_msg_t *) app_evt->evt->data;
592 rmp->handle = mp->handle;
593 app_send_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt);
597 handle_mq_event (session_event_t * e)
599 switch (e->event_type)
601 case SESSION_CTRL_EVT_BOUND:
602 session_bound_handler ((session_bound_msg_t *) e->data);
604 case SESSION_CTRL_EVT_ACCEPTED:
605 session_accepted_handler ((session_accepted_msg_t *) e->data);
607 case SESSION_CTRL_EVT_CONNECTED:
608 session_connected_handler ((session_connected_msg_t *) e->data);
610 case SESSION_CTRL_EVT_DISCONNECTED:
611 session_disconnected_handler ((session_disconnected_msg_t *) e->data);
613 case SESSION_CTRL_EVT_RESET:
614 session_reset_handler ((session_reset_msg_t *) e->data);
616 case SESSION_IO_EVT_RX:
619 ECHO_LOG (0, "unhandled event %u", e->event_type);
624 echo_process_rpcs (echo_main_t * em)
628 while (em->state < STATE_DATA_DONE && !em->time_to_stop)
630 if (svm_msg_q_sub (em->rpc_msq_queue, &msg, SVM_Q_TIMEDWAIT, 1))
632 rpc = svm_msg_q_msg_data (em->rpc_msq_queue, &msg);
633 ((echo_rpc_t) rpc->fp) (rpc->arg, rpc->opaque);
634 svm_msg_q_free_msg (em->rpc_msq_queue, &msg);
639 echo_mq_thread_fn (void *arg)
641 clib_mem_set_thread_index ();
642 echo_main_t *em = &echo_main;
646 wait_for_state_change (em, STATE_ATTACHED, 0);
647 if (em->state < STATE_ATTACHED || !em->our_event_queue)
649 ECHO_FAIL ("Application failed to attach");
655 if (!(rv = svm_msg_q_sub (em->our_event_queue,
656 &msg, SVM_Q_TIMEDWAIT, 1)))
658 e = svm_msg_q_msg_data (em->our_event_queue, &msg);
660 svm_msg_q_free_msg (em->our_event_queue, &msg);
663 && (em->time_to_stop || em->state == STATE_DETACHED))
670 clients_run (echo_main_t * em)
673 echo_notify_event (em, ECHO_EVT_FIRST_QCONNECT);
674 for (i = 0; i < em->n_connects; i++)
675 echo_send_connect (em->uri, SESSION_INVALID_INDEX);
676 wait_for_state_change (em, STATE_READY, 0);
677 ECHO_LOG (1, "App is ready");
678 echo_process_rpcs (em);
682 server_run (echo_main_t * em)
684 echo_send_listen (em);
685 wait_for_state_change (em, STATE_READY, 0);
686 ECHO_LOG (1, "App is ready");
687 echo_process_rpcs (em);
689 echo_send_unbind (em);
690 if (wait_for_state_change (em, STATE_DISCONNECTED, TIMEOUT))
692 ECHO_FAIL ("Timeout waiting for state disconnected");
698 print_usage_and_exit (void)
700 echo_main_t *em = &echo_main;
703 "Usage: vpp_echo [socket-name SOCKET] [client|server] [uri URI] [OPTIONS]\n"
704 "Generates traffic and assert correct teardown of the QUIC hoststack\n"
706 " socket-name PATH Specify the binary socket path to connect to VPP\n"
707 " use-svm-api Use SVM API to connect to VPP\n"
708 " test-bytes[:assert] Check data correctness when receiving (assert fails on first error)\n"
709 " fifo-size N Use N Kb fifos\n"
710 " rx-buf N Use N Kb RX buffer\n"
711 " tx-buf N Use N Kb TX test buffer\n"
712 " appns NAMESPACE Use the namespace NAMESPACE\n"
713 " all-scope all-scope option\n"
714 " local-scope local-scope option\n"
715 " global-scope global-scope option\n"
716 " secret SECRET set namespace secret\n"
717 " chroot prefix PATH Use PATH as memory root path\n"
718 " sclose=[Y|N|W] When a stream is done, pass[N] send[Y] or wait[W] for close\n"
720 " time START:END Time between evts START & END, events being :\n"
721 " start - Start of the app\n"
722 " qconnect - first Connection connect sent\n"
723 " qconnected - last Connection connected\n"
724 " sconnect - first Stream connect sent\n"
725 " sconnected - last Stream got connected\n"
726 " lastbyte - Last expected byte received\n"
727 " exit - Exiting of the app\n"
728 " json Output global stats in json\n"
729 " log=N Set the log level to [0: no output, 1:errors, 2:log]\n"
731 " nclients N Open N clients sending data\n"
732 " nthreads N Use N busy loop threads for data [in addition to main & msg queue]\n"
733 " TX=1337[Kb|Mb|GB] Send 1337 [K|M|G]bytes, use TX=RX to reflect the data\n"
734 " RX=1337[Kb|Mb|GB] Expect 1337 [K|M|G]bytes\n" "\n");
735 for (i = 0; i < TRANSPORT_N_PROTO; i++)
737 echo_proto_cb_vft_t *vft = em->available_proto_cb_vft[i];
738 if (vft && vft->print_usage_cb)
739 vft->print_usage_cb ();
741 fprintf (stderr, "\nDefault configuration is :\n"
742 " server nclients 1/1 RX=64Kb TX=RX\n"
743 " client nclients 1/1 RX=64Kb TX=64Kb\n");
748 echo_process_each_proto_opts (unformat_input_t * a)
750 echo_main_t *em = &echo_main;
752 for (i = 0; i < TRANSPORT_N_PROTO; i++)
754 echo_proto_cb_vft_t *vft = em->available_proto_cb_vft[i];
755 if (vft && vft->process_opts_cb)
756 if ((rv = vft->process_opts_cb (a)))
763 echo_set_each_proto_defaults_before_opts (echo_main_t * em)
766 for (i = 0; i < TRANSPORT_N_PROTO; i++)
768 echo_proto_cb_vft_t *vft = em->available_proto_cb_vft[i];
769 if (vft && vft->set_defaults_before_opts_cb)
770 vft->set_defaults_before_opts_cb ();
775 echo_process_opts (int argc, char **argv)
777 echo_main_t *em = &echo_main;
778 unformat_input_t _argv, *a = &_argv;
784 unformat_init_command_line (a, argv);
785 while (unformat_check_input (a) != UNFORMAT_END_OF_INPUT)
787 if (echo_process_each_proto_opts (a))
789 else if (unformat (a, "chroot prefix %s", &chroot_prefix))
790 vl_set_memory_root_path ((char *) chroot_prefix);
791 else if (unformat (a, "uri %s", &uri))
792 em->uri = format (0, "%s%c", uri, 0);
793 else if (unformat (a, "server"))
795 else if (unformat (a, "client"))
797 else if (unformat (a, "test-bytes:assert"))
798 em->test_return_packets = RETURN_PACKETS_ASSERT;
799 else if (unformat (a, "test-bytes"))
800 em->test_return_packets = RETURN_PACKETS_LOG_WRONG;
801 else if (unformat (a, "socket-name %s", &em->socket_name))
803 else if (unformat (a, "use-svm-api"))
804 em->use_sock_api = 0;
805 else if (unformat (a, "fifo-size %d", &tmp))
806 em->fifo_size = tmp << 10;
807 else if (unformat (a, "rx-buf %d", &tmp))
808 em->rx_buf_size = tmp << 10;
809 else if (unformat (a, "tx-buf %d", &tmp))
810 em->rx_buf_size = tmp << 10;
811 else if (unformat (a, "nclients %d", &em->n_clients))
813 em->n_sessions = em->n_clients + 1;
814 em->n_connects = em->n_clients;
816 else if (unformat (a, "nthreads %d", &em->n_rx_threads))
818 else if (unformat (a, "appns %_%v%_", &em->appns_id))
820 else if (unformat (a, "all-scope"))
821 em->appns_flags |= (APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE
822 | APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE);
823 else if (unformat (a, "local-scope"))
824 em->appns_flags = APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE;
825 else if (unformat (a, "global-scope"))
826 em->appns_flags = APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
827 else if (unformat (a, "secret %lu", &em->appns_secret))
829 else if (unformat (a, "TX=RX"))
830 em->data_source = ECHO_RX_DATA_SOURCE;
831 else if (unformat (a, "TX=%U", unformat_data, &em->bytes_to_send))
833 else if (unformat (a, "RX=%U", unformat_data, &em->bytes_to_receive))
835 else if (unformat (a, "json"))
837 else if (unformat (a, "log=%d", &em->log_lvl))
839 else if (unformat (a, "sclose=%U",
840 echo_unformat_close, &em->send_stream_disconnects))
842 else if (unformat (a, "time %U:%U",
843 echo_unformat_timing_event, &em->timing.start_event,
844 echo_unformat_timing_event, &em->timing.end_event))
847 print_usage_and_exit ();
850 /* setting default for unset values
852 * bytes_to_send / bytes_to_receive & data_source */
853 if (em->bytes_to_receive == (u64) ~ 0)
854 em->bytes_to_receive = 64 << 10; /* default */
855 if (em->bytes_to_send == (u64) ~ 0)
856 em->bytes_to_send = 64 << 10; /* default */
857 else if (em->bytes_to_send == 0)
858 em->data_source = ECHO_NO_DATA_SOURCE;
860 em->data_source = ECHO_TEST_DATA_SOURCE;
862 if (em->data_source == ECHO_INVALID_DATA_SOURCE)
864 em->i_am_master ? ECHO_RX_DATA_SOURCE : ECHO_TEST_DATA_SOURCE;
865 if (em->data_source == ECHO_RX_DATA_SOURCE)
866 em->bytes_to_send = em->bytes_to_receive;
868 /* disconnect flags */
871 em->bytes_to_send == 0 ? ECHO_CLOSE_F_ACTIVE : ECHO_CLOSE_F_PASSIVE;
874 em->bytes_to_receive == 0 ? ECHO_CLOSE_F_PASSIVE : ECHO_CLOSE_F_ACTIVE;
875 if (em->send_stream_disconnects == ECHO_CLOSE_F_INVALID)
876 em->send_stream_disconnects = default_f_active;
880 echo_process_uri (echo_main_t * em)
882 unformat_input_t _input, *input = &_input;
884 unformat_init_string (input, (char *) em->uri, strlen ((char *) em->uri));
886 (input, "%U://%U/%d", unformat_transport_proto,
887 &em->uri_elts.transport_proto, unformat_ip4_address,
888 &em->uri_elts.ip.ip4, &port))
889 em->uri_elts.is_ip4 = 1;
892 (input, "%U://%U/%d", unformat_transport_proto,
893 &em->uri_elts.transport_proto, unformat_ip6_address,
894 &em->uri_elts.ip.ip6, &port))
895 em->uri_elts.is_ip4 = 0;
897 ECHO_FAIL ("Unable to process uri");
898 em->uri_elts.port = clib_host_to_net_u16 (port);
899 unformat_free (input);
902 static void __clib_constructor
905 /* init memory before proto register themselves */
906 echo_main_t *em = &echo_main;
907 clib_mem_init_thread_safe (0, 256 << 20);
908 clib_memset (em, 0, sizeof (*em));
912 main (int argc, char **argv)
914 echo_main_t *em = &echo_main;
915 fifo_segment_main_t *sm = &em->segment_main;
918 svm_msg_q_cfg_t _cfg, *cfg = &_cfg;
919 u32 rpc_queue_size = 64 << 10;
921 em->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
922 clib_spinlock_init (&em->sid_vpp_handles_lock);
923 em->shared_segment_handles = hash_create (0, sizeof (uword));
924 clib_spinlock_init (&em->segment_handles_lock);
925 em->socket_name = format (0, "%s%c", API_SOCKET_FILE, 0);
926 em->use_sock_api = 1;
927 em->fifo_size = 64 << 10;
931 em->max_test_msg = 50;
932 em->time_to_stop = 0;
934 em->n_rx_threads = 4;
935 em->test_return_packets = RETURN_PACKETS_NOTEST;
936 em->timing.start_event = ECHO_EVT_FIRST_QCONNECT;
937 em->timing.end_event = ECHO_EVT_LAST_BYTE;
938 em->bytes_to_receive = ~0; /* defaulted when we know if server/client */
939 em->bytes_to_send = ~0; /* defaulted when we know if server/client */
940 em->rx_buf_size = 1 << 20;
941 em->tx_buf_size = 1 << 20;
942 em->data_source = ECHO_INVALID_DATA_SOURCE;
943 em->uri = format (0, "%s%c", "tcp://0.0.0.0/1234", 0);
944 echo_set_each_proto_defaults_before_opts (em);
945 echo_process_opts (argc, argv);
946 echo_process_uri (em);
947 em->proto_cb_vft = em->available_proto_cb_vft[em->uri_elts.transport_proto];
948 if (!em->proto_cb_vft)
950 ECHO_FAIL ("Protocol %U is not supported",
951 format_transport_proto, em->uri_elts.transport_proto);
954 if (em->proto_cb_vft->set_defaults_after_opts_cb)
955 em->proto_cb_vft->set_defaults_after_opts_cb ();
957 vec_validate (em->data_thread_handles, em->n_rx_threads);
958 vec_validate (em->data_thread_args, em->n_clients);
959 for (i = 0; i < em->n_clients; i++)
960 em->data_thread_args[i] = SESSION_INVALID_INDEX;
961 clib_time_init (&em->clib_time);
962 init_error_string_table ();
963 fifo_segment_main_init (sm, HIGH_SEGMENT_BASEVA, 20);
964 vec_validate (em->connect_test_data, em->tx_buf_size);
965 for (i = 0; i < em->tx_buf_size; i++)
966 em->connect_test_data[i] = i & 0xff;
969 svm_msg_q_ring_cfg_t rc[1] = {
970 {rpc_queue_size, sizeof (echo_rpc_msg_t), 0},
973 cfg->consumer_pid = getpid ();
975 cfg->q_nitems = rpc_queue_size;
977 em->rpc_msq_queue = svm_msg_q_alloc (cfg);
979 signal (SIGINT, stop_signal);
980 signal (SIGQUIT, stop_signal);
981 signal (SIGTERM, stop_signal);
982 echo_api_hookup (em);
984 app_name = em->i_am_master ? "echo_server" : "echo_client";
985 if (connect_to_vpp (app_name))
988 ECHO_FAIL ("Couldn't connect to vpe, exiting...\n");
992 echo_session_prealloc (em);
993 echo_notify_event (em, ECHO_EVT_START);
995 echo_send_attach (em);
996 if (wait_for_state_change (em, STATE_ATTACHED, TIMEOUT))
998 ECHO_FAIL ("Couldn't attach to vpp, did you run <session enable> ?\n");
1001 if (pthread_create (&em->mq_thread_handle,
1002 NULL /*attr */ , echo_mq_thread_fn, 0))
1004 ECHO_FAIL ("pthread create errored\n");
1007 for (i = 0; i < em->n_rx_threads; i++)
1008 if (pthread_create (&em->data_thread_handles[i],
1009 NULL /*attr */ , echo_data_thread_fn, (void *) i))
1011 ECHO_FAIL ("pthread create errored\n");
1014 if (em->i_am_master)
1018 echo_notify_event (em, ECHO_EVT_EXIT);
1019 if (em->output_json)
1020 print_global_json_stats (em);
1022 print_global_stats (em);
1023 echo_free_sessions (em);
1024 echo_assert_test_suceeded (em);
1025 echo_send_detach (em);
1026 if (wait_for_state_change (em, STATE_DETACHED, TIMEOUT))
1028 ECHO_FAIL ("ECHO-ERROR: Couldn't detach from vpp, exiting...\n");
1032 pthread_join (em->mq_thread_handle, (void **) &rv);
1035 ECHO_FAIL ("mq pthread errored %d", rv);
1038 if (em->use_sock_api)
1039 vl_socket_client_disconnect ();
1041 vl_client_disconnect_from_vlib ();
1042 ECHO_LOG (0, "Test complete !\n");
1043 exit (em->has_failed);
1047 * fd.io coding-style-patch-verification: ON
1050 * eval: (c-set-style "gnu")