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