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