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 <http/http.h>
17 #include <vnet/session/session.h>
18 #include <http/http_timer.h>
20 static http_main_t http_main;
22 #define HTTP_FIFO_THRESH (16 << 10)
23 #define CONTENT_LEN_STR "Content-Length: "
25 /* HTTP state machine result */
26 typedef enum http_sm_result_t_
33 const char *http_status_code_str[] = {
34 #define _(c, s, str) str,
35 foreach_http_status_code
39 const char *http_content_type_str[] = {
40 #define _(s, ext, str) str,
41 foreach_http_content_type
45 const http_buffer_type_t msg_to_buf_type[] = {
46 [HTTP_MSG_DATA_INLINE] = HTTP_BUFFER_FIFO,
47 [HTTP_MSG_DATA_PTR] = HTTP_BUFFER_PTR,
51 format_http_state (u8 *s, va_list *va)
53 http_state_t state = va_arg (*va, http_state_t);
58 return format (s, "idle");
59 case HTTP_STATE_WAIT_APP_METHOD:
60 return format (s, "wait app method");
61 case HTTP_STATE_WAIT_SERVER_REPLY:
62 return format (s, "wait server reply");
63 case HTTP_STATE_CLIENT_IO_MORE_DATA:
64 return format (s, "client io more data");
65 case HTTP_STATE_WAIT_CLIENT_METHOD:
66 return format (s, "wait client method");
67 case HTTP_STATE_WAIT_APP_REPLY:
68 return format (s, "wait app reply");
69 case HTTP_STATE_APP_IO_MORE_DATA:
70 return format (s, "app io more data");
74 return format (s, "unknown");
77 #define http_state_change(_hc, _state) \
80 HTTP_DBG (1, "changing http state %U -> %U", format_http_state, \
81 (_hc)->http_state, format_http_state, _state); \
82 (_hc)->http_state = _state; \
86 static inline http_worker_t *
87 http_worker_get (u32 thread_index)
89 return &http_main.wrk[thread_index];
93 http_conn_alloc_w_thread (u32 thread_index)
95 http_worker_t *wrk = http_worker_get (thread_index);
98 pool_get_aligned_safe (wrk->conn_pool, hc, CLIB_CACHE_LINE_BYTES);
99 clib_memset (hc, 0, sizeof (*hc));
100 hc->c_thread_index = thread_index;
101 hc->h_hc_index = hc - wrk->conn_pool;
102 hc->h_pa_session_handle = SESSION_INVALID_HANDLE;
103 hc->h_tc_session_handle = SESSION_INVALID_HANDLE;
104 return hc->h_hc_index;
107 static inline http_conn_t *
108 http_conn_get_w_thread (u32 hc_index, u32 thread_index)
110 http_worker_t *wrk = http_worker_get (thread_index);
111 return pool_elt_at_index (wrk->conn_pool, hc_index);
115 http_conn_free (http_conn_t *hc)
117 http_worker_t *wrk = http_worker_get (hc->c_thread_index);
118 pool_put (wrk->conn_pool, hc);
122 http_listener_alloc (void)
124 http_main_t *hm = &http_main;
127 pool_get_zero (hm->listener_pool, lhc);
128 lhc->c_c_index = lhc - hm->listener_pool;
129 return lhc->c_c_index;
133 http_listener_get (u32 lhc_index)
135 return pool_elt_at_index (http_main.listener_pool, lhc_index);
139 http_listener_free (http_conn_t *lhc)
141 http_main_t *hm = &http_main;
143 vec_free (lhc->app_name);
145 memset (lhc, 0xfc, sizeof (*lhc));
146 pool_put (hm->listener_pool, lhc);
150 http_disconnect_transport (http_conn_t *hc)
152 vnet_disconnect_args_t a = {
153 .handle = hc->h_tc_session_handle,
154 .app_index = http_main.app_index,
157 hc->state = HTTP_CONN_STATE_CLOSED;
159 if (vnet_disconnect_session (&a))
160 clib_warning ("disconnect returned");
164 http_conn_timeout_cb (void *hc_handlep)
169 hs_handle = pointer_to_uword (hc_handlep);
170 hc = http_conn_get_w_thread (hs_handle & 0x00FFFFFF, hs_handle >> 24);
172 HTTP_DBG (1, "terminate thread %d index %d hs %llx", hs_handle >> 24,
173 hs_handle & 0x00FFFFFF, hc);
177 hc->timer_handle = ~0;
178 session_transport_closing_notify (&hc->connection);
179 http_disconnect_transport (hc);
183 http_ts_accept_callback (session_t *ts)
185 session_t *ts_listener, *as, *asl;
186 app_worker_t *app_wrk;
187 http_conn_t *lhc, *hc;
188 u32 hc_index, thresh;
191 ts_listener = listen_session_get_from_handle (ts->listener_handle);
192 lhc = http_listener_get (ts_listener->opaque);
194 hc_index = http_conn_alloc_w_thread (ts->thread_index);
195 hc = http_conn_get_w_thread (hc_index, ts->thread_index);
196 clib_memcpy_fast (hc, lhc, sizeof (*lhc));
197 hc->c_thread_index = ts->thread_index;
198 hc->h_hc_index = hc_index;
200 hc->h_tc_session_handle = session_handle (ts);
201 hc->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
203 hc->state = HTTP_CONN_STATE_ESTABLISHED;
204 http_state_change (hc, HTTP_STATE_WAIT_CLIENT_METHOD);
206 ts->session_state = SESSION_STATE_READY;
207 ts->opaque = hc_index;
210 * Alloc session and initialize
212 as = session_alloc (hc->c_thread_index);
213 hc->c_s_index = as->session_index;
215 as->app_wrk_index = hc->h_pa_wrk_index;
216 as->connection_index = hc->c_c_index;
217 as->session_state = SESSION_STATE_ACCEPTING;
219 asl = listen_session_get_from_handle (lhc->h_pa_session_handle);
220 as->session_type = asl->session_type;
221 as->listener_handle = lhc->h_pa_session_handle;
224 * Init session fifos and notify app
226 if ((rv = app_worker_init_accepted (as)))
228 HTTP_DBG (1, "failed to allocate fifos");
233 hc->h_pa_session_handle = session_handle (as);
234 hc->h_pa_wrk_index = as->app_wrk_index;
235 app_wrk = app_worker_get (as->app_wrk_index);
237 HTTP_DBG (1, "Accepted on listener %u new connection [%u]%x",
238 ts_listener->opaque, vlib_get_thread_index (), hc_index);
240 if ((rv = app_worker_accept_notify (app_wrk, as)))
242 HTTP_DBG (0, "app accept returned");
247 /* Avoid enqueuing small chunks of data on transport tx notifications. If
248 * the fifo is small (under 16K) we set the threshold to it's size, meaning
249 * a notification will be given when the fifo empties.
251 ts = session_get_from_handle (hc->h_tc_session_handle);
252 thresh = clib_min (svm_fifo_size (ts->tx_fifo), HTTP_FIFO_THRESH);
253 svm_fifo_set_deq_thresh (ts->tx_fifo, thresh);
255 http_conn_timer_start (hc);
261 http_ts_connected_callback (u32 http_app_index, u32 ho_hc_index, session_t *ts,
266 http_conn_t *hc, *ho_hc;
267 app_worker_t *app_wrk;
272 clib_warning ("ERROR: %d", err);
276 new_hc_index = http_conn_alloc_w_thread (ts->thread_index);
277 hc = http_conn_get_w_thread (new_hc_index, ts->thread_index);
278 ho_hc = http_conn_get_w_thread (ho_hc_index, 0);
280 ASSERT (ho_hc->state == HTTP_CONN_STATE_CONNECTING);
282 clib_memcpy_fast (hc, ho_hc, sizeof (*hc));
284 hc->c_thread_index = ts->thread_index;
285 hc->h_tc_session_handle = session_handle (ts);
286 hc->c_c_index = new_hc_index;
287 hc->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
288 hc->state = HTTP_CONN_STATE_ESTABLISHED;
289 http_state_change (hc, HTTP_STATE_WAIT_APP_METHOD);
291 ts->session_state = SESSION_STATE_READY;
292 ts->opaque = new_hc_index;
294 /* allocate app session and initialize */
296 as = session_alloc (hc->c_thread_index);
297 hc->c_s_index = as->session_index;
298 as->connection_index = hc->c_c_index;
299 as->app_wrk_index = hc->h_pa_wrk_index;
300 as->session_state = SESSION_STATE_READY;
301 as->opaque = hc->h_pa_app_api_ctx;
302 as->session_type = session_type_from_proto_and_ip (
303 TRANSPORT_PROTO_HTTP, session_type_is_ip4 (ts->session_type));
305 HTTP_DBG (1, "half-open hc index %d, hc index %d", ho_hc_index,
308 app_wrk = app_worker_get (hc->h_pa_wrk_index);
311 clib_warning ("no app worker");
315 if ((rv = app_worker_init_connected (app_wrk, as)))
317 HTTP_DBG (1, "failed to allocate fifos");
321 app_worker_connect_notify (app_wrk, as, err, hc->h_pa_app_api_ctx);
322 hc->h_pa_session_handle = session_handle (as);
323 http_conn_timer_start (hc);
329 http_ts_disconnect_callback (session_t *ts)
333 hc = http_conn_get_w_thread (ts->opaque, ts->thread_index);
335 if (hc->state < HTTP_CONN_STATE_TRANSPORT_CLOSED)
336 hc->state = HTTP_CONN_STATE_TRANSPORT_CLOSED;
338 /* Nothing more to rx, propagate to app */
339 if (!svm_fifo_max_dequeue_cons (ts->rx_fifo))
340 session_transport_closing_notify (&hc->connection);
344 http_ts_reset_callback (session_t *ts)
348 hc = http_conn_get_w_thread (ts->opaque, ts->thread_index);
350 hc->state = HTTP_CONN_STATE_CLOSED;
351 http_buffer_free (&hc->tx_buf);
352 http_state_change (hc, HTTP_STATE_WAIT_CLIENT_METHOD);
353 session_transport_reset_notify (&hc->connection);
355 http_disconnect_transport (hc);
359 * http error boilerplate
361 static const char *http_error_template = "HTTP/1.1 %s\r\n"
363 "Content-Type: text/html\r\n"
364 "Connection: close\r\n"
365 "Pragma: no-cache\r\n"
366 "Content-Length: 0\r\n\r\n";
368 static const char *http_redirect_template = "HTTP/1.1 %s\r\n";
371 * http response boilerplate
373 static const char *http_response_template = "HTTP/1.1 %s\r\n"
375 "Expires: %U GMT\r\n"
377 "Content-Type: %s\r\n"
378 "Content-Length: %lu\r\n\r\n";
380 static const char *http_request_template = "GET %s HTTP/1.1\r\n"
381 "User-Agent: VPP HTTP client\r\n"
385 http_send_data (http_conn_t *hc, u8 *data, u32 length, u32 offset)
387 const u32 max_burst = 64 << 10;
392 ts = session_get_from_handle (hc->h_tc_session_handle);
394 to_send = clib_min (length - offset, max_burst);
395 sent = svm_fifo_enqueue (ts->tx_fifo, to_send, data + offset);
400 if (svm_fifo_set_event (ts->tx_fifo))
401 session_send_io_evt_to_thread (ts->tx_fifo, SESSION_IO_EVT_TX);
403 return (offset + sent);
407 http_send_error (http_conn_t *hc, http_status_code_t ec)
409 http_main_t *hm = &http_main;
413 if (ec >= HTTP_N_STATUS)
414 ec = HTTP_STATUS_INTERNAL_ERROR;
416 now = clib_timebase_now (&hm->timebase);
417 data = format (0, http_error_template, http_status_code_str[ec],
418 format_clib_timebase_time, now);
419 http_send_data (hc, data, vec_len (data), 0);
424 http_read_message (http_conn_t *hc)
426 u32 max_deq, cursize;
430 ts = session_get_from_handle (hc->h_tc_session_handle);
432 cursize = vec_len (hc->rx_buf);
433 max_deq = svm_fifo_max_dequeue (ts->rx_fifo);
434 if (PREDICT_FALSE (max_deq == 0))
437 vec_validate (hc->rx_buf, cursize + max_deq - 1);
438 n_read = svm_fifo_dequeue (ts->rx_fifo, max_deq, hc->rx_buf + cursize);
439 ASSERT (n_read == max_deq);
441 if (svm_fifo_is_empty (ts->rx_fifo))
442 svm_fifo_unset_event (ts->rx_fifo);
444 vec_set_len (hc->rx_buf, cursize + n_read);
449 v_find_index (u8 *vec, u32 offset, char *str)
451 int start_index = offset;
452 u32 slen = (u32) strnlen_s_inline (str, 16);
453 u32 vlen = vec_len (vec);
460 for (; start_index < (vlen - slen); start_index++)
462 if (!memcmp (vec + start_index, str, slen))
470 http_parse_header (http_conn_t *hc, int *content_length)
472 unformat_input_t input;
476 i = v_find_index (hc->rx_buf, hc->rx_buf_offset, CONTENT_LEN_STR);
479 clib_warning ("cannot find '%s' in the header!", CONTENT_LEN_STR);
483 hc->rx_buf_offset = i;
485 i = v_find_index (hc->rx_buf, hc->rx_buf_offset, "\n");
488 clib_warning ("end of line missing; incomplete data");
492 len = i - hc->rx_buf_offset;
493 line = vec_new (u8, len);
494 clib_memcpy (line, hc->rx_buf + hc->rx_buf_offset, len);
496 unformat_init_vector (&input, line);
497 if (!unformat (&input, CONTENT_LEN_STR "%d", content_length))
499 clib_warning ("failed to unformat content length!");
502 unformat_free (&input);
504 /* skip rest of the header */
505 hc->rx_buf_offset += len;
506 i = v_find_index (hc->rx_buf, hc->rx_buf_offset, "<html>");
509 clib_warning ("<html> tag not found");
512 hc->rx_buf_offset = i;
517 static http_sm_result_t
518 http_state_wait_server_reply (http_conn_t *hc, transport_send_params_t *sp)
520 int i, rv, content_length;
522 app_worker_t *app_wrk;
524 http_status_code_t ec;
526 rv = http_read_message (hc);
528 /* Nothing yet, wait for data or timer expire */
532 if (vec_len (hc->rx_buf) < 8)
534 ec = HTTP_STATUS_BAD_REQUEST;
538 if ((i = v_find_index (hc->rx_buf, 0, "200 OK")) >= 0)
540 msg.type = HTTP_MSG_REPLY;
541 msg.content_type = HTTP_CONTENT_TEXT_HTML;
542 msg.code = HTTP_STATUS_OK;
543 msg.data.type = HTTP_MSG_DATA_INLINE;
546 rv = http_parse_header (hc, &content_length);
549 clib_warning ("failed to parse http reply");
550 session_transport_closing_notify (&hc->connection);
551 http_disconnect_transport (hc);
554 msg.data.len = content_length;
555 u32 dlen = vec_len (hc->rx_buf) - hc->rx_buf_offset;
556 as = session_get_from_handle (hc->h_pa_session_handle);
557 svm_fifo_seg_t segs[2] = { { (u8 *) &msg, sizeof (msg) },
558 { &hc->rx_buf[hc->rx_buf_offset], dlen } };
560 rv = svm_fifo_enqueue_segments (as->rx_fifo, segs, 2,
561 0 /* allow partial */);
564 clib_warning ("error enqueue");
565 return HTTP_SM_ERROR;
568 hc->rx_buf_offset += dlen;
569 hc->to_recv = content_length - dlen;
571 if (hc->rx_buf_offset == vec_len (hc->rx_buf))
573 vec_reset_length (hc->rx_buf);
574 hc->rx_buf_offset = 0;
577 if (hc->to_recv == 0)
579 hc->rx_buf_offset = 0;
580 vec_reset_length (hc->rx_buf);
581 http_state_change (hc, HTTP_STATE_WAIT_APP_METHOD);
585 http_state_change (hc, HTTP_STATE_CLIENT_IO_MORE_DATA);
588 app_wrk = app_worker_get_if_valid (as->app_wrk_index);
590 app_worker_rx_notify (app_wrk, as);
595 HTTP_DBG (0, "Unknown http method %v", hc->rx_buf);
596 ec = HTTP_STATUS_METHOD_NOT_ALLOWED;
602 http_send_error (hc, ec);
603 session_transport_closing_notify (&hc->connection);
604 http_disconnect_transport (hc);
606 return HTTP_SM_ERROR;
609 static http_sm_result_t
610 http_state_wait_client_method (http_conn_t *hc, transport_send_params_t *sp)
612 http_status_code_t ec;
613 app_worker_t *app_wrk;
620 rv = http_read_message (hc);
622 /* Nothing yet, wait for data or timer expire */
626 if (vec_len (hc->rx_buf) < 8)
628 ec = HTTP_STATUS_BAD_REQUEST;
632 if ((i = v_find_index (hc->rx_buf, 0, "GET ")) >= 0)
634 hc->method = HTTP_REQ_GET;
635 hc->rx_buf_offset = i + 5;
637 i = v_find_index (hc->rx_buf, hc->rx_buf_offset, "HTTP");
640 ec = HTTP_STATUS_BAD_REQUEST;
644 HTTP_DBG (0, "GET method %v", hc->rx_buf);
645 len = i - hc->rx_buf_offset - 1;
647 else if ((i = v_find_index (hc->rx_buf, 0, "POST ")) >= 0)
649 hc->method = HTTP_REQ_POST;
650 hc->rx_buf_offset = i + 6;
651 len = vec_len (hc->rx_buf) - hc->rx_buf_offset - 1;
652 HTTP_DBG (0, "POST method %v", hc->rx_buf);
656 HTTP_DBG (0, "Unknown http method %v", hc->rx_buf);
657 ec = HTTP_STATUS_METHOD_NOT_ALLOWED;
661 buf = &hc->rx_buf[hc->rx_buf_offset];
663 msg.type = HTTP_MSG_REQUEST;
664 msg.method_type = hc->method;
665 msg.content_type = HTTP_CONTENT_TEXT_HTML;
666 msg.data.type = HTTP_MSG_DATA_INLINE;
669 svm_fifo_seg_t segs[2] = { { (u8 *) &msg, sizeof (msg) }, { buf, len } };
671 as = session_get_from_handle (hc->h_pa_session_handle);
672 rv = svm_fifo_enqueue_segments (as->rx_fifo, segs, 2, 0 /* allow partial */);
673 if (rv < 0 || rv != sizeof (msg) + len)
675 clib_warning ("failed app enqueue");
676 /* This should not happen as we only handle 1 request per session,
677 * and fifo is allocated, but going forward we should consider
679 return HTTP_SM_ERROR;
682 vec_free (hc->rx_buf);
683 http_state_change (hc, HTTP_STATE_WAIT_APP_REPLY);
685 app_wrk = app_worker_get_if_valid (as->app_wrk_index);
687 app_worker_rx_notify (app_wrk, as);
693 http_send_error (hc, ec);
694 session_transport_closing_notify (&hc->connection);
695 http_disconnect_transport (hc);
697 return HTTP_SM_ERROR;
700 static http_sm_result_t
701 http_state_wait_app_reply (http_conn_t *hc, transport_send_params_t *sp)
703 http_main_t *hm = &http_main;
708 http_status_code_t sc;
712 as = session_get_from_handle (hc->h_pa_session_handle);
714 rv = svm_fifo_dequeue (as->tx_fifo, sizeof (msg), (u8 *) &msg);
715 ASSERT (rv == sizeof (msg));
717 if (msg.data.type > HTTP_MSG_DATA_PTR)
719 clib_warning ("no data");
720 sc = HTTP_STATUS_INTERNAL_ERROR;
724 if (msg.type != HTTP_MSG_REPLY)
726 clib_warning ("unexpected message type %d", msg.type);
727 sc = HTTP_STATUS_INTERNAL_ERROR;
731 http_buffer_init (&hc->tx_buf, msg_to_buf_type[msg.data.type], as->tx_fifo,
735 * Add headers. For now:
742 now = clib_timebase_now (&hm->timebase);
748 format (0, http_response_template, http_status_code_str[msg.code],
750 format_clib_timebase_time, now,
752 format_clib_timebase_time, now + 600.0,
756 http_content_type_str[msg.content_type],
760 case HTTP_STATUS_MOVED:
762 format (0, http_redirect_template, http_status_code_str[msg.code]);
763 /* Location: http(s)://new-place already queued up as data */
766 return HTTP_SM_ERROR;
769 offset = http_send_data (hc, header, vec_len (header), 0);
770 if (offset != vec_len (header))
772 clib_warning ("couldn't send response header!");
773 sc = HTTP_STATUS_INTERNAL_ERROR;
779 /* Start sending the actual data */
780 http_state_change (hc, HTTP_STATE_APP_IO_MORE_DATA);
782 ASSERT (sp->max_burst_size >= offset);
783 sp->max_burst_size -= offset;
784 return HTTP_SM_CONTINUE;
787 clib_warning ("unexpected msg type from app %u", msg.type);
788 http_send_error (hc, sc);
789 http_state_change (hc, HTTP_STATE_WAIT_CLIENT_METHOD);
790 session_transport_closing_notify (&hc->connection);
791 http_disconnect_transport (hc);
795 static http_sm_result_t
796 http_state_wait_app_method (http_conn_t *hc, transport_send_params_t *sp)
800 u8 *buf = 0, *request;
804 as = session_get_from_handle (hc->h_pa_session_handle);
806 rv = svm_fifo_dequeue (as->tx_fifo, sizeof (msg), (u8 *) &msg);
807 ASSERT (rv == sizeof (msg));
809 if (msg.data.type > HTTP_MSG_DATA_PTR)
811 clib_warning ("no data");
815 if (msg.type != HTTP_MSG_REQUEST)
817 clib_warning ("unexpected message type %d", msg.type);
821 vec_validate (buf, msg.data.len - 1);
822 rv = svm_fifo_dequeue (as->tx_fifo, msg.data.len, buf);
823 ASSERT (rv == msg.data.len);
825 request = format (0, http_request_template, buf);
826 offset = http_send_data (hc, request, vec_len (request), 0);
827 if (offset != vec_len (request))
829 clib_warning ("sending request failed!");
833 http_state_change (hc, HTTP_STATE_WAIT_SERVER_REPLY);
841 session_transport_closing_notify (&hc->connection);
842 http_disconnect_transport (hc);
843 return HTTP_SM_ERROR;
846 static http_sm_result_t
847 http_state_client_io_more_data (http_conn_t *hc, transport_send_params_t *sp)
850 app_worker_t *app_wrk;
851 svm_fifo_seg_t _seg, *seg = &_seg;
852 u32 max_len, max_deq, max_enq, n_segs = 1;
855 as = session_get_from_handle (hc->h_pa_session_handle);
856 ts = session_get_from_handle (hc->h_tc_session_handle);
858 max_deq = svm_fifo_max_dequeue (ts->rx_fifo);
861 HTTP_DBG (1, "no data to deq");
865 max_enq = svm_fifo_max_enqueue (as->rx_fifo);
868 HTTP_DBG (1, "app's rx fifo full");
869 svm_fifo_add_want_deq_ntf (as->rx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
873 max_len = clib_min (max_enq, max_deq);
874 len = svm_fifo_segments (ts->rx_fifo, 0, seg, &n_segs, max_len);
877 HTTP_DBG (1, "svm_fifo_segments() len %d", len);
881 rv = svm_fifo_enqueue_segments (as->rx_fifo, seg, 1, 0 /* allow partial */);
884 clib_warning ("data enqueue failed, rv: %d", rv);
885 return HTTP_SM_ERROR;
888 svm_fifo_dequeue_drop (ts->rx_fifo, rv);
889 if (rv > hc->to_recv)
891 clib_warning ("http protocol error: received more data than expected");
892 session_transport_closing_notify (&hc->connection);
893 http_disconnect_transport (hc);
894 http_state_change (hc, HTTP_STATE_WAIT_APP_METHOD);
895 return HTTP_SM_ERROR;
898 HTTP_DBG (1, "drained %d from ts; remains %d", rv, hc->to_recv);
900 app_wrk = app_worker_get_if_valid (as->app_wrk_index);
902 app_worker_rx_notify (app_wrk, as);
904 if (svm_fifo_max_dequeue_cons (ts->rx_fifo))
905 session_enqueue_notify (ts);
910 static http_sm_result_t
911 http_state_app_io_more_data (http_conn_t *hc, transport_send_params_t *sp)
913 u32 max_send = 64 << 10, n_segs;
914 http_buffer_t *hb = &hc->tx_buf;
919 max_send = clib_min (max_send, sp->max_burst_size);
920 ts = session_get_from_handle (hc->h_tc_session_handle);
921 if ((seg = http_buffer_get_segs (hb, max_send, &n_segs)))
922 sent = svm_fifo_enqueue_segments (ts->tx_fifo, seg, n_segs,
923 1 /* allow partial */);
927 /* Ask scheduler to notify app of deq event if needed */
928 sp->bytes_dequeued += http_buffer_drain (hb, sent);
929 sp->max_burst_size -= sent;
932 /* Not finished sending all data */
933 if (!http_buffer_is_drained (hb))
935 if (sent && svm_fifo_set_event (ts->tx_fifo))
936 session_send_io_evt_to_thread (ts->tx_fifo, SESSION_IO_EVT_TX);
938 if (svm_fifo_max_enqueue (ts->tx_fifo) < HTTP_FIFO_THRESH)
940 /* Deschedule http session and wait for deq notification if
941 * underlying ts tx fifo almost full */
942 svm_fifo_add_want_deq_ntf (ts->tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
943 transport_connection_deschedule (&hc->connection);
944 sp->flags |= TRANSPORT_SND_F_DESCHED;
949 if (sent && svm_fifo_set_event (ts->tx_fifo))
950 session_send_io_evt_to_thread (ts->tx_fifo, SESSION_IO_EVT_TX_FLUSH);
952 /* Finished transaction, back to HTTP_STATE_WAIT_METHOD */
953 http_state_change (hc, HTTP_STATE_WAIT_CLIENT_METHOD);
954 http_buffer_free (&hc->tx_buf);
960 typedef http_sm_result_t (*http_sm_handler) (http_conn_t *,
961 transport_send_params_t *sp);
963 static http_sm_handler state_funcs[HTTP_N_STATES] = {
965 http_state_wait_app_method,
966 http_state_wait_client_method,
967 http_state_wait_server_reply,
968 http_state_wait_app_reply,
969 http_state_client_io_more_data,
970 http_state_app_io_more_data,
974 http_req_run_state_machine (http_conn_t *hc, transport_send_params_t *sp)
976 http_sm_result_t res;
980 res = state_funcs[hc->http_state](hc, sp);
981 if (res == HTTP_SM_ERROR)
983 HTTP_DBG (1, "error in state machine %d", res);
987 while (res == HTTP_SM_CONTINUE);
989 /* Reset the session expiration timer */
990 http_conn_timer_update (hc);
994 http_ts_rx_callback (session_t *ts)
998 hc = http_conn_get_w_thread (ts->opaque, ts->thread_index);
1001 clib_warning ("http connection not found (ts %d)", ts->opaque);
1005 if (hc->state == HTTP_CONN_STATE_CLOSED)
1007 svm_fifo_dequeue_drop_all (ts->tx_fifo);
1011 http_req_run_state_machine (hc, 0);
1013 if (hc->state == HTTP_CONN_STATE_TRANSPORT_CLOSED)
1015 if (!svm_fifo_max_dequeue_cons (ts->rx_fifo))
1016 session_transport_closing_notify (&hc->connection);
1022 http_ts_builtin_tx_callback (session_t *ts)
1026 hc = http_conn_get_w_thread (ts->opaque, ts->thread_index);
1027 transport_connection_reschedule (&hc->connection);
1033 http_ts_cleanup_callback (session_t *ts, session_cleanup_ntf_t ntf)
1037 if (ntf == SESSION_CLEANUP_TRANSPORT)
1040 hc = http_conn_get_w_thread (ts->opaque, ts->thread_index);
1043 clib_warning ("no http connection for %u", ts->session_index);
1047 vec_free (hc->rx_buf);
1049 http_buffer_free (&hc->tx_buf);
1050 http_conn_timer_stop (hc);
1052 session_transport_delete_notify (&hc->connection);
1053 http_conn_free (hc);
1057 http_add_segment_callback (u32 client_index, u64 segment_handle)
1059 /* No-op for builtin */
1064 http_del_segment_callback (u32 client_index, u64 segment_handle)
1069 static session_cb_vft_t http_app_cb_vft = {
1070 .session_accept_callback = http_ts_accept_callback,
1071 .session_disconnect_callback = http_ts_disconnect_callback,
1072 .session_connected_callback = http_ts_connected_callback,
1073 .session_reset_callback = http_ts_reset_callback,
1074 .session_cleanup_callback = http_ts_cleanup_callback,
1075 .add_segment_callback = http_add_segment_callback,
1076 .del_segment_callback = http_del_segment_callback,
1077 .builtin_app_rx_callback = http_ts_rx_callback,
1078 .builtin_app_tx_callback = http_ts_builtin_tx_callback,
1081 static clib_error_t *
1082 http_transport_enable (vlib_main_t *vm, u8 is_en)
1084 vnet_app_detach_args_t _da, *da = &_da;
1085 vnet_app_attach_args_t _a, *a = &_a;
1086 u64 options[APP_OPTIONS_N_OPTIONS];
1087 http_main_t *hm = &http_main;
1091 da->app_index = hm->app_index;
1092 da->api_client_index = APP_INVALID_INDEX;
1093 vnet_application_detach (da);
1097 vec_validate (hm->wrk, vlib_num_workers ());
1099 clib_memset (a, 0, sizeof (*a));
1100 clib_memset (options, 0, sizeof (options));
1102 a->session_cb_vft = &http_app_cb_vft;
1103 a->api_client_index = APP_INVALID_INDEX;
1104 a->options = options;
1105 a->name = format (0, "http");
1106 a->options[APP_OPTIONS_SEGMENT_SIZE] = hm->first_seg_size;
1107 a->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = hm->add_seg_size;
1108 a->options[APP_OPTIONS_RX_FIFO_SIZE] = hm->fifo_size;
1109 a->options[APP_OPTIONS_TX_FIFO_SIZE] = hm->fifo_size;
1110 a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
1111 a->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
1112 a->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_IS_TRANSPORT_APP;
1114 if (vnet_application_attach (a))
1115 return clib_error_return (0, "failed to attach http app");
1117 hm->app_index = a->app_index;
1120 clib_timebase_init (&hm->timebase, 0 /* GMT */, CLIB_TIMEBASE_DAYLIGHT_NONE,
1121 &vm->clib_time /* share the system clock */);
1123 http_timers_init (vm, http_conn_timeout_cb);
1129 http_transport_connect (transport_endpoint_cfg_t *tep)
1131 vnet_connect_args_t _cargs, *cargs = &_cargs;
1132 http_main_t *hm = &http_main;
1133 session_endpoint_cfg_t *sep = (session_endpoint_cfg_t *) tep;
1138 app_worker_t *app_wrk = app_worker_get (sep->app_wrk_index);
1140 clib_memset (cargs, 0, sizeof (*cargs));
1141 clib_memcpy (&cargs->sep_ext, sep, sizeof (session_endpoint_cfg_t));
1142 cargs->sep.transport_proto = TRANSPORT_PROTO_TCP;
1143 cargs->app_index = hm->app_index;
1144 app = application_get (app_wrk->app_index);
1145 cargs->sep_ext.ns_index = app->ns_index;
1147 hc_index = http_conn_alloc_w_thread (0 /* ts->thread_index */);
1148 hc = http_conn_get_w_thread (hc_index, 0);
1149 hc->h_pa_wrk_index = sep->app_wrk_index;
1150 hc->h_pa_app_api_ctx = sep->opaque;
1151 hc->state = HTTP_CONN_STATE_CONNECTING;
1152 cargs->api_context = hc_index;
1154 HTTP_DBG (1, "hc ho_index %x", hc_index);
1156 if ((error = vnet_connect (cargs)))
1163 http_start_listen (u32 app_listener_index, transport_endpoint_cfg_t *tep)
1165 vnet_listen_args_t _args = {}, *args = &_args;
1166 session_t *ts_listener, *app_listener;
1167 http_main_t *hm = &http_main;
1168 session_endpoint_cfg_t *sep;
1169 app_worker_t *app_wrk;
1170 transport_proto_t tp;
1176 sep = (session_endpoint_cfg_t *) tep;
1178 app_wrk = app_worker_get (sep->app_wrk_index);
1179 app = application_get (app_wrk->app_index);
1181 args->app_index = hm->app_index;
1182 args->sep_ext = *sep;
1183 args->sep_ext.ns_index = app->ns_index;
1184 tp = sep->ext_cfg ? TRANSPORT_PROTO_TLS : TRANSPORT_PROTO_TCP;
1185 args->sep_ext.transport_proto = tp;
1187 if (vnet_listen (args))
1188 return SESSION_INVALID_INDEX;
1190 lhc_index = http_listener_alloc ();
1191 lhc = http_listener_get (lhc_index);
1193 /* Grab transport connection listener and link to http listener */
1194 lhc->h_tc_session_handle = args->handle;
1195 al = app_listener_get_w_handle (lhc->h_tc_session_handle);
1196 ts_listener = app_listener_get_session (al);
1197 ts_listener->opaque = lhc_index;
1199 /* Grab application listener and link to http listener */
1200 app_listener = listen_session_get (app_listener_index);
1201 lhc->h_pa_wrk_index = sep->app_wrk_index;
1202 lhc->h_pa_session_handle = listen_session_get_handle (app_listener);
1203 lhc->c_s_index = app_listener_index;
1204 lhc->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
1206 if (vec_len (app->name))
1207 lhc->app_name = vec_dup (app->name);
1209 lhc->app_name = format (0, "VPP server app");
1215 http_stop_listen (u32 listener_index)
1220 lhc = http_listener_get (listener_index);
1222 vnet_unlisten_args_t a = {
1223 .handle = lhc->h_tc_session_handle,
1224 .app_index = http_main.app_index,
1225 .wrk_map_index = 0 /* default wrk */
1228 if ((rv = vnet_unlisten (&a)))
1229 clib_warning ("unlisten returned %d", rv);
1231 http_listener_free (lhc);
1237 http_transport_close (u32 hc_index, u32 thread_index)
1242 HTTP_DBG (1, "App disconnecting %x", hc_index);
1244 hc = http_conn_get_w_thread (hc_index, thread_index);
1245 if (hc->state == HTTP_CONN_STATE_CONNECTING)
1247 hc->state = HTTP_CONN_STATE_APP_CLOSED;
1248 http_disconnect_transport (hc);
1252 as = session_get_from_handle (hc->h_pa_session_handle);
1254 /* Nothing more to send, confirm close */
1255 if (!svm_fifo_max_dequeue_cons (as->tx_fifo))
1257 session_transport_closed_notify (&hc->connection);
1258 http_disconnect_transport (hc);
1262 /* Wait for all data to be written to ts */
1263 hc->state = HTTP_CONN_STATE_APP_CLOSED;
1267 static transport_connection_t *
1268 http_transport_get_connection (u32 hc_index, u32 thread_index)
1270 http_conn_t *hc = http_conn_get_w_thread (hc_index, thread_index);
1271 return &hc->connection;
1274 static transport_connection_t *
1275 http_transport_get_listener (u32 listener_index)
1277 http_conn_t *lhc = http_listener_get (listener_index);
1278 return &lhc->connection;
1282 http_app_tx_callback (void *session, transport_send_params_t *sp)
1284 session_t *as = (session_t *) session;
1285 u32 max_burst_sz, sent;
1288 HTTP_DBG (1, "app session conn index %x", as->connection_index);
1290 hc = http_conn_get_w_thread (as->connection_index, as->thread_index);
1291 if (!http_state_is_tx_valid (hc))
1293 if (hc->state != HTTP_CONN_STATE_CLOSED)
1294 clib_warning ("app data req state '%U' session state %u",
1295 format_http_state, hc->http_state, hc->state);
1296 svm_fifo_dequeue_drop_all (as->tx_fifo);
1300 max_burst_sz = sp->max_burst_size * TRANSPORT_PACER_MIN_MSS;
1301 sp->max_burst_size = max_burst_sz;
1303 http_req_run_state_machine (hc, sp);
1305 if (hc->state == HTTP_CONN_STATE_APP_CLOSED)
1307 if (!svm_fifo_max_dequeue_cons (as->tx_fifo))
1308 http_disconnect_transport (hc);
1311 sent = max_burst_sz - sp->max_burst_size;
1313 return sent > 0 ? clib_max (sent / TRANSPORT_PACER_MIN_MSS, 1) : 0;
1317 http_transport_get_endpoint (u32 hc_index, u32 thread_index,
1318 transport_endpoint_t *tep, u8 is_lcl)
1320 http_conn_t *hc = http_conn_get_w_thread (hc_index, thread_index);
1323 ts = session_get_from_handle (hc->h_tc_session_handle);
1324 session_get_endpoint (ts, tep, is_lcl);
1328 format_http_connection (u8 *s, va_list *args)
1330 http_conn_t *hc = va_arg (*args, http_conn_t *);
1333 ts = session_get_from_handle (hc->h_tc_session_handle);
1334 s = format (s, "[%d:%d][H] app_wrk %u ts %d:%d", hc->c_thread_index,
1335 hc->c_s_index, hc->h_pa_wrk_index, ts->thread_index,
1342 format_http_listener (u8 *s, va_list *args)
1344 http_conn_t *lhc = va_arg (*args, http_conn_t *);
1348 al = app_listener_get_w_handle (lhc->h_tc_session_handle);
1349 lts = app_listener_get_session (al);
1350 s = format (s, "[%d:%d][H] app_wrk %u ts %d:%d", lhc->c_thread_index,
1351 lhc->c_s_index, lhc->h_pa_wrk_index, lts->thread_index,
1352 lts->session_index);
1358 format_http_conn_state (u8 *s, va_list *args)
1360 http_conn_t *hc = va_arg (*args, http_conn_t *);
1364 case HTTP_CONN_STATE_LISTEN:
1365 s = format (s, "LISTEN");
1367 case HTTP_CONN_STATE_CONNECTING:
1368 s = format (s, "CONNECTING");
1370 case HTTP_CONN_STATE_ESTABLISHED:
1371 s = format (s, "ESTABLISHED");
1373 case HTTP_CONN_STATE_TRANSPORT_CLOSED:
1374 s = format (s, "TRANSPORT_CLOSED");
1376 case HTTP_CONN_STATE_APP_CLOSED:
1377 s = format (s, "APP_CLOSED");
1379 case HTTP_CONN_STATE_CLOSED:
1380 s = format (s, "CLOSED");
1388 format_http_transport_connection (u8 *s, va_list *args)
1390 u32 tc_index = va_arg (*args, u32);
1391 u32 thread_index = va_arg (*args, u32);
1392 u32 verbose = va_arg (*args, u32);
1395 hc = http_conn_get_w_thread (tc_index, thread_index);
1397 s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_http_connection, hc);
1401 format (s, "%-" SESSION_CLI_STATE_LEN "U", format_http_conn_state, hc);
1403 s = format (s, "\n");
1410 format_http_transport_listener (u8 *s, va_list *args)
1412 u32 tc_index = va_arg (*args, u32);
1413 u32 __clib_unused thread_index = va_arg (*args, u32);
1414 u32 __clib_unused verbose = va_arg (*args, u32);
1415 http_conn_t *lhc = http_listener_get (tc_index);
1417 s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_http_listener, lhc);
1420 format (s, "%-" SESSION_CLI_STATE_LEN "U", format_http_conn_state, lhc);
1424 static const transport_proto_vft_t http_proto = {
1425 .enable = http_transport_enable,
1426 .connect = http_transport_connect,
1427 .start_listen = http_start_listen,
1428 .stop_listen = http_stop_listen,
1429 .close = http_transport_close,
1430 .custom_tx = http_app_tx_callback,
1431 .get_connection = http_transport_get_connection,
1432 .get_listener = http_transport_get_listener,
1433 .get_transport_endpoint = http_transport_get_endpoint,
1434 .format_connection = format_http_transport_connection,
1435 .format_listener = format_http_transport_listener,
1436 .transport_options = {
1439 .tx_type = TRANSPORT_TX_INTERNAL,
1440 .service_type = TRANSPORT_SERVICE_APP,
1444 static clib_error_t *
1445 http_transport_init (vlib_main_t *vm)
1447 http_main_t *hm = &http_main;
1449 transport_register_protocol (TRANSPORT_PROTO_HTTP, &http_proto,
1450 FIB_PROTOCOL_IP4, ~0);
1451 transport_register_protocol (TRANSPORT_PROTO_HTTP, &http_proto,
1452 FIB_PROTOCOL_IP6, ~0);
1454 /* Default values, configurable via startup conf */
1455 hm->add_seg_size = 256 << 20;
1456 hm->first_seg_size = 32 << 20;
1457 hm->fifo_size = 512 << 10;
1462 VLIB_INIT_FUNCTION (http_transport_init);
1464 static clib_error_t *
1465 http_config_fn (vlib_main_t *vm, unformat_input_t *input)
1467 http_main_t *hm = &http_main;
1470 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1472 if (unformat (input, "first-segment-size %U", unformat_memory_size,
1475 hm->first_seg_size = clib_max (mem_sz, 1 << 20);
1476 if (hm->first_seg_size != mem_sz)
1477 clib_warning ("first seg size too small %u", mem_sz);
1479 else if (unformat (input, "add-segment-size %U", unformat_memory_size,
1482 hm->add_seg_size = clib_max (mem_sz, 1 << 20);
1483 if (hm->add_seg_size != mem_sz)
1484 clib_warning ("add seg size too small %u", mem_sz);
1486 else if (unformat (input, "fifo-size %U", unformat_memory_size, &mem_sz))
1488 hm->fifo_size = clib_clamp (mem_sz, 4 << 10, 2 << 30);
1489 if (hm->fifo_size != mem_sz)
1490 clib_warning ("invalid fifo size %lu", mem_sz);
1493 return clib_error_return (0, "unknown input `%U'",
1494 format_unformat_error, input);
1499 VLIB_CONFIG_FUNCTION (http_config_fn, "http");
1501 VLIB_PLUGIN_REGISTER () = {
1502 .version = VPP_BUILD_VER,
1503 .description = "Hypertext Transfer Protocol (HTTP)",
1504 .default_disabled = 0,
1508 * fd.io coding-style-patch-verification: ON
1511 * eval: (c-set-style "gnu")