blob: 763a46e95906ba028c1e295972fa89bb6a6b6b61 [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;
516 a->segment_name = segment_name;
517 a->segment_name_length = ARRAY_LEN (segment_name);
518
519 if (vnet_application_attach (a))
520 {
521 clib_warning ("failed to attach server");
522 return -1;
523 }
524 hsm->app_index = a->app_index;
525 return 0;
526}
527
528static int
529server_listen ()
530{
531 http_server_main_t *hsm = &http_server_main;
532 vnet_bind_args_t _a, *a = &_a;
533 memset (a, 0, sizeof (*a));
534 a->app_index = hsm->app_index;
535 a->uri = "tcp://0.0.0.0/80";
536 return vnet_bind_uri (a);
537}
538
539static int
540server_create (vlib_main_t * vm)
541{
542 http_server_main_t *hsm = &http_server_main;
543 u32 num_threads;
544 vlib_thread_main_t *vtm = vlib_get_thread_main ();
545
546 if (hsm->my_client_index == (u32) ~ 0)
547 {
548 if (create_api_loopback (vm))
549 return -1;
550 }
551
552 num_threads = 1 /* main thread */ + vtm->n_threads;
553 vec_validate (http_server_main.vpp_queue, num_threads - 1);
554
555 if (server_attach ())
556 {
557 clib_warning ("failed to attach server");
558 return -1;
559 }
560 if (server_listen ())
561 {
562 clib_warning ("failed to start listening");
563 return -1;
564 }
565 return 0;
566}
567
568/* Get our api client index */
569static void
570vl_api_memclnt_create_reply_t_handler (vl_api_memclnt_create_reply_t * mp)
571{
572 vlib_main_t *vm = vlib_get_main ();
573 http_server_main_t *hsm = &http_server_main;
574 hsm->my_client_index = mp->index;
575 vlib_process_signal_event (vm, hsm->node_index, 1 /* evt */ ,
576 0 /* data */ );
577}
578
579#define foreach_tcp_http_server_api_msg \
580_(MEMCLNT_CREATE_REPLY, memclnt_create_reply) \
581
582static clib_error_t *
583tcp_http_server_api_hookup (vlib_main_t * vm)
584{
585 vl_msg_api_msg_config_t _c, *c = &_c;
586
587 /* Hook up client-side static APIs to our handlers */
588#define _(N,n) do { \
589 c->id = VL_API_##N; \
590 c->name = #n; \
591 c->handler = vl_api_##n##_t_handler; \
592 c->cleanup = vl_noop_handler; \
593 c->endian = vl_api_##n##_t_endian; \
594 c->print = vl_api_##n##_t_print; \
595 c->size = sizeof(vl_api_##n##_t); \
596 c->traced = 1; /* trace, so these msgs print */ \
597 c->replay = 0; /* don't replay client create/delete msgs */ \
598 c->message_bounce = 0; /* don't bounce this message */ \
599 vl_msg_api_config(c);} while (0);
600
601 foreach_tcp_http_server_api_msg;
602#undef _
603
604 return 0;
605}
606
607static clib_error_t *
608server_create_command_fn (vlib_main_t * vm,
609 unformat_input_t * input, vlib_cli_command_t * cmd)
610{
611 int rv;
612
613 tcp_http_server_api_hookup (vm);
614 vnet_session_enable_disable (vm, 1 /* turn on TCP, etc. */ );
615 rv = server_create (vm);
616 switch (rv)
617 {
618 case 0:
619 break;
620 default:
621 return clib_error_return (0, "server_create returned %d", rv);
622 }
623 return 0;
624}
625
626/* *INDENT-OFF* */
627VLIB_CLI_COMMAND (server_create_command, static) =
628{
629 .path = "test http server",
630 .short_help = "test http server",
631 .function = server_create_command_fn,
632};
633/* *INDENT-ON* */
634
635static clib_error_t *
636builtin_http_server_main_init (vlib_main_t * vm)
637{
638 http_server_main_t *hsm = &http_server_main;
639 hsm->my_client_index = ~0;
640 hsm->vlib_main = vm;
641
642 return 0;
643}
644
645VLIB_INIT_FUNCTION (builtin_http_server_main_init);
646
647/*
648* fd.io coding-style-patch-verification: ON
649*
650* Local Variables:
651* eval: (c-set-style "gnu")
652* End:
653*/