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, 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,
50 static inline http_worker_t *
51 http_worker_get (u32 thread_index)
53 return &http_main.wrk[thread_index];
57 http_conn_alloc_w_thread (u32 thread_index)
59 http_worker_t *wrk = http_worker_get (thread_index);
62 pool_get_zero (wrk->conn_pool, hc);
63 hc->c_thread_index = thread_index;
64 hc->h_hc_index = hc - wrk->conn_pool;
65 hc->h_pa_session_handle = SESSION_INVALID_HANDLE;
66 hc->h_tc_session_handle = SESSION_INVALID_HANDLE;
67 return hc->h_hc_index;
70 static inline http_conn_t *
71 http_conn_get_w_thread (u32 hc_index, u32 thread_index)
73 http_worker_t *wrk = http_worker_get (thread_index);
74 return pool_elt_at_index (wrk->conn_pool, hc_index);
78 http_conn_free (http_conn_t *hc)
80 http_worker_t *wrk = http_worker_get (hc->c_thread_index);
81 pool_put (wrk->conn_pool, hc);
85 http_listener_alloc (void)
87 http_main_t *hm = &http_main;
90 pool_get_zero (hm->listener_pool, lhc);
91 lhc->c_c_index = lhc - hm->listener_pool;
92 return lhc->c_c_index;
96 http_listener_get (u32 lhc_index)
98 return pool_elt_at_index (http_main.listener_pool, lhc_index);
102 http_listener_free (http_conn_t *lhc)
104 http_main_t *hm = &http_main;
107 memset (lhc, 0xfc, sizeof (*lhc));
108 pool_put (hm->listener_pool, lhc);
112 http_disconnect_transport (http_conn_t *hc)
114 vnet_disconnect_args_t a = {
115 .handle = hc->h_tc_session_handle,
116 .app_index = http_main.app_index,
119 hc->state = HTTP_CONN_STATE_CLOSED;
121 if (vnet_disconnect_session (&a))
122 clib_warning ("disconnect returned");
126 http_conn_timeout_cb (void *hc_handlep)
131 hs_handle = pointer_to_uword (hc_handlep);
132 hc = http_conn_get_w_thread (hs_handle & 0x00FFFFFF, hs_handle >> 24);
134 HTTP_DBG (1, "terminate thread %d index %d hs %llx", hs_handle >> 24,
135 hs_handle & 0x00FFFFFF, hc);
139 hc->timer_handle = ~0;
140 session_transport_closing_notify (&hc->connection);
141 http_disconnect_transport (hc);
145 http_ts_accept_callback (session_t *ts)
147 session_t *ts_listener, *as, *asl;
148 app_worker_t *app_wrk;
149 http_conn_t *lhc, *hc;
150 u32 hc_index, thresh;
153 ts_listener = listen_session_get_from_handle (ts->listener_handle);
154 lhc = http_listener_get (ts_listener->opaque);
156 hc_index = http_conn_alloc_w_thread (ts->thread_index);
157 hc = http_conn_get_w_thread (hc_index, ts->thread_index);
158 clib_memcpy_fast (hc, lhc, sizeof (*lhc));
159 hc->c_thread_index = ts->thread_index;
160 hc->h_hc_index = hc_index;
162 hc->h_tc_session_handle = session_handle (ts);
163 hc->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
165 hc->state = HTTP_CONN_STATE_ESTABLISHED;
166 hc->http_state = HTTP_STATE_WAIT_METHOD;
168 ts->session_state = SESSION_STATE_READY;
169 ts->opaque = hc_index;
172 * Alloc session and initialize
174 as = session_alloc (hc->c_thread_index);
175 hc->c_s_index = as->session_index;
177 as->app_wrk_index = hc->h_pa_wrk_index;
178 as->connection_index = hc->c_c_index;
179 as->session_state = SESSION_STATE_ACCEPTING;
181 asl = listen_session_get_from_handle (lhc->h_pa_session_handle);
182 as->session_type = asl->session_type;
183 as->listener_handle = lhc->h_pa_session_handle;
186 * Init session fifos and notify app
188 if ((rv = app_worker_init_accepted (as)))
190 HTTP_DBG (1, "failed to allocate fifos");
195 hc->h_pa_session_handle = session_handle (as);
196 hc->h_pa_wrk_index = as->app_wrk_index;
197 app_wrk = app_worker_get (as->app_wrk_index);
199 HTTP_DBG (1, "Accepted on listener %u new connection [%u]%x",
200 ts_listener->opaque, vlib_get_thread_index (), hc_index);
202 if ((rv = app_worker_accept_notify (app_wrk, as)))
204 HTTP_DBG (0, "app accept returned");
209 /* Avoid enqueuing small chunks of data on transport tx notifications. If
210 * the fifo is small (under 16K) we set the threshold to it's size, meaning
211 * a notification will be given when the fifo empties.
213 ts = session_get_from_handle (hc->h_tc_session_handle);
214 thresh = clib_min (svm_fifo_size (ts->tx_fifo), HTTP_FIFO_THRESH);
215 svm_fifo_set_deq_thresh (ts->tx_fifo, thresh);
217 http_conn_timer_start (hc);
223 http_ts_connected_callback (u32 http_app_index, u32 ho_hc_index, session_t *ts,
228 http_conn_t *hc, *ho_hc;
229 app_worker_t *app_wrk;
234 clib_warning ("ERROR: %d", err);
238 new_hc_index = http_conn_alloc_w_thread (ts->thread_index);
239 hc = http_conn_get_w_thread (new_hc_index, ts->thread_index);
240 ho_hc = http_conn_get_w_thread (ho_hc_index, 0);
242 ASSERT (ho_hc->state == HTTP_CONN_STATE_CONNECTING);
244 clib_memcpy_fast (hc, ho_hc, sizeof (*hc));
246 hc->c_thread_index = ts->thread_index;
247 hc->h_tc_session_handle = session_handle (ts);
248 hc->c_c_index = new_hc_index;
249 hc->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
250 hc->state = HTTP_CONN_STATE_ESTABLISHED;
251 hc->http_state = HTTP_STATE_WAIT_APP;
253 ts->session_state = SESSION_STATE_READY;
254 ts->opaque = new_hc_index;
256 /* allocate app session and initialize */
258 as = session_alloc (hc->c_thread_index);
259 hc->c_s_index = as->session_index;
260 as->connection_index = hc->c_c_index;
261 as->app_wrk_index = hc->h_pa_wrk_index;
262 as->session_state = SESSION_STATE_READY;
263 as->session_type = session_type_from_proto_and_ip (
264 TRANSPORT_PROTO_HTTP, session_type_is_ip4 (ts->session_type));
266 app_wrk = app_worker_get (hc->h_pa_wrk_index);
269 clib_warning ("no app worker");
273 if ((rv = app_worker_init_connected (app_wrk, as)))
275 HTTP_DBG (1, "failed to allocate fifos");
279 app_worker_connect_notify (app_wrk, as, err, hc->h_pa_app_api_ctx);
280 hc->h_pa_session_handle = session_handle (as);
281 http_conn_timer_start (hc);
287 http_ts_disconnect_callback (session_t *ts)
291 hc = http_conn_get_w_thread (ts->opaque, ts->thread_index);
293 if (hc->state < HTTP_CONN_STATE_TRANSPORT_CLOSED)
294 hc->state = HTTP_CONN_STATE_TRANSPORT_CLOSED;
296 /* Nothing more to rx, propagate to app */
297 if (!svm_fifo_max_dequeue_cons (ts->rx_fifo))
298 session_transport_closing_notify (&hc->connection);
302 http_ts_reset_callback (session_t *ts)
306 hc = http_conn_get_w_thread (ts->opaque, ts->thread_index);
308 hc->state = HTTP_CONN_STATE_CLOSED;
309 http_buffer_free (&hc->tx_buf);
310 hc->http_state = HTTP_STATE_WAIT_METHOD;
311 session_transport_reset_notify (&hc->connection);
313 http_disconnect_transport (hc);
317 * http error boilerplate
319 static const char *http_error_template = "HTTP/1.1 %s\r\n"
321 "Content-Type: text/html\r\n"
322 "Connection: close\r\n"
323 "Pragma: no-cache\r\n"
324 "Content-Length: 0\r\n\r\n";
327 * http response boilerplate
329 static const char *http_response_template = "HTTP/1.1 200 OK\r\n"
331 "Expires: %U GMT\r\n"
332 "Server: VPP Static\r\n"
333 "Content-Type: %s\r\n"
334 "Content-Length: %lu\r\n\r\n";
336 static const char *http_request_template = "GET %s HTTP/1.1\r\n"
337 "User-Agent: VPP HTTP client\r\n"
341 send_data (http_conn_t *hc, u8 *data, u32 length, u32 offset)
343 const u32 max_burst = 64 << 10;
348 ts = session_get_from_handle (hc->h_tc_session_handle);
350 to_send = clib_min (length - offset, max_burst);
351 sent = svm_fifo_enqueue (ts->tx_fifo, to_send, data + offset);
356 if (svm_fifo_set_event (ts->tx_fifo))
357 session_send_io_evt_to_thread (ts->tx_fifo, SESSION_IO_EVT_TX);
359 return (offset + sent);
363 send_error (http_conn_t *hc, http_status_code_t ec)
365 http_main_t *hm = &http_main;
369 if (ec >= HTTP_N_STATUS)
370 ec = HTTP_STATUS_INTERNAL_ERROR;
372 now = clib_timebase_now (&hm->timebase);
373 data = format (0, http_error_template, http_status_code_str[ec],
374 format_clib_timebase_time, now);
375 send_data (hc, data, vec_len (data), 0);
380 read_http_message (http_conn_t *hc)
382 u32 max_deq, cursize;
386 ts = session_get_from_handle (hc->h_tc_session_handle);
388 cursize = vec_len (hc->rx_buf);
389 max_deq = svm_fifo_max_dequeue (ts->rx_fifo);
390 if (PREDICT_FALSE (max_deq == 0))
393 vec_validate (hc->rx_buf, cursize + max_deq - 1);
394 n_read = svm_fifo_dequeue (ts->rx_fifo, max_deq, hc->rx_buf + cursize);
395 ASSERT (n_read == max_deq);
397 if (svm_fifo_is_empty (ts->rx_fifo))
398 svm_fifo_unset_event (ts->rx_fifo);
400 vec_set_len (hc->rx_buf, cursize + n_read);
405 v_find_index (u8 *vec, u32 offset, char *str)
407 int start_index = offset;
408 u32 slen = (u32) strnlen_s_inline (str, 16);
409 u32 vlen = vec_len (vec);
416 for (; start_index < (vlen - slen); start_index++)
418 if (!memcmp (vec + start_index, str, slen))
426 * waiting for request method from peer - parse request method and data
428 static http_sm_result_t
429 state_srv_wait_method (http_conn_t *hc, transport_send_params_t *sp)
431 http_status_code_t ec;
432 app_worker_t *app_wrk;
439 rv = read_http_message (hc);
441 /* Nothing yet, wait for data or timer expire */
445 if (vec_len (hc->rx_buf) < 8)
447 ec = HTTP_STATUS_BAD_REQUEST;
451 if ((i = v_find_index (hc->rx_buf, 0, "GET ")) >= 0)
453 hc->method = HTTP_REQ_GET;
454 hc->rx_buf_offset = i + 5;
456 i = v_find_index (hc->rx_buf, hc->rx_buf_offset, "HTTP");
459 ec = HTTP_STATUS_BAD_REQUEST;
463 len = i - hc->rx_buf_offset - 1;
465 else if ((i = v_find_index (hc->rx_buf, 0, "POST ")) >= 0)
467 hc->method = HTTP_REQ_POST;
468 hc->rx_buf_offset = i + 6;
469 len = vec_len (hc->rx_buf) - hc->rx_buf_offset - 1;
473 HTTP_DBG (0, "Unknown http method");
474 ec = HTTP_STATUS_METHOD_NOT_ALLOWED;
478 buf = &hc->rx_buf[hc->rx_buf_offset];
480 msg.type = HTTP_MSG_REQUEST;
481 msg.method_type = hc->method;
482 msg.content_type = HTTP_CONTENT_TEXT_HTML;
483 msg.data.type = HTTP_MSG_DATA_INLINE;
486 svm_fifo_seg_t segs[2] = { { (u8 *) &msg, sizeof (msg) }, { buf, len } };
488 as = session_get_from_handle (hc->h_pa_session_handle);
489 rv = svm_fifo_enqueue_segments (as->rx_fifo, segs, 2, 0 /* allow partial */);
490 if (rv < 0 || rv != sizeof (msg) + len)
492 clib_warning ("failed app enqueue");
493 /* This should not happen as we only handle 1 request per session,
494 * and fifo is allocated, but going forward we should consider
496 return HTTP_SM_ERROR;
499 vec_free (hc->rx_buf);
500 hc->http_state = HTTP_STATE_WAIT_APP;
502 app_wrk = app_worker_get_if_valid (as->app_wrk_index);
503 app_worker_lock_and_send_event (app_wrk, as, SESSION_IO_EVT_RX);
510 session_transport_closing_notify (&hc->connection);
511 http_disconnect_transport (hc);
513 return HTTP_SM_ERROR;
517 * waiting for data from app
519 static http_sm_result_t
520 state_srv_wait_app (http_conn_t *hc, transport_send_params_t *sp)
522 http_main_t *hm = &http_main;
523 http_status_code_t ec;
531 as = session_get_from_handle (hc->h_pa_session_handle);
533 rv = svm_fifo_dequeue (as->tx_fifo, sizeof (msg), (u8 *) &msg);
534 ASSERT (rv == sizeof (msg));
536 if (msg.type != HTTP_MSG_REPLY || msg.data.type > HTTP_MSG_DATA_PTR)
538 clib_warning ("unexpected msg type from app %u", msg.type);
539 ec = HTTP_STATUS_INTERNAL_ERROR;
543 if (msg.code != HTTP_STATUS_OK)
549 http_buffer_init (&hc->tx_buf, msg_to_buf_type[msg.data.type], as->tx_fifo,
553 * Add headers. For now:
559 now = clib_timebase_now (&hm->timebase);
560 header = format (0, http_response_template,
562 format_clib_timebase_time, now,
564 format_clib_timebase_time, now + 600.0,
566 http_content_type_str[msg.content_type],
570 offset = send_data (hc, header, vec_len (header), 0);
571 if (offset != vec_len (header))
573 clib_warning ("couldn't send response header!");
574 ec = HTTP_STATUS_INTERNAL_ERROR;
579 /* Start sending the actual data */
580 hc->http_state = HTTP_STATE_IO_MORE_DATA;
582 ASSERT (sp->max_burst_size >= offset);
583 sp->max_burst_size -= offset;
585 return HTTP_SM_CONTINUE;
590 hc->http_state = HTTP_STATE_WAIT_METHOD;
591 session_transport_closing_notify (&hc->connection);
592 http_disconnect_transport (hc);
597 static http_sm_result_t
598 state_srv_send_more_data (http_conn_t *hc, transport_send_params_t *sp)
600 u32 max_send = 64 << 10, n_segs;
601 http_buffer_t *hb = &hc->tx_buf;
606 max_send = clib_min (max_send, sp->max_burst_size);
607 ts = session_get_from_handle (hc->h_tc_session_handle);
608 if ((seg = http_buffer_get_segs (hb, max_send, &n_segs)))
609 sent = svm_fifo_enqueue_segments (ts->tx_fifo, seg, n_segs,
610 1 /* allow partial */);
614 /* Ask scheduler to notify app of deq event if needed */
615 sp->bytes_dequeued += http_buffer_drain (hb, sent);
616 sp->max_burst_size -= sent;
619 /* Not finished sending all data */
620 if (!http_buffer_is_drained (hb))
622 if (sent && svm_fifo_set_event (ts->tx_fifo))
623 session_send_io_evt_to_thread (ts->tx_fifo, SESSION_IO_EVT_TX);
625 if (svm_fifo_max_enqueue (ts->tx_fifo) < HTTP_FIFO_THRESH)
627 /* Deschedule http session and wait for deq notification if
628 * underlying ts tx fifo almost full */
629 svm_fifo_add_want_deq_ntf (ts->tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
630 transport_connection_deschedule (&hc->connection);
631 sp->flags |= TRANSPORT_SND_F_DESCHED;
636 if (sent && svm_fifo_set_event (ts->tx_fifo))
637 session_send_io_evt_to_thread (ts->tx_fifo, SESSION_IO_EVT_TX_FLUSH);
639 /* Finished transaction, back to HTTP_STATE_WAIT_METHOD */
640 hc->http_state = HTTP_STATE_WAIT_METHOD;
641 http_buffer_free (&hc->tx_buf);
648 parse_http_header (http_conn_t *hc, int *content_length)
650 unformat_input_t input;
654 if ((i = v_find_index (hc->rx_buf, hc->rx_buf_offset, "200 OK") < 0))
656 clib_warning ("bad response code");
660 i = v_find_index (hc->rx_buf, hc->rx_buf_offset, CONTENT_LEN_STR);
663 clib_warning ("cannot find '%s' in the header!", CONTENT_LEN_STR);
667 hc->rx_buf_offset = i;
669 i = v_find_index (hc->rx_buf, hc->rx_buf_offset, "\n");
672 clib_warning ("end of line missing; incomplete data");
676 len = i - hc->rx_buf_offset;
677 line = vec_new (u8, len);
678 clib_memcpy (line, hc->rx_buf + hc->rx_buf_offset, len);
680 unformat_init_vector (&input, line);
681 if (!unformat (&input, CONTENT_LEN_STR "%d", content_length))
683 clib_warning ("failed to unformat content length!");
686 unformat_free (&input);
688 /* skip rest of the header */
689 hc->rx_buf_offset += len;
690 i = v_find_index (hc->rx_buf, hc->rx_buf_offset, "<html>");
693 clib_warning ("<html> tag not found");
696 hc->rx_buf_offset = i;
702 state_cln_wait_method (http_conn_t *hc, transport_send_params_t *sp)
706 app_worker_t *app_wrk;
707 int rv, content_length;
709 rv = read_http_message (hc);
713 msg.type = HTTP_MSG_REPLY;
714 msg.content_type = HTTP_CONTENT_TEXT_HTML;
715 msg.code = HTTP_STATUS_OK;
716 msg.data.type = HTTP_MSG_DATA_INLINE;
719 rv = parse_http_header (hc, &content_length);
722 clib_warning ("failed to parse http reply");
723 session_transport_closing_notify (&hc->connection);
724 http_disconnect_transport (hc);
728 msg.data.len = content_length;
729 u32 dlen = vec_len (hc->rx_buf) - hc->rx_buf_offset;
730 as = session_get_from_handle (hc->h_pa_session_handle);
731 svm_fifo_seg_t segs[2] = { { (u8 *) &msg, sizeof (msg) },
732 { &hc->rx_buf[hc->rx_buf_offset], dlen } };
734 rv = svm_fifo_enqueue_segments (as->rx_fifo, segs, 2, 0 /* allow partial */);
737 clib_warning ("error enqueue");
738 return HTTP_SM_ERROR;
740 hc->rx_buf_offset += dlen;
741 hc->http_state = HTTP_STATE_IO_MORE_DATA;
742 hc->to_recv = content_length - dlen;
744 if (hc->rx_buf_offset == vec_len (hc->rx_buf))
746 vec_reset_length (hc->rx_buf);
747 hc->rx_buf_offset = 0;
750 if (hc->to_recv == 0)
752 hc->rx_buf_offset = 0;
753 vec_reset_length (hc->rx_buf);
754 hc->http_state = HTTP_STATE_WAIT_APP;
757 app_wrk = app_worker_get_if_valid (as->app_wrk_index);
758 app_worker_lock_and_send_event (app_wrk, as, SESSION_IO_EVT_RX);
763 cln_drain_rx_buf (http_conn_t *hc, session_t *ts, session_t *as)
765 app_worker_t *app_wrk;
766 u32 max_enq, n_enq, dlen = vec_len (hc->rx_buf) - hc->rx_buf_offset;
769 max_enq = svm_fifo_max_enqueue (as->rx_fifo);
770 n_enq = clib_min (max_enq, dlen);
771 rv = svm_fifo_enqueue (as->rx_fifo, n_enq, &hc->rx_buf[hc->rx_buf_offset]);
774 clib_warning ("enqueue failed");
778 hc->rx_buf_offset += rv;
780 if (hc->rx_buf_offset >= vec_len (hc->rx_buf))
782 vec_reset_length (hc->rx_buf);
783 hc->rx_buf_offset = 0;
786 app_wrk = app_worker_get_if_valid (as->app_wrk_index);
789 app_worker_lock_and_send_event (app_wrk, as, SESSION_IO_EVT_RX);
793 static http_sm_result_t
794 state_cln_recv_more_data (http_conn_t *hc, transport_send_params_t *sp)
801 as = session_get_from_handle (hc->h_pa_session_handle);
802 ts = session_get_from_handle (hc->h_tc_session_handle);
804 u32 dlen = vec_len (hc->rx_buf) - hc->rx_buf_offset;
807 rv = cln_drain_rx_buf (hc, ts, as);
810 clib_warning ("drain rx error!");
811 return HTTP_SM_ERROR;
813 goto maybe_reschedule;
816 if (hc->to_recv == 0)
818 ASSERT (vec_len (hc->rx_buf) == 0);
819 ASSERT (hc->rx_buf_offset == 0);
820 hc->http_state = HTTP_STATE_WAIT_APP;
824 max_deq = svm_fifo_max_dequeue (ts->rx_fifo);
828 ASSERT (vec_len (hc->rx_buf) == 0);
829 ASSERT (hc->rx_buf_offset == 0);
831 vec_validate (hc->rx_buf, max_deq - 1);
832 n_read = svm_fifo_dequeue (ts->rx_fifo, max_deq, hc->rx_buf);
833 ASSERT (n_read == max_deq);
835 if (svm_fifo_is_empty (ts->rx_fifo))
836 svm_fifo_unset_event (ts->rx_fifo);
838 hc->to_recv -= n_read;
839 vec_set_len (hc->rx_buf, max_deq);
842 if (hc->rx_buf_offset < vec_len (hc->rx_buf) ||
843 svm_fifo_max_dequeue_cons (ts->rx_fifo))
845 if (svm_fifo_set_event (ts->rx_fifo))
846 session_send_io_evt_to_thread (ts->rx_fifo, SESSION_IO_EVT_BUILTIN_RX);
848 return HTTP_SM_CONTINUE;
851 static http_sm_result_t
852 state_cln_wait_app (http_conn_t *hc, transport_send_params_t *sp)
856 http_status_code_t ec;
857 u8 *buf = 0, *request;
861 as = session_get_from_handle (hc->h_pa_session_handle);
862 rv = svm_fifo_dequeue (as->tx_fifo, sizeof (msg), (u8 *) &msg);
863 ASSERT (rv == sizeof (msg));
864 if (msg.type != HTTP_MSG_REQUEST || msg.data.type > HTTP_MSG_DATA_PTR)
866 clib_warning ("unexpected msg type from app %u", msg.type);
867 ec = HTTP_STATUS_INTERNAL_ERROR;
871 vec_validate (buf, msg.data.len - 1);
872 rv = svm_fifo_dequeue (as->tx_fifo, msg.data.len, buf);
873 ASSERT (rv == msg.data.len);
875 request = format (0, http_request_template, buf);
876 offset = send_data (hc, request, vec_len (request), 0);
877 if (offset != vec_len (request))
879 clib_warning ("sending request failed!");
880 ec = HTTP_STATUS_INTERNAL_ERROR;
884 hc->http_state = HTTP_STATE_WAIT_METHOD;
889 return HTTP_SM_CONTINUE;
893 session_transport_closing_notify (&hc->connection);
894 http_disconnect_transport (hc);
898 typedef http_sm_result_t (*http_sm_handler) (http_conn_t *,
899 transport_send_params_t *sp);
901 static http_sm_handler srv_state_funcs[HTTP_N_STATES] = {
902 /* Waiting for GET, POST, etc. */
903 state_srv_wait_method,
904 /* Wait for data from app */
907 state_srv_send_more_data,
910 static http_sm_handler cln_state_funcs[HTTP_N_STATES] = {
912 state_cln_wait_method,
913 /* wait for data from app */
915 /* receive more data */
916 state_cln_recv_more_data,
920 http_req_run_state_machine (http_conn_t *hc, transport_send_params_t *sp)
922 http_sm_result_t res;
923 http_sm_handler *state_fn =
924 hc->is_client ? cln_state_funcs : srv_state_funcs;
927 res = state_fn[hc->http_state](hc, sp);
928 if (res == HTTP_SM_ERROR)
931 while (res == HTTP_SM_CONTINUE);
933 /* Reset the session expiration timer */
934 http_conn_timer_update (hc);
938 http_ts_server_rx_callback (session_t *ts, http_conn_t *hc)
940 if (hc->http_state != HTTP_STATE_WAIT_METHOD)
942 clib_warning ("tcp data in req state %u", hc->http_state);
946 http_req_run_state_machine (hc, 0);
948 if (hc->state == HTTP_CONN_STATE_TRANSPORT_CLOSED)
950 if (!svm_fifo_max_dequeue_cons (ts->rx_fifo))
951 session_transport_closing_notify (&hc->connection);
957 http_ts_client_rx_callback (session_t *ts, http_conn_t *hc)
959 if (hc->http_state != HTTP_STATE_WAIT_METHOD &&
960 hc->http_state != HTTP_STATE_IO_MORE_DATA)
962 clib_warning ("http in unexpected state %d (ts %d)", hc->http_state,
967 http_req_run_state_machine (hc, 0);
969 if (hc->state == HTTP_CONN_STATE_TRANSPORT_CLOSED)
971 if (!svm_fifo_max_dequeue_cons (ts->rx_fifo))
972 session_transport_closing_notify (&hc->connection);
978 http_ts_rx_callback (session_t *ts)
982 hc = http_conn_get_w_thread (ts->opaque, ts->thread_index);
984 return http_ts_client_rx_callback (ts, hc);
985 return http_ts_server_rx_callback (ts, hc);
989 http_ts_builtin_tx_callback (session_t *ts)
993 hc = http_conn_get_w_thread (ts->opaque, ts->thread_index);
994 transport_connection_reschedule (&hc->connection);
1000 http_ts_cleanup_callback (session_t *ts, session_cleanup_ntf_t ntf)
1004 if (ntf == SESSION_CLEANUP_TRANSPORT)
1007 hc = http_conn_get_w_thread (ts->opaque, ts->thread_index);
1010 clib_warning ("no http connection for %u", ts->session_index);
1014 vec_free (hc->rx_buf);
1016 http_buffer_free (&hc->tx_buf);
1017 http_conn_timer_stop (hc);
1019 session_transport_delete_notify (&hc->connection);
1020 http_conn_free (hc);
1024 http_add_segment_callback (u32 client_index, u64 segment_handle)
1026 /* No-op for builtin */
1031 http_del_segment_callback (u32 client_index, u64 segment_handle)
1036 static session_cb_vft_t http_app_cb_vft = {
1037 .session_accept_callback = http_ts_accept_callback,
1038 .session_disconnect_callback = http_ts_disconnect_callback,
1039 .session_connected_callback = http_ts_connected_callback,
1040 .session_reset_callback = http_ts_reset_callback,
1041 .session_cleanup_callback = http_ts_cleanup_callback,
1042 .add_segment_callback = http_add_segment_callback,
1043 .del_segment_callback = http_del_segment_callback,
1044 .builtin_app_rx_callback = http_ts_rx_callback,
1045 .builtin_app_tx_callback = http_ts_builtin_tx_callback,
1048 static clib_error_t *
1049 http_transport_enable (vlib_main_t *vm, u8 is_en)
1051 vnet_app_detach_args_t _da, *da = &_da;
1052 vnet_app_attach_args_t _a, *a = &_a;
1053 u64 options[APP_OPTIONS_N_OPTIONS];
1054 http_main_t *hm = &http_main;
1058 da->app_index = hm->app_index;
1059 da->api_client_index = APP_INVALID_INDEX;
1060 vnet_application_detach (da);
1064 vec_validate (hm->wrk, vlib_num_workers ());
1066 clib_memset (a, 0, sizeof (*a));
1067 clib_memset (options, 0, sizeof (options));
1069 a->session_cb_vft = &http_app_cb_vft;
1070 a->api_client_index = APP_INVALID_INDEX;
1071 a->options = options;
1072 a->name = format (0, "http");
1073 a->options[APP_OPTIONS_SEGMENT_SIZE] = hm->first_seg_size;
1074 a->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = hm->add_seg_size;
1075 a->options[APP_OPTIONS_RX_FIFO_SIZE] = hm->fifo_size;
1076 a->options[APP_OPTIONS_TX_FIFO_SIZE] = hm->fifo_size;
1077 a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
1078 a->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
1079 a->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_IS_TRANSPORT_APP;
1081 if (vnet_application_attach (a))
1082 return clib_error_return (0, "failed to attach http app");
1084 hm->app_index = a->app_index;
1087 clib_timebase_init (&hm->timebase, 0 /* GMT */, CLIB_TIMEBASE_DAYLIGHT_NONE,
1088 &vm->clib_time /* share the system clock */);
1090 http_timers_init (vm, http_conn_timeout_cb);
1096 http_transport_connect (transport_endpoint_cfg_t *tep)
1098 vnet_connect_args_t _cargs, *cargs = &_cargs;
1099 http_main_t *hm = &http_main;
1100 session_endpoint_cfg_t *sep = (session_endpoint_cfg_t *) tep;
1105 app_worker_t *app_wrk = app_worker_get (sep->app_wrk_index);
1107 clib_memset (cargs, 0, sizeof (*cargs));
1108 clib_memcpy (&cargs->sep_ext, sep, sizeof (session_endpoint_cfg_t));
1109 cargs->sep.transport_proto = TRANSPORT_PROTO_TCP;
1110 cargs->app_index = hm->app_index;
1111 app = application_get (app_wrk->app_index);
1112 cargs->sep_ext.ns_index = app->ns_index;
1114 hc_index = http_conn_alloc_w_thread (0 /* ts->thread_index */);
1115 hc = http_conn_get_w_thread (hc_index, 0);
1116 hc->h_pa_wrk_index = sep->app_wrk_index;
1117 hc->h_pa_app_api_ctx = sep->opaque;
1119 hc->state = HTTP_CONN_STATE_CONNECTING;
1120 cargs->api_context = hc_index;
1122 if ((error = vnet_connect (cargs)))
1129 http_start_listen (u32 app_listener_index, transport_endpoint_cfg_t *tep)
1131 vnet_listen_args_t _args = {}, *args = &_args;
1132 session_t *ts_listener, *app_listener;
1133 http_main_t *hm = &http_main;
1134 session_endpoint_cfg_t *sep;
1135 app_worker_t *app_wrk;
1136 transport_proto_t tp;
1142 sep = (session_endpoint_cfg_t *) tep;
1144 app_wrk = app_worker_get (sep->app_wrk_index);
1145 app = application_get (app_wrk->app_index);
1147 args->app_index = hm->app_index;
1148 args->sep_ext = *sep;
1149 args->sep_ext.ns_index = app->ns_index;
1150 tp = sep->ext_cfg ? TRANSPORT_PROTO_TLS : TRANSPORT_PROTO_TCP;
1151 args->sep_ext.transport_proto = tp;
1153 if (vnet_listen (args))
1154 return SESSION_INVALID_INDEX;
1156 lhc_index = http_listener_alloc ();
1157 lhc = http_listener_get (lhc_index);
1159 /* Grab transport connection listener and link to http listener */
1160 lhc->h_tc_session_handle = args->handle;
1161 al = app_listener_get_w_handle (lhc->h_tc_session_handle);
1162 ts_listener = app_listener_get_session (al);
1163 ts_listener->opaque = lhc_index;
1165 /* Grab application listener and link to http listener */
1166 app_listener = listen_session_get (app_listener_index);
1167 lhc->h_pa_wrk_index = sep->app_wrk_index;
1168 lhc->h_pa_session_handle = listen_session_get_handle (app_listener);
1169 lhc->c_s_index = app_listener_index;
1170 lhc->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
1176 http_stop_listen (u32 listener_index)
1181 lhc = http_listener_get (listener_index);
1183 vnet_unlisten_args_t a = {
1184 .handle = lhc->h_tc_session_handle,
1185 .app_index = http_main.app_index,
1186 .wrk_map_index = 0 /* default wrk */
1189 if ((rv = vnet_unlisten (&a)))
1190 clib_warning ("unlisten returned %d", rv);
1192 http_listener_free (lhc);
1198 http_transport_close (u32 hc_index, u32 thread_index)
1203 HTTP_DBG (1, "App disconnecting %x", hc_index);
1205 hc = http_conn_get_w_thread (hc_index, thread_index);
1206 if (hc->state == HTTP_CONN_STATE_CONNECTING)
1208 hc->state = HTTP_CONN_STATE_APP_CLOSED;
1209 http_disconnect_transport (hc);
1213 as = session_get_from_handle (hc->h_pa_session_handle);
1215 /* Nothing more to send, confirm close */
1216 if (!svm_fifo_max_dequeue_cons (as->tx_fifo))
1218 session_transport_closed_notify (&hc->connection);
1219 http_disconnect_transport (hc);
1223 /* Wait for all data to be written to ts */
1224 hc->state = HTTP_CONN_STATE_APP_CLOSED;
1228 static transport_connection_t *
1229 http_transport_get_connection (u32 hc_index, u32 thread_index)
1231 http_conn_t *hc = http_conn_get_w_thread (hc_index, thread_index);
1232 return &hc->connection;
1235 static transport_connection_t *
1236 http_transport_get_listener (u32 listener_index)
1238 http_conn_t *lhc = http_listener_get (listener_index);
1239 return &lhc->connection;
1243 http_app_tx_callback (void *session, transport_send_params_t *sp)
1245 session_t *as = (session_t *) session;
1246 u32 max_burst_sz, sent;
1249 hc = http_conn_get_w_thread (as->connection_index, as->thread_index);
1250 if (hc->http_state < HTTP_STATE_WAIT_APP)
1252 if (hc->state != HTTP_CONN_STATE_CLOSED)
1253 clib_warning ("app data req state %u session state %u", hc->http_state,
1255 svm_fifo_dequeue_drop_all (as->tx_fifo);
1259 max_burst_sz = sp->max_burst_size * TRANSPORT_PACER_MIN_MSS;
1260 sp->max_burst_size = max_burst_sz;
1262 http_req_run_state_machine (hc, sp);
1264 if (hc->state == HTTP_CONN_STATE_APP_CLOSED)
1266 if (!svm_fifo_max_dequeue_cons (as->tx_fifo))
1267 http_disconnect_transport (hc);
1270 sent = max_burst_sz - sp->max_burst_size;
1272 return sent > 0 ? clib_max (sent / TRANSPORT_PACER_MIN_MSS, 1) : 0;
1276 http_transport_get_endpoint (u32 hc_index, u32 thread_index,
1277 transport_endpoint_t *tep, u8 is_lcl)
1279 http_conn_t *hc = http_conn_get_w_thread (hc_index, thread_index);
1282 ts = session_get_from_handle (hc->h_tc_session_handle);
1283 session_get_endpoint (ts, tep, is_lcl);
1287 format_http_connection (u8 *s, va_list *args)
1289 http_conn_t *hc = va_arg (*args, http_conn_t *);
1292 ts = session_get_from_handle (hc->h_tc_session_handle);
1293 s = format (s, "[%d:%d][H] app_wrk %u ts %d:%d", hc->c_thread_index,
1294 hc->c_s_index, hc->h_pa_wrk_index, ts->thread_index,
1301 format_http_listener (u8 *s, va_list *args)
1303 http_conn_t *lhc = va_arg (*args, http_conn_t *);
1307 al = app_listener_get_w_handle (lhc->h_tc_session_handle);
1308 lts = app_listener_get_session (al);
1309 s = format (s, "[%d:%d][H] app_wrk %u ts %d:%d", lhc->c_thread_index,
1310 lhc->c_s_index, lhc->h_pa_wrk_index, lts->thread_index,
1311 lts->session_index);
1317 format_http_conn_state (u8 *s, va_list *args)
1319 http_conn_t *hc = va_arg (*args, http_conn_t *);
1323 case HTTP_CONN_STATE_LISTEN:
1324 s = format (s, "LISTEN");
1326 case HTTP_CONN_STATE_CONNECTING:
1327 s = format (s, "CONNECTING");
1329 case HTTP_CONN_STATE_ESTABLISHED:
1330 s = format (s, "ESTABLISHED");
1332 case HTTP_CONN_STATE_TRANSPORT_CLOSED:
1333 s = format (s, "TRANSPORT_CLOSED");
1335 case HTTP_CONN_STATE_APP_CLOSED:
1336 s = format (s, "APP_CLOSED");
1338 case HTTP_CONN_STATE_CLOSED:
1339 s = format (s, "CLOSED");
1347 format_http_transport_connection (u8 *s, va_list *args)
1349 u32 tc_index = va_arg (*args, u32);
1350 u32 thread_index = va_arg (*args, u32);
1351 u32 verbose = va_arg (*args, u32);
1354 hc = http_conn_get_w_thread (tc_index, thread_index);
1356 s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_http_connection, hc);
1360 format (s, "%-" SESSION_CLI_STATE_LEN "U", format_http_conn_state, hc);
1362 s = format (s, "\n");
1369 format_http_transport_listener (u8 *s, va_list *args)
1371 u32 tc_index = va_arg (*args, u32);
1372 u32 __clib_unused thread_index = va_arg (*args, u32);
1373 u32 __clib_unused verbose = va_arg (*args, u32);
1374 http_conn_t *lhc = http_listener_get (tc_index);
1376 s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_http_listener, lhc);
1379 format (s, "%-" SESSION_CLI_STATE_LEN "U", format_http_conn_state, lhc);
1383 static const transport_proto_vft_t http_proto = {
1384 .enable = http_transport_enable,
1385 .connect = http_transport_connect,
1386 .start_listen = http_start_listen,
1387 .stop_listen = http_stop_listen,
1388 .close = http_transport_close,
1389 .custom_tx = http_app_tx_callback,
1390 .get_connection = http_transport_get_connection,
1391 .get_listener = http_transport_get_listener,
1392 .get_transport_endpoint = http_transport_get_endpoint,
1393 .format_connection = format_http_transport_connection,
1394 .format_listener = format_http_transport_listener,
1395 .transport_options = {
1398 .tx_type = TRANSPORT_TX_INTERNAL,
1399 .service_type = TRANSPORT_SERVICE_APP,
1403 static clib_error_t *
1404 http_transport_init (vlib_main_t *vm)
1406 http_main_t *hm = &http_main;
1408 transport_register_protocol (TRANSPORT_PROTO_HTTP, &http_proto,
1409 FIB_PROTOCOL_IP4, ~0);
1410 transport_register_protocol (TRANSPORT_PROTO_HTTP, &http_proto,
1411 FIB_PROTOCOL_IP6, ~0);
1413 /* Default values, configurable via startup conf */
1414 hm->add_seg_size = 256 << 20;
1415 hm->first_seg_size = 32 << 20;
1416 hm->fifo_size = 512 << 10;
1421 VLIB_INIT_FUNCTION (http_transport_init);
1423 static clib_error_t *
1424 http_config_fn (vlib_main_t *vm, unformat_input_t *input)
1426 http_main_t *hm = &http_main;
1429 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1431 if (unformat (input, "first-segment-size %U", unformat_memory_size,
1434 hm->first_seg_size = clib_max (mem_sz, 1 << 20);
1435 if (hm->first_seg_size != mem_sz)
1436 clib_warning ("first seg size too small %u", mem_sz);
1438 else if (unformat (input, "add-segment-size %U", unformat_memory_size,
1441 hm->add_seg_size = clib_max (mem_sz, 1 << 20);
1442 if (hm->add_seg_size != mem_sz)
1443 clib_warning ("add seg size too small %u", mem_sz);
1445 else if (unformat (input, "fifo-size %U", unformat_memory_size, &mem_sz))
1447 hm->fifo_size = clib_clamp (mem_sz, 4 << 10, 2 << 30);
1448 if (hm->fifo_size != mem_sz)
1449 clib_warning ("invalid fifo size %lu", mem_sz);
1452 return clib_error_return (0, "unknown input `%U'",
1453 format_unformat_error, input);
1458 VLIB_CONFIG_FUNCTION (http_config_fn, "http");
1460 VLIB_PLUGIN_REGISTER () = {
1461 .version = VPP_BUILD_VER,
1462 .description = "Hypertext Transfer Protocol (HTTP)",
1463 .default_disabled = 0,
1467 * fd.io coding-style-patch-verification: ON
1470 * eval: (c-set-style "gnu")