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