session: fix http server rpc to main
[vpp.git] / src / vnet / session-apps / http_server.c
index 719608a..ccbe711 100644 (file)
@@ -1,5 +1,5 @@
 /*
-* Copyright (c) 2015-2017 Cisco and/or its affiliates.
+* Copyright (c) 2015-2019 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
@@ -16,6 +16,7 @@
 #include <vnet/vnet.h>
 #include <vnet/session/application.h>
 #include <vnet/session/application_interface.h>
+#include <vnet/session/session.h>
 
 typedef enum
 {
@@ -276,7 +277,10 @@ send_data (http_session_t * hs, u8 * data)
       /* Made any progress? */
       if (actual_transfer <= 0)
        {
+         http_server_sessions_reader_unlock ();
          vlib_process_suspend (vm, delay);
+         http_server_sessions_reader_lock ();
+
          /* 10s deadman timer */
          if (vlib_time_now (vm) > last_sent_timer + 10.0)
            {
@@ -441,7 +445,8 @@ alloc_http_process (http_server_args * args)
 
   /* Save the args (pointer) in the node runtime */
   save_args = vlib_node_get_runtime_data (vm, n->index);
-  *save_args = args;
+  *save_args = clib_mem_alloc (sizeof (*args));
+  clib_memcpy_fast (*save_args, args, sizeof (*args));
 
   vlib_start_process (vm, n->runtime_index);
 }
@@ -475,9 +480,9 @@ session_rx_request (http_session_t * hs)
 }
 
 static int
-http_server_rx_callback (stream_session_t * s)
+http_server_rx_callback (session_t * s)
 {
-  http_server_args *args;
+  http_server_args args;
   http_session_t *hs;
   int rv;
 
@@ -492,22 +497,22 @@ http_server_rx_callback (stream_session_t * s)
     return rv;
 
   /* send the command to a new/recycled vlib process */
-  args = clib_mem_alloc (sizeof (*args));
-  args->hs_index = hs->session_index;
-  args->thread_index = hs->thread_index;
+  args.hs_index = hs->session_index;
+  args.thread_index = hs->thread_index;
 
   http_server_sessions_reader_unlock ();
 
-  /* Send an RPC request via the thread-0 input node */
+  /* Send RPC request to main thread */
   if (vlib_get_thread_index () != 0)
-    session_send_rpc_evt_to_thread (0, alloc_http_process_callback, args);
+    vlib_rpc_call_main_thread (alloc_http_process_callback, (u8 *) & args,
+                              sizeof (args));
   else
-    alloc_http_process (args);
+    alloc_http_process (&args);
   return 0;
 }
 
 static int
-http_server_rx_callback_static (stream_session_t * s)
+http_server_rx_callback_static (session_t * s)
 {
   http_server_main_t *hsm = &http_server_main;
   vnet_disconnect_args_t _a = { 0 }, *a = &_a;
@@ -569,8 +574,8 @@ close_session:
   return 0;
 
 postpone:
-  svm_fifo_set_event (hs->rx_fifo);
-  session_send_io_evt_to_thread (hs->rx_fifo, FIFO_EVENT_BUILTIN_RX);
+  (void) svm_fifo_set_event (hs->rx_fifo);
+  session_send_io_evt_to_thread (hs->rx_fifo, SESSION_IO_EVT_BUILTIN_RX);
   return 0;
 
 wait_for_data:
@@ -578,13 +583,13 @@ wait_for_data:
 }
 
 static int
-http_server_session_accept_callback (stream_session_t * s)
+http_server_session_accept_callback (session_t * s)
 {
   http_server_main_t *hsm = &http_server_main;
   http_session_t *hs;
 
   hsm->vpp_queue[s->thread_index] =
-    session_manager_get_vpp_event_queue (s->thread_index);
+    session_main_get_vpp_event_queue (s->thread_index);
 
   if (!hsm->is_static)
     http_server_sessions_writer_lock ();
@@ -592,8 +597,8 @@ http_server_session_accept_callback (stream_session_t * s)
   hs = http_server_session_alloc (s->thread_index);
   http_server_session_lookup_add (s->thread_index, s->session_index,
                                  hs->session_index);
-  hs->rx_fifo = s->server_rx_fifo;
-  hs->tx_fifo = s->server_tx_fifo;
+  hs->rx_fifo = s->rx_fifo;
+  hs->tx_fifo = s->tx_fifo;
   hs->vpp_session_index = s->session_index;
   hs->vpp_session_handle = session_handle (s);
   hs->session_state = HTTP_STATE_ESTABLISHED;
@@ -606,7 +611,7 @@ http_server_session_accept_callback (stream_session_t * s)
 }
 
 static void
-http_server_session_disconnect_callback (stream_session_t * s)
+http_server_session_disconnect_callback (session_t * s)
 {
   http_server_main_t *hsm = &http_server_main;
   vnet_disconnect_args_t _a = { 0 }, *a = &_a;
@@ -627,7 +632,7 @@ http_server_session_disconnect_callback (stream_session_t * s)
 }
 
 static void
-http_server_session_reset_callback (stream_session_t * s)
+http_server_session_reset_callback (session_t * s)
 {
   http_server_main_t *hsm = &http_server_main;
   vnet_disconnect_args_t _a = { 0 }, *a = &_a;
@@ -649,7 +654,7 @@ http_server_session_reset_callback (stream_session_t * s)
 
 static int
 http_server_session_connected_callback (u32 app_index, u32 api_context,
-                                       stream_session_t * s, u8 is_fail)
+                                       session_t * s, u8 is_fail)
 {
   clib_warning ("called...");
   return -1;
@@ -727,7 +732,7 @@ static int
 http_server_listen ()
 {
   http_server_main_t *hsm = &http_server_main;
-  vnet_bind_args_t _a, *a = &_a;
+  vnet_listen_args_t _a, *a = &_a;
   clib_memset (a, 0, sizeof (*a));
   a->app_index = hsm->app_index;
   a->uri = "tcp://0.0.0.0/80";
@@ -769,6 +774,7 @@ http_server_create_command_fn (vlib_main_t * vm,
                               vlib_cli_command_t * cmd)
 {
   http_server_main_t *hsm = &http_server_main;
+  unformat_input_t _line_input, *line_input = &_line_input;
   u64 seg_size;
   u8 *html;
   int rv;
@@ -777,13 +783,19 @@ http_server_create_command_fn (vlib_main_t * vm,
   hsm->private_segment_size = 0;
   hsm->fifo_size = 0;
   hsm->is_static = 0;
-  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
+
+  /* Get a line of input. */
+  if (!unformat_user (input, unformat_line_input, line_input))
+    goto start_server;
+
+  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
     {
-      if (unformat (input, "static"))
+      if (unformat (line_input, "static"))
        hsm->is_static = 1;
-      else if (unformat (input, "prealloc-fifos %d", &hsm->prealloc_fifos))
+      else
+       if (unformat (line_input, "prealloc-fifos %d", &hsm->prealloc_fifos))
        ;
-      else if (unformat (input, "private-segment-size %U",
+      else if (unformat (line_input, "private-segment-size %U",
                         unformat_memory_size, &seg_size))
        {
          if (seg_size >= 0x100000000ULL)
@@ -794,14 +806,18 @@ http_server_create_command_fn (vlib_main_t * vm,
            }
          hsm->private_segment_size = seg_size;
        }
-      else if (unformat (input, "fifo-size %d", &hsm->fifo_size))
+      else if (unformat (line_input, "fifo-size %d", &hsm->fifo_size))
        hsm->fifo_size <<= 10;
-      else if (unformat (input, "uri %s", &hsm->uri))
+      else if (unformat (line_input, "uri %s", &hsm->uri))
        ;
       else
        return clib_error_return (0, "unknown input `%U'",
-                                 format_unformat_error, input);
+                                 format_unformat_error, line_input);
     }
+  unformat_free (line_input);
+
+start_server:
+
   if (hsm->my_client_index != (u32) ~ 0)
     return clib_error_return (0, "test http server is already running");