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;
310 static clib_error_t *
313 vnet_connect_args_t _a = {}, *a = &_a;
314 hcc_main_t *hcm = &hcc_main;
318 clib_memcpy (&a->sep_ext, &hcm->connect_sep, sizeof (hcm->connect_sep));
319 a->app_index = hcm->app_index;
321 /* allocate http session on main thread */
322 wrk = hcc_worker_get (0);
323 hs = hcc_session_alloc (wrk);
324 a->api_context = hs->session_index;
326 int rv = vnet_connect (a);
329 return clib_error_return (0, "connect returned: %U", format_session_error,
334 static clib_error_t *
335 hcc_run (vlib_main_t *vm)
337 vlib_thread_main_t *vtm = vlib_get_thread_main ();
338 hcc_main_t *hcm = &hcc_main;
339 uword event_type, *event_data = 0;
341 clib_error_t *err = 0;
344 num_threads = 1 /* main thread */ + vtm->n_threads;
345 vec_validate (hcm->wrk, num_threads);
346 vec_foreach (wrk, hcm->wrk)
348 wrk->thread_index = wrk - hcm->wrk;
351 if ((err = hcc_attach ()))
353 return clib_error_return (0, "http client attach: %U", format_clib_error,
357 if ((err = hcc_connect ()))
359 return clib_error_return (0, "http client connect: %U",
360 format_clib_error, err);
363 vlib_process_wait_for_event_or_clock (vm, 10);
364 event_type = vlib_process_get_events (vm, &event_data);
368 err = clib_error_return (0, "timeout");
371 case HCC_REPLY_RECEIVED:
372 vlib_cli_output (vm, "%v", hcm->http_response);
373 vec_free (hcm->http_response);
376 clib_error_return (0, "unexpected event %d", event_type);
381 vec_free (event_data);
388 hcc_main_t *hcm = &hcc_main;
389 vnet_app_detach_args_t _da, *da = &_da;
392 if (!hcm->test_client_attached)
395 da->app_index = hcm->app_index;
396 da->api_client_index = ~0;
397 rv = vnet_application_detach (da);
398 hcm->test_client_attached = 0;
404 static clib_error_t *
405 hcc_command_fn (vlib_main_t *vm, unformat_input_t *input,
406 vlib_cli_command_t *cmd)
408 unformat_input_t _line_input, *line_input = &_line_input;
409 hcc_main_t *hcm = &hcc_main;
411 clib_error_t *err = 0;
414 hcm->prealloc_fifos = 0;
415 hcm->private_segment_size = 0;
418 if (hcm->test_client_attached)
419 return clib_error_return (0, "failed: already running!");
421 /* Get a line of input. */
422 if (!unformat_user (input, unformat_line_input, line_input))
423 return clib_error_return (0, "expected URI");
425 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
427 if (unformat (line_input, "prealloc-fifos %d", &hcm->prealloc_fifos))
429 else if (unformat (line_input, "private-segment-size %U",
430 unformat_memory_size, &seg_size))
431 hcm->private_segment_size = seg_size;
432 else if (unformat (line_input, "fifo-size %d", &hcm->fifo_size))
433 hcm->fifo_size <<= 10;
434 else if (unformat (line_input, "uri %s", &hcm->uri))
436 else if (unformat (line_input, "query %s", &hcm->http_query))
440 err = clib_error_return (0, "unknown input `%U'",
441 format_unformat_error, line_input);
446 hcm->cli_node_index = vlib_get_current_process (vm)->node_runtime.node_index;
450 err = clib_error_return (0, "URI not defined");
454 if ((rv = parse_uri ((char *) hcm->uri, &hcm->connect_sep)))
456 err = clib_error_return (0, "Uri parse error: %d", rv);
460 vnet_session_enable_disable (vm, 1 /* turn on TCP, etc. */);
466 /* don't override last error */
468 err = clib_error_return (0, "failed: app detach");
469 clib_warning ("WARNING: app detach failed...");
474 vec_free (hcm->http_query);
475 unformat_free (line_input);
479 VLIB_CLI_COMMAND (hcc_command, static) = {
480 .path = "http cli client",
481 .short_help = "uri http://<ip-addr> query <query-string>",
482 .function = hcc_command_fn,
485 static clib_error_t *
486 hcc_main_init (vlib_main_t *vm)
488 hcc_main_t *hcm = &hcc_main;
495 VLIB_INIT_FUNCTION (hcc_main_init);
498 * fd.io coding-style-patch-verification: ON
501 * eval: (c-set-style "gnu")