blob: 4258fc430103df36fe174a49db2f03b6284ed229 [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>
Florin Coras6792ec02017-03-13 03:49:51 -070024#include <vpp/app/version.h>
25
Florin Corasf03a59a2017-06-09 21:07:32 -070026#define TCP_BUILTIN_CLIENT_DBG (0)
Florin Coras3e350af2017-03-30 02:54:28 -070027
Florin Coras6792ec02017-03-13 03:49:51 -070028static void
Florin Coras68810622017-07-24 17:40:28 -070029signal_evt_to_cli_i (int *code)
30{
31 tclient_main_t *tm = &tclient_main;
32 ASSERT (vlib_get_thread_index () == 0);
33 vlib_process_signal_event (tm->vlib_main, tm->cli_node_index, *code, 0);
34}
35
36static void
37signal_evt_to_cli (int code)
38{
39 if (vlib_get_thread_index () != 0)
40 vl_api_rpc_call_main_thread (signal_evt_to_cli_i, (u8 *) & code,
41 sizeof (code));
42 else
43 signal_evt_to_cli_i (&code);
44}
45
46static void
Florin Coras6792ec02017-03-13 03:49:51 -070047send_test_chunk (tclient_main_t * tm, session_t * s)
48{
49 u8 *test_data = tm->connect_test_data;
Dave Barach45ce3fb2017-03-28 12:31:33 -040050 int test_buf_offset;
Florin Coras6792ec02017-03-13 03:49:51 -070051 u32 bytes_this_chunk;
52 session_fifo_event_t evt;
Florin Coras68810622017-07-24 17:40:28 -070053 svm_fifo_t *txf;
Florin Coras6792ec02017-03-13 03:49:51 -070054 int rv;
Florin Coras82b13a82017-04-25 11:58:06 -070055
56 ASSERT (vec_len (test_data) > 0);
57
Florin Coras3e350af2017-03-30 02:54:28 -070058 test_buf_offset = s->bytes_sent % vec_len (test_data);
59 bytes_this_chunk = vec_len (test_data) - test_buf_offset;
Florin Coras6792ec02017-03-13 03:49:51 -070060
Florin Coras3e350af2017-03-30 02:54:28 -070061 bytes_this_chunk = bytes_this_chunk < s->bytes_to_send
62 ? bytes_this_chunk : s->bytes_to_send;
63
Florin Coras68810622017-07-24 17:40:28 -070064 txf = s->server_tx_fifo;
65 rv = svm_fifo_enqueue_nowait (txf, bytes_this_chunk,
Florin Coras3e350af2017-03-30 02:54:28 -070066 test_data + test_buf_offset);
67
68 /* If we managed to enqueue data... */
69 if (rv > 0)
Florin Coras6792ec02017-03-13 03:49:51 -070070 {
Dave Barach0194f1a2017-05-15 10:11:39 -040071 /* Account for it... */
72 s->bytes_to_send -= rv;
73 s->bytes_sent += rv;
74
Florin Coras3e350af2017-03-30 02:54:28 -070075 if (TCP_BUILTIN_CLIENT_DBG)
Florin Coras6792ec02017-03-13 03:49:51 -070076 {
Florin Coras3e350af2017-03-30 02:54:28 -070077 /* *INDENT-OFF* */
78 ELOG_TYPE_DECLARE (e) =
79 {
Dave Barach0194f1a2017-05-15 10:11:39 -040080 .format = "tx-enq: xfer %d bytes, sent %u remain %u",
81 .format_args = "i4i4i4",
Florin Coras3e350af2017-03-30 02:54:28 -070082 };
83 /* *INDENT-ON* */
84 struct
85 {
Dave Barach0194f1a2017-05-15 10:11:39 -040086 u32 data[3];
Florin Coras3e350af2017-03-30 02:54:28 -070087 } *ed;
88 ed = ELOG_DATA (&vlib_global_main.elog_main, e);
89 ed->data[0] = rv;
Dave Barach0194f1a2017-05-15 10:11:39 -040090 ed->data[1] = s->bytes_sent;
91 ed->data[2] = s->bytes_to_send;
Florin Coras3e350af2017-03-30 02:54:28 -070092 }
Florin Coras6792ec02017-03-13 03:49:51 -070093
Florin Corasf03a59a2017-06-09 21:07:32 -070094 /* Poke the session layer */
Florin Coras68810622017-07-24 17:40:28 -070095 if (svm_fifo_set_event (txf))
Florin Coras3e350af2017-03-30 02:54:28 -070096 {
97 /* Fabricate TX event, send to vpp */
Florin Coras68810622017-07-24 17:40:28 -070098 evt.fifo = txf;
Florin Corasa5464812017-04-19 13:00:05 -070099 evt.event_type = FIFO_EVENT_APP_TX;
Florin Coras3e350af2017-03-30 02:54:28 -0700100
Florin Coras68810622017-07-24 17:40:28 -0700101 if (unix_shared_memory_queue_add
102 (tm->vpp_event_queue[txf->master_thread_index], (u8 *) & evt,
103 0 /* do wait for mutex */ ))
Florin Corasf03a59a2017-06-09 21:07:32 -0700104 clib_warning ("could not enqueue event");
Florin Coras6792ec02017-03-13 03:49:51 -0700105 }
106 }
107}
108
109static void
110receive_test_chunk (tclient_main_t * tm, session_t * s)
111{
112 svm_fifo_t *rx_fifo = s->server_rx_fifo;
Florin Coras3e350af2017-03-30 02:54:28 -0700113 int n_read, test_bytes = 0;
Florin Coras68810622017-07-24 17:40:28 -0700114 u32 my_thread_index = vlib_get_thread_index ();
Florin Coras6792ec02017-03-13 03:49:51 -0700115
Florin Coras6792ec02017-03-13 03:49:51 -0700116 /* Allow enqueuing of new event */
Florin Coras3e350af2017-03-30 02:54:28 -0700117 // svm_fifo_unset_event (rx_fifo);
Florin Coras6792ec02017-03-13 03:49:51 -0700118
Florin Coras93992a92017-05-24 18:03:56 -0700119 if (test_bytes)
120 {
Florin Coras68810622017-07-24 17:40:28 -0700121 n_read = svm_fifo_dequeue_nowait (rx_fifo,
122 vec_len (tm->rx_buf[my_thread_index]),
123 tm->rx_buf[my_thread_index]);
Florin Coras93992a92017-05-24 18:03:56 -0700124 }
125 else
126 {
127 n_read = svm_fifo_max_dequeue (rx_fifo);
128 svm_fifo_dequeue_drop (rx_fifo, n_read);
129 }
130
Florin Coras3e350af2017-03-30 02:54:28 -0700131 if (n_read > 0)
Florin Coras6792ec02017-03-13 03:49:51 -0700132 {
Florin Coras3e350af2017-03-30 02:54:28 -0700133 if (TCP_BUILTIN_CLIENT_DBG)
Florin Coras6792ec02017-03-13 03:49:51 -0700134 {
Florin Coras3e350af2017-03-30 02:54:28 -0700135 /* *INDENT-OFF* */
136 ELOG_TYPE_DECLARE (e) =
137 {
138 .format = "rx-deq: %d bytes",
139 .format_args = "i4",
140 };
141 /* *INDENT-ON* */
142 struct
143 {
144 u32 data[1];
145 } *ed;
146 ed = ELOG_DATA (&vlib_global_main.elog_main, e);
147 ed->data[0] = n_read;
148 }
149
150 if (test_bytes)
151 {
152 int i;
Florin Coras6792ec02017-03-13 03:49:51 -0700153 for (i = 0; i < n_read; i++)
154 {
Florin Coras68810622017-07-24 17:40:28 -0700155 if (tm->rx_buf[my_thread_index][i]
156 != ((s->bytes_received + i) & 0xff))
Florin Coras6792ec02017-03-13 03:49:51 -0700157 {
158 clib_warning ("read %d error at byte %lld, 0x%x not 0x%x",
Florin Coras68810622017-07-24 17:40:28 -0700159 n_read, s->bytes_received + i,
160 tm->rx_buf[my_thread_index][i],
Florin Coras6792ec02017-03-13 03:49:51 -0700161 ((s->bytes_received + i) & 0xff));
162 }
163 }
Florin Coras6792ec02017-03-13 03:49:51 -0700164 }
Florin Coras3e350af2017-03-30 02:54:28 -0700165 s->bytes_to_receive -= n_read;
166 s->bytes_received += n_read;
Florin Coras6792ec02017-03-13 03:49:51 -0700167 }
Florin Coras6792ec02017-03-13 03:49:51 -0700168}
169
Dave Barach10d8cc62017-05-30 09:30:07 -0400170static uword
171builtin_client_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
172 vlib_frame_t * frame)
Florin Coras6792ec02017-03-13 03:49:51 -0700173{
174 tclient_main_t *tm = &tclient_main;
Dave Barach10d8cc62017-05-30 09:30:07 -0400175 int my_thread_index = vlib_get_thread_index ();
Florin Coras6792ec02017-03-13 03:49:51 -0700176 session_t *sp;
Florin Coras6792ec02017-03-13 03:49:51 -0700177 int i;
Dave Barach10d8cc62017-05-30 09:30:07 -0400178 int delete_session;
179 u32 *connection_indices;
Dave Barach2c25a622017-06-26 11:35:07 -0400180 u32 *connections_this_batch;
181 u32 nconnections_this_batch;
Dave Barach0194f1a2017-05-15 10:11:39 -0400182
Dave Barach10d8cc62017-05-30 09:30:07 -0400183 connection_indices = tm->connection_index_by_thread[my_thread_index];
Dave Barach2c25a622017-06-26 11:35:07 -0400184 connections_this_batch =
185 tm->connections_this_batch_by_thread[my_thread_index];
Florin Coras6792ec02017-03-13 03:49:51 -0700186
Dave Barach2c25a622017-06-26 11:35:07 -0400187 if ((tm->run_test == 0) ||
188 ((vec_len (connection_indices) == 0)
189 && vec_len (connections_this_batch) == 0))
Dave Barach10d8cc62017-05-30 09:30:07 -0400190 return 0;
Florin Coras6792ec02017-03-13 03:49:51 -0700191
Dave Barach2c25a622017-06-26 11:35:07 -0400192 /* Grab another pile of connections */
193 if (PREDICT_FALSE (vec_len (connections_this_batch) == 0))
194 {
195 nconnections_this_batch =
196 clib_min (tm->connections_per_batch, vec_len (connection_indices));
197
198 ASSERT (nconnections_this_batch > 0);
199 vec_validate (connections_this_batch, nconnections_this_batch - 1);
200 clib_memcpy (connections_this_batch,
201 connection_indices + vec_len (connection_indices)
202 - nconnections_this_batch,
203 nconnections_this_batch * sizeof (u32));
204 _vec_len (connection_indices) -= nconnections_this_batch;
205 }
206
207 if (PREDICT_FALSE (tm->prev_conns != tm->connections_per_batch
208 && tm->prev_conns == vec_len (connections_this_batch)))
209 {
210 tm->repeats++;
211 tm->prev_conns = vec_len (connections_this_batch);
212 if (tm->repeats == 500000)
213 {
214 clib_warning ("stuck clients");
215 }
216 }
217 else
218 {
219 tm->prev_conns = vec_len (connections_this_batch);
220 tm->repeats = 0;
221 }
222
223 for (i = 0; i < vec_len (connections_this_batch); i++)
Florin Coras6792ec02017-03-13 03:49:51 -0700224 {
Dave Barach10d8cc62017-05-30 09:30:07 -0400225 delete_session = 1;
226
Dave Barach2c25a622017-06-26 11:35:07 -0400227 sp = pool_elt_at_index (tm->sessions, connections_this_batch[i]);
Dave Barach10d8cc62017-05-30 09:30:07 -0400228
Dave Barach2c25a622017-06-26 11:35:07 -0400229 if (sp->bytes_to_send > 0)
Florin Coras6792ec02017-03-13 03:49:51 -0700230 {
Dave Barach10d8cc62017-05-30 09:30:07 -0400231 send_test_chunk (tm, sp);
232 delete_session = 0;
Florin Coras6792ec02017-03-13 03:49:51 -0700233 }
Dave Barach2c25a622017-06-26 11:35:07 -0400234 if (sp->bytes_to_receive > 0)
Florin Coras6792ec02017-03-13 03:49:51 -0700235 {
Dave Barach10d8cc62017-05-30 09:30:07 -0400236 receive_test_chunk (tm, sp);
237 delete_session = 0;
238 }
239 if (PREDICT_FALSE (delete_session == 1))
240 {
Dave Barach2c25a622017-06-26 11:35:07 -0400241 u32 index, thread_index;
242 stream_session_t *s;
243
244 __sync_fetch_and_add (&tm->tx_total, sp->bytes_sent);
Dave Barach10d8cc62017-05-30 09:30:07 -0400245 __sync_fetch_and_add (&tm->rx_total, sp->bytes_received);
Florin Corasf03a59a2017-06-09 21:07:32 -0700246
Florin Corascea194d2017-10-02 00:18:51 -0700247 session_parse_handle (sp->vpp_session_handle,
248 &index, &thread_index);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700249 s = session_get_if_valid (index, thread_index);
Dave Barach2c25a622017-06-26 11:35:07 -0400250
251 if (s)
Florin Coras93992a92017-05-24 18:03:56 -0700252 {
Florin Coras68810622017-07-24 17:40:28 -0700253 vnet_disconnect_args_t _a, *a = &_a;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700254 a->handle = session_handle (s);
Florin Coras68810622017-07-24 17:40:28 -0700255 a->app_index = tm->app_index;
256 vnet_disconnect_session (a);
257
Dave Barach2c25a622017-06-26 11:35:07 -0400258 vec_delete (connections_this_batch, 1, i);
259 i--;
Florin Coras93992a92017-05-24 18:03:56 -0700260 __sync_fetch_and_add (&tm->ready_connections, -1);
261 }
262 else
Dave Barach2c25a622017-06-26 11:35:07 -0400263 clib_warning ("session AWOL?");
Dave Barach10d8cc62017-05-30 09:30:07 -0400264
265 /* Kick the debug CLI process */
266 if (tm->ready_connections == 0)
Florin Coras6792ec02017-03-13 03:49:51 -0700267 {
Florin Coras68810622017-07-24 17:40:28 -0700268 signal_evt_to_cli (2);
Florin Coras6792ec02017-03-13 03:49:51 -0700269 }
Florin Coras6792ec02017-03-13 03:49:51 -0700270 }
Florin Coras6792ec02017-03-13 03:49:51 -0700271 }
Dave Barach2c25a622017-06-26 11:35:07 -0400272
273 tm->connection_index_by_thread[my_thread_index] = connection_indices;
274 tm->connections_this_batch_by_thread[my_thread_index] =
275 connections_this_batch;
Florin Coras6792ec02017-03-13 03:49:51 -0700276 return 0;
277}
278
Dave Barach10d8cc62017-05-30 09:30:07 -0400279/* *INDENT-OFF* */
280VLIB_REGISTER_NODE (builtin_client_node) =
281{
282 .function = builtin_client_node_fn,
283 .name = "builtin-tcp-client",
284 .type = VLIB_NODE_TYPE_INPUT,
285 .state = VLIB_NODE_STATE_DISABLED,
286};
287/* *INDENT-ON* */
288
Florin Coras6cf30ad2017-04-04 23:08:23 -0700289static int
Florin Coras6792ec02017-03-13 03:49:51 -0700290create_api_loopback (tclient_main_t * tm)
291{
Florin Coras6792ec02017-03-13 03:49:51 -0700292 api_main_t *am = &api_main;
293 vl_shmem_hdr_t *shmem_hdr;
Florin Coras6792ec02017-03-13 03:49:51 -0700294
295 shmem_hdr = am->shmem_hdr;
296 tm->vl_input_queue = shmem_hdr->vl_input_queue;
Dave Barach52851e62017-08-07 09:35:25 -0400297 tm->my_client_index =
298 vl_api_memclnt_create_internal ("tcp_test_client", tm->vl_input_queue);
Florin Coras6792ec02017-03-13 03:49:51 -0700299 return 0;
300}
301
Florin Coras6cf30ad2017-04-04 23:08:23 -0700302static int
303tcp_test_clients_init (vlib_main_t * vm)
304{
305 tclient_main_t *tm = &tclient_main;
Florin Coras68810622017-07-24 17:40:28 -0700306 vlib_thread_main_t *vtm = vlib_get_thread_main ();
307 u32 num_threads;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700308 int i;
309
Florin Coras6cf30ad2017-04-04 23:08:23 -0700310 if (create_api_loopback (tm))
311 return -1;
312
Florin Coras68810622017-07-24 17:40:28 -0700313 num_threads = 1 /* main thread */ + vtm->n_threads;
314
Florin Corasf03a59a2017-06-09 21:07:32 -0700315 /* Init test data. Big buffer */
316 vec_validate (tm->connect_test_data, 1024 * 1024 - 1);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700317 for (i = 0; i < vec_len (tm->connect_test_data); i++)
318 tm->connect_test_data[i] = i & 0xff;
319
Florin Coras68810622017-07-24 17:40:28 -0700320 vec_validate (tm->rx_buf, num_threads - 1);
321 for (i = 0; i < num_threads; i++)
322 vec_validate (tm->rx_buf[i], vec_len (tm->connect_test_data) - 1);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700323
324 tm->is_init = 1;
Dave Barach10d8cc62017-05-30 09:30:07 -0400325
Florin Coras68810622017-07-24 17:40:28 -0700326 vec_validate (tm->connection_index_by_thread, vtm->n_vlib_mains);
327 vec_validate (tm->connections_this_batch_by_thread, vtm->n_vlib_mains);
328 vec_validate (tm->vpp_event_queue, vtm->n_vlib_mains);
329
Dave Barach10d8cc62017-05-30 09:30:07 -0400330 return 0;
331}
332
333static int
334builtin_session_connected_callback (u32 app_index, u32 api_context,
335 stream_session_t * s, u8 is_fail)
336{
Florin Corasf03a59a2017-06-09 21:07:32 -0700337 tclient_main_t *tm = &tclient_main;
338 session_t *session;
339 u32 session_index;
Florin Coras68810622017-07-24 17:40:28 -0700340 u8 thread_index = vlib_get_thread_index ();
341
Florin Corasf03a59a2017-06-09 21:07:32 -0700342 if (is_fail)
Dave Barach10d8cc62017-05-30 09:30:07 -0400343 {
Florin Corasf03a59a2017-06-09 21:07:32 -0700344 clib_warning ("connection %d failed!", api_context);
Florin Coras68810622017-07-24 17:40:28 -0700345 signal_evt_to_cli (-1);
346 return 0;
Dave Barach10d8cc62017-05-30 09:30:07 -0400347 }
348
Dave Barachac0c96b2017-08-02 09:19:32 -0400349 ASSERT (s->thread_index == thread_index);
350
Florin Coras68810622017-07-24 17:40:28 -0700351 if (!tm->vpp_event_queue[thread_index])
352 tm->vpp_event_queue[thread_index] =
353 session_manager_get_vpp_event_queue (thread_index);
Florin Corasf03a59a2017-06-09 21:07:32 -0700354
355 /*
356 * Setup session
357 */
Florin Coras68810622017-07-24 17:40:28 -0700358 clib_spinlock_lock_if_init (&tm->sessions_lock);
Florin Corasf03a59a2017-06-09 21:07:32 -0700359 pool_get (tm->sessions, session);
Florin Coras68810622017-07-24 17:40:28 -0700360 clib_spinlock_unlock_if_init (&tm->sessions_lock);
361
Florin Corasf03a59a2017-06-09 21:07:32 -0700362 memset (session, 0, sizeof (*session));
363 session_index = session - tm->sessions;
Dave Barach2c25a622017-06-26 11:35:07 -0400364 session->bytes_to_send = tm->bytes_to_send;
365 session->bytes_to_receive = tm->no_return ? 0ULL : tm->bytes_to_send;
Florin Corasf03a59a2017-06-09 21:07:32 -0700366 session->server_rx_fifo = s->server_rx_fifo;
367 session->server_rx_fifo->client_session_index = session_index;
368 session->server_tx_fifo = s->server_tx_fifo;
369 session->server_tx_fifo->client_session_index = session_index;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700370 session->vpp_session_handle = session_handle (s);
Florin Corasf03a59a2017-06-09 21:07:32 -0700371
Florin Coras68810622017-07-24 17:40:28 -0700372 vec_add1 (tm->connection_index_by_thread[thread_index], session_index);
Florin Corasf03a59a2017-06-09 21:07:32 -0700373 __sync_fetch_and_add (&tm->ready_connections, 1);
374 if (tm->ready_connections == tm->expected_connections)
375 {
376 tm->run_test = 1;
Florin Corasf03a59a2017-06-09 21:07:32 -0700377 /* Signal the CLI process that the action is starting... */
Florin Coras68810622017-07-24 17:40:28 -0700378 signal_evt_to_cli (1);
Florin Corasf03a59a2017-06-09 21:07:32 -0700379 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700380
381 return 0;
382}
383
384static void
385builtin_session_reset_callback (stream_session_t * s)
386{
Florin Coras3eb50622017-07-13 01:24:57 -0400387 if (s->session_state == SESSION_STATE_READY)
388 clib_warning ("Reset active connection %U", format_stream_session, s, 2);
Florin Coras6534b7a2017-07-18 05:38:03 -0400389 stream_session_cleanup (s);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700390 return;
391}
392
393static int
Florin Coras6cf30ad2017-04-04 23:08:23 -0700394builtin_session_create_callback (stream_session_t * s)
395{
396 return 0;
397}
398
399static void
400builtin_session_disconnect_callback (stream_session_t * s)
401{
Florin Coras6534b7a2017-07-18 05:38:03 -0400402 tclient_main_t *tm = &tclient_main;
403 vnet_disconnect_args_t _a, *a = &_a;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700404 a->handle = session_handle (s);
Florin Coras6534b7a2017-07-18 05:38:03 -0400405 a->app_index = tm->app_index;
406 vnet_disconnect_session (a);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700407 return;
408}
409
410static int
411builtin_server_rx_callback (stream_session_t * s)
412{
413 return 0;
414}
415
416/* *INDENT-OFF* */
Florin Corasf03a59a2017-06-09 21:07:32 -0700417static session_cb_vft_t builtin_clients = {
418 .session_reset_callback = builtin_session_reset_callback,
419 .session_connected_callback = builtin_session_connected_callback,
420 .session_accept_callback = builtin_session_create_callback,
421 .session_disconnect_callback = builtin_session_disconnect_callback,
422 .builtin_server_rx_callback = builtin_server_rx_callback
423};
Florin Coras6cf30ad2017-04-04 23:08:23 -0700424/* *INDENT-ON* */
425
Florin Corascea194d2017-10-02 00:18:51 -0700426static clib_error_t *
427attach_builtin_test_clients_app (u8 * appns_id, u64 appns_flags,
428 u64 appns_secret)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700429{
Dave Barach0194f1a2017-05-15 10:11:39 -0400430 tclient_main_t *tm = &tclient_main;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700431 vnet_app_attach_args_t _a, *a = &_a;
432 u8 segment_name[128];
Florin Corasf03a59a2017-06-09 21:07:32 -0700433 u32 segment_name_length, prealloc_fifos;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700434 u64 options[16];
Florin Corascea194d2017-10-02 00:18:51 -0700435 clib_error_t *error = 0;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700436
437 segment_name_length = ARRAY_LEN (segment_name);
438
439 memset (a, 0, sizeof (*a));
440 memset (options, 0, sizeof (options));
441
Dave Barach0194f1a2017-05-15 10:11:39 -0400442 a->api_client_index = tm->my_client_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700443 a->segment_name = segment_name;
444 a->segment_name_length = segment_name_length;
445 a->session_cb_vft = &builtin_clients;
446
Florin Corasf03a59a2017-06-09 21:07:32 -0700447 prealloc_fifos = tm->prealloc_fifos ? tm->expected_connections : 1;
448
Florin Coras6cf30ad2017-04-04 23:08:23 -0700449 options[SESSION_OPTIONS_ACCEPT_COOKIE] = 0x12345678;
Florin Corasf03a59a2017-06-09 21:07:32 -0700450 options[SESSION_OPTIONS_SEGMENT_SIZE] = (2ULL << 32);
451 options[SESSION_OPTIONS_RX_FIFO_SIZE] = tm->fifo_size;
Florin Coras6534b7a2017-07-18 05:38:03 -0400452 options[SESSION_OPTIONS_TX_FIFO_SIZE] = tm->fifo_size;
Dave Barach2c25a622017-06-26 11:35:07 -0400453 options[APP_OPTIONS_PRIVATE_SEGMENT_COUNT] = tm->private_segment_count;
454 options[APP_OPTIONS_PRIVATE_SEGMENT_SIZE] = tm->private_segment_size;
Florin Corasf03a59a2017-06-09 21:07:32 -0700455 options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = prealloc_fifos;
456
Florin Corasa5464812017-04-19 13:00:05 -0700457 options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_BUILTIN_APP;
Florin Corascea194d2017-10-02 00:18:51 -0700458 if (appns_id)
459 {
460 options[APP_OPTIONS_FLAGS] |= appns_flags;
461 options[APP_OPTIONS_NAMESPACE_SECRET] = appns_secret;
462 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700463 a->options = options;
Florin Corascea194d2017-10-02 00:18:51 -0700464 a->namespace_id = appns_id;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700465
Florin Corascea194d2017-10-02 00:18:51 -0700466 if ((error = vnet_application_attach (a)))
467 return error;
Florin Corasf03a59a2017-06-09 21:07:32 -0700468
469 tm->app_index = a->app_index;
470 return 0;
471}
472
473static void *
474tclient_thread_fn (void *arg)
475{
476 return 0;
477}
478
479/** Start a transmit thread */
480int
481start_tx_pthread (tclient_main_t * tm)
482{
483 if (tm->client_thread_handle == 0)
484 {
485 int rv = pthread_create (&tm->client_thread_handle,
486 NULL /*attr */ ,
487 tclient_thread_fn, 0);
488 if (rv)
489 {
490 tm->client_thread_handle = 0;
491 return -1;
492 }
493 }
494 return 0;
495}
496
Florin Corascea194d2017-10-02 00:18:51 -0700497clib_error_t *
Florin Corasf03a59a2017-06-09 21:07:32 -0700498clients_connect (vlib_main_t * vm, u8 * uri, u32 n_clients)
499{
500 tclient_main_t *tm = &tclient_main;
501 vnet_connect_args_t _a, *a = &_a;
Florin Corascea194d2017-10-02 00:18:51 -0700502 clib_error_t *error = 0;
Florin Corasf03a59a2017-06-09 21:07:32 -0700503 int i;
504 for (i = 0; i < n_clients; i++)
505 {
506 memset (a, 0, sizeof (*a));
507
508 a->uri = (char *) uri;
509 a->api_context = i;
510 a->app_index = tm->app_index;
511 a->mp = 0;
Florin Corascea194d2017-10-02 00:18:51 -0700512
513 if ((error = vnet_connect_uri (a)))
514 return error;
515
Florin Corasf03a59a2017-06-09 21:07:32 -0700516
Florin Coras66b11312017-07-31 17:18:03 -0700517 /* Crude pacing for call setups */
518 if ((i % 4) == 0)
519 vlib_process_suspend (vm, 10e-6);
Florin Coras4eeeaaf2017-09-05 14:03:37 -0400520 ASSERT (i + 1 >= tm->ready_connections);
Florin Coras9d063042017-09-14 03:08:00 -0400521 while (i + 1 - tm->ready_connections > 1000)
Florin Coras4eeeaaf2017-09-05 14:03:37 -0400522 {
523 vlib_process_suspend (vm, 100e-6);
524 }
Florin Corasf03a59a2017-06-09 21:07:32 -0700525 }
Florin Corascea194d2017-10-02 00:18:51 -0700526 return 0;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700527}
Florin Coras6792ec02017-03-13 03:49:51 -0700528
529static clib_error_t *
530test_tcp_clients_command_fn (vlib_main_t * vm,
531 unformat_input_t * input,
532 vlib_cli_command_t * cmd)
533{
Florin Coras6cf30ad2017-04-04 23:08:23 -0700534 tclient_main_t *tm = &tclient_main;
Dave Barach10d8cc62017-05-30 09:30:07 -0400535 vlib_thread_main_t *thread_main = vlib_get_thread_main ();
Florin Corasf03a59a2017-06-09 21:07:32 -0700536 uword *event_data = 0, event_type;
Florin Corascea194d2017-10-02 00:18:51 -0700537 u8 *default_connect_uri = (u8 *) "tcp://6.0.1.1/1234", *uri, *appns_id;
538 u64 tmp, total_bytes, appns_flags = 0, appns_secret = 0;
Florin Coras66b11312017-07-31 17:18:03 -0700539 f64 test_timeout = 20.0, syn_timeout = 20.0, delta;
540 f64 time_before_connects;
Florin Coras6792ec02017-03-13 03:49:51 -0700541 u32 n_clients = 1;
Florin Coras66b11312017-07-31 17:18:03 -0700542 int preallocate_sessions = 0;
Florin Corasf03a59a2017-06-09 21:07:32 -0700543 char *transfer_type;
Florin Corascea194d2017-10-02 00:18:51 -0700544 clib_error_t *error = 0;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700545 int i;
Florin Coras6792ec02017-03-13 03:49:51 -0700546
547 tm->bytes_to_send = 8192;
Florin Corasf03a59a2017-06-09 21:07:32 -0700548 tm->no_return = 0;
549 tm->fifo_size = 64 << 10;
Dave Barach2c25a622017-06-26 11:35:07 -0400550 tm->connections_per_batch = 1000;
551 tm->private_segment_count = 0;
552 tm->private_segment_size = 0;
Florin Coras68810622017-07-24 17:40:28 -0700553 tm->vlib_main = vm;
554 if (thread_main->n_vlib_mains > 1)
555 clib_spinlock_init (&tm->sessions_lock);
Florin Coras6792ec02017-03-13 03:49:51 -0700556 vec_free (tm->connect_uri);
557
558 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
559 {
560 if (unformat (input, "nclients %d", &n_clients))
561 ;
Dave Barach0194f1a2017-05-15 10:11:39 -0400562 else if (unformat (input, "mbytes %lld", &tmp))
563 tm->bytes_to_send = tmp << 20;
564 else if (unformat (input, "gbytes %lld", &tmp))
565 tm->bytes_to_send = tmp << 30;
Florin Coras3e350af2017-03-30 02:54:28 -0700566 else if (unformat (input, "bytes %lld", &tm->bytes_to_send))
Florin Coras6792ec02017-03-13 03:49:51 -0700567 ;
568 else if (unformat (input, "uri %s", &tm->connect_uri))
569 ;
Florin Coras66b11312017-07-31 17:18:03 -0700570 else if (unformat (input, "test-timeout %f", &test_timeout))
571 ;
572 else if (unformat (input, "syn-timeout %f", &syn_timeout))
Dave Barach10d8cc62017-05-30 09:30:07 -0400573 ;
Florin Corasf03a59a2017-06-09 21:07:32 -0700574 else if (unformat (input, "no-return"))
575 tm->no_return = 1;
576 else if (unformat (input, "fifo-size %d", &tm->fifo_size))
577 tm->fifo_size <<= 10;
Dave Barach2c25a622017-06-26 11:35:07 -0400578 else if (unformat (input, "private-segment-count %d",
579 &tm->private_segment_count))
580 ;
Dave Barach91f3e742017-09-01 19:12:11 -0400581 else if (unformat (input, "private-segment-size %U",
582 unformat_memory_size, &tmp))
583 {
584 if (tmp >= 0x100000000ULL)
585 return clib_error_return
586 (0, "private segment size %lld (%llu) too large", tmp, tmp);
587 tm->private_segment_size = tmp;
588 }
Dave Barach2c25a622017-06-26 11:35:07 -0400589 else if (unformat (input, "preallocate-fifos"))
590 tm->prealloc_fifos = 1;
Florin Coras66b11312017-07-31 17:18:03 -0700591 else if (unformat (input, "preallocate-sessions"))
592 preallocate_sessions = 1;
Dave Barach2c25a622017-06-26 11:35:07 -0400593 else
594 if (unformat (input, "client-batch %d", &tm->connections_per_batch))
595 ;
Florin Corascea194d2017-10-02 00:18:51 -0700596 else if (unformat (input, "appns %_%v%_", &appns_id))
597 ;
598 else if (unformat (input, "all-scope"))
599 appns_flags |= (APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE
600 | APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE);
601 else if (unformat (input, "local-scope"))
602 appns_flags = APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE;
603 else if (unformat (input, "global-scope"))
604 appns_flags = APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
605 else if (unformat (input, "secret %lu", &appns_secret))
606 ;
Florin Coras6792ec02017-03-13 03:49:51 -0700607 else
608 return clib_error_return (0, "unknown input `%U'",
609 format_unformat_error, input);
610 }
611
Florin Corasf03a59a2017-06-09 21:07:32 -0700612 /* Store cli process node index for signalling */
613 tm->cli_node_index = vlib_get_current_process (vm)->node_runtime.node_index;
614
Florin Coras6cf30ad2017-04-04 23:08:23 -0700615 if (tm->is_init == 0)
616 {
617 if (tcp_test_clients_init (vm))
618 return clib_error_return (0, "failed init");
619 }
620
Florin Coras66b11312017-07-31 17:18:03 -0700621
Florin Coras6792ec02017-03-13 03:49:51 -0700622 tm->ready_connections = 0;
623 tm->expected_connections = n_clients;
Dave Barach10d8cc62017-05-30 09:30:07 -0400624 tm->rx_total = 0;
Florin Corasf03a59a2017-06-09 21:07:32 -0700625 tm->tx_total = 0;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700626
Florin Corasf03a59a2017-06-09 21:07:32 -0700627 uri = default_connect_uri;
Florin Coras6792ec02017-03-13 03:49:51 -0700628 if (tm->connect_uri)
629 uri = tm->connect_uri;
630
Florin Coras3e350af2017-03-30 02:54:28 -0700631#if TCP_BUILTIN_CLIENT_PTHREAD
Florin Corasf03a59a2017-06-09 21:07:32 -0700632 start_tx_pthread ();
633#endif
634
Florin Coras68810622017-07-24 17:40:28 -0700635 vlib_worker_thread_barrier_sync (vm);
Florin Corasf03a59a2017-06-09 21:07:32 -0700636 vnet_session_enable_disable (vm, 1 /* turn on TCP, etc. */ );
Florin Coras68810622017-07-24 17:40:28 -0700637 vlib_worker_thread_barrier_release (vm);
Florin Corasf03a59a2017-06-09 21:07:32 -0700638
639 if (tm->test_client_attached == 0)
Florin Coras6792ec02017-03-13 03:49:51 -0700640 {
Florin Corascea194d2017-10-02 00:18:51 -0700641 if ((error = attach_builtin_test_clients_app (appns_id, appns_flags,
642 appns_secret)))
Florin Coras6792ec02017-03-13 03:49:51 -0700643 {
Florin Corascea194d2017-10-02 00:18:51 -0700644 vec_free (appns_id);
645 clib_error_report (error);
646 return error;
Florin Coras6792ec02017-03-13 03:49:51 -0700647 }
Florin Corascea194d2017-10-02 00:18:51 -0700648 vec_free (appns_id);
Florin Coras6792ec02017-03-13 03:49:51 -0700649 }
Dave Barach10d8cc62017-05-30 09:30:07 -0400650 tm->test_client_attached = 1;
Florin Coras6792ec02017-03-13 03:49:51 -0700651
Dave Barach10d8cc62017-05-30 09:30:07 -0400652 /* Turn on the builtin client input nodes */
653 for (i = 0; i < thread_main->n_vlib_mains; i++)
654 vlib_node_set_state (vlib_mains[i], builtin_client_node.index,
655 VLIB_NODE_STATE_POLLING);
656
Florin Coras66b11312017-07-31 17:18:03 -0700657 if (preallocate_sessions)
658 {
659 session_t *sp __attribute__ ((unused));
660 for (i = 0; i < n_clients; i++)
661 pool_get (tm->sessions, sp);
662 for (i = 0; i < n_clients; i++)
663 pool_put_index (tm->sessions, i);
664 }
665
Dave Barach10d8cc62017-05-30 09:30:07 -0400666 /* Fire off connect requests */
Florin Coras66b11312017-07-31 17:18:03 -0700667 time_before_connects = vlib_time_now (vm);
Florin Corascea194d2017-10-02 00:18:51 -0700668 if ((error = clients_connect (vm, uri, n_clients)))
669 return error;
Florin Coras6792ec02017-03-13 03:49:51 -0700670
Dave Barach10d8cc62017-05-30 09:30:07 -0400671 /* Park until the sessions come up, or ten seconds elapse... */
Florin Coras66b11312017-07-31 17:18:03 -0700672 vlib_process_wait_for_event_or_clock (vm, syn_timeout);
Dave Barach10d8cc62017-05-30 09:30:07 -0400673 event_type = vlib_process_get_events (vm, &event_data);
Dave Barach10d8cc62017-05-30 09:30:07 -0400674 switch (event_type)
675 {
676 case ~0:
677 vlib_cli_output (vm, "Timeout with only %d sessions active...",
678 tm->ready_connections);
679 goto cleanup;
680
681 case 1:
Florin Coras66b11312017-07-31 17:18:03 -0700682 delta = vlib_time_now (vm) - time_before_connects;
683
684 if (delta != 0.0)
685 {
686 vlib_cli_output
687 (vm, "%d three-way handshakes in %.2f seconds, %.2f/sec",
688 n_clients, delta, ((f64) n_clients) / delta);
689 }
690
Florin Coras68810622017-07-24 17:40:28 -0700691 tm->test_start_time = vlib_time_now (tm->vlib_main);
Dave Barach10d8cc62017-05-30 09:30:07 -0400692 vlib_cli_output (vm, "Test started at %.6f", tm->test_start_time);
693 break;
694
695 default:
696 vlib_cli_output (vm, "unexpected event(1): %d", event_type);
697 goto cleanup;
698 }
699
700 /* Now wait for the sessions to finish... */
Florin Coras66b11312017-07-31 17:18:03 -0700701 vlib_process_wait_for_event_or_clock (vm, test_timeout);
Dave Barach10d8cc62017-05-30 09:30:07 -0400702 event_type = vlib_process_get_events (vm, &event_data);
Dave Barach10d8cc62017-05-30 09:30:07 -0400703 switch (event_type)
704 {
705 case ~0:
706 vlib_cli_output (vm, "Timeout with %d sessions still active...",
707 tm->ready_connections);
708 goto cleanup;
709
710 case 2:
Florin Coras68810622017-07-24 17:40:28 -0700711 tm->test_end_time = vlib_time_now (vm);
Dave Barach10d8cc62017-05-30 09:30:07 -0400712 vlib_cli_output (vm, "Test finished at %.6f", tm->test_end_time);
713 break;
714
715 default:
716 vlib_cli_output (vm, "unexpected event(2): %d", event_type);
717 goto cleanup;
718 }
719
720 delta = tm->test_end_time - tm->test_start_time;
721
722 if (delta != 0.0)
723 {
Florin Corasf03a59a2017-06-09 21:07:32 -0700724 total_bytes = (tm->no_return ? tm->tx_total : tm->rx_total);
725 transfer_type = tm->no_return ? "half-duplex" : "full-duplex";
Dave Barach10d8cc62017-05-30 09:30:07 -0400726 vlib_cli_output (vm,
727 "%lld bytes (%lld mbytes, %lld gbytes) in %.2f seconds",
Florin Corasf03a59a2017-06-09 21:07:32 -0700728 total_bytes, total_bytes / (1ULL << 20),
729 total_bytes / (1ULL << 30), delta);
730 vlib_cli_output (vm, "%.2f bytes/second %s",
731 ((f64) total_bytes) / (delta), transfer_type);
732 vlib_cli_output (vm, "%.4f gbit/second %s",
733 (((f64) total_bytes * 8.0) / delta / 1e9),
734 transfer_type);
Dave Barach10d8cc62017-05-30 09:30:07 -0400735 }
736 else
737 vlib_cli_output (vm, "zero delta-t?");
738
739cleanup:
Dave Barach2c25a622017-06-26 11:35:07 -0400740 tm->run_test = 0;
Dave Barach10d8cc62017-05-30 09:30:07 -0400741 for (i = 0; i < vec_len (tm->connection_index_by_thread); i++)
Dave Barach2c25a622017-06-26 11:35:07 -0400742 {
743 vec_reset_length (tm->connection_index_by_thread[i]);
744 vec_reset_length (tm->connections_this_batch_by_thread[i]);
745 }
Florin Coras68810622017-07-24 17:40:28 -0700746
Dave Barach2c25a622017-06-26 11:35:07 -0400747 pool_free (tm->sessions);
Florin Coras6792ec02017-03-13 03:49:51 -0700748
Dave Barach818eb542017-08-02 13:56:13 -0400749 /* Detach the application, so we can use different fifo sizes next time */
750 if (tm->test_client_attached)
751 {
752 vnet_app_detach_args_t _da, *da = &_da;
753 int rv;
754
755 da->app_index = tm->app_index;
756
757 rv = vnet_application_detach (da);
758 if (rv)
759 vlib_cli_output (vm, "WARNING: app detach failed...");
760 tm->test_client_attached = 0;
761 tm->app_index = ~0;
762 }
Florin Coras6792ec02017-03-13 03:49:51 -0700763 return 0;
764}
765
Florin Coras3e350af2017-03-30 02:54:28 -0700766/* *INDENT-OFF* */
Florin Coras6792ec02017-03-13 03:49:51 -0700767VLIB_CLI_COMMAND (test_clients_command, static) =
768{
769 .path = "test tcp clients",
Florin Coras2245c0f2017-08-14 08:45:15 -0700770 .short_help = "test tcp clients [nclients %d] [[m|g]bytes <bytes>] "
771 "[test-timeout <time>][syn-timeout <time>][no-return][fifo-size <size>]"
772 "[private-segment-count <count>][private-segment-size <bytes>[m|g]]"
773 "[preallocate-fifos][preallocate-sessions][client-batch <batch-size>]"
774 "[uri <tcp://ip/port>]",
Florin Coras6792ec02017-03-13 03:49:51 -0700775 .function = test_tcp_clients_command_fn,
Florin Coras68810622017-07-24 17:40:28 -0700776 .is_mp_safe = 1,
Florin Coras6792ec02017-03-13 03:49:51 -0700777};
778/* *INDENT-ON* */
779
Florin Coras6cf30ad2017-04-04 23:08:23 -0700780clib_error_t *
781tcp_test_clients_main_init (vlib_main_t * vm)
782{
783 tclient_main_t *tm = &tclient_main;
784 tm->is_init = 0;
785 return 0;
786}
787
788VLIB_INIT_FUNCTION (tcp_test_clients_main_init);
789
Florin Coras6792ec02017-03-13 03:49:51 -0700790/*
791 * fd.io coding-style-patch-verification: ON
792 *
793 * Local Variables:
794 * eval: (c-set-style "gnu")
795 * End:
796 */