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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
25 #define HCC_DBG(_fmt, _args...) clib_warning (_fmt, ##_args)
27 #define HCC_DBG(_fmt, _args...)
32 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
36 u32 vpp_session_index;
43 hcc_session_t *sessions;
54 u32 private_segment_size;
58 session_endpoint_cfg_t connect_sep;
60 u8 test_client_attached;
61 vlib_main_t *vlib_main;
70 HCC_REPLY_RECEIVED = 100,
73 static hcc_main_t hcc_main;
76 hcc_worker_get (u32 thread_index)
78 return vec_elt_at_index (hcc_main.wrk, thread_index);
81 static hcc_session_t *
82 hcc_session_alloc (hcc_worker_t *wrk)
85 pool_get_zero (wrk->sessions, hs);
86 hs->session_index = hs - wrk->sessions;
87 hs->thread_index = wrk->thread_index;
91 static hcc_session_t *
92 hcc_session_get (u32 hs_index, u32 thread_index)
94 hcc_worker_t *wrk = hcc_worker_get (thread_index);
95 return pool_elt_at_index (wrk->sessions, hs_index);
99 hcc_session_free (u32 thread_index, hcc_session_t *hs)
101 hcc_worker_t *wrk = hcc_worker_get (thread_index);
102 pool_put (wrk->sessions, hs);
106 hcc_ts_accept_callback (session_t *ts)
108 clib_warning ("bug");
113 hcc_ts_disconnect_callback (session_t *s)
115 hcc_main_t *hcm = &hcc_main;
116 vnet_disconnect_args_t _a = { 0 }, *a = &_a;
118 a->handle = session_handle (s);
119 a->app_index = hcm->app_index;
120 vnet_disconnect_session (a);
124 hcc_ts_connected_callback (u32 app_index, u32 hc_index, session_t *as,
127 hcc_main_t *hcm = &hcc_main;
128 hcc_session_t *hs, *new_hs;
133 HCC_DBG ("hc_index: %d", hc_index);
137 clib_warning ("connected error: hc_index(%d): %U", hc_index,
138 format_session_error, err);
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));
148 hs->vpp_session_index = as->session_index;
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);
156 svm_fifo_seg_t segs[2] = { { (u8 *) &msg, sizeof (msg) },
157 { hcm->http_query, vec_len (hcm->http_query) } };
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))
162 clib_warning ("failed app enqueue");
166 if (svm_fifo_set_event (as->tx_fifo))
167 session_send_io_evt_to_thread (as->tx_fifo, SESSION_IO_EVT_TX);
173 hcc_ts_reset_callback (session_t *s)
175 hcc_main_t *hcm = &hcc_main;
177 vnet_disconnect_args_t _a = { 0 }, *a = &_a;
179 hs = hcc_session_get (s->opaque, s->thread_index);
182 a->handle = session_handle (s);
183 a->app_index = hcm->app_index;
184 vnet_disconnect_session (a);
188 hcc_ts_tx_callback (session_t *ts)
190 clib_warning ("bug");
195 hcc_session_disconnect (session_t *s)
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);
205 hcc_ts_rx_callback (session_t *ts)
207 hcc_main_t *hcm = &hcc_main;
212 hs = hcc_session_get (ts->opaque, ts->thread_index);
216 clib_warning ("session is closed");
220 if (hs->to_recv == 0)
222 rv = svm_fifo_dequeue (ts->rx_fifo, sizeof (msg), (u8 *) &msg);
223 ASSERT (rv == sizeof (msg));
225 if (msg.type != HTTP_MSG_REPLY || msg.code != HTTP_STATUS_OK)
227 clib_warning ("unexpected msg type %d", msg.type);
230 vec_validate (hcm->http_response, msg.data.len - 1);
231 vec_reset_length (hcm->http_response);
232 hs->to_recv = msg.data.len;
235 u32 max_deq = svm_fifo_max_dequeue (ts->rx_fifo);
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);
242 clib_warning ("app dequeue(n=%d) failed; rv = %d", n_deq, rv);
249 vec_set_len (hcm->http_response, curr + n_deq);
250 ASSERT (hs->to_recv >= rv);
252 HCC_DBG ("app rcvd %d, remains %d", rv, hs->to_recv);
254 if (hs->to_recv == 0)
256 hcc_session_disconnect (ts);
257 vlib_process_signal_event_mt (hcm->vlib_main, hcm->cli_node_index,
258 HCC_REPLY_RECEIVED, 0);
265 hcc_ts_cleanup_callback (session_t *s, session_cleanup_ntf_t ntf)
269 hs = hcc_session_get (s->thread_index, s->opaque);
273 hcc_session_free (s->thread_index, hs);
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,
286 static clib_error_t *
289 hcc_main_t *hcm = &hcc_main;
290 vnet_app_attach_args_t _a, *a = &_a;
292 u32 segment_size = 128 << 20;
295 if (hcm->private_segment_size)
296 segment_size = hcm->private_segment_size;
298 clib_memset (a, 0, sizeof (*a));
299 clib_memset (options, 0, sizeof (options));
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;
315 a->namespace_id = hcm->appns_id;
316 a->options[APP_OPTIONS_NAMESPACE_SECRET] = hcm->appns_secret;
319 if ((rv = vnet_application_attach (a)))
320 return clib_error_return (0, "attach returned %d", rv);
322 hcm->app_index = a->app_index;
324 hcm->test_client_attached = 1;
329 hcc_connect_rpc (void *rpc_args)
331 vnet_connect_args_t *a = rpc_args;
334 rv = vnet_connect (a);
336 clib_warning (0, "connect returned: %U", format_session_error, rv);
343 hcc_program_connect (vnet_connect_args_t *a)
345 session_send_rpc_evt_to_thread_force (transport_cl_thread (),
349 static clib_error_t *
352 vnet_connect_args_t *a = 0;
353 hcc_main_t *hcm = &hcc_main;
358 clib_memset (a, 0, sizeof (a[0]));
360 clib_memcpy (&a->sep_ext, &hcm->connect_sep, sizeof (hcm->connect_sep));
361 a->app_index = hcm->app_index;
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;
368 hcc_program_connect (a);
372 static clib_error_t *
373 hcc_run (vlib_main_t *vm)
375 vlib_thread_main_t *vtm = vlib_get_thread_main ();
376 hcc_main_t *hcm = &hcc_main;
377 uword event_type, *event_data = 0;
379 clib_error_t *err = 0;
382 num_threads = 1 /* main thread */ + vtm->n_threads;
383 vec_validate (hcm->wrk, num_threads);
384 vec_foreach (wrk, hcm->wrk)
386 wrk->thread_index = wrk - hcm->wrk;
389 if ((err = hcc_attach ()))
391 return clib_error_return (0, "http client attach: %U", format_clib_error,
395 if ((err = hcc_connect ()))
397 return clib_error_return (0, "http client connect: %U",
398 format_clib_error, err);
401 vlib_process_wait_for_event_or_clock (vm, 10);
402 event_type = vlib_process_get_events (vm, &event_data);
406 err = clib_error_return (0, "timeout");
409 case HCC_REPLY_RECEIVED:
410 vlib_cli_output (vm, "%v", hcm->http_response);
411 vec_free (hcm->http_response);
414 clib_error_return (0, "unexpected event %d", event_type);
419 vec_free (event_data);
426 hcc_main_t *hcm = &hcc_main;
427 vnet_app_detach_args_t _da, *da = &_da;
430 if (!hcm->test_client_attached)
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;
442 static clib_error_t *
443 hcc_command_fn (vlib_main_t *vm, unformat_input_t *input,
444 vlib_cli_command_t *cmd)
446 unformat_input_t _line_input, *line_input = &_line_input;
447 hcc_main_t *hcm = &hcc_main;
450 clib_error_t *err = 0;
453 hcm->prealloc_fifos = 0;
454 hcm->private_segment_size = 0;
457 if (hcm->test_client_attached)
458 return clib_error_return (0, "failed: already running!");
460 /* Get a line of input. */
461 if (!unformat_user (input, unformat_line_input, line_input))
462 return clib_error_return (0, "expected URI");
464 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
466 if (unformat (line_input, "prealloc-fifos %d", &hcm->prealloc_fifos))
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))
475 else if (unformat (line_input, "appns %_%v%_", &appns_id))
477 else if (unformat (line_input, "secret %lu", &hcm->appns_secret))
479 else if (unformat (line_input, "query %s", &hcm->http_query))
483 err = clib_error_return (0, "unknown input `%U'",
484 format_unformat_error, line_input);
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;
495 err = clib_error_return (0, "URI not defined");
499 if ((rv = parse_uri ((char *) hcm->uri, &hcm->connect_sep)))
501 err = clib_error_return (0, "Uri parse error: %d", rv);
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);
513 /* don't override last error */
515 err = clib_error_return (0, "failed: app detach");
516 clib_warning ("WARNING: app detach failed...");
521 vec_free (hcm->http_query);
522 unformat_free (line_input);
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,
534 static clib_error_t *
535 hcc_main_init (vlib_main_t *vm)
537 hcc_main_t *hcm = &hcc_main;
544 VLIB_INIT_FUNCTION (hcc_main_init);
547 * fd.io coding-style-patch-verification: ON
550 * eval: (c-set-style "gnu")