session: fix http server rpc to main
[vpp.git] / src / vnet / session-apps / http_server.c
1 /*
2 * Copyright (c) 2015-2019 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 <vnet/session/application.h>
18 #include <vnet/session/application_interface.h>
19 #include <vnet/session/session.h>
20
21 typedef enum
22 {
23   EVENT_WAKEUP = 1,
24 } http_process_event_t;
25
26 typedef struct
27 {
28   u32 hs_index;
29   u32 thread_index;
30   u64 node_index;
31 } http_server_args;
32
33 typedef enum
34 {
35   HTTP_STATE_CLOSED,
36   HTTP_STATE_ESTABLISHED,
37   HTTP_STATE_OK_SENT,
38 } http_session_state_t;
39 typedef struct
40 {
41   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
42 #define _(type, name) type name;
43   foreach_app_session_field
44 #undef _
45   u32 thread_index;
46   u8 *rx_buf;
47   u32 vpp_session_index;
48   u64 vpp_session_handle;
49 } http_session_t;
50
51 typedef struct
52 {
53   http_session_t **sessions;
54   clib_rwlock_t sessions_lock;
55   u32 **session_to_http_session;
56
57   svm_msg_q_t **vpp_queue;
58
59   uword *handler_by_get_request;
60
61   u32 *free_http_cli_process_node_indices;
62
63   /* Sever's event queue */
64   svm_queue_t *vl_input_queue;
65
66   /* API client handle */
67   u32 my_client_index;
68
69   u32 app_index;
70
71   /* process node index for evnt scheduling */
72   u32 node_index;
73
74   u32 prealloc_fifos;
75   u32 private_segment_size;
76   u32 fifo_size;
77   u8 *uri;
78   u32 is_static;
79   vlib_main_t *vlib_main;
80 } http_server_main_t;
81
82 http_server_main_t http_server_main;
83
84 static void
85 http_server_sessions_reader_lock (void)
86 {
87   clib_rwlock_reader_lock (&http_server_main.sessions_lock);
88 }
89
90 static void
91 http_server_sessions_reader_unlock (void)
92 {
93   clib_rwlock_reader_unlock (&http_server_main.sessions_lock);
94 }
95
96 static void
97 http_server_sessions_writer_lock (void)
98 {
99   clib_rwlock_writer_lock (&http_server_main.sessions_lock);
100 }
101
102 static void
103 http_server_sessions_writer_unlock (void)
104 {
105   clib_rwlock_writer_unlock (&http_server_main.sessions_lock);
106 }
107
108 static void
109 http_server_session_lookup_add (u32 thread_index, u32 s_index, u32 hs_index)
110 {
111   http_server_main_t *hsm = &http_server_main;
112   vec_validate (hsm->session_to_http_session[thread_index], s_index);
113   hsm->session_to_http_session[thread_index][s_index] = hs_index;
114 }
115
116 static void
117 http_server_session_lookup_del (u32 thread_index, u32 s_index)
118 {
119   http_server_main_t *hsm = &http_server_main;
120   hsm->session_to_http_session[thread_index][s_index] = ~0;
121 }
122
123 static http_session_t *
124 http_server_session_lookup (u32 thread_index, u32 s_index)
125 {
126   http_server_main_t *hsm = &http_server_main;
127   u32 hs_index;
128
129   if (s_index < vec_len (hsm->session_to_http_session[thread_index]))
130     {
131       hs_index = hsm->session_to_http_session[thread_index][s_index];
132       if (hs_index < vec_len (hsm->sessions[thread_index]))
133         return &hsm->sessions[thread_index][hs_index];
134     }
135   return 0;
136 }
137
138 static http_session_t *
139 http_server_session_alloc (u32 thread_index)
140 {
141   http_server_main_t *hsm = &http_server_main;
142   http_session_t *hs;
143   pool_get (hsm->sessions[thread_index], hs);
144   memset (hs, 0, sizeof (*hs));
145   hs->session_index = hs - hsm->sessions[thread_index];
146   hs->thread_index = thread_index;
147   return hs;
148 }
149
150 static http_session_t *
151 http_server_session_get (u32 thread_index, u32 hs_index)
152 {
153   http_server_main_t *hsm = &http_server_main;
154   return pool_elt_at_index (hsm->sessions[thread_index], hs_index);
155 }
156
157 static void
158 http_server_session_free (http_session_t * hs)
159 {
160   http_server_main_t *hsm = &http_server_main;
161   pool_put (hsm->sessions[hs->thread_index], hs);
162   if (CLIB_DEBUG)
163     memset (hs, 0xfa, sizeof (*hs));
164 }
165
166 static void
167 http_server_session_cleanup (http_session_t * hs)
168 {
169   if (!hs)
170     return;
171   http_server_session_lookup_del (hs->thread_index, hs->vpp_session_index);
172   vec_free (hs->rx_buf);
173   http_server_session_free (hs);
174 }
175
176 static void
177 http_process_free (http_server_args * args)
178 {
179   vlib_node_runtime_t *rt;
180   vlib_main_t *vm = &vlib_global_main;
181   http_server_main_t *hsm = &http_server_main;
182   vlib_node_t *n;
183   u32 node_index;
184   http_server_args **save_args;
185
186   node_index = args->node_index;
187   ASSERT (node_index != 0);
188
189   n = vlib_get_node (vm, node_index);
190   rt = vlib_node_get_runtime (vm, n->index);
191   save_args = vlib_node_get_runtime_data (vm, n->index);
192
193   /* Reset process session pointer */
194   clib_mem_free (*save_args);
195   *save_args = 0;
196
197   /* Turn off the process node */
198   vlib_node_set_state (vm, rt->node_index, VLIB_NODE_STATE_DISABLED);
199
200   /* add node index to the freelist */
201   vec_add1 (hsm->free_http_cli_process_node_indices, node_index);
202 }
203
204 /* *INDENT-OFF* */
205 static const char *http_ok =
206     "HTTP/1.1 200 OK\r\n";
207
208 static const char *http_response =
209     "Content-Type: text/html\r\n"
210     "Expires: Mon, 11 Jan 1970 10:10:10 GMT\r\n"
211     "Connection: close \r\n"
212     "Pragma: no-cache\r\n"
213     "Content-Length: %d\r\n\r\n%s";
214
215 static const char *http_error_template =
216     "HTTP/1.1 %s\r\n"
217     "Content-Type: text/html\r\n"
218     "Expires: Mon, 11 Jan 1970 10:10:10 GMT\r\n"
219     "Connection: close\r\n"
220     "Pragma: no-cache\r\n"
221     "Content-Length: 0\r\n\r\n";
222
223 /* Header, including incantation to suppress favicon.ico requests */
224 static const char *html_header_template =
225     "<html><head><title>%v</title></head>"
226     "<link rel=\"icon\" href=\"data:,\">"
227     "<body><pre>";
228
229 static const char *html_footer =
230     "</pre></body></html>\r\n";
231
232 static const char *html_header_static =
233     "<html><head><title>static reply</title></head>"
234     "<link rel=\"icon\" href=\"data:,\">"
235     "<body><pre>hello</pre></body></html>\r\n";
236 /* *INDENT-ON* */
237
238 static u8 *static_http;
239 static u8 *static_ok;
240
241 static void
242 http_cli_output (uword arg, u8 * buffer, uword buffer_bytes)
243 {
244   u8 **output_vecp = (u8 **) arg;
245   u8 *output_vec;
246   u32 offset;
247
248   output_vec = *output_vecp;
249
250   offset = vec_len (output_vec);
251   vec_validate (output_vec, offset + buffer_bytes - 1);
252   clib_memcpy_fast (output_vec + offset, buffer, buffer_bytes);
253
254   *output_vecp = output_vec;
255 }
256
257 void
258 send_data (http_session_t * hs, u8 * data)
259 {
260   http_server_main_t *hsm = &http_server_main;
261   vnet_disconnect_args_t _a = { 0 }, *a = &_a;
262   vlib_main_t *vm = vlib_get_main ();
263   f64 last_sent_timer = vlib_time_now (vm);
264   u32 offset, bytes_to_send;
265   f64 delay = 10e-3;
266
267   bytes_to_send = vec_len (data);
268   offset = 0;
269
270   while (bytes_to_send > 0)
271     {
272       int actual_transfer;
273
274       actual_transfer = svm_fifo_enqueue_nowait
275         (hs->tx_fifo, bytes_to_send, data + offset);
276
277       /* Made any progress? */
278       if (actual_transfer <= 0)
279         {
280           http_server_sessions_reader_unlock ();
281           vlib_process_suspend (vm, delay);
282           http_server_sessions_reader_lock ();
283
284           /* 10s deadman timer */
285           if (vlib_time_now (vm) > last_sent_timer + 10.0)
286             {
287               a->handle = hs->vpp_session_handle;
288               a->app_index = hsm->app_index;
289               vnet_disconnect_session (a);
290               break;
291             }
292           /* Exponential backoff, within reason */
293           if (delay < 1.0)
294             delay = delay * 2.0;
295         }
296       else
297         {
298           last_sent_timer = vlib_time_now (vm);
299           offset += actual_transfer;
300           bytes_to_send -= actual_transfer;
301
302           if (svm_fifo_set_event (hs->tx_fifo))
303             session_send_io_evt_to_thread (hs->tx_fifo,
304                                            SESSION_IO_EVT_TX_FLUSH);
305           delay = 10e-3;
306         }
307     }
308 }
309
310 static void
311 send_error (http_session_t * hs, char *str)
312 {
313   u8 *data;
314
315   data = format (0, http_error_template, str);
316   send_data (hs, data);
317   vec_free (data);
318 }
319
320 static uword
321 http_cli_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
322                   vlib_frame_t * f)
323 {
324   u8 *request = 0, *reply = 0, *http = 0, *html = 0;
325   http_server_main_t *hsm = &http_server_main;
326   http_server_args **save_args;
327   http_server_args *args;
328   unformat_input_t input;
329   http_session_t *hs;
330   int i;
331
332   save_args = vlib_node_get_runtime_data (hsm->vlib_main, rt->node_index);
333   args = *save_args;
334
335   http_server_sessions_reader_lock ();
336
337   hs = http_server_session_get (args->thread_index, args->hs_index);
338   ASSERT (hs);
339
340   request = hs->rx_buf;
341   if (vec_len (request) < 7)
342     {
343       send_error (hs, "400 Bad Request");
344       goto out;
345     }
346
347   for (i = 0; i < vec_len (request) - 4; i++)
348     {
349       if (request[i] == 'G' &&
350           request[i + 1] == 'E' &&
351           request[i + 2] == 'T' && request[i + 3] == ' ')
352         goto found;
353     }
354 bad_request:
355   send_error (hs, "400 Bad Request");
356   goto out;
357
358 found:
359   /* Lose "GET " */
360   vec_delete (request, i + 5, 0);
361
362   /* Replace slashes with spaces, stop at the end of the path */
363   i = 0;
364   while (1)
365     {
366       if (request[i] == '/')
367         request[i] = ' ';
368       else if (request[i] == ' ')
369         {
370           /* vlib_cli_input is vector-based, no need for a NULL */
371           _vec_len (request) = i;
372           break;
373         }
374       i++;
375       /* Should never happen */
376       if (i == vec_len (request))
377         goto bad_request;
378     }
379
380   /* Generate the html header */
381   html = format (0, html_header_template, request /* title */ );
382
383   /* Run the command */
384   unformat_init_vector (&input, vec_dup (request));
385   vlib_cli_input (vm, &input, http_cli_output, (uword) & reply);
386   unformat_free (&input);
387   request = 0;
388
389   /* Generate the html page */
390   html = format (html, "%v", reply);
391   html = format (html, html_footer);
392   /* And the http reply */
393   http = format (0, http_ok, vec_len (http_ok));
394   http = format (http, http_response, vec_len (html), html);
395
396   /* Send it */
397   send_data (hs, http);
398
399 out:
400   /* Cleanup */
401   http_server_sessions_reader_unlock ();
402   vec_free (reply);
403   vec_free (html);
404   vec_free (http);
405
406   http_process_free (args);
407   return (0);
408 }
409
410 static void
411 alloc_http_process (http_server_args * args)
412 {
413   char *name;
414   vlib_node_t *n;
415   http_server_main_t *hsm = &http_server_main;
416   vlib_main_t *vm = hsm->vlib_main;
417   uword l = vec_len (hsm->free_http_cli_process_node_indices);
418   http_server_args **save_args;
419
420   if (vec_len (hsm->free_http_cli_process_node_indices) > 0)
421     {
422       n = vlib_get_node (vm, hsm->free_http_cli_process_node_indices[l - 1]);
423       vlib_node_set_state (vm, n->index, VLIB_NODE_STATE_POLLING);
424       _vec_len (hsm->free_http_cli_process_node_indices) = l - 1;
425     }
426   else
427     {
428       static vlib_node_registration_t r = {
429         .function = http_cli_process,
430         .type = VLIB_NODE_TYPE_PROCESS,
431         .process_log2_n_stack_bytes = 16,
432         .runtime_data_bytes = sizeof (void *),
433       };
434
435       name = (char *) format (0, "http-cli-%d", l);
436       r.name = name;
437       vlib_register_node (vm, &r);
438       vec_free (name);
439
440       n = vlib_get_node (vm, r.index);
441     }
442
443   /* Save the node index in the args. It won't be zero. */
444   args->node_index = n->index;
445
446   /* Save the args (pointer) in the node runtime */
447   save_args = vlib_node_get_runtime_data (vm, n->index);
448   *save_args = clib_mem_alloc (sizeof (*args));
449   clib_memcpy_fast (*save_args, args, sizeof (*args));
450
451   vlib_start_process (vm, n->runtime_index);
452 }
453
454 static void
455 alloc_http_process_callback (void *cb_args)
456 {
457   alloc_http_process ((http_server_args *) cb_args);
458 }
459
460 static int
461 session_rx_request (http_session_t * hs)
462 {
463   u32 max_dequeue, cursize;
464   int n_read;
465
466   cursize = vec_len (hs->rx_buf);
467   max_dequeue = svm_fifo_max_dequeue (hs->rx_fifo);
468   if (PREDICT_FALSE (max_dequeue == 0))
469     return -1;
470
471   vec_validate (hs->rx_buf, cursize + max_dequeue - 1);
472   n_read = app_recv_stream_raw (hs->rx_fifo, hs->rx_buf + cursize,
473                                 max_dequeue, 0, 0 /* peek */ );
474   ASSERT (n_read == max_dequeue);
475   if (svm_fifo_is_empty (hs->rx_fifo))
476     svm_fifo_unset_event (hs->rx_fifo);
477
478   _vec_len (hs->rx_buf) = cursize + n_read;
479   return 0;
480 }
481
482 static int
483 http_server_rx_callback (session_t * s)
484 {
485   http_server_args args;
486   http_session_t *hs;
487   int rv;
488
489   http_server_sessions_reader_lock ();
490
491   hs = http_server_session_lookup (s->thread_index, s->session_index);
492   if (!hs || hs->session_state != HTTP_STATE_ESTABLISHED)
493     return -1;
494
495   rv = session_rx_request (hs);
496   if (rv)
497     return rv;
498
499   /* send the command to a new/recycled vlib process */
500   args.hs_index = hs->session_index;
501   args.thread_index = hs->thread_index;
502
503   http_server_sessions_reader_unlock ();
504
505   /* Send RPC request to main thread */
506   if (vlib_get_thread_index () != 0)
507     vlib_rpc_call_main_thread (alloc_http_process_callback, (u8 *) & args,
508                                sizeof (args));
509   else
510     alloc_http_process (&args);
511   return 0;
512 }
513
514 static int
515 http_server_rx_callback_static (session_t * s)
516 {
517   http_server_main_t *hsm = &http_server_main;
518   vnet_disconnect_args_t _a = { 0 }, *a = &_a;
519   http_session_t *hs;
520   u32 request_len;
521   u8 *request = 0;
522   int i, rv;
523
524   hs = http_server_session_lookup (s->thread_index, s->session_index);
525   if (!hs || hs->session_state == HTTP_STATE_CLOSED)
526     return 0;
527
528   /* ok 200 was sent */
529   if (hs->session_state == HTTP_STATE_OK_SENT)
530     goto send_data;
531
532   rv = session_rx_request (hs);
533   if (rv)
534     goto wait_for_data;
535
536   request = hs->rx_buf;
537   request_len = vec_len (request);
538   if (vec_len (request) < 7)
539     {
540       send_error (hs, "400 Bad Request");
541       goto close_session;
542     }
543
544   for (i = 0; i < request_len - 4; i++)
545     {
546       if (request[i] == 'G' &&
547           request[i + 1] == 'E' &&
548           request[i + 2] == 'T' && request[i + 3] == ' ')
549         goto find_end;
550     }
551   send_error (hs, "400 Bad Request");
552   goto close_session;
553
554 find_end:
555
556   /* check for the end sequence: /r/n/r/n */
557   if (request[request_len - 1] != 0xa || request[request_len - 3] != 0xa
558       || request[request_len - 2] != 0xd || request[request_len - 4] != 0xd)
559     goto wait_for_data;
560
561   /* send 200 OK first */
562   send_data (hs, static_ok);
563   hs->session_state = HTTP_STATE_OK_SENT;
564   goto postpone;
565
566 send_data:
567   send_data (hs, static_http);
568   http_server_session_cleanup (hs);
569
570 close_session:
571   a->handle = session_handle (s);
572   a->app_index = hsm->app_index;
573   vnet_disconnect_session (a);
574   return 0;
575
576 postpone:
577   (void) svm_fifo_set_event (hs->rx_fifo);
578   session_send_io_evt_to_thread (hs->rx_fifo, SESSION_IO_EVT_BUILTIN_RX);
579   return 0;
580
581 wait_for_data:
582   return 0;
583 }
584
585 static int
586 http_server_session_accept_callback (session_t * s)
587 {
588   http_server_main_t *hsm = &http_server_main;
589   http_session_t *hs;
590
591   hsm->vpp_queue[s->thread_index] =
592     session_main_get_vpp_event_queue (s->thread_index);
593
594   if (!hsm->is_static)
595     http_server_sessions_writer_lock ();
596
597   hs = http_server_session_alloc (s->thread_index);
598   http_server_session_lookup_add (s->thread_index, s->session_index,
599                                   hs->session_index);
600   hs->rx_fifo = s->rx_fifo;
601   hs->tx_fifo = s->tx_fifo;
602   hs->vpp_session_index = s->session_index;
603   hs->vpp_session_handle = session_handle (s);
604   hs->session_state = HTTP_STATE_ESTABLISHED;
605
606   if (!hsm->is_static)
607     http_server_sessions_writer_unlock ();
608
609   s->session_state = SESSION_STATE_READY;
610   return 0;
611 }
612
613 static void
614 http_server_session_disconnect_callback (session_t * s)
615 {
616   http_server_main_t *hsm = &http_server_main;
617   vnet_disconnect_args_t _a = { 0 }, *a = &_a;
618   http_session_t *hs;
619
620   if (!hsm->is_static)
621     http_server_sessions_writer_lock ();
622
623   hs = http_server_session_lookup (s->thread_index, s->session_index);
624   http_server_session_cleanup (hs);
625
626   if (!hsm->is_static)
627     http_server_sessions_writer_unlock ();
628
629   a->handle = session_handle (s);
630   a->app_index = hsm->app_index;
631   vnet_disconnect_session (a);
632 }
633
634 static void
635 http_server_session_reset_callback (session_t * s)
636 {
637   http_server_main_t *hsm = &http_server_main;
638   vnet_disconnect_args_t _a = { 0 }, *a = &_a;
639   http_session_t *hs;
640
641   if (!hsm->is_static)
642     http_server_sessions_writer_lock ();
643
644   hs = http_server_session_lookup (s->thread_index, s->session_index);
645   http_server_session_cleanup (hs);
646
647   if (!hsm->is_static)
648     http_server_sessions_writer_unlock ();
649
650   a->handle = session_handle (s);
651   a->app_index = hsm->app_index;
652   vnet_disconnect_session (a);
653 }
654
655 static int
656 http_server_session_connected_callback (u32 app_index, u32 api_context,
657                                         session_t * s, u8 is_fail)
658 {
659   clib_warning ("called...");
660   return -1;
661 }
662
663 static int
664 http_server_add_segment_callback (u32 client_index, u64 segment_handle)
665 {
666   clib_warning ("called...");
667   return -1;
668 }
669
670 static session_cb_vft_t http_server_session_cb_vft = {
671   .session_accept_callback = http_server_session_accept_callback,
672   .session_disconnect_callback = http_server_session_disconnect_callback,
673   .session_connected_callback = http_server_session_connected_callback,
674   .add_segment_callback = http_server_add_segment_callback,
675   .builtin_app_rx_callback = http_server_rx_callback,
676   .session_reset_callback = http_server_session_reset_callback
677 };
678
679 static int
680 http_server_attach ()
681 {
682   vnet_app_add_tls_cert_args_t _a_cert, *a_cert = &_a_cert;
683   vnet_app_add_tls_key_args_t _a_key, *a_key = &_a_key;
684   http_server_main_t *hsm = &http_server_main;
685   u64 options[APP_OPTIONS_N_OPTIONS];
686   vnet_app_attach_args_t _a, *a = &_a;
687   u32 segment_size = 128 << 20;
688
689   clib_memset (a, 0, sizeof (*a));
690   clib_memset (options, 0, sizeof (options));
691
692   if (hsm->private_segment_size)
693     segment_size = hsm->private_segment_size;
694
695   a->api_client_index = ~0;
696   a->name = format (0, "test_http_server");
697   a->session_cb_vft = &http_server_session_cb_vft;
698   a->options = options;
699   a->options[APP_OPTIONS_SEGMENT_SIZE] = segment_size;
700   a->options[APP_OPTIONS_RX_FIFO_SIZE] =
701     hsm->fifo_size ? hsm->fifo_size : 8 << 10;
702   a->options[APP_OPTIONS_TX_FIFO_SIZE] =
703     hsm->fifo_size ? hsm->fifo_size : 32 << 10;
704   a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
705   a->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = hsm->prealloc_fifos;
706
707   if (vnet_application_attach (a))
708     {
709       vec_free (a->name);
710       clib_warning ("failed to attach server");
711       return -1;
712     }
713   vec_free (a->name);
714   hsm->app_index = a->app_index;
715
716   clib_memset (a_cert, 0, sizeof (*a_cert));
717   a_cert->app_index = a->app_index;
718   vec_validate (a_cert->cert, test_srv_crt_rsa_len);
719   clib_memcpy_fast (a_cert->cert, test_srv_crt_rsa, test_srv_crt_rsa_len);
720   vnet_app_add_tls_cert (a_cert);
721
722   clib_memset (a_key, 0, sizeof (*a_key));
723   a_key->app_index = a->app_index;
724   vec_validate (a_key->key, test_srv_key_rsa_len);
725   clib_memcpy_fast (a_key->key, test_srv_key_rsa, test_srv_key_rsa_len);
726   vnet_app_add_tls_key (a_key);
727
728   return 0;
729 }
730
731 static int
732 http_server_listen ()
733 {
734   http_server_main_t *hsm = &http_server_main;
735   vnet_listen_args_t _a, *a = &_a;
736   clib_memset (a, 0, sizeof (*a));
737   a->app_index = hsm->app_index;
738   a->uri = "tcp://0.0.0.0/80";
739   if (hsm->uri)
740     a->uri = (char *) hsm->uri;
741   return vnet_bind_uri (a);
742 }
743
744 static int
745 http_server_create (vlib_main_t * vm)
746 {
747   vlib_thread_main_t *vtm = vlib_get_thread_main ();
748   http_server_main_t *hsm = &http_server_main;
749   u32 num_threads;
750
751   num_threads = 1 /* main thread */  + vtm->n_threads;
752   vec_validate (hsm->vpp_queue, num_threads - 1);
753   vec_validate (hsm->sessions, num_threads - 1);
754   vec_validate (hsm->session_to_http_session, num_threads - 1);
755
756   clib_rwlock_init (&hsm->sessions_lock);
757
758   if (http_server_attach ())
759     {
760       clib_warning ("failed to attach server");
761       return -1;
762     }
763   if (http_server_listen ())
764     {
765       clib_warning ("failed to start listening");
766       return -1;
767     }
768   return 0;
769 }
770
771 static clib_error_t *
772 http_server_create_command_fn (vlib_main_t * vm,
773                                unformat_input_t * input,
774                                vlib_cli_command_t * cmd)
775 {
776   http_server_main_t *hsm = &http_server_main;
777   unformat_input_t _line_input, *line_input = &_line_input;
778   u64 seg_size;
779   u8 *html;
780   int rv;
781
782   hsm->prealloc_fifos = 0;
783   hsm->private_segment_size = 0;
784   hsm->fifo_size = 0;
785   hsm->is_static = 0;
786
787   /* Get a line of input. */
788   if (!unformat_user (input, unformat_line_input, line_input))
789     goto start_server;
790
791   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
792     {
793       if (unformat (line_input, "static"))
794         hsm->is_static = 1;
795       else
796         if (unformat (line_input, "prealloc-fifos %d", &hsm->prealloc_fifos))
797         ;
798       else if (unformat (line_input, "private-segment-size %U",
799                          unformat_memory_size, &seg_size))
800         {
801           if (seg_size >= 0x100000000ULL)
802             {
803               vlib_cli_output (vm, "private segment size %llu, too large",
804                                seg_size);
805               return 0;
806             }
807           hsm->private_segment_size = seg_size;
808         }
809       else if (unformat (line_input, "fifo-size %d", &hsm->fifo_size))
810         hsm->fifo_size <<= 10;
811       else if (unformat (line_input, "uri %s", &hsm->uri))
812         ;
813       else
814         return clib_error_return (0, "unknown input `%U'",
815                                   format_unformat_error, line_input);
816     }
817   unformat_free (line_input);
818
819 start_server:
820
821   if (hsm->my_client_index != (u32) ~ 0)
822     return clib_error_return (0, "test http server is already running");
823
824   vnet_session_enable_disable (vm, 1 /* turn on TCP, etc. */ );
825
826   if (hsm->is_static)
827     {
828       http_server_session_cb_vft.builtin_app_rx_callback =
829         http_server_rx_callback_static;
830       html = format (0, html_header_static);
831       static_http = format (0, http_response, vec_len (html), html);
832       static_ok = format (0, http_ok);
833     }
834   rv = http_server_create (vm);
835   switch (rv)
836     {
837     case 0:
838       break;
839     default:
840       return clib_error_return (0, "server_create returned %d", rv);
841     }
842   return 0;
843 }
844
845 /* *INDENT-OFF* */
846 VLIB_CLI_COMMAND (http_server_create_command, static) =
847 {
848   .path = "test http server",
849   .short_help = "test http server",
850   .function = http_server_create_command_fn,
851 };
852 /* *INDENT-ON* */
853
854 static clib_error_t *
855 http_server_main_init (vlib_main_t * vm)
856 {
857   http_server_main_t *hsm = &http_server_main;
858
859   hsm->my_client_index = ~0;
860   hsm->vlib_main = vm;
861   return 0;
862 }
863
864 VLIB_INIT_FUNCTION (http_server_main_init);
865
866 /*
867 * fd.io coding-style-patch-verification: ON
868 *
869 * Local Variables:
870 * eval: (c-set-style "gnu")
871 * End:
872 */