http_static: add dynamic GET / POST method hooks

Add .json output to format_vnet_sw_interface_cntrs(...)

Type: feature

Signed-off-by: Dave Barach <dave@barachs.net>
Change-Id: Ied036ebfaaafbf1dfc2a4e396c00f09f40659400
This commit is contained in:
Dave Barach
2019-09-11 21:35:48 -04:00
parent da9513af29
commit 5554c56a65
3 changed files with 342 additions and 178 deletions

View File

@ -19,12 +19,20 @@
#define __included_http_static_h__
#include <vnet/vnet.h>
#include <vnet/session/application.h>
#include <vnet/session/application_interface.h>
#include <vnet/session/session.h>
#include <vnet/ip/ip.h>
#include <vnet/ethernet/ethernet.h>
#include <vppinfra/hash.h>
#include <vppinfra/error.h>
#include <vppinfra/time_range.h>
#include <vppinfra/tw_timer_2t_1w_2048sl.h>
#include <vppinfra/bihash_vec8_8.h>
/** @file Static http server definitions
*/
typedef struct
{
@ -38,11 +46,168 @@ typedef struct
extern http_static_main_t http_static_main;
/** \brief Session States
*/
typedef enum
{
/** Session is closed */
HTTP_STATE_CLOSED,
/** Session is established */
HTTP_STATE_ESTABLISHED,
/** Session has sent an OK response */
HTTP_STATE_OK_SENT,
/** Session has sent an HTML response */
HTTP_STATE_SEND_MORE_DATA,
/** Number of states */
HTTP_STATE_N_STATES,
} http_session_state_t;
typedef enum
{
CALLED_FROM_RX,
CALLED_FROM_TX,
CALLED_FROM_TIMER,
} http_state_machine_called_from_t;
typedef enum
{
HTTP_BUILTIN_METHOD_GET = 0,
HTTP_BUILTIN_METHOD_POST,
} http_builtin_method_type_t;
/** \brief Application session
*/
typedef struct
{
CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
/** Base class instance variables */
#define _(type, name) type name;
foreach_app_session_field
#undef _
/** rx thread index */
u32 thread_index;
/** rx buffer */
u8 *rx_buf;
/** vpp session index, handle */
u32 vpp_session_index;
u64 vpp_session_handle;
/** Timeout timer handle */
u32 timer_handle;
/** Fully-resolved file path */
u8 *path;
/** File data, a vector */
u8 *data;
/** Current data send offset */
u32 data_offset;
/** Need to free data in detach_cache_entry */
int free_data;
/** File cache pool index */
u32 cache_pool_index;
/** state machine called from... */
http_state_machine_called_from_t called_from;
} http_session_t;
/** \brief In-memory file data cache entry
*/
typedef struct
{
/** Name of the file */
u8 *filename;
/** Contents of the file, as a u8 * vector */
u8 *data;
/** Last time the cache entry was used */
f64 last_used;
/** Cache LRU links */
u32 next_index;
u32 prev_index;
/** Reference count, so we don't recycle while referenced */
int inuse;
} file_data_cache_t;
/** \brief Main data structure
*/
typedef struct
{
/** Per thread vector of session pools */
http_session_t **sessions;
/** Session pool reader writer lock */
clib_rwlock_t sessions_lock;
/** vpp session to http session index map */
u32 **session_to_http_session;
/** Enable debug messages */
int debug_level;
/** vpp message/event queue */
svm_msg_q_t **vpp_queue;
/** Unified file data cache pool */
file_data_cache_t *cache_pool;
/** Hash table which maps file name to file data */
BVT (clib_bihash) name_to_data;
/** Hash tables for built-in GET and POST handlers */
uword *get_url_handlers;
uword *post_url_handlers;
/** Current cache size */
u64 cache_size;
/** Max cache size in bytes */
u64 cache_limit;
/** Number of cache evictions */
u64 cache_evictions;
/** Cache LRU listheads */
u32 first_index;
u32 last_index;
/** root path to be served */
u8 *www_root;
/** Server's event queue */
svm_queue_t *vl_input_queue;
/** API client handle */
u32 my_client_index;
/** Application index */
u32 app_index;
/** Process node index for event scheduling */
u32 node_index;
/** Session cleanup timer wheel */
tw_timer_wheel_2t_1w_2048sl_t tw;
clib_spinlock_t tw_lock;
/** Time base, so we can generate browser cache control http spew */
clib_timebase_t timebase;
/** Number of preallocated fifos, usually 0 */
u32 prealloc_fifos;
/** Private segment size, usually 0 */
u32 private_segment_size;
/** Size of the allocated rx, tx fifos, roughly 8K or so */
u32 fifo_size;
/** The bind URI, defaults to tcp://0.0.0.0/80 */
u8 *uri;
vlib_main_t *vlib_main;
} http_static_server_main_t;
extern http_static_server_main_t http_static_server_main;
int http_static_server_enable_api (u32 fifo_size, u32 cache_limit,
u32 prealloc_fifos,
u32 private_segment_size,
u8 * www_root, u8 * uri);
void http_static_server_register_builtin_handler
(void *fp, char *url, int type);
#endif /* __included_http_static_h__ */
/*

File diff suppressed because it is too large Load Diff

View File

@ -214,12 +214,21 @@ format_vnet_hw_if_index_name (u8 * s, va_list * args)
u8 *
format_vnet_sw_interface_cntrs (u8 * s, vnet_interface_main_t * im,
vnet_sw_interface_t * si)
vnet_sw_interface_t * si, int json)
{
u32 indent, n_printed;
int j, n_counters;
char *x = "";
int json_need_comma_nl = 0;
u8 *n = 0;
/*
* to output a json snippet, stick quotes in lots of places
* definitely deserves a one-character variable name.
*/
if (json)
x = "\"";
indent = format_get_indent (s);
n_printed = 0;
@ -242,6 +251,21 @@ format_vnet_sw_interface_cntrs (u8 * s, vnet_interface_main_t * im,
if (vtotal.packets == 0)
continue;
if (json)
{
if (json_need_comma_nl)
{
vec_add1 (s, ',');
vec_add1 (s, '\n');
}
s = format (s, "%s%s_packets%s: %s%Ld%s,\n", x, cm->name, x, x,
vtotal.packets, x);
s = format (s, "%s%s_bytes%s: %s%Ld%s", x, cm->name, x, x,
vtotal.bytes, x);
json_need_comma_nl = 1;
continue;
}
if (n_printed > 0)
s = format (s, "\n%U", format_white_space, indent);
n_printed += 2;
@ -277,6 +301,19 @@ format_vnet_sw_interface_cntrs (u8 * s, vnet_interface_main_t * im,
if (vtotal == 0)
continue;
if (json)
{
if (json_need_comma_nl)
{
vec_add1 (s, ',');
vec_add1 (s, '\n');
}
s = format (s, "%s%s%s: %s%Ld%s", x, cm->name, x, x, vtotal, x);
json_need_comma_nl = 1;
continue;
}
if (n_printed > 0)
s = format (s, "\n%U", format_white_space, indent);
n_printed += 1;
@ -315,7 +352,7 @@ format_vnet_sw_interface (u8 * s, va_list * args)
format_vnet_sw_interface_flags, si->flags,
format_vnet_sw_interface_mtu, si);
s = format_vnet_sw_interface_cntrs (s, im, si);
s = format_vnet_sw_interface_cntrs (s, im, si, 0 /* want json */ );
return s;
}
@ -338,7 +375,7 @@ format_vnet_sw_interface_name_override (u8 * s, va_list * args)
name, si->sw_if_index,
format_vnet_sw_interface_flags, si->flags);
s = format_vnet_sw_interface_cntrs (s, im, si);
s = format_vnet_sw_interface_cntrs (s, im, si, 0 /* want json */ );
return s;
}