blob: 37441bb2188533773529f2d44433ec13bc639a92 [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;
Florin Coras4399c2e2018-01-25 06:34:42 -080030} http_server_args;
Florin Coras0e9c33b2017-08-14 22:33:41 -070031
32typedef struct
33{
Florin Coras149d62f2017-11-01 15:05:49 -070034 u8 **rx_buf;
Florin Coras3c2fed52018-07-04 04:15:05 -070035 svm_msg_q_t **vpp_queue;
Dave Barach1015a1e2017-05-08 19:15:03 -040036 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 */
Florin Corase86a8ed2018-01-05 03:20:25 -080043 svm_queue_t *vl_input_queue;
Dave Barach1015a1e2017-05-08 19:15:03 -040044
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;
Florin Coras1d6d0852017-11-17 14:26:01 -080052
53 u32 prealloc_fifos;
54 u32 private_segment_size;
55 u32 fifo_size;
Florin Coras371ca502018-02-21 12:07:41 -080056 u8 *uri;
Dave Barach1015a1e2017-05-08 19:15:03 -040057 vlib_main_t *vlib_main;
58} http_server_main_t;
59
60http_server_main_t http_server_main;
61
62static void
Florin Coras4399c2e2018-01-25 06:34:42 -080063free_http_process (http_server_args * args)
Dave Barach1015a1e2017-05-08 19:15:03 -040064{
65 vlib_node_runtime_t *rt;
66 vlib_main_t *vm = &vlib_global_main;
67 http_server_main_t *hsm = &http_server_main;
68 vlib_node_t *n;
69 u32 node_index;
Florin Coras4399c2e2018-01-25 06:34:42 -080070 http_server_args **save_args;
Dave Barach1015a1e2017-05-08 19:15:03 -040071
Florin Coras0e9c33b2017-08-14 22:33:41 -070072 node_index = args->node_index;
Dave Barach1015a1e2017-05-08 19:15:03 -040073 ASSERT (node_index != 0);
74
75 n = vlib_get_node (vm, node_index);
Florin Coras0e9c33b2017-08-14 22:33:41 -070076 rt = vlib_node_get_runtime (vm, n->index);
77 save_args = vlib_node_get_runtime_data (vm, n->index);
Dave Barach1015a1e2017-05-08 19:15:03 -040078
Dave Barach1015a1e2017-05-08 19:15:03 -040079 /* Reset process session pointer */
Florin Coras0e9c33b2017-08-14 22:33:41 -070080 clib_mem_free (*save_args);
81 *save_args = 0;
Dave Barach1015a1e2017-05-08 19:15:03 -040082
83 /* Turn off the process node */
84 vlib_node_set_state (vm, rt->node_index, VLIB_NODE_STATE_DISABLED);
85
86 /* add node index to the freelist */
87 vec_add1 (hsm->free_http_cli_process_node_indices, node_index);
88}
89
Florin Coras898cd8f2018-06-08 02:02:13 -070090/* *INDENT-OFF* */
91static const char *http_response =
92 "HTTP/1.1 200 OK\r\n"
93 "Content-Type: text/html\r\n"
94 "Expires: Mon, 11 Jan 1970 10:10:10 GMT\r\n"
95 "Connection: keep-alive \r\n"
96 "Pragma: no-cache\r\n"
97 "Content-Length: %d\r\n\r\n%s";
Dave Barach1015a1e2017-05-08 19:15:03 -040098
Florin Coras898cd8f2018-06-08 02:02:13 -070099static const char *http_error_template =
100 "HTTP/1.1 %s\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"
105 "Content-Length: 0\r\n\r\n";
Dave Barach1015a1e2017-05-08 19:15:03 -0400106
107/* Header, including incantation to suppress favicon.ico requests */
Florin Coras898cd8f2018-06-08 02:02:13 -0700108static const char *html_header_template =
109 "<html><head><title>%v</title></head>"
110 "<link rel=\"icon\" href=\"data:,\">"
111 "<body><pre>";
Dave Barach1015a1e2017-05-08 19:15:03 -0400112
Florin Coras898cd8f2018-06-08 02:02:13 -0700113static const char *html_footer =
114 "</pre></body></html>\r\n";
Dave Barach1015a1e2017-05-08 19:15:03 -0400115
Florin Coras898cd8f2018-06-08 02:02:13 -0700116static const char *html_header_static =
117 "<html><head><title>static reply</title></head>"
118 "<link rel=\"icon\" href=\"data:,\">"
119 "<body><pre>hello</pre></body></html>\r\n";
120/* *INDENT-ON* */
Florin Coras149d62f2017-11-01 15:05:49 -0700121
122static u8 *static_http;
123
Dave Barach1015a1e2017-05-08 19:15:03 -0400124static void
125http_cli_output (uword arg, u8 * buffer, uword buffer_bytes)
126{
127 u8 **output_vecp = (u8 **) arg;
128 u8 *output_vec;
129 u32 offset;
130
131 output_vec = *output_vecp;
132
133 offset = vec_len (output_vec);
134 vec_validate (output_vec, offset + buffer_bytes - 1);
Dave Barach178cf492018-11-13 16:34:13 -0500135 clib_memcpy_fast (output_vec + offset, buffer, buffer_bytes);
Dave Barach1015a1e2017-05-08 19:15:03 -0400136
137 *output_vecp = output_vec;
138}
139
140void
Florin Coras149d62f2017-11-01 15:05:49 -0700141send_data (stream_session_t * s, u8 * data)
Dave Barach1015a1e2017-05-08 19:15:03 -0400142{
Dave Barach1015a1e2017-05-08 19:15:03 -0400143 u32 offset, bytes_to_send;
144 f64 delay = 10e-3;
145 http_server_main_t *hsm = &http_server_main;
146 vlib_main_t *vm = hsm->vlib_main;
147 f64 last_sent_timer = vlib_time_now (vm);
148
149 bytes_to_send = vec_len (data);
150 offset = 0;
151
152 while (bytes_to_send > 0)
153 {
154 int actual_transfer;
155
156 actual_transfer = svm_fifo_enqueue_nowait
157 (s->server_tx_fifo, bytes_to_send, data + offset);
158
159 /* Made any progress? */
160 if (actual_transfer <= 0)
161 {
162 vlib_process_suspend (vm, delay);
163 /* 10s deadman timer */
164 if (vlib_time_now (vm) > last_sent_timer + 10.0)
165 {
166 /* $$$$ FC: reset transport session here? */
167 break;
168 }
169 /* Exponential backoff, within reason */
170 if (delay < 1.0)
171 delay = delay * 2.0;
172 }
173 else
174 {
175 last_sent_timer = vlib_time_now (vm);
176 offset += actual_transfer;
177 bytes_to_send -= actual_transfer;
178
179 if (svm_fifo_set_event (s->server_tx_fifo))
Florin Coras3c2fed52018-07-04 04:15:05 -0700180 session_send_io_evt_to_thread (s->server_tx_fifo,
181 FIFO_EVENT_APP_TX);
Dave Barach1015a1e2017-05-08 19:15:03 -0400182 delay = 10e-3;
183 }
184 }
185}
186
187static void
Florin Coras149d62f2017-11-01 15:05:49 -0700188send_error (stream_session_t * s, char *str)
Dave Barach1015a1e2017-05-08 19:15:03 -0400189{
190 u8 *data;
191
192 data = format (0, http_error_template, str);
Florin Coras149d62f2017-11-01 15:05:49 -0700193 send_data (s, data);
Dave Barach1015a1e2017-05-08 19:15:03 -0400194 vec_free (data);
195}
196
197static uword
198http_cli_process (vlib_main_t * vm,
199 vlib_node_runtime_t * rt, vlib_frame_t * f)
200{
201 http_server_main_t *hsm = &http_server_main;
202 u8 *request = 0, *reply = 0;
Florin Coras4399c2e2018-01-25 06:34:42 -0800203 http_server_args **save_args;
204 http_server_args *args;
Florin Coras149d62f2017-11-01 15:05:49 -0700205 stream_session_t *s;
Dave Barach1015a1e2017-05-08 19:15:03 -0400206 unformat_input_t input;
207 int i;
208 u8 *http = 0, *html = 0;
209
Florin Coras0e9c33b2017-08-14 22:33:41 -0700210 save_args = vlib_node_get_runtime_data (hsm->vlib_main, rt->node_index);
211 args = *save_args;
Florin Coras149d62f2017-11-01 15:05:49 -0700212 s = session_get_from_handle (args->session_handle);
213 ASSERT (s);
Dave Barach1015a1e2017-05-08 19:15:03 -0400214
Florin Coras0e9c33b2017-08-14 22:33:41 -0700215 request = (u8 *) (void *) (args->data);
Dave Barach1015a1e2017-05-08 19:15:03 -0400216 if (vec_len (request) < 7)
217 {
Florin Coras149d62f2017-11-01 15:05:49 -0700218 send_error (s, "400 Bad Request");
Dave Barach1015a1e2017-05-08 19:15:03 -0400219 goto out;
220 }
221
222 for (i = 0; i < vec_len (request) - 4; i++)
223 {
224 if (request[i] == 'G' &&
225 request[i + 1] == 'E' &&
226 request[i + 2] == 'T' && request[i + 3] == ' ')
227 goto found;
228 }
229bad_request:
Florin Coras149d62f2017-11-01 15:05:49 -0700230 send_error (s, "400 Bad Request");
Dave Barach1015a1e2017-05-08 19:15:03 -0400231 goto out;
232
233found:
234 /* Lose "GET " */
235 vec_delete (request, i + 5, 0);
236
237 /* Replace slashes with spaces, stop at the end of the path */
238 i = 0;
239 while (1)
240 {
241 if (request[i] == '/')
242 request[i] = ' ';
243 else if (request[i] == ' ')
244 {
245 /* vlib_cli_input is vector-based, no need for a NULL */
246 _vec_len (request) = i;
247 break;
248 }
249 i++;
250 /* Should never happen */
251 if (i == vec_len (request))
252 goto bad_request;
253 }
254
255 /* Generate the html header */
256 html = format (0, html_header_template, request /* title */ );
257
258 /* Run the command */
259 unformat_init_vector (&input, request);
260 vlib_cli_input (vm, &input, http_cli_output, (uword) & reply);
261 unformat_free (&input);
262 request = 0;
263
264 /* Generate the html page */
265 html = format (html, "%v", reply);
266 html = format (html, html_footer);
267 /* And the http reply */
268 http = format (0, http_response, vec_len (html), html);
269
270 /* Send it */
Florin Coras149d62f2017-11-01 15:05:49 -0700271 send_data (s, http);
Dave Barach1015a1e2017-05-08 19:15:03 -0400272
273out:
274 /* Cleanup */
275 vec_free (request);
276 vec_free (reply);
277 vec_free (html);
278 vec_free (http);
279
Florin Coras0e9c33b2017-08-14 22:33:41 -0700280 free_http_process (args);
Dave Barach1015a1e2017-05-08 19:15:03 -0400281 return (0);
282}
283
284static void
Florin Coras4399c2e2018-01-25 06:34:42 -0800285alloc_http_process (http_server_args * args)
Dave Barach1015a1e2017-05-08 19:15:03 -0400286{
287 char *name;
288 vlib_node_t *n;
289 http_server_main_t *hsm = &http_server_main;
290 vlib_main_t *vm = hsm->vlib_main;
291 uword l = vec_len (hsm->free_http_cli_process_node_indices);
Florin Coras4399c2e2018-01-25 06:34:42 -0800292 http_server_args **save_args;
Dave Barach1015a1e2017-05-08 19:15:03 -0400293
294 if (vec_len (hsm->free_http_cli_process_node_indices) > 0)
295 {
296 n = vlib_get_node (vm, hsm->free_http_cli_process_node_indices[l - 1]);
Dave Barach1015a1e2017-05-08 19:15:03 -0400297 vlib_node_set_state (vm, n->index, VLIB_NODE_STATE_POLLING);
Dave Barach1015a1e2017-05-08 19:15:03 -0400298 _vec_len (hsm->free_http_cli_process_node_indices) = l - 1;
299 }
300 else
301 {
302 static vlib_node_registration_t r = {
303 .function = http_cli_process,
304 .type = VLIB_NODE_TYPE_PROCESS,
305 .process_log2_n_stack_bytes = 16,
306 .runtime_data_bytes = sizeof (void *),
307 };
308
309 name = (char *) format (0, "http-cli-%d", l);
Dave Barach1015a1e2017-05-08 19:15:03 -0400310 r.name = name;
311 vlib_register_node (vm, &r);
312 vec_free (name);
313
314 n = vlib_get_node (vm, r.index);
315 }
316
Florin Coras0e9c33b2017-08-14 22:33:41 -0700317 /* Save the node index in the args. It won't be zero. */
318 args->node_index = n->index;
Dave Barach1015a1e2017-05-08 19:15:03 -0400319
Florin Coras0e9c33b2017-08-14 22:33:41 -0700320 /* Save the args (pointer) in the node runtime */
321 save_args = vlib_node_get_runtime_data (vm, n->index);
322 *save_args = args;
Dave Barach1015a1e2017-05-08 19:15:03 -0400323
324 vlib_start_process (vm, n->runtime_index);
325}
326
Florin Coras0e9c33b2017-08-14 22:33:41 -0700327static void
328alloc_http_process_callback (void *cb_args)
329{
Florin Coras4399c2e2018-01-25 06:34:42 -0800330 alloc_http_process ((http_server_args *) cb_args);
Florin Coras0e9c33b2017-08-14 22:33:41 -0700331}
332
333static int
Florin Coras149d62f2017-11-01 15:05:49 -0700334session_rx_request (stream_session_t * s)
Florin Coras0e9c33b2017-08-14 22:33:41 -0700335{
Florin Coras0e9c33b2017-08-14 22:33:41 -0700336 http_server_main_t *hsm = &http_server_main;
337 svm_fifo_t *rx_fifo;
Florin Coras149d62f2017-11-01 15:05:49 -0700338 u32 max_dequeue;
339 int actual_transfer;
Florin Coras0e9c33b2017-08-14 22:33:41 -0700340
341 rx_fifo = s->server_rx_fifo;
342 max_dequeue = svm_fifo_max_dequeue (rx_fifo);
343 svm_fifo_unset_event (rx_fifo);
344 if (PREDICT_FALSE (max_dequeue == 0))
Florin Coras149d62f2017-11-01 15:05:49 -0700345 return -1;
Florin Coras0e9c33b2017-08-14 22:33:41 -0700346
Florin Coras149d62f2017-11-01 15:05:49 -0700347 vec_validate (hsm->rx_buf[s->thread_index], max_dequeue - 1);
348 _vec_len (hsm->rx_buf[s->thread_index]) = max_dequeue;
Florin Coras0e9c33b2017-08-14 22:33:41 -0700349
350 actual_transfer = svm_fifo_dequeue_nowait (rx_fifo, max_dequeue,
Florin Coras149d62f2017-11-01 15:05:49 -0700351 hsm->rx_buf[s->thread_index]);
Florin Coras0e9c33b2017-08-14 22:33:41 -0700352 ASSERT (actual_transfer > 0);
Florin Coras149d62f2017-11-01 15:05:49 -0700353 _vec_len (hsm->rx_buf[s->thread_index]) = actual_transfer;
354 return 0;
355}
356
357static int
358http_server_rx_callback (stream_session_t * s)
359{
360 http_server_main_t *hsm = &http_server_main;
Florin Coras4399c2e2018-01-25 06:34:42 -0800361 http_server_args *args;
JingLiuZTE7cafe762017-11-08 15:01:27 +0800362 int rv;
Florin Coras149d62f2017-11-01 15:05:49 -0700363
JingLiuZTE7cafe762017-11-08 15:01:27 +0800364 rv = session_rx_request (s);
365 if (rv)
366 return rv;
Florin Coras0e9c33b2017-08-14 22:33:41 -0700367
368 /* send the command to a new/recycled vlib process */
369 args = clib_mem_alloc (sizeof (*args));
Florin Coras149d62f2017-11-01 15:05:49 -0700370 args->data = vec_dup (hsm->rx_buf[s->thread_index]);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700371 args->session_handle = session_handle (s);
Florin Coras0e9c33b2017-08-14 22:33:41 -0700372
373 /* Send an RPC request via the thread-0 input node */
374 if (vlib_get_thread_index () != 0)
Florin Coras3c2fed52018-07-04 04:15:05 -0700375 session_send_rpc_evt_to_thread (0, alloc_http_process_callback, args);
Florin Coras0e9c33b2017-08-14 22:33:41 -0700376 else
377 alloc_http_process (args);
378 return 0;
379}
380
Dave Barach1015a1e2017-05-08 19:15:03 -0400381static int
Florin Coras149d62f2017-11-01 15:05:49 -0700382http_server_rx_callback_static (stream_session_t * s)
383{
384 http_server_main_t *hsm = &http_server_main;
385 u8 *request = 0;
386 int i;
JingLiuZTE7cafe762017-11-08 15:01:27 +0800387 int rv;
Florin Coras149d62f2017-11-01 15:05:49 -0700388
JingLiuZTE7cafe762017-11-08 15:01:27 +0800389 rv = session_rx_request (s);
390 if (rv)
391 return rv;
Florin Coras149d62f2017-11-01 15:05:49 -0700392
393 request = hsm->rx_buf[s->thread_index];
394 if (vec_len (request) < 7)
395 {
396 send_error (s, "400 Bad Request");
397 goto out;
398 }
399
400 for (i = 0; i < vec_len (request) - 4; i++)
401 {
402 if (request[i] == 'G' &&
403 request[i + 1] == 'E' &&
404 request[i + 2] == 'T' && request[i + 3] == ' ')
405 goto found;
406 }
407 send_error (s, "400 Bad Request");
408 goto out;
409
410found:
411
412 /* Send it */
413 send_data (s, static_http);
414
415out:
416 /* Cleanup */
417 vec_free (request);
Florin Coras156e5ca2017-11-13 12:07:38 -0800418 hsm->rx_buf[s->thread_index] = request;
Florin Coras149d62f2017-11-01 15:05:49 -0700419 return 0;
420}
421
422static int
Florin Coras4399c2e2018-01-25 06:34:42 -0800423http_server_session_accept_callback (stream_session_t * s)
Dave Barach1015a1e2017-05-08 19:15:03 -0400424{
425 http_server_main_t *bsm = &http_server_main;
426
427 bsm->vpp_queue[s->thread_index] =
428 session_manager_get_vpp_event_queue (s->thread_index);
429 s->session_state = SESSION_STATE_READY;
430 bsm->byte_index = 0;
431 return 0;
432}
433
434static void
Florin Coras4399c2e2018-01-25 06:34:42 -0800435http_server_session_disconnect_callback (stream_session_t * s)
Dave Barach1015a1e2017-05-08 19:15:03 -0400436{
437 http_server_main_t *bsm = &http_server_main;
438 vnet_disconnect_args_t _a, *a = &_a;
439
Florin Coras3cbc04b2017-10-02 00:18:51 -0700440 a->handle = session_handle (s);
Dave Barach1015a1e2017-05-08 19:15:03 -0400441 a->app_index = bsm->app_index;
442 vnet_disconnect_session (a);
443}
444
445static void
Florin Coras4399c2e2018-01-25 06:34:42 -0800446http_server_session_reset_callback (stream_session_t * s)
Dave Barach1015a1e2017-05-08 19:15:03 -0400447{
448 clib_warning ("called.. ");
Dave Barach1015a1e2017-05-08 19:15:03 -0400449 stream_session_cleanup (s);
450}
451
Dave Barach1015a1e2017-05-08 19:15:03 -0400452static int
Florin Coras4399c2e2018-01-25 06:34:42 -0800453http_server_session_connected_callback (u32 app_index, u32 api_context,
454 stream_session_t * s, u8 is_fail)
Dave Barach1015a1e2017-05-08 19:15:03 -0400455{
456 clib_warning ("called...");
457 return -1;
458}
459
460static int
Florin Corasfa76a762018-11-29 12:40:10 -0800461http_server_add_segment_callback (u32 client_index, u64 segment_handle)
Dave Barach1015a1e2017-05-08 19:15:03 -0400462{
463 clib_warning ("called...");
464 return -1;
465}
466
Florin Coras4399c2e2018-01-25 06:34:42 -0800467static session_cb_vft_t http_server_session_cb_vft = {
468 .session_accept_callback = http_server_session_accept_callback,
469 .session_disconnect_callback = http_server_session_disconnect_callback,
470 .session_connected_callback = http_server_session_connected_callback,
471 .add_segment_callback = http_server_add_segment_callback,
Florin Coras371ca502018-02-21 12:07:41 -0800472 .builtin_app_rx_callback = http_server_rx_callback,
Florin Coras4399c2e2018-01-25 06:34:42 -0800473 .session_reset_callback = http_server_session_reset_callback
Dave Barach1015a1e2017-05-08 19:15:03 -0400474};
475
476/* Abuse VPP's input queue */
477static int
478create_api_loopback (vlib_main_t * vm)
479{
480 http_server_main_t *hsm = &http_server_main;
Dave Barach1015a1e2017-05-08 19:15:03 -0400481 api_main_t *am = &api_main;
482 vl_shmem_hdr_t *shmem_hdr;
Dave Barach1015a1e2017-05-08 19:15:03 -0400483
484 shmem_hdr = am->shmem_hdr;
485 hsm->vl_input_queue = shmem_hdr->vl_input_queue;
Florin Coras0e9c33b2017-08-14 22:33:41 -0700486 hsm->my_client_index =
Florin Coras4399c2e2018-01-25 06:34:42 -0800487 vl_api_memclnt_create_internal ("http_server", hsm->vl_input_queue);
Dave Barach1015a1e2017-05-08 19:15:03 -0400488 return 0;
489}
490
491static int
492server_attach ()
493{
Florin Coras371ca502018-02-21 12:07:41 -0800494 vnet_app_add_tls_cert_args_t _a_cert, *a_cert = &_a_cert;
495 vnet_app_add_tls_key_args_t _a_key, *a_key = &_a_key;
Dave Barach1015a1e2017-05-08 19:15:03 -0400496 http_server_main_t *hsm = &http_server_main;
Florin Corasff6e7692017-12-11 04:59:01 -0800497 u64 options[APP_OPTIONS_N_OPTIONS];
Dave Barach1015a1e2017-05-08 19:15:03 -0400498 vnet_app_attach_args_t _a, *a = &_a;
Florin Corasff6e7692017-12-11 04:59:01 -0800499 u32 segment_size = 128 << 20;
Dave Barach1015a1e2017-05-08 19:15:03 -0400500
Dave Barachb7b92992018-10-17 10:38:51 -0400501 clib_memset (a, 0, sizeof (*a));
502 clib_memset (options, 0, sizeof (options));
Dave Barach1015a1e2017-05-08 19:15:03 -0400503
Florin Corasff6e7692017-12-11 04:59:01 -0800504 if (hsm->private_segment_size)
505 segment_size = hsm->private_segment_size;
506
Dave Barach1015a1e2017-05-08 19:15:03 -0400507 a->api_client_index = hsm->my_client_index;
Florin Coras4399c2e2018-01-25 06:34:42 -0800508 a->session_cb_vft = &http_server_session_cb_vft;
Dave Barach1015a1e2017-05-08 19:15:03 -0400509 a->options = options;
Florin Corasff6e7692017-12-11 04:59:01 -0800510 a->options[APP_OPTIONS_SEGMENT_SIZE] = segment_size;
511 a->options[APP_OPTIONS_RX_FIFO_SIZE] =
Florin Coras1d6d0852017-11-17 14:26:01 -0800512 hsm->fifo_size ? hsm->fifo_size : 8 << 10;
Florin Corasff6e7692017-12-11 04:59:01 -0800513 a->options[APP_OPTIONS_TX_FIFO_SIZE] =
Florin Coras1d6d0852017-11-17 14:26:01 -0800514 hsm->fifo_size ? hsm->fifo_size : 32 << 10;
Florin Coras7999e832017-10-31 01:51:04 -0700515 a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
Florin Coras1d6d0852017-11-17 14:26:01 -0800516 a->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = hsm->prealloc_fifos;
Dave Barach1015a1e2017-05-08 19:15:03 -0400517
518 if (vnet_application_attach (a))
519 {
520 clib_warning ("failed to attach server");
521 return -1;
522 }
523 hsm->app_index = a->app_index;
Florin Coras371ca502018-02-21 12:07:41 -0800524
Dave Barachb7b92992018-10-17 10:38:51 -0400525 clib_memset (a_cert, 0, sizeof (*a_cert));
Florin Coras371ca502018-02-21 12:07:41 -0800526 a_cert->app_index = a->app_index;
527 vec_validate (a_cert->cert, test_srv_crt_rsa_len);
Dave Barach178cf492018-11-13 16:34:13 -0500528 clib_memcpy_fast (a_cert->cert, test_srv_crt_rsa, test_srv_crt_rsa_len);
Florin Coras371ca502018-02-21 12:07:41 -0800529 vnet_app_add_tls_cert (a_cert);
530
Dave Barachb7b92992018-10-17 10:38:51 -0400531 clib_memset (a_key, 0, sizeof (*a_key));
Florin Coras371ca502018-02-21 12:07:41 -0800532 a_key->app_index = a->app_index;
533 vec_validate (a_key->key, test_srv_key_rsa_len);
Dave Barach178cf492018-11-13 16:34:13 -0500534 clib_memcpy_fast (a_key->key, test_srv_key_rsa, test_srv_key_rsa_len);
Florin Coras371ca502018-02-21 12:07:41 -0800535 vnet_app_add_tls_key (a_key);
536
Dave Barach1015a1e2017-05-08 19:15:03 -0400537 return 0;
538}
539
540static int
Florin Coras4399c2e2018-01-25 06:34:42 -0800541http_server_listen ()
Dave Barach1015a1e2017-05-08 19:15:03 -0400542{
543 http_server_main_t *hsm = &http_server_main;
544 vnet_bind_args_t _a, *a = &_a;
Dave Barachb7b92992018-10-17 10:38:51 -0400545 clib_memset (a, 0, sizeof (*a));
Dave Barach1015a1e2017-05-08 19:15:03 -0400546 a->app_index = hsm->app_index;
547 a->uri = "tcp://0.0.0.0/80";
Florin Coras371ca502018-02-21 12:07:41 -0800548 if (hsm->uri)
549 a->uri = (char *) hsm->uri;
Dave Barach1015a1e2017-05-08 19:15:03 -0400550 return vnet_bind_uri (a);
551}
552
553static int
Florin Coras4399c2e2018-01-25 06:34:42 -0800554http_server_create (vlib_main_t * vm)
Dave Barach1015a1e2017-05-08 19:15:03 -0400555{
556 http_server_main_t *hsm = &http_server_main;
557 u32 num_threads;
558 vlib_thread_main_t *vtm = vlib_get_thread_main ();
559
Florin Coras0e9c33b2017-08-14 22:33:41 -0700560 ASSERT (hsm->my_client_index == (u32) ~ 0);
561 if (create_api_loopback (vm))
562 return -1;
Dave Barach1015a1e2017-05-08 19:15:03 -0400563
564 num_threads = 1 /* main thread */ + vtm->n_threads;
565 vec_validate (http_server_main.vpp_queue, num_threads - 1);
566
567 if (server_attach ())
568 {
569 clib_warning ("failed to attach server");
570 return -1;
571 }
Florin Coras4399c2e2018-01-25 06:34:42 -0800572 if (http_server_listen ())
Dave Barach1015a1e2017-05-08 19:15:03 -0400573 {
574 clib_warning ("failed to start listening");
575 return -1;
576 }
577 return 0;
578}
579
Dave Barach1015a1e2017-05-08 19:15:03 -0400580static clib_error_t *
Florin Coras4399c2e2018-01-25 06:34:42 -0800581http_server_create_command_fn (vlib_main_t * vm,
582 unformat_input_t * input,
583 vlib_cli_command_t * cmd)
Dave Barach1015a1e2017-05-08 19:15:03 -0400584{
Florin Coras0e9c33b2017-08-14 22:33:41 -0700585 http_server_main_t *hsm = &http_server_main;
Florin Coras149d62f2017-11-01 15:05:49 -0700586 int rv, is_static = 0;
Florin Coras1d6d0852017-11-17 14:26:01 -0800587 u64 seg_size;
Florin Coras149d62f2017-11-01 15:05:49 -0700588 u8 *html;
Dave Barach1015a1e2017-05-08 19:15:03 -0400589
Florin Coras1d6d0852017-11-17 14:26:01 -0800590 hsm->prealloc_fifos = 0;
591 hsm->private_segment_size = 0;
592 hsm->fifo_size = 0;
Florin Coras149d62f2017-11-01 15:05:49 -0700593 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
594 {
595 if (unformat (input, "static"))
596 is_static = 1;
Florin Coras1d6d0852017-11-17 14:26:01 -0800597 else if (unformat (input, "prealloc-fifos %d", &hsm->prealloc_fifos))
598 ;
599 else if (unformat (input, "private-segment-size %U",
600 unformat_memory_size, &seg_size))
601 {
602 if (seg_size >= 0x100000000ULL)
603 {
604 vlib_cli_output (vm, "private segment size %llu, too large",
605 seg_size);
606 return 0;
607 }
608 hsm->private_segment_size = seg_size;
609 }
610 else if (unformat (input, "fifo-size %d", &hsm->fifo_size))
611 hsm->fifo_size <<= 10;
Florin Coras371ca502018-02-21 12:07:41 -0800612 else if (unformat (input, "uri %s", &hsm->uri))
613 ;
Florin Coras149d62f2017-11-01 15:05:49 -0700614 else
615 return clib_error_return (0, "unknown input `%U'",
616 format_unformat_error, input);
617 }
Florin Coras0e9c33b2017-08-14 22:33:41 -0700618 if (hsm->my_client_index != (u32) ~ 0)
619 return clib_error_return (0, "test http server is already running");
620
Dave Barach1015a1e2017-05-08 19:15:03 -0400621 vnet_session_enable_disable (vm, 1 /* turn on TCP, etc. */ );
Florin Coras149d62f2017-11-01 15:05:49 -0700622
623 if (is_static)
624 {
Florin Coras371ca502018-02-21 12:07:41 -0800625 http_server_session_cb_vft.builtin_app_rx_callback =
Florin Coras149d62f2017-11-01 15:05:49 -0700626 http_server_rx_callback_static;
627 html = format (0, html_header_static);
628 static_http = format (0, http_response, vec_len (html), html);
629 }
Florin Coras4399c2e2018-01-25 06:34:42 -0800630 rv = http_server_create (vm);
Dave Barach1015a1e2017-05-08 19:15:03 -0400631 switch (rv)
632 {
633 case 0:
634 break;
635 default:
636 return clib_error_return (0, "server_create returned %d", rv);
637 }
638 return 0;
639}
640
641/* *INDENT-OFF* */
Florin Coras4399c2e2018-01-25 06:34:42 -0800642VLIB_CLI_COMMAND (http_server_create_command, static) =
Dave Barach1015a1e2017-05-08 19:15:03 -0400643{
644 .path = "test http server",
645 .short_help = "test http server",
Florin Coras4399c2e2018-01-25 06:34:42 -0800646 .function = http_server_create_command_fn,
Dave Barach1015a1e2017-05-08 19:15:03 -0400647};
648/* *INDENT-ON* */
649
650static clib_error_t *
Florin Coras4399c2e2018-01-25 06:34:42 -0800651http_server_main_init (vlib_main_t * vm)
Dave Barach1015a1e2017-05-08 19:15:03 -0400652{
653 http_server_main_t *hsm = &http_server_main;
Florin Coras149d62f2017-11-01 15:05:49 -0700654 vlib_thread_main_t *vtm = vlib_get_thread_main ();
655 u32 num_threads;
656
Dave Barach1015a1e2017-05-08 19:15:03 -0400657 hsm->my_client_index = ~0;
658 hsm->vlib_main = vm;
Florin Coras149d62f2017-11-01 15:05:49 -0700659 num_threads = 1 /* main thread */ + vtm->n_threads;
660 vec_validate (hsm->rx_buf, num_threads - 1);
Dave Barach1015a1e2017-05-08 19:15:03 -0400661 return 0;
662}
663
Florin Coras4399c2e2018-01-25 06:34:42 -0800664VLIB_INIT_FUNCTION (http_server_main_init);
Dave Barach1015a1e2017-05-08 19:15:03 -0400665
666/*
667* fd.io coding-style-patch-verification: ON
668*
669* Local Variables:
670* eval: (c-set-style "gnu")
671* End:
672*/