blob: e37050604002e5b90b7e6e8b0c92cfcfaa5486fe [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
27/* define message IDs */
28#include <vpp/api/vpe_msg_enum.h>
29
30/* define message structures */
31#define vl_typedefs
32#include <vpp/api/vpe_all_api_h.h>
33#undef vl_typedefs
34
35/* define generated endian-swappers */
36#define vl_endianfun
37#include <vpp/api/vpe_all_api_h.h>
38#undef vl_endianfun
39
40/* instantiate all the print functions we know about */
41#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
42#define vl_printfun
43#include <vpp/api/vpe_all_api_h.h>
44#undef vl_printfun
45
Florin Coras3e350af2017-03-30 02:54:28 -070046#define TCP_BUILTIN_CLIENT_DBG (1)
47#define TCP_BUILTIN_CLIENT_VPP_THREAD (0)
48#define TCP_BUILTIN_CLIENT_PTHREAD (!TCP_BUILTIN_CLIENT_VPP_THREAD)
49
Florin Coras6792ec02017-03-13 03:49:51 -070050static void
51send_test_chunk (tclient_main_t * tm, session_t * s)
52{
53 u8 *test_data = tm->connect_test_data;
Dave Barach45ce3fb2017-03-28 12:31:33 -040054 int test_buf_offset;
Florin Coras6792ec02017-03-13 03:49:51 -070055 u32 bytes_this_chunk;
56 session_fifo_event_t evt;
57 static int serial_number = 0;
58 int rv;
Florin Coras3e350af2017-03-30 02:54:28 -070059 test_buf_offset = s->bytes_sent % vec_len (test_data);
60 bytes_this_chunk = vec_len (test_data) - test_buf_offset;
Florin Coras6792ec02017-03-13 03:49:51 -070061
Florin Coras3e350af2017-03-30 02:54:28 -070062 bytes_this_chunk = bytes_this_chunk < s->bytes_to_send
63 ? bytes_this_chunk : s->bytes_to_send;
64
65 rv = svm_fifo_enqueue_nowait (s->server_tx_fifo, 0 /*pid */ ,
66 bytes_this_chunk,
67 test_data + test_buf_offset);
68
69 /* If we managed to enqueue data... */
70 if (rv > 0)
Florin Coras6792ec02017-03-13 03:49:51 -070071 {
Florin Coras3e350af2017-03-30 02:54:28 -070072 if (TCP_BUILTIN_CLIENT_DBG)
Florin Coras6792ec02017-03-13 03:49:51 -070073 {
Florin Coras3e350af2017-03-30 02:54:28 -070074 /* *INDENT-OFF* */
75 ELOG_TYPE_DECLARE (e) =
76 {
77 .format = "tx-enq: %d bytes",
78 .format_args = "i4",
79 };
80 /* *INDENT-ON* */
81 struct
82 {
83 u32 data[1];
84 } *ed;
85 ed = ELOG_DATA (&vlib_global_main.elog_main, e);
86 ed->data[0] = rv;
87 }
Florin Coras6792ec02017-03-13 03:49:51 -070088
Florin Coras3e350af2017-03-30 02:54:28 -070089 /* Account for it... */
90 s->bytes_to_send -= rv;
91 s->bytes_sent += rv;
Florin Coras6792ec02017-03-13 03:49:51 -070092
Florin Coras3e350af2017-03-30 02:54:28 -070093 /* Poke the TCP state machine */
94 if (svm_fifo_set_event (s->server_tx_fifo))
95 {
96 /* Fabricate TX event, send to vpp */
97 evt.fifo = s->server_tx_fifo;
98 evt.event_type = FIFO_EVENT_SERVER_TX;
99 evt.event_id = serial_number++;
100
101 unix_shared_memory_queue_add (tm->vpp_event_queue, (u8 *) & evt,
102 0 /* do wait for mutex */ );
Florin Coras6792ec02017-03-13 03:49:51 -0700103 }
104 }
105}
106
107static void
108receive_test_chunk (tclient_main_t * tm, session_t * s)
109{
110 svm_fifo_t *rx_fifo = s->server_rx_fifo;
Florin Coras3e350af2017-03-30 02:54:28 -0700111 int n_read, test_bytes = 0;
Florin Coras6792ec02017-03-13 03:49:51 -0700112
Florin Coras6792ec02017-03-13 03:49:51 -0700113 /* Allow enqueuing of new event */
Florin Coras3e350af2017-03-30 02:54:28 -0700114 // svm_fifo_unset_event (rx_fifo);
Florin Coras6792ec02017-03-13 03:49:51 -0700115
Florin Coras3e350af2017-03-30 02:54:28 -0700116 n_read = svm_fifo_dequeue_nowait (rx_fifo, 0, vec_len (tm->rx_buf),
117 tm->rx_buf);
118 if (n_read > 0)
Florin Coras6792ec02017-03-13 03:49:51 -0700119 {
Florin Coras3e350af2017-03-30 02:54:28 -0700120 if (TCP_BUILTIN_CLIENT_DBG)
Florin Coras6792ec02017-03-13 03:49:51 -0700121 {
Florin Coras3e350af2017-03-30 02:54:28 -0700122 /* *INDENT-OFF* */
123 ELOG_TYPE_DECLARE (e) =
124 {
125 .format = "rx-deq: %d bytes",
126 .format_args = "i4",
127 };
128 /* *INDENT-ON* */
129 struct
130 {
131 u32 data[1];
132 } *ed;
133 ed = ELOG_DATA (&vlib_global_main.elog_main, e);
134 ed->data[0] = n_read;
135 }
136
137 if (test_bytes)
138 {
139 int i;
Florin Coras6792ec02017-03-13 03:49:51 -0700140 for (i = 0; i < n_read; i++)
141 {
142 if (tm->rx_buf[i] != ((s->bytes_received + i) & 0xff))
143 {
144 clib_warning ("read %d error at byte %lld, 0x%x not 0x%x",
Florin Coras3e350af2017-03-30 02:54:28 -0700145 n_read, s->bytes_received + i, tm->rx_buf[i],
Florin Coras6792ec02017-03-13 03:49:51 -0700146 ((s->bytes_received + i) & 0xff));
147 }
148 }
Florin Coras6792ec02017-03-13 03:49:51 -0700149 }
Florin Coras3e350af2017-03-30 02:54:28 -0700150 s->bytes_to_receive -= n_read;
151 s->bytes_received += n_read;
Florin Coras6792ec02017-03-13 03:49:51 -0700152 }
Florin Coras6792ec02017-03-13 03:49:51 -0700153}
154
Florin Coras3e350af2017-03-30 02:54:28 -0700155#if TCP_BUILTIN_CLIENT_VPP_THREAD
156static void
157#else
Florin Coras6792ec02017-03-13 03:49:51 -0700158static void *
Florin Coras3e350af2017-03-30 02:54:28 -0700159#endif
Florin Coras6792ec02017-03-13 03:49:51 -0700160tclient_thread_fn (void *arg)
161{
162 tclient_main_t *tm = &tclient_main;
163 vl_api_disconnect_session_t *dmp;
164 session_t *sp;
165 struct timespec ts, tsrem;
166 int i;
167 int try_tx, try_rx;
168 u32 *session_indices = 0;
169
170 /* stats thread wants no signals. */
171 {
172 sigset_t s;
173 sigfillset (&s);
174 pthread_sigmask (SIG_SETMASK, &s, 0);
175 }
176
Florin Coras3e350af2017-03-30 02:54:28 -0700177 clib_per_cpu_mheaps[os_get_cpu_number ()] = clib_per_cpu_mheaps[0];
178
Florin Coras6792ec02017-03-13 03:49:51 -0700179 while (1)
180 {
181 /* Wait until we're told to get busy */
182 while (tm->run_test == 0
183 || (tm->ready_connections != tm->expected_connections))
184 {
185 ts.tv_sec = 0;
186 ts.tv_nsec = 100000000;
187 while (nanosleep (&ts, &tsrem) < 0)
188 ts = tsrem;
189 }
190 tm->run_test = 0;
191
192 clib_warning ("Run %d iterations", tm->n_iterations);
193
194 for (i = 0; i < tm->n_iterations; i++)
195 {
196 session_t *sp;
197
198 do
199 {
200 try_tx = try_rx = 0;
201
202 /* *INDENT-OFF* */
203 pool_foreach (sp, tm->sessions, ({
204 if (sp->bytes_to_send > 0)
205 {
206 send_test_chunk (tm, sp);
207 try_tx = 1;
208 }
209 }));
210 pool_foreach (sp, tm->sessions, ({
211 if (sp->bytes_to_receive > 0)
212 {
213 receive_test_chunk (tm, sp);
214 try_rx = 1;
215 }
216 }));
217 /* *INDENT-ON* */
218
219 }
220 while (try_tx || try_rx);
221 }
222 clib_warning ("Done %d iterations", tm->n_iterations);
223
224 /* Disconnect sessions... */
225 vec_reset_length (session_indices);
Florin Coras3e350af2017-03-30 02:54:28 -0700226
227 /* *INDENT-OFF* */
228 pool_foreach (sp, tm->sessions, ({
229 vec_add1 (session_indices, sp - tm->sessions);
230 }));
231 /* *INDENT-ON* */
Florin Coras6792ec02017-03-13 03:49:51 -0700232
233 for (i = 0; i < vec_len (session_indices); i++)
234 {
235 sp = pool_elt_at_index (tm->sessions, session_indices[i]);
236 dmp = vl_msg_api_alloc_as_if_client (sizeof (*dmp));
237 memset (dmp, 0, sizeof (*dmp));
238 dmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION);
239 dmp->client_index = tm->my_client_index;
240 dmp->session_index = sp->vpp_session_index;
241 dmp->session_thread_index = sp->vpp_session_thread;
242 vl_msg_api_send_shmem (tm->vl_input_queue, (u8 *) & dmp);
243 pool_put (tm->sessions, sp);
244 }
245 }
246 /* NOTREACHED */
Florin Coras3e350af2017-03-30 02:54:28 -0700247#if TCP_BUILTIN_CLIENT_PTHREAD
Florin Coras6792ec02017-03-13 03:49:51 -0700248 return 0;
Florin Coras3e350af2017-03-30 02:54:28 -0700249#endif
Florin Coras6792ec02017-03-13 03:49:51 -0700250}
251
252/* So we don't get "no handler for... " msgs */
253static void
254vl_api_memclnt_create_reply_t_handler (vl_api_memclnt_create_reply_t * mp)
255{
256 tclient_main_t *tm = &tclient_main;
257
258 tm->my_client_index = mp->index;
259}
260
261static void
262vl_api_connect_uri_reply_t_handler (vl_api_connect_uri_reply_t * mp)
263{
264 tclient_main_t *tm = &tclient_main;
265 session_t *session;
266 u32 session_index;
267 u64 key;
268 i32 retval = /* clib_net_to_host_u32 ( */ mp->retval /*) */ ;
269
270 if (retval < 0)
271 {
272 clib_warning ("connection failed: retval %d", retval);
273 return;
274 }
275
276 tm->our_event_queue = (unix_shared_memory_queue_t *)
277 mp->vpp_event_queue_address;
278
279 tm->vpp_event_queue = (unix_shared_memory_queue_t *)
280 mp->vpp_event_queue_address;
281
282 /*
283 * Setup session
284 */
285 pool_get (tm->sessions, session);
286 memset (session, 0, sizeof (*session));
287 session_index = session - tm->sessions;
288 session->bytes_to_receive = session->bytes_to_send = tm->bytes_to_send;
289
290 session->server_rx_fifo = (svm_fifo_t *) mp->server_rx_fifo;
291 session->server_rx_fifo->client_session_index = session_index;
292 session->server_tx_fifo = (svm_fifo_t *) mp->server_tx_fifo;
293 session->server_tx_fifo->client_session_index = session_index;
294
295 session->vpp_session_index = mp->session_index;
296 session->vpp_session_thread = mp->session_thread_index;
297
298 /* Add it to the session lookup table */
299 key = (((u64) mp->session_thread_index) << 32) | (u64) mp->session_index;
300 hash_set (tm->session_index_by_vpp_handles, key, session_index);
301
302 tm->ready_connections++;
303}
304
305static void
306create_api_loopback (tclient_main_t * tm)
307{
308 vl_api_memclnt_create_t _m, *mp = &_m;
309 extern void vl_api_memclnt_create_t_handler (vl_api_memclnt_create_t *);
310 api_main_t *am = &api_main;
311 vl_shmem_hdr_t *shmem_hdr;
312
313 /*
314 * Create a "loopback" API client connection
315 * Don't do things like this unless you know what you're doing...
316 */
317
318 shmem_hdr = am->shmem_hdr;
319 tm->vl_input_queue = shmem_hdr->vl_input_queue;
320 memset (mp, 0, sizeof (*mp));
321 mp->_vl_msg_id = VL_API_MEMCLNT_CREATE;
322 mp->context = 0xFEEDFACE;
323 mp->input_queue = (u64) tm->vl_input_queue;
324 strncpy ((char *) mp->name, "tcp_tester", sizeof (mp->name) - 1);
325
326 vl_api_memclnt_create_t_handler (mp);
327}
328
329#define foreach_tclient_static_api_msg \
330_(MEMCLNT_CREATE_REPLY, memclnt_create_reply) \
331_(CONNECT_URI_REPLY, connect_uri_reply)
332
333static clib_error_t *
334tclient_api_hookup (vlib_main_t * vm)
335{
336 tclient_main_t *tm = &tclient_main;
337 vl_msg_api_msg_config_t _c, *c = &_c;
338 int i;
339
340 /* Init test data */
341 vec_validate (tm->connect_test_data, 64 * 1024 - 1);
342 for (i = 0; i < vec_len (tm->connect_test_data); i++)
343 tm->connect_test_data[i] = i & 0xff;
344
345 tm->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
346 vec_validate (tm->rx_buf, vec_len (tm->connect_test_data) - 1);
347
348 /* Hook up client-side static APIs to our handlers */
349#define _(N,n) do { \
350 c->id = VL_API_##N; \
351 c->name = #n; \
352 c->handler = vl_api_##n##_t_handler; \
353 c->cleanup = vl_noop_handler; \
354 c->endian = vl_api_##n##_t_endian; \
355 c->print = vl_api_##n##_t_print; \
356 c->size = sizeof(vl_api_##n##_t); \
357 c->traced = 1; /* trace, so these msgs print */ \
358 c->replay = 0; /* don't replay client create/delete msgs */ \
359 c->message_bounce = 0; /* don't bounce this message */ \
360 vl_msg_api_config(c);} while (0);
361
362 foreach_tclient_static_api_msg;
363#undef _
364
365 return 0;
366}
367
368VLIB_API_INIT_FUNCTION (tclient_api_hookup);
369
370static clib_error_t *
371test_tcp_clients_command_fn (vlib_main_t * vm,
372 unformat_input_t * input,
373 vlib_cli_command_t * cmd)
374{
Florin Coras3e350af2017-03-30 02:54:28 -0700375 u8 *connect_uri = (u8 *) "tcp://6.0.1.1/1234";
Florin Coras6792ec02017-03-13 03:49:51 -0700376 u8 *uri;
377 tclient_main_t *tm = &tclient_main;
378 int i;
379 u32 n_clients = 1;
380
381 tm->bytes_to_send = 8192;
382 tm->n_iterations = 1;
383 vec_free (tm->connect_uri);
384
385 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
386 {
387 if (unformat (input, "nclients %d", &n_clients))
388 ;
389 else if (unformat (input, "iterations %d", &tm->n_iterations))
390 ;
Florin Coras3e350af2017-03-30 02:54:28 -0700391 else if (unformat (input, "bytes %lld", &tm->bytes_to_send))
Florin Coras6792ec02017-03-13 03:49:51 -0700392 ;
393 else if (unformat (input, "uri %s", &tm->connect_uri))
394 ;
395 else
396 return clib_error_return (0, "unknown input `%U'",
397 format_unformat_error, input);
398 }
399
400 tm->ready_connections = 0;
401 tm->expected_connections = n_clients;
402 uri = connect_uri;
403 if (tm->connect_uri)
404 uri = tm->connect_uri;
405
406 create_api_loopback (tm);
407
Florin Coras3e350af2017-03-30 02:54:28 -0700408#if TCP_BUILTIN_CLIENT_PTHREAD
Florin Coras6792ec02017-03-13 03:49:51 -0700409 /* Start a transmit thread */
410 if (tm->client_thread_handle == 0)
411 {
412 int rv = pthread_create (&tm->client_thread_handle,
Florin Coras3e350af2017-03-30 02:54:28 -0700413 NULL /*attr */ ,
414 tclient_thread_fn, 0);
Florin Coras6792ec02017-03-13 03:49:51 -0700415 if (rv)
416 {
417 tm->client_thread_handle = 0;
418 return clib_error_return (0, "pthread_create returned %d", rv);
419 }
420 }
Florin Coras3e350af2017-03-30 02:54:28 -0700421#endif
Florin Coras6792ec02017-03-13 03:49:51 -0700422
423 /* Fire off connect requests, in something approaching a normal manner */
424 for (i = 0; i < n_clients; i++)
425 {
426 vl_api_connect_uri_t *cmp;
427 cmp = vl_msg_api_alloc_as_if_client (sizeof (*cmp));
428 memset (cmp, 0, sizeof (*cmp));
429
430 cmp->_vl_msg_id = ntohs (VL_API_CONNECT_URI);
431 cmp->client_index = tm->my_client_index;
432 cmp->context = ntohl (0xfeedface);
433 memcpy (cmp->uri, uri, strlen ((char *) uri) + 1);
434 vl_msg_api_send_shmem (tm->vl_input_queue, (u8 *) & cmp);
435 }
436
437 tm->run_test = 1;
438
439 return 0;
440}
441
Florin Coras3e350af2017-03-30 02:54:28 -0700442#if TCP_BUILTIN_CLIENT_VPP_THREAD
443/* *INDENT-OFF* */
444VLIB_REGISTER_THREAD (builtin_client_reg, static) = {
445 .name = "tcp-builtin-client",
446 .function = tclient_thread_fn,
447 .fixed_count = 1,
448 .count = 1,
449 .no_data_structure_clone = 1,
450};
451/* *INDENT-ON* */
452#endif
453
Florin Coras6792ec02017-03-13 03:49:51 -0700454/* *INDENT-OFF* */
455VLIB_CLI_COMMAND (test_clients_command, static) =
456{
457 .path = "test tcp clients",
458 .short_help = "test tcp clients",
459 .function = test_tcp_clients_command_fn,
460};
461/* *INDENT-ON* */
462
463/*
464 * fd.io coding-style-patch-verification: ON
465 *
466 * Local Variables:
467 * eval: (c-set-style "gnu")
468 * End:
469 */