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);
783 app_worker_rx_notify (app_wrk, as);
789 cln_drain_rx_buf (http_conn_t *hc, session_t *ts, session_t *as)
791 app_worker_t *app_wrk;
792 u32 max_enq, n_enq, dlen = vec_len (hc->rx_buf) - hc->rx_buf_offset;
795 max_enq = svm_fifo_max_enqueue (as->rx_fifo);
796 n_enq = clib_min (max_enq, dlen);
797 rv = svm_fifo_enqueue (as->rx_fifo, n_enq, &hc->rx_buf[hc->rx_buf_offset]);
800 clib_warning ("enqueue failed");
804 hc->rx_buf_offset += rv;
806 if (hc->rx_buf_offset >= vec_len (hc->rx_buf))
808 vec_reset_length (hc->rx_buf);
809 hc->rx_buf_offset = 0;
812 app_wrk = app_worker_get_if_valid (as->app_wrk_index);
815 app_worker_rx_notify (app_wrk, as);
819 static http_sm_result_t
820 state_cln_recv_more_data (http_conn_t *hc, transport_send_params_t *sp)
827 as = session_get_from_handle (hc->h_pa_session_handle);
828 ts = session_get_from_handle (hc->h_tc_session_handle);
830 u32 dlen = vec_len (hc->rx_buf) - hc->rx_buf_offset;
833 rv = cln_drain_rx_buf (hc, ts, as);
836 clib_warning ("drain rx error!");
837 return HTTP_SM_ERROR;
839 goto maybe_reschedule;
842 if (hc->to_recv == 0)
844 ASSERT (vec_len (hc->rx_buf) == 0);
845 ASSERT (hc->rx_buf_offset == 0);
846 hc->http_state = HTTP_STATE_WAIT_APP;
850 max_deq = svm_fifo_max_dequeue (ts->rx_fifo);
854 ASSERT (vec_len (hc->rx_buf) == 0);
855 ASSERT (hc->rx_buf_offset == 0);
857 vec_validate (hc->rx_buf, max_deq - 1);
858 n_read = svm_fifo_dequeue (ts->rx_fifo, max_deq, hc->rx_buf);
859 ASSERT (n_read == max_deq);
861 if (svm_fifo_is_empty (ts->rx_fifo))
862 svm_fifo_unset_event (ts->rx_fifo);
864 hc->to_recv -= n_read;
865 vec_set_len (hc->rx_buf, max_deq);
868 if (hc->rx_buf_offset < vec_len (hc->rx_buf) ||
869 svm_fifo_max_dequeue_cons (ts->rx_fifo))
871 /* TODO is the flag really needed? */
872 if (svm_fifo_set_event (ts->rx_fifo))
873 session_enqueue_notify (ts);
875 return HTTP_SM_CONTINUE;
878 static http_sm_result_t
879 state_cln_wait_app (http_conn_t *hc, transport_send_params_t *sp)
883 http_status_code_t ec;
884 u8 *buf = 0, *request;
888 as = session_get_from_handle (hc->h_pa_session_handle);
889 rv = svm_fifo_dequeue (as->tx_fifo, sizeof (msg), (u8 *) &msg);
890 ASSERT (rv == sizeof (msg));
891 if (msg.type != HTTP_MSG_REQUEST || msg.data.type > HTTP_MSG_DATA_PTR)
893 clib_warning ("unexpected msg type from app %u", msg.type);
894 ec = HTTP_STATUS_INTERNAL_ERROR;
898 vec_validate (buf, msg.data.len - 1);
899 rv = svm_fifo_dequeue (as->tx_fifo, msg.data.len, buf);
900 ASSERT (rv == msg.data.len);
902 request = format (0, http_request_template, buf);
903 offset = send_data (hc, request, vec_len (request), 0);
904 if (offset != vec_len (request))
906 clib_warning ("sending request failed!");
907 ec = HTTP_STATUS_INTERNAL_ERROR;
911 hc->http_state = HTTP_STATE_WAIT_METHOD;
916 return HTTP_SM_CONTINUE;
920 session_transport_closing_notify (&hc->connection);
921 http_disconnect_transport (hc);
925 typedef http_sm_result_t (*http_sm_handler) (http_conn_t *,
926 transport_send_params_t *sp);
928 static http_sm_handler srv_state_funcs[HTTP_N_STATES] = {
929 /* Waiting for GET, POST, etc. */
930 state_srv_wait_method,
931 /* Wait for data from app */
934 state_srv_send_more_data,
937 static http_sm_handler cln_state_funcs[HTTP_N_STATES] = {
939 state_cln_wait_method,
940 /* wait for data from app */
942 /* receive more data */
943 state_cln_recv_more_data,
947 http_req_run_state_machine (http_conn_t *hc, transport_send_params_t *sp)
949 http_sm_result_t res;
950 http_sm_handler *state_fn =
951 hc->is_client ? cln_state_funcs : srv_state_funcs;
954 res = state_fn[hc->http_state](hc, sp);
955 if (res == HTTP_SM_ERROR)
958 while (res == HTTP_SM_CONTINUE);
960 /* Reset the session expiration timer */
961 http_conn_timer_update (hc);
965 http_ts_server_rx_callback (session_t *ts, http_conn_t *hc)
967 if (hc->http_state != HTTP_STATE_WAIT_METHOD)
969 clib_warning ("tcp data in req state %u", hc->http_state);
973 http_req_run_state_machine (hc, 0);
975 if (hc->state == HTTP_CONN_STATE_TRANSPORT_CLOSED)
977 if (!svm_fifo_max_dequeue_cons (ts->rx_fifo))
978 session_transport_closing_notify (&hc->connection);
984 http_ts_client_rx_callback (session_t *ts, http_conn_t *hc)
986 if (hc->http_state != HTTP_STATE_WAIT_METHOD &&
987 hc->http_state != HTTP_STATE_IO_MORE_DATA)
989 clib_warning ("http in unexpected state %d (ts %d)", hc->http_state,
994 http_req_run_state_machine (hc, 0);
996 if (hc->state == HTTP_CONN_STATE_TRANSPORT_CLOSED)
998 if (!svm_fifo_max_dequeue_cons (ts->rx_fifo))
999 session_transport_closing_notify (&hc->connection);
1005 http_ts_rx_callback (session_t *ts)
1009 hc = http_conn_get_w_thread (ts->opaque, ts->thread_index);
1011 return http_ts_client_rx_callback (ts, hc);
1012 return http_ts_server_rx_callback (ts, hc);
1016 http_ts_builtin_tx_callback (session_t *ts)
1020 hc = http_conn_get_w_thread (ts->opaque, ts->thread_index);
1021 transport_connection_reschedule (&hc->connection);
1027 http_ts_cleanup_callback (session_t *ts, session_cleanup_ntf_t ntf)
1031 if (ntf == SESSION_CLEANUP_TRANSPORT)
1034 hc = http_conn_get_w_thread (ts->opaque, ts->thread_index);
1037 clib_warning ("no http connection for %u", ts->session_index);
1041 vec_free (hc->rx_buf);
1043 http_buffer_free (&hc->tx_buf);
1044 http_conn_timer_stop (hc);
1046 session_transport_delete_notify (&hc->connection);
1047 http_conn_free (hc);
1051 http_add_segment_callback (u32 client_index, u64 segment_handle)
1053 /* No-op for builtin */
1058 http_del_segment_callback (u32 client_index, u64 segment_handle)
1063 static session_cb_vft_t http_app_cb_vft = {
1064 .session_accept_callback = http_ts_accept_callback,
1065 .session_disconnect_callback = http_ts_disconnect_callback,
1066 .session_connected_callback = http_ts_connected_callback,
1067 .session_reset_callback = http_ts_reset_callback,
1068 .session_cleanup_callback = http_ts_cleanup_callback,
1069 .add_segment_callback = http_add_segment_callback,
1070 .del_segment_callback = http_del_segment_callback,
1071 .builtin_app_rx_callback = http_ts_rx_callback,
1072 .builtin_app_tx_callback = http_ts_builtin_tx_callback,
1075 static clib_error_t *
1076 http_transport_enable (vlib_main_t *vm, u8 is_en)
1078 vnet_app_detach_args_t _da, *da = &_da;
1079 vnet_app_attach_args_t _a, *a = &_a;
1080 u64 options[APP_OPTIONS_N_OPTIONS];
1081 http_main_t *hm = &http_main;
1085 da->app_index = hm->app_index;
1086 da->api_client_index = APP_INVALID_INDEX;
1087 vnet_application_detach (da);
1091 vec_validate (hm->wrk, vlib_num_workers ());
1093 clib_memset (a, 0, sizeof (*a));
1094 clib_memset (options, 0, sizeof (options));
1096 a->session_cb_vft = &http_app_cb_vft;
1097 a->api_client_index = APP_INVALID_INDEX;
1098 a->options = options;
1099 a->name = format (0, "http");
1100 a->options[APP_OPTIONS_SEGMENT_SIZE] = hm->first_seg_size;
1101 a->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = hm->add_seg_size;
1102 a->options[APP_OPTIONS_RX_FIFO_SIZE] = hm->fifo_size;
1103 a->options[APP_OPTIONS_TX_FIFO_SIZE] = hm->fifo_size;
1104 a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
1105 a->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
1106 a->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_IS_TRANSPORT_APP;
1108 if (vnet_application_attach (a))
1109 return clib_error_return (0, "failed to attach http app");
1111 hm->app_index = a->app_index;
1114 clib_timebase_init (&hm->timebase, 0 /* GMT */, CLIB_TIMEBASE_DAYLIGHT_NONE,
1115 &vm->clib_time /* share the system clock */);
1117 http_timers_init (vm, http_conn_timeout_cb);
1123 http_transport_connect (transport_endpoint_cfg_t *tep)
1125 vnet_connect_args_t _cargs, *cargs = &_cargs;
1126 http_main_t *hm = &http_main;
1127 session_endpoint_cfg_t *sep = (session_endpoint_cfg_t *) tep;
1132 app_worker_t *app_wrk = app_worker_get (sep->app_wrk_index);
1134 clib_memset (cargs, 0, sizeof (*cargs));
1135 clib_memcpy (&cargs->sep_ext, sep, sizeof (session_endpoint_cfg_t));
1136 cargs->sep.transport_proto = TRANSPORT_PROTO_TCP;
1137 cargs->app_index = hm->app_index;
1138 app = application_get (app_wrk->app_index);
1139 cargs->sep_ext.ns_index = app->ns_index;
1141 hc_index = http_conn_alloc_w_thread (0 /* ts->thread_index */);
1142 hc = http_conn_get_w_thread (hc_index, 0);
1143 hc->h_pa_wrk_index = sep->app_wrk_index;
1144 hc->h_pa_app_api_ctx = sep->opaque;
1146 hc->state = HTTP_CONN_STATE_CONNECTING;
1147 cargs->api_context = hc_index;
1149 if ((error = vnet_connect (cargs)))
1156 http_start_listen (u32 app_listener_index, transport_endpoint_cfg_t *tep)
1158 vnet_listen_args_t _args = {}, *args = &_args;
1159 session_t *ts_listener, *app_listener;
1160 http_main_t *hm = &http_main;
1161 session_endpoint_cfg_t *sep;
1162 app_worker_t *app_wrk;
1163 transport_proto_t tp;
1169 sep = (session_endpoint_cfg_t *) tep;
1171 app_wrk = app_worker_get (sep->app_wrk_index);
1172 app = application_get (app_wrk->app_index);
1174 args->app_index = hm->app_index;
1175 args->sep_ext = *sep;
1176 args->sep_ext.ns_index = app->ns_index;
1177 tp = sep->ext_cfg ? TRANSPORT_PROTO_TLS : TRANSPORT_PROTO_TCP;
1178 args->sep_ext.transport_proto = tp;
1180 if (vnet_listen (args))
1181 return SESSION_INVALID_INDEX;
1183 lhc_index = http_listener_alloc ();
1184 lhc = http_listener_get (lhc_index);
1186 /* Grab transport connection listener and link to http listener */
1187 lhc->h_tc_session_handle = args->handle;
1188 al = app_listener_get_w_handle (lhc->h_tc_session_handle);
1189 ts_listener = app_listener_get_session (al);
1190 ts_listener->opaque = lhc_index;
1192 /* Grab application listener and link to http listener */
1193 app_listener = listen_session_get (app_listener_index);
1194 lhc->h_pa_wrk_index = sep->app_wrk_index;
1195 lhc->h_pa_session_handle = listen_session_get_handle (app_listener);
1196 lhc->c_s_index = app_listener_index;
1197 lhc->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
1203 http_stop_listen (u32 listener_index)
1208 lhc = http_listener_get (listener_index);
1210 vnet_unlisten_args_t a = {
1211 .handle = lhc->h_tc_session_handle,
1212 .app_index = http_main.app_index,
1213 .wrk_map_index = 0 /* default wrk */
1216 if ((rv = vnet_unlisten (&a)))
1217 clib_warning ("unlisten returned %d", rv);
1219 http_listener_free (lhc);
1225 http_transport_close (u32 hc_index, u32 thread_index)
1230 HTTP_DBG (1, "App disconnecting %x", hc_index);
1232 hc = http_conn_get_w_thread (hc_index, thread_index);
1233 if (hc->state == HTTP_CONN_STATE_CONNECTING)
1235 hc->state = HTTP_CONN_STATE_APP_CLOSED;
1236 http_disconnect_transport (hc);
1240 as = session_get_from_handle (hc->h_pa_session_handle);
1242 /* Nothing more to send, confirm close */
1243 if (!svm_fifo_max_dequeue_cons (as->tx_fifo))
1245 session_transport_closed_notify (&hc->connection);
1246 http_disconnect_transport (hc);
1250 /* Wait for all data to be written to ts */
1251 hc->state = HTTP_CONN_STATE_APP_CLOSED;
1255 static transport_connection_t *
1256 http_transport_get_connection (u32 hc_index, u32 thread_index)
1258 http_conn_t *hc = http_conn_get_w_thread (hc_index, thread_index);
1259 return &hc->connection;
1262 static transport_connection_t *
1263 http_transport_get_listener (u32 listener_index)
1265 http_conn_t *lhc = http_listener_get (listener_index);
1266 return &lhc->connection;
1270 http_app_tx_callback (void *session, transport_send_params_t *sp)
1272 session_t *as = (session_t *) session;
1273 u32 max_burst_sz, sent;
1276 hc = http_conn_get_w_thread (as->connection_index, as->thread_index);
1277 if (hc->http_state < HTTP_STATE_WAIT_APP)
1279 if (hc->state != HTTP_CONN_STATE_CLOSED)
1280 clib_warning ("app data req state %u session state %u", hc->http_state,
1282 svm_fifo_dequeue_drop_all (as->tx_fifo);
1286 max_burst_sz = sp->max_burst_size * TRANSPORT_PACER_MIN_MSS;
1287 sp->max_burst_size = max_burst_sz;
1289 http_req_run_state_machine (hc, sp);
1291 if (hc->state == HTTP_CONN_STATE_APP_CLOSED)
1293 if (!svm_fifo_max_dequeue_cons (as->tx_fifo))
1294 http_disconnect_transport (hc);
1297 sent = max_burst_sz - sp->max_burst_size;
1299 return sent > 0 ? clib_max (sent / TRANSPORT_PACER_MIN_MSS, 1) : 0;
1303 http_transport_get_endpoint (u32 hc_index, u32 thread_index,
1304 transport_endpoint_t *tep, u8 is_lcl)
1306 http_conn_t *hc = http_conn_get_w_thread (hc_index, thread_index);
1309 ts = session_get_from_handle (hc->h_tc_session_handle);
1310 session_get_endpoint (ts, tep, is_lcl);
1314 format_http_connection (u8 *s, va_list *args)
1316 http_conn_t *hc = va_arg (*args, http_conn_t *);
1319 ts = session_get_from_handle (hc->h_tc_session_handle);
1320 s = format (s, "[%d:%d][H] app_wrk %u ts %d:%d", hc->c_thread_index,
1321 hc->c_s_index, hc->h_pa_wrk_index, ts->thread_index,
1328 format_http_listener (u8 *s, va_list *args)
1330 http_conn_t *lhc = va_arg (*args, http_conn_t *);
1334 al = app_listener_get_w_handle (lhc->h_tc_session_handle);
1335 lts = app_listener_get_session (al);
1336 s = format (s, "[%d:%d][H] app_wrk %u ts %d:%d", lhc->c_thread_index,
1337 lhc->c_s_index, lhc->h_pa_wrk_index, lts->thread_index,
1338 lts->session_index);
1344 format_http_conn_state (u8 *s, va_list *args)
1346 http_conn_t *hc = va_arg (*args, http_conn_t *);
1350 case HTTP_CONN_STATE_LISTEN:
1351 s = format (s, "LISTEN");
1353 case HTTP_CONN_STATE_CONNECTING:
1354 s = format (s, "CONNECTING");
1356 case HTTP_CONN_STATE_ESTABLISHED:
1357 s = format (s, "ESTABLISHED");
1359 case HTTP_CONN_STATE_TRANSPORT_CLOSED:
1360 s = format (s, "TRANSPORT_CLOSED");
1362 case HTTP_CONN_STATE_APP_CLOSED:
1363 s = format (s, "APP_CLOSED");
1365 case HTTP_CONN_STATE_CLOSED:
1366 s = format (s, "CLOSED");
1374 format_http_transport_connection (u8 *s, va_list *args)
1376 u32 tc_index = va_arg (*args, u32);
1377 u32 thread_index = va_arg (*args, u32);
1378 u32 verbose = va_arg (*args, u32);
1381 hc = http_conn_get_w_thread (tc_index, thread_index);
1383 s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_http_connection, hc);
1387 format (s, "%-" SESSION_CLI_STATE_LEN "U", format_http_conn_state, hc);
1389 s = format (s, "\n");
1396 format_http_transport_listener (u8 *s, va_list *args)
1398 u32 tc_index = va_arg (*args, u32);
1399 u32 __clib_unused thread_index = va_arg (*args, u32);
1400 u32 __clib_unused verbose = va_arg (*args, u32);
1401 http_conn_t *lhc = http_listener_get (tc_index);
1403 s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_http_listener, lhc);
1406 format (s, "%-" SESSION_CLI_STATE_LEN "U", format_http_conn_state, lhc);
1410 static const transport_proto_vft_t http_proto = {
1411 .enable = http_transport_enable,
1412 .connect = http_transport_connect,
1413 .start_listen = http_start_listen,
1414 .stop_listen = http_stop_listen,
1415 .close = http_transport_close,
1416 .custom_tx = http_app_tx_callback,
1417 .get_connection = http_transport_get_connection,
1418 .get_listener = http_transport_get_listener,
1419 .get_transport_endpoint = http_transport_get_endpoint,
1420 .format_connection = format_http_transport_connection,
1421 .format_listener = format_http_transport_listener,
1422 .transport_options = {
1425 .tx_type = TRANSPORT_TX_INTERNAL,
1426 .service_type = TRANSPORT_SERVICE_APP,
1430 static clib_error_t *
1431 http_transport_init (vlib_main_t *vm)
1433 http_main_t *hm = &http_main;
1435 transport_register_protocol (TRANSPORT_PROTO_HTTP, &http_proto,
1436 FIB_PROTOCOL_IP4, ~0);
1437 transport_register_protocol (TRANSPORT_PROTO_HTTP, &http_proto,
1438 FIB_PROTOCOL_IP6, ~0);
1440 /* Default values, configurable via startup conf */
1441 hm->add_seg_size = 256 << 20;
1442 hm->first_seg_size = 32 << 20;
1443 hm->fifo_size = 512 << 10;
1448 VLIB_INIT_FUNCTION (http_transport_init);
1450 static clib_error_t *
1451 http_config_fn (vlib_main_t *vm, unformat_input_t *input)
1453 http_main_t *hm = &http_main;
1456 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1458 if (unformat (input, "first-segment-size %U", unformat_memory_size,
1461 hm->first_seg_size = clib_max (mem_sz, 1 << 20);
1462 if (hm->first_seg_size != mem_sz)
1463 clib_warning ("first seg size too small %u", mem_sz);
1465 else if (unformat (input, "add-segment-size %U", unformat_memory_size,
1468 hm->add_seg_size = clib_max (mem_sz, 1 << 20);
1469 if (hm->add_seg_size != mem_sz)
1470 clib_warning ("add seg size too small %u", mem_sz);
1472 else if (unformat (input, "fifo-size %U", unformat_memory_size, &mem_sz))
1474 hm->fifo_size = clib_clamp (mem_sz, 4 << 10, 2 << 30);
1475 if (hm->fifo_size != mem_sz)
1476 clib_warning ("invalid fifo size %lu", mem_sz);
1479 return clib_error_return (0, "unknown input `%U'",
1480 format_unformat_error, input);
1485 VLIB_CONFIG_FUNCTION (http_config_fn, "http");
1487 VLIB_PLUGIN_REGISTER () = {
1488 .version = VPP_BUILD_VER,
1489 .description = "Hypertext Transfer Protocol (HTTP)",
1490 .default_disabled = 0,
1494 * fd.io coding-style-patch-verification: ON
1497 * eval: (c-set-style "gnu")