]> git.proxmox.com Git - mirror_frr.git/commitdiff
bgpd: store the bgp as identifier in the configured as-notation
authorPhilippe Guibert <philippe.guibert@6wind.com>
Tue, 15 Nov 2022 15:42:40 +0000 (16:42 +0100)
committerPhilippe Guibert <philippe.guibert@6wind.com>
Fri, 10 Feb 2023 09:19:06 +0000 (10:19 +0100)
This is a preliminary work to handle various ways to configure
a BGP Autonomous System. When creating a BGP instance, the
user may want to define the AS number as a dotted value,
instead of using an integer value.

To handle both cases, an as_pretty char attribute will store
the as number as it has been given to the vtysh command:

router bgp <as number>

Whenever the as integer of the BGP instance was dumped,
the as_pretty original format is used.

The json output reuses the integer value to keep backward
compatibility with old displays.

Signed-off-by: Philippe Guibert <philippe.guibert@6wind.com>
bgpd/bgp_evpn.c
bgpd/bgp_vty.c
bgpd/bgp_vty.h
bgpd/bgpd.c
bgpd/bgpd.h
tests/bgpd/test_capability.c
tests/bgpd/test_mp_attr.c
tests/bgpd/test_packet.c

index d8acbcd19a302ddb99d1ef7851cfe9457125f998..6ccd64dba4670c7221c63522364e9dc8e3112ec4 100644 (file)
@@ -6252,13 +6252,14 @@ int bgp_evpn_local_l3vni_add(vni_t l3vni, vrf_id_t vrf_id,
 
                ret = bgp_get_vty(&bgp_vrf, &as, vrf_id_to_name(vrf_id),
                                  vrf_id == VRF_DEFAULT
-                                 ? BGP_INSTANCE_TYPE_DEFAULT
-                                 : BGP_INSTANCE_TYPE_VRF);
+                                         ? BGP_INSTANCE_TYPE_DEFAULT
+                                         : BGP_INSTANCE_TYPE_VRF,
+                                 NULL);
                switch (ret) {
                case BGP_ERR_AS_MISMATCH:
                        flog_err(EC_BGP_EVPN_AS_MISMATCH,
-                                "BGP instance is already running; AS is %u",
-                                as);
+                                "BGP instance is already running; AS is %s",
+                                bgp_vrf->as_pretty);
                        return -1;
                case BGP_ERR_INSTANCE_MISMATCH:
                        flog_err(EC_BGP_EVPN_INSTANCE_MISMATCH,
index daa435bb2d5c8281ebc3345fafbaf08681456b40..6023d9811d49c99ce38565e71a054d042187a4c5 100644 (file)
@@ -594,9 +594,9 @@ static const char *get_bgp_default_af_flag(afi_t afi, safi_t safi)
 }
 
 int bgp_get_vty(struct bgp **bgp, as_t *as, const char *name,
-               enum bgp_instance_type inst_type)
+               enum bgp_instance_type inst_type, const char *as_pretty)
 {
-       int ret = bgp_get(bgp, as, name, inst_type);
+       int ret = bgp_get(bgp, as, name, inst_type, as_pretty);
 
        if (ret == BGP_CREATED) {
                bgp_timers_set(*bgp, DFLT_BGP_KEEPALIVE, DFLT_BGP_HOLDTIME,
@@ -1531,17 +1531,19 @@ DEFUN_NOSH (router_bgp,
                if (inst_type == BGP_INSTANCE_TYPE_DEFAULT)
                        is_new_bgp = (bgp_lookup(as, name) == NULL);
 
-               ret = bgp_get_vty(&bgp, &as, name, inst_type);
+               ret = bgp_get_vty(&bgp, &as, name, inst_type,
+                                 argv[idx_asn]->arg);
                switch (ret) {
                case BGP_ERR_AS_MISMATCH:
-                       vty_out(vty, "BGP is already running; AS is %u\n", as);
+                       vty_out(vty, "BGP is already running; AS is %s\n",
+                               bgp->as_pretty);
                        return CMD_WARNING_CONFIG_FAILED;
                case BGP_ERR_INSTANCE_MISMATCH:
                        vty_out(vty,
                                "BGP instance name and AS number mismatch\n");
                        vty_out(vty,
-                               "BGP instance is already running; AS is %u\n",
-                               as);
+                               "BGP instance is already running; AS is %s\n",
+                               bgp->as_pretty);
                        return CMD_WARNING_CONFIG_FAILED;
                }
 
@@ -9486,7 +9488,7 @@ DEFPY(af_import_vrf_route_map, af_import_vrf_route_map_cmd,
 
                /* Auto-create assuming the same AS */
                ret = bgp_get_vty(&bgp_default, &as, NULL,
-                                 BGP_INSTANCE_TYPE_DEFAULT);
+                                 BGP_INSTANCE_TYPE_DEFAULT, NULL);
 
                if (ret) {
                        vty_out(vty,
@@ -9598,7 +9600,7 @@ DEFPY(bgp_imexport_vrf, bgp_imexport_vrf_cmd,
        if (!bgp_default) {
                /* Auto-create assuming the same AS */
                ret = bgp_get_vty(&bgp_default, &as, NULL,
-                                 BGP_INSTANCE_TYPE_DEFAULT);
+                                 BGP_INSTANCE_TYPE_DEFAULT, NULL);
 
                if (ret) {
                        vty_out(vty,
@@ -9613,7 +9615,7 @@ DEFPY(bgp_imexport_vrf, bgp_imexport_vrf_cmd,
                        vrf_bgp = bgp_default;
                else
                        /* Auto-create assuming the same AS */
-                       ret = bgp_get_vty(&vrf_bgp, &as, import_name, bgp_type);
+                       ret = bgp_get_vty(&vrf_bgp, &as, import_name, bgp_type, NULL);
 
                if (ret) {
                        vty_out(vty,
@@ -10293,8 +10295,8 @@ DEFUN (show_bgp_views,
                /* Skip VRFs. */
                if (bgp->inst_type == BGP_INSTANCE_TYPE_VRF)
                        continue;
-               vty_out(vty, "\t%s (AS%u)\n", bgp->name ? bgp->name : "(null)",
-                       bgp->as);
+               vty_out(vty, "\t%s (AS%s)\n", bgp->name ? bgp->name : "(null)",
+                       bgp->as_pretty);
        }
 
        return CMD_SUCCESS;
@@ -11085,8 +11087,8 @@ static int bgp_show_summary(struct vty *vty, struct bgp *bgp, int afi, int safi,
                                                : bgp->name);
                        } else {
                                vty_out(vty,
-                                       "BGP router identifier %pI4, local AS number %u vrf-id %d",
-                                       &bgp->router_id, bgp->as,
+                                       "BGP router identifier %pI4, local AS number %s vrf-id %d",
+                                       &bgp->router_id, bgp->as_pretty,
                                        bgp->vrf_id == VRF_UNKNOWN
                                                ? -1
                                                : (int)bgp->vrf_id);
@@ -15917,8 +15919,8 @@ static int bgp_show_one_peer_group(struct vty *vty, struct peer_group *group,
                        json_object_int_add(json_peer_group, "remoteAs",
                                            group->bgp->as);
                else
-                       vty_out(vty, "\nBGP peer-group %s, remote AS %u\n",
-                               group->name, group->bgp->as);
+                       vty_out(vty, "\nBGP peer-group %s, remote AS %s\n",
+                               group->name, group->bgp->as_pretty);
        } else {
                if (!json)
                        vty_out(vty, "\nBGP peer-group %s\n", group->name);
@@ -17944,7 +17946,7 @@ int bgp_config_write(struct vty *vty)
                        continue;
 
                /* Router bgp ASN */
-               vty_out(vty, "router bgp %u", bgp->as);
+               vty_out(vty, "router bgp %s", bgp->as_pretty);
 
                if (bgp->name)
                        vty_out(vty, " %s %s",
index 019789dff84a8e86a54efae7c3f487d6c99dece6..8ed7c965e4f5be9d7450a4a983bebd63db86e8e3 100644 (file)
@@ -150,7 +150,7 @@ extern void bgp_vty_init(void);
 extern void community_alias_vty(void);
 extern const char *get_afi_safi_str(afi_t afi, safi_t safi, bool for_json);
 extern int bgp_get_vty(struct bgp **bgp, as_t *as, const char *name,
-                      enum bgp_instance_type inst_type);
+                      enum bgp_instance_type inst_type, const char *as_pretty);
 extern void bgp_config_write_update_delay(struct vty *vty, struct bgp *bgp);
 extern void bgp_config_write_wpkt_quanta(struct vty *vty, struct bgp *bgp);
 extern void bgp_config_write_rpkt_quanta(struct vty *vty, struct bgp *bgp);
index 39010e76f9d1bd164336aa29564af93b72fe9221..9fca97521204053a4591d7653672460c17461e69 100644 (file)
@@ -3189,23 +3189,27 @@ static void bgp_vrf_string_name_delete(void *data)
 
 /* BGP instance creation by `router bgp' commands. */
 static struct bgp *bgp_create(as_t *as, const char *name,
-                             enum bgp_instance_type inst_type)
+                             enum bgp_instance_type inst_type,
+                             const char *as_pretty)
 {
        struct bgp *bgp;
        afi_t afi;
        safi_t safi;
 
        bgp = XCALLOC(MTYPE_BGP, sizeof(struct bgp));
-
+       bgp->as = *as;
+       if (as_pretty)
+               bgp->as_pretty = XSTRDUP(MTYPE_BGP, as_pretty);
        if (BGP_DEBUG(zebra, ZEBRA)) {
                if (inst_type == BGP_INSTANCE_TYPE_DEFAULT)
-                       zlog_debug("Creating Default VRF, AS %u", *as);
+                       zlog_debug("Creating Default VRF, AS %s",
+                                  bgp->as_pretty);
                else
-                       zlog_debug("Creating %s %s, AS %u",
+                       zlog_debug("Creating %s %s, AS %s",
                                   (inst_type == BGP_INSTANCE_TYPE_VRF)
                                           ? "VRF"
                                           : "VIEW",
-                                  name, *as);
+                                  name, bgp->as_pretty);
        }
 
        /* Default the EVPN VRF to the default one */
@@ -3282,7 +3286,6 @@ static struct bgp *bgp_create(as_t *as, const char *name,
        bgp->condition_check_period = DEFAULT_CONDITIONAL_ROUTES_POLL_TIME;
        bgp_addpath_init_bgp_data(&bgp->tx_addpath);
        bgp->fast_convergence = false;
-       bgp->as = *as;
        bgp->llgr_stale_time = BGP_DEFAULT_LLGR_STALE_TIME;
 
 #ifdef ENABLE_BGP_VNC
@@ -3519,7 +3522,7 @@ int bgp_lookup_by_as_name_type(struct bgp **bgp_val, as_t *as, const char *name,
 
 /* Called from VTY commands. */
 int bgp_get(struct bgp **bgp_val, as_t *as, const char *name,
-           enum bgp_instance_type inst_type)
+           enum bgp_instance_type inst_type, const char *as_pretty)
 {
        struct bgp *bgp;
        struct vrf *vrf = NULL;
@@ -3529,7 +3532,7 @@ int bgp_get(struct bgp **bgp_val, as_t *as, const char *name,
        if (ret || *bgp_val)
                return ret;
 
-       bgp = bgp_create(as, name, inst_type);
+       bgp = bgp_create(as, name, inst_type, as_pretty);
 
        /*
         * view instances will never work inside of a vrf
@@ -3925,6 +3928,7 @@ void bgp_free(struct bgp *bgp)
                        ecommunity_free(&bgp->vpn_policy[afi].rtlist[dir]);
        }
 
+       XFREE(MTYPE_BGP, bgp->as_pretty);
        XFREE(MTYPE_BGP, bgp->name);
        XFREE(MTYPE_BGP, bgp->name_pretty);
        XFREE(MTYPE_BGP, bgp->snmp_stats);
index 61119ab6e0b96370836ec386cff57630f968873c..a0627af75192f70db1c8d1d003d93920316f9427 100644 (file)
@@ -343,6 +343,7 @@ struct bgp_srv6_function {
 struct bgp {
        /* AS number of this BGP instance.  */
        as_t as;
+       char *as_pretty;
 
        /* Name of this BGP instance.  */
        char *name;
@@ -2161,7 +2162,8 @@ extern void bgp_option_norib_set_runtime(void);
 /* unset the bgp no-rib option during runtime and reset all peers */
 extern void bgp_option_norib_unset_runtime(void);
 
-extern int bgp_get(struct bgp **, as_t *, const char *, enum bgp_instance_type);
+extern int bgp_get(struct bgp **bgp, as_t *as, const char *name,
+                  enum bgp_instance_type kind, const char *as_pretty);
 extern void bgp_instance_up(struct bgp *);
 extern void bgp_instance_down(struct bgp *);
 extern int bgp_delete(struct bgp *);
index 51792825da94d4bce8a268d201aab5af04798c27..5a63758d060e758a9ffaf810e6127af9d047ff0f 100644 (file)
@@ -952,7 +952,8 @@ int main(void)
        if (fileno(stdout) >= 0)
                tty = isatty(fileno(stdout));
 
-       if (bgp_get(&bgp, &asn, NULL, BGP_INSTANCE_TYPE_DEFAULT) < 0)
+       if (bgp_get(&bgp, &asn, NULL, BGP_INSTANCE_TYPE_DEFAULT,
+                   NULL) < 0)
                return -1;
 
        peer = peer_create_accept(bgp);
index c5ce5d3cd2c01872392c79ee859eacf16f4b1269..3f6401a2a3d60d13f1f36f1145240b2dc1a963d8 100644 (file)
@@ -1094,7 +1094,8 @@ int main(void)
        if (fileno(stdout) >= 0)
                tty = isatty(fileno(stdout));
 
-       if (bgp_get(&bgp, &asn, NULL, BGP_INSTANCE_TYPE_DEFAULT) < 0)
+       if (bgp_get(&bgp, &asn, NULL, BGP_INSTANCE_TYPE_DEFAULT,
+                   NULL) < 0)
                return -1;
 
        peer = peer_create_accept(bgp);
index 2ce8b561bbb14b88255dc55ca85918f5d2f2e34a..cf91f5570f94586bf0f0a0198d2e163c0f3e7b3e 100644 (file)
@@ -63,7 +63,8 @@ int main(int argc, char *argv[])
        vrf_init(NULL, NULL, NULL, NULL);
        bgp_option_set(BGP_OPT_NO_LISTEN);
 
-       if (bgp_get(&bgp, &asn, NULL, BGP_INSTANCE_TYPE_DEFAULT) < 0)
+       if (bgp_get(&bgp, &asn, NULL, BGP_INSTANCE_TYPE_DEFAULT,
+                   NULL) < 0)
                return -1;
 
        peer = peer_create_accept(bgp);