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;
60 HCC_REPLY_RECEIVED = 100,
63 static hcc_main_t hcc_main;
66 hcc_worker_get (u32 thread_index)
68 return vec_elt_at_index (hcc_main.wrk, thread_index);
71 static hcc_session_t *
72 hcc_session_alloc (hcc_worker_t *wrk)
75 pool_get_zero (wrk->sessions, hs);
76 hs->session_index = hs - wrk->sessions;
77 hs->thread_index = wrk->thread_index;
81 static hcc_session_t *
82 hcc_session_get (u32 hs_index, u32 thread_index)
84 hcc_worker_t *wrk = hcc_worker_get (thread_index);
85 return pool_elt_at_index (wrk->sessions, hs_index);
89 hcc_session_free (u32 thread_index, hcc_session_t *hs)
91 hcc_worker_t *wrk = hcc_worker_get (thread_index);
92 pool_put (wrk->sessions, hs);
96 hcc_ts_accept_callback (session_t *ts)
103 hcc_ts_disconnect_callback (session_t *s)
105 hcc_main_t *hcm = &hcc_main;
106 vnet_disconnect_args_t _a = { 0 }, *a = &_a;
108 a->handle = session_handle (s);
109 a->app_index = hcm->app_index;
110 vnet_disconnect_session (a);
114 hcc_ts_connected_callback (u32 app_index, u32 hc_index, session_t *as,
117 hcc_main_t *hcm = &hcc_main;
118 hcc_session_t *hs, *new_hs;
125 clib_warning ("connected error: hc_index(%d): %U", hc_index,
126 format_session_error, err);
130 /* TODO delete half open session once the support is added in http layer */
131 hs = hcc_session_get (hc_index, 0);
132 wrk = hcc_worker_get (as->thread_index);
133 new_hs = hcc_session_alloc (wrk);
134 clib_memcpy_fast (new_hs, hs, sizeof (*hs));
136 hs->vpp_session_index = as->session_index;
138 msg.type = HTTP_MSG_REQUEST;
139 msg.method_type = HTTP_REQ_GET;
140 msg.content_type = HTTP_CONTENT_TEXT_HTML;
141 msg.data.type = HTTP_MSG_DATA_INLINE;
142 msg.data.len = vec_len (hcm->http_query);
144 svm_fifo_seg_t segs[2] = { { (u8 *) &msg, sizeof (msg) },
145 { hcm->http_query, vec_len (hcm->http_query) } };
147 rv = svm_fifo_enqueue_segments (as->tx_fifo, segs, 2, 0 /* allow partial */);
148 if (rv < 0 || rv != sizeof (msg) + vec_len (hcm->http_query))
150 clib_warning ("failed app enqueue");
154 if (svm_fifo_set_event (as->tx_fifo))
155 session_send_io_evt_to_thread (as->tx_fifo, SESSION_IO_EVT_TX);
161 hcc_ts_reset_callback (session_t *s)
163 hcc_main_t *hcm = &hcc_main;
165 vnet_disconnect_args_t _a = { 0 }, *a = &_a;
167 hs = hcc_session_get (s->opaque, s->thread_index);
170 a->handle = session_handle (s);
171 a->app_index = hcm->app_index;
172 vnet_disconnect_session (a);
176 hcc_ts_tx_callback (session_t *ts)
178 clib_warning ("bug");
183 hcc_session_disconnect (session_t *s)
185 hcc_main_t *hcm = &hcc_main;
186 vnet_disconnect_args_t _a = { 0 }, *a = &_a;
187 a->handle = session_handle (s);
188 a->app_index = hcm->app_index;
189 vnet_disconnect_session (a);
193 hcc_ts_rx_callback (session_t *ts)
195 hcc_main_t *hcm = &hcc_main;
200 hs = hcc_session_get (ts->opaque, ts->thread_index);
204 clib_warning ("session is closed");
210 rv = svm_fifo_dequeue (ts->rx_fifo, sizeof (msg), (u8 *) &msg);
211 ASSERT (rv == sizeof (msg));
213 if (msg.type != HTTP_MSG_REPLY || msg.code != HTTP_STATUS_OK)
215 clib_warning ("unexpected msg type %d", msg.type);
218 vec_validate (hcm->http_response, msg.data.len - 1);
219 vec_reset_length (hcm->http_response);
220 hs->to_recv = msg.data.len;
223 u32 max_deq = svm_fifo_max_dequeue (ts->rx_fifo);
225 u32 n_deq = clib_min (hs->to_recv, max_deq);
226 u32 curr = vec_len (hcm->http_response);
227 rv = svm_fifo_dequeue (ts->rx_fifo, n_deq, hcm->http_response + curr);
230 clib_warning ("app dequeue failed");
237 vec_set_len (hcm->http_response, curr + n_deq);
238 ASSERT (hs->to_recv >= rv);
241 if (hs->to_recv == 0)
243 hcc_session_disconnect (ts);
244 vlib_process_signal_event_mt (hcm->vlib_main, hcm->cli_node_index,
245 HCC_REPLY_RECEIVED, 0);
252 hcc_ts_cleanup_callback (session_t *s, session_cleanup_ntf_t ntf)
256 hs = hcc_session_get (s->thread_index, s->opaque);
260 hcc_session_free (s->thread_index, hs);
263 static session_cb_vft_t hcc_session_cb_vft = {
264 .session_accept_callback = hcc_ts_accept_callback,
265 .session_disconnect_callback = hcc_ts_disconnect_callback,
266 .session_connected_callback = hcc_ts_connected_callback,
267 .builtin_app_rx_callback = hcc_ts_rx_callback,
268 .builtin_app_tx_callback = hcc_ts_tx_callback,
269 .session_reset_callback = hcc_ts_reset_callback,
270 .session_cleanup_callback = hcc_ts_cleanup_callback,
273 static clib_error_t *
276 hcc_main_t *hcm = &hcc_main;
277 vnet_app_attach_args_t _a, *a = &_a;
279 u32 segment_size = 128 << 20;
282 if (hcm->private_segment_size)
283 segment_size = hcm->private_segment_size;
285 clib_memset (a, 0, sizeof (*a));
286 clib_memset (options, 0, sizeof (options));
288 a->api_client_index = ~0;
289 a->name = format (0, "http_cli_client");
290 a->session_cb_vft = &hcc_session_cb_vft;
291 a->options = options;
292 a->options[APP_OPTIONS_SEGMENT_SIZE] = segment_size;
293 a->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = segment_size;
294 a->options[APP_OPTIONS_RX_FIFO_SIZE] =
295 hcm->fifo_size ? hcm->fifo_size : 8 << 10;
296 a->options[APP_OPTIONS_TX_FIFO_SIZE] =
297 hcm->fifo_size ? hcm->fifo_size : 32 << 10;
298 a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
299 a->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = hcm->prealloc_fifos;
301 if ((rv = vnet_application_attach (a)))
302 return clib_error_return (0, "attach returned %d", rv);
304 hcm->app_index = a->app_index;
306 hcm->test_client_attached = 1;
311 hcc_connect_rpc (void *rpc_args)
313 vnet_connect_args_t *a = rpc_args;
316 rv = vnet_connect (a);
318 clib_warning (0, "connect returned: %U", format_session_error, rv);
325 hcc_program_connect (vnet_connect_args_t *a)
327 session_send_rpc_evt_to_thread_force (transport_cl_thread (),
331 static clib_error_t *
334 vnet_connect_args_t *a = 0;
335 hcc_main_t *hcm = &hcc_main;
340 clib_memset (a, 0, sizeof (a[0]));
342 clib_memcpy (&a->sep_ext, &hcm->connect_sep, sizeof (hcm->connect_sep));
343 a->app_index = hcm->app_index;
345 /* allocate http session on main thread */
346 wrk = hcc_worker_get (0);
347 hs = hcc_session_alloc (wrk);
348 a->api_context = hs->session_index;
350 hcc_program_connect (a);
354 static clib_error_t *
355 hcc_run (vlib_main_t *vm)
357 vlib_thread_main_t *vtm = vlib_get_thread_main ();
358 hcc_main_t *hcm = &hcc_main;
359 uword event_type, *event_data = 0;
361 clib_error_t *err = 0;
364 num_threads = 1 /* main thread */ + vtm->n_threads;
365 vec_validate (hcm->wrk, num_threads);
366 vec_foreach (wrk, hcm->wrk)
368 wrk->thread_index = wrk - hcm->wrk;
371 if ((err = hcc_attach ()))
373 return clib_error_return (0, "http client attach: %U", format_clib_error,
377 if ((err = hcc_connect ()))
379 return clib_error_return (0, "http client connect: %U",
380 format_clib_error, err);
383 vlib_process_wait_for_event_or_clock (vm, 10);
384 event_type = vlib_process_get_events (vm, &event_data);
388 err = clib_error_return (0, "timeout");
391 case HCC_REPLY_RECEIVED:
392 vlib_cli_output (vm, "%v", hcm->http_response);
393 vec_free (hcm->http_response);
396 clib_error_return (0, "unexpected event %d", event_type);
401 vec_free (event_data);
408 hcc_main_t *hcm = &hcc_main;
409 vnet_app_detach_args_t _da, *da = &_da;
412 if (!hcm->test_client_attached)
415 da->app_index = hcm->app_index;
416 da->api_client_index = ~0;
417 rv = vnet_application_detach (da);
418 hcm->test_client_attached = 0;
424 static clib_error_t *
425 hcc_command_fn (vlib_main_t *vm, unformat_input_t *input,
426 vlib_cli_command_t *cmd)
428 unformat_input_t _line_input, *line_input = &_line_input;
429 hcc_main_t *hcm = &hcc_main;
431 clib_error_t *err = 0;
434 hcm->prealloc_fifos = 0;
435 hcm->private_segment_size = 0;
438 if (hcm->test_client_attached)
439 return clib_error_return (0, "failed: already running!");
441 /* Get a line of input. */
442 if (!unformat_user (input, unformat_line_input, line_input))
443 return clib_error_return (0, "expected URI");
445 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
447 if (unformat (line_input, "prealloc-fifos %d", &hcm->prealloc_fifos))
449 else if (unformat (line_input, "private-segment-size %U",
450 unformat_memory_size, &seg_size))
451 hcm->private_segment_size = seg_size;
452 else if (unformat (line_input, "fifo-size %d", &hcm->fifo_size))
453 hcm->fifo_size <<= 10;
454 else if (unformat (line_input, "uri %s", &hcm->uri))
456 else if (unformat (line_input, "query %s", &hcm->http_query))
460 err = clib_error_return (0, "unknown input `%U'",
461 format_unformat_error, line_input);
466 hcm->cli_node_index = vlib_get_current_process (vm)->node_runtime.node_index;
470 err = clib_error_return (0, "URI not defined");
474 if ((rv = parse_uri ((char *) hcm->uri, &hcm->connect_sep)))
476 err = clib_error_return (0, "Uri parse error: %d", rv);
480 vlib_worker_thread_barrier_sync (vm);
481 vnet_session_enable_disable (vm, 1 /* turn on TCP, etc. */);
482 vlib_worker_thread_barrier_release (vm);
488 /* don't override last error */
490 err = clib_error_return (0, "failed: app detach");
491 clib_warning ("WARNING: app detach failed...");
496 vec_free (hcm->http_query);
497 unformat_free (line_input);
501 VLIB_CLI_COMMAND (hcc_command, static) = {
502 .path = "http cli client",
503 .short_help = "uri http://<ip-addr> query <query-string>",
504 .function = hcc_command_fn,
508 static clib_error_t *
509 hcc_main_init (vlib_main_t *vm)
511 hcc_main_t *hcm = &hcc_main;
518 VLIB_INIT_FUNCTION (hcc_main_init);
521 * fd.io coding-style-patch-verification: ON
524 * eval: (c-set-style "gnu")