blob: 719608a79d11bf00fe7fa7f75b107755506a582c [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 Coras844a36d2018-12-20 09:50:50 -080027 u32 hs_index;
28 u32 thread_index;
Florin Coras0e9c33b2017-08-14 22:33:41 -070029 u64 node_index;
Florin Coras4399c2e2018-01-25 06:34:42 -080030} http_server_args;
Florin Coras0e9c33b2017-08-14 22:33:41 -070031
Florin Coras844a36d2018-12-20 09:50:50 -080032typedef enum
33{
34 HTTP_STATE_CLOSED,
35 HTTP_STATE_ESTABLISHED,
36 HTTP_STATE_OK_SENT,
37} http_session_state_t;
38typedef struct
39{
40 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
41#define _(type, name) type name;
42 foreach_app_session_field
43#undef _
44 u32 thread_index;
45 u8 *rx_buf;
46 u32 vpp_session_index;
47 u64 vpp_session_handle;
48} http_session_t;
49
Florin Coras0e9c33b2017-08-14 22:33:41 -070050typedef struct
51{
Florin Coras844a36d2018-12-20 09:50:50 -080052 http_session_t **sessions;
53 clib_rwlock_t sessions_lock;
54 u32 **session_to_http_session;
55
Florin Coras3c2fed52018-07-04 04:15:05 -070056 svm_msg_q_t **vpp_queue;
Dave Barach1015a1e2017-05-08 19:15:03 -040057
58 uword *handler_by_get_request;
59
60 u32 *free_http_cli_process_node_indices;
61
62 /* Sever's event queue */
Florin Corase86a8ed2018-01-05 03:20:25 -080063 svm_queue_t *vl_input_queue;
Dave Barach1015a1e2017-05-08 19:15:03 -040064
65 /* API client handle */
66 u32 my_client_index;
67
68 u32 app_index;
69
70 /* process node index for evnt scheduling */
71 u32 node_index;
Florin Coras1d6d0852017-11-17 14:26:01 -080072
73 u32 prealloc_fifos;
74 u32 private_segment_size;
75 u32 fifo_size;
Florin Coras371ca502018-02-21 12:07:41 -080076 u8 *uri;
Florin Coras844a36d2018-12-20 09:50:50 -080077 u32 is_static;
Dave Barach1015a1e2017-05-08 19:15:03 -040078 vlib_main_t *vlib_main;
79} http_server_main_t;
80
81http_server_main_t http_server_main;
82
83static void
Florin Coras844a36d2018-12-20 09:50:50 -080084http_server_sessions_reader_lock (void)
85{
86 clib_rwlock_reader_lock (&http_server_main.sessions_lock);
87}
88
89static void
90http_server_sessions_reader_unlock (void)
91{
92 clib_rwlock_reader_unlock (&http_server_main.sessions_lock);
93}
94
95static void
96http_server_sessions_writer_lock (void)
97{
98 clib_rwlock_writer_lock (&http_server_main.sessions_lock);
99}
100
101static void
102http_server_sessions_writer_unlock (void)
103{
104 clib_rwlock_writer_unlock (&http_server_main.sessions_lock);
105}
106
107static void
108http_server_session_lookup_add (u32 thread_index, u32 s_index, u32 hs_index)
109{
110 http_server_main_t *hsm = &http_server_main;
111 vec_validate (hsm->session_to_http_session[thread_index], s_index);
112 hsm->session_to_http_session[thread_index][s_index] = hs_index;
113}
114
115static void
116http_server_session_lookup_del (u32 thread_index, u32 s_index)
117{
118 http_server_main_t *hsm = &http_server_main;
119 hsm->session_to_http_session[thread_index][s_index] = ~0;
120}
121
122static http_session_t *
123http_server_session_lookup (u32 thread_index, u32 s_index)
124{
125 http_server_main_t *hsm = &http_server_main;
126 u32 hs_index;
127
128 if (s_index < vec_len (hsm->session_to_http_session[thread_index]))
129 {
130 hs_index = hsm->session_to_http_session[thread_index][s_index];
131 if (hs_index < vec_len (hsm->sessions[thread_index]))
132 return &hsm->sessions[thread_index][hs_index];
133 }
134 return 0;
135}
136
137static http_session_t *
138http_server_session_alloc (u32 thread_index)
139{
140 http_server_main_t *hsm = &http_server_main;
141 http_session_t *hs;
142 pool_get (hsm->sessions[thread_index], hs);
143 memset (hs, 0, sizeof (*hs));
144 hs->session_index = hs - hsm->sessions[thread_index];
145 hs->thread_index = thread_index;
146 return hs;
147}
148
149static http_session_t *
150http_server_session_get (u32 thread_index, u32 hs_index)
151{
152 http_server_main_t *hsm = &http_server_main;
153 return pool_elt_at_index (hsm->sessions[thread_index], hs_index);
154}
155
156static void
157http_server_session_free (http_session_t * hs)
158{
159 http_server_main_t *hsm = &http_server_main;
160 pool_put (hsm->sessions[hs->thread_index], hs);
161 if (CLIB_DEBUG)
162 memset (hs, 0xfa, sizeof (*hs));
163}
164
165static void
166http_server_session_cleanup (http_session_t * hs)
167{
168 if (!hs)
169 return;
170 http_server_session_lookup_del (hs->thread_index, hs->vpp_session_index);
171 vec_free (hs->rx_buf);
172 http_server_session_free (hs);
173}
174
175static void
176http_process_free (http_server_args * args)
Dave Barach1015a1e2017-05-08 19:15:03 -0400177{
178 vlib_node_runtime_t *rt;
179 vlib_main_t *vm = &vlib_global_main;
180 http_server_main_t *hsm = &http_server_main;
181 vlib_node_t *n;
182 u32 node_index;
Florin Coras4399c2e2018-01-25 06:34:42 -0800183 http_server_args **save_args;
Dave Barach1015a1e2017-05-08 19:15:03 -0400184
Florin Coras0e9c33b2017-08-14 22:33:41 -0700185 node_index = args->node_index;
Dave Barach1015a1e2017-05-08 19:15:03 -0400186 ASSERT (node_index != 0);
187
188 n = vlib_get_node (vm, node_index);
Florin Coras0e9c33b2017-08-14 22:33:41 -0700189 rt = vlib_node_get_runtime (vm, n->index);
190 save_args = vlib_node_get_runtime_data (vm, n->index);
Dave Barach1015a1e2017-05-08 19:15:03 -0400191
Dave Barach1015a1e2017-05-08 19:15:03 -0400192 /* Reset process session pointer */
Florin Coras0e9c33b2017-08-14 22:33:41 -0700193 clib_mem_free (*save_args);
194 *save_args = 0;
Dave Barach1015a1e2017-05-08 19:15:03 -0400195
196 /* Turn off the process node */
197 vlib_node_set_state (vm, rt->node_index, VLIB_NODE_STATE_DISABLED);
198
199 /* add node index to the freelist */
200 vec_add1 (hsm->free_http_cli_process_node_indices, node_index);
201}
202
Florin Coras898cd8f2018-06-08 02:02:13 -0700203/* *INDENT-OFF* */
Florin Coras844a36d2018-12-20 09:50:50 -0800204static const char *http_ok =
205 "HTTP/1.1 200 OK\r\n";
206
Florin Coras898cd8f2018-06-08 02:02:13 -0700207static const char *http_response =
Florin Coras898cd8f2018-06-08 02:02:13 -0700208 "Content-Type: text/html\r\n"
209 "Expires: Mon, 11 Jan 1970 10:10:10 GMT\r\n"
Florin Coras844a36d2018-12-20 09:50:50 -0800210 "Connection: close \r\n"
Florin Coras898cd8f2018-06-08 02:02:13 -0700211 "Pragma: no-cache\r\n"
212 "Content-Length: %d\r\n\r\n%s";
Dave Barach1015a1e2017-05-08 19:15:03 -0400213
Florin Coras898cd8f2018-06-08 02:02:13 -0700214static const char *http_error_template =
215 "HTTP/1.1 %s\r\n"
216 "Content-Type: text/html\r\n"
217 "Expires: Mon, 11 Jan 1970 10:10:10 GMT\r\n"
218 "Connection: close\r\n"
219 "Pragma: no-cache\r\n"
220 "Content-Length: 0\r\n\r\n";
Dave Barach1015a1e2017-05-08 19:15:03 -0400221
222/* Header, including incantation to suppress favicon.ico requests */
Florin Coras898cd8f2018-06-08 02:02:13 -0700223static const char *html_header_template =
224 "<html><head><title>%v</title></head>"
225 "<link rel=\"icon\" href=\"data:,\">"
226 "<body><pre>";
Dave Barach1015a1e2017-05-08 19:15:03 -0400227
Florin Coras898cd8f2018-06-08 02:02:13 -0700228static const char *html_footer =
229 "</pre></body></html>\r\n";
Dave Barach1015a1e2017-05-08 19:15:03 -0400230
Florin Coras898cd8f2018-06-08 02:02:13 -0700231static const char *html_header_static =
232 "<html><head><title>static reply</title></head>"
233 "<link rel=\"icon\" href=\"data:,\">"
234 "<body><pre>hello</pre></body></html>\r\n";
235/* *INDENT-ON* */
Florin Coras149d62f2017-11-01 15:05:49 -0700236
237static u8 *static_http;
Florin Coras844a36d2018-12-20 09:50:50 -0800238static u8 *static_ok;
Florin Coras149d62f2017-11-01 15:05:49 -0700239
Dave Barach1015a1e2017-05-08 19:15:03 -0400240static void
241http_cli_output (uword arg, u8 * buffer, uword buffer_bytes)
242{
243 u8 **output_vecp = (u8 **) arg;
244 u8 *output_vec;
245 u32 offset;
246
247 output_vec = *output_vecp;
248
249 offset = vec_len (output_vec);
250 vec_validate (output_vec, offset + buffer_bytes - 1);
Dave Barach178cf492018-11-13 16:34:13 -0500251 clib_memcpy_fast (output_vec + offset, buffer, buffer_bytes);
Dave Barach1015a1e2017-05-08 19:15:03 -0400252
253 *output_vecp = output_vec;
254}
255
256void
Florin Coras844a36d2018-12-20 09:50:50 -0800257send_data (http_session_t * hs, u8 * data)
Dave Barach1015a1e2017-05-08 19:15:03 -0400258{
Florin Coras844a36d2018-12-20 09:50:50 -0800259 http_server_main_t *hsm = &http_server_main;
260 vnet_disconnect_args_t _a = { 0 }, *a = &_a;
261 vlib_main_t *vm = vlib_get_main ();
262 f64 last_sent_timer = vlib_time_now (vm);
Dave Barach1015a1e2017-05-08 19:15:03 -0400263 u32 offset, bytes_to_send;
264 f64 delay = 10e-3;
Dave Barach1015a1e2017-05-08 19:15:03 -0400265
266 bytes_to_send = vec_len (data);
267 offset = 0;
268
269 while (bytes_to_send > 0)
270 {
271 int actual_transfer;
272
273 actual_transfer = svm_fifo_enqueue_nowait
Florin Coras844a36d2018-12-20 09:50:50 -0800274 (hs->tx_fifo, bytes_to_send, data + offset);
Dave Barach1015a1e2017-05-08 19:15:03 -0400275
276 /* Made any progress? */
277 if (actual_transfer <= 0)
278 {
279 vlib_process_suspend (vm, delay);
280 /* 10s deadman timer */
281 if (vlib_time_now (vm) > last_sent_timer + 10.0)
282 {
Florin Coras844a36d2018-12-20 09:50:50 -0800283 a->handle = hs->vpp_session_handle;
284 a->app_index = hsm->app_index;
285 vnet_disconnect_session (a);
Dave Barach1015a1e2017-05-08 19:15:03 -0400286 break;
287 }
288 /* Exponential backoff, within reason */
289 if (delay < 1.0)
290 delay = delay * 2.0;
291 }
292 else
293 {
294 last_sent_timer = vlib_time_now (vm);
295 offset += actual_transfer;
296 bytes_to_send -= actual_transfer;
297
Florin Coras844a36d2018-12-20 09:50:50 -0800298 if (svm_fifo_set_event (hs->tx_fifo))
299 session_send_io_evt_to_thread (hs->tx_fifo,
300 SESSION_IO_EVT_TX_FLUSH);
Dave Barach1015a1e2017-05-08 19:15:03 -0400301 delay = 10e-3;
302 }
303 }
304}
305
306static void
Florin Coras844a36d2018-12-20 09:50:50 -0800307send_error (http_session_t * hs, char *str)
Dave Barach1015a1e2017-05-08 19:15:03 -0400308{
309 u8 *data;
310
311 data = format (0, http_error_template, str);
Florin Coras844a36d2018-12-20 09:50:50 -0800312 send_data (hs, data);
Dave Barach1015a1e2017-05-08 19:15:03 -0400313 vec_free (data);
314}
315
316static uword
Florin Coras844a36d2018-12-20 09:50:50 -0800317http_cli_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
318 vlib_frame_t * f)
Dave Barach1015a1e2017-05-08 19:15:03 -0400319{
Florin Coras844a36d2018-12-20 09:50:50 -0800320 u8 *request = 0, *reply = 0, *http = 0, *html = 0;
Dave Barach1015a1e2017-05-08 19:15:03 -0400321 http_server_main_t *hsm = &http_server_main;
Florin Coras4399c2e2018-01-25 06:34:42 -0800322 http_server_args **save_args;
323 http_server_args *args;
Dave Barach1015a1e2017-05-08 19:15:03 -0400324 unformat_input_t input;
Florin Coras844a36d2018-12-20 09:50:50 -0800325 http_session_t *hs;
Dave Barach1015a1e2017-05-08 19:15:03 -0400326 int i;
Dave Barach1015a1e2017-05-08 19:15:03 -0400327
Florin Coras0e9c33b2017-08-14 22:33:41 -0700328 save_args = vlib_node_get_runtime_data (hsm->vlib_main, rt->node_index);
329 args = *save_args;
Dave Barach1015a1e2017-05-08 19:15:03 -0400330
Florin Coras844a36d2018-12-20 09:50:50 -0800331 http_server_sessions_reader_lock ();
332
333 hs = http_server_session_get (args->thread_index, args->hs_index);
334 ASSERT (hs);
335
336 request = hs->rx_buf;
Dave Barach1015a1e2017-05-08 19:15:03 -0400337 if (vec_len (request) < 7)
338 {
Florin Coras844a36d2018-12-20 09:50:50 -0800339 send_error (hs, "400 Bad Request");
Dave Barach1015a1e2017-05-08 19:15:03 -0400340 goto out;
341 }
342
343 for (i = 0; i < vec_len (request) - 4; i++)
344 {
345 if (request[i] == 'G' &&
346 request[i + 1] == 'E' &&
347 request[i + 2] == 'T' && request[i + 3] == ' ')
348 goto found;
349 }
350bad_request:
Florin Coras844a36d2018-12-20 09:50:50 -0800351 send_error (hs, "400 Bad Request");
Dave Barach1015a1e2017-05-08 19:15:03 -0400352 goto out;
353
354found:
355 /* Lose "GET " */
356 vec_delete (request, i + 5, 0);
357
358 /* Replace slashes with spaces, stop at the end of the path */
359 i = 0;
360 while (1)
361 {
362 if (request[i] == '/')
363 request[i] = ' ';
364 else if (request[i] == ' ')
365 {
366 /* vlib_cli_input is vector-based, no need for a NULL */
367 _vec_len (request) = i;
368 break;
369 }
370 i++;
371 /* Should never happen */
372 if (i == vec_len (request))
373 goto bad_request;
374 }
375
376 /* Generate the html header */
377 html = format (0, html_header_template, request /* title */ );
378
379 /* Run the command */
Florin Coras844a36d2018-12-20 09:50:50 -0800380 unformat_init_vector (&input, vec_dup (request));
Dave Barach1015a1e2017-05-08 19:15:03 -0400381 vlib_cli_input (vm, &input, http_cli_output, (uword) & reply);
382 unformat_free (&input);
383 request = 0;
384
385 /* Generate the html page */
386 html = format (html, "%v", reply);
387 html = format (html, html_footer);
388 /* And the http reply */
Florin Coras844a36d2018-12-20 09:50:50 -0800389 http = format (0, http_ok, vec_len (http_ok));
390 http = format (http, http_response, vec_len (html), html);
Dave Barach1015a1e2017-05-08 19:15:03 -0400391
392 /* Send it */
Florin Coras844a36d2018-12-20 09:50:50 -0800393 send_data (hs, http);
Dave Barach1015a1e2017-05-08 19:15:03 -0400394
395out:
396 /* Cleanup */
Florin Coras844a36d2018-12-20 09:50:50 -0800397 http_server_sessions_reader_unlock ();
Dave Barach1015a1e2017-05-08 19:15:03 -0400398 vec_free (reply);
399 vec_free (html);
400 vec_free (http);
401
Florin Coras844a36d2018-12-20 09:50:50 -0800402 http_process_free (args);
Dave Barach1015a1e2017-05-08 19:15:03 -0400403 return (0);
404}
405
406static void
Florin Coras4399c2e2018-01-25 06:34:42 -0800407alloc_http_process (http_server_args * args)
Dave Barach1015a1e2017-05-08 19:15:03 -0400408{
409 char *name;
410 vlib_node_t *n;
411 http_server_main_t *hsm = &http_server_main;
412 vlib_main_t *vm = hsm->vlib_main;
413 uword l = vec_len (hsm->free_http_cli_process_node_indices);
Florin Coras4399c2e2018-01-25 06:34:42 -0800414 http_server_args **save_args;
Dave Barach1015a1e2017-05-08 19:15:03 -0400415
416 if (vec_len (hsm->free_http_cli_process_node_indices) > 0)
417 {
418 n = vlib_get_node (vm, hsm->free_http_cli_process_node_indices[l - 1]);
Dave Barach1015a1e2017-05-08 19:15:03 -0400419 vlib_node_set_state (vm, n->index, VLIB_NODE_STATE_POLLING);
Dave Barach1015a1e2017-05-08 19:15:03 -0400420 _vec_len (hsm->free_http_cli_process_node_indices) = l - 1;
421 }
422 else
423 {
424 static vlib_node_registration_t r = {
425 .function = http_cli_process,
426 .type = VLIB_NODE_TYPE_PROCESS,
427 .process_log2_n_stack_bytes = 16,
428 .runtime_data_bytes = sizeof (void *),
429 };
430
431 name = (char *) format (0, "http-cli-%d", l);
Dave Barach1015a1e2017-05-08 19:15:03 -0400432 r.name = name;
433 vlib_register_node (vm, &r);
434 vec_free (name);
435
436 n = vlib_get_node (vm, r.index);
437 }
438
Florin Coras0e9c33b2017-08-14 22:33:41 -0700439 /* Save the node index in the args. It won't be zero. */
440 args->node_index = n->index;
Dave Barach1015a1e2017-05-08 19:15:03 -0400441
Florin Coras0e9c33b2017-08-14 22:33:41 -0700442 /* Save the args (pointer) in the node runtime */
443 save_args = vlib_node_get_runtime_data (vm, n->index);
444 *save_args = args;
Dave Barach1015a1e2017-05-08 19:15:03 -0400445
446 vlib_start_process (vm, n->runtime_index);
447}
448
Florin Coras0e9c33b2017-08-14 22:33:41 -0700449static void
450alloc_http_process_callback (void *cb_args)
451{
Florin Coras4399c2e2018-01-25 06:34:42 -0800452 alloc_http_process ((http_server_args *) cb_args);
Florin Coras0e9c33b2017-08-14 22:33:41 -0700453}
454
455static int
Florin Coras844a36d2018-12-20 09:50:50 -0800456session_rx_request (http_session_t * hs)
Florin Coras0e9c33b2017-08-14 22:33:41 -0700457{
Florin Coras844a36d2018-12-20 09:50:50 -0800458 u32 max_dequeue, cursize;
459 int n_read;
Florin Coras0e9c33b2017-08-14 22:33:41 -0700460
Florin Coras844a36d2018-12-20 09:50:50 -0800461 cursize = vec_len (hs->rx_buf);
462 max_dequeue = svm_fifo_max_dequeue (hs->rx_fifo);
Florin Coras0e9c33b2017-08-14 22:33:41 -0700463 if (PREDICT_FALSE (max_dequeue == 0))
Florin Coras149d62f2017-11-01 15:05:49 -0700464 return -1;
Florin Coras0e9c33b2017-08-14 22:33:41 -0700465
Florin Coras844a36d2018-12-20 09:50:50 -0800466 vec_validate (hs->rx_buf, cursize + max_dequeue - 1);
467 n_read = app_recv_stream_raw (hs->rx_fifo, hs->rx_buf + cursize,
468 max_dequeue, 0, 0 /* peek */ );
469 ASSERT (n_read == max_dequeue);
470 if (svm_fifo_is_empty (hs->rx_fifo))
471 svm_fifo_unset_event (hs->rx_fifo);
Florin Coras0e9c33b2017-08-14 22:33:41 -0700472
Florin Coras844a36d2018-12-20 09:50:50 -0800473 _vec_len (hs->rx_buf) = cursize + n_read;
Florin Coras149d62f2017-11-01 15:05:49 -0700474 return 0;
475}
476
477static int
478http_server_rx_callback (stream_session_t * s)
479{
Florin Coras4399c2e2018-01-25 06:34:42 -0800480 http_server_args *args;
Florin Coras844a36d2018-12-20 09:50:50 -0800481 http_session_t *hs;
JingLiuZTE7cafe762017-11-08 15:01:27 +0800482 int rv;
Florin Coras149d62f2017-11-01 15:05:49 -0700483
Florin Coras844a36d2018-12-20 09:50:50 -0800484 http_server_sessions_reader_lock ();
485
486 hs = http_server_session_lookup (s->thread_index, s->session_index);
487 if (!hs || hs->session_state != HTTP_STATE_ESTABLISHED)
488 return -1;
489
490 rv = session_rx_request (hs);
JingLiuZTE7cafe762017-11-08 15:01:27 +0800491 if (rv)
492 return rv;
Florin Coras0e9c33b2017-08-14 22:33:41 -0700493
494 /* send the command to a new/recycled vlib process */
495 args = clib_mem_alloc (sizeof (*args));
Florin Coras844a36d2018-12-20 09:50:50 -0800496 args->hs_index = hs->session_index;
497 args->thread_index = hs->thread_index;
498
499 http_server_sessions_reader_unlock ();
Florin Coras0e9c33b2017-08-14 22:33:41 -0700500
501 /* Send an RPC request via the thread-0 input node */
502 if (vlib_get_thread_index () != 0)
Florin Coras3c2fed52018-07-04 04:15:05 -0700503 session_send_rpc_evt_to_thread (0, alloc_http_process_callback, args);
Florin Coras0e9c33b2017-08-14 22:33:41 -0700504 else
505 alloc_http_process (args);
506 return 0;
507}
508
Dave Barach1015a1e2017-05-08 19:15:03 -0400509static int
Florin Coras149d62f2017-11-01 15:05:49 -0700510http_server_rx_callback_static (stream_session_t * s)
511{
512 http_server_main_t *hsm = &http_server_main;
Florin Coras844a36d2018-12-20 09:50:50 -0800513 vnet_disconnect_args_t _a = { 0 }, *a = &_a;
514 http_session_t *hs;
515 u32 request_len;
Florin Coras149d62f2017-11-01 15:05:49 -0700516 u8 *request = 0;
Florin Coras844a36d2018-12-20 09:50:50 -0800517 int i, rv;
Florin Coras149d62f2017-11-01 15:05:49 -0700518
Florin Coras844a36d2018-12-20 09:50:50 -0800519 hs = http_server_session_lookup (s->thread_index, s->session_index);
520 if (!hs || hs->session_state == HTTP_STATE_CLOSED)
521 return 0;
522
523 /* ok 200 was sent */
524 if (hs->session_state == HTTP_STATE_OK_SENT)
525 goto send_data;
526
527 rv = session_rx_request (hs);
JingLiuZTE7cafe762017-11-08 15:01:27 +0800528 if (rv)
Florin Coras844a36d2018-12-20 09:50:50 -0800529 goto wait_for_data;
Florin Coras149d62f2017-11-01 15:05:49 -0700530
Florin Coras844a36d2018-12-20 09:50:50 -0800531 request = hs->rx_buf;
532 request_len = vec_len (request);
Florin Coras149d62f2017-11-01 15:05:49 -0700533 if (vec_len (request) < 7)
534 {
Florin Coras844a36d2018-12-20 09:50:50 -0800535 send_error (hs, "400 Bad Request");
536 goto close_session;
Florin Coras149d62f2017-11-01 15:05:49 -0700537 }
538
Florin Coras844a36d2018-12-20 09:50:50 -0800539 for (i = 0; i < request_len - 4; i++)
Florin Coras149d62f2017-11-01 15:05:49 -0700540 {
541 if (request[i] == 'G' &&
542 request[i + 1] == 'E' &&
543 request[i + 2] == 'T' && request[i + 3] == ' ')
Florin Coras844a36d2018-12-20 09:50:50 -0800544 goto find_end;
Florin Coras149d62f2017-11-01 15:05:49 -0700545 }
Florin Coras844a36d2018-12-20 09:50:50 -0800546 send_error (hs, "400 Bad Request");
547 goto close_session;
Florin Coras149d62f2017-11-01 15:05:49 -0700548
Florin Coras844a36d2018-12-20 09:50:50 -0800549find_end:
Florin Coras149d62f2017-11-01 15:05:49 -0700550
Florin Coras844a36d2018-12-20 09:50:50 -0800551 /* check for the end sequence: /r/n/r/n */
552 if (request[request_len - 1] != 0xa || request[request_len - 3] != 0xa
553 || request[request_len - 2] != 0xd || request[request_len - 4] != 0xd)
554 goto wait_for_data;
Florin Coras149d62f2017-11-01 15:05:49 -0700555
Florin Coras844a36d2018-12-20 09:50:50 -0800556 /* send 200 OK first */
557 send_data (hs, static_ok);
558 hs->session_state = HTTP_STATE_OK_SENT;
559 goto postpone;
560
561send_data:
562 send_data (hs, static_http);
563 http_server_session_cleanup (hs);
564
565close_session:
566 a->handle = session_handle (s);
567 a->app_index = hsm->app_index;
568 vnet_disconnect_session (a);
569 return 0;
570
571postpone:
572 svm_fifo_set_event (hs->rx_fifo);
573 session_send_io_evt_to_thread (hs->rx_fifo, FIFO_EVENT_BUILTIN_RX);
574 return 0;
575
576wait_for_data:
Florin Coras149d62f2017-11-01 15:05:49 -0700577 return 0;
578}
579
580static int
Florin Coras4399c2e2018-01-25 06:34:42 -0800581http_server_session_accept_callback (stream_session_t * s)
Dave Barach1015a1e2017-05-08 19:15:03 -0400582{
Florin Coras844a36d2018-12-20 09:50:50 -0800583 http_server_main_t *hsm = &http_server_main;
584 http_session_t *hs;
Dave Barach1015a1e2017-05-08 19:15:03 -0400585
Florin Coras844a36d2018-12-20 09:50:50 -0800586 hsm->vpp_queue[s->thread_index] =
Dave Barach1015a1e2017-05-08 19:15:03 -0400587 session_manager_get_vpp_event_queue (s->thread_index);
Florin Coras844a36d2018-12-20 09:50:50 -0800588
589 if (!hsm->is_static)
590 http_server_sessions_writer_lock ();
591
592 hs = http_server_session_alloc (s->thread_index);
593 http_server_session_lookup_add (s->thread_index, s->session_index,
594 hs->session_index);
595 hs->rx_fifo = s->server_rx_fifo;
596 hs->tx_fifo = s->server_tx_fifo;
597 hs->vpp_session_index = s->session_index;
598 hs->vpp_session_handle = session_handle (s);
599 hs->session_state = HTTP_STATE_ESTABLISHED;
600
601 if (!hsm->is_static)
602 http_server_sessions_writer_unlock ();
603
Dave Barach1015a1e2017-05-08 19:15:03 -0400604 s->session_state = SESSION_STATE_READY;
Dave Barach1015a1e2017-05-08 19:15:03 -0400605 return 0;
606}
607
608static void
Florin Coras4399c2e2018-01-25 06:34:42 -0800609http_server_session_disconnect_callback (stream_session_t * s)
Dave Barach1015a1e2017-05-08 19:15:03 -0400610{
Florin Coras844a36d2018-12-20 09:50:50 -0800611 http_server_main_t *hsm = &http_server_main;
Florin Coras4af830c2018-12-04 09:21:36 -0800612 vnet_disconnect_args_t _a = { 0 }, *a = &_a;
Florin Coras844a36d2018-12-20 09:50:50 -0800613 http_session_t *hs;
614
615 if (!hsm->is_static)
616 http_server_sessions_writer_lock ();
617
618 hs = http_server_session_lookup (s->thread_index, s->session_index);
619 http_server_session_cleanup (hs);
620
621 if (!hsm->is_static)
622 http_server_sessions_writer_unlock ();
Dave Barach1015a1e2017-05-08 19:15:03 -0400623
Florin Coras3cbc04b2017-10-02 00:18:51 -0700624 a->handle = session_handle (s);
Florin Coras844a36d2018-12-20 09:50:50 -0800625 a->app_index = hsm->app_index;
Dave Barach1015a1e2017-05-08 19:15:03 -0400626 vnet_disconnect_session (a);
627}
628
629static void
Florin Coras4399c2e2018-01-25 06:34:42 -0800630http_server_session_reset_callback (stream_session_t * s)
Dave Barach1015a1e2017-05-08 19:15:03 -0400631{
Florin Coras844a36d2018-12-20 09:50:50 -0800632 http_server_main_t *hsm = &http_server_main;
Florin Coras4af830c2018-12-04 09:21:36 -0800633 vnet_disconnect_args_t _a = { 0 }, *a = &_a;
Florin Coras844a36d2018-12-20 09:50:50 -0800634 http_session_t *hs;
635
636 if (!hsm->is_static)
637 http_server_sessions_writer_lock ();
638
639 hs = http_server_session_lookup (s->thread_index, s->session_index);
640 http_server_session_cleanup (hs);
641
642 if (!hsm->is_static)
643 http_server_sessions_writer_unlock ();
644
Florin Coras4af830c2018-12-04 09:21:36 -0800645 a->handle = session_handle (s);
Florin Coras844a36d2018-12-20 09:50:50 -0800646 a->app_index = hsm->app_index;
Florin Coras4af830c2018-12-04 09:21:36 -0800647 vnet_disconnect_session (a);
Dave Barach1015a1e2017-05-08 19:15:03 -0400648}
649
Dave Barach1015a1e2017-05-08 19:15:03 -0400650static int
Florin Coras4399c2e2018-01-25 06:34:42 -0800651http_server_session_connected_callback (u32 app_index, u32 api_context,
652 stream_session_t * s, u8 is_fail)
Dave Barach1015a1e2017-05-08 19:15:03 -0400653{
654 clib_warning ("called...");
655 return -1;
656}
657
658static int
Florin Corasfa76a762018-11-29 12:40:10 -0800659http_server_add_segment_callback (u32 client_index, u64 segment_handle)
Dave Barach1015a1e2017-05-08 19:15:03 -0400660{
661 clib_warning ("called...");
662 return -1;
663}
664
Florin Coras4399c2e2018-01-25 06:34:42 -0800665static session_cb_vft_t http_server_session_cb_vft = {
666 .session_accept_callback = http_server_session_accept_callback,
667 .session_disconnect_callback = http_server_session_disconnect_callback,
668 .session_connected_callback = http_server_session_connected_callback,
669 .add_segment_callback = http_server_add_segment_callback,
Florin Coras371ca502018-02-21 12:07:41 -0800670 .builtin_app_rx_callback = http_server_rx_callback,
Florin Coras4399c2e2018-01-25 06:34:42 -0800671 .session_reset_callback = http_server_session_reset_callback
Dave Barach1015a1e2017-05-08 19:15:03 -0400672};
673
Dave Barach1015a1e2017-05-08 19:15:03 -0400674static int
Florin Coras844a36d2018-12-20 09:50:50 -0800675http_server_attach ()
Dave Barach1015a1e2017-05-08 19:15:03 -0400676{
Florin Coras371ca502018-02-21 12:07:41 -0800677 vnet_app_add_tls_cert_args_t _a_cert, *a_cert = &_a_cert;
678 vnet_app_add_tls_key_args_t _a_key, *a_key = &_a_key;
Dave Barach1015a1e2017-05-08 19:15:03 -0400679 http_server_main_t *hsm = &http_server_main;
Florin Corasff6e7692017-12-11 04:59:01 -0800680 u64 options[APP_OPTIONS_N_OPTIONS];
Dave Barach1015a1e2017-05-08 19:15:03 -0400681 vnet_app_attach_args_t _a, *a = &_a;
Florin Corasff6e7692017-12-11 04:59:01 -0800682 u32 segment_size = 128 << 20;
Dave Barach1015a1e2017-05-08 19:15:03 -0400683
Dave Barachb7b92992018-10-17 10:38:51 -0400684 clib_memset (a, 0, sizeof (*a));
685 clib_memset (options, 0, sizeof (options));
Dave Barach1015a1e2017-05-08 19:15:03 -0400686
Florin Corasff6e7692017-12-11 04:59:01 -0800687 if (hsm->private_segment_size)
688 segment_size = hsm->private_segment_size;
689
Florin Coras844a36d2018-12-20 09:50:50 -0800690 a->api_client_index = ~0;
691 a->name = format (0, "test_http_server");
Florin Coras4399c2e2018-01-25 06:34:42 -0800692 a->session_cb_vft = &http_server_session_cb_vft;
Dave Barach1015a1e2017-05-08 19:15:03 -0400693 a->options = options;
Florin Corasff6e7692017-12-11 04:59:01 -0800694 a->options[APP_OPTIONS_SEGMENT_SIZE] = segment_size;
695 a->options[APP_OPTIONS_RX_FIFO_SIZE] =
Florin Coras1d6d0852017-11-17 14:26:01 -0800696 hsm->fifo_size ? hsm->fifo_size : 8 << 10;
Florin Corasff6e7692017-12-11 04:59:01 -0800697 a->options[APP_OPTIONS_TX_FIFO_SIZE] =
Florin Coras1d6d0852017-11-17 14:26:01 -0800698 hsm->fifo_size ? hsm->fifo_size : 32 << 10;
Florin Coras7999e832017-10-31 01:51:04 -0700699 a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
Florin Coras1d6d0852017-11-17 14:26:01 -0800700 a->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = hsm->prealloc_fifos;
Dave Barach1015a1e2017-05-08 19:15:03 -0400701
702 if (vnet_application_attach (a))
703 {
Florin Coras844a36d2018-12-20 09:50:50 -0800704 vec_free (a->name);
Dave Barach1015a1e2017-05-08 19:15:03 -0400705 clib_warning ("failed to attach server");
706 return -1;
707 }
Florin Coras844a36d2018-12-20 09:50:50 -0800708 vec_free (a->name);
Dave Barach1015a1e2017-05-08 19:15:03 -0400709 hsm->app_index = a->app_index;
Florin Coras371ca502018-02-21 12:07:41 -0800710
Dave Barachb7b92992018-10-17 10:38:51 -0400711 clib_memset (a_cert, 0, sizeof (*a_cert));
Florin Coras371ca502018-02-21 12:07:41 -0800712 a_cert->app_index = a->app_index;
713 vec_validate (a_cert->cert, test_srv_crt_rsa_len);
Dave Barach178cf492018-11-13 16:34:13 -0500714 clib_memcpy_fast (a_cert->cert, test_srv_crt_rsa, test_srv_crt_rsa_len);
Florin Coras371ca502018-02-21 12:07:41 -0800715 vnet_app_add_tls_cert (a_cert);
716
Dave Barachb7b92992018-10-17 10:38:51 -0400717 clib_memset (a_key, 0, sizeof (*a_key));
Florin Coras371ca502018-02-21 12:07:41 -0800718 a_key->app_index = a->app_index;
719 vec_validate (a_key->key, test_srv_key_rsa_len);
Dave Barach178cf492018-11-13 16:34:13 -0500720 clib_memcpy_fast (a_key->key, test_srv_key_rsa, test_srv_key_rsa_len);
Florin Coras371ca502018-02-21 12:07:41 -0800721 vnet_app_add_tls_key (a_key);
722
Dave Barach1015a1e2017-05-08 19:15:03 -0400723 return 0;
724}
725
726static int
Florin Coras4399c2e2018-01-25 06:34:42 -0800727http_server_listen ()
Dave Barach1015a1e2017-05-08 19:15:03 -0400728{
729 http_server_main_t *hsm = &http_server_main;
730 vnet_bind_args_t _a, *a = &_a;
Dave Barachb7b92992018-10-17 10:38:51 -0400731 clib_memset (a, 0, sizeof (*a));
Dave Barach1015a1e2017-05-08 19:15:03 -0400732 a->app_index = hsm->app_index;
733 a->uri = "tcp://0.0.0.0/80";
Florin Coras371ca502018-02-21 12:07:41 -0800734 if (hsm->uri)
735 a->uri = (char *) hsm->uri;
Dave Barach1015a1e2017-05-08 19:15:03 -0400736 return vnet_bind_uri (a);
737}
738
739static int
Florin Coras4399c2e2018-01-25 06:34:42 -0800740http_server_create (vlib_main_t * vm)
Dave Barach1015a1e2017-05-08 19:15:03 -0400741{
Florin Coras844a36d2018-12-20 09:50:50 -0800742 vlib_thread_main_t *vtm = vlib_get_thread_main ();
Dave Barach1015a1e2017-05-08 19:15:03 -0400743 http_server_main_t *hsm = &http_server_main;
744 u32 num_threads;
Dave Barach1015a1e2017-05-08 19:15:03 -0400745
746 num_threads = 1 /* main thread */ + vtm->n_threads;
Florin Coras844a36d2018-12-20 09:50:50 -0800747 vec_validate (hsm->vpp_queue, num_threads - 1);
748 vec_validate (hsm->sessions, num_threads - 1);
749 vec_validate (hsm->session_to_http_session, num_threads - 1);
Dave Barach1015a1e2017-05-08 19:15:03 -0400750
Florin Coras844a36d2018-12-20 09:50:50 -0800751 clib_rwlock_init (&hsm->sessions_lock);
752
753 if (http_server_attach ())
Dave Barach1015a1e2017-05-08 19:15:03 -0400754 {
755 clib_warning ("failed to attach server");
756 return -1;
757 }
Florin Coras4399c2e2018-01-25 06:34:42 -0800758 if (http_server_listen ())
Dave Barach1015a1e2017-05-08 19:15:03 -0400759 {
760 clib_warning ("failed to start listening");
761 return -1;
762 }
763 return 0;
764}
765
Dave Barach1015a1e2017-05-08 19:15:03 -0400766static clib_error_t *
Florin Coras4399c2e2018-01-25 06:34:42 -0800767http_server_create_command_fn (vlib_main_t * vm,
768 unformat_input_t * input,
769 vlib_cli_command_t * cmd)
Dave Barach1015a1e2017-05-08 19:15:03 -0400770{
Florin Coras0e9c33b2017-08-14 22:33:41 -0700771 http_server_main_t *hsm = &http_server_main;
Florin Coras1d6d0852017-11-17 14:26:01 -0800772 u64 seg_size;
Florin Coras149d62f2017-11-01 15:05:49 -0700773 u8 *html;
Florin Coras844a36d2018-12-20 09:50:50 -0800774 int rv;
Dave Barach1015a1e2017-05-08 19:15:03 -0400775
Florin Coras1d6d0852017-11-17 14:26:01 -0800776 hsm->prealloc_fifos = 0;
777 hsm->private_segment_size = 0;
778 hsm->fifo_size = 0;
Florin Coras844a36d2018-12-20 09:50:50 -0800779 hsm->is_static = 0;
Florin Coras149d62f2017-11-01 15:05:49 -0700780 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
781 {
782 if (unformat (input, "static"))
Florin Coras844a36d2018-12-20 09:50:50 -0800783 hsm->is_static = 1;
Florin Coras1d6d0852017-11-17 14:26:01 -0800784 else if (unformat (input, "prealloc-fifos %d", &hsm->prealloc_fifos))
785 ;
786 else if (unformat (input, "private-segment-size %U",
787 unformat_memory_size, &seg_size))
788 {
789 if (seg_size >= 0x100000000ULL)
790 {
791 vlib_cli_output (vm, "private segment size %llu, too large",
792 seg_size);
793 return 0;
794 }
795 hsm->private_segment_size = seg_size;
796 }
797 else if (unformat (input, "fifo-size %d", &hsm->fifo_size))
798 hsm->fifo_size <<= 10;
Florin Coras371ca502018-02-21 12:07:41 -0800799 else if (unformat (input, "uri %s", &hsm->uri))
800 ;
Florin Coras149d62f2017-11-01 15:05:49 -0700801 else
802 return clib_error_return (0, "unknown input `%U'",
803 format_unformat_error, input);
804 }
Florin Coras0e9c33b2017-08-14 22:33:41 -0700805 if (hsm->my_client_index != (u32) ~ 0)
806 return clib_error_return (0, "test http server is already running");
807
Dave Barach1015a1e2017-05-08 19:15:03 -0400808 vnet_session_enable_disable (vm, 1 /* turn on TCP, etc. */ );
Florin Coras149d62f2017-11-01 15:05:49 -0700809
Florin Coras844a36d2018-12-20 09:50:50 -0800810 if (hsm->is_static)
Florin Coras149d62f2017-11-01 15:05:49 -0700811 {
Florin Coras371ca502018-02-21 12:07:41 -0800812 http_server_session_cb_vft.builtin_app_rx_callback =
Florin Coras149d62f2017-11-01 15:05:49 -0700813 http_server_rx_callback_static;
814 html = format (0, html_header_static);
815 static_http = format (0, http_response, vec_len (html), html);
Florin Coras844a36d2018-12-20 09:50:50 -0800816 static_ok = format (0, http_ok);
Florin Coras149d62f2017-11-01 15:05:49 -0700817 }
Florin Coras4399c2e2018-01-25 06:34:42 -0800818 rv = http_server_create (vm);
Dave Barach1015a1e2017-05-08 19:15:03 -0400819 switch (rv)
820 {
821 case 0:
822 break;
823 default:
824 return clib_error_return (0, "server_create returned %d", rv);
825 }
826 return 0;
827}
828
829/* *INDENT-OFF* */
Florin Coras4399c2e2018-01-25 06:34:42 -0800830VLIB_CLI_COMMAND (http_server_create_command, static) =
Dave Barach1015a1e2017-05-08 19:15:03 -0400831{
832 .path = "test http server",
833 .short_help = "test http server",
Florin Coras4399c2e2018-01-25 06:34:42 -0800834 .function = http_server_create_command_fn,
Dave Barach1015a1e2017-05-08 19:15:03 -0400835};
836/* *INDENT-ON* */
837
838static clib_error_t *
Florin Coras4399c2e2018-01-25 06:34:42 -0800839http_server_main_init (vlib_main_t * vm)
Dave Barach1015a1e2017-05-08 19:15:03 -0400840{
841 http_server_main_t *hsm = &http_server_main;
Florin Coras149d62f2017-11-01 15:05:49 -0700842
Dave Barach1015a1e2017-05-08 19:15:03 -0400843 hsm->my_client_index = ~0;
844 hsm->vlib_main = vm;
Dave Barach1015a1e2017-05-08 19:15:03 -0400845 return 0;
846}
847
Florin Coras4399c2e2018-01-25 06:34:42 -0800848VLIB_INIT_FUNCTION (http_server_main_init);
Dave Barach1015a1e2017-05-08 19:15:03 -0400849
850/*
851* fd.io coding-style-patch-verification: ON
852*
853* Local Variables:
854* eval: (c-set-style "gnu")
855* End:
856*/