blob: 8b4801cd49e4449bf27fdbf79093a4fedba8087f [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>
17#include <vlibmemory/api.h>
18#include <vnet/session/application.h>
19#include <vnet/session/application_interface.h>
20
21/* define message IDs */
22#include <vpp/api/vpe_msg_enum.h>
23
24/* define message structures */
25#define vl_typedefs
26#include <vpp/api/vpe_all_api_h.h>
27#undef vl_typedefs
28
29/* define generated endian-swappers */
30#define vl_endianfun
31#include <vpp/api/vpe_all_api_h.h>
32#undef vl_endianfun
33
34/* instantiate all the print functions we know about */
35#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
36#define vl_printfun
37#include <vpp/api/vpe_all_api_h.h>
38#undef vl_printfun
39
40typedef enum
41{
42 EVENT_WAKEUP = 1,
43} http_process_event_t;
44
45typedef struct
46{
47 u8 *rx_buf;
48 unix_shared_memory_queue_t **vpp_queue;
49 u64 byte_index;
50
51 uword *handler_by_get_request;
52
53 u32 *free_http_cli_process_node_indices;
54
55 /* Sever's event queue */
56 unix_shared_memory_queue_t *vl_input_queue;
57
58 /* API client handle */
59 u32 my_client_index;
60
61 u32 app_index;
62
63 /* process node index for evnt scheduling */
64 u32 node_index;
65 vlib_main_t *vlib_main;
66} http_server_main_t;
67
68http_server_main_t http_server_main;
69
70static void
71free_http_process (stream_session_t * s)
72{
73 vlib_node_runtime_t *rt;
74 vlib_main_t *vm = &vlib_global_main;
75 http_server_main_t *hsm = &http_server_main;
76 vlib_node_t *n;
77 u32 node_index;
78 stream_session_t **save_s;
79
80 node_index = (u64) (s->opaque[0]);
81 ASSERT (node_index != 0);
82
83 n = vlib_get_node (vm, node_index);
84 rt = vlib_node_get_runtime (vm, n->runtime_index);
85 save_s = (stream_session_t **) vlib_node_get_runtime (vm, n->runtime_index);
86
87 /* Reset session saved node index */
88 s->opaque[0] = 0;
89 /* Reset process session pointer */
90 *save_s = 0;
91
92 /* Turn off the process node */
93 vlib_node_set_state (vm, rt->node_index, VLIB_NODE_STATE_DISABLED);
94
95 /* add node index to the freelist */
96 vec_add1 (hsm->free_http_cli_process_node_indices, node_index);
97}
98
99static const char
100 *http_response = "HTTP/1.1 200 OK\r\n"
101 "Content-Type: text/html\r\n"
102 "Expires: Mon, 11 Jan 1970 10:10:10 GMT\r\n"
103 "Connection: close\r\n"
104 "Pragma: no-cache\r\n" "Content-Length: %d\r\n\r\n%s";
105
106static const char
107 *http_error_template = "HTTP/1.1 %s\r\n"
108 "Content-Type: text/html\r\n"
109 "Expires: Mon, 11 Jan 1970 10:10:10 GMT\r\n"
110 "Connection: close\r\n" "Pragma: no-cache\r\n" "Content-Length: 0\r\n\r\n";
111
112/* Header, including incantation to suppress favicon.ico requests */
113static const char
114 *html_header_template = "<html><head><title>%v</title>"
115 "</head><link rel=\"icon\" href=\"data:,\"><body><pre>";
116
117static const char *html_footer = "</pre></body></html>\r\n";
118
119static void
120http_cli_output (uword arg, u8 * buffer, uword buffer_bytes)
121{
122 u8 **output_vecp = (u8 **) arg;
123 u8 *output_vec;
124 u32 offset;
125
126 output_vec = *output_vecp;
127
128 offset = vec_len (output_vec);
129 vec_validate (output_vec, offset + buffer_bytes - 1);
130 clib_memcpy (output_vec + offset, buffer, buffer_bytes);
131
132 *output_vecp = output_vec;
133}
134
135void
136send_data (stream_session_t * s, u8 * data)
137{
138 session_fifo_event_t evt;
139 u32 offset, bytes_to_send;
140 f64 delay = 10e-3;
141 http_server_main_t *hsm = &http_server_main;
142 vlib_main_t *vm = hsm->vlib_main;
143 f64 last_sent_timer = vlib_time_now (vm);
144
145 bytes_to_send = vec_len (data);
146 offset = 0;
147
148 while (bytes_to_send > 0)
149 {
150 int actual_transfer;
151
152 actual_transfer = svm_fifo_enqueue_nowait
153 (s->server_tx_fifo, bytes_to_send, data + offset);
154
155 /* Made any progress? */
156 if (actual_transfer <= 0)
157 {
158 vlib_process_suspend (vm, delay);
159 /* 10s deadman timer */
160 if (vlib_time_now (vm) > last_sent_timer + 10.0)
161 {
162 /* $$$$ FC: reset transport session here? */
163 break;
164 }
165 /* Exponential backoff, within reason */
166 if (delay < 1.0)
167 delay = delay * 2.0;
168 }
169 else
170 {
171 last_sent_timer = vlib_time_now (vm);
172 offset += actual_transfer;
173 bytes_to_send -= actual_transfer;
174
175 if (svm_fifo_set_event (s->server_tx_fifo))
176 {
177 /* Fabricate TX event, send to vpp */
178 evt.fifo = s->server_tx_fifo;
179 evt.event_type = FIFO_EVENT_APP_TX;
180 evt.event_id = 0;
181
182 unix_shared_memory_queue_add (hsm->vpp_queue[s->thread_index],
183 (u8 *) & evt,
184 0 /* do wait for mutex */ );
185 }
186 delay = 10e-3;
187 }
188 }
189}
190
191static void
192send_error (stream_session_t * s, char *str)
193{
194 u8 *data;
195
196 data = format (0, http_error_template, str);
197 send_data (s, data);
198 vec_free (data);
199}
200
201static uword
202http_cli_process (vlib_main_t * vm,
203 vlib_node_runtime_t * rt, vlib_frame_t * f)
204{
205 http_server_main_t *hsm = &http_server_main;
206 u8 *request = 0, *reply = 0;
207 stream_session_t **save_s;
208 stream_session_t *s;
209 unformat_input_t input;
210 int i;
211 u8 *http = 0, *html = 0;
212
213 save_s = vlib_node_get_runtime_data (hsm->vlib_main, rt->node_index);
214 s = *save_s;
215
216 request = (u8 *) (void *) (s->opaque[1]);
217 s->opaque[1] = 0;
218
219 if (vec_len (request) < 7)
220 {
221 send_error (s, "400 Bad Request");
222 goto out;
223 }
224
225 for (i = 0; i < vec_len (request) - 4; i++)
226 {
227 if (request[i] == 'G' &&
228 request[i + 1] == 'E' &&
229 request[i + 2] == 'T' && request[i + 3] == ' ')
230 goto found;
231 }
232bad_request:
233 send_error (s, "400 Bad Request");
234 goto out;
235
236found:
237 /* Lose "GET " */
238 vec_delete (request, i + 5, 0);
239
240 /* Replace slashes with spaces, stop at the end of the path */
241 i = 0;
242 while (1)
243 {
244 if (request[i] == '/')
245 request[i] = ' ';
246 else if (request[i] == ' ')
247 {
248 /* vlib_cli_input is vector-based, no need for a NULL */
249 _vec_len (request) = i;
250 break;
251 }
252 i++;
253 /* Should never happen */
254 if (i == vec_len (request))
255 goto bad_request;
256 }
257
258 /* Generate the html header */
259 html = format (0, html_header_template, request /* title */ );
260
261 /* Run the command */
262 unformat_init_vector (&input, request);
263 vlib_cli_input (vm, &input, http_cli_output, (uword) & reply);
264 unformat_free (&input);
265 request = 0;
266
267 /* Generate the html page */
268 html = format (html, "%v", reply);
269 html = format (html, html_footer);
270 /* And the http reply */
271 http = format (0, http_response, vec_len (html), html);
272
273 /* Send it */
274 send_data (s, http);
275
276out:
277 /* Cleanup */
278 vec_free (request);
279 vec_free (reply);
280 vec_free (html);
281 vec_free (http);
282
283 free_http_process (s);
284 return (0);
285}
286
287static void
288alloc_http_process (stream_session_t * s)
289{
290 char *name;
291 vlib_node_t *n;
292 http_server_main_t *hsm = &http_server_main;
293 vlib_main_t *vm = hsm->vlib_main;
294 uword l = vec_len (hsm->free_http_cli_process_node_indices);
295 stream_session_t **save_s;
296
297 if (vec_len (hsm->free_http_cli_process_node_indices) > 0)
298 {
299 n = vlib_get_node (vm, hsm->free_http_cli_process_node_indices[l - 1]);
300
301 vlib_node_set_state (vm, n->index, VLIB_NODE_STATE_POLLING);
302
303 _vec_len (hsm->free_http_cli_process_node_indices) = l - 1;
304 }
305 else
306 {
307 static vlib_node_registration_t r = {
308 .function = http_cli_process,
309 .type = VLIB_NODE_TYPE_PROCESS,
310 .process_log2_n_stack_bytes = 16,
311 .runtime_data_bytes = sizeof (void *),
312 };
313
314 name = (char *) format (0, "http-cli-%d", l);
315
316 r.name = name;
317 vlib_register_node (vm, &r);
318 vec_free (name);
319
320 n = vlib_get_node (vm, r.index);
321 }
322
323 /* Save the node index in the stream_session_t. It won't be zero. */
324 s->opaque[0] = (u64) n->index;
325
326 /* Save the stream_session_t (pointer) in the node runtime */
327 save_s = vlib_node_get_runtime_data (vm, n->index);
328 *save_s = s;
329
330 vlib_start_process (vm, n->runtime_index);
331}
332
333static int
334builtin_session_accept_callback (stream_session_t * s)
335{
336 http_server_main_t *bsm = &http_server_main;
337
338 bsm->vpp_queue[s->thread_index] =
339 session_manager_get_vpp_event_queue (s->thread_index);
340 s->session_state = SESSION_STATE_READY;
341 bsm->byte_index = 0;
342 return 0;
343}
344
345static void
346builtin_session_disconnect_callback (stream_session_t * s)
347{
348 http_server_main_t *bsm = &http_server_main;
349 vnet_disconnect_args_t _a, *a = &_a;
350
351 a->handle = stream_session_handle (s);
352 a->app_index = bsm->app_index;
353 vnet_disconnect_session (a);
354}
355
356static void
357builtin_session_reset_callback (stream_session_t * s)
358{
359 clib_warning ("called.. ");
360
361 stream_session_cleanup (s);
362}
363
364
365static int
366builtin_session_connected_callback (u32 app_index, u32 api_context,
367 stream_session_t * s, u8 is_fail)
368{
369 clib_warning ("called...");
370 return -1;
371}
372
373static int
374builtin_add_segment_callback (u32 client_index,
375 const u8 * seg_name, u32 seg_size)
376{
377 clib_warning ("called...");
378 return -1;
379}
380
381static int
382builtin_redirect_connect_callback (u32 client_index, void *mp)
383{
384 clib_warning ("called...");
385 return -1;
386}
387
Dave Barache5f1d272017-05-10 13:34:04 -0400388static void
389alloc_http_process_callback (void *s_arg)
390{
391 stream_session_t *s = (stream_session_t *) s_arg;
392 alloc_http_process (s);
393}
394
Dave Barach1015a1e2017-05-08 19:15:03 -0400395static int
396http_server_rx_callback (stream_session_t * s)
397{
398 u32 max_dequeue;
399 int actual_transfer;
400 http_server_main_t *hsm = &http_server_main;
401 svm_fifo_t *rx_fifo;
402
403 rx_fifo = s->server_rx_fifo;
404
405 max_dequeue = svm_fifo_max_dequeue (rx_fifo);
406
407 svm_fifo_unset_event (rx_fifo);
408
409 if (PREDICT_FALSE (max_dequeue == 0))
410 return 0;
411
412 vec_validate (hsm->rx_buf, max_dequeue - 1);
413 _vec_len (hsm->rx_buf) = max_dequeue;
414
415 actual_transfer = svm_fifo_dequeue_nowait (rx_fifo, max_dequeue,
416 hsm->rx_buf);
417 ASSERT (actual_transfer > 0);
418
419 _vec_len (hsm->rx_buf) = actual_transfer;
420
421 /* send the command to a new/recycled vlib process */
422 s->opaque[1] = (u64) vec_dup (hsm->rx_buf);
423
Dave Barache5f1d272017-05-10 13:34:04 -0400424 /* Send an RPC request via the thread-0 input node */
425 if (vlib_get_thread_index () != 0)
426 {
427 session_fifo_event_t evt;
428 evt.rpc_args.fp = alloc_http_process_callback;
429 evt.rpc_args.arg = s;
430 evt.event_type = FIFO_EVENT_RPC;
431 unix_shared_memory_queue_add
432 (session_manager_get_vpp_event_queue (0 /* main thread */ ),
433 (u8 *) & evt, 0 /* do wait for mutex */ );
434 }
435 else
436 alloc_http_process (s);
Dave Barach1015a1e2017-05-08 19:15:03 -0400437 return 0;
438}
439
440static session_cb_vft_t builtin_session_cb_vft = {
441 .session_accept_callback = builtin_session_accept_callback,
442 .session_disconnect_callback = builtin_session_disconnect_callback,
443 .session_connected_callback = builtin_session_connected_callback,
444 .add_segment_callback = builtin_add_segment_callback,
445 .redirect_connect_callback = builtin_redirect_connect_callback,
446 .builtin_server_rx_callback = http_server_rx_callback,
447 .session_reset_callback = builtin_session_reset_callback
448};
449
450/* Abuse VPP's input queue */
451static int
452create_api_loopback (vlib_main_t * vm)
453{
454 http_server_main_t *hsm = &http_server_main;
455 vl_api_memclnt_create_t _m, *mp = &_m;
456 extern void vl_api_memclnt_create_t_handler (vl_api_memclnt_create_t *);
457 api_main_t *am = &api_main;
458 vl_shmem_hdr_t *shmem_hdr;
459 uword *event_data = 0, event_type;
460 int resolved = 0;
461
462 /*
463 * Create a "loopback" API client connection
464 * Don't do things like this unless you know what you're doing...
465 */
466
467 shmem_hdr = am->shmem_hdr;
468 hsm->vl_input_queue = shmem_hdr->vl_input_queue;
469 memset (mp, 0, sizeof (*mp));
470 mp->_vl_msg_id = VL_API_MEMCLNT_CREATE;
471 mp->context = 0xFEEDFACE;
472 mp->input_queue = (u64) hsm->vl_input_queue;
473 strncpy ((char *) mp->name, "tcp_http_server", sizeof (mp->name) - 1);
474
475 vl_api_memclnt_create_t_handler (mp);
476
477 /* Wait for reply */
478 hsm->node_index = vlib_get_current_process (vm)->node_runtime.node_index;
479 vlib_process_wait_for_event_or_clock (vm, 1.0);
480 event_type = vlib_process_get_events (vm, &event_data);
481 switch (event_type)
482 {
483 case 1:
484 resolved = 1;
485 break;
486 case ~0:
487 /* timed out */
488 break;
489 default:
490 clib_warning ("unknown event_type %d", event_type);
491 }
492 if (!resolved)
493 return -1;
494
495 return 0;
496}
497
498static int
499server_attach ()
500{
501 http_server_main_t *hsm = &http_server_main;
502 u8 segment_name[128];
503 u64 options[SESSION_OPTIONS_N_OPTIONS];
504 vnet_app_attach_args_t _a, *a = &_a;
505
506 memset (a, 0, sizeof (*a));
507 memset (options, 0, sizeof (options));
508
509 a->api_client_index = hsm->my_client_index;
510 a->session_cb_vft = &builtin_session_cb_vft;
511 a->options = options;
512 a->options[SESSION_OPTIONS_SEGMENT_SIZE] = 128 << 20;
513 a->options[SESSION_OPTIONS_RX_FIFO_SIZE] = 8 << 10;
514 a->options[SESSION_OPTIONS_TX_FIFO_SIZE] = 32 << 10;
515 a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_BUILTIN_APP;
Dave Barach10d8cc62017-05-30 09:30:07 -0400516 a->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = 16;
Dave Barach1015a1e2017-05-08 19:15:03 -0400517 a->segment_name = segment_name;
518 a->segment_name_length = ARRAY_LEN (segment_name);
519
520 if (vnet_application_attach (a))
521 {
522 clib_warning ("failed to attach server");
523 return -1;
524 }
525 hsm->app_index = a->app_index;
526 return 0;
527}
528
529static int
530server_listen ()
531{
532 http_server_main_t *hsm = &http_server_main;
533 vnet_bind_args_t _a, *a = &_a;
534 memset (a, 0, sizeof (*a));
535 a->app_index = hsm->app_index;
536 a->uri = "tcp://0.0.0.0/80";
537 return vnet_bind_uri (a);
538}
539
540static int
541server_create (vlib_main_t * vm)
542{
543 http_server_main_t *hsm = &http_server_main;
544 u32 num_threads;
545 vlib_thread_main_t *vtm = vlib_get_thread_main ();
546
547 if (hsm->my_client_index == (u32) ~ 0)
548 {
549 if (create_api_loopback (vm))
550 return -1;
551 }
552
553 num_threads = 1 /* main thread */ + vtm->n_threads;
554 vec_validate (http_server_main.vpp_queue, num_threads - 1);
555
556 if (server_attach ())
557 {
558 clib_warning ("failed to attach server");
559 return -1;
560 }
561 if (server_listen ())
562 {
563 clib_warning ("failed to start listening");
564 return -1;
565 }
566 return 0;
567}
568
569/* Get our api client index */
570static void
571vl_api_memclnt_create_reply_t_handler (vl_api_memclnt_create_reply_t * mp)
572{
573 vlib_main_t *vm = vlib_get_main ();
574 http_server_main_t *hsm = &http_server_main;
575 hsm->my_client_index = mp->index;
576 vlib_process_signal_event (vm, hsm->node_index, 1 /* evt */ ,
577 0 /* data */ );
578}
579
580#define foreach_tcp_http_server_api_msg \
581_(MEMCLNT_CREATE_REPLY, memclnt_create_reply) \
582
583static clib_error_t *
584tcp_http_server_api_hookup (vlib_main_t * vm)
585{
586 vl_msg_api_msg_config_t _c, *c = &_c;
587
588 /* Hook up client-side static APIs to our handlers */
589#define _(N,n) do { \
590 c->id = VL_API_##N; \
591 c->name = #n; \
592 c->handler = vl_api_##n##_t_handler; \
593 c->cleanup = vl_noop_handler; \
594 c->endian = vl_api_##n##_t_endian; \
595 c->print = vl_api_##n##_t_print; \
596 c->size = sizeof(vl_api_##n##_t); \
597 c->traced = 1; /* trace, so these msgs print */ \
598 c->replay = 0; /* don't replay client create/delete msgs */ \
599 c->message_bounce = 0; /* don't bounce this message */ \
600 vl_msg_api_config(c);} while (0);
601
602 foreach_tcp_http_server_api_msg;
603#undef _
604
605 return 0;
606}
607
608static clib_error_t *
609server_create_command_fn (vlib_main_t * vm,
610 unformat_input_t * input, vlib_cli_command_t * cmd)
611{
612 int rv;
613
614 tcp_http_server_api_hookup (vm);
615 vnet_session_enable_disable (vm, 1 /* turn on TCP, etc. */ );
616 rv = server_create (vm);
617 switch (rv)
618 {
619 case 0:
620 break;
621 default:
622 return clib_error_return (0, "server_create returned %d", rv);
623 }
624 return 0;
625}
626
627/* *INDENT-OFF* */
628VLIB_CLI_COMMAND (server_create_command, static) =
629{
630 .path = "test http server",
631 .short_help = "test http server",
632 .function = server_create_command_fn,
633};
634/* *INDENT-ON* */
635
636static clib_error_t *
637builtin_http_server_main_init (vlib_main_t * vm)
638{
639 http_server_main_t *hsm = &http_server_main;
640 hsm->my_client_index = ~0;
641 hsm->vlib_main = vm;
642
643 return 0;
644}
645
646VLIB_INIT_FUNCTION (builtin_http_server_main_init);
647
648/*
649* fd.io coding-style-patch-verification: ON
650*
651* Local Variables:
652* eval: (c-set-style "gnu")
653* End:
654*/