http: fix rescheduling when transport fifo full
[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, u32 offset, char *str)
364 {
365   int start_index = offset;
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 < (vlen - slen); start_index++)
375     {
376       if (!memcmp (vec + start_index, 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_status_code_t ec;
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       ec = HTTP_STATUS_BAD_REQUEST;
406       goto error;
407     }
408
409   if ((i = v_find_index (hc->rx_buf, 0, "GET ")) >= 0)
410     {
411       hc->method = HTTP_REQ_GET;
412       hc->rx_buf_offset = i + 5;
413
414       i = v_find_index (hc->rx_buf, hc->rx_buf_offset, "HTTP");
415       if (i < 0)
416         {
417           ec = HTTP_STATUS_BAD_REQUEST;
418           goto error;
419         }
420
421       len = i - hc->rx_buf_offset;
422     }
423   else if ((i = v_find_index (hc->rx_buf, 0, "POST ")) >= 0)
424     {
425       hc->method = HTTP_REQ_POST;
426       hc->rx_buf_offset = i + 6;
427       len = vec_len (hc->rx_buf) - hc->rx_buf_offset;
428     }
429   else
430     {
431       HTTP_DBG (0, "Unknown http method");
432       ec = HTTP_STATUS_METHOD_NOT_ALLOWED;
433       goto error;
434     }
435
436   buf = &hc->rx_buf[hc->rx_buf_offset];
437
438   msg.type = HTTP_MSG_REQUEST;
439   msg.method_type = hc->method;
440   msg.data.content_type = HTTP_CONTENT_TEXT_HTML;
441   msg.data.len = len;
442   msg.data.offset = 0;
443
444   svm_fifo_seg_t segs[2] = { { (u8 *) &msg, sizeof (msg) }, { buf, len } };
445
446   as = session_get_from_handle (hc->h_pa_session_handle);
447   rv = svm_fifo_enqueue_segments (as->rx_fifo, segs, 2, 0 /* allow partial */);
448   if (rv < 0 || rv != sizeof (msg) + len)
449     {
450       clib_warning ("failed app enqueue");
451       /* This should not happen as we only handle 1 request per session,
452        * and fifo is allocated, but going forward we should consider
453        * rescheduling */
454       return -1;
455     }
456
457   vec_free (hc->rx_buf);
458   hc->req_state = HTTP_REQ_STATE_WAIT_APP;
459
460   app_wrk = app_worker_get_if_valid (as->app_wrk_index);
461   app_worker_lock_and_send_event (app_wrk, as, SESSION_IO_EVT_RX);
462
463   return 0;
464
465 error:
466
467   send_error (hc, ec);
468   session_transport_closing_notify (&hc->connection);
469   http_disconnect_transport (hc);
470
471   return -1;
472 }
473
474 /**
475  * waiting for data from app
476  */
477 static int
478 state_wait_app (http_conn_t *hc, transport_send_params_t *sp)
479 {
480   http_main_t *hm = &http_main;
481   http_status_code_t ec;
482   http_msg_t msg;
483   session_t *as;
484   u8 *header;
485   u32 offset;
486   f64 now;
487   int rv;
488
489   as = session_get_from_handle (hc->h_pa_session_handle);
490
491   rv = svm_fifo_dequeue (as->tx_fifo, sizeof (msg), (u8 *) &msg);
492   ASSERT (rv == sizeof (msg));
493
494   if (msg.type != HTTP_MSG_REPLY)
495     {
496       clib_warning ("unexpected msg type from app %u", msg.type);
497       ec = HTTP_STATUS_INTERNAL_ERROR;
498       goto error;
499     }
500
501   if (msg.code != HTTP_STATUS_OK)
502     {
503       ec = msg.code;
504       goto error;
505     }
506
507   http_buffer_init (&hc->tx_buf, as->tx_fifo, msg.data.len);
508
509   /*
510    * Add headers. For now:
511    * - current time
512    * - expiration time
513    * - content type
514    * - data length
515    */
516   now = clib_timebase_now (&hm->timebase);
517   header = format (0, http_response_template,
518                    /* Date */
519                    format_clib_timebase_time, now,
520                    /* Expires */
521                    format_clib_timebase_time, now + 600.0,
522                    /* Content type */
523                    http_content_type_str[msg.data.content_type],
524                    /* Length */
525                    msg.data.len);
526
527   offset = send_data (hc, header, vec_len (header), 0);
528   if (offset != vec_len (header))
529     {
530       clib_warning ("couldn't send response header!");
531       ec = HTTP_STATUS_INTERNAL_ERROR;
532       goto error;
533     }
534   vec_free (header);
535
536   /* Start sending the actual data */
537   hc->req_state = HTTP_REQ_STATE_SEND_MORE_DATA;
538
539   return 1;
540
541 error:
542
543   send_error (hc, ec);
544   hc->req_state = HTTP_REQ_STATE_WAIT_METHOD;
545   session_transport_closing_notify (&hc->connection);
546   http_disconnect_transport (hc);
547
548   /* stop state machine processing */
549   return 0;
550 }
551
552 static int
553 state_send_more_data (http_conn_t *hc, transport_send_params_t *sp)
554 {
555   u32 max_send = 64 << 10, n_segs;
556   http_buffer_t *hb = &hc->tx_buf;
557   svm_fifo_seg_t *seg;
558   session_t *ts;
559   int sent = 0;
560
561   ts = session_get_from_handle (hc->h_tc_session_handle);
562   if ((seg = http_buffer_get_segs (hb, max_send, &n_segs)))
563     sent = svm_fifo_enqueue_segments (ts->tx_fifo, seg, n_segs,
564                                       1 /* allow partial */);
565
566   if (sent > 0)
567     {
568       http_buffer_drain (hb, sent);
569
570       /* Ask scheduler to notify app of deq event if needed */
571       sp->max_burst_size = sent;
572     }
573   else
574     {
575       sp->max_burst_size = 0;
576     }
577
578   /* Not finished sending all data */
579   if (!http_buffer_is_drained (hb))
580     {
581       if (sent && svm_fifo_set_event (ts->tx_fifo))
582         session_send_io_evt_to_thread (ts->tx_fifo, SESSION_IO_EVT_TX);
583
584       if (svm_fifo_max_enqueue (ts->tx_fifo) < 16 << 10)
585         {
586           /* Deschedule http session and wait for deq notification if
587            * underlying ts tx fifo almost full */
588           svm_fifo_add_want_deq_ntf (ts->tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
589           transport_connection_deschedule (&hc->connection);
590           sp->flags |= TRANSPORT_SND_F_DESCHED;
591         }
592     }
593   else
594     {
595       if (sent && svm_fifo_set_event (ts->tx_fifo))
596         session_send_io_evt_to_thread (ts->tx_fifo, SESSION_IO_EVT_TX_FLUSH);
597
598       /* Finished transaction, back to HTTP_REQ_STATE_WAIT_METHOD */
599       hc->req_state = HTTP_REQ_STATE_WAIT_METHOD;
600       http_buffer_free (&hc->tx_buf);
601     }
602
603   return 0;
604 }
605
606 typedef int (*http_sm_handler) (http_conn_t *, transport_send_params_t *sp);
607
608 static http_sm_handler req_state_funcs[HTTP_REQ_N_STATES] = {
609   /* Waiting for GET, POST, etc. */
610   state_wait_method,
611   /* Wait for data from app */
612   state_wait_app,
613   /* Send more data */
614   state_send_more_data,
615 };
616
617 static void
618 http_req_run_state_machine (http_conn_t *hc, transport_send_params_t *sp)
619 {
620   int rv;
621
622   do
623     {
624       rv = req_state_funcs[hc->req_state](hc, sp);
625       if (rv < 0)
626         return;
627     }
628   while (rv);
629
630   /* Reset the session expiration timer */
631   http_conn_timer_update (hc);
632 }
633
634 static int
635 http_ts_rx_callback (session_t *ts)
636 {
637   http_conn_t *hc;
638
639   hc = http_conn_get_w_thread (ts->opaque, ts->thread_index);
640
641   if (hc->req_state != HTTP_REQ_STATE_WAIT_METHOD)
642     {
643       clib_warning ("tcp data in req state %u", hc->req_state);
644       return 0;
645     }
646
647   http_req_run_state_machine (hc, 0);
648
649   if (hc->state == HTTP_CONN_STATE_TRANSPORT_CLOSED)
650     {
651       if (!svm_fifo_max_dequeue_cons (ts->rx_fifo))
652         session_transport_closing_notify (&hc->connection);
653     }
654   return 0;
655 }
656
657 int
658 http_ts_builtin_tx_callback (session_t *ts)
659 {
660   http_conn_t *hc;
661
662   hc = http_conn_get_w_thread (ts->opaque, ts->thread_index);
663   transport_connection_reschedule (&hc->connection);
664
665   return 0;
666 }
667
668 static void
669 http_ts_cleanup_callback (session_t *ts, session_cleanup_ntf_t ntf)
670 {
671   http_conn_t *hc;
672
673   if (ntf == SESSION_CLEANUP_TRANSPORT)
674     return;
675
676   hc = http_conn_get_w_thread (ts->opaque, ts->thread_index);
677   if (!hc)
678     {
679       clib_warning ("no http connection for %u", ts->session_index);
680       return;
681     }
682
683   vec_free (hc->rx_buf);
684
685   http_buffer_free (&hc->tx_buf);
686   http_conn_timer_stop (hc);
687
688   session_transport_delete_notify (&hc->connection);
689   http_conn_free (hc);
690 }
691
692 int
693 http_add_segment_callback (u32 client_index, u64 segment_handle)
694 {
695   /* No-op for builtin */
696   return 0;
697 }
698
699 int
700 http_del_segment_callback (u32 client_index, u64 segment_handle)
701 {
702   return 0;
703 }
704
705 static session_cb_vft_t http_app_cb_vft = {
706   .session_accept_callback = http_ts_accept_callback,
707   .session_disconnect_callback = http_ts_disconnect_callback,
708   .session_connected_callback = http_ts_connected_callback,
709   .session_reset_callback = http_ts_reset_callback,
710   .session_cleanup_callback = http_ts_cleanup_callback,
711   .add_segment_callback = http_add_segment_callback,
712   .del_segment_callback = http_del_segment_callback,
713   .builtin_app_rx_callback = http_ts_rx_callback,
714   .builtin_app_tx_callback = http_ts_builtin_tx_callback,
715 };
716
717 static clib_error_t *
718 http_transport_enable (vlib_main_t *vm, u8 is_en)
719 {
720   u32 add_segment_size = 256 << 20, first_seg_size = 32 << 20;
721   vnet_app_detach_args_t _da, *da = &_da;
722   vnet_app_attach_args_t _a, *a = &_a;
723   u64 options[APP_OPTIONS_N_OPTIONS];
724   http_main_t *hm = &http_main;
725   u32 fifo_size = 128 << 12;
726
727   if (!is_en)
728     {
729       da->app_index = hm->app_index;
730       da->api_client_index = APP_INVALID_INDEX;
731       vnet_application_detach (da);
732       return 0;
733     }
734
735   vec_validate (hm->wrk, vlib_num_workers ());
736
737   first_seg_size = hm->first_seg_size ? hm->first_seg_size : first_seg_size;
738   fifo_size = hm->fifo_size ? hm->fifo_size : fifo_size;
739
740   clib_memset (a, 0, sizeof (*a));
741   clib_memset (options, 0, sizeof (options));
742
743   a->session_cb_vft = &http_app_cb_vft;
744   a->api_client_index = APP_INVALID_INDEX;
745   a->options = options;
746   a->name = format (0, "http");
747   a->options[APP_OPTIONS_SEGMENT_SIZE] = first_seg_size;
748   a->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = add_segment_size;
749   a->options[APP_OPTIONS_RX_FIFO_SIZE] = fifo_size;
750   a->options[APP_OPTIONS_TX_FIFO_SIZE] = fifo_size;
751   a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
752   a->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
753   a->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_IS_TRANSPORT_APP;
754
755   if (vnet_application_attach (a))
756     return clib_error_return (0, "failed to attach http app");
757
758   hm->app_index = a->app_index;
759   vec_free (a->name);
760
761   clib_timebase_init (&hm->timebase, 0 /* GMT */, CLIB_TIMEBASE_DAYLIGHT_NONE,
762                       &vm->clib_time /* share the system clock */);
763
764   http_timers_init (vm, http_conn_timeout_cb);
765
766   return 0;
767 }
768
769 static int
770 http_transport_connect (transport_endpoint_cfg_t *tep)
771 {
772   return -1;
773 }
774
775 static u32
776 http_start_listen (u32 app_listener_index, transport_endpoint_t *tep)
777 {
778   vnet_listen_args_t _args = {}, *args = &_args;
779   session_t *tc_listener, *app_listener;
780   http_main_t *hm = &http_main;
781   session_endpoint_cfg_t *sep;
782   app_worker_t *app_wrk;
783   transport_proto_t tp;
784   app_listener_t *al;
785   application_t *app;
786   http_conn_t *lhc;
787   u32 lhc_index;
788
789   sep = (session_endpoint_cfg_t *) tep;
790
791   app_wrk = app_worker_get (sep->app_wrk_index);
792   app = application_get (app_wrk->app_index);
793
794   args->app_index = hm->app_index;
795   args->sep_ext = *sep;
796   args->sep_ext.ns_index = app->ns_index;
797   tp = sep->ext_cfg ? TRANSPORT_PROTO_TLS : TRANSPORT_PROTO_TCP;
798   args->sep_ext.transport_proto = tp;
799
800   if (vnet_listen (args))
801     return SESSION_INVALID_INDEX;
802
803   lhc_index = http_listener_alloc ();
804   lhc = http_listener_get (lhc_index);
805
806   /* Grab transport connection listener and link to http listener */
807   lhc->h_tc_session_handle = args->handle;
808   al = app_listener_get_w_handle (lhc->h_tc_session_handle);
809   tc_listener = app_listener_get_session (al);
810   tc_listener->opaque = lhc_index;
811
812   /* Grab application listener and link to http listener */
813   app_listener = listen_session_get (app_listener_index);
814   lhc->h_pa_wrk_index = sep->app_wrk_index;
815   lhc->h_pa_session_handle = listen_session_get_handle (app_listener);
816   lhc->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
817
818   return lhc_index;
819 }
820
821 static void
822 http_transport_close (u32 hc_index, u32 thread_index)
823 {
824   session_t *as;
825   http_conn_t *hc;
826
827   HTTP_DBG (1, "App disconnecting %x", hc_index);
828
829   hc = http_conn_get_w_thread (hc_index, thread_index);
830   as = session_get_from_handle (hc->h_pa_session_handle);
831
832   /* Nothing more to send, confirm close */
833   if (!svm_fifo_max_dequeue_cons (as->tx_fifo))
834     {
835       session_transport_closed_notify (&hc->connection);
836       http_disconnect_transport (hc);
837     }
838   else
839     {
840       /* Wait for all data to be written to ts */
841       hc->state = HTTP_CONN_STATE_APP_CLOSED;
842     }
843 }
844
845 static transport_connection_t *
846 http_transport_get_connection (u32 hc_index, u32 thread_index)
847 {
848   http_conn_t *hc = http_conn_get_w_thread (hc_index, thread_index);
849   return &hc->connection;
850 }
851
852 static transport_connection_t *
853 http_transport_get_listener (u32 listener_index)
854 {
855   http_conn_t *lhc = http_listener_get (listener_index);
856   return &lhc->connection;
857 }
858
859 static int
860 http_app_tx_callback (void *session, transport_send_params_t *sp)
861 {
862   session_t *as = (session_t *) session;
863   http_conn_t *hc;
864
865   sp->flags = 0;
866
867   hc = http_conn_get_w_thread (as->connection_index, as->thread_index);
868   if (hc->req_state < HTTP_REQ_STATE_WAIT_APP)
869     {
870       clib_warning ("app data in req state %u", hc->req_state);
871       return 0;
872     }
873
874   http_req_run_state_machine (hc, sp);
875
876   if (hc->state == HTTP_CONN_STATE_CLOSED)
877     {
878       if (!svm_fifo_max_dequeue_cons (as->rx_fifo))
879         http_disconnect_transport (hc);
880     }
881   return 0;
882 }
883
884 static u8 *
885 format_http_connection (u8 *s, va_list *args)
886 {
887   http_conn_t *hc = va_arg (*args, http_conn_t *);
888   session_t *ts;
889
890   ts = session_get_from_handle (hc->h_tc_session_handle);
891   s = format (s, "[%d:%d][H] app_wrk %u ts %d:%d", hc->c_thread_index,
892               hc->c_s_index, hc->h_pa_wrk_index, ts->thread_index,
893               ts->session_index);
894
895   return s;
896 }
897
898 static u8 *
899 format_http_listener (u8 *s, va_list *args)
900 {
901   http_conn_t *lhc = va_arg (*args, http_conn_t *);
902   app_listener_t *al;
903   session_t *lts;
904
905   al = app_listener_get_w_handle (lhc->h_tc_session_handle);
906   lts = app_listener_get_session (al);
907   s = format (s, "[%d:%d][H] app_wrk %u ts %d:%d", lhc->c_thread_index,
908               lhc->c_s_index, lhc->h_pa_wrk_index, lts->thread_index,
909               lts->session_index);
910
911   return s;
912 }
913
914 static u8 *
915 format_http_conn_state (u8 *s, va_list *args)
916 {
917   http_conn_t *hc = va_arg (*args, http_conn_t *);
918
919   switch (hc->state)
920     {
921     case HTTP_CONN_STATE_LISTEN:
922       s = format (s, "LISTEN");
923       break;
924     case HTTP_CONN_STATE_CONNECTING:
925       s = format (s, "CONNECTING");
926       break;
927     case HTTP_CONN_STATE_ESTABLISHED:
928       s = format (s, "ESTABLISHED");
929       break;
930     case HTTP_CONN_STATE_TRANSPORT_CLOSED:
931       s = format (s, "TRANSPORT_CLOSED");
932       break;
933     case HTTP_CONN_STATE_APP_CLOSED:
934       s = format (s, "APP_CLOSED");
935       break;
936     case HTTP_CONN_STATE_CLOSED:
937       s = format (s, "CLOSED");
938       break;
939     }
940
941   return s;
942 }
943
944 static u8 *
945 format_http_transport_connection (u8 *s, va_list *args)
946 {
947   u32 tc_index = va_arg (*args, u32);
948   u32 thread_index = va_arg (*args, u32);
949   u32 verbose = va_arg (*args, u32);
950   http_conn_t *hc;
951
952   hc = http_conn_get_w_thread (tc_index, thread_index);
953
954   s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_http_connection, hc);
955   if (verbose)
956     {
957       s =
958         format (s, "%-" SESSION_CLI_STATE_LEN "U", format_http_conn_state, hc);
959       if (verbose > 1)
960         s = format (s, "\n");
961     }
962
963   return s;
964 }
965
966 static u8 *
967 format_http_transport_listener (u8 *s, va_list *args)
968 {
969   u32 tc_index = va_arg (*args, u32);
970   u32 __clib_unused thread_index = va_arg (*args, u32);
971   u32 __clib_unused verbose = va_arg (*args, u32);
972   http_conn_t *lhc = http_listener_get (tc_index);
973
974   s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_http_listener, lhc);
975   if (verbose)
976     s =
977       format (s, "%-" SESSION_CLI_STATE_LEN "U", format_http_conn_state, lhc);
978   return s;
979 }
980
981 static const transport_proto_vft_t http_proto = {
982   .enable = http_transport_enable,
983   .connect = http_transport_connect,
984   .start_listen = http_start_listen,
985   .close = http_transport_close,
986   .custom_tx = http_app_tx_callback,
987   .get_connection = http_transport_get_connection,
988   .get_listener = http_transport_get_listener,
989   .format_connection = format_http_transport_connection,
990   .format_listener = format_http_transport_listener,
991   .transport_options = {
992     .name = "http",
993     .short_name = "H",
994     .tx_type = TRANSPORT_TX_INTERNAL,
995     .service_type = TRANSPORT_SERVICE_APP,
996   },
997 };
998
999 static clib_error_t *
1000 http_transport_init (vlib_main_t *vm)
1001 {
1002   transport_register_protocol (TRANSPORT_PROTO_HTTP, &http_proto,
1003                                FIB_PROTOCOL_IP4, ~0);
1004   transport_register_protocol (TRANSPORT_PROTO_HTTP, &http_proto,
1005                                FIB_PROTOCOL_IP6, ~0);
1006   return 0;
1007 }
1008
1009 VLIB_INIT_FUNCTION (http_transport_init);
1010
1011 VLIB_PLUGIN_REGISTER () = {
1012   .version = VPP_BUILD_VER,
1013   .description = "Hypertext Transfer Protocol (HTTP)",
1014   .default_disabled = 0,
1015 };
1016
1017 /*
1018  * fd.io coding-style-patch-verification: ON
1019  *
1020  * Local Variables:
1021  * eval: (c-set-style "gnu")
1022  * End:
1023  */