d404e734d48500102c25de2ab08a68ab586209d7
[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   if (app_wrk)
783     app_worker_rx_notify (app_wrk, as);
784
785   return HTTP_SM_STOP;
786 }
787
788 static int
789 cln_drain_rx_buf (http_conn_t *hc, session_t *ts, session_t *as)
790 {
791   app_worker_t *app_wrk;
792   u32 max_enq, n_enq, dlen = vec_len (hc->rx_buf) - hc->rx_buf_offset;
793   int rv;
794
795   max_enq = svm_fifo_max_enqueue (as->rx_fifo);
796   n_enq = clib_min (max_enq, dlen);
797   rv = svm_fifo_enqueue (as->rx_fifo, n_enq, &hc->rx_buf[hc->rx_buf_offset]);
798   if (rv < 0)
799     {
800       clib_warning ("enqueue failed");
801       return -1;
802     }
803
804   hc->rx_buf_offset += rv;
805
806   if (hc->rx_buf_offset >= vec_len (hc->rx_buf))
807     {
808       vec_reset_length (hc->rx_buf);
809       hc->rx_buf_offset = 0;
810     }
811
812   app_wrk = app_worker_get_if_valid (as->app_wrk_index);
813   ASSERT (app_wrk);
814
815   app_worker_rx_notify (app_wrk, as);
816   return 1;
817 }
818
819 static http_sm_result_t
820 state_cln_recv_more_data (http_conn_t *hc, transport_send_params_t *sp)
821 {
822   session_t *as;
823   u32 max_deq;
824   session_t *ts;
825   int n_read, rv;
826
827   as = session_get_from_handle (hc->h_pa_session_handle);
828   ts = session_get_from_handle (hc->h_tc_session_handle);
829
830   u32 dlen = vec_len (hc->rx_buf) - hc->rx_buf_offset;
831   if (dlen)
832     {
833       rv = cln_drain_rx_buf (hc, ts, as);
834       if (rv < 0)
835         {
836           clib_warning ("drain rx error!");
837           return HTTP_SM_ERROR;
838         }
839       goto maybe_reschedule;
840     }
841
842   if (hc->to_recv == 0)
843     {
844       ASSERT (vec_len (hc->rx_buf) == 0);
845       ASSERT (hc->rx_buf_offset == 0);
846       hc->http_state = HTTP_STATE_WAIT_APP;
847       return HTTP_SM_STOP;
848     }
849
850   max_deq = svm_fifo_max_dequeue (ts->rx_fifo);
851   if (max_deq == 0)
852     return HTTP_SM_STOP;
853
854   ASSERT (vec_len (hc->rx_buf) == 0);
855   ASSERT (hc->rx_buf_offset == 0);
856
857   vec_validate (hc->rx_buf, max_deq - 1);
858   n_read = svm_fifo_dequeue (ts->rx_fifo, max_deq, hc->rx_buf);
859   ASSERT (n_read == max_deq);
860
861   if (svm_fifo_is_empty (ts->rx_fifo))
862     svm_fifo_unset_event (ts->rx_fifo);
863
864   hc->to_recv -= n_read;
865   vec_set_len (hc->rx_buf, max_deq);
866
867 maybe_reschedule:
868   if (hc->rx_buf_offset < vec_len (hc->rx_buf) ||
869       svm_fifo_max_dequeue_cons (ts->rx_fifo))
870     {
871       /* TODO is the flag really needed? */
872       if (svm_fifo_set_event (ts->rx_fifo))
873         session_enqueue_notify (ts);
874     }
875   return HTTP_SM_CONTINUE;
876 }
877
878 static http_sm_result_t
879 state_cln_wait_app (http_conn_t *hc, transport_send_params_t *sp)
880 {
881   session_t *as;
882   http_msg_t msg;
883   http_status_code_t ec;
884   u8 *buf = 0, *request;
885   u32 offset;
886   int rv;
887
888   as = session_get_from_handle (hc->h_pa_session_handle);
889   rv = svm_fifo_dequeue (as->tx_fifo, sizeof (msg), (u8 *) &msg);
890   ASSERT (rv == sizeof (msg));
891   if (msg.type != HTTP_MSG_REQUEST || msg.data.type > HTTP_MSG_DATA_PTR)
892     {
893       clib_warning ("unexpected msg type from app %u", msg.type);
894       ec = HTTP_STATUS_INTERNAL_ERROR;
895       goto error;
896     }
897
898   vec_validate (buf, msg.data.len - 1);
899   rv = svm_fifo_dequeue (as->tx_fifo, msg.data.len, buf);
900   ASSERT (rv == msg.data.len);
901
902   request = format (0, http_request_template, buf);
903   offset = send_data (hc, request, vec_len (request), 0);
904   if (offset != vec_len (request))
905     {
906       clib_warning ("sending request failed!");
907       ec = HTTP_STATUS_INTERNAL_ERROR;
908       goto error;
909     }
910
911   hc->http_state = HTTP_STATE_WAIT_METHOD;
912
913   vec_free (buf);
914   vec_free (request);
915
916   return HTTP_SM_CONTINUE;
917
918 error:
919   send_error (hc, ec);
920   session_transport_closing_notify (&hc->connection);
921   http_disconnect_transport (hc);
922   return HTTP_SM_STOP;
923 }
924
925 typedef http_sm_result_t (*http_sm_handler) (http_conn_t *,
926                                              transport_send_params_t *sp);
927
928 static http_sm_handler srv_state_funcs[HTTP_N_STATES] = {
929   /* Waiting for GET, POST, etc. */
930   state_srv_wait_method,
931   /* Wait for data from app */
932   state_srv_wait_app,
933   /* Send more data */
934   state_srv_send_more_data,
935 };
936
937 static http_sm_handler cln_state_funcs[HTTP_N_STATES] = {
938   /* wait for reply */
939   state_cln_wait_method,
940   /* wait for data from app */
941   state_cln_wait_app,
942   /* receive more data */
943   state_cln_recv_more_data,
944 };
945
946 static void
947 http_req_run_state_machine (http_conn_t *hc, transport_send_params_t *sp)
948 {
949   http_sm_result_t res;
950   http_sm_handler *state_fn =
951     hc->is_client ? cln_state_funcs : srv_state_funcs;
952   do
953     {
954       res = state_fn[hc->http_state](hc, sp);
955       if (res == HTTP_SM_ERROR)
956         return;
957     }
958   while (res == HTTP_SM_CONTINUE);
959
960   /* Reset the session expiration timer */
961   http_conn_timer_update (hc);
962 }
963
964 static int
965 http_ts_server_rx_callback (session_t *ts, http_conn_t *hc)
966 {
967   if (hc->http_state != HTTP_STATE_WAIT_METHOD)
968     {
969       clib_warning ("tcp data in req state %u", hc->http_state);
970       return 0;
971     }
972
973   http_req_run_state_machine (hc, 0);
974
975   if (hc->state == HTTP_CONN_STATE_TRANSPORT_CLOSED)
976     {
977       if (!svm_fifo_max_dequeue_cons (ts->rx_fifo))
978         session_transport_closing_notify (&hc->connection);
979     }
980   return 0;
981 }
982
983 static int
984 http_ts_client_rx_callback (session_t *ts, http_conn_t *hc)
985 {
986   if (hc->http_state != HTTP_STATE_WAIT_METHOD &&
987       hc->http_state != HTTP_STATE_IO_MORE_DATA)
988     {
989       clib_warning ("http in unexpected state %d (ts %d)", hc->http_state,
990                     ts->session_index);
991       return 0;
992     }
993
994   http_req_run_state_machine (hc, 0);
995
996   if (hc->state == HTTP_CONN_STATE_TRANSPORT_CLOSED)
997     {
998       if (!svm_fifo_max_dequeue_cons (ts->rx_fifo))
999         session_transport_closing_notify (&hc->connection);
1000     }
1001   return 0;
1002 }
1003
1004 static int
1005 http_ts_rx_callback (session_t *ts)
1006 {
1007   http_conn_t *hc;
1008
1009   hc = http_conn_get_w_thread (ts->opaque, ts->thread_index);
1010   if (hc->is_client)
1011     return http_ts_client_rx_callback (ts, hc);
1012   return http_ts_server_rx_callback (ts, hc);
1013 }
1014
1015 int
1016 http_ts_builtin_tx_callback (session_t *ts)
1017 {
1018   http_conn_t *hc;
1019
1020   hc = http_conn_get_w_thread (ts->opaque, ts->thread_index);
1021   transport_connection_reschedule (&hc->connection);
1022
1023   return 0;
1024 }
1025
1026 static void
1027 http_ts_cleanup_callback (session_t *ts, session_cleanup_ntf_t ntf)
1028 {
1029   http_conn_t *hc;
1030
1031   if (ntf == SESSION_CLEANUP_TRANSPORT)
1032     return;
1033
1034   hc = http_conn_get_w_thread (ts->opaque, ts->thread_index);
1035   if (!hc)
1036     {
1037       clib_warning ("no http connection for %u", ts->session_index);
1038       return;
1039     }
1040
1041   vec_free (hc->rx_buf);
1042
1043   http_buffer_free (&hc->tx_buf);
1044   http_conn_timer_stop (hc);
1045
1046   session_transport_delete_notify (&hc->connection);
1047   http_conn_free (hc);
1048 }
1049
1050 int
1051 http_add_segment_callback (u32 client_index, u64 segment_handle)
1052 {
1053   /* No-op for builtin */
1054   return 0;
1055 }
1056
1057 int
1058 http_del_segment_callback (u32 client_index, u64 segment_handle)
1059 {
1060   return 0;
1061 }
1062
1063 static session_cb_vft_t http_app_cb_vft = {
1064   .session_accept_callback = http_ts_accept_callback,
1065   .session_disconnect_callback = http_ts_disconnect_callback,
1066   .session_connected_callback = http_ts_connected_callback,
1067   .session_reset_callback = http_ts_reset_callback,
1068   .session_cleanup_callback = http_ts_cleanup_callback,
1069   .add_segment_callback = http_add_segment_callback,
1070   .del_segment_callback = http_del_segment_callback,
1071   .builtin_app_rx_callback = http_ts_rx_callback,
1072   .builtin_app_tx_callback = http_ts_builtin_tx_callback,
1073 };
1074
1075 static clib_error_t *
1076 http_transport_enable (vlib_main_t *vm, u8 is_en)
1077 {
1078   vnet_app_detach_args_t _da, *da = &_da;
1079   vnet_app_attach_args_t _a, *a = &_a;
1080   u64 options[APP_OPTIONS_N_OPTIONS];
1081   http_main_t *hm = &http_main;
1082
1083   if (!is_en)
1084     {
1085       da->app_index = hm->app_index;
1086       da->api_client_index = APP_INVALID_INDEX;
1087       vnet_application_detach (da);
1088       return 0;
1089     }
1090
1091   vec_validate (hm->wrk, vlib_num_workers ());
1092
1093   clib_memset (a, 0, sizeof (*a));
1094   clib_memset (options, 0, sizeof (options));
1095
1096   a->session_cb_vft = &http_app_cb_vft;
1097   a->api_client_index = APP_INVALID_INDEX;
1098   a->options = options;
1099   a->name = format (0, "http");
1100   a->options[APP_OPTIONS_SEGMENT_SIZE] = hm->first_seg_size;
1101   a->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = hm->add_seg_size;
1102   a->options[APP_OPTIONS_RX_FIFO_SIZE] = hm->fifo_size;
1103   a->options[APP_OPTIONS_TX_FIFO_SIZE] = hm->fifo_size;
1104   a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
1105   a->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
1106   a->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_IS_TRANSPORT_APP;
1107
1108   if (vnet_application_attach (a))
1109     return clib_error_return (0, "failed to attach http app");
1110
1111   hm->app_index = a->app_index;
1112   vec_free (a->name);
1113
1114   clib_timebase_init (&hm->timebase, 0 /* GMT */, CLIB_TIMEBASE_DAYLIGHT_NONE,
1115                       &vm->clib_time /* share the system clock */);
1116
1117   http_timers_init (vm, http_conn_timeout_cb);
1118
1119   return 0;
1120 }
1121
1122 static int
1123 http_transport_connect (transport_endpoint_cfg_t *tep)
1124 {
1125   vnet_connect_args_t _cargs, *cargs = &_cargs;
1126   http_main_t *hm = &http_main;
1127   session_endpoint_cfg_t *sep = (session_endpoint_cfg_t *) tep;
1128   application_t *app;
1129   http_conn_t *hc;
1130   int error;
1131   u32 hc_index;
1132   app_worker_t *app_wrk = app_worker_get (sep->app_wrk_index);
1133
1134   clib_memset (cargs, 0, sizeof (*cargs));
1135   clib_memcpy (&cargs->sep_ext, sep, sizeof (session_endpoint_cfg_t));
1136   cargs->sep.transport_proto = TRANSPORT_PROTO_TCP;
1137   cargs->app_index = hm->app_index;
1138   app = application_get (app_wrk->app_index);
1139   cargs->sep_ext.ns_index = app->ns_index;
1140
1141   hc_index = http_conn_alloc_w_thread (0 /* ts->thread_index */);
1142   hc = http_conn_get_w_thread (hc_index, 0);
1143   hc->h_pa_wrk_index = sep->app_wrk_index;
1144   hc->h_pa_app_api_ctx = sep->opaque;
1145   hc->is_client = 1;
1146   hc->state = HTTP_CONN_STATE_CONNECTING;
1147   cargs->api_context = hc_index;
1148
1149   if ((error = vnet_connect (cargs)))
1150     return error;
1151
1152   return 0;
1153 }
1154
1155 static u32
1156 http_start_listen (u32 app_listener_index, transport_endpoint_cfg_t *tep)
1157 {
1158   vnet_listen_args_t _args = {}, *args = &_args;
1159   session_t *ts_listener, *app_listener;
1160   http_main_t *hm = &http_main;
1161   session_endpoint_cfg_t *sep;
1162   app_worker_t *app_wrk;
1163   transport_proto_t tp;
1164   app_listener_t *al;
1165   application_t *app;
1166   http_conn_t *lhc;
1167   u32 lhc_index;
1168
1169   sep = (session_endpoint_cfg_t *) tep;
1170
1171   app_wrk = app_worker_get (sep->app_wrk_index);
1172   app = application_get (app_wrk->app_index);
1173
1174   args->app_index = hm->app_index;
1175   args->sep_ext = *sep;
1176   args->sep_ext.ns_index = app->ns_index;
1177   tp = sep->ext_cfg ? TRANSPORT_PROTO_TLS : TRANSPORT_PROTO_TCP;
1178   args->sep_ext.transport_proto = tp;
1179
1180   if (vnet_listen (args))
1181     return SESSION_INVALID_INDEX;
1182
1183   lhc_index = http_listener_alloc ();
1184   lhc = http_listener_get (lhc_index);
1185
1186   /* Grab transport connection listener and link to http listener */
1187   lhc->h_tc_session_handle = args->handle;
1188   al = app_listener_get_w_handle (lhc->h_tc_session_handle);
1189   ts_listener = app_listener_get_session (al);
1190   ts_listener->opaque = lhc_index;
1191
1192   /* Grab application listener and link to http listener */
1193   app_listener = listen_session_get (app_listener_index);
1194   lhc->h_pa_wrk_index = sep->app_wrk_index;
1195   lhc->h_pa_session_handle = listen_session_get_handle (app_listener);
1196   lhc->c_s_index = app_listener_index;
1197   lhc->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
1198
1199   return lhc_index;
1200 }
1201
1202 static u32
1203 http_stop_listen (u32 listener_index)
1204 {
1205   http_conn_t *lhc;
1206   int rv;
1207
1208   lhc = http_listener_get (listener_index);
1209
1210   vnet_unlisten_args_t a = {
1211     .handle = lhc->h_tc_session_handle,
1212     .app_index = http_main.app_index,
1213     .wrk_map_index = 0 /* default wrk */
1214   };
1215
1216   if ((rv = vnet_unlisten (&a)))
1217     clib_warning ("unlisten returned %d", rv);
1218
1219   http_listener_free (lhc);
1220
1221   return 0;
1222 }
1223
1224 static void
1225 http_transport_close (u32 hc_index, u32 thread_index)
1226 {
1227   session_t *as;
1228   http_conn_t *hc;
1229
1230   HTTP_DBG (1, "App disconnecting %x", hc_index);
1231
1232   hc = http_conn_get_w_thread (hc_index, thread_index);
1233   if (hc->state == HTTP_CONN_STATE_CONNECTING)
1234     {
1235       hc->state = HTTP_CONN_STATE_APP_CLOSED;
1236       http_disconnect_transport (hc);
1237       return;
1238     }
1239
1240   as = session_get_from_handle (hc->h_pa_session_handle);
1241
1242   /* Nothing more to send, confirm close */
1243   if (!svm_fifo_max_dequeue_cons (as->tx_fifo))
1244     {
1245       session_transport_closed_notify (&hc->connection);
1246       http_disconnect_transport (hc);
1247     }
1248   else
1249     {
1250       /* Wait for all data to be written to ts */
1251       hc->state = HTTP_CONN_STATE_APP_CLOSED;
1252     }
1253 }
1254
1255 static transport_connection_t *
1256 http_transport_get_connection (u32 hc_index, u32 thread_index)
1257 {
1258   http_conn_t *hc = http_conn_get_w_thread (hc_index, thread_index);
1259   return &hc->connection;
1260 }
1261
1262 static transport_connection_t *
1263 http_transport_get_listener (u32 listener_index)
1264 {
1265   http_conn_t *lhc = http_listener_get (listener_index);
1266   return &lhc->connection;
1267 }
1268
1269 static int
1270 http_app_tx_callback (void *session, transport_send_params_t *sp)
1271 {
1272   session_t *as = (session_t *) session;
1273   u32 max_burst_sz, sent;
1274   http_conn_t *hc;
1275
1276   hc = http_conn_get_w_thread (as->connection_index, as->thread_index);
1277   if (hc->http_state < HTTP_STATE_WAIT_APP)
1278     {
1279       if (hc->state != HTTP_CONN_STATE_CLOSED)
1280         clib_warning ("app data req state %u session state %u", hc->http_state,
1281                       hc->state);
1282       svm_fifo_dequeue_drop_all (as->tx_fifo);
1283       return 0;
1284     }
1285
1286   max_burst_sz = sp->max_burst_size * TRANSPORT_PACER_MIN_MSS;
1287   sp->max_burst_size = max_burst_sz;
1288
1289   http_req_run_state_machine (hc, sp);
1290
1291   if (hc->state == HTTP_CONN_STATE_APP_CLOSED)
1292     {
1293       if (!svm_fifo_max_dequeue_cons (as->tx_fifo))
1294         http_disconnect_transport (hc);
1295     }
1296
1297   sent = max_burst_sz - sp->max_burst_size;
1298
1299   return sent > 0 ? clib_max (sent / TRANSPORT_PACER_MIN_MSS, 1) : 0;
1300 }
1301
1302 static void
1303 http_transport_get_endpoint (u32 hc_index, u32 thread_index,
1304                              transport_endpoint_t *tep, u8 is_lcl)
1305 {
1306   http_conn_t *hc = http_conn_get_w_thread (hc_index, thread_index);
1307   session_t *ts;
1308
1309   ts = session_get_from_handle (hc->h_tc_session_handle);
1310   session_get_endpoint (ts, tep, is_lcl);
1311 }
1312
1313 static u8 *
1314 format_http_connection (u8 *s, va_list *args)
1315 {
1316   http_conn_t *hc = va_arg (*args, http_conn_t *);
1317   session_t *ts;
1318
1319   ts = session_get_from_handle (hc->h_tc_session_handle);
1320   s = format (s, "[%d:%d][H] app_wrk %u ts %d:%d", hc->c_thread_index,
1321               hc->c_s_index, hc->h_pa_wrk_index, ts->thread_index,
1322               ts->session_index);
1323
1324   return s;
1325 }
1326
1327 static u8 *
1328 format_http_listener (u8 *s, va_list *args)
1329 {
1330   http_conn_t *lhc = va_arg (*args, http_conn_t *);
1331   app_listener_t *al;
1332   session_t *lts;
1333
1334   al = app_listener_get_w_handle (lhc->h_tc_session_handle);
1335   lts = app_listener_get_session (al);
1336   s = format (s, "[%d:%d][H] app_wrk %u ts %d:%d", lhc->c_thread_index,
1337               lhc->c_s_index, lhc->h_pa_wrk_index, lts->thread_index,
1338               lts->session_index);
1339
1340   return s;
1341 }
1342
1343 static u8 *
1344 format_http_conn_state (u8 *s, va_list *args)
1345 {
1346   http_conn_t *hc = va_arg (*args, http_conn_t *);
1347
1348   switch (hc->state)
1349     {
1350     case HTTP_CONN_STATE_LISTEN:
1351       s = format (s, "LISTEN");
1352       break;
1353     case HTTP_CONN_STATE_CONNECTING:
1354       s = format (s, "CONNECTING");
1355       break;
1356     case HTTP_CONN_STATE_ESTABLISHED:
1357       s = format (s, "ESTABLISHED");
1358       break;
1359     case HTTP_CONN_STATE_TRANSPORT_CLOSED:
1360       s = format (s, "TRANSPORT_CLOSED");
1361       break;
1362     case HTTP_CONN_STATE_APP_CLOSED:
1363       s = format (s, "APP_CLOSED");
1364       break;
1365     case HTTP_CONN_STATE_CLOSED:
1366       s = format (s, "CLOSED");
1367       break;
1368     }
1369
1370   return s;
1371 }
1372
1373 static u8 *
1374 format_http_transport_connection (u8 *s, va_list *args)
1375 {
1376   u32 tc_index = va_arg (*args, u32);
1377   u32 thread_index = va_arg (*args, u32);
1378   u32 verbose = va_arg (*args, u32);
1379   http_conn_t *hc;
1380
1381   hc = http_conn_get_w_thread (tc_index, thread_index);
1382
1383   s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_http_connection, hc);
1384   if (verbose)
1385     {
1386       s =
1387         format (s, "%-" SESSION_CLI_STATE_LEN "U", format_http_conn_state, hc);
1388       if (verbose > 1)
1389         s = format (s, "\n");
1390     }
1391
1392   return s;
1393 }
1394
1395 static u8 *
1396 format_http_transport_listener (u8 *s, va_list *args)
1397 {
1398   u32 tc_index = va_arg (*args, u32);
1399   u32 __clib_unused thread_index = va_arg (*args, u32);
1400   u32 __clib_unused verbose = va_arg (*args, u32);
1401   http_conn_t *lhc = http_listener_get (tc_index);
1402
1403   s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_http_listener, lhc);
1404   if (verbose)
1405     s =
1406       format (s, "%-" SESSION_CLI_STATE_LEN "U", format_http_conn_state, lhc);
1407   return s;
1408 }
1409
1410 static const transport_proto_vft_t http_proto = {
1411   .enable = http_transport_enable,
1412   .connect = http_transport_connect,
1413   .start_listen = http_start_listen,
1414   .stop_listen = http_stop_listen,
1415   .close = http_transport_close,
1416   .custom_tx = http_app_tx_callback,
1417   .get_connection = http_transport_get_connection,
1418   .get_listener = http_transport_get_listener,
1419   .get_transport_endpoint = http_transport_get_endpoint,
1420   .format_connection = format_http_transport_connection,
1421   .format_listener = format_http_transport_listener,
1422   .transport_options = {
1423     .name = "http",
1424     .short_name = "H",
1425     .tx_type = TRANSPORT_TX_INTERNAL,
1426     .service_type = TRANSPORT_SERVICE_APP,
1427   },
1428 };
1429
1430 static clib_error_t *
1431 http_transport_init (vlib_main_t *vm)
1432 {
1433   http_main_t *hm = &http_main;
1434
1435   transport_register_protocol (TRANSPORT_PROTO_HTTP, &http_proto,
1436                                FIB_PROTOCOL_IP4, ~0);
1437   transport_register_protocol (TRANSPORT_PROTO_HTTP, &http_proto,
1438                                FIB_PROTOCOL_IP6, ~0);
1439
1440   /* Default values, configurable via startup conf */
1441   hm->add_seg_size = 256 << 20;
1442   hm->first_seg_size = 32 << 20;
1443   hm->fifo_size = 512 << 10;
1444
1445   return 0;
1446 }
1447
1448 VLIB_INIT_FUNCTION (http_transport_init);
1449
1450 static clib_error_t *
1451 http_config_fn (vlib_main_t *vm, unformat_input_t *input)
1452 {
1453   http_main_t *hm = &http_main;
1454   uword mem_sz;
1455
1456   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1457     {
1458       if (unformat (input, "first-segment-size %U", unformat_memory_size,
1459                     &mem_sz))
1460         {
1461           hm->first_seg_size = clib_max (mem_sz, 1 << 20);
1462           if (hm->first_seg_size != mem_sz)
1463             clib_warning ("first seg size too small %u", mem_sz);
1464         }
1465       else if (unformat (input, "add-segment-size %U", unformat_memory_size,
1466                          &mem_sz))
1467         {
1468           hm->add_seg_size = clib_max (mem_sz, 1 << 20);
1469           if (hm->add_seg_size != mem_sz)
1470             clib_warning ("add seg size too small %u", mem_sz);
1471         }
1472       else if (unformat (input, "fifo-size %U", unformat_memory_size, &mem_sz))
1473         {
1474           hm->fifo_size = clib_clamp (mem_sz, 4 << 10, 2 << 30);
1475           if (hm->fifo_size != mem_sz)
1476             clib_warning ("invalid fifo size %lu", mem_sz);
1477         }
1478       else
1479         return clib_error_return (0, "unknown input `%U'",
1480                                   format_unformat_error, input);
1481     }
1482   return 0;
1483 }
1484
1485 VLIB_CONFIG_FUNCTION (http_config_fn, "http");
1486
1487 VLIB_PLUGIN_REGISTER () = {
1488   .version = VPP_BUILD_VER,
1489   .description = "Hypertext Transfer Protocol (HTTP)",
1490   .default_disabled = 0,
1491 };
1492
1493 /*
1494  * fd.io coding-style-patch-verification: ON
1495  *
1496  * Local Variables:
1497  * eval: (c-set-style "gnu")
1498  * End:
1499  */