diff options
| author | Lu Qiuwen <[email protected]> | 2023-06-28 17:53:11 +0800 |
|---|---|---|
| committer | Lu Qiuwen <[email protected]> | 2023-06-28 18:04:56 +0800 |
| commit | f514b86717651ec14d1140aed18f913403f83283 (patch) | |
| tree | 20f028c98fa32614a13e92abe98a64f28eadf047 | |
| parent | d453bee52d0091aa86126e651da9d96c1046e775 (diff) | |
支持通过HTTP方式响应健康状态。v4.6.33-20230628
| -rw-r--r-- | service/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | service/include/sc_common.h | 6 | ||||
| -rw-r--r-- | service/src/core.c | 2 | ||||
| -rw-r--r-- | service/src/http_serv.c | 139 | ||||
| -rw-r--r-- | service/src/olp.c | 126 |
5 files changed, 274 insertions, 1 deletions
diff --git a/service/CMakeLists.txt b/service/CMakeLists.txt index 5f0403c..7590711 100644 --- a/service/CMakeLists.txt +++ b/service/CMakeLists.txt @@ -9,7 +9,7 @@ add_executable(mrzcpd src/core.c src/devmgr.c src/mrb.c src/hwinfo.c src/vdev.c src/vdata.c src/app.c src/monit.c src/node.c src/node_shmdev.c src/node_phydev.c src/node_lb.c src/node_classifier.c src/node_etherfabric.c src/node_eth_ingress.c src/node_eth_egress.c src/node_bfd.c src/node_vwire.c - src/node_health_check.c src/node_forwarder.c src/node_bridge.c src/pdump.c) + src/node_health_check.c src/node_forwarder.c src/node_bridge.c src/pdump.c src/olp.c src/http_serv.c) target_link_libraries(mrzcpd MESA_prof_load_static msgpack-c-static ${DPDK_LIBRARY} ${SYSTEMD_LIBRARIES}) target_link_libraries(mrzcpd rt pthread dl infra z elf) diff --git a/service/include/sc_common.h b/service/include/sc_common.h index 5dddeca..2959678 100644 --- a/service/include/sc_common.h +++ b/service/include/sc_common.h @@ -53,6 +53,8 @@ struct rpc_server_handler; struct node_eth_ingress_main; struct node_eth_egress_main; struct node_bfd_main; +struct olp_manager_main; +struct http_serv_main; /* Service Instance */ struct sc_main @@ -140,6 +142,10 @@ struct sc_main struct node_etherfabric_main * etherfabric_node_main; /* Bfd Node */ struct node_bfd_main * bfd_node_main; + /* OLP Manager */ + struct olp_manager_main * olp_mgr_main; + /* HTTP Server */ + struct http_serv_main * ht_server_main; }; struct sc_main * sc_main_get(); diff --git a/service/src/core.c b/service/src/core.c index 0a33eaa..fffc419 100644 --- a/service/src/core.c +++ b/service/src/core.c @@ -914,6 +914,7 @@ extern int vwire_init(struct sc_main * sc); extern int health_check_init(struct sc_main * sc); extern int bridge_init(struct sc_main * sc); extern int mr_pdump_init(struct sc_main * sc); +extern int http_serv_init(struct sc_main * sc_main); int main(int argc, char * argv[]) { @@ -1237,6 +1238,7 @@ int main(int argc, char * argv[]) } rte_metrics_init(SOCKET_ID_ANY); + http_serv_init(sc); /* 延迟监测 */ if (sc->en_pkt_latency) diff --git a/service/src/http_serv.c b/service/src/http_serv.c new file mode 100644 index 0000000..e40fd7f --- /dev/null +++ b/service/src/http_serv.c @@ -0,0 +1,139 @@ +#include <assert.h> +#include <stdio.h> +#include <rte_graph.h> +#include <rte_malloc.h> + +#include <MESA_prof_load.h> +#include <cJSON.h> +#include <common.h> +#include <sc_common.h> +#include <sc_vdev.h> + +#include <event2/event.h> +#include <event2/http.h> +#include <event2/buffer.h> + +struct http_serv_main +{ + struct event_base * ev_base; + struct evhttp * ev_http; + pthread_t ev_dispatch_tid; +}; + +/* config example: + * [:0] + * name = olp0 + * type = niagara_3296 + * serial_number = 12345678abcdefgh + * + * [olp_channel:0] + * device = olp0 + * state = 3 + * watchdog = 1 + * watchdog_kick_time_in_ms = 300 + * watchdog_timeout_in_ms = 600 + */ + +/* liveness probe callback */ +static void http_serv_probe_cb(struct evhttp_request * req, void * arg) +{ + struct evbuffer * evb = evbuffer_new(); + if (!evb) + { + MR_ERROR("failed to create response buffer"); + return; + } + + evbuffer_add_printf(evb, "Probe successfully, MRZCPD is running."); + evhttp_send_reply(req, HTTP_OK, "OK", evb); + evbuffer_free(evb); +} + +/* event_base dispatch thread */ +static void * http_serv_dispatch(void * arg) +{ + struct event_base * base = (struct event_base *)arg; + event_base_dispatch(base); + return NULL; +} + +int http_serv_deinit(struct sc_main * sc_main) +{ + struct http_serv_main * ht_server_main = sc_main->ht_server_main; + assert(ht_server_main != NULL); + + /* stop event_base dispatch thread */ + event_base_loopexit(ht_server_main->ev_base, NULL); + pthread_join(ht_server_main->ev_dispatch_tid, NULL); + + /* release all ctx */ + evhttp_free(ht_server_main->ev_http); + event_base_free(ht_server_main->ev_base); + + /* free http_serv_main */ + FREE(ht_server_main); + return 0; +} + +int http_serv_init(struct sc_main * sc_main) +{ + struct http_serv_main * http_server_main = ZMALLOC(sizeof(struct http_serv_main)); + MR_VERIFY_MALLOC(http_server_main); + + struct event_config * cfg = event_config_new(); + struct event_base * base = event_base_new_with_config(cfg); + if (!base) + { + MR_ERROR("Couldn't create an event_base."); + goto errout; + } + + event_config_free(cfg); + cfg = NULL; + + /* Create a new evhttp object to socket requests. */ + struct evhttp * http = evhttp_new(base); + if (!http) + { + MR_ERROR("couldn't create evhttp. Exiting.\n"); + goto errout; + } + + /* The /dump URI will dump all requests to stdout and say 200 ok. */ + evhttp_set_cb(http, "/probe", http_serv_probe_cb, (void *)http_server_main); + + /* Load the listen address and port */ + char str_listen_addr[INET_ADDRSTRLEN] = {}; + MESA_load_profile_string_def(sc_main->local_cfgfile, "http_server", "listen_addr", str_listen_addr, + sizeof(str_listen_addr), "0.0.0.0"); + + unsigned int listen_port = 9086; + MESA_load_profile_uint_def(sc_main->local_cfgfile, "http_server", "listen_port", &listen_port, 9086); + + struct evhttp_bound_socket * socket = evhttp_bind_socket_with_handle(http, str_listen_addr, listen_port); + if (!socket) + { + MR_ERROR("couldn't bind to %s:%d.", str_listen_addr, listen_port); + goto errout; + } + + /* create a thread to do the event_base dispatch */ + pthread_t tid; + if (pthread_create(&tid, NULL, http_serv_dispatch, base) != 0) + { + MR_ERROR("failed to create thread for event_base dispatch"); + goto errout; + } + + /* save the event_base, ev_http handles to http_serv_main */ + http_server_main->ev_base = base; + http_server_main->ev_http = http; + http_server_main->ev_dispatch_tid = tid; + + sc_main->ht_server_main = http_server_main; + MR_INFO("HTTP Server is listening on %s:%u", str_listen_addr, listen_port); + return 0; + +errout: + return -1; +}
\ No newline at end of file diff --git a/service/src/olp.c b/service/src/olp.c new file mode 100644 index 0000000..956aa58 --- /dev/null +++ b/service/src/olp.c @@ -0,0 +1,126 @@ +#include <assert.h> +#include <net/if.h> +#include <stdio.h> +#include <sys/ioctl.h> +#include <sys/queue.h> +#include <unistd.h> +#include <sched.h> + +#include <rte_bus_pci.h> +#include <rte_config.h> +#include <rte_debug.h> +#include <rte_eth_bond.h> +#include <rte_ethdev.h> +#include <rte_ether.h> +#include <rte_flow.h> +#include <rte_graph.h> +#include <rte_malloc.h> +#include <rte_node_eth_api.h> +#include <rte_pci.h> +#include <rte_string_fns.h> + +#include <MESA_prof_load.h> +#include <cJSON.h> +#include <common.h> +#include <sc_common.h> +#include <sc_devmgr.h> +#include <sc_mrb.h> +#include <sc_vdev.h> + +#define NR_OLP_DEVICE_MAX 8 +#define NR_OLP_CHANNEL_PER_DEVICE_MAX 8 + +enum olp_device_type +{ + OLP_DEVICE_TYPE_UNKNOWN, + OLP_DEVICE_TYPE_NIAGARA_3296, +}; + +enum olp_channel_state +{ + OLP_CHANNEL_STATE_FORCE_ACTIVE, + OLP_CHANNEL_STATE_FORCE_BYPASS, + OLP_CHANNEL_STATE_REGULAR, +}; + +struct olp_manager_main +{ + struct olp_dev_desc * olp_dev_descs[NR_OLP_DEVICE_MAX]; + unsigned int nr_olp_dev_descs; +}; + +struct olp_dev_desc +{ + char devsym[MR_SYMBOL_MAX]; + enum olp_device_type type; + struct olp_channel * channels[NR_OLP_CHANNEL_PER_DEVICE_MAX]; +}; + +struct olp_channel +{ + unsigned int olp_channel_id; + enum olp_channel_state state; + + struct olp_dev_desc * dev_desc; + unsigned int en_watchdog; + unsigned int watchdog_kick_time_in_ms; + unsigned int watchdog_timeout_in_ms; +}; + +/* config example: + * [olp_device:0] + * name = olp0 + * type = niagara_3296 + * serial_number = 12345678abcdefgh + * + * [olp_channel:0] + * device = olp0 + * state = 3 + * watchdog = 1 + * watchdog_kick_time_in_ms = 300 + * watchdog_timeout_in_ms = 600 + */ + +int olp_manager_config(struct olp_manager_main * olp_mgr, const char * cfgfile) +{ + for (unsigned int i = 0; i < NR_OLP_DEVICE_MAX; i++) + { + char str_section_header[MR_SYMBOL_MAX]; + snprintf(str_section_header, MR_SYMBOL_MAX, "olp_device:%u", i); + + char olp_dev_sym[MR_SYMBOL_MAX]; + int ret = MESA_load_profile_string_nodef(cfgfile, str_section_header, "name", olp_dev_sym, MR_SYMBOL_MAX); + if (ret < 0) + { + continue; + } + + unsigned int olp_dev_type = 0; + MESA_load_profile_uint_def(cfgfile, str_section_header, "type", &olp_dev_type, OLP_DEVICE_TYPE_UNKNOWN); + switch (olp_dev_type) + { + case OLP_DEVICE_TYPE_NIAGARA_3296: + case OLP_DEVICE_TYPE_UNKNOWN: + default: + MR_ERROR("olp device %s is not supported (type = %d) \n", olp_dev_sym, olp_dev_type); + continue; + } + + struct olp_dev_desc * dev_desc = ZMALLOC(sizeof(struct olp_dev_desc)); + MR_VERIFY_MALLOC(dev_desc); + snprintf(dev_desc->devsym, MR_SYMBOL_MAX, "%s", olp_dev_sym); + dev_desc->type = olp_dev_type; + + /* add the olp device to the list */ + olp_mgr->olp_dev_descs[olp_mgr->nr_olp_dev_descs++] = dev_desc; + } + + return 0; +} + +int olp_manager_init(struct sc_main * sc_main) +{ + struct olp_manager_main * olp_mgr_main = ZMALLOC(sizeof(struct olp_manager_main)); + MR_VERIFY_MALLOC(olp_mgr_main); + sc_main->olp_mgr_main = olp_mgr_main; +}
\ No newline at end of file |
