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