From 43765e2b4eaa8e1f2a5f1562414e04962c777ff3 Mon Sep 17 00:00:00 2001 From: Dave Barach Date: Tue, 17 Sep 2019 09:47:35 -0400 Subject: builtinurl: initial working attempt Note that the builtin URLs are disabled by default. To activate, "builtinurl enable" or use the builtinurl_enable API. See .../extras/http/sample.md for some Hugo-friendly .md w/ embedded Javascript that accesses the builtin URLs. Type: feature Signed-off-by: Dave Barach Change-Id: I6d82d9292c41d6d2d90be73ba8a1a043fb20c986 --- src/plugins/builtinurl/CMakeLists.txt | 30 ++++ src/plugins/builtinurl/builtins.c | 129 ++++++++++++++++ src/plugins/builtinurl/builtinurl.api | 43 ++++++ src/plugins/builtinurl/builtinurl.c | 208 ++++++++++++++++++++++++++ src/plugins/builtinurl/builtinurl.h | 57 +++++++ src/plugins/builtinurl/builtinurl_all_api_h.h | 19 +++ src/plugins/builtinurl/builtinurl_msg_enum.h | 31 ++++ src/plugins/builtinurl/builtinurl_test.c | 149 ++++++++++++++++++ 8 files changed, 666 insertions(+) create mode 100644 src/plugins/builtinurl/CMakeLists.txt create mode 100644 src/plugins/builtinurl/builtins.c create mode 100644 src/plugins/builtinurl/builtinurl.api create mode 100644 src/plugins/builtinurl/builtinurl.c create mode 100644 src/plugins/builtinurl/builtinurl.h create mode 100644 src/plugins/builtinurl/builtinurl_all_api_h.h create mode 100644 src/plugins/builtinurl/builtinurl_msg_enum.h create mode 100644 src/plugins/builtinurl/builtinurl_test.c (limited to 'src/plugins') diff --git a/src/plugins/builtinurl/CMakeLists.txt b/src/plugins/builtinurl/CMakeLists.txt new file mode 100644 index 00000000000..e866f1174f1 --- /dev/null +++ b/src/plugins/builtinurl/CMakeLists.txt @@ -0,0 +1,30 @@ + +# Copyright (c) +# 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: +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +add_vpp_plugin(builtinurl + SOURCES + builtins.c + builtinurl.c + builtinurl.h + + API_FILES + builtinurl.api + + INSTALL_HEADERS + builtinurl_all_api_h.h + builtinurl_msg_enum.h + + API_TEST_SOURCES + builtinurl_test.c +) diff --git a/src/plugins/builtinurl/builtins.c b/src/plugins/builtinurl/builtins.c new file mode 100644 index 00000000000..73582220995 --- /dev/null +++ b/src/plugins/builtinurl/builtins.c @@ -0,0 +1,129 @@ +/* + * Copyright (c) 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: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include + +int +handle_get_version (u8 * request, http_session_t * hs) +{ + u8 *s = 0; + + /* Build some json bullshit */ + s = format (s, "{\"vpp_details\": {"); + s = format (s, " \"version\": \"%s\",", VPP_BUILD_VER); + s = format (s, " \"build_date\": \"%s\"}}\r\n", VPP_BUILD_DATE); + + hs->data = s; + hs->data_offset = 0; + hs->cache_pool_index = ~0; + hs->free_data = 1; + return 0; +} + +void +trim_path_from_request (u8 * s, char *path) +{ + u8 *cp; + int trim_length = strlen (path) + 1 /* remove '?' */ ; + + /* Get rid of the path and question-mark */ + vec_delete (s, trim_length, 0); + + /* Tail trim irrelevant browser info */ + cp = s; + while ((cp - s) < vec_len (s)) + { + if (*cp == ' ') + { + /* + * Makes request a vector which happens to look + * like a c-string. + */ + *cp = 0; + _vec_len (s) = cp - s; + break; + } + cp++; + } +} + +int +handle_get_interface_stats (u8 * request, http_session_t * hs) +{ + u8 *s = 0, *stats = 0; + u32 sw_if_index; + uword *p; + vnet_sw_interface_t *si; + u8 *format_vnet_sw_interface_cntrs (u8 * s, vnet_interface_main_t * im, + vnet_sw_interface_t * si, int json); + + vnet_main_t *vnm = vnet_get_main (); + + trim_path_from_request (request, "interface_stats.json"); + + /* get data */ + + p = hash_get (vnm->interface_main.hw_interface_by_name, request); + if (!p) + { + clib_warning ("Couldn't find interface '%v'", request); + + s = format (s, "{\"interface_stats\": {"); + s = format (s, " \"name\": \"%s\",", request); + s = format (s, " \"stats\": \"%s\"", "ERRORUnknownInterface"); + s = format (s, "}}\r\n"); + goto out; + } + + sw_if_index = p[0]; + si = vnet_get_sw_interface (vnm, sw_if_index); + + stats = format_vnet_sw_interface_cntrs (stats, &vnm->interface_main, si, + 1 /* want json */ ); + /* Build answer */ + s = format (s, "{\"interface_stats\": {"); + s = format (s, "\"name\": \"%s\",\n", request); + s = format (s, "%s", stats); + s = format (s, "}}\n"); + vec_free (stats); + +out: + hs->data = s; + hs->data_offset = 0; + hs->cache_pool_index = ~0; + hs->free_data = 1; + return 0; +} + +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_interface_stats, + "interface_stats.json", HTTP_BUILTIN_METHOD_POST); +} + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/src/plugins/builtinurl/builtinurl.api b/src/plugins/builtinurl/builtinurl.api new file mode 100644 index 00000000000..f292fd77a8e --- /dev/null +++ b/src/plugins/builtinurl/builtinurl.api @@ -0,0 +1,43 @@ +/* + * builtinurl.api - binary API skeleton + * + * Copyright (c) + * 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: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @file builtinurl.api + * @brief VPP control-plane API messages. + * + * This file defines VPP control-plane binary API messages which are generally + * called through a shared memory interface. + */ + +/* Version and type recitations */ + +option version = "1.0.0"; + +/** @brief API to enable / disable builtinurl on an interface + @param client_index - opaque cookie to identify the sender + @param context - sender context, to match reply w/ request + @param enable_disable - 1 to enable, 0 to disable the feature + @param sw_if_index - interface handle +*/ + +autoreply define builtinurl_enable { + /* Client identifier, set from api_main.my_client_index */ + u32 client_index; + + /* Arbitrary context, so client can match reply to request */ + u32 context; +}; diff --git a/src/plugins/builtinurl/builtinurl.c b/src/plugins/builtinurl/builtinurl.c new file mode 100644 index 00000000000..796035ebce5 --- /dev/null +++ b/src/plugins/builtinurl/builtinurl.c @@ -0,0 +1,208 @@ +/* + * builtinurl.c - skeleton vpp engine plug-in + * + * Copyright (c) 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: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include + +#include +#include +#include +#include + +/* define message IDs */ +#include + +/* define message structures */ +#define vl_typedefs +#include +#undef vl_typedefs + +/* define generated endian-swappers */ +#define vl_endianfun +#include +#undef vl_endianfun + +/* instantiate all the print functions we know about */ +#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__) +#define vl_printfun +#include +#undef vl_printfun + +/* Get the API version number */ +#define vl_api_version(n,v) static u32 api_version=(v); +#include +#undef vl_api_version + +#define REPLY_MSG_ID_BASE bmp->msg_id_base +#include + +builtinurl_main_t builtinurl_main; + +/* List of message types that this plugin understands */ + +#define foreach_builtinurl_plugin_api_msg \ +_(BUILTINURL_ENABLE, builtinurl_enable) + +/* Action function shared between message handler and debug CLI */ + +int +builtinurl_enable (builtinurl_main_t * bmp) +{ + void (*fp) (void *, char *, int); + + if (bmp->initialized) + return 0; + + /* Look up the builtin URL registration handler */ + fp = vlib_get_plugin_symbol + ("http_static_plugin.so", "http_static_server_register_builtin_handler"); + + /* Most likely, the http_static plugin isn't loaded. Done. */ + if (fp == 0) + return VNET_API_ERROR_NO_SUCH_TABLE; + + bmp->register_handler = fp; + builtinurl_handler_init (bmp); + bmp->initialized = 1; + + return 0; +} + +static clib_error_t * +builtinurl_enable_command_fn (vlib_main_t * vm, + unformat_input_t * input, + vlib_cli_command_t * cmd) +{ + builtinurl_main_t *bmp = &builtinurl_main; + + int rv; + + rv = builtinurl_enable (bmp); + + switch (rv) + { + case 0: + break; + + case VNET_API_ERROR_NO_SUCH_TABLE: + return clib_error_return + (0, "http_static_server_register_builtin_handler undefined"); + break; + + default: + return clib_error_return (0, "builtinurl_enable returned %d", rv); + } + return 0; +} + +/* *INDENT-OFF* */ +VLIB_CLI_COMMAND (builtinurl_enable_command, static) = +{ + .path = "builtinurl enable", + .short_help = "Turn on builtin http/https GET and POST urls", + .function = builtinurl_enable_command_fn, +}; +/* *INDENT-ON* */ + +/* API message handler */ +static void vl_api_builtinurl_enable_t_handler + (vl_api_builtinurl_enable_t * mp) +{ + vl_api_builtinurl_enable_reply_t *rmp; + builtinurl_main_t *bmp = &builtinurl_main; + int rv; + + rv = builtinurl_enable (bmp); + + REPLY_MACRO (VL_API_BUILTINURL_ENABLE_REPLY); +} + +/* Set up the API message handling tables */ +static clib_error_t * +builtinurl_plugin_api_hookup (vlib_main_t * vm) +{ + builtinurl_main_t *bmp = &builtinurl_main; +#define _(N,n) \ + vl_msg_api_set_handlers((VL_API_##N + bmp->msg_id_base), \ + #n, \ + vl_api_##n##_t_handler, \ + vl_noop_handler, \ + vl_api_##n##_t_endian, \ + vl_api_##n##_t_print, \ + sizeof(vl_api_##n##_t), 1); + foreach_builtinurl_plugin_api_msg; +#undef _ + + return 0; +} + +#define vl_msg_name_crc_list +#include +#undef vl_msg_name_crc_list + +static void +setup_message_id_table (builtinurl_main_t * bmp, api_main_t * am) +{ +#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + bmp->msg_id_base); + foreach_vl_msg_name_crc_builtinurl; +#undef _ +} + +static clib_error_t * +builtinurl_init (vlib_main_t * vm) +{ + builtinurl_main_t *bmp = &builtinurl_main; + clib_error_t *error = 0; + u8 *name; + + bmp->vlib_main = vm; + bmp->vnet_main = vnet_get_main (); + + name = format (0, "builtinurl_%08x%c", api_version, 0); + + /* Ask for a correctly-sized block of API message decode slots */ + bmp->msg_id_base = vl_msg_api_get_msg_ids + ((char *) name, VL_MSG_FIRST_AVAILABLE); + + error = builtinurl_plugin_api_hookup (vm); + + /* Add our API messages to the global name_crc hash table */ + setup_message_id_table (bmp, &api_main); + + vec_free (name); + + return error; +} + +VLIB_INIT_FUNCTION (builtinurl_init); + +/* *INDENT-OFF* */ +VLIB_PLUGIN_REGISTER () = +{ + .version = VPP_BUILD_VER, + .description = "vpp built-in URL support", +}; +/* *INDENT-ON* */ + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/src/plugins/builtinurl/builtinurl.h b/src/plugins/builtinurl/builtinurl.h new file mode 100644 index 00000000000..91302c1eee5 --- /dev/null +++ b/src/plugins/builtinurl/builtinurl.h @@ -0,0 +1,57 @@ + +/* + * builtinurl.h - built-in URLs for the http static server + * + * Copyright (c) 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: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef __included_builtinurl_h__ +#define __included_builtinurl_h__ + +#include +#include +#include + +#include +#include + +typedef struct +{ + /* API message ID base */ + u16 msg_id_base; + + /* GET / POST handler registration function */ + void (*register_handler) (void *, char *, int); + + /* Been there, done that */ + int initialized; + + /* convenience */ + vlib_main_t *vlib_main; + vnet_main_t *vnet_main; + ethernet_main_t *ethernet_main; +} builtinurl_main_t; + +extern builtinurl_main_t builtinurl_main; + +void builtinurl_handler_init (builtinurl_main_t * bm); + +#endif /* __included_builtinurl_h__ */ + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/src/plugins/builtinurl/builtinurl_all_api_h.h b/src/plugins/builtinurl/builtinurl_all_api_h.h new file mode 100644 index 00000000000..a4c152b6e03 --- /dev/null +++ b/src/plugins/builtinurl/builtinurl_all_api_h.h @@ -0,0 +1,19 @@ + +/* + * builtinurl_all_api_h.h - skeleton vpp engine plug-in api #include file + * + * Copyright (c) 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: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/* Include the generated file, see BUILT_SOURCES in Makefile.am */ +#include diff --git a/src/plugins/builtinurl/builtinurl_msg_enum.h b/src/plugins/builtinurl/builtinurl_msg_enum.h new file mode 100644 index 00000000000..5658576a735 --- /dev/null +++ b/src/plugins/builtinurl/builtinurl_msg_enum.h @@ -0,0 +1,31 @@ + +/* + * builtinurl_msg_enum.h - skeleton vpp engine plug-in message enumeration + * + * Copyright (c) 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: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef included_builtinurl_msg_enum_h +#define included_builtinurl_msg_enum_h + +#include + +#define vl_msg_id(n,h) n, +typedef enum { +#include + /* We'll want to know how many messages IDs we need... */ + VL_MSG_FIRST_AVAILABLE, +} vl_msg_id_t; +#undef vl_msg_id + +#endif /* included_builtinurl_msg_enum_h */ diff --git a/src/plugins/builtinurl/builtinurl_test.c b/src/plugins/builtinurl/builtinurl_test.c new file mode 100644 index 00000000000..c71be664608 --- /dev/null +++ b/src/plugins/builtinurl/builtinurl_test.c @@ -0,0 +1,149 @@ +/* + * builtinurl.c - skeleton vpp-api-test plug-in + * + * Copyright (c) 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: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include +#include +#include +#include +#include + +uword unformat_sw_if_index (unformat_input_t * input, va_list * args); + +/* Declare message IDs */ +#include + +/* define message structures */ +#define vl_typedefs +#include +#undef vl_typedefs + +/* declare message handlers for each api */ + +#define vl_endianfun /* define message structures */ +#include +#undef vl_endianfun + +/* instantiate all the print functions we know about */ +#define vl_print(handle, ...) +#define vl_printfun +#include +#undef vl_printfun + +/* Get the API version number. */ +#define vl_api_version(n,v) static u32 api_version=(v); +#include +#undef vl_api_version + + +typedef struct +{ + /* API message ID base */ + u16 msg_id_base; + vat_main_t *vat_main; +} builtinurl_test_main_t; + +builtinurl_test_main_t builtinurl_test_main; + +#define __plugin_msg_base builtinurl_test_main.msg_id_base +#include + +#define foreach_standard_reply_retval_handler \ +_(builtinurl_enable_reply) + +#define _(n) \ + static void vl_api_##n##_t_handler \ + (vl_api_##n##_t * mp) \ + { \ + vat_main_t * vam = builtinurl_test_main.vat_main; \ + i32 retval = ntohl(mp->retval); \ + if (vam->async_mode) { \ + vam->async_errors += (retval < 0); \ + } else { \ + vam->retval = retval; \ + vam->result_ready = 1; \ + } \ + } +foreach_standard_reply_retval_handler; +#undef _ + +/* + * Table of message reply handlers, must include boilerplate handlers + * we just generated + */ +#define foreach_vpe_api_reply_msg \ +_(BUILTINURL_ENABLE_REPLY, builtinurl_enable_reply) + + +static int +api_builtinurl_enable_disable (vat_main_t * vam) +{ + vl_api_builtinurl_enable_t *mp; + int ret; + + /* Construct the API message */ + M (BUILTINURL_ENABLE, mp); + + /* send it... */ + S (mp); + + /* Wait for a reply... */ + W (ret); + return ret; +} + +/* + * List of messages that the api test plugin sends, + * and that the data plane plugin processes + */ +#define foreach_vpe_api_msg \ +_(builtinurl_enable_disable, "") + +static void +builtinurl_api_hookup (vat_main_t * vam) +{ + builtinurl_test_main_t *btmp = &builtinurl_test_main; + /* Hook up handlers for replies from the data plane plug-in */ +#define _(N,n) \ + vl_msg_api_set_handlers((VL_API_##N + btmp->msg_id_base), \ + #n, \ + vl_api_##n##_t_handler, \ + vl_noop_handler, \ + vl_api_##n##_t_endian, \ + vl_api_##n##_t_print, \ + sizeof(vl_api_##n##_t), 1); + foreach_vpe_api_reply_msg; +#undef _ + + /* API messages we can send */ +#define _(n,h) hash_set_mem (vam->function_by_name, #n, api_##n); + foreach_vpe_api_msg; +#undef _ + + /* Help strings */ +#define _(n,h) hash_set_mem (vam->help_by_name, #n, h); + foreach_vpe_api_msg; +#undef _ +} + +VAT_PLUGIN_REGISTER (builtinurl); + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ -- cgit 1.2.3-korg