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");
78 http_state_change (http_conn_t *hc, http_state_t state)
80 HTTP_DBG (1, "changing http state %U -> %U", format_http_state,
81 hc->http_state, format_http_state, state);
82 ASSERT (hc->http_state != state);
83 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;
144 memset (lhc, 0xfc, sizeof (*lhc));
145 pool_put (hm->listener_pool, lhc);
149 http_disconnect_transport (http_conn_t *hc)
151 vnet_disconnect_args_t a = {
152 .handle = hc->h_tc_session_handle,
153 .app_index = http_main.app_index,
156 hc->state = HTTP_CONN_STATE_CLOSED;
158 if (vnet_disconnect_session (&a))
159 clib_warning ("disconnect returned");
163 http_conn_timeout_cb (void *hc_handlep)
168 hs_handle = pointer_to_uword (hc_handlep);
169 hc = http_conn_get_w_thread (hs_handle & 0x00FFFFFF, hs_handle >> 24);
171 HTTP_DBG (1, "terminate thread %d index %d hs %llx", hs_handle >> 24,
172 hs_handle & 0x00FFFFFF, hc);
176 hc->timer_handle = ~0;
177 session_transport_closing_notify (&hc->connection);
178 http_disconnect_transport (hc);
182 http_ts_accept_callback (session_t *ts)
184 session_t *ts_listener, *as, *asl;
185 app_worker_t *app_wrk;
186 http_conn_t *lhc, *hc;
187 u32 hc_index, thresh;
190 ts_listener = listen_session_get_from_handle (ts->listener_handle);
191 lhc = http_listener_get (ts_listener->opaque);
193 hc_index = http_conn_alloc_w_thread (ts->thread_index);
194 hc = http_conn_get_w_thread (hc_index, ts->thread_index);
195 clib_memcpy_fast (hc, lhc, sizeof (*lhc));
196 hc->c_thread_index = ts->thread_index;
197 hc->h_hc_index = hc_index;
199 hc->h_tc_session_handle = session_handle (ts);
200 hc->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
202 hc->state = HTTP_CONN_STATE_ESTABLISHED;
203 http_state_change (hc, HTTP_STATE_WAIT_CLIENT_METHOD);
205 ts->session_state = SESSION_STATE_READY;
206 ts->opaque = hc_index;
209 * Alloc session and initialize
211 as = session_alloc (hc->c_thread_index);
212 hc->c_s_index = as->session_index;
214 as->app_wrk_index = hc->h_pa_wrk_index;
215 as->connection_index = hc->c_c_index;
216 as->session_state = SESSION_STATE_ACCEPTING;
218 asl = listen_session_get_from_handle (lhc->h_pa_session_handle);
219 as->session_type = asl->session_type;
220 as->listener_handle = lhc->h_pa_session_handle;
223 * Init session fifos and notify app
225 if ((rv = app_worker_init_accepted (as)))
227 HTTP_DBG (1, "failed to allocate fifos");
232 hc->h_pa_session_handle = session_handle (as);
233 hc->h_pa_wrk_index = as->app_wrk_index;
234 app_wrk = app_worker_get (as->app_wrk_index);
236 HTTP_DBG (1, "Accepted on listener %u new connection [%u]%x",
237 ts_listener->opaque, vlib_get_thread_index (), hc_index);
239 if ((rv = app_worker_accept_notify (app_wrk, as)))
241 HTTP_DBG (0, "app accept returned");
246 /* Avoid enqueuing small chunks of data on transport tx notifications. If
247 * the fifo is small (under 16K) we set the threshold to it's size, meaning
248 * a notification will be given when the fifo empties.
250 ts = session_get_from_handle (hc->h_tc_session_handle);
251 thresh = clib_min (svm_fifo_size (ts->tx_fifo), HTTP_FIFO_THRESH);
252 svm_fifo_set_deq_thresh (ts->tx_fifo, thresh);
254 http_conn_timer_start (hc);
260 http_ts_connected_callback (u32 http_app_index, u32 ho_hc_index, session_t *ts,
265 http_conn_t *hc, *ho_hc;
266 app_worker_t *app_wrk;
271 clib_warning ("ERROR: %d", err);
275 new_hc_index = http_conn_alloc_w_thread (ts->thread_index);
276 hc = http_conn_get_w_thread (new_hc_index, ts->thread_index);
277 ho_hc = http_conn_get_w_thread (ho_hc_index, 0);
279 ASSERT (ho_hc->state == HTTP_CONN_STATE_CONNECTING);
281 clib_memcpy_fast (hc, ho_hc, sizeof (*hc));
283 hc->c_thread_index = ts->thread_index;
284 hc->h_tc_session_handle = session_handle (ts);
285 hc->c_c_index = new_hc_index;
286 hc->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
287 hc->state = HTTP_CONN_STATE_ESTABLISHED;
288 http_state_change (hc, HTTP_STATE_WAIT_APP_METHOD);
290 ts->session_state = SESSION_STATE_READY;
291 ts->opaque = new_hc_index;
293 /* allocate app session and initialize */
295 as = session_alloc (hc->c_thread_index);
296 hc->c_s_index = as->session_index;
297 as->connection_index = hc->c_c_index;
298 as->app_wrk_index = hc->h_pa_wrk_index;
299 as->session_state = SESSION_STATE_READY;
300 as->opaque = hc->h_pa_app_api_ctx;
301 as->session_type = session_type_from_proto_and_ip (
302 TRANSPORT_PROTO_HTTP, session_type_is_ip4 (ts->session_type));
304 HTTP_DBG (1, "half-open hc index %d, hc index %d", ho_hc_index,
307 app_wrk = app_worker_get (hc->h_pa_wrk_index);
310 clib_warning ("no app worker");
314 if ((rv = app_worker_init_connected (app_wrk, as)))
316 HTTP_DBG (1, "failed to allocate fifos");
320 app_worker_connect_notify (app_wrk, as, err, hc->h_pa_app_api_ctx);
321 hc->h_pa_session_handle = session_handle (as);
322 http_conn_timer_start (hc);
328 http_ts_disconnect_callback (session_t *ts)
332 hc = http_conn_get_w_thread (ts->opaque, ts->thread_index);
334 if (hc->state < HTTP_CONN_STATE_TRANSPORT_CLOSED)
335 hc->state = HTTP_CONN_STATE_TRANSPORT_CLOSED;
337 /* Nothing more to rx, propagate to app */
338 if (!svm_fifo_max_dequeue_cons (ts->rx_fifo))
339 session_transport_closing_notify (&hc->connection);
343 http_ts_reset_callback (session_t *ts)
347 hc = http_conn_get_w_thread (ts->opaque, ts->thread_index);
349 hc->state = HTTP_CONN_STATE_CLOSED;
350 http_buffer_free (&hc->tx_buf);
351 http_state_change (hc, HTTP_STATE_WAIT_CLIENT_METHOD);
352 session_transport_reset_notify (&hc->connection);
354 http_disconnect_transport (hc);
358 * http error boilerplate
360 static const char *http_error_template = "HTTP/1.1 %s\r\n"
362 "Content-Type: text/html\r\n"
363 "Connection: close\r\n"
364 "Pragma: no-cache\r\n"
365 "Content-Length: 0\r\n\r\n";
367 static const char *http_redirect_template = "HTTP/1.1 %s\r\n";
370 * http response boilerplate
372 static const char *http_response_template = "HTTP/1.1 %s\r\n"
374 "Expires: %U GMT\r\n"
375 "Server: VPP Static\r\n"
376 "Content-Type: %s\r\n"
377 "Content-Length: %lu\r\n\r\n";
379 static const char *http_request_template = "GET %s HTTP/1.1\r\n"
380 "User-Agent: VPP HTTP client\r\n"
384 http_send_data (http_conn_t *hc, u8 *data, u32 length, u32 offset)
386 const u32 max_burst = 64 << 10;
391 ts = session_get_from_handle (hc->h_tc_session_handle);
393 to_send = clib_min (length - offset, max_burst);
394 sent = svm_fifo_enqueue (ts->tx_fifo, to_send, data + offset);
399 if (svm_fifo_set_event (ts->tx_fifo))
400 session_send_io_evt_to_thread (ts->tx_fifo, SESSION_IO_EVT_TX);
402 return (offset + sent);
406 http_send_error (http_conn_t *hc, http_status_code_t ec)
408 http_main_t *hm = &http_main;
412 if (ec >= HTTP_N_STATUS)
413 ec = HTTP_STATUS_INTERNAL_ERROR;
415 now = clib_timebase_now (&hm->timebase);
416 data = format (0, http_error_template, http_status_code_str[ec],
417 format_clib_timebase_time, now);
418 http_send_data (hc, data, vec_len (data), 0);
423 http_read_message (http_conn_t *hc)
425 u32 max_deq, cursize;
429 ts = session_get_from_handle (hc->h_tc_session_handle);
431 cursize = vec_len (hc->rx_buf);
432 max_deq = svm_fifo_max_dequeue (ts->rx_fifo);
433 if (PREDICT_FALSE (max_deq == 0))
436 vec_validate (hc->rx_buf, cursize + max_deq - 1);
437 n_read = svm_fifo_dequeue (ts->rx_fifo, max_deq, hc->rx_buf + cursize);
438 ASSERT (n_read == max_deq);
440 if (svm_fifo_is_empty (ts->rx_fifo))
441 svm_fifo_unset_event (ts->rx_fifo);
443 vec_set_len (hc->rx_buf, cursize + n_read);
448 v_find_index (u8 *vec, u32 offset, char *str)
450 int start_index = offset;
451 u32 slen = (u32) strnlen_s_inline (str, 16);
452 u32 vlen = vec_len (vec);
459 for (; start_index < (vlen - slen); start_index++)
461 if (!memcmp (vec + start_index, str, slen))
469 http_parse_header (http_conn_t *hc, int *content_length)
471 unformat_input_t input;
475 i = v_find_index (hc->rx_buf, hc->rx_buf_offset, CONTENT_LEN_STR);
478 clib_warning ("cannot find '%s' in the header!", CONTENT_LEN_STR);
482 hc->rx_buf_offset = i;
484 i = v_find_index (hc->rx_buf, hc->rx_buf_offset, "\n");
487 clib_warning ("end of line missing; incomplete data");
491 len = i - hc->rx_buf_offset;
492 line = vec_new (u8, len);
493 clib_memcpy (line, hc->rx_buf + hc->rx_buf_offset, len);
495 unformat_init_vector (&input, line);
496 if (!unformat (&input, CONTENT_LEN_STR "%d", content_length))
498 clib_warning ("failed to unformat content length!");
501 unformat_free (&input);
503 /* skip rest of the header */
504 hc->rx_buf_offset += len;
505 i = v_find_index (hc->rx_buf, hc->rx_buf_offset, "<html>");
508 clib_warning ("<html> tag not found");
511 hc->rx_buf_offset = i;
516 static http_sm_result_t
517 http_state_wait_server_reply (http_conn_t *hc, transport_send_params_t *sp)
519 int i, rv, content_length;
521 app_worker_t *app_wrk;
523 http_status_code_t ec;
525 rv = http_read_message (hc);
527 /* Nothing yet, wait for data or timer expire */
531 if (vec_len (hc->rx_buf) < 8)
533 ec = HTTP_STATUS_BAD_REQUEST;
537 if ((i = v_find_index (hc->rx_buf, 0, "200 OK")) >= 0)
539 msg.type = HTTP_MSG_REPLY;
540 msg.content_type = HTTP_CONTENT_TEXT_HTML;
541 msg.code = HTTP_STATUS_OK;
542 msg.data.type = HTTP_MSG_DATA_INLINE;
545 rv = http_parse_header (hc, &content_length);
548 clib_warning ("failed to parse http reply");
549 session_transport_closing_notify (&hc->connection);
550 http_disconnect_transport (hc);
553 msg.data.len = content_length;
554 u32 dlen = vec_len (hc->rx_buf) - hc->rx_buf_offset;
555 as = session_get_from_handle (hc->h_pa_session_handle);
556 svm_fifo_seg_t segs[2] = { { (u8 *) &msg, sizeof (msg) },
557 { &hc->rx_buf[hc->rx_buf_offset], dlen } };
559 rv = svm_fifo_enqueue_segments (as->rx_fifo, segs, 2,
560 0 /* allow partial */);
563 clib_warning ("error enqueue");
564 return HTTP_SM_ERROR;
567 hc->rx_buf_offset += dlen;
568 hc->to_recv = content_length - dlen;
570 if (hc->rx_buf_offset == vec_len (hc->rx_buf))
572 vec_reset_length (hc->rx_buf);
573 hc->rx_buf_offset = 0;
576 if (hc->to_recv == 0)
578 hc->rx_buf_offset = 0;
579 vec_reset_length (hc->rx_buf);
580 http_state_change (hc, HTTP_STATE_WAIT_CLIENT_METHOD);
584 http_state_change (hc, HTTP_STATE_CLIENT_IO_MORE_DATA);
587 app_wrk = app_worker_get_if_valid (as->app_wrk_index);
588 app_worker_rx_notify (app_wrk, as);
593 HTTP_DBG (0, "Unknown http method %v", hc->rx_buf);
594 ec = HTTP_STATUS_METHOD_NOT_ALLOWED;
601 http_send_error (hc, ec);
602 session_transport_closing_notify (&hc->connection);
603 http_disconnect_transport (hc);
605 return HTTP_SM_ERROR;
608 static http_sm_result_t
609 http_state_wait_client_method (http_conn_t *hc, transport_send_params_t *sp)
611 http_status_code_t ec;
612 app_worker_t *app_wrk;
619 rv = http_read_message (hc);
621 /* Nothing yet, wait for data or timer expire */
625 if (vec_len (hc->rx_buf) < 8)
627 ec = HTTP_STATUS_BAD_REQUEST;
631 if ((i = v_find_index (hc->rx_buf, 0, "GET ")) >= 0)
633 hc->method = HTTP_REQ_GET;
634 hc->rx_buf_offset = i + 5;
636 i = v_find_index (hc->rx_buf, hc->rx_buf_offset, "HTTP");
639 ec = HTTP_STATUS_BAD_REQUEST;
643 HTTP_DBG (0, "GET method %v", hc->rx_buf);
644 len = i - hc->rx_buf_offset - 1;
646 else if ((i = v_find_index (hc->rx_buf, 0, "POST ")) >= 0)
648 hc->method = HTTP_REQ_POST;
649 hc->rx_buf_offset = i + 6;
650 len = vec_len (hc->rx_buf) - hc->rx_buf_offset - 1;
651 HTTP_DBG (0, "POST method %v", hc->rx_buf);
655 HTTP_DBG (0, "Unknown http method %v", hc->rx_buf);
656 ec = HTTP_STATUS_METHOD_NOT_ALLOWED;
660 buf = &hc->rx_buf[hc->rx_buf_offset];
662 msg.type = HTTP_MSG_REQUEST;
663 msg.method_type = hc->method;
664 msg.content_type = HTTP_CONTENT_TEXT_HTML;
665 msg.data.type = HTTP_MSG_DATA_INLINE;
668 svm_fifo_seg_t segs[2] = { { (u8 *) &msg, sizeof (msg) }, { buf, len } };
670 as = session_get_from_handle (hc->h_pa_session_handle);
671 rv = svm_fifo_enqueue_segments (as->rx_fifo, segs, 2, 0 /* allow partial */);
672 if (rv < 0 || rv != sizeof (msg) + len)
674 clib_warning ("failed app enqueue");
675 /* This should not happen as we only handle 1 request per session,
676 * and fifo is allocated, but going forward we should consider
678 return HTTP_SM_ERROR;
681 vec_free (hc->rx_buf);
682 http_state_change (hc, HTTP_STATE_WAIT_APP_REPLY);
684 app_wrk = app_worker_get_if_valid (as->app_wrk_index);
686 app_worker_rx_notify (app_wrk, as);
692 http_send_error (hc, ec);
693 session_transport_closing_notify (&hc->connection);
694 http_disconnect_transport (hc);
696 return HTTP_SM_ERROR;
699 static http_sm_result_t
700 http_state_wait_app_reply (http_conn_t *hc, transport_send_params_t *sp)
702 http_main_t *hm = &http_main;
707 http_status_code_t sc;
711 as = session_get_from_handle (hc->h_pa_session_handle);
713 rv = svm_fifo_dequeue (as->tx_fifo, sizeof (msg), (u8 *) &msg);
714 ASSERT (rv == sizeof (msg));
716 if (msg.data.type > HTTP_MSG_DATA_PTR)
718 clib_warning ("no data");
719 sc = HTTP_STATUS_INTERNAL_ERROR;
723 if (msg.type != HTTP_MSG_REPLY)
725 clib_warning ("unexpected message type %d", msg.type);
726 sc = HTTP_STATUS_INTERNAL_ERROR;
730 http_buffer_init (&hc->tx_buf, msg_to_buf_type[msg.data.type], as->tx_fifo,
734 * Add headers. For now:
740 now = clib_timebase_now (&hm->timebase);
746 format (0, http_response_template, http_status_code_str[msg.code],
748 format_clib_timebase_time, now,
750 format_clib_timebase_time, now + 600.0,
752 http_content_type_str[msg.content_type],
756 case HTTP_STATUS_MOVED:
758 format (0, http_redirect_template, http_status_code_str[msg.code]);
759 /* Location: http(s)://new-place already queued up as data */
762 return HTTP_SM_ERROR;
765 offset = http_send_data (hc, header, vec_len (header), 0);
766 if (offset != vec_len (header))
768 clib_warning ("couldn't send response header!");
769 sc = HTTP_STATUS_INTERNAL_ERROR;
775 /* Start sending the actual data */
776 http_state_change (hc, HTTP_STATE_APP_IO_MORE_DATA);
778 ASSERT (sp->max_burst_size >= offset);
779 sp->max_burst_size -= offset;
780 return HTTP_SM_CONTINUE;
783 clib_warning ("unexpected msg type from app %u", msg.type);
784 http_send_error (hc, sc);
785 http_state_change (hc, HTTP_STATE_WAIT_CLIENT_METHOD);
786 session_transport_closing_notify (&hc->connection);
787 http_disconnect_transport (hc);
791 static http_sm_result_t
792 http_state_wait_app_method (http_conn_t *hc, transport_send_params_t *sp)
794 http_status_code_t sc;
797 u8 *buf = 0, *request;
801 as = session_get_from_handle (hc->h_pa_session_handle);
803 rv = svm_fifo_dequeue (as->tx_fifo, sizeof (msg), (u8 *) &msg);
804 ASSERT (rv == sizeof (msg));
806 if (msg.data.type > HTTP_MSG_DATA_PTR)
808 clib_warning ("no data");
809 sc = HTTP_STATUS_INTERNAL_ERROR;
813 if (msg.type != HTTP_MSG_REQUEST)
815 clib_warning ("unexpected message type %d", msg.type);
816 sc = HTTP_STATUS_INTERNAL_ERROR;
822 vec_validate (buf, msg.data.len - 1);
823 rv = svm_fifo_dequeue (as->tx_fifo, msg.data.len, buf);
824 ASSERT (rv == msg.data.len);
826 request = format (0, http_request_template, buf);
827 offset = http_send_data (hc, request, vec_len (request), 0);
828 if (offset != vec_len (request))
830 clib_warning ("sending request failed!");
831 sc = HTTP_STATUS_INTERNAL_ERROR;
835 http_state_change (hc, HTTP_STATE_WAIT_SERVER_REPLY);
840 return HTTP_SM_CONTINUE;
843 clib_warning ("unexpected msg type from app %u", msg.type);
844 http_send_error (hc, sc);
845 session_transport_closing_notify (&hc->connection);
846 http_disconnect_transport (hc);
851 http_app_enqueue (http_conn_t *hc, session_t *as)
853 app_worker_t *app_wrk;
854 u32 dlen, max_enq, n_enq;
857 dlen = vec_len (hc->rx_buf) - hc->rx_buf_offset;
861 max_enq = svm_fifo_max_enqueue (as->rx_fifo);
862 n_enq = clib_min (max_enq, dlen);
863 rv = svm_fifo_enqueue (as->rx_fifo, n_enq, &hc->rx_buf[hc->rx_buf_offset]);
867 hc->rx_buf_offset += rv;
868 if (hc->rx_buf_offset >= vec_len (hc->rx_buf))
870 vec_reset_length (hc->rx_buf);
871 hc->rx_buf_offset = 0;
874 app_wrk = app_worker_get_if_valid (as->app_wrk_index);
876 app_worker_rx_notify (app_wrk, as);
879 static http_sm_result_t
880 http_state_client_io_more_data (http_conn_t *hc, transport_send_params_t *sp)
886 as = session_get_from_handle (hc->h_pa_session_handle);
887 ts = session_get_from_handle (hc->h_tc_session_handle);
889 http_app_enqueue (hc, as);
891 if (hc->to_recv == 0)
893 http_state_change (hc, HTTP_STATE_WAIT_CLIENT_METHOD);
897 max_deq = svm_fifo_max_dequeue (ts->rx_fifo);
900 vec_validate (hc->rx_buf, max_deq - 1);
901 n_read = svm_fifo_dequeue (ts->rx_fifo, max_deq, hc->rx_buf);
902 ASSERT (n_read == max_deq);
904 if (svm_fifo_is_empty (ts->rx_fifo))
905 svm_fifo_unset_event (ts->rx_fifo);
907 hc->to_recv -= n_read;
908 vec_set_len (hc->rx_buf, n_read);
911 if (hc->rx_buf_offset < vec_len (hc->rx_buf) ||
912 svm_fifo_max_dequeue_cons (ts->rx_fifo))
914 session_enqueue_notify (ts);
916 return HTTP_SM_CONTINUE;
919 static http_sm_result_t
920 http_state_app_io_more_data (http_conn_t *hc, transport_send_params_t *sp)
922 u32 max_send = 64 << 10, n_segs;
923 http_buffer_t *hb = &hc->tx_buf;
928 max_send = clib_min (max_send, sp->max_burst_size);
929 ts = session_get_from_handle (hc->h_tc_session_handle);
930 if ((seg = http_buffer_get_segs (hb, max_send, &n_segs)))
931 sent = svm_fifo_enqueue_segments (ts->tx_fifo, seg, n_segs,
932 1 /* allow partial */);
936 /* Ask scheduler to notify app of deq event if needed */
937 sp->bytes_dequeued += http_buffer_drain (hb, sent);
938 sp->max_burst_size -= sent;
941 /* Not finished sending all data */
942 if (!http_buffer_is_drained (hb))
944 if (sent && svm_fifo_set_event (ts->tx_fifo))
945 session_send_io_evt_to_thread (ts->tx_fifo, SESSION_IO_EVT_TX);
947 if (svm_fifo_max_enqueue (ts->tx_fifo) < HTTP_FIFO_THRESH)
949 /* Deschedule http session and wait for deq notification if
950 * underlying ts tx fifo almost full */
951 svm_fifo_add_want_deq_ntf (ts->tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
952 transport_connection_deschedule (&hc->connection);
953 sp->flags |= TRANSPORT_SND_F_DESCHED;
958 if (sent && svm_fifo_set_event (ts->tx_fifo))
959 session_send_io_evt_to_thread (ts->tx_fifo, SESSION_IO_EVT_TX_FLUSH);
961 /* Finished transaction, back to HTTP_STATE_WAIT_METHOD */
962 http_state_change (hc, HTTP_STATE_WAIT_CLIENT_METHOD);
963 http_buffer_free (&hc->tx_buf);
969 typedef http_sm_result_t (*http_sm_handler) (http_conn_t *,
970 transport_send_params_t *sp);
972 static http_sm_handler state_funcs[HTTP_N_STATES] = {
974 http_state_wait_app_method,
975 http_state_wait_client_method,
976 http_state_wait_server_reply,
977 http_state_wait_app_reply,
978 http_state_client_io_more_data,
979 http_state_app_io_more_data,
983 http_req_run_state_machine (http_conn_t *hc, transport_send_params_t *sp)
985 http_sm_result_t res;
988 res = state_funcs[hc->http_state](hc, sp);
989 if (res == HTTP_SM_ERROR)
991 HTTP_DBG (1, "error in state machine %d", res);
995 while (res == HTTP_SM_CONTINUE);
997 /* Reset the session expiration timer */
998 http_conn_timer_update (hc);
1002 http_ts_rx_callback (session_t *ts)
1006 hc = http_conn_get_w_thread (ts->opaque, ts->thread_index);
1009 clib_warning ("http connection not found (ts %d)", ts->opaque);
1013 http_req_run_state_machine (hc, 0);
1015 if (hc->state == HTTP_CONN_STATE_TRANSPORT_CLOSED)
1017 if (!svm_fifo_max_dequeue_cons (ts->rx_fifo))
1018 session_transport_closing_notify (&hc->connection);
1024 http_ts_builtin_tx_callback (session_t *ts)
1028 hc = http_conn_get_w_thread (ts->opaque, ts->thread_index);
1029 transport_connection_reschedule (&hc->connection);
1035 http_ts_cleanup_callback (session_t *ts, session_cleanup_ntf_t ntf)
1039 if (ntf == SESSION_CLEANUP_TRANSPORT)
1042 hc = http_conn_get_w_thread (ts->opaque, ts->thread_index);
1045 clib_warning ("no http connection for %u", ts->session_index);
1049 vec_free (hc->rx_buf);
1051 http_buffer_free (&hc->tx_buf);
1052 http_conn_timer_stop (hc);
1054 session_transport_delete_notify (&hc->connection);
1055 http_conn_free (hc);
1059 http_add_segment_callback (u32 client_index, u64 segment_handle)
1061 /* No-op for builtin */
1066 http_del_segment_callback (u32 client_index, u64 segment_handle)
1071 static session_cb_vft_t http_app_cb_vft = {
1072 .session_accept_callback = http_ts_accept_callback,
1073 .session_disconnect_callback = http_ts_disconnect_callback,
1074 .session_connected_callback = http_ts_connected_callback,
1075 .session_reset_callback = http_ts_reset_callback,
1076 .session_cleanup_callback = http_ts_cleanup_callback,
1077 .add_segment_callback = http_add_segment_callback,
1078 .del_segment_callback = http_del_segment_callback,
1079 .builtin_app_rx_callback = http_ts_rx_callback,
1080 .builtin_app_tx_callback = http_ts_builtin_tx_callback,
1083 static clib_error_t *
1084 http_transport_enable (vlib_main_t *vm, u8 is_en)
1086 vnet_app_detach_args_t _da, *da = &_da;
1087 vnet_app_attach_args_t _a, *a = &_a;
1088 u64 options[APP_OPTIONS_N_OPTIONS];
1089 http_main_t *hm = &http_main;
1093 da->app_index = hm->app_index;
1094 da->api_client_index = APP_INVALID_INDEX;
1095 vnet_application_detach (da);
1099 vec_validate (hm->wrk, vlib_num_workers ());
1101 clib_memset (a, 0, sizeof (*a));
1102 clib_memset (options, 0, sizeof (options));
1104 a->session_cb_vft = &http_app_cb_vft;
1105 a->api_client_index = APP_INVALID_INDEX;
1106 a->options = options;
1107 a->name = format (0, "http");
1108 a->options[APP_OPTIONS_SEGMENT_SIZE] = hm->first_seg_size;
1109 a->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = hm->add_seg_size;
1110 a->options[APP_OPTIONS_RX_FIFO_SIZE] = hm->fifo_size;
1111 a->options[APP_OPTIONS_TX_FIFO_SIZE] = hm->fifo_size;
1112 a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
1113 a->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
1114 a->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_IS_TRANSPORT_APP;
1116 if (vnet_application_attach (a))
1117 return clib_error_return (0, "failed to attach http app");
1119 hm->app_index = a->app_index;
1122 clib_timebase_init (&hm->timebase, 0 /* GMT */, CLIB_TIMEBASE_DAYLIGHT_NONE,
1123 &vm->clib_time /* share the system clock */);
1125 http_timers_init (vm, http_conn_timeout_cb);
1131 http_transport_connect (transport_endpoint_cfg_t *tep)
1133 vnet_connect_args_t _cargs, *cargs = &_cargs;
1134 http_main_t *hm = &http_main;
1135 session_endpoint_cfg_t *sep = (session_endpoint_cfg_t *) tep;
1140 app_worker_t *app_wrk = app_worker_get (sep->app_wrk_index);
1142 clib_memset (cargs, 0, sizeof (*cargs));
1143 clib_memcpy (&cargs->sep_ext, sep, sizeof (session_endpoint_cfg_t));
1144 cargs->sep.transport_proto = TRANSPORT_PROTO_TCP;
1145 cargs->app_index = hm->app_index;
1146 app = application_get (app_wrk->app_index);
1147 cargs->sep_ext.ns_index = app->ns_index;
1149 hc_index = http_conn_alloc_w_thread (0 /* ts->thread_index */);
1150 hc = http_conn_get_w_thread (hc_index, 0);
1151 hc->h_pa_wrk_index = sep->app_wrk_index;
1152 hc->h_pa_app_api_ctx = sep->opaque;
1153 hc->state = HTTP_CONN_STATE_CONNECTING;
1154 cargs->api_context = hc_index;
1156 HTTP_DBG (1, "hc ho_index %x", hc_index);
1158 if ((error = vnet_connect (cargs)))
1165 http_start_listen (u32 app_listener_index, transport_endpoint_cfg_t *tep)
1167 vnet_listen_args_t _args = {}, *args = &_args;
1168 session_t *ts_listener, *app_listener;
1169 http_main_t *hm = &http_main;
1170 session_endpoint_cfg_t *sep;
1171 app_worker_t *app_wrk;
1172 transport_proto_t tp;
1178 sep = (session_endpoint_cfg_t *) tep;
1180 app_wrk = app_worker_get (sep->app_wrk_index);
1181 app = application_get (app_wrk->app_index);
1183 args->app_index = hm->app_index;
1184 args->sep_ext = *sep;
1185 args->sep_ext.ns_index = app->ns_index;
1186 tp = sep->ext_cfg ? TRANSPORT_PROTO_TLS : TRANSPORT_PROTO_TCP;
1187 args->sep_ext.transport_proto = tp;
1189 if (vnet_listen (args))
1190 return SESSION_INVALID_INDEX;
1192 lhc_index = http_listener_alloc ();
1193 lhc = http_listener_get (lhc_index);
1195 /* Grab transport connection listener and link to http listener */
1196 lhc->h_tc_session_handle = args->handle;
1197 al = app_listener_get_w_handle (lhc->h_tc_session_handle);
1198 ts_listener = app_listener_get_session (al);
1199 ts_listener->opaque = lhc_index;
1201 /* Grab application listener and link to http listener */
1202 app_listener = listen_session_get (app_listener_index);
1203 lhc->h_pa_wrk_index = sep->app_wrk_index;
1204 lhc->h_pa_session_handle = listen_session_get_handle (app_listener);
1205 lhc->c_s_index = app_listener_index;
1206 lhc->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
1212 http_stop_listen (u32 listener_index)
1217 lhc = http_listener_get (listener_index);
1219 vnet_unlisten_args_t a = {
1220 .handle = lhc->h_tc_session_handle,
1221 .app_index = http_main.app_index,
1222 .wrk_map_index = 0 /* default wrk */
1225 if ((rv = vnet_unlisten (&a)))
1226 clib_warning ("unlisten returned %d", rv);
1228 http_listener_free (lhc);
1234 http_transport_close (u32 hc_index, u32 thread_index)
1239 HTTP_DBG (1, "App disconnecting %x", hc_index);
1241 hc = http_conn_get_w_thread (hc_index, thread_index);
1242 if (hc->state == HTTP_CONN_STATE_CONNECTING)
1244 hc->state = HTTP_CONN_STATE_APP_CLOSED;
1245 http_disconnect_transport (hc);
1249 as = session_get_from_handle (hc->h_pa_session_handle);
1251 /* Nothing more to send, confirm close */
1252 if (!svm_fifo_max_dequeue_cons (as->tx_fifo))
1254 session_transport_closed_notify (&hc->connection);
1255 http_disconnect_transport (hc);
1259 /* Wait for all data to be written to ts */
1260 hc->state = HTTP_CONN_STATE_APP_CLOSED;
1264 static transport_connection_t *
1265 http_transport_get_connection (u32 hc_index, u32 thread_index)
1267 http_conn_t *hc = http_conn_get_w_thread (hc_index, thread_index);
1268 return &hc->connection;
1271 static transport_connection_t *
1272 http_transport_get_listener (u32 listener_index)
1274 http_conn_t *lhc = http_listener_get (listener_index);
1275 return &lhc->connection;
1279 http_app_tx_callback (void *session, transport_send_params_t *sp)
1281 session_t *as = (session_t *) session;
1282 u32 max_burst_sz, sent;
1285 HTTP_DBG (1, "app session conn index %x", as->connection_index);
1287 hc = http_conn_get_w_thread (as->connection_index, as->thread_index);
1288 if (!http_state_is_tx_valid (hc))
1290 if (hc->state != HTTP_CONN_STATE_CLOSED)
1291 clib_warning ("app data req state '%U' session state %u",
1292 format_http_state, hc->http_state, hc->state);
1293 svm_fifo_dequeue_drop_all (as->tx_fifo);
1297 max_burst_sz = sp->max_burst_size * TRANSPORT_PACER_MIN_MSS;
1298 sp->max_burst_size = max_burst_sz;
1300 http_req_run_state_machine (hc, sp);
1302 if (hc->state == HTTP_CONN_STATE_APP_CLOSED)
1304 if (!svm_fifo_max_dequeue_cons (as->tx_fifo))
1305 http_disconnect_transport (hc);
1308 sent = max_burst_sz - sp->max_burst_size;
1310 return sent > 0 ? clib_max (sent / TRANSPORT_PACER_MIN_MSS, 1) : 0;
1314 http_transport_get_endpoint (u32 hc_index, u32 thread_index,
1315 transport_endpoint_t *tep, u8 is_lcl)
1317 http_conn_t *hc = http_conn_get_w_thread (hc_index, thread_index);
1320 ts = session_get_from_handle (hc->h_tc_session_handle);
1321 session_get_endpoint (ts, tep, is_lcl);
1325 format_http_connection (u8 *s, va_list *args)
1327 http_conn_t *hc = va_arg (*args, http_conn_t *);
1330 ts = session_get_from_handle (hc->h_tc_session_handle);
1331 s = format (s, "[%d:%d][H] app_wrk %u ts %d:%d", hc->c_thread_index,
1332 hc->c_s_index, hc->h_pa_wrk_index, ts->thread_index,
1339 format_http_listener (u8 *s, va_list *args)
1341 http_conn_t *lhc = va_arg (*args, http_conn_t *);
1345 al = app_listener_get_w_handle (lhc->h_tc_session_handle);
1346 lts = app_listener_get_session (al);
1347 s = format (s, "[%d:%d][H] app_wrk %u ts %d:%d", lhc->c_thread_index,
1348 lhc->c_s_index, lhc->h_pa_wrk_index, lts->thread_index,
1349 lts->session_index);
1355 format_http_conn_state (u8 *s, va_list *args)
1357 http_conn_t *hc = va_arg (*args, http_conn_t *);
1361 case HTTP_CONN_STATE_LISTEN:
1362 s = format (s, "LISTEN");
1364 case HTTP_CONN_STATE_CONNECTING:
1365 s = format (s, "CONNECTING");
1367 case HTTP_CONN_STATE_ESTABLISHED:
1368 s = format (s, "ESTABLISHED");
1370 case HTTP_CONN_STATE_TRANSPORT_CLOSED:
1371 s = format (s, "TRANSPORT_CLOSED");
1373 case HTTP_CONN_STATE_APP_CLOSED:
1374 s = format (s, "APP_CLOSED");
1376 case HTTP_CONN_STATE_CLOSED:
1377 s = format (s, "CLOSED");
1385 format_http_transport_connection (u8 *s, va_list *args)
1387 u32 tc_index = va_arg (*args, u32);
1388 u32 thread_index = va_arg (*args, u32);
1389 u32 verbose = va_arg (*args, u32);
1392 hc = http_conn_get_w_thread (tc_index, thread_index);
1394 s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_http_connection, hc);
1398 format (s, "%-" SESSION_CLI_STATE_LEN "U", format_http_conn_state, hc);
1400 s = format (s, "\n");
1407 format_http_transport_listener (u8 *s, va_list *args)
1409 u32 tc_index = va_arg (*args, u32);
1410 u32 __clib_unused thread_index = va_arg (*args, u32);
1411 u32 __clib_unused verbose = va_arg (*args, u32);
1412 http_conn_t *lhc = http_listener_get (tc_index);
1414 s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_http_listener, lhc);
1417 format (s, "%-" SESSION_CLI_STATE_LEN "U", format_http_conn_state, lhc);
1421 static const transport_proto_vft_t http_proto = {
1422 .enable = http_transport_enable,
1423 .connect = http_transport_connect,
1424 .start_listen = http_start_listen,
1425 .stop_listen = http_stop_listen,
1426 .close = http_transport_close,
1427 .custom_tx = http_app_tx_callback,
1428 .get_connection = http_transport_get_connection,
1429 .get_listener = http_transport_get_listener,
1430 .get_transport_endpoint = http_transport_get_endpoint,
1431 .format_connection = format_http_transport_connection,
1432 .format_listener = format_http_transport_listener,
1433 .transport_options = {
1436 .tx_type = TRANSPORT_TX_INTERNAL,
1437 .service_type = TRANSPORT_SERVICE_APP,
1441 static clib_error_t *
1442 http_transport_init (vlib_main_t *vm)
1444 http_main_t *hm = &http_main;
1446 transport_register_protocol (TRANSPORT_PROTO_HTTP, &http_proto,
1447 FIB_PROTOCOL_IP4, ~0);
1448 transport_register_protocol (TRANSPORT_PROTO_HTTP, &http_proto,
1449 FIB_PROTOCOL_IP6, ~0);
1451 /* Default values, configurable via startup conf */
1452 hm->add_seg_size = 256 << 20;
1453 hm->first_seg_size = 32 << 20;
1454 hm->fifo_size = 512 << 10;
1459 VLIB_INIT_FUNCTION (http_transport_init);
1461 static clib_error_t *
1462 http_config_fn (vlib_main_t *vm, unformat_input_t *input)
1464 http_main_t *hm = &http_main;
1467 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1469 if (unformat (input, "first-segment-size %U", unformat_memory_size,
1472 hm->first_seg_size = clib_max (mem_sz, 1 << 20);
1473 if (hm->first_seg_size != mem_sz)
1474 clib_warning ("first seg size too small %u", mem_sz);
1476 else if (unformat (input, "add-segment-size %U", unformat_memory_size,
1479 hm->add_seg_size = clib_max (mem_sz, 1 << 20);
1480 if (hm->add_seg_size != mem_sz)
1481 clib_warning ("add seg size too small %u", mem_sz);
1483 else if (unformat (input, "fifo-size %U", unformat_memory_size, &mem_sz))
1485 hm->fifo_size = clib_clamp (mem_sz, 4 << 10, 2 << 30);
1486 if (hm->fifo_size != mem_sz)
1487 clib_warning ("invalid fifo size %lu", mem_sz);
1490 return clib_error_return (0, "unknown input `%U'",
1491 format_unformat_error, input);
1496 VLIB_CONFIG_FUNCTION (http_config_fn, "http");
1498 VLIB_PLUGIN_REGISTER () = {
1499 .version = VPP_BUILD_VER,
1500 .description = "Hypertext Transfer Protocol (HTTP)",
1501 .default_disabled = 0,
1505 * fd.io coding-style-patch-verification: ON
1508 * eval: (c-set-style "gnu")