]> git.proxmox.com Git - mirror_frr.git/blobdiff - zebra/zebra_rnh.c
Merge pull request #13395 from LabNConsulting/chopps/mgmtd-debug-flags
[mirror_frr.git] / zebra / zebra_rnh.c
index 7934a9d2066e34b7875413399eb78f430da85e15..3bbcd38d1cfe9479293c92bfbc6f3b5b206e67df 100644 (file)
@@ -1,21 +1,6 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
 /* Zebra next hop tracking code
  * Copyright (C) 2013 Cumulus Networks, Inc.
- *
- * This file is part of GNU Zebra.
- *
- * GNU Zebra is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License as published by the
- * Free Software Foundation; either version 2, or (at your option) any
- * later version.
- *
- * GNU Zebra is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; see the file COPYING; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
 #include <zebra.h>
@@ -28,7 +13,7 @@
 #include "log.h"
 #include "sockunion.h"
 #include "linklist.h"
-#include "thread.h"
+#include "frrevent.h"
 #include "workqueue.h"
 #include "prefix.h"
 #include "routemap.h"
@@ -62,7 +47,8 @@ static void free_state(vrf_id_t vrf_id, struct route_entry *re,
 static void copy_state(struct rnh *rnh, const struct route_entry *re,
                       struct route_node *rn);
 static bool compare_state(struct route_entry *r1, struct route_entry *r2);
-static void print_rnh(struct route_node *rn, struct vty *vty);
+static void print_rnh(struct route_node *rn, struct vty *vty,
+                     json_object *json);
 static int zebra_client_cleanup_rnh(struct zserv *client);
 
 void zebra_rnh_init(void)
@@ -340,7 +326,7 @@ void zebra_register_rnh_pseudowire(vrf_id_t vrf_id, struct zebra_pw *pw,
 
        *nht_exists = false;
 
-       zvrf = vrf_info_lookup(vrf_id);
+       zvrf = zebra_vrf_lookup_by_id(vrf_id);
        if (!zvrf)
                return;
 
@@ -803,7 +789,8 @@ void zebra_evaluate_rnh(struct zebra_vrf *zvrf, afi_t afi, int force,
 }
 
 void zebra_print_rnh_table(vrf_id_t vrfid, afi_t afi, safi_t safi,
-                          struct vty *vty, const struct prefix *p)
+                          struct vty *vty, const struct prefix *p,
+                          json_object *json)
 {
        struct route_table *table;
        struct route_node *rn;
@@ -820,7 +807,7 @@ void zebra_print_rnh_table(vrf_id_t vrfid, afi_t afi, safi_t safi,
                        continue;
 
                if (rn->info)
-                       print_rnh(rn, vty);
+                       print_rnh(rn, vty, json);
        }
 }
 
@@ -1268,10 +1255,192 @@ failure:
        return -1;
 }
 
-static void print_nh(struct nexthop *nexthop, struct vty *vty)
+
+/*
+ * Render a nexthop into a json object; the caller allocates and owns
+ * the json object memory.
+ */
+void show_nexthop_json_helper(json_object *json_nexthop,
+                             const struct nexthop *nexthop,
+                             const struct route_entry *re)
 {
-       char buf[BUFSIZ];
-       struct zebra_ns *zns = zebra_ns_lookup(nexthop->vrf_id);
+       json_object *json_labels = NULL;
+       json_object *json_backups = NULL;
+       json_object *json_seg6local = NULL;
+       json_object *json_seg6 = NULL;
+       int i;
+
+       json_object_int_add(json_nexthop, "flags", nexthop->flags);
+
+       if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_DUPLICATE))
+               json_object_boolean_true_add(json_nexthop, "duplicate");
+
+       if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB))
+               json_object_boolean_true_add(json_nexthop, "fib");
+
+       switch (nexthop->type) {
+       case NEXTHOP_TYPE_IPV4:
+       case NEXTHOP_TYPE_IPV4_IFINDEX:
+               json_object_string_addf(json_nexthop, "ip", "%pI4",
+                                       &nexthop->gate.ipv4);
+               json_object_string_add(json_nexthop, "afi", "ipv4");
+
+               if (nexthop->ifindex) {
+                       json_object_int_add(json_nexthop, "interfaceIndex",
+                                           nexthop->ifindex);
+                       json_object_string_add(json_nexthop, "interfaceName",
+                                              ifindex2ifname(nexthop->ifindex,
+                                                             nexthop->vrf_id));
+               }
+               break;
+       case NEXTHOP_TYPE_IPV6:
+       case NEXTHOP_TYPE_IPV6_IFINDEX:
+               json_object_string_addf(json_nexthop, "ip", "%pI6",
+                                       &nexthop->gate.ipv6);
+               json_object_string_add(json_nexthop, "afi", "ipv6");
+
+               if (nexthop->ifindex) {
+                       json_object_int_add(json_nexthop, "interfaceIndex",
+                                           nexthop->ifindex);
+                       json_object_string_add(json_nexthop, "interfaceName",
+                                              ifindex2ifname(nexthop->ifindex,
+                                                             nexthop->vrf_id));
+               }
+               break;
+
+       case NEXTHOP_TYPE_IFINDEX:
+               json_object_boolean_true_add(json_nexthop, "directlyConnected");
+               json_object_int_add(json_nexthop, "interfaceIndex",
+                                   nexthop->ifindex);
+               json_object_string_add(
+                       json_nexthop, "interfaceName",
+                       ifindex2ifname(nexthop->ifindex, nexthop->vrf_id));
+               break;
+       case NEXTHOP_TYPE_BLACKHOLE:
+               json_object_boolean_true_add(json_nexthop, "unreachable");
+               switch (nexthop->bh_type) {
+               case BLACKHOLE_REJECT:
+                       json_object_boolean_true_add(json_nexthop, "reject");
+                       break;
+               case BLACKHOLE_ADMINPROHIB:
+                       json_object_boolean_true_add(json_nexthop,
+                                                    "adminProhibited");
+                       break;
+               case BLACKHOLE_NULL:
+                       json_object_boolean_true_add(json_nexthop, "blackhole");
+                       break;
+               case BLACKHOLE_UNSPEC:
+                       break;
+               }
+               break;
+       }
+
+       /* This nexthop is a resolver for the parent nexthop.
+        * Set resolver flag for better clarity and delimiter
+        * in flat list of nexthops in json.
+        */
+       if (nexthop->rparent)
+               json_object_boolean_true_add(json_nexthop, "resolver");
+
+       if ((re == NULL || (nexthop->vrf_id != re->vrf_id)))
+               json_object_string_add(json_nexthop, "vrf",
+                                      vrf_id_to_name(nexthop->vrf_id));
+
+       if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_DUPLICATE))
+               json_object_boolean_true_add(json_nexthop, "duplicate");
+
+       if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE))
+               json_object_boolean_true_add(json_nexthop, "active");
+
+       if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ONLINK))
+               json_object_boolean_true_add(json_nexthop, "onLink");
+
+       if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_LINKDOWN))
+               json_object_boolean_true_add(json_nexthop, "linkDown");
+
+       if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
+               json_object_boolean_true_add(json_nexthop, "recursive");
+
+       if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_HAS_BACKUP)) {
+               json_backups = json_object_new_array();
+               for (i = 0; i < nexthop->backup_num; i++) {
+                       json_object_array_add(
+                               json_backups,
+                               json_object_new_int(nexthop->backup_idx[i]));
+               }
+
+               json_object_object_add(json_nexthop, "backupIndex",
+                                      json_backups);
+       }
+
+       switch (nexthop->type) {
+       case NEXTHOP_TYPE_IPV4:
+       case NEXTHOP_TYPE_IPV4_IFINDEX:
+               if (nexthop->src.ipv4.s_addr)
+                       json_object_string_addf(json_nexthop, "source", "%pI4",
+                                               &nexthop->src.ipv4);
+               break;
+       case NEXTHOP_TYPE_IPV6:
+       case NEXTHOP_TYPE_IPV6_IFINDEX:
+               if (!IPV6_ADDR_SAME(&nexthop->src.ipv6, &in6addr_any))
+                       json_object_string_addf(json_nexthop, "source", "%pI6",
+                                               &nexthop->src.ipv6);
+               break;
+       case NEXTHOP_TYPE_IFINDEX:
+       case NEXTHOP_TYPE_BLACKHOLE:
+               break;
+       }
+
+       if (nexthop->nh_label && nexthop->nh_label->num_labels) {
+               json_labels = json_object_new_array();
+
+               for (int label_index = 0;
+                    label_index < nexthop->nh_label->num_labels; label_index++)
+                       json_object_array_add(
+                               json_labels,
+                               json_object_new_int((
+                                       (nexthop->nh_label_type ==
+                                        ZEBRA_LSP_EVPN)
+                                               ? label2vni(
+                                                         &nexthop->nh_label->label
+                                                                  [label_index])
+                                               : nexthop->nh_label->label
+                                                         [label_index])));
+
+               json_object_object_add(json_nexthop, "labels", json_labels);
+       }
+
+       if (nexthop->weight)
+               json_object_int_add(json_nexthop, "weight", nexthop->weight);
+
+       if (nexthop->srte_color)
+               json_object_int_add(json_nexthop, "srteColor",
+                                   nexthop->srte_color);
+
+       if (nexthop->nh_srv6) {
+               json_seg6local = json_object_new_object();
+               json_object_string_add(
+                       json_seg6local, "action",
+                       seg6local_action2str(
+                               nexthop->nh_srv6->seg6local_action));
+               json_object_object_add(json_nexthop, "seg6local",
+                                      json_seg6local);
+
+               json_seg6 = json_object_new_object();
+               json_object_string_addf(json_seg6, "segs", "%pI6",
+                                       &nexthop->nh_srv6->seg6_segs);
+               json_object_object_add(json_nexthop, "seg6", json_seg6);
+       }
+}
+
+/*
+ * Helper for nexthop output, used in the 'show ip route' path
+ */
+void show_route_nexthop_helper(struct vty *vty, const struct route_entry *re,
+                              const struct nexthop *nexthop)
+{
+       char buf[MPLS_LABEL_STRLEN];
+       int i;
 
        switch (nexthop->type) {
        case NEXTHOP_TYPE_IPV4:
@@ -1279,62 +1448,216 @@ static void print_nh(struct nexthop *nexthop, struct vty *vty)
                vty_out(vty, " via %pI4", &nexthop->gate.ipv4);
                if (nexthop->ifindex)
                        vty_out(vty, ", %s",
-                               ifindex2ifname_per_ns(zns, nexthop->ifindex));
+                               ifindex2ifname(nexthop->ifindex,
+                                              nexthop->vrf_id));
                break;
        case NEXTHOP_TYPE_IPV6:
        case NEXTHOP_TYPE_IPV6_IFINDEX:
-               vty_out(vty, " %s",
-                       inet_ntop(AF_INET6, &nexthop->gate.ipv6, buf, BUFSIZ));
+               vty_out(vty, " via %s",
+                       inet_ntop(AF_INET6, &nexthop->gate.ipv6, buf,
+                                 sizeof(buf)));
                if (nexthop->ifindex)
-                       vty_out(vty, ", via %s",
-                               ifindex2ifname_per_ns(zns, nexthop->ifindex));
+                       vty_out(vty, ", %s",
+                               ifindex2ifname(nexthop->ifindex,
+                                              nexthop->vrf_id));
                break;
+
        case NEXTHOP_TYPE_IFINDEX:
                vty_out(vty, " is directly connected, %s",
-                       ifindex2ifname_per_ns(zns, nexthop->ifindex));
+                       ifindex2ifname(nexthop->ifindex, nexthop->vrf_id));
                break;
        case NEXTHOP_TYPE_BLACKHOLE:
-               vty_out(vty, " is directly connected, Null0");
+               vty_out(vty, " unreachable");
+               switch (nexthop->bh_type) {
+               case BLACKHOLE_REJECT:
+                       vty_out(vty, " (ICMP unreachable)");
+                       break;
+               case BLACKHOLE_ADMINPROHIB:
+                       vty_out(vty, " (ICMP admin-prohibited)");
+                       break;
+               case BLACKHOLE_NULL:
+                       vty_out(vty, " (blackhole)");
+                       break;
+               case BLACKHOLE_UNSPEC:
+                       break;
+               }
                break;
-       default:
+       }
+
+       if ((re == NULL || (nexthop->vrf_id != re->vrf_id)))
+               vty_out(vty, " (vrf %s)", vrf_id_to_name(nexthop->vrf_id));
+
+       if (!CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE))
+               vty_out(vty, " inactive");
+
+       if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ONLINK))
+               vty_out(vty, " onlink");
+
+       if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_LINKDOWN))
+               vty_out(vty, " linkdown");
+
+       if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
+               vty_out(vty, " (recursive)");
+
+       switch (nexthop->type) {
+       case NEXTHOP_TYPE_IPV4:
+       case NEXTHOP_TYPE_IPV4_IFINDEX:
+               if (nexthop->src.ipv4.s_addr) {
+                       vty_out(vty, ", src %pI4", &nexthop->src.ipv4);
+                       /* SR-TE information */
+                       if (nexthop->srte_color)
+                               vty_out(vty, ", SR-TE color %u",
+                                       nexthop->srte_color);
+               }
                break;
+       case NEXTHOP_TYPE_IPV6:
+       case NEXTHOP_TYPE_IPV6_IFINDEX:
+               if (!IPV6_ADDR_SAME(&nexthop->src.ipv6, &in6addr_any))
+                       vty_out(vty, ", src %pI6", &nexthop->src.ipv6);
+               break;
+       case NEXTHOP_TYPE_IFINDEX:
+       case NEXTHOP_TYPE_BLACKHOLE:
+               break;
+       }
+
+       /* Label information */
+       if (nexthop->nh_label && nexthop->nh_label->num_labels) {
+               vty_out(vty, ", label %s",
+                       mpls_label2str(nexthop->nh_label->num_labels,
+                                      nexthop->nh_label->label, buf,
+                                      sizeof(buf), nexthop->nh_label_type, 1));
+       }
+
+       if (nexthop->nh_srv6) {
+               seg6local_context2str(buf, sizeof(buf),
+                                     &nexthop->nh_srv6->seg6local_ctx,
+                                     nexthop->nh_srv6->seg6local_action);
+               vty_out(vty, ", seg6local %s %s",
+                       seg6local_action2str(
+                               nexthop->nh_srv6->seg6local_action),
+                       buf);
+               vty_out(vty, ", seg6 %pI6", &nexthop->nh_srv6->seg6_segs);
+       }
+
+       if (nexthop->weight)
+               vty_out(vty, ", weight %u", nexthop->weight);
+
+       if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_HAS_BACKUP)) {
+               vty_out(vty, ", backup %d", nexthop->backup_idx[0]);
+
+               for (i = 1; i < nexthop->backup_num; i++)
+                       vty_out(vty, ",%d", nexthop->backup_idx[i]);
        }
-       vty_out(vty, "\n");
 }
 
-static void print_rnh(struct route_node *rn, struct vty *vty)
+static void print_rnh(struct route_node *rn, struct vty *vty, json_object *json)
 {
        struct rnh *rnh;
        struct nexthop *nexthop;
        struct listnode *node;
        struct zserv *client;
        char buf[BUFSIZ];
+       json_object *json_nht = NULL;
+       json_object *json_client_array = NULL;
+       json_object *json_client = NULL;
+       json_object *json_nexthop_array = NULL;
+       json_object *json_nexthop = NULL;
 
        rnh = rn->info;
-       vty_out(vty, "%s%s\n",
-               inet_ntop(rn->p.family, &rn->p.u.prefix, buf, BUFSIZ),
-               CHECK_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED) ? "(Connected)"
-                                                           : "");
-       if (rnh->state) {
-               vty_out(vty, " resolved via %s\n",
-                       zebra_route_string(rnh->state->type));
-               for (nexthop = rnh->state->nhe->nhg.nexthop; nexthop;
-                    nexthop = nexthop->next)
-                       print_nh(nexthop, vty);
-       } else
-               vty_out(vty, " unresolved%s\n",
+
+       if (json) {
+               json_nht = json_object_new_object();
+               json_nexthop_array = json_object_new_array();
+               json_client_array = json_object_new_array();
+
+               json_object_object_add(
+                       json,
+                       inet_ntop(rn->p.family, &rn->p.u.prefix, buf, BUFSIZ),
+                       json_nht);
+               json_object_boolean_add(
+                       json_nht, "nhtConnected",
+                       CHECK_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED));
+               json_object_object_add(json_nht, "clientList",
+                                      json_client_array);
+               json_object_object_add(json_nht, "nexthops",
+                                      json_nexthop_array);
+       } else {
+               vty_out(vty, "%s%s\n",
+                       inet_ntop(rn->p.family, &rn->p.u.prefix, buf, BUFSIZ),
                        CHECK_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED)
                                ? "(Connected)"
                                : "");
+       }
+
+       if (rnh->state) {
+               if (json)
+                       json_object_string_add(
+                               json_nht, "resolvedProtocol",
+                               zebra_route_string(rnh->state->type));
+               else
+                       vty_out(vty, " resolved via %s\n",
+                               zebra_route_string(rnh->state->type));
+
+               for (nexthop = rnh->state->nhe->nhg.nexthop; nexthop;
+                    nexthop = nexthop->next) {
+                       if (json) {
+                               json_nexthop = json_object_new_object();
+                               json_object_array_add(json_nexthop_array,
+                                                     json_nexthop);
+                               show_nexthop_json_helper(json_nexthop, nexthop,
+                                                        NULL);
+                       } else {
+                               show_route_nexthop_helper(vty, NULL, nexthop);
+                               vty_out(vty, "\n");
+                       }
+               }
+       } else {
+               if (json)
+                       json_object_boolean_add(
+                               json_nht, "unresolved",
+                               CHECK_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED));
+               else
+                       vty_out(vty, " unresolved%s\n",
+                               CHECK_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED)
+                                       ? "(Connected)"
+                                       : "");
+       }
+
+       if (!json)
+               vty_out(vty, " Client list:");
+
+       for (ALL_LIST_ELEMENTS_RO(rnh->client_list, node, client)) {
+               if (json) {
+                       json_client = json_object_new_object();
+                       json_object_array_add(json_client_array, json_client);
+
+                       json_object_string_add(
+                               json_client, "protocol",
+                               zebra_route_string(client->proto));
+                       json_object_int_add(json_client, "socket",
+                                           client->sock);
+                       json_object_string_add(json_client, "protocolFiltered",
+                                              (rnh->filtered[client->proto]
+                                                       ? "(filtered)"
+                                                       : "none"));
+               } else {
+                       vty_out(vty, " %s(fd %d)%s",
+                               zebra_route_string(client->proto), client->sock,
+                               rnh->filtered[client->proto] ? "(filtered)"
+                                                            : "");
+               }
+       }
+
+       if (!list_isempty(rnh->zebra_pseudowire_list)) {
+               if (json)
+                       json_object_boolean_true_add(json_nht,
+                                                    "zebraPseudowires");
+               else
+                       vty_out(vty, " zebra[pseudowires]");
+       }
 
-       vty_out(vty, " Client list:");
-       for (ALL_LIST_ELEMENTS_RO(rnh->client_list, node, client))
-               vty_out(vty, " %s(fd %d)%s", zebra_route_string(client->proto),
-                       client->sock,
-                       rnh->filtered[client->proto] ? "(filtered)" : "");
-       if (!list_isempty(rnh->zebra_pseudowire_list))
-               vty_out(vty, " zebra[pseudowires]");
-       vty_out(vty, "\n");
+       if (!json)
+               vty_out(vty, "\n");
 }
 
 static int zebra_cleanup_rnh_client(vrf_id_t vrf_id, afi_t afi, safi_t safi,