From 30aaf9820dae6c4dd1657ac1b92f214560765cd6 Mon Sep 17 00:00:00 2001 From: Igor Ryzhov Date: Fri, 14 May 2021 01:37:38 +0300 Subject: [PATCH] Revert "bgpd: simplify bgp_global_local_as_modify code" This reverts commit debb7b1931cdf5b3e06ca244bf09bb009b5a8677. --- bgpd/bgp_nb_config.c | 58 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 48 insertions(+), 10 deletions(-) diff --git a/bgpd/bgp_nb_config.c b/bgpd/bgp_nb_config.c index 7573c40cfa..887d6311f4 100644 --- a/bgpd/bgp_nb_config.c +++ b/bgpd/bgp_nb_config.c @@ -258,26 +258,64 @@ int bgp_router_destroy(struct nb_cb_destroy_args *args) int bgp_global_local_as_modify(struct nb_cb_modify_args *args) { struct bgp *bgp; + as_t as; + const struct lyd_node *vrf_dnode; + const char *vrf_name; + const char *name = NULL; + enum bgp_instance_type inst_type; + int ret; + bool is_view_inst = false; switch (args->event) { case NB_EV_VALIDATE: - /* - * Changing AS number is not allowed, but we must allow it - * once, when the BGP instance is created the first time. - * If the instance already exists - return the validation - * error. - */ - bgp = nb_running_get_entry_non_rec( - lyd_parent(lyd_parent(args->dnode)), NULL, false); - if (bgp) { + as = yang_dnode_get_uint32(args->dnode, NULL); + + inst_type = BGP_INSTANCE_TYPE_DEFAULT; + + vrf_dnode = yang_dnode_get_parent(args->dnode, + "control-plane-protocol"); + vrf_name = yang_dnode_get_string(vrf_dnode, "./vrf"); + + if (strmatch(vrf_name, VRF_DEFAULT_NAME)) { + name = NULL; + } else { + name = vrf_name; + inst_type = BGP_INSTANCE_TYPE_VRF; + } + + is_view_inst = yang_dnode_get_bool(args->dnode, + "../instance-type-view"); + if (is_view_inst) + inst_type = BGP_INSTANCE_TYPE_VIEW; + + ret = bgp_lookup_by_as_name_type(&bgp, &as, name, inst_type); + switch (ret) { + case BGP_ERR_AS_MISMATCH: snprintf(args->errmsg, args->errmsg_len, - "Changing AS number is not allowed"); + "BGP instance is already running; AS is %u", + as); + return NB_ERR_VALIDATION; + case BGP_ERR_INSTANCE_MISMATCH: + snprintf(args->errmsg, args->errmsg_len, + "BGP instance type mismatch"); return NB_ERR_VALIDATION; } break; case NB_EV_PREPARE: case NB_EV_ABORT: + return NB_OK; case NB_EV_APPLY: + /* NOTE: handled in bgp_global_create callback, the as change + * will be rejected in validate phase. + */ + as = yang_dnode_get_uint32(args->dnode, NULL); + bgp = nb_running_get_entry(args->dnode, NULL, true); + if (bgp->as != as) { + snprintf(args->errmsg, args->errmsg_len, + "BGP instance is already running; AS is %u", + bgp->as); + return NB_ERR_INCONSISTENCY; + } break; } -- 2.39.5