089350eb50659d8045ef6700d0a523f95d3ec31d
[vpp.git] / src / plugins / hs_apps / http_tps.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 <vnet/session/application.h>
17 #include <vnet/session/application_interface.h>
18 #include <vnet/session/session.h>
19 #include <http/http.h>
20
21 typedef struct
22 {
23   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
24   u32 session_index;
25   u32 thread_index;
26   u64 data_len;
27   u64 data_offset;
28   u32 vpp_session_index;
29   u8 *uri;
30 } hts_session_t;
31
32 typedef struct hs_main_
33 {
34   hts_session_t **sessions;
35   u32 app_index;
36
37   u32 ckpair_index;
38   u8 *test_data;
39
40   /** Hash table of listener uris to handles */
41   uword *uri_to_handle;
42
43   /*
44    * Configs
45    */
46   u8 *uri;
47   u32 fifo_size;
48   u64 segment_size;
49   u8 debug_level;
50   u8 no_zc;
51   u8 *default_uri;
52 } hts_main_t;
53
54 static hts_main_t hts_main;
55
56 static hts_session_t *
57 hts_session_alloc (u32 thread_index)
58 {
59   hts_main_t *htm = &hts_main;
60   hts_session_t *hs;
61
62   pool_get_zero (htm->sessions[thread_index], hs);
63   hs->session_index = hs - htm->sessions[thread_index];
64   hs->thread_index = thread_index;
65
66   return hs;
67 }
68
69 static hts_session_t *
70 hts_session_get (u32 thread_index, u32 hts_index)
71 {
72   hts_main_t *htm = &hts_main;
73
74   if (pool_is_free_index (htm->sessions[thread_index], hts_index))
75     return 0;
76
77   return pool_elt_at_index (htm->sessions[thread_index], hts_index);
78 }
79
80 static void
81 hts_session_free (hts_session_t *hs)
82 {
83   hts_main_t *htm = &hts_main;
84   u32 thread = hs->thread_index;
85
86   if (htm->debug_level > 0)
87     clib_warning ("Freeing session %u", hs->session_index);
88
89   if (CLIB_DEBUG)
90     clib_memset (hs, 0xfa, sizeof (*hs));
91
92   pool_put (htm->sessions[thread], hs);
93 }
94
95 static void
96 hts_session_tx_zc (hts_session_t *hs, session_t *ts)
97 {
98   u32 to_send, space;
99   u64 max_send;
100   int rv;
101
102   rv = svm_fifo_fill_chunk_list (ts->tx_fifo);
103   if (rv < 0)
104     {
105       svm_fifo_add_want_deq_ntf (ts->tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
106       return;
107     }
108
109   max_send = hs->data_len - hs->data_offset;
110   space = svm_fifo_max_enqueue (ts->tx_fifo);
111   ASSERT (space != 0);
112   to_send = clib_min (space, max_send);
113
114   svm_fifo_enqueue_nocopy (ts->tx_fifo, to_send);
115
116   hs->data_offset += to_send;
117
118   if (to_send < max_send)
119     svm_fifo_add_want_deq_ntf (ts->tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
120
121   if (svm_fifo_set_event (ts->tx_fifo))
122     session_send_io_evt_to_thread (ts->tx_fifo, SESSION_IO_EVT_TX);
123 }
124
125 static void
126 hts_session_tx_no_zc (hts_session_t *hs, session_t *ts)
127 {
128   u32 n_segs, buf_offset, buf_left;
129   u64 max_send = 32 << 10, left;
130   hts_main_t *htm = &hts_main;
131   svm_fifo_seg_t seg[2];
132   int sent;
133
134   left = hs->data_len - hs->data_offset;
135   max_send = clib_min (left, max_send);
136   buf_offset = hs->data_offset % vec_len (htm->test_data);
137   buf_left = vec_len (htm->test_data) - buf_offset;
138
139   if (buf_left < max_send)
140     {
141       seg[0].data = htm->test_data + buf_offset;
142       seg[0].len = buf_left;
143       seg[1].data = htm->test_data;
144       seg[1].len = max_send - buf_left;
145       n_segs = 2;
146     }
147   else
148     {
149       seg[0].data = htm->test_data + buf_offset;
150       seg[0].len = max_send;
151       n_segs = 1;
152     }
153
154   sent = svm_fifo_enqueue_segments (ts->tx_fifo, seg, n_segs,
155                                     1 /* allow partial */);
156
157   if (sent <= 0)
158     {
159       svm_fifo_add_want_deq_ntf (ts->tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
160       return;
161     }
162
163   hs->data_offset += sent;
164
165   if (sent < left)
166     svm_fifo_add_want_deq_ntf (ts->tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
167
168   if (svm_fifo_set_event (ts->tx_fifo))
169     session_send_io_evt_to_thread (ts->tx_fifo, SESSION_IO_EVT_TX);
170 }
171
172 static inline void
173 hts_session_tx (hts_session_t *hs, session_t *ts)
174 {
175   hts_main_t *htm = &hts_main;
176
177   if (!htm->no_zc)
178     hts_session_tx_zc (hs, ts);
179   else
180     hts_session_tx_no_zc (hs, ts);
181 }
182
183 static void
184 hts_start_send_data (hts_session_t *hs, http_status_code_t status)
185 {
186   http_msg_t msg;
187   session_t *ts;
188   int rv;
189
190   msg.type = HTTP_MSG_REPLY;
191   msg.code = status;
192   msg.content_type = HTTP_CONTENT_APP_OCTET_STREAM;
193   msg.data.type = HTTP_MSG_DATA_INLINE;
194   msg.data.len = hs->data_len;
195
196   ts = session_get (hs->vpp_session_index, hs->thread_index);
197   rv = svm_fifo_enqueue (ts->tx_fifo, sizeof (msg), (u8 *) &msg);
198   ASSERT (rv == sizeof (msg));
199
200   if (!msg.data.len)
201     {
202       if (svm_fifo_set_event (ts->tx_fifo))
203         session_send_io_evt_to_thread (ts->tx_fifo, SESSION_IO_EVT_TX);
204       return;
205     }
206
207   hts_session_tx (hs, ts);
208 }
209
210 static int
211 try_test_file (hts_session_t *hs, u8 *request)
212 {
213   char *test_str = "test_file";
214   hts_main_t *htm = &hts_main;
215   unformat_input_t input;
216   uword file_size;
217   int rc = 0;
218
219   if (memcmp (request, test_str, clib_strnlen (test_str, 9)))
220     return -1;
221
222   unformat_init_vector (&input, vec_dup (request));
223   if (!unformat (&input, "test_file_%U", unformat_memory_size, &file_size))
224     {
225       rc = -1;
226       goto done;
227     }
228
229   if (unformat_check_input (&input) != UNFORMAT_END_OF_INPUT)
230     {
231       rc = -1;
232       goto done;
233     }
234
235   if (htm->debug_level)
236     clib_warning ("Requested file size %U", format_memory_size, file_size);
237
238   hs->data_len = file_size;
239
240   hts_start_send_data (hs, HTTP_STATUS_OK);
241
242 done:
243   unformat_free (&input);
244
245   return rc;
246 }
247
248 static int
249 hts_ts_rx_callback (session_t *ts)
250 {
251   hts_session_t *hs;
252   u8 *request = 0;
253   http_msg_t msg;
254   int rv;
255
256   hs = hts_session_get (ts->thread_index, ts->opaque);
257
258   /* Read the http message header */
259   rv = svm_fifo_dequeue (ts->rx_fifo, sizeof (msg), (u8 *) &msg);
260   ASSERT (rv == sizeof (msg));
261
262   if (msg.type != HTTP_MSG_REQUEST || msg.method_type != HTTP_REQ_GET)
263     {
264       hts_start_send_data (hs, HTTP_STATUS_METHOD_NOT_ALLOWED);
265       goto done;
266     }
267
268   if (!msg.data.len)
269     {
270       hts_start_send_data (hs, HTTP_STATUS_BAD_REQUEST);
271       goto done;
272     }
273
274   vec_validate (request, msg.data.len - 1);
275   rv = svm_fifo_dequeue (ts->rx_fifo, msg.data.len, request);
276
277   if (try_test_file (hs, request))
278     hts_start_send_data (hs, HTTP_STATUS_NOT_FOUND);
279
280 done:
281
282   return 0;
283 }
284
285 static int
286 hs_ts_tx_callback (session_t *ts)
287 {
288   hts_session_t *hs;
289
290   hs = hts_session_get (ts->thread_index, ts->opaque);
291   if (!hs)
292     return 0;
293
294   hts_session_tx (hs, ts);
295
296   return 0;
297 }
298
299 static int
300 hts_ts_accept_callback (session_t *ts)
301 {
302   hts_main_t *htm = &hts_main;
303   hts_session_t *hs;
304
305   hs = hts_session_alloc (ts->thread_index);
306   hs->vpp_session_index = ts->session_index;
307
308   ts->opaque = hs->session_index;
309   ts->session_state = SESSION_STATE_READY;
310
311   if (htm->debug_level > 0)
312     clib_warning ("Accepted session %u", ts->opaque);
313
314   return 0;
315 }
316
317 static int
318 hts_ts_connected_callback (u32 app_index, u32 api_context, session_t *s,
319                            session_error_t err)
320 {
321   clib_warning ("called...");
322   return -1;
323 }
324
325 static void
326 hts_ts_disconnect_callback (session_t *ts)
327 {
328   hts_main_t *htm = &hts_main;
329   vnet_disconnect_args_t _a = { 0 }, *a = &_a;
330
331   if (htm->debug_level > 0)
332     clib_warning ("Closed session %u", ts->opaque);
333
334   a->handle = session_handle (ts);
335   a->app_index = htm->app_index;
336   vnet_disconnect_session (a);
337 }
338
339 static void
340 hts_ts_reset_callback (session_t *ts)
341 {
342   hts_main_t *htm = &hts_main;
343   vnet_disconnect_args_t _a = { 0 }, *a = &_a;
344
345   if (htm->debug_level > 0)
346     clib_warning ("Reset session %u", ts->opaque);
347
348   a->handle = session_handle (ts);
349   a->app_index = htm->app_index;
350   vnet_disconnect_session (a);
351 }
352
353 static void
354 hts_ts_cleanup_callback (session_t *s, session_cleanup_ntf_t ntf)
355 {
356   hts_session_t *hs;
357
358   if (ntf == SESSION_CLEANUP_TRANSPORT)
359     return;
360
361   hs = hts_session_get (s->thread_index, s->opaque);
362   if (!hs)
363     return;
364
365   hts_session_free (hs);
366 }
367
368 static int
369 hts_add_segment_callback (u32 client_index, u64 segment_handle)
370 {
371   return 0;
372 }
373
374 static int
375 hts_del_segment_callback (u32 client_index, u64 segment_handle)
376 {
377   return 0;
378 }
379
380 static session_cb_vft_t hs_session_cb_vft = {
381   .session_accept_callback = hts_ts_accept_callback,
382   .session_disconnect_callback = hts_ts_disconnect_callback,
383   .session_connected_callback = hts_ts_connected_callback,
384   .add_segment_callback = hts_add_segment_callback,
385   .del_segment_callback = hts_del_segment_callback,
386   .builtin_app_rx_callback = hts_ts_rx_callback,
387   .builtin_app_tx_callback = hs_ts_tx_callback,
388   .session_reset_callback = hts_ts_reset_callback,
389   .session_cleanup_callback = hts_ts_cleanup_callback,
390 };
391
392 static int
393 hts_attach (hts_main_t *hm)
394 {
395   vnet_app_add_cert_key_pair_args_t _ck_pair, *ck_pair = &_ck_pair;
396   u64 options[APP_OPTIONS_N_OPTIONS];
397   vnet_app_attach_args_t _a, *a = &_a;
398
399   clib_memset (a, 0, sizeof (*a));
400   clib_memset (options, 0, sizeof (options));
401
402   a->api_client_index = ~0;
403   a->name = format (0, "http_tps");
404   a->session_cb_vft = &hs_session_cb_vft;
405   a->options = options;
406   a->options[APP_OPTIONS_SEGMENT_SIZE] = hm->segment_size;
407   a->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = hm->segment_size;
408   a->options[APP_OPTIONS_RX_FIFO_SIZE] = hm->fifo_size;
409   a->options[APP_OPTIONS_TX_FIFO_SIZE] = hm->fifo_size;
410   a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
411
412   if (vnet_application_attach (a))
413     {
414       vec_free (a->name);
415       clib_warning ("failed to attach server");
416       return -1;
417     }
418   vec_free (a->name);
419   hm->app_index = a->app_index;
420
421   clib_memset (ck_pair, 0, sizeof (*ck_pair));
422   ck_pair->cert = (u8 *) test_srv_crt_rsa;
423   ck_pair->key = (u8 *) test_srv_key_rsa;
424   ck_pair->cert_len = test_srv_crt_rsa_len;
425   ck_pair->key_len = test_srv_key_rsa_len;
426   vnet_app_add_cert_key_pair (ck_pair);
427   hm->ckpair_index = ck_pair->index;
428
429   return 0;
430 }
431
432 static int
433 hts_transport_needs_crypto (transport_proto_t proto)
434 {
435   return proto == TRANSPORT_PROTO_TLS || proto == TRANSPORT_PROTO_DTLS ||
436          proto == TRANSPORT_PROTO_QUIC;
437 }
438
439 static clib_error_t *
440 hts_listen (hts_main_t *htm, u8 *listen_uri, u8 is_del)
441 {
442   session_endpoint_cfg_t sep = SESSION_ENDPOINT_CFG_NULL;
443   vnet_listen_args_t _a, *a = &_a;
444   u8 need_crypto, *uri;
445   hts_session_t *hls;
446   session_t *ls;
447   uword *p;
448   int rv;
449
450   uri = listen_uri ? listen_uri : htm->default_uri;
451   p = hash_get_mem (htm->uri_to_handle, uri);
452
453   if (is_del)
454     {
455       if (!p)
456         return clib_error_return (0, "not listening on %v", uri);
457
458       hls = hts_session_get (0, *p);
459       ls = listen_session_get (hls->vpp_session_index);
460
461       vnet_unlisten_args_t ua = {
462         .handle = listen_session_get_handle (ls),
463         .app_index = htm->app_index,
464         .wrk_map_index = 0 /* default wrk */
465       };
466
467       hash_unset_mem (htm->uri_to_handle, uri);
468
469       if (vnet_unlisten (&ua))
470         return clib_error_return (0, "failed to unlisten");
471
472       vec_free (hls->uri);
473       hts_session_free (hls);
474
475       return 0;
476     }
477
478   if (p)
479     return clib_error_return (0, "already listening %v", uri);
480
481   if (parse_uri ((char *) uri, &sep))
482     return clib_error_return (0, "failed to parse uri %v", uri);
483
484   clib_memset (a, 0, sizeof (*a));
485   a->app_index = htm->app_index;
486
487   need_crypto = hts_transport_needs_crypto (sep.transport_proto);
488
489   sep.transport_proto = TRANSPORT_PROTO_HTTP;
490   clib_memcpy (&a->sep_ext, &sep, sizeof (sep));
491
492   if (need_crypto)
493     {
494       session_endpoint_alloc_ext_cfg (&a->sep_ext,
495                                       TRANSPORT_ENDPT_EXT_CFG_CRYPTO);
496       a->sep_ext.ext_cfg->crypto.ckpair_index = htm->ckpair_index;
497     }
498
499   rv = vnet_listen (a);
500
501   if (need_crypto)
502     clib_mem_free (a->sep_ext.ext_cfg);
503
504   if (rv)
505     return clib_error_return (0, "failed to listen on %v", uri);
506
507   hls = hts_session_alloc (0);
508   hls->uri = vec_dup (uri);
509   ls = listen_session_get_from_handle (a->handle);
510   hls->vpp_session_index = ls->session_index;
511   hash_set_mem (htm->uri_to_handle, hls->uri, hls->session_index);
512
513   return 0;
514 }
515
516 static int
517 hts_create (vlib_main_t *vm)
518 {
519   vlib_thread_main_t *vtm = vlib_get_thread_main ();
520   hts_main_t *htm = &hts_main;
521   u32 num_threads;
522
523   num_threads = 1 /* main thread */ + vtm->n_threads;
524   vec_validate (htm->sessions, num_threads - 1);
525
526   if (htm->no_zc)
527     vec_validate (htm->test_data, (64 << 10) - 1);
528
529   if (hts_attach (htm))
530     {
531       clib_warning ("failed to attach server");
532       return -1;
533     }
534
535   htm->default_uri = format (0, "tcp://0.0.0.0/80%c", 0);
536   htm->uri_to_handle = hash_create_vec (0, sizeof (u8), sizeof (uword));
537
538   return 0;
539 }
540
541 static clib_error_t *
542 hts_create_command_fn (vlib_main_t *vm, unformat_input_t *input,
543                        vlib_cli_command_t *cmd)
544 {
545   unformat_input_t _line_input, *line_input = &_line_input;
546   hts_main_t *htm = &hts_main;
547   clib_error_t *error = 0;
548   u8 is_del = 0;
549   u64 mem_size;
550   u8 *uri = 0;
551
552   /* Get a line of input. */
553   if (!unformat_user (input, unformat_line_input, line_input))
554     goto start_server;
555
556   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
557     {
558       if (unformat (line_input, "private-segment-size %U",
559                     unformat_memory_size, &mem_size))
560         htm->segment_size = mem_size;
561       else if (unformat (line_input, "fifo-size %U", unformat_memory_size,
562                          &mem_size))
563         htm->fifo_size = mem_size;
564       else if (unformat (line_input, "uri %s", &uri))
565         ;
566       else if (unformat (line_input, "no-zc"))
567         htm->no_zc = 1;
568       else if (unformat (line_input, "debug"))
569         htm->debug_level = 1;
570       else if (unformat (line_input, "del"))
571         is_del = 1;
572       else
573         {
574           error = clib_error_return (0, "unknown input `%U'",
575                                      format_unformat_error, line_input);
576           break;
577         }
578     }
579
580   unformat_free (line_input);
581
582   if (error)
583     goto done;
584
585 start_server:
586
587   if (htm->app_index == (u32) ~0)
588     {
589       vnet_session_enable_disable (vm, 1 /* is_enable */);
590
591       if (hts_create (vm))
592         {
593           error = clib_error_return (0, "http tps create failed");
594           goto done;
595         }
596     }
597
598   error = hts_listen (htm, uri, is_del);
599
600 done:
601
602   vec_free (uri);
603   return error;
604 }
605
606 VLIB_CLI_COMMAND (http_tps_command, static) = {
607   .path = "http tps",
608   .short_help = "http tps [uri <uri>] [fifo-size <nbytes>] "
609                 "[segment-size <nMG>] [prealloc-fifos <n>] [debug] [no-zc] "
610                 "[del]",
611   .function = hts_create_command_fn,
612 };
613
614 static clib_error_t *
615 hts_show_command_fn (vlib_main_t *vm, unformat_input_t *input,
616                      vlib_cli_command_t *cmd)
617 {
618   unformat_input_t _line_input, *line_input = &_line_input;
619   hts_main_t *htm = &hts_main;
620   clib_error_t *error = 0;
621   u8 do_listeners = 0;
622   hts_session_t **sessions;
623   u32 n_listeners = 0, n_sessions = 0;
624
625   if (!unformat_user (input, unformat_line_input, line_input))
626     goto no_input;
627
628   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
629     {
630       if (unformat (line_input, "listeners"))
631         do_listeners = 1;
632       else
633         {
634           error = clib_error_return (0, "unknown input `%U'",
635                                      format_unformat_error, line_input);
636           break;
637         }
638     }
639
640   if (error)
641     return error;
642
643 no_input:
644
645   if (htm->app_index == ~0)
646     {
647       vlib_cli_output (vm, "http tps not enabled");
648       goto done;
649     }
650
651   if (do_listeners)
652     {
653       uword handle;
654       u8 *s = 0, *uri;
655
656       /* clang-format off */
657       hash_foreach (uri, handle, htm->uri_to_handle, ({
658         s = format (s, "%-30v%lx\n", uri, handle);
659       }));
660       /* clang-format on */
661
662       if (s)
663         {
664           vlib_cli_output (vm, "%-29s%s", "URI", "Index");
665           vlib_cli_output (vm, "%v", s);
666           vec_free (s);
667         }
668       goto done;
669     }
670
671   n_listeners = hash_elts (htm->uri_to_handle);
672   vec_foreach (sessions, htm->sessions)
673     n_sessions += pool_elts (*sessions);
674
675   vlib_cli_output (vm, " app index: %u\n listeners: %u\n sesions: %u",
676                    htm->app_index, n_listeners, n_sessions - n_listeners);
677
678 done:
679   return 0;
680 }
681
682 VLIB_CLI_COMMAND (show_http_tps_command, static) = {
683   .path = "show http tps",
684   .short_help = "http tps [listeners]",
685   .function = hts_show_command_fn,
686 };
687
688 static clib_error_t *
689 hs_main_init (vlib_main_t *vm)
690 {
691   hts_main_t *htm = &hts_main;
692
693   htm->app_index = ~0;
694   htm->segment_size = 128 << 20;
695   htm->fifo_size = 64 << 10;
696
697   return 0;
698 }
699
700 VLIB_INIT_FUNCTION (hs_main_init);
701
702 /*
703  * fd.io coding-style-patch-verification: ON
704  *
705  * Local Variables:
706  * eval: (c-set-style "gnu")
707  * End:
708  */