]> git.puffer.fish Git - matthieu/frr.git/commitdiff
bgpd: Fix `dampening flap-statistics json` crash
authorDonatas Abraitis <donatas.abraitis@gmail.com>
Thu, 15 Jul 2021 13:11:45 +0000 (16:11 +0300)
committermergify-bot <noreply@mergify.io>
Fri, 23 Jul 2021 09:20:58 +0000 (09:20 +0000)
With fix:
```
exit1-debian-9# sh ip bgp dampening flap-statistics
BGP table version is 22, local router ID is 10.10.10.200, vrf id 0
Default local pref 100, local AS 65001
Status codes:  s suppressed, d damped, h history, * valid, > best, = multipath,
               i internal, r RIB-failure, S Stale, R Removed
Nexthop codes: @NNN nexthop's vrf id, < announce-nh-self
Origin codes:  i - IGP, e - EGP, ? - incomplete
RPKI validation codes: V valid, I invalid, N Not found

   Network          From            Flaps Duration Reuse    Path
*d 10.0.0.0/24      192.168.0.2     4    00:00:50 00:35:56 65000 ?
*d 10.10.10.100/32  192.168.0.2     4    00:00:50 00:35:56 65000 ?
*d 192.168.1.0/24   192.168.0.2     4    00:00:50 00:35:56 65000 ?

Displayed  3 routes and 10 total paths
exit1-debian-9# sh ip bgp dampening flap-statistics json
{
 "vrfId": 0,
 "vrfName": "default",
 "tableVersion": 22,
 "routerId": "10.10.10.200",
 "defaultLocPrf": 100,
 "localAS": 65001,
 "routes": { "10.0.0.0/24": [
  {
    "valid":true,
    "damped":true,
    "pathFrom":"external",
    "peerHost":"192.168.0.2",
    "bdiFlap":4,
    "peerUptime":"00:00:54",
    "peerUptimeMsec":54000,
    "peerUptimeEstablishedEpoch":1626355135,
    "reuseTimerMsecs":2151000,
    "asPath":"65000",
    "origin":"?"
  }
],"10.10.10.100/32": [
  {
    "valid":true,
    "damped":true,
    "pathFrom":"external",
    "peerHost":"192.168.0.2",
    "bdiFlap":4,
    "peerUptime":"00:00:54",
    "peerUptimeMsec":54000,
    "peerUptimeEstablishedEpoch":1626355135,
    "reuseTimerMsecs":2151000,
    "asPath":"65000",
    "origin":"?"
  }
],"192.168.1.0/24": [
  {
    "valid":true,
    "damped":true,
    "pathFrom":"external",
    "peerHost":"192.168.0.2",
    "bdiFlap":4,
    "peerUptime":"00:00:54",
    "peerUptimeMsec":54000,
    "peerUptimeEstablishedEpoch":1626355135,
    "reuseTimerMsecs":2151000,
    "asPath":"65000",
    "origin":"?"
  }
] }  }
```

Signed-off-by: Donatas Abraitis <donatas.abraitis@gmail.com>
(cherry picked from commit 3125804678d3c0fc5b0b923e101ef58d4f454a4c)

bgpd/bgp_route.c

index 1d00c287605682acb82bdd7ef83a97ea042ab406..7a0e8adab0d040a08abeb8f4337e3b6fa830949d 100644 (file)
@@ -9234,97 +9234,85 @@ static void damp_route_vty_out(struct vty *vty, const struct prefix *p,
 static void flap_route_vty_out(struct vty *vty, const struct prefix *p,
                               struct bgp_path_info *path, int display,
                               afi_t afi, safi_t safi, bool use_json,
-                              json_object *json)
+                              json_object *json_paths)
 {
-       struct attr *attr;
+       struct attr *attr = path->attr;
        struct bgp_damp_info *bdi;
        char timebuf[BGP_UPTIME_LEN];
        int len;
+       json_object *json_path = NULL;
 
        if (!path->extra)
                return;
 
+       if (use_json)
+               json_path = json_object_new_object();
+
        bdi = path->extra->damp_info;
 
        /* short status lead text */
-       route_vty_short_status_out(vty, path, p, json);
+       route_vty_short_status_out(vty, path, p, json_path);
 
-       /* print prefix and mask */
        if (!use_json) {
                if (!display)
                        route_vty_out_route(path->net, p, vty, NULL, false);
                else
                        vty_out(vty, "%*s", 17, " ");
-       }
 
-       len = vty_out(vty, "%s", path->peer->host);
-       len = 16 - len;
-       if (len < 1) {
-               if (!use_json)
+               len = vty_out(vty, "%s", path->peer->host);
+               len = 16 - len;
+               if (len < 1)
                        vty_out(vty, "\n%*s", 33, " ");
-       } else {
-               if (use_json)
-                       json_object_int_add(json, "peerHost", len);
                else
                        vty_out(vty, "%*s", len, " ");
-       }
 
-       len = vty_out(vty, "%d", bdi->flap);
-       len = 5 - len;
-       if (len < 1) {
-               if (!use_json)
+               len = vty_out(vty, "%d", bdi->flap);
+               len = 5 - len;
+               if (len < 1)
                        vty_out(vty, " ");
-       } else {
-               if (use_json)
-                       json_object_int_add(json, "bdiFlap", len);
                else
                        vty_out(vty, "%*s", len, " ");
-       }
 
-       if (use_json)
-               peer_uptime(bdi->start_time, timebuf, BGP_UPTIME_LEN, use_json,
-                           json);
-       else
                vty_out(vty, "%s ", peer_uptime(bdi->start_time, timebuf,
                                                BGP_UPTIME_LEN, 0, NULL));
 
-       if (CHECK_FLAG(path->flags, BGP_PATH_DAMPED)
-           && !CHECK_FLAG(path->flags, BGP_PATH_HISTORY)) {
-               if (use_json)
-                       bgp_damp_reuse_time_vty(vty, path, timebuf,
-                                               BGP_UPTIME_LEN, afi, safi,
-                                               use_json, json);
-               else
+               if (CHECK_FLAG(path->flags, BGP_PATH_DAMPED)
+                   && !CHECK_FLAG(path->flags, BGP_PATH_HISTORY))
                        vty_out(vty, "%s ",
                                bgp_damp_reuse_time_vty(vty, path, timebuf,
                                                        BGP_UPTIME_LEN, afi,
-                                                       safi, use_json, json));
-       } else {
-               if (!use_json)
+                                                       safi, use_json, NULL));
+               else
                        vty_out(vty, "%*s ", 8, " ");
-       }
 
-       /* Print attribute */
-       attr = path->attr;
-
-       /* Print aspath */
-       if (attr->aspath) {
-               if (use_json)
-                       json_object_string_add(json, "asPath",
-                                              attr->aspath->str);
-               else
+               if (attr->aspath)
                        aspath_print_vty(vty, "%s", attr->aspath, " ");
-       }
 
-       /* Print origin */
-       if (use_json)
-               json_object_string_add(json, "origin",
-                                      bgp_origin_str[attr->origin]);
-       else
                vty_out(vty, "%s", bgp_origin_str[attr->origin]);
 
-       if (!use_json)
                vty_out(vty, "\n");
+       } else {
+               json_object_string_add(json_path, "peerHost", path->peer->host);
+               json_object_int_add(json_path, "bdiFlap", bdi->flap);
+
+               peer_uptime(bdi->start_time, timebuf, BGP_UPTIME_LEN, use_json,
+                           json_path);
+
+               if (CHECK_FLAG(path->flags, BGP_PATH_DAMPED)
+                   && !CHECK_FLAG(path->flags, BGP_PATH_HISTORY))
+                       bgp_damp_reuse_time_vty(vty, path, timebuf,
+                                               BGP_UPTIME_LEN, afi, safi,
+                                               use_json, json_path);
+
+               if (attr->aspath)
+                       json_object_string_add(json_path, "asPath",
+                                              attr->aspath->str);
+
+               json_object_string_add(json_path, "origin",
+                                      bgp_origin_str[attr->origin]);
+
+               json_object_array_add(json_paths, json_path);
+       }
 }
 
 static void route_vty_out_advertised_to(struct vty *vty, struct peer *peer,