]> git.proxmox.com Git - mirror_frr.git/blobdiff - zebra/zebra_rib.c
zebra: clean up zapi organization
[mirror_frr.git] / zebra / zebra_rib.c
index 72dbfb12fc2e4bada1fefab240aa87158060898b..67832f2d3fe54ba157f6c205ccb6d8dcf00f29cc 100644 (file)
@@ -42,7 +42,6 @@
 #include "zebra/rib.h"
 #include "zebra/rt.h"
 #include "zebra/zebra_ns.h"
-#include "zebra/zserv.h"
 #include "zebra/zebra_vrf.h"
 #include "zebra/redistribute.h"
 #include "zebra/zebra_routemap.h"
@@ -51,6 +50,7 @@
 #include "zebra/interface.h"
 #include "zebra/connected.h"
 #include "zebra/zebra_vxlan.h"
+#include "zebra/zapi_msg.h"
 
 DEFINE_HOOK(rib_update, (struct route_node * rn, const char *reason),
            (rn, reason))
@@ -137,11 +137,8 @@ uint8_t route_distance(int type)
        return distance;
 }
 
-int is_zebra_valid_kernel_table(u_int32_t table_id)
+int is_zebra_valid_kernel_table(uint32_t table_id)
 {
-       if ((table_id > ZEBRA_KERNEL_TABLE_MAX))
-               return 0;
-
 #ifdef linux
        if ((table_id == RT_TABLE_UNSPEC) || (table_id == RT_TABLE_LOCAL)
            || (table_id == RT_TABLE_COMPAT))
@@ -151,7 +148,7 @@ int is_zebra_valid_kernel_table(u_int32_t table_id)
        return 1;
 }
 
-int is_zebra_main_routing_table(u_int32_t table_id)
+int is_zebra_main_routing_table(uint32_t table_id)
 {
        if ((table_id == RT_TABLE_MAIN)
            || (table_id == zebrad.rtm_table_default))
@@ -162,7 +159,7 @@ int is_zebra_main_routing_table(u_int32_t table_id)
 int zebra_check_addr(struct prefix *p)
 {
        if (p->family == AF_INET) {
-               u_int32_t addr;
+               uint32_t addr;
 
                addr = p->u.prefix4.s_addr;
                addr = ntohl(addr);
@@ -685,7 +682,8 @@ struct route_entry *rib_match_ipv4_multicast(vrf_id_t vrf_id,
                char buf[BUFSIZ];
                inet_ntop(AF_INET, &addr, buf, BUFSIZ);
 
-               zlog_debug("%s: %s: found %s, using %s", __func__, buf,
+               zlog_debug("%s: %s: vrf: %u found %s, using %s",
+                          __func__, buf, vrf_id,
                           mre ? (ure ? "MRIB+URIB" : "MRIB")
                               : ure ? "URIB" : "nothing",
                           re == ure ? "URIB" : re == mre ? "MRIB" : "none");
@@ -1608,9 +1606,9 @@ static void rib_process(struct route_node *rn)
                                if (re != old_selected) {
                                        if (IS_ZEBRA_DEBUG_RIB)
                                                zlog_debug(
-                                                       "%s: %s: imported via import-table but denied "
+                                                       "%s: %u:%s: imported via import-table but denied "
                                                        "by the ip protocol table route-map",
-                                                       __func__, buf);
+                                                       __func__, vrf_id, buf);
                                        rib_unlink(rn, re);
                                } else
                                        SET_FLAG(re->status,
@@ -1738,7 +1736,7 @@ static void rib_process(struct route_node *rn)
  * picked from it and processed by rib_process(). Don't process more,
  * than one RN record; operate only in the specified sub-queue.
  */
-static unsigned int process_subq(struct list *subq, u_char qindex)
+static unsigned int process_subq(struct list *subq, uint8_t qindex)
 {
        struct listnode *lnode = listhead(subq);
        struct route_node *rnode;
@@ -1841,31 +1839,31 @@ static wq_item_status meta_queue_process(struct work_queue *dummy, void *data)
 /*
  * Map from rib types to queue type (priority) in meta queue
  */
-static const u_char meta_queue_map[ZEBRA_ROUTE_MAX] = {
-               [ZEBRA_ROUTE_SYSTEM] = 4,
-               [ZEBRA_ROUTE_KERNEL] = 0,
-               [ZEBRA_ROUTE_CONNECT] = 0,
-               [ZEBRA_ROUTE_STATIC] = 1,
-               [ZEBRA_ROUTE_RIP] = 2,
-               [ZEBRA_ROUTE_RIPNG] = 2,
-               [ZEBRA_ROUTE_OSPF] = 2,
-               [ZEBRA_ROUTE_OSPF6] = 2,
-               [ZEBRA_ROUTE_ISIS] = 2,
-               [ZEBRA_ROUTE_BGP] = 3,
-               [ZEBRA_ROUTE_PIM] = 4, // Shouldn't happen but for safety
-               [ZEBRA_ROUTE_EIGRP] = 2,
-               [ZEBRA_ROUTE_NHRP] = 2,
-               [ZEBRA_ROUTE_HSLS] = 4,
-               [ZEBRA_ROUTE_OLSR] = 4,
-               [ZEBRA_ROUTE_TABLE] = 1,
-               [ZEBRA_ROUTE_LDP] = 4,
-               [ZEBRA_ROUTE_VNC] = 3,
-               [ZEBRA_ROUTE_VNC_DIRECT] = 3,
-               [ZEBRA_ROUTE_VNC_DIRECT_RH] = 3,
-               [ZEBRA_ROUTE_BGP_DIRECT] = 3,
-               [ZEBRA_ROUTE_BGP_DIRECT_EXT] = 3,
-               [ZEBRA_ROUTE_BABEL] = 2,
-               [ZEBRA_ROUTE_ALL] = 4, // Shouldn't happen but for safety
+static const uint8_t meta_queue_map[ZEBRA_ROUTE_MAX] = {
+       [ZEBRA_ROUTE_SYSTEM] = 4,
+       [ZEBRA_ROUTE_KERNEL] = 0,
+       [ZEBRA_ROUTE_CONNECT] = 0,
+       [ZEBRA_ROUTE_STATIC] = 1,
+       [ZEBRA_ROUTE_RIP] = 2,
+       [ZEBRA_ROUTE_RIPNG] = 2,
+       [ZEBRA_ROUTE_OSPF] = 2,
+       [ZEBRA_ROUTE_OSPF6] = 2,
+       [ZEBRA_ROUTE_ISIS] = 2,
+       [ZEBRA_ROUTE_BGP] = 3,
+       [ZEBRA_ROUTE_PIM] = 4, // Shouldn't happen but for safety
+       [ZEBRA_ROUTE_EIGRP] = 2,
+       [ZEBRA_ROUTE_NHRP] = 2,
+       [ZEBRA_ROUTE_HSLS] = 4,
+       [ZEBRA_ROUTE_OLSR] = 4,
+       [ZEBRA_ROUTE_TABLE] = 1,
+       [ZEBRA_ROUTE_LDP] = 4,
+       [ZEBRA_ROUTE_VNC] = 3,
+       [ZEBRA_ROUTE_VNC_DIRECT] = 3,
+       [ZEBRA_ROUTE_VNC_DIRECT_RH] = 3,
+       [ZEBRA_ROUTE_BGP_DIRECT] = 3,
+       [ZEBRA_ROUTE_BGP_DIRECT_EXT] = 3,
+       [ZEBRA_ROUTE_BABEL] = 2,
+       [ZEBRA_ROUTE_ALL] = 4, // Shouldn't happen but for safety
 };
 
 /* Look into the RN and queue it into one or more priority queues,
@@ -1876,7 +1874,7 @@ static void rib_meta_queue_add(struct meta_queue *mq, struct route_node *rn)
        struct route_entry *re;
 
        RNODE_FOREACH_RE (rn, re) {
-               u_char qindex = meta_queue_map[re->type];
+               uint8_t qindex = meta_queue_map[re->type];
                struct zebra_vrf *zvrf;
 
                /* Invariant: at this point we always have rn->info set. */
@@ -2123,8 +2121,8 @@ void rib_unlink(struct route_node *rn, struct route_entry *re)
 
        /* free RE and nexthops */
        if (re->type == ZEBRA_ROUTE_STATIC)
-               zebra_deregister_rnh_static_nexthops(re->vrf_id, re->ng.nexthop,
-                                                    rn);
+               zebra_deregister_rnh_static_nexthops(re->ng.nexthop->vrf_id,
+                                                    re->ng.nexthop, rn);
        nexthops_free(re->ng.nexthop);
        XFREE(MTYPE_RE, re);
 }
@@ -2191,9 +2189,9 @@ void _route_entry_dump(const char *func, union prefixconstptr pp,
 
        for (ALL_NEXTHOPS(re->ng, nexthop)) {
                inet_ntop(p->family, &nexthop->gate, straddr, INET6_ADDRSTRLEN);
-               zlog_debug("%s: %s %s[%u] with flags %s%s%s", func,
+               zlog_debug("%s: %s %s[%u] vrf %u with flags %s%s%s", func,
                           (nexthop->rparent ? "  NH" : "NH"), straddr,
-                          nexthop->ifindex,
+                          nexthop->ifindex, nexthop->vrf_id,
                           (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE)
                                    ? "ACTIVE "
                                    : ""),
@@ -2221,7 +2219,8 @@ void rib_lookup_and_dump(struct prefix_ipv4 *p, vrf_id_t vrf_id)
        /* Lookup table.  */
        table = zebra_vrf_table(AFI_IP, SAFI_UNICAST, vrf_id);
        if (!table) {
-               zlog_err("%s: zebra_vrf_table() returned NULL", __func__);
+               zlog_err("%s:%u zebra_vrf_table() returned NULL",
+                        __func__, vrf_id);
                return;
        }
 
@@ -2230,7 +2229,7 @@ void rib_lookup_and_dump(struct prefix_ipv4 *p, vrf_id_t vrf_id)
 
        /* No route for this prefix. */
        if (!rn) {
-               zlog_debug("%s: lookup failed for %s", __func__,
+               zlog_debug("%s:%u lookup failed for %s", __func__, vrf_id,
                           prefix2str((struct prefix *)p, prefix_buf,
                                      sizeof(prefix_buf)));
                return;
@@ -2241,8 +2240,9 @@ void rib_lookup_and_dump(struct prefix_ipv4 *p, vrf_id_t vrf_id)
 
        /* let's go */
        RNODE_FOREACH_RE (rn, re) {
-               zlog_debug("%s: rn %p, re %p: %s, %s", __func__, (void *)rn,
-                          (void *)re,
+               zlog_debug("%s:%u rn %p, re %p: %s, %s",
+                          __func__, vrf_id,
+                          (void *)rn, (void *)re,
                           (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED)
                                    ? "removed"
                                    : "NOT removed"),
@@ -2266,7 +2266,8 @@ void rib_lookup_and_pushup(struct prefix_ipv4 *p, vrf_id_t vrf_id)
        rib_dest_t *dest;
 
        if (NULL == (table = zebra_vrf_table(AFI_IP, SAFI_UNICAST, vrf_id))) {
-               zlog_err("%s: zebra_vrf_table() returned NULL", __func__);
+               zlog_err("%s:%u zebra_vrf_table() returned NULL",
+                        __func__, vrf_id);
                return;
        }
 
@@ -2391,9 +2392,9 @@ int rib_add_multipath(afi_t afi, safi_t safi, struct prefix *p,
 }
 
 void rib_delete(afi_t afi, safi_t safi, vrf_id_t vrf_id, int type,
-               u_short instance, int flags, struct prefix *p,
+               unsigned short instance, int flags, struct prefix *p,
                struct prefix_ipv6 *src_p, const struct nexthop *nh,
-               u_int32_t table_id, u_int32_t metric, bool fromkernel,
+               uint32_t table_id, uint32_t metric, bool fromkernel,
                struct ethaddr *rmac)
 {
        struct route_table *table;
@@ -2490,8 +2491,7 @@ void rib_delete(afi_t afi, safi_t safi, vrf_id_t vrf_id, int type,
                 * If we can show that this code path is
                 * dead then we can remove it.
                 */
-               if (fib && type == ZEBRA_ROUTE_KERNEL
-                   && CHECK_FLAG(flags, ZEBRA_FLAG_SELFROUTE)) {
+               if (fib && CHECK_FLAG(flags, ZEBRA_FLAG_SELFROUTE)) {
                        if (IS_ZEBRA_DEBUG_RIB) {
                                rnode_debug(
                                        rn, vrf_id,
@@ -2581,10 +2581,11 @@ void rib_delete(afi_t afi, safi_t safi, vrf_id_t vrf_id, int type,
 }
 
 
-int rib_add(afi_t afi, safi_t safi, vrf_id_t vrf_id, int type, u_short instance,
-           int flags, struct prefix *p, struct prefix_ipv6 *src_p,
-           const struct nexthop *nh, u_int32_t table_id, u_int32_t metric,
-           u_int32_t mtu, uint8_t distance, route_tag_t tag)
+int rib_add(afi_t afi, safi_t safi, vrf_id_t vrf_id, int type,
+           unsigned short instance, int flags, struct prefix *p,
+           struct prefix_ipv6 *src_p, const struct nexthop *nh,
+           uint32_t table_id, uint32_t metric, uint32_t mtu, uint8_t distance,
+           route_tag_t tag)
 {
        struct route_entry *re;
        struct nexthop *nexthop;
@@ -2701,40 +2702,8 @@ void rib_update(vrf_id_t vrf_id, rib_update_event_t event)
                rib_update_table(table, event);
 }
 
-/* Remove all routes which comes from non main table.  */
-static void rib_weed_table(struct route_table *table)
-{
-       struct route_node *rn;
-       struct route_entry *re;
-       struct route_entry *next;
-
-       if (table)
-               for (rn = route_top(table); rn; rn = srcdest_route_next(rn))
-                       RNODE_FOREACH_RE_SAFE (rn, re, next) {
-                               if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED))
-                                       continue;
-
-                               if (re->table != zebrad.rtm_table_default
-                                   && re->table != RT_TABLE_MAIN)
-                                       rib_delnode(rn, re);
-                       }
-}
-
-/* Delete all routes from non main table. */
-void rib_weed_tables(void)
-{
-       struct vrf *vrf;
-       struct zebra_vrf *zvrf;
-
-       RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id)
-               if ((zvrf = vrf->info) != NULL) {
-                       rib_weed_table(zvrf->table[AFI_IP][SAFI_UNICAST]);
-                       rib_weed_table(zvrf->table[AFI_IP6][SAFI_UNICAST]);
-               }
-}
-
 /* Delete self installed routes after zebra is relaunched.  */
-static void rib_sweep_table(struct route_table *table)
+void rib_sweep_table(struct route_table *table)
 {
        struct route_node *rn;
        struct route_entry *re;
@@ -2795,11 +2764,13 @@ void rib_sweep_route(void)
                rib_sweep_table(zvrf->table[AFI_IP][SAFI_UNICAST]);
                rib_sweep_table(zvrf->table[AFI_IP6][SAFI_UNICAST]);
        }
+
+       zebra_ns_sweep_route();
 }
 
 /* Remove specific by protocol routes from 'table'. */
-static unsigned long rib_score_proto_table(u_char proto, u_short instance,
-                                          struct route_table *table)
+unsigned long rib_score_proto_table(uint8_t proto, unsigned short instance,
+                                   struct route_table *table)
 {
        struct route_node *rn;
        struct route_entry *re;
@@ -2821,7 +2792,7 @@ static unsigned long rib_score_proto_table(u_char proto, u_short instance,
 }
 
 /* Remove specific by protocol routes. */
-unsigned long rib_score_proto(u_char proto, u_short instance)
+unsigned long rib_score_proto(uint8_t proto, unsigned short instance)
 {
        struct vrf *vrf;
        struct zebra_vrf *zvrf;
@@ -2836,6 +2807,8 @@ unsigned long rib_score_proto(u_char proto, u_short instance)
                                         proto, instance,
                                         zvrf->table[AFI_IP6][SAFI_UNICAST]);
 
+       cnt += zebra_ns_score_proto(proto, instance);
+
        return cnt;
 }