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_interface.h>
17 #include <vnet/session/session.h>
18 #include <http/http.h>
23 #define HCC_DBG(_fmt, _args...) clib_warning (_fmt, ##_args)
25 #define HCC_DBG(_fmt, _args...)
30 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
34 u32 vpp_session_index;
41 hcc_session_t *sessions;
52 u32 private_segment_size;
56 session_endpoint_cfg_t connect_sep;
58 u8 test_client_attached;
59 vlib_main_t *vlib_main;
68 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);
139 vlib_process_signal_event_mt (hcm->vlib_main, hcm->cli_node_index,
140 HCC_CONNECT_FAILED, 0);
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));
150 hs->vpp_session_index = as->session_index;
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);
158 svm_fifo_seg_t segs[2] = { { (u8 *) &msg, sizeof (msg) },
159 { hcm->http_query, vec_len (hcm->http_query) } };
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))
164 clib_warning ("failed app enqueue");
168 if (svm_fifo_set_event (as->tx_fifo))
169 session_send_io_evt_to_thread (as->tx_fifo, SESSION_IO_EVT_TX);
175 hcc_ts_reset_callback (session_t *s)
177 hcc_main_t *hcm = &hcc_main;
179 vnet_disconnect_args_t _a = { 0 }, *a = &_a;
181 hs = hcc_session_get (s->opaque, s->thread_index);
184 a->handle = session_handle (s);
185 a->app_index = hcm->app_index;
186 vnet_disconnect_session (a);
190 hcc_ts_tx_callback (session_t *ts)
192 clib_warning ("bug");
197 hcc_session_disconnect (session_t *s)
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);
207 hcc_ts_rx_callback (session_t *ts)
209 hcc_main_t *hcm = &hcc_main;
214 hs = hcc_session_get (ts->opaque, ts->thread_index);
218 clib_warning ("session is closed");
222 if (hs->to_recv == 0)
224 rv = svm_fifo_dequeue (ts->rx_fifo, sizeof (msg), (u8 *) &msg);
225 ASSERT (rv == sizeof (msg));
227 if (msg.type != HTTP_MSG_REPLY || msg.code != HTTP_STATUS_OK)
229 clib_warning ("unexpected msg type %d", msg.type);
232 vec_validate (hcm->http_response, msg.data.len - 1);
233 vec_reset_length (hcm->http_response);
234 hs->to_recv = msg.data.len;
237 u32 max_deq = svm_fifo_max_dequeue (ts->rx_fifo);
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);
244 clib_warning ("app dequeue(n=%d) failed; rv = %d", n_deq, rv);
251 vec_set_len (hcm->http_response, curr + n_deq);
252 ASSERT (hs->to_recv >= rv);
254 HCC_DBG ("app rcvd %d, remains %d", rv, hs->to_recv);
256 if (hs->to_recv == 0)
258 hcc_session_disconnect (ts);
259 vlib_process_signal_event_mt (hcm->vlib_main, hcm->cli_node_index,
260 HCC_REPLY_RECEIVED, 0);
267 hcc_ts_cleanup_callback (session_t *s, session_cleanup_ntf_t ntf)
271 hs = hcc_session_get (s->thread_index, s->opaque);
275 hcc_session_free (s->thread_index, hs);
279 hcc_ts_transport_closed (session_t *s)
281 hcc_main_t *hcm = &hcc_main;
283 HCC_DBG ("transport closed");
285 vlib_process_signal_event_mt (hcm->vlib_main, hcm->cli_node_index,
286 HCC_TRANSPORT_CLOSED, 0);
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,
300 static clib_error_t *
303 hcc_main_t *hcm = &hcc_main;
304 vnet_app_attach_args_t _a, *a = &_a;
306 u32 segment_size = 128 << 20;
309 if (hcm->private_segment_size)
310 segment_size = hcm->private_segment_size;
312 clib_memset (a, 0, sizeof (*a));
313 clib_memset (options, 0, sizeof (options));
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;
329 a->namespace_id = hcm->appns_id;
330 a->options[APP_OPTIONS_NAMESPACE_SECRET] = hcm->appns_secret;
333 if ((rv = vnet_application_attach (a)))
334 return clib_error_return (0, "attach returned %d", rv);
336 hcm->app_index = a->app_index;
338 hcm->test_client_attached = 1;
343 hcc_connect_rpc (void *rpc_args)
345 vnet_connect_args_t *a = rpc_args;
348 rv = vnet_connect (a);
350 clib_warning (0, "connect returned: %U", format_session_error, rv);
357 hcc_program_connect (vnet_connect_args_t *a)
359 session_send_rpc_evt_to_thread_force (transport_cl_thread (),
363 static clib_error_t *
366 vnet_connect_args_t *a = 0;
367 hcc_main_t *hcm = &hcc_main;
372 clib_memset (a, 0, sizeof (a[0]));
374 clib_memcpy (&a->sep_ext, &hcm->connect_sep, sizeof (hcm->connect_sep));
375 a->app_index = hcm->app_index;
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;
382 hcc_program_connect (a);
386 static clib_error_t *
387 hcc_run (vlib_main_t *vm, int print_output)
389 vlib_thread_main_t *vtm = vlib_get_thread_main ();
390 hcc_main_t *hcm = &hcc_main;
391 uword event_type, *event_data = 0;
393 clib_error_t *err = 0;
396 num_threads = 1 /* main thread */ + vtm->n_threads;
397 vec_validate (hcm->wrk, num_threads);
398 vec_foreach (wrk, hcm->wrk)
400 wrk->thread_index = wrk - hcm->wrk;
403 if ((err = hcc_attach ()))
405 return clib_error_return (0, "http client attach: %U", format_clib_error,
409 if ((err = hcc_connect ()))
411 return clib_error_return (0, "http client connect: %U",
412 format_clib_error, err);
415 vlib_process_wait_for_event_or_clock (vm, 10);
416 event_type = vlib_process_get_events (vm, &event_data);
420 err = clib_error_return (0, "timeout");
423 case HCC_REPLY_RECEIVED:
425 vlib_cli_output (vm, "%v", hcm->http_response);
426 vec_free (hcm->http_response);
428 case HCC_TRANSPORT_CLOSED:
429 err = clib_error_return (0, "error, transport closed");
431 case HCC_CONNECT_FAILED:
432 err = clib_error_return (0, "failed to connect");
435 err = clib_error_return (0, "unexpected event %d", event_type);
440 vec_free (event_data);
447 hcc_main_t *hcm = &hcc_main;
448 vnet_app_detach_args_t _da, *da = &_da;
451 if (!hcm->test_client_attached)
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;
463 static clib_error_t *
464 hcc_command_fn (vlib_main_t *vm, unformat_input_t *input,
465 vlib_cli_command_t *cmd)
467 unformat_input_t _line_input, *line_input = &_line_input;
468 hcc_main_t *hcm = &hcc_main;
471 clib_error_t *err = 0;
472 int rv, print_output = 1;
474 hcm->prealloc_fifos = 0;
475 hcm->private_segment_size = 0;
478 if (hcm->test_client_attached)
479 return clib_error_return (0, "failed: already running!");
481 /* Get a line of input. */
482 if (!unformat_user (input, unformat_line_input, line_input))
483 return clib_error_return (0, "expected URI");
485 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
487 if (unformat (line_input, "prealloc-fifos %d", &hcm->prealloc_fifos))
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))
496 else if (unformat (line_input, "no-output"))
498 else if (unformat (line_input, "appns %_%v%_", &appns_id))
500 else if (unformat (line_input, "secret %lu", &hcm->appns_secret))
502 else if (unformat (line_input, "query %s", &hcm->http_query))
506 err = clib_error_return (0, "unknown input `%U'",
507 format_unformat_error, line_input);
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;
518 err = clib_error_return (0, "URI not defined");
522 if ((rv = parse_uri ((char *) hcm->uri, &hcm->connect_sep)))
524 err = clib_error_return (0, "Uri parse error: %d", rv);
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);
532 err = hcc_run (vm, print_output);
536 /* don't override last error */
538 err = clib_error_return (0, "failed: app detach");
539 clib_warning ("WARNING: app detach failed...");
544 vec_free (hcm->http_query);
545 unformat_free (line_input);
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,
557 static clib_error_t *
558 hcc_main_init (vlib_main_t *vm)
560 hcc_main_t *hcm = &hcc_main;
567 VLIB_INIT_FUNCTION (hcc_main_init);
570 * fd.io coding-style-patch-verification: ON
573 * eval: (c-set-style "gnu")