hsa: refactor test http server to use http proto
[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 (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 (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   clib_warning ("called");
661   return 0;
662 }
663
664 static void
665 http_ts_cleanup_callback (session_t *ts, session_cleanup_ntf_t ntf)
666 {
667   http_conn_t *hc;
668
669   if (ntf == SESSION_CLEANUP_TRANSPORT)
670     return;
671
672   hc = http_conn_get_w_thread (ts->opaque, ts->thread_index);
673   if (!hc)
674     {
675       clib_warning ("no http connection for %u", ts->session_index);
676       return;
677     }
678
679   vec_free (hc->rx_buf);
680
681   http_buffer_free (&hc->tx_buf);
682   http_conn_timer_stop (hc);
683
684   session_transport_delete_notify (&hc->connection);
685   http_conn_free (hc);
686 }
687
688 int
689 http_add_segment_callback (u32 client_index, u64 segment_handle)
690 {
691   /* No-op for builtin */
692   return 0;
693 }
694
695 int
696 http_del_segment_callback (u32 client_index, u64 segment_handle)
697 {
698   return 0;
699 }
700
701 static session_cb_vft_t http_app_cb_vft = {
702   .session_accept_callback = http_ts_accept_callback,
703   .session_disconnect_callback = http_ts_disconnect_callback,
704   .session_connected_callback = http_ts_connected_callback,
705   .session_reset_callback = http_ts_reset_callback,
706   .session_cleanup_callback = http_ts_cleanup_callback,
707   .add_segment_callback = http_add_segment_callback,
708   .del_segment_callback = http_del_segment_callback,
709   .builtin_app_rx_callback = http_ts_rx_callback,
710   .builtin_app_tx_callback = http_ts_builtin_tx_callback,
711 };
712
713 static clib_error_t *
714 http_transport_enable (vlib_main_t *vm, u8 is_en)
715 {
716   u32 add_segment_size = 256 << 20, first_seg_size = 32 << 20;
717   vnet_app_detach_args_t _da, *da = &_da;
718   vnet_app_attach_args_t _a, *a = &_a;
719   u64 options[APP_OPTIONS_N_OPTIONS];
720   http_main_t *hm = &http_main;
721   u32 fifo_size = 128 << 12;
722
723   if (!is_en)
724     {
725       da->app_index = hm->app_index;
726       da->api_client_index = APP_INVALID_INDEX;
727       vnet_application_detach (da);
728       return 0;
729     }
730
731   vec_validate (hm->wrk, vlib_num_workers ());
732
733   first_seg_size = hm->first_seg_size ? hm->first_seg_size : first_seg_size;
734   fifo_size = hm->fifo_size ? hm->fifo_size : fifo_size;
735
736   clib_memset (a, 0, sizeof (*a));
737   clib_memset (options, 0, sizeof (options));
738
739   a->session_cb_vft = &http_app_cb_vft;
740   a->api_client_index = APP_INVALID_INDEX;
741   a->options = options;
742   a->name = format (0, "http");
743   a->options[APP_OPTIONS_SEGMENT_SIZE] = first_seg_size;
744   a->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = add_segment_size;
745   a->options[APP_OPTIONS_RX_FIFO_SIZE] = fifo_size;
746   a->options[APP_OPTIONS_TX_FIFO_SIZE] = fifo_size;
747   a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
748   a->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
749   a->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_IS_TRANSPORT_APP;
750
751   if (vnet_application_attach (a))
752     return clib_error_return (0, "failed to attach http app");
753
754   hm->app_index = a->app_index;
755   vec_free (a->name);
756
757   clib_timebase_init (&hm->timebase, 0 /* GMT */, CLIB_TIMEBASE_DAYLIGHT_NONE,
758                       &vm->clib_time /* share the system clock */);
759
760   http_timers_init (vm, http_conn_timeout_cb);
761
762   return 0;
763 }
764
765 static int
766 http_transport_connect (transport_endpoint_cfg_t *tep)
767 {
768   return -1;
769 }
770
771 static u32
772 http_start_listen (u32 app_listener_index, transport_endpoint_t *tep)
773 {
774   vnet_listen_args_t _args = {}, *args = &_args;
775   session_t *tc_listener, *app_listener;
776   http_main_t *hm = &http_main;
777   session_endpoint_cfg_t *sep;
778   app_worker_t *app_wrk;
779   transport_proto_t tp;
780   app_listener_t *al;
781   application_t *app;
782   http_conn_t *lhc;
783   u32 lhc_index;
784
785   sep = (session_endpoint_cfg_t *) tep;
786
787   app_wrk = app_worker_get (sep->app_wrk_index);
788   app = application_get (app_wrk->app_index);
789
790   args->app_index = hm->app_index;
791   args->sep_ext = *sep;
792   args->sep_ext.ns_index = app->ns_index;
793   tp = sep->ext_cfg ? TRANSPORT_PROTO_TLS : TRANSPORT_PROTO_TCP;
794   args->sep_ext.transport_proto = tp;
795
796   if (vnet_listen (args))
797     return SESSION_INVALID_INDEX;
798
799   lhc_index = http_listener_alloc ();
800   lhc = http_listener_get (lhc_index);
801
802   /* Grab transport connection listener and link to http listener */
803   lhc->h_tc_session_handle = args->handle;
804   al = app_listener_get_w_handle (lhc->h_tc_session_handle);
805   tc_listener = app_listener_get_session (al);
806   tc_listener->opaque = lhc_index;
807
808   /* Grab application listener and link to http listener */
809   app_listener = listen_session_get (app_listener_index);
810   lhc->h_pa_wrk_index = sep->app_wrk_index;
811   lhc->h_pa_session_handle = listen_session_get_handle (app_listener);
812   lhc->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
813
814   return lhc_index;
815 }
816
817 static void
818 http_transport_close (u32 hc_index, u32 thread_index)
819 {
820   session_t *as;
821   http_conn_t *hc;
822
823   HTTP_DBG (1, "App disconnecting %x", hc_index);
824
825   hc = http_conn_get_w_thread (hc_index, thread_index);
826   as = session_get_from_handle (hc->h_pa_session_handle);
827
828   /* Nothing more to send, confirm close */
829   if (!svm_fifo_max_dequeue_cons (as->tx_fifo))
830     {
831       session_transport_closed_notify (&hc->connection);
832       http_disconnect_transport (hc);
833     }
834   else
835     {
836       /* Wait for all data to be written to ts */
837       hc->state = HTTP_CONN_STATE_APP_CLOSED;
838     }
839 }
840
841 static transport_connection_t *
842 http_transport_get_connection (u32 hc_index, u32 thread_index)
843 {
844   http_conn_t *hc = http_conn_get_w_thread (hc_index, thread_index);
845   return &hc->connection;
846 }
847
848 static transport_connection_t *
849 http_transport_get_listener (u32 listener_index)
850 {
851   http_conn_t *lhc = http_listener_get (listener_index);
852   return &lhc->connection;
853 }
854
855 static int
856 http_app_tx_callback (void *session, transport_send_params_t *sp)
857 {
858   session_t *as = (session_t *) session;
859   http_conn_t *hc;
860
861   hc = http_conn_get_w_thread (as->connection_index, as->thread_index);
862   if (hc->req_state < HTTP_REQ_STATE_WAIT_APP)
863     {
864       clib_warning ("app data in req state %u", hc->req_state);
865       return 0;
866     }
867
868   http_req_run_state_machine (hc, sp);
869
870   if (hc->state == HTTP_CONN_STATE_CLOSED)
871     {
872       if (!svm_fifo_max_dequeue_cons (as->rx_fifo))
873         http_disconnect_transport (hc);
874     }
875   return 0;
876 }
877
878 static u8 *
879 format_http_connection (u8 *s, va_list *args)
880 {
881   http_conn_t *hc = va_arg (*args, http_conn_t *);
882   session_t *ts;
883
884   ts = session_get_from_handle (hc->h_tc_session_handle);
885   s = format (s, "[%d:%d][H] app_wrk %u ts %d:%d", hc->c_thread_index,
886               hc->c_s_index, hc->h_pa_wrk_index, ts->thread_index,
887               ts->session_index);
888
889   return s;
890 }
891
892 static u8 *
893 format_http_listener (u8 *s, va_list *args)
894 {
895   http_conn_t *lhc = va_arg (*args, http_conn_t *);
896   app_listener_t *al;
897   session_t *lts;
898
899   al = app_listener_get_w_handle (lhc->h_tc_session_handle);
900   lts = app_listener_get_session (al);
901   s = format (s, "[%d:%d][H] app_wrk %u ts %d:%d", lhc->c_thread_index,
902               lhc->c_s_index, lhc->h_pa_wrk_index, lts->thread_index,
903               lts->session_index);
904
905   return s;
906 }
907
908 static u8 *
909 format_http_conn_state (u8 *s, va_list *args)
910 {
911   http_conn_t *hc = va_arg (*args, http_conn_t *);
912
913   switch (hc->state)
914     {
915     case HTTP_CONN_STATE_LISTEN:
916       s = format (s, "LISTEN");
917       break;
918     case HTTP_CONN_STATE_CONNECTING:
919       s = format (s, "CONNECTING");
920       break;
921     case HTTP_CONN_STATE_ESTABLISHED:
922       s = format (s, "ESTABLISHED");
923       break;
924     case HTTP_CONN_STATE_TRANSPORT_CLOSED:
925       s = format (s, "TRANSPORT_CLOSED");
926       break;
927     case HTTP_CONN_STATE_APP_CLOSED:
928       s = format (s, "APP_CLOSED");
929       break;
930     case HTTP_CONN_STATE_CLOSED:
931       s = format (s, "CLOSED");
932       break;
933     }
934
935   return s;
936 }
937
938 static u8 *
939 format_http_transport_connection (u8 *s, va_list *args)
940 {
941   u32 tc_index = va_arg (*args, u32);
942   u32 thread_index = va_arg (*args, u32);
943   u32 verbose = va_arg (*args, u32);
944   http_conn_t *hc;
945
946   hc = http_conn_get_w_thread (tc_index, thread_index);
947
948   s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_http_connection, hc);
949   if (verbose)
950     {
951       s =
952         format (s, "%-" SESSION_CLI_STATE_LEN "U", format_http_conn_state, hc);
953       if (verbose > 1)
954         s = format (s, "\n");
955     }
956
957   return s;
958 }
959
960 static u8 *
961 format_http_transport_listener (u8 *s, va_list *args)
962 {
963   u32 tc_index = va_arg (*args, u32);
964   u32 __clib_unused thread_index = va_arg (*args, u32);
965   u32 __clib_unused verbose = va_arg (*args, u32);
966   http_conn_t *lhc = http_listener_get (tc_index);
967
968   s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_http_listener, lhc);
969   if (verbose)
970     s =
971       format (s, "%-" SESSION_CLI_STATE_LEN "U", format_http_conn_state, lhc);
972   return s;
973 }
974
975 static const transport_proto_vft_t http_proto = {
976   .enable = http_transport_enable,
977   .connect = http_transport_connect,
978   .start_listen = http_start_listen,
979   .close = http_transport_close,
980   .custom_tx = http_app_tx_callback,
981   .get_connection = http_transport_get_connection,
982   .get_listener = http_transport_get_listener,
983   .format_connection = format_http_transport_connection,
984   .format_listener = format_http_transport_listener,
985   .transport_options = {
986     .name = "http",
987     .short_name = "H",
988     .tx_type = TRANSPORT_TX_INTERNAL,
989     .service_type = TRANSPORT_SERVICE_APP,
990   },
991 };
992
993 static clib_error_t *
994 http_transport_init (vlib_main_t *vm)
995 {
996   transport_register_protocol (TRANSPORT_PROTO_HTTP, &http_proto,
997                                FIB_PROTOCOL_IP4, ~0);
998   transport_register_protocol (TRANSPORT_PROTO_HTTP, &http_proto,
999                                FIB_PROTOCOL_IP6, ~0);
1000   return 0;
1001 }
1002
1003 VLIB_INIT_FUNCTION (http_transport_init);
1004
1005 VLIB_PLUGIN_REGISTER () = {
1006   .version = VPP_BUILD_VER,
1007   .description = "Hypertext Transfer Protocol (HTTP)",
1008   .default_disabled = 0,
1009 };
1010
1011 /*
1012  * fd.io coding-style-patch-verification: ON
1013  *
1014  * Local Variables:
1015  * eval: (c-set-style "gnu")
1016  * End:
1017  */