9e8e156107bbd1daf98142d0f66c33b76a1c2f37
[vpp.git] / src / vnet / tcp / builtin_client.c
1 /*
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
46 #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
50 static void
51 send_test_chunk (tclient_main_t * tm, session_t * s)
52 {
53   u8 *test_data = tm->connect_test_data;
54   int test_buf_offset;
55   u32 bytes_this_chunk;
56   session_fifo_event_t evt;
57   static int serial_number = 0;
58   int rv;
59   test_buf_offset = s->bytes_sent % vec_len (test_data);
60   bytes_this_chunk = vec_len (test_data) - test_buf_offset;
61
62   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)
71     {
72       if (TCP_BUILTIN_CLIENT_DBG)
73         {
74           /* *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         }
88
89       /* Account for it... */
90       s->bytes_to_send -= rv;
91       s->bytes_sent += rv;
92
93       /* 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 */ );
103         }
104     }
105 }
106
107 static void
108 receive_test_chunk (tclient_main_t * tm, session_t * s)
109 {
110   svm_fifo_t *rx_fifo = s->server_rx_fifo;
111   int n_read, test_bytes = 0;
112
113   /* Allow enqueuing of new event */
114   // svm_fifo_unset_event (rx_fifo);
115
116   n_read = svm_fifo_dequeue_nowait (rx_fifo, 0, vec_len (tm->rx_buf),
117                                     tm->rx_buf);
118   if (n_read > 0)
119     {
120       if (TCP_BUILTIN_CLIENT_DBG)
121         {
122           /* *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;
140           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",
145                                 n_read, s->bytes_received + i, tm->rx_buf[i],
146                                 ((s->bytes_received + i) & 0xff));
147                 }
148             }
149         }
150       s->bytes_to_receive -= n_read;
151       s->bytes_received += n_read;
152     }
153 }
154
155 #if TCP_BUILTIN_CLIENT_VPP_THREAD
156 static void
157 #else
158 static void *
159 #endif
160 tclient_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
177   clib_per_cpu_mheaps[vlib_get_thread_index ()] = clib_per_cpu_mheaps[0];
178
179   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);
226
227       /* *INDENT-OFF* */
228       pool_foreach (sp, tm->sessions, ({
229         vec_add1 (session_indices, sp - tm->sessions);
230       }));
231       /* *INDENT-ON* */
232
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 */
247 #if TCP_BUILTIN_CLIENT_PTHREAD
248   return 0;
249 #endif
250 }
251
252 /* So we don't get "no handler for... " msgs */
253 static void
254 vl_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
261 static void
262 vl_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
305 static void
306 create_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
333 static clib_error_t *
334 tclient_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
368 VLIB_API_INIT_FUNCTION (tclient_api_hookup);
369
370 static clib_error_t *
371 test_tcp_clients_command_fn (vlib_main_t * vm,
372                              unformat_input_t * input,
373                              vlib_cli_command_t * cmd)
374 {
375   u8 *connect_uri = (u8 *) "tcp://6.0.1.1/1234";
376   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         ;
391       else if (unformat (input, "bytes %lld", &tm->bytes_to_send))
392         ;
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
408 #if TCP_BUILTIN_CLIENT_PTHREAD
409   /* Start a transmit thread */
410   if (tm->client_thread_handle == 0)
411     {
412       int rv = pthread_create (&tm->client_thread_handle,
413                                NULL /*attr */ ,
414                                tclient_thread_fn, 0);
415       if (rv)
416         {
417           tm->client_thread_handle = 0;
418           return clib_error_return (0, "pthread_create returned %d", rv);
419         }
420     }
421 #endif
422   vnet_session_enable_disable (vm, 1 /* turn on TCP, etc. */ );
423
424   /* Fire off connect requests, in something approaching a normal manner */
425   for (i = 0; i < n_clients; i++)
426     {
427       vl_api_connect_uri_t *cmp;
428       cmp = vl_msg_api_alloc_as_if_client (sizeof (*cmp));
429       memset (cmp, 0, sizeof (*cmp));
430
431       cmp->_vl_msg_id = ntohs (VL_API_CONNECT_URI);
432       cmp->client_index = tm->my_client_index;
433       cmp->context = ntohl (0xfeedface);
434       memcpy (cmp->uri, uri, strlen ((char *) uri) + 1);
435       vl_msg_api_send_shmem (tm->vl_input_queue, (u8 *) & cmp);
436     }
437
438   tm->run_test = 1;
439
440   return 0;
441 }
442
443 #if TCP_BUILTIN_CLIENT_VPP_THREAD
444 /* *INDENT-OFF* */
445 VLIB_REGISTER_THREAD (builtin_client_reg, static) = {
446   .name = "tcp-builtin-client",
447   .function = tclient_thread_fn,
448   .fixed_count = 1,
449   .count = 1,
450   .no_data_structure_clone = 1,
451 };
452 /* *INDENT-ON* */
453 #endif
454
455 /* *INDENT-OFF* */
456 VLIB_CLI_COMMAND (test_clients_command, static) =
457 {
458   .path = "test tcp clients",
459   .short_help = "test tcp clients [nclients %d] [iterations %d] [bytes %d] [uri tcp://1.2.3.4/1234]",
460   .function = test_tcp_clients_command_fn,
461 };
462 /* *INDENT-ON* */
463
464 /*
465  * fd.io coding-style-patch-verification: ON
466  *
467  * Local Variables:
468  * eval: (c-set-style "gnu")
469  * End:
470  */