2 * Copyright (c) 2022 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 #include <http/http.h>
17 #include <vnet/session/session.h>
18 #include <http/http_timer.h>
20 static http_main_t http_main;
22 #define HTTP_FIFO_THRESH (16 << 10)
23 #define CONTENT_LEN_STR "Content-Length: "
25 /* HTTP state machine result */
26 typedef enum http_sm_result_t_
33 const char *http_status_code_str[] = {
34 #define _(c, s, str) str,
35 foreach_http_status_code
39 const char *http_content_type_str[] = {
40 #define _(s, str) str,
41 foreach_http_content_type
45 const http_buffer_type_t msg_to_buf_type[] = {
46 [HTTP_MSG_DATA_INLINE] = HTTP_BUFFER_FIFO,
47 [HTTP_MSG_DATA_PTR] = HTTP_BUFFER_PTR,
50 static inline http_worker_t *
51 http_worker_get (u32 thread_index)
53 return &http_main.wrk[thread_index];
57 http_conn_alloc_w_thread (u32 thread_index)
59 http_worker_t *wrk = http_worker_get (thread_index);
62 pool_get_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";
328 * http response boilerplate
330 static const char *http_response_template = "HTTP/1.1 200 OK\r\n"
332 "Expires: %U GMT\r\n"
333 "Server: VPP Static\r\n"
334 "Content-Type: %s\r\n"
335 "Content-Length: %lu\r\n\r\n";
337 static const char *http_request_template = "GET %s HTTP/1.1\r\n"
338 "User-Agent: VPP HTTP client\r\n"
342 send_data (http_conn_t *hc, u8 *data, u32 length, u32 offset)
344 const u32 max_burst = 64 << 10;
349 ts = session_get_from_handle (hc->h_tc_session_handle);
351 to_send = clib_min (length - offset, max_burst);
352 sent = svm_fifo_enqueue (ts->tx_fifo, to_send, data + offset);
357 if (svm_fifo_set_event (ts->tx_fifo))
358 session_send_io_evt_to_thread (ts->tx_fifo, SESSION_IO_EVT_TX);
360 return (offset + sent);
364 send_error (http_conn_t *hc, http_status_code_t ec)
366 http_main_t *hm = &http_main;
370 if (ec >= HTTP_N_STATUS)
371 ec = HTTP_STATUS_INTERNAL_ERROR;
373 now = clib_timebase_now (&hm->timebase);
374 data = format (0, http_error_template, http_status_code_str[ec],
375 format_clib_timebase_time, now);
376 send_data (hc, data, vec_len (data), 0);
381 read_http_message (http_conn_t *hc)
383 u32 max_deq, cursize;
387 ts = session_get_from_handle (hc->h_tc_session_handle);
389 cursize = vec_len (hc->rx_buf);
390 max_deq = svm_fifo_max_dequeue (ts->rx_fifo);
391 if (PREDICT_FALSE (max_deq == 0))
394 vec_validate (hc->rx_buf, cursize + max_deq - 1);
395 n_read = svm_fifo_dequeue (ts->rx_fifo, max_deq, hc->rx_buf + cursize);
396 ASSERT (n_read == max_deq);
398 if (svm_fifo_is_empty (ts->rx_fifo))
399 svm_fifo_unset_event (ts->rx_fifo);
401 vec_set_len (hc->rx_buf, cursize + n_read);
406 v_find_index (u8 *vec, u32 offset, char *str)
408 int start_index = offset;
409 u32 slen = (u32) strnlen_s_inline (str, 16);
410 u32 vlen = vec_len (vec);
417 for (; start_index < (vlen - slen); start_index++)
419 if (!memcmp (vec + start_index, str, slen))
427 * waiting for request method from peer - parse request method and data
429 static http_sm_result_t
430 state_srv_wait_method (http_conn_t *hc, transport_send_params_t *sp)
432 http_status_code_t ec;
433 app_worker_t *app_wrk;
440 rv = read_http_message (hc);
442 /* Nothing yet, wait for data or timer expire */
446 if (vec_len (hc->rx_buf) < 8)
448 ec = HTTP_STATUS_BAD_REQUEST;
452 if ((i = v_find_index (hc->rx_buf, 0, "GET ")) >= 0)
454 hc->method = HTTP_REQ_GET;
455 hc->rx_buf_offset = i + 5;
457 i = v_find_index (hc->rx_buf, hc->rx_buf_offset, "HTTP");
460 ec = HTTP_STATUS_BAD_REQUEST;
464 len = i - hc->rx_buf_offset - 1;
466 else if ((i = v_find_index (hc->rx_buf, 0, "POST ")) >= 0)
468 hc->method = HTTP_REQ_POST;
469 hc->rx_buf_offset = i + 6;
470 len = vec_len (hc->rx_buf) - hc->rx_buf_offset - 1;
474 HTTP_DBG (0, "Unknown http method");
475 ec = HTTP_STATUS_METHOD_NOT_ALLOWED;
479 buf = &hc->rx_buf[hc->rx_buf_offset];
481 msg.type = HTTP_MSG_REQUEST;
482 msg.method_type = hc->method;
483 msg.content_type = HTTP_CONTENT_TEXT_HTML;
484 msg.data.type = HTTP_MSG_DATA_INLINE;
487 svm_fifo_seg_t segs[2] = { { (u8 *) &msg, sizeof (msg) }, { buf, len } };
489 as = session_get_from_handle (hc->h_pa_session_handle);
490 rv = svm_fifo_enqueue_segments (as->rx_fifo, segs, 2, 0 /* allow partial */);
491 if (rv < 0 || rv != sizeof (msg) + len)
493 clib_warning ("failed app enqueue");
494 /* This should not happen as we only handle 1 request per session,
495 * and fifo is allocated, but going forward we should consider
497 return HTTP_SM_ERROR;
500 vec_free (hc->rx_buf);
501 hc->http_state = HTTP_STATE_WAIT_APP;
503 app_wrk = app_worker_get_if_valid (as->app_wrk_index);
504 app_worker_lock_and_send_event (app_wrk, as, SESSION_IO_EVT_RX);
511 session_transport_closing_notify (&hc->connection);
512 http_disconnect_transport (hc);
514 return HTTP_SM_ERROR;
518 * waiting for data from app
520 static http_sm_result_t
521 state_srv_wait_app (http_conn_t *hc, transport_send_params_t *sp)
523 http_main_t *hm = &http_main;
524 http_status_code_t ec;
532 as = session_get_from_handle (hc->h_pa_session_handle);
534 rv = svm_fifo_dequeue (as->tx_fifo, sizeof (msg), (u8 *) &msg);
535 ASSERT (rv == sizeof (msg));
537 if (msg.type != HTTP_MSG_REPLY || msg.data.type > HTTP_MSG_DATA_PTR)
539 clib_warning ("unexpected msg type from app %u", msg.type);
540 ec = HTTP_STATUS_INTERNAL_ERROR;
544 if (msg.code != HTTP_STATUS_OK)
550 http_buffer_init (&hc->tx_buf, msg_to_buf_type[msg.data.type], as->tx_fifo,
554 * Add headers. For now:
560 now = clib_timebase_now (&hm->timebase);
561 header = format (0, http_response_template,
563 format_clib_timebase_time, now,
565 format_clib_timebase_time, now + 600.0,
567 http_content_type_str[msg.content_type],
571 offset = send_data (hc, header, vec_len (header), 0);
572 if (offset != vec_len (header))
574 clib_warning ("couldn't send response header!");
575 ec = HTTP_STATUS_INTERNAL_ERROR;
580 /* Start sending the actual data */
581 hc->http_state = HTTP_STATE_IO_MORE_DATA;
583 ASSERT (sp->max_burst_size >= offset);
584 sp->max_burst_size -= offset;
586 return HTTP_SM_CONTINUE;
591 hc->http_state = HTTP_STATE_WAIT_METHOD;
592 session_transport_closing_notify (&hc->connection);
593 http_disconnect_transport (hc);
598 static http_sm_result_t
599 state_srv_send_more_data (http_conn_t *hc, transport_send_params_t *sp)
601 u32 max_send = 64 << 10, n_segs;
602 http_buffer_t *hb = &hc->tx_buf;
607 max_send = clib_min (max_send, sp->max_burst_size);
608 ts = session_get_from_handle (hc->h_tc_session_handle);
609 if ((seg = http_buffer_get_segs (hb, max_send, &n_segs)))
610 sent = svm_fifo_enqueue_segments (ts->tx_fifo, seg, n_segs,
611 1 /* allow partial */);
615 /* Ask scheduler to notify app of deq event if needed */
616 sp->bytes_dequeued += http_buffer_drain (hb, sent);
617 sp->max_burst_size -= sent;
620 /* Not finished sending all data */
621 if (!http_buffer_is_drained (hb))
623 if (sent && svm_fifo_set_event (ts->tx_fifo))
624 session_send_io_evt_to_thread (ts->tx_fifo, SESSION_IO_EVT_TX);
626 if (svm_fifo_max_enqueue (ts->tx_fifo) < HTTP_FIFO_THRESH)
628 /* Deschedule http session and wait for deq notification if
629 * underlying ts tx fifo almost full */
630 svm_fifo_add_want_deq_ntf (ts->tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
631 transport_connection_deschedule (&hc->connection);
632 sp->flags |= TRANSPORT_SND_F_DESCHED;
637 if (sent && svm_fifo_set_event (ts->tx_fifo))
638 session_send_io_evt_to_thread (ts->tx_fifo, SESSION_IO_EVT_TX_FLUSH);
640 /* Finished transaction, back to HTTP_STATE_WAIT_METHOD */
641 hc->http_state = HTTP_STATE_WAIT_METHOD;
642 http_buffer_free (&hc->tx_buf);
649 parse_http_header (http_conn_t *hc, int *content_length)
651 unformat_input_t input;
655 if ((i = v_find_index (hc->rx_buf, hc->rx_buf_offset, "200 OK") < 0))
657 clib_warning ("bad response code");
661 i = v_find_index (hc->rx_buf, hc->rx_buf_offset, CONTENT_LEN_STR);
664 clib_warning ("cannot find '%s' in the header!", CONTENT_LEN_STR);
668 hc->rx_buf_offset = i;
670 i = v_find_index (hc->rx_buf, hc->rx_buf_offset, "\n");
673 clib_warning ("end of line missing; incomplete data");
677 len = i - hc->rx_buf_offset;
678 line = vec_new (u8, len);
679 clib_memcpy (line, hc->rx_buf + hc->rx_buf_offset, len);
681 unformat_init_vector (&input, line);
682 if (!unformat (&input, CONTENT_LEN_STR "%d", content_length))
684 clib_warning ("failed to unformat content length!");
687 unformat_free (&input);
689 /* skip rest of the header */
690 hc->rx_buf_offset += len;
691 i = v_find_index (hc->rx_buf, hc->rx_buf_offset, "<html>");
694 clib_warning ("<html> tag not found");
697 hc->rx_buf_offset = i;
703 state_cln_wait_method (http_conn_t *hc, transport_send_params_t *sp)
707 app_worker_t *app_wrk;
708 int rv, content_length;
710 rv = read_http_message (hc);
714 msg.type = HTTP_MSG_REPLY;
715 msg.content_type = HTTP_CONTENT_TEXT_HTML;
716 msg.code = HTTP_STATUS_OK;
717 msg.data.type = HTTP_MSG_DATA_INLINE;
720 rv = parse_http_header (hc, &content_length);
723 clib_warning ("failed to parse http reply");
724 session_transport_closing_notify (&hc->connection);
725 http_disconnect_transport (hc);
729 msg.data.len = content_length;
730 u32 dlen = vec_len (hc->rx_buf) - hc->rx_buf_offset;
731 as = session_get_from_handle (hc->h_pa_session_handle);
732 svm_fifo_seg_t segs[2] = { { (u8 *) &msg, sizeof (msg) },
733 { &hc->rx_buf[hc->rx_buf_offset], dlen } };
735 rv = svm_fifo_enqueue_segments (as->rx_fifo, segs, 2, 0 /* allow partial */);
738 clib_warning ("error enqueue");
739 return HTTP_SM_ERROR;
741 hc->rx_buf_offset += dlen;
742 hc->http_state = HTTP_STATE_IO_MORE_DATA;
743 hc->to_recv = content_length - dlen;
745 if (hc->rx_buf_offset == vec_len (hc->rx_buf))
747 vec_reset_length (hc->rx_buf);
748 hc->rx_buf_offset = 0;
751 if (hc->to_recv == 0)
753 hc->rx_buf_offset = 0;
754 vec_reset_length (hc->rx_buf);
755 hc->http_state = HTTP_STATE_WAIT_APP;
758 app_wrk = app_worker_get_if_valid (as->app_wrk_index);
759 app_worker_lock_and_send_event (app_wrk, as, SESSION_IO_EVT_RX);
764 cln_drain_rx_buf (http_conn_t *hc, session_t *ts, session_t *as)
766 app_worker_t *app_wrk;
767 u32 max_enq, n_enq, dlen = vec_len (hc->rx_buf) - hc->rx_buf_offset;
770 max_enq = svm_fifo_max_enqueue (as->rx_fifo);
771 n_enq = clib_min (max_enq, dlen);
772 rv = svm_fifo_enqueue (as->rx_fifo, n_enq, &hc->rx_buf[hc->rx_buf_offset]);
775 clib_warning ("enqueue failed");
779 hc->rx_buf_offset += rv;
781 if (hc->rx_buf_offset >= vec_len (hc->rx_buf))
783 vec_reset_length (hc->rx_buf);
784 hc->rx_buf_offset = 0;
787 app_wrk = app_worker_get_if_valid (as->app_wrk_index);
790 app_worker_lock_and_send_event (app_wrk, as, SESSION_IO_EVT_RX);
794 static http_sm_result_t
795 state_cln_recv_more_data (http_conn_t *hc, transport_send_params_t *sp)
802 as = session_get_from_handle (hc->h_pa_session_handle);
803 ts = session_get_from_handle (hc->h_tc_session_handle);
805 u32 dlen = vec_len (hc->rx_buf) - hc->rx_buf_offset;
808 rv = cln_drain_rx_buf (hc, ts, as);
811 clib_warning ("drain rx error!");
812 return HTTP_SM_ERROR;
814 goto maybe_reschedule;
817 if (hc->to_recv == 0)
819 ASSERT (vec_len (hc->rx_buf) == 0);
820 ASSERT (hc->rx_buf_offset == 0);
821 hc->http_state = HTTP_STATE_WAIT_APP;
825 max_deq = svm_fifo_max_dequeue (ts->rx_fifo);
829 ASSERT (vec_len (hc->rx_buf) == 0);
830 ASSERT (hc->rx_buf_offset == 0);
832 vec_validate (hc->rx_buf, max_deq - 1);
833 n_read = svm_fifo_dequeue (ts->rx_fifo, max_deq, hc->rx_buf);
834 ASSERT (n_read == max_deq);
836 if (svm_fifo_is_empty (ts->rx_fifo))
837 svm_fifo_unset_event (ts->rx_fifo);
839 hc->to_recv -= n_read;
840 vec_set_len (hc->rx_buf, max_deq);
843 if (hc->rx_buf_offset < vec_len (hc->rx_buf) ||
844 svm_fifo_max_dequeue_cons (ts->rx_fifo))
846 if (svm_fifo_set_event (ts->rx_fifo))
847 session_send_io_evt_to_thread (ts->rx_fifo, SESSION_IO_EVT_BUILTIN_RX);
849 return HTTP_SM_CONTINUE;
852 static http_sm_result_t
853 state_cln_wait_app (http_conn_t *hc, transport_send_params_t *sp)
857 http_status_code_t ec;
858 u8 *buf = 0, *request;
862 as = session_get_from_handle (hc->h_pa_session_handle);
863 rv = svm_fifo_dequeue (as->tx_fifo, sizeof (msg), (u8 *) &msg);
864 ASSERT (rv == sizeof (msg));
865 if (msg.type != HTTP_MSG_REQUEST || msg.data.type > HTTP_MSG_DATA_PTR)
867 clib_warning ("unexpected msg type from app %u", msg.type);
868 ec = HTTP_STATUS_INTERNAL_ERROR;
872 vec_validate (buf, msg.data.len - 1);
873 rv = svm_fifo_dequeue (as->tx_fifo, msg.data.len, buf);
874 ASSERT (rv == msg.data.len);
876 request = format (0, http_request_template, buf);
877 offset = send_data (hc, request, vec_len (request), 0);
878 if (offset != vec_len (request))
880 clib_warning ("sending request failed!");
881 ec = HTTP_STATUS_INTERNAL_ERROR;
885 hc->http_state = HTTP_STATE_WAIT_METHOD;
890 return HTTP_SM_CONTINUE;
894 session_transport_closing_notify (&hc->connection);
895 http_disconnect_transport (hc);
899 typedef http_sm_result_t (*http_sm_handler) (http_conn_t *,
900 transport_send_params_t *sp);
902 static http_sm_handler srv_state_funcs[HTTP_N_STATES] = {
903 /* Waiting for GET, POST, etc. */
904 state_srv_wait_method,
905 /* Wait for data from app */
908 state_srv_send_more_data,
911 static http_sm_handler cln_state_funcs[HTTP_N_STATES] = {
913 state_cln_wait_method,
914 /* wait for data from app */
916 /* receive more data */
917 state_cln_recv_more_data,
921 http_req_run_state_machine (http_conn_t *hc, transport_send_params_t *sp)
923 http_sm_result_t res;
924 http_sm_handler *state_fn =
925 hc->is_client ? cln_state_funcs : srv_state_funcs;
928 res = state_fn[hc->http_state](hc, sp);
929 if (res == HTTP_SM_ERROR)
932 while (res == HTTP_SM_CONTINUE);
934 /* Reset the session expiration timer */
935 http_conn_timer_update (hc);
939 http_ts_server_rx_callback (session_t *ts, http_conn_t *hc)
941 if (hc->http_state != HTTP_STATE_WAIT_METHOD)
943 clib_warning ("tcp data in req state %u", hc->http_state);
947 http_req_run_state_machine (hc, 0);
949 if (hc->state == HTTP_CONN_STATE_TRANSPORT_CLOSED)
951 if (!svm_fifo_max_dequeue_cons (ts->rx_fifo))
952 session_transport_closing_notify (&hc->connection);
958 http_ts_client_rx_callback (session_t *ts, http_conn_t *hc)
960 if (hc->http_state != HTTP_STATE_WAIT_METHOD &&
961 hc->http_state != HTTP_STATE_IO_MORE_DATA)
963 clib_warning ("http in unexpected state %d (ts %d)", hc->http_state,
968 http_req_run_state_machine (hc, 0);
970 if (hc->state == HTTP_CONN_STATE_TRANSPORT_CLOSED)
972 if (!svm_fifo_max_dequeue_cons (ts->rx_fifo))
973 session_transport_closing_notify (&hc->connection);
979 http_ts_rx_callback (session_t *ts)
983 hc = http_conn_get_w_thread (ts->opaque, ts->thread_index);
985 return http_ts_client_rx_callback (ts, hc);
986 return http_ts_server_rx_callback (ts, hc);
990 http_ts_builtin_tx_callback (session_t *ts)
994 hc = http_conn_get_w_thread (ts->opaque, ts->thread_index);
995 transport_connection_reschedule (&hc->connection);
1001 http_ts_cleanup_callback (session_t *ts, session_cleanup_ntf_t ntf)
1005 if (ntf == SESSION_CLEANUP_TRANSPORT)
1008 hc = http_conn_get_w_thread (ts->opaque, ts->thread_index);
1011 clib_warning ("no http connection for %u", ts->session_index);
1015 vec_free (hc->rx_buf);
1017 http_buffer_free (&hc->tx_buf);
1018 http_conn_timer_stop (hc);
1020 session_transport_delete_notify (&hc->connection);
1021 http_conn_free (hc);
1025 http_add_segment_callback (u32 client_index, u64 segment_handle)
1027 /* No-op for builtin */
1032 http_del_segment_callback (u32 client_index, u64 segment_handle)
1037 static session_cb_vft_t http_app_cb_vft = {
1038 .session_accept_callback = http_ts_accept_callback,
1039 .session_disconnect_callback = http_ts_disconnect_callback,
1040 .session_connected_callback = http_ts_connected_callback,
1041 .session_reset_callback = http_ts_reset_callback,
1042 .session_cleanup_callback = http_ts_cleanup_callback,
1043 .add_segment_callback = http_add_segment_callback,
1044 .del_segment_callback = http_del_segment_callback,
1045 .builtin_app_rx_callback = http_ts_rx_callback,
1046 .builtin_app_tx_callback = http_ts_builtin_tx_callback,
1049 static clib_error_t *
1050 http_transport_enable (vlib_main_t *vm, u8 is_en)
1052 vnet_app_detach_args_t _da, *da = &_da;
1053 vnet_app_attach_args_t _a, *a = &_a;
1054 u64 options[APP_OPTIONS_N_OPTIONS];
1055 http_main_t *hm = &http_main;
1059 da->app_index = hm->app_index;
1060 da->api_client_index = APP_INVALID_INDEX;
1061 vnet_application_detach (da);
1065 vec_validate (hm->wrk, vlib_num_workers ());
1067 clib_memset (a, 0, sizeof (*a));
1068 clib_memset (options, 0, sizeof (options));
1070 a->session_cb_vft = &http_app_cb_vft;
1071 a->api_client_index = APP_INVALID_INDEX;
1072 a->options = options;
1073 a->name = format (0, "http");
1074 a->options[APP_OPTIONS_SEGMENT_SIZE] = hm->first_seg_size;
1075 a->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = hm->add_seg_size;
1076 a->options[APP_OPTIONS_RX_FIFO_SIZE] = hm->fifo_size;
1077 a->options[APP_OPTIONS_TX_FIFO_SIZE] = hm->fifo_size;
1078 a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
1079 a->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
1080 a->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_IS_TRANSPORT_APP;
1082 if (vnet_application_attach (a))
1083 return clib_error_return (0, "failed to attach http app");
1085 hm->app_index = a->app_index;
1088 clib_timebase_init (&hm->timebase, 0 /* GMT */, CLIB_TIMEBASE_DAYLIGHT_NONE,
1089 &vm->clib_time /* share the system clock */);
1091 http_timers_init (vm, http_conn_timeout_cb);
1097 http_transport_connect (transport_endpoint_cfg_t *tep)
1099 vnet_connect_args_t _cargs, *cargs = &_cargs;
1100 http_main_t *hm = &http_main;
1101 session_endpoint_cfg_t *sep = (session_endpoint_cfg_t *) tep;
1106 app_worker_t *app_wrk = app_worker_get (sep->app_wrk_index);
1108 clib_memset (cargs, 0, sizeof (*cargs));
1109 clib_memcpy (&cargs->sep_ext, sep, sizeof (session_endpoint_cfg_t));
1110 cargs->sep.transport_proto = TRANSPORT_PROTO_TCP;
1111 cargs->app_index = hm->app_index;
1112 app = application_get (app_wrk->app_index);
1113 cargs->sep_ext.ns_index = app->ns_index;
1115 hc_index = http_conn_alloc_w_thread (0 /* ts->thread_index */);
1116 hc = http_conn_get_w_thread (hc_index, 0);
1117 hc->h_pa_wrk_index = sep->app_wrk_index;
1118 hc->h_pa_app_api_ctx = sep->opaque;
1120 hc->state = HTTP_CONN_STATE_CONNECTING;
1121 cargs->api_context = hc_index;
1123 if ((error = vnet_connect (cargs)))
1130 http_start_listen (u32 app_listener_index, transport_endpoint_cfg_t *tep)
1132 vnet_listen_args_t _args = {}, *args = &_args;
1133 session_t *ts_listener, *app_listener;
1134 http_main_t *hm = &http_main;
1135 session_endpoint_cfg_t *sep;
1136 app_worker_t *app_wrk;
1137 transport_proto_t tp;
1143 sep = (session_endpoint_cfg_t *) tep;
1145 app_wrk = app_worker_get (sep->app_wrk_index);
1146 app = application_get (app_wrk->app_index);
1148 args->app_index = hm->app_index;
1149 args->sep_ext = *sep;
1150 args->sep_ext.ns_index = app->ns_index;
1151 tp = sep->ext_cfg ? TRANSPORT_PROTO_TLS : TRANSPORT_PROTO_TCP;
1152 args->sep_ext.transport_proto = tp;
1154 if (vnet_listen (args))
1155 return SESSION_INVALID_INDEX;
1157 lhc_index = http_listener_alloc ();
1158 lhc = http_listener_get (lhc_index);
1160 /* Grab transport connection listener and link to http listener */
1161 lhc->h_tc_session_handle = args->handle;
1162 al = app_listener_get_w_handle (lhc->h_tc_session_handle);
1163 ts_listener = app_listener_get_session (al);
1164 ts_listener->opaque = lhc_index;
1166 /* Grab application listener and link to http listener */
1167 app_listener = listen_session_get (app_listener_index);
1168 lhc->h_pa_wrk_index = sep->app_wrk_index;
1169 lhc->h_pa_session_handle = listen_session_get_handle (app_listener);
1170 lhc->c_s_index = app_listener_index;
1171 lhc->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
1177 http_stop_listen (u32 listener_index)
1182 lhc = http_listener_get (listener_index);
1184 vnet_unlisten_args_t a = {
1185 .handle = lhc->h_tc_session_handle,
1186 .app_index = http_main.app_index,
1187 .wrk_map_index = 0 /* default wrk */
1190 if ((rv = vnet_unlisten (&a)))
1191 clib_warning ("unlisten returned %d", rv);
1193 http_listener_free (lhc);
1199 http_transport_close (u32 hc_index, u32 thread_index)
1204 HTTP_DBG (1, "App disconnecting %x", hc_index);
1206 hc = http_conn_get_w_thread (hc_index, thread_index);
1207 if (hc->state == HTTP_CONN_STATE_CONNECTING)
1209 hc->state = HTTP_CONN_STATE_APP_CLOSED;
1210 http_disconnect_transport (hc);
1214 as = session_get_from_handle (hc->h_pa_session_handle);
1216 /* Nothing more to send, confirm close */
1217 if (!svm_fifo_max_dequeue_cons (as->tx_fifo))
1219 session_transport_closed_notify (&hc->connection);
1220 http_disconnect_transport (hc);
1224 /* Wait for all data to be written to ts */
1225 hc->state = HTTP_CONN_STATE_APP_CLOSED;
1229 static transport_connection_t *
1230 http_transport_get_connection (u32 hc_index, u32 thread_index)
1232 http_conn_t *hc = http_conn_get_w_thread (hc_index, thread_index);
1233 return &hc->connection;
1236 static transport_connection_t *
1237 http_transport_get_listener (u32 listener_index)
1239 http_conn_t *lhc = http_listener_get (listener_index);
1240 return &lhc->connection;
1244 http_app_tx_callback (void *session, transport_send_params_t *sp)
1246 session_t *as = (session_t *) session;
1247 u32 max_burst_sz, sent;
1250 hc = http_conn_get_w_thread (as->connection_index, as->thread_index);
1251 if (hc->http_state < HTTP_STATE_WAIT_APP)
1253 if (hc->state != HTTP_CONN_STATE_CLOSED)
1254 clib_warning ("app data req state %u session state %u", hc->http_state,
1256 svm_fifo_dequeue_drop_all (as->tx_fifo);
1260 max_burst_sz = sp->max_burst_size * TRANSPORT_PACER_MIN_MSS;
1261 sp->max_burst_size = max_burst_sz;
1263 http_req_run_state_machine (hc, sp);
1265 if (hc->state == HTTP_CONN_STATE_APP_CLOSED)
1267 if (!svm_fifo_max_dequeue_cons (as->tx_fifo))
1268 http_disconnect_transport (hc);
1271 sent = max_burst_sz - sp->max_burst_size;
1273 return sent > 0 ? clib_max (sent / TRANSPORT_PACER_MIN_MSS, 1) : 0;
1277 http_transport_get_endpoint (u32 hc_index, u32 thread_index,
1278 transport_endpoint_t *tep, u8 is_lcl)
1280 http_conn_t *hc = http_conn_get_w_thread (hc_index, thread_index);
1283 ts = session_get_from_handle (hc->h_tc_session_handle);
1284 session_get_endpoint (ts, tep, is_lcl);
1288 format_http_connection (u8 *s, va_list *args)
1290 http_conn_t *hc = va_arg (*args, http_conn_t *);
1293 ts = session_get_from_handle (hc->h_tc_session_handle);
1294 s = format (s, "[%d:%d][H] app_wrk %u ts %d:%d", hc->c_thread_index,
1295 hc->c_s_index, hc->h_pa_wrk_index, ts->thread_index,
1302 format_http_listener (u8 *s, va_list *args)
1304 http_conn_t *lhc = va_arg (*args, http_conn_t *);
1308 al = app_listener_get_w_handle (lhc->h_tc_session_handle);
1309 lts = app_listener_get_session (al);
1310 s = format (s, "[%d:%d][H] app_wrk %u ts %d:%d", lhc->c_thread_index,
1311 lhc->c_s_index, lhc->h_pa_wrk_index, lts->thread_index,
1312 lts->session_index);
1318 format_http_conn_state (u8 *s, va_list *args)
1320 http_conn_t *hc = va_arg (*args, http_conn_t *);
1324 case HTTP_CONN_STATE_LISTEN:
1325 s = format (s, "LISTEN");
1327 case HTTP_CONN_STATE_CONNECTING:
1328 s = format (s, "CONNECTING");
1330 case HTTP_CONN_STATE_ESTABLISHED:
1331 s = format (s, "ESTABLISHED");
1333 case HTTP_CONN_STATE_TRANSPORT_CLOSED:
1334 s = format (s, "TRANSPORT_CLOSED");
1336 case HTTP_CONN_STATE_APP_CLOSED:
1337 s = format (s, "APP_CLOSED");
1339 case HTTP_CONN_STATE_CLOSED:
1340 s = format (s, "CLOSED");
1348 format_http_transport_connection (u8 *s, va_list *args)
1350 u32 tc_index = va_arg (*args, u32);
1351 u32 thread_index = va_arg (*args, u32);
1352 u32 verbose = va_arg (*args, u32);
1355 hc = http_conn_get_w_thread (tc_index, thread_index);
1357 s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_http_connection, hc);
1361 format (s, "%-" SESSION_CLI_STATE_LEN "U", format_http_conn_state, hc);
1363 s = format (s, "\n");
1370 format_http_transport_listener (u8 *s, va_list *args)
1372 u32 tc_index = va_arg (*args, u32);
1373 u32 __clib_unused thread_index = va_arg (*args, u32);
1374 u32 __clib_unused verbose = va_arg (*args, u32);
1375 http_conn_t *lhc = http_listener_get (tc_index);
1377 s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_http_listener, lhc);
1380 format (s, "%-" SESSION_CLI_STATE_LEN "U", format_http_conn_state, lhc);
1384 static const transport_proto_vft_t http_proto = {
1385 .enable = http_transport_enable,
1386 .connect = http_transport_connect,
1387 .start_listen = http_start_listen,
1388 .stop_listen = http_stop_listen,
1389 .close = http_transport_close,
1390 .custom_tx = http_app_tx_callback,
1391 .get_connection = http_transport_get_connection,
1392 .get_listener = http_transport_get_listener,
1393 .get_transport_endpoint = http_transport_get_endpoint,
1394 .format_connection = format_http_transport_connection,
1395 .format_listener = format_http_transport_listener,
1396 .transport_options = {
1399 .tx_type = TRANSPORT_TX_INTERNAL,
1400 .service_type = TRANSPORT_SERVICE_APP,
1404 static clib_error_t *
1405 http_transport_init (vlib_main_t *vm)
1407 http_main_t *hm = &http_main;
1409 transport_register_protocol (TRANSPORT_PROTO_HTTP, &http_proto,
1410 FIB_PROTOCOL_IP4, ~0);
1411 transport_register_protocol (TRANSPORT_PROTO_HTTP, &http_proto,
1412 FIB_PROTOCOL_IP6, ~0);
1414 /* Default values, configurable via startup conf */
1415 hm->add_seg_size = 256 << 20;
1416 hm->first_seg_size = 32 << 20;
1417 hm->fifo_size = 512 << 10;
1422 VLIB_INIT_FUNCTION (http_transport_init);
1424 static clib_error_t *
1425 http_config_fn (vlib_main_t *vm, unformat_input_t *input)
1427 http_main_t *hm = &http_main;
1430 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1432 if (unformat (input, "first-segment-size %U", unformat_memory_size,
1435 hm->first_seg_size = clib_max (mem_sz, 1 << 20);
1436 if (hm->first_seg_size != mem_sz)
1437 clib_warning ("first seg size too small %u", mem_sz);
1439 else if (unformat (input, "add-segment-size %U", unformat_memory_size,
1442 hm->add_seg_size = clib_max (mem_sz, 1 << 20);
1443 if (hm->add_seg_size != mem_sz)
1444 clib_warning ("add seg size too small %u", mem_sz);
1446 else if (unformat (input, "fifo-size %U", unformat_memory_size, &mem_sz))
1448 hm->fifo_size = clib_clamp (mem_sz, 4 << 10, 2 << 30);
1449 if (hm->fifo_size != mem_sz)
1450 clib_warning ("invalid fifo size %lu", mem_sz);
1453 return clib_error_return (0, "unknown input `%U'",
1454 format_unformat_error, input);
1459 VLIB_CONFIG_FUNCTION (http_config_fn, "http");
1461 VLIB_PLUGIN_REGISTER () = {
1462 .version = VPP_BUILD_VER,
1463 .description = "Hypertext Transfer Protocol (HTTP)",
1464 .default_disabled = 0,
1468 * fd.io coding-style-patch-verification: ON
1471 * eval: (c-set-style "gnu")