http_static: code cleanup

Type: refactor

Signed-off-by: Florin Coras <fcoras@cisco.com>
Change-Id: Ic8838c8ef558d671740094a98b5a627a18c8c808
This commit is contained in:
Florin Coras
2022-01-26 16:28:58 -08:00
committed by Damjan Marion
parent aedcfaf80c
commit ae0e3e748f
6 changed files with 226 additions and 351 deletions

View File

@ -425,6 +425,7 @@ F: src/plugins/flowprobe/
Plugin - http_static
I: http_static
M: Dave Barach <vpp@barachs.net>
M: Florin Coras <fcoras@cisco.com>
F: src/plugins/http_static/
Plugin - builtinurl

View File

@ -19,8 +19,7 @@
#include <vpp/app/version.h>
int
handle_get_version (http_builtin_method_type_t reqtype,
u8 * request, http_session_t * hs)
handle_get_version (http_req_method_t reqtype, u8 *request, hss_session_t *hs)
{
u8 *s = 0;
@ -64,8 +63,8 @@ trim_path_from_request (u8 * s, char *path)
}
int
handle_get_interface_stats (http_builtin_method_type_t reqtype,
u8 * request, http_session_t * hs)
handle_get_interface_stats (http_req_method_t reqtype, u8 *request,
hss_session_t *hs)
{
u8 *s = 0, *stats = 0;
uword *p;
@ -81,7 +80,7 @@ handle_get_interface_stats (http_builtin_method_type_t reqtype,
vnet_interface_main_t *im = &vnm->interface_main;
/* Get stats for a single interface via http POST */
if (reqtype == HTTP_BUILTIN_METHOD_POST)
if (reqtype == HTTP_REQ_POST)
{
trim_path_from_request (request, "interface_stats.json");
@ -143,8 +142,8 @@ out:
}
int
handle_get_interface_list (http_builtin_method_type_t reqtype,
u8 * request, http_session_t * hs)
handle_get_interface_list (http_req_method_t reqtype, u8 *request,
hss_session_t *hs)
{
u8 *s = 0;
int i;
@ -188,14 +187,13 @@ void
builtinurl_handler_init (builtinurl_main_t * bm)
{
bm->register_handler (handle_get_version, "version.json",
HTTP_BUILTIN_METHOD_GET);
bm->register_handler (handle_get_version, "version.json", HTTP_REQ_GET);
bm->register_handler (handle_get_interface_list, "interface_list.json",
HTTP_BUILTIN_METHOD_GET);
bm->register_handler (handle_get_interface_stats,
"interface_stats.json", HTTP_BUILTIN_METHOD_GET);
bm->register_handler (handle_get_interface_stats,
"interface_stats.json", HTTP_BUILTIN_METHOD_POST);
HTTP_REQ_GET);
bm->register_handler (handle_get_interface_stats, "interface_stats.json",
HTTP_REQ_GET);
bm->register_handler (handle_get_interface_stats, "interface_stats.json",
HTTP_REQ_POST);
}
/*

View File

@ -1,7 +1,5 @@
/*
* http_static.c - skeleton vpp engine plug-in
*
* Copyright (c) <current-year> <your-organization>
* Copyright (c) 2017-2022 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:
@ -31,17 +29,85 @@
#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
#define REPLY_MSG_ID_BASE hmp->msg_id_base
#define REPLY_MSG_ID_BASE hsm->msg_id_base
#include <vlibapi/api_helper_macros.h>
http_static_main_t http_static_main;
/** \brief Register a builtin GET or POST handler
*/
__clib_export void
http_static_server_register_builtin_handler (void *fp, char *url,
http_req_method_t request_type)
{
hss_main_t *hsm = &hss_main;
uword *p, *builtin_table;
builtin_table = (request_type == HTTP_REQ_GET) ? hsm->get_url_handlers :
hsm->post_url_handlers;
p = hash_get_mem (builtin_table, url);
if (p)
{
clib_warning ("WARNING: attempt to replace handler for %s '%s' ignored",
(request_type == HTTP_REQ_GET) ? "GET" : "POST", url);
return;
}
hash_set_mem (builtin_table, url, (uword) fp);
/*
* Need to update the hash table pointer in http_static_server_main
* in case we just expanded it...
*/
if (request_type == HTTP_REQ_GET)
hsm->get_url_handlers = builtin_table;
else
hsm->post_url_handlers = builtin_table;
}
/** \brief API helper function for vl_api_http_static_enable_t messages
*/
static int
hss_enable_api (u32 fifo_size, u32 cache_limit, u32 prealloc_fifos,
u32 private_segment_size, u8 *www_root, u8 *uri)
{
hss_main_t *hsm = &hss_main;
int rv;
hsm->fifo_size = fifo_size;
hsm->cache_limit = cache_limit;
hsm->prealloc_fifos = prealloc_fifos;
hsm->private_segment_size = private_segment_size;
hsm->www_root = format (0, "%s%c", www_root, 0);
hsm->uri = format (0, "%s%c", uri, 0);
if (vec_len (hsm->www_root) < 2)
return VNET_API_ERROR_INVALID_VALUE;
if (hsm->app_index != ~0)
return VNET_API_ERROR_APP_ALREADY_ATTACHED;
vnet_session_enable_disable (hsm->vlib_main, 1 /* turn on TCP, etc. */);
rv = hss_create (hsm->vlib_main);
switch (rv)
{
case 0:
break;
default:
vec_free (hsm->www_root);
vec_free (hsm->uri);
return VNET_API_ERROR_INIT_FAILED;
}
return 0;
}
/* API message handler */
static void vl_api_http_static_enable_t_handler
(vl_api_http_static_enable_t * mp)
{
vl_api_http_static_enable_reply_t *rmp;
http_static_main_t *hmp = &http_static_main;
hss_main_t *hsm = &hss_main;
int rv;
mp->uri[ARRAY_LEN (mp->uri) - 1] = 0;
@ -57,28 +123,23 @@ static void vl_api_http_static_enable_t_handler
#include <http_static/http_static.api.c>
static clib_error_t *
http_static_init (vlib_main_t * vm)
hss_api_init (vlib_main_t *vm)
{
http_static_main_t *hmp = &http_static_main;
hmp->vlib_main = vm;
hmp->vnet_main = vnet_get_main ();
hss_main_t *hsm = &hss_main;
/* Ask for a correctly-sized block of API message decode slots */
hmp->msg_id_base = setup_message_id_table ();
hsm->msg_id_base = setup_message_id_table ();
return 0;
}
VLIB_INIT_FUNCTION (http_static_init);
VLIB_INIT_FUNCTION (hss_api_init);
/* *INDENT-OFF* */
VLIB_PLUGIN_REGISTER () =
{
.version = VPP_BUILD_VER,
.description = "HTTP Static Server"
};
/* *INDENT-ON* */
/*
* fd.io coding-style-patch-verification: ON

View File

@ -1,8 +1,5 @@
/*
* http_static.h - skeleton vpp engine plug-in header file
*
* Copyright (c) <current-year> <your-organization>
* Copyright (c) 2017-2022 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:
@ -18,76 +15,29 @@
#ifndef __included_http_static_h__
#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 <http/http.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 http_static.h
* Static http server definitions
*/
typedef struct
{
/* API message ID base */
u16 msg_id_base;
/* convenience */
vlib_main_t *vlib_main;
vnet_main_t *vnet_main;
} http_static_main_t;
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
{
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 _
u32 session_index;
/** 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;
session_handle_t vpp_session_handle;
/** Fully-resolved file path */
u8 *path;
/** File data, a vector */
@ -96,10 +46,9 @@ typedef struct
u32 data_offset;
/** Need to free data in detach_cache_entry */
int free_data;
/** File cache pool index */
u32 cache_pool_index;
} http_session_t;
} hss_session_t;
/** \brief In-memory file data cache entry
*/
@ -116,7 +65,7 @@ typedef struct
u32 prev_index;
/** Reference count, so we don't recycle while referenced */
int inuse;
} file_data_cache_t;
} hss_cache_entry_t;
/** \brief Main data structure
*/
@ -124,15 +73,12 @@ typedef struct
typedef struct
{
/** Per thread vector of session pools */
http_session_t **sessions;
hss_session_t **sessions;
/** Session pool reader writer lock */
clib_spinlock_t cache_lock;
/** Enable debug messages */
int debug_level;
/** Unified file data cache pool */
file_data_cache_t *cache_pool;
hss_cache_entry_t *cache_pool;
/** Hash table which maps file name to file data */
BVT (clib_bihash) name_to_data;
@ -160,16 +106,21 @@ typedef struct
/** Cert and key pair for tls */
u32 ckpair_index;
/* API message ID base */
u16 msg_id_base;
vlib_main_t *vlib_main;
/*
* Config
*/
/** Enable debug messages */
int debug_level;
/** Number of preallocated fifos, usually 0 */
u32 prealloc_fifos;
/** Private segment size, usually 0 */
u32 private_segment_size;
u64 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 */
@ -180,11 +131,10 @@ typedef struct
extern hss_main_t hss_main;
int hss_enable_api (u32 fifo_size, u32 cache_limit, u32 prealloc_fifos,
u32 private_segment_size, u8 *www_root, u8 *uri);
int hss_create (vlib_main_t *vm);
void http_static_server_register_builtin_handler
(void *fp, char *url, int type);
void http_static_server_register_builtin_handler (void *fp, char *url,
http_req_method_t type);
#endif /* __included_http_static_h__ */

File diff suppressed because it is too large Load Diff

View File

@ -16,8 +16,7 @@ mactime_ip_neighbor_copy (index_t ipni, void *ctx)
}
static int
handle_get_mactime (http_builtin_method_type_t reqtype,
u8 * request, http_session_t * hs)
handle_get_mactime (http_req_method_t reqtype, u8 *request, hss_session_t *hs)
{
mactime_main_t *mm = &mactime_main;
mactime_device_t *dp;
@ -169,7 +168,7 @@ mactime_url_init (vlib_main_t * vm)
return;
}
(*fp) (handle_get_mactime, "mactime.json", HTTP_BUILTIN_METHOD_GET);
(*fp) (handle_get_mactime, "mactime.json", HTTP_REQ_GET);
}
/*