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;
270 ho_hc = http_conn_get_w_thread (ho_hc_index, 0);
271 ASSERT (ho_hc->state == HTTP_CONN_STATE_CONNECTING);
275 clib_warning ("half-open hc index %d, error: %U", ho_hc_index,
276 format_session_error, err);
277 app_wrk = app_worker_get_if_valid (ho_hc->h_pa_wrk_index);
279 app_worker_connect_notify (app_wrk, 0, err, ho_hc->h_pa_app_api_ctx);
283 new_hc_index = http_conn_alloc_w_thread (ts->thread_index);
284 hc = http_conn_get_w_thread (new_hc_index, ts->thread_index);
286 clib_memcpy_fast (hc, ho_hc, sizeof (*hc));
288 hc->c_thread_index = ts->thread_index;
289 hc->h_tc_session_handle = session_handle (ts);
290 hc->c_c_index = new_hc_index;
291 hc->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
292 hc->state = HTTP_CONN_STATE_ESTABLISHED;
293 http_state_change (hc, HTTP_STATE_WAIT_APP_METHOD);
295 ts->session_state = SESSION_STATE_READY;
296 ts->opaque = new_hc_index;
298 /* allocate app session and initialize */
300 as = session_alloc (hc->c_thread_index);
301 hc->c_s_index = as->session_index;
302 as->connection_index = hc->c_c_index;
303 as->app_wrk_index = hc->h_pa_wrk_index;
304 as->session_state = SESSION_STATE_READY;
305 as->opaque = hc->h_pa_app_api_ctx;
306 as->session_type = session_type_from_proto_and_ip (
307 TRANSPORT_PROTO_HTTP, session_type_is_ip4 (ts->session_type));
309 HTTP_DBG (1, "half-open hc index %d, hc index %d", ho_hc_index,
312 app_wrk = app_worker_get (hc->h_pa_wrk_index);
315 clib_warning ("no app worker");
319 if ((rv = app_worker_init_connected (app_wrk, as)))
321 HTTP_DBG (1, "failed to allocate fifos");
325 app_worker_connect_notify (app_wrk, as, err, hc->h_pa_app_api_ctx);
326 hc->h_pa_session_handle = session_handle (as);
327 http_conn_timer_start (hc);
333 http_ts_disconnect_callback (session_t *ts)
337 hc = http_conn_get_w_thread (ts->opaque, ts->thread_index);
339 if (hc->state < HTTP_CONN_STATE_TRANSPORT_CLOSED)
340 hc->state = HTTP_CONN_STATE_TRANSPORT_CLOSED;
342 /* Nothing more to rx, propagate to app */
343 if (!svm_fifo_max_dequeue_cons (ts->rx_fifo))
344 session_transport_closing_notify (&hc->connection);
348 http_ts_reset_callback (session_t *ts)
352 hc = http_conn_get_w_thread (ts->opaque, ts->thread_index);
354 hc->state = HTTP_CONN_STATE_CLOSED;
355 http_buffer_free (&hc->tx_buf);
356 http_state_change (hc, HTTP_STATE_WAIT_CLIENT_METHOD);
357 session_transport_reset_notify (&hc->connection);
359 http_disconnect_transport (hc);
363 * http error boilerplate
365 static const char *http_error_template = "HTTP/1.1 %s\r\n"
367 "Content-Type: text/html\r\n"
368 "Connection: close\r\n"
369 "Pragma: no-cache\r\n"
370 "Content-Length: 0\r\n\r\n";
372 static const char *http_redirect_template = "HTTP/1.1 %s\r\n";
375 * http response boilerplate
377 static const char *http_response_template = "HTTP/1.1 %s\r\n"
379 "Expires: %U GMT\r\n"
381 "Content-Type: %s\r\n"
382 "Content-Length: %lu\r\n\r\n";
384 static const char *http_request_template = "GET %s HTTP/1.1\r\n"
389 http_send_data (http_conn_t *hc, u8 *data, u32 length, u32 offset)
391 const u32 max_burst = 64 << 10;
396 ts = session_get_from_handle (hc->h_tc_session_handle);
398 to_send = clib_min (length - offset, max_burst);
399 sent = svm_fifo_enqueue (ts->tx_fifo, to_send, data + offset);
404 if (svm_fifo_set_event (ts->tx_fifo))
405 session_send_io_evt_to_thread (ts->tx_fifo, SESSION_IO_EVT_TX);
407 return (offset + sent);
411 http_send_error (http_conn_t *hc, http_status_code_t ec)
413 http_main_t *hm = &http_main;
417 if (ec >= HTTP_N_STATUS)
418 ec = HTTP_STATUS_INTERNAL_ERROR;
420 now = clib_timebase_now (&hm->timebase);
421 data = format (0, http_error_template, http_status_code_str[ec],
422 format_clib_timebase_time, now);
423 http_send_data (hc, data, vec_len (data), 0);
428 http_read_message (http_conn_t *hc)
430 u32 max_deq, cursize;
434 ts = session_get_from_handle (hc->h_tc_session_handle);
436 cursize = vec_len (hc->rx_buf);
437 max_deq = svm_fifo_max_dequeue (ts->rx_fifo);
438 if (PREDICT_FALSE (max_deq == 0))
441 vec_validate (hc->rx_buf, cursize + max_deq - 1);
442 n_read = svm_fifo_dequeue (ts->rx_fifo, max_deq, hc->rx_buf + cursize);
443 ASSERT (n_read == max_deq);
445 if (svm_fifo_is_empty (ts->rx_fifo))
446 svm_fifo_unset_event (ts->rx_fifo);
448 vec_set_len (hc->rx_buf, cursize + n_read);
453 v_find_index (u8 *vec, u32 offset, char *str)
455 int start_index = offset;
456 u32 slen = (u32) strnlen_s_inline (str, 16);
457 u32 vlen = vec_len (vec);
464 for (; start_index < (vlen - slen); start_index++)
466 if (!memcmp (vec + start_index, str, slen))
474 http_parse_header (http_conn_t *hc, int *content_length)
476 unformat_input_t input;
480 i = v_find_index (hc->rx_buf, hc->rx_buf_offset, CONTENT_LEN_STR);
483 clib_warning ("cannot find '%s' in the header!", CONTENT_LEN_STR);
487 hc->rx_buf_offset = i;
489 i = v_find_index (hc->rx_buf, hc->rx_buf_offset, "\n");
492 clib_warning ("end of line missing; incomplete data");
496 len = i - hc->rx_buf_offset;
497 line = vec_new (u8, len);
498 clib_memcpy (line, hc->rx_buf + hc->rx_buf_offset, len);
500 unformat_init_vector (&input, line);
501 if (!unformat (&input, CONTENT_LEN_STR "%d", content_length))
503 clib_warning ("failed to unformat content length!");
506 unformat_free (&input);
508 /* skip rest of the header */
509 hc->rx_buf_offset += len;
510 i = v_find_index (hc->rx_buf, hc->rx_buf_offset, "<html>");
513 clib_warning ("<html> tag not found");
516 hc->rx_buf_offset = i;
521 static http_sm_result_t
522 http_state_wait_server_reply (http_conn_t *hc, transport_send_params_t *sp)
524 int i, rv, content_length;
526 app_worker_t *app_wrk;
529 rv = http_read_message (hc);
531 /* Nothing yet, wait for data or timer expire */
534 HTTP_DBG (1, "no data to deq");
538 if (vec_len (hc->rx_buf) < 8)
540 clib_warning ("response buffer too short");
544 if ((i = v_find_index (hc->rx_buf, 0, "200 OK")) >= 0)
546 msg.type = HTTP_MSG_REPLY;
547 msg.content_type = HTTP_CONTENT_TEXT_HTML;
548 msg.code = HTTP_STATUS_OK;
549 msg.data.type = HTTP_MSG_DATA_INLINE;
552 rv = http_parse_header (hc, &content_length);
555 clib_warning ("failed to parse http reply");
558 msg.data.len = content_length;
559 u32 dlen = vec_len (hc->rx_buf) - hc->rx_buf_offset;
560 as = session_get_from_handle (hc->h_pa_session_handle);
561 svm_fifo_seg_t segs[2] = { { (u8 *) &msg, sizeof (msg) },
562 { &hc->rx_buf[hc->rx_buf_offset], dlen } };
564 rv = svm_fifo_enqueue_segments (as->rx_fifo, segs, 2,
565 0 /* allow partial */);
568 clib_warning ("error enqueue");
569 return HTTP_SM_ERROR;
572 hc->rx_buf_offset += dlen;
573 hc->to_recv = content_length - dlen;
575 if (hc->rx_buf_offset == vec_len (hc->rx_buf))
577 vec_reset_length (hc->rx_buf);
578 hc->rx_buf_offset = 0;
581 if (hc->to_recv == 0)
583 hc->rx_buf_offset = 0;
584 vec_reset_length (hc->rx_buf);
585 http_state_change (hc, HTTP_STATE_WAIT_APP_METHOD);
589 http_state_change (hc, HTTP_STATE_CLIENT_IO_MORE_DATA);
592 app_wrk = app_worker_get_if_valid (as->app_wrk_index);
594 app_worker_rx_notify (app_wrk, as);
599 clib_warning ("Unknown http method %v", hc->rx_buf);
604 session_transport_closing_notify (&hc->connection);
605 session_transport_closed_notify (&hc->connection);
606 http_disconnect_transport (hc);
607 return HTTP_SM_ERROR;
610 static http_sm_result_t
611 http_state_wait_client_method (http_conn_t *hc, transport_send_params_t *sp)
613 http_status_code_t ec;
614 app_worker_t *app_wrk;
621 rv = http_read_message (hc);
623 /* Nothing yet, wait for data or timer expire */
627 if (vec_len (hc->rx_buf) < 8)
629 ec = HTTP_STATUS_BAD_REQUEST;
633 if ((i = v_find_index (hc->rx_buf, 0, "GET ")) >= 0)
635 hc->method = HTTP_REQ_GET;
636 hc->rx_buf_offset = i + 5;
638 i = v_find_index (hc->rx_buf, hc->rx_buf_offset, "HTTP");
641 ec = HTTP_STATUS_BAD_REQUEST;
645 HTTP_DBG (0, "GET method %v", hc->rx_buf);
646 len = i - hc->rx_buf_offset - 1;
648 else if ((i = v_find_index (hc->rx_buf, 0, "POST ")) >= 0)
650 hc->method = HTTP_REQ_POST;
651 hc->rx_buf_offset = i + 6;
652 len = vec_len (hc->rx_buf) - hc->rx_buf_offset - 1;
653 HTTP_DBG (0, "POST method %v", hc->rx_buf);
657 HTTP_DBG (0, "Unknown http method %v", hc->rx_buf);
658 ec = HTTP_STATUS_METHOD_NOT_ALLOWED;
662 buf = &hc->rx_buf[hc->rx_buf_offset];
664 msg.type = HTTP_MSG_REQUEST;
665 msg.method_type = hc->method;
666 msg.content_type = HTTP_CONTENT_TEXT_HTML;
667 msg.data.type = HTTP_MSG_DATA_INLINE;
670 svm_fifo_seg_t segs[2] = { { (u8 *) &msg, sizeof (msg) }, { buf, len } };
672 as = session_get_from_handle (hc->h_pa_session_handle);
673 rv = svm_fifo_enqueue_segments (as->rx_fifo, segs, 2, 0 /* allow partial */);
674 if (rv < 0 || rv != sizeof (msg) + len)
676 clib_warning ("failed app enqueue");
677 /* This should not happen as we only handle 1 request per session,
678 * and fifo is allocated, but going forward we should consider
680 return HTTP_SM_ERROR;
683 vec_free (hc->rx_buf);
684 http_state_change (hc, HTTP_STATE_WAIT_APP_REPLY);
686 app_wrk = app_worker_get_if_valid (as->app_wrk_index);
688 app_worker_rx_notify (app_wrk, as);
694 http_send_error (hc, ec);
695 session_transport_closing_notify (&hc->connection);
696 http_disconnect_transport (hc);
698 return HTTP_SM_ERROR;
701 static http_sm_result_t
702 http_state_wait_app_reply (http_conn_t *hc, transport_send_params_t *sp)
704 http_main_t *hm = &http_main;
709 http_status_code_t sc;
713 as = session_get_from_handle (hc->h_pa_session_handle);
715 rv = svm_fifo_dequeue (as->tx_fifo, sizeof (msg), (u8 *) &msg);
716 ASSERT (rv == sizeof (msg));
718 if (msg.data.type > HTTP_MSG_DATA_PTR)
720 clib_warning ("no data");
721 sc = HTTP_STATUS_INTERNAL_ERROR;
725 if (msg.type != HTTP_MSG_REPLY)
727 clib_warning ("unexpected message type %d", msg.type);
728 sc = HTTP_STATUS_INTERNAL_ERROR;
732 http_buffer_init (&hc->tx_buf, msg_to_buf_type[msg.data.type], as->tx_fifo,
736 * Add headers. For now:
743 now = clib_timebase_now (&hm->timebase);
747 case HTTP_STATUS_NOT_FOUND:
748 case HTTP_STATUS_METHOD_NOT_ALLOWED:
749 case HTTP_STATUS_BAD_REQUEST:
750 case HTTP_STATUS_INTERNAL_ERROR:
753 format (0, http_response_template, http_status_code_str[msg.code],
755 format_clib_timebase_time, now,
757 format_clib_timebase_time, now + 600.0,
761 http_content_type_str[msg.content_type],
765 case HTTP_STATUS_MOVED:
767 format (0, http_redirect_template, http_status_code_str[msg.code]);
768 /* Location: http(s)://new-place already queued up as data */
771 clib_warning ("unsupported status code: %d", msg.code);
772 return HTTP_SM_ERROR;
775 offset = http_send_data (hc, header, vec_len (header), 0);
776 if (offset != vec_len (header))
778 clib_warning ("couldn't send response header!");
779 sc = HTTP_STATUS_INTERNAL_ERROR;
785 /* Start sending the actual data */
786 http_state_change (hc, HTTP_STATE_APP_IO_MORE_DATA);
788 ASSERT (sp->max_burst_size >= offset);
789 sp->max_burst_size -= offset;
790 return HTTP_SM_CONTINUE;
793 clib_warning ("unexpected msg type from app %u", msg.type);
794 http_send_error (hc, sc);
795 http_state_change (hc, HTTP_STATE_WAIT_CLIENT_METHOD);
796 session_transport_closing_notify (&hc->connection);
797 http_disconnect_transport (hc);
801 static http_sm_result_t
802 http_state_wait_app_method (http_conn_t *hc, transport_send_params_t *sp)
806 u8 *buf = 0, *request;
810 as = session_get_from_handle (hc->h_pa_session_handle);
812 rv = svm_fifo_dequeue (as->tx_fifo, sizeof (msg), (u8 *) &msg);
813 ASSERT (rv == sizeof (msg));
815 if (msg.data.type > HTTP_MSG_DATA_PTR)
817 clib_warning ("no data");
821 if (msg.type != HTTP_MSG_REQUEST)
823 clib_warning ("unexpected message type %d", msg.type);
827 vec_validate (buf, msg.data.len - 1);
828 rv = svm_fifo_dequeue (as->tx_fifo, msg.data.len, buf);
829 ASSERT (rv == msg.data.len);
831 request = format (0, http_request_template, buf, hc->app_name);
832 offset = http_send_data (hc, request, vec_len (request), 0);
833 if (offset != vec_len (request))
835 clib_warning ("sending request failed!");
839 http_state_change (hc, HTTP_STATE_WAIT_SERVER_REPLY);
847 svm_fifo_dequeue_drop_all (as->tx_fifo);
848 session_transport_closing_notify (&hc->connection);
849 session_transport_closed_notify (&hc->connection);
850 http_disconnect_transport (hc);
851 return HTTP_SM_ERROR;
854 static http_sm_result_t
855 http_state_client_io_more_data (http_conn_t *hc, transport_send_params_t *sp)
858 app_worker_t *app_wrk;
859 svm_fifo_seg_t _seg, *seg = &_seg;
860 u32 max_len, max_deq, max_enq, n_segs = 1;
863 as = session_get_from_handle (hc->h_pa_session_handle);
864 ts = session_get_from_handle (hc->h_tc_session_handle);
866 max_deq = svm_fifo_max_dequeue (ts->rx_fifo);
869 HTTP_DBG (1, "no data to deq");
873 max_enq = svm_fifo_max_enqueue (as->rx_fifo);
876 HTTP_DBG (1, "app's rx fifo full");
877 svm_fifo_add_want_deq_ntf (as->rx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
881 max_len = clib_min (max_enq, max_deq);
882 len = svm_fifo_segments (ts->rx_fifo, 0, seg, &n_segs, max_len);
885 HTTP_DBG (1, "svm_fifo_segments() len %d", len);
889 rv = svm_fifo_enqueue_segments (as->rx_fifo, seg, 1, 0 /* allow partial */);
892 clib_warning ("data enqueue failed, rv: %d", rv);
893 return HTTP_SM_ERROR;
896 svm_fifo_dequeue_drop (ts->rx_fifo, rv);
897 if (rv > hc->to_recv)
899 clib_warning ("http protocol error: received more data than expected");
900 session_transport_closing_notify (&hc->connection);
901 http_disconnect_transport (hc);
902 http_state_change (hc, HTTP_STATE_WAIT_APP_METHOD);
903 return HTTP_SM_ERROR;
906 HTTP_DBG (1, "drained %d from ts; remains %d", rv, hc->to_recv);
908 if (hc->to_recv == 0)
910 hc->rx_buf_offset = 0;
911 vec_reset_length (hc->rx_buf);
912 http_state_change (hc, HTTP_STATE_WAIT_APP_METHOD);
915 app_wrk = app_worker_get_if_valid (as->app_wrk_index);
917 app_worker_rx_notify (app_wrk, as);
919 if (svm_fifo_max_dequeue_cons (ts->rx_fifo))
920 session_enqueue_notify (ts);
925 static http_sm_result_t
926 http_state_app_io_more_data (http_conn_t *hc, transport_send_params_t *sp)
928 u32 max_send = 64 << 10, n_segs;
929 http_buffer_t *hb = &hc->tx_buf;
934 max_send = clib_min (max_send, sp->max_burst_size);
935 ts = session_get_from_handle (hc->h_tc_session_handle);
936 if ((seg = http_buffer_get_segs (hb, max_send, &n_segs)))
937 sent = svm_fifo_enqueue_segments (ts->tx_fifo, seg, n_segs,
938 1 /* allow partial */);
942 /* Ask scheduler to notify app of deq event if needed */
943 sp->bytes_dequeued += http_buffer_drain (hb, sent);
944 sp->max_burst_size -= sent;
947 /* Not finished sending all data */
948 if (!http_buffer_is_drained (hb))
950 if (sent && svm_fifo_set_event (ts->tx_fifo))
951 session_send_io_evt_to_thread (ts->tx_fifo, SESSION_IO_EVT_TX);
953 if (svm_fifo_max_enqueue (ts->tx_fifo) < HTTP_FIFO_THRESH)
955 /* Deschedule http session and wait for deq notification if
956 * underlying ts tx fifo almost full */
957 svm_fifo_add_want_deq_ntf (ts->tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
958 transport_connection_deschedule (&hc->connection);
959 sp->flags |= TRANSPORT_SND_F_DESCHED;
964 if (sent && svm_fifo_set_event (ts->tx_fifo))
965 session_send_io_evt_to_thread (ts->tx_fifo, SESSION_IO_EVT_TX_FLUSH);
967 /* Finished transaction, back to HTTP_STATE_WAIT_METHOD */
968 http_state_change (hc, HTTP_STATE_WAIT_CLIENT_METHOD);
969 http_buffer_free (&hc->tx_buf);
975 typedef http_sm_result_t (*http_sm_handler) (http_conn_t *,
976 transport_send_params_t *sp);
978 static http_sm_handler state_funcs[HTTP_N_STATES] = {
980 http_state_wait_app_method,
981 http_state_wait_client_method,
982 http_state_wait_server_reply,
983 http_state_wait_app_reply,
984 http_state_client_io_more_data,
985 http_state_app_io_more_data,
989 http_req_run_state_machine (http_conn_t *hc, transport_send_params_t *sp)
991 http_sm_result_t res;
995 res = state_funcs[hc->http_state](hc, sp);
996 if (res == HTTP_SM_ERROR)
998 HTTP_DBG (1, "error in state machine %d", res);
1002 while (res == HTTP_SM_CONTINUE);
1004 /* Reset the session expiration timer */
1005 http_conn_timer_update (hc);
1009 http_ts_rx_callback (session_t *ts)
1013 hc = http_conn_get_w_thread (ts->opaque, ts->thread_index);
1016 clib_warning ("http connection not found (ts %d)", ts->opaque);
1020 if (hc->state == HTTP_CONN_STATE_CLOSED)
1022 svm_fifo_dequeue_drop_all (ts->tx_fifo);
1026 http_req_run_state_machine (hc, 0);
1028 if (hc->state == HTTP_CONN_STATE_TRANSPORT_CLOSED)
1030 if (!svm_fifo_max_dequeue_cons (ts->rx_fifo))
1031 session_transport_closing_notify (&hc->connection);
1037 http_ts_builtin_tx_callback (session_t *ts)
1041 hc = http_conn_get_w_thread (ts->opaque, ts->thread_index);
1042 transport_connection_reschedule (&hc->connection);
1048 http_ts_cleanup_callback (session_t *ts, session_cleanup_ntf_t ntf)
1052 if (ntf == SESSION_CLEANUP_TRANSPORT)
1055 hc = http_conn_get_w_thread (ts->opaque, ts->thread_index);
1058 clib_warning ("no http connection for %u", ts->session_index);
1062 vec_free (hc->rx_buf);
1064 http_buffer_free (&hc->tx_buf);
1065 http_conn_timer_stop (hc);
1067 session_transport_delete_notify (&hc->connection);
1068 http_conn_free (hc);
1072 http_add_segment_callback (u32 client_index, u64 segment_handle)
1074 /* No-op for builtin */
1079 http_del_segment_callback (u32 client_index, u64 segment_handle)
1084 static session_cb_vft_t http_app_cb_vft = {
1085 .session_accept_callback = http_ts_accept_callback,
1086 .session_disconnect_callback = http_ts_disconnect_callback,
1087 .session_connected_callback = http_ts_connected_callback,
1088 .session_reset_callback = http_ts_reset_callback,
1089 .session_cleanup_callback = http_ts_cleanup_callback,
1090 .add_segment_callback = http_add_segment_callback,
1091 .del_segment_callback = http_del_segment_callback,
1092 .builtin_app_rx_callback = http_ts_rx_callback,
1093 .builtin_app_tx_callback = http_ts_builtin_tx_callback,
1096 static clib_error_t *
1097 http_transport_enable (vlib_main_t *vm, u8 is_en)
1099 vnet_app_detach_args_t _da, *da = &_da;
1100 vnet_app_attach_args_t _a, *a = &_a;
1101 u64 options[APP_OPTIONS_N_OPTIONS];
1102 http_main_t *hm = &http_main;
1106 da->app_index = hm->app_index;
1107 da->api_client_index = APP_INVALID_INDEX;
1108 vnet_application_detach (da);
1112 vec_validate (hm->wrk, vlib_num_workers ());
1114 clib_memset (a, 0, sizeof (*a));
1115 clib_memset (options, 0, sizeof (options));
1117 a->session_cb_vft = &http_app_cb_vft;
1118 a->api_client_index = APP_INVALID_INDEX;
1119 a->options = options;
1120 a->name = format (0, "http");
1121 a->options[APP_OPTIONS_SEGMENT_SIZE] = hm->first_seg_size;
1122 a->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = hm->add_seg_size;
1123 a->options[APP_OPTIONS_RX_FIFO_SIZE] = hm->fifo_size;
1124 a->options[APP_OPTIONS_TX_FIFO_SIZE] = hm->fifo_size;
1125 a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
1126 a->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
1127 a->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_IS_TRANSPORT_APP;
1129 if (vnet_application_attach (a))
1130 return clib_error_return (0, "failed to attach http app");
1132 hm->app_index = a->app_index;
1135 clib_timebase_init (&hm->timebase, 0 /* GMT */, CLIB_TIMEBASE_DAYLIGHT_NONE,
1136 &vm->clib_time /* share the system clock */);
1138 http_timers_init (vm, http_conn_timeout_cb);
1144 http_transport_connect (transport_endpoint_cfg_t *tep)
1146 vnet_connect_args_t _cargs, *cargs = &_cargs;
1147 http_main_t *hm = &http_main;
1148 session_endpoint_cfg_t *sep = (session_endpoint_cfg_t *) tep;
1153 app_worker_t *app_wrk = app_worker_get (sep->app_wrk_index);
1155 clib_memset (cargs, 0, sizeof (*cargs));
1156 clib_memcpy (&cargs->sep_ext, sep, sizeof (session_endpoint_cfg_t));
1157 cargs->sep.transport_proto = TRANSPORT_PROTO_TCP;
1158 cargs->app_index = hm->app_index;
1159 app = application_get (app_wrk->app_index);
1160 cargs->sep_ext.ns_index = app->ns_index;
1162 hc_index = http_conn_alloc_w_thread (0 /* ts->thread_index */);
1163 hc = http_conn_get_w_thread (hc_index, 0);
1164 hc->h_pa_wrk_index = sep->app_wrk_index;
1165 hc->h_pa_app_api_ctx = sep->opaque;
1166 hc->state = HTTP_CONN_STATE_CONNECTING;
1167 cargs->api_context = hc_index;
1169 if (vec_len (app->name))
1170 hc->app_name = vec_dup (app->name);
1172 hc->app_name = format (0, "VPP HTTP client");
1174 HTTP_DBG (1, "hc ho_index %x", hc_index);
1176 if ((error = vnet_connect (cargs)))
1183 http_start_listen (u32 app_listener_index, transport_endpoint_cfg_t *tep)
1185 vnet_listen_args_t _args = {}, *args = &_args;
1186 session_t *ts_listener, *app_listener;
1187 http_main_t *hm = &http_main;
1188 session_endpoint_cfg_t *sep;
1189 app_worker_t *app_wrk;
1190 transport_proto_t tp;
1196 sep = (session_endpoint_cfg_t *) tep;
1198 app_wrk = app_worker_get (sep->app_wrk_index);
1199 app = application_get (app_wrk->app_index);
1201 args->app_index = hm->app_index;
1202 args->sep_ext = *sep;
1203 args->sep_ext.ns_index = app->ns_index;
1204 tp = sep->ext_cfg ? TRANSPORT_PROTO_TLS : TRANSPORT_PROTO_TCP;
1205 args->sep_ext.transport_proto = tp;
1207 if (vnet_listen (args))
1208 return SESSION_INVALID_INDEX;
1210 lhc_index = http_listener_alloc ();
1211 lhc = http_listener_get (lhc_index);
1213 /* Grab transport connection listener and link to http listener */
1214 lhc->h_tc_session_handle = args->handle;
1215 al = app_listener_get_w_handle (lhc->h_tc_session_handle);
1216 ts_listener = app_listener_get_session (al);
1217 ts_listener->opaque = lhc_index;
1219 /* Grab application listener and link to http listener */
1220 app_listener = listen_session_get (app_listener_index);
1221 lhc->h_pa_wrk_index = sep->app_wrk_index;
1222 lhc->h_pa_session_handle = listen_session_get_handle (app_listener);
1223 lhc->c_s_index = app_listener_index;
1224 lhc->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
1226 if (vec_len (app->name))
1227 lhc->app_name = vec_dup (app->name);
1229 lhc->app_name = format (0, "VPP server app");
1235 http_stop_listen (u32 listener_index)
1240 lhc = http_listener_get (listener_index);
1242 vnet_unlisten_args_t a = {
1243 .handle = lhc->h_tc_session_handle,
1244 .app_index = http_main.app_index,
1245 .wrk_map_index = 0 /* default wrk */
1248 if ((rv = vnet_unlisten (&a)))
1249 clib_warning ("unlisten returned %d", rv);
1251 http_listener_free (lhc);
1257 http_transport_close (u32 hc_index, u32 thread_index)
1262 HTTP_DBG (1, "App disconnecting %x", hc_index);
1264 hc = http_conn_get_w_thread (hc_index, thread_index);
1265 if (hc->state == HTTP_CONN_STATE_CONNECTING)
1267 hc->state = HTTP_CONN_STATE_APP_CLOSED;
1268 http_disconnect_transport (hc);
1271 else if (hc->state == HTTP_CONN_STATE_CLOSED)
1273 HTTP_DBG (1, "nothing to do, already closed");
1276 as = session_get_from_handle (hc->h_pa_session_handle);
1278 /* Nothing more to send, confirm close */
1279 if (!svm_fifo_max_dequeue_cons (as->tx_fifo))
1281 session_transport_closed_notify (&hc->connection);
1282 http_disconnect_transport (hc);
1286 /* Wait for all data to be written to ts */
1287 hc->state = HTTP_CONN_STATE_APP_CLOSED;
1291 static transport_connection_t *
1292 http_transport_get_connection (u32 hc_index, u32 thread_index)
1294 http_conn_t *hc = http_conn_get_w_thread (hc_index, thread_index);
1295 return &hc->connection;
1298 static transport_connection_t *
1299 http_transport_get_listener (u32 listener_index)
1301 http_conn_t *lhc = http_listener_get (listener_index);
1302 return &lhc->connection;
1306 http_app_tx_callback (void *session, transport_send_params_t *sp)
1308 session_t *as = (session_t *) session;
1309 u32 max_burst_sz, sent;
1312 HTTP_DBG (1, "app session conn index %x", as->connection_index);
1314 hc = http_conn_get_w_thread (as->connection_index, as->thread_index);
1315 if (!http_state_is_tx_valid (hc))
1317 if (hc->state != HTTP_CONN_STATE_CLOSED)
1318 clib_warning ("app data req state '%U' session state %u",
1319 format_http_state, hc->http_state, hc->state);
1320 svm_fifo_dequeue_drop_all (as->tx_fifo);
1324 max_burst_sz = sp->max_burst_size * TRANSPORT_PACER_MIN_MSS;
1325 sp->max_burst_size = max_burst_sz;
1327 http_req_run_state_machine (hc, sp);
1329 if (hc->state == HTTP_CONN_STATE_APP_CLOSED)
1331 if (!svm_fifo_max_dequeue_cons (as->tx_fifo))
1332 http_disconnect_transport (hc);
1335 sent = max_burst_sz - sp->max_burst_size;
1337 return sent > 0 ? clib_max (sent / TRANSPORT_PACER_MIN_MSS, 1) : 0;
1341 http_transport_get_endpoint (u32 hc_index, u32 thread_index,
1342 transport_endpoint_t *tep, u8 is_lcl)
1344 http_conn_t *hc = http_conn_get_w_thread (hc_index, thread_index);
1347 ts = session_get_from_handle (hc->h_tc_session_handle);
1348 session_get_endpoint (ts, tep, is_lcl);
1352 format_http_connection (u8 *s, va_list *args)
1354 http_conn_t *hc = va_arg (*args, http_conn_t *);
1357 ts = session_get_from_handle (hc->h_tc_session_handle);
1358 s = format (s, "[%d:%d][H] app_wrk %u ts %d:%d", hc->c_thread_index,
1359 hc->c_s_index, hc->h_pa_wrk_index, ts->thread_index,
1366 format_http_listener (u8 *s, va_list *args)
1368 http_conn_t *lhc = va_arg (*args, http_conn_t *);
1372 al = app_listener_get_w_handle (lhc->h_tc_session_handle);
1373 lts = app_listener_get_session (al);
1374 s = format (s, "[%d:%d][H] app_wrk %u ts %d:%d", lhc->c_thread_index,
1375 lhc->c_s_index, lhc->h_pa_wrk_index, lts->thread_index,
1376 lts->session_index);
1382 format_http_conn_state (u8 *s, va_list *args)
1384 http_conn_t *hc = va_arg (*args, http_conn_t *);
1388 case HTTP_CONN_STATE_LISTEN:
1389 s = format (s, "LISTEN");
1391 case HTTP_CONN_STATE_CONNECTING:
1392 s = format (s, "CONNECTING");
1394 case HTTP_CONN_STATE_ESTABLISHED:
1395 s = format (s, "ESTABLISHED");
1397 case HTTP_CONN_STATE_TRANSPORT_CLOSED:
1398 s = format (s, "TRANSPORT_CLOSED");
1400 case HTTP_CONN_STATE_APP_CLOSED:
1401 s = format (s, "APP_CLOSED");
1403 case HTTP_CONN_STATE_CLOSED:
1404 s = format (s, "CLOSED");
1412 format_http_transport_connection (u8 *s, va_list *args)
1414 u32 tc_index = va_arg (*args, u32);
1415 u32 thread_index = va_arg (*args, u32);
1416 u32 verbose = va_arg (*args, u32);
1419 hc = http_conn_get_w_thread (tc_index, thread_index);
1421 s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_http_connection, hc);
1425 format (s, "%-" SESSION_CLI_STATE_LEN "U", format_http_conn_state, hc);
1427 s = format (s, "\n");
1434 format_http_transport_listener (u8 *s, va_list *args)
1436 u32 tc_index = va_arg (*args, u32);
1437 u32 __clib_unused thread_index = va_arg (*args, u32);
1438 u32 __clib_unused verbose = va_arg (*args, u32);
1439 http_conn_t *lhc = http_listener_get (tc_index);
1441 s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_http_listener, lhc);
1444 format (s, "%-" SESSION_CLI_STATE_LEN "U", format_http_conn_state, lhc);
1448 static const transport_proto_vft_t http_proto = {
1449 .enable = http_transport_enable,
1450 .connect = http_transport_connect,
1451 .start_listen = http_start_listen,
1452 .stop_listen = http_stop_listen,
1453 .close = http_transport_close,
1454 .custom_tx = http_app_tx_callback,
1455 .get_connection = http_transport_get_connection,
1456 .get_listener = http_transport_get_listener,
1457 .get_transport_endpoint = http_transport_get_endpoint,
1458 .format_connection = format_http_transport_connection,
1459 .format_listener = format_http_transport_listener,
1460 .transport_options = {
1463 .tx_type = TRANSPORT_TX_INTERNAL,
1464 .service_type = TRANSPORT_SERVICE_APP,
1468 static clib_error_t *
1469 http_transport_init (vlib_main_t *vm)
1471 http_main_t *hm = &http_main;
1473 transport_register_protocol (TRANSPORT_PROTO_HTTP, &http_proto,
1474 FIB_PROTOCOL_IP4, ~0);
1475 transport_register_protocol (TRANSPORT_PROTO_HTTP, &http_proto,
1476 FIB_PROTOCOL_IP6, ~0);
1478 /* Default values, configurable via startup conf */
1479 hm->add_seg_size = 256 << 20;
1480 hm->first_seg_size = 32 << 20;
1481 hm->fifo_size = 512 << 10;
1486 VLIB_INIT_FUNCTION (http_transport_init);
1488 static clib_error_t *
1489 http_config_fn (vlib_main_t *vm, unformat_input_t *input)
1491 http_main_t *hm = &http_main;
1494 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1496 if (unformat (input, "first-segment-size %U", unformat_memory_size,
1499 hm->first_seg_size = clib_max (mem_sz, 1 << 20);
1500 if (hm->first_seg_size != mem_sz)
1501 clib_warning ("first seg size too small %u", mem_sz);
1503 else if (unformat (input, "add-segment-size %U", unformat_memory_size,
1506 hm->add_seg_size = clib_max (mem_sz, 1 << 20);
1507 if (hm->add_seg_size != mem_sz)
1508 clib_warning ("add seg size too small %u", mem_sz);
1510 else if (unformat (input, "fifo-size %U", unformat_memory_size, &mem_sz))
1512 hm->fifo_size = clib_clamp (mem_sz, 4 << 10, 2 << 30);
1513 if (hm->fifo_size != mem_sz)
1514 clib_warning ("invalid fifo size %lu", mem_sz);
1517 return clib_error_return (0, "unknown input `%U'",
1518 format_unformat_error, input);
1523 VLIB_CONFIG_FUNCTION (http_config_fn, "http");
1525 VLIB_PLUGIN_REGISTER () = {
1526 .version = VPP_BUILD_VER,
1527 .description = "Hypertext Transfer Protocol (HTTP)",
1528 .default_disabled = 0,
1532 * fd.io coding-style-patch-verification: ON
1535 * eval: (c-set-style "gnu")