LCOV - code coverage report
Current view: top level - plugins/http - http.c (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 79 686 11.5 %
Date: 2023-10-26 01:39:38 Functions: 12 53 22.6 %

          Line data    Source code
       1             : /*
       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:
       6             :  *
       7             :  *     http://www.apache.org/licenses/LICENSE-2.0
       8             :  *
       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.
      14             :  */
      15             : 
      16             : #include <http/http.h>
      17             : #include <vnet/session/session.h>
      18             : #include <http/http_timer.h>
      19             : 
      20             : static http_main_t http_main;
      21             : 
      22             : #define HTTP_FIFO_THRESH (16 << 10)
      23             : #define CONTENT_LEN_STR  "Content-Length: "
      24             : 
      25             : /* HTTP state machine result */
      26             : typedef enum http_sm_result_t_
      27             : {
      28             :   HTTP_SM_STOP = 0,
      29             :   HTTP_SM_CONTINUE = 1,
      30             :   HTTP_SM_ERROR = -1,
      31             : } http_sm_result_t;
      32             : 
      33             : const char *http_status_code_str[] = {
      34             : #define _(c, s, str) str,
      35             :   foreach_http_status_code
      36             : #undef _
      37             : };
      38             : 
      39             : const char *http_content_type_str[] = {
      40             : #define _(s, ext, str) str,
      41             :   foreach_http_content_type
      42             : #undef _
      43             : };
      44             : 
      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,
      48             : };
      49             : 
      50             : static inline http_worker_t *
      51           0 : http_worker_get (u32 thread_index)
      52             : {
      53           0 :   return &http_main.wrk[thread_index];
      54             : }
      55             : 
      56             : static inline u32
      57           0 : http_conn_alloc_w_thread (u32 thread_index)
      58             : {
      59           0 :   http_worker_t *wrk = http_worker_get (thread_index);
      60             :   http_conn_t *hc;
      61             : 
      62           0 :   pool_get_aligned_safe (wrk->conn_pool, hc, CLIB_CACHE_LINE_BYTES);
      63           0 :   clib_memset (hc, 0, sizeof (*hc));
      64           0 :   hc->c_thread_index = thread_index;
      65           0 :   hc->h_hc_index = hc - wrk->conn_pool;
      66           0 :   hc->h_pa_session_handle = SESSION_INVALID_HANDLE;
      67           0 :   hc->h_tc_session_handle = SESSION_INVALID_HANDLE;
      68           0 :   return hc->h_hc_index;
      69             : }
      70             : 
      71             : static inline http_conn_t *
      72           0 : http_conn_get_w_thread (u32 hc_index, u32 thread_index)
      73             : {
      74           0 :   http_worker_t *wrk = http_worker_get (thread_index);
      75           0 :   return pool_elt_at_index (wrk->conn_pool, hc_index);
      76             : }
      77             : 
      78             : void
      79           0 : http_conn_free (http_conn_t *hc)
      80             : {
      81           0 :   http_worker_t *wrk = http_worker_get (hc->c_thread_index);
      82           0 :   pool_put (wrk->conn_pool, hc);
      83           0 : }
      84             : 
      85             : static u32
      86           3 : http_listener_alloc (void)
      87             : {
      88           3 :   http_main_t *hm = &http_main;
      89             :   http_conn_t *lhc;
      90             : 
      91           3 :   pool_get_zero (hm->listener_pool, lhc);
      92           3 :   lhc->c_c_index = lhc - hm->listener_pool;
      93           3 :   return lhc->c_c_index;
      94             : }
      95             : 
      96             : http_conn_t *
      97           9 : http_listener_get (u32 lhc_index)
      98             : {
      99           9 :   return pool_elt_at_index (http_main.listener_pool, lhc_index);
     100             : }
     101             : 
     102             : void
     103           0 : http_listener_free (http_conn_t *lhc)
     104             : {
     105           0 :   http_main_t *hm = &http_main;
     106             : 
     107             :   if (CLIB_DEBUG)
     108           0 :     memset (lhc, 0xfc, sizeof (*lhc));
     109           0 :   pool_put (hm->listener_pool, lhc);
     110           0 : }
     111             : 
     112             : void
     113           0 : http_disconnect_transport (http_conn_t *hc)
     114             : {
     115           0 :   vnet_disconnect_args_t a = {
     116           0 :     .handle = hc->h_tc_session_handle,
     117           0 :     .app_index = http_main.app_index,
     118             :   };
     119             : 
     120           0 :   hc->state = HTTP_CONN_STATE_CLOSED;
     121             : 
     122           0 :   if (vnet_disconnect_session (&a))
     123           0 :     clib_warning ("disconnect returned");
     124           0 : }
     125             : 
     126             : static void
     127           0 : http_conn_timeout_cb (void *hc_handlep)
     128             : {
     129             :   http_conn_t *hc;
     130             :   uword hs_handle;
     131             : 
     132           0 :   hs_handle = pointer_to_uword (hc_handlep);
     133           0 :   hc = http_conn_get_w_thread (hs_handle & 0x00FFFFFF, hs_handle >> 24);
     134             : 
     135             :   HTTP_DBG (1, "terminate thread %d index %d hs %llx", hs_handle >> 24,
     136             :             hs_handle & 0x00FFFFFF, hc);
     137           0 :   if (!hc)
     138           0 :     return;
     139             : 
     140           0 :   hc->timer_handle = ~0;
     141           0 :   session_transport_closing_notify (&hc->connection);
     142           0 :   http_disconnect_transport (hc);
     143             : }
     144             : 
     145             : int
     146           0 : http_ts_accept_callback (session_t *ts)
     147             : {
     148             :   session_t *ts_listener, *as, *asl;
     149             :   app_worker_t *app_wrk;
     150             :   http_conn_t *lhc, *hc;
     151             :   u32 hc_index, thresh;
     152             :   int rv;
     153             : 
     154           0 :   ts_listener = listen_session_get_from_handle (ts->listener_handle);
     155           0 :   lhc = http_listener_get (ts_listener->opaque);
     156             : 
     157           0 :   hc_index = http_conn_alloc_w_thread (ts->thread_index);
     158           0 :   hc = http_conn_get_w_thread (hc_index, ts->thread_index);
     159           0 :   clib_memcpy_fast (hc, lhc, sizeof (*lhc));
     160           0 :   hc->c_thread_index = ts->thread_index;
     161           0 :   hc->h_hc_index = hc_index;
     162             : 
     163           0 :   hc->h_tc_session_handle = session_handle (ts);
     164           0 :   hc->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
     165             : 
     166           0 :   hc->state = HTTP_CONN_STATE_ESTABLISHED;
     167           0 :   hc->http_state = HTTP_STATE_WAIT_METHOD;
     168             : 
     169           0 :   ts->session_state = SESSION_STATE_READY;
     170           0 :   ts->opaque = hc_index;
     171             : 
     172             :   /*
     173             :    * Alloc session and initialize
     174             :    */
     175           0 :   as = session_alloc (hc->c_thread_index);
     176           0 :   hc->c_s_index = as->session_index;
     177             : 
     178           0 :   as->app_wrk_index = hc->h_pa_wrk_index;
     179           0 :   as->connection_index = hc->c_c_index;
     180           0 :   as->session_state = SESSION_STATE_ACCEPTING;
     181             : 
     182           0 :   asl = listen_session_get_from_handle (lhc->h_pa_session_handle);
     183           0 :   as->session_type = asl->session_type;
     184           0 :   as->listener_handle = lhc->h_pa_session_handle;
     185             : 
     186             :   /*
     187             :    * Init session fifos and notify app
     188             :    */
     189           0 :   if ((rv = app_worker_init_accepted (as)))
     190             :     {
     191             :       HTTP_DBG (1, "failed to allocate fifos");
     192           0 :       session_free (as);
     193           0 :       return rv;
     194             :     }
     195             : 
     196           0 :   hc->h_pa_session_handle = session_handle (as);
     197           0 :   hc->h_pa_wrk_index = as->app_wrk_index;
     198           0 :   app_wrk = app_worker_get (as->app_wrk_index);
     199             : 
     200             :   HTTP_DBG (1, "Accepted on listener %u new connection [%u]%x",
     201             :             ts_listener->opaque, vlib_get_thread_index (), hc_index);
     202             : 
     203           0 :   if ((rv = app_worker_accept_notify (app_wrk, as)))
     204             :     {
     205             :       HTTP_DBG (0, "app accept returned");
     206           0 :       session_free (as);
     207           0 :       return rv;
     208             :     }
     209             : 
     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.
     213             :    */
     214           0 :   ts = session_get_from_handle (hc->h_tc_session_handle);
     215           0 :   thresh = clib_min (svm_fifo_size (ts->tx_fifo), HTTP_FIFO_THRESH);
     216           0 :   svm_fifo_set_deq_thresh (ts->tx_fifo, thresh);
     217             : 
     218           0 :   http_conn_timer_start (hc);
     219             : 
     220           0 :   return 0;
     221             : }
     222             : 
     223             : static int
     224           0 : http_ts_connected_callback (u32 http_app_index, u32 ho_hc_index, session_t *ts,
     225             :                             session_error_t err)
     226             : {
     227             :   u32 new_hc_index;
     228             :   session_t *as;
     229             :   http_conn_t *hc, *ho_hc;
     230             :   app_worker_t *app_wrk;
     231             :   int rv;
     232             : 
     233           0 :   if (err)
     234             :     {
     235           0 :       clib_warning ("ERROR: %d", err);
     236           0 :       return 0;
     237             :     }
     238             : 
     239           0 :   new_hc_index = http_conn_alloc_w_thread (ts->thread_index);
     240           0 :   hc = http_conn_get_w_thread (new_hc_index, ts->thread_index);
     241           0 :   ho_hc = http_conn_get_w_thread (ho_hc_index, 0);
     242             : 
     243           0 :   ASSERT (ho_hc->state == HTTP_CONN_STATE_CONNECTING);
     244             : 
     245           0 :   clib_memcpy_fast (hc, ho_hc, sizeof (*hc));
     246             : 
     247           0 :   hc->c_thread_index = ts->thread_index;
     248           0 :   hc->h_tc_session_handle = session_handle (ts);
     249           0 :   hc->c_c_index = new_hc_index;
     250           0 :   hc->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
     251           0 :   hc->state = HTTP_CONN_STATE_ESTABLISHED;
     252           0 :   hc->http_state = HTTP_STATE_WAIT_APP;
     253             : 
     254           0 :   ts->session_state = SESSION_STATE_READY;
     255           0 :   ts->opaque = new_hc_index;
     256             : 
     257             :   /* allocate app session and initialize */
     258             : 
     259           0 :   as = session_alloc (hc->c_thread_index);
     260           0 :   hc->c_s_index = as->session_index;
     261           0 :   as->connection_index = hc->c_c_index;
     262           0 :   as->app_wrk_index = hc->h_pa_wrk_index;
     263           0 :   as->session_state = SESSION_STATE_READY;
     264           0 :   as->opaque = hc->h_pa_app_api_ctx;
     265           0 :   as->session_type = session_type_from_proto_and_ip (
     266           0 :     TRANSPORT_PROTO_HTTP, session_type_is_ip4 (ts->session_type));
     267             : 
     268           0 :   app_wrk = app_worker_get (hc->h_pa_wrk_index);
     269           0 :   if (!app_wrk)
     270             :     {
     271           0 :       clib_warning ("no app worker");
     272           0 :       return -1;
     273             :     }
     274             : 
     275           0 :   if ((rv = app_worker_init_connected (app_wrk, as)))
     276             :     {
     277             :       HTTP_DBG (1, "failed to allocate fifos");
     278           0 :       session_free (as);
     279           0 :       return rv;
     280             :     }
     281           0 :   app_worker_connect_notify (app_wrk, as, err, hc->h_pa_app_api_ctx);
     282           0 :   hc->h_pa_session_handle = session_handle (as);
     283           0 :   http_conn_timer_start (hc);
     284             : 
     285           0 :   return 0;
     286             : }
     287             : 
     288             : static void
     289           0 : http_ts_disconnect_callback (session_t *ts)
     290             : {
     291             :   http_conn_t *hc;
     292             : 
     293           0 :   hc = http_conn_get_w_thread (ts->opaque, ts->thread_index);
     294             : 
     295           0 :   if (hc->state < HTTP_CONN_STATE_TRANSPORT_CLOSED)
     296           0 :     hc->state = HTTP_CONN_STATE_TRANSPORT_CLOSED;
     297             : 
     298             :   /* Nothing more to rx, propagate to app */
     299           0 :   if (!svm_fifo_max_dequeue_cons (ts->rx_fifo))
     300           0 :     session_transport_closing_notify (&hc->connection);
     301           0 : }
     302             : 
     303             : static void
     304           0 : http_ts_reset_callback (session_t *ts)
     305             : {
     306             :   http_conn_t *hc;
     307             : 
     308           0 :   hc = http_conn_get_w_thread (ts->opaque, ts->thread_index);
     309             : 
     310           0 :   hc->state = HTTP_CONN_STATE_CLOSED;
     311           0 :   http_buffer_free (&hc->tx_buf);
     312           0 :   hc->http_state = HTTP_STATE_WAIT_METHOD;
     313           0 :   session_transport_reset_notify (&hc->connection);
     314             : 
     315           0 :   http_disconnect_transport (hc);
     316           0 : }
     317             : 
     318             : /**
     319             :  * http error boilerplate
     320             :  */
     321             : static const char *http_error_template = "HTTP/1.1 %s\r\n"
     322             :                                          "Date: %U GMT\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";
     327             : 
     328             : static const char *http_redirect_template = "HTTP/1.1 %s\r\n";
     329             : 
     330             : /**
     331             :  * http response boilerplate
     332             :  */
     333             : static const char *http_response_template = "HTTP/1.1 %s\r\n"
     334             :                                             "Date: %U GMT\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";
     339             : 
     340             : static const char *http_request_template = "GET %s HTTP/1.1\r\n"
     341             :                                            "User-Agent: VPP HTTP client\r\n"
     342             :                                            "Accept: */*\r\n";
     343             : 
     344             : static u32
     345           0 : send_data (http_conn_t *hc, u8 *data, u32 length, u32 offset)
     346             : {
     347           0 :   const u32 max_burst = 64 << 10;
     348             :   session_t *ts;
     349             :   u32 to_send;
     350             :   int sent;
     351             : 
     352           0 :   ts = session_get_from_handle (hc->h_tc_session_handle);
     353             : 
     354           0 :   to_send = clib_min (length - offset, max_burst);
     355           0 :   sent = svm_fifo_enqueue (ts->tx_fifo, to_send, data + offset);
     356             : 
     357           0 :   if (sent <= 0)
     358           0 :     return offset;
     359             : 
     360           0 :   if (svm_fifo_set_event (ts->tx_fifo))
     361           0 :     session_send_io_evt_to_thread (ts->tx_fifo, SESSION_IO_EVT_TX);
     362             : 
     363           0 :   return (offset + sent);
     364             : }
     365             : 
     366             : static void
     367           0 : send_error (http_conn_t *hc, http_status_code_t ec)
     368             : {
     369           0 :   http_main_t *hm = &http_main;
     370             :   u8 *data;
     371             :   f64 now;
     372             : 
     373           0 :   if (ec >= HTTP_N_STATUS)
     374           0 :     ec = HTTP_STATUS_INTERNAL_ERROR;
     375             : 
     376           0 :   now = clib_timebase_now (&hm->timebase);
     377           0 :   data = format (0, http_error_template, http_status_code_str[ec],
     378             :                  format_clib_timebase_time, now);
     379           0 :   send_data (hc, data, vec_len (data), 0);
     380           0 :   vec_free (data);
     381           0 : }
     382             : 
     383             : static int
     384           0 : read_http_message (http_conn_t *hc)
     385             : {
     386             :   u32 max_deq, cursize;
     387             :   session_t *ts;
     388             :   int n_read;
     389             : 
     390           0 :   ts = session_get_from_handle (hc->h_tc_session_handle);
     391             : 
     392           0 :   cursize = vec_len (hc->rx_buf);
     393           0 :   max_deq = svm_fifo_max_dequeue (ts->rx_fifo);
     394           0 :   if (PREDICT_FALSE (max_deq == 0))
     395           0 :     return -1;
     396             : 
     397           0 :   vec_validate (hc->rx_buf, cursize + max_deq - 1);
     398           0 :   n_read = svm_fifo_dequeue (ts->rx_fifo, max_deq, hc->rx_buf + cursize);
     399           0 :   ASSERT (n_read == max_deq);
     400             : 
     401           0 :   if (svm_fifo_is_empty (ts->rx_fifo))
     402           0 :     svm_fifo_unset_event (ts->rx_fifo);
     403             : 
     404           0 :   vec_set_len (hc->rx_buf, cursize + n_read);
     405           0 :   return 0;
     406             : }
     407             : 
     408             : static int
     409           0 : v_find_index (u8 *vec, u32 offset, char *str)
     410             : {
     411           0 :   int start_index = offset;
     412           0 :   u32 slen = (u32) strnlen_s_inline (str, 16);
     413           0 :   u32 vlen = vec_len (vec);
     414             : 
     415           0 :   ASSERT (slen > 0);
     416             : 
     417           0 :   if (vlen <= slen)
     418           0 :     return -1;
     419             : 
     420           0 :   for (; start_index < (vlen - slen); start_index++)
     421             :     {
     422           0 :       if (!memcmp (vec + start_index, str, slen))
     423           0 :         return start_index;
     424             :     }
     425             : 
     426           0 :   return -1;
     427             : }
     428             : 
     429             : /**
     430             :  * waiting for request method from peer - parse request method and data
     431             :  */
     432             : static http_sm_result_t
     433           0 : state_srv_wait_method (http_conn_t *hc, transport_send_params_t *sp)
     434             : {
     435             :   http_status_code_t ec;
     436             :   app_worker_t *app_wrk;
     437             :   http_msg_t msg;
     438             :   session_t *as;
     439             :   int i, rv;
     440             :   u32 len;
     441             :   u8 *buf;
     442             : 
     443           0 :   rv = read_http_message (hc);
     444             : 
     445             :   /* Nothing yet, wait for data or timer expire */
     446           0 :   if (rv)
     447           0 :     return HTTP_SM_STOP;
     448             : 
     449           0 :   if (vec_len (hc->rx_buf) < 8)
     450             :     {
     451           0 :       ec = HTTP_STATUS_BAD_REQUEST;
     452           0 :       goto error;
     453             :     }
     454             : 
     455           0 :   if ((i = v_find_index (hc->rx_buf, 0, "GET ")) >= 0)
     456             :     {
     457           0 :       hc->method = HTTP_REQ_GET;
     458           0 :       hc->rx_buf_offset = i + 5;
     459             : 
     460           0 :       i = v_find_index (hc->rx_buf, hc->rx_buf_offset, "HTTP");
     461           0 :       if (i < 0)
     462             :         {
     463           0 :           ec = HTTP_STATUS_BAD_REQUEST;
     464           0 :           goto error;
     465             :         }
     466             : 
     467           0 :       len = i - hc->rx_buf_offset - 1;
     468             :     }
     469           0 :   else if ((i = v_find_index (hc->rx_buf, 0, "POST ")) >= 0)
     470             :     {
     471           0 :       hc->method = HTTP_REQ_POST;
     472           0 :       hc->rx_buf_offset = i + 6;
     473           0 :       len = vec_len (hc->rx_buf) - hc->rx_buf_offset - 1;
     474             :     }
     475             :   else
     476             :     {
     477             :       HTTP_DBG (0, "Unknown http method");
     478           0 :       ec = HTTP_STATUS_METHOD_NOT_ALLOWED;
     479           0 :       goto error;
     480             :     }
     481             : 
     482           0 :   buf = &hc->rx_buf[hc->rx_buf_offset];
     483             : 
     484           0 :   msg.type = HTTP_MSG_REQUEST;
     485           0 :   msg.method_type = hc->method;
     486           0 :   msg.content_type = HTTP_CONTENT_TEXT_HTML;
     487           0 :   msg.data.type = HTTP_MSG_DATA_INLINE;
     488           0 :   msg.data.len = len;
     489             : 
     490           0 :   svm_fifo_seg_t segs[2] = { { (u8 *) &msg, sizeof (msg) }, { buf, len } };
     491             : 
     492           0 :   as = session_get_from_handle (hc->h_pa_session_handle);
     493           0 :   rv = svm_fifo_enqueue_segments (as->rx_fifo, segs, 2, 0 /* allow partial */);
     494           0 :   if (rv < 0 || rv != sizeof (msg) + len)
     495             :     {
     496           0 :       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
     499             :        * rescheduling */
     500           0 :       return HTTP_SM_ERROR;
     501             :     }
     502             : 
     503           0 :   vec_free (hc->rx_buf);
     504           0 :   hc->http_state = HTTP_STATE_WAIT_APP;
     505             : 
     506           0 :   app_wrk = app_worker_get_if_valid (as->app_wrk_index);
     507           0 :   if (app_wrk)
     508           0 :     app_worker_rx_notify (app_wrk, as);
     509             : 
     510           0 :   return HTTP_SM_STOP;
     511             : 
     512           0 : error:
     513             : 
     514           0 :   send_error (hc, ec);
     515           0 :   session_transport_closing_notify (&hc->connection);
     516           0 :   http_disconnect_transport (hc);
     517             : 
     518           0 :   return HTTP_SM_ERROR;
     519             : }
     520             : 
     521             : /**
     522             :  * waiting for data from app
     523             :  */
     524             : static http_sm_result_t
     525           0 : state_srv_wait_app (http_conn_t *hc, transport_send_params_t *sp)
     526             : {
     527           0 :   http_main_t *hm = &http_main;
     528             :   http_status_code_t ec;
     529             :   http_msg_t msg;
     530             :   session_t *as;
     531             :   u8 *header;
     532             :   u32 offset;
     533             :   f64 now;
     534             :   int rv;
     535             : 
     536           0 :   as = session_get_from_handle (hc->h_pa_session_handle);
     537             : 
     538           0 :   rv = svm_fifo_dequeue (as->tx_fifo, sizeof (msg), (u8 *) &msg);
     539           0 :   ASSERT (rv == sizeof (msg));
     540             : 
     541           0 :   if (msg.type != HTTP_MSG_REPLY || msg.data.type > HTTP_MSG_DATA_PTR)
     542             :     {
     543           0 :       clib_warning ("unexpected msg type from app %u", msg.type);
     544           0 :       ec = HTTP_STATUS_INTERNAL_ERROR;
     545           0 :       goto error;
     546             :     }
     547             : 
     548           0 :   ec = msg.code;
     549             : 
     550           0 :   switch (msg.code)
     551             :     {
     552           0 :     case HTTP_STATUS_OK:
     553             :     case HTTP_STATUS_MOVED:
     554           0 :       break;
     555           0 :     default:
     556           0 :       goto error;
     557             :     }
     558             : 
     559           0 :   http_buffer_init (&hc->tx_buf, msg_to_buf_type[msg.data.type], as->tx_fifo,
     560             :                     msg.data.len);
     561             : 
     562             :   /*
     563             :    * Add headers. For now:
     564             :    * - current time
     565             :    * - expiration time
     566             :    * - content type
     567             :    * - data length
     568             :    */
     569           0 :   now = clib_timebase_now (&hm->timebase);
     570             : 
     571           0 :   switch (msg.code)
     572             :     {
     573           0 :     case HTTP_STATUS_OK:
     574           0 :       header =
     575           0 :         format (0, http_response_template, http_status_code_str[msg.code],
     576             :                 /* Date */
     577             :                 format_clib_timebase_time, now,
     578             :                 /* Expires */
     579             :                 format_clib_timebase_time, now + 600.0,
     580             :                 /* Content type */
     581           0 :                 http_content_type_str[msg.content_type],
     582             :                 /* Length */
     583             :                 msg.data.len);
     584           0 :       break;
     585           0 :     case HTTP_STATUS_MOVED:
     586           0 :       header =
     587           0 :         format (0, http_redirect_template, http_status_code_str[msg.code]);
     588             :       /* Location: http(s)://new-place already queued up as data */
     589           0 :       break;
     590           0 :     default:
     591           0 :       goto error;
     592             :     }
     593             : 
     594           0 :   offset = send_data (hc, header, vec_len (header), 0);
     595           0 :   if (offset != vec_len (header))
     596             :     {
     597           0 :       clib_warning ("couldn't send response header!");
     598           0 :       ec = HTTP_STATUS_INTERNAL_ERROR;
     599           0 :       goto error;
     600             :     }
     601           0 :   vec_free (header);
     602             : 
     603             :   /* Start sending the actual data */
     604           0 :   hc->http_state = HTTP_STATE_IO_MORE_DATA;
     605             : 
     606           0 :   ASSERT (sp->max_burst_size >= offset);
     607           0 :   sp->max_burst_size -= offset;
     608             : 
     609           0 :   return HTTP_SM_CONTINUE;
     610             : 
     611           0 : error:
     612             : 
     613           0 :   send_error (hc, ec);
     614           0 :   hc->http_state = HTTP_STATE_WAIT_METHOD;
     615           0 :   session_transport_closing_notify (&hc->connection);
     616           0 :   http_disconnect_transport (hc);
     617             : 
     618           0 :   return HTTP_SM_STOP;
     619             : }
     620             : 
     621             : static http_sm_result_t
     622           0 : state_srv_send_more_data (http_conn_t *hc, transport_send_params_t *sp)
     623             : {
     624           0 :   u32 max_send = 64 << 10, n_segs;
     625           0 :   http_buffer_t *hb = &hc->tx_buf;
     626             :   svm_fifo_seg_t *seg;
     627             :   session_t *ts;
     628           0 :   int sent = 0;
     629             : 
     630           0 :   max_send = clib_min (max_send, sp->max_burst_size);
     631           0 :   ts = session_get_from_handle (hc->h_tc_session_handle);
     632           0 :   if ((seg = http_buffer_get_segs (hb, max_send, &n_segs)))
     633           0 :     sent = svm_fifo_enqueue_segments (ts->tx_fifo, seg, n_segs,
     634             :                                       1 /* allow partial */);
     635             : 
     636           0 :   if (sent > 0)
     637             :     {
     638             :       /* Ask scheduler to notify app of deq event if needed */
     639           0 :       sp->bytes_dequeued += http_buffer_drain (hb, sent);
     640           0 :       sp->max_burst_size -= sent;
     641             :     }
     642             : 
     643             :   /* Not finished sending all data */
     644           0 :   if (!http_buffer_is_drained (hb))
     645             :     {
     646           0 :       if (sent && svm_fifo_set_event (ts->tx_fifo))
     647           0 :         session_send_io_evt_to_thread (ts->tx_fifo, SESSION_IO_EVT_TX);
     648             : 
     649           0 :       if (svm_fifo_max_enqueue (ts->tx_fifo) < HTTP_FIFO_THRESH)
     650             :         {
     651             :           /* Deschedule http session and wait for deq notification if
     652             :            * underlying ts tx fifo almost full */
     653           0 :           svm_fifo_add_want_deq_ntf (ts->tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
     654           0 :           transport_connection_deschedule (&hc->connection);
     655           0 :           sp->flags |= TRANSPORT_SND_F_DESCHED;
     656             :         }
     657             :     }
     658             :   else
     659             :     {
     660           0 :       if (sent && svm_fifo_set_event (ts->tx_fifo))
     661           0 :         session_send_io_evt_to_thread (ts->tx_fifo, SESSION_IO_EVT_TX_FLUSH);
     662             : 
     663             :       /* Finished transaction, back to HTTP_STATE_WAIT_METHOD */
     664           0 :       hc->http_state = HTTP_STATE_WAIT_METHOD;
     665           0 :       http_buffer_free (&hc->tx_buf);
     666             :     }
     667             : 
     668           0 :   return HTTP_SM_STOP;
     669             : }
     670             : 
     671             : static int
     672           0 : parse_http_header (http_conn_t *hc, int *content_length)
     673             : {
     674             :   unformat_input_t input;
     675             :   int i, len;
     676             :   u8 *line;
     677             : 
     678           0 :   if ((i = v_find_index (hc->rx_buf, hc->rx_buf_offset, "200 OK") < 0))
     679             :     {
     680           0 :       clib_warning ("bad response code");
     681           0 :       return -1;
     682             :     }
     683             : 
     684           0 :   i = v_find_index (hc->rx_buf, hc->rx_buf_offset, CONTENT_LEN_STR);
     685           0 :   if (i < 0)
     686             :     {
     687           0 :       clib_warning ("cannot find '%s' in the header!", CONTENT_LEN_STR);
     688           0 :       return -1;
     689             :     }
     690             : 
     691           0 :   hc->rx_buf_offset = i;
     692             : 
     693           0 :   i = v_find_index (hc->rx_buf, hc->rx_buf_offset, "\n");
     694           0 :   if (i < 0)
     695             :     {
     696           0 :       clib_warning ("end of line missing; incomplete data");
     697           0 :       return -1;
     698             :     }
     699             : 
     700           0 :   len = i - hc->rx_buf_offset;
     701           0 :   line = vec_new (u8, len);
     702           0 :   clib_memcpy (line, hc->rx_buf + hc->rx_buf_offset, len);
     703             : 
     704           0 :   unformat_init_vector (&input, line);
     705           0 :   if (!unformat (&input, CONTENT_LEN_STR "%d", content_length))
     706             :     {
     707           0 :       clib_warning ("failed to unformat content length!");
     708           0 :       return -1;
     709             :     }
     710           0 :   unformat_free (&input);
     711             : 
     712             :   /* skip rest of the header */
     713           0 :   hc->rx_buf_offset += len;
     714           0 :   i = v_find_index (hc->rx_buf, hc->rx_buf_offset, "<html>");
     715           0 :   if (i < 0)
     716             :     {
     717           0 :       clib_warning ("<html> tag not found");
     718           0 :       return -1;
     719             :     }
     720           0 :   hc->rx_buf_offset = i;
     721             : 
     722           0 :   return 0;
     723             : }
     724             : 
     725             : static int
     726           0 : state_cln_wait_method (http_conn_t *hc, transport_send_params_t *sp)
     727             : {
     728             :   session_t *as;
     729             :   http_msg_t msg;
     730             :   app_worker_t *app_wrk;
     731             :   int rv, content_length;
     732             : 
     733           0 :   rv = read_http_message (hc);
     734           0 :   if (rv)
     735           0 :     return HTTP_SM_STOP;
     736             : 
     737           0 :   msg.type = HTTP_MSG_REPLY;
     738           0 :   msg.content_type = HTTP_CONTENT_TEXT_HTML;
     739           0 :   msg.code = HTTP_STATUS_OK;
     740           0 :   msg.data.type = HTTP_MSG_DATA_INLINE;
     741           0 :   msg.data.len = 0;
     742             : 
     743           0 :   rv = parse_http_header (hc, &content_length);
     744           0 :   if (rv)
     745             :     {
     746           0 :       clib_warning ("failed to parse http reply");
     747           0 :       session_transport_closing_notify (&hc->connection);
     748           0 :       http_disconnect_transport (hc);
     749           0 :       return -1;
     750             :     }
     751             : 
     752           0 :   msg.data.len = content_length;
     753           0 :   u32 dlen = vec_len (hc->rx_buf) - hc->rx_buf_offset;
     754           0 :   as = session_get_from_handle (hc->h_pa_session_handle);
     755           0 :   svm_fifo_seg_t segs[2] = { { (u8 *) &msg, sizeof (msg) },
     756           0 :                              { &hc->rx_buf[hc->rx_buf_offset], dlen } };
     757             : 
     758           0 :   rv = svm_fifo_enqueue_segments (as->rx_fifo, segs, 2, 0 /* allow partial */);
     759           0 :   if (rv < 0)
     760             :     {
     761           0 :       clib_warning ("error enqueue");
     762           0 :       return HTTP_SM_ERROR;
     763             :     }
     764           0 :   hc->rx_buf_offset += dlen;
     765           0 :   hc->http_state = HTTP_STATE_IO_MORE_DATA;
     766           0 :   hc->to_recv = content_length - dlen;
     767             : 
     768           0 :   if (hc->rx_buf_offset == vec_len (hc->rx_buf))
     769             :     {
     770           0 :       vec_reset_length (hc->rx_buf);
     771           0 :       hc->rx_buf_offset = 0;
     772             :     }
     773             : 
     774           0 :   if (hc->to_recv == 0)
     775             :     {
     776           0 :       hc->rx_buf_offset = 0;
     777           0 :       vec_reset_length (hc->rx_buf);
     778           0 :       hc->http_state = HTTP_STATE_WAIT_APP;
     779             :     }
     780             : 
     781           0 :   app_wrk = app_worker_get_if_valid (as->app_wrk_index);
     782           0 :   app_worker_rx_notify (app_wrk, as);
     783           0 :   return HTTP_SM_STOP;
     784             : }
     785             : 
     786             : static int
     787           0 : cln_drain_rx_buf (http_conn_t *hc, session_t *ts, session_t *as)
     788             : {
     789             :   app_worker_t *app_wrk;
     790           0 :   u32 max_enq, n_enq, dlen = vec_len (hc->rx_buf) - hc->rx_buf_offset;
     791             :   int rv;
     792             : 
     793           0 :   max_enq = svm_fifo_max_enqueue (as->rx_fifo);
     794           0 :   n_enq = clib_min (max_enq, dlen);
     795           0 :   rv = svm_fifo_enqueue (as->rx_fifo, n_enq, &hc->rx_buf[hc->rx_buf_offset]);
     796           0 :   if (rv < 0)
     797             :     {
     798           0 :       clib_warning ("enqueue failed");
     799           0 :       return -1;
     800             :     }
     801             : 
     802           0 :   hc->rx_buf_offset += rv;
     803             : 
     804           0 :   if (hc->rx_buf_offset >= vec_len (hc->rx_buf))
     805             :     {
     806           0 :       vec_reset_length (hc->rx_buf);
     807           0 :       hc->rx_buf_offset = 0;
     808             :     }
     809             : 
     810           0 :   app_wrk = app_worker_get_if_valid (as->app_wrk_index);
     811           0 :   ASSERT (app_wrk);
     812             : 
     813           0 :   app_worker_rx_notify (app_wrk, as);
     814           0 :   return 1;
     815             : }
     816             : 
     817             : static http_sm_result_t
     818           0 : state_cln_recv_more_data (http_conn_t *hc, transport_send_params_t *sp)
     819             : {
     820             :   session_t *as;
     821             :   u32 max_deq;
     822             :   session_t *ts;
     823             :   int n_read, rv;
     824             : 
     825           0 :   as = session_get_from_handle (hc->h_pa_session_handle);
     826           0 :   ts = session_get_from_handle (hc->h_tc_session_handle);
     827             : 
     828           0 :   u32 dlen = vec_len (hc->rx_buf) - hc->rx_buf_offset;
     829           0 :   if (dlen)
     830             :     {
     831           0 :       rv = cln_drain_rx_buf (hc, ts, as);
     832           0 :       if (rv < 0)
     833             :         {
     834           0 :           clib_warning ("drain rx error!");
     835           0 :           return HTTP_SM_ERROR;
     836             :         }
     837           0 :       goto maybe_reschedule;
     838             :     }
     839             : 
     840           0 :   if (hc->to_recv == 0)
     841             :     {
     842           0 :       ASSERT (vec_len (hc->rx_buf) == 0);
     843           0 :       ASSERT (hc->rx_buf_offset == 0);
     844           0 :       hc->http_state = HTTP_STATE_WAIT_APP;
     845           0 :       return HTTP_SM_STOP;
     846             :     }
     847             : 
     848           0 :   max_deq = svm_fifo_max_dequeue (ts->rx_fifo);
     849           0 :   if (max_deq == 0)
     850           0 :     return HTTP_SM_STOP;
     851             : 
     852           0 :   ASSERT (vec_len (hc->rx_buf) == 0);
     853           0 :   ASSERT (hc->rx_buf_offset == 0);
     854             : 
     855           0 :   vec_validate (hc->rx_buf, max_deq - 1);
     856           0 :   n_read = svm_fifo_dequeue (ts->rx_fifo, max_deq, hc->rx_buf);
     857           0 :   ASSERT (n_read == max_deq);
     858             : 
     859           0 :   if (svm_fifo_is_empty (ts->rx_fifo))
     860           0 :     svm_fifo_unset_event (ts->rx_fifo);
     861             : 
     862           0 :   hc->to_recv -= n_read;
     863           0 :   vec_set_len (hc->rx_buf, max_deq);
     864             : 
     865           0 : maybe_reschedule:
     866           0 :   if (hc->rx_buf_offset < vec_len (hc->rx_buf) ||
     867           0 :       svm_fifo_max_dequeue_cons (ts->rx_fifo))
     868             :     {
     869             :       /* TODO is the flag really needed? */
     870           0 :       if (svm_fifo_set_event (ts->rx_fifo))
     871           0 :         session_enqueue_notify (ts);
     872             :     }
     873           0 :   return HTTP_SM_CONTINUE;
     874             : }
     875             : 
     876             : static http_sm_result_t
     877           0 : state_cln_wait_app (http_conn_t *hc, transport_send_params_t *sp)
     878             : {
     879             :   session_t *as;
     880             :   http_msg_t msg;
     881             :   http_status_code_t ec;
     882           0 :   u8 *buf = 0, *request;
     883             :   u32 offset;
     884             :   int rv;
     885             : 
     886           0 :   as = session_get_from_handle (hc->h_pa_session_handle);
     887           0 :   rv = svm_fifo_dequeue (as->tx_fifo, sizeof (msg), (u8 *) &msg);
     888           0 :   ASSERT (rv == sizeof (msg));
     889           0 :   if (msg.type != HTTP_MSG_REQUEST || msg.data.type > HTTP_MSG_DATA_PTR)
     890             :     {
     891           0 :       clib_warning ("unexpected msg type from app %u", msg.type);
     892           0 :       ec = HTTP_STATUS_INTERNAL_ERROR;
     893           0 :       goto error;
     894             :     }
     895             : 
     896           0 :   vec_validate (buf, msg.data.len - 1);
     897           0 :   rv = svm_fifo_dequeue (as->tx_fifo, msg.data.len, buf);
     898           0 :   ASSERT (rv == msg.data.len);
     899             : 
     900           0 :   request = format (0, http_request_template, buf);
     901           0 :   offset = send_data (hc, request, vec_len (request), 0);
     902           0 :   if (offset != vec_len (request))
     903             :     {
     904           0 :       clib_warning ("sending request failed!");
     905           0 :       ec = HTTP_STATUS_INTERNAL_ERROR;
     906           0 :       goto error;
     907             :     }
     908             : 
     909           0 :   hc->http_state = HTTP_STATE_WAIT_METHOD;
     910             : 
     911           0 :   vec_free (buf);
     912           0 :   vec_free (request);
     913             : 
     914           0 :   return HTTP_SM_CONTINUE;
     915             : 
     916           0 : error:
     917           0 :   send_error (hc, ec);
     918           0 :   session_transport_closing_notify (&hc->connection);
     919           0 :   http_disconnect_transport (hc);
     920           0 :   return HTTP_SM_STOP;
     921             : }
     922             : 
     923             : typedef http_sm_result_t (*http_sm_handler) (http_conn_t *,
     924             :                                              transport_send_params_t *sp);
     925             : 
     926             : static http_sm_handler srv_state_funcs[HTTP_N_STATES] = {
     927             :   /* Waiting for GET, POST, etc. */
     928             :   state_srv_wait_method,
     929             :   /* Wait for data from app */
     930             :   state_srv_wait_app,
     931             :   /* Send more data */
     932             :   state_srv_send_more_data,
     933             : };
     934             : 
     935             : static http_sm_handler cln_state_funcs[HTTP_N_STATES] = {
     936             :   /* wait for reply */
     937             :   state_cln_wait_method,
     938             :   /* wait for data from app */
     939             :   state_cln_wait_app,
     940             :   /* receive more data */
     941             :   state_cln_recv_more_data,
     942             : };
     943             : 
     944             : static void
     945           0 : http_req_run_state_machine (http_conn_t *hc, transport_send_params_t *sp)
     946             : {
     947             :   http_sm_result_t res;
     948           0 :   http_sm_handler *state_fn =
     949           0 :     hc->is_client ? cln_state_funcs : srv_state_funcs;
     950             :   do
     951             :     {
     952           0 :       res = state_fn[hc->http_state](hc, sp);
     953           0 :       if (res == HTTP_SM_ERROR)
     954           0 :         return;
     955             :     }
     956           0 :   while (res == HTTP_SM_CONTINUE);
     957             : 
     958             :   /* Reset the session expiration timer */
     959           0 :   http_conn_timer_update (hc);
     960             : }
     961             : 
     962             : static int
     963           0 : http_ts_server_rx_callback (session_t *ts, http_conn_t *hc)
     964             : {
     965           0 :   if (hc->http_state != HTTP_STATE_WAIT_METHOD)
     966             :     {
     967           0 :       clib_warning ("tcp data in req state %u", hc->http_state);
     968           0 :       return 0;
     969             :     }
     970             : 
     971           0 :   http_req_run_state_machine (hc, 0);
     972             : 
     973           0 :   if (hc->state == HTTP_CONN_STATE_TRANSPORT_CLOSED)
     974             :     {
     975           0 :       if (!svm_fifo_max_dequeue_cons (ts->rx_fifo))
     976           0 :         session_transport_closing_notify (&hc->connection);
     977             :     }
     978           0 :   return 0;
     979             : }
     980             : 
     981             : static int
     982           0 : http_ts_client_rx_callback (session_t *ts, http_conn_t *hc)
     983             : {
     984           0 :   if (hc->http_state != HTTP_STATE_WAIT_METHOD &&
     985           0 :       hc->http_state != HTTP_STATE_IO_MORE_DATA)
     986             :     {
     987           0 :       clib_warning ("http in unexpected state %d (ts %d)", hc->http_state,
     988             :                     ts->session_index);
     989           0 :       return 0;
     990             :     }
     991             : 
     992           0 :   http_req_run_state_machine (hc, 0);
     993             : 
     994           0 :   if (hc->state == HTTP_CONN_STATE_TRANSPORT_CLOSED)
     995             :     {
     996           0 :       if (!svm_fifo_max_dequeue_cons (ts->rx_fifo))
     997           0 :         session_transport_closing_notify (&hc->connection);
     998             :     }
     999           0 :   return 0;
    1000             : }
    1001             : 
    1002             : static int
    1003           0 : http_ts_rx_callback (session_t *ts)
    1004             : {
    1005             :   http_conn_t *hc;
    1006             : 
    1007           0 :   hc = http_conn_get_w_thread (ts->opaque, ts->thread_index);
    1008           0 :   if (hc->is_client)
    1009           0 :     return http_ts_client_rx_callback (ts, hc);
    1010           0 :   return http_ts_server_rx_callback (ts, hc);
    1011             : }
    1012             : 
    1013             : int
    1014           0 : http_ts_builtin_tx_callback (session_t *ts)
    1015             : {
    1016             :   http_conn_t *hc;
    1017             : 
    1018           0 :   hc = http_conn_get_w_thread (ts->opaque, ts->thread_index);
    1019           0 :   transport_connection_reschedule (&hc->connection);
    1020             : 
    1021           0 :   return 0;
    1022             : }
    1023             : 
    1024             : static void
    1025           0 : http_ts_cleanup_callback (session_t *ts, session_cleanup_ntf_t ntf)
    1026             : {
    1027             :   http_conn_t *hc;
    1028             : 
    1029           0 :   if (ntf == SESSION_CLEANUP_TRANSPORT)
    1030           0 :     return;
    1031             : 
    1032           0 :   hc = http_conn_get_w_thread (ts->opaque, ts->thread_index);
    1033           0 :   if (!hc)
    1034             :     {
    1035           0 :       clib_warning ("no http connection for %u", ts->session_index);
    1036           0 :       return;
    1037             :     }
    1038             : 
    1039           0 :   vec_free (hc->rx_buf);
    1040             : 
    1041           0 :   http_buffer_free (&hc->tx_buf);
    1042           0 :   http_conn_timer_stop (hc);
    1043             : 
    1044           0 :   session_transport_delete_notify (&hc->connection);
    1045           0 :   http_conn_free (hc);
    1046             : }
    1047             : 
    1048             : int
    1049           3 : http_add_segment_callback (u32 client_index, u64 segment_handle)
    1050             : {
    1051             :   /* No-op for builtin */
    1052           3 :   return 0;
    1053             : }
    1054             : 
    1055             : int
    1056           0 : http_del_segment_callback (u32 client_index, u64 segment_handle)
    1057             : {
    1058           0 :   return 0;
    1059             : }
    1060             : 
    1061             : static session_cb_vft_t http_app_cb_vft = {
    1062             :   .session_accept_callback = http_ts_accept_callback,
    1063             :   .session_disconnect_callback = http_ts_disconnect_callback,
    1064             :   .session_connected_callback = http_ts_connected_callback,
    1065             :   .session_reset_callback = http_ts_reset_callback,
    1066             :   .session_cleanup_callback = http_ts_cleanup_callback,
    1067             :   .add_segment_callback = http_add_segment_callback,
    1068             :   .del_segment_callback = http_del_segment_callback,
    1069             :   .builtin_app_rx_callback = http_ts_rx_callback,
    1070             :   .builtin_app_tx_callback = http_ts_builtin_tx_callback,
    1071             : };
    1072             : 
    1073             : static clib_error_t *
    1074          57 : http_transport_enable (vlib_main_t *vm, u8 is_en)
    1075             : {
    1076          57 :   vnet_app_detach_args_t _da, *da = &_da;
    1077          57 :   vnet_app_attach_args_t _a, *a = &_a;
    1078             :   u64 options[APP_OPTIONS_N_OPTIONS];
    1079          57 :   http_main_t *hm = &http_main;
    1080             : 
    1081          57 :   if (!is_en)
    1082             :     {
    1083           8 :       da->app_index = hm->app_index;
    1084           8 :       da->api_client_index = APP_INVALID_INDEX;
    1085           8 :       vnet_application_detach (da);
    1086           8 :       return 0;
    1087             :     }
    1088             : 
    1089          49 :   vec_validate (hm->wrk, vlib_num_workers ());
    1090             : 
    1091          49 :   clib_memset (a, 0, sizeof (*a));
    1092          49 :   clib_memset (options, 0, sizeof (options));
    1093             : 
    1094          49 :   a->session_cb_vft = &http_app_cb_vft;
    1095          49 :   a->api_client_index = APP_INVALID_INDEX;
    1096          49 :   a->options = options;
    1097          49 :   a->name = format (0, "http");
    1098          49 :   a->options[APP_OPTIONS_SEGMENT_SIZE] = hm->first_seg_size;
    1099          49 :   a->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = hm->add_seg_size;
    1100          49 :   a->options[APP_OPTIONS_RX_FIFO_SIZE] = hm->fifo_size;
    1101          49 :   a->options[APP_OPTIONS_TX_FIFO_SIZE] = hm->fifo_size;
    1102          49 :   a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
    1103          49 :   a->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
    1104          49 :   a->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_IS_TRANSPORT_APP;
    1105             : 
    1106          49 :   if (vnet_application_attach (a))
    1107           0 :     return clib_error_return (0, "failed to attach http app");
    1108             : 
    1109          49 :   hm->app_index = a->app_index;
    1110          49 :   vec_free (a->name);
    1111             : 
    1112          49 :   clib_timebase_init (&hm->timebase, 0 /* GMT */, CLIB_TIMEBASE_DAYLIGHT_NONE,
    1113             :                       &vm->clib_time /* share the system clock */);
    1114             : 
    1115          49 :   http_timers_init (vm, http_conn_timeout_cb);
    1116             : 
    1117          49 :   return 0;
    1118             : }
    1119             : 
    1120             : static int
    1121           0 : http_transport_connect (transport_endpoint_cfg_t *tep)
    1122             : {
    1123           0 :   vnet_connect_args_t _cargs, *cargs = &_cargs;
    1124           0 :   http_main_t *hm = &http_main;
    1125           0 :   session_endpoint_cfg_t *sep = (session_endpoint_cfg_t *) tep;
    1126             :   application_t *app;
    1127             :   http_conn_t *hc;
    1128             :   int error;
    1129             :   u32 hc_index;
    1130           0 :   app_worker_t *app_wrk = app_worker_get (sep->app_wrk_index);
    1131             : 
    1132           0 :   clib_memset (cargs, 0, sizeof (*cargs));
    1133           0 :   clib_memcpy (&cargs->sep_ext, sep, sizeof (session_endpoint_cfg_t));
    1134           0 :   cargs->sep.transport_proto = TRANSPORT_PROTO_TCP;
    1135           0 :   cargs->app_index = hm->app_index;
    1136           0 :   app = application_get (app_wrk->app_index);
    1137           0 :   cargs->sep_ext.ns_index = app->ns_index;
    1138             : 
    1139           0 :   hc_index = http_conn_alloc_w_thread (0 /* ts->thread_index */);
    1140           0 :   hc = http_conn_get_w_thread (hc_index, 0);
    1141           0 :   hc->h_pa_wrk_index = sep->app_wrk_index;
    1142           0 :   hc->h_pa_app_api_ctx = sep->opaque;
    1143           0 :   hc->is_client = 1;
    1144           0 :   hc->state = HTTP_CONN_STATE_CONNECTING;
    1145           0 :   cargs->api_context = hc_index;
    1146             : 
    1147           0 :   if ((error = vnet_connect (cargs)))
    1148           0 :     return error;
    1149             : 
    1150           0 :   return 0;
    1151             : }
    1152             : 
    1153             : static u32
    1154           3 : http_start_listen (u32 app_listener_index, transport_endpoint_cfg_t *tep)
    1155             : {
    1156           3 :   vnet_listen_args_t _args = {}, *args = &_args;
    1157             :   session_t *ts_listener, *app_listener;
    1158           3 :   http_main_t *hm = &http_main;
    1159             :   session_endpoint_cfg_t *sep;
    1160             :   app_worker_t *app_wrk;
    1161             :   transport_proto_t tp;
    1162             :   app_listener_t *al;
    1163             :   application_t *app;
    1164             :   http_conn_t *lhc;
    1165             :   u32 lhc_index;
    1166             : 
    1167           3 :   sep = (session_endpoint_cfg_t *) tep;
    1168             : 
    1169           3 :   app_wrk = app_worker_get (sep->app_wrk_index);
    1170           3 :   app = application_get (app_wrk->app_index);
    1171             : 
    1172           3 :   args->app_index = hm->app_index;
    1173           3 :   args->sep_ext = *sep;
    1174           3 :   args->sep_ext.ns_index = app->ns_index;
    1175           3 :   tp = sep->ext_cfg ? TRANSPORT_PROTO_TLS : TRANSPORT_PROTO_TCP;
    1176           3 :   args->sep_ext.transport_proto = tp;
    1177             : 
    1178           3 :   if (vnet_listen (args))
    1179           0 :     return SESSION_INVALID_INDEX;
    1180             : 
    1181           3 :   lhc_index = http_listener_alloc ();
    1182           3 :   lhc = http_listener_get (lhc_index);
    1183             : 
    1184             :   /* Grab transport connection listener and link to http listener */
    1185           3 :   lhc->h_tc_session_handle = args->handle;
    1186           3 :   al = app_listener_get_w_handle (lhc->h_tc_session_handle);
    1187           3 :   ts_listener = app_listener_get_session (al);
    1188           3 :   ts_listener->opaque = lhc_index;
    1189             : 
    1190             :   /* Grab application listener and link to http listener */
    1191           3 :   app_listener = listen_session_get (app_listener_index);
    1192           3 :   lhc->h_pa_wrk_index = sep->app_wrk_index;
    1193           3 :   lhc->h_pa_session_handle = listen_session_get_handle (app_listener);
    1194           3 :   lhc->c_s_index = app_listener_index;
    1195           3 :   lhc->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
    1196             : 
    1197           3 :   return lhc_index;
    1198             : }
    1199             : 
    1200             : static u32
    1201           0 : http_stop_listen (u32 listener_index)
    1202             : {
    1203             :   http_conn_t *lhc;
    1204             :   int rv;
    1205             : 
    1206           0 :   lhc = http_listener_get (listener_index);
    1207             : 
    1208           0 :   vnet_unlisten_args_t a = {
    1209           0 :     .handle = lhc->h_tc_session_handle,
    1210           0 :     .app_index = http_main.app_index,
    1211             :     .wrk_map_index = 0 /* default wrk */
    1212             :   };
    1213             : 
    1214           0 :   if ((rv = vnet_unlisten (&a)))
    1215           0 :     clib_warning ("unlisten returned %d", rv);
    1216             : 
    1217           0 :   http_listener_free (lhc);
    1218             : 
    1219           0 :   return 0;
    1220             : }
    1221             : 
    1222             : static void
    1223           0 : http_transport_close (u32 hc_index, u32 thread_index)
    1224             : {
    1225             :   session_t *as;
    1226             :   http_conn_t *hc;
    1227             : 
    1228             :   HTTP_DBG (1, "App disconnecting %x", hc_index);
    1229             : 
    1230           0 :   hc = http_conn_get_w_thread (hc_index, thread_index);
    1231           0 :   if (hc->state == HTTP_CONN_STATE_CONNECTING)
    1232             :     {
    1233           0 :       hc->state = HTTP_CONN_STATE_APP_CLOSED;
    1234           0 :       http_disconnect_transport (hc);
    1235           0 :       return;
    1236             :     }
    1237             : 
    1238           0 :   as = session_get_from_handle (hc->h_pa_session_handle);
    1239             : 
    1240             :   /* Nothing more to send, confirm close */
    1241           0 :   if (!svm_fifo_max_dequeue_cons (as->tx_fifo))
    1242             :     {
    1243           0 :       session_transport_closed_notify (&hc->connection);
    1244           0 :       http_disconnect_transport (hc);
    1245             :     }
    1246             :   else
    1247             :     {
    1248             :       /* Wait for all data to be written to ts */
    1249           0 :       hc->state = HTTP_CONN_STATE_APP_CLOSED;
    1250             :     }
    1251             : }
    1252             : 
    1253             : static transport_connection_t *
    1254           0 : http_transport_get_connection (u32 hc_index, u32 thread_index)
    1255             : {
    1256           0 :   http_conn_t *hc = http_conn_get_w_thread (hc_index, thread_index);
    1257           0 :   return &hc->connection;
    1258             : }
    1259             : 
    1260             : static transport_connection_t *
    1261           6 : http_transport_get_listener (u32 listener_index)
    1262             : {
    1263           6 :   http_conn_t *lhc = http_listener_get (listener_index);
    1264           6 :   return &lhc->connection;
    1265             : }
    1266             : 
    1267             : static int
    1268           0 : http_app_tx_callback (void *session, transport_send_params_t *sp)
    1269             : {
    1270           0 :   session_t *as = (session_t *) session;
    1271             :   u32 max_burst_sz, sent;
    1272             :   http_conn_t *hc;
    1273             : 
    1274           0 :   hc = http_conn_get_w_thread (as->connection_index, as->thread_index);
    1275           0 :   if (hc->http_state < HTTP_STATE_WAIT_APP)
    1276             :     {
    1277           0 :       if (hc->state != HTTP_CONN_STATE_CLOSED)
    1278           0 :         clib_warning ("app data req state %u session state %u", hc->http_state,
    1279             :                       hc->state);
    1280           0 :       svm_fifo_dequeue_drop_all (as->tx_fifo);
    1281           0 :       return 0;
    1282             :     }
    1283             : 
    1284           0 :   max_burst_sz = sp->max_burst_size * TRANSPORT_PACER_MIN_MSS;
    1285           0 :   sp->max_burst_size = max_burst_sz;
    1286             : 
    1287           0 :   http_req_run_state_machine (hc, sp);
    1288             : 
    1289           0 :   if (hc->state == HTTP_CONN_STATE_APP_CLOSED)
    1290             :     {
    1291           0 :       if (!svm_fifo_max_dequeue_cons (as->tx_fifo))
    1292           0 :         http_disconnect_transport (hc);
    1293             :     }
    1294             : 
    1295           0 :   sent = max_burst_sz - sp->max_burst_size;
    1296             : 
    1297           0 :   return sent > 0 ? clib_max (sent / TRANSPORT_PACER_MIN_MSS, 1) : 0;
    1298             : }
    1299             : 
    1300             : static void
    1301           0 : http_transport_get_endpoint (u32 hc_index, u32 thread_index,
    1302             :                              transport_endpoint_t *tep, u8 is_lcl)
    1303             : {
    1304           0 :   http_conn_t *hc = http_conn_get_w_thread (hc_index, thread_index);
    1305             :   session_t *ts;
    1306             : 
    1307           0 :   ts = session_get_from_handle (hc->h_tc_session_handle);
    1308           0 :   session_get_endpoint (ts, tep, is_lcl);
    1309           0 : }
    1310             : 
    1311             : static u8 *
    1312           0 : format_http_connection (u8 *s, va_list *args)
    1313             : {
    1314           0 :   http_conn_t *hc = va_arg (*args, http_conn_t *);
    1315             :   session_t *ts;
    1316             : 
    1317           0 :   ts = session_get_from_handle (hc->h_tc_session_handle);
    1318           0 :   s = format (s, "[%d:%d][H] app_wrk %u ts %d:%d", hc->c_thread_index,
    1319           0 :               hc->c_s_index, hc->h_pa_wrk_index, ts->thread_index,
    1320             :               ts->session_index);
    1321             : 
    1322           0 :   return s;
    1323             : }
    1324             : 
    1325             : static u8 *
    1326           0 : format_http_listener (u8 *s, va_list *args)
    1327             : {
    1328           0 :   http_conn_t *lhc = va_arg (*args, http_conn_t *);
    1329             :   app_listener_t *al;
    1330             :   session_t *lts;
    1331             : 
    1332           0 :   al = app_listener_get_w_handle (lhc->h_tc_session_handle);
    1333           0 :   lts = app_listener_get_session (al);
    1334           0 :   s = format (s, "[%d:%d][H] app_wrk %u ts %d:%d", lhc->c_thread_index,
    1335           0 :               lhc->c_s_index, lhc->h_pa_wrk_index, lts->thread_index,
    1336             :               lts->session_index);
    1337             : 
    1338           0 :   return s;
    1339             : }
    1340             : 
    1341             : static u8 *
    1342           0 : format_http_conn_state (u8 *s, va_list *args)
    1343             : {
    1344           0 :   http_conn_t *hc = va_arg (*args, http_conn_t *);
    1345             : 
    1346           0 :   switch (hc->state)
    1347             :     {
    1348           0 :     case HTTP_CONN_STATE_LISTEN:
    1349           0 :       s = format (s, "LISTEN");
    1350           0 :       break;
    1351           0 :     case HTTP_CONN_STATE_CONNECTING:
    1352           0 :       s = format (s, "CONNECTING");
    1353           0 :       break;
    1354           0 :     case HTTP_CONN_STATE_ESTABLISHED:
    1355           0 :       s = format (s, "ESTABLISHED");
    1356           0 :       break;
    1357           0 :     case HTTP_CONN_STATE_TRANSPORT_CLOSED:
    1358           0 :       s = format (s, "TRANSPORT_CLOSED");
    1359           0 :       break;
    1360           0 :     case HTTP_CONN_STATE_APP_CLOSED:
    1361           0 :       s = format (s, "APP_CLOSED");
    1362           0 :       break;
    1363           0 :     case HTTP_CONN_STATE_CLOSED:
    1364           0 :       s = format (s, "CLOSED");
    1365           0 :       break;
    1366             :     }
    1367             : 
    1368           0 :   return s;
    1369             : }
    1370             : 
    1371             : static u8 *
    1372           0 : format_http_transport_connection (u8 *s, va_list *args)
    1373             : {
    1374           0 :   u32 tc_index = va_arg (*args, u32);
    1375           0 :   u32 thread_index = va_arg (*args, u32);
    1376           0 :   u32 verbose = va_arg (*args, u32);
    1377             :   http_conn_t *hc;
    1378             : 
    1379           0 :   hc = http_conn_get_w_thread (tc_index, thread_index);
    1380             : 
    1381           0 :   s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_http_connection, hc);
    1382           0 :   if (verbose)
    1383             :     {
    1384             :       s =
    1385           0 :         format (s, "%-" SESSION_CLI_STATE_LEN "U", format_http_conn_state, hc);
    1386           0 :       if (verbose > 1)
    1387           0 :         s = format (s, "\n");
    1388             :     }
    1389             : 
    1390           0 :   return s;
    1391             : }
    1392             : 
    1393             : static u8 *
    1394           0 : format_http_transport_listener (u8 *s, va_list *args)
    1395             : {
    1396           0 :   u32 tc_index = va_arg (*args, u32);
    1397           0 :   u32 __clib_unused thread_index = va_arg (*args, u32);
    1398           0 :   u32 __clib_unused verbose = va_arg (*args, u32);
    1399           0 :   http_conn_t *lhc = http_listener_get (tc_index);
    1400             : 
    1401           0 :   s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_http_listener, lhc);
    1402           0 :   if (verbose)
    1403             :     s =
    1404           0 :       format (s, "%-" SESSION_CLI_STATE_LEN "U", format_http_conn_state, lhc);
    1405           0 :   return s;
    1406             : }
    1407             : 
    1408             : static const transport_proto_vft_t http_proto = {
    1409             :   .enable = http_transport_enable,
    1410             :   .connect = http_transport_connect,
    1411             :   .start_listen = http_start_listen,
    1412             :   .stop_listen = http_stop_listen,
    1413             :   .close = http_transport_close,
    1414             :   .custom_tx = http_app_tx_callback,
    1415             :   .get_connection = http_transport_get_connection,
    1416             :   .get_listener = http_transport_get_listener,
    1417             :   .get_transport_endpoint = http_transport_get_endpoint,
    1418             :   .format_connection = format_http_transport_connection,
    1419             :   .format_listener = format_http_transport_listener,
    1420             :   .transport_options = {
    1421             :     .name = "http",
    1422             :     .short_name = "H",
    1423             :     .tx_type = TRANSPORT_TX_INTERNAL,
    1424             :     .service_type = TRANSPORT_SERVICE_APP,
    1425             :   },
    1426             : };
    1427             : 
    1428             : static clib_error_t *
    1429         575 : http_transport_init (vlib_main_t *vm)
    1430             : {
    1431         575 :   http_main_t *hm = &http_main;
    1432             : 
    1433         575 :   transport_register_protocol (TRANSPORT_PROTO_HTTP, &http_proto,
    1434             :                                FIB_PROTOCOL_IP4, ~0);
    1435         575 :   transport_register_protocol (TRANSPORT_PROTO_HTTP, &http_proto,
    1436             :                                FIB_PROTOCOL_IP6, ~0);
    1437             : 
    1438             :   /* Default values, configurable via startup conf */
    1439         575 :   hm->add_seg_size = 256 << 20;
    1440         575 :   hm->first_seg_size = 32 << 20;
    1441         575 :   hm->fifo_size = 512 << 10;
    1442             : 
    1443         575 :   return 0;
    1444             : }
    1445             : 
    1446        1151 : VLIB_INIT_FUNCTION (http_transport_init);
    1447             : 
    1448             : static clib_error_t *
    1449         575 : http_config_fn (vlib_main_t *vm, unformat_input_t *input)
    1450             : {
    1451         575 :   http_main_t *hm = &http_main;
    1452             :   uword mem_sz;
    1453             : 
    1454         575 :   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
    1455             :     {
    1456           0 :       if (unformat (input, "first-segment-size %U", unformat_memory_size,
    1457             :                     &mem_sz))
    1458             :         {
    1459           0 :           hm->first_seg_size = clib_max (mem_sz, 1 << 20);
    1460           0 :           if (hm->first_seg_size != mem_sz)
    1461           0 :             clib_warning ("first seg size too small %u", mem_sz);
    1462             :         }
    1463           0 :       else if (unformat (input, "add-segment-size %U", unformat_memory_size,
    1464             :                          &mem_sz))
    1465             :         {
    1466           0 :           hm->add_seg_size = clib_max (mem_sz, 1 << 20);
    1467           0 :           if (hm->add_seg_size != mem_sz)
    1468           0 :             clib_warning ("add seg size too small %u", mem_sz);
    1469             :         }
    1470           0 :       else if (unformat (input, "fifo-size %U", unformat_memory_size, &mem_sz))
    1471             :         {
    1472           0 :           hm->fifo_size = clib_clamp (mem_sz, 4 << 10, 2 << 30);
    1473           0 :           if (hm->fifo_size != mem_sz)
    1474           0 :             clib_warning ("invalid fifo size %lu", mem_sz);
    1475             :         }
    1476             :       else
    1477           0 :         return clib_error_return (0, "unknown input `%U'",
    1478             :                                   format_unformat_error, input);
    1479             :     }
    1480         575 :   return 0;
    1481             : }
    1482             : 
    1483        3482 : VLIB_CONFIG_FUNCTION (http_config_fn, "http");
    1484             : 
    1485             : VLIB_PLUGIN_REGISTER () = {
    1486             :   .version = VPP_BUILD_VER,
    1487             :   .description = "Hypertext Transfer Protocol (HTTP)",
    1488             :   .default_disabled = 0,
    1489             : };
    1490             : 
    1491             : /*
    1492             :  * fd.io coding-style-patch-verification: ON
    1493             :  *
    1494             :  * Local Variables:
    1495             :  * eval: (c-set-style "gnu")
    1496             :  * End:
    1497             :  */

Generated by: LCOV version 1.14