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,
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_aligned_safe (wrk->conn_pool, hc, CLIB_CACHE_LINE_BYTES);
63 clib_memset (hc, 0, sizeof (*hc));
64 hc->c_thread_index = thread_index;
65 hc->h_hc_index = hc - wrk->conn_pool;
66 hc->h_pa_session_handle = SESSION_INVALID_HANDLE;
67 hc->h_tc_session_handle = SESSION_INVALID_HANDLE;
68 return hc->h_hc_index;
71 static inline http_conn_t *
72 http_conn_get_w_thread (u32 hc_index, u32 thread_index)
74 http_worker_t *wrk = http_worker_get (thread_index);
75 return pool_elt_at_index (wrk->conn_pool, hc_index);
79 http_conn_free (http_conn_t *hc)
81 http_worker_t *wrk = http_worker_get (hc->c_thread_index);
82 pool_put (wrk->conn_pool, hc);
86 http_listener_alloc (void)
88 http_main_t *hm = &http_main;
91 pool_get_zero (hm->listener_pool, lhc);
92 lhc->c_c_index = lhc - hm->listener_pool;
93 return lhc->c_c_index;
97 http_listener_get (u32 lhc_index)
99 return pool_elt_at_index (http_main.listener_pool, lhc_index);
103 http_listener_free (http_conn_t *lhc)
105 http_main_t *hm = &http_main;
108 memset (lhc, 0xfc, sizeof (*lhc));
109 pool_put (hm->listener_pool, lhc);
113 http_disconnect_transport (http_conn_t *hc)
115 vnet_disconnect_args_t a = {
116 .handle = hc->h_tc_session_handle,
117 .app_index = http_main.app_index,
120 hc->state = HTTP_CONN_STATE_CLOSED;
122 if (vnet_disconnect_session (&a))
123 clib_warning ("disconnect returned");
127 http_conn_timeout_cb (void *hc_handlep)
132 hs_handle = pointer_to_uword (hc_handlep);
133 hc = http_conn_get_w_thread (hs_handle & 0x00FFFFFF, hs_handle >> 24);
135 HTTP_DBG (1, "terminate thread %d index %d hs %llx", hs_handle >> 24,
136 hs_handle & 0x00FFFFFF, hc);
140 hc->timer_handle = ~0;
141 session_transport_closing_notify (&hc->connection);
142 http_disconnect_transport (hc);
146 http_ts_accept_callback (session_t *ts)
148 session_t *ts_listener, *as, *asl;
149 app_worker_t *app_wrk;
150 http_conn_t *lhc, *hc;
151 u32 hc_index, thresh;
154 ts_listener = listen_session_get_from_handle (ts->listener_handle);
155 lhc = http_listener_get (ts_listener->opaque);
157 hc_index = http_conn_alloc_w_thread (ts->thread_index);
158 hc = http_conn_get_w_thread (hc_index, ts->thread_index);
159 clib_memcpy_fast (hc, lhc, sizeof (*lhc));
160 hc->c_thread_index = ts->thread_index;
161 hc->h_hc_index = hc_index;
163 hc->h_tc_session_handle = session_handle (ts);
164 hc->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
166 hc->state = HTTP_CONN_STATE_ESTABLISHED;
167 hc->http_state = HTTP_STATE_WAIT_METHOD;
169 ts->session_state = SESSION_STATE_READY;
170 ts->opaque = hc_index;
173 * Alloc session and initialize
175 as = session_alloc (hc->c_thread_index);
176 hc->c_s_index = as->session_index;
178 as->app_wrk_index = hc->h_pa_wrk_index;
179 as->connection_index = hc->c_c_index;
180 as->session_state = SESSION_STATE_ACCEPTING;
182 asl = listen_session_get_from_handle (lhc->h_pa_session_handle);
183 as->session_type = asl->session_type;
184 as->listener_handle = lhc->h_pa_session_handle;
187 * Init session fifos and notify app
189 if ((rv = app_worker_init_accepted (as)))
191 HTTP_DBG (1, "failed to allocate fifos");
196 hc->h_pa_session_handle = session_handle (as);
197 hc->h_pa_wrk_index = as->app_wrk_index;
198 app_wrk = app_worker_get (as->app_wrk_index);
200 HTTP_DBG (1, "Accepted on listener %u new connection [%u]%x",
201 ts_listener->opaque, vlib_get_thread_index (), hc_index);
203 if ((rv = app_worker_accept_notify (app_wrk, as)))
205 HTTP_DBG (0, "app accept returned");
210 /* Avoid enqueuing small chunks of data on transport tx notifications. If
211 * the fifo is small (under 16K) we set the threshold to it's size, meaning
212 * a notification will be given when the fifo empties.
214 ts = session_get_from_handle (hc->h_tc_session_handle);
215 thresh = clib_min (svm_fifo_size (ts->tx_fifo), HTTP_FIFO_THRESH);
216 svm_fifo_set_deq_thresh (ts->tx_fifo, thresh);
218 http_conn_timer_start (hc);
224 http_ts_connected_callback (u32 http_app_index, u32 ho_hc_index, session_t *ts,
229 http_conn_t *hc, *ho_hc;
230 app_worker_t *app_wrk;
235 clib_warning ("ERROR: %d", err);
239 new_hc_index = http_conn_alloc_w_thread (ts->thread_index);
240 hc = http_conn_get_w_thread (new_hc_index, ts->thread_index);
241 ho_hc = http_conn_get_w_thread (ho_hc_index, 0);
243 ASSERT (ho_hc->state == HTTP_CONN_STATE_CONNECTING);
245 clib_memcpy_fast (hc, ho_hc, sizeof (*hc));
247 hc->c_thread_index = ts->thread_index;
248 hc->h_tc_session_handle = session_handle (ts);
249 hc->c_c_index = new_hc_index;
250 hc->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
251 hc->state = HTTP_CONN_STATE_ESTABLISHED;
252 hc->http_state = HTTP_STATE_WAIT_APP;
254 ts->session_state = SESSION_STATE_READY;
255 ts->opaque = new_hc_index;
257 /* allocate app session and initialize */
259 as = session_alloc (hc->c_thread_index);
260 hc->c_s_index = as->session_index;
261 as->connection_index = hc->c_c_index;
262 as->app_wrk_index = hc->h_pa_wrk_index;
263 as->session_state = SESSION_STATE_READY;
264 as->opaque = hc->h_pa_app_api_ctx;
265 as->session_type = session_type_from_proto_and_ip (
266 TRANSPORT_PROTO_HTTP, session_type_is_ip4 (ts->session_type));
268 app_wrk = app_worker_get (hc->h_pa_wrk_index);
271 clib_warning ("no app worker");
275 if ((rv = app_worker_init_connected (app_wrk, as)))
277 HTTP_DBG (1, "failed to allocate fifos");
281 app_worker_connect_notify (app_wrk, as, err, hc->h_pa_app_api_ctx);
282 hc->h_pa_session_handle = session_handle (as);
283 http_conn_timer_start (hc);
289 http_ts_disconnect_callback (session_t *ts)
293 hc = http_conn_get_w_thread (ts->opaque, ts->thread_index);
295 if (hc->state < HTTP_CONN_STATE_TRANSPORT_CLOSED)
296 hc->state = HTTP_CONN_STATE_TRANSPORT_CLOSED;
298 /* Nothing more to rx, propagate to app */
299 if (!svm_fifo_max_dequeue_cons (ts->rx_fifo))
300 session_transport_closing_notify (&hc->connection);
304 http_ts_reset_callback (session_t *ts)
308 hc = http_conn_get_w_thread (ts->opaque, ts->thread_index);
310 hc->state = HTTP_CONN_STATE_CLOSED;
311 http_buffer_free (&hc->tx_buf);
312 hc->http_state = HTTP_STATE_WAIT_METHOD;
313 session_transport_reset_notify (&hc->connection);
315 http_disconnect_transport (hc);
319 * http error boilerplate
321 static const char *http_error_template = "HTTP/1.1 %s\r\n"
323 "Content-Type: text/html\r\n"
324 "Connection: close\r\n"
325 "Pragma: no-cache\r\n"
326 "Content-Length: 0\r\n\r\n";
328 static const char *http_redirect_template = "HTTP/1.1 %s\r\n";
331 * http response boilerplate
333 static const char *http_response_template = "HTTP/1.1 %s\r\n"
335 "Expires: %U GMT\r\n"
336 "Server: VPP Static\r\n"
337 "Content-Type: %s\r\n"
338 "Content-Length: %lu\r\n\r\n";
340 static const char *http_request_template = "GET %s HTTP/1.1\r\n"
341 "User-Agent: VPP HTTP client\r\n"
345 send_data (http_conn_t *hc, u8 *data, u32 length, u32 offset)
347 const u32 max_burst = 64 << 10;
352 ts = session_get_from_handle (hc->h_tc_session_handle);
354 to_send = clib_min (length - offset, max_burst);
355 sent = svm_fifo_enqueue (ts->tx_fifo, to_send, data + offset);
360 if (svm_fifo_set_event (ts->tx_fifo))
361 session_send_io_evt_to_thread (ts->tx_fifo, SESSION_IO_EVT_TX);
363 return (offset + sent);
367 send_error (http_conn_t *hc, http_status_code_t ec)
369 http_main_t *hm = &http_main;
373 if (ec >= HTTP_N_STATUS)
374 ec = HTTP_STATUS_INTERNAL_ERROR;
376 now = clib_timebase_now (&hm->timebase);
377 data = format (0, http_error_template, http_status_code_str[ec],
378 format_clib_timebase_time, now);
379 send_data (hc, data, vec_len (data), 0);
384 read_http_message (http_conn_t *hc)
386 u32 max_deq, cursize;
390 ts = session_get_from_handle (hc->h_tc_session_handle);
392 cursize = vec_len (hc->rx_buf);
393 max_deq = svm_fifo_max_dequeue (ts->rx_fifo);
394 if (PREDICT_FALSE (max_deq == 0))
397 vec_validate (hc->rx_buf, cursize + max_deq - 1);
398 n_read = svm_fifo_dequeue (ts->rx_fifo, max_deq, hc->rx_buf + cursize);
399 ASSERT (n_read == max_deq);
401 if (svm_fifo_is_empty (ts->rx_fifo))
402 svm_fifo_unset_event (ts->rx_fifo);
404 vec_set_len (hc->rx_buf, cursize + n_read);
409 v_find_index (u8 *vec, u32 offset, char *str)
411 int start_index = offset;
412 u32 slen = (u32) strnlen_s_inline (str, 16);
413 u32 vlen = vec_len (vec);
420 for (; start_index < (vlen - slen); start_index++)
422 if (!memcmp (vec + start_index, str, slen))
430 * waiting for request method from peer - parse request method and data
432 static http_sm_result_t
433 state_srv_wait_method (http_conn_t *hc, transport_send_params_t *sp)
435 http_status_code_t ec;
436 app_worker_t *app_wrk;
443 rv = read_http_message (hc);
445 /* Nothing yet, wait for data or timer expire */
449 if (vec_len (hc->rx_buf) < 8)
451 ec = HTTP_STATUS_BAD_REQUEST;
455 if ((i = v_find_index (hc->rx_buf, 0, "GET ")) >= 0)
457 hc->method = HTTP_REQ_GET;
458 hc->rx_buf_offset = i + 5;
460 i = v_find_index (hc->rx_buf, hc->rx_buf_offset, "HTTP");
463 ec = HTTP_STATUS_BAD_REQUEST;
467 len = i - hc->rx_buf_offset - 1;
469 else if ((i = v_find_index (hc->rx_buf, 0, "POST ")) >= 0)
471 hc->method = HTTP_REQ_POST;
472 hc->rx_buf_offset = i + 6;
473 len = vec_len (hc->rx_buf) - hc->rx_buf_offset - 1;
477 HTTP_DBG (0, "Unknown http method");
478 ec = HTTP_STATUS_METHOD_NOT_ALLOWED;
482 buf = &hc->rx_buf[hc->rx_buf_offset];
484 msg.type = HTTP_MSG_REQUEST;
485 msg.method_type = hc->method;
486 msg.content_type = HTTP_CONTENT_TEXT_HTML;
487 msg.data.type = HTTP_MSG_DATA_INLINE;
490 svm_fifo_seg_t segs[2] = { { (u8 *) &msg, sizeof (msg) }, { buf, len } };
492 as = session_get_from_handle (hc->h_pa_session_handle);
493 rv = svm_fifo_enqueue_segments (as->rx_fifo, segs, 2, 0 /* allow partial */);
494 if (rv < 0 || rv != sizeof (msg) + len)
496 clib_warning ("failed app enqueue");
497 /* This should not happen as we only handle 1 request per session,
498 * and fifo is allocated, but going forward we should consider
500 return HTTP_SM_ERROR;
503 vec_free (hc->rx_buf);
504 hc->http_state = HTTP_STATE_WAIT_APP;
506 app_wrk = app_worker_get_if_valid (as->app_wrk_index);
508 app_worker_rx_notify (app_wrk, as);
515 session_transport_closing_notify (&hc->connection);
516 http_disconnect_transport (hc);
518 return HTTP_SM_ERROR;
522 * waiting for data from app
524 static http_sm_result_t
525 state_srv_wait_app (http_conn_t *hc, transport_send_params_t *sp)
527 http_main_t *hm = &http_main;
528 http_status_code_t ec;
536 as = session_get_from_handle (hc->h_pa_session_handle);
538 rv = svm_fifo_dequeue (as->tx_fifo, sizeof (msg), (u8 *) &msg);
539 ASSERT (rv == sizeof (msg));
541 if (msg.type != HTTP_MSG_REPLY || msg.data.type > HTTP_MSG_DATA_PTR)
543 clib_warning ("unexpected msg type from app %u", msg.type);
544 ec = HTTP_STATUS_INTERNAL_ERROR;
553 case HTTP_STATUS_MOVED:
559 http_buffer_init (&hc->tx_buf, msg_to_buf_type[msg.data.type], as->tx_fifo,
563 * Add headers. For now:
569 now = clib_timebase_now (&hm->timebase);
575 format (0, http_response_template, http_status_code_str[msg.code],
577 format_clib_timebase_time, now,
579 format_clib_timebase_time, now + 600.0,
581 http_content_type_str[msg.content_type],
585 case HTTP_STATUS_MOVED:
587 format (0, http_redirect_template, http_status_code_str[msg.code]);
588 /* Location: http(s)://new-place already queued up as data */
594 offset = send_data (hc, header, vec_len (header), 0);
595 if (offset != vec_len (header))
597 clib_warning ("couldn't send response header!");
598 ec = HTTP_STATUS_INTERNAL_ERROR;
603 /* Start sending the actual data */
604 hc->http_state = HTTP_STATE_IO_MORE_DATA;
606 ASSERT (sp->max_burst_size >= offset);
607 sp->max_burst_size -= offset;
609 return HTTP_SM_CONTINUE;
614 hc->http_state = HTTP_STATE_WAIT_METHOD;
615 session_transport_closing_notify (&hc->connection);
616 http_disconnect_transport (hc);
621 static http_sm_result_t
622 state_srv_send_more_data (http_conn_t *hc, transport_send_params_t *sp)
624 u32 max_send = 64 << 10, n_segs;
625 http_buffer_t *hb = &hc->tx_buf;
630 max_send = clib_min (max_send, sp->max_burst_size);
631 ts = session_get_from_handle (hc->h_tc_session_handle);
632 if ((seg = http_buffer_get_segs (hb, max_send, &n_segs)))
633 sent = svm_fifo_enqueue_segments (ts->tx_fifo, seg, n_segs,
634 1 /* allow partial */);
638 /* Ask scheduler to notify app of deq event if needed */
639 sp->bytes_dequeued += http_buffer_drain (hb, sent);
640 sp->max_burst_size -= sent;
643 /* Not finished sending all data */
644 if (!http_buffer_is_drained (hb))
646 if (sent && svm_fifo_set_event (ts->tx_fifo))
647 session_send_io_evt_to_thread (ts->tx_fifo, SESSION_IO_EVT_TX);
649 if (svm_fifo_max_enqueue (ts->tx_fifo) < HTTP_FIFO_THRESH)
651 /* Deschedule http session and wait for deq notification if
652 * underlying ts tx fifo almost full */
653 svm_fifo_add_want_deq_ntf (ts->tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
654 transport_connection_deschedule (&hc->connection);
655 sp->flags |= TRANSPORT_SND_F_DESCHED;
660 if (sent && svm_fifo_set_event (ts->tx_fifo))
661 session_send_io_evt_to_thread (ts->tx_fifo, SESSION_IO_EVT_TX_FLUSH);
663 /* Finished transaction, back to HTTP_STATE_WAIT_METHOD */
664 hc->http_state = HTTP_STATE_WAIT_METHOD;
665 http_buffer_free (&hc->tx_buf);
672 parse_http_header (http_conn_t *hc, int *content_length)
674 unformat_input_t input;
678 if ((i = v_find_index (hc->rx_buf, hc->rx_buf_offset, "200 OK") < 0))
680 clib_warning ("bad response code");
684 i = v_find_index (hc->rx_buf, hc->rx_buf_offset, CONTENT_LEN_STR);
687 clib_warning ("cannot find '%s' in the header!", CONTENT_LEN_STR);
691 hc->rx_buf_offset = i;
693 i = v_find_index (hc->rx_buf, hc->rx_buf_offset, "\n");
696 clib_warning ("end of line missing; incomplete data");
700 len = i - hc->rx_buf_offset;
701 line = vec_new (u8, len);
702 clib_memcpy (line, hc->rx_buf + hc->rx_buf_offset, len);
704 unformat_init_vector (&input, line);
705 if (!unformat (&input, CONTENT_LEN_STR "%d", content_length))
707 clib_warning ("failed to unformat content length!");
710 unformat_free (&input);
712 /* skip rest of the header */
713 hc->rx_buf_offset += len;
714 i = v_find_index (hc->rx_buf, hc->rx_buf_offset, "<html>");
717 clib_warning ("<html> tag not found");
720 hc->rx_buf_offset = i;
726 state_cln_wait_method (http_conn_t *hc, transport_send_params_t *sp)
730 app_worker_t *app_wrk;
731 int rv, content_length;
733 rv = read_http_message (hc);
737 msg.type = HTTP_MSG_REPLY;
738 msg.content_type = HTTP_CONTENT_TEXT_HTML;
739 msg.code = HTTP_STATUS_OK;
740 msg.data.type = HTTP_MSG_DATA_INLINE;
743 rv = parse_http_header (hc, &content_length);
746 clib_warning ("failed to parse http reply");
747 session_transport_closing_notify (&hc->connection);
748 http_disconnect_transport (hc);
752 msg.data.len = content_length;
753 u32 dlen = vec_len (hc->rx_buf) - hc->rx_buf_offset;
754 as = session_get_from_handle (hc->h_pa_session_handle);
755 svm_fifo_seg_t segs[2] = { { (u8 *) &msg, sizeof (msg) },
756 { &hc->rx_buf[hc->rx_buf_offset], dlen } };
758 rv = svm_fifo_enqueue_segments (as->rx_fifo, segs, 2, 0 /* allow partial */);
761 clib_warning ("error enqueue");
762 return HTTP_SM_ERROR;
764 hc->rx_buf_offset += dlen;
765 hc->http_state = HTTP_STATE_IO_MORE_DATA;
766 hc->to_recv = content_length - dlen;
768 if (hc->rx_buf_offset == vec_len (hc->rx_buf))
770 vec_reset_length (hc->rx_buf);
771 hc->rx_buf_offset = 0;
774 if (hc->to_recv == 0)
776 hc->rx_buf_offset = 0;
777 vec_reset_length (hc->rx_buf);
778 hc->http_state = HTTP_STATE_WAIT_APP;
781 app_wrk = app_worker_get_if_valid (as->app_wrk_index);
782 app_worker_rx_notify (app_wrk, as);
787 cln_drain_rx_buf (http_conn_t *hc, session_t *ts, session_t *as)
789 app_worker_t *app_wrk;
790 u32 max_enq, n_enq, dlen = vec_len (hc->rx_buf) - hc->rx_buf_offset;
793 max_enq = svm_fifo_max_enqueue (as->rx_fifo);
794 n_enq = clib_min (max_enq, dlen);
795 rv = svm_fifo_enqueue (as->rx_fifo, n_enq, &hc->rx_buf[hc->rx_buf_offset]);
798 clib_warning ("enqueue failed");
802 hc->rx_buf_offset += rv;
804 if (hc->rx_buf_offset >= vec_len (hc->rx_buf))
806 vec_reset_length (hc->rx_buf);
807 hc->rx_buf_offset = 0;
810 app_wrk = app_worker_get_if_valid (as->app_wrk_index);
813 app_worker_rx_notify (app_wrk, as);
817 static http_sm_result_t
818 state_cln_recv_more_data (http_conn_t *hc, transport_send_params_t *sp)
825 as = session_get_from_handle (hc->h_pa_session_handle);
826 ts = session_get_from_handle (hc->h_tc_session_handle);
828 u32 dlen = vec_len (hc->rx_buf) - hc->rx_buf_offset;
831 rv = cln_drain_rx_buf (hc, ts, as);
834 clib_warning ("drain rx error!");
835 return HTTP_SM_ERROR;
837 goto maybe_reschedule;
840 if (hc->to_recv == 0)
842 ASSERT (vec_len (hc->rx_buf) == 0);
843 ASSERT (hc->rx_buf_offset == 0);
844 hc->http_state = HTTP_STATE_WAIT_APP;
848 max_deq = svm_fifo_max_dequeue (ts->rx_fifo);
852 ASSERT (vec_len (hc->rx_buf) == 0);
853 ASSERT (hc->rx_buf_offset == 0);
855 vec_validate (hc->rx_buf, max_deq - 1);
856 n_read = svm_fifo_dequeue (ts->rx_fifo, max_deq, hc->rx_buf);
857 ASSERT (n_read == max_deq);
859 if (svm_fifo_is_empty (ts->rx_fifo))
860 svm_fifo_unset_event (ts->rx_fifo);
862 hc->to_recv -= n_read;
863 vec_set_len (hc->rx_buf, max_deq);
866 if (hc->rx_buf_offset < vec_len (hc->rx_buf) ||
867 svm_fifo_max_dequeue_cons (ts->rx_fifo))
869 /* TODO is the flag really needed? */
870 if (svm_fifo_set_event (ts->rx_fifo))
871 session_enqueue_notify (ts);
873 return HTTP_SM_CONTINUE;
876 static http_sm_result_t
877 state_cln_wait_app (http_conn_t *hc, transport_send_params_t *sp)
881 http_status_code_t ec;
882 u8 *buf = 0, *request;
886 as = session_get_from_handle (hc->h_pa_session_handle);
887 rv = svm_fifo_dequeue (as->tx_fifo, sizeof (msg), (u8 *) &msg);
888 ASSERT (rv == sizeof (msg));
889 if (msg.type != HTTP_MSG_REQUEST || msg.data.type > HTTP_MSG_DATA_PTR)
891 clib_warning ("unexpected msg type from app %u", msg.type);
892 ec = HTTP_STATUS_INTERNAL_ERROR;
896 vec_validate (buf, msg.data.len - 1);
897 rv = svm_fifo_dequeue (as->tx_fifo, msg.data.len, buf);
898 ASSERT (rv == msg.data.len);
900 request = format (0, http_request_template, buf);
901 offset = send_data (hc, request, vec_len (request), 0);
902 if (offset != vec_len (request))
904 clib_warning ("sending request failed!");
905 ec = HTTP_STATUS_INTERNAL_ERROR;
909 hc->http_state = HTTP_STATE_WAIT_METHOD;
914 return HTTP_SM_CONTINUE;
918 session_transport_closing_notify (&hc->connection);
919 http_disconnect_transport (hc);
923 typedef http_sm_result_t (*http_sm_handler) (http_conn_t *,
924 transport_send_params_t *sp);
926 static http_sm_handler srv_state_funcs[HTTP_N_STATES] = {
927 /* Waiting for GET, POST, etc. */
928 state_srv_wait_method,
929 /* Wait for data from app */
932 state_srv_send_more_data,
935 static http_sm_handler cln_state_funcs[HTTP_N_STATES] = {
937 state_cln_wait_method,
938 /* wait for data from app */
940 /* receive more data */
941 state_cln_recv_more_data,
945 http_req_run_state_machine (http_conn_t *hc, transport_send_params_t *sp)
947 http_sm_result_t res;
948 http_sm_handler *state_fn =
949 hc->is_client ? cln_state_funcs : srv_state_funcs;
952 res = state_fn[hc->http_state](hc, sp);
953 if (res == HTTP_SM_ERROR)
956 while (res == HTTP_SM_CONTINUE);
958 /* Reset the session expiration timer */
959 http_conn_timer_update (hc);
963 http_ts_server_rx_callback (session_t *ts, http_conn_t *hc)
965 if (hc->http_state != HTTP_STATE_WAIT_METHOD)
967 clib_warning ("tcp data in req state %u", hc->http_state);
971 http_req_run_state_machine (hc, 0);
973 if (hc->state == HTTP_CONN_STATE_TRANSPORT_CLOSED)
975 if (!svm_fifo_max_dequeue_cons (ts->rx_fifo))
976 session_transport_closing_notify (&hc->connection);
982 http_ts_client_rx_callback (session_t *ts, http_conn_t *hc)
984 if (hc->http_state != HTTP_STATE_WAIT_METHOD &&
985 hc->http_state != HTTP_STATE_IO_MORE_DATA)
987 clib_warning ("http in unexpected state %d (ts %d)", hc->http_state,
992 http_req_run_state_machine (hc, 0);
994 if (hc->state == HTTP_CONN_STATE_TRANSPORT_CLOSED)
996 if (!svm_fifo_max_dequeue_cons (ts->rx_fifo))
997 session_transport_closing_notify (&hc->connection);
1003 http_ts_rx_callback (session_t *ts)
1007 hc = http_conn_get_w_thread (ts->opaque, ts->thread_index);
1009 return http_ts_client_rx_callback (ts, hc);
1010 return http_ts_server_rx_callback (ts, hc);
1014 http_ts_builtin_tx_callback (session_t *ts)
1018 hc = http_conn_get_w_thread (ts->opaque, ts->thread_index);
1019 transport_connection_reschedule (&hc->connection);
1025 http_ts_cleanup_callback (session_t *ts, session_cleanup_ntf_t ntf)
1029 if (ntf == SESSION_CLEANUP_TRANSPORT)
1032 hc = http_conn_get_w_thread (ts->opaque, ts->thread_index);
1035 clib_warning ("no http connection for %u", ts->session_index);
1039 vec_free (hc->rx_buf);
1041 http_buffer_free (&hc->tx_buf);
1042 http_conn_timer_stop (hc);
1044 session_transport_delete_notify (&hc->connection);
1045 http_conn_free (hc);
1049 http_add_segment_callback (u32 client_index, u64 segment_handle)
1051 /* No-op for builtin */
1056 http_del_segment_callback (u32 client_index, u64 segment_handle)
1061 static session_cb_vft_t http_app_cb_vft = {
1062 .session_accept_callback = http_ts_accept_callback,
1063 .session_disconnect_callback = http_ts_disconnect_callback,
1064 .session_connected_callback = http_ts_connected_callback,
1065 .session_reset_callback = http_ts_reset_callback,
1066 .session_cleanup_callback = http_ts_cleanup_callback,
1067 .add_segment_callback = http_add_segment_callback,
1068 .del_segment_callback = http_del_segment_callback,
1069 .builtin_app_rx_callback = http_ts_rx_callback,
1070 .builtin_app_tx_callback = http_ts_builtin_tx_callback,
1073 static clib_error_t *
1074 http_transport_enable (vlib_main_t *vm, u8 is_en)
1076 vnet_app_detach_args_t _da, *da = &_da;
1077 vnet_app_attach_args_t _a, *a = &_a;
1078 u64 options[APP_OPTIONS_N_OPTIONS];
1079 http_main_t *hm = &http_main;
1083 da->app_index = hm->app_index;
1084 da->api_client_index = APP_INVALID_INDEX;
1085 vnet_application_detach (da);
1089 vec_validate (hm->wrk, vlib_num_workers ());
1091 clib_memset (a, 0, sizeof (*a));
1092 clib_memset (options, 0, sizeof (options));
1094 a->session_cb_vft = &http_app_cb_vft;
1095 a->api_client_index = APP_INVALID_INDEX;
1096 a->options = options;
1097 a->name = format (0, "http");
1098 a->options[APP_OPTIONS_SEGMENT_SIZE] = hm->first_seg_size;
1099 a->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = hm->add_seg_size;
1100 a->options[APP_OPTIONS_RX_FIFO_SIZE] = hm->fifo_size;
1101 a->options[APP_OPTIONS_TX_FIFO_SIZE] = hm->fifo_size;
1102 a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
1103 a->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
1104 a->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_IS_TRANSPORT_APP;
1106 if (vnet_application_attach (a))
1107 return clib_error_return (0, "failed to attach http app");
1109 hm->app_index = a->app_index;
1112 clib_timebase_init (&hm->timebase, 0 /* GMT */, CLIB_TIMEBASE_DAYLIGHT_NONE,
1113 &vm->clib_time /* share the system clock */);
1115 http_timers_init (vm, http_conn_timeout_cb);
1121 http_transport_connect (transport_endpoint_cfg_t *tep)
1123 vnet_connect_args_t _cargs, *cargs = &_cargs;
1124 http_main_t *hm = &http_main;
1125 session_endpoint_cfg_t *sep = (session_endpoint_cfg_t *) tep;
1130 app_worker_t *app_wrk = app_worker_get (sep->app_wrk_index);
1132 clib_memset (cargs, 0, sizeof (*cargs));
1133 clib_memcpy (&cargs->sep_ext, sep, sizeof (session_endpoint_cfg_t));
1134 cargs->sep.transport_proto = TRANSPORT_PROTO_TCP;
1135 cargs->app_index = hm->app_index;
1136 app = application_get (app_wrk->app_index);
1137 cargs->sep_ext.ns_index = app->ns_index;
1139 hc_index = http_conn_alloc_w_thread (0 /* ts->thread_index */);
1140 hc = http_conn_get_w_thread (hc_index, 0);
1141 hc->h_pa_wrk_index = sep->app_wrk_index;
1142 hc->h_pa_app_api_ctx = sep->opaque;
1144 hc->state = HTTP_CONN_STATE_CONNECTING;
1145 cargs->api_context = hc_index;
1147 if ((error = vnet_connect (cargs)))
1154 http_start_listen (u32 app_listener_index, transport_endpoint_cfg_t *tep)
1156 vnet_listen_args_t _args = {}, *args = &_args;
1157 session_t *ts_listener, *app_listener;
1158 http_main_t *hm = &http_main;
1159 session_endpoint_cfg_t *sep;
1160 app_worker_t *app_wrk;
1161 transport_proto_t tp;
1167 sep = (session_endpoint_cfg_t *) tep;
1169 app_wrk = app_worker_get (sep->app_wrk_index);
1170 app = application_get (app_wrk->app_index);
1172 args->app_index = hm->app_index;
1173 args->sep_ext = *sep;
1174 args->sep_ext.ns_index = app->ns_index;
1175 tp = sep->ext_cfg ? TRANSPORT_PROTO_TLS : TRANSPORT_PROTO_TCP;
1176 args->sep_ext.transport_proto = tp;
1178 if (vnet_listen (args))
1179 return SESSION_INVALID_INDEX;
1181 lhc_index = http_listener_alloc ();
1182 lhc = http_listener_get (lhc_index);
1184 /* Grab transport connection listener and link to http listener */
1185 lhc->h_tc_session_handle = args->handle;
1186 al = app_listener_get_w_handle (lhc->h_tc_session_handle);
1187 ts_listener = app_listener_get_session (al);
1188 ts_listener->opaque = lhc_index;
1190 /* Grab application listener and link to http listener */
1191 app_listener = listen_session_get (app_listener_index);
1192 lhc->h_pa_wrk_index = sep->app_wrk_index;
1193 lhc->h_pa_session_handle = listen_session_get_handle (app_listener);
1194 lhc->c_s_index = app_listener_index;
1195 lhc->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
1201 http_stop_listen (u32 listener_index)
1206 lhc = http_listener_get (listener_index);
1208 vnet_unlisten_args_t a = {
1209 .handle = lhc->h_tc_session_handle,
1210 .app_index = http_main.app_index,
1211 .wrk_map_index = 0 /* default wrk */
1214 if ((rv = vnet_unlisten (&a)))
1215 clib_warning ("unlisten returned %d", rv);
1217 http_listener_free (lhc);
1223 http_transport_close (u32 hc_index, u32 thread_index)
1228 HTTP_DBG (1, "App disconnecting %x", hc_index);
1230 hc = http_conn_get_w_thread (hc_index, thread_index);
1231 if (hc->state == HTTP_CONN_STATE_CONNECTING)
1233 hc->state = HTTP_CONN_STATE_APP_CLOSED;
1234 http_disconnect_transport (hc);
1238 as = session_get_from_handle (hc->h_pa_session_handle);
1240 /* Nothing more to send, confirm close */
1241 if (!svm_fifo_max_dequeue_cons (as->tx_fifo))
1243 session_transport_closed_notify (&hc->connection);
1244 http_disconnect_transport (hc);
1248 /* Wait for all data to be written to ts */
1249 hc->state = HTTP_CONN_STATE_APP_CLOSED;
1253 static transport_connection_t *
1254 http_transport_get_connection (u32 hc_index, u32 thread_index)
1256 http_conn_t *hc = http_conn_get_w_thread (hc_index, thread_index);
1257 return &hc->connection;
1260 static transport_connection_t *
1261 http_transport_get_listener (u32 listener_index)
1263 http_conn_t *lhc = http_listener_get (listener_index);
1264 return &lhc->connection;
1268 http_app_tx_callback (void *session, transport_send_params_t *sp)
1270 session_t *as = (session_t *) session;
1271 u32 max_burst_sz, sent;
1274 hc = http_conn_get_w_thread (as->connection_index, as->thread_index);
1275 if (hc->http_state < HTTP_STATE_WAIT_APP)
1277 if (hc->state != HTTP_CONN_STATE_CLOSED)
1278 clib_warning ("app data req state %u session state %u", hc->http_state,
1280 svm_fifo_dequeue_drop_all (as->tx_fifo);
1284 max_burst_sz = sp->max_burst_size * TRANSPORT_PACER_MIN_MSS;
1285 sp->max_burst_size = max_burst_sz;
1287 http_req_run_state_machine (hc, sp);
1289 if (hc->state == HTTP_CONN_STATE_APP_CLOSED)
1291 if (!svm_fifo_max_dequeue_cons (as->tx_fifo))
1292 http_disconnect_transport (hc);
1295 sent = max_burst_sz - sp->max_burst_size;
1297 return sent > 0 ? clib_max (sent / TRANSPORT_PACER_MIN_MSS, 1) : 0;
1301 http_transport_get_endpoint (u32 hc_index, u32 thread_index,
1302 transport_endpoint_t *tep, u8 is_lcl)
1304 http_conn_t *hc = http_conn_get_w_thread (hc_index, thread_index);
1307 ts = session_get_from_handle (hc->h_tc_session_handle);
1308 session_get_endpoint (ts, tep, is_lcl);
1312 format_http_connection (u8 *s, va_list *args)
1314 http_conn_t *hc = va_arg (*args, http_conn_t *);
1317 ts = session_get_from_handle (hc->h_tc_session_handle);
1318 s = format (s, "[%d:%d][H] app_wrk %u ts %d:%d", hc->c_thread_index,
1319 hc->c_s_index, hc->h_pa_wrk_index, ts->thread_index,
1326 format_http_listener (u8 *s, va_list *args)
1328 http_conn_t *lhc = va_arg (*args, http_conn_t *);
1332 al = app_listener_get_w_handle (lhc->h_tc_session_handle);
1333 lts = app_listener_get_session (al);
1334 s = format (s, "[%d:%d][H] app_wrk %u ts %d:%d", lhc->c_thread_index,
1335 lhc->c_s_index, lhc->h_pa_wrk_index, lts->thread_index,
1336 lts->session_index);
1342 format_http_conn_state (u8 *s, va_list *args)
1344 http_conn_t *hc = va_arg (*args, http_conn_t *);
1348 case HTTP_CONN_STATE_LISTEN:
1349 s = format (s, "LISTEN");
1351 case HTTP_CONN_STATE_CONNECTING:
1352 s = format (s, "CONNECTING");
1354 case HTTP_CONN_STATE_ESTABLISHED:
1355 s = format (s, "ESTABLISHED");
1357 case HTTP_CONN_STATE_TRANSPORT_CLOSED:
1358 s = format (s, "TRANSPORT_CLOSED");
1360 case HTTP_CONN_STATE_APP_CLOSED:
1361 s = format (s, "APP_CLOSED");
1363 case HTTP_CONN_STATE_CLOSED:
1364 s = format (s, "CLOSED");
1372 format_http_transport_connection (u8 *s, va_list *args)
1374 u32 tc_index = va_arg (*args, u32);
1375 u32 thread_index = va_arg (*args, u32);
1376 u32 verbose = va_arg (*args, u32);
1379 hc = http_conn_get_w_thread (tc_index, thread_index);
1381 s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_http_connection, hc);
1385 format (s, "%-" SESSION_CLI_STATE_LEN "U", format_http_conn_state, hc);
1387 s = format (s, "\n");
1394 format_http_transport_listener (u8 *s, va_list *args)
1396 u32 tc_index = va_arg (*args, u32);
1397 u32 __clib_unused thread_index = va_arg (*args, u32);
1398 u32 __clib_unused verbose = va_arg (*args, u32);
1399 http_conn_t *lhc = http_listener_get (tc_index);
1401 s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_http_listener, lhc);
1404 format (s, "%-" SESSION_CLI_STATE_LEN "U", format_http_conn_state, lhc);
1408 static const transport_proto_vft_t http_proto = {
1409 .enable = http_transport_enable,
1410 .connect = http_transport_connect,
1411 .start_listen = http_start_listen,
1412 .stop_listen = http_stop_listen,
1413 .close = http_transport_close,
1414 .custom_tx = http_app_tx_callback,
1415 .get_connection = http_transport_get_connection,
1416 .get_listener = http_transport_get_listener,
1417 .get_transport_endpoint = http_transport_get_endpoint,
1418 .format_connection = format_http_transport_connection,
1419 .format_listener = format_http_transport_listener,
1420 .transport_options = {
1423 .tx_type = TRANSPORT_TX_INTERNAL,
1424 .service_type = TRANSPORT_SERVICE_APP,
1428 static clib_error_t *
1429 http_transport_init (vlib_main_t *vm)
1431 http_main_t *hm = &http_main;
1433 transport_register_protocol (TRANSPORT_PROTO_HTTP, &http_proto,
1434 FIB_PROTOCOL_IP4, ~0);
1435 transport_register_protocol (TRANSPORT_PROTO_HTTP, &http_proto,
1436 FIB_PROTOCOL_IP6, ~0);
1438 /* Default values, configurable via startup conf */
1439 hm->add_seg_size = 256 << 20;
1440 hm->first_seg_size = 32 << 20;
1441 hm->fifo_size = 512 << 10;
1446 VLIB_INIT_FUNCTION (http_transport_init);
1448 static clib_error_t *
1449 http_config_fn (vlib_main_t *vm, unformat_input_t *input)
1451 http_main_t *hm = &http_main;
1454 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1456 if (unformat (input, "first-segment-size %U", unformat_memory_size,
1459 hm->first_seg_size = clib_max (mem_sz, 1 << 20);
1460 if (hm->first_seg_size != mem_sz)
1461 clib_warning ("first seg size too small %u", mem_sz);
1463 else if (unformat (input, "add-segment-size %U", unformat_memory_size,
1466 hm->add_seg_size = clib_max (mem_sz, 1 << 20);
1467 if (hm->add_seg_size != mem_sz)
1468 clib_warning ("add seg size too small %u", mem_sz);
1470 else if (unformat (input, "fifo-size %U", unformat_memory_size, &mem_sz))
1472 hm->fifo_size = clib_clamp (mem_sz, 4 << 10, 2 << 30);
1473 if (hm->fifo_size != mem_sz)
1474 clib_warning ("invalid fifo size %lu", mem_sz);
1477 return clib_error_return (0, "unknown input `%U'",
1478 format_unformat_error, input);
1483 VLIB_CONFIG_FUNCTION (http_config_fn, "http");
1485 VLIB_PLUGIN_REGISTER () = {
1486 .version = VPP_BUILD_VER,
1487 .description = "Hypertext Transfer Protocol (HTTP)",
1488 .default_disabled = 0,
1492 * fd.io coding-style-patch-verification: ON
1495 * eval: (c-set-style "gnu")