From 2b75a01146165ac28d7941187255e8818cfeb031 Mon Sep 17 00:00:00 2001 From: wangmenglan Date: Mon, 27 Feb 2023 14:30:37 +0800 Subject: [PATCH] =?UTF-8?q?TSG-13912=20=E6=94=AF=E6=8C=81=E5=AF=B9?= =?UTF-8?q?=E7=AC=AC=E4=B8=89=E6=96=B9=E8=AE=BE=E5=A4=87=E5=81=A5=E5=BA=B7?= =?UTF-8?q?=E6=A3=80=E6=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit bfdd程序重启后, 支持将配置重新下发给bfdd程序 --- common/include/bfd.h | 2 ++ common/src/bfd.cpp | 17 +++++++++++------ platform/src/health_check.cpp | 35 +++++++++++++++++++++++++++++++++-- 3 files changed, 46 insertions(+), 8 deletions(-) diff --git a/common/include/bfd.h b/common/include/bfd.h index fc0cfc1..204244a 100644 --- a/common/include/bfd.h +++ b/common/include/bfd.h @@ -14,6 +14,8 @@ struct bfd_vtysh_client { // exec after connect server int (*pre_config)(struct bfd_vtysh_client *); + // recover config after connect server + int (*recover_config)(struct bfd_vtysh_client *); }; int bfd_vtysh_connect(struct bfd_vtysh_client *client); diff --git a/common/src/bfd.cpp b/common/src/bfd.cpp index 4b4c426..3bfe13c 100644 --- a/common/src/bfd.cpp +++ b/common/src/bfd.cpp @@ -63,6 +63,12 @@ int bfd_vtysh_connect(struct bfd_vtysh_client *client) bfd_vtysh_close(client); } + if (client->recover_config) { + ret = client->recover_config(client); + if (ret < 0) + bfd_vtysh_close(client); + } + LOG_DEBUG("bfd vtysh_connect(%d): succ", client->fd); return 0; } @@ -71,7 +77,7 @@ int bfd_vtysh_connect(struct bfd_vtysh_client *client) // return -1 : fail void bfd_vtysh_close(struct bfd_vtysh_client *client) { - if (client->fd >= 0) { + if (client->fd > 0) { close(client->fd); client->fd = -1; } @@ -147,9 +153,6 @@ static int bfd_vtysh_cmd_exec(struct bfd_vtysh_client *client, const char *cmd, if (ret < 0) goto out_err; - // if (data == NULL) - // return 0; - bufvalid = buf; do { ssize_t nread; @@ -277,7 +280,6 @@ int bfd_vtysh_del_dev(struct bfd_vtysh_client *client, const char *peer_addr) // return 1 : up // return 0 : down -// return -1 : fail int bfd_vtysh_get_dev_active(struct bfd_vtysh_client *client, char *addr) { int ret = 0; @@ -285,6 +287,9 @@ int bfd_vtysh_get_dev_active(struct bfd_vtysh_client *client, char *addr) char cmd[256] = {0}; char stackbuf[4096]; + if (client->fd <= 0) + return -1; + snprintf(cmd, sizeof(cmd), "do show bfd peer %s", addr); ret = bfd_vtysh_cmd_exec(client, cmd, stackbuf, sizeof(stackbuf)-1); if (ret < 0) { @@ -294,7 +299,7 @@ int bfd_vtysh_get_dev_active(struct bfd_vtysh_client *client, char *addr) // ex: Status: up/Status: down if ((p = strstr(stackbuf, "Status: ")) == NULL) - return -1; + return 0; if (strncmp(p+8, "up", 2) == 0) return 1; return 0; diff --git a/platform/src/health_check.cpp b/platform/src/health_check.cpp index 3f3647f..8e9bfa8 100644 --- a/platform/src/health_check.cpp +++ b/platform/src/health_check.cpp @@ -80,6 +80,22 @@ void health_check_session_init(const char *profile) health_check_session_foreach(); } +static int health_check_session_recover_cfg(struct bfd_vtysh_client *client) +{ + int ret = 0; + struct session_iterm *tmp = NULL; + struct session_iterm *node = NULL; + HASH_ITER(hh1, g_handle.root_by_id, node, tmp) { + if (node->policy.method != HEALTH_CHECK_METHOD_BFD) + continue; + + ret = bfd_vtysh_add_dev(client, node->policy.address, node->policy.retires, node->policy.interval_ms); + if (ret != 0) + return -1; + } + return 0; +} + static void health_check_session_init_bfd_client(struct bfd_vtysh_client *client) { memset(client, 0, sizeof(*client)); @@ -87,6 +103,14 @@ static void health_check_session_init_bfd_client(struct bfd_vtysh_client *client client->pre_config = bfd_vtysh_pre_config; } +static void health_check_session_recover_bfd(struct bfd_vtysh_client *client) +{ + memset(client, 0, sizeof(*client)); + snprintf(client->path, sizeof(client->path), path); + client->pre_config = bfd_vtysh_pre_config; + client->recover_config = health_check_session_recover_cfg; +} + // return 0 : success // return -1 : key exist // struct health_check *policy : need deep copy @@ -264,8 +288,15 @@ static void *_health_check_session_foreach(void *arg) continue; is_active = bfd_vtysh_get_dev_active(&client, node->policy.address); - if (is_active == -1) - continue; + if (is_active == -1) { + bfd_vtysh_close(&client); + health_check_session_recover_bfd(&client); + bfd_vtysh_connect(&client); + is_active = bfd_vtysh_get_dev_active(&client, node->policy.address); + if (is_active == -1) + is_active = 0; + } + if (node->is_active != is_active) { node->is_active = is_active; if (node->is_active == 1) {