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->session_type = session_type_from_proto_and_ip (
265 TRANSPORT_PROTO_HTTP, session_type_is_ip4 (ts->session_type));
267 app_wrk = app_worker_get (hc->h_pa_wrk_index);
270 clib_warning ("no app worker");
274 if ((rv = app_worker_init_connected (app_wrk, as)))
276 HTTP_DBG (1, "failed to allocate fifos");
280 app_worker_connect_notify (app_wrk, as, err, hc->h_pa_app_api_ctx);
281 hc->h_pa_session_handle = session_handle (as);
282 http_conn_timer_start (hc);
288 http_ts_disconnect_callback (session_t *ts)
292 hc = http_conn_get_w_thread (ts->opaque, ts->thread_index);
294 if (hc->state < HTTP_CONN_STATE_TRANSPORT_CLOSED)
295 hc->state = HTTP_CONN_STATE_TRANSPORT_CLOSED;
297 /* Nothing more to rx, propagate to app */
298 if (!svm_fifo_max_dequeue_cons (ts->rx_fifo))
299 session_transport_closing_notify (&hc->connection);
303 http_ts_reset_callback (session_t *ts)
307 hc = http_conn_get_w_thread (ts->opaque, ts->thread_index);
309 hc->state = HTTP_CONN_STATE_CLOSED;
310 http_buffer_free (&hc->tx_buf);
311 hc->http_state = HTTP_STATE_WAIT_METHOD;
312 session_transport_reset_notify (&hc->connection);
314 http_disconnect_transport (hc);
318 * http error boilerplate
320 static const char *http_error_template = "HTTP/1.1 %s\r\n"
322 "Content-Type: text/html\r\n"
323 "Connection: close\r\n"
324 "Pragma: no-cache\r\n"
325 "Content-Length: 0\r\n\r\n";
327 static const char *http_redirect_template = "HTTP/1.1 %s\r\n";
330 * http response boilerplate
332 static const char *http_response_template = "HTTP/1.1 %s\r\n"
334 "Expires: %U GMT\r\n"
335 "Server: VPP Static\r\n"
336 "Content-Type: %s\r\n"
337 "Content-Length: %lu\r\n\r\n";
339 static const char *http_request_template = "GET %s HTTP/1.1\r\n"
340 "User-Agent: VPP HTTP client\r\n"
344 send_data (http_conn_t *hc, u8 *data, u32 length, u32 offset)
346 const u32 max_burst = 64 << 10;
351 ts = session_get_from_handle (hc->h_tc_session_handle);
353 to_send = clib_min (length - offset, max_burst);
354 sent = svm_fifo_enqueue (ts->tx_fifo, to_send, data + offset);
359 if (svm_fifo_set_event (ts->tx_fifo))
360 session_send_io_evt_to_thread (ts->tx_fifo, SESSION_IO_EVT_TX);
362 return (offset + sent);
366 send_error (http_conn_t *hc, http_status_code_t ec)
368 http_main_t *hm = &http_main;
372 if (ec >= HTTP_N_STATUS)
373 ec = HTTP_STATUS_INTERNAL_ERROR;
375 now = clib_timebase_now (&hm->timebase);
376 data = format (0, http_error_template, http_status_code_str[ec],
377 format_clib_timebase_time, now);
378 send_data (hc, data, vec_len (data), 0);
383 read_http_message (http_conn_t *hc)
385 u32 max_deq, cursize;
389 ts = session_get_from_handle (hc->h_tc_session_handle);
391 cursize = vec_len (hc->rx_buf);
392 max_deq = svm_fifo_max_dequeue (ts->rx_fifo);
393 if (PREDICT_FALSE (max_deq == 0))
396 vec_validate (hc->rx_buf, cursize + max_deq - 1);
397 n_read = svm_fifo_dequeue (ts->rx_fifo, max_deq, hc->rx_buf + cursize);
398 ASSERT (n_read == max_deq);
400 if (svm_fifo_is_empty (ts->rx_fifo))
401 svm_fifo_unset_event (ts->rx_fifo);
403 vec_set_len (hc->rx_buf, cursize + n_read);
408 v_find_index (u8 *vec, u32 offset, char *str)
410 int start_index = offset;
411 u32 slen = (u32) strnlen_s_inline (str, 16);
412 u32 vlen = vec_len (vec);
419 for (; start_index < (vlen - slen); start_index++)
421 if (!memcmp (vec + start_index, str, slen))
429 * waiting for request method from peer - parse request method and data
431 static http_sm_result_t
432 state_srv_wait_method (http_conn_t *hc, transport_send_params_t *sp)
434 http_status_code_t ec;
435 app_worker_t *app_wrk;
442 rv = read_http_message (hc);
444 /* Nothing yet, wait for data or timer expire */
448 if (vec_len (hc->rx_buf) < 8)
450 ec = HTTP_STATUS_BAD_REQUEST;
454 if ((i = v_find_index (hc->rx_buf, 0, "GET ")) >= 0)
456 hc->method = HTTP_REQ_GET;
457 hc->rx_buf_offset = i + 5;
459 i = v_find_index (hc->rx_buf, hc->rx_buf_offset, "HTTP");
462 ec = HTTP_STATUS_BAD_REQUEST;
466 len = i - hc->rx_buf_offset - 1;
468 else if ((i = v_find_index (hc->rx_buf, 0, "POST ")) >= 0)
470 hc->method = HTTP_REQ_POST;
471 hc->rx_buf_offset = i + 6;
472 len = vec_len (hc->rx_buf) - hc->rx_buf_offset - 1;
476 HTTP_DBG (0, "Unknown http method");
477 ec = HTTP_STATUS_METHOD_NOT_ALLOWED;
481 buf = &hc->rx_buf[hc->rx_buf_offset];
483 msg.type = HTTP_MSG_REQUEST;
484 msg.method_type = hc->method;
485 msg.content_type = HTTP_CONTENT_TEXT_HTML;
486 msg.data.type = HTTP_MSG_DATA_INLINE;
489 svm_fifo_seg_t segs[2] = { { (u8 *) &msg, sizeof (msg) }, { buf, len } };
491 as = session_get_from_handle (hc->h_pa_session_handle);
492 rv = svm_fifo_enqueue_segments (as->rx_fifo, segs, 2, 0 /* allow partial */);
493 if (rv < 0 || rv != sizeof (msg) + len)
495 clib_warning ("failed app enqueue");
496 /* This should not happen as we only handle 1 request per session,
497 * and fifo is allocated, but going forward we should consider
499 return HTTP_SM_ERROR;
502 vec_free (hc->rx_buf);
503 hc->http_state = HTTP_STATE_WAIT_APP;
505 app_wrk = app_worker_get_if_valid (as->app_wrk_index);
507 app_worker_rx_notify (app_wrk, as);
514 session_transport_closing_notify (&hc->connection);
515 http_disconnect_transport (hc);
517 return HTTP_SM_ERROR;
521 * waiting for data from app
523 static http_sm_result_t
524 state_srv_wait_app (http_conn_t *hc, transport_send_params_t *sp)
526 http_main_t *hm = &http_main;
527 http_status_code_t ec;
535 as = session_get_from_handle (hc->h_pa_session_handle);
537 rv = svm_fifo_dequeue (as->tx_fifo, sizeof (msg), (u8 *) &msg);
538 ASSERT (rv == sizeof (msg));
540 if (msg.type != HTTP_MSG_REPLY || msg.data.type > HTTP_MSG_DATA_PTR)
542 clib_warning ("unexpected msg type from app %u", msg.type);
543 ec = HTTP_STATUS_INTERNAL_ERROR;
552 case HTTP_STATUS_MOVED:
558 http_buffer_init (&hc->tx_buf, msg_to_buf_type[msg.data.type], as->tx_fifo,
562 * Add headers. For now:
568 now = clib_timebase_now (&hm->timebase);
574 format (0, http_response_template, http_status_code_str[msg.code],
576 format_clib_timebase_time, now,
578 format_clib_timebase_time, now + 600.0,
580 http_content_type_str[msg.content_type],
584 case HTTP_STATUS_MOVED:
586 format (0, http_redirect_template, http_status_code_str[msg.code]);
587 /* Location: http(s)://new-place already queued up as data */
593 offset = send_data (hc, header, vec_len (header), 0);
594 if (offset != vec_len (header))
596 clib_warning ("couldn't send response header!");
597 ec = HTTP_STATUS_INTERNAL_ERROR;
602 /* Start sending the actual data */
603 hc->http_state = HTTP_STATE_IO_MORE_DATA;
605 ASSERT (sp->max_burst_size >= offset);
606 sp->max_burst_size -= offset;
608 return HTTP_SM_CONTINUE;
613 hc->http_state = HTTP_STATE_WAIT_METHOD;
614 session_transport_closing_notify (&hc->connection);
615 http_disconnect_transport (hc);
620 static http_sm_result_t
621 state_srv_send_more_data (http_conn_t *hc, transport_send_params_t *sp)
623 u32 max_send = 64 << 10, n_segs;
624 http_buffer_t *hb = &hc->tx_buf;
629 max_send = clib_min (max_send, sp->max_burst_size);
630 ts = session_get_from_handle (hc->h_tc_session_handle);
631 if ((seg = http_buffer_get_segs (hb, max_send, &n_segs)))
632 sent = svm_fifo_enqueue_segments (ts->tx_fifo, seg, n_segs,
633 1 /* allow partial */);
637 /* Ask scheduler to notify app of deq event if needed */
638 sp->bytes_dequeued += http_buffer_drain (hb, sent);
639 sp->max_burst_size -= sent;
642 /* Not finished sending all data */
643 if (!http_buffer_is_drained (hb))
645 if (sent && svm_fifo_set_event (ts->tx_fifo))
646 session_send_io_evt_to_thread (ts->tx_fifo, SESSION_IO_EVT_TX);
648 if (svm_fifo_max_enqueue (ts->tx_fifo) < HTTP_FIFO_THRESH)
650 /* Deschedule http session and wait for deq notification if
651 * underlying ts tx fifo almost full */
652 svm_fifo_add_want_deq_ntf (ts->tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
653 transport_connection_deschedule (&hc->connection);
654 sp->flags |= TRANSPORT_SND_F_DESCHED;
659 if (sent && svm_fifo_set_event (ts->tx_fifo))
660 session_send_io_evt_to_thread (ts->tx_fifo, SESSION_IO_EVT_TX_FLUSH);
662 /* Finished transaction, back to HTTP_STATE_WAIT_METHOD */
663 hc->http_state = HTTP_STATE_WAIT_METHOD;
664 http_buffer_free (&hc->tx_buf);
671 parse_http_header (http_conn_t *hc, int *content_length)
673 unformat_input_t input;
677 if ((i = v_find_index (hc->rx_buf, hc->rx_buf_offset, "200 OK") < 0))
679 clib_warning ("bad response code");
683 i = v_find_index (hc->rx_buf, hc->rx_buf_offset, CONTENT_LEN_STR);
686 clib_warning ("cannot find '%s' in the header!", CONTENT_LEN_STR);
690 hc->rx_buf_offset = i;
692 i = v_find_index (hc->rx_buf, hc->rx_buf_offset, "\n");
695 clib_warning ("end of line missing; incomplete data");
699 len = i - hc->rx_buf_offset;
700 line = vec_new (u8, len);
701 clib_memcpy (line, hc->rx_buf + hc->rx_buf_offset, len);
703 unformat_init_vector (&input, line);
704 if (!unformat (&input, CONTENT_LEN_STR "%d", content_length))
706 clib_warning ("failed to unformat content length!");
709 unformat_free (&input);
711 /* skip rest of the header */
712 hc->rx_buf_offset += len;
713 i = v_find_index (hc->rx_buf, hc->rx_buf_offset, "<html>");
716 clib_warning ("<html> tag not found");
719 hc->rx_buf_offset = i;
725 state_cln_wait_method (http_conn_t *hc, transport_send_params_t *sp)
729 app_worker_t *app_wrk;
730 int rv, content_length;
732 rv = read_http_message (hc);
736 msg.type = HTTP_MSG_REPLY;
737 msg.content_type = HTTP_CONTENT_TEXT_HTML;
738 msg.code = HTTP_STATUS_OK;
739 msg.data.type = HTTP_MSG_DATA_INLINE;
742 rv = parse_http_header (hc, &content_length);
745 clib_warning ("failed to parse http reply");
746 session_transport_closing_notify (&hc->connection);
747 http_disconnect_transport (hc);
751 msg.data.len = content_length;
752 u32 dlen = vec_len (hc->rx_buf) - hc->rx_buf_offset;
753 as = session_get_from_handle (hc->h_pa_session_handle);
754 svm_fifo_seg_t segs[2] = { { (u8 *) &msg, sizeof (msg) },
755 { &hc->rx_buf[hc->rx_buf_offset], dlen } };
757 rv = svm_fifo_enqueue_segments (as->rx_fifo, segs, 2, 0 /* allow partial */);
760 clib_warning ("error enqueue");
761 return HTTP_SM_ERROR;
763 hc->rx_buf_offset += dlen;
764 hc->http_state = HTTP_STATE_IO_MORE_DATA;
765 hc->to_recv = content_length - dlen;
767 if (hc->rx_buf_offset == vec_len (hc->rx_buf))
769 vec_reset_length (hc->rx_buf);
770 hc->rx_buf_offset = 0;
773 if (hc->to_recv == 0)
775 hc->rx_buf_offset = 0;
776 vec_reset_length (hc->rx_buf);
777 hc->http_state = HTTP_STATE_WAIT_APP;
780 app_wrk = app_worker_get_if_valid (as->app_wrk_index);
781 app_worker_rx_notify (app_wrk, as);
786 cln_drain_rx_buf (http_conn_t *hc, session_t *ts, session_t *as)
788 app_worker_t *app_wrk;
789 u32 max_enq, n_enq, dlen = vec_len (hc->rx_buf) - hc->rx_buf_offset;
792 max_enq = svm_fifo_max_enqueue (as->rx_fifo);
793 n_enq = clib_min (max_enq, dlen);
794 rv = svm_fifo_enqueue (as->rx_fifo, n_enq, &hc->rx_buf[hc->rx_buf_offset]);
797 clib_warning ("enqueue failed");
801 hc->rx_buf_offset += rv;
803 if (hc->rx_buf_offset >= vec_len (hc->rx_buf))
805 vec_reset_length (hc->rx_buf);
806 hc->rx_buf_offset = 0;
809 app_wrk = app_worker_get_if_valid (as->app_wrk_index);
812 app_worker_rx_notify (app_wrk, as);
816 static http_sm_result_t
817 state_cln_recv_more_data (http_conn_t *hc, transport_send_params_t *sp)
824 as = session_get_from_handle (hc->h_pa_session_handle);
825 ts = session_get_from_handle (hc->h_tc_session_handle);
827 u32 dlen = vec_len (hc->rx_buf) - hc->rx_buf_offset;
830 rv = cln_drain_rx_buf (hc, ts, as);
833 clib_warning ("drain rx error!");
834 return HTTP_SM_ERROR;
836 goto maybe_reschedule;
839 if (hc->to_recv == 0)
841 ASSERT (vec_len (hc->rx_buf) == 0);
842 ASSERT (hc->rx_buf_offset == 0);
843 hc->http_state = HTTP_STATE_WAIT_APP;
847 max_deq = svm_fifo_max_dequeue (ts->rx_fifo);
851 ASSERT (vec_len (hc->rx_buf) == 0);
852 ASSERT (hc->rx_buf_offset == 0);
854 vec_validate (hc->rx_buf, max_deq - 1);
855 n_read = svm_fifo_dequeue (ts->rx_fifo, max_deq, hc->rx_buf);
856 ASSERT (n_read == max_deq);
858 if (svm_fifo_is_empty (ts->rx_fifo))
859 svm_fifo_unset_event (ts->rx_fifo);
861 hc->to_recv -= n_read;
862 vec_set_len (hc->rx_buf, max_deq);
865 if (hc->rx_buf_offset < vec_len (hc->rx_buf) ||
866 svm_fifo_max_dequeue_cons (ts->rx_fifo))
868 /* TODO is the flag really needed? */
869 if (svm_fifo_set_event (ts->rx_fifo))
870 session_enqueue_notify (ts);
872 return HTTP_SM_CONTINUE;
875 static http_sm_result_t
876 state_cln_wait_app (http_conn_t *hc, transport_send_params_t *sp)
880 http_status_code_t ec;
881 u8 *buf = 0, *request;
885 as = session_get_from_handle (hc->h_pa_session_handle);
886 rv = svm_fifo_dequeue (as->tx_fifo, sizeof (msg), (u8 *) &msg);
887 ASSERT (rv == sizeof (msg));
888 if (msg.type != HTTP_MSG_REQUEST || msg.data.type > HTTP_MSG_DATA_PTR)
890 clib_warning ("unexpected msg type from app %u", msg.type);
891 ec = HTTP_STATUS_INTERNAL_ERROR;
895 vec_validate (buf, msg.data.len - 1);
896 rv = svm_fifo_dequeue (as->tx_fifo, msg.data.len, buf);
897 ASSERT (rv == msg.data.len);
899 request = format (0, http_request_template, buf);
900 offset = send_data (hc, request, vec_len (request), 0);
901 if (offset != vec_len (request))
903 clib_warning ("sending request failed!");
904 ec = HTTP_STATUS_INTERNAL_ERROR;
908 hc->http_state = HTTP_STATE_WAIT_METHOD;
913 return HTTP_SM_CONTINUE;
917 session_transport_closing_notify (&hc->connection);
918 http_disconnect_transport (hc);
922 typedef http_sm_result_t (*http_sm_handler) (http_conn_t *,
923 transport_send_params_t *sp);
925 static http_sm_handler srv_state_funcs[HTTP_N_STATES] = {
926 /* Waiting for GET, POST, etc. */
927 state_srv_wait_method,
928 /* Wait for data from app */
931 state_srv_send_more_data,
934 static http_sm_handler cln_state_funcs[HTTP_N_STATES] = {
936 state_cln_wait_method,
937 /* wait for data from app */
939 /* receive more data */
940 state_cln_recv_more_data,
944 http_req_run_state_machine (http_conn_t *hc, transport_send_params_t *sp)
946 http_sm_result_t res;
947 http_sm_handler *state_fn =
948 hc->is_client ? cln_state_funcs : srv_state_funcs;
951 res = state_fn[hc->http_state](hc, sp);
952 if (res == HTTP_SM_ERROR)
955 while (res == HTTP_SM_CONTINUE);
957 /* Reset the session expiration timer */
958 http_conn_timer_update (hc);
962 http_ts_server_rx_callback (session_t *ts, http_conn_t *hc)
964 if (hc->http_state != HTTP_STATE_WAIT_METHOD)
966 clib_warning ("tcp data in req state %u", hc->http_state);
970 http_req_run_state_machine (hc, 0);
972 if (hc->state == HTTP_CONN_STATE_TRANSPORT_CLOSED)
974 if (!svm_fifo_max_dequeue_cons (ts->rx_fifo))
975 session_transport_closing_notify (&hc->connection);
981 http_ts_client_rx_callback (session_t *ts, http_conn_t *hc)
983 if (hc->http_state != HTTP_STATE_WAIT_METHOD &&
984 hc->http_state != HTTP_STATE_IO_MORE_DATA)
986 clib_warning ("http in unexpected state %d (ts %d)", hc->http_state,
991 http_req_run_state_machine (hc, 0);
993 if (hc->state == HTTP_CONN_STATE_TRANSPORT_CLOSED)
995 if (!svm_fifo_max_dequeue_cons (ts->rx_fifo))
996 session_transport_closing_notify (&hc->connection);
1002 http_ts_rx_callback (session_t *ts)
1006 hc = http_conn_get_w_thread (ts->opaque, ts->thread_index);
1008 return http_ts_client_rx_callback (ts, hc);
1009 return http_ts_server_rx_callback (ts, hc);
1013 http_ts_builtin_tx_callback (session_t *ts)
1017 hc = http_conn_get_w_thread (ts->opaque, ts->thread_index);
1018 transport_connection_reschedule (&hc->connection);
1024 http_ts_cleanup_callback (session_t *ts, session_cleanup_ntf_t ntf)
1028 if (ntf == SESSION_CLEANUP_TRANSPORT)
1031 hc = http_conn_get_w_thread (ts->opaque, ts->thread_index);
1034 clib_warning ("no http connection for %u", ts->session_index);
1038 vec_free (hc->rx_buf);
1040 http_buffer_free (&hc->tx_buf);
1041 http_conn_timer_stop (hc);
1043 session_transport_delete_notify (&hc->connection);
1044 http_conn_free (hc);
1048 http_add_segment_callback (u32 client_index, u64 segment_handle)
1050 /* No-op for builtin */
1055 http_del_segment_callback (u32 client_index, u64 segment_handle)
1060 static session_cb_vft_t http_app_cb_vft = {
1061 .session_accept_callback = http_ts_accept_callback,
1062 .session_disconnect_callback = http_ts_disconnect_callback,
1063 .session_connected_callback = http_ts_connected_callback,
1064 .session_reset_callback = http_ts_reset_callback,
1065 .session_cleanup_callback = http_ts_cleanup_callback,
1066 .add_segment_callback = http_add_segment_callback,
1067 .del_segment_callback = http_del_segment_callback,
1068 .builtin_app_rx_callback = http_ts_rx_callback,
1069 .builtin_app_tx_callback = http_ts_builtin_tx_callback,
1072 static clib_error_t *
1073 http_transport_enable (vlib_main_t *vm, u8 is_en)
1075 vnet_app_detach_args_t _da, *da = &_da;
1076 vnet_app_attach_args_t _a, *a = &_a;
1077 u64 options[APP_OPTIONS_N_OPTIONS];
1078 http_main_t *hm = &http_main;
1082 da->app_index = hm->app_index;
1083 da->api_client_index = APP_INVALID_INDEX;
1084 vnet_application_detach (da);
1088 vec_validate (hm->wrk, vlib_num_workers ());
1090 clib_memset (a, 0, sizeof (*a));
1091 clib_memset (options, 0, sizeof (options));
1093 a->session_cb_vft = &http_app_cb_vft;
1094 a->api_client_index = APP_INVALID_INDEX;
1095 a->options = options;
1096 a->name = format (0, "http");
1097 a->options[APP_OPTIONS_SEGMENT_SIZE] = hm->first_seg_size;
1098 a->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = hm->add_seg_size;
1099 a->options[APP_OPTIONS_RX_FIFO_SIZE] = hm->fifo_size;
1100 a->options[APP_OPTIONS_TX_FIFO_SIZE] = hm->fifo_size;
1101 a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
1102 a->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
1103 a->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_IS_TRANSPORT_APP;
1105 if (vnet_application_attach (a))
1106 return clib_error_return (0, "failed to attach http app");
1108 hm->app_index = a->app_index;
1111 clib_timebase_init (&hm->timebase, 0 /* GMT */, CLIB_TIMEBASE_DAYLIGHT_NONE,
1112 &vm->clib_time /* share the system clock */);
1114 http_timers_init (vm, http_conn_timeout_cb);
1120 http_transport_connect (transport_endpoint_cfg_t *tep)
1122 vnet_connect_args_t _cargs, *cargs = &_cargs;
1123 http_main_t *hm = &http_main;
1124 session_endpoint_cfg_t *sep = (session_endpoint_cfg_t *) tep;
1129 app_worker_t *app_wrk = app_worker_get (sep->app_wrk_index);
1131 clib_memset (cargs, 0, sizeof (*cargs));
1132 clib_memcpy (&cargs->sep_ext, sep, sizeof (session_endpoint_cfg_t));
1133 cargs->sep.transport_proto = TRANSPORT_PROTO_TCP;
1134 cargs->app_index = hm->app_index;
1135 app = application_get (app_wrk->app_index);
1136 cargs->sep_ext.ns_index = app->ns_index;
1138 hc_index = http_conn_alloc_w_thread (0 /* ts->thread_index */);
1139 hc = http_conn_get_w_thread (hc_index, 0);
1140 hc->h_pa_wrk_index = sep->app_wrk_index;
1141 hc->h_pa_app_api_ctx = sep->opaque;
1143 hc->state = HTTP_CONN_STATE_CONNECTING;
1144 cargs->api_context = hc_index;
1146 if ((error = vnet_connect (cargs)))
1153 http_start_listen (u32 app_listener_index, transport_endpoint_cfg_t *tep)
1155 vnet_listen_args_t _args = {}, *args = &_args;
1156 session_t *ts_listener, *app_listener;
1157 http_main_t *hm = &http_main;
1158 session_endpoint_cfg_t *sep;
1159 app_worker_t *app_wrk;
1160 transport_proto_t tp;
1166 sep = (session_endpoint_cfg_t *) tep;
1168 app_wrk = app_worker_get (sep->app_wrk_index);
1169 app = application_get (app_wrk->app_index);
1171 args->app_index = hm->app_index;
1172 args->sep_ext = *sep;
1173 args->sep_ext.ns_index = app->ns_index;
1174 tp = sep->ext_cfg ? TRANSPORT_PROTO_TLS : TRANSPORT_PROTO_TCP;
1175 args->sep_ext.transport_proto = tp;
1177 if (vnet_listen (args))
1178 return SESSION_INVALID_INDEX;
1180 lhc_index = http_listener_alloc ();
1181 lhc = http_listener_get (lhc_index);
1183 /* Grab transport connection listener and link to http listener */
1184 lhc->h_tc_session_handle = args->handle;
1185 al = app_listener_get_w_handle (lhc->h_tc_session_handle);
1186 ts_listener = app_listener_get_session (al);
1187 ts_listener->opaque = lhc_index;
1189 /* Grab application listener and link to http listener */
1190 app_listener = listen_session_get (app_listener_index);
1191 lhc->h_pa_wrk_index = sep->app_wrk_index;
1192 lhc->h_pa_session_handle = listen_session_get_handle (app_listener);
1193 lhc->c_s_index = app_listener_index;
1194 lhc->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
1200 http_stop_listen (u32 listener_index)
1205 lhc = http_listener_get (listener_index);
1207 vnet_unlisten_args_t a = {
1208 .handle = lhc->h_tc_session_handle,
1209 .app_index = http_main.app_index,
1210 .wrk_map_index = 0 /* default wrk */
1213 if ((rv = vnet_unlisten (&a)))
1214 clib_warning ("unlisten returned %d", rv);
1216 http_listener_free (lhc);
1222 http_transport_close (u32 hc_index, u32 thread_index)
1227 HTTP_DBG (1, "App disconnecting %x", hc_index);
1229 hc = http_conn_get_w_thread (hc_index, thread_index);
1230 if (hc->state == HTTP_CONN_STATE_CONNECTING)
1232 hc->state = HTTP_CONN_STATE_APP_CLOSED;
1233 http_disconnect_transport (hc);
1237 as = session_get_from_handle (hc->h_pa_session_handle);
1239 /* Nothing more to send, confirm close */
1240 if (!svm_fifo_max_dequeue_cons (as->tx_fifo))
1242 session_transport_closed_notify (&hc->connection);
1243 http_disconnect_transport (hc);
1247 /* Wait for all data to be written to ts */
1248 hc->state = HTTP_CONN_STATE_APP_CLOSED;
1252 static transport_connection_t *
1253 http_transport_get_connection (u32 hc_index, u32 thread_index)
1255 http_conn_t *hc = http_conn_get_w_thread (hc_index, thread_index);
1256 return &hc->connection;
1259 static transport_connection_t *
1260 http_transport_get_listener (u32 listener_index)
1262 http_conn_t *lhc = http_listener_get (listener_index);
1263 return &lhc->connection;
1267 http_app_tx_callback (void *session, transport_send_params_t *sp)
1269 session_t *as = (session_t *) session;
1270 u32 max_burst_sz, sent;
1273 hc = http_conn_get_w_thread (as->connection_index, as->thread_index);
1274 if (hc->http_state < HTTP_STATE_WAIT_APP)
1276 if (hc->state != HTTP_CONN_STATE_CLOSED)
1277 clib_warning ("app data req state %u session state %u", hc->http_state,
1279 svm_fifo_dequeue_drop_all (as->tx_fifo);
1283 max_burst_sz = sp->max_burst_size * TRANSPORT_PACER_MIN_MSS;
1284 sp->max_burst_size = max_burst_sz;
1286 http_req_run_state_machine (hc, sp);
1288 if (hc->state == HTTP_CONN_STATE_APP_CLOSED)
1290 if (!svm_fifo_max_dequeue_cons (as->tx_fifo))
1291 http_disconnect_transport (hc);
1294 sent = max_burst_sz - sp->max_burst_size;
1296 return sent > 0 ? clib_max (sent / TRANSPORT_PACER_MIN_MSS, 1) : 0;
1300 http_transport_get_endpoint (u32 hc_index, u32 thread_index,
1301 transport_endpoint_t *tep, u8 is_lcl)
1303 http_conn_t *hc = http_conn_get_w_thread (hc_index, thread_index);
1306 ts = session_get_from_handle (hc->h_tc_session_handle);
1307 session_get_endpoint (ts, tep, is_lcl);
1311 format_http_connection (u8 *s, va_list *args)
1313 http_conn_t *hc = va_arg (*args, http_conn_t *);
1316 ts = session_get_from_handle (hc->h_tc_session_handle);
1317 s = format (s, "[%d:%d][H] app_wrk %u ts %d:%d", hc->c_thread_index,
1318 hc->c_s_index, hc->h_pa_wrk_index, ts->thread_index,
1325 format_http_listener (u8 *s, va_list *args)
1327 http_conn_t *lhc = va_arg (*args, http_conn_t *);
1331 al = app_listener_get_w_handle (lhc->h_tc_session_handle);
1332 lts = app_listener_get_session (al);
1333 s = format (s, "[%d:%d][H] app_wrk %u ts %d:%d", lhc->c_thread_index,
1334 lhc->c_s_index, lhc->h_pa_wrk_index, lts->thread_index,
1335 lts->session_index);
1341 format_http_conn_state (u8 *s, va_list *args)
1343 http_conn_t *hc = va_arg (*args, http_conn_t *);
1347 case HTTP_CONN_STATE_LISTEN:
1348 s = format (s, "LISTEN");
1350 case HTTP_CONN_STATE_CONNECTING:
1351 s = format (s, "CONNECTING");
1353 case HTTP_CONN_STATE_ESTABLISHED:
1354 s = format (s, "ESTABLISHED");
1356 case HTTP_CONN_STATE_TRANSPORT_CLOSED:
1357 s = format (s, "TRANSPORT_CLOSED");
1359 case HTTP_CONN_STATE_APP_CLOSED:
1360 s = format (s, "APP_CLOSED");
1362 case HTTP_CONN_STATE_CLOSED:
1363 s = format (s, "CLOSED");
1371 format_http_transport_connection (u8 *s, va_list *args)
1373 u32 tc_index = va_arg (*args, u32);
1374 u32 thread_index = va_arg (*args, u32);
1375 u32 verbose = va_arg (*args, u32);
1378 hc = http_conn_get_w_thread (tc_index, thread_index);
1380 s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_http_connection, hc);
1384 format (s, "%-" SESSION_CLI_STATE_LEN "U", format_http_conn_state, hc);
1386 s = format (s, "\n");
1393 format_http_transport_listener (u8 *s, va_list *args)
1395 u32 tc_index = va_arg (*args, u32);
1396 u32 __clib_unused thread_index = va_arg (*args, u32);
1397 u32 __clib_unused verbose = va_arg (*args, u32);
1398 http_conn_t *lhc = http_listener_get (tc_index);
1400 s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_http_listener, lhc);
1403 format (s, "%-" SESSION_CLI_STATE_LEN "U", format_http_conn_state, lhc);
1407 static const transport_proto_vft_t http_proto = {
1408 .enable = http_transport_enable,
1409 .connect = http_transport_connect,
1410 .start_listen = http_start_listen,
1411 .stop_listen = http_stop_listen,
1412 .close = http_transport_close,
1413 .custom_tx = http_app_tx_callback,
1414 .get_connection = http_transport_get_connection,
1415 .get_listener = http_transport_get_listener,
1416 .get_transport_endpoint = http_transport_get_endpoint,
1417 .format_connection = format_http_transport_connection,
1418 .format_listener = format_http_transport_listener,
1419 .transport_options = {
1422 .tx_type = TRANSPORT_TX_INTERNAL,
1423 .service_type = TRANSPORT_SERVICE_APP,
1427 static clib_error_t *
1428 http_transport_init (vlib_main_t *vm)
1430 http_main_t *hm = &http_main;
1432 transport_register_protocol (TRANSPORT_PROTO_HTTP, &http_proto,
1433 FIB_PROTOCOL_IP4, ~0);
1434 transport_register_protocol (TRANSPORT_PROTO_HTTP, &http_proto,
1435 FIB_PROTOCOL_IP6, ~0);
1437 /* Default values, configurable via startup conf */
1438 hm->add_seg_size = 256 << 20;
1439 hm->first_seg_size = 32 << 20;
1440 hm->fifo_size = 512 << 10;
1445 VLIB_INIT_FUNCTION (http_transport_init);
1447 static clib_error_t *
1448 http_config_fn (vlib_main_t *vm, unformat_input_t *input)
1450 http_main_t *hm = &http_main;
1453 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1455 if (unformat (input, "first-segment-size %U", unformat_memory_size,
1458 hm->first_seg_size = clib_max (mem_sz, 1 << 20);
1459 if (hm->first_seg_size != mem_sz)
1460 clib_warning ("first seg size too small %u", mem_sz);
1462 else if (unformat (input, "add-segment-size %U", unformat_memory_size,
1465 hm->add_seg_size = clib_max (mem_sz, 1 << 20);
1466 if (hm->add_seg_size != mem_sz)
1467 clib_warning ("add seg size too small %u", mem_sz);
1469 else if (unformat (input, "fifo-size %U", unformat_memory_size, &mem_sz))
1471 hm->fifo_size = clib_clamp (mem_sz, 4 << 10, 2 << 30);
1472 if (hm->fifo_size != mem_sz)
1473 clib_warning ("invalid fifo size %lu", mem_sz);
1476 return clib_error_return (0, "unknown input `%U'",
1477 format_unformat_error, input);
1482 VLIB_CONFIG_FUNCTION (http_config_fn, "http");
1484 VLIB_PLUGIN_REGISTER () = {
1485 .version = VPP_BUILD_VER,
1486 .description = "Hypertext Transfer Protocol (HTTP)",
1487 .default_disabled = 0,
1491 * fd.io coding-style-patch-verification: ON
1494 * eval: (c-set-style "gnu")