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