blob: f307880bcfd06bd0c5f20d82bb4632734548392a [file] [log] [blame]
Dave Barach1015a1e2017-05-08 19:15:03 -04001/*
2* Copyright (c) 2015-2017 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/vnet.h>
Dave Barach1015a1e2017-05-08 19:15:03 -040017#include <vnet/session/application.h>
18#include <vnet/session/application_interface.h>
19
Dave Barach1015a1e2017-05-08 19:15:03 -040020typedef enum
21{
22 EVENT_WAKEUP = 1,
23} http_process_event_t;
24
25typedef struct
26{
Florin Coras0e9c33b2017-08-14 22:33:41 -070027 u64 session_handle;
28 u64 node_index;
29 u8 *data;
30} builtin_http_server_args;
31
32typedef struct
33{
Dave Barach1015a1e2017-05-08 19:15:03 -040034 u8 *rx_buf;
35 unix_shared_memory_queue_t **vpp_queue;
36 u64 byte_index;
37
38 uword *handler_by_get_request;
39
40 u32 *free_http_cli_process_node_indices;
41
42 /* Sever's event queue */
43 unix_shared_memory_queue_t *vl_input_queue;
44
45 /* API client handle */
46 u32 my_client_index;
47
48 u32 app_index;
49
50 /* process node index for evnt scheduling */
51 u32 node_index;
52 vlib_main_t *vlib_main;
53} http_server_main_t;
54
55http_server_main_t http_server_main;
56
57static void
Florin Coras0e9c33b2017-08-14 22:33:41 -070058free_http_process (builtin_http_server_args * args)
Dave Barach1015a1e2017-05-08 19:15:03 -040059{
60 vlib_node_runtime_t *rt;
61 vlib_main_t *vm = &vlib_global_main;
62 http_server_main_t *hsm = &http_server_main;
63 vlib_node_t *n;
64 u32 node_index;
Florin Coras0e9c33b2017-08-14 22:33:41 -070065 builtin_http_server_args **save_args;
Dave Barach1015a1e2017-05-08 19:15:03 -040066
Florin Coras0e9c33b2017-08-14 22:33:41 -070067 node_index = args->node_index;
Dave Barach1015a1e2017-05-08 19:15:03 -040068 ASSERT (node_index != 0);
69
70 n = vlib_get_node (vm, node_index);
Florin Coras0e9c33b2017-08-14 22:33:41 -070071 rt = vlib_node_get_runtime (vm, n->index);
72 save_args = vlib_node_get_runtime_data (vm, n->index);
Dave Barach1015a1e2017-05-08 19:15:03 -040073
Dave Barach1015a1e2017-05-08 19:15:03 -040074 /* Reset process session pointer */
Florin Coras0e9c33b2017-08-14 22:33:41 -070075 clib_mem_free (*save_args);
76 *save_args = 0;
Dave Barach1015a1e2017-05-08 19:15:03 -040077
78 /* Turn off the process node */
79 vlib_node_set_state (vm, rt->node_index, VLIB_NODE_STATE_DISABLED);
80
81 /* add node index to the freelist */
82 vec_add1 (hsm->free_http_cli_process_node_indices, node_index);
83}
84
85static const char
86 *http_response = "HTTP/1.1 200 OK\r\n"
87 "Content-Type: text/html\r\n"
88 "Expires: Mon, 11 Jan 1970 10:10:10 GMT\r\n"
89 "Connection: close\r\n"
90 "Pragma: no-cache\r\n" "Content-Length: %d\r\n\r\n%s";
91
92static const char
93 *http_error_template = "HTTP/1.1 %s\r\n"
94 "Content-Type: text/html\r\n"
95 "Expires: Mon, 11 Jan 1970 10:10:10 GMT\r\n"
96 "Connection: close\r\n" "Pragma: no-cache\r\n" "Content-Length: 0\r\n\r\n";
97
98/* Header, including incantation to suppress favicon.ico requests */
99static const char
100 *html_header_template = "<html><head><title>%v</title>"
101 "</head><link rel=\"icon\" href=\"data:,\"><body><pre>";
102
103static const char *html_footer = "</pre></body></html>\r\n";
104
105static void
106http_cli_output (uword arg, u8 * buffer, uword buffer_bytes)
107{
108 u8 **output_vecp = (u8 **) arg;
109 u8 *output_vec;
110 u32 offset;
111
112 output_vec = *output_vecp;
113
114 offset = vec_len (output_vec);
115 vec_validate (output_vec, offset + buffer_bytes - 1);
116 clib_memcpy (output_vec + offset, buffer, buffer_bytes);
117
118 *output_vecp = output_vec;
119}
120
121void
Florin Coras0e9c33b2017-08-14 22:33:41 -0700122send_data (builtin_http_server_args * args, u8 * data)
Dave Barach1015a1e2017-05-08 19:15:03 -0400123{
124 session_fifo_event_t evt;
125 u32 offset, bytes_to_send;
126 f64 delay = 10e-3;
127 http_server_main_t *hsm = &http_server_main;
128 vlib_main_t *vm = hsm->vlib_main;
129 f64 last_sent_timer = vlib_time_now (vm);
Florin Coras0e9c33b2017-08-14 22:33:41 -0700130 stream_session_t *s;
Dave Barach1015a1e2017-05-08 19:15:03 -0400131
Florin Corascea194d2017-10-02 00:18:51 -0700132 s = session_get_from_handle (args->session_handle);
Florin Coras0e9c33b2017-08-14 22:33:41 -0700133 ASSERT (s);
Dave Barach1015a1e2017-05-08 19:15:03 -0400134 bytes_to_send = vec_len (data);
135 offset = 0;
136
137 while (bytes_to_send > 0)
138 {
139 int actual_transfer;
140
141 actual_transfer = svm_fifo_enqueue_nowait
142 (s->server_tx_fifo, bytes_to_send, data + offset);
143
144 /* Made any progress? */
145 if (actual_transfer <= 0)
146 {
147 vlib_process_suspend (vm, delay);
148 /* 10s deadman timer */
149 if (vlib_time_now (vm) > last_sent_timer + 10.0)
150 {
151 /* $$$$ FC: reset transport session here? */
152 break;
153 }
154 /* Exponential backoff, within reason */
155 if (delay < 1.0)
156 delay = delay * 2.0;
157 }
158 else
159 {
160 last_sent_timer = vlib_time_now (vm);
161 offset += actual_transfer;
162 bytes_to_send -= actual_transfer;
163
164 if (svm_fifo_set_event (s->server_tx_fifo))
165 {
166 /* Fabricate TX event, send to vpp */
167 evt.fifo = s->server_tx_fifo;
168 evt.event_type = FIFO_EVENT_APP_TX;
Dave Barach1015a1e2017-05-08 19:15:03 -0400169
170 unix_shared_memory_queue_add (hsm->vpp_queue[s->thread_index],
171 (u8 *) & evt,
172 0 /* do wait for mutex */ );
173 }
174 delay = 10e-3;
175 }
176 }
177}
178
179static void
Florin Coras0e9c33b2017-08-14 22:33:41 -0700180send_error (builtin_http_server_args * args, char *str)
Dave Barach1015a1e2017-05-08 19:15:03 -0400181{
182 u8 *data;
183
184 data = format (0, http_error_template, str);
Florin Coras0e9c33b2017-08-14 22:33:41 -0700185 send_data (args, data);
Dave Barach1015a1e2017-05-08 19:15:03 -0400186 vec_free (data);
187}
188
189static uword
190http_cli_process (vlib_main_t * vm,
191 vlib_node_runtime_t * rt, vlib_frame_t * f)
192{
193 http_server_main_t *hsm = &http_server_main;
194 u8 *request = 0, *reply = 0;
Florin Coras0e9c33b2017-08-14 22:33:41 -0700195 builtin_http_server_args **save_args;
196 builtin_http_server_args *args;
Dave Barach1015a1e2017-05-08 19:15:03 -0400197 unformat_input_t input;
198 int i;
199 u8 *http = 0, *html = 0;
200
Florin Coras0e9c33b2017-08-14 22:33:41 -0700201 save_args = vlib_node_get_runtime_data (hsm->vlib_main, rt->node_index);
202 args = *save_args;
Dave Barach1015a1e2017-05-08 19:15:03 -0400203
Florin Coras0e9c33b2017-08-14 22:33:41 -0700204 request = (u8 *) (void *) (args->data);
Dave Barach1015a1e2017-05-08 19:15:03 -0400205 if (vec_len (request) < 7)
206 {
Florin Coras0e9c33b2017-08-14 22:33:41 -0700207 send_error (args, "400 Bad Request");
Dave Barach1015a1e2017-05-08 19:15:03 -0400208 goto out;
209 }
210
211 for (i = 0; i < vec_len (request) - 4; i++)
212 {
213 if (request[i] == 'G' &&
214 request[i + 1] == 'E' &&
215 request[i + 2] == 'T' && request[i + 3] == ' ')
216 goto found;
217 }
218bad_request:
Florin Coras0e9c33b2017-08-14 22:33:41 -0700219 send_error (args, "400 Bad Request");
Dave Barach1015a1e2017-05-08 19:15:03 -0400220 goto out;
221
222found:
223 /* Lose "GET " */
224 vec_delete (request, i + 5, 0);
225
226 /* Replace slashes with spaces, stop at the end of the path */
227 i = 0;
228 while (1)
229 {
230 if (request[i] == '/')
231 request[i] = ' ';
232 else if (request[i] == ' ')
233 {
234 /* vlib_cli_input is vector-based, no need for a NULL */
235 _vec_len (request) = i;
236 break;
237 }
238 i++;
239 /* Should never happen */
240 if (i == vec_len (request))
241 goto bad_request;
242 }
243
244 /* Generate the html header */
245 html = format (0, html_header_template, request /* title */ );
246
247 /* Run the command */
248 unformat_init_vector (&input, request);
249 vlib_cli_input (vm, &input, http_cli_output, (uword) & reply);
250 unformat_free (&input);
251 request = 0;
252
253 /* Generate the html page */
254 html = format (html, "%v", reply);
255 html = format (html, html_footer);
256 /* And the http reply */
257 http = format (0, http_response, vec_len (html), html);
258
259 /* Send it */
Florin Coras0e9c33b2017-08-14 22:33:41 -0700260 send_data (args, http);
Dave Barach1015a1e2017-05-08 19:15:03 -0400261
262out:
263 /* Cleanup */
264 vec_free (request);
265 vec_free (reply);
266 vec_free (html);
267 vec_free (http);
268
Florin Coras0e9c33b2017-08-14 22:33:41 -0700269 free_http_process (args);
Dave Barach1015a1e2017-05-08 19:15:03 -0400270 return (0);
271}
272
273static void
Florin Coras0e9c33b2017-08-14 22:33:41 -0700274alloc_http_process (builtin_http_server_args * args)
Dave Barach1015a1e2017-05-08 19:15:03 -0400275{
276 char *name;
277 vlib_node_t *n;
278 http_server_main_t *hsm = &http_server_main;
279 vlib_main_t *vm = hsm->vlib_main;
280 uword l = vec_len (hsm->free_http_cli_process_node_indices);
Florin Coras0e9c33b2017-08-14 22:33:41 -0700281 builtin_http_server_args **save_args;
Dave Barach1015a1e2017-05-08 19:15:03 -0400282
283 if (vec_len (hsm->free_http_cli_process_node_indices) > 0)
284 {
285 n = vlib_get_node (vm, hsm->free_http_cli_process_node_indices[l - 1]);
Dave Barach1015a1e2017-05-08 19:15:03 -0400286 vlib_node_set_state (vm, n->index, VLIB_NODE_STATE_POLLING);
Dave Barach1015a1e2017-05-08 19:15:03 -0400287 _vec_len (hsm->free_http_cli_process_node_indices) = l - 1;
288 }
289 else
290 {
291 static vlib_node_registration_t r = {
292 .function = http_cli_process,
293 .type = VLIB_NODE_TYPE_PROCESS,
294 .process_log2_n_stack_bytes = 16,
295 .runtime_data_bytes = sizeof (void *),
296 };
297
298 name = (char *) format (0, "http-cli-%d", l);
Dave Barach1015a1e2017-05-08 19:15:03 -0400299 r.name = name;
300 vlib_register_node (vm, &r);
301 vec_free (name);
302
303 n = vlib_get_node (vm, r.index);
304 }
305
Florin Coras0e9c33b2017-08-14 22:33:41 -0700306 /* Save the node index in the args. It won't be zero. */
307 args->node_index = n->index;
Dave Barach1015a1e2017-05-08 19:15:03 -0400308
Florin Coras0e9c33b2017-08-14 22:33:41 -0700309 /* Save the args (pointer) in the node runtime */
310 save_args = vlib_node_get_runtime_data (vm, n->index);
311 *save_args = args;
Dave Barach1015a1e2017-05-08 19:15:03 -0400312
313 vlib_start_process (vm, n->runtime_index);
314}
315
Florin Coras0e9c33b2017-08-14 22:33:41 -0700316static void
317alloc_http_process_callback (void *cb_args)
318{
319 alloc_http_process ((builtin_http_server_args *) cb_args);
320}
321
322static int
323http_server_rx_callback (stream_session_t * s)
324{
325 u32 max_dequeue;
326 int actual_transfer;
327 http_server_main_t *hsm = &http_server_main;
328 svm_fifo_t *rx_fifo;
329 builtin_http_server_args *args;
330
331 rx_fifo = s->server_rx_fifo;
332 max_dequeue = svm_fifo_max_dequeue (rx_fifo);
333 svm_fifo_unset_event (rx_fifo);
334 if (PREDICT_FALSE (max_dequeue == 0))
335 return 0;
336
337 vec_validate (hsm->rx_buf, max_dequeue - 1);
338 _vec_len (hsm->rx_buf) = max_dequeue;
339
340 actual_transfer = svm_fifo_dequeue_nowait (rx_fifo, max_dequeue,
341 hsm->rx_buf);
342 ASSERT (actual_transfer > 0);
343 _vec_len (hsm->rx_buf) = actual_transfer;
344
345 /* send the command to a new/recycled vlib process */
346 args = clib_mem_alloc (sizeof (*args));
347 args->data = vec_dup (hsm->rx_buf);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700348 args->session_handle = session_handle (s);
Florin Coras0e9c33b2017-08-14 22:33:41 -0700349
350 /* Send an RPC request via the thread-0 input node */
351 if (vlib_get_thread_index () != 0)
352 {
353 session_fifo_event_t evt;
354 evt.rpc_args.fp = alloc_http_process_callback;
355 evt.rpc_args.arg = args;
356 evt.event_type = FIFO_EVENT_RPC;
357 unix_shared_memory_queue_add
358 (session_manager_get_vpp_event_queue (0 /* main thread */ ),
359 (u8 *) & evt, 0 /* do wait for mutex */ );
360 }
361 else
362 alloc_http_process (args);
363 return 0;
364}
365
Dave Barach1015a1e2017-05-08 19:15:03 -0400366static int
367builtin_session_accept_callback (stream_session_t * s)
368{
369 http_server_main_t *bsm = &http_server_main;
370
371 bsm->vpp_queue[s->thread_index] =
372 session_manager_get_vpp_event_queue (s->thread_index);
373 s->session_state = SESSION_STATE_READY;
374 bsm->byte_index = 0;
375 return 0;
376}
377
378static void
379builtin_session_disconnect_callback (stream_session_t * s)
380{
381 http_server_main_t *bsm = &http_server_main;
382 vnet_disconnect_args_t _a, *a = &_a;
383
Florin Coras3cbc04b2017-10-02 00:18:51 -0700384 a->handle = session_handle (s);
Dave Barach1015a1e2017-05-08 19:15:03 -0400385 a->app_index = bsm->app_index;
386 vnet_disconnect_session (a);
387}
388
389static void
390builtin_session_reset_callback (stream_session_t * s)
391{
392 clib_warning ("called.. ");
393
394 stream_session_cleanup (s);
395}
396
Dave Barach1015a1e2017-05-08 19:15:03 -0400397static int
398builtin_session_connected_callback (u32 app_index, u32 api_context,
399 stream_session_t * s, u8 is_fail)
400{
401 clib_warning ("called...");
402 return -1;
403}
404
405static int
406builtin_add_segment_callback (u32 client_index,
407 const u8 * seg_name, u32 seg_size)
408{
409 clib_warning ("called...");
410 return -1;
411}
412
413static int
414builtin_redirect_connect_callback (u32 client_index, void *mp)
415{
416 clib_warning ("called...");
417 return -1;
418}
419
Dave Barach1015a1e2017-05-08 19:15:03 -0400420static session_cb_vft_t builtin_session_cb_vft = {
421 .session_accept_callback = builtin_session_accept_callback,
422 .session_disconnect_callback = builtin_session_disconnect_callback,
423 .session_connected_callback = builtin_session_connected_callback,
424 .add_segment_callback = builtin_add_segment_callback,
425 .redirect_connect_callback = builtin_redirect_connect_callback,
426 .builtin_server_rx_callback = http_server_rx_callback,
427 .session_reset_callback = builtin_session_reset_callback
428};
429
430/* Abuse VPP's input queue */
431static int
432create_api_loopback (vlib_main_t * vm)
433{
434 http_server_main_t *hsm = &http_server_main;
Dave Barach1015a1e2017-05-08 19:15:03 -0400435 api_main_t *am = &api_main;
436 vl_shmem_hdr_t *shmem_hdr;
Dave Barach1015a1e2017-05-08 19:15:03 -0400437
438 shmem_hdr = am->shmem_hdr;
439 hsm->vl_input_queue = shmem_hdr->vl_input_queue;
Florin Coras0e9c33b2017-08-14 22:33:41 -0700440 hsm->my_client_index =
441 vl_api_memclnt_create_internal ("tcp_test_client", hsm->vl_input_queue);
Dave Barach1015a1e2017-05-08 19:15:03 -0400442 return 0;
443}
444
445static int
446server_attach ()
447{
448 http_server_main_t *hsm = &http_server_main;
449 u8 segment_name[128];
450 u64 options[SESSION_OPTIONS_N_OPTIONS];
451 vnet_app_attach_args_t _a, *a = &_a;
452
453 memset (a, 0, sizeof (*a));
454 memset (options, 0, sizeof (options));
455
456 a->api_client_index = hsm->my_client_index;
457 a->session_cb_vft = &builtin_session_cb_vft;
458 a->options = options;
459 a->options[SESSION_OPTIONS_SEGMENT_SIZE] = 128 << 20;
460 a->options[SESSION_OPTIONS_RX_FIFO_SIZE] = 8 << 10;
461 a->options[SESSION_OPTIONS_TX_FIFO_SIZE] = 32 << 10;
462 a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_BUILTIN_APP;
Dave Barach10d8cc62017-05-30 09:30:07 -0400463 a->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = 16;
Dave Barach1015a1e2017-05-08 19:15:03 -0400464 a->segment_name = segment_name;
465 a->segment_name_length = ARRAY_LEN (segment_name);
466
467 if (vnet_application_attach (a))
468 {
469 clib_warning ("failed to attach server");
470 return -1;
471 }
472 hsm->app_index = a->app_index;
473 return 0;
474}
475
476static int
477server_listen ()
478{
479 http_server_main_t *hsm = &http_server_main;
480 vnet_bind_args_t _a, *a = &_a;
481 memset (a, 0, sizeof (*a));
482 a->app_index = hsm->app_index;
483 a->uri = "tcp://0.0.0.0/80";
484 return vnet_bind_uri (a);
485}
486
487static int
488server_create (vlib_main_t * vm)
489{
490 http_server_main_t *hsm = &http_server_main;
491 u32 num_threads;
492 vlib_thread_main_t *vtm = vlib_get_thread_main ();
493
Florin Coras0e9c33b2017-08-14 22:33:41 -0700494 ASSERT (hsm->my_client_index == (u32) ~ 0);
495 if (create_api_loopback (vm))
496 return -1;
Dave Barach1015a1e2017-05-08 19:15:03 -0400497
498 num_threads = 1 /* main thread */ + vtm->n_threads;
499 vec_validate (http_server_main.vpp_queue, num_threads - 1);
500
501 if (server_attach ())
502 {
503 clib_warning ("failed to attach server");
504 return -1;
505 }
506 if (server_listen ())
507 {
508 clib_warning ("failed to start listening");
509 return -1;
510 }
511 return 0;
512}
513
Dave Barach1015a1e2017-05-08 19:15:03 -0400514static clib_error_t *
515server_create_command_fn (vlib_main_t * vm,
516 unformat_input_t * input, vlib_cli_command_t * cmd)
517{
Florin Coras0e9c33b2017-08-14 22:33:41 -0700518 http_server_main_t *hsm = &http_server_main;
Dave Barach1015a1e2017-05-08 19:15:03 -0400519 int rv;
520
Florin Coras0e9c33b2017-08-14 22:33:41 -0700521 if (hsm->my_client_index != (u32) ~ 0)
522 return clib_error_return (0, "test http server is already running");
523
Dave Barach1015a1e2017-05-08 19:15:03 -0400524 vnet_session_enable_disable (vm, 1 /* turn on TCP, etc. */ );
525 rv = server_create (vm);
526 switch (rv)
527 {
528 case 0:
529 break;
530 default:
531 return clib_error_return (0, "server_create returned %d", rv);
532 }
533 return 0;
534}
535
536/* *INDENT-OFF* */
537VLIB_CLI_COMMAND (server_create_command, static) =
538{
539 .path = "test http server",
540 .short_help = "test http server",
541 .function = server_create_command_fn,
542};
543/* *INDENT-ON* */
544
545static clib_error_t *
546builtin_http_server_main_init (vlib_main_t * vm)
547{
548 http_server_main_t *hsm = &http_server_main;
549 hsm->my_client_index = ~0;
550 hsm->vlib_main = vm;
551
552 return 0;
553}
554
555VLIB_INIT_FUNCTION (builtin_http_server_main_init);
556
557/*
558* fd.io coding-style-patch-verification: ON
559*
560* Local Variables:
561* eval: (c-set-style "gnu")
562* End:
563*/