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

Generated by: LCOV version 1.14