http: add http protocol plugin
[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 const char *http_status_code_str[] = {
23 #define _(c, s, str) str,
24   foreach_http_status_code
25 #undef _
26 };
27
28 const char *http_content_type_str[] = {
29 #define _(s, str) str,
30   foreach_http_content_type
31 #undef _
32 };
33
34 static inline http_worker_t *
35 http_worker_get (u32 thread_index)
36 {
37   return &http_main.wrk[thread_index];
38 }
39
40 static inline u32
41 http_conn_alloc_w_thread (u32 thread_index)
42 {
43   http_worker_t *wrk = http_worker_get (thread_index);
44   http_conn_t *hc;
45
46   pool_get_zero (wrk->conn_pool, hc);
47   hc->c_thread_index = thread_index;
48   hc->h_hc_index = hc - wrk->conn_pool;
49   hc->h_pa_session_handle = SESSION_INVALID_HANDLE;
50   hc->h_tc_session_handle = SESSION_INVALID_HANDLE;
51   return hc->h_hc_index;
52 }
53
54 static inline http_conn_t *
55 http_conn_get_w_thread (u32 hc_index, u32 thread_index)
56 {
57   http_worker_t *wrk = http_worker_get (thread_index);
58   return pool_elt_at_index (wrk->conn_pool, hc_index);
59 }
60
61 void
62 http_conn_free (http_conn_t *hc)
63 {
64   http_worker_t *wrk = http_worker_get (hc->c_thread_index);
65   pool_put (wrk->conn_pool, hc);
66 }
67
68 static u32
69 http_listener_alloc (void)
70 {
71   http_main_t *hm = &http_main;
72   http_conn_t *ctx;
73
74   pool_get_zero (hm->listener_ctx_pool, ctx);
75   ctx->c_c_index = ctx - hm->listener_ctx_pool;
76   return ctx->c_c_index;
77 }
78
79 http_conn_t *
80 http_listener_get (u32 ctx_index)
81 {
82   return pool_elt_at_index (http_main.listener_ctx_pool, ctx_index);
83 }
84
85 void
86 http_disconnect_transport (http_conn_t *hc)
87 {
88   vnet_disconnect_args_t a = {
89     .handle = hc->h_tc_session_handle,
90     .app_index = http_main.app_index,
91   };
92
93   hc->state = HTTP_CONN_STATE_CLOSED;
94
95   if (vnet_disconnect_session (&a))
96     clib_warning ("disconnect returned");
97 }
98
99 static void
100 http_buffer_init (http_buffer_t *hb, svm_fifo_t *f, u32 data_len)
101 {
102   hb->len = data_len;
103   hb->offset = 0;
104   hb->cur_seg = 0;
105   hb->src = f;
106   hb->segs = 0;
107 }
108
109 static void
110 http_buffer_free (http_buffer_t *hb)
111 {
112   hb->src = 0;
113   vec_free (hb->segs);
114 }
115
116 svm_fifo_seg_t *
117 http_buffer_get_segs (http_buffer_t *hb, u32 max_len, u32 *n_segs)
118 {
119   u32 _n_segs = 5;
120   int len;
121
122   max_len = clib_max (hb->len - hb->offset, max_len);
123
124   vec_validate (hb->segs, _n_segs);
125
126   len = svm_fifo_segments (hb->src, 0, hb->segs, &_n_segs, max_len);
127   if (len < 0)
128     return 0;
129
130   *n_segs = _n_segs;
131
132   HTTP_DBG (1, "available to send %u n_segs %u", len, *n_segs);
133
134   return hb->segs;
135 }
136
137 void
138 http_buffer_drain (http_buffer_t *hb, u32 len)
139 {
140   hb->offset += len;
141   svm_fifo_dequeue_drop (hb->src, len);
142   HTTP_DBG (1, "drained %u len %u offset %u", len, hb->len, hb->offset);
143 }
144
145 static inline u8
146 http_buffer_is_drained (http_buffer_t *hb)
147 {
148   ASSERT (hb->offset <= hb->len);
149   return (hb->offset == hb->len);
150 }
151
152 static void
153 http_conn_timeout_cb (void *hc_handlep)
154 {
155   http_conn_t *hc;
156   uword hs_handle;
157
158   hs_handle = pointer_to_uword (hc_handlep);
159   hc = http_conn_get_w_thread (hs_handle & 0x00FFFFFF, hs_handle >> 24);
160
161   HTTP_DBG (1, "terminate thread %d index %d hs %llx", hs_handle >> 24,
162             hs_handle & 0x00FFFFFF, hc);
163   if (!hc)
164     return;
165
166   hc->timer_handle = ~0;
167   session_transport_closing_notify (&hc->connection);
168   http_disconnect_transport (hc);
169 }
170
171 int
172 http_ts_accept_callback (session_t *ts)
173 {
174   session_t *ts_listener, *as, *asl;
175   app_worker_t *app_wrk;
176   http_conn_t *lhc, *hc;
177   u32 hc_index;
178   int rv;
179
180   ts_listener = listen_session_get_from_handle (ts->listener_handle);
181   lhc = http_listener_get (ts_listener->opaque);
182
183   hc_index = http_conn_alloc_w_thread (ts->thread_index);
184   hc = http_conn_get_w_thread (hc_index, ts->thread_index);
185   clib_memcpy_fast (hc, lhc, sizeof (*lhc));
186   hc->c_thread_index = vlib_get_thread_index ();
187   hc->h_hc_index = hc_index;
188
189   hc->h_tc_session_handle = session_handle (ts);
190   hc->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
191
192   hc->state = HTTP_CONN_STATE_ESTABLISHED;
193   hc->req_state = HTTP_REQ_STATE_WAIT_METHOD;
194
195   ts->session_state = SESSION_STATE_READY;
196   ts->opaque = hc_index;
197
198   /*
199    * Alloc session and initialize
200    */
201   as = session_alloc (hc->c_thread_index);
202   as->session_state = SESSION_STATE_CREATED;
203   hc->c_s_index = as->session_index;
204
205   as->app_wrk_index = hc->h_pa_wrk_index;
206   as->connection_index = hc->c_c_index;
207   as->session_state = SESSION_STATE_ACCEPTING;
208
209   asl = listen_session_get_from_handle (lhc->h_pa_session_handle);
210   as->session_type = asl->session_type;
211   as->listener_handle = lhc->h_pa_session_handle;
212
213   /*
214    * Init session fifos and notify app
215    */
216   if ((rv = app_worker_init_accepted (as)))
217     {
218       HTTP_DBG (1, "failed to allocate fifos");
219       session_free (as);
220       return rv;
221     }
222
223   hc->h_pa_session_handle = session_handle (as);
224   hc->h_pa_wrk_index = as->app_wrk_index;
225   app_wrk = app_worker_get (as->app_wrk_index);
226
227   HTTP_DBG (1, "Accepted on listener %u new connection [%u]%x",
228             ts_listener->opaque, vlib_get_thread_index (), hc_index);
229
230   if ((rv = app_worker_accept_notify (app_wrk, as)))
231     {
232       HTTP_DBG (0, "app accept returned");
233       session_free (as);
234       return rv;
235     }
236
237   http_conn_timer_start (hc);
238
239   return 0;
240 }
241
242 static int
243 http_ts_connected_callback (u32 http_app_index, u32 hc_index, session_t *ts,
244                             session_error_t err)
245 {
246   clib_warning ("not supported");
247   return 0;
248 }
249
250 static void
251 http_ts_disconnect_callback (session_t *ts)
252 {
253   http_conn_t *hc;
254
255   hc = http_conn_get_w_thread (ts->opaque, ts->thread_index);
256
257   if (hc->state < HTTP_CONN_STATE_TRANSPORT_CLOSED)
258     hc->state = HTTP_CONN_STATE_TRANSPORT_CLOSED;
259
260   if (!svm_fifo_max_dequeue_cons (ts->rx_fifo))
261     session_transport_closing_notify (&hc->connection);
262 }
263
264 static void
265 http_ts_reset_callback (session_t *ts)
266 {
267   http_conn_t *ctx;
268
269   ctx = http_conn_get_w_thread (ts->opaque, ts->thread_index);
270
271   if (ctx->state < HTTP_CONN_STATE_TRANSPORT_CLOSED)
272     ctx->state = HTTP_CONN_STATE_TRANSPORT_CLOSED;
273
274   if (!svm_fifo_max_dequeue_cons (ts->rx_fifo))
275     session_transport_reset_notify (&ctx->connection);
276 }
277
278 /**
279  * http error boilerplate
280  */
281 static const char *http_error_template = "HTTP/1.1 %s\r\n"
282                                          "Date: %U GMT\r\n"
283                                          "Content-Type: text/html\r\n"
284                                          "Connection: close\r\n"
285                                          "Pragma: no-cache\r\n"
286                                          "Content-Length: 0\r\n\r\n";
287
288 /**
289  * http response boilerplate
290  */
291 static const char *http_response_template = "HTTP/1.1 200 OK\r\n"
292                                             "Date: %U GMT\r\n"
293                                             "Expires: %U GMT\r\n"
294                                             "Server: VPP Static\r\n"
295                                             "Content-Type: %s\r\n"
296                                             "Content-Length: %d\r\n\r\n";
297
298 static u32
299 send_data (http_conn_t *hc, u8 *data, u32 length, u32 offset)
300 {
301   const u32 max_burst = 64 << 10;
302   session_t *ts;
303   u32 to_send;
304   int sent;
305
306   ts = session_get_from_handle (hc->h_tc_session_handle);
307
308   to_send = clib_min (length - offset, max_burst);
309   sent = svm_fifo_enqueue (ts->tx_fifo, to_send, data + offset);
310
311   if (sent <= 0)
312     return offset;
313
314   if (svm_fifo_set_event (ts->tx_fifo))
315     session_send_io_evt_to_thread (ts->tx_fifo, SESSION_IO_EVT_TX);
316
317   return (offset + sent);
318 }
319
320 static void
321 send_error (http_conn_t *hc, http_status_code_t ec)
322 {
323   http_main_t *hm = &http_main;
324   u8 *data;
325   f64 now;
326
327   if (ec >= HTTP_N_STATUS)
328     ec = HTTP_STATUS_INTERNAL_ERROR;
329
330   now = clib_timebase_now (&hm->timebase);
331   data = format (0, http_error_template, http_status_code_str[ec],
332                  format_clib_timebase_time, now);
333   send_data (hc, data, vec_len (data), 0);
334   vec_free (data);
335 }
336
337 static int
338 read_request (http_conn_t *hc)
339 {
340   u32 max_deq, cursize;
341   session_t *ts;
342   int n_read;
343
344   ts = session_get_from_handle (hc->h_tc_session_handle);
345
346   cursize = vec_len (hc->rx_buf);
347   max_deq = svm_fifo_max_dequeue (ts->rx_fifo);
348   if (PREDICT_FALSE (max_deq == 0))
349     return -1;
350
351   vec_validate (hc->rx_buf, cursize + max_deq - 1);
352   n_read = svm_fifo_dequeue (ts->rx_fifo, max_deq, hc->rx_buf + cursize);
353   ASSERT (n_read == max_deq);
354
355   if (svm_fifo_is_empty (ts->rx_fifo))
356     svm_fifo_unset_event (ts->rx_fifo);
357
358   _vec_len (hc->rx_buf) = cursize + n_read;
359   return 0;
360 }
361
362 static int
363 v_find_index (u8 *vec, char *str)
364 {
365   int start_index;
366   u32 slen = (u32) strnlen_s_inline (str, 8);
367   u32 vlen = vec_len (vec);
368
369   ASSERT (slen > 0);
370
371   if (vlen <= slen)
372     return -1;
373
374   for (start_index = 0; start_index < (vlen - slen); start_index++)
375     {
376       if (!memcmp (vec, str, slen))
377         return start_index;
378     }
379
380   return -1;
381 }
382
383 /**
384  * waiting for request method from peer - parse request method and data
385  */
386 static int
387 state_wait_method (http_conn_t *hc, transport_send_params_t *sp)
388 {
389   http_main_t *hm = &http_main;
390   app_worker_t *app_wrk;
391   http_msg_t msg;
392   session_t *as;
393   int i, rv;
394   u32 len;
395   u8 *buf;
396
397   rv = read_request (hc);
398
399   /* Nothing yet, wait for data or timer expire */
400   if (rv)
401     return 0;
402
403   if (vec_len (hc->rx_buf) < 8)
404     {
405       send_error (hc, HTTP_STATUS_BAD_REQUEST);
406       http_disconnect_transport (hc);
407       return -1;
408     }
409
410   if ((i = v_find_index (hc->rx_buf, "GET ")) >= 0)
411     {
412       hc->method = HTTP_REQ_GET;
413       hc->rx_buf_offset = i + 5;
414     }
415   else if ((i = v_find_index (hc->rx_buf, "POST ")) >= 0)
416     {
417       hc->method = HTTP_REQ_POST;
418       hc->rx_buf_offset = i + 6;
419     }
420   else
421     {
422       if (hm->debug_level > 1)
423         clib_warning ("Unknown http method");
424
425       send_error (hc, HTTP_STATUS_METHOD_NOT_ALLOWED);
426       http_disconnect_transport (hc);
427       return -1;
428     }
429
430   buf = &hc->rx_buf[hc->rx_buf_offset];
431   len = vec_len (hc->rx_buf) - hc->rx_buf_offset;
432
433   msg.type = HTTP_MSG_REQUEST;
434   msg.data.content_type = HTTP_CONTENT_TEXT_HTML;
435   msg.data.len = len;
436   msg.data.offset = 0;
437
438   svm_fifo_seg_t segs[2] = { { (u8 *) &msg, sizeof (msg) }, { buf, len } };
439
440   as = session_get_from_handle (hc->h_pa_session_handle);
441   rv = svm_fifo_enqueue_segments (as->rx_fifo, segs, 2, 0 /* allow partial */);
442   if (rv < 0 || rv != sizeof (msg) + len)
443     {
444       clib_warning ("failed app enqueue");
445       /* This should not happen as we only handle 1 request per session,
446        * and fifo is allocated, but going forward we should consider
447        * rescheduling */
448       return -1;
449     }
450
451   vec_free (hc->rx_buf);
452   hc->req_state = HTTP_REQ_STATE_WAIT_APP;
453
454   app_wrk = app_worker_get_if_valid (as->app_wrk_index);
455   app_worker_lock_and_send_event (app_wrk, as, SESSION_IO_EVT_RX);
456
457   return 0;
458 }
459
460 /**
461  * waiting for data from app
462  */
463 static int
464 state_wait_app (http_conn_t *hc, transport_send_params_t *sp)
465 {
466   http_main_t *hm = &http_main;
467   http_status_code_t ec;
468   http_msg_t msg;
469   session_t *as;
470   u8 *header;
471   u32 offset;
472   f64 now;
473   int rv;
474
475   as = session_get_from_handle (hc->h_pa_session_handle);
476
477   rv = svm_fifo_dequeue (as->tx_fifo, sizeof (msg), (u8 *) &msg);
478   ASSERT (rv == sizeof (msg));
479
480   if (msg.type != HTTP_MSG_REPLY)
481     {
482       clib_warning ("unexpected msg type from app %u", msg.type);
483       ec = HTTP_STATUS_INTERNAL_ERROR;
484       goto error;
485     }
486
487   if (msg.code != HTTP_STATUS_OK)
488     {
489       ec = msg.code;
490       goto error;
491     }
492
493   http_buffer_init (&hc->tx_buf, as->tx_fifo, msg.data.len);
494
495   /*
496    * Add headers. For now:
497    * - current time
498    * - expiration time
499    * - content type
500    * - data length
501    */
502   now = clib_timebase_now (&hm->timebase);
503   header = format (0, http_response_template,
504                    /* Date */
505                    format_clib_timebase_time, now,
506                    /* Expires */
507                    format_clib_timebase_time, now + 600.0,
508                    /* Content type */
509                    http_content_type_str[msg.data.content_type],
510                    /* Length */
511                    msg.data.len);
512
513   offset = send_data (hc, header, vec_len (header), 0);
514   if (offset != vec_len (header))
515     {
516       clib_warning ("couldn't send response header!");
517       ec = HTTP_STATUS_INTERNAL_ERROR;
518       goto error;
519     }
520   vec_free (header);
521
522   /* Start sending the actual data */
523   hc->req_state = HTTP_REQ_STATE_SEND_MORE_DATA;
524
525   return 1;
526
527 error:
528
529   send_error (hc, ec);
530   hc->req_state = HTTP_REQ_STATE_WAIT_METHOD;
531   http_disconnect_transport (hc);
532
533   /* stop state machine processing */
534   return 0;
535 }
536
537 static int
538 state_send_more_data (http_conn_t *hc, transport_send_params_t *sp)
539 {
540   u32 max_send = 64 << 10, n_segs;
541   http_buffer_t *hb = &hc->tx_buf;
542   svm_fifo_seg_t *seg;
543   session_t *ts;
544   int sent = 0;
545
546   ts = session_get_from_handle (hc->h_tc_session_handle);
547   if ((seg = http_buffer_get_segs (hb, max_send, &n_segs)))
548     sent = svm_fifo_enqueue_segments (ts->tx_fifo, seg, n_segs,
549                                       1 /* allow partial */);
550
551   if (sent > 0)
552     {
553       http_buffer_drain (hb, sent);
554
555       /* Ask scheduler to notify app of deq event if needed */
556       sp->max_burst_size = sent;
557     }
558   else
559     {
560       sp->max_burst_size = 0;
561     }
562
563   /* Not finished sending all data */
564   if (!http_buffer_is_drained (hb))
565     {
566       if (svm_fifo_set_event (ts->tx_fifo))
567         session_send_io_evt_to_thread (ts->tx_fifo, SESSION_IO_EVT_TX);
568
569       if (svm_fifo_max_enqueue (ts->tx_fifo) < 16 << 10)
570         {
571           /* Deschedule http session and wait for deq notification if
572            * underlying ts tx fifo almost full */
573           svm_fifo_add_want_deq_ntf (ts->tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
574           transport_connection_deschedule (&hc->connection);
575           sp->flags |= TRANSPORT_SND_F_DESCHED;
576         }
577     }
578   else
579     {
580       if (svm_fifo_set_event (ts->tx_fifo))
581         session_send_io_evt_to_thread (ts->tx_fifo, SESSION_IO_EVT_TX_FLUSH);
582
583       /* Finished transaction, back to HTTP_REQ_STATE_WAIT_METHOD */
584       hc->req_state = HTTP_REQ_STATE_WAIT_METHOD;
585       http_buffer_free (&hc->tx_buf);
586     }
587
588   return 0;
589 }
590
591 typedef int (*http_sm_handler) (http_conn_t *, transport_send_params_t *sp);
592
593 static http_sm_handler req_state_funcs[HTTP_REQ_N_STATES] = {
594   /* Waiting for GET, POST, etc. */
595   state_wait_method,
596   /* Wait for data from app */
597   state_wait_app,
598   /* Send more data */
599   state_send_more_data,
600 };
601
602 static void
603 http_req_run_state_machine (http_conn_t *hc, transport_send_params_t *sp)
604 {
605   int rv;
606
607   do
608     {
609       rv = req_state_funcs[hc->req_state](hc, sp);
610       if (rv < 0)
611         return;
612     }
613   while (rv);
614
615   /* Reset the session expiration timer */
616   http_conn_timer_update (hc);
617 }
618
619 static int
620 http_ts_rx_callback (session_t *ts)
621 {
622   http_conn_t *hc;
623
624   hc = http_conn_get_w_thread (ts->opaque, ts->thread_index);
625
626   if (hc->req_state != HTTP_REQ_STATE_WAIT_METHOD)
627     {
628       clib_warning ("tcp data in req state %u", hc->req_state);
629       return 0;
630     }
631
632   http_req_run_state_machine (hc, 0);
633
634   if (hc->state == HTTP_CONN_STATE_TRANSPORT_CLOSED)
635     {
636       if (!svm_fifo_max_dequeue_cons (ts->rx_fifo))
637         session_transport_closing_notify (&hc->connection);
638     }
639   return 0;
640 }
641
642 int
643 http_ts_builtin_tx_callback (session_t *ts)
644 {
645   clib_warning ("called");
646   return 0;
647 }
648
649 static void
650 http_ts_cleanup_callback (session_t *ts, session_cleanup_ntf_t ntf)
651 {
652   http_conn_t *hc;
653
654   if (ntf == SESSION_CLEANUP_TRANSPORT)
655     return;
656
657   hc = http_conn_get_w_thread (ts->opaque, ts->thread_index);
658   if (!hc)
659     {
660       clib_warning ("no http connection for %u", ts->session_index);
661       return;
662     }
663
664   vec_free (hc->rx_buf);
665
666   http_buffer_free (&hc->tx_buf);
667   http_conn_timer_stop (hc);
668
669   session_transport_delete_notify (&hc->connection);
670   http_conn_free (hc);
671 }
672
673 int
674 http_add_segment_callback (u32 client_index, u64 segment_handle)
675 {
676   /* No-op for builtin */
677   return 0;
678 }
679
680 int
681 http_del_segment_callback (u32 client_index, u64 segment_handle)
682 {
683   return 0;
684 }
685
686 static session_cb_vft_t http_app_cb_vft = {
687   .session_accept_callback = http_ts_accept_callback,
688   .session_disconnect_callback = http_ts_disconnect_callback,
689   .session_connected_callback = http_ts_connected_callback,
690   .session_reset_callback = http_ts_reset_callback,
691   .session_cleanup_callback = http_ts_cleanup_callback,
692   .add_segment_callback = http_add_segment_callback,
693   .del_segment_callback = http_del_segment_callback,
694   .builtin_app_rx_callback = http_ts_rx_callback,
695   .builtin_app_tx_callback = http_ts_builtin_tx_callback,
696 };
697
698 static clib_error_t *
699 http_transport_enable (vlib_main_t *vm, u8 is_en)
700 {
701   u32 add_segment_size = 256 << 20, first_seg_size = 32 << 20;
702   vnet_app_detach_args_t _da, *da = &_da;
703   vnet_app_attach_args_t _a, *a = &_a;
704   u64 options[APP_OPTIONS_N_OPTIONS];
705   http_main_t *hm = &http_main;
706   u32 fifo_size = 128 << 12;
707
708   if (!is_en)
709     {
710       da->app_index = hm->app_index;
711       da->api_client_index = APP_INVALID_INDEX;
712       vnet_application_detach (da);
713       return 0;
714     }
715
716   vec_validate (hm->wrk, vlib_num_workers ());
717
718   first_seg_size = hm->first_seg_size ? hm->first_seg_size : first_seg_size;
719   fifo_size = hm->fifo_size ? hm->fifo_size : fifo_size;
720
721   clib_memset (a, 0, sizeof (*a));
722   clib_memset (options, 0, sizeof (options));
723
724   a->session_cb_vft = &http_app_cb_vft;
725   a->api_client_index = APP_INVALID_INDEX;
726   a->options = options;
727   a->name = format (0, "http");
728   a->options[APP_OPTIONS_SEGMENT_SIZE] = first_seg_size;
729   a->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = add_segment_size;
730   a->options[APP_OPTIONS_RX_FIFO_SIZE] = fifo_size;
731   a->options[APP_OPTIONS_TX_FIFO_SIZE] = fifo_size;
732   a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
733   a->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
734   a->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_IS_TRANSPORT_APP;
735
736   if (vnet_application_attach (a))
737     return clib_error_return (0, "failed to attach http app");
738
739   hm->app_index = a->app_index;
740   vec_free (a->name);
741
742   clib_timebase_init (&hm->timebase, 0 /* GMT */, CLIB_TIMEBASE_DAYLIGHT_NONE,
743                       &vm->clib_time /* share the system clock */);
744
745   http_timers_init (vm, http_conn_timeout_cb);
746
747   return 0;
748 }
749
750 static int
751 http_transport_connect (transport_endpoint_cfg_t *tep)
752 {
753   return -1;
754 }
755
756 static u32
757 http_start_listen (u32 app_listener_index, transport_endpoint_t *tep)
758 {
759   vnet_listen_args_t _args = {}, *args = &_args;
760   session_t *tc_listener, *app_listener;
761   http_main_t *hm = &http_main;
762   session_endpoint_cfg_t *sep;
763   app_worker_t *app_wrk;
764   transport_proto_t tp;
765   app_listener_t *al;
766   application_t *app;
767   http_conn_t *lhc;
768   u32 lhc_index;
769
770   sep = (session_endpoint_cfg_t *) tep;
771
772   app_wrk = app_worker_get (sep->app_wrk_index);
773   app = application_get (app_wrk->app_index);
774
775   args->app_index = hm->app_index;
776   args->sep_ext = *sep;
777   args->sep_ext.ns_index = app->ns_index;
778   tp = sep->ext_cfg ? TRANSPORT_PROTO_TLS : TRANSPORT_PROTO_TCP;
779   args->sep_ext.transport_proto = tp;
780
781   if (vnet_listen (args))
782     return SESSION_INVALID_INDEX;
783
784   lhc_index = http_listener_alloc ();
785   lhc = http_listener_get (lhc_index);
786
787   /* Grab transport connection listener and link to http listener */
788   lhc->h_tc_session_handle = args->handle;
789   al = app_listener_get_w_handle (lhc->h_tc_session_handle);
790   tc_listener = app_listener_get_session (al);
791   tc_listener->opaque = lhc_index;
792
793   /* Grab application listener and link to http listener */
794   app_listener = listen_session_get (app_listener_index);
795   lhc->h_pa_wrk_index = sep->app_wrk_index;
796   lhc->h_pa_session_handle = listen_session_get_handle (app_listener);
797   lhc->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
798
799   return lhc_index;
800 }
801
802 static void
803 http_transport_close (u32 hc_index, u32 thread_index)
804 {
805   session_t *as;
806   http_conn_t *hc;
807
808   HTTP_DBG (1, "App disconnecting %x", hc_index);
809
810   hc = http_conn_get_w_thread (hc_index, thread_index);
811   as = session_get_from_handle (hc->h_pa_session_handle);
812
813   /* Nothing more to send, confirm close */
814   if (!svm_fifo_max_dequeue_cons (as->tx_fifo))
815     {
816       session_transport_closed_notify (&hc->connection);
817       http_disconnect_transport (hc);
818     }
819   else
820     {
821       /* Wait for all data to be written to ts */
822       hc->state = HTTP_CONN_STATE_APP_CLOSED;
823     }
824 }
825
826 static transport_connection_t *
827 http_transport_get_connection (u32 hc_index, u32 thread_index)
828 {
829   http_conn_t *hc = http_conn_get_w_thread (hc_index, thread_index);
830   return &hc->connection;
831 }
832
833 static transport_connection_t *
834 http_transport_get_listener (u32 listener_index)
835 {
836   http_conn_t *lhc = http_listener_get (listener_index);
837   return &lhc->connection;
838 }
839
840 static int
841 http_app_tx_callback (void *session, transport_send_params_t *sp)
842 {
843   session_t *as = (session_t *) session;
844   http_conn_t *hc;
845
846   hc = http_conn_get_w_thread (as->connection_index, as->thread_index);
847   if (hc->req_state < HTTP_REQ_STATE_WAIT_APP)
848     {
849       clib_warning ("app data in req state %u", hc->req_state);
850       return 0;
851     }
852
853   http_req_run_state_machine (hc, sp);
854
855   if (hc->state == HTTP_CONN_STATE_CLOSED)
856     {
857       if (!svm_fifo_max_dequeue_cons (as->rx_fifo))
858         http_disconnect_transport (hc);
859     }
860   return 0;
861 }
862
863 static u8 *
864 format_http_connection (u8 *s, va_list *args)
865 {
866   http_conn_t *hc = va_arg (*args, http_conn_t *);
867   session_t *ts;
868
869   ts = session_get_from_handle (hc->h_tc_session_handle);
870   s = format (s, "[%d:%d][H] app_wrk %u ts %d:%d", hc->c_thread_index,
871               hc->c_s_index, hc->h_pa_wrk_index, ts->thread_index,
872               ts->session_index);
873
874   return s;
875 }
876
877 static u8 *
878 format_http_listener (u8 *s, va_list *args)
879 {
880   http_conn_t *lhc = va_arg (*args, http_conn_t *);
881   app_listener_t *al;
882   session_t *lts;
883
884   al = app_listener_get_w_handle (lhc->h_tc_session_handle);
885   lts = app_listener_get_session (al);
886   s = format (s, "[%d:%d][H] app_wrk %u ts %d:%d", lhc->c_thread_index,
887               lhc->c_s_index, lhc->h_pa_wrk_index, lts->thread_index,
888               lts->session_index);
889
890   return s;
891 }
892
893 static u8 *
894 format_http_conn_state (u8 *s, va_list *args)
895 {
896   http_conn_t *hc = va_arg (*args, http_conn_t *);
897
898   switch (hc->state)
899     {
900     case HTTP_CONN_STATE_LISTEN:
901       s = format (s, "LISTEN");
902       break;
903     case HTTP_CONN_STATE_CONNECTING:
904       s = format (s, "CONNECTING");
905       break;
906     case HTTP_CONN_STATE_ESTABLISHED:
907       s = format (s, "ESTABLISHED");
908       break;
909     case HTTP_CONN_STATE_TRANSPORT_CLOSED:
910       s = format (s, "TRANSPORT_CLOSED");
911       break;
912     case HTTP_CONN_STATE_APP_CLOSED:
913       s = format (s, "APP_CLOSED");
914       break;
915     case HTTP_CONN_STATE_CLOSED:
916       s = format (s, "CLOSED");
917       break;
918     }
919
920   return s;
921 }
922
923 static u8 *
924 format_http_transport_connection (u8 *s, va_list *args)
925 {
926   u32 tc_index = va_arg (*args, u32);
927   u32 thread_index = va_arg (*args, u32);
928   u32 verbose = va_arg (*args, u32);
929   http_conn_t *hc;
930
931   hc = http_conn_get_w_thread (tc_index, thread_index);
932
933   s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_http_connection, hc);
934   if (verbose)
935     {
936       s =
937         format (s, "%-" SESSION_CLI_STATE_LEN "U", format_http_conn_state, hc);
938       if (verbose > 1)
939         s = format (s, "\n");
940     }
941
942   return s;
943 }
944
945 static u8 *
946 format_http_transport_listener (u8 *s, va_list *args)
947 {
948   u32 tc_index = va_arg (*args, u32);
949   u32 __clib_unused thread_index = va_arg (*args, u32);
950   u32 __clib_unused verbose = va_arg (*args, u32);
951   http_conn_t *lhc = http_listener_get (tc_index);
952
953   s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_http_listener, lhc);
954   if (verbose)
955     s =
956       format (s, "%-" SESSION_CLI_STATE_LEN "U", format_http_conn_state, lhc);
957   return s;
958 }
959
960 static const transport_proto_vft_t http_proto = {
961   .enable = http_transport_enable,
962   .connect = http_transport_connect,
963   .start_listen = http_start_listen,
964   .close = http_transport_close,
965   .custom_tx = http_app_tx_callback,
966   .get_connection = http_transport_get_connection,
967   .get_listener = http_transport_get_listener,
968   .format_connection = format_http_transport_connection,
969   .format_listener = format_http_transport_listener,
970   .transport_options = {
971     .name = "http",
972     .short_name = "H",
973     .tx_type = TRANSPORT_TX_INTERNAL,
974     .service_type = TRANSPORT_SERVICE_APP,
975   },
976 };
977
978 static clib_error_t *
979 http_transport_init (vlib_main_t *vm)
980 {
981   transport_register_protocol (TRANSPORT_PROTO_HTTP, &http_proto,
982                                FIB_PROTOCOL_IP4, ~0);
983   transport_register_protocol (TRANSPORT_PROTO_HTTP, &http_proto,
984                                FIB_PROTOCOL_IP6, ~0);
985   return 0;
986 }
987
988 VLIB_INIT_FUNCTION (http_transport_init);
989
990 VLIB_PLUGIN_REGISTER () = {
991   .version = VPP_BUILD_VER,
992   .description = "Hypertext Transfer Protocol (HTTP)",
993   .default_disabled = 0,
994 };
995
996 /*
997  * fd.io coding-style-patch-verification: ON
998  *
999  * Local Variables:
1000  * eval: (c-set-style "gnu")
1001  * End:
1002  */