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