FD.io VPP  v19.08-27-gf4dcae4
Vector Packet Processing
static_server.c File Reference

Simple Static http server, sufficient to serve .html / .css / .js content. More...

+ Include dependency graph for static_server.c:

Go to the source code of this file.

Data Structures

struct  http_session_t
 Application session. More...
 
struct  file_data_cache_t
 In-memory file data cache entry. More...
 
struct  http_static_server_main_t
 Main data structure. More...
 

Enumerations

enum  http_session_state_t {
  HTTP_STATE_CLOSED, HTTP_STATE_ESTABLISHED, HTTP_STATE_OK_SENT, HTTP_STATE_CLOSED,
  HTTP_STATE_ESTABLISHED, HTTP_STATE_OK_SENT, HTTP_STATE_SEND_MORE_DATA, HTTP_STATE_N_STATES
}
 Session States. More...
 
enum  state_machine_called_from_t { CALLED_FROM_RX, CALLED_FROM_TX, CALLED_FROM_TIMER }
 

Functions

static u8format_state_machine_called_from (u8 *s, va_list *args)
 Format the called-from enum. More...
 
static void http_static_server_sessions_reader_lock (void)
 Acquire reader lock on the sessions pools. More...
 
static void http_static_server_sessions_reader_unlock (void)
 Drop reader lock on the sessions pools. More...
 
static void http_static_server_sessions_writer_lock (void)
 Acquire writer lock on the sessions pools. More...
 
static void http_static_server_sessions_writer_unlock (void)
 Drop writer lock on the sessions pools. More...
 
static void http_static_server_session_timer_start (http_session_t *hs)
 Start a session cleanup timer. More...
 
static void http_static_server_session_timer_stop (http_session_t *hs)
 stop a session cleanup timer More...
 
static http_session_thttp_static_server_session_alloc (u32 thread_index)
 Allocate an http session. More...
 
static http_session_thttp_static_server_session_get (u32 thread_index, u32 hs_index)
 Get an http session by index. More...
 
static void http_static_server_session_free (http_session_t *hs)
 Free an http session. More...
 
static void http_static_server_session_lookup_add (u32 thread_index, u32 s_index, u32 hs_index)
 add a session to the vpp < – > http session index map More...
 
static void http_static_server_session_lookup_del (u32 thread_index, u32 s_index)
 Remove a session from the vpp < – > http session index map. More...
 
static http_session_thttp_static_server_session_lookup (u32 thread_index, u32 s_index)
 lookup a session in the vpp < – > http session index map More...
 
static void http_static_server_detach_cache_entry (http_session_t *hs)
 Detach cache entry from session. More...
 
static void http_static_server_session_cleanup (http_session_t *hs)
 clean up a session More...
 
static void http_static_server_session_disconnect (http_session_t *hs)
 Disconnect a session. More...
 
static u32 static_send_data (http_session_t *hs, u8 *data, u32 length, u32 offset)
 send http data More...
 
static void send_error (http_session_t *hs, char *str)
 Send an http error string. More...
 
static int session_rx_request (http_session_t *hs)
 Retrieve data from the application layer. More...
 
static void lru_validate (http_static_server_main_t *hsm)
 Sanity-check the forward and reverse LRU lists. More...
 
static void lru_remove (http_static_server_main_t *hsm, file_data_cache_t *ep)
 Remove a data cache entry from the LRU lists. More...
 
static void lru_add (http_static_server_main_t *hsm, file_data_cache_t *ep, f64 now)
 Add an entry to the LRU lists, tag w/ supplied timestamp. More...
 
static void lru_update (http_static_server_main_t *hsm, file_data_cache_t *ep, f64 now)
 Remove and re-add a cache entry from/to the LRU lists. More...
 
static int state_closed (session_t *s, http_session_t *hs, state_machine_called_from_t cf)
 Session-layer (main) data rx callback. More...
 
static void close_session (http_session_t *hs)
 
static int state_established (session_t *s, http_session_t *hs, state_machine_called_from_t cf)
 established state - waiting for GET, POST, etc. More...
 
static int state_send_more_data (session_t *s, http_session_t *hs, state_machine_called_from_t cf)
 
static int state_sent_ok (session_t *s, http_session_t *hs, state_machine_called_from_t cf)
 
static int http_static_server_rx_tx_callback (session_t *s, state_machine_called_from_t cf)
 
static int http_static_server_rx_callback (session_t *s)
 
static int http_static_server_tx_callback (session_t *s)
 
static int http_static_server_session_accept_callback (session_t *s)
 Session accept callback. More...
 
static void http_static_server_session_disconnect_callback (session_t *s)
 Session disconnect callback. More...
 
static void http_static_server_session_reset_callback (session_t *s)
 Session reset callback. More...
 
static int http_static_server_session_connected_callback (u32 app_index, u32 api_context, session_t *s, u8 is_fail)
 
static int http_static_server_add_segment_callback (u32 client_index, u64 segment_handle)
 
static int http_static_server_attach ()
 
static int http_static_server_listen ()
 
static void http_static_server_session_cleanup_cb (void *hs_handlep)
 
static void http_expired_timers_dispatch (u32 *expired_timers)
 Expired session timer-wheel callback. More...
 
static uword http_static_server_process (vlib_main_t *vm, vlib_node_runtime_t *rt, vlib_frame_t *f)
 Timer-wheel expiration process. More...
 
static int http_static_server_create (vlib_main_t *vm)
 
int http_static_server_enable_api (u32 fifo_size, u32 cache_limit, u32 prealloc_fifos, u32 private_segment_size, u8 *www_root, u8 *uri)
 API helper function for vl_api_http_static_enable_t messages. More...
 
static clib_error_thttp_static_server_create_command_fn (vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
 
u8format_hsm_cache_entry (u8 *s, va_list *args)
 format a file cache entry More...
 
u8format_http_session_state (u8 *s, va_list *args)
 
u8format_http_session (u8 *s, va_list *args)
 
static clib_error_thttp_show_static_server_command_fn (vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
 
static clib_error_thttp_static_server_main_init (vlib_main_t *vm)
 

Variables

http_static_server_main_t http_static_server_main
 
static const char * http_error_template
 http error boilerplate More...
 
static const char * http_response_template
 http response boilerplate More...
 
static void * state_funcs [HTTP_STATE_N_STATES]
 
static session_cb_vft_t http_static_server_session_cb_vft
 Session-layer virtual function table. More...
 
vlib_node_registration_t http_static_server_process_node
 (constructor) VLIB_REGISTER_NODE (http_static_server_process_node) More...
 
static vlib_cli_command_t http_static_server_create_command
 (constructor) VLIB_CLI_COMMAND (http_static_server_create_command) More...
 
static vlib_cli_command_t http_show_static_server_command
 (constructor) VLIB_CLI_COMMAND (http_show_static_server_command) More...
 

Detailed Description

Simple Static http server, sufficient to serve .html / .css / .js content.

Definition in file static_server.c.

Enumeration Type Documentation

Session States.

Enumerator
HTTP_STATE_CLOSED 
HTTP_STATE_ESTABLISHED 
HTTP_STATE_OK_SENT 
HTTP_STATE_CLOSED 

Session is closed.

HTTP_STATE_ESTABLISHED 

Session is established.

HTTP_STATE_OK_SENT 

Session has sent an OK response.

HTTP_STATE_SEND_MORE_DATA 

Session has sent an HTML response.

HTTP_STATE_N_STATES 

Number of states.

Definition at line 39 of file static_server.c.

Enumerator
CALLED_FROM_RX 
CALLED_FROM_TX 
CALLED_FROM_TIMER 

Definition at line 53 of file static_server.c.

Function Documentation

static void close_session ( http_session_t hs)
static

Definition at line 670 of file static_server.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

u8* format_hsm_cache_entry ( u8 s,
va_list *  args 
)

format a file cache entry

Definition at line 1524 of file static_server.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

u8* format_http_session ( u8 s,
va_list *  args 
)

Definition at line 1568 of file static_server.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

u8* format_http_session_state ( u8 s,
va_list *  args 
)

Definition at line 1541 of file static_server.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static u8* format_state_machine_called_from ( u8 s,
va_list *  args 
)
static

Format the called-from enum.

Definition at line 181 of file static_server.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static void http_expired_timers_dispatch ( u32 expired_timers)
static

Expired session timer-wheel callback.

Definition at line 1275 of file static_server.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static clib_error_t* http_show_static_server_command_fn ( vlib_main_t vm,
unformat_input_t input,
vlib_cli_command_t cmd 
)
static

Definition at line 1585 of file static_server.c.

+ Here is the call graph for this function:

static int http_static_server_add_segment_callback ( u32  client_index,
u64  segment_handle 
)
static

Definition at line 1167 of file static_server.c.

static int http_static_server_attach ( )
static

Definition at line 1187 of file static_server.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static int http_static_server_create ( vlib_main_t vm)
static

Definition at line 1328 of file static_server.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static clib_error_t* http_static_server_create_command_fn ( vlib_main_t vm,
unformat_input_t input,
vlib_cli_command_t cmd 
)
static

Definition at line 1408 of file static_server.c.

+ Here is the call graph for this function:

static void http_static_server_detach_cache_entry ( http_session_t hs)
static

Detach cache entry from session.

Definition at line 361 of file static_server.c.

+ Here is the caller graph for this function:

int http_static_server_enable_api ( u32  fifo_size,
u32  cache_limit,
u32  prealloc_fifos,
u32  private_segment_size,
u8 www_root,
u8 uri 
)

API helper function for vl_api_http_static_enable_t messages.

Definition at line 1371 of file static_server.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static int http_static_server_listen ( )
static

Definition at line 1240 of file static_server.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static clib_error_t* http_static_server_main_init ( vlib_main_t vm)
static

Definition at line 1698 of file static_server.c.

+ Here is the call graph for this function:

static uword http_static_server_process ( vlib_main_t vm,
vlib_node_runtime_t rt,
vlib_frame_t f 
)
static

Timer-wheel expiration process.

Definition at line 1293 of file static_server.c.

+ Here is the call graph for this function:

static int http_static_server_rx_callback ( session_t s)
static

Definition at line 1072 of file static_server.c.

+ Here is the call graph for this function:

static int http_static_server_rx_tx_callback ( session_t s,
state_machine_called_from_t  cf 
)
inlinestatic

Definition at line 1033 of file static_server.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static int http_static_server_session_accept_callback ( session_t s)
static

Session accept callback.

Definition at line 1088 of file static_server.c.

+ Here is the call graph for this function:

static http_session_t* http_static_server_session_alloc ( u32  thread_index)
static

Allocate an http session.

Definition at line 274 of file static_server.c.

+ Here is the caller graph for this function:

static void http_static_server_session_cleanup ( http_session_t hs)
static

clean up a session

Definition at line 389 of file static_server.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static void http_static_server_session_cleanup_cb ( void *  hs_handlep)
static

Definition at line 1253 of file static_server.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static int http_static_server_session_connected_callback ( u32  app_index,
u32  api_context,
session_t s,
u8  is_fail 
)
static

Definition at line 1159 of file static_server.c.

static void http_static_server_session_disconnect ( http_session_t hs)
static

Disconnect a session.

Definition at line 406 of file static_server.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static void http_static_server_session_disconnect_callback ( session_t s)
static

Session disconnect callback.

Definition at line 1118 of file static_server.c.

+ Here is the call graph for this function:

static void http_static_server_session_free ( http_session_t hs)
static

Free an http session.

Definition at line 301 of file static_server.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static http_session_t* http_static_server_session_get ( u32  thread_index,
u32  hs_index 
)
static

Get an http session by index.

Definition at line 290 of file static_server.c.

+ Here is the caller graph for this function:

static http_session_t* http_static_server_session_lookup ( u32  thread_index,
u32  s_index 
)
static

lookup a session in the vpp < – > http session index map

Definition at line 344 of file static_server.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static void http_static_server_session_lookup_add ( u32  thread_index,
u32  s_index,
u32  hs_index 
)
static

add a session to the vpp < – > http session index map

Definition at line 323 of file static_server.c.

+ Here is the caller graph for this function:

static void http_static_server_session_lookup_del ( u32  thread_index,
u32  s_index 
)
static

Remove a session from the vpp < – > http session index map.

Definition at line 334 of file static_server.c.

+ Here is the caller graph for this function:

static void http_static_server_session_reset_callback ( session_t s)
static

Session reset callback.

Definition at line 1140 of file static_server.c.

+ Here is the call graph for this function:

static void http_static_server_session_timer_start ( http_session_t hs)
static

Start a session cleanup timer.

Definition at line 243 of file static_server.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static void http_static_server_session_timer_stop ( http_session_t hs)
static

stop a session cleanup timer

Definition at line 262 of file static_server.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static void http_static_server_sessions_reader_lock ( void  )
static

Acquire reader lock on the sessions pools.

Definition at line 211 of file static_server.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static void http_static_server_sessions_reader_unlock ( void  )
static

Drop reader lock on the sessions pools.

Definition at line 219 of file static_server.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static void http_static_server_sessions_writer_lock ( void  )
static

Acquire writer lock on the sessions pools.

Definition at line 227 of file static_server.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static void http_static_server_sessions_writer_unlock ( void  )
static

Drop writer lock on the sessions pools.

Definition at line 235 of file static_server.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static int http_static_server_tx_callback ( session_t s)
static

Definition at line 1078 of file static_server.c.

+ Here is the call graph for this function:

static void lru_add ( http_static_server_main_t hsm,
file_data_cache_t ep,
f64  now 
)
inlinestatic

Add an entry to the LRU lists, tag w/ supplied timestamp.

Definition at line 606 of file static_server.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static void lru_remove ( http_static_server_main_t hsm,
file_data_cache_t ep 
)
inlinestatic

Remove a data cache entry from the LRU lists.

Definition at line 572 of file static_server.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static void lru_update ( http_static_server_main_t hsm,
file_data_cache_t ep,
f64  now 
)
inlinestatic

Remove and re-add a cache entry from/to the LRU lists.

Definition at line 643 of file static_server.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static void lru_validate ( http_static_server_main_t hsm)
inlinestatic

Sanity-check the forward and reverse LRU lists.

Definition at line 527 of file static_server.c.

+ Here is the caller graph for this function:

static void send_error ( http_session_t hs,
char *  str 
)
static

Send an http error string.

Parameters
hs- the http session
str- the error string, e.g. "404 Not Found"

Definition at line 488 of file static_server.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static int session_rx_request ( http_session_t hs)
static

Retrieve data from the application layer.

Definition at line 503 of file static_server.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static int state_closed ( session_t s,
http_session_t hs,
state_machine_called_from_t  cf 
)
static

Session-layer (main) data rx callback.

Parse the http request, and reply to it. Future extensions might include POST processing, active content, etc.closed state - should never really get here

Definition at line 661 of file static_server.c.

+ Here is the call graph for this function:

static int state_established ( session_t s,
http_session_t hs,
state_machine_called_from_t  cf 
)
static

established state - waiting for GET, POST, etc.

Definition at line 679 of file static_server.c.

+ Here is the call graph for this function:

static int state_send_more_data ( session_t s,
http_session_t hs,
state_machine_called_from_t  cf 
)
static

Definition at line 940 of file static_server.c.

+ Here is the call graph for this function:

static int state_sent_ok ( session_t s,
http_session_t hs,
state_machine_called_from_t  cf 
)
static

Definition at line 966 of file static_server.c.

+ Here is the call graph for this function:

static u32 static_send_data ( http_session_t hs,
u8 data,
u32  length,
u32  offset 
)
static

send http data

Parameters
hs- http session
data- the data vector to transmit
offset- transmit offset for this operation
Returns
offset for next transmit operation, may be unchanged w/ full fifo

Definition at line 444 of file static_server.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Variable Documentation

const char* http_error_template
static
Initial value:
=
"HTTP/1.1 %s\r\n"
"Date: %U GMT\r\n"
"Content-Type: text/html\r\n"
"Connection: close\r\n"
"Pragma: no-cache\r\n"
"Content-Length: 0\r\n\r\n"

http error boilerplate

Definition at line 417 of file static_server.c.

const char* http_response_template
static
Initial value:
=
"Date: %U GMT\r\n"
"Expires: %U GMT\r\n"
"Server: VPP Static\r\n"
"Content-Type: text/%s\r\n"
"Content-Length: %d\r\n\r\n"

http response boilerplate

Definition at line 427 of file static_server.c.

vlib_cli_command_t http_show_static_server_command
static
Initial value:
=
{
.path = "show http static server",
.short_help = "show http static server sessions cache [verbose [<nn>]]",
}
static clib_error_t * http_show_static_server_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)

(constructor) VLIB_CLI_COMMAND (http_show_static_server_command)

Definition at line 1689 of file static_server.c.

vlib_cli_command_t http_static_server_create_command
static
Initial value:
=
{
.path = "http static server",
.short_help = "http static server www-root <path> [prealloc-fifos <nn>]\n"
"[private-segment-size <nnMG>] [fifo-size <nbytes>] [uri <uri>]\n"
"[debug [nn]]\n",
}
static clib_error_t * http_static_server_create_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)

(constructor) VLIB_CLI_COMMAND (http_static_server_create_command)

Definition at line 1511 of file static_server.c.

http_static_server_main_t http_static_server_main

Definition at line 175 of file static_server.c.

vlib_node_registration_t http_static_server_process_node
Initial value:
=
{
.name = "static-http-server-process",
.state = VLIB_NODE_STATE_DISABLED,
}
static uword http_static_server_process(vlib_main_t *vm, vlib_node_runtime_t *rt, vlib_frame_t *f)
Timer-wheel expiration process.

(constructor) VLIB_REGISTER_NODE (http_static_server_process_node)

Definition at line 1318 of file static_server.c.

session_cb_vft_t http_static_server_session_cb_vft
static
Initial value:
= {
.session_accept_callback = http_static_server_session_accept_callback,
.session_disconnect_callback =
.session_connected_callback = http_static_server_session_connected_callback,
.add_segment_callback = http_static_server_add_segment_callback,
.builtin_app_rx_callback = http_static_server_rx_callback,
.builtin_app_tx_callback = http_static_server_tx_callback,
}
static int http_static_server_session_connected_callback(u32 app_index, u32 api_context, session_t *s, u8 is_fail)
static int http_static_server_tx_callback(session_t *s)
static int http_static_server_session_accept_callback(session_t *s)
Session accept callback.
static int http_static_server_rx_callback(session_t *s)
static void http_static_server_session_reset_callback(session_t *s)
Session reset callback.
static void http_static_server_session_disconnect_callback(session_t *s)
Session disconnect callback.
static int http_static_server_add_segment_callback(u32 client_index, u64 segment_handle)

Session-layer virtual function table.

Definition at line 1175 of file static_server.c.

void* state_funcs[HTTP_STATE_N_STATES]
static
Initial value:
= {
}
static int state_sent_ok(session_t *s, http_session_t *hs, state_machine_called_from_t cf)
static int state_send_more_data(session_t *s, http_session_t *hs, state_machine_called_from_t cf)
static int state_closed(session_t *s, http_session_t *hs, state_machine_called_from_t cf)
Session-layer (main) data rx callback.
static int state_established(session_t *s, http_session_t *hs, state_machine_called_from_t cf)
established state - waiting for GET, POST, etc.

Definition at line 1022 of file static_server.c.