FD.io VPP  v20.09-64-g4f7b92f0a
Vector Packet Processing
http_server.c
Go to the documentation of this file.
1 /*
2 * Copyright (c) 2017-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>
19 #include <vnet/session/session.h>
21 
22 typedef enum
23 {
26 
27 typedef struct
28 {
33 
34 typedef enum
35 {
40 typedef struct
41 {
42  CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
43 #define _(type, name) type name;
45 #undef _
52 
53 typedef struct
54 {
58 
60 
62 
64 
65  /* Sever's event queue */
67 
68  /* API client handle */
70 
72 
73  /* process node index for evnt scheduling */
75 
76  tw_timer_wheel_2t_1w_2048sl_t tw;
78 
82  u8 *uri;
86 
88 
89 static void
91 {
92  clib_rwlock_reader_lock (&http_server_main.sessions_lock);
93 }
94 
95 static void
97 {
98  clib_rwlock_reader_unlock (&http_server_main.sessions_lock);
99 }
100 
101 static void
103 {
104  clib_rwlock_writer_lock (&http_server_main.sessions_lock);
105 }
106 
107 static void
109 {
110  clib_rwlock_writer_unlock (&http_server_main.sessions_lock);
111 }
112 
113 static http_session_t *
115 {
117  http_session_t *hs;
118  pool_get (hsm->sessions[thread_index], hs);
119  memset (hs, 0, sizeof (*hs));
120  hs->session_index = hs - hsm->sessions[thread_index];
121  hs->thread_index = thread_index;
122  hs->timer_handle = ~0;
123  return hs;
124 }
125 
126 static http_session_t *
127 http_server_session_get (u32 thread_index, u32 hs_index)
128 {
130  if (pool_is_free_index (hsm->sessions[thread_index], hs_index))
131  return 0;
132  return pool_elt_at_index (hsm->sessions[thread_index], hs_index);
133 }
134 
135 static void
137 {
139  pool_put (hsm->sessions[hs->thread_index], hs);
140  if (CLIB_DEBUG)
141  memset (hs, 0xfa, sizeof (*hs));
142 }
143 
144 static void
145 http_server_session_lookup_add (u32 thread_index, u32 s_index, u32 hs_index)
146 {
148  vec_validate (hsm->session_to_http_session[thread_index], s_index);
149  hsm->session_to_http_session[thread_index][s_index] = hs_index;
150 }
151 
152 static void
153 http_server_session_lookup_del (u32 thread_index, u32 s_index)
154 {
156  hsm->session_to_http_session[thread_index][s_index] = ~0;
157 }
158 
159 static http_session_t *
160 http_server_session_lookup (u32 thread_index, u32 s_index)
161 {
163  u32 hs_index;
164 
165  if (s_index < vec_len (hsm->session_to_http_session[thread_index]))
166  {
167  hs_index = hsm->session_to_http_session[thread_index][s_index];
168  return http_server_session_get (thread_index, hs_index);
169  }
170  return 0;
171 }
172 
173 
174 static void
176 {
177  u32 hs_handle;
178  hs_handle = hs->thread_index << 24 | hs->session_index;
179  clib_spinlock_lock (&http_server_main.tw_lock);
180  hs->timer_handle = tw_timer_start_2t_1w_2048sl (&http_server_main.tw,
181  hs_handle, 0, 60);
182  clib_spinlock_unlock (&http_server_main.tw_lock);
183 }
184 
185 static void
187 {
188  if (hs->timer_handle == ~0)
189  return;
190  clib_spinlock_lock (&http_server_main.tw_lock);
191  tw_timer_stop_2t_1w_2048sl (&http_server_main.tw, hs->timer_handle);
192  clib_spinlock_unlock (&http_server_main.tw_lock);
193 }
194 
195 static void
197 {
198  vnet_disconnect_args_t _a = { 0 }, *a = &_a;
199  a->handle = hs->vpp_session_handle;
200  a->app_index = http_server_main.app_index;
202 }
203 
204 static void
206 {
210  vlib_node_t *n;
211  u32 node_index;
212  http_server_args **save_args;
213 
214  node_index = args->node_index;
215  ASSERT (node_index != 0);
216 
217  n = vlib_get_node (vm, node_index);
218  rt = vlib_node_get_runtime (vm, n->index);
219  save_args = vlib_node_get_runtime_data (vm, n->index);
220 
221  /* Reset process session pointer */
222  clib_mem_free (*save_args);
223  *save_args = 0;
224 
225  /* Turn off the process node */
226  vlib_node_set_state (vm, rt->node_index, VLIB_NODE_STATE_DISABLED);
227 
228  /* add node index to the freelist */
229  vec_add1 (hsm->free_http_cli_process_node_indices, node_index);
230 }
231 
232 /* *INDENT-OFF* */
233 static const char *http_ok =
234  "HTTP/1.1 200 OK\r\n";
235 
236 static const char *http_response =
237  "Content-Type: text/html\r\n"
238  "Expires: Mon, 11 Jan 1970 10:10:10 GMT\r\n"
239  "Connection: close \r\n"
240  "Pragma: no-cache\r\n"
241  "Content-Length: %d\r\n\r\n%s";
242 
243 static const char *http_error_template =
244  "HTTP/1.1 %s\r\n"
245  "Content-Type: text/html\r\n"
246  "Expires: Mon, 11 Jan 1970 10:10:10 GMT\r\n"
247  "Connection: close\r\n"
248  "Pragma: no-cache\r\n"
249  "Content-Length: 0\r\n\r\n";
250 
251 /* Header, including incantation to suppress favicon.ico requests */
252 static const char *html_header_template =
253  "<html><head><title>%v</title></head>"
254  "<link rel=\"icon\" href=\"data:,\">"
255  "<body><pre>";
256 
257 static const char *html_footer =
258  "</pre></body></html>\r\n";
259 
260 static const char *html_header_static =
261  "<html><head><title>static reply</title></head>"
262  "<link rel=\"icon\" href=\"data:,\">"
263  "<body><pre>hello</pre></body></html>\r\n";
264 /* *INDENT-ON* */
265 
266 static u8 *static_http;
267 static u8 *static_ok;
268 
269 static void
270 http_cli_output (uword arg, u8 * buffer, uword buffer_bytes)
271 {
272  u8 **output_vecp = (u8 **) arg;
273  u8 *output_vec;
274  u32 offset;
275 
276  output_vec = *output_vecp;
277 
278  offset = vec_len (output_vec);
279  vec_validate (output_vec, offset + buffer_bytes - 1);
280  clib_memcpy_fast (output_vec + offset, buffer, buffer_bytes);
281 
282  *output_vecp = output_vec;
283 }
284 
285 void
287 {
289  vnet_disconnect_args_t _a = { 0 }, *a = &_a;
291  f64 last_sent_timer = vlib_time_now (vm);
292  u32 offset, bytes_to_send;
293  f64 delay = 10e-3;
294 
295  bytes_to_send = vec_len (data);
296  offset = 0;
297 
298  while (bytes_to_send > 0)
299  {
300  int actual_transfer;
301 
302  actual_transfer = svm_fifo_enqueue
303  (hs->tx_fifo, bytes_to_send, data + offset);
304 
305  /* Made any progress? */
306  if (actual_transfer <= 0)
307  {
309  vlib_process_suspend (vm, delay);
311 
312  /* 10s deadman timer */
313  if (vlib_time_now (vm) > last_sent_timer + 10.0)
314  {
315  a->handle = hs->vpp_session_handle;
316  a->app_index = hsm->app_index;
318  break;
319  }
320  /* Exponential backoff, within reason */
321  if (delay < 1.0)
322  delay = delay * 2.0;
323  }
324  else
325  {
326  last_sent_timer = vlib_time_now (vm);
327  offset += actual_transfer;
328  bytes_to_send -= actual_transfer;
329 
330  if (svm_fifo_set_event (hs->tx_fifo))
331  session_send_io_evt_to_thread (hs->tx_fifo,
333  delay = 10e-3;
334  }
335  }
336 }
337 
338 static void
339 send_error (http_session_t * hs, char *str)
340 {
341  u8 *data;
342 
343  data = format (0, http_error_template, str);
344  send_data (hs, data);
345  vec_free (data);
346 }
347 
348 static uword
350  vlib_frame_t * f)
351 {
352  u8 *request = 0, *reply = 0, *http = 0, *html = 0;
354  http_server_args **save_args;
355  http_server_args *args;
356  unformat_input_t input;
357  http_session_t *hs;
358  int i;
359 
360  save_args = vlib_node_get_runtime_data (hsm->vlib_main, rt->node_index);
361  args = *save_args;
362 
364 
365  hs = http_server_session_get (args->thread_index, args->hs_index);
366  ASSERT (hs);
367 
368  request = hs->rx_buf;
369  if (vec_len (request) < 7)
370  {
371  send_error (hs, "400 Bad Request");
372  goto out;
373  }
374 
375  for (i = 0; i < vec_len (request) - 4; i++)
376  {
377  if (request[i] == 'G' &&
378  request[i + 1] == 'E' &&
379  request[i + 2] == 'T' && request[i + 3] == ' ')
380  goto found;
381  }
382 bad_request:
383  send_error (hs, "400 Bad Request");
384  goto out;
385 
386 found:
387  /* Lose "GET " */
388  vec_delete (request, i + 5, 0);
389 
390  /* Replace slashes with spaces, stop at the end of the path */
391  i = 0;
392  while (1)
393  {
394  if (request[i] == '/')
395  request[i] = ' ';
396  else if (request[i] == ' ')
397  {
398  /* vlib_cli_input is vector-based, no need for a NULL */
399  _vec_len (request) = i;
400  break;
401  }
402  i++;
403  /* Should never happen */
404  if (i == vec_len (request))
405  goto bad_request;
406  }
407 
408  /* Generate the html header */
409  html = format (0, html_header_template, request /* title */ );
410 
411  /* Run the command */
412  unformat_init_vector (&input, vec_dup (request));
413  vlib_cli_input (vm, &input, http_cli_output, (uword) & reply);
414  unformat_free (&input);
415  request = 0;
416 
417  /* Generate the html page */
418  html = format (html, "%v", reply);
419  html = format (html, html_footer);
420  /* And the http reply */
421  http = format (0, http_ok);
422  http = format (http, http_response, vec_len (html), html);
423 
424  /* Send it */
425  send_data (hs, http);
426 
427 out:
428  /* Cleanup */
430  vec_free (reply);
431  vec_free (html);
432  vec_free (http);
433 
434  http_process_free (args);
435  return (0);
436 }
437 
438 static void
440 {
441  char *name;
442  vlib_node_t *n;
444  vlib_main_t *vm = hsm->vlib_main;
446  http_server_args **save_args;
447 
449  {
451  vlib_node_set_state (vm, n->index, VLIB_NODE_STATE_POLLING);
452  _vec_len (hsm->free_http_cli_process_node_indices) = l - 1;
453  }
454  else
455  {
456  static vlib_node_registration_t r = {
457  .function = http_cli_process,
458  .type = VLIB_NODE_TYPE_PROCESS,
459  .process_log2_n_stack_bytes = 16,
460  .runtime_data_bytes = sizeof (void *),
461  };
462 
463  name = (char *) format (0, "http-cli-%d", l);
464  r.name = name;
465  vlib_register_node (vm, &r);
466  vec_free (name);
467 
468  n = vlib_get_node (vm, r.index);
469  }
470 
471  /* Save the node index in the args. It won't be zero. */
472  args->node_index = n->index;
473 
474  /* Save the args (pointer) in the node runtime */
475  save_args = vlib_node_get_runtime_data (vm, n->index);
476  *save_args = clib_mem_alloc (sizeof (*args));
477  clib_memcpy_fast (*save_args, args, sizeof (*args));
478 
480 }
481 
482 static void
484 {
485  alloc_http_process ((http_server_args *) cb_args);
486 }
487 
488 static int
490 {
491  u32 max_dequeue, cursize;
492  int n_read;
493 
494  cursize = vec_len (hs->rx_buf);
495  max_dequeue = svm_fifo_max_dequeue_cons (hs->rx_fifo);
496  if (PREDICT_FALSE (max_dequeue == 0))
497  return -1;
498 
499  vec_validate (hs->rx_buf, cursize + max_dequeue - 1);
500  n_read = app_recv_stream_raw (hs->rx_fifo, hs->rx_buf + cursize,
501  max_dequeue, 0, 0 /* peek */ );
502  ASSERT (n_read == max_dequeue);
503  if (svm_fifo_is_empty_cons (hs->rx_fifo))
504  svm_fifo_unset_event (hs->rx_fifo);
505 
506  _vec_len (hs->rx_buf) = cursize + n_read;
507  return 0;
508 }
509 
510 static int
512 {
513  http_server_args args;
514  http_session_t *hs;
515  int rv;
516 
518 
520  if (!hs || hs->session_state != HTTP_STATE_ESTABLISHED)
521  return -1;
522 
523  rv = session_rx_request (hs);
524  if (rv)
525  return rv;
526 
527  /* send the command to a new/recycled vlib process */
528  args.hs_index = hs->session_index;
529  args.thread_index = hs->thread_index;
530 
532 
533  /* Send RPC request to main thread */
534  if (vlib_get_thread_index () != 0)
536  sizeof (args));
537  else
538  alloc_http_process (&args);
539  return 0;
540 }
541 
542 static int
544 {
545  http_session_t *hs;
546  u32 request_len;
547  u8 *request = 0;
548  int i, rv;
549 
551  if (!hs || hs->session_state == HTTP_STATE_CLOSED)
552  return 0;
553 
554  /* ok 200 was sent */
555  if (hs->session_state == HTTP_STATE_OK_SENT)
556  goto send_data;
557 
558  rv = session_rx_request (hs);
559  if (rv)
560  goto wait_for_data;
561 
562  request = hs->rx_buf;
563  request_len = vec_len (request);
564  if (vec_len (request) < 7)
565  {
566  send_error (hs, "400 Bad Request");
567  goto close_session;
568  }
569 
570  for (i = 0; i < request_len - 4; i++)
571  {
572  if (request[i] == 'G' &&
573  request[i + 1] == 'E' &&
574  request[i + 2] == 'T' && request[i + 3] == ' ')
575  goto find_end;
576  }
577  send_error (hs, "400 Bad Request");
578  goto close_session;
579 
580 find_end:
581 
582  /* check for the end sequence: /r/n/r/n */
583  if (request[request_len - 1] != 0xa || request[request_len - 3] != 0xa
584  || request[request_len - 2] != 0xd || request[request_len - 4] != 0xd)
585  goto wait_for_data;
586 
587  /* send 200 OK first */
588  send_data (hs, static_ok);
589  hs->session_state = HTTP_STATE_OK_SENT;
590  goto postpone;
591 
592 send_data:
593  send_data (hs, static_http);
594 
597  return 0;
598 
599 postpone:
600  (void) svm_fifo_set_event (hs->rx_fifo);
602  return 0;
603 
604 wait_for_data:
605  return 0;
606 }
607 
608 static int
610 {
612  http_session_t *hs;
613 
614  hsm->vpp_queue[s->thread_index] =
616 
617  if (!hsm->is_static)
619 
622  hs->session_index);
623  hs->rx_fifo = s->rx_fifo;
624  hs->tx_fifo = s->tx_fifo;
627  hs->session_state = HTTP_STATE_ESTABLISHED;
629 
630  if (!hsm->is_static)
632 
633  s->session_state = SESSION_STATE_READY;
634  return 0;
635 }
636 
637 static void
639 {
641  vnet_disconnect_args_t _a = { 0 }, *a = &_a;
642 
643  a->handle = session_handle (s);
644  a->app_index = hsm->app_index;
646 }
647 
648 static void
650 {
652  vnet_disconnect_args_t _a = { 0 }, *a = &_a;
653 
654  a->handle = session_handle (s);
655  a->app_index = hsm->app_index;
657 }
658 
659 static int
661  session_t * s, session_error_t err)
662 {
663  clib_warning ("called...");
664  return -1;
665 }
666 
667 static int
668 http_server_add_segment_callback (u32 client_index, u64 segment_handle)
669 {
670  clib_warning ("called...");
671  return -1;
672 }
673 
674 static void
676 {
678  http_session_t *hs;
679 
680  if (ntf == SESSION_CLEANUP_TRANSPORT)
681  return;
682 
683  if (!hsm->is_static)
685 
687  if (!hs)
688  goto done;
689 
691  vec_free (hs->rx_buf);
694 
695 done:
696 
697  if (!hsm->is_static)
699 }
700 
701 static session_cb_vft_t http_server_session_cb_vft = {
703  .session_disconnect_callback = http_server_session_disconnect_callback,
704  .session_connected_callback = http_server_session_connected_callback,
705  .add_segment_callback = http_server_add_segment_callback,
706  .builtin_app_rx_callback = http_server_rx_callback,
707  .session_reset_callback = http_server_session_reset_callback,
708  .session_cleanup_callback = http_server_cleanup_callback,
709 };
710 
711 static int
713 {
714  vnet_app_add_tls_cert_args_t _a_cert, *a_cert = &_a_cert;
715  vnet_app_add_tls_key_args_t _a_key, *a_key = &_a_key;
718  vnet_app_attach_args_t _a, *a = &_a;
719  u32 segment_size = 128 << 20;
720 
721  clib_memset (a, 0, sizeof (*a));
722  clib_memset (options, 0, sizeof (options));
723 
724  if (hsm->private_segment_size)
725  segment_size = hsm->private_segment_size;
726 
727  a->api_client_index = ~0;
728  a->name = format (0, "test_http_server");
729  a->session_cb_vft = &http_server_session_cb_vft;
730  a->options = options;
731  a->options[APP_OPTIONS_SEGMENT_SIZE] = segment_size;
732  a->options[APP_OPTIONS_RX_FIFO_SIZE] =
733  hsm->fifo_size ? hsm->fifo_size : 8 << 10;
734  a->options[APP_OPTIONS_TX_FIFO_SIZE] =
735  hsm->fifo_size ? hsm->fifo_size : 32 << 10;
736  a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
738 
739  if (vnet_application_attach (a))
740  {
741  vec_free (a->name);
742  clib_warning ("failed to attach server");
743  return -1;
744  }
745  vec_free (a->name);
746  hsm->app_index = a->app_index;
747 
748  clib_memset (a_cert, 0, sizeof (*a_cert));
749  a_cert->app_index = a->app_index;
750  vec_validate (a_cert->cert, test_srv_crt_rsa_len);
752  vnet_app_add_tls_cert (a_cert);
753 
754  clib_memset (a_key, 0, sizeof (*a_key));
755  a_key->app_index = a->app_index;
756  vec_validate (a_key->key, test_srv_key_rsa_len);
758  vnet_app_add_tls_key (a_key);
759 
760  return 0;
761 }
762 
763 static int
765 {
767  vnet_listen_args_t _a, *a = &_a;
768  clib_memset (a, 0, sizeof (*a));
769  a->app_index = hsm->app_index;
770  a->uri = "tcp://0.0.0.0/80";
771  if (hsm->uri)
772  a->uri = (char *) hsm->uri;
773  return vnet_bind_uri (a);
774 }
775 
776 static void
778 {
779  http_session_t *hs;
780  uword hs_handle;
781  hs_handle = pointer_to_uword (hs_handlep);
782  hs = http_server_session_get (hs_handle >> 24, hs_handle & 0x00FFFFFF);
783  if (!hs)
784  return;
785  hs->timer_handle = ~0;
787 }
788 
789 static void
791 {
792  u32 hs_handle;
793  int i;
794 
795  for (i = 0; i < vec_len (expired_timers); i++)
796  {
797  /* Get session handle. The first bit is the timer id */
798  hs_handle = expired_timers[i] & 0x7FFFFFFF;
799  session_send_rpc_evt_to_thread (hs_handle >> 24,
801  uword_to_pointer (hs_handle, void *));
802  }
803 }
804 
805 static uword
807  vlib_frame_t * f)
808 {
810  f64 now, timeout = 1.0;
811  uword *event_data = 0;
812  uword __clib_unused event_type;
813 
814  while (1)
815  {
817  now = vlib_time_now (vm);
818  event_type = vlib_process_get_events (vm, (uword **) & event_data);
819 
820  /* expire timers */
821  clib_spinlock_lock (&http_server_main.tw_lock);
822  tw_timer_expire_timers_2t_1w_2048sl (&hsm->tw, now);
823  clib_spinlock_unlock (&http_server_main.tw_lock);
824 
825  vec_reset_length (event_data);
826  }
827  return 0;
828 }
829 
830 /* *INDENT-OFF* */
832 {
833  .function = http_server_process,
834  .type = VLIB_NODE_TYPE_PROCESS,
835  .name = "http-server-process",
836  .state = VLIB_NODE_STATE_DISABLED,
837 };
838 /* *INDENT-ON* */
839 
840 static int
842 {
845  u32 num_threads;
846  vlib_node_t *n;
847 
848  num_threads = 1 /* main thread */ + vtm->n_threads;
849  vec_validate (hsm->vpp_queue, num_threads - 1);
850  vec_validate (hsm->sessions, num_threads - 1);
851  vec_validate (hsm->session_to_http_session, num_threads - 1);
852 
854  clib_spinlock_init (&hsm->tw_lock);
855 
856  if (http_server_attach ())
857  {
858  clib_warning ("failed to attach server");
859  return -1;
860  }
861  if (http_server_listen ())
862  {
863  clib_warning ("failed to start listening");
864  return -1;
865  }
866 
867  /* Init timer wheel and process */
868  tw_timer_wheel_init_2t_1w_2048sl (&hsm->tw, http_expired_timers_dispatch,
869  1 /* timer interval */ , ~0);
871  VLIB_NODE_STATE_POLLING);
872  n = vlib_get_node (vm, http_server_process_node.index);
874 
875  return 0;
876 }
877 
878 static clib_error_t *
880  unformat_input_t * input,
881  vlib_cli_command_t * cmd)
882 {
884  unformat_input_t _line_input, *line_input = &_line_input;
885  u64 seg_size;
886  u8 *html;
887  int rv;
888 
889  hsm->prealloc_fifos = 0;
890  hsm->private_segment_size = 0;
891  hsm->fifo_size = 0;
892  hsm->is_static = 0;
893 
894  /* Get a line of input. */
895  if (!unformat_user (input, unformat_line_input, line_input))
896  goto start_server;
897 
898  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
899  {
900  if (unformat (line_input, "static"))
901  hsm->is_static = 1;
902  else
903  if (unformat (line_input, "prealloc-fifos %d", &hsm->prealloc_fifos))
904  ;
905  else if (unformat (line_input, "private-segment-size %U",
906  unformat_memory_size, &seg_size))
907  {
908  if (seg_size >= 0x100000000ULL)
909  {
910  vlib_cli_output (vm, "private segment size %llu, too large",
911  seg_size);
912  return 0;
913  }
914  hsm->private_segment_size = seg_size;
915  }
916  else if (unformat (line_input, "fifo-size %d", &hsm->fifo_size))
917  hsm->fifo_size <<= 10;
918  else if (unformat (line_input, "uri %s", &hsm->uri))
919  ;
920  else
921  return clib_error_return (0, "unknown input `%U'",
922  format_unformat_error, line_input);
923  }
924  unformat_free (line_input);
925 
926 start_server:
927 
928  if (hsm->my_client_index != (u32) ~ 0)
929  return clib_error_return (0, "test http server is already running");
930 
931  vnet_session_enable_disable (vm, 1 /* turn on TCP, etc. */ );
932 
933  if (hsm->is_static)
934  {
935  http_server_session_cb_vft.builtin_app_rx_callback =
937  html = format (0, html_header_static);
938  static_http = format (0, http_response, vec_len (html), html);
939  static_ok = format (0, http_ok);
940  }
941  rv = http_server_create (vm);
942  switch (rv)
943  {
944  case 0:
945  break;
946  default:
947  return clib_error_return (0, "server_create returned %d", rv);
948  }
949  return 0;
950 }
951 
952 /* *INDENT-OFF* */
953 VLIB_CLI_COMMAND (http_server_create_command, static) =
954 {
955  .path = "test http server",
956  .short_help = "test http server",
957  .function = http_server_create_command_fn,
958 };
959 /* *INDENT-ON* */
960 
961 static clib_error_t *
963 {
965 
966  hsm->my_client_index = ~0;
967  hsm->vlib_main = vm;
968  return 0;
969 }
970 
972 
973 /*
974 * fd.io coding-style-patch-verification: ON
975 *
976 * Local Variables:
977 * eval: (c-set-style "gnu")
978 * End:
979 */
static void http_server_session_timer_start(http_session_t *hs)
Definition: http_server.c:175
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:509
#define foreach_app_session_field
flag for dgram mode
static void clib_rwlock_reader_lock(clib_rwlock_t *p)
Definition: lock.h:167
static void http_server_sessions_reader_unlock(void)
Definition: http_server.c:96
static int app_recv_stream_raw(svm_fifo_t *f, u8 *buf, u32 len, u8 clear_evt, u8 peek)
vlib_main_t vlib_global_main
Definition: main.c:1983
#define CLIB_CACHE_LINE_ALIGN_MARK(mark)
Definition: cache.h:60
static uword http_server_process(vlib_main_t *vm, vlib_node_runtime_t *rt, vlib_frame_t *f)
Definition: http_server.c:806
static_always_inline void clib_spinlock_unlock(clib_spinlock_t *p)
Definition: lock.h:119
static_always_inline void clib_spinlock_lock(clib_spinlock_t *p)
Definition: lock.h:80
static int http_server_create(vlib_main_t *vm)
Definition: http_server.c:841
static f64 vlib_process_wait_for_event_or_clock(vlib_main_t *vm, f64 dt)
Suspend a cooperative multi-tasking thread Waits for an event, or for the indicated number of seconds...
Definition: node_funcs.h:751
a
Definition: bitmap.h:538
static void clib_rwlock_writer_lock(clib_rwlock_t *p)
Definition: lock.h:190
static const char * html_header_template
Definition: http_server.c:252
svm_fifo_t * tx_fifo
svm_queue_t * vl_input_queue
Definition: http_server.c:66
static const char * http_response
Definition: http_server.c:236
u32 session_index
Index in thread pool where session was allocated.
unsigned long u64
Definition: types.h:89
static int session_rx_request(http_session_t *hs)
Definition: http_server.c:489
static svm_msg_q_t * session_main_get_vpp_event_queue(u32 thread_index)
Definition: session.h:633
u32 timer_handle
Timeout timer handle.
Definition: http_server.c:50
#define clib_memcpy_fast(a, b, c)
Definition: string.h:81
static void http_server_sessions_writer_unlock(void)
Definition: http_server.c:108
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
u32 index
Definition: node.h:279
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:333
svm_fifo_t * rx_fifo
Pointers to rx/tx buffers.
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:592
u32 vpp_session_index
vpp session index, handle
Definition: http_server.c:48
void session_send_rpc_evt_to_thread(u32 thread_index, void *fp, void *rpc_args)
Definition: session.c:110
static int svm_fifo_is_empty_cons(svm_fifo_t *f)
Check if fifo is empty optimized for consumer.
Definition: svm_fifo.h:471
uword unformat_user(unformat_input_t *input, unformat_function_t *func,...)
Definition: unformat.c:989
static void http_process_free(http_server_args *args)
Definition: http_server.c:205
vlib_main_t * vm
Definition: in2out_ed.c:1582
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:424
static int http_server_rx_callback_static(session_t *s)
Definition: http_server.c:543
struct _vnet_application_add_tls_cert_args_t vnet_app_add_tls_cert_args_t
static void http_server_session_lookup_add(u32 thread_index, u32 s_index, u32 hs_index)
Definition: http_server.c:145
int vlib_cli_input(vlib_main_t *vm, unformat_input_t *input, vlib_cli_output_function_t *function, uword function_arg)
Definition: cli.c:656
#define pool_get(P, E)
Allocate an object E from a pool P (unspecified alignment).
Definition: pool.h:252
clib_error_t * vnet_app_add_tls_cert(vnet_app_add_tls_cert_args_t *a)
Definition: application.c:1325
unsigned char u8
Definition: types.h:56
u8 data[128]
Definition: ipsec_types.api:89
struct _vnet_bind_args_t vnet_listen_args_t
static void alloc_http_process_callback(void *cb_args)
Definition: http_server.c:483
#define vec_reset_length(v)
Reset vector length to zero NULL-pointer tolerant.
double f64
Definition: types.h:142
static session_handle_t session_handle(session_t *s)
static void http_server_session_disconnect(http_session_t *hs)
Definition: http_server.c:196
static uword vlib_process_suspend(vlib_main_t *vm, f64 dt)
Suspend a vlib cooperative multi-tasking thread for a period of time.
Definition: node_funcs.h:482
static int http_server_session_accept_callback(session_t *s)
Definition: http_server.c:609
http_session_state_t
Definition: http_server.c:34
int(* builtin_app_rx_callback)(session_t *session)
Direct RX callback for built-in application.
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:173
static uword vlib_process_get_events(vlib_main_t *vm, uword **data_vector)
Return the first event type which has occurred and a vector of per-event data of that type...
Definition: node_funcs.h:579
struct _vnet_disconnect_args_t vnet_disconnect_args_t
static const u32 test_srv_key_rsa_len
Definition: tls_test.h:77
static void http_expired_timers_dispatch(u32 *expired_timers)
Definition: http_server.c:790
static u32 svm_fifo_max_dequeue_cons(svm_fifo_t *f)
Fifo max bytes to dequeue optimized for consumer.
Definition: svm_fifo.h:404
foreach_app_session_field u32 thread_index
rx thread index
Definition: http_server.c:46
static int http_server_rx_callback(session_t *s)
Definition: http_server.c:511
static int http_server_listen()
Definition: http_server.c:764
#define clib_error_return(e, args...)
Definition: error.h:99
static int http_server_session_connected_callback(u32 app_index, u32 api_context, session_t *s, session_error_t err)
Definition: http_server.c:660
svm_msg_q_t ** vpp_queue
Definition: http_server.c:59
unsigned int u32
Definition: types.h:88
int session_send_io_evt_to_thread(svm_fifo_t *f, session_evt_type_t evt_type)
Definition: session.c:79
tw_timer_wheel_2t_1w_2048sl_t tw
Definition: http_server.c:76
static void http_server_sessions_writer_lock(void)
Definition: http_server.c:102
struct _vnet_app_attach_args_t vnet_app_attach_args_t
static void http_server_session_free(http_session_t *hs)
Definition: http_server.c:136
unformat_function_t unformat_line_input
Definition: format.h:283
static void clib_spinlock_init(clib_spinlock_t *p)
Definition: lock.h:63
static session_cb_vft_t http_server_session_cb_vft
Definition: http_server.c:701
clib_error_t * vnet_app_add_tls_key(vnet_app_add_tls_key_args_t *a)
Definition: application.c:1335
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:534
static void clib_rwlock_init(clib_rwlock_t *p)
Definition: lock.h:150
static u8 * static_ok
Definition: http_server.c:267
static void clib_rwlock_reader_unlock(clib_rwlock_t *p)
Definition: lock.h:182
u64 vpp_session_handle
Definition: http_server.c:49
static const char * html_header_static
Definition: http_server.c:260
static void http_server_session_close_cb(void *hs_handlep)
Definition: http_server.c:777
static u8 * static_http
Definition: http_server.c:266
struct _unformat_input_t unformat_input_t
u32 vlib_register_node(vlib_main_t *vm, vlib_node_registration_t *r)
Definition: node.c:495
#define pool_put(P, E)
Free an object E in pool P.
Definition: pool.h:302
#define vec_dup(V)
Return copy of vector (no header, no alignment)
Definition: vec.h:429
int svm_fifo_enqueue(svm_fifo_t *f, u32 len, const u8 *src)
Enqueue data to fifo.
Definition: svm_fifo.c:838
static void * vlib_node_get_runtime_data(vlib_main_t *vm, u32 node_index)
Get node runtime private data by node index.
Definition: node_funcs.h:136
#define PREDICT_FALSE(x)
Definition: clib.h:120
clib_spinlock_t tw_lock
Definition: http_server.c:77
static void svm_fifo_unset_event(svm_fifo_t *f)
Unset fifo event flag.
Definition: svm_fifo.h:714
static const char * html_footer
Definition: http_server.c:257
Application session.
Definition: http_server.c:40
u32 node_index
Node index.
Definition: node.h:487
static const char test_srv_crt_rsa[]
Definition: tls_test.h:23
clib_error_t * vnet_session_enable_disable(vlib_main_t *vm, u8 is_en)
Definition: session.c:1768
int vnet_application_attach(vnet_app_attach_args_t *a)
Attach application to vpp.
Definition: application.c:827
void unformat_init_vector(unformat_input_t *input, u8 *vector_string)
Definition: unformat.c:1037
static u8 svm_fifo_set_event(svm_fifo_t *f)
Set fifo event flag.
Definition: svm_fifo.h:701
static void clib_rwlock_writer_unlock(clib_rwlock_t *p)
Definition: lock.h:204
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:169
vlib_main_t * vlib_main
Definition: http_server.c:84
#define UNFORMAT_END_OF_INPUT
Definition: format.h:145
u32 runtime_index
Definition: node.h:282
static_always_inline uword vlib_get_thread_index(void)
Definition: threads.h:219
static int http_server_attach()
Definition: http_server.c:712
sll srl srl sll sra u16x4 i
Definition: vector_sse42.h:317
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:380
vlib_node_registration_t http_server_process_node
(constructor) VLIB_REGISTER_NODE (http_server_process_node)
Definition: http_server.c:831
#define clib_warning(format, args...)
Definition: error.h:59
static vlib_node_runtime_t * vlib_node_get_runtime(vlib_main_t *vm, u32 node_index)
Get node runtime by node index.
Definition: node_funcs.h:115
#define pool_is_free_index(P, I)
Use free bitmap to query whether given index is free.
Definition: pool.h:299
string name[64]
Definition: ip.api:44
u32 ** session_to_http_session
Definition: http_server.c:57
static const char test_srv_key_rsa[]
Definition: tls_test.h:49
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:158
u8 * rx_buf
rx buffer
Definition: http_server.c:47
#define uword_to_pointer(u, type)
Definition: types.h:136
#define ASSERT(truth)
static http_session_t * http_server_session_get(u32 thread_index, u32 hs_index)
Definition: http_server.c:127
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:696
#define vec_delete(V, N, M)
Delete N elements starting at element M.
Definition: vec.h:854
static void clib_mem_free(void *p)
Definition: mem.h:215
struct _vnet_application_add_tls_key_args_t vnet_app_add_tls_key_args_t
static void http_server_session_lookup_del(u32 thread_index, u32 s_index)
Definition: http_server.c:153
static void vlib_node_set_state(vlib_main_t *vm, u32 node_index, vlib_node_state_t new_state)
Set node dispatch state.
Definition: node_funcs.h:174
static void http_server_session_timer_stop(http_session_t *hs)
Definition: http_server.c:186
static void * clib_mem_alloc(uword size)
Definition: mem.h:157
static uword pointer_to_uword(const void *p)
Definition: types.h:131
static void http_server_cleanup_callback(session_t *s, session_cleanup_ntf_t ntf)
Definition: http_server.c:675
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:23
u8 thread_index
Index of the thread that allocated the session.
uword * handler_by_get_request
Definition: http_server.c:61
static http_session_t * http_server_session_alloc(u32 thread_index)
Definition: http_server.c:114
struct _vlib_node_registration vlib_node_registration_t
static clib_error_t * http_server_main_init(vlib_main_t *vm)
Definition: http_server.c:962
http_server_main_t http_server_main
Definition: http_server.c:87
static void alloc_http_process(http_server_args *args)
Definition: http_server.c:439
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
void vlib_rpc_call_main_thread(void *callback, u8 *args, u32 arg_size)
Definition: threads.c:1938
volatile u8 session_state
State in session layer state machine.
static void close_session(http_session_t *hs)
u64 uword
Definition: types.h:112
int vnet_bind_uri(vnet_listen_args_t *a)
static void unformat_free(unformat_input_t *i)
Definition: format.h:163
static int http_server_add_segment_callback(u32 client_index, u64 segment_handle)
Definition: http_server.c:668
int vnet_disconnect_session(vnet_disconnect_args_t *a)
Definition: application.c:1087
vhost_user_req_t request
Definition: vhost_user.h:104
static clib_error_t * http_server_create_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: http_server.c:879
session_cleanup_ntf_t
struct _svm_queue svm_queue_t
static const char * http_error_template
Definition: http_server.c:243
unformat_function_t unformat_memory_size
Definition: format.h:296
struct clib_bihash_value offset
template key/value backing page structure
static struct option options[]
Definition: main.c:52
u8 * format_unformat_error(u8 *s, va_list *va)
Definition: unformat.c:91
static vlib_thread_main_t * vlib_get_thread_main()
Definition: global_funcs.h:32
static vlib_node_t * vlib_get_node(vlib_main_t *vm, u32 i)
Get vlib node by index.
Definition: node_funcs.h:85
enum session_error_ session_error_t
static const char * http_ok
Definition: http_server.c:233
int(* session_accept_callback)(session_t *new_session)
Notify server of newly accepted session.
http_session_t ** sessions
Definition: http_server.c:55
http_process_event_t
Definition: http_server.c:22
void vlib_start_process(vlib_main_t *vm, uword process_index)
Definition: main.c:1586
static http_session_t * http_server_session_lookup(u32 thread_index, u32 s_index)
Definition: http_server.c:160
static void http_server_session_reset_callback(session_t *s)
Definition: http_server.c:649
static void http_cli_output(uword arg, u8 *buffer, uword buffer_bytes)
Definition: http_server.c:270
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:978
static const u32 test_srv_crt_rsa_len
Definition: tls_test.h:47
void send_data(http_session_t *hs, u8 *data)
Definition: http_server.c:286
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:171
u32 * free_http_cli_process_node_indices
Definition: http_server.c:63
static void http_server_session_disconnect_callback(session_t *s)
Definition: http_server.c:638
static uword http_cli_process(vlib_main_t *vm, vlib_node_runtime_t *rt, vlib_frame_t *f)
Definition: http_server.c:349
static void http_server_sessions_reader_lock(void)
Definition: http_server.c:90
static void send_error(http_session_t *hs, char *str)
Definition: http_server.c:339
clib_rwlock_t sessions_lock
Definition: http_server.c:56