blob: 5fa5446091c1e3f9e40967c24a7828bf49ea0c59 [file] [log] [blame]
Florin Coras6792ec02017-03-13 03:49:51 -07001/*
2 * builtin_client.c - vpp built-in tcp client/connect code
3 *
4 * Copyright (c) 2017 by Cisco and/or its affiliates.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#include <vnet/vnet.h>
19#include <vnet/plugin/plugin.h>
20#include <vnet/tcp/builtin_client.h>
21
22#include <vlibapi/api.h>
23#include <vlibmemory/api.h>
24#include <vlibsocket/api.h>
25#include <vpp/app/version.h>
26
Florin Corasf03a59a2017-06-09 21:07:32 -070027#define TCP_BUILTIN_CLIENT_DBG (0)
Florin Coras3e350af2017-03-30 02:54:28 -070028
Florin Coras6792ec02017-03-13 03:49:51 -070029static void
Florin Coras68810622017-07-24 17:40:28 -070030signal_evt_to_cli_i (int *code)
31{
32 tclient_main_t *tm = &tclient_main;
33 ASSERT (vlib_get_thread_index () == 0);
34 vlib_process_signal_event (tm->vlib_main, tm->cli_node_index, *code, 0);
35}
36
37static void
38signal_evt_to_cli (int code)
39{
40 if (vlib_get_thread_index () != 0)
41 vl_api_rpc_call_main_thread (signal_evt_to_cli_i, (u8 *) & code,
42 sizeof (code));
43 else
44 signal_evt_to_cli_i (&code);
45}
46
47static void
Florin Coras6792ec02017-03-13 03:49:51 -070048send_test_chunk (tclient_main_t * tm, session_t * s)
49{
50 u8 *test_data = tm->connect_test_data;
Dave Barach45ce3fb2017-03-28 12:31:33 -040051 int test_buf_offset;
Florin Coras6792ec02017-03-13 03:49:51 -070052 u32 bytes_this_chunk;
53 session_fifo_event_t evt;
54 static int serial_number = 0;
Florin Coras68810622017-07-24 17:40:28 -070055 svm_fifo_t *txf;
Florin Coras6792ec02017-03-13 03:49:51 -070056 int rv;
Florin Coras82b13a82017-04-25 11:58:06 -070057
58 ASSERT (vec_len (test_data) > 0);
59
Florin Coras3e350af2017-03-30 02:54:28 -070060 test_buf_offset = s->bytes_sent % vec_len (test_data);
61 bytes_this_chunk = vec_len (test_data) - test_buf_offset;
Florin Coras6792ec02017-03-13 03:49:51 -070062
Florin Coras3e350af2017-03-30 02:54:28 -070063 bytes_this_chunk = bytes_this_chunk < s->bytes_to_send
64 ? bytes_this_chunk : s->bytes_to_send;
65
Florin Coras68810622017-07-24 17:40:28 -070066 txf = s->server_tx_fifo;
67 rv = svm_fifo_enqueue_nowait (txf, bytes_this_chunk,
Florin Coras3e350af2017-03-30 02:54:28 -070068 test_data + test_buf_offset);
69
70 /* If we managed to enqueue data... */
71 if (rv > 0)
Florin Coras6792ec02017-03-13 03:49:51 -070072 {
Dave Barach0194f1a2017-05-15 10:11:39 -040073 /* Account for it... */
74 s->bytes_to_send -= rv;
75 s->bytes_sent += rv;
76
Florin Coras3e350af2017-03-30 02:54:28 -070077 if (TCP_BUILTIN_CLIENT_DBG)
Florin Coras6792ec02017-03-13 03:49:51 -070078 {
Florin Coras3e350af2017-03-30 02:54:28 -070079 /* *INDENT-OFF* */
80 ELOG_TYPE_DECLARE (e) =
81 {
Dave Barach0194f1a2017-05-15 10:11:39 -040082 .format = "tx-enq: xfer %d bytes, sent %u remain %u",
83 .format_args = "i4i4i4",
Florin Coras3e350af2017-03-30 02:54:28 -070084 };
85 /* *INDENT-ON* */
86 struct
87 {
Dave Barach0194f1a2017-05-15 10:11:39 -040088 u32 data[3];
Florin Coras3e350af2017-03-30 02:54:28 -070089 } *ed;
90 ed = ELOG_DATA (&vlib_global_main.elog_main, e);
91 ed->data[0] = rv;
Dave Barach0194f1a2017-05-15 10:11:39 -040092 ed->data[1] = s->bytes_sent;
93 ed->data[2] = s->bytes_to_send;
Florin Coras3e350af2017-03-30 02:54:28 -070094 }
Florin Coras6792ec02017-03-13 03:49:51 -070095
Florin Corasf03a59a2017-06-09 21:07:32 -070096 /* Poke the session layer */
Florin Coras68810622017-07-24 17:40:28 -070097 if (svm_fifo_set_event (txf))
Florin Coras3e350af2017-03-30 02:54:28 -070098 {
99 /* Fabricate TX event, send to vpp */
Florin Coras68810622017-07-24 17:40:28 -0700100 evt.fifo = txf;
Florin Corasa5464812017-04-19 13:00:05 -0700101 evt.event_type = FIFO_EVENT_APP_TX;
Florin Coras3e350af2017-03-30 02:54:28 -0700102 evt.event_id = serial_number++;
103
Florin Coras68810622017-07-24 17:40:28 -0700104 if (unix_shared_memory_queue_add
105 (tm->vpp_event_queue[txf->master_thread_index], (u8 *) & evt,
106 0 /* do wait for mutex */ ))
Florin Corasf03a59a2017-06-09 21:07:32 -0700107 clib_warning ("could not enqueue event");
Florin Coras6792ec02017-03-13 03:49:51 -0700108 }
109 }
110}
111
112static void
113receive_test_chunk (tclient_main_t * tm, session_t * s)
114{
115 svm_fifo_t *rx_fifo = s->server_rx_fifo;
Florin Coras3e350af2017-03-30 02:54:28 -0700116 int n_read, test_bytes = 0;
Florin Coras68810622017-07-24 17:40:28 -0700117 u32 my_thread_index = vlib_get_thread_index ();
Florin Coras6792ec02017-03-13 03:49:51 -0700118
Florin Coras6792ec02017-03-13 03:49:51 -0700119 /* Allow enqueuing of new event */
Florin Coras3e350af2017-03-30 02:54:28 -0700120 // svm_fifo_unset_event (rx_fifo);
Florin Coras6792ec02017-03-13 03:49:51 -0700121
Florin Coras93992a92017-05-24 18:03:56 -0700122 if (test_bytes)
123 {
Florin Coras68810622017-07-24 17:40:28 -0700124 n_read = svm_fifo_dequeue_nowait (rx_fifo,
125 vec_len (tm->rx_buf[my_thread_index]),
126 tm->rx_buf[my_thread_index]);
Florin Coras93992a92017-05-24 18:03:56 -0700127 }
128 else
129 {
130 n_read = svm_fifo_max_dequeue (rx_fifo);
131 svm_fifo_dequeue_drop (rx_fifo, n_read);
132 }
133
Florin Coras3e350af2017-03-30 02:54:28 -0700134 if (n_read > 0)
Florin Coras6792ec02017-03-13 03:49:51 -0700135 {
Florin Coras3e350af2017-03-30 02:54:28 -0700136 if (TCP_BUILTIN_CLIENT_DBG)
Florin Coras6792ec02017-03-13 03:49:51 -0700137 {
Florin Coras3e350af2017-03-30 02:54:28 -0700138 /* *INDENT-OFF* */
139 ELOG_TYPE_DECLARE (e) =
140 {
141 .format = "rx-deq: %d bytes",
142 .format_args = "i4",
143 };
144 /* *INDENT-ON* */
145 struct
146 {
147 u32 data[1];
148 } *ed;
149 ed = ELOG_DATA (&vlib_global_main.elog_main, e);
150 ed->data[0] = n_read;
151 }
152
153 if (test_bytes)
154 {
155 int i;
Florin Coras6792ec02017-03-13 03:49:51 -0700156 for (i = 0; i < n_read; i++)
157 {
Florin Coras68810622017-07-24 17:40:28 -0700158 if (tm->rx_buf[my_thread_index][i]
159 != ((s->bytes_received + i) & 0xff))
Florin Coras6792ec02017-03-13 03:49:51 -0700160 {
161 clib_warning ("read %d error at byte %lld, 0x%x not 0x%x",
Florin Coras68810622017-07-24 17:40:28 -0700162 n_read, s->bytes_received + i,
163 tm->rx_buf[my_thread_index][i],
Florin Coras6792ec02017-03-13 03:49:51 -0700164 ((s->bytes_received + i) & 0xff));
165 }
166 }
Florin Coras6792ec02017-03-13 03:49:51 -0700167 }
Florin Coras3e350af2017-03-30 02:54:28 -0700168 s->bytes_to_receive -= n_read;
169 s->bytes_received += n_read;
Florin Coras6792ec02017-03-13 03:49:51 -0700170 }
Florin Coras6792ec02017-03-13 03:49:51 -0700171}
172
Dave Barach10d8cc62017-05-30 09:30:07 -0400173static uword
174builtin_client_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
175 vlib_frame_t * frame)
Florin Coras6792ec02017-03-13 03:49:51 -0700176{
177 tclient_main_t *tm = &tclient_main;
Dave Barach10d8cc62017-05-30 09:30:07 -0400178 int my_thread_index = vlib_get_thread_index ();
Florin Coras6792ec02017-03-13 03:49:51 -0700179 session_t *sp;
Florin Coras6792ec02017-03-13 03:49:51 -0700180 int i;
Dave Barach10d8cc62017-05-30 09:30:07 -0400181 int delete_session;
182 u32 *connection_indices;
Dave Barach2c25a622017-06-26 11:35:07 -0400183 u32 *connections_this_batch;
184 u32 nconnections_this_batch;
Dave Barach0194f1a2017-05-15 10:11:39 -0400185
Dave Barach10d8cc62017-05-30 09:30:07 -0400186 connection_indices = tm->connection_index_by_thread[my_thread_index];
Dave Barach2c25a622017-06-26 11:35:07 -0400187 connections_this_batch =
188 tm->connections_this_batch_by_thread[my_thread_index];
Florin Coras6792ec02017-03-13 03:49:51 -0700189
Dave Barach2c25a622017-06-26 11:35:07 -0400190 if ((tm->run_test == 0) ||
191 ((vec_len (connection_indices) == 0)
192 && vec_len (connections_this_batch) == 0))
Dave Barach10d8cc62017-05-30 09:30:07 -0400193 return 0;
Florin Coras6792ec02017-03-13 03:49:51 -0700194
Dave Barach2c25a622017-06-26 11:35:07 -0400195 /* Grab another pile of connections */
196 if (PREDICT_FALSE (vec_len (connections_this_batch) == 0))
197 {
198 nconnections_this_batch =
199 clib_min (tm->connections_per_batch, vec_len (connection_indices));
200
201 ASSERT (nconnections_this_batch > 0);
202 vec_validate (connections_this_batch, nconnections_this_batch - 1);
203 clib_memcpy (connections_this_batch,
204 connection_indices + vec_len (connection_indices)
205 - nconnections_this_batch,
206 nconnections_this_batch * sizeof (u32));
207 _vec_len (connection_indices) -= nconnections_this_batch;
208 }
209
210 if (PREDICT_FALSE (tm->prev_conns != tm->connections_per_batch
211 && tm->prev_conns == vec_len (connections_this_batch)))
212 {
213 tm->repeats++;
214 tm->prev_conns = vec_len (connections_this_batch);
215 if (tm->repeats == 500000)
216 {
217 clib_warning ("stuck clients");
218 }
219 }
220 else
221 {
222 tm->prev_conns = vec_len (connections_this_batch);
223 tm->repeats = 0;
224 }
225
226 for (i = 0; i < vec_len (connections_this_batch); i++)
Florin Coras6792ec02017-03-13 03:49:51 -0700227 {
Dave Barach10d8cc62017-05-30 09:30:07 -0400228 delete_session = 1;
229
Dave Barach2c25a622017-06-26 11:35:07 -0400230 sp = pool_elt_at_index (tm->sessions, connections_this_batch[i]);
Dave Barach10d8cc62017-05-30 09:30:07 -0400231
Dave Barach2c25a622017-06-26 11:35:07 -0400232 if (sp->bytes_to_send > 0)
Florin Coras6792ec02017-03-13 03:49:51 -0700233 {
Dave Barach10d8cc62017-05-30 09:30:07 -0400234 send_test_chunk (tm, sp);
235 delete_session = 0;
Florin Coras6792ec02017-03-13 03:49:51 -0700236 }
Dave Barach2c25a622017-06-26 11:35:07 -0400237 if (sp->bytes_to_receive > 0)
Florin Coras6792ec02017-03-13 03:49:51 -0700238 {
Dave Barach10d8cc62017-05-30 09:30:07 -0400239 receive_test_chunk (tm, sp);
240 delete_session = 0;
241 }
242 if (PREDICT_FALSE (delete_session == 1))
243 {
Dave Barach2c25a622017-06-26 11:35:07 -0400244 u32 index, thread_index;
245 stream_session_t *s;
246
247 __sync_fetch_and_add (&tm->tx_total, sp->bytes_sent);
Dave Barach10d8cc62017-05-30 09:30:07 -0400248 __sync_fetch_and_add (&tm->rx_total, sp->bytes_received);
Florin Corasf03a59a2017-06-09 21:07:32 -0700249
Dave Barach2c25a622017-06-26 11:35:07 -0400250 stream_session_parse_handle (sp->vpp_session_handle,
251 &index, &thread_index);
252 s = stream_session_get_if_valid (index, thread_index);
253
254 if (s)
Florin Coras93992a92017-05-24 18:03:56 -0700255 {
Florin Coras68810622017-07-24 17:40:28 -0700256 vnet_disconnect_args_t _a, *a = &_a;
257 a->handle = stream_session_handle (s);
258 a->app_index = tm->app_index;
259 vnet_disconnect_session (a);
260
Dave Barach2c25a622017-06-26 11:35:07 -0400261 vec_delete (connections_this_batch, 1, i);
262 i--;
Florin Coras93992a92017-05-24 18:03:56 -0700263 __sync_fetch_and_add (&tm->ready_connections, -1);
264 }
265 else
Dave Barach2c25a622017-06-26 11:35:07 -0400266 clib_warning ("session AWOL?");
Dave Barach10d8cc62017-05-30 09:30:07 -0400267
268 /* Kick the debug CLI process */
269 if (tm->ready_connections == 0)
Florin Coras6792ec02017-03-13 03:49:51 -0700270 {
Florin Coras68810622017-07-24 17:40:28 -0700271 signal_evt_to_cli (2);
Florin Coras6792ec02017-03-13 03:49:51 -0700272 }
Florin Coras6792ec02017-03-13 03:49:51 -0700273 }
Florin Coras6792ec02017-03-13 03:49:51 -0700274 }
Dave Barach2c25a622017-06-26 11:35:07 -0400275
276 tm->connection_index_by_thread[my_thread_index] = connection_indices;
277 tm->connections_this_batch_by_thread[my_thread_index] =
278 connections_this_batch;
Florin Coras6792ec02017-03-13 03:49:51 -0700279 return 0;
280}
281
Dave Barach10d8cc62017-05-30 09:30:07 -0400282/* *INDENT-OFF* */
283VLIB_REGISTER_NODE (builtin_client_node) =
284{
285 .function = builtin_client_node_fn,
286 .name = "builtin-tcp-client",
287 .type = VLIB_NODE_TYPE_INPUT,
288 .state = VLIB_NODE_STATE_DISABLED,
289};
290/* *INDENT-ON* */
291
Florin Coras6cf30ad2017-04-04 23:08:23 -0700292static int
Florin Coras6792ec02017-03-13 03:49:51 -0700293create_api_loopback (tclient_main_t * tm)
294{
Florin Coras6792ec02017-03-13 03:49:51 -0700295 api_main_t *am = &api_main;
296 vl_shmem_hdr_t *shmem_hdr;
Florin Coras6792ec02017-03-13 03:49:51 -0700297
298 shmem_hdr = am->shmem_hdr;
299 tm->vl_input_queue = shmem_hdr->vl_input_queue;
Dave Barach52851e62017-08-07 09:35:25 -0400300 tm->my_client_index =
301 vl_api_memclnt_create_internal ("tcp_test_client", tm->vl_input_queue);
Florin Coras6792ec02017-03-13 03:49:51 -0700302 return 0;
303}
304
Florin Coras6cf30ad2017-04-04 23:08:23 -0700305static int
306tcp_test_clients_init (vlib_main_t * vm)
307{
308 tclient_main_t *tm = &tclient_main;
Florin Coras68810622017-07-24 17:40:28 -0700309 vlib_thread_main_t *vtm = vlib_get_thread_main ();
310 u32 num_threads;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700311 int i;
312
Florin Coras6cf30ad2017-04-04 23:08:23 -0700313 if (create_api_loopback (tm))
314 return -1;
315
Florin Coras68810622017-07-24 17:40:28 -0700316 num_threads = 1 /* main thread */ + vtm->n_threads;
317
Florin Corasf03a59a2017-06-09 21:07:32 -0700318 /* Init test data. Big buffer */
319 vec_validate (tm->connect_test_data, 1024 * 1024 - 1);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700320 for (i = 0; i < vec_len (tm->connect_test_data); i++)
321 tm->connect_test_data[i] = i & 0xff;
322
Florin Coras68810622017-07-24 17:40:28 -0700323 vec_validate (tm->rx_buf, num_threads - 1);
324 for (i = 0; i < num_threads; i++)
325 vec_validate (tm->rx_buf[i], vec_len (tm->connect_test_data) - 1);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700326
327 tm->is_init = 1;
Dave Barach10d8cc62017-05-30 09:30:07 -0400328
Florin Coras68810622017-07-24 17:40:28 -0700329 vec_validate (tm->connection_index_by_thread, vtm->n_vlib_mains);
330 vec_validate (tm->connections_this_batch_by_thread, vtm->n_vlib_mains);
331 vec_validate (tm->vpp_event_queue, vtm->n_vlib_mains);
332
Dave Barach10d8cc62017-05-30 09:30:07 -0400333 return 0;
334}
335
336static int
337builtin_session_connected_callback (u32 app_index, u32 api_context,
338 stream_session_t * s, u8 is_fail)
339{
Florin Corasf03a59a2017-06-09 21:07:32 -0700340 tclient_main_t *tm = &tclient_main;
341 session_t *session;
342 u32 session_index;
Florin Coras68810622017-07-24 17:40:28 -0700343 u8 thread_index = vlib_get_thread_index ();
344
Florin Corasf03a59a2017-06-09 21:07:32 -0700345 if (is_fail)
Dave Barach10d8cc62017-05-30 09:30:07 -0400346 {
Florin Corasf03a59a2017-06-09 21:07:32 -0700347 clib_warning ("connection %d failed!", api_context);
Florin Coras68810622017-07-24 17:40:28 -0700348 signal_evt_to_cli (-1);
349 return 0;
Dave Barach10d8cc62017-05-30 09:30:07 -0400350 }
351
Dave Barachac0c96b2017-08-02 09:19:32 -0400352 ASSERT (s->thread_index == thread_index);
353
Florin Coras68810622017-07-24 17:40:28 -0700354 if (!tm->vpp_event_queue[thread_index])
355 tm->vpp_event_queue[thread_index] =
356 session_manager_get_vpp_event_queue (thread_index);
Florin Corasf03a59a2017-06-09 21:07:32 -0700357
358 /*
359 * Setup session
360 */
Florin Coras68810622017-07-24 17:40:28 -0700361 clib_spinlock_lock_if_init (&tm->sessions_lock);
Florin Corasf03a59a2017-06-09 21:07:32 -0700362 pool_get (tm->sessions, session);
Florin Coras68810622017-07-24 17:40:28 -0700363 clib_spinlock_unlock_if_init (&tm->sessions_lock);
364
Florin Corasf03a59a2017-06-09 21:07:32 -0700365 memset (session, 0, sizeof (*session));
366 session_index = session - tm->sessions;
Dave Barach2c25a622017-06-26 11:35:07 -0400367 session->bytes_to_send = tm->bytes_to_send;
368 session->bytes_to_receive = tm->no_return ? 0ULL : tm->bytes_to_send;
Florin Corasf03a59a2017-06-09 21:07:32 -0700369 session->server_rx_fifo = s->server_rx_fifo;
370 session->server_rx_fifo->client_session_index = session_index;
371 session->server_tx_fifo = s->server_tx_fifo;
372 session->server_tx_fifo->client_session_index = session_index;
373 session->vpp_session_handle = stream_session_handle (s);
374
Florin Coras68810622017-07-24 17:40:28 -0700375 vec_add1 (tm->connection_index_by_thread[thread_index], session_index);
Florin Corasf03a59a2017-06-09 21:07:32 -0700376 __sync_fetch_and_add (&tm->ready_connections, 1);
377 if (tm->ready_connections == tm->expected_connections)
378 {
379 tm->run_test = 1;
Florin Corasf03a59a2017-06-09 21:07:32 -0700380 /* Signal the CLI process that the action is starting... */
Florin Coras68810622017-07-24 17:40:28 -0700381 signal_evt_to_cli (1);
Florin Corasf03a59a2017-06-09 21:07:32 -0700382 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700383
384 return 0;
385}
386
387static void
388builtin_session_reset_callback (stream_session_t * s)
389{
Florin Coras3eb50622017-07-13 01:24:57 -0400390 if (s->session_state == SESSION_STATE_READY)
391 clib_warning ("Reset active connection %U", format_stream_session, s, 2);
Florin Coras6534b7a2017-07-18 05:38:03 -0400392 stream_session_cleanup (s);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700393 return;
394}
395
396static int
Florin Coras6cf30ad2017-04-04 23:08:23 -0700397builtin_session_create_callback (stream_session_t * s)
398{
399 return 0;
400}
401
402static void
403builtin_session_disconnect_callback (stream_session_t * s)
404{
Florin Coras6534b7a2017-07-18 05:38:03 -0400405 tclient_main_t *tm = &tclient_main;
406 vnet_disconnect_args_t _a, *a = &_a;
407 a->handle = stream_session_handle (s);
408 a->app_index = tm->app_index;
409 vnet_disconnect_session (a);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700410 return;
411}
412
413static int
414builtin_server_rx_callback (stream_session_t * s)
415{
416 return 0;
417}
418
419/* *INDENT-OFF* */
Florin Corasf03a59a2017-06-09 21:07:32 -0700420static session_cb_vft_t builtin_clients = {
421 .session_reset_callback = builtin_session_reset_callback,
422 .session_connected_callback = builtin_session_connected_callback,
423 .session_accept_callback = builtin_session_create_callback,
424 .session_disconnect_callback = builtin_session_disconnect_callback,
425 .builtin_server_rx_callback = builtin_server_rx_callback
426};
Florin Coras6cf30ad2017-04-04 23:08:23 -0700427/* *INDENT-ON* */
428
429static int
Florin Corasf03a59a2017-06-09 21:07:32 -0700430attach_builtin_test_clients_app (void)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700431{
Dave Barach0194f1a2017-05-15 10:11:39 -0400432 tclient_main_t *tm = &tclient_main;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700433 vnet_app_attach_args_t _a, *a = &_a;
434 u8 segment_name[128];
Florin Corasf03a59a2017-06-09 21:07:32 -0700435 u32 segment_name_length, prealloc_fifos;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700436 u64 options[16];
437
438 segment_name_length = ARRAY_LEN (segment_name);
439
440 memset (a, 0, sizeof (*a));
441 memset (options, 0, sizeof (options));
442
Dave Barach0194f1a2017-05-15 10:11:39 -0400443 a->api_client_index = tm->my_client_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700444 a->segment_name = segment_name;
445 a->segment_name_length = segment_name_length;
446 a->session_cb_vft = &builtin_clients;
447
Florin Corasf03a59a2017-06-09 21:07:32 -0700448 prealloc_fifos = tm->prealloc_fifos ? tm->expected_connections : 1;
449
Florin Coras6cf30ad2017-04-04 23:08:23 -0700450 options[SESSION_OPTIONS_ACCEPT_COOKIE] = 0x12345678;
Florin Corasf03a59a2017-06-09 21:07:32 -0700451 options[SESSION_OPTIONS_SEGMENT_SIZE] = (2ULL << 32);
452 options[SESSION_OPTIONS_RX_FIFO_SIZE] = tm->fifo_size;
Florin Coras6534b7a2017-07-18 05:38:03 -0400453 options[SESSION_OPTIONS_TX_FIFO_SIZE] = tm->fifo_size;
Dave Barach2c25a622017-06-26 11:35:07 -0400454 options[APP_OPTIONS_PRIVATE_SEGMENT_COUNT] = tm->private_segment_count;
455 options[APP_OPTIONS_PRIVATE_SEGMENT_SIZE] = tm->private_segment_size;
Florin Corasf03a59a2017-06-09 21:07:32 -0700456 options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = prealloc_fifos;
457
Florin Corasa5464812017-04-19 13:00:05 -0700458 options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_BUILTIN_APP;
459
Florin Coras6cf30ad2017-04-04 23:08:23 -0700460 a->options = options;
461
Florin Corasf03a59a2017-06-09 21:07:32 -0700462 if (vnet_application_attach (a))
463 return -1;
464
465 tm->app_index = a->app_index;
466 return 0;
467}
468
469static void *
470tclient_thread_fn (void *arg)
471{
472 return 0;
473}
474
475/** Start a transmit thread */
476int
477start_tx_pthread (tclient_main_t * tm)
478{
479 if (tm->client_thread_handle == 0)
480 {
481 int rv = pthread_create (&tm->client_thread_handle,
482 NULL /*attr */ ,
483 tclient_thread_fn, 0);
484 if (rv)
485 {
486 tm->client_thread_handle = 0;
487 return -1;
488 }
489 }
490 return 0;
491}
492
493void
494clients_connect (vlib_main_t * vm, u8 * uri, u32 n_clients)
495{
496 tclient_main_t *tm = &tclient_main;
497 vnet_connect_args_t _a, *a = &_a;
498 int i;
499 for (i = 0; i < n_clients; i++)
500 {
501 memset (a, 0, sizeof (*a));
502
503 a->uri = (char *) uri;
504 a->api_context = i;
505 a->app_index = tm->app_index;
506 a->mp = 0;
507 vnet_connect_uri (a);
508
Florin Coras66b11312017-07-31 17:18:03 -0700509 /* Crude pacing for call setups */
510 if ((i % 4) == 0)
511 vlib_process_suspend (vm, 10e-6);
Florin Corasf03a59a2017-06-09 21:07:32 -0700512 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700513}
Florin Coras6792ec02017-03-13 03:49:51 -0700514
515static clib_error_t *
516test_tcp_clients_command_fn (vlib_main_t * vm,
517 unformat_input_t * input,
518 vlib_cli_command_t * cmd)
519{
Florin Coras6cf30ad2017-04-04 23:08:23 -0700520 tclient_main_t *tm = &tclient_main;
Dave Barach10d8cc62017-05-30 09:30:07 -0400521 vlib_thread_main_t *thread_main = vlib_get_thread_main ();
Florin Corasf03a59a2017-06-09 21:07:32 -0700522 uword *event_data = 0, event_type;
523 u8 *default_connect_uri = (u8 *) "tcp://6.0.1.1/1234", *uri;
524 u64 tmp, total_bytes;
Florin Coras66b11312017-07-31 17:18:03 -0700525 f64 test_timeout = 20.0, syn_timeout = 20.0, delta;
526 f64 time_before_connects;
Florin Coras6792ec02017-03-13 03:49:51 -0700527 u32 n_clients = 1;
Florin Coras66b11312017-07-31 17:18:03 -0700528 int preallocate_sessions = 0;
Florin Corasf03a59a2017-06-09 21:07:32 -0700529 char *transfer_type;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700530 int i;
Florin Coras6792ec02017-03-13 03:49:51 -0700531
532 tm->bytes_to_send = 8192;
Florin Corasf03a59a2017-06-09 21:07:32 -0700533 tm->no_return = 0;
534 tm->fifo_size = 64 << 10;
Dave Barach2c25a622017-06-26 11:35:07 -0400535 tm->connections_per_batch = 1000;
536 tm->private_segment_count = 0;
537 tm->private_segment_size = 0;
Florin Coras68810622017-07-24 17:40:28 -0700538 tm->vlib_main = vm;
539 if (thread_main->n_vlib_mains > 1)
540 clib_spinlock_init (&tm->sessions_lock);
Florin Coras6792ec02017-03-13 03:49:51 -0700541 vec_free (tm->connect_uri);
542
543 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
544 {
545 if (unformat (input, "nclients %d", &n_clients))
546 ;
Dave Barach0194f1a2017-05-15 10:11:39 -0400547 else if (unformat (input, "mbytes %lld", &tmp))
548 tm->bytes_to_send = tmp << 20;
549 else if (unformat (input, "gbytes %lld", &tmp))
550 tm->bytes_to_send = tmp << 30;
Florin Coras3e350af2017-03-30 02:54:28 -0700551 else if (unformat (input, "bytes %lld", &tm->bytes_to_send))
Florin Coras6792ec02017-03-13 03:49:51 -0700552 ;
553 else if (unformat (input, "uri %s", &tm->connect_uri))
554 ;
Florin Coras66b11312017-07-31 17:18:03 -0700555 else if (unformat (input, "test-timeout %f", &test_timeout))
556 ;
557 else if (unformat (input, "syn-timeout %f", &syn_timeout))
Dave Barach10d8cc62017-05-30 09:30:07 -0400558 ;
Florin Corasf03a59a2017-06-09 21:07:32 -0700559 else if (unformat (input, "no-return"))
560 tm->no_return = 1;
561 else if (unformat (input, "fifo-size %d", &tm->fifo_size))
562 tm->fifo_size <<= 10;
Dave Barach2c25a622017-06-26 11:35:07 -0400563 else if (unformat (input, "private-segment-count %d",
564 &tm->private_segment_count))
565 ;
566 else if (unformat (input, "private-segment-size %dm", &tmp))
567 tm->private_segment_size = tmp << 20;
568 else if (unformat (input, "private-segment-size %dg", &tmp))
569 tm->private_segment_size = tmp << 30;
570 else if (unformat (input, "private-segment-size %d", &tmp))
571 tm->private_segment_size = tmp;
572 else if (unformat (input, "preallocate-fifos"))
573 tm->prealloc_fifos = 1;
Florin Coras66b11312017-07-31 17:18:03 -0700574 else if (unformat (input, "preallocate-sessions"))
575 preallocate_sessions = 1;
Dave Barach2c25a622017-06-26 11:35:07 -0400576 else
577 if (unformat (input, "client-batch %d", &tm->connections_per_batch))
578 ;
Florin Coras6792ec02017-03-13 03:49:51 -0700579 else
580 return clib_error_return (0, "unknown input `%U'",
581 format_unformat_error, input);
582 }
583
Florin Corasf03a59a2017-06-09 21:07:32 -0700584 /* Store cli process node index for signalling */
585 tm->cli_node_index = vlib_get_current_process (vm)->node_runtime.node_index;
586
Florin Coras6cf30ad2017-04-04 23:08:23 -0700587 if (tm->is_init == 0)
588 {
589 if (tcp_test_clients_init (vm))
590 return clib_error_return (0, "failed init");
591 }
592
Florin Coras66b11312017-07-31 17:18:03 -0700593
Florin Coras6792ec02017-03-13 03:49:51 -0700594 tm->ready_connections = 0;
595 tm->expected_connections = n_clients;
Dave Barach10d8cc62017-05-30 09:30:07 -0400596 tm->rx_total = 0;
Florin Corasf03a59a2017-06-09 21:07:32 -0700597 tm->tx_total = 0;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700598
Florin Corasf03a59a2017-06-09 21:07:32 -0700599 uri = default_connect_uri;
Florin Coras6792ec02017-03-13 03:49:51 -0700600 if (tm->connect_uri)
601 uri = tm->connect_uri;
602
Florin Coras3e350af2017-03-30 02:54:28 -0700603#if TCP_BUILTIN_CLIENT_PTHREAD
Florin Corasf03a59a2017-06-09 21:07:32 -0700604 start_tx_pthread ();
605#endif
606
Florin Coras68810622017-07-24 17:40:28 -0700607 vlib_worker_thread_barrier_sync (vm);
Florin Corasf03a59a2017-06-09 21:07:32 -0700608 vnet_session_enable_disable (vm, 1 /* turn on TCP, etc. */ );
Florin Coras68810622017-07-24 17:40:28 -0700609 vlib_worker_thread_barrier_release (vm);
Florin Corasf03a59a2017-06-09 21:07:32 -0700610
611 if (tm->test_client_attached == 0)
Florin Coras6792ec02017-03-13 03:49:51 -0700612 {
Florin Corasf03a59a2017-06-09 21:07:32 -0700613 if (attach_builtin_test_clients_app ())
Florin Coras6792ec02017-03-13 03:49:51 -0700614 {
Florin Corasf03a59a2017-06-09 21:07:32 -0700615 return clib_error_return (0, "app attach failed");
Florin Coras6792ec02017-03-13 03:49:51 -0700616 }
617 }
Dave Barach10d8cc62017-05-30 09:30:07 -0400618 tm->test_client_attached = 1;
Florin Coras6792ec02017-03-13 03:49:51 -0700619
Dave Barach10d8cc62017-05-30 09:30:07 -0400620 /* Turn on the builtin client input nodes */
621 for (i = 0; i < thread_main->n_vlib_mains; i++)
622 vlib_node_set_state (vlib_mains[i], builtin_client_node.index,
623 VLIB_NODE_STATE_POLLING);
624
Florin Coras66b11312017-07-31 17:18:03 -0700625 if (preallocate_sessions)
626 {
627 session_t *sp __attribute__ ((unused));
628 for (i = 0; i < n_clients; i++)
629 pool_get (tm->sessions, sp);
630 for (i = 0; i < n_clients; i++)
631 pool_put_index (tm->sessions, i);
632 }
633
Dave Barach10d8cc62017-05-30 09:30:07 -0400634 /* Fire off connect requests */
Florin Coras66b11312017-07-31 17:18:03 -0700635 time_before_connects = vlib_time_now (vm);
Florin Corasf03a59a2017-06-09 21:07:32 -0700636 clients_connect (vm, uri, n_clients);
Florin Coras6792ec02017-03-13 03:49:51 -0700637
Dave Barach10d8cc62017-05-30 09:30:07 -0400638 /* Park until the sessions come up, or ten seconds elapse... */
Florin Coras66b11312017-07-31 17:18:03 -0700639 vlib_process_wait_for_event_or_clock (vm, syn_timeout);
Dave Barach10d8cc62017-05-30 09:30:07 -0400640 event_type = vlib_process_get_events (vm, &event_data);
Dave Barach10d8cc62017-05-30 09:30:07 -0400641 switch (event_type)
642 {
643 case ~0:
644 vlib_cli_output (vm, "Timeout with only %d sessions active...",
645 tm->ready_connections);
646 goto cleanup;
647
648 case 1:
Florin Coras66b11312017-07-31 17:18:03 -0700649 delta = vlib_time_now (vm) - time_before_connects;
650
651 if (delta != 0.0)
652 {
653 vlib_cli_output
654 (vm, "%d three-way handshakes in %.2f seconds, %.2f/sec",
655 n_clients, delta, ((f64) n_clients) / delta);
656 }
657
Florin Coras68810622017-07-24 17:40:28 -0700658 tm->test_start_time = vlib_time_now (tm->vlib_main);
Dave Barach10d8cc62017-05-30 09:30:07 -0400659 vlib_cli_output (vm, "Test started at %.6f", tm->test_start_time);
660 break;
661
662 default:
663 vlib_cli_output (vm, "unexpected event(1): %d", event_type);
664 goto cleanup;
665 }
666
667 /* Now wait for the sessions to finish... */
Florin Coras66b11312017-07-31 17:18:03 -0700668 vlib_process_wait_for_event_or_clock (vm, test_timeout);
Dave Barach10d8cc62017-05-30 09:30:07 -0400669 event_type = vlib_process_get_events (vm, &event_data);
Dave Barach10d8cc62017-05-30 09:30:07 -0400670 switch (event_type)
671 {
672 case ~0:
673 vlib_cli_output (vm, "Timeout with %d sessions still active...",
674 tm->ready_connections);
675 goto cleanup;
676
677 case 2:
Florin Coras68810622017-07-24 17:40:28 -0700678 tm->test_end_time = vlib_time_now (vm);
Dave Barach10d8cc62017-05-30 09:30:07 -0400679 vlib_cli_output (vm, "Test finished at %.6f", tm->test_end_time);
680 break;
681
682 default:
683 vlib_cli_output (vm, "unexpected event(2): %d", event_type);
684 goto cleanup;
685 }
686
687 delta = tm->test_end_time - tm->test_start_time;
688
689 if (delta != 0.0)
690 {
Florin Corasf03a59a2017-06-09 21:07:32 -0700691 total_bytes = (tm->no_return ? tm->tx_total : tm->rx_total);
692 transfer_type = tm->no_return ? "half-duplex" : "full-duplex";
Dave Barach10d8cc62017-05-30 09:30:07 -0400693 vlib_cli_output (vm,
694 "%lld bytes (%lld mbytes, %lld gbytes) in %.2f seconds",
Florin Corasf03a59a2017-06-09 21:07:32 -0700695 total_bytes, total_bytes / (1ULL << 20),
696 total_bytes / (1ULL << 30), delta);
697 vlib_cli_output (vm, "%.2f bytes/second %s",
698 ((f64) total_bytes) / (delta), transfer_type);
699 vlib_cli_output (vm, "%.4f gbit/second %s",
700 (((f64) total_bytes * 8.0) / delta / 1e9),
701 transfer_type);
Dave Barach10d8cc62017-05-30 09:30:07 -0400702 }
703 else
704 vlib_cli_output (vm, "zero delta-t?");
705
706cleanup:
Dave Barach2c25a622017-06-26 11:35:07 -0400707 tm->run_test = 0;
Dave Barach10d8cc62017-05-30 09:30:07 -0400708 for (i = 0; i < vec_len (tm->connection_index_by_thread); i++)
Dave Barach2c25a622017-06-26 11:35:07 -0400709 {
710 vec_reset_length (tm->connection_index_by_thread[i]);
711 vec_reset_length (tm->connections_this_batch_by_thread[i]);
712 }
Florin Coras68810622017-07-24 17:40:28 -0700713
Dave Barach2c25a622017-06-26 11:35:07 -0400714 pool_free (tm->sessions);
Florin Coras6792ec02017-03-13 03:49:51 -0700715
Dave Barach818eb542017-08-02 13:56:13 -0400716 /* Detach the application, so we can use different fifo sizes next time */
717 if (tm->test_client_attached)
718 {
719 vnet_app_detach_args_t _da, *da = &_da;
720 int rv;
721
722 da->app_index = tm->app_index;
723
724 rv = vnet_application_detach (da);
725 if (rv)
726 vlib_cli_output (vm, "WARNING: app detach failed...");
727 tm->test_client_attached = 0;
728 tm->app_index = ~0;
729 }
Florin Coras6792ec02017-03-13 03:49:51 -0700730 return 0;
731}
732
Florin Coras3e350af2017-03-30 02:54:28 -0700733/* *INDENT-OFF* */
Florin Coras6792ec02017-03-13 03:49:51 -0700734VLIB_CLI_COMMAND (test_clients_command, static) =
735{
736 .path = "test tcp clients",
Dave Barach0194f1a2017-05-15 10:11:39 -0400737 .short_help = "test tcp clients [nclients %d]"
738 "[iterations %d] [bytes %d] [uri tcp://6.0.1.1/1234]",
Florin Coras6792ec02017-03-13 03:49:51 -0700739 .function = test_tcp_clients_command_fn,
Florin Coras68810622017-07-24 17:40:28 -0700740 .is_mp_safe = 1,
Florin Coras6792ec02017-03-13 03:49:51 -0700741};
742/* *INDENT-ON* */
743
Florin Coras6cf30ad2017-04-04 23:08:23 -0700744clib_error_t *
745tcp_test_clients_main_init (vlib_main_t * vm)
746{
747 tclient_main_t *tm = &tclient_main;
748 tm->is_init = 0;
749 return 0;
750}
751
752VLIB_INIT_FUNCTION (tcp_test_clients_main_init);
753
Florin Coras6792ec02017-03-13 03:49:51 -0700754/*
755 * fd.io coding-style-patch-verification: ON
756 *
757 * Local Variables:
758 * eval: (c-set-style "gnu")
759 * End:
760 */