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