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