hs-test: more debug output in http3 test
[vpp.git] / src / plugins / hs_apps / http_client_cli.c
1 /*
2  * Copyright (c) 2022 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include <vnet/session/application_interface.h>
17 #include <vnet/session/session.h>
18 #include <http/http.h>
19
20 #define HCC_DEBUG 0
21
22 #if HCC_DEBUG
23 #define HCC_DBG(_fmt, _args...) clib_warning (_fmt, ##_args)
24 #else
25 #define HCC_DBG(_fmt, _args...)
26 #endif
27
28 typedef struct
29 {
30   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
31   u32 session_index;
32   u32 thread_index;
33   u32 rx_offset;
34   u32 vpp_session_index;
35   u32 to_recv;
36   u8 is_closed;
37 } hcc_session_t;
38
39 typedef struct
40 {
41   hcc_session_t *sessions;
42   u8 *rx_buf;
43   u32 thread_index;
44 } hcc_worker_t;
45
46 typedef struct
47 {
48   hcc_worker_t *wrk;
49   u32 app_index;
50
51   u32 prealloc_fifos;
52   u32 private_segment_size;
53   u32 fifo_size;
54   u8 *uri;
55   u8 *http_query;
56   session_endpoint_cfg_t connect_sep;
57
58   u8 test_client_attached;
59   vlib_main_t *vlib_main;
60   u32 cli_node_index;
61   u8 *http_response;
62   u8 *appns_id;
63   u64 appns_secret;
64 } hcc_main_t;
65
66 typedef enum
67 {
68   HCC_REPLY_RECEIVED = 100,
69   HCC_TRANSPORT_CLOSED,
70   HCC_CONNECT_FAILED,
71 } hcc_cli_signal_t;
72
73 static hcc_main_t hcc_main;
74
75 static hcc_worker_t *
76 hcc_worker_get (u32 thread_index)
77 {
78   return vec_elt_at_index (hcc_main.wrk, thread_index);
79 }
80
81 static hcc_session_t *
82 hcc_session_alloc (hcc_worker_t *wrk)
83 {
84   hcc_session_t *hs;
85   pool_get_zero (wrk->sessions, hs);
86   hs->session_index = hs - wrk->sessions;
87   hs->thread_index = wrk->thread_index;
88   return hs;
89 }
90
91 static hcc_session_t *
92 hcc_session_get (u32 hs_index, u32 thread_index)
93 {
94   hcc_worker_t *wrk = hcc_worker_get (thread_index);
95   return pool_elt_at_index (wrk->sessions, hs_index);
96 }
97
98 static void
99 hcc_session_free (u32 thread_index, hcc_session_t *hs)
100 {
101   hcc_worker_t *wrk = hcc_worker_get (thread_index);
102   pool_put (wrk->sessions, hs);
103 }
104
105 static int
106 hcc_ts_accept_callback (session_t *ts)
107 {
108   clib_warning ("bug");
109   return -1;
110 }
111
112 static void
113 hcc_ts_disconnect_callback (session_t *s)
114 {
115   hcc_main_t *hcm = &hcc_main;
116   vnet_disconnect_args_t _a = { 0 }, *a = &_a;
117
118   a->handle = session_handle (s);
119   a->app_index = hcm->app_index;
120   vnet_disconnect_session (a);
121 }
122
123 static int
124 hcc_ts_connected_callback (u32 app_index, u32 hc_index, session_t *as,
125                            session_error_t err)
126 {
127   hcc_main_t *hcm = &hcc_main;
128   hcc_session_t *hs, *new_hs;
129   hcc_worker_t *wrk;
130   http_msg_t msg;
131   int rv;
132
133   HCC_DBG ("hc_index: %d", hc_index);
134
135   if (err)
136     {
137       clib_warning ("connected error: hc_index(%d): %U", hc_index,
138                     format_session_error, err);
139       vlib_process_signal_event_mt (hcm->vlib_main, hcm->cli_node_index,
140                                     HCC_CONNECT_FAILED, 0);
141       return -1;
142     }
143
144   /* TODO delete half open session once the support is added in http layer */
145   hs = hcc_session_get (hc_index, 0);
146   wrk = hcc_worker_get (as->thread_index);
147   new_hs = hcc_session_alloc (wrk);
148   clib_memcpy_fast (new_hs, hs, sizeof (*hs));
149
150   hs->vpp_session_index = as->session_index;
151
152   msg.type = HTTP_MSG_REQUEST;
153   msg.method_type = HTTP_REQ_GET;
154   msg.content_type = HTTP_CONTENT_TEXT_HTML;
155   msg.data.type = HTTP_MSG_DATA_INLINE;
156   msg.data.len = vec_len (hcm->http_query);
157
158   svm_fifo_seg_t segs[2] = { { (u8 *) &msg, sizeof (msg) },
159                              { hcm->http_query, vec_len (hcm->http_query) } };
160
161   rv = svm_fifo_enqueue_segments (as->tx_fifo, segs, 2, 0 /* allow partial */);
162   if (rv < 0 || rv != sizeof (msg) + vec_len (hcm->http_query))
163     {
164       clib_warning ("failed app enqueue");
165       return -1;
166     }
167
168   if (svm_fifo_set_event (as->tx_fifo))
169     session_send_io_evt_to_thread (as->tx_fifo, SESSION_IO_EVT_TX);
170
171   return 0;
172 }
173
174 static void
175 hcc_ts_reset_callback (session_t *s)
176 {
177   hcc_main_t *hcm = &hcc_main;
178   hcc_session_t *hs;
179   vnet_disconnect_args_t _a = { 0 }, *a = &_a;
180
181   hs = hcc_session_get (s->opaque, s->thread_index);
182   hs->is_closed = 1;
183
184   a->handle = session_handle (s);
185   a->app_index = hcm->app_index;
186   vnet_disconnect_session (a);
187 }
188
189 static int
190 hcc_ts_tx_callback (session_t *ts)
191 {
192   clib_warning ("bug");
193   return -1;
194 }
195
196 static void
197 hcc_session_disconnect (session_t *s)
198 {
199   hcc_main_t *hcm = &hcc_main;
200   vnet_disconnect_args_t _a = { 0 }, *a = &_a;
201   a->handle = session_handle (s);
202   a->app_index = hcm->app_index;
203   vnet_disconnect_session (a);
204 }
205
206 static int
207 hcc_ts_rx_callback (session_t *ts)
208 {
209   hcc_main_t *hcm = &hcc_main;
210   hcc_session_t *hs;
211   http_msg_t msg;
212   int rv;
213
214   hs = hcc_session_get (ts->opaque, ts->thread_index);
215
216   if (hs->is_closed)
217     {
218       clib_warning ("session is closed");
219       return 0;
220     }
221
222   if (hs->to_recv == 0)
223     {
224       rv = svm_fifo_dequeue (ts->rx_fifo, sizeof (msg), (u8 *) &msg);
225       ASSERT (rv == sizeof (msg));
226
227       if (msg.type != HTTP_MSG_REPLY || msg.code != HTTP_STATUS_OK)
228         {
229           clib_warning ("unexpected msg type %d", msg.type);
230           return 0;
231         }
232       vec_validate (hcm->http_response, msg.data.len - 1);
233       vec_reset_length (hcm->http_response);
234       hs->to_recv = msg.data.len;
235     }
236
237   u32 max_deq = svm_fifo_max_dequeue (ts->rx_fifo);
238
239   u32 n_deq = clib_min (hs->to_recv, max_deq);
240   u32 curr = vec_len (hcm->http_response);
241   rv = svm_fifo_dequeue (ts->rx_fifo, n_deq, hcm->http_response + curr);
242   if (rv < 0)
243     {
244       clib_warning ("app dequeue(n=%d) failed; rv = %d", n_deq, rv);
245       return -1;
246     }
247
248   if (rv != n_deq)
249     return -1;
250
251   vec_set_len (hcm->http_response, curr + n_deq);
252   ASSERT (hs->to_recv >= rv);
253   hs->to_recv -= rv;
254   HCC_DBG ("app rcvd %d, remains %d", rv, hs->to_recv);
255
256   if (hs->to_recv == 0)
257     {
258       hcc_session_disconnect (ts);
259       vlib_process_signal_event_mt (hcm->vlib_main, hcm->cli_node_index,
260                                     HCC_REPLY_RECEIVED, 0);
261     }
262
263   return 0;
264 }
265
266 static void
267 hcc_ts_cleanup_callback (session_t *s, session_cleanup_ntf_t ntf)
268 {
269   hcc_session_t *hs;
270
271   hs = hcc_session_get (s->thread_index, s->opaque);
272   if (!hs)
273     return;
274
275   hcc_session_free (s->thread_index, hs);
276 }
277
278 static void
279 hcc_ts_transport_closed (session_t *s)
280 {
281   hcc_main_t *hcm = &hcc_main;
282
283   HCC_DBG ("transport closed");
284
285   vlib_process_signal_event_mt (hcm->vlib_main, hcm->cli_node_index,
286                                 HCC_TRANSPORT_CLOSED, 0);
287 }
288
289 static session_cb_vft_t hcc_session_cb_vft = {
290   .session_accept_callback = hcc_ts_accept_callback,
291   .session_disconnect_callback = hcc_ts_disconnect_callback,
292   .session_connected_callback = hcc_ts_connected_callback,
293   .builtin_app_rx_callback = hcc_ts_rx_callback,
294   .builtin_app_tx_callback = hcc_ts_tx_callback,
295   .session_reset_callback = hcc_ts_reset_callback,
296   .session_cleanup_callback = hcc_ts_cleanup_callback,
297   .session_transport_closed_callback = hcc_ts_transport_closed,
298 };
299
300 static clib_error_t *
301 hcc_attach ()
302 {
303   hcc_main_t *hcm = &hcc_main;
304   vnet_app_attach_args_t _a, *a = &_a;
305   u64 options[18];
306   u32 segment_size = 128 << 20;
307   int rv;
308
309   if (hcm->private_segment_size)
310     segment_size = hcm->private_segment_size;
311
312   clib_memset (a, 0, sizeof (*a));
313   clib_memset (options, 0, sizeof (options));
314
315   a->api_client_index = ~0;
316   a->name = format (0, "http_cli_client");
317   a->session_cb_vft = &hcc_session_cb_vft;
318   a->options = options;
319   a->options[APP_OPTIONS_SEGMENT_SIZE] = segment_size;
320   a->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = segment_size;
321   a->options[APP_OPTIONS_RX_FIFO_SIZE] =
322     hcm->fifo_size ? hcm->fifo_size : 8 << 10;
323   a->options[APP_OPTIONS_TX_FIFO_SIZE] =
324     hcm->fifo_size ? hcm->fifo_size : 32 << 10;
325   a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
326   a->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = hcm->prealloc_fifos;
327   if (hcm->appns_id)
328     {
329       a->namespace_id = hcm->appns_id;
330       a->options[APP_OPTIONS_NAMESPACE_SECRET] = hcm->appns_secret;
331     }
332
333   if ((rv = vnet_application_attach (a)))
334     return clib_error_return (0, "attach returned %d", rv);
335
336   hcm->app_index = a->app_index;
337   vec_free (a->name);
338   hcm->test_client_attached = 1;
339   return 0;
340 }
341
342 static int
343 hcc_connect_rpc (void *rpc_args)
344 {
345   vnet_connect_args_t *a = rpc_args;
346   int rv;
347
348   rv = vnet_connect (a);
349   if (rv)
350     clib_warning (0, "connect returned: %U", format_session_error, rv);
351
352   vec_free (a);
353   return rv;
354 }
355
356 static void
357 hcc_program_connect (vnet_connect_args_t *a)
358 {
359   session_send_rpc_evt_to_thread_force (transport_cl_thread (),
360                                         hcc_connect_rpc, a);
361 }
362
363 static clib_error_t *
364 hcc_connect ()
365 {
366   vnet_connect_args_t *a = 0;
367   hcc_main_t *hcm = &hcc_main;
368   hcc_worker_t *wrk;
369   hcc_session_t *hs;
370
371   vec_validate (a, 0);
372   clib_memset (a, 0, sizeof (a[0]));
373
374   clib_memcpy (&a->sep_ext, &hcm->connect_sep, sizeof (hcm->connect_sep));
375   a->app_index = hcm->app_index;
376
377   /* allocate http session on main thread */
378   wrk = hcc_worker_get (0);
379   hs = hcc_session_alloc (wrk);
380   a->api_context = hs->session_index;
381
382   hcc_program_connect (a);
383   return 0;
384 }
385
386 static clib_error_t *
387 hcc_run (vlib_main_t *vm, int print_output)
388 {
389   vlib_thread_main_t *vtm = vlib_get_thread_main ();
390   hcc_main_t *hcm = &hcc_main;
391   uword event_type, *event_data = 0;
392   u32 num_threads;
393   clib_error_t *err = 0;
394   hcc_worker_t *wrk;
395
396   num_threads = 1 /* main thread */ + vtm->n_threads;
397   vec_validate (hcm->wrk, num_threads);
398   vec_foreach (wrk, hcm->wrk)
399     {
400       wrk->thread_index = wrk - hcm->wrk;
401     }
402
403   if ((err = hcc_attach ()))
404     {
405       return clib_error_return (0, "http client attach: %U", format_clib_error,
406                                 err);
407     }
408
409   if ((err = hcc_connect ()))
410     {
411       return clib_error_return (0, "http client connect: %U",
412                                 format_clib_error, err);
413     }
414
415   vlib_process_wait_for_event_or_clock (vm, 10);
416   event_type = vlib_process_get_events (vm, &event_data);
417   switch (event_type)
418     {
419     case ~0:
420       err = clib_error_return (0, "timeout");
421       goto cleanup;
422
423     case HCC_REPLY_RECEIVED:
424       if (print_output)
425         vlib_cli_output (vm, "%v", hcm->http_response);
426       vec_free (hcm->http_response);
427       break;
428     case HCC_TRANSPORT_CLOSED:
429       err = clib_error_return (0, "error, transport closed");
430       break;
431     case HCC_CONNECT_FAILED:
432       err = clib_error_return (0, "failed to connect");
433       break;
434     default:
435       err = clib_error_return (0, "unexpected event %d", event_type);
436       break;
437     }
438
439 cleanup:
440   vec_free (event_data);
441   return err;
442 }
443
444 static int
445 hcc_detach ()
446 {
447   hcc_main_t *hcm = &hcc_main;
448   vnet_app_detach_args_t _da, *da = &_da;
449   int rv;
450
451   if (!hcm->test_client_attached)
452     return 0;
453
454   da->app_index = hcm->app_index;
455   da->api_client_index = ~0;
456   rv = vnet_application_detach (da);
457   hcm->test_client_attached = 0;
458   hcm->app_index = ~0;
459
460   return rv;
461 }
462
463 static clib_error_t *
464 hcc_command_fn (vlib_main_t *vm, unformat_input_t *input,
465                 vlib_cli_command_t *cmd)
466 {
467   unformat_input_t _line_input, *line_input = &_line_input;
468   hcc_main_t *hcm = &hcc_main;
469   u64 seg_size;
470   u8 *appns_id = 0;
471   clib_error_t *err = 0;
472   int rv, print_output = 1;
473
474   hcm->prealloc_fifos = 0;
475   hcm->private_segment_size = 0;
476   hcm->fifo_size = 0;
477
478   if (hcm->test_client_attached)
479     return clib_error_return (0, "failed: already running!");
480
481   /* Get a line of input. */
482   if (!unformat_user (input, unformat_line_input, line_input))
483     return clib_error_return (0, "expected URI");
484
485   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
486     {
487       if (unformat (line_input, "prealloc-fifos %d", &hcm->prealloc_fifos))
488         ;
489       else if (unformat (line_input, "private-segment-size %U",
490                          unformat_memory_size, &seg_size))
491         hcm->private_segment_size = seg_size;
492       else if (unformat (line_input, "fifo-size %d", &hcm->fifo_size))
493         hcm->fifo_size <<= 10;
494       else if (unformat (line_input, "uri %s", &hcm->uri))
495         ;
496       else if (unformat (line_input, "no-output"))
497         print_output = 0;
498       else if (unformat (line_input, "appns %_%v%_", &appns_id))
499         ;
500       else if (unformat (line_input, "secret %lu", &hcm->appns_secret))
501         ;
502       else if (unformat (line_input, "query %s", &hcm->http_query))
503         ;
504       else
505         {
506           err = clib_error_return (0, "unknown input `%U'",
507                                    format_unformat_error, line_input);
508           goto done;
509         }
510     }
511
512   vec_free (hcm->appns_id);
513   hcm->appns_id = appns_id;
514   hcm->cli_node_index = vlib_get_current_process (vm)->node_runtime.node_index;
515
516   if (!hcm->uri)
517     {
518       err = clib_error_return (0, "URI not defined");
519       goto done;
520     }
521
522   if ((rv = parse_uri ((char *) hcm->uri, &hcm->connect_sep)))
523     {
524       err = clib_error_return (0, "Uri parse error: %d", rv);
525       goto done;
526     }
527
528   vlib_worker_thread_barrier_sync (vm);
529   vnet_session_enable_disable (vm, 1 /* turn on TCP, etc. */);
530   vlib_worker_thread_barrier_release (vm);
531
532   err = hcc_run (vm, print_output);
533
534   if (hcc_detach ())
535     {
536       /* don't override last error */
537       if (!err)
538         err = clib_error_return (0, "failed: app detach");
539       clib_warning ("WARNING: app detach failed...");
540     }
541
542 done:
543   vec_free (hcm->uri);
544   vec_free (hcm->http_query);
545   unformat_free (line_input);
546   return err;
547 }
548
549 VLIB_CLI_COMMAND (hcc_command, static) = {
550   .path = "http cli client",
551   .short_help = "[appns <app-ns> secret <appns-secret>] uri http://<ip-addr> "
552                 "query <query-string> [no-output]",
553   .function = hcc_command_fn,
554   .is_mp_safe = 1,
555 };
556
557 static clib_error_t *
558 hcc_main_init (vlib_main_t *vm)
559 {
560   hcc_main_t *hcm = &hcc_main;
561
562   hcm->app_index = ~0;
563   hcm->vlib_main = vm;
564   return 0;
565 }
566
567 VLIB_INIT_FUNCTION (hcc_main_init);
568
569 /*
570  * fd.io coding-style-patch-verification: ON
571  *
572  * Local Variables:
573  * eval: (c-set-style "gnu")
574  * End:
575  */