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>
24 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
28 u32 vpp_session_index;
35 hcc_session_t *sessions;
46 u32 private_segment_size;
50 session_endpoint_cfg_t connect_sep;
52 u8 test_client_attached;
53 vlib_main_t *vlib_main;
62 HCC_REPLY_RECEIVED = 100,
65 static hcc_main_t hcc_main;
68 hcc_worker_get (u32 thread_index)
70 return vec_elt_at_index (hcc_main.wrk, thread_index);
73 static hcc_session_t *
74 hcc_session_alloc (hcc_worker_t *wrk)
77 pool_get_zero (wrk->sessions, hs);
78 hs->session_index = hs - wrk->sessions;
79 hs->thread_index = wrk->thread_index;
83 static hcc_session_t *
84 hcc_session_get (u32 hs_index, u32 thread_index)
86 hcc_worker_t *wrk = hcc_worker_get (thread_index);
87 return pool_elt_at_index (wrk->sessions, hs_index);
91 hcc_session_free (u32 thread_index, hcc_session_t *hs)
93 hcc_worker_t *wrk = hcc_worker_get (thread_index);
94 pool_put (wrk->sessions, hs);
98 hcc_ts_accept_callback (session_t *ts)
100 clib_warning ("bug");
105 hcc_ts_disconnect_callback (session_t *s)
107 hcc_main_t *hcm = &hcc_main;
108 vnet_disconnect_args_t _a = { 0 }, *a = &_a;
110 a->handle = session_handle (s);
111 a->app_index = hcm->app_index;
112 vnet_disconnect_session (a);
116 hcc_ts_connected_callback (u32 app_index, u32 hc_index, session_t *as,
119 hcc_main_t *hcm = &hcc_main;
120 hcc_session_t *hs, *new_hs;
127 clib_warning ("connected error: hc_index(%d): %U", hc_index,
128 format_session_error, err);
132 /* TODO delete half open session once the support is added in http layer */
133 hs = hcc_session_get (hc_index, 0);
134 wrk = hcc_worker_get (as->thread_index);
135 new_hs = hcc_session_alloc (wrk);
136 clib_memcpy_fast (new_hs, hs, sizeof (*hs));
138 hs->vpp_session_index = as->session_index;
140 msg.type = HTTP_MSG_REQUEST;
141 msg.method_type = HTTP_REQ_GET;
142 msg.content_type = HTTP_CONTENT_TEXT_HTML;
143 msg.data.type = HTTP_MSG_DATA_INLINE;
144 msg.data.len = vec_len (hcm->http_query);
146 svm_fifo_seg_t segs[2] = { { (u8 *) &msg, sizeof (msg) },
147 { hcm->http_query, vec_len (hcm->http_query) } };
149 rv = svm_fifo_enqueue_segments (as->tx_fifo, segs, 2, 0 /* allow partial */);
150 if (rv < 0 || rv != sizeof (msg) + vec_len (hcm->http_query))
152 clib_warning ("failed app enqueue");
156 if (svm_fifo_set_event (as->tx_fifo))
157 session_send_io_evt_to_thread (as->tx_fifo, SESSION_IO_EVT_TX);
163 hcc_ts_reset_callback (session_t *s)
165 hcc_main_t *hcm = &hcc_main;
167 vnet_disconnect_args_t _a = { 0 }, *a = &_a;
169 hs = hcc_session_get (s->opaque, s->thread_index);
172 a->handle = session_handle (s);
173 a->app_index = hcm->app_index;
174 vnet_disconnect_session (a);
178 hcc_ts_tx_callback (session_t *ts)
180 clib_warning ("bug");
185 hcc_session_disconnect (session_t *s)
187 hcc_main_t *hcm = &hcc_main;
188 vnet_disconnect_args_t _a = { 0 }, *a = &_a;
189 a->handle = session_handle (s);
190 a->app_index = hcm->app_index;
191 vnet_disconnect_session (a);
195 hcc_ts_rx_callback (session_t *ts)
197 hcc_main_t *hcm = &hcc_main;
202 hs = hcc_session_get (ts->opaque, ts->thread_index);
206 clib_warning ("session is closed");
212 rv = svm_fifo_dequeue (ts->rx_fifo, sizeof (msg), (u8 *) &msg);
213 ASSERT (rv == sizeof (msg));
215 if (msg.type != HTTP_MSG_REPLY || msg.code != HTTP_STATUS_OK)
217 clib_warning ("unexpected msg type %d", msg.type);
220 vec_validate (hcm->http_response, msg.data.len - 1);
221 vec_reset_length (hcm->http_response);
222 hs->to_recv = msg.data.len;
225 u32 max_deq = svm_fifo_max_dequeue (ts->rx_fifo);
227 u32 n_deq = clib_min (hs->to_recv, max_deq);
228 u32 curr = vec_len (hcm->http_response);
229 rv = svm_fifo_dequeue (ts->rx_fifo, n_deq, hcm->http_response + curr);
232 clib_warning ("app dequeue failed");
239 vec_set_len (hcm->http_response, curr + n_deq);
240 ASSERT (hs->to_recv >= rv);
243 if (hs->to_recv == 0)
245 hcc_session_disconnect (ts);
246 vlib_process_signal_event_mt (hcm->vlib_main, hcm->cli_node_index,
247 HCC_REPLY_RECEIVED, 0);
254 hcc_ts_cleanup_callback (session_t *s, session_cleanup_ntf_t ntf)
258 hs = hcc_session_get (s->thread_index, s->opaque);
262 hcc_session_free (s->thread_index, hs);
265 static session_cb_vft_t hcc_session_cb_vft = {
266 .session_accept_callback = hcc_ts_accept_callback,
267 .session_disconnect_callback = hcc_ts_disconnect_callback,
268 .session_connected_callback = hcc_ts_connected_callback,
269 .builtin_app_rx_callback = hcc_ts_rx_callback,
270 .builtin_app_tx_callback = hcc_ts_tx_callback,
271 .session_reset_callback = hcc_ts_reset_callback,
272 .session_cleanup_callback = hcc_ts_cleanup_callback,
275 static clib_error_t *
278 hcc_main_t *hcm = &hcc_main;
279 vnet_app_attach_args_t _a, *a = &_a;
281 u32 segment_size = 128 << 20;
284 if (hcm->private_segment_size)
285 segment_size = hcm->private_segment_size;
287 clib_memset (a, 0, sizeof (*a));
288 clib_memset (options, 0, sizeof (options));
290 a->api_client_index = ~0;
291 a->name = format (0, "http_cli_client");
292 a->session_cb_vft = &hcc_session_cb_vft;
293 a->options = options;
294 a->options[APP_OPTIONS_SEGMENT_SIZE] = segment_size;
295 a->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = segment_size;
296 a->options[APP_OPTIONS_RX_FIFO_SIZE] =
297 hcm->fifo_size ? hcm->fifo_size : 8 << 10;
298 a->options[APP_OPTIONS_TX_FIFO_SIZE] =
299 hcm->fifo_size ? hcm->fifo_size : 32 << 10;
300 a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
301 a->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = hcm->prealloc_fifos;
304 a->namespace_id = hcm->appns_id;
305 a->options[APP_OPTIONS_NAMESPACE_SECRET] = hcm->appns_secret;
308 if ((rv = vnet_application_attach (a)))
309 return clib_error_return (0, "attach returned %d", rv);
311 hcm->app_index = a->app_index;
313 hcm->test_client_attached = 1;
318 hcc_connect_rpc (void *rpc_args)
320 vnet_connect_args_t *a = rpc_args;
323 rv = vnet_connect (a);
325 clib_warning (0, "connect returned: %U", format_session_error, rv);
332 hcc_program_connect (vnet_connect_args_t *a)
334 session_send_rpc_evt_to_thread_force (transport_cl_thread (),
338 static clib_error_t *
341 vnet_connect_args_t *a = 0;
342 hcc_main_t *hcm = &hcc_main;
347 clib_memset (a, 0, sizeof (a[0]));
349 clib_memcpy (&a->sep_ext, &hcm->connect_sep, sizeof (hcm->connect_sep));
350 a->app_index = hcm->app_index;
352 /* allocate http session on main thread */
353 wrk = hcc_worker_get (0);
354 hs = hcc_session_alloc (wrk);
355 a->api_context = hs->session_index;
357 hcc_program_connect (a);
361 static clib_error_t *
362 hcc_run (vlib_main_t *vm)
364 vlib_thread_main_t *vtm = vlib_get_thread_main ();
365 hcc_main_t *hcm = &hcc_main;
366 uword event_type, *event_data = 0;
368 clib_error_t *err = 0;
371 num_threads = 1 /* main thread */ + vtm->n_threads;
372 vec_validate (hcm->wrk, num_threads);
373 vec_foreach (wrk, hcm->wrk)
375 wrk->thread_index = wrk - hcm->wrk;
378 if ((err = hcc_attach ()))
380 return clib_error_return (0, "http client attach: %U", format_clib_error,
384 if ((err = hcc_connect ()))
386 return clib_error_return (0, "http client connect: %U",
387 format_clib_error, err);
390 vlib_process_wait_for_event_or_clock (vm, 10);
391 event_type = vlib_process_get_events (vm, &event_data);
395 err = clib_error_return (0, "timeout");
398 case HCC_REPLY_RECEIVED:
399 vlib_cli_output (vm, "%v", hcm->http_response);
400 vec_free (hcm->http_response);
403 clib_error_return (0, "unexpected event %d", event_type);
408 vec_free (event_data);
415 hcc_main_t *hcm = &hcc_main;
416 vnet_app_detach_args_t _da, *da = &_da;
419 if (!hcm->test_client_attached)
422 da->app_index = hcm->app_index;
423 da->api_client_index = ~0;
424 rv = vnet_application_detach (da);
425 hcm->test_client_attached = 0;
431 static clib_error_t *
432 hcc_command_fn (vlib_main_t *vm, unformat_input_t *input,
433 vlib_cli_command_t *cmd)
435 unformat_input_t _line_input, *line_input = &_line_input;
436 hcc_main_t *hcm = &hcc_main;
439 clib_error_t *err = 0;
442 hcm->prealloc_fifos = 0;
443 hcm->private_segment_size = 0;
446 if (hcm->test_client_attached)
447 return clib_error_return (0, "failed: already running!");
449 /* Get a line of input. */
450 if (!unformat_user (input, unformat_line_input, line_input))
451 return clib_error_return (0, "expected URI");
453 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
455 if (unformat (line_input, "prealloc-fifos %d", &hcm->prealloc_fifos))
457 else if (unformat (line_input, "private-segment-size %U",
458 unformat_memory_size, &seg_size))
459 hcm->private_segment_size = seg_size;
460 else if (unformat (line_input, "fifo-size %d", &hcm->fifo_size))
461 hcm->fifo_size <<= 10;
462 else if (unformat (line_input, "uri %s", &hcm->uri))
464 else if (unformat (line_input, "appns %_%v%_", &appns_id))
466 else if (unformat (line_input, "secret %lu", &hcm->appns_secret))
468 else if (unformat (line_input, "query %s", &hcm->http_query))
472 err = clib_error_return (0, "unknown input `%U'",
473 format_unformat_error, line_input);
478 vec_free (hcm->appns_id);
479 hcm->appns_id = appns_id;
480 hcm->cli_node_index = vlib_get_current_process (vm)->node_runtime.node_index;
484 err = clib_error_return (0, "URI not defined");
488 if ((rv = parse_uri ((char *) hcm->uri, &hcm->connect_sep)))
490 err = clib_error_return (0, "Uri parse error: %d", rv);
494 vlib_worker_thread_barrier_sync (vm);
495 vnet_session_enable_disable (vm, 1 /* turn on TCP, etc. */);
496 vlib_worker_thread_barrier_release (vm);
502 /* don't override last error */
504 err = clib_error_return (0, "failed: app detach");
505 clib_warning ("WARNING: app detach failed...");
510 vec_free (hcm->http_query);
511 unformat_free (line_input);
515 VLIB_CLI_COMMAND (hcc_command, static) = {
516 .path = "http cli client",
517 .short_help = "[appns <app-ns> secret <appns-secret>] uri http://<ip-addr> "
518 "query <query-string>",
519 .function = hcc_command_fn,
523 static clib_error_t *
524 hcc_main_init (vlib_main_t *vm)
526 hcc_main_t *hcm = &hcc_main;
533 VLIB_INIT_FUNCTION (hcc_main_init);
536 * fd.io coding-style-patch-verification: ON
539 * eval: (c-set-style "gnu")