From f262e748ea5718e7759eb78bf05f1f7313b7aa1e Mon Sep 17 00:00:00 2001 From: huangxin Date: Mon, 13 Mar 2023 17:03:02 +0800 Subject: [PATCH] =?UTF-8?q?OCT=201.=20=E4=BC=98=E5=8C=96vCPE=20BRAS?= =?UTF-8?q?=E5=90=8E=E7=BA=BF=E7=A8=8Blinux=E7=B3=BB=E7=BB=9F=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E6=8F=8F=E8=BF=B0=E7=AC=A6fd=E8=B5=84=E6=BA=90?= =?UTF-8?q?=E5=8D=A0=E7=94=A8=201100+->300+?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 16 +++++++-- srcs/CMakeLists.txt | 2 +- srcs/libs/CMakeLists.txt | 4 ++- srcs/libs/configure/config.c | 2 ++ srcs/libs/configure/config_help.c | 4 ++- srcs/libs/hardware/cpu.c | 27 +++------------ srcs/libs/hardware/disk.c | 26 +++----------- srcs/libs/hardware/hardware.c | 57 +++++++++++++++---------------- srcs/libs/hardware/ipmltools.c | 42 +++++++++-------------- srcs/libs/hardware/memory.c | 32 +++++------------ srcs/libs/include/config.h | 2 ++ srcs/libs/init/init_runtime.c | 5 ++- 12 files changed, 90 insertions(+), 129 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c4c0eb5..bdfefa9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,7 +2,7 @@ CMAKE_MINIMUM_REQUIRED(VERSION 3.10 FATAL_ERROR) INCLUDE(CMakeDependentOption) OPTION(VCPE_AGENT "Enable vCPE agent test application" OFF) -OPTION(USED_LWIP "PPPoE of LWIP support for vCPE" OFF) +OPTION(VCPE_PPPOE "Enable vCPE work befof PPPoE mode" OFF) OPTION(USED_OPENDHCPD "DHCP server for vCPE" OFF) OPTION(USED_OPENDHCPDDNS "DHCP And DNS server for vCPE" OFF) OPTION(USED_USER_VNI "Support pass user vni id from console command line" OFF) @@ -18,6 +18,8 @@ INCLUDE(system_libs) SET(VCPE_VERSION "1.0.1") PROJECT(vCPE VERSION "${VCPE_VERSION}") CMAKE_DEPENDENT_OPTION(USED_HTTP_SVR "Build-in http(s) server support" ON "USED_OPENDHCPD OR USED_OPENDHCPDDNS" OFF) +CMAKE_DEPENDENT_OPTION(USED_LWIP "PPPoE of LWIP support for vCPE" ON "VCPE_PPPOE" OFF) +CMAKE_DEPENDENT_OPTION(USED_ZMQ "ZeroMQ support for vCPE" ON "VCPE_PPPOE" OFF) # 数据库开关 OPTION(USED_REDIS "Add redis database support for vCPE" OFF) @@ -84,7 +86,7 @@ INCLUDE(./depend/third_libs.cmake) IF (USED_JSON_VALIDATE) LIST(APPEND COMMON_DEFINE "-DJSON_SCHEMA_ON") -ENDIF() +ENDIF () IF (USED_USER_VNI) LIST(APPEND COMMON_DEFINE "-DUSERVNI_ON") @@ -96,6 +98,16 @@ IF (USED_HTTP_SVR) MESSAGE("Select Option USED_HTTP_SVR") ENDIF () +IF (USED_ZMQ) + LIST(APPEND COMMON_DEFINE "-DZEROMQ_ON") + MESSAGE("Select Option USED_ZMQ") +ENDIF () + +IF (VCPE_PPPOE) + LIST(APPEND COMMON_DEFINE "-DPPPOE_ON") + MESSAGE("Select Option VCPE_PPPOE") +ENDIF () + IF (USED_LWIP) LIST(APPEND COMMON_DEFINE "-DLWIP_ON") MESSAGE("Select Option USED_LWIP") diff --git a/srcs/CMakeLists.txt b/srcs/CMakeLists.txt index c337c5b..f26eeef 100644 --- a/srcs/CMakeLists.txt +++ b/srcs/CMakeLists.txt @@ -16,7 +16,7 @@ SET(CMAKE_C_STANDARD 99) FILE(GLOB VCPE_HEADS include/*.h include/uthash/*.h include/s2j/*.h ./httpserver/include/*.h ./httpserver/src/haywire/*.h ./httpserver/src/haywire/configuration/*.h) -IF (USED_LWIP) +IF (VCPE_PPPOE) AUX_SOURCE_DIRECTORY(pppoe VCPE_SRC) AUX_SOURCE_DIRECTORY(user VCPE_SRC) ENDIF () diff --git a/srcs/libs/CMakeLists.txt b/srcs/libs/CMakeLists.txt index faaf2cd..4ca2b34 100644 --- a/srcs/libs/CMakeLists.txt +++ b/srcs/libs/CMakeLists.txt @@ -17,7 +17,9 @@ AUX_SOURCE_DIRECTORY(banner C_SRC) AUX_SOURCE_DIRECTORY(configure C_SRC) AUX_SOURCE_DIRECTORY(network C_SRC) AUX_SOURCE_DIRECTORY(task C_SRC) -AUX_SOURCE_DIRECTORY(mq C_SRC) +IF (USED_ZMQ) + AUX_SOURCE_DIRECTORY(mq C_SRC) +ENDIF () AUX_SOURCE_DIRECTORY(cmdline C_SRC) AUX_SOURCE_DIRECTORY(crypto C_SRC) AUX_SOURCE_DIRECTORY(hardware C_SRC) diff --git a/srcs/libs/configure/config.c b/srcs/libs/configure/config.c index 2500260..bd68487 100644 --- a/srcs/libs/configure/config.c +++ b/srcs/libs/configure/config.c @@ -98,10 +98,12 @@ static CFG_ITEM g_cfgItem[] = { DEF_CFG_ITEM(CFG_DB_SQLITE_DB_NAME, "database.sqlite_dbname", VAL_STR, "", "SQLite3 database file name"), DEF_CFG_ITEM(CFG_DB_SQLITE_PASSWD, "database.sqlite_passwd", VAL_STR, ".main", "SQLite3 database password"), #endif +#ifdef ZEROMQ_ON /* 消息队列相配置 */ /* ZeroMq配置 */ DEF_CFG_ITEM(CFG_MQ_SVR_PORT, "zero_mq.svr_port", VAL_INT, "6278", "ZeroMQ server port"), DEF_CFG_ITEM(CFG_MQ_DATA_PATH, "zero_mq.agent_addr", VAL_STR, "ipc:///tmp/msg_fifo0", "ZeroMQ Agent data path"), +#endif /* vxLan 隧道配置 */ DEF_CFG_ITEM(CFG_VXLAN_NIC_NAME, "vxlan_wan.nic", VAL_STR, "", "Network card name to send data"), DEF_CFG_ITEM(CFG_VXLAN_SUPPORT, "vxlan_wan.enable", VAL_BOOL, "1", "Is support vxLan tune"), diff --git a/srcs/libs/configure/config_help.c b/srcs/libs/configure/config_help.c index 218229c..49670a7 100644 --- a/srcs/libs/configure/config_help.c +++ b/srcs/libs/configure/config_help.c @@ -183,10 +183,12 @@ const char *cfg_get_mysql_database() { } #endif +#ifdef ZEROMQ_ON int cfg_get_zero_mq_port() { return (unsigned short)cfg_get_integral_value(CFG_MQ_SVR_PORT); } const char *cfg_get_zero_mq_data_path() { return cfg_get_string_value(CFG_MQ_DATA_PATH); -} \ No newline at end of file +} +#endif \ No newline at end of file diff --git a/srcs/libs/hardware/cpu.c b/srcs/libs/hardware/cpu.c index ae080ef..9502d91 100644 --- a/srcs/libs/hardware/cpu.c +++ b/srcs/libs/hardware/cpu.c @@ -20,7 +20,7 @@ typedef struct { #define CMD_CPU_CORE ("cat /proc/cpuinfo| grep \"cpu cores\" | uniq | awk \'{print $4}\'") #define CMP_CPU_LOGIC_CORE ("cat /proc/cpuinfo| grep \"cpu cores\" | wc -l") -#define SUM_CPU_TIME(x) ((x).user + (x).nice + (x).sys + (x).idle + (x).irq) +#define SUM_CPU_TIME(x) ((x).user + (x).nice + (x).sys + (x).idle + (x).irq) static int g_isInit = FALSE; static CPU_INFO g_cpuInfo; @@ -103,7 +103,7 @@ static int get_cpu_time_info(unsigned long *pTotal, unsigned long *pIdle) { *pTotal = *pIdle = 0; for (i = 0; i < nCpu; i++) { - *pIdle += pCpu[i].cpu_times.idle; + *pIdle += pCpu[i].cpu_times.idle; *pTotal += SUM_CPU_TIME(pCpu[i].cpu_times); } @@ -135,25 +135,7 @@ static void cpuUsedRefresh() { } } -_Noreturn void cpuCalcCb(void *UNUSED(pArg)) { - do { - unsigned int period = cfg_get_cpu_refresh_period(); - - if (cfg_get_watch_cpu()) { - cpuUsedRefresh(); - } - - if (period < REFRESH_MAX_PERIOD && period >= 1) { - uv_sleep(1000 * period); - } else { - uv_sleep(1000); - } - } while (TRUE); -} - int cpu_watch_init() { - static uv_thread_t uvThread; - memset(&g_cpuInfo, 0, sizeof(CPU_INFO)); g_cpuInfo.nCpus = get_cpu_number(); @@ -163,9 +145,6 @@ int cpu_watch_init() { get_cpu_desc(&g_cpuInfo.cpuCoreDesc); g_isInit = TRUE; - - uv_thread_create(&uvThread, cpuCalcCb, NULL); - return ERR_SUCCESS; } @@ -184,6 +163,8 @@ int get_cpu_info(PCPU_INFO pInfo) { get_cpu_desc(&g_cpuInfo.cpuCoreDesc); g_isInit = TRUE; + } else if (cfg_get_watch_cpu()) { + cpuUsedRefresh(); } memcpy(pInfo, &g_cpuInfo, sizeof(CPU_INFO)); diff --git a/srcs/libs/hardware/disk.c b/srcs/libs/hardware/disk.c index c5fee99..da61056 100644 --- a/srcs/libs/hardware/disk.c +++ b/srcs/libs/hardware/disk.c @@ -12,7 +12,7 @@ #include "uthash/uthash.h" #include "config.h" -#define MAX_SIZE_LEN (16) +#define MAX_SIZE_LEN (16) //#define CMD_DISK_FILESYSTEM ("df -h | awk \'{if (NR > 1){print $0}}\'") #define CMD_DISK_FILESYSTEM ("df -h | awk \'{if (NR > 1){print $1\"|\"$2\"|\"$3\"|\"$4\"|\"$5\"|\"$6}}\'") @@ -105,6 +105,10 @@ int get_disk_info(PDISK_INFO pInfo) { return -ERR_INPUT_PARAMS; } + if (cfg_get_watch_disk()) { + disk_info_refresh(); + } + uv_rwlock_rdlock(&g_uvLock); pInfo->nItems = HASH_COUNT(g_diskPartInfo); pInfo->timestamp = time(NULL); @@ -122,32 +126,12 @@ int get_disk_info(PDISK_INFO pInfo) { return ERR_SUCCESS; } -_Noreturn void diskRefreshCb(void *UNUSED(pArg)) { - do { - unsigned int period = cfg_get_disk_refresh_period(); - - if (cfg_get_watch_disk()) { - disk_info_refresh(); - } - - if (period < REFRESH_MAX_PERIOD && period >= 1) { - uv_sleep(1000 * period); - } else { - uv_sleep(1000); - } - } while (TRUE); -} - int disk_watch_info() { - static uv_thread_t uvThread; - uv_rwlock_init(&g_uvLock); if (disk_info_refresh() != ERR_SUCCESS) { return -ERR_SYS_DISK_GET_INFO; } - uv_thread_create(&uvThread, diskRefreshCb, NULL); - return ERR_SUCCESS; } \ No newline at end of file diff --git a/srcs/libs/hardware/hardware.c b/srcs/libs/hardware/hardware.c index cafa018..6b941b9 100644 --- a/srcs/libs/hardware/hardware.c +++ b/srcs/libs/hardware/hardware.c @@ -9,51 +9,48 @@ #include "user_errno.h" #include "config.h" #include "misc.h" +#include "task_manager.h" static HARDWARE_INFO g_hardwareInfo; -_Noreturn void hardwareRefreshCb(void *UNUSED(pArg)) { - do { - unsigned int period = cfg_get_hardware_refresh_period(); +void hardwareRefreshCb(uv_timer_t *UNUSED(pArg)) { + if (cfg_get_watch_sensor()) { + get_sensor_info(&g_hardwareInfo.sensorInfo); + } + if (cfg_get_watch_disk()) { + get_disk_info(&g_hardwareInfo.diskInfo); + } + if (cfg_get_watch_cpu()) { + get_cpu_info(&g_hardwareInfo.cpuInfo); + } + if (cfg_get_watch_memory()) { + get_memory_info(&g_hardwareInfo.memInfo); + } - if (cfg_get_watch_sensor()) { - get_sensor_info(&g_hardwareInfo.sensorInfo); - } - - if (cfg_get_watch_disk()) { - get_disk_info(&g_hardwareInfo.diskInfo); - } - - if (cfg_get_watch_cpu()) { - get_cpu_info(&g_hardwareInfo.cpuInfo); - } - - if (cfg_get_watch_memory()) { - get_memory_info(&g_hardwareInfo.memInfo); - } - - if (period < REFRESH_MAX_PERIOD && period >= 1) { - uv_sleep(1000 * period); - } else { - uv_sleep(1000); - } - - uv_sleep(1000); - } while (TRUE); + printf("%s\n", get_hardware_json()); } int init_hardware() { - static uv_thread_t uvThread; + static uv_timer_t uvTm; + unsigned int period = cfg_get_hardware_refresh_period(); + + if (period >= REFRESH_MAX_PERIOD) { + period = 10000; + } else { + period *= 1000; + } + + uv_timer_init(get_task_manager(), &uvTm); memset(&g_hardwareInfo, 0, sizeof(HARDWARE_INFO)); - uv_thread_create(&uvThread, hardwareRefreshCb, NULL); - cpu_watch_init(); memory_watch_init(); disk_watch_info(); sensor_watch_init(); + uv_timer_start(&uvTm, hardwareRefreshCb, period, period); + return ERR_SUCCESS; } diff --git a/srcs/libs/hardware/ipmltools.c b/srcs/libs/hardware/ipmltools.c index 57bb077..13b84b3 100644 --- a/srcs/libs/hardware/ipmltools.c +++ b/srcs/libs/hardware/ipmltools.c @@ -12,10 +12,11 @@ #include "misc.h" #include "config.h" -#define MAX_SENSOR_STR (32) +#define MAX_SENSOR_STR (32) //#define CMD_SENSOR_INFO ("ipmitool sensor | awk '{gsub(/\|/, \"\"); print $0}'") -#define CMD_SENSOR_INFO ("ipmitool sensor") +#define CMD_SENSOR_INFO ("ipmitool sensor") +#define CMD_IPMITOOL_ENABLE ("whereis ipmitool | awk '{print $2}'") typedef struct { char name[MAX_SENSOR_STR]; ///< 传感器名称 @@ -42,9 +43,16 @@ static int sensor_info_refresh() { int errCode = ERR_SUCCESS; FILE *fp; char buf[1024]; + char *pRet = NULL; + + if (shell_with_output(CMD_IPMITOOL_ENABLE, &pRet) != ERR_SUCCESS || pRet == NULL || strlen(pRet) == 0) { + if (pRet) { + free(pRet); + } + return -ERR_ITEM_UNEXISTS; + } fp = popen(CMD_SENSOR_INFO, "r"); - if (fp == NULL) { return -ERR_OPEN_FILE; } @@ -132,6 +140,10 @@ int get_sensor_info(PSENSOR_INFO pInfo) { return -ERR_INPUT_PARAMS; } + if (cfg_get_watch_sensor()) { + sensor_info_refresh(); + } + uv_rwlock_rdlock(&g_uvLock); pInfo->nItems = HASH_COUNT(g_pSensorInfo); pInfo->timestamp = time(NULL); @@ -149,27 +161,9 @@ int get_sensor_info(PSENSOR_INFO pInfo) { return ERR_SUCCESS; } -_Noreturn void sensorRefreshCb(void *UNUSED(pArg)) { - do { - unsigned int period = cfg_get_sensor_refresh_period(); - - if (cfg_get_watch_sensor()) { - sensor_info_refresh(); - } - - if (period < REFRESH_MAX_PERIOD && period >= 1) { - uv_sleep(1000 * period); - } else { - uv_sleep(1000); - } - } while (TRUE); -} - int sensor_watch_init() { - - static uv_thread_t uvThread; - int i, devOk = FALSE; - int n = ARRAY_SIZE(g_ipmiDev); + int i, devOk = FALSE; + int n = ARRAY_SIZE(g_ipmiDev); uv_rwlock_init(&g_uvLock); @@ -188,7 +182,5 @@ int sensor_watch_init() { return -ERR_SYS_SENSOR_GET_INFO; } - uv_thread_create(&uvThread, sensorRefreshCb, NULL); - return ERR_SUCCESS; } \ No newline at end of file diff --git a/srcs/libs/hardware/memory.c b/srcs/libs/hardware/memory.c index 5d5cc9a..e777a1a 100644 --- a/srcs/libs/hardware/memory.c +++ b/srcs/libs/hardware/memory.c @@ -14,11 +14,11 @@ #define CMD_MEMORY_INFO ("cat /proc/meminfo | awk \'{print $1\"|\"$2\"|\"$3}\'") -#define M_SIZE (1024 * 1024) +#define M_SIZE (1024 * 1024) -#define KEY_NAME (32) -#define KEY_VALUE (32) -#define VALUE_UNIT (8) +#define KEY_NAME (32) +#define KEY_VALUE (32) +#define VALUE_UNIT (8) typedef struct { char itemName[KEY_NAME]; @@ -95,33 +95,13 @@ static int memory_info_refresh() { return errCode; } -_Noreturn void memRefreshCb(void *UNUSED(pArg)) { - do { - unsigned int period = cfg_get_mem_refresh_period(); - - if (cfg_get_watch_memory()) { - memory_info_refresh(); - } - - if (period < REFRESH_MAX_PERIOD && period >= 1) { - uv_sleep(1000 * period); - } else { - uv_sleep(1000); - } - } while (TRUE); -} - int memory_watch_init() { - static uv_thread_t uvThread; - uv_rwlock_init(&g_uvLock); if (memory_info_refresh() != ERR_SUCCESS) { return -ERR_SYS_DISK_GET_INFO; } - uv_thread_create(&uvThread, memRefreshCb, NULL); - return ERR_SUCCESS; } @@ -142,6 +122,10 @@ int get_memory_info(PMEMORY_INFO pInfo) { return -ERR_INPUT_PARAMS; } + if (cfg_get_watch_memory()) { + memory_info_refresh(); + } + uv_rwlock_rdlock(&g_uvLock); for (i = 0; i < n; i++) { diff --git a/srcs/libs/include/config.h b/srcs/libs/include/config.h index 8d8f40b..1730a78 100644 --- a/srcs/libs/include/config.h +++ b/srcs/libs/include/config.h @@ -61,8 +61,10 @@ typedef enum { CFG_DB_SQLITE_DB_NAME, CFG_DB_SQLITE_PASSWD, #endif +#ifdef ZEROMQ_ON CFG_MQ_SVR_PORT, CFG_MQ_DATA_PATH, +#endif CFG_VXLAN_NIC_NAME, CFG_VXLAN_SUPPORT, CFG_VXLAN_PEER_IP, diff --git a/srcs/libs/init/init_runtime.c b/srcs/libs/init/init_runtime.c index 19660fe..a72dd56 100644 --- a/srcs/libs/init/init_runtime.c +++ b/srcs/libs/init/init_runtime.c @@ -134,7 +134,7 @@ int user_init(const char *pAppCfgFile, const char *pCfgDirectory, const char *pK if (cfg_get_hardware_watch_enable()) { init_hardware(); } - +#ifdef ZEROMQ_ON if ((ret = mq_init()) != ERR_SUCCESS) { LOG_MOD(error, ZLOG_MOD_INIT, "Message queue init error: %d\n", ret); } @@ -142,6 +142,7 @@ int user_init(const char *pAppCfgFile, const char *pCfgDirectory, const char *pK if ((ret = mq_data_init()) != ERR_SUCCESS) { LOG_MOD(error, ZLOG_MOD_INIT, "Message queue init error: %d\n", ret); } +#endif #ifdef HTTPSERVER_ON http_svr_init(); #endif @@ -156,7 +157,9 @@ void user_uninit() { #ifdef HTTPSERVER_ON http_svr_uinit(); #endif +#ifdef ZEROMQ_ON mq_uninit(); +#endif zlog_fini(); uninit_config_system(); uv_loop_close(get_task_manager());